def get_constant_value(self, tree): if isinstance(tree, model.BinaryOperation): if self.check_num_num(tree): return tree.evaluate(model.Scope()) if self.check_num_ref(tree) or self.check_ref_num(tree) or \ self.check_ref_ref(tree): return model.Number(0) if isinstance(tree, model.UnaryOperation): if isinstance(tree.expr, model.Number): return tree.evaluate(model.Scope()) return tree
def test_scope(): f = model.Function([], [model.Number(0)]) ft = model.Number(42) t = model.Number(10) parent = model.Scope() parent["foo"] = f parent["bar"] = t assert parent["bar"] is t assert parent["foo"] is f scope = model.Scope(parent) assert scope["bar"] is t scope["bar"] = ft assert scope["bar"] is ft assert parent["bar"] is t
def test_reference(): name = "foo" value = model.Number(10) ref = model.Reference(name) scope = model.Scope() scope[name] = value assert ref.evaluate(scope) is value
def test_conditional(self): scope = model.Scope() scope["a"] = model.Number(10) cond = model.Conditional( model.BinaryOperation(model.Number(4), ">", model.Number(3)), [ model.Assign( "x", model.BinaryOperation(model.Number(3), "-", model.Number(5))), model.Assign( "y", model.BinaryOperation(model.Number(5), "*", model.Number(5))) ], [ model.Assign( "x", model.BinaryOperation(model.Reference("a"), "*", model.Number(0))) ]) folder = ConstantFolder() tree = folder.visit(cond) assert (isinstance(tree, model.Conditional) and tree.condition.value > 0 and isinstance(tree.if_true[0], model.Assign) and tree.if_true[0].value.value == -2 and isinstance(tree.if_true[1], model.Assign) and tree.if_true[1].value.value == 25 and len(tree.if_true) == 2 and len(tree.if_false) == 1 and isinstance(tree.if_false[0], model.Assign) and tree.if_false[0].value.value == 0)
def test_ref_num(self): scope = model.Scope() scope["a"] = model.Number(10) op = model.BinaryOperation(model.Reference("a"), "*", model.Number(0)) folder = ConstantFolder() tree = folder.visit(op) assert isinstance(tree, model.Number) and tree.value == 0
def test_function_definition(): name = "foo" scope = model.Scope() n = model.Number(10) f = model.Function([], [n]) d = model.FunctionDefinition(name, f) d.evaluate(scope) assert scope[name] is f
def test_binary_operation(): a = model.Number(10) b = model.Number(20) for op in [ "+", "-", "*", "/", "%", "==", "!=", "<", ">", "<=", ">=", "&&", "||" ]: model.BinaryOperation(a, op, b).evaluate(model.Scope())
def test_assign_var(self): scope = model.Scope() scope["a"] = model.Number(0) assign = model.Assign("x", model.Reference("a")) folder = ConstantFolder() tree = folder.visit(assign) assert (isinstance(tree, model.Assign) and tree.name == "x" and isinstance(tree.value, model.Reference) and tree.value.name == "a")
def test_number(): scope = model.Scope() n = model.Number(42) assert n.evaluate(scope) is n assert n == model.Number(42) assert n != model.Number(43) d = {n: 42, model.Number(10): 10} assert d[model.Number(42)] == 42 assert d[model.Number(10)] == 10
def test_function_call(): arg_name = "arg" f = model.Function([arg_name], [model.Reference(arg_name)]) scope = model.Scope() f_name = "foo" f_r = model.Reference(f_name) scope[f_name] = f n = model.Number(10) call = model.FunctionCall(f_r, [n]) assert call.evaluate(scope) is n
def test_bool_print(): strio = io.StringIO() old_stdout = sys.stdout sys.stdout = strio try: a = model.Number(10) b = model.Number(20) for op in ["==", "!=", "<", ">", "<=", ">=", "&&", "||"]: model.Print(model.BinaryOperation(a, op, b)).evaluate(model.Scope()) finally: sys.stdout = old_stdout map(int, strio.getvalue().split())
def test_function_with_body_args(self): parent = model.Scope() parent["f"] = model.Function(("a", "b"), [ model.Print( model.BinaryOperation(model.Reference("a"), "+", model.Reference("b"))) ]) parent["x"] = model.Number(10) scope = model.Scope(parent) scope["y"] = model.Number(20) definition = model.FunctionDefinition("f", parent["f"]) printer = PrettyPrinter() result = printer.visit(definition) assert result == "def f(a, b) {\n\tprint (a) + (b);\n};\n" definition.evaluate(scope) call = model.FunctionCall( model.Reference("f"), [model.Number(5), model.UnaryOperation("-", model.Number(3))]) result = printer.visit(call) assert result == "f(5, -(3));\n"
def test_function_definition(self): scope = model.Scope() function = model.Function(["a"], [ model.Print( model.BinaryOperation(model.Reference('a'), '-', model.Reference("a"))) ]) definition = model.FunctionDefinition("f", function) folder = ConstantFolder() tree = folder.visit(definition) assert (isinstance(tree, model.FunctionDefinition) and tree.name == "f" and len(tree.function.args) == 1 and len(tree.function.body) == 1 and isinstance(tree.function.body[0], model.Print) and isinstance(tree.function.body[0].expr, model.Number) and tree.function.body[0].expr.value == 0)
def test_mixed_conditional(self): scope = model.Scope() scope["a"] = model.Number(-10) scope["b"] = model.Number(5) scope["c"] = model.Number(10) """ if not (a == c) and b < c: print(a) """ conditional = model.Conditional( model.BinaryOperation( model.UnaryOperation( "!", model.BinaryOperation(model.Reference("a"), "==", model.Reference("c"))), "&&", model.BinaryOperation(model.Reference("b"), "<", model.Number(42))), [model.Assign("res", model.Reference("a"))]) printer = PrettyPrinter() result = printer.visit(conditional) assert (result == "if ((!((a) == (c))) && ((b) < (42))) {\n\tres =" + " a;\n};\n")
def test_function_call(self): scope = model.Scope() scope["x"] = model.Number(10) function = model.Function(["a", "b", "c"], []) model.FunctionDefinition("f", function).evaluate(scope) call = model.FunctionCall(model.Reference("f"), [ model.BinaryOperation(model.Number(2), "*", model.Number(3)), model.BinaryOperation(model.Number(0), "*", model.Reference("x")), model.Reference("x") ]) folder = ConstantFolder() tree = folder.visit(call) assert (isinstance(tree, model.FunctionCall) and isinstance(tree.fun_expr, model.Reference) and tree.fun_expr.name == "f" and len(tree.args) == 3 and isinstance(tree.args[0], model.Number) and tree.args[0].value == 6 and isinstance(tree.args[1], model.Number) and tree.args[1].value == 0 and isinstance(tree.args[2], model.Reference) and tree.args[2].name == "x")
def test_conditional(): scope = model.Scope() true = model.Number(1) false = model.Number(0) f = model.FunctionCall(model.Function([], [true]), []) cond = model.Conditional(true, None, None) cond.evaluate(scope) cond = model.Conditional(false, None, None) cond.evaluate(scope) cond = model.Conditional(true, [], None) cond.evaluate(scope) cond = model.Conditional(true, None, []) cond.evaluate(scope) cond = model.Conditional(false, [], None) cond.evaluate(scope) cond = model.Conditional(false, None, []) cond.evaluate(scope) cond = model.Conditional(true, [], []) cond.evaluate(scope) cond = model.Conditional(true, [], []) cond.evaluate(scope) cond = model.Conditional(true, [true], None) assert cond.evaluate(scope) is true cond = model.Conditional(false, None, [false]) assert cond.evaluate(scope) is false cond = model.Conditional(true, [f], None) assert cond.evaluate(scope) is true cond = model.Conditional(false, None, [f]) assert cond.evaluate(scope) is true # If one of the following assertions fail, it means that Conditional has # evaluated wrong branch. cond = model.Conditional(false, [true], None) assert cond.evaluate(scope) is not true cond = model.Conditional(false, [true], []) assert cond.evaluate(scope) is not true cond = model.Conditional(true, None, [false]) assert cond.evaluate(scope) is not false cond = model.Conditional(true, [], [false]) assert cond.evaluate(scope) is not false
def test_unary_operation(): a = model.Number(10) for op in ["-", "!"]: model.UnaryOperation(op, a).evaluate(model.Scope())
def __init__(self): self.scope = m.Scope()
def test_number(): scope = model.Scope() n = model.Number(42) assert n.evaluate(scope) is n
def test_function(): f = model.Function([], []) assert f.evaluate(model.Scope()) is f
def test_function(): n = model.Number(0) f = model.Function([], [n]) assert f.evaluate(model.Scope()) is n