def parse(self, tokens, wrap=True): self.tokens = [token for token in tokens] self.position = 0 program = self.parse_block(False) if wrap: return model.FunctionCall(model.Function([], program), []) return program
def test_function_call(self): reference = model.Reference("foo") call = model.FunctionCall( reference, [model.Number(1), model.Number(2), model.Number(3)]) printer = PrettyPrinter() result = printer.visit(call) assert result == "foo(1, 2, 3);\n"
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 parse_unary(self): token = self.peek_token() if self.is_unary(token.value): self.drop_tokens() return model.UnaryOperation(token.value, self.parse_unary()) primary = self.parse_primary() while self.peek_token().value == '(': parameters = self.parse_parameters() primary = model.FunctionCall(primary, parameters) return primary
def mytest(): printer = PrettyPrinter() cond2 = model.Conditional(model.Number(30), [model.Number(10)], [model.Number(20)]) cond = model.Conditional( model.Number(42), [model.Number(1), cond2, model.Number(3)], [model.Number(4), model.Number(5)]) cond3 = model.Conditional(model.Number(228), [], []) func2 = model.Function(('p111', 'p222'), [ model.Reference("x"), model.Print(model.Reference("p111")), model.Read("myVar") ]) fd2 = model.FunctionDefinition("anotherPrettyFunc", func2) func = model.Function( (), [model.UnaryOperation("-", model.Number(100000000)), fd2, cond, cond3]) fd = model.FunctionDefinition("myPrettyFunc", func) printer.visit(fd) asd = model.BinaryOperation( model.BinaryOperation(model.Number(123), "+", model.Reference("xx")), "*", model.UnaryOperation( "-", model.BinaryOperation(model.Reference("xx"), "+", model.Reference("yy")))) printer.visit(asd) asd2 = model.BinaryOperation( model.BinaryOperation(model.Number(123), "/", model.Reference("xx")), "/", model.BinaryOperation( model.BinaryOperation(model.Reference("xx"), "/", model.Reference("yy")), "/", model.BinaryOperation(model.Number(123), "/", model.Reference("yy")))) printer.visit(asd2) printer.visit(model.FunctionCall(model.Reference("func"), [asd, asd2])) cond4 = model.Conditional(model.Number(228), []) printer.visit(cond4)
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_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_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 main(): v = PrettyPrinter() v.visit(m.Print(m.Number(10))) v.visit(m.UnaryOperation('-', m.Number(10))) v.visit( m.BinaryOperation( m.BinaryOperation(m.Reference('foo'), '+', m.Number(7)), '*', m.Number(2))) v.visit(m.Read("read")) v.visit( m.Conditional( m.Number(5), [m.BinaryOperation(m.Reference('var'), '-', m.Number(-5))])) v.visit( m.FunctionDefinition( 'summer', m.Function(['a', 'b'], [ m.Print( m.BinaryOperation(m.Reference('a'), '+', m.Reference('b'))), m.BinaryOperation(m.Reference('a'), '+', m.Reference('b')) ]))) v.visit( m.FunctionCall( m.Reference('summer'), [m.Number(1), m.BinaryOperation(m.Number(2), '+', m.Number(3))])) v.visit( m.Print( m.Conditional(m.BinaryOperation(m.Number(4), '-', m.Number(4)), [], [ m.BinaryOperation(m.Number(9), '/', m.Number(3)), m.Reference('var') ]))) v.visit( m.FunctionDefinition( 'abs', m.Function(['a', 'b'], [ m.Conditional( m.BinaryOperation( m.BinaryOperation(m.Reference('a'), '-', m.Reference('b')), '>', m.Number(0)), [ m.Print( m.BinaryOperation(m.Reference('a'), '-', m.Reference('b'))) ], [ m.Print( m.BinaryOperation(m.Reference('b'), '-', m.Reference('a'))) ]) ]))) v.visit( m.FunctionCall( m.Reference('abs'), [m.Number(23), m.UnaryOperation('-', m.Number(-30))])) v.visit(m.FunctionDefinition('fu', m.Function([], []))) v.visit(m.FunctionCall(m.Reference('fu'), [])) v.visit(m.Print(m.BinaryOperation(m.Number(5), '&&', m.Number(6)))) v.visit(m.Print(m.BinaryOperation(m.Number(3), '<', m.Number(5)))) v.visit(m.Print(m.Conditional(m.Number(3), None, None))) v.visit(m.Print(m.BinaryOperation(m.Number(1), '||', m.Number(4))))
def visit_function_call(self, func_call): return m.FunctionCall(func_call.fun_expr.accept(self), self.optimize_list(func_call.args))
def visit_function_call(self, func_call): args = self.process_list(func_call.args) fun_expr = func_call.fun_expr.visit(self) return model.FunctionCall(fun_expr, args)