wip(0.S1, 0.S2): state records, mut and read-only state parameters, entry injection, module var refused; ludic migrate state
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
63a1fa1378
commit
1e8b5b0523
35 changed files with 1227 additions and 30 deletions
29
examples/state/counter.ludic
Normal file
29
examples/state/counter.ludic
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
# counter.ludic — 0.S: no function writes a global. The changing data is a `state`; it reaches a
|
||||
# function only as a parameter - `c: mut Tally` to change it, `c: Tally` to read it - and the
|
||||
# runtime supplies it where the program is entered: `entry (c: mut Tally)`, an `@On` listener's
|
||||
# `(c: mut Tally)`, and any function value (`fn tick` is tick with its Tally supplied).
|
||||
#
|
||||
# Running it prints: 11 3 heard 5
|
||||
program Counter {
|
||||
state Tally {
|
||||
n: int = 0
|
||||
heard: int = 0
|
||||
runs: []int = null
|
||||
}
|
||||
event Ping { amount: int }
|
||||
@On(Ping) handler Heard(c: mut Tally) { c.heard += amount }
|
||||
function bump(c: mut Tally, by: int) -> void {
|
||||
c.n = c.n + by
|
||||
if c.runs == null { c.runs = new []int }
|
||||
push(c.runs, by)
|
||||
}
|
||||
function total(c: Tally) -> int { return c.n }
|
||||
function tick(c: mut Tally, by: int) -> void { bump(c, by) }
|
||||
function run_later(f: fn(int) -> void, by: int) -> void { f(by) }
|
||||
entry (c: mut Tally) {
|
||||
bump(c, 3)
|
||||
run_later(fn tick, 8)
|
||||
emit Ping(amount: 5)
|
||||
print(`{total(c)} {len(c.runs) + 1} heard {c.heard}`)
|
||||
}
|
||||
}
|
||||
12
examples/state/port_field.ludic
Normal file
12
examples/state/port_field.ludic
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
# port_field.ludic — 0.S: a port member bound to a state's field, `money: Wallet.cash` - read
|
||||
# through the Wallet the runtime supplies, so a port needs no wrapper and no global.
|
||||
#
|
||||
# Running it prints: 40
|
||||
program PortField {
|
||||
state Wallet { cash: int = 40 }
|
||||
port Purse { money: fn() -> int }
|
||||
bind Purse { money: Wallet.cash }
|
||||
entry {
|
||||
print(Purse.money())
|
||||
}
|
||||
}
|
||||
14
examples/state/tested.ludic
Normal file
14
examples/state/tested.ludic
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
# tested.ludic — 0.S: a test declares the states it uses, `test "name" (c: mut Tally)`, and gets a
|
||||
# fresh one (each test is a process of its own). Its runner prints:
|
||||
# == 2 passed, 0 failed ==
|
||||
program Tested {
|
||||
state Tally { n: int = 0 }
|
||||
function bump(c: mut Tally) -> void { c.n += 1 }
|
||||
test "a bump counts one" (c: mut Tally) {
|
||||
bump(c)
|
||||
expect_eq(c.n, 1)
|
||||
}
|
||||
test "reading needs no mut" (c: Tally) {
|
||||
expect_eq(c.n, 0)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue