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:
parent
3a87d02696
commit
808c4a6f7a
19 changed files with 415 additions and 1 deletions
38
examples/actions/pack.ludic
Normal file
38
examples/actions/pack.ludic
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
# pack.ludic - 0.R: what happened is an action; what it means for a state is that state's reducer.
|
||||
# The entry dispatches; drain_actions() runs every reducer of each action, in order, each given its
|
||||
# own state - and an action a reducer dispatches goes behind the queue.
|
||||
program PackActions {
|
||||
state Pack {
|
||||
items: []int = new []int
|
||||
weight: int = 0
|
||||
}
|
||||
state Log {
|
||||
lines: []string = new []string
|
||||
}
|
||||
action PickUp { item: int, kg: int = 1 }
|
||||
action Drop { item: int }
|
||||
action Overload {}
|
||||
|
||||
reducer Pack on PickUp(p: mut Pack, a: PickUp) {
|
||||
push(p.items, a.item)
|
||||
p.weight += a.kg
|
||||
if p.weight > 10 { dispatch Overload {} }
|
||||
}
|
||||
reducer Log on PickUp(l: mut Log, a: PickUp) { push(l.lines, `picked {a.item}`) }
|
||||
reducer Pack on Drop(p: mut Pack, a: Drop) {
|
||||
var kept = new []int
|
||||
for i in 0 .. len(p.items) { if p.items[i] != a.item { push(kept, p.items[i]) } }
|
||||
p.items = kept
|
||||
}
|
||||
reducer Log on Overload(l: mut Log, a: Overload) { push(l.lines, "too heavy") }
|
||||
|
||||
entry (p: Pack, l: Log) {
|
||||
dispatch PickUp { item: 7 }
|
||||
dispatch PickUp { item: 9, kg: 12 }
|
||||
dispatch Drop { item: 7 }
|
||||
print(len(p.items)) # 0: nothing is reduced until the queue is drained
|
||||
drain_actions()
|
||||
print(`{len(p.items)} {p.items[0]} {p.weight}`)
|
||||
for i in 0 .. len(l.lines) { print(l.lines[i]) }
|
||||
}
|
||||
}
|
||||
24
examples/actions/phases.ludic
Normal file
24
examples/actions/phases.ludic
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
# phases.ludic - 0.R: in the frame loop the queue is drained at the end of every phase, so what the
|
||||
# Input phase dispatches is reduced before Update reads it. The input translates keys to actions and
|
||||
# touches no state of its own.
|
||||
program PhaseActions {
|
||||
state Hero {
|
||||
x: int = 0
|
||||
jumps: int = 0
|
||||
}
|
||||
action Move { dx: int }
|
||||
action Jump {}
|
||||
reducer Hero on Move(h: mut Hero, a: Move) { h.x += a.dx }
|
||||
reducer Hero on Jump(h: mut Hero, a: Jump) { h.jumps += 1 }
|
||||
|
||||
handler Keys phase Input {
|
||||
let k = Input.key()
|
||||
if k == 'd' { dispatch Move { dx: 1 } }
|
||||
if k == 'a' { dispatch Move { dx: -1 } }
|
||||
if k == ' ' { dispatch Jump {} }
|
||||
if k == 'q' { quit() }
|
||||
}
|
||||
handler Show(h: Hero) phase Update {
|
||||
print(`{h.x} {h.jumps}`)
|
||||
}
|
||||
}
|
||||
15
examples/actions/runaway.ludic
Normal file
15
examples/actions/runaway.ludic
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
# runaway.ludic - 0.R: a reducer that dispatches what dispatches it is stopped, by name, after
|
||||
# 64 rounds, rather than spinning
|
||||
program Runaway {
|
||||
state Echo { n: int = 0 }
|
||||
action Ping {}
|
||||
reducer Echo on Ping(e: mut Echo, a: Ping) {
|
||||
e.n += 1
|
||||
dispatch Ping {}
|
||||
}
|
||||
entry {
|
||||
dispatch Ping {}
|
||||
drain_actions()
|
||||
print("not reached")
|
||||
}
|
||||
}
|
||||
5
examples/rejected/dispatch_unknown.ludic
Normal file
5
examples/rejected/dispatch_unknown.ludic
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
# 0.R: only an action is dispatched
|
||||
program DispatchUnknown {
|
||||
action Buy { cost: int }
|
||||
entry { dispatch Sell { cost: 1 } }
|
||||
}
|
||||
7
examples/rejected/reducer_not_action.ludic
Normal file
7
examples/rejected/reducer_not_action.ludic
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
# 0.R: a reducer is on an action
|
||||
program ReducerNotAction {
|
||||
state Pack { n: int = 0 }
|
||||
property Buy { cost: int = 0 }
|
||||
reducer Pack on Buy(p: mut Pack, a: Buy) { p.n += 1 }
|
||||
entry { print(1) }
|
||||
}
|
||||
8
examples/rejected/reducer_two_states.ludic
Normal file
8
examples/rejected/reducer_two_states.ludic
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
# 0.R: a reducer takes its own state and the action; what else it needs rides in the action
|
||||
program ReducerTwoStates {
|
||||
state Pack { n: int = 0 }
|
||||
state Wallet { cash: int = 0 }
|
||||
action Buy { cost: int }
|
||||
reducer Pack on Buy(p: mut Pack, w: mut Wallet) { p.n += 1 }
|
||||
entry { print(1) }
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue