aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2025-07-29 06:04:29 +0900
committernsfisis <nsfisis@gmail.com>2025-08-15 10:04:35 +0900
commitbd5bbdebf116bb03da335d0e18a55a5597b55548 (patch)
tree978a9c6c4240c6997dead272e33c0019c173734c
parent53eec2c3f3e0de5b252215fb662e50e0c1829513 (diff)
downloadducc-bd5bbdebf116bb03da335d0e18a55a5597b55548.tar.gz
ducc-bd5bbdebf116bb03da335d0e18a55a5597b55548.tar.zst
ducc-bd5bbdebf116bb03da335d0e18a55a5597b55548.zip
feat: function main() now returns zero if no return value are explicitly provided
-rw-r--r--codegen.c4
-rw-r--r--main.c1
-rw-r--r--tests/064.sh22
3 files changed, 26 insertions, 1 deletions
diff --git a/codegen.c b/codegen.c
index 2240b22..d290e95 100644
--- a/codegen.c
+++ b/codegen.c
@@ -437,6 +437,10 @@ void codegen_func(CodeGen* g, AstNode* ast) {
codegen_func_prologue(g, ast);
codegen_stmt(g, ast->node_body);
+ if (string_equals_cstr(&ast->name, "main")) {
+ // C99: 5.1.2.2.3
+ printf(" mov rax, 0\n");
+ }
codegen_func_epilogue(g, ast);
printf("\n");
diff --git a/main.c b/main.c
index e9a4fa0..34a847f 100644
--- a/main.c
+++ b/main.c
@@ -26,5 +26,4 @@ int main(int argc, char** argv) {
Program* prog = parse(tokens);
analyze(prog);
codegen(prog);
- return 0;
}
diff --git a/tests/064.sh b/tests/064.sh
new file mode 100644
index 0000000..80fba4f
--- /dev/null
+++ b/tests/064.sh
@@ -0,0 +1,22 @@
+set -e
+
+# C99: 5.1.2.2.3
+bash ../../test_exit_code.sh 0 <<'EOF'
+int main() {
+}
+EOF
+
+bash ../../test_exit_code.sh 0 <<'EOF'
+int main() {
+ 1 + 2 + 3;
+}
+EOF
+
+bash ../../test_exit_code.sh 0 <<'EOF'
+int main() {
+ if (1) {
+ } else {
+ return 1;
+ }
+}
+EOF