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_simple_return_1(self): ''' Parse return stmt with integer. ''' input_string = 'func start { return 1 }' real_ast = _parse(input_string) expected_ast = _std_module() expected_ast.decl_list[0].body.append(ast.Return(expr=ast.Number(1)), ) misc.assert_equal(self, expected_ast, real_ast)
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 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_if(self): ''' Parse if stmt. ''' input_string = 'func start { if 1 {} }' real_ast = _parse(input_string) expected_ast = _std_module() expected_ast.decl_list[0].body.append( ast.If( condition=ast.Number(1), branch_if=[], )) misc.assert_equal(self, expected_ast, real_ast)
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)
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_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)
def test_var_decl_with_type_and_initialization(self): ''' Parse var decl stmt with initiaization. ''' input_string = 'func start { testVar := Int(666) }' real_ast = _parse(input_string) expected_ast = _std_module() var = ast.VarDecl( name='testVar', expr=ast.FuncCall( expr=ast.Ident('Int'), arg_list=[ ast.Number(666), ], ), ) 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 p_expr_number(p): 'expr : NUMBER' p[0] = ast.Number(value=p[1])