M3: Ranges (infix <-, inclusive, 4<-1 descends)#34
Merged
Conversation
Add an infix range operator `<-` that materializes an inclusive `[]Num`:
`1 <- 4` -> [1,2,3,4]; `4 <- 1` descends to [4,3,2,1]. It is pure array
sugar — no distinct Range type — so the result composes with `.size`,
indexing, and `for` loops.
- Lexer: reuses the existing `LeftArrow` token (no new token).
- Parser: a new non-associative `parse_range` precedence level between
comparison and pipeline. The `for` header keeps consuming its own `<-`
in `parse_for_loop`, so `for n <- coll` is unaffected (regression-tested).
- AST: new `Expr::Range { start, end, span }`.
- Type checker: `Num <- Num -> []Num`.
- Codegen: `generate_range` emits the `{ptr, size}` array shape with a
runtime fill loop; element count `|hi - lo| + 1` and direction are decided
at runtime (ends may be dynamic). Backing store is GC-allocated so the
array may safely escape the frame.
Ships `examples/ranges.ql` (wired into the JIT + native-AOT examples gate),
`tests/ranges_test.rs`, parser unit tests, and LANGUAGE.md docs (symbol
table, prose, feature matrix).
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
M3: Ranges via infix
<-Adds an infix range operator
<-that materializes an inclusive[]Num:1 <- 4→[1, 2, 3, 4]4 <- 1→[4, 3, 2, 1](descends when the left end is larger)5 <- 5→[5]It is pure array sugar — there is no distinct Range type. The result is a
[]Num, so it composes with.size, indexing[i], andforloops.Implementation (localized to the new precedence level + a checker arm + range codegen)
LeftArrowtoken — no new token.parse_rangelevel between comparison and pipeline. Theforheader still consumes its own<-inparse_for_loop, sofor n <- collparses exactly as before (regression-tested).Expr::Range { start, end, span }(+span()arm).Num <- Num → []Num.generate_rangeemits the shared{ptr, size}array shape with a runtime fill loop; element count|hi - lo| + 1and ascending/descending direction are decided at runtime (ends may be dynamic). Backing storage is GC-allocated (__alloc) so the array may safely escape the frame.Coexistence
The pre-existing
for n <- collection => bodyloop is unchanged. The infix range only applies in general expression position (between two value expressions); theforheader's<-is parsed separately. Both forms are covered by tests.Tests & docs
examples/ranges.ql(exit code 14), wired intotests/examples_test.rs— runs under both JIT and native AOT (clang + gcc).tests/ranges_test.rs:(1<-4).size == 4, single-point, ascending values, descending order, dynamic ends, range drives aforloop, andfor n <- [...]still runs.<-parses asExpr::Range;forheader still parses asForLoop.LANGUAGE.md: symbol table, prose section, and feature matrix.Gate
cargo build,cargo test(incl. native-AOT examples gate with clang+gcc),cargo fmt --check,cargo clippy --all-targets -- -D warnings— all green. Ran/code-reviewand/simplify; addressed findings (reusedptr_len_struct_type, collapsed a redundant subtraction, trimmed docs/tests). The doc-comment overstatement flagged by review was corrected; numeric-edge findings (f64 truncation, NaN/overflow) were left as-is for consistency with the language's unguarded f64-everywhere 0.9 core.🤖 Generated with Claude Code