lang: multi-iterable for loops — drop ':', add '..=', open ranges, arrow bodies
The for header is now a comma-separated list of iterables with a
positional capture group and no ':' separator:
for xs (x) { } // collection
for 0..n (i) { } // range (end exclusive)
for 1..=5 (a) { } // ..= inclusive end
for xs, 0.. (x, i) { } // index idiom (replaces (x, i))
for xs, ys (x, y) { } // parallel (zip) iteration
for xs (x) => sum += x; // arrow body (full statement)
First-iterable-wins: the first iterable's length drives the loop and
must be bounded; the other positions follow by their own cursors (a
non-first range's end is not consulted or evaluated; a shorter
non-first collection is read past its length on mismatch). The old
single-iterable index capture is replaced by the trailing open range.
Capture/call disambiguation is positional: the paren group immediately
before '{' or '=>' is the capture, every earlier top-level group is a
call. 'for zip(a, b) (x, y)' calls zip; 'for f(n) { }' reads (n) as
the capture and errors with a parenthesize/add-capture hint. The old
':' form errors with a migration hint.
Lowering is unified across forms: one cursor slot per position (ranges
start at their start, collections at 0), all advanced together, the
first position's bound terminating. inline for keeps the single
bounded comptime range.
Migrated the full corpus (examples, library modules, issue repros,
in-source test strings). New coverage: examples/0050 (the full feature
surface) and examples/1149-1155 (seven diagnostic faces). specs.md For
Loop section + grammar rewritten; readme teaser updated.
This commit is contained in:
@@ -118,7 +118,7 @@ main :: () {
|
||||
// For loop basic
|
||||
farr : [4]s32 = .[10, 20, 30, 40];
|
||||
out("for:");
|
||||
for farr: (it) {
|
||||
for farr (it) {
|
||||
out(" ");
|
||||
out(int_to_string(it));
|
||||
}
|
||||
@@ -126,14 +126,14 @@ main :: () {
|
||||
|
||||
// For with print
|
||||
out("for-print:");
|
||||
for farr: (it) {
|
||||
for farr (it) {
|
||||
print(" {}", it);
|
||||
}
|
||||
out("\n");
|
||||
|
||||
// For with index
|
||||
out("for-idx:");
|
||||
for farr: (_, ix) {
|
||||
for farr, 0.. (_, ix) {
|
||||
out(" ");
|
||||
out(int_to_string(ix));
|
||||
}
|
||||
@@ -141,14 +141,14 @@ main :: () {
|
||||
|
||||
// For with print two args
|
||||
out("for-2arg:");
|
||||
for farr: (it, ix) {
|
||||
for farr, 0.. (it, ix) {
|
||||
print(" {}@{}", it, ix);
|
||||
}
|
||||
out("\n");
|
||||
|
||||
// For with break
|
||||
out("for-break:");
|
||||
for farr: (it) {
|
||||
for farr (it) {
|
||||
if it == 30 { break; }
|
||||
print(" {}", it);
|
||||
}
|
||||
@@ -156,7 +156,7 @@ main :: () {
|
||||
|
||||
// For with continue
|
||||
out("for-continue:");
|
||||
for farr: (it) {
|
||||
for farr (it) {
|
||||
if it == 20 { continue; }
|
||||
print(" {}", it);
|
||||
}
|
||||
@@ -165,14 +165,14 @@ main :: () {
|
||||
// For on slice
|
||||
fsl : []s32 = .[10, 20, 30];
|
||||
out("for-slice:");
|
||||
for fsl: (it) {
|
||||
for fsl (it) {
|
||||
print(" {}", it);
|
||||
}
|
||||
out("\n");
|
||||
|
||||
// For on slice with index
|
||||
out("for-slice-idx:");
|
||||
for fsl: (it, ix) {
|
||||
for fsl, 0.. (it, ix) {
|
||||
print(" {}:{}", ix, it);
|
||||
}
|
||||
out("\n");
|
||||
@@ -181,8 +181,8 @@ main :: () {
|
||||
nf_a : [2]s32 = .[0, 1];
|
||||
nf_b : [2]s32 = .[0, 1];
|
||||
out("for-nested:");
|
||||
for nf_a: (oa) {
|
||||
for nf_b: (ob) {
|
||||
for nf_a (oa) {
|
||||
for nf_b (ob) {
|
||||
print(" ({},{})", oa, ob);
|
||||
}
|
||||
}
|
||||
@@ -191,7 +191,7 @@ main :: () {
|
||||
// For with break preserving index
|
||||
fbi : [5]s32 = .[10, 20, 30, 40, 50];
|
||||
fbi_idx := 0;
|
||||
for fbi: (it, ix) {
|
||||
for fbi, 0.. (it, ix) {
|
||||
if it == 30 { fbi_idx = ix; break; }
|
||||
}
|
||||
print("for-break-idx: {}\n", fbi_idx);
|
||||
|
||||
Reference in New Issue
Block a user