From 5e630e9d34f082c5737cf61aa103b1db3c1ed6e8 Mon Sep 17 00:00:00 2001 From: nsfisis Date: Sat, 3 May 2025 17:57:17 +0900 Subject: for stmt --- main.c | 8 +++++--- tests/012.sh | 17 +++++++++++++++++ tests/013.sh | 17 +++++++++++++++++ 3 files changed, 39 insertions(+), 3 deletions(-) create mode 100644 tests/012.sh create mode 100644 tests/013.sh diff --git a/main.c b/main.c index 7298d56..c22f875 100644 --- a/main.c +++ b/main.c @@ -852,8 +852,8 @@ void gen_for_stmt(CODEGEN* g, AST* ast) { printf(" # gen_for_stmt\n"); int label = gen_new_label(g); - *g->loop_labels = label; g->loop_labels += 1; + *g->loop_labels = label; gen_expr(g, ast->expr1, GEN_RVAL); printf(".Lbegin%d:\n", label); @@ -875,14 +875,16 @@ void gen_break_stmt(CODEGEN* g, AST* ast) { assert_ast_kind(ast, AST_BREAK_STMT); printf(" # gen_break_stmt\n"); - todo(); + int label = *g->loop_labels; + printf(" jmp .Lend%d\n", label); } void gen_continue_stmt(CODEGEN* g, AST* ast) { assert_ast_kind(ast, AST_CONTINUE_STMT); printf(" # gen_continue_stmt\n"); - todo(); + int label = *g->loop_labels; + printf(" jmp .Lcontinue%d\n", label); } void gen_expr_stmt(CODEGEN* g, AST* ast) { diff --git a/tests/012.sh b/tests/012.sh new file mode 100644 index 0000000..279e3c2 --- /dev/null +++ b/tests/012.sh @@ -0,0 +1,17 @@ +set -e + +bash ../../test_exit_code.sh 25 <<'EOF' +int main() { + int i; + int ret; + i = 0; + ret = 0; + for (i = 0; i < 10; i = i + 1) { + if (i % 2 == 0) { + continue; + } + ret = ret + i; + } + return ret; +} +EOF diff --git a/tests/013.sh b/tests/013.sh new file mode 100644 index 0000000..8381d0e --- /dev/null +++ b/tests/013.sh @@ -0,0 +1,17 @@ +set -e + +bash ../../test_exit_code.sh 66 <<'EOF' +int main() { + int i; + int ret; + i = 0; + ret = 0; + for (i = 0; i < 100; i = i + 1) { + if (i == 12) { + break; + } + ret = ret + i; + } + return ret; +} +EOF -- cgit v1.3-1-g0d28