From 60c0e084b159801a45570b56f152f0f5e9193d72 Mon Sep 17 00:00:00 2001 From: hachem Date: Tue, 8 Sep 2026 12:16:51 +0200 Subject: feat: add while loops --- src/parser.c | 46 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) (limited to 'src/parser.c') 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; -- cgit v1.3