diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2024-02-25 16:02:36 -0500 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2024-02-25 16:02:36 -0500 |
| commit | 741617a17e9b318d58e2c73868396280d6a583db (patch) | |
| tree | d96fb6689fcd47cf11dd14c9c700a8bd01e960fc /parse.c | |
| parent | 70f7f15781c4e8000dbcc927984c3198f92ba15e (diff) | |
Add for-else block
Diffstat (limited to 'parse.c')
| -rw-r--r-- | parse.c | 11 |
1 files changed, 10 insertions, 1 deletions
@@ -847,6 +847,7 @@ PARSER(parse_for) { // for [k,] v in iter [<indent>] body const char *start = pos; if (!match_word(&pos, "for")) return NULL; + int64_t starting_indent = get_indent(ctx->file, pos); ast_t *index = expect(ctx, start, &pos, parse_var, "I expected an iteration variable for this 'for'"); spaces(&pos); ast_t *value = NULL; @@ -857,7 +858,15 @@ PARSER(parse_for) { ast_t *iter = expect(ctx, start, &pos, parse_expr, "I expected an iterable value for this 'for'"); match(&pos, "do"); // optional ast_t *body = expect(ctx, start, &pos, parse_opt_indented_block, "I expected a body for this 'for'"); - return NewAST(ctx->file, start, pos, For, .index=value ? index : NULL, .value=value ? value : index, .iter=iter, .body=body); + + const char *else_start = pos; + whitespace(&else_start); + ast_t *empty = NULL; + if (match_word(&else_start, "else") && get_indent(ctx->file, else_start) == starting_indent) { + pos = else_start; + empty = expect(ctx, pos, &pos, parse_opt_indented_block, "I expected a body for this 'else'"); + } + return NewAST(ctx->file, start, pos, For, .index=value ? index : NULL, .value=value ? value : index, .iter=iter, .body=body, .empty=empty); } PARSER(parse_while) { |
