ReviewOS

home-lang/home

Parser: multi-pattern switch arms '.A, .B, .C => expr' (18 sites in 5 kernel files)

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

Problem

The parser does not accept multi-pattern switch arms (comma-separated patterns sharing a single body):

switch (x) {
    .A, .B, .C => return 1,
    else => return 0,
}

Today fails with error[EP0001]: Expected semicolon or newline between statements at the comma between patterns.

Background

Zig and Rust both support this — it's the canonical "fall-through" alternative to C-style implicit fallthrough. Each comma-separated pattern is treated as an independent arm with the same body, which is far more concise than duplicating the body.

Scope

$ grep -rEn "\.[A-Z][a-zA-Z0-9_]*, \.[A-Z]" home-os/kernel/src --include='*.home' | wc -l
18
$ grep -rlEn "\.[A-Z][a-zA-Z0-9_]*, \.[A-Z]" home-os/kernel/src --include='*.home' | wc -l
5 files

Affected kernel files (sample)

  • kernel/src/iot/conversation.home:482 (.Device, .DeviceType => return ...)
  • kernel/src/iot/ambient_sound.home:113 (.SmokeAlarm, .COAlarm, .FireAlarm, ... => .Critical)
  • (3 more)

Expected behavior

Parse comma-separated patterns as a single arm:

switch (x) {
    .A, .B, .C => body,
    .D => other_body,
    else => fallback,
}

Equivalent semantics: any of .A, .B, .C matches → execute body. Same as desugar:

switch (x) {
    .A => body,
    .B => body,
    .C => body,
    .D => other_body,
    else => fallback,
}

Both switch and match should accept this form. Both statement-form and expression-form (issue #45).

Implementation hint

In switch/match-arm parsing, after consuming the first pattern, peek for ,. If next is , AND the token after that looks like another pattern (not =>, not arm body), consume , and parse another pattern, repeating. Once pattern list is collected, expect =>, parse body. Lower to multiple arms with shared body (codegen handles deduplication or just emits the body for each).

Tests

Positive:

  • Two-pattern arm: switch (x) { .A, .B => 1, else => 0 }
  • Multi-pattern arm: switch (x) { .A, .B, .C, .D => 1, else => 0 }
  • Mixed: switch (x) { .A, .B => 1, .C => 2, else => 0 }
  • Expression-form: let v = switch (x) { .A, .B => 1, else => 0 }

Regression:

  • Single-pattern arms keep working

Acceptance

  1. The 5 listed kernel files clear home-debug ast past the multi-pattern arm.
  2. Existing test suite green.

Sign in to comment on this issue.