ludic/examples/lang/registry.ludic
Orkuncakilkaya 7e7f3ab297 feat(lang): L8 registries by declaration - registry and def
registry NAME of RECORD [as PREFIX] is a global table; def NAME key { ... } in
any file is one entry, collected in source order and filled before any code
runs. Each entry gets an index constant (PREFIX_KEY), the table PREFIX_COUNT,
and a record with a key field gets it filled and a NAME_find(key). A record
literal naming a field its record lacks is now an error everywhere; a global's
initializer is lowered as its own file's code (its errors, and what its module
may see, were whichever statement came last).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-24 13:34:51 +03:00

24 lines
1.1 KiB
Text

# registry.ludic — L8: registration by declaration. A registry is a table of records; each `def`
# is one entry, written where it belongs - in any file, in source order - rather than as a call
# in an init function. The compiler fills the table before any code runs, gives every entry an
# index constant (the prefix and the key: HF_CRATE), a count (HF_COUNT), fills a `key` field
# when the record has one, and adds a lookup by key (furnishings_find).
#
# Running it prints: 3 Crate true 2 fire_ring -1
program Registry {
def Furnishings bench { name: "Bench", cost: 90, seat: true }
property Furnishing {
key: string = ""
name: string = ""
cost: int = 0
seat: bool = false
}
registry Furnishings of Furnishing as HF
def Furnishings crate { name: "Crate", cost: 30 }
def Furnishings fire_ring { name: "Fire ring", cost: 120 }
entry {
var seat = "false"
if Furnishings[HF_BENCH].seat { seat = "true" }
print(`{HF_COUNT} {Furnishings[HF_CRATE].name} {seat} {furnishings_find("fire_ring")} {Furnishings[2].key} {furnishings_find("nope")}`)
}
}