Extend `enum` from named int constants to a tagged union: a variant may
carry a payload (`enum Tile { Empty, Wall, Door(int), Portal(int, int) }`).
Such enums box to a heap record (an i32 tag at offset 0, then one 8-byte
slot per payload position); an all-bare enum keeps its zero-cost compile-
time-ordinal representation, byte-for-byte unchanged (every golden render
and the bootstrap fixpoint still hold).
- Parser: variant payload declarations, stored as N_PARAM kids on the
variant node.
- Construction: by name — `Door(3)`, `Portal(x, y)`, bare `Empty` — resolved
ahead of the function-call fallback and boxed with the payloads coerced to
their declared types.
- match: destructures a tagged scrutinee, switching on the tag and binding
each arm's payload names in a scoped local frame.
- Checking pass: a tagged `match` must be exhaustive (cover every variant or
end in `_`), and constructor/pattern arities and binding forms are checked
— all reported where the scrutinee's type is known.
Adds selfhost/tests/enums.ludic to the regression suite and documents the
feature in LANGUAGE.md and the enum/match pages.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
1.9 KiB
| id | name | category | kind | tokens | sig | tip | order |
|---|---|---|---|---|---|---|---|
| kw-match | match | control | keyword | match | match value { 0, 1 => … _ => … } | Multi-way branch on one value, matching one or more literals per arm. | 4 |
A match replaces an if/else ladder that tests one value against several constants. It evaluates the subject once, then takes the first arm whose pattern matches; an arm lists one or more literal patterns separated by commas and points at a body with =>, and a lone _ arm is the catch-all default. Patterns are compile-time constants — integers, char literals like 'w', or enum variants such as Action.Guard — which makes match ideal for dispatching on a key press, a tile code, or a mode. Each arm's body is a single statement or a { … } block; matching lowers to plain branches, so it is as cheap as the if chain it replaces.
When the subject is a tagged-union enum (an enum whose variants carry payloads), match also destructures it: an arm names a variant and binds its payload — Door(n) => …, Portal(x, y) => … — with n, x, y in scope for that arm's body. A tagged match must be exhaustive: it covers every variant or ends with a _, or the compiler rejects it, so a newly added variant flags every match that must handle it. See enum for the full picture.
program Steering {
property Velocity { delta_x: int = 0, delta_y: int = 0 }
model Player { Velocity }
handler Boot phase Start {
spawn Hero { Velocity { delta_x: 0, delta_y: 0 } }
}
handler ReadKeys phase Input {
let pressed = Input.key()
for (velocity) in query [Velocity, {Player}] {
match pressed {
'w' => velocity.delta_y = -1
's' => velocity.delta_y = 1
'a', 'h' => velocity.delta_x = -1
'd', 'l' => velocity.delta_x = 1
_ => velocity.delta_x = 0
}
}
}
}