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:
35
src/sema.zig
35
src/sema.zig
@@ -1186,24 +1186,24 @@ pub const Analyzer = struct {
|
||||
}
|
||||
},
|
||||
.for_expr => |fe| {
|
||||
try self.analyzeNode(fe.iterable);
|
||||
for (fe.iterables) |it| {
|
||||
try self.analyzeNode(it.expr);
|
||||
if (it.range_end) |re| try self.analyzeNode(re);
|
||||
}
|
||||
try self.pushScope();
|
||||
if (!std.mem.eql(u8, fe.capture_name, "_")) {
|
||||
for (fe.captures, 0..) |cap, i| {
|
||||
if (std.mem.eql(u8, cap.name, "_")) continue;
|
||||
const it = fe.iterables[i];
|
||||
var cap_ty: ?Type = null;
|
||||
if (fe.range_end != null) {
|
||||
if (it.is_range) {
|
||||
cap_ty = .{ .signed = 64 };
|
||||
} else if (self.elementTypeOf(self.inferExprType(fe.iterable))) |elem| {
|
||||
cap_ty = if (fe.capture_by_ref)
|
||||
} else if (self.elementTypeOf(self.inferExprType(it.expr))) |elem| {
|
||||
cap_ty = if (cap.by_ref)
|
||||
(if (elem.toName()) |en| Type{ .pointer_type = .{ .pointee_name = en, .is_raw = innerNameIsRaw(elem) } } else elem)
|
||||
else
|
||||
elem;
|
||||
}
|
||||
try self.addSymbol(fe.capture_name, .variable, cap_ty, node.span);
|
||||
}
|
||||
if (fe.index_name) |idx_name| {
|
||||
if (!std.mem.eql(u8, idx_name, "_")) {
|
||||
try self.addSymbol(idx_name, .variable, .{ .signed = 64 }, node.span);
|
||||
}
|
||||
try self.addSymbol(cap.name, .variable, cap_ty, node.span);
|
||||
}
|
||||
try self.analyzeNode(fe.body);
|
||||
self.popScope();
|
||||
@@ -1680,7 +1680,12 @@ pub fn findNodeAtOffset(node: *Node, offset: u32) ?*Node {
|
||||
if (findNodeAtOffset(we.body, offset)) |found| return found;
|
||||
},
|
||||
.for_expr => |fe| {
|
||||
if (findNodeAtOffset(fe.iterable, offset)) |found| return found;
|
||||
for (fe.iterables) |it| {
|
||||
if (findNodeAtOffset(it.expr, offset)) |found| return found;
|
||||
if (it.range_end) |re| {
|
||||
if (findNodeAtOffset(re, offset)) |found| return found;
|
||||
}
|
||||
}
|
||||
if (findNodeAtOffset(fe.body, offset)) |found| return found;
|
||||
},
|
||||
.spread_expr => |se| {
|
||||
@@ -2242,9 +2247,9 @@ test "sema: for-loop captures resolve element, by-ref pointer, and range cursor"
|
||||
"List :: struct ($T: Type) { items: [*]T = null; len: s64 = 0; }" ++
|
||||
"Game :: struct { legal: List(Move);" ++
|
||||
" scan :: (self: *Game) {" ++
|
||||
" for self.legal: (m) { a := m.flag; }" ++
|
||||
" for self.legal: (*p) { b := p.flag; }" ++
|
||||
" for 0..10: (i) { c := i; }" ++
|
||||
" for self.legal (m) { a := m.flag; }" ++
|
||||
" for self.legal (*p) { b := p.flag; }" ++
|
||||
" for 0..10 (i) { c := i; }" ++
|
||||
" } }";
|
||||
var parser = parser_mod.Parser.init(alloc, source);
|
||||
const root = try parser.parse();
|
||||
|
||||
Reference in New Issue
Block a user