compiler(0.R): a dispatched action's record is the queue's to keep - one list per action, filled in place, all free again when the drain ends
dispatch lowered to ludic_act_push(k, new A { ... }): a fresh record every dispatch, and an input
system dispatches Move and FrameTime every frame. The queue now keeps a list per action
(kept<k>, used<k>); ludic_act_new__A hands out the next (made only when all are queued), new's
emitter fills it field by field as it fills a fresh one (every field, default or given), and
drain_actions sets every used<k> back to 0 once the queue is empty. A reducer only ever reads
its action during the drain, so nothing sees a record after it is reused. Actions are visible
to the program's file, where the queue lives, as reducers already were.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
3e3c6088fb
commit
93bc8a90db
4 changed files with 65 additions and 7 deletions
|
|
@ -26,6 +26,7 @@ const ACTION_PASSES: int = 64
|
|||
function parse_action() -> void {
|
||||
let c = parse_component()
|
||||
c.uns = 3
|
||||
c.vis = 1 # the program's own file makes and keeps its records
|
||||
push(prog, c)
|
||||
push(g_act_names, c.s)
|
||||
push(g_act_nodes, c)
|
||||
|
|
@ -55,6 +56,14 @@ function parse_dispatch() -> Node {
|
|||
let nw = node(E_NEW)
|
||||
nw.s = name
|
||||
if is_op("{") { nw.a = record() }
|
||||
# its record is the queue's: one of the action's kept records, filled as `new` fills a fresh one
|
||||
let take = node(E_CALL)
|
||||
let tc = node(E_ID)
|
||||
tc.s = `ludic_act_new__{name}`
|
||||
tc.pos = -1
|
||||
take.a = tc
|
||||
take.pos = -1
|
||||
nw.b = take
|
||||
let call = node(E_CALL)
|
||||
let callee = node(E_ID)
|
||||
callee.s = "ludic_act_push"
|
||||
|
|
@ -145,13 +154,20 @@ function red_check(i: int) -> void {
|
|||
}
|
||||
# the queue, its push and its drain: each action's reducers in the order of their states' names
|
||||
function act_src() -> pointer {
|
||||
var src = "export state LudicActions {\n kinds: []int = new []int\n vals: []pointer = new []pointer\n head: int = 0\n depth: int = 0\n}\n"
|
||||
var src = "export state LudicActions {\n kinds: []int = new []int\n vals: []pointer = new []pointer\n head: int = 0\n depth: int = 0\n"
|
||||
var a = 0
|
||||
while a < len(g_act_names) {
|
||||
src = src + ` kept{itoa(a)}: []{g_act_names[a]} = new []{g_act_names[a]}\n used{itoa(a)}: int = 0\n`
|
||||
a += 1
|
||||
}
|
||||
src = src + "}\n"
|
||||
src = src + act_new_src()
|
||||
src = src + "export function ludic_act_push(q: mut LudicActions, k: int, v: pointer) -> void {\n push(q.kinds, k)\n push(q.vals, v)\n}\n"
|
||||
src = src + "export function drain_actions(q: mut LudicActions) -> void {\n if q.depth > 0 or len(q.kinds) == 0 { return }\n q.depth = 1\n var pass = 0\n var round_end = len(q.kinds)\n"
|
||||
src = src + " while q.head < len(q.kinds) {\n if q.head == round_end {\n pass += 1\n round_end = len(q.kinds)\n"
|
||||
src = src + ` if pass >= {itoa(ACTION_PASSES)} {{ ludic_act_runaway(q.kinds[q.head]) }}\n }}\n`
|
||||
src = src + " let k = q.kinds[q.head]\n let v = q.vals[q.head]\n q.head += 1\n"
|
||||
var a = 0
|
||||
a = 0
|
||||
while a < len(g_act_names) {
|
||||
let rs = red_sorted(g_act_names[a])
|
||||
if len(rs) > 0 {
|
||||
|
|
@ -165,7 +181,13 @@ function act_src() -> pointer {
|
|||
}
|
||||
a += 1
|
||||
}
|
||||
src = src + " }\n List.clear(q.kinds)\n List.clear(q.vals)\n q.head = 0\n q.depth = 0\n}\n" # in place: a drain a phase, seven a frame, allocates nothing
|
||||
src = src + " }\n List.clear(q.kinds)\n List.clear(q.vals)\n q.head = 0\n q.depth = 0\n" # in place: a drain a phase, seven a frame, allocates nothing
|
||||
a = 0
|
||||
while a < len(g_act_names) {
|
||||
src = src + ` q.used{itoa(a)} = 0\n` # every record read: each is free again
|
||||
a += 1
|
||||
}
|
||||
src = src + "}\n"
|
||||
src = src + "export function ludic_act_runaway(k: int) -> void {\n var name = \"?\"\n"
|
||||
a = 0
|
||||
while a < len(g_act_names) {
|
||||
|
|
@ -175,6 +197,19 @@ function act_src() -> pointer {
|
|||
src = src + " print(`actions: {name} is still being dispatched after " + itoa(ACTION_PASSES) + " rounds of reducers - a reducer dispatches what dispatches it`)\n exit(1)\n}\n"
|
||||
return src
|
||||
}
|
||||
# each action's next kept record, made only when all it has are queued: `dispatch` fills it in
|
||||
# place, and the drain hands them all back once every reducer has read them
|
||||
function act_new_src() -> pointer {
|
||||
var src = ""
|
||||
var a = 0
|
||||
while a < len(g_act_names) {
|
||||
let n = g_act_names[a]
|
||||
let k = itoa(a)
|
||||
src = src + `export function ludic_act_new__{n}(q: mut LudicActions) -> {n} {{\n if q.used{k} >= len(q.kept{k}) {{ push(q.kept{k}, new {n}) }}\n q.used{k} += 1\n return q.kept{k}[q.used{k} - 1]\n}}\n`
|
||||
a += 1
|
||||
}
|
||||
return src
|
||||
}
|
||||
function red_sorted(act: pointer) -> []int {
|
||||
let out = new []int
|
||||
var i = 0
|
||||
|
|
@ -198,5 +233,5 @@ function red_sorted(act: pointer) -> []int {
|
|||
# a call to the queue from anywhere: the queue is the runtime's to supply, like a state an entry point
|
||||
# is given (drain_actions() from a ludic.base runner or a program's own loop)
|
||||
function is_action_builtin(name: pointer) -> bool {
|
||||
return (name == "drain_actions") or (name == "ludic_act_push")
|
||||
return (name == "drain_actions") or (name == "ludic_act_push") or str_starts(name, "ludic_act_new__")
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue