ludic/selfhost/backend/game/emit_query.ludic
Orkuncakilkaya 23726afa90
All checks were successful
bootstrap / cfree-fixpoint (push) Successful in 12s
ci / build-and-test (push) Successful in 50s
commit-lint / conventional-commits (push) Successful in 3s
docs / build-and-deploy (push) Successful in 2s
refactor(selfhost): reorganise into concern-based subdirectories
Split the flat 38-file selfhost/ into concern-based subdirectories:

  frontend/        lex, parse, parse_game, ast
  support/         str, buf, io
  backend/         core IR + expression/statement lowering
  backend/game/    ECS/scene/event/world lowering
  backend/stdlib/  the namespaced Math.*/Text.*/Crypto.*/… intrinsics

and split the three oversized emitters at responsibility boundaries so
no file mixes concerns:

  emit_game.ludic  -> + emit_world.ludic         (reflection world table,
                                                  tick helpers, @main synthesis)
  emit_expr.ludic  -> + emit_call.ludic          (namespaced builtins, call
                                                  lowering, expr dispatch)
  emit_text.ludic  -> + emit_text_prelude.ludic  (emitted string-builder runtime)

FRAGS in tools/x/selfhost.ludic is updated to the new paths with the link
order preserved, and the Python doc/vocabulary tooling is updated to walk
the new layout. Because the build is a plain in-order concatenation and
every split lands on a blank-line boundary, the regenerated seed is
byte-identical: `x reseed` leaves selfhost/ludicc.seed.ll unchanged,
`x bootstrap-cfree` still reaches its fixed point, and both `x test` (56)
and `x selfhost-test` (29, incl. golden renders) stay green.

Closes #29

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-08-31 00:26:02 +03:00

101 lines
4 KiB
Text

# emit_query.ludic — the ECS query loop. Iterates live entities, filters by the
# components/archetypes each carries, binds the requested components, and runs
# the body once per match. Mirrors ll_query in compiler/back/ir_ecs.c.
function find_arch_id(name: pointer) -> int {
var i = 0; var n = 1
while i < len(prog) { if prog[i].kind == N_ARCH { if (prog[i].s == name) { return n }; n = n + 1 }; i = i + 1 }
return 0
}
function emit_query(st: Node) -> void {
let me = itoa(MAX_ENT)
let ip = emit_alloca("i32")
store_at("i32", "0", ip)
if nself < len(self_stk) { self_stk[nself] = ip } else { push(self_stk, ip) }
nself = nself + 1
let cond = lbl("qcond"); let body = lbl("qbody"); let nxt = lbl("qnext"); let endl = lbl("qend")
emit(" br label %"); emit(cond); emit("\n")
emit(cond); emit(":\n")
let i0 = emit_bind(`load i32, ptr {ip}`)
let ec = emit_bind("load i32, ptr @L_entc")
let lt = emit_bind(`icmp slt i32 {i0}, {ec}`)
emit(" br i1 "); emit(lt); emit(", label %"); emit(body); emit(", label %"); emit(endl); emit("\n")
emit(body); emit(":\n"); g_term = false
let i1 = emit_bind(`load i32, ptr {ip}`)
# alive?
let ap = nreg(); emit(" "); emit(ap); emit(" = getelementptr inbounds ["); emit(me); emit(" x i32], ptr @L_alive, i32 0, i32 "); emit(i1); emit("\n")
let al = emit_bind(`load i32, ptr {ap}`)
let alc = emit_bind(`icmp ne i32 {al}, 0`)
let ka = lbl("qa")
emit(" br i1 "); emit(alc); emit(", label %"); emit(ka); emit(", label %"); emit(nxt); emit("\n")
emit(ka); emit(":\n")
let terms = st.c
# component / archetype filters
var t = 0
while t < len(terms.kids) {
let tm = terms.kids[t]
let ak = find_arch_id(tm.s)
var ok = "0"
if ak > 0 {
let kp = nreg(); emit(" "); emit(kp); emit(" = getelementptr inbounds ["); emit(me); emit(" x i32], ptr @L_kind, i32 0, i32 "); emit(i1); emit("\n")
let kv = emit_bind(`load i32, ptr {kp}`)
let kok = emit_bind(`icmp eq i32 {kv}, {itoa(ak)}`)
let mev = emit_bind(`load i32, ptr @ME_{tm.s}`) # model-enabled flag
let meok = emit_bind(`icmp ne i32 {mev}, 0`)
ok = emit_bind(`and i1 {kok}, {meok}`)
} else {
let hp = nreg(); emit(" "); emit(hp); emit(" = getelementptr inbounds ["); emit(me); emit(" x i8], ptr @H_"); emit(tm.s); emit(", i32 0, i32 "); emit(i1); emit("\n")
let hv = emit_bind(`load i8, ptr {hp}`)
ok = emit_bind(`icmp ne i8 {hv}, 0`)
}
let keep = lbl("qt")
emit(" br i1 "); emit(ok); emit(", label %"); emit(keep); emit(", label %"); emit(nxt); emit("\n")
emit(keep); emit(":\n")
t = t + 1
}
# bind the requested components to the loop variables, in order
let save = nloc
var vi = 0
t = 0
while t < len(terms.kids) {
let tm = terms.kids[t]
if tm.ival == 0 and find_arch_id(tm.s) == 0 {
if vi < len(st.kids) {
let slot = nreg()
emit(" "); emit(slot); emit(" = getelementptr inbounds ["); emit(me); emit(" x %Cmp_"); emit(tm.s); emit("], ptr @S_"); emit(tm.s); emit(", i32 0, i32 "); emit(i1); emit("\n")
let vslot = emit_alloca("ptr")
emit(" store ptr "); emit(slot); emit(", ptr "); emit(vslot); emit("\n")
loc_push(st.kids[vi].s, vslot, tm.s)
vi = vi + 1
}
}
t = t + 1
}
# optional where-clause
if (st.b != null) {
let w = emit_expr(st.b)
let wc = emit_bind(`icmp ne i32 {w.code}, 0`)
let kw = lbl("qw")
emit(" br i1 "); emit(wc); emit(", label %"); emit(kw); emit(", label %"); emit(nxt); emit("\n")
emit(kw); emit(":\n")
}
loop_push(nxt, endl)
emit_block(st.a)
loop_pop()
nloc = save
if not g_term { emit(" br label %"); emit(nxt); emit("\n") }
emit(nxt); emit(":\n")
let i2 = emit_bind(`load i32, ptr {ip}`)
let i3 = emit_bind(`add i32 {i2}, 1`)
store_at("i32", i3, ip)
emit(" br label %"); emit(cond); emit("\n")
emit(endl); emit(":\n"); g_term = false
nself = nself - 1
}