ludic/selfhost/check/check_expr.ludic
Orkuncakilkaya e4265f5fdb feat(lang): 27.1 - a state machine as data: @Machine(Record.field) on a registry of transitions
A registry marked `@Machine(Deer.mood)` is the transitions of a machine over that enum field of the
records a state's Table<Deer> holds. Its record has from and to (the enum's variants), on: string (an
action's name, "" for a transition the tick asks), guard: fn(Row<Deer>, reads...) -> bool and
enter: fn(Row<Deer>, reads...) -> void; the states are the enum's variants and the start is the
field's default. The rows are data (an .lres or defs), the names the studio already edits.

Written by the compiler (machines.ludic, machines_write.ludic): for each action an `on` names, a row
reducer in the registry's file (named ..__machine__DeerSteps, so it sits beside the program's own
row reducer on the same action, after it): the row's state, the first transition from it on that
action whose guard passes, the field set, enter run - guards and enters called by name. When a row
leaves a state on a guard alone, `state DeerStepsMachine` (the kept row view) and
deer_steps_tick(m: mut DeerStepsMachine, s: mut Herd, reads...), one transition a row a tick.
Nothing allocates.

The table is the whole machine: the field written anywhere else - an assignment, or a `machine`
block's become over it - is a type error (check_stmt.ludic, ck_machine_write). Guards and enters take
the row first, are the record's module's, keep a row reducer's rules (and may be handed the row);
a guard writes nothing through it. The graph is checked, each error at its row (in the .lres when
the rows are there): a state never reached from the start, a state with no way out, an `on` naming
no action or an action with no @Target, a self-transition with no guard, two ways out of a state on
one trigger behind an unguarded first. Also refused: @Machine off a registry, a field that is not a
plain enum with a default, a @Column field, no table (or two) of the record, a transitions record of
another shape, a machine outside its table's state's module.

ludic schema's code section gains `machines` (registry, record, field, enum, table, start, states,
actions, tick, module, at); ludic deps names a machine's reducer `reducer Deer in Herd.deer on Spook
(machine DeerSteps)`. vocab @Machine; docs annot-machine, kw-machine; LANGUAGE.md "A machine as
data"; examples actions/machine (+ deer_steps.lres) and ten rejects; test.ludic feat, reject and
schema cases (not run); changes/machines.md. Reseeded; bootstrap-cfree fixpoint holds (317642
lines); Maroon Lake's `ludic build --check` is clean against this tree.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-30 16:37:37 +03:00

183 lines
5.9 KiB
Text

# check_expr.ludic — L4: the type of an expression, and every mix-up inside it. Each form works
# out its operands' types first, so an error deep in an argument is found as well as one at the top.
function ck_field(r: Node, name: pointer) -> pointer {
var i = 0
while i < len(r.kids) {
if (r.kids[i].s == name) { return r.kids[i].ty }
i += 1
}
return null
}
# anything the checker does not type is still walked for the errors inside it
function ck_walk(e: Node) -> void {
if e == null { return }
ck_any(e.a)
ck_any(e.b)
ck_any(e.c)
var i = 0
while i < len(e.kids) {
ck_any(e.kids[i])
i += 1
}
}
function ck_any(n: Node) -> void {
if n == null { return }
if n.kind == N_BLOCK { ck_block(n); return }
if n.kind == E_FINIT { ck_expr(n.a); return }
if (n.kind >= S_LET and n.kind <= S_BECOME) or n.kind == S_EMIT or n.kind == S_UNSAFE { ck_stmt(n); return }
ck_expr(n)
}
function ck_expr(e: Node) -> pointer {
if e == null { return "?" }
let ex = ck_expect
ck_expect = null
let k = e.kind
if k == E_INT {
if e.s != null { return "long" } # a literal past 2^31 - 1
return "int"
}
if k == E_FLOAT {
if is_float_file(e.file) { return "float" }
return "fixed"
}
if k == E_STR {
if e.ival == KEY_LIT or e.ival == KEY_PLURAL { return "Key" } # k"..." / kn"..." (i18n.ludic)
return "string"
}
if k == E_SLICE { ck_walk(e); return "string" }
if k == E_BOOL { return "bool" }
if k == E_NULL { return "null" }
if k == E_ID { return ck_id(e) }
if k == E_FNREF {
let f = ck_fn(e.s)
ck_vis(f, e.s, e)
if f != null { return fn_sig_of(f) }
# the emitter's own refusal, here too, so a check-only build finds it (a bind to a function
# that is gone)
if find_fn(e.s) == null { ck_err("fnref", e, `fn {e.s}: no function called {e.s}`) }
return "?"
}
if k == E_NEW { return ck_new(e) }
if k == E_LIST { return ck_list(e) }
if k == E_BIN { return ck_bin(e) }
if k == E_UN { return ck_un(e) }
if k == E_MEMBER { return ck_member(e) }
if k == E_INDEX { return ck_index_of(e) }
if k == E_CALL {
ck_call_expect = ex
return ck_call(e)
}
if k == S_EMIT { ck_emit(e); return "?" }
ck_walk(e)
return "?"
}
function ck_id(e: Node) -> pointer {
let li = ck_local(e.s)
if li >= 0 {
mg_local_use(e, ck_tys[li]) # 0.S2 --migrate-prune: a state used here
return ck_tys[li]
}
let g = ck_global(e.s)
if g != null {
mg_ref(e, g)
ck_vis(g, e.s, e)
return g.ty
}
let fv = mg_field_var(e.s) # 0.S2: a var a package moved into its state
if fv != null {
mg_ref(e, fv)
return fv.ty
}
return "?"
}
# --check: the module rules (uses, export, ports - emit_vis.ludic) asked here, where every reference
# is resolved, since a check-only build stops before the emitter that asks them otherwise
var g_check_only: bool = false
var ck_vis_file: pointer = null # a global's initializer is its declaration's file's code (as emitted)
var ck_vis_skip: bool = false # a port's bind, checked where it was written (ports.ludic)
function ck_vis(d: Node, name: pointer, at: Node) -> void {
if not g_check_only or ck_vis_skip or d == null or at == null or at.file == null { return }
g_err_file = at.file
if ck_vis_file != null { g_err_file = ck_vis_file }
g_err_line = at.line
vis_check(d, name)
}
function ck_new(e: Node) -> pointer {
let r = ck_record(e.s)
ck_vis(r, e.s, e)
if r == null and g_check_only and port_find(e.s) >= 0 {
ck_err("port", e, `{e.s} is a port: it is filled once with 'bind {e.s} {{ ... }}' where the program is put together, not made with new`)
}
if e.a != null {
var i = 0
while i < len(e.a.kids) {
let fi = e.a.kids[i]
let vt = ck_expr(fi.a)
if r != null and fi.kind == E_FINIT {
let ft = ck_field(r, fi.s)
if ft != null { ck_give(ft, vt, fi.a, `field {fi.s} of {e.s}`) }
if ft == null { ck_err("field", fi.a, `{gen_show(e.s)} has no field {fi.s}`) }
}
i += 1
}
}
return e.s
}
function ck_list(e: Node) -> pointer {
var t: pointer = "?"
var i = 0
let saved = ck_vis_file
while i < len(e.kids) {
# a registry's entries are their defs' files' code (emit_list_elem)
if saved != null and e.kids[i].file != null { ck_vis_file = e.kids[i].file }
let et = ck_expr(e.kids[i])
ck_vis_file = saved
if ck_unknown(t) { t = et }
else { ck_give(t, et, e.kids[i], `an element of a []{t}`) }
i += 1
}
if ck_unknown(t) or (t == "null") { return "?" }
return "[]" + t
}
function ck_un(e: Node) -> pointer {
let t = ck_expr(e.a)
if (e.s == "not") or (e.s == "!") {
ck_cond(e.a, t)
return "bool"
}
if (e.s == "-") and (t == "bool") { ck_err("bool", e, "- on a bool") }
return t
}
function ck_member(e: Node) -> pointer {
let b = e.a
if b.kind == E_ID and ck_local(b.s) < 0 and ck_global(b.s) == null {
# a namespace, an enum or a component name: Math.PI, Color.Red, Thing.of
let en = ck_enum(b.s)
if en != null and not enum_is_tagged(en) { return b.s }
return "?"
}
let bt = ck_expr(b)
ck_mem_node = e # 27.1: what a write to it writes into (ck_machine_write)
ck_mem_base = bt
if ck_unknown(bt) { return "?" }
let r = ck_record(bt)
if r != null {
let ft = ck_field(r, e.s)
if ft != null { return ft }
}
return "?"
}
function ck_index_of(e: Node) -> pointer {
let bt = ck_expr(e.a)
let it = ck_expr(e.b)
if ck_is_raw(bt) { ck_raw(e, `indexing {ck_a(bt)}`) }
if not ck_unknown(it) and not ck_is_int(it) { ck_err("index", e.b, `an index wants an int and this is {ck_a(it)}`) }
if ck_unknown(bt) { return "?" }
if is_slice_ty(bt) { return slice_elem(bt) }
if (bt == "string") or (bt == "words") { return "int" }
if (bt == "floats") { return "float" }
if (bt == "doubles") { return "double" }
if (bt == "fixeds") { return "fixed" }
if (bt == "pointers") { return "pointer" }
return "?"
}