diff options
| author | hachem <im@hachem.wtf> | 2026-09-08 12:16:51 +0200 |
|---|---|---|
| committer | hachem <im@hachem.wtf> | 2026-09-08 12:16:51 +0200 |
| commit | 60c0e084b159801a45570b56f152f0f5e9193d72 (patch) | |
| tree | ad6c72d6782ab5d9edff336e1e852ca7b995bcdc /src/parser.c | |
| parent | 9d47486aac9cd1a4422b78ee273b7a27bf7b733a (diff) | |
feat: add while loops
Diffstat (limited to 'src/parser.c')
| -rw-r--r-- | src/parser.c | 46 |
1 files changed, 45 insertions, 1 deletions
diff --git a/src/parser.c b/src/parser.c index 4c01c86..ddaee72 100644 --- a/src/parser.c +++ b/src/parser.c @@ -481,7 +481,8 @@ error: return false; } -static bool parse_if(struct Parser* parser, struct Statement* out) +static bool parse_condition(struct Parser* parser, struct Expr** out_left, + struct Token* out_comparison, struct Expr** out_right) { struct Expr* left = parse_expression(parser); if (left == NULL) @@ -503,6 +504,20 @@ static bool parse_if(struct Parser* parser, struct Statement* out) return false; } + *out_left = left; + *out_comparison = comparison; + *out_right = right; + return true; +} + +static bool parse_if(struct Parser* parser, struct Statement* out) +{ + struct Expr* left; + struct Token comparison; + struct Expr* right; + if (!parse_condition(parser, &left, &comparison, &right)) + return false; + struct Statement* body; size_t body_count; if (!parse_block(parser, &body, &body_count)) @@ -535,11 +550,40 @@ static bool parse_if(struct Parser* parser, struct Statement* out) return true; } +static bool parse_while(struct Parser* parser, struct Statement* out) +{ + struct Expr* left; + struct Token comparison; + struct Expr* right; + if (!parse_condition(parser, &left, &comparison, &right)) + return false; + + struct Statement* body; + size_t body_count; + if (!parse_block(parser, &body, &body_count)) + { + free_expr(left); + free_expr(right); + return false; + } + + out->kind = STATEMENT_WHILE; + out->loop.left = left; + out->loop.comparison = comparison; + out->loop.right = right; + out->loop.body = body; + out->loop.body_count = body_count; + return true; +} + static bool parse_statement(struct Parser* parser, struct Statement* out) { if (match_token(parser, TOKEN_IF)) return parse_if(parser, out); + if (match_token(parser, TOKEN_WHILE)) + return parse_while(parser, out); + if (match_token(parser, TOKEN_SYSCALL)) { out->kind = STATEMENT_SYSCALL; |
