def test_cond_node(self):
     result = CondNode([
         (BinaryFunctionNode(operator.eq, NumberNode(4),
                             NumberNode(5)), NumberNode(9)),
         (BinaryFunctionNode(operator.eq, NumberNode(7523),
                             NumberNode(123)), NumberNode(5)),
         (BinaryFunctionNode(operator.eq, NumberNode(7),
                             NumberNode(7)), NumberNode(34))
     ]).evaluate()
     expected = 34
     self.assertEqual(result, expected)
 def test_evaluate_cond_basic(self):
     exp = [
         CondNode([(BinaryFunctionNode(operator.eq, NumberNode(123),
                                       NumberNode(3)), NumberNode(23)),
                   (BinaryFunctionNode(operator.eq, NumberNode(246),
                                       NumberNode(542)), NumberNode(55)),
                   (BinaryFunctionNode(operator.eq, NumberNode(67),
                                       NumberNode(67)), NumberNode(1))])
     ]
     result = evaluate(exp)
     expected = [1]
     self.assertEqual(result, expected)
 def test_evaluate_math_multiply_basic(self):
     exp = [
         BinaryFunctionNode(operator.mul, NumberNode(10), NumberNode(10))
     ]
     result = evaluate(exp)
     expected = [100]
     self.assertEqual(result, expected)
 def test_evaluate_math_subtract_basic(self):
     exp = [
         BinaryFunctionNode(operator.sub, NumberNode(48), NumberNode(22))
     ]
     result = evaluate(exp)
     expected = [26]
     self.assertEqual(result, expected)
 def test_evaluate_math_add_basic(self):
     exp = [
         BinaryFunctionNode(operator.add, NumberNode(123), NumberNode(41))
     ]
     result = evaluate(exp)
     expected = [164]
     self.assertEqual(result, expected)
 def test_evaluate_math_divide_basic(self):
     exp = [
         BinaryFunctionNode(operator.truediv, NumberNode(225),
                            NumberNode(5))
     ]
     result = evaluate(exp)
     expected = [45]
     self.assertEqual(result, expected)
Exemple #7
0
def parse_operator(tokens):
    curr = tokens.pop(0)

    if curr.type == TokenType.PLUS:
        exp1 = parse_expression(tokens)
        exp2 = parse_expression(tokens)

        return BinaryFunctionNode(operator.add, exp1, exp2)
    elif curr.type == TokenType.MINUS:
        exp1 = parse_expression(tokens)
        exp2 = parse_expression(tokens)

        return BinaryFunctionNode(operator.sub, exp1, exp2)
    elif curr.type == TokenType.MULTIPLY:
        exp1 = parse_expression(tokens)
        exp2 = parse_expression(tokens)

        return BinaryFunctionNode(operator.mul, exp1, exp2)
    elif curr.type == TokenType.DIVIDE:
        exp1 = parse_expression(tokens)
        exp2 = parse_expression(tokens)

        return BinaryFunctionNode(operator.truediv, exp1, exp2)
    elif curr.type == TokenType.DEFINE:
        name = tokens.pop(0)
        exp = parse_expression(tokens)

        return DefineNode(name.value, exp)
    elif curr.type == TokenType.AND:
        exp1 = parse_expression(tokens)
        exp2 = parse_expression(tokens)

        return BinaryFunctionNode(operator.and_, exp1, exp2)
    elif curr.type == TokenType.OR:
        exp1 = parse_expression(tokens)
        exp2 = parse_expression(tokens)

        return BinaryFunctionNode(operator.or_, exp1, exp2)
    elif curr.type == TokenType.NOT:
        return UnaryFunctionNode(operator.not_, parse_expression(tokens))
    elif curr.type == TokenType.EQ:
        exp1 = parse_expression(tokens)
        exp2 = parse_expression(tokens)

        return BinaryFunctionNode(operator.eq, exp1, exp2)
    elif curr.type == TokenType.PRINT:
        exp = parse_operator(tokens)

        return PrintNode(exp)
    elif curr.type == TokenType.COND:
        cond_cases = parse_cond_cases(tokens)

        return CondNode(cond_cases)
 def test_binary_function_node(self):
     result = BinaryFunctionNode(operator.add, NumberNode(4),
                                 NumberNode(7)).evaluate()
     expected = 11
     self.assertEqual(result, expected)