ludic/selfhost/backend/game/emit_spawn.ludic
Orkuncakilkaya 647dfec334 feat(compiler): list literals, typed compound assignment, file:line diagnostics
- `[a, b, c]` list literals (E_LIST → emit_list); static_type learns
  slice-element, `new T`, list, string and literal kinds
- `x op= y` lowers through the same path as `x = x op y` (emit_bin_vals):
  fixed `*=`/`/=` use the Q16.16 64-bit paths, string `+=` concatenates,
  int→long widens; unary `-` keeps a fixed operand's type (arith_ty)
- one `unescape()` table for "strings", 'chars' and `interpolation`;
  `'\''`, `'\\'`, `'\"'` no longer read as 0; unterminated char literals
  and unexpected characters are errors instead of silently skipped
- every diagnostic is `file:line: error: msg` (g_parse_file / g_err_file,
  Node.file + Node.line set by node()); tok_desc() in expectation errors;
  duplicate `function` names and unknown `phase` names are reported in
  source terms (phase_id used to default unknown phases to Overlay)
- interpolation holes skip braces inside string literals
- hand-IR preludes move from the user `@fn_` prefix to `@lp_` so a user
  `is_ws` / `str_eq` / `path_join` no longer collides at link time
- `@ClearColor(expr)` accepts any constant expression; `Os.pid()` added
  (docs page + inventory); `str_starts()` in support/str
- main.ludic: `else if` flag ladder, char literals, stale script comments
- examples/lang/operators.ludic covers all of the above; os.ludic covers
  Os.pid; docs pages for Os.pid and the Overlay phase; ten changesets
- reseeded: selfhost/ludicc.seed.ll is the new compiler's own fixpoint

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-09-05 01:12:16 +03:00

272 lines
13 KiB
Text

# emit_spawn.ludic — spawn / despawn / self(), and seeding a component's fields
# from its declared defaults plus any per-spawn overrides.
# run a hook body with `self()` bound to entity `e` (a constructor knows its entity)
function emit_hook_with_self(e: pointer, body: Node) -> void {
let sslot = emit_alloca("i32")
emit(" store i32 "); emit(e); emit(", ptr "); emit(sslot); emit("\n")
if nself < len(self_stk) { self_stk[nself] = sslot } else { push(self_stk, sslot) }
nself += 1
emit_block(body)
nself -= 1
}
function emit_init_component(e: pointer, comp: pointer, rec: Node) -> void {
let me = itoa(MAX_ENT)
let c = find_comp(comp)
if (c == null) { perr(`spawn: unknown property {comp}`) }
let hp = nreg(); emit(" "); emit(hp); emit(" = getelementptr inbounds ["); emit(me); emit(" x i8], ptr @H_"); emit(comp); emit(", i32 0, i32 "); emit(e); emit("\n")
emit(" store i8 1, ptr "); emit(hp); emit("\n")
let slot = nreg()
emit(" "); emit(slot); emit(" = getelementptr inbounds ["); emit(me); emit(" x %Cmp_"); emit(comp); emit("], ptr @S_"); emit(comp); emit(", i32 0, i32 "); emit(e); emit("\n")
# defaults
var f = 0
while f < len(c.kids) {
let fd = c.kids[f]
let addr = nreg()
emit(" "); emit(addr); emit(" = getelementptr inbounds %Cmp_"); emit(comp); emit(", ptr "); emit(slot); emit(", i32 0, i32 "); emit(itoa(f)); emit("\n")
let lt = llty(fd.ty)
var v = "0"
if (lt == "ptr") { v = "null" }
if (fd.a != null) { let dv = emit_expr(fd.a); v = dv.code }
emit(" store "); emit(lt); emit(" "); emit(v); emit(", ptr "); emit(addr); emit("\n")
f += 1
}
# per-spawn overrides
if (rec != null) {
var j = 0
while j < len(rec.kids) {
let fi = rec.kids[j]
let fidx = field_index(c, fi.s)
if fidx >= 0 {
let addr = nreg()
emit(" "); emit(addr); emit(" = getelementptr inbounds %Cmp_"); emit(comp); emit(", ptr "); emit(slot); emit(", i32 0, i32 "); emit(itoa(fidx)); emit("\n")
let dv = emit_expr(fi.a)
emit(" store "); emit(llty(field_type(c, fi.s))); emit(" "); emit(dv.code); emit(", ptr "); emit(addr); emit("\n")
}
j += 1
}
}
# @OnAttach(Property) runs once the property is attached and seeded
let ab = onattach_body(comp)
if (ab != null) {
let save = nloc
let vslot = emit_alloca("ptr")
emit(" store ptr "); emit(slot); emit(", ptr "); emit(vslot); emit("\n")
loc_push(comp, vslot, comp)
emit_hook_with_self(e, ab)
nloc = save
}
# EV1: a @Public attach hook fires prop_<P>_attach with the entity
let aev = `prop_{comp}_attach`
if (find_event(aev) != null) { emit(" call void @ev_"); emit(aev); emit("(i32 "); emit(e); emit(")\n") }
}
# bind each of a model's properties to entity `e`'s component storage, so an
# @OnSpawn hook body can address them by name (like a query binding for one entity).
function emit_bind_props(model: Node, e: pointer) -> void {
let me = itoa(MAX_ENT)
var c = 0
while c < len(model.kids) {
let pname = model.kids[c].s
let slot = nreg()
emit(" "); emit(slot); emit(" = getelementptr inbounds ["); emit(me); emit(" x %Cmp_"); emit(pname); emit("], ptr @S_"); emit(pname); emit(", i32 0, i32 "); emit(e); emit("\n")
let vslot = emit_alloca("ptr")
emit(" store ptr "); emit(slot); emit(", ptr "); emit(vslot); emit("\n")
loc_push(pname, vslot, pname)
c += 1
}
}
# the record for component `cn` in a spawn or prefab node (null when absent)
function spawn_record(n: Node, cn: pointer) -> Node {
if (n == null) { return null }
var i = 0
while i < len(n.kids) { if (n.kids[i].s == cn) { return n.kids[i].a }; i += 1 }
return null
}
# a prefab's preset fields with the spawn's own overrides after them (later wins)
function merge_records(base: Node, over: Node) -> Node {
if (base == null) { return over }
if (over == null) { return base }
let m = node(E_REC)
var i = 0
while i < len(base.kids) { push(m.kids, base.kids[i]); i += 1 }
i = 0
while i < len(over.kids) { push(m.kids, over.kids[i]); i += 1 }
return m
}
# the model behind a spawn name: a model itself, or the model at the end of a
# prefab chain (`prefab Grunt: Foe`, `prefab Foe: Creature` -> Creature)
function spawn_model(name: pointer) -> pointer {
var n = name
var depth = 0
while true {
let p = find_prefab(n)
if (p == null) { return n }
n = p.ty
depth += 1
if depth > 16 { perr(`prefab {name}: the chain of prefabs never reaches a model`) }
}
return n
}
# the preset record for component `cn` along a prefab chain, base prefabs first
function prefab_record(name: pointer, cn: pointer) -> Node {
let p = find_prefab(name)
if (p == null) { return null }
return merge_records(prefab_record(p.ty, cn), spawn_record(p, cn))
}
function emit_spawn(st: Node) -> pointer {
let e = emit_bind("call i32 @L_alloc()")
let model = spawn_model(st.s)
let ak = find_arch_id(model)
if (find_prefab(st.s) != null) and (ak == 0) { perr(`prefab {st.s}: unknown model {model}`) }
if ak > 0 {
let me = itoa(MAX_ENT)
let kp = nreg(); emit(" "); emit(kp); emit(" = getelementptr inbounds ["); emit(me); emit(" x i32], ptr @L_kind, i32 0, i32 "); emit(e); emit("\n")
emit(" store i32 "); emit(itoa(ak)); emit(", ptr "); emit(kp); emit("\n")
let arch = find_arch(model)
var c = 0
while c < len(arch.kids) {
let cn = arch.kids[c].s
emit_init_component(e, cn, merge_records(prefab_record(st.s, cn), spawn_record(st, cn)))
c += 1
}
let ob = onspawn_body(model) # @OnSpawn(Model) hook runs after init
if (ob != null) {
let save = nloc
emit_bind_props(arch, e)
emit_hook_with_self(e, ob)
nloc = save
}
# EV1: a @Public spawn hook also fires the public event model_<M>_spawn, so
# mods (native or foreign, over the ABI) see the entity born.
let sev = `model_{model}_spawn`
if (find_event(sev) != null) { emit(" call void @ev_"); emit(sev); emit("(i32 "); emit(e); emit(")\n") }
} else {
var i = 0
while i < len(st.kids) { emit_init_component(e, st.kids[i].s, st.kids[i].a); i += 1 }
}
return e # the new entity id (for ludic_spawn_<M>)
}
function emit_despawn(st: Node) -> void {
let v = emit_expr(st.a)
# @OnDespawn: dispatch on the entity's kind and run the matching model's hook
if len(g_ondespawn) > 0 {
let me = itoa(MAX_ENT)
let kp = nreg(); emit(" "); emit(kp); emit(" = getelementptr inbounds ["); emit(me); emit(" x i32], ptr @L_kind, i32 0, i32 "); emit(v.code); emit("\n")
let kind = emit_bind(`load i32, ptr {kp}`)
var i = 0
while i < len(g_ondespawn) {
let mname = g_ondespawn[i].s
let c = emit_bind(`icmp eq i32 {kind}, {itoa(find_arch_id(mname))}`)
let yes = lbl("dh"); let no = lbl("dhn")
emit(" br i1 "); emit(c); emit(", label %"); emit(yes); emit(", label %"); emit(no); emit("\n")
emit(yes); emit(":\n call void @on_despawn_"); emit(mname); emit("(i32 "); emit(v.code); emit(", i32 0)\n") # reason = EndReason.Despawned
let dev = `model_{mname}_despawn` # EV1: @Public despawn event
if (find_event(dev) != null) { emit(" call void @ev_"); emit(dev); emit("(i32 "); emit(v.code); emit(", i32 0)\n") }
emit(" br label %"); emit(no); emit("\n"); emit(no); emit(":\n")
i += 1
}
}
if len(g_events) > 0 { emit(" call void @ludic_sweep_entity(i32 "); emit(v.code); emit(")\n") } # EV5: drop entity-scoped listeners
emit(" call void @L_free_entity(i32 "); emit(v.code); emit(")\n")
}
# attach P on e [{ overrides }] — add a property to a live entity. Structural
# (not a toggle): seeds the property's fields and fires @OnAttach, but only on a
# real transition — if the entity already has the property it is a no-op, so the
# hook fires once per genuine attach (flecs/Bevy "real add" semantics).
function emit_attach(st: Node) -> void {
let ev = emit_expr(st.a)
let me = itoa(MAX_ENT)
let hp = nreg(); emit(" "); emit(hp); emit(" = getelementptr inbounds ["); emit(me); emit(" x i8], ptr @H_"); emit(st.s); emit(", i32 0, i32 "); emit(ev.code); emit("\n")
let cur = emit_bind(`load i8, ptr {hp}`)
let isnew = emit_bind(`icmp eq i8 {cur}, 0`)
let doit = lbl("attach"); let done = lbl("attdone")
emit(" br i1 "); emit(isnew); emit(", label %"); emit(doit); emit(", label %"); emit(done); emit("\n")
emit(doit); emit(":\n"); g_term = false
emit_init_component(ev.code, st.s, st.b) # sets has=1, seeds defaults+overrides, fires @OnAttach
if not g_term { emit(" br label %"); emit(done); emit("\n") }
emit(done); emit(":\n"); g_term = false
}
# detach P on e — remove a property from a live entity. Fires @OnDetach with the
# property bound by name (its data still lives in @S_ storage, so the teardown
# body reads the outgoing value), then clears the has-flag so queries skip it.
# Only fires on a real transition; detaching an absent property is a no-op.
function emit_detach(st: Node) -> void {
let ev = emit_expr(st.a)
let me = itoa(MAX_ENT)
let hp = nreg(); emit(" "); emit(hp); emit(" = getelementptr inbounds ["); emit(me); emit(" x i8], ptr @H_"); emit(st.s); emit(", i32 0, i32 "); emit(ev.code); emit("\n")
let cur = emit_bind(`load i8, ptr {hp}`)
let here = emit_bind(`icmp ne i8 {cur}, 0`)
let doit = lbl("detach"); let done = lbl("detdone")
emit(" br i1 "); emit(here); emit(", label %"); emit(doit); emit(", label %"); emit(done); emit("\n")
emit(doit); emit(":\n"); g_term = false
emit(" store i8 0, ptr "); emit(hp); emit("\n") # clear has-flag (data persists in @S_)
let hb = ondetach_body(st.s) # @OnDetach reads the outgoing value
if (hb != null) {
let save = nloc
let slot = nreg(); emit(" "); emit(slot); emit(" = getelementptr inbounds ["); emit(me); emit(" x %Cmp_"); emit(st.s); emit("], ptr @S_"); emit(st.s); emit(", i32 0, i32 "); emit(ev.code); emit("\n")
let vslot = emit_alloca("ptr")
emit(" store ptr "); emit(slot); emit(", ptr "); emit(vslot); emit("\n")
loc_push(st.s, vslot, st.s)
emit_block(hb)
nloc = save
}
let dev = `prop_{st.s}_detach` # EV1: @Public detach event
if (find_event(dev) != null) { emit(" call void @ev_"); emit(dev); emit("(i32 "); emit(ev.code); emit(")\n") }
if not g_term { emit(" br label %"); emit(done); emit("\n") }
emit(done); emit(":\n"); g_term = false
}
# enable/disable. `<P> on <e>` toggles a property's has-flag on an entity (its
# data persists, so re-enabling restores it, and queries already skip a cleared
# flag). A bare `<Model>` / `<Handler>` flips a global enabled flag.
function find_sys(name: pointer) -> Node {
var i = 0
while i < len(prog) { let d = prog[i]; if d.kind == N_SYS and (d.s == name) { return d }; i += 1 }
return null
}
function emit_toggle(st: Node) -> void {
var val = "0"; if st.ival == 1 { val = "1" }
if (st.ty != null) and (st.ty == "system") { return } # `disable system <fn>` is compile-time (handled in emit_one_engine_system); no runtime code
if (st.ty != null) and (st.ty == "layer") { # enable/disable layer L
emit(" store i32 "); emit(val); emit(", ptr @LE_"); emit(st.s); emit("\n")
var lev = `layer_{st.s}_hide`; if st.ival == 1 { lev = `layer_{st.s}_show` } # public layer -> event
if (find_event(lev) != null) { emit(" call void @ev_"); emit(lev); emit("()\n") }
return
}
if (st.a != null) {
let ev = emit_expr(st.a)
let me = itoa(MAX_ENT)
let hp = nreg(); emit(" "); emit(hp); emit(" = getelementptr inbounds ["); emit(me); emit(" x i8], ptr @H_"); emit(st.s); emit(", i32 0, i32 "); emit(ev.code); emit("\n")
emit(" store i8 "); emit(val); emit(", ptr "); emit(hp); emit("\n")
var hb: Node = null
if st.ival == 1 { hb = onenable_body(st.s) } else { hb = ondisable_body(st.s) }
if (hb != null) {
let save = nloc
let slot = nreg(); emit(" "); emit(slot); emit(" = getelementptr inbounds ["); emit(me); emit(" x %Cmp_"); emit(st.s); emit("], ptr @S_"); emit(st.s); emit(", i32 0, i32 "); emit(ev.code); emit("\n")
let vslot = emit_alloca("ptr")
emit(" store ptr "); emit(slot); emit(", ptr "); emit(vslot); emit("\n")
loc_push(st.s, vslot, st.s)
emit_block(hb)
nloc = save
}
# EV1: a @Public enable/disable hook fires prop_<P>_enable / prop_<P>_disable
var tev = `prop_{st.s}_disable`; if st.ival == 1 { tev = `prop_{st.s}_enable` }
if (find_event(tev) != null) { emit(" call void @ev_"); emit(tev); emit("(i32 "); emit(ev.code); emit(")\n") }
} else {
var g = "@HE_"; if is_model(st.s) { g = "@ME_" } # model vs handler
var hname = st.s
if (g == "@HE_") and (find_sys(hname) == null) and (g_cur_scene != null) { # a scene's own handler, by its bare name
let qualified = `{g_cur_scene.s}_{hname}`
if (find_sys(qualified) != null) { hname = qualified }
}
emit(" store i32 "); emit(val); emit(", ptr "); emit(g); emit(hname); emit("\n")
}
}