Three enable/disable statement scopes, each a reversible flag flip:
- `disable P on e` / `enable P on e` — one property on one entity. Clears
the has-flag so queries stop matching; field data persists in storage, so
enable restores it untouched. @OnDisable(P)/@OnEnable(P) handler hooks run
at the toggle point with the property bound by name.
- `disable Model` / `enable Model` — @ME_<Model> global flag; the model's
entities drop out of every query while disabled.
- `disable Handler` / `enable Handler` — @HE_<Handler> global flag; the
handler stops being called each phase while disabled.
Nothing is copied or freed — each toggle is one global store or one has-flag
store. Reduces entirely to existing ECS machinery (has-flags, kind filter,
per-phase call guards), so the data-oriented model is untouched.
New AST node S_TOGGLE; emit_toggle lowers it. Query {Model} filter now ANDs
@ME_; phase calls now guard on @HE_. Parser gains enable/disable statements
and @OnEnable/@OnDisable annotations.
Vocabulary: `on` promoted from RESERVED to CLAUSE (parser now dispatches on
it); enable/disable added as STMT keywords — synced across ludic_syntax.h,
the TextMate grammar, and LudicTokens.kt (check-vocabulary.py clean).
examples/toggle.ludic demonstrates all three scopes (prints 6 0 7 1 0);
test.sh smoke asserts it. Reseeded; C-free fixpoint holds; goldens identical.
Also: stop tracking tools/.idea/ (gitignored).
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
95 lines
4 KiB
Text
95 lines
4 KiB
Text
# emit_ecs.ludic — ECS storage and the entity allocator. For each component:
|
|
# a %Cmp_ layout, a dense @S_ store, and an @H_ "has this component" array.
|
|
# Entities are integer handles; L_alloc reuses freed slots. Mirrors the ECS
|
|
# parts of compiler/back/ir_decl.c.
|
|
|
|
const MAX_ENT: int = 1024
|
|
|
|
fn has_ecs() -> bool {
|
|
let i = 0
|
|
while i < len(prog) { let k = prog[i].kind; if k == N_COMP or k == N_SYS { return true }; i = i + 1 }
|
|
return false
|
|
}
|
|
fn has_systems() -> bool {
|
|
let i = 0
|
|
while i < len(prog) { if prog[i].kind == N_SYS { return true }; i = i + 1 }
|
|
return false
|
|
}
|
|
|
|
fn emit_ecs_storage() -> void {
|
|
emith("@L_running = internal global i32 1\n")
|
|
emith("@L_key = internal global i32 0\n")
|
|
emith("@L_entc = internal global i32 0\n")
|
|
let me = itoa(MAX_ENT)
|
|
emith(sconcat("@L_alive = internal global [", sconcat(me, " x i32] zeroinitializer\n")))
|
|
emith(sconcat("@L_kind = internal global [", sconcat(me, " x i32] zeroinitializer\n")))
|
|
emith(sconcat("@L_freelist = internal global [", sconcat(me, " x i32] zeroinitializer\n")))
|
|
emith("@L_freen = internal global i32 0\n")
|
|
let i = 0
|
|
while i < len(prog) {
|
|
let c = prog[i]
|
|
if c.kind == N_COMP {
|
|
emith(sconcat("%Cmp_", sconcat(c.s, " = type { ")))
|
|
if len(c.kids) == 0 { emith("i32") }
|
|
let f = 0
|
|
while f < len(c.kids) { if f > 0 { emith(", ") }; emith(llty(c.kids[f].ty)); f = f + 1 }
|
|
emith(" }\n")
|
|
emith(sconcat("@S_", sconcat(c.s, sconcat(" = internal global [", sconcat(me, sconcat(" x %Cmp_", sconcat(c.s, "] zeroinitializer\n")))))))
|
|
emith(sconcat("@H_", sconcat(c.s, sconcat(" = internal global [", sconcat(me, " x i8] zeroinitializer\n")))))
|
|
}
|
|
# one enabled-flag global per model and per handler (default enabled)
|
|
if c.kind == N_ARCH { emith(sconcat("@ME_", sconcat(c.s, " = internal global i32 1\n"))) }
|
|
if c.kind == N_SYS { emith(sconcat("@HE_", sconcat(c.s, " = internal global i32 1\n"))) }
|
|
i = i + 1
|
|
}
|
|
}
|
|
|
|
# L_reset(e): clear every has-flag and the archetype kind for entity e
|
|
fn emit_ecs_allocator() -> void {
|
|
let me = itoa(MAX_ENT)
|
|
emit("define void @L_reset(i32 %e) {\nentry:\n")
|
|
let i = 0
|
|
while i < len(prog) {
|
|
if prog[i].kind == N_COMP {
|
|
let hn = sconcat("%h", itoa(i))
|
|
emit(" "); emit(hn); emit(" = getelementptr inbounds ["); emit(me); emit(" x i8], ptr @H_"); emit(prog[i].s); emit(", i32 0, i32 %e\n")
|
|
emit(" store i8 0, ptr "); emit(hn); emit("\n")
|
|
}
|
|
i = i + 1
|
|
}
|
|
emit(" %k = getelementptr inbounds ["); emit(me); emit(" x i32], ptr @L_kind, i32 0, i32 %e\n")
|
|
emit(" store i32 0, ptr %k\n ret void\n}\n\n")
|
|
|
|
emit("define i32 @L_alloc() {\nentry:\n")
|
|
emit(" %fn = load i32, ptr @L_freen\n")
|
|
emit(" %has = icmp sgt i32 %fn, 0\n")
|
|
emit(" br i1 %has, label %reuse, label %fresh\n")
|
|
emit("reuse:\n")
|
|
emit(" %fn1 = sub i32 %fn, 1\n")
|
|
emit(" store i32 %fn1, ptr @L_freen\n")
|
|
emit(" %fp = getelementptr inbounds ["); emit(me); emit(" x i32], ptr @L_freelist, i32 0, i32 %fn1\n")
|
|
emit(" %re = load i32, ptr %fp\n")
|
|
emit(" br label %done\n")
|
|
emit("fresh:\n")
|
|
emit(" %ec = load i32, ptr @L_entc\n")
|
|
emit(" %ec1 = add i32 %ec, 1\n")
|
|
emit(" store i32 %ec1, ptr @L_entc\n")
|
|
emit(" br label %done\n")
|
|
emit("done:\n")
|
|
emit(" %e = phi i32 [ %re, %reuse ], [ %ec, %fresh ]\n")
|
|
emit(" call void @L_reset(i32 %e)\n")
|
|
emit(" %ap = getelementptr inbounds ["); emit(me); emit(" x i32], ptr @L_alive, i32 0, i32 %e\n")
|
|
emit(" store i32 1, ptr %ap\n")
|
|
emit(" ret i32 %e\n}\n\n")
|
|
|
|
emit("define void @L_free_entity(i32 %e) {\nentry:\n")
|
|
emit(" %ap = getelementptr inbounds ["); emit(me); emit(" x i32], ptr @L_alive, i32 0, i32 %e\n")
|
|
emit(" store i32 0, ptr %ap\n")
|
|
emit(" call void @L_reset(i32 %e)\n")
|
|
emit(" %fn = load i32, ptr @L_freen\n")
|
|
emit(" %fp = getelementptr inbounds ["); emit(me); emit(" x i32], ptr @L_freelist, i32 0, i32 %fn\n")
|
|
emit(" store i32 %e, ptr %fp\n")
|
|
emit(" %fn1 = add i32 %fn, 1\n")
|
|
emit(" store i32 %fn1, ptr @L_freen\n")
|
|
emit(" ret void\n}\n\n")
|
|
}
|