diff options
| author | nsfisis <nsfisis@gmail.com> | 2025-05-04 21:42:26 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2025-05-04 21:43:26 +0900 |
| commit | 8047c8beca7a5d04c206cf5ffc3eea9ba1e33f10 (patch) | |
| tree | 818943f0361f6049d23b9600a35684876f163e67 | |
| parent | 3bfcc240c7c68fd3aff8535df4e9e8e384eb9205 (diff) | |
| download | P4Dcc-8047c8beca7a5d04c206cf5ffc3eea9ba1e33f10.tar.gz P4Dcc-8047c8beca7a5d04c206cf5ffc3eea9ba1e33f10.tar.zst P4Dcc-8047c8beca7a5d04c206cf5ffc3eea9ba1e33f10.zip | |
support return stmt without result
| -rw-r--r-- | main.c | 11 | ||||
| -rw-r--r-- | tests/037.sh | 18 |
2 files changed, 27 insertions, 2 deletions
@@ -1063,6 +1063,11 @@ struct AstNode* parse_expr(struct Parser* p) { struct AstNode* parse_return_stmt(struct Parser* p) { expect(p, TK_K_RETURN); + if (peek_token(p)->kind == TK_SEMICOLON) { + next_token(p); + return ast_new(AST_RETURN_STMT); + } + struct AstNode* expr = parse_expr(p); expect(p, TK_SEMICOLON); @@ -1668,8 +1673,10 @@ void gen_return_stmt(struct CodeGen* g, struct AstNode* ast) { assert_ast_kind(ast, AST_RETURN_STMT); printf(" # gen_return_stmt\n"); - gen_expr(g, ast->expr1, GEN_RVAL); - printf(" pop rax\n"); + if (ast->expr1) { + gen_expr(g, ast->expr1, GEN_RVAL); + printf(" pop rax\n"); + } gen_func_epilogue(g, ast); } diff --git a/tests/037.sh b/tests/037.sh new file mode 100644 index 0000000..bc6867b --- /dev/null +++ b/tests/037.sh @@ -0,0 +1,18 @@ +set -e + +cat <<'EOF' > expected +hi +EOF +bash ../../test_diff.sh <<'EOF' +int printf(); + +void f() { + printf("hi\n"); + return; +} + +int main() { + f(); + return 0; +} +EOF |
