plum

#treesitter#compiler#wasm

git clone https://git.pyrossh.dev/plum

A statically typed, imperative programming language inspired by rust, python


430a13e — Peter John 2026-11-10T22:31:03+05:30
std/game: annotated locals the checker no longer needs, stale walk comments
docs/game.md CHANGED
@@ -185,7 +185,9 @@ struct PlayScreen[Screen<State, Msg>](id: Int) =
185
185
  gfx.clear(io, Color.Black)
186
186
  shown.draw(io)
187
187
 
188
- fun main(io: Io) -> Game<State, Msg> =
188
+ fun main(io: Io) -> Game<State, Msg> = start()
189
+
190
+ fun start() -> Game<State, Msg> =
189
191
  tree: SceneTree<Msg> := SceneTree.ofScreens([
190
192
  RoseTree.of(TitleScreen(id: 10), [RoseTree.leaf(Label(id: 1, x: 20.0, y: 20.0, text: "Press Enter", size: 24.0, color: Color.White))]),
191
193
  RoseTree.of(PlayScreen(id: 20), [RoseTree.leaf(Label(id: 1, x: 20.0, y: 20.0, text: "run 0", size: 24.0, color: Color.Yellow))])])
std/game/Clickable.plum CHANGED
@@ -6,8 +6,8 @@ import std/math/Vec2
6
6
  # An actor the pointer can hit, as well as an `Actor<M>`: `struct Button<M>[Actor<M>,
7
7
  # Clickable](..)`. `hit` says whether `p`, in the actor's own coordinates, is on it. Only a
8
8
  # `Clickable` actor gets pointer events (`SceneTree.input`) and is found by
9
- # `SceneTree.hit`; the tree's pointer walk skips every other actor without a test, and
9
+ # `SceneTree.hit`; the tree's pointer walk skips every other actor with one type-tag test,
10
- # computes no transform for one without children. A `hit` on an actor that isn't
10
+ # and computes no transform for one without children. A `hit` on an actor that isn't
11
11
  # `Clickable` is a plain method the tree never calls.
12
12
  trait Clickable =
13
13
  fun hit(self, p: Vec2) -> Bool
std/game/Game.plum CHANGED
@@ -188,9 +188,8 @@ struct Game<S: Reducer<M>, M>(
188
188
  name := chosen<S, M>(tree, state.screen())
189
189
  if Some(e) := screensError<S, M>(tree, name)
190
190
  crash e
191
- none: Option<Shift> := None
192
191
  Game(
193
- tree: tree, store: Store.of(state), current: name, screenKey: keyOf<S, M>(tree, name), unknown: "", shift: none, first: tree, initial: state,
192
+ tree: tree, store: Store.of(state), current: name, screenKey: keyOf<S, M>(tree, name), unknown: "", shift: None, first: tree, initial: state,
194
193
  exited: false, ticks: 0, pointer: NoPointer, view: Vec2(x: 0.0, y: 0.0))
195
194
 
196
195
  # The key of the current screen's actor (kept with its name, so a tick doesn't look it up).
@@ -250,7 +249,7 @@ struct Game<S: Reducer<M>, M>(
250
249
  if self.exited then self else self.tick(io, input, dt, view)
251
250
 
252
251
  fun tick(self, io: Io, input: Input, dt: Float, view: Vec2) -> Game<S, M> =
253
- ctx: Ctx<M> := Ctx.of(input, view)
252
+ ctx := Ctx.of(input, view)
254
253
  key := self.currentKey()
255
254
  name := self.current
256
255
  routed := routeAll(self.liveAt(key), ctx, input.events, 0, self.pointer)
@@ -262,15 +261,14 @@ struct Game<S: Reducer<M>, M>(
262
261
  ctx.keepOnly(true)
263
262
  now := keyNow<S, M>(applied.root.kids(), stepKey, name)
264
263
  back := self.tree.withRoot(spliced(self.tree.root, applied.root, key, now))
265
- begin: Turn<S, M> := Turn(tree: back, store: self.store, key: now, restart: false, exit: false)
264
+ begin := Turn(tree: back, store: self.store, key: now, restart: false, exit: false)
266
265
  turn := ctx.out.fold(begin, |tu, c| tu.take(c, name))
267
266
  flushed := turn.store.flush(io)
268
267
  pointer := lastPointer(input.events, self.pointer)
269
268
  if turn.restart && !turn.exit
270
- none: Option<Shift> := None
271
269
  again := chosen<S, M>(self.first, self.initial.screen())
272
270
  Game(
273
- ..self, tree: self.first, store: Store.of(self.initial), current: again, screenKey: keyOf<S, M>(self.first, again), unknown: "", shift: none,
271
+ ..self, tree: self.first, store: Store.of(self.initial), current: again, screenKey: keyOf<S, M>(self.first, again), unknown: "", shift: None,
274
272
  ticks: self.ticks + 1, pointer: pointer, view: view)
275
273
  else
276
274
  ran := Game(..self, tree: turn.tree, store: flushed, screenKey: turn.key, shift: advanced(self.shift, dt), exited: turn.exit, ticks: self.ticks + 1, pointer: pointer, view: view)
@@ -300,7 +298,7 @@ struct Game<S: Reducer<M>, M>(
300
298
  entered := onScreen<S, M>(self.tree, key, |s, t| s.enter(t, st, whole))
301
299
  now := keyNow<S, M>(entered.root.kids(), key, to)
302
300
  how := transitionTo<S, M>(entered, now, self.current)
303
- shift: Option<Shift> := if how.secs() > 0.0 then Some(Shift(from: self.current, how: how, t: 0.0)) else None
301
+ shift := if how.secs() > 0.0 then Some(Shift(from: self.current, how: how, t: 0.0)) else None
304
302
  Game(..self, tree: entered, current: to, screenKey: now, unknown: "", shift: shift)
305
303
 
306
304
  # Draws a frame: the current screen's `draw` (by default the tree's) of what is shown.
@@ -383,8 +381,8 @@ fun chosen<S, M>(tree: SceneTree<M>, asked: Str) -> Str =
383
381
  # or no screen named `name`.
384
382
  fun screensError<S, M>(tree: SceneTree<M>, name: Str) -> Option<Str> =
385
383
  kids := tree.root.kids().chain()
386
- names: List<Str> := kids.map(|n| n.top().map(|a| nameOf<S, M>(a)).unwrapOr(""))
384
+ names := kids.map(|n| n.top().map(|a| nameOf<S, M>(a)).unwrapOr(""))
387
- keys: List<Int> := kids.map(|n| n.top().map(|a| a.key()).unwrapOr(NoScreen))
385
+ keys := kids.map(|n| n.top().map(|a| a.key()).unwrapOr(NoScreen))
388
386
  plain := 0.until(kids.length()).find(|i| names.get(i).unwrapOr("") == "")
389
387
  twice := 0.until(kids.length()).find(|i| 0.until(i).find(|j| names.get(j).unwrapOr("") == names.get(i).unwrapOr("")).isSome())
390
388
  sameKey := 0.until(kids.length()).find(|i| 0.until(i).find(|j| keys.get(j).unwrapOr(NoScreen) == keys.get(i).unwrapOr(NoScreen)).isSome())
std/game/SceneTree.plum CHANGED
@@ -56,8 +56,9 @@ import std/game/Ctx
56
56
  # children and later siblings) needs the siblings' result before the node is built, so the
57
57
  # walk recurses along the chain; that costs more than the nodes it saves, even when 99% of
58
58
  # the actors stand still (benchmarks/scene_trees, workloads 10 and 11). `input`, which
59
- # recurses anyway, keeps every node off the hit actor's path, and the hit node itself when
59
+ # rebuilds only the nodes on the hit actor's path (and relinks the siblings before each),
60
+ # keeps every node off that path, and the hit node itself when the actor's `input`
60
- # the actor's `input` returns it as it was (`Actor`'s default); a value compared by
61
+ # returns it as it was (`Actor`'s default); a value compared by
61
62
  # `@sameRef` is never updated in place, so a changed actor is always another
62
63
  # object. `withRoot`, `apply` with nothing to do and `input` that hits nothing return the
63
64
  # tree itself.
@@ -104,8 +105,7 @@ struct SceneTree<M>(root: RoseTree<Actor<M>>, camX: Float, camY: Float, camScale
104
105
  # Every actor of the type `T`, in tree order (a parent before its children, siblings
105
106
  # in order): `tree.allAs<Orb>()`.
106
107
  fun allAs<T>(self) -> List<T> =
107
- nil: List<T> := Nil
108
- self.root.fold(nil, |acc, a| match a
108
+ self.root.fold(Nil, |acc, a| match a
109
109
  T as t => Cons(head: t, tail: acc)
110
110
  _ => acc).reverse()
111
111
 
@@ -151,10 +151,9 @@ struct SceneTree<M>(root: RoseTree<Actor<M>>, camX: Float, camY: Float, camScale
151
151
  r := if gameOnly(ctx.out, 0)
152
152
  stepPlain(self.root, ctx, dt)
153
153
  else
154
- w: TreeWalk<M> := TreeWalk.ofTree(ctx.out)
154
+ w := TreeWalk.ofTree(ctx.out)
155
155
  ctx.keepOnly(true)
156
- empty: RoseTree<Actor<M>> := RoseEmpty
157
- stepChain(self.root, ctx, dt, w, empty)
156
+ stepChain(self.root, ctx, dt, w, RoseEmpty)
158
157
  s := self.withRoot(r)
159
158
  if gameOnly(ctx.out, 0)
160
159
  s
@@ -171,9 +170,8 @@ struct SceneTree<M>(root: RoseTree<Actor<M>>, camX: Float, camY: Float, camScale
171
170
  if gameOnly(cmds, 0)
172
171
  self
173
172
  else
174
- w: TreeWalk<M> := TreeWalk.ofTree(cmds)
173
+ w := TreeWalk.ofTree(cmds)
175
- empty: RoseTree<Actor<M>> := RoseEmpty
176
- self.withRoot(applyChain(self.root, w, empty))
174
+ self.withRoot(applyChain(self.root, w, RoseEmpty))
177
175
 
178
176
  # The tree after the pointer event `e` (in tree coordinates; a key event changes
179
177
  # nothing): the topmost shown `Clickable` actor under it that says it is hit (as `hit`
@@ -231,8 +229,7 @@ fun nodeOf<M>(t: RoseTree<Actor<M>>, id: Int) -> RoseTree<Actor<M>> = match t
231
229
 
232
230
  # The actors of the chain `c` (not their subtrees) that are `T`s, in order.
233
231
  fun chainAs<T, M>(c: RoseTree<Actor<M>>) -> List<T> =
234
- nil: List<T> := Nil
235
- chainAsOnto<T, M>(c, nil).reverse()
232
+ chainAsOnto<T, M>(c, Nil).reverse()
236
233
 
237
234
  fun chainAsOnto<T, M>(c: RoseTree<Actor<M>>, acc: List<T>) -> List<T> = match c
238
235
  RoseEmpty => acc
@@ -443,8 +440,7 @@ fun toLocal(pl: Place, p: Vec2) -> Vec2 =
443
440
  # Only a `Clickable` actor can be hit: any other is skipped with no transform computed,
444
441
  # unless it has children, whose coordinates its place gives.
445
442
  fun hitPath<M>(t: RoseTree<Actor<M>>, p: Vec2, i: Int) -> Option<List<Int>> =
446
- none: Option<List<Int>> := None
447
- hitPathFrom(t, p, i, none)
443
+ hitPathFrom(t, p, i, None)
448
444
 
449
445
  # `hitPath`'s walk along the chain, one tail call per sibling (recursion only into
450
446
  # children), so a chain of any length runs in constant stack. A later sibling is on top of
@@ -469,8 +465,7 @@ fun hitPathAt<M>(a: Actor<M>, k: RoseTree<Actor<M>>, p: Vec2, i: Int) -> Option<
469
465
  match below
470
466
  Some(path) => Some(Cons(head: i, tail: path))
471
467
  None =>
472
- nil: List<Int> := Nil
473
- if c.hit(q) then Some(Cons(head: i, tail: nil)) else None
468
+ if c.hit(q) then Some(Cons(head: i, tail: Nil)) else None
474
469
  _ =>
475
470
  if k.isEmpty() || !isShown(a)
476
471
  None
@@ -511,8 +506,7 @@ fun relinked<T>(t: RoseTree<T>, i: Int, node: RoseTree<T>) -> RoseTree<T> = matc
511
506
  # coordinates) that says it is hit: later siblings before earlier ones, and in each, its
512
507
  # children before itself. Other actors are skipped as `hitPath` skips them.
513
508
  fun hitChain<M>(t: RoseTree<Actor<M>>, p: Vec2) -> Option<Actor<M>> =
514
- none: Option<Actor<M>> := None
515
- hitFrom(t, p, none)
509
+ hitFrom(t, p, None)
516
510
 
517
511
  # `hitChain`'s walk along the chain, as `hitPathFrom`: the last sibling hit wins.
518
512
  fun hitFrom<M>(t: RoseTree<Actor<M>>, p: Vec2, best: Option<Actor<M>>) -> Option<Actor<M>> = match t