make_enum(name, variants: []EnumVariant) -> Type mints a nominal enum from a variant list passed as a VALUE, not a hardcoded literal — the open-ended form the channel-result constructors are special cases of. Pure sx over declare/define; no compiler machinery. Because variants is an ordinary comptime value, a non-generic builder can ASSEMBLE it in a local before minting. examples/0620: build_level fills a local array, then make_enum mints Level from it — exercising define decoding a value-arg SLICE (decodeVariantElements' slice branch), vs. the inline .[ … ] array the 0614-0618 examples pass directly. No compiler change (locks existing capability). Suite green (678).
94 lines
4.7 KiB
Plaintext
94 lines
4.7 KiB
Plaintext
// Comptime type metaprogramming — `declare` / `define` (construct a NEW nominal
|
|
// type from data), plus `type_info` / `field_type` (reflect a type → data) and
|
|
// the data model they reflect INTO and construct FROM.
|
|
//
|
|
// This is a SEPARATE on-demand module rather than part of the prelude: its data
|
|
// types would otherwise intern into every module's type table and shift every
|
|
// `.ir` snapshot. Import it explicitly: #import "modules/std/meta.sx";
|
|
//
|
|
// All four are comptime-only builtins — reaching one at runtime is a hard error
|
|
// (the type must be minted / reflected at compile time).
|
|
|
|
// One variant of a constructed enum: a name plus an optional payload type.
|
|
// `payload = void` means a tagless variant (e.g. `closed`).
|
|
EnumVariant :: struct {
|
|
name: string;
|
|
payload: Type;
|
|
}
|
|
|
|
// The shape of an enum/tagged-union being reflected or constructed. The type's
|
|
// NAME is supplied to `declare(name)`, not here — `declare` needs it at compile
|
|
// time to register the forward type so the body can reference itself (`*Name`).
|
|
EnumInfo :: struct {
|
|
variants: []EnumVariant;
|
|
}
|
|
|
|
// The reflected/constructed type shape. A tagged union over the kinds of type
|
|
// that can be minted. Only `` .`enum `` ships today; struct/tuple land later.
|
|
// The variant uses the backtick raw-identifier escape so it reads as the
|
|
// keyword `enum` (`` .`enum(...) ``) rather than a mangled `enum_`.
|
|
TypeInfo :: enum {
|
|
`enum: EnumInfo;
|
|
}
|
|
|
|
// The compiler's ONLY type-construction primitives (comptime-only #builtins):
|
|
// declare(name) — mint a NEW empty (undefined) nominal type NAMED
|
|
// `name`, returned as a `Type` handle. The compiler
|
|
// registers the forward type at compile time, so the
|
|
// body of `define` can reference it BY NAME — that's how
|
|
// self-reference works (`payload = *List` resolves to the
|
|
// forward `List`). Using the type before its `define` is
|
|
// a loud error; a pointer to it is fine.
|
|
// define(handle, info) — fill a declared handle's body from a `TypeInfo`, and
|
|
// RETURN the handle so the one-shot form chains:
|
|
// List :: define(declare("List"), .enum(.{ variants = .[
|
|
// EnumVariant.{ name = "cons", payload = *List },
|
|
// EnumVariant.{ name = "nil", payload = void } ] }));
|
|
declare :: (name: string) -> Type #builtin;
|
|
define :: (handle: Type, info: TypeInfo) -> Type #builtin;
|
|
type_info :: ($T: Type) -> TypeInfo #builtin;
|
|
field_type :: ($T: Type, idx: i64) -> Type #builtin;
|
|
|
|
// --- Type constructors built in sx library code (no compiler machinery) ---
|
|
//
|
|
// The channel result types, expressed as type-fns over declare/define. They
|
|
// demonstrate that a programmatically-built enum carries a full enum through
|
|
// codegen: `RecvResult(i64)` constructs and matches like any hand-written enum,
|
|
// and is one nominal type across sites (the type-fn identity path). The channel
|
|
// library (N3) consumes these once it lands.
|
|
|
|
// The GENERAL enum constructor: mint a nominal enum NAMED `name` from a variant
|
|
// list passed as a VALUE (a `[]EnumVariant`), rather than a hardcoded literal.
|
|
// Because `variants` is an ordinary comptime value, a caller can ASSEMBLE it in
|
|
// a local (conditionally, in a loop, from type args) before minting — see
|
|
// `examples/0620`. `define` decodes the slice via `decodeVariantElements`. The
|
|
// channel constructors above are the special-cased shapes; `make_enum` is the
|
|
// open-ended one every other constructor could be written over.
|
|
//
|
|
// Call it from a non-generic `() -> Type` builder (whose whole body is
|
|
// comptime-evaluated, so locals are in scope) or inline with a literal arg
|
|
// (`E :: make_enum("E", .[ … ])`). A *generic* type-fn comptime-evaluates only
|
|
// its return EXPRESSION, so build the list inline in the return there, not in a
|
|
// preceding local.
|
|
make_enum :: (name: string, variants: []EnumVariant) -> Type {
|
|
return define(declare(name), .enum(.{ variants = variants }));
|
|
}
|
|
|
|
// A blocking recv: a value, or the channel was closed (drained).
|
|
RecvResult :: ($T: Type) -> Type {
|
|
return define(declare("RecvResult"), .enum(.{ variants = .[
|
|
EnumVariant.{ name = "value", payload = T },
|
|
EnumVariant.{ name = "closed", payload = void },
|
|
] }));
|
|
}
|
|
|
|
// A non-blocking try-recv: a value, currently empty, or closed — three states
|
|
// a bool can't express.
|
|
TryResult :: ($T: Type) -> Type {
|
|
return define(declare("TryResult"), .enum(.{ variants = .[
|
|
EnumVariant.{ name = "value", payload = T },
|
|
EnumVariant.{ name = "empty", payload = void },
|
|
EnumVariant.{ name = "closed", payload = void },
|
|
] }));
|
|
}
|