def test_class_decl_with_methods(self): ''' Parse class decl with binded func. ''' input_string = ''' class X { bind { func n1 {} func n2 {} } } ''' real_ast = _parse(input_string) expected_ast = ast.Module(decl_list=[ ast.ClassDecl( name='X', decl_list=[ ast.FuncDecl( name='n1', signature=ast.FuncSignature(), ), ast.FuncDecl( name='n2', signature=ast.FuncSignature(), ), ], ) ]) misc.assert_equal(self, expected_ast, real_ast)
def p_func_signature(p): 'func_signature : generic params ARROW type' p[0] = ast.FuncSignature( param_list=p[2], return_type=p[4], generic_param_list=p[1], )
def test_print_int_constant(self): check_translation( test_case=self, input_ast=ast.Module(decl_list=[ ast.FuncDecl(name='start', signature=ast.FuncSignature(), body=[ ast.FuncCall( expr=ast.Ident('print'), arg_list=[ast.Number(1)], ), ]) ]), expected_output=''' void start(void); void start(void) { Int const_0; const_0 = 1; print_Int(&const_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, )
def test_generic_func_1(self): ''' First test of generic funcs. ''' input_string = 'func testFunc <Int> {}' real_ast = _parse(input_string) expected_ast = ast.Module(decl_list=[ ast.FuncDecl( name='testFunc', signature=ast.FuncSignature(generic_param_list=['Int'], ), ) ]) misc.assert_equal(self, expected_ast, real_ast)
def test_simple_func(self): ''' Parse minimal fnction decl. ''' input_string = 'func testfunc2 {}' real_ast = _parse(input_string) expected_ast = ast.Module(decl_list=[ ast.FuncDecl( name='testfunc2', signature=ast.FuncSignature(), ) ]) misc.assert_equal(self, expected_ast, real_ast)
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)
def _std_module(): ''' Return basic ast. Usage: expected_ast = _std_module() <add specific detaild to expected_ast> ''' return ast.Module(decl_list=[ ast.FuncDecl( name='start', signature=ast.FuncSignature(), ) ])
def test_multiply_func_params(self): check_translation( test_case=self, input_ast=ast.Module(decl_list=[ ast.FuncDecl( name='testFunc', signature=ast.FuncSignature(param_list=[ ast.Param(name='n1', datatype=ast.Ident('Int')), ast.Param(name='n2', datatype=ast.Ident('Int')), ], ), ), ast.FuncDecl(name='start', signature=ast.FuncSignature(), body=[ ast.FuncCall( expr=ast.Ident('testFunc'), arg_list=[ast.Number(1), ast.Number(2)], ), ]) ]), expected_output=''' void testFunc_Int_Int(Int* n1, Int* n2); void start(void); void testFunc_Int_Int(Int* n1, Int* n2) { } void start(void) { Int const_0; Int const_1; const_0 = 1; const_1 = 2; testFunc_Int_Int(&const_0, &const_1); } ''', )
def test_print_result_of_nested_calls(self): body = [ ast.FuncCall( expr=ast.Ident('print'), arg_list=[ ast.FuncCall( expr=ast.Ident('plus'), arg_list=[ ast.Number(1), ast.FuncCall( expr=ast.Ident('plus'), arg_list=[ ast.Number(2), ast.Number(3), ], ), ], ), ], ), ] check_translation( test_case=self, input_ast=ast.Module(decl_list=[ ast.FuncDecl( name='start', signature=ast.FuncSignature(), body=body, ) ]), expected_output=''' void start(void); void start(void) { Int tmp_0; Int tmp_1; Int const_0; Int const_1; Int const_2; const_0 = 1; const_1 = 2; const_2 = 3; plus_Int_Int(&tmp_0, &const_1, &const_2); plus_Int_Int(&tmp_1, &const_0, &tmp_0); print_Int(&tmp_1); } ''', )
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)
def test_simple_func_with_param(self): ''' Parse func that takes one param. ''' input_string = 'func testfunc (param ParamType) {}' real_ast = _parse(input_string) signature = ast.FuncSignature(param_list=[ ast.Param(name='param', datatype=datatype.SimpleDataType('ParamType')) ], ) expected_ast = ast.Module( decl_list=[ast.FuncDecl( name='testfunc', signature=signature, )]) misc.assert_equal(self, expected_ast, real_ast)
def test_bad_constant_type_error(self): class BadConstantClass(object): def __init__(self): pass input_ast = ast.Module(decl_list=[ ast.FuncDecl( name='start', signature=ast.FuncSignature(), ) ]) constants = input_ast.decl_list[0].constants # shortcut constants['badConst'] = BadConstantClass() input_ast.ident_list = {} generator_ = generator.Generator(input_ast) self.assertRaisesRegexp( Exception, 'Bad type:.*BadConstantClass', generator_.generate, )
def test_bad_stmt_type_error(self): class BadStmtClass(object): def __init__(self): pass input_ast = ast.Module(decl_list=[ ast.FuncDecl( name='start', signature=ast.FuncSignature(), body=[ BadStmtClass(), ], ) ]) input_ast.ident_list = \ ident_table.ident_table(input_ast) generator_ = generator.Generator(input_ast) self.assertRaisesRegexp( Exception, 'Bad stmt type:.*BadStmtClass', generator_.generate, )
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
def p_func_signature_without_return_type(p): 'func_signature : generic params' p[0] = ast.FuncSignature( param_list=p[2], generic_param_list=p[1], )
def create_constructor_func(class_decl): return ast.FuncSignature( return_type=datatype.SimpleDataType(class_decl.name), )