From f69f862bf6e744834d781867a85f962c783f314e Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Sun, 6 Apr 2025 16:41:58 -0400 Subject: Explicitly support optional `do` for `while` and `for` loops --- src/parse.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/parse.c b/src/parse.c index 848ac340..29432c31 100644 --- a/src/parse.c +++ b/src/parse.c @@ -1041,6 +1041,9 @@ PARSER(parse_for) { expect_str(ctx, start, &pos, "in", "I expected an 'in' for this 'for'"); ast_t *iter = expect(ctx, start, &pos, parse_expr, "I expected an iterable value for this 'for'"); + + (void)match_word(&pos, "do"); // Optional 'do' + ast_t *body = expect(ctx, start, &pos, parse_block, "I expected a body for this 'for'"); const char *else_start = pos; @@ -1055,7 +1058,7 @@ PARSER(parse_for) { } PARSER(parse_do) { - // do: [] body + // do [] body const char *start = pos; if (!match_word(&pos, "do")) return NULL; ast_t *body = expect(ctx, start, &pos, parse_block, "I expected a body for this 'do'"); @@ -1063,7 +1066,7 @@ PARSER(parse_do) { } PARSER(parse_while) { - // while condition: [] body + // while condition ["do"] [] body const char *start = pos; if (!match_word(&pos, "while")) return NULL; @@ -1074,13 +1077,16 @@ PARSER(parse_while) { if (!when->__data.When.else_body) when->__data.When.else_body = NewAST(ctx->file, pos, pos, Stop); return NewAST(ctx->file, start, pos, While, .body=when); } + + (void)match_word(&pos, "do"); // Optional 'do' + ast_t *condition = expect(ctx, start, &pos, parse_expr, "I don't see a viable condition for this 'while'"); ast_t *body = expect(ctx, start, &pos, parse_block, "I expected a body for this 'while'"); return NewAST(ctx->file, start, pos, While, .condition=condition, .body=body); } PARSER(parse_repeat) { - // repeat: [] body + // repeat [] body const char *start = pos; if (!match_word(&pos, "repeat")) return NULL; ast_t *body = expect(ctx, start, &pos, parse_block, "I expected a body for this 'repeat'"); -- cgit v1.2.3