Files
hakorune/docs/reference/language/match-guards.md
Selfhosting Dev c8063c9e41 pyvm: split op handlers into ops_core/ops_box/ops_ctrl; add ops_flow + intrinsic; delegate vm.py without behavior change
net-plugin: modularize constants (consts.rs) and sockets (sockets.rs); remove legacy commented socket code; fix unused imports
mir: move instruction unit tests to tests/mir_instruction_unit.rs (file lean-up); no semantic changes
runner/pyvm: ensure using pre-strip; misc docs updates

Build: cargo build ok; legacy cfg warnings remain as before
2025-09-21 08:53:00 +09:00

1.6 KiB

Match Guards — Syntax and Lowering (MVP + Design Notes)

Status: reference + design additions during freeze (no implementation changes)

Scope

  • Guarded branches as a readable form of first-match selection.
  • Canonical lowering target: if/else chain + PHI merges.

Syntax (MVP)

  • Guard chain (first-match wins):
    guard <cond> -> { /* then */ }
    guard <cond> -> { /* then */ }
    else         -> { /* else */ }
    
  • Conditions may combine comparisons, is/as type checks, and literals with && / ||.

Lowering

  • Always lowers to a linear if/else chain with early exit on first true guard.
  • Merge points use normal PHI formation invariants (see reference/mir/phi_invariants.md).

Design additions (frozen; docs only)

  • Range Pattern (sugar):
    • guard x in '0'..'9' -> { ... }
    • Lowers to: ('0' <= x && x <= '9').
    • Multiple ranges: in A..B || C..D → OR of each bound check.
  • CharClass (predefined sets):
    • Digit ≡ '0'..'9', AZ ≡ 'A'..'Z', az ≡ 'a'..'z', Alnum ≡ Digit || AZ || az, Space ≡ ' '\t\r\n (MVP set; expandable later).
    • guard ch in Digit -> { ... } expands to range checks.

Errors & Rules (MVP)

  • Default _ branch does not accept guards.
  • Type guard succeeds inside the then-branch; bindings (e.g., StringBox(s)) are introduced at branch head.
  • Short-circuit semantics follow standard branch evaluation (right side is evaluated only if needed).

Observability (design)

  • NYASH_FLOW_TRACE=1 may trace how guard chains desugar into if/else.

Notes

  • This page describes existing guard semantics and adds range/charclass as documentation-only sugar during freeze.