green(metatype): declare(name) + self-reference (recursive enums via *Name)
declare now takes the type's NAME — `declare(name) -> Type` — because the
compiler needs it at compile time to register the forward type, which is
what makes self-reference resolve. EnumInfo drops `name` (it lives on
declare now); define completes the handle's body in place (the slot is
already named).
Self-reference mechanism (evalComptimeType): before lowering a comptime
type expression, preregisterForwardTypes scans it (and a called ctor fn's
body) for `declare("Name")` calls and registers each as an empty forward
nominal type AND binds it as a type alias. The alias is essential: a
`Name :: ctor()` decl makes `Name` a const_decl author, so a `*Name`
self-reference resolves through the forward-ALIAS path
(type_aliases_by_source), which a bare findByName registration doesn't
satisfy. With both in place `*Name` resolves to the forward slot at lower
time; the interp's declare returns that same slot; define fills it.
List :: make_list();
make_list :: () -> Type {
h := declare("List");
return define(h, .enum(.{ variants = .[
EnumVariant.{ name = "cons", payload = *List },
EnumVariant.{ name = "nil", payload = void } ] }));
}
Verified: cons/nil construct + match (direct and through the pointer),
multi-node list traversal via a recursive `count(*List)`. meta.sx
RecvResult/TryResult + examples 0614/0615/0617 updated to declare(name);
full suite green (673).
This commit is contained in:
@@ -16,11 +16,10 @@ EnumVariant :: struct {
|
||||
payload: Type;
|
||||
}
|
||||
|
||||
// The shape of an enum/tagged-union being reflected or constructed. `name` is
|
||||
// the type's name — it travels WITH the shape (so `define` can name the slot and
|
||||
// `type_info` round-trips it); the compiler derives nothing from a binding LHS.
|
||||
// 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 {
|
||||
name: string;
|
||||
variants: []EnumVariant;
|
||||
}
|
||||
|
||||
@@ -33,20 +32,19 @@ TypeInfo :: enum {
|
||||
}
|
||||
|
||||
// The compiler's ONLY type-construction primitives (comptime-only #builtins):
|
||||
// declare() — mint a NEW empty (undefined) nominal type, returned
|
||||
// as a `Type` handle. Using it before its `define` is a
|
||||
// loud error; references to it (`*Self`) are fine.
|
||||
// define(handle, info) — fill a declared handle's body from a `TypeInfo`
|
||||
// (which carries the type's name), and RETURN the
|
||||
// handle so the one-shot form chains:
|
||||
// T :: define(declare(), info);
|
||||
// The recursive / mutually-recursive form keeps them apart so the handle can be
|
||||
// referenced inside its own definition:
|
||||
// List :: declare();
|
||||
// define(List, .enum(.{ name = "List", variants = .[
|
||||
// EnumVariant.{ name = "cons", payload = *List },
|
||||
// EnumVariant.{ name = "nil", payload = void } ] }));
|
||||
declare :: () -> Type #builtin;
|
||||
// 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;
|
||||
@@ -61,7 +59,7 @@ field_type :: ($T: Type, idx: i64) -> Type #builtin;
|
||||
|
||||
// A blocking recv: a value, or the channel was closed (drained).
|
||||
RecvResult :: ($T: Type) -> Type {
|
||||
return define(declare(), .enum(.{ name = "RecvResult", variants = .[
|
||||
return define(declare("RecvResult"), .enum(.{ variants = .[
|
||||
EnumVariant.{ name = "value", payload = T },
|
||||
EnumVariant.{ name = "closed", payload = void },
|
||||
] }));
|
||||
@@ -70,7 +68,7 @@ RecvResult :: ($T: Type) -> Type {
|
||||
// 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(), .enum(.{ name = "TryResult", variants = .[
|
||||
return define(declare("TryResult"), .enum(.{ variants = .[
|
||||
EnumVariant.{ name = "value", payload = T },
|
||||
EnumVariant.{ name = "empty", payload = void },
|
||||
EnumVariant.{ name = "closed", payload = void },
|
||||
|
||||
Reference in New Issue
Block a user