feat(lang): 0.R1 - actions and reducers

action Name { fields } is a typed record; reducer State on Action(s: mut State, a: Action) { ... }
in the module that owns the state takes exactly that state and the action (a second state is
refused); dispatch Action { fields } queues one from anywhere, the queue supplied by the runtime.
The queue is drained at the end of every phase of the frame loop, after every phase of ludic.base's
core_tick_all, and by drain_actions(): in dispatch order, each action's reducers in the order of
their states' names, an action a reducer dispatches queued behind, a queue still growing after 64
rounds stopped with the action named. Examples actions/pack, phases, runaway; rejects for a second
state, a reducer on a non-action and an unknown dispatch; ludic.base's actions_test; LANGUAGE.md.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Orkun ÇAKILKAYA 2026-09-25 19:13:46 +03:00
parent 3a87d02696
commit 808c4a6f7a
19 changed files with 415 additions and 1 deletions

View file

@ -967,6 +967,7 @@ function emit_call(e: Node) -> Val {
vis_check(fn2, name)
state_inject_generated(e, fn2) # 0.S: a call the compiler wrote gets its states
state_inject_runtime(e, fn2) # and a call into the runtime
if is_action_builtin(fn2.s) { state_inject(e, fn2) } # 0.R: the action queue
call_fill_defaults(e, fn2) # L11 - for code the checker does not walk
reorder_named(e, param_labels(fn2))
# evaluate args first (their IR is emitted before the call instruction), coercing
@ -1119,6 +1120,6 @@ function emit_expr(e: Node) -> Val {
let c = emit_bind(`icmp eq i32 {a.code}, 0`)
return val(emit_bind(`zext i1 {c} to i32`), "bool")
}
perr("cannot emit expression")
perr(`cannot emit expression (node kind {itoa(e.kind)})`)
return val("0", "int")
}

View file

@ -38,6 +38,8 @@ function vis_check(d: Node, what0: pointer) -> void {
# 0.S: a state's instance, supplied by the runtime (a component's or a view's glue, an entry's
# parameters): the code that names it only passes it on
if d.kind == N_VAR and d.uns == 1 and is_state_ty(d.ty) and len(d.s) > 6 and (d.s[0 .. 6] == "state$") { return }
# 0.R: the action queue and the reducers are the runtime's to call, from any module
if d.kind == N_FN and (is_action_builtin(d.s) or (len(d.s) > 14 and (d.s[0 .. 14] == "ludic_reduce__"))) { return }
let what = vis_plain(what0)
var here = g_err_file
if here == null { return }

View file

@ -345,6 +345,7 @@ function emit_calls_for_phase(phase: pointer) -> void {
emit_engine_systems_for_phase(phase) # engine-owned systems run after every user handler
emit_dyn_systems_for_phase(phase) # #64: mod-registered systems run last
if (phase == "Overlay") { emit_scene_menu_render() } # a scene's `shows` menu paints last
if len(g_act_names) > 0 { emit(` call void {fn_sym("drain_actions")}()\n`) } # 0.R: this phase's actions, reduced
}
# the menu of the live `scene X shows Menu`, drawn on top of everything else