From 5670654667355c55104386a199a78381e9dad207 Mon Sep 17 00:00:00 2001 From: Yaossg Date: Sun, 17 Nov 2024 10:52:27 +0800 Subject: [PATCH] do-while --- README.md | 2 +- boot.c | 22 +++++++++++++++++++++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 9ceab42..649122e 100644 --- a/README.md +++ b/README.md @@ -66,7 +66,7 @@ $ sh boot.sh ### 支持的流程控制 - `if` `else` -- `while` `for` +- `while` `for` `do` - `break` `continue` - `return` diff --git a/boot.c b/boot.c index 1c53544..8225237 100644 --- a/boot.c +++ b/boot.c @@ -68,6 +68,7 @@ const int TOKEN_VOID = 109; const int TOKEN_CONST = 110; const int TOKEN_CHAR = 111; const int TOKEN_FOR = 112; +const int TOKEN_DO = 113; const int TOKEN_STRING = 150; int parse_int(int ch) { @@ -193,6 +194,8 @@ void parse_id_like(int ch) { token_type = TOKEN_CHAR; } else if (streq(id, "for")) { token_type = TOKEN_FOR; + } else if (streq(id, "do")) { + token_type = TOKEN_DO; } if (token_type != TOKEN_ID) { rewind_id(0); @@ -249,7 +252,7 @@ void next_token() { } else if (ch == '/') { int ch2 = getchar(); if (ch2 == '/') { - while ((ch = getchar()) != -1 && ch != '\n'); + do ch = getchar(); while (ch != -1 && ch != '\n'); next_token(); return; } else if (ch2 == '*') { @@ -1174,6 +1177,21 @@ void parse_for() { asm_pop_label(); } +void parse_do_while() { + int cont_label = next_label(); + int break_label = next_label(); + asm_push_label(break_label, cont_label); + asm_label(cont_label); + parse_stmt(); // body + expect_token(TOKEN_WHILE); + expect_token(TOKEN_PAREN_LEFT); + int cond = parse_expr(); + asm_bnez(cond, cont_label); + expect_token(TOKEN_PAREN_RIGHT); + asm_label(break_label); + asm_pop_label(); +} + void parse_stmt() { next_token(); int decl_type; @@ -1186,6 +1204,8 @@ void parse_stmt() { } else if (token_type == TOKEN_FOR) { parse_for(); return; + } else if (token_type == TOKEN_DO) { + parse_do_while(); } else if (token_type == TOKEN_BRACE_LEFT) { while (1) { next_token();