Skip to content

Commit

Permalink
feat: support null statement (#130)
Browse files Browse the repository at this point in the history
  • Loading branch information
kumachan-mis authored Oct 9, 2023
1 parent 3c118a3 commit 30258bc
Show file tree
Hide file tree
Showing 12 changed files with 66 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/ast/ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ typedef enum AstType {
AST_CMPD_STMT,
AST_RET_STMT,
AST_EXPR_STMT,
AST_NULL_STMT,
AST_IF_STMT,
AST_WHILE_STMT,

Expand Down
3 changes: 3 additions & 0 deletions src/immcgen/immcgen.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ Vector* immcgen_generate_immcode(Immcgen* immcgen) {
case SRT_EXPR_STMT:
codes = gen_expression_stmt_immcode(immcgen);
break;
case SRT_NULL_STMT:
codes = gen_null_stmt_immcode();
break;
case SRT_IF_STMT:
codes = gen_if_else_stmt_immcode(immcgen);
break;
Expand Down
4 changes: 4 additions & 0 deletions src/immcgen/statement.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ Vector* gen_expression_stmt_immcode(Immcgen* immcgen) {
return codes;
}

Vector* gen_null_stmt_immcode(void) {
return new_vector(&t_immc);
}

Vector* gen_if_else_stmt_immcode(Immcgen* immcgen) {
Vector* gen_if_without_else_stmt_immcode(Immcgen * immcgen);
Vector* gen_if_with_else_stmt_immcode(Immcgen * immcgen);
Expand Down
1 change: 1 addition & 0 deletions src/immcgen/statement.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
Vector* gen_compound_stmt_immcode(Immcgen* immcgen);
Vector* gen_return_stmt_immcode(Immcgen* immcgen);
Vector* gen_expression_stmt_immcode(Immcgen* immcgen);
Vector* gen_null_stmt_immcode(void);
Vector* gen_if_else_stmt_immcode(Immcgen* immcgen);
Vector* gen_while_stmt_immcode(Immcgen* immcgen);

Expand Down
7 changes: 7 additions & 0 deletions src/parser/statement.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,13 @@ ParserReturn* parse_expression_stmt(Parser* parser) {
Ast* ast = NULL;
Error* err = NULL;

CToken* ctoken = vector_at(parser->ctokens, parser->index);
if (ctoken->type == CTOKEN_SEMICOLON) {
parser->index++;
ast = new_ast(AST_NULL_STMT, 0);
return new_parserret(ast);
}

parserret_assign(&ast, &err, parse_expr(parser));
if (err != NULL) {
return new_parserret_error(err);
Expand Down
8 changes: 8 additions & 0 deletions src/resolver/statement.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ ResolverReturn* resolve_stmt(Resolver* resolver) {
case AST_EXPR_STMT:
resolverret_assign(&srt, &errs, resolve_expression_stmt(resolver));
break;
case AST_NULL_STMT:
resolverret_assign(&srt, &errs, resolve_null_stmt());
break;
case AST_IF_STMT:
resolverret_assign(&srt, &errs, resolve_if_else_stmt(resolver));
break;
Expand Down Expand Up @@ -156,6 +159,11 @@ ResolverReturn* resolve_expression_stmt(Resolver* resolver) {
return new_resolverret(srt);
}

ResolverReturn* resolve_null_stmt(void) {
Srt* srt = new_srt(SRT_NULL_STMT, 0);
return new_resolverret(srt);
}

ResolverReturn* resolve_if_else_stmt(Resolver* resolver) {
Srt* srt = new_srt(SRT_IF_STMT, 0);
Srt* child_srt = NULL;
Expand Down
1 change: 1 addition & 0 deletions src/resolver/statement.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ ResolverReturn* resolve_stmt(Resolver* resolver);
ResolverReturn* resolve_compound_stmt(Resolver* resolver);
ResolverReturn* resolve_return_stmt(Resolver* resolver);
ResolverReturn* resolve_expression_stmt(Resolver* resolver);
ResolverReturn* resolve_null_stmt(void);
ResolverReturn* resolve_if_else_stmt(Resolver* resolver);
ResolverReturn* resolve_while_stmt(Resolver* resolver);

Expand Down
1 change: 1 addition & 0 deletions src/srt/srt.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ typedef enum SrtType {
SRT_CMPD_STMT,
SRT_RET_STMT,
SRT_EXPR_STMT,
SRT_NULL_STMT,
SRT_IF_STMT,
SRT_WHILE_STMT,

Expand Down
2 changes: 2 additions & 0 deletions test-fixtures/exprstmt/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ void put_int(int x);

int main(void) {
int x, y;
;
x = 1 + 3;
y = x - 2;
put_int(x);
put_int(y);
;
return 0;
}
12 changes: 12 additions & 0 deletions tests/immcgen/test_statement.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ void test_immcgen_compound_stmt_empty(void);
void test_immcgen_return_stmt_non_void(void);
void test_immcgen_return_stmt_void(void);
void test_immcgen_expression_stmt(void);
void test_immcgen_null_stmt(void);
void test_immcgen_if_stmt(void);
void test_immcgen_if_else_stmt(void);
void test_immcgen_if_else_stmt_chain(void);
Expand All @@ -35,6 +36,7 @@ CU_Suite* add_test_suite_stmt_immcgen(void) {
CU_ADD_TEST(suite, test_immcgen_return_stmt_non_void);
CU_ADD_TEST(suite, test_immcgen_return_stmt_void);
CU_ADD_TEST(suite, test_immcgen_expression_stmt);
CU_ADD_TEST(suite, test_immcgen_null_stmt);
CU_ADD_TEST(suite, test_immcgen_if_stmt);
CU_ADD_TEST(suite, test_immcgen_if_else_stmt);
CU_ADD_TEST(suite, test_immcgen_if_else_stmt_chain);
Expand Down Expand Up @@ -771,6 +773,16 @@ void test_immcgen_expression_stmt(void) {
delete_vector(expected);
}

void test_immcgen_null_stmt(void) {
Srt* input = new_srt(SRT_NULL_STMT, 0);

Vector* expected = new_vector(&t_immc);

run_stmt_immcgen_test(input, NULL, -1, expected);

delete_vector(expected);
}

void test_immcgen_if_stmt(void) {
Srt* input = new_srt(
SRT_IF_STMT, 2, // non-terminal
Expand Down
14 changes: 14 additions & 0 deletions tests/parser/test_statement.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ void test_parse_compound_stmt_empty(void);
void test_parse_return_stmt_non_void(void);
void test_parse_return_stmt_void(void);
void test_parse_expression_stmt(void);
void test_parse_null_stmt(void);
void test_parse_if_stmt(void);
void test_parse_if_else_stmt(void);
void test_parse_if_else_stmt_chain(void);
Expand All @@ -38,6 +39,7 @@ CU_Suite* add_test_suite_stmt_parser(void) {
CU_ADD_TEST(suite, test_parse_return_stmt_non_void);
CU_ADD_TEST(suite, test_parse_return_stmt_void);
CU_ADD_TEST(suite, test_parse_expression_stmt);
CU_ADD_TEST(suite, test_parse_null_stmt);
CU_ADD_TEST(suite, test_parse_if_stmt);
CU_ADD_TEST(suite, test_parse_if_else_stmt);
CU_ADD_TEST(suite, test_parse_if_else_stmt_chain);
Expand Down Expand Up @@ -744,6 +746,18 @@ void test_parse_expression_stmt(void) {
delete_ast(expected);
}

void test_parse_null_stmt(void) {
Vector* input = new_vector(&t_ctoken);
vector_push(input, new_ctoken(CTOKEN_SEMICOLON));
vector_push(input, new_ctoken(CTOKEN_EOF));

Ast* expected = new_ast(AST_NULL_STMT, 0);

run_stmt_parser_test(input, expected);

delete_ast(expected);
}

void test_parse_if_stmt(void) {
Vector* input = new_vector(&t_ctoken);
vector_push(input, new_ctoken(CTOKEN_LBRACE));
Expand Down
12 changes: 12 additions & 0 deletions tests/resolver/test_statement.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ void test_resolve_return_stmt_void(void);
void test_resolve_expression_stmt_scalar(void);
void test_resolve_expression_stmt_array(void);
void test_resolve_expression_stmt_function(void);
void test_resolve_null_stmt(void);
void test_resolve_if_stmt(void);
void test_resolve_if_else_stmt(void);
void test_resolve_if_else_stmt_chain(void);
Expand Down Expand Up @@ -47,6 +48,7 @@ CU_Suite* add_test_suite_stmt_resolver(void) {
CU_ADD_TEST(suite, test_resolve_expression_stmt_scalar);
CU_ADD_TEST(suite, test_resolve_expression_stmt_array);
CU_ADD_TEST(suite, test_resolve_expression_stmt_function);
CU_ADD_TEST(suite, test_resolve_null_stmt);
CU_ADD_TEST(suite, test_resolve_if_stmt);
CU_ADD_TEST(suite, test_resolve_if_else_stmt);
CU_ADD_TEST(suite, test_resolve_if_else_stmt_chain);
Expand Down Expand Up @@ -898,6 +900,16 @@ void test_resolve_expression_stmt_function(void) {
delete_srt(expected);
}

void test_resolve_null_stmt(void) {
Ast* input = new_ast(AST_NULL_STMT, 0);

Srt* expected = new_srt(SRT_NULL_STMT, 0);

run_stmt_resolver_test(input, NULL, NULL, expected);

delete_srt(expected);
}

void test_resolve_if_stmt(void) {
Ast* input = new_ast(AST_CMPD_STMT, 2, // non-terminal
new_ast(AST_IF_STMT, 2, // non-terminal
Expand Down

0 comments on commit 30258bc

Please sign in to comment.