diff options
| author | nsfisis <nsfisis@gmail.com> | 2025-05-09 22:28:20 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2025-05-09 22:28:20 +0900 |
| commit | 3b0f4c321ac99de44c09275dda74f6bfb7e59e05 (patch) | |
| tree | 4ebc21bfad408d79b95c55f7c0911b7a95dc1f83 | |
| parent | 0f67c605a059f89f0a43d1dc926c158bed28b8d7 (diff) | |
| download | ducc-3b0f4c321ac99de44c09275dda74f6bfb7e59e05.tar.gz ducc-3b0f4c321ac99de44c09275dda74f6bfb7e59e05.tar.zst ducc-3b0f4c321ac99de44c09275dda74f6bfb7e59e05.zip | |
implement do-while loop
| -rw-r--r-- | main.c | 41 | ||||
| -rw-r--r-- | tests/047.sh | 93 |
2 files changed, 134 insertions, 0 deletions
@@ -75,6 +75,7 @@ enum TokenKind { TokenKind_keyword_char, TokenKind_keyword_const, TokenKind_keyword_continue, + TokenKind_keyword_do, TokenKind_keyword_else, TokenKind_keyword_enum, TokenKind_keyword_for, @@ -301,6 +302,8 @@ void tokenize_all(Lexer* l) { tok->kind = TokenKind_keyword_const; } else if (ident_len == 8 && strstr(l->src + start, "continue") == l->src + start) { tok->kind = TokenKind_keyword_continue; + } else if (ident_len == 2 && strstr(l->src + start, "do") == l->src + start) { + tok->kind = TokenKind_keyword_do; } else if (ident_len == 4 && strstr(l->src + start, "else") == l->src + start) { tok->kind = TokenKind_keyword_else; } else if (ident_len == 4 && strstr(l->src + start, "enum") == l->src + start) { @@ -481,6 +484,7 @@ enum AstNodeKind { AstNodeKind_break_stmt, AstNodeKind_continue_stmt, AstNodeKind_deref_expr, + AstNodeKind_do_while_stmt, AstNodeKind_enum_def, AstNodeKind_enum_member, AstNodeKind_expr_stmt, @@ -1336,6 +1340,21 @@ AstNode* parse_while_stmt(Parser* p) { return stmt; } +AstNode* parse_do_while_stmt(Parser* p) { + expect(p, TokenKind_keyword_do); + AstNode* body = parse_stmt(p); + expect(p, TokenKind_keyword_while); + expect(p, TokenKind_paren_l); + AstNode* cond = parse_expr(p); + expect(p, TokenKind_paren_r); + expect(p, TokenKind_semicolon); + + AstNode* stmt = ast_new(AstNodeKind_do_while_stmt); + stmt->node_cond = cond; + stmt->node_body = body; + return stmt; +} + AstNode* parse_break_stmt(Parser* p) { expect(p, TokenKind_keyword_break); expect(p, TokenKind_semicolon); @@ -1415,6 +1434,8 @@ AstNode* parse_stmt(Parser* p) { return parse_for_stmt(p); } else if (t->kind == TokenKind_keyword_while) { return parse_while_stmt(p); + } else if (t->kind == TokenKind_keyword_do) { + return parse_do_while_stmt(p); } else if (t->kind == TokenKind_keyword_break) { return parse_break_stmt(p); } else if (t->kind == TokenKind_keyword_continue) { @@ -1992,6 +2013,24 @@ void codegen_for_stmt(CodeGen* g, AstNode* ast) { --g->loop_labels; } +void codegen_do_while_stmt(CodeGen* g, AstNode* ast) { + int label = codegen_new_label(g); + ++g->loop_labels; + *g->loop_labels = label; + + printf(".Lbegin%d:\n", label); + codegen_stmt(g, ast->node_body); + printf(".Lcontinue%d:\n", label); + codegen_expr(g, ast->node_cond, GenMode_rval); + printf(" pop rax\n"); + printf(" cmp rax, 0\n"); + printf(" je .Lend%d\n", label); + printf(" jmp .Lbegin%d\n", label); + printf(".Lend%d:\n", label); + + --g->loop_labels; +} + void codegen_break_stmt(CodeGen* g, AstNode* ast) { int label = *g->loop_labels; printf(" jmp .Lend%d\n", label); @@ -2027,6 +2066,8 @@ void codegen_stmt(CodeGen* g, AstNode* ast) { codegen_if_stmt(g, ast); } else if (ast->kind == AstNodeKind_for_stmt) { codegen_for_stmt(g, ast); + } else if (ast->kind == AstNodeKind_do_while_stmt) { + codegen_do_while_stmt(g, ast); } else if (ast->kind == AstNodeKind_break_stmt) { codegen_break_stmt(g, ast); } else if (ast->kind == AstNodeKind_continue_stmt) { diff --git a/tests/047.sh b/tests/047.sh new file mode 100644 index 0000000..f50ee69 --- /dev/null +++ b/tests/047.sh @@ -0,0 +1,93 @@ +set -e + +cat <<'EOF' > expected +body 0 +foo 1 +body 1 +foo 2 +body 2 +foo 3 +body 3 +foo 4 +body 4 +foo 5 +EOF +bash ../../test_diff.sh <<'EOF' +int printf(); + +int foo(int i) { + printf("foo %d\n", i); + return i; +} + +int main() { + int i = 0; + do { + printf("body %d\n", i); + ++i; + } while (foo(i) < 5); + + return 0; +} +EOF + +cat <<'EOF' > expected +body 0 +foo 1 +body 1 +foo 2 +body 2 +EOF +bash ../../test_diff.sh <<'EOF' +int printf(); + +int foo(int i) { + printf("foo %d\n", i); + return i; +} + +int main() { + int i = 0; + do { + printf("body %d\n", i); + ++i; + if (i == 3) { + break; + } + } while (foo(i) < 5); + + return 0; +} +EOF + +cat <<'EOF' > expected +body 1 +foo 1 +foo 2 +body 3 +foo 3 +foo 4 +body 5 +foo 5 +EOF +bash ../../test_diff.sh <<'EOF' +int printf(); + +int foo(int i) { + printf("foo %d\n", i); + return i; +} + +int main() { + int i = 0; + do { + ++i; + if (i % 2 == 0) { + continue; + } + printf("body %d\n", i); + } while (foo(i) < 5); + + return 0; +} +EOF |
