ReviewOS

home-lang/home

Parser: Zig while-with-continue-expression 'while (cond) : (expr) { ... }' (86 kernel sites in 13 files)

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

Problem

The parser does not accept Zig's while (cond) : (continue_expr) { body } syntax. Today this fails with error[EP0001]: Expected '{' at the : separator:

while (i < n) : (i += 1) {
    foo(i)
}

The : (expr) is Zig's continue expression — evaluated after each iteration of the body, before the condition is re-checked. It's the canonical idiom for index-incrementing loops in Zig.

Scope

Kernel-wide: 86 sites across 13 files use this idiom — it's the dominant pattern for indexed iteration where Zig's for doesn't fit (e.g., manual index control, mutable counter shared with body).

Affected kernel files (sample)

  • kernel/src/security/hardened_usercopy.home:137 (while (i < region_count) : (i += 1))
  • kernel/src/security/secure_boot.home:129
  • kernel/src/security/dm_verity.home:192
  • kernel/src/security/keyring.home
  • kernel/src/security/module_signing.home
  • kernel/src/fs/btree_dir.home:244
  • kernel/src/fs/largefile.home:373
  • ...13 files total

Expected behavior

After parsing the parenthesized condition, peek for :. If present, consume it, parse the parenthesized continue-expression, then parse the body block. Lower to a while AST node with condition, continue_expr, and body slots — codegen evaluates the continue-expr after each body iteration, before the next condition test.

while (cond)                    { body }   // existing — no continue expr
while (cond) : (continue_expr)  { body }   // new — continue expr

Implementation hint

In parseWhileStatement (or wherever Tok.While dispatches): after the condition's closing ), peek for :. If next is :, consume :, expect (, parse the continue-expression (a single expression — typically i += 1), expect ), then continue to body parsing as today.

The codegen lowering: while (cond) : (cexpr) { body } is equivalent to:

while (cond) {
    body
    cexpr
}

Except continue statements still execute cexpr before re-checking cond — so a naive lowering needs care if continue is used in body. Cleanest: lower with explicit cexpr evaluation hooked into the continue-target jump.

Tests

Positive:

  • while (i < n) : (i += 1) { foo(i) } — basic
  • while (cond) : (i += 2) { ... } — non-trivial continue-expr
  • Nested: while (i < m) : (i += 1) { while (j < n) : (j += 1) { ... } }
  • With continue in body — verify cexpr still runs

Regression:

  • Existing while (cond) { body } (no :) parses identically.

Acceptance

  1. The 13 listed kernel files clear home-debug ast past the while ... : ... line.
  2. Existing test suite green.

Sign in to comment on this issue.