Bindings now signal mutability the way Rust/Swift do, instead of `let` meaning
"local" and `var` meaning "module-level":
- `let x = e` -> immutable binding; a later `x = …` is a compile error
(`cannot assign to immutable 'x' … use var`).
- `var x = e` -> mutable binding, at local OR module scope (position decides
scope; the keyword decides mutability).
- `const` -> unchanged (compile-time).
Immutability is of the *binding*, not the object: `let n = new Node; n.kind = 1`
is fine (mutation through the reference); only rebinding `n` is rejected. The
check lives in emit_assign — a direct `name =` whose target is a `let` local
(loc_mut == 0) errors; field/index targets and `var`/param/loop bindings are
unaffected.
Delivered as three reseeds so the self-hosting compiler never had to compile
source its own rules would reject:
A) add `var` as a local statement + per-local mutability tracking (loc_mut),
no enforcement;
B) migrate every reassigned `let` -> `var` across the compiler, runtime and
examples (337 declarations), driven by a per-function, string/comment-aware
scan (binding targets only, never `x.f =` / `x[i] =`);
C) turn on the check. bootstrap-cfree (compiler vs its own source) and every
golden build (which splices the runtime) then proved zero reassigned `let`
was missed anywhere.
Also folded in: removed leftover debug instrumentation in block() (a `cur=` /
print_int(777…) trace on the separator-error path) and fixed parse.ludic's stale
header comment (no more `struct`). Reseeded (21711 lines); C-free fixpoint holds;
goldens identical; 17/17; vocab + doc-fences clean.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
144 lines
6.3 KiB
Text
144 lines
6.3 KiB
Text
# emit_spawn.ludic — spawn / despawn / self(), and seeding a component's fields
|
|
# from its declared defaults plus any per-spawn overrides.
|
|
|
|
fn emit_init_component(e: ptr, comp: ptr, rec: Node) -> void {
|
|
let me = itoa(MAX_ENT)
|
|
let c = find_comp(comp)
|
|
if ptr_is_null(c) { perr(sconcat("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 streq(lt, "ptr") { v = "null" }
|
|
if not ptr_is_null(fd.a) { let dv = emit_expr(fd.a); v = dv.code }
|
|
emit(" store "); emit(lt); emit(" "); emit(v); emit(", ptr "); emit(addr); emit("\n")
|
|
f = f + 1
|
|
}
|
|
# per-spawn overrides
|
|
if not ptr_is_null(rec) {
|
|
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 = j + 1
|
|
}
|
|
}
|
|
# @OnAttach(Property) runs once the property is attached and seeded
|
|
let ab = onattach_body(comp)
|
|
if not ptr_is_null(ab) {
|
|
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_block(ab)
|
|
nloc = save
|
|
}
|
|
}
|
|
|
|
# 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).
|
|
fn emit_bind_props(model: Node, e: ptr) -> 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 = c + 1
|
|
}
|
|
}
|
|
|
|
fn emit_spawn(st: Node) -> void {
|
|
let e = emit_bind("call i32 @L_alloc()")
|
|
let ak = find_arch_id(st.s)
|
|
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(st.s)
|
|
var c = 0
|
|
while c < len(arch.kids) {
|
|
let cn = arch.kids[c].s
|
|
var rec = ptr_null()
|
|
var i = 0
|
|
while i < len(st.kids) { if streq(st.kids[i].s, cn) { rec = st.kids[i].a }; i = i + 1 }
|
|
emit_init_component(e, cn, rec)
|
|
c = c + 1
|
|
}
|
|
let ob = onspawn_body(st.s) # @OnSpawn(Model) hook runs after init
|
|
if not ptr_is_null(ob) {
|
|
let save = nloc
|
|
emit_bind_props(arch, e)
|
|
emit_block(ob)
|
|
nloc = save
|
|
}
|
|
} else {
|
|
var i = 0
|
|
while i < len(st.kids) { emit_init_component(e, st.kids[i].s, st.kids[i].a); i = i + 1 }
|
|
}
|
|
}
|
|
|
|
fn 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(sconcat("load i32, ptr ", kp))
|
|
var i = 0
|
|
while i < len(g_ondespawn) {
|
|
let mname = g_ondespawn[i].s
|
|
let c = emit_bind(sconcat("icmp eq i32 ", sconcat(kind, sconcat(", ", 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(")\n")
|
|
emit(" br label %"); emit(no); emit("\n"); emit(no); emit(":\n")
|
|
i = i + 1
|
|
}
|
|
}
|
|
emit(" call void @L_free_entity(i32 "); emit(v.code); emit(")\n")
|
|
}
|
|
|
|
# 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.
|
|
fn emit_toggle(st: Node) -> void {
|
|
var val = "0"; if st.ival == 1 { val = "1" }
|
|
if not ptr_is_null(st.a) {
|
|
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 = ptr_null()
|
|
if st.ival == 1 { hb = onenable_body(st.s) } else { hb = ondisable_body(st.s) }
|
|
if not ptr_is_null(hb) {
|
|
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
|
|
}
|
|
} else {
|
|
var g = "@HE_"; if is_model(st.s) { g = "@ME_" } # model vs handler
|
|
emit(" store i32 "); emit(val); emit(", ptr "); emit(g); emit(st.s); emit("\n")
|
|
}
|
|
}
|