feat(types): tagged-union enums — variant payloads + binding match + exhaustiveness (#56)
All checks were successful
bootstrap / cfree-fixpoint (push) Successful in 25s
ci / build-and-test (push) Successful in 1m26s
commit-lint / conventional-commits (push) Successful in 2s
docs / build-and-deploy (push) Successful in 23s

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>
This commit is contained in:
Orkun ÇAKILKAYA 2026-09-01 03:07:46 +03:00
parent 7c91d24595
commit 872f458cb2
11 changed files with 17990 additions and 16481 deletions

View file

@ -0,0 +1,50 @@
program T {
# a tagged-union enum (issue #56): variants carry payloads, are constructed by
# name, and destructure in `match` with the payload names bound in each arm.
enum Tile { Empty, Wall, Door(int), Portal(int, int) }
# match binds each variant's payload; the arms are exhaustive (no `_`).
function walk_cost(t: Tile) -> int {
match t {
Empty => { return 1 }
Wall => { return 0 }
Door(n) => { return 10 + n }
Portal(x, y) => { return x + y }
}
return -1
}
# a nested match and an enum flowing through a return
function open(t: Tile) -> Tile {
match t {
Door(n) => { return Portal(n, n) }
_ => { return t }
}
return t
}
entry {
print(walk_cost(Empty)) # 1
print(walk_cost(Wall)) # 0
print(walk_cost(Door(5))) # 15
print(walk_cost(Portal(3, 4))) # 7
let d = Door(9)
print(walk_cost(d)) # 19
print(walk_cost(open(d))) # 18 (Door(9) -> Portal(9,9) -> 18)
print(walk_cost(open(Wall))) # 0 (default arm returns t unchanged)
# a match with a default arm, OR-ed nullary patterns, and a bound arm
let t = Portal(2, 40)
match t {
Empty, Wall => { print(100) }
Portal(x, y) => { print(x * y) } # 80
_ => { print(999) }
}
# payload-less variant boxed and matched
var here: Tile = Empty
here = Door(7)
print(walk_cost(here)) # 17
}
}