diff options
| author | Zuhaitz Méndez Fernández de Aránguiz <zuhaitz@debian> | 2026-01-18 01:54:52 +0000 |
|---|---|---|
| committer | Zuhaitz Méndez Fernández de Aránguiz <zuhaitz@debian> | 2026-01-18 01:54:52 +0000 |
| commit | 1eed9181e082883987116224a5043b8b64a0ec95 (patch) | |
| tree | 75b57956dd7c565886c613ae49e1b181caa6b661 /src | |
| parent | efb6cda22ec9ca124c22b40d1b0049c3992bbf32 (diff) | |
Support for '..='
Diffstat (limited to 'src')
| -rw-r--r-- | src/ast/ast.h | 1 | ||||
| -rw-r--r-- | src/codegen/codegen.c | 9 | ||||
| -rw-r--r-- | src/lexer/token.c | 12 | ||||
| -rw-r--r-- | src/parser/parser_stmt.c | 12 | ||||
| -rw-r--r-- | src/zprep.h | 1 |
5 files changed, 32 insertions, 3 deletions
diff --git a/src/ast/ast.h b/src/ast/ast.h index a528129..2288860 100644 --- a/src/ast/ast.h +++ b/src/ast/ast.h @@ -248,6 +248,7 @@ struct ASTNode ASTNode *start; ASTNode *end; char *step; + int is_inclusive; ASTNode *body; } for_range; diff --git a/src/codegen/codegen.c b/src/codegen/codegen.c index f4f8914..9539338 100644 --- a/src/codegen/codegen.c +++ b/src/codegen/codegen.c @@ -1839,7 +1839,14 @@ void codegen_node_single(ParserContext *ctx, ASTNode *node, FILE *out) fprintf(out, "ZC_AUTO %s = ", node->for_range.var_name); } codegen_expression(ctx, node->for_range.start, out); - fprintf(out, "; %s < ", node->for_range.var_name); + if (node->for_range.is_inclusive) + { + fprintf(out, "; %s <= ", node->for_range.var_name); + } + else + { + fprintf(out, "; %s < ", node->for_range.var_name); + } codegen_expression(ctx, node->for_range.end, out); fprintf(out, "; %s", node->for_range.var_name); if (node->for_range.step) diff --git a/src/lexer/token.c b/src/lexer/token.c index eec7b87..9cc29a6 100644 --- a/src/lexer/token.c +++ b/src/lexer/token.c @@ -356,8 +356,16 @@ Token lexer_next(Lexer *l) } else if (s[0] == '.' && s[1] == '.') { - len = 2; - type = TOK_DOTDOT; + if (s[2] == '=') + { + len = 3; + type = TOK_DOTDOT_EQ; + } + else + { + len = 2; + type = TOK_DOTDOT; + } } else if ((s[0] == '-' && s[1] == '>') || (s[0] == '=' && s[1] == '>')) { diff --git a/src/parser/parser_stmt.c b/src/parser/parser_stmt.c index ca8cd8f..bfb45d8 100644 --- a/src/parser/parser_stmt.c +++ b/src/parser/parser_stmt.c @@ -1555,9 +1555,20 @@ ASTNode *parse_for(ParserContext *ctx, Lexer *l) if (in_tok.type == TOK_IDENT && strncmp(in_tok.start, "in", 2) == 0) { ASTNode *start_expr = parse_expression(ctx, l); + int is_inclusive = 0; if (lexer_peek(l).type == TOK_DOTDOT) { lexer_next(l); // consume .. + } + + else if (lexer_peek(l).type == TOK_DOTDOT_EQ) + { + is_inclusive = 1; + lexer_next(l); // consume ..= + } + + if (1) // Block to keep scope for variables + { ASTNode *end_expr = parse_expression(ctx, l); ASTNode *n = ast_create(NODE_FOR_RANGE); @@ -1566,6 +1577,7 @@ ASTNode *parse_for(ParserContext *ctx, Lexer *l) n->for_range.var_name[var.len] = 0; n->for_range.start = start_expr; n->for_range.end = end_expr; + n->for_range.is_inclusive = is_inclusive; if (lexer_peek(l).type == TOK_IDENT && strncmp(lexer_peek(l).start, "step", 4) == 0) { diff --git a/src/zprep.h b/src/zprep.h index 8a4ba3e..a242548 100644 --- a/src/zprep.h +++ b/src/zprep.h @@ -52,6 +52,7 @@ typedef enum TOK_OP, TOK_AT, TOK_DOTDOT, + TOK_DOTDOT_EQ, TOK_ARROW, TOK_PIPE, TOK_TEST, |
