def my_tests(): reader = Print(Read('n')) print_binary = Print( BinaryOperation( Reference('x'), '/', UnaryOperation('!', BinaryOperation(Reference('y'), '%', Number(2))))) conditional = Conditional( BinaryOperation(BinaryOperation(Reference('x'), '*', Number(2)), '<', UnaryOperation('-', Reference('y'))), [ print_binary, UnaryOperation('-', UnaryOperation('!', Reference('s'))) ], []) definition_max3 = FunctionDefinition( 'max3', Function(["a", "b", "c"], [ Conditional(BinaryOperation(Reference("a"), ">", Reference("b")), [ Conditional( BinaryOperation(Reference("a"), ">=", Reference("c")), [Reference("a")], [Reference("c")]) ], [ Conditional( BinaryOperation(Reference("b"), "<", Reference("c")), [Reference("c")], [Reference("b")]) ]) ])) fun_call = Print(FunctionCall(definition_max3, [Number(1), Number(2)])) definition_log2 = FunctionDefinition( "log2", Function(["k", "n", "step"], [ Conditional( BinaryOperation( BinaryOperation(Reference("k"), "*", Number(2)), "<=", Reference("n")), [ FunctionCall(Reference("log_2_def"), [ BinaryOperation(Reference("k"), "*", Number(2)), Reference("n"), BinaryOperation(Reference("step"), "+", Number(1)) ]) ], [Reference("step")]) ])) empty_func = FunctionDefinition("emptyFunc", Function([], [])) printer = PrettyPrinter() printer.visit(reader) printer.visit(conditional) printer.visit(print_binary) printer.visit(definition_max3) printer.visit(fun_call) printer.visit(definition_log2) printer.visit(empty_func)
def test_smoke2(self, pcv): prog2 = Conditional(BinaryOperation(Number(4), "=", Number(5)), [ Print(Number(123)), ], [ Number(456), ]) assert pcv(prog2) == False
def test(): f1 = Conditional(FunctionCall(Reference('x'), [Number(22)]), [Conditional(BinaryOperation(Number(0), '-', Number(6)), [],[Conditional(Number(0), [Conditional(UnaryOperation('-', Number(20)), [],[FunctionDefinition('foobar', Function(['ab', 'cd'], [ Print(BinaryOperation(UnaryOperation('-', Number(120)), '*', BinaryOperation(UnaryOperation('-', Number(20)), '+', Reference('z')))), Read('x') ]))])],[])])],[Conditional(Number(0), [Conditional(Number(0), [],[Read('xxx')])],[])]) f = FunctionDefinition('foo', Function(['a', 'b'], [ FunctionDefinition('bar', Function(['c', 'd'], [ Read('c') ])), Conditional(Number(6), [Conditional(Number(5), [Read('x')])],[f1]) ])) pr = PrettyPrinter() pr.visit(f)
def test_conditional_unpure_false_empty(self, pcv): assert pcv(Conditional(Number(0), [], [Number(30), Print(Number(0)), Number(40)])) == False
def test_conditional_unpure_true_empty(self, pcv): assert pcv(Conditional(Number(0), [Number(10), Print(Number(0)), Number(20)], [])) == False
def test_function_unpure(self, pcv): assert pcv(Function([], [Number(10), Print(Number(5)), Number(20)])) == False
def test_embedded(self, nrcv): prog = Print(FunctionDefinition("foo", Function([], []))) assert nrcv(prog) == set(["foo"])
def visitPrint(self, printer): return Print(printer.expr.visit(self))
def test_function_call_unpure_expr(self, pcv): cond = Conditional(Number(1), [Print(Number(1)), Reference("foo")]) assert pcv(FunctionCall(cond, [])) == False
def test_print(self, pcv): assert pcv(Print(Number(10))) == False
def test_unary_operation_unpure(self, pcv): return pcv(UnaryOperation("-", Print(Number(10)))) == True
if __name__ == '__main__': printer = PrettyPrinter() folder = ConstantFolder() function = Function(['a', 'b'], [Conditional(Number(42), [Number(42)], None)]) definition = FunctionDefinition('x', function) conditional = Conditional( BinaryOperation(Reference('a'), '-', BinaryOperation(Reference('a'), '-', Reference('a'))), [ Conditional( BinaryOperation( BinaryOperation(Number(42), '+', Reference('a')), '*', Number(0)), [], [Conditional(Number(42), [definition], [])]) ], []) printer.visit(conditional) conditional = folder.visit(conditional) prnt = Print( BinaryOperation( Number(1), '+', BinaryOperation( Number(2), '+', BinaryOperation(Reference('a'), '-', Reference('a'))))) printer.visit(prnt) prnt = folder.visit(prnt) printer.visit(conditional) printer.visit(prnt)
def visitPrint(self, prnt): expr_without_const = prnt.expr.accept(self) return Print(expr_without_const)
self.visit(uoper.expr, False) print(')', end='') def visitReference(self, refer): print(refer.name, end='') def visitPrint(self, prnt): print('print', end=' ') self.visit(prnt.expr, False) def visitRead(self, read): print('read ' + read.name, end='') if __name__ == '__main__': printer = PrettyPrinter() function = Function([], []) definition = FunctionDefinition('x', function) conditional = Conditional(BinaryOperation(Number(42), '-', Number(42)), [ Conditional( BinaryOperation(BinaryOperation(Number(42), '+', Reference('a')), '*', Number(0)), [], [Conditional(Number(42), [definition], [])]) ], []) printer.visit(definition) printer.visit(conditional) printer.visit(Read('x')) printer.visit(Print(BinaryOperation(Number(2), '+', Reference('a')))) call = FunctionCall(Reference('foo'), []) printer.visit(call)
def test_conditional_unpure_cond_non_empty(self, pcv): assert pcv(Conditional(Print(Number(0)), [Number(10), Number(20)], [Number(30), Number(0), Number(40)])) == False
def test_conditional_unpure_cond_empty(self, pcv): assert pcv(Conditional(Print(Number(0)), [], [])) == False
def test_good(self, nrcv_good): assert nrcv_good(Print(Number(10)))
def test_function_call_unpure_args(self, pcv): assert pcv(FunctionCall(Reference("foo"), [Number(10), Print(Number(0)), Number(20)])) == False
def test_good_with_bad_embed(self, nrcv_good): assert nrcv_good(Print(Conditional(Number(0), [], [Number(10)])))
def test_binary_operation_unpure_rhs(self, pcv): return pcv(BinaryOperation(Number(10), "+", Print(Number(20)))) == False
def my_tests(): print_binary = Print( BinaryOperation( Number(56), '/', UnaryOperation('!', BinaryOperation(Number(8), '%', Number(2))))) conditional = Conditional( BinaryOperation(BinaryOperation(Number(4), '*', Number(2)), '<', UnaryOperation('-', Number(-5))), [print_binary, print_binary], []) definition_max3 = FunctionDefinition( 'max3', Function(["a", "b", "c"], [ Conditional(BinaryOperation(Reference("a"), "-", Reference("a")), [ Conditional(BinaryOperation(Reference("a"), "*", Number(0)), [], [Reference("c")]) ], [ Conditional(BinaryOperation(Number(0), "*", Reference("c")), [Reference("c")]) ]) ])) fun_call = FunctionCall( definition_max3, [Number(1), BinaryOperation(Number(1), '*', Number(7)), Number(3)]) definition_log2 = FunctionDefinition( "log2", Function(["k", "n", "step"], [ Conditional( BinaryOperation( BinaryOperation(Reference("k"), "-", Reference("k")), "<=", Reference("n")), [ FunctionCall(Reference("log_2_def"), [ BinaryOperation(Reference("k"), "*", Number(0)), Reference("n"), BinaryOperation(Number(4), "+", Number(5)) ]) ], [Reference("step")]) ])) folder = ConstantFolder() my_printer = printer.PrettyPrinter() conditional_c = folder.visit(conditional) definition_max3_c = folder.visit(definition_max3) fun_call_c = folder.visit(fun_call) definition_log2_c = folder.visit(definition_log2) if conditional_c == conditional: print('!') if definition_max3_c == definition_max3: print('!') if definition_log2_c == definition_log2: print('!') if fun_call_c == fun_call: print('!') my_printer.visit(conditional_c) my_printer.visit(conditional) my_printer.visit(definition_max3_c) my_printer.visit(definition_max3) my_printer.visit(fun_call_c) my_printer.visit(fun_call) my_printer.visit(definition_log2_c)