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>
This commit is contained in:
Orkun ÇAKILKAYA 2026-09-30 16:37:37 +03:00
parent a637aec63e
commit e4265f5fdb
33 changed files with 131217 additions and 109412 deletions

View file

@ -7,6 +7,8 @@
# row_reducers record, table (State.path), state, action, target (its @Target field), predicted,
# net, module, at row_verbs name, record, module, at (27.3)
# actions also carry their target: the @Target field, or null
# machines registry, record, field, enum, table (State.path), start, states (the enum's
# variants), actions (those its rows' `on` name), tick (or null), module, at (27.1)
# events name, module, at, cancellable, net (@ToServer / @ToClients), emits (sites) and
# listeners (the @On handlers: handler, module, at)
# ports name, module, at, bound, members (name, type, default as written, required)
@ -74,6 +76,8 @@ function sc_code(b: Buf) -> void {
cx_row_reducers(b)
buf_puts(b, "\n ],\n \"row_verbs\": [")
cx_row_verbs(b)
buf_puts(b, "\n ],\n \"machines\": [")
cx_machines(b)
buf_puts(b, "\n ],\n \"dispatch\": [")
cx_dispatch(b)
buf_puts(b, "\n ],\n \"events\": [")
@ -318,6 +322,60 @@ function cx_row_reducers(b: Buf) -> void {
i += 1
}
}
# 27.1: each @Machine registry: the field it drives, its table, and what the compiler wrote for it
function cx_machines(b: Buf) -> void {
let keys = new []pointer
var i = 0
while i < len(g_mc_reg) {
let v = g_rg_var[reg_find(g_mc_reg[i])]
if cx_written(v) { push(keys, sc_key(g_mc_reg[i], v.file, v.line)) } else { push(keys, null) }
i += 1
}
let xs = cx_order(keys)
i = 0
while i < len(xs) {
let m = xs[i]
let v = g_rg_var[reg_find(g_mc_reg[m])]
sc_sep(b, i == 0, " ")
buf_puts(b, "{\"registry\": ")
jq_put(b, g_mc_reg[m])
buf_puts(b, ", \"record\": ")
jq_put(b, g_mc_rec[m])
buf_puts(b, ", \"field\": ")
jq_put(b, g_mc_field[m])
buf_puts(b, ", \"enum\": ")
sc_str(b, g_mc_enum[m])
buf_puts(b, ", \"table\": ")
if g_mc_state[m] == null { buf_puts(b, "null") } else { jq_put(b, `{g_mc_state[m]}.{g_mc_path[m]}`) }
buf_puts(b, ", \"start\": ")
sc_str(b, g_mc_start[m])
buf_puts(b, ", \"states\": ")
cx_mc_states(b, g_mc_enum[m])
buf_puts(b, ", \"actions\": ")
cx_list(b, g_mc_acts[m])
buf_puts(b, ", \"tick\": ")
sc_str(b, g_mc_tick[m])
buf_puts(b, ", ")
cx_module(b, v.file)
buf_puts(b, ", ")
cx_at(b, v)
buf_puts(b, "}")
i += 1
}
}
function cx_mc_states(b: Buf, en: pointer) -> void {
buf_puts(b, "[")
if en != null {
let d = mc_enum_decl(en)
var i = 0
while d != null and i < len(d.kids) {
if i > 0 { buf_puts(b, ", ") }
jq_put(b, d.kids[i].s)
i += 1
}
}
buf_puts(b, "]")
}
function cx_row_verbs(b: Buf) -> void {
let keys = new []pointer
var i = 0