示例#1
0
 def test_function_call_args(self):
     scope = codegen.Scope()
     scope.reserve_name("aFunction",
                        type=types.Function(dtypes.Number, dtypes.String))
     func_call = codegen.FunctionCall(scope.variables["aFunction"],
                                      [codegen.Number(123)])
     self.assertCodeEqual(func_call.as_source_code(), "aFunction 123")
示例#2
0
 def test_if(self):
     scope = codegen.Scope()
     if_expr = codegen.If(parent_scope=scope)
     if_expr.condition = codegen.Equals(codegen.Number(1),
                                        codegen.Number(2))
     if_expr.true_branch.value = codegen.Number(3)
     if_expr.false_branch.value = codegen.Number(4)
     if_expr = codegen.simplify(if_expr)
     self.assertCodeEqual(
         if_expr.as_source_code(),
         """
         if (1 == 2) then
             3
         else
             4
     """,
     )
示例#3
0
 def test_function_call_nested(self):
     scope = codegen.Scope()
     scope.reserve_name("aFunction",
                        type=types.Function(dtypes.Number, dtypes.String))
     scope.reserve_name("aFunction2",
                        type=types.Function(dtypes.String, dtypes.Number))
     func_call_1 = scope.variables["aFunction"].apply(codegen.Number(123))
     func_call_2 = scope.variables["aFunction2"].apply(func_call_1)
     self.assertCodeEqual(func_call_2.as_source_code(),
                          "aFunction2 (aFunction 123)")
示例#4
0
 def test_let_two_assignments(self):
     module = codegen.Module()
     func = codegen.Function("myfunc", parent_scope=module)
     let = func.body
     x = let.add_assignment("x", codegen.Number(1))
     y = let.add_assignment("y", codegen.Number(2))
     let.value = codegen.Add(x, y)
     func = codegen.simplify(func)
     # TODO SOMEDAY - remove unnecessary parenthesis in final expression
     self.assertCodeEqual(
         func.as_source_code(),
         """
         myfunc =
             let
                 x = 1
                 y = 2
             in
                 (x + y)
         """,
     )
示例#5
0
    def test_multiple_indent(self):
        scope = codegen.Scope()
        scope.reserve_name(
            "aFunction",
            type=types.Function.for_multiple_inputs(
                [dtypes.Number, dtypes.Number], dtypes.String),
        )
        let1 = codegen.Let()
        name1 = let1.add_assignment("x", codegen.Number(1))
        name2 = let1.add_assignment("y", codegen.Number(2))
        let1.value = codegen.Add(name1, name2)

        case1 = codegen.Case(codegen.Add(codegen.Number(7), codegen.Number(8)))
        branch1 = case1.add_branch(codegen.Number(15))
        branch1.value = codegen.Number(16)
        branch2 = case1.add_branch(codegen.Otherwise())
        branch2.value = codegen.Number(17)

        let2 = codegen.Let()
        name3 = let2.add_assignment("a", codegen.Number(3))
        name4 = let2.add_assignment("b", case1)
        let2.add_assignment("c", codegen.Number(4))
        let2.value = codegen.Add(name3, name4)

        func_call = scope.variables["aFunction"].apply(let1, let2)
        func_call = codegen.simplify(func_call)
        self.assertCodeEqual(
            func_call.as_source_code(),
            """
        aFunction (let
                       x = 1
                       y = 2
                   in
                       (x + y)) (let
                                     a = 3
                                     b = case (7 + 8) of
                                             15 ->
                                                 16
                                             _ ->
                                                 17

                                     c = 4
                                 in
                                     (a + b))
        """,
        )
示例#6
0
    def test_case(self):
        scope = codegen.Scope()
        tmp = scope.reserve_name("tmp")
        case = codegen.Case(scope.variables[tmp])
        branch1 = case.add_branch(codegen.String("x"))
        branch1.value = codegen.Number(1)
        branch2 = case.add_branch(codegen.String("y"))
        branch2.value = codegen.Number(2)
        branch3 = case.add_branch(codegen.Otherwise())
        branch3.value = codegen.Number(3)

        case = codegen.simplify(case)
        self.assertCodeEqual(
            case.as_source_code(),
            """
            case tmp of
                "x" ->
                    1
                "y" ->
                    2
                _ ->
                    3
        """,
        )
示例#7
0
 def test_record_update(self):
     rec = types.Record()
     rec.add_field("name", dtypes.String)
     rec.add_field("age", dtypes.Number)
     rec.add_field("height", dtypes.Number)
     scope = codegen.Scope()
     tmp = scope.reserve_name("tmp", type=rec)
     var = scope.variables[tmp]
     update = codegen.RecordUpdate(var,
                                   name=codegen.String("Fred"),
                                   age=codegen.Number(34))
     self.assertCodeEqual(
         update.as_source_code(),
         """
         { tmp | age = 34, name = "Fred" }
     """,
     )
示例#8
0
 def test_function_call_using_apply(self):
     scope = codegen.Scope()
     scope.reserve_name("aFunction",
                        type=types.Function(dtypes.Number, dtypes.String))
     func_call = scope.variables["aFunction"].apply(codegen.Number(123))
     self.assertCodeEqual(func_call.as_source_code(), "aFunction 123")