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>
61 lines
2.5 KiB
Text
61 lines
2.5 KiB
Text
# emit_save.ludic — save() / load() snapshot of the whole ECS world. The
|
|
# compiler writes the entity/component half itself (it knows the shape) and
|
|
# hands the open file to rt_save_state/rt_load_state for the runtime's own
|
|
# state. Mirrors compiler/back/ir_save.c. save()->@L_save, load()->@L_load.
|
|
|
|
var g_iok: int = 0
|
|
|
|
fn emit_io(fn2: ptr, p: ptr, bytes: ptr) -> void {
|
|
let r = sconcat("%io", itoa(g_iok)); g_iok = g_iok + 1
|
|
emit(" "); emit(r); emit(" = call i64 @"); emit(fn2); emit("(ptr "); emit(p); emit(", i64 1, i64 "); emit(bytes); emit(", ptr %f)\n")
|
|
}
|
|
|
|
fn emit_snapshot_blocks(fn2: ptr) -> void {
|
|
g_iok = 0
|
|
let me = itoa(MAX_ENT)
|
|
emit_io(fn2, "@L_entc", "4")
|
|
emit_io(fn2, "@L_freen", "4")
|
|
emit(" %nalive = mul i64 "); emit(me); emit(", 4\n")
|
|
emit_io(fn2, "@L_alive", "%nalive")
|
|
emit_io(fn2, "@L_freelist", "%nalive")
|
|
emit_io(fn2, "@L_kind", "%nalive")
|
|
var i = 0
|
|
while i < len(prog) { if prog[i].kind == N_VAR { emit_io(fn2, sconcat("@g_", prog[i].s), "4") }; i = i + 1 }
|
|
var ci = 0
|
|
i = 0
|
|
while i < len(prog) {
|
|
if prog[i].kind == N_COMP {
|
|
let c = prog[i].s
|
|
let csz = sconcat("%csz", itoa(ci))
|
|
emit(" "); emit(csz); emit(" = ptrtoint ptr getelementptr (%Cmp_"); emit(c); emit(", ptr null, i32 "); emit(me); emit(") to i64\n")
|
|
emit_io(fn2, sconcat("@S_", c), csz)
|
|
emit_io(fn2, sconcat("@H_", c), me)
|
|
ci = ci + 1
|
|
}
|
|
i = i + 1
|
|
}
|
|
}
|
|
|
|
fn emit_snapshot() -> void {
|
|
emith("@.sav_path = private unnamed_addr constant [10 x i8] c\"ludic.sav\\00\"\n")
|
|
emith("@.sav_wb = private unnamed_addr constant [3 x i8] c\"wb\\00\"\n")
|
|
emith("@.sav_rb = private unnamed_addr constant [3 x i8] c\"rb\\00\"\n")
|
|
let has_save = not ptr_is_null(find_fn("rt_save_state"))
|
|
let has_load = not ptr_is_null(find_fn("rt_load_state"))
|
|
|
|
emit("define void @L_save() {\nentry:\n")
|
|
emit(" %f = call ptr @fopen(ptr @.sav_path, ptr @.sav_wb)\n")
|
|
emit(" %bad = icmp eq ptr %f, null\n")
|
|
emit(" br i1 %bad, label %out, label %go\ngo:\n")
|
|
emit_snapshot_blocks("fwrite")
|
|
if has_save { emit(" call void @fn_rt_save_state(ptr %f)\n") }
|
|
emit(" %c = call i32 @fclose(ptr %f)\n br label %out\nout:\n ret void\n}\n\n")
|
|
|
|
emit("define i32 @L_load() {\nentry:\n")
|
|
emit(" %f = call ptr @fopen(ptr @.sav_path, ptr @.sav_rb)\n")
|
|
emit(" %bad = icmp eq ptr %f, null\n")
|
|
emit(" br i1 %bad, label %miss, label %go\nmiss:\n ret i32 0\ngo:\n")
|
|
emit_snapshot_blocks("fread")
|
|
if has_load { emit(" call void @fn_rt_load_state(ptr %f)\n") }
|
|
emit(" %c = call i32 @fclose(ptr %f)\n ret i32 1\n}\n\n")
|
|
}
|