def test_div(self): stack = [] tokens = self._lexer.lex('10/5') self.assertEqual(3, len(tokens)) stack.append(tokens[0]) stack.append(tokens[2]) binop = get_op(tokens[1]) answer = binop.eval(stack, op=None) self.assertEqual(2, answer.token) tokens = self._lexer.lex('12/5') self.assertEqual(3, len(tokens)) stack.append(tokens[0]) stack.append(tokens[2]) binop = get_op(tokens[1]) answer = binop.eval(stack, op=None) self.assertEqual(2.4, answer.token)
def test_string_concat(self): stack = [] tokens = self._lexer.lex('"A " + "B"') self.assertEqual(3, len(tokens)) stack.append(tokens[0]) stack.append(tokens[2]) binop = get_op(tokens[1]) answer = binop.eval(stack, op=None) self.assertEqual("A B", answer.token)
def test_exp(self): stack = [] tokens = self._lexer.lex('2^3') self.assertEqual(3, len(tokens)) stack.append(tokens[0]) stack.append(tokens[2]) binop = get_op(tokens[1]) answer = binop.eval(stack, op=None) self.assertEqual(8, answer.token)
def test_spaces(self): stack = [] tokens = self._lexer.lex(' 10 + 7 ') self.assertEqual(3, len(tokens)) stack.append(tokens[0]) stack.append(tokens[2]) binop = get_op(lexer_token("+", "op")) answer = binop.eval(stack, op=None) self.assertEqual(17, answer.token)
def test_comma(self): stack = [] tokens = self._lexer.lex('1,2') self.assertEqual(3, len(tokens)) stack.append(tokens[0]) stack.append(tokens[2]) binop = get_op(tokens[1]) self.assertIsInstance(binop, BinOpComma) answer = binop.eval(stack, op=None) self.assertEqual([1, 2], answer.token)
def test_minus(self): stack = [] tokens = self._lexer.lex('10-7') self.assertEqual(3, len(tokens)) stack.append(tokens[0]) stack.append(tokens[2]) binop = get_op(lexer_token("-", "op")) answer = binop.eval( stack, op=None) # Op is not needed for this test. Only used for DEF FNx self.assertEqual(3, answer.token)
def one_op(self, op_stack, data_stack): """ Perform one operation. :param op_stack: The operation stack, for example '*' :param data_stack: The operand stack. :return: None, it pushes the result back onto the data stack. """ from basic_operators import get_op, get_precedence current_op = op_stack.pop() eval_class = get_op(current_op) result = eval_class.eval(data_stack, op=current_op) # Some operators, like parens, don't return a result if result is not None: data_stack.append(result)