ReviewOS

home-lang/home

Parser: Zig-style 'for (slice) |item|' loop syntax (2 kernel files)

#53
Closed glennmichael123 opened this 24 days ago · 0 comments
24 days ago

Problem

The parser accepts Rust-style for IDENT in EXPR { ... } but NOT the Zig-style for (EXPR) |IDENT| { ... }. Today this fails:

for (pronouns) |p| {
    if (strings_equal(lower, p)) { return true; }
}

for (sev_str) |c| {
    if (pos < buffer.len) buffer[pos] = c;
    pos += 1;
}

Diagnostic: error[EP0001]: Expected 'in' after iterator variable at the ) closing the iterable expression — the parser reads for ( sev_str as for IDENT and demands in next.

Affected kernel files

  • kernel/src/fs/fsck.home:176 (for (sev_str) |c| { ... })
  • kernel/src/iot/conversation.home:381 (for (pronouns) |p| { ... })

Expected behavior

Both forms should parse and produce equivalent for-loop AST:

  • Rust-style: for item in iter { ... } (already works)
  • Zig-style: for (iter) |item| { ... } (new)
  • Zig-style with index: for (iter, 0..) |item, idx| { ... }

The kernel uses both styles; standardize on supporting both rather than forcing kernel-side cleanup.

Implementation hint

In the for-statement parser, peek for ( immediately after for to disambiguate:

  • for ( → expect Zig-style: parse iterable expr, ), |, capture identifier(s), |, body block
  • for IDENT → expect Rust-style: capture identifier, in, iterable expr, body block

Both lower to the same ForLoop AST node — just bind the variable from different syntactic slots.

Tests

Positive:

  • for (slice) |item| { ... }
  • for (slice, 0..) |item, idx| { ... }
  • for item in slice { ... } (regression — must still work)

Acceptance

  1. The 2 listed kernel files clear home-debug ast past the for-loop point.
  2. Existing test suite green.

Sign in to comment on this issue.