示例#1
0
 def test_reserve_name_function_arg(self):
     scope = codegen.Scope()
     scope.reserve_function_arg_name("arg_name")
     scope.reserve_name("myfunc")
     func = codegen.Function("myfunc",
                             args=["arg_name"],
                             parent_scope=scope)
     self.assertNotIn("arg_name2", func.all_reserved_names())
示例#2
0
 def test_variable_reference_function_arg_check(self):
     module = codegen.Module()
     func_name = module.reserve_name("myfunc")
     func = codegen.Function(func_name,
                             args=["my_arg"],
                             parent_scope=module)
     # Can't use undefined 'some_name'
     self.assertRaises(AssertionError, lambda: func.variables["some_name"])
     # But can use function argument 'my_arg'
     ref = func.variables["my_arg"]
     self.assertCodeEqual(ref.as_source_code(), "my_arg")
示例#3
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"
     """,
     )
示例#4
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"
     """,
     )
示例#5
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
     """,
     )
示例#6
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"
        """,
        )
示例#7
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"
     """,
     )
示例#8
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)
         """,
     )
示例#9
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)