コード例 #1
0
    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
コード例 #2
0
ファイル: run_smoke_tests.py プロジェクト: krinkinmu/yat
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
コード例 #3
0
ファイル: run_smoke_tests.py プロジェクト: yeputons/yat
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
コード例 #4
0
 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)
コード例 #5
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
コード例 #6
0
ファイル: run_smoke_tests.py プロジェクト: yeputons/yat
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
コード例 #7
0
ファイル: run_smoke_tests.py プロジェクト: krinkinmu/yat
def test_binary_operation():
    a = model.Number(10)
    b = model.Number(20)
    for op in [
            "+", "-", "*", "/", "%", "==", "!=", "<", ">", "<=", ">=", "&&",
            "||"
    ]:
        model.BinaryOperation(a, op, b).evaluate(model.Scope())
コード例 #8
0
 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")
コード例 #9
0
ファイル: run_smoke_tests.py プロジェクト: yeputons/yat
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
コード例 #10
0
ファイル: run_smoke_tests.py プロジェクト: yeputons/yat
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
コード例 #11
0
ファイル: run_smoke_tests.py プロジェクト: yeputons/yat
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())
コード例 #12
0
    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"
コード例 #13
0
 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)
コード例 #14
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")
コード例 #15
0
    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")
コード例 #16
0
ファイル: run_smoke_tests.py プロジェクト: yeputons/yat
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
コード例 #17
0
ファイル: run_smoke_tests.py プロジェクト: yeputons/yat
def test_unary_operation():
    a = model.Number(10)
    for op in ["-", "!"]:
        model.UnaryOperation(op, a).evaluate(model.Scope())
コード例 #18
0
ファイル: folder.py プロジェクト: vladmosin/paradigms
 def __init__(self):
     self.scope = m.Scope()
コード例 #19
0
ファイル: run_smoke_tests.py プロジェクト: krinkinmu/yat
def test_number():
    scope = model.Scope()
    n = model.Number(42)
    assert n.evaluate(scope) is n
コード例 #20
0
ファイル: run_smoke_tests.py プロジェクト: yeputons/yat
def test_function():
    f = model.Function([], [])
    assert f.evaluate(model.Scope()) is f
コード例 #21
0
ファイル: run_smoke_tests.py プロジェクト: krinkinmu/yat
def test_function():
    n = model.Number(0)
    f = model.Function([], [n])
    assert f.evaluate(model.Scope()) is n