From fba2b99b65d5023675a3f270adbc87ef0b0ede8f Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Thu, 23 May 2024 12:40:21 -0400 Subject: Support 'while when' --- parse.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'parse.c') diff --git a/parse.c b/parse.c index 28e91b27..75aff30b 100644 --- a/parse.c +++ b/parse.c @@ -983,10 +983,18 @@ PARSER(parse_while) { // while condition [do] [] body const char *start = pos; if (!match_word(&pos, "while")) return NULL; + + const char *tmp = pos; + // Shorthand form: `while when ...` + if (match_word(&tmp, "when")) { + ast_t *when = expect(ctx, start, &pos, parse_when, "I expected a 'when' block after this"); + 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); + } ast_t *condition = expect(ctx, start, &pos, parse_expr, "I don't see a viable condition for this 'while'"); expect_str(ctx, start, &pos, ":", "I expected a ':' here"); ast_t *body = expect(ctx, start, &pos, parse_opt_indented_block, "I expected a body for this 'while'"); - const char *tmp = pos; + tmp = pos; whitespace(&tmp); return NewAST(ctx->file, start, pos, While, .condition=condition, .body=body); } -- cgit v1.2.3