wcc: Hare-style enum [storage] { ... } type

`type Foo = enum [intT] { NAME [= expr], ... };`. Storage defaults
to i32; members auto-increment from 0 (or last+1) when `= expr` is
omitted, and value expressions can reference earlier siblings —
enough surface for io::mode-style flag enums (`RDWR = READ | WRITE`).

`Foo.MEMBER` folds to an N_INTLIT in the checker, typed as the
named enum. Binops on enum values yield the same enum (type_eq on
the named pointer), so `mode.R | mode.W` is a `mode`. Enum ↔ int
is a reinterpret-only `as` cast — same register, no tag wrap — so
`mode.RDWR as i32` and `1 as mode` both work without runtime ops.

`is`/`?`/`!` are still tagged-union-only. CSP runtime (chan/proc)
is unchanged; only the type-system slot is touched here.
This commit is contained in:
2026-05-12 04:15:31 +09:00
parent 22999cd3fa
commit 34817eedcd
10 changed files with 257 additions and 1 deletions

View File

@@ -178,6 +178,7 @@ typedef enum {
TK_IS, /* Hare-style type test: e is T */
TK_VOID, /* `void` — both a type name and a zero-size value */
TK_YIELD, /* `yield expr;` — value-return from a match arm */
TK_ENUM, /* Hare-style `enum [storage] { ... }` type form */
TK_LAST /* sentinel for tables */
} Tkind;
@@ -300,6 +301,11 @@ typedef enum {
N_TBANG, /* `!T` — error-flagged type. lhs = inner type. */
N_YIELD, /* `yield expr;` — set the enclosing match's value-
* return and jump to its end label. lhs = value. */
N_TENUM, /* `enum [storage] { ... }` type form.
* lhs = storage type expr or NULL (default i32);
* list = chain of N_TENUMMEMBER. */
N_TENUMMEMBER, /* enum member. str=name, lhs=value expr or NULL
* (auto-increment when omitted). */
N_LAST
} Nkind;
@@ -377,7 +383,11 @@ typedef enum {
TY_UNTYPED_STR,
TY_UNTYPED_RUNE,
TY_UNTYPED_BOOL,
TY_UNTYPED_NIL
TY_UNTYPED_NIL,
/* Appended at the tail to keep existing TY_* values stable —
* lib/ww/typ.ww mirrors them as explicit `def` numbers. */
TY_ENUM /* `enum [storage] { ... }`. sub=storage,
* fields=member list (Tfield.offset = u64 value). */
} TypeKind;
typedef struct Tfield Tfield;