Skip to content

Commit

Permalink
improve resolver test
Browse files Browse the repository at this point in the history
  • Loading branch information
kumachan-mis committed Dec 9, 2023
1 parent 54d387b commit d4214ef
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 50 deletions.
4 changes: 2 additions & 2 deletions src/resolver/statement.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
106 changes: 58 additions & 48 deletions tests/resolver/test_statement.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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
Expand All @@ -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
Expand All @@ -1311,43 +1315,47 @@ 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(
SRT_ASSIGN_EXPR, new_integer_dtype(DTYPE_INT), 2, // non-terminal
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);

delete_srt(expected);
}

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));
Expand All @@ -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
Expand All @@ -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
Expand All @@ -1386,15 +1395,16 @@ 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(
SRT_ASSIGN_EXPR, new_integer_dtype(DTYPE_INT), 2, // non-terminal
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);

Expand Down
64 changes: 64 additions & 0 deletions tests/resolver/test_statement_error.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit d4214ef

Please sign in to comment.