aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2025-05-04 21:42:26 +0900
committernsfisis <nsfisis@gmail.com>2025-05-04 21:43:26 +0900
commit8047c8beca7a5d04c206cf5ffc3eea9ba1e33f10 (patch)
tree818943f0361f6049d23b9600a35684876f163e67
parent3bfcc240c7c68fd3aff8535df4e9e8e384eb9205 (diff)
downloadP4Dcc-8047c8beca7a5d04c206cf5ffc3eea9ba1e33f10.tar.gz
P4Dcc-8047c8beca7a5d04c206cf5ffc3eea9ba1e33f10.tar.zst
P4Dcc-8047c8beca7a5d04c206cf5ffc3eea9ba1e33f10.zip
support return stmt without result
-rw-r--r--main.c11
-rw-r--r--tests/037.sh18
2 files changed, 27 insertions, 2 deletions
diff --git a/main.c b/main.c
index 25d0274..bb80e4f 100644
--- a/main.c
+++ b/main.c
@@ -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