Esempio n. 1
0
 def p_stmt_var_decl_2(p):
     'stmt : IDENT DOUBLECOLONASSIGN expr'
     p[0] = ast.VarDecl(
         name=p[1],
         expr=p[3],
         allocate_memory_on_stack=True,
     )
Esempio n. 2
0
    def test_bad_expr_type_error(self):
        class BadExprClass(object):
            def __init__(self):
                self.binded_var_name = 'xxx'

        input_ast = ast.Module(decl_list=[
            ast.FuncDecl(
                name='start',
                signature=ast.FuncSignature(),
                body=[
                    ast.VarDecl(
                        name='testVar',
                        expr=BadExprClass(),
                        datatype=datatype.SimpleDataType('Int'),
                    ),
                ],
            )
        ])
        input_ast.ident_list = \
            ident_table.ident_table(input_ast)
        generator_ = generator.Generator(input_ast)
        self.assertRaisesRegexp(
            Exception,
            'Bad expr type:.*BadExprClass',
            generator_.generate,
        )
Esempio n. 3
0
 def test_var_decl_without_initialization(self):
     ''' Parse var decl stmt. '''
     input_string = 'func start { testVar := Int() }'
     real_ast = _parse(input_string)
     expected_ast = _std_module()
     var = ast.VarDecl(name='testVar',
                       expr=ast.FuncCall(expr=ast.Ident('Int'), ))
     expected_ast.decl_list[0].body.append(var)
     misc.assert_equal(self, expected_ast, real_ast)
Esempio n. 4
0
 def test_var_decl_with_ctor(self):
     ''' Parse var decl stmt with constructor call. '''
     input_string = 'func start { p := Parser() }'
     real_ast = _parse(input_string)
     expected_ast = _std_module()
     var = ast.VarDecl(
         name='p',
         expr=ast.FuncCall(expr=ast.Ident('Parser'), ),
     )
     expected_ast.decl_list[0].body.append(var)
     misc.assert_equal(self, expected_ast, real_ast)
Esempio n. 5
0
 def test_var_decl_with_initialization(self):
     ''' Parse var decl stmt with
         initiaization and without explicit  type.
     '''
     input_string = 'func start { testVar := 666 }'
     real_ast = _parse(input_string)
     expected_ast = _std_module()
     var = ast.VarDecl(
         name='testVar',
         expr=ast.Number(666),
     )
     expected_ast.decl_list[0].body.append(var)
     misc.assert_equal(self, expected_ast, real_ast)
Esempio n. 6
0
 def test_var_decl_with_init_2(self):
     ''' Parse var decl stmt with
         complex initiaization.
     '''
     input_string = 'func start { v2 := plus(1 2) }'
     real_ast = _parse(input_string)
     expected_ast = _std_module()
     var = ast.VarDecl(
         name='v2',
         expr=ast.FuncCall(
             expr=ast.Ident('plus'),
             arg_list=[ast.Number(1), ast.Number(2)],
         ),
     )
     expected_ast.decl_list[0].body.append(var)
     misc.assert_equal(self, expected_ast, real_ast)
Esempio n. 7
0
 def test_var_decl_with_ctor_and_args(self):
     ''' Parse var decl stmt with
         constructor call with args.
     '''
     input_string = 'func start { p := Parser(lexer 1) }'
     real_ast = _parse(input_string)
     expected_ast = _std_module()
     expected_ast.decl_list[0].body.append(
         ast.VarDecl(
             name='p',
             expr=ast.FuncCall(
                 expr=ast.Ident('Parser'),
                 arg_list=[
                     ast.Ident('lexer'),
                     ast.Number(1),
                 ],
             ),
         ))
     misc.assert_equal(self, expected_ast, real_ast)
Esempio n. 8
0
 def p_stmt_var_decl_1(p):
     'stmt : IDENT COLONASSIGN expr'
     p[0] = ast.VarDecl(name=p[1], expr=p[3])