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>
104 lines
3.3 KiB
Text
104 lines
3.3 KiB
Text
# emit_decl.ludic — functions, main, and the whole-program driver. A function's
|
|
# body is built into a scratch buffer so entry-block allocas can be spliced in
|
|
# ahead of it (see emit_alloca). Returns go through a %retval slot + `ret` label.
|
|
|
|
fn emit_params_sig(d: Node) -> void {
|
|
var i = 0
|
|
while i < len(d.kids) {
|
|
if i > 0 { emit(", ") }
|
|
emit(llty(d.kids[i].ty)); emit(" %arg_"); emit(d.kids[i].s)
|
|
i = i + 1
|
|
}
|
|
}
|
|
|
|
fn emit_fn(d: Node) -> void {
|
|
ll_t = 0; ll_lbl = 0; g_term = false; loc_reset(); nloop = 0
|
|
ret_ty = d.ty
|
|
let fbody = buf_new()
|
|
falloc = buf_new()
|
|
let saved = code
|
|
code = fbody
|
|
let rl = llty(ret_ty)
|
|
if not streq(rl, "void") { buf_puts(falloc, " %retval = alloca "); buf_puts(falloc, rl); buf_puts(falloc, "\n") }
|
|
# params: store each incoming argument into a stack slot
|
|
var i = 0
|
|
while i < len(d.kids) {
|
|
let p = d.kids[i]
|
|
let slot = emit_alloca(llty(p.ty))
|
|
emit(" store "); emit(llty(p.ty)); emit(" %arg_"); emit(p.s); emit(", ptr "); emit(slot); emit("\n")
|
|
loc_push(p.s, slot, p.ty)
|
|
i = i + 1
|
|
}
|
|
emit_block(d.a)
|
|
if not g_term { emit(" br label %ret\n") }
|
|
emit("ret:\n")
|
|
if streq(rl, "void") { emit(" ret void\n") }
|
|
else { let r = emit_bind(sconcat("load ", sconcat(rl, ", ptr %retval"))); emit(" ret "); emit(rl); emit(" "); emit(r); emit("\n") }
|
|
code = saved
|
|
emit("define "); emit(rl); emit(" @fn_"); emit(d.s); emit("("); emit_params_sig(d); emit(") {\nentry:\n")
|
|
emit(buf_str(falloc))
|
|
emit(buf_str(fbody))
|
|
emit("}\n\n")
|
|
}
|
|
|
|
fn emit_main(d: Node) -> void {
|
|
ll_t = 0; ll_lbl = 0; g_term = false; loc_reset(); nloop = 0
|
|
ret_ty = "int"
|
|
let fbody = buf_new()
|
|
falloc = buf_new()
|
|
let saved = code
|
|
code = fbody
|
|
buf_puts(falloc, " %retval = alloca i32\n")
|
|
emit(" store i32 %argc, ptr @L_argc\n")
|
|
emit(" store ptr %argv, ptr @L_argv\n")
|
|
emit(" store i32 0, ptr %retval\n")
|
|
emit_block(d.a)
|
|
if not g_term { emit(" br label %ret\n") }
|
|
emit("ret:\n")
|
|
let r = emit_bind("load i32, ptr %retval")
|
|
emit(" ret i32 "); emit(r); emit("\n")
|
|
code = saved
|
|
emit("define i32 @main(i32 %argc, ptr %argv) {\nentry:\n")
|
|
emit(buf_str(falloc))
|
|
emit(buf_str(fbody))
|
|
emit("}\n")
|
|
}
|
|
|
|
fn emit_program() -> void {
|
|
head = buf_new()
|
|
code = buf_new()
|
|
loc_name = new []ptr; loc_reg = new []ptr; loc_ty = new []ptr; loc_mut = new []int
|
|
brk_lbl = new []ptr; cnt_lbl = new []ptr
|
|
self_stk = new []ptr
|
|
mach_stk = new []Node
|
|
emit_header()
|
|
if has_ecs() { emit_ecs_storage() }
|
|
var i = 0
|
|
while i < len(prog) { if prog[i].kind == N_FN { emit_fn(prog[i]) }; i = i + 1 }
|
|
if has_ecs() { emit_ecs_allocator(); emit_snapshot() }
|
|
if has_ui() { emit_ui_build() }
|
|
if has_systems() { emit_game_main() }
|
|
else {
|
|
i = 0
|
|
while i < len(prog) { if prog[i].kind == N_MAIN { emit_main(prog[i]) }; i = i + 1 }
|
|
}
|
|
}
|
|
|
|
# Flush the emitted IR. With a null path it goes to stdout (the pipe the shell
|
|
# drivers read); with a path it is written to that file so ludicc can hand it to
|
|
# clang itself.
|
|
fn ir_flush(path: ptr) -> bool {
|
|
let h = buf_str(head)
|
|
let c = buf_str(code)
|
|
if ptr_is_null(path) {
|
|
print_str(h)
|
|
print_str(c)
|
|
return true
|
|
}
|
|
let f = file_open(path, "wb")
|
|
if ptr_is_null(f) { return false }
|
|
file_write(f, h, slen(h))
|
|
file_write(f, c, slen(c))
|
|
file_close(f)
|
|
return true
|
|
}
|