Esempio n. 1
0
 def test_let_no_assignments(self):
     module = codegen.Module()
     func = codegen.Function("myfunc", parent_scope=module)
     func.body.value = codegen.String("Hello")
     func = codegen.simplify(func)
     self.assertCodeEqual(
         func.as_source_code(),
         """
         myfunc =
             "Hello"
     """,
     )
Esempio n. 2
0
 def test_string_join_omit_empty_2(self):
     scope = codegen.Scope()
     scope.reserve_name("tmp", type=dtypes.String)
     var = scope.variables["tmp"]
     join = codegen.StringConcat([codegen.String(""), var])
     join = codegen.simplify(join)
     self.assertCodeEqual(
         join.as_source_code(),
         """
         tmp
     """,
     )
Esempio n. 3
0
 def test_function(self):
     module = codegen.Module()
     func = codegen.Function("myfunc",
                             args=["myarg1", "myarg2"],
                             parent_scope=module)
     func.body.value = codegen.String("hello")
     func = codegen.simplify(func)
     self.assertCodeEqual(
         func.as_source_code(),
         """
         myfunc myarg1 myarg2 =
             "hello"
     """,
     )
Esempio n. 4
0
 def test_function_args_name_reserved_check(self):
     module = codegen.Module()
     module.reserve_function_arg_name("myarg")
     func_name = module.reserve_name("myfunc")
     func = codegen.Function(func_name, args=["myarg"], parent_scope=module)
     func.body.value = func.variables["myarg"]
     func = codegen.simplify(func)
     self.assertCodeEqual(
         func.as_source_code(),
         """
        myfunc myarg =
            myarg
     """,
     )
Esempio n. 5
0
    def test_add_function(self):
        module = codegen.Module(name="Main")
        func_name = module.reserve_name("myfunc")
        func = codegen.Function(func_name, parent_scope=module)
        func.body.value = codegen.String("hi")
        module.add_function(func_name, func)
        func = codegen.simplify(func)
        self.assertCodeEqual(
            module.as_source_code(),
            """
            module Main exposing (myfunc)

            myfunc =
                "hi"
        """,
        )
Esempio n. 6
0
 def test_let_one_assignment(self):
     module = codegen.Module()
     func = codegen.Function("myfunc", parent_scope=module)
     let = func.body
     x = let.add_assignment("x", codegen.String("Hello"))
     self.assertEqual(x.name, "x")
     self.assertEqual(x.type, dtypes.String)
     let.value = x
     func = codegen.simplify(func)
     self.assertCodeEqual(
         func.as_source_code(),
         """
         myfunc =
             "Hello"
     """,
     )
Esempio n. 7
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))
        """,
        )
Esempio n. 8
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
     """,
     )
Esempio n. 9
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)
         """,
     )
Esempio n. 10
0
 def test_string_join_collapse_strings(self):
     scope = codegen.Scope()
     scope.reserve_name("tmp", type=dtypes.String)
     var = scope.variables["tmp"]
     join1 = codegen.StringConcat([
         codegen.String("hello "),
         codegen.String("there "),
         var,
         codegen.String(" how"),
         codegen.String(" are you?"),
     ])
     join1 = codegen.simplify(join1)
     self.assertCodeEqual(
         join1.as_source_code(),
         """
         String.concat [ "hello there "
                       , tmp
                       , " how are you?"
                       ]
         """,
     )
Esempio n. 11
0
 def test_function_typed(self):
     module = codegen.Module()
     function_type = types.Function.for_multiple_inputs(
         [dtypes.String, dtypes.Number], dtypes.String)
     module.reserve_name("myfunc", type=function_type)
     func = codegen.Function(
         "myfunc",
         args=["myarg1", "myarg2"],
         parent_scope=module,
         function_type=function_type,
     )
     func.body.value = codegen.String("hello")
     func = codegen.simplify(func)
     self.assertCodeEqual(
         func.as_source_code(),
         """
         myfunc : String -> number -> String
         myfunc myarg1 myarg2 =
             "hello"
     """,
     )
     self.assertEqual(func.variables["myarg2"].type, dtypes.Number)
Esempio n. 12
0
 def test_string_join_one(self):
     join = codegen.StringConcat([codegen.String("hello")])
     join = codegen.simplify(join)
     self.assertCodeEqual(join.as_source_code(), '"hello"')
Esempio n. 13
0
 def test_string_join_empty(self):
     join = codegen.StringConcat([])
     join = codegen.simplify(join)
     self.assertCodeEqual(join.as_source_code(), '""')