From d4214ef2507d088468cefd60c335f25a043afe84 Mon Sep 17 00:00:00 2001 From: kumachan-mis <29433058+kumachan-mis@users.noreply.github.com> Date: Sat, 9 Dec 2023 09:40:00 +0000 Subject: [PATCH] improve resolver test --- src/resolver/statement.c | 4 +- tests/resolver/test_statement.c | 106 ++++++++++++++------------ tests/resolver/test_statement_error.c | 64 ++++++++++++++++ 3 files changed, 124 insertions(+), 50 deletions(-) diff --git a/src/resolver/statement.c b/src/resolver/statement.c index bda07260..d53effa4 100644 --- a/src/resolver/statement.c +++ b/src/resolver/statement.c @@ -273,7 +273,7 @@ ResolverReturn* resolve_continue_stmt(Resolver* resolver) { } ResolverReturn* resolve_break_stmt(Resolver* resolver) { - if (!resolver->inside_loop) { + if (!resolver->inside_loop && resolver->switch_cases == NULL) { Vector* errs = new_vector(&t_error); Error* err = new_error("break statement is not allowed outside of loop or switch"); vector_push(errs, err); @@ -381,7 +381,7 @@ ResolverReturn* resolve_switch_stmt(Resolver* resolver) { if (!dtype_isinteger(child_srt->dtype)) { errs = new_vector(&t_error); - err = new_error("expression of switch statement should have integer type"); + err = new_error("condition of switch statement should have integer type"); vector_push(errs, err); delete_srt(srt); return new_resolverret_errors(errs); diff --git a/tests/resolver/test_statement.c b/tests/resolver/test_statement.c index c305fa5b..eb7d9687 100644 --- a/tests/resolver/test_statement.c +++ b/tests/resolver/test_statement.c @@ -1262,27 +1262,30 @@ void test_resolve_if_else_stmt_chain(void) { } void test_resolve_switch_stmt_iliteral(void) { - Ast* input = new_ast( - AST_SWITCH_STMT, 2, // non-terminal - new_identifier_ast(AST_IDENT_EXPR, new_string("x")), - new_ast(AST_CMPD_STMT, 3, - new_ast(AST_CASE_STMT, 2, // non-terminal - new_iliteral_ast(AST_INT_EXPR, new_signed_iliteral(INTEGER_INT, 0)), - new_ast(AST_EXPR_STMT, 1, // non-terminal - new_ast(AST_ASSIGN_EXPR, 2, // non-terminal - new_identifier_ast(AST_IDENT_EXPR, new_string("y")), - new_iliteral_ast(AST_INT_EXPR, new_signed_iliteral(INTEGER_INT, 1))))), - new_ast(AST_CASE_STMT, 2, // non-terminal - new_iliteral_ast(AST_INT_EXPR, new_signed_iliteral(INTEGER_INT, 1)), - new_ast(AST_EXPR_STMT, 1, // non-terminal - new_ast(AST_ASSIGN_EXPR, 2, // non-terminal - new_identifier_ast(AST_IDENT_EXPR, new_string("y")), - new_iliteral_ast(AST_INT_EXPR, new_signed_iliteral(INTEGER_INT, 2))))), - new_ast(AST_DEFAULT_STMT, 1, // non-terminal - new_ast(AST_EXPR_STMT, 1, // non-terminal - new_ast(AST_ASSIGN_EXPR, 2, // non-terminal - new_identifier_ast(AST_IDENT_EXPR, new_string("y")), - new_iliteral_ast(AST_INT_EXPR, new_signed_iliteral(INTEGER_INT, 9))))))); + Ast* input = + new_ast(AST_SWITCH_STMT, 2, // non-terminal + new_identifier_ast(AST_IDENT_EXPR, new_string("x")), + new_ast(AST_CMPD_STMT, 6, + new_ast(AST_CASE_STMT, 2, // non-terminal + new_iliteral_ast(AST_INT_EXPR, new_signed_iliteral(INTEGER_INT, 0)), + new_ast(AST_EXPR_STMT, 1, // non-terminal + new_ast(AST_ASSIGN_EXPR, 2, // non-terminal + new_identifier_ast(AST_IDENT_EXPR, new_string("y")), + new_iliteral_ast(AST_INT_EXPR, new_signed_iliteral(INTEGER_INT, 1))))), + new_ast(AST_BREAK_STMT, 0), + new_ast(AST_CASE_STMT, 2, // non-terminal + new_iliteral_ast(AST_INT_EXPR, new_signed_iliteral(INTEGER_INT, 1)), + new_ast(AST_EXPR_STMT, 1, // non-terminal + new_ast(AST_ASSIGN_EXPR, 2, // non-terminal + new_identifier_ast(AST_IDENT_EXPR, new_string("y")), + new_iliteral_ast(AST_INT_EXPR, new_signed_iliteral(INTEGER_INT, 2))))), + new_ast(AST_BREAK_STMT, 0), + new_ast(AST_DEFAULT_STMT, 1, // non-terminal + new_ast(AST_EXPR_STMT, 1, // non-terminal + new_ast(AST_ASSIGN_EXPR, 2, // non-terminal + new_identifier_ast(AST_IDENT_EXPR, new_string("y")), + new_iliteral_ast(AST_INT_EXPR, new_signed_iliteral(INTEGER_INT, 9))))), + new_ast(AST_BREAK_STMT, 0))); SymbolTable* local_table = new_symboltable(); symboltable_define_memory(local_table, new_string("x"), new_integer_dtype(DTYPE_INT)); @@ -1292,7 +1295,7 @@ void test_resolve_switch_stmt_iliteral(void) { SRT_SWITCH_STMT, 2, // non-terminal new_identifier_srt(SRT_IDENT_EXPR, new_integer_dtype(DTYPE_INT), new_string("x")), new_srt( - SRT_CMPD_STMT, 3, // non-terminal + SRT_CMPD_STMT, 6, // non-terminal new_srt(SRT_CASE_STMT, 2, // non-terminal new_iliteral_srt(new_integer_dtype(DTYPE_INT), new_signed_iliteral(INTEGER_INT, 0)), new_srt(SRT_EXPR_STMT, 1, // non-terminal @@ -1302,6 +1305,7 @@ void test_resolve_switch_stmt_iliteral(void) { SRT_ADDR_EXPR, new_pointer_dtype(new_integer_dtype(DTYPE_INT)), 1, // non-terminal new_identifier_srt(SRT_IDENT_EXPR, new_integer_dtype(DTYPE_INT), new_string("y"))), new_iliteral_srt(new_integer_dtype(DTYPE_INT), new_signed_iliteral(INTEGER_INT, 1))))), + new_srt(SRT_BREAK_STMT, 0), new_srt(SRT_CASE_STMT, 2, // non-terminal new_iliteral_srt(new_integer_dtype(DTYPE_INT), new_signed_iliteral(INTEGER_INT, 1)), new_srt(SRT_EXPR_STMT, 1, // non-terminal @@ -1311,6 +1315,7 @@ void test_resolve_switch_stmt_iliteral(void) { SRT_ADDR_EXPR, new_pointer_dtype(new_integer_dtype(DTYPE_INT)), 1, // non-terminal new_identifier_srt(SRT_IDENT_EXPR, new_integer_dtype(DTYPE_INT), new_string("y"))), new_iliteral_srt(new_integer_dtype(DTYPE_INT), new_signed_iliteral(INTEGER_INT, 2))))), + new_srt(SRT_BREAK_STMT, 0), new_srt(SRT_DEFAULT_STMT, 1, // non-terminal new_srt(SRT_EXPR_STMT, 1, // non-terminal new_dtyped_srt( @@ -1318,8 +1323,8 @@ void test_resolve_switch_stmt_iliteral(void) { new_dtyped_srt( SRT_ADDR_EXPR, new_pointer_dtype(new_integer_dtype(DTYPE_INT)), 1, // non-terminal new_identifier_srt(SRT_IDENT_EXPR, new_integer_dtype(DTYPE_INT), new_string("y"))), - new_iliteral_srt(new_integer_dtype(DTYPE_INT), - new_signed_iliteral(INTEGER_INT, 9))))))); + new_iliteral_srt(new_integer_dtype(DTYPE_INT), new_signed_iliteral(INTEGER_INT, 9))))), + new_srt(SRT_BREAK_STMT, 0))); run_stmt_resolver_test(input, local_table, NULL, expected); @@ -1327,27 +1332,30 @@ void test_resolve_switch_stmt_iliteral(void) { } void test_resolve_switch_stmt_enum(void) { - Ast* input = new_ast( - AST_SWITCH_STMT, 2, // non-terminal - new_identifier_ast(AST_IDENT_EXPR, new_string("x")), - new_ast(AST_CMPD_STMT, 3, - new_ast(AST_CASE_STMT, 2, // non-terminal - new_identifier_ast(AST_IDENT_EXPR, new_string("A")), - new_ast(AST_EXPR_STMT, 1, // non-terminal - new_ast(AST_ASSIGN_EXPR, 2, // non-terminal - new_identifier_ast(AST_IDENT_EXPR, new_string("y")), - new_iliteral_ast(AST_INT_EXPR, new_signed_iliteral(INTEGER_INT, 1))))), - new_ast(AST_CASE_STMT, 2, // non-terminal - new_identifier_ast(AST_IDENT_EXPR, new_string("B")), - new_ast(AST_EXPR_STMT, 1, // non-terminal - new_ast(AST_ASSIGN_EXPR, 2, // non-terminal - new_identifier_ast(AST_IDENT_EXPR, new_string("y")), - new_iliteral_ast(AST_INT_EXPR, new_signed_iliteral(INTEGER_INT, 2))))), - new_ast(AST_DEFAULT_STMT, 1, // non-terminal - new_ast(AST_EXPR_STMT, 1, // non-terminal - new_ast(AST_ASSIGN_EXPR, 2, // non-terminal - new_identifier_ast(AST_IDENT_EXPR, new_string("y")), - new_iliteral_ast(AST_INT_EXPR, new_signed_iliteral(INTEGER_INT, 9))))))); + Ast* input = + new_ast(AST_SWITCH_STMT, 2, // non-terminal + new_identifier_ast(AST_IDENT_EXPR, new_string("x")), + new_ast(AST_CMPD_STMT, 6, + new_ast(AST_CASE_STMT, 2, // non-terminal + new_identifier_ast(AST_IDENT_EXPR, new_string("A")), + new_ast(AST_EXPR_STMT, 1, // non-terminal + new_ast(AST_ASSIGN_EXPR, 2, // non-terminal + new_identifier_ast(AST_IDENT_EXPR, new_string("y")), + new_iliteral_ast(AST_INT_EXPR, new_signed_iliteral(INTEGER_INT, 1))))), + new_ast(AST_BREAK_STMT, 0), + new_ast(AST_CASE_STMT, 2, // non-terminal + new_identifier_ast(AST_IDENT_EXPR, new_string("B")), + new_ast(AST_EXPR_STMT, 1, // non-terminal + new_ast(AST_ASSIGN_EXPR, 2, // non-terminal + new_identifier_ast(AST_IDENT_EXPR, new_string("y")), + new_iliteral_ast(AST_INT_EXPR, new_signed_iliteral(INTEGER_INT, 2))))), + new_ast(AST_BREAK_STMT, 0), + new_ast(AST_DEFAULT_STMT, 1, // non-terminal + new_ast(AST_EXPR_STMT, 1, // non-terminal + new_ast(AST_ASSIGN_EXPR, 2, // non-terminal + new_identifier_ast(AST_IDENT_EXPR, new_string("y")), + new_iliteral_ast(AST_INT_EXPR, new_signed_iliteral(INTEGER_INT, 9))))), + new_ast(AST_BREAK_STMT, 0))); SymbolTable* local_table = new_symboltable(); symboltable_define_memory(local_table, new_string("x"), new_integer_dtype(DTYPE_INT)); @@ -1367,7 +1375,7 @@ void test_resolve_switch_stmt_enum(void) { SRT_SWITCH_STMT, 2, // non-terminal new_identifier_srt(SRT_IDENT_EXPR, new_integer_dtype(DTYPE_INT), new_string("x")), new_srt( - SRT_CMPD_STMT, 3, // non-terminal + SRT_CMPD_STMT, 6, // non-terminal new_srt(SRT_CASE_STMT, 2, // non-terminal new_iliteral_srt(new_integer_dtype(DTYPE_INT), new_signed_iliteral(INTEGER_INT, 0)), new_srt(SRT_EXPR_STMT, 1, // non-terminal @@ -1377,6 +1385,7 @@ void test_resolve_switch_stmt_enum(void) { SRT_ADDR_EXPR, new_pointer_dtype(new_integer_dtype(DTYPE_INT)), 1, // non-terminal new_identifier_srt(SRT_IDENT_EXPR, new_integer_dtype(DTYPE_INT), new_string("y"))), new_iliteral_srt(new_integer_dtype(DTYPE_INT), new_signed_iliteral(INTEGER_INT, 1))))), + new_srt(SRT_BREAK_STMT, 0), new_srt(SRT_CASE_STMT, 2, // non-terminal new_iliteral_srt(new_integer_dtype(DTYPE_INT), new_signed_iliteral(INTEGER_INT, 1)), new_srt(SRT_EXPR_STMT, 1, // non-terminal @@ -1386,6 +1395,7 @@ void test_resolve_switch_stmt_enum(void) { SRT_ADDR_EXPR, new_pointer_dtype(new_integer_dtype(DTYPE_INT)), 1, // non-terminal new_identifier_srt(SRT_IDENT_EXPR, new_integer_dtype(DTYPE_INT), new_string("y"))), new_iliteral_srt(new_integer_dtype(DTYPE_INT), new_signed_iliteral(INTEGER_INT, 2))))), + new_srt(SRT_BREAK_STMT, 0), new_srt(SRT_DEFAULT_STMT, 1, // non-terminal new_srt(SRT_EXPR_STMT, 1, // non-terminal new_dtyped_srt( @@ -1393,8 +1403,8 @@ void test_resolve_switch_stmt_enum(void) { new_dtyped_srt( SRT_ADDR_EXPR, new_pointer_dtype(new_integer_dtype(DTYPE_INT)), 1, // non-terminal new_identifier_srt(SRT_IDENT_EXPR, new_integer_dtype(DTYPE_INT), new_string("y"))), - new_iliteral_srt(new_integer_dtype(DTYPE_INT), - new_signed_iliteral(INTEGER_INT, 9))))))); + new_iliteral_srt(new_integer_dtype(DTYPE_INT), new_signed_iliteral(INTEGER_INT, 9))))), + new_srt(SRT_BREAK_STMT, 0))); run_stmt_resolver_test(input, local_table, NULL, expected); diff --git a/tests/resolver/test_statement_error.c b/tests/resolver/test_statement_error.c index 6cdb77a0..fcc37f6c 100644 --- a/tests/resolver/test_statement_error.c +++ b/tests/resolver/test_statement_error.c @@ -527,12 +527,76 @@ void test_resolve_if_else_stmt_error_else_child(void) { } void test_resolve_switch_stmt_error_condition_child(void) { + Ast* input = + new_ast(AST_SWITCH_STMT, 2, // non-terminal + new_identifier_ast(AST_IDENT_EXPR, new_string("x")), + new_ast(AST_CMPD_STMT, 2, // non-terminal + new_ast(AST_CASE_STMT, 2, // non-terminal + new_iliteral_ast(AST_INT_EXPR, new_signed_iliteral(INTEGER_INT, 1)), + new_ast(AST_EXPR_STMT, 1, // non-terminal + new_ast(AST_ASSIGN_EXPR, 2, // non-terminal + new_identifier_ast(AST_IDENT_EXPR, new_string("y")), + new_iliteral_ast(AST_INT_EXPR, new_signed_iliteral(INTEGER_INT, 1))))), + new_ast(AST_BREAK_STMT, 0))); + + SymbolTable* local_table = new_symboltable(); + symboltable_define_memory(local_table, new_string("y"), new_integer_dtype(DTYPE_INT)); + + Vector* expected = new_vector(&t_error); + vector_push(expected, new_error("identifier 'x' is used before declared")); + + run_stmt_resolver_error_test(input, NULL, NULL, expected); + + delete_vector(expected); } void test_resolve_switch_stmt_error_condition_non_integer(void) { + Ast* input = + new_ast(AST_SWITCH_STMT, 2, // non-terminal + new_identifier_ast(AST_IDENT_EXPR, new_string("x")), + new_ast(AST_CMPD_STMT, 2, // non-terminal + new_ast(AST_CASE_STMT, 2, // non-terminal + new_iliteral_ast(AST_INT_EXPR, new_signed_iliteral(INTEGER_INT, 1)), + new_ast(AST_EXPR_STMT, 1, // non-terminal + new_ast(AST_ASSIGN_EXPR, 2, // non-terminal + new_identifier_ast(AST_IDENT_EXPR, new_string("y")), + new_iliteral_ast(AST_INT_EXPR, new_signed_iliteral(INTEGER_INT, 1))))), + new_ast(AST_BREAK_STMT, 0))); + + SymbolTable* local_table = new_symboltable(); + symboltable_define_memory(local_table, new_string("x"), new_pointer_dtype(new_integer_dtype(DTYPE_INT))); + symboltable_define_memory(local_table, new_string("y"), new_integer_dtype(DTYPE_INT)); + + Vector* expected = new_vector(&t_error); + vector_push(expected, new_error("condition of switch statement should have integer type")); + + run_stmt_resolver_error_test(input, local_table, NULL, expected); + + delete_vector(expected); } void test_resolve_switch_stmt_error_body_child(void) { + Ast* input = + new_ast(AST_SWITCH_STMT, 2, // non-terminal + new_identifier_ast(AST_IDENT_EXPR, new_string("x")), + new_ast(AST_CMPD_STMT, 2, // non-terminal + new_ast(AST_CASE_STMT, 2, // non-terminal + new_iliteral_ast(AST_INT_EXPR, new_signed_iliteral(INTEGER_INT, 1)), + new_ast(AST_EXPR_STMT, 1, // non-terminal + new_ast(AST_ASSIGN_EXPR, 2, // non-terminal + new_identifier_ast(AST_IDENT_EXPR, new_string("y")), + new_iliteral_ast(AST_INT_EXPR, new_signed_iliteral(INTEGER_INT, 1))))), + new_ast(AST_BREAK_STMT, 0))); + + SymbolTable* local_table = new_symboltable(); + symboltable_define_memory(local_table, new_string("x"), new_integer_dtype(DTYPE_INT)); + + Vector* expected = new_vector(&t_error); + vector_push(expected, new_error("identifier 'x' is used before declared")); + + run_stmt_resolver_error_test(input, NULL, NULL, expected); + + delete_vector(expected); } void test_resolve_while_stmt_error_condition_child(void) {