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_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_simple_return_3(self): ''' Parse return stmt with func call. ''' input_string = 'func start { return x() }' real_ast = _parse(input_string) expected_ast = _std_module() expected_ast.decl_list[0].body.append( ast.Return(expr=ast.FuncCall(expr=ast.Ident('x'), ), ), ) misc.assert_equal(self, expected_ast, real_ast)
def test_nested_func_call_1(self): ''' Parse nested func call. ''' input_string = 'func start { a()() }' real_ast = _parse(input_string) expected_ast = _std_module() expected_ast.decl_list[0].body.append( ast.FuncCall(expr=ast.FuncCall(expr=ast.Ident('a'), ), )) misc.assert_equal(self, expected_ast, real_ast)
def test_simple_func_call(self): ''' Parse simple func call. ''' input_string = 'func start { fname2() }' real_ast = _parse(input_string) expected_ast = _std_module() funccall = ast.FuncCall(expr=ast.Ident('fname2'), ) expected_ast.decl_list[0].body.append(funccall) misc.assert_equal(self, expected_ast, real_ast)
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)
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)
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)
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_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)
def p_expr_ident(p): 'expr : IDENT' p[0] = ast.Ident(name=p[1])