Beispiel #1
0
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)
Beispiel #2
0
 def test_smoke2(self, pcv):
     prog2 = Conditional(BinaryOperation(Number(4), "=", Number(5)), [
         Print(Number(123)),
     ], [
         Number(456),
     ])
     assert pcv(prog2) == False
Beispiel #3
0
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)
Beispiel #4
0
 def test_conditional_unpure_false_empty(self, pcv):
     assert pcv(Conditional(Number(0), [], [Number(30), Print(Number(0)), Number(40)])) == False
Beispiel #5
0
 def test_conditional_unpure_true_empty(self, pcv):
     assert pcv(Conditional(Number(0), [Number(10), Print(Number(0)), Number(20)], [])) == False
Beispiel #6
0
 def test_function_unpure(self, pcv):
     assert pcv(Function([], [Number(10), Print(Number(5)), Number(20)])) == False
Beispiel #7
0
 def test_embedded(self, nrcv):
     prog = Print(FunctionDefinition("foo", Function([], [])))
     assert nrcv(prog) == set(["foo"])
Beispiel #8
0
 def visitPrint(self, printer):
     return Print(printer.expr.visit(self))
Beispiel #9
0
 def test_function_call_unpure_expr(self, pcv):
     cond = Conditional(Number(1), [Print(Number(1)), Reference("foo")])
     assert pcv(FunctionCall(cond, [])) == False
Beispiel #10
0
 def test_print(self, pcv):
     assert pcv(Print(Number(10))) == False
Beispiel #11
0
 def test_unary_operation_unpure(self, pcv):
     return pcv(UnaryOperation("-", Print(Number(10)))) == True
Beispiel #12
0

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)
Beispiel #13
0
    def visitPrint(self, prnt):
        expr_without_const = prnt.expr.accept(self)

        return Print(expr_without_const)
Beispiel #14
0
        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)
Beispiel #15
0
 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
Beispiel #16
0
 def test_conditional_unpure_cond_empty(self, pcv):
     assert pcv(Conditional(Print(Number(0)), [], [])) == False
Beispiel #17
0
 def test_good(self, nrcv_good):
     assert nrcv_good(Print(Number(10)))
Beispiel #18
0
 def test_function_call_unpure_args(self, pcv):
     assert pcv(FunctionCall(Reference("foo"), [Number(10), Print(Number(0)), Number(20)])) == False
Beispiel #19
0
 def test_good_with_bad_embed(self, nrcv_good):
     assert nrcv_good(Print(Conditional(Number(0), [], [Number(10)])))
Beispiel #20
0
 def test_binary_operation_unpure_rhs(self, pcv):
     return pcv(BinaryOperation(Number(10), "+", Print(Number(20)))) == False
Beispiel #21
0
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)