Example #1
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,
        )
Example #2
0
 def standart_funcs():
     datatype_int = datatype.SimpleDataType('Int')
     datatype_string = datatype.SimpleDataType('String')
     std_signature = ast.FuncSignature(
         return_type=datatype.SimpleDataType('Int'),
         param_list=[
             ast.Param(name='a', datatype=datatype_int),
             ast.Param(name='b', datatype=datatype_int),
         ],
     )
     ident_list = {
         # Int constructor
         'Int': ast.FuncSignature(
             return_type=datatype_int,
             param_list=[
                 ast.Param(name='n', datatype=datatype_int),
             ],
         ),
         'printNewLine': ast.FuncSignature(),
         'print': [
             ast.FuncSignature(
                 param_list=[
                     ast.Param(name='s', datatype=datatype_string),
                 ],
             ),
             ast.FuncSignature(
                 param_list=[
                     ast.Param(name='n', datatype=datatype_int),
                 ],
             ),
         ],
         'allocInt': ast.FuncSignature(
             return_type=datatype.SimpleDataType(
                 name='Int',
                 prefix_list=['R'],
             ),
         ),
         'isEqual': std_signature,
         'isLess': std_signature,
         'isGreater': std_signature,
         'minus': std_signature,
         'plus': std_signature,
         'multiply': std_signature,
     }
     return ident_list
Example #3
0
 def test_simple_func_with_2_params(self):
     ''' Parse func that takes two params. '''
     input_string = 'func testfunc (par1 ParamType, par2 ParamType) {}'
     real_ast = _parse(input_string)
     signature = ast.FuncSignature(param_list=[
         ast.Param(
             name='par1',
             datatype=datatype.SimpleDataType('ParamType'),
         ),
         ast.Param(
             name='par2',
             datatype=datatype.SimpleDataType('ParamType'),
         )
     ])
     expected_ast = ast.Module(
         decl_list=[ast.FuncDecl(
             name='testfunc',
             signature=signature,
         )])
     misc.assert_equal(self, expected_ast, real_ast)
Example #4
0
 def test_simple_func_with_return_value(self):
     ''' Parse func that returns Int. '''
     input_string = 'func testfunc2 -> Int {}'
     real_ast = _parse(input_string)
     expected_ast = ast.Module(decl_list=[
         ast.FuncDecl(
             name='testfunc2',
             signature=ast.FuncSignature(
                 return_type=datatype.SimpleDataType('Int'), ),
         )
     ])
     misc.assert_equal(self, expected_ast, real_ast)
Example #5
0
 def test_const_decl(self):
     ''' Parse constant decl. '''
     input_string = 'const importantIdent Int := 10'
     real_ast = _parse(input_string)
     expected_ast = ast.Module(decl_list=[
         ast.ConstDecl(
             name='importantIdent',
             datatype=datatype.SimpleDataType('Int'),
             expr=ast.Number(10),
         )
     ])
     misc.assert_equal(self, expected_ast, real_ast)
Example #6
0
 def test_class_type_decl(self):
     ''' Parse class decl. '''
     input_string = ('class MyClass {\n'
                     '  store {\n'
                     '    field1 Int\n'
                     '    field2 Float\n'
                     '  }\n'
                     '}\n')
     real_ast = _parse(input_string)
     expected_ast = ast.Module(decl_list=[
         ast.ClassDecl(name='MyClass',
                       field_list=[
                           ast.Field(
                               name='field1',
                               datatype=datatype.SimpleDataType('Int'),
                           ),
                           ast.Field(
                               name='field2',
                               datatype=datatype.SimpleDataType('Float'),
                           )
                       ])
     ])
     misc.assert_equal(self, expected_ast, real_ast)
Example #7
0
 def test_type_prefix_1(self):
     input_string = 'func testFunc -> RM:Int {}'
     real_ast = _parse(input_string)
     expected_ast = ast.Module(decl_list=[
         ast.FuncDecl(
             name='testFunc',
             signature=ast.FuncSignature(
                 return_type=datatype.SimpleDataType(
                     name='Int',
                     prefix_list=['R', 'M'],
                 ), ),
         )
     ])
     misc.assert_equal(self, expected_ast, real_ast)
Example #8
0
 def p_type_ident_2(p):
     'type : IDENT COLON IDENT'
     p[0] = datatype.SimpleDataType(
         name=p[3],
         prefix_list=list(p[1]),
     )
Example #9
0
 def p_type_ident_1(p):
     'type : IDENT'
     p[0] = datatype.SimpleDataType(name=p[1])
Example #10
0
 def create_constructor_func(class_decl):
     return ast.FuncSignature(
         return_type=datatype.SimpleDataType(class_decl.name),
     )