ludic/selfhost/emit_ui.ludic
Orkuncakilkaya e1d7797e29 Phase 6f: let = immutable, var = mutable (binding-only)
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>
2026-08-27 23:52:59 +03:00

125 lines
4.5 KiB
Text

# emit_ui.ludic — a `ui` block is data: the compiler flattens the widget tree
# and emits @ui_build(), which hands every property to the Ludic UI runtime via
# rt_ui_set(index, key, value). Mirrors compiler/back/ir_ui.c. Layout/drawing/
# focus all live in runtime/native/ui.ludic.
var uiw: []Node # flattened widgets, pre-order
var uiw_parent: []int
var ui_roots: []int # first-widget index of each ui block
fn ui_wtype(w: Node) -> int {
let s = w.s
if streq(s, "panel") { return 0 }; if streq(s, "col") { return 1 }; if streq(s, "row") { return 2 }
if streq(s, "label") { return 3 }; if streq(s, "button") { return 4 }
if streq(s, "image") { return 5 }; if streq(s, "spacer") { return 6 }
return 0
}
fn ui_prop(w: Node, key: ptr) -> Node {
var i = 0
while i < len(w.b.kids) { if streq(w.b.kids[i].s, key) { return w.b.kids[i].a }; i = i + 1 }
return ptr_null()
}
fn ui_flatten(w: Node, parent: int) -> void {
let idx = len(uiw)
push(uiw, w); push(uiw_parent, parent)
var i = 0
while i < len(w.kids) { ui_flatten(w.kids[i], idx); i = i + 1 }
}
fn ui_flatten_all() -> void {
uiw = new []Node; uiw_parent = new []int; ui_roots = new []int
var i = 0
while i < len(prog) {
if prog[i].kind == N_UI and prog[i].ival == 1 { push(ui_roots, len(uiw)); ui_flatten(prog[i].a, 0 - 1) }
i = i + 1
}
}
fn has_ui() -> bool {
var i = 0
while i < len(prog) { if prog[i].kind == N_UI and prog[i].ival == 1 { return true }; i = i + 1 }
return false
}
# index of a UI_<name>: a ui block's root, or a widget's id=
fn ui_index_of(nm: ptr) -> int {
let s = substr(nm, 3, slen(nm) - 3) # strip "UI_"
let u = 0; var bi = 0
var i = 0
while i < len(prog) {
if prog[i].kind == N_UI and prog[i].ival == 1 { if streq(prog[i].s, s) { return ui_roots[bi] }; bi = bi + 1 }
i = i + 1
}
i = 0
while i < len(uiw) {
let idp = ui_prop(uiw[i], "id")
if not ptr_is_null(idp) { if idp.kind == E_ID and streq(idp.s, s) { return i } }
i = i + 1
}
return 0
}
fn is_ui_ident(nm: ptr) -> bool {
return slen(nm) > 3 and peek8(nm, 0) == 85 and peek8(nm, 1) == 73 and peek8(nm, 2) == 95 # "UI_"
}
fn ll_ui_set(idx: int, key: int, val: ptr) -> void {
emit(" call void @fn_rt_ui_set(i32 "); emit(itoa(idx)); emit(", i32 "); emit(itoa(key)); emit(", i32 "); emit(val); emit(")\n")
}
fn ui_prop_key(k: ptr) -> int {
if streq(k,"w") { return 2 }; if streq(k,"h") { return 3 }; if streq(k,"x") { return 4 }; if streq(k,"y") { return 5 }
if streq(k,"pad") { return 7 }; if streq(k,"gap") { return 8 }; if streq(k,"bg") { return 9 }; if streq(k,"fg") { return 10 }
if streq(k,"border") { return 11 }; if streq(k,"grow") { return 13 }; if streq(k,"font") { return 14 }; if streq(k,"size") { return 15 }
if streq(k,"inset") { return 16 }; if streq(k,"focus") { return 17 }
return 0 - 1
}
fn emit_ui_build() -> void {
ui_flatten_all()
ll_t = 0; ll_lbl = 0; g_term = false; loc_reset(); nloop = 0
let fbody = buf_new()
falloc = buf_new()
let saved = code
code = fbody
emit(" call void @fn_rt_ui_reset(i32 "); emit(itoa(len(uiw))); emit(")\n")
var i = 0
while i < len(uiw) {
let w = uiw[i]
let t = ui_wtype(w)
ll_ui_set(i, 0, itoa(t))
ll_ui_set(i, 1, itoa(uiw_parent[i]))
if t == 4 { ll_ui_set(i, 17, "1") }
var p = 0
while p < len(w.b.kids) {
let pr = w.b.kids[p]
let k = pr.s
let v = pr.a
if streq(k, "id") { p = p + 1; continue }
if streq(k, "text") {
let sv = emit_expr(v)
emit(" call void @fn_rt_ui_static_text(i32 "); emit(itoa(i)); emit(", ptr "); emit(sv.code); emit(")\n")
} else { if streq(k, "skin") or streq(k, "image") {
let sv = emit_expr(v)
let r = emit_bind(sconcat("call i32 @fn_rt_image_load(ptr ", sconcat(sv.code, ")")))
if streq(k, "skin") { ll_ui_set(i, 20, r) } else { ll_ui_set(i, 19, r) }
} else { if streq(k, "align") {
if v.kind == E_ID {
var a = 0
if streq(v.s, "center") { a = 1 }; if streq(v.s, "end") { a = 2 }
ll_ui_set(i, 12, itoa(a))
} else { let vv = emit_expr(v); ll_ui_set(i, 12, vv.code) }
} else {
let key = ui_prop_key(k)
if key >= 0 {
let vv = emit_expr(v)
ll_ui_set(i, key, vv.code)
if key == 4 or key == 5 { ll_ui_set(i, 6, "1") }
}
} } }
p = p + 1
}
i = i + 1
}
code = saved
emit("define void @ui_build() {\nentry:\n")
emit(buf_str(falloc))
emit(buf_str(fbody))
emit(" ret void\n}\n\n")
}