ludic/selfhost/backend/game/emit_machine.ludic
Orkuncakilkaya 647dfec334 feat(compiler): list literals, typed compound assignment, file:line diagnostics
- `[a, b, c]` list literals (E_LIST → emit_list); static_type learns
  slice-element, `new T`, list, string and literal kinds
- `x op= y` lowers through the same path as `x = x op y` (emit_bin_vals):
  fixed `*=`/`/=` use the Q16.16 64-bit paths, string `+=` concatenates,
  int→long widens; unary `-` keeps a fixed operand's type (arith_ty)
- one `unescape()` table for "strings", 'chars' and `interpolation`;
  `'\''`, `'\\'`, `'\"'` no longer read as 0; unterminated char literals
  and unexpected characters are errors instead of silently skipped
- every diagnostic is `file:line: error: msg` (g_parse_file / g_err_file,
  Node.file + Node.line set by node()); tok_desc() in expectation errors;
  duplicate `function` names and unknown `phase` names are reported in
  source terms (phase_id used to default unknown phases to Overlay)
- interpolation holes skip braces inside string literals
- hand-IR preludes move from the user `@fn_` prefix to `@lp_` so a user
  `is_ws` / `str_eq` / `path_join` no longer collides at link time
- `@ClearColor(expr)` accepts any constant expression; `Os.pid()` added
  (docs page + inventory); `str_starts()` in support/str
- main.ludic: `else if` flag ladder, char literals, stale script comments
- examples/lang/operators.ludic covers all of the above; os.ludic covers
  Os.pid; docs pages for Os.pid and the Overlay phase; ten changesets
- reseeded: selfhost/ludicc.seed.ll is the new compiler's own fixpoint

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-09-05 01:12:16 +03:00

90 lines
3.7 KiB
Text

# emit_machine.ludic — `machine <state> { state Name = v { .. } }` dispatches on
# an int state store; `become Name` writes the target state's value back. The
# store is either a named program-scope `var` (loaded/stored directly) or, for
# older code, a register index (reg()/set_reg()).
# If `a` names a program-scope `var`, return it; else null (a register index).
function machine_var(a: Node) -> Node {
if a.kind == E_ID {
let g = find_global(a.s)
if (g != null) { if g.kind == N_VAR { return g } }
}
return null
}
# the value a state dispatches on: its explicit `= expr`; else, when the machine's
# store is a `var` declared with an enum type, the variant named like the state
# (`var mode: HeroState` + `state Rolling` -> HeroState.Rolling); else its ordinal.
function machine_state_value(m: Node, state: Node) -> pointer {
if (state.b != null) { return emit_expr(state.b).code }
let gv = machine_var(m.a)
if (gv != null) {
if (gv.ty != null) {
let ord = enum_ordinal(gv.ty, state.s)
if ord >= 0 { return itoa(ord) }
if (find_enum(gv.ty) != null) { perr(`machine state {state.s} is not a variant of enum {gv.ty}`) }
}
}
return itoa(state.ival)
}
function emit_machine(st: Node) -> void {
let gv = machine_var(st.a)
var s = ""
if (gv != null) { s = emit_bind(`load i32, ptr @g_{st.a.s}`) }
else {
let regv = emit_expr(st.a)
s = emit_bind(`call i32 @fn_rt_reg(i32 {regv.code})`)
}
if nmach < len(mach_stk) { mach_stk[nmach] = st } else { push(mach_stk, st) }
nmach += 1
let endl = lbl("smend")
var i = 0
while i < len(st.kids) {
let state = st.kids[i]
let v = machine_state_value(st, state)
let c = emit_bind(`icmp eq i32 {s}, {v}`)
let body = lbl("sbody"); let nxt = lbl("sarm")
emit(" br i1 "); emit(c); emit(", label %"); emit(body); emit(", label %"); emit(nxt); emit("\n")
emit(body); emit(":\n"); g_term = false
emit_block(state.a)
if not g_term { emit(" br label %"); emit(endl); emit("\n") }
emit(nxt); emit(":\n"); g_term = false
i += 1
}
if not g_term { emit(" br label %"); emit(endl); emit("\n") }
emit(endl); emit(":\n"); g_term = false
nmach -= 1
}
# `become Name` — a machine state transition when Name is a state of an
# enclosing `machine`, otherwise a scene transition. A scene transition runs the
# source scene's on-exit, stores the target scene id into @L_scene, and runs the
# target's on-enter (two direct calls and a store — no dispatch table).
function emit_become(st: Node) -> void {
if nmach > 0 { # inside a machine: try a state first
let m = mach_stk[nmach - 1]
var target: Node = null
var i = 0
while i < len(m.kids) { if (m.kids[i].s == st.s) { target = m.kids[i] }; i += 1 }
if (target != null) {
let gv = machine_var(m.a)
let sv = machine_state_value(m, target)
if (gv != null) {
emit(" store i32 "); emit(sv); emit(", ptr @g_"); emit(m.a.s); emit("\n")
} else {
let regv = emit_expr(m.a)
emit(" call void @fn_rt_set_reg(i32 "); emit(regv.code); emit(", i32 "); emit(sv); emit(")\n")
}
return
}
}
let sc = find_scene(st.s) # else a scene transition
if (sc == null) { perr(`become: no state or scene {st.s}`) }
# leave the scene we are in: statically known inside a scene's own handler, else
# (a global handler, an event listener, a function) whichever scene is live now
if (g_cur_scene != null) { emit(" call void @scene_exit_"); emit(g_cur_scene.s); emit("()\n") }
else { emit(" call void @L_scene_leave()\n") }
emit(" store i32 "); emit(itoa(sc.ival)); emit(", ptr @L_scene\n")
emit(" call void @scene_enter_"); emit(sc.s); emit("()\n")
}