support range-for: for i in lo..hi#200
Merged
Merged
Conversation
counted iteration previously meant a manual mut i := 0; while i < n loop,
or materializing a list with range(). this adds native range syntax that
lowers to a counted loop with no allocation:
for i in 0..n: # exclusive, i in [0, n)
for i in 0..=n: # inclusive, i in [0, n]
bounds may be any Int expression, and the optional value, index binding
works too (the index is the 0-based position). break and continue behave
as in any other loop.
the lexer emits dotdot/dotdoteq tokens; the parser recognizes the operator
in the for-iterable position and builds a range node; the checker requires
Int bounds and binds the loop variable to Int; the ir emitter emits the
counted loop directly. matrix_math.pith is reworked to range-for.
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.
summary
counted iteration previously meant a manual
mut i := 0; while i < nloop ormaterializing a list with
range(). this adds native range syntax that lowersto a counted loop with no allocation:
bounds may be any
Intexpression (for i in 1..n + 1:), the optionalvalue, indexbinding works (the index is the 0-based position), andbreak/
continuebehave as in any other loop.examples/matrix_math.pithis reworked fromwhilecounters to range-for asa worked example (its output snapshot is unchanged).
how
.now lookaheads to emitdotdot(..) anddotdoteq(..=)parse_for_stmtrecognizes the range operator in the iterableposition and builds a
rangenode. range stays scoped to for-iterables fornow, so the rest of the expression grammar is untouched.
Intbounds and binds the loopvariable to
Int(E217otherwise)ir_emit_for_stmtdetects the range node and emits a countedloop directly — init the counter to
lo, comparelt/lteagainsthi,bind the loop variable, step by one — reusing the existing break/continue
label scaffolding.
what was tested
tests/cases/test_range_for.pithcovering exclusive,inclusive, empty (
3..3), expression bounds, break/continue, and thevalue, indexbindinginvalid_range_boundsassertingE217on non-Intbounds
examples via native and self-hosted compilers (83 each), and bootstrap fixed
point — all 0 failures
notes
range is intentionally limited to the for-iterable position in this change.
generalizing it to a first-class lazy value belongs with the iterator-protocol
work, which would also let
.map().filter()fuse without intermediate lists.