ReviewOS

home-lang/home

Parser: brace-less single-statement 'if' (189 kernel sites)

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

Problem

The parser requires { ... } after an if (cond) and rejects single-statement form. Today this fails with Expected '{':

if (region_count >= 64) return;
if (pid >= MAX_PROCESSES) return 0
if (journal.head == 0) journal.head = 1

Scope

A grep over home-os/kernel/src shows 189 brace-less if sites across many files. They're written deliberately in this terse style (early-return guards, single assignments). Bracing all of them is a 189-edit codemod with mechanical risk; the better fix is to allow the single-statement form in the parser.

Expected behavior

After if (cond), accept either:

  • { ... block ... } (existing form — unchanged)
  • A single statement followed by an optional else clause (also single-statement or brace block)

Examples that should parse:

if (cond) return 0;
if (cond) x = y;
if (cond) foo();
if (cond) doA() else doB();
if (cond) return 0 else return 1;

Disambiguation rule

The classic dangling-else hazard: if (a) if (b) x else y — bind else to the innermost if (matches C/Zig/Rust/JS convention). Document this in the parser comment for future maintainers.

Notes

  • This does NOT change if-as-expression (const x = if (c) a else b) — that's a separate construct, tracked separately.
  • Statement-form match/switch still requires braces around the arm list — only if is affected.
  • The 9-file bucket from the 2026-05-07 audit ("Expected '{'" / "Expected '{' after if condition") closes once this lands; an additional ~180 sites that the parser hasn't yet reached (because it bails on the first one in each file) also unblock.

Blocked kernel files (immediately visible to audit)

  • kernel/src/fs/extents.home:49 (also requires if-as-expression — separate fix)
  • kernel/src/fs/journal.home:567
  • kernel/src/perf/baseline.home:102
  • kernel/src/drivers/pci.home:93 (separate root cause — see #17)
  • kernel/src/security/secure_boot.home:111
  • kernel/src/security/hardened_usercopy.home:104
  • kernel/src/security/capabilities.home:111
  • kernel/src/sched/rt_classes.home:416
  • kernel/src/iot/audio_processing.home:336 (also if-expression)
  • kernel/src/iot/voice_routines.home:337 (different shape — ?[]const u8 return type)
  • kernel/src/arch/arm64/rpi/gpio.home:353 (covered by #45 — match-as-expression)
  • kernel/src/drivers/platform/laptop.home:92 (different shape — try keyword)

Implementation hint

In parseIfStatement (or wherever parseStatement dispatches Tok.If), after the parenthesized condition, peek the next token. If it's {, parse a brace block as today. Otherwise, parse a single statement (no surrounding braces) and synthesize a single-element block AST node so downstream codegen sees the same shape.

For the else branch, apply the same peek: brace block OR single statement OR another if (chained else if).

Tests to add

Positive:

  • if (c) return 0;
  • if (c) x = 1; else x = 2;
  • if (a) if (b) c() else d(); — else binds to inner if
  • if (c) foo() else if (d) bar() else baz();

Regression (must still work):

  • if (c) { ... }
  • if (c) { ... } else { ... }
  • if (c) { ... } else if (d) { ... } else { ... }

Acceptance

  1. The 8 listed kernel files clear home-debug ast for the immediate failure point (some still have downstream different-shape failures).
  2. Existing 1106+ test suite green.
  3. Diagnostic snapshots for the new positive cases.

Sign in to comment on this issue.