Files
hakorune/docs/reference/language/match-guards.md

41 lines
1.6 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# Match Guards — Syntax and Lowering (MVP + Design Notes)
Status: reference + design additions during featurepause (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):
```nyash
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 the featurepause.