plum

#treesitter#compiler#wasm

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

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


3efcfd1 — Peter John 2026-11-09T13:03:47+05:30
Plan: one way to make a game: Game + Store + Screens, actors are Actor[M]
Files changed (1) hide show
  1. docs/plans/2026-11-09-actor-trait.md +120 -29
docs/plans/2026-11-09-actor-trait.md CHANGED
@@ -12,8 +12,9 @@ transform, size, colour, visible, dead) plus the game's `kind: K`, and the kind
12
12
  - `Actor` carries fields that most kinds don't use: a label has no size, a button never
13
13
  dies, a star is never hidden.
14
14
 
15
- Wanted: each kind is its own struct with only its own fields and its own methods, the
15
+ Wanted: each kind is its own struct with only its own fields and its own methods, and the
16
- stage holds a tree of any mix of them, and there is no type parameter on the stage.
16
+ tree holds any mix of them. The tree's only type parameter is the game's message type `M`
17
+ (see "One way to make a game").
17
18
 
18
19
  Option chosen: add traits back to the language, with required and optional (default)
19
20
  methods and trait objects. Rejected:
@@ -149,8 +150,8 @@ score := store.state.score # S is RunState here: no downcast
149
150
  - Bounds and trait objects are separate: `ETree[Actor]` holds a mix; `Store[RunState, Msg]`
150
151
  holds exactly a `RunState`.
151
152
  - `std/game/Store.plum` gives `Reducer[M]` and `Store[S, M]` (`dispatch`, then `flush(io)`
152
- calling `handle` for each queued message, in order). Games may keep their own update
153
+ calling `handle` for each queued message, in order). Only `Game` calls them (see "One way
153
- loop; orb_run and orb_run_tree move to `Store` in task 7.
154
+ to make a game"); a game writes its `Reducer`, never a loop that dispatches.
154
155
 
155
156
  ### Code generation
156
157
 
@@ -215,7 +216,8 @@ struct EStage(root: ETree[Actor], camX: Float, camY: Float, camScale: Float) =
215
216
  ```
216
217
 
217
218
  - `ETree[T]` is unchanged and stays a plain generic tree.
219
+ - The trait the tree holds is `Actor[M]` (see "One way to make a game"); `Ctx[M]` and
218
- - `ECtx` and `ECmd` lose their type parameter: `ESpawn(parent: Int, node: ETree[Actor])`.
220
+ `Cmd[M]` carry the message type: `Spawn(parent: Int, node: FRoseTree[Actor[M]])`.
219
221
  - The stage uses only the trait: `key`, `place().local()`, `opacity`, `shown`, `gone`,
220
222
  `step`, `paint`, `hit`. Behaviour stays as it is now: command order, the bitset `EWalk`,
221
223
  dead actors dropped with their subtree, hidden actors still stepped, draw and hit order.
@@ -235,12 +237,12 @@ tree). After this work:
235
237
  - `ETree` moves to `std/collections/functional/FRoseTree.plum` as `FRoseTree[T]`, a plain
236
238
  generic collection.
237
239
  - `std/game/scene` is the retained scene on `FRoseTree[Actor]`. The names drop the `E`:
238
- `SceneTree` (was `EStage`; a screen is a `Screen`, see "Game and screens"), `Actor`, `Place`, `Ctx`, `Cmd`, `Spawn`, `Kill`, `Drawn`,
240
+ `SceneTree` (was `EStage`; a screen is a `Screen`, see "One way to make a game"), `Actor`, `Place`, `Ctx`, `Cmd`, `Spawn`, `Kill`, `Drawn`,
239
241
  `Group`, `Box`.
240
242
  - Deleted: std/game_enum_tree, and the immediate-mode `SceneNode[M]` with `Clickable`
241
243
  and `msgOf`.
242
- - `std/game/ui.plum` becomes actors (its widgets are `is Actor`; a click is found with
244
+ - `std/game/ui.plum` becomes actors (its widgets are `is Actor[M]`; a button sends its
243
- `Scene.hitId`, as the port's button does now).
245
+ `onClick: M` from its `input` method).
244
246
  - Ported to the new scene: examples/games/orb_run.plum (orb_run_tree.plum becomes it,
245
247
  after a last headless comparison of the two), examples/games/kids/balloon_pop.plum,
246
248
  the web-test stress fixtures, docs/game.md.
@@ -249,27 +251,112 @@ tree). After this work:
249
251
  what it still buys on the retained tree.
250
252
  - benchmarks/scene_trees: one branch entry, the new scene.
251
253
 
252
- ## Game and screens (from master)
254
+ ## One way to make a game
253
255
 
256
+ The user's rule: there is one way to make a game. Every game is a `Game` over a `Store`,
257
+ its actors are `Actor[M]` and it is made of `Screen`s. There is no plain `update`/`draw`
258
+ game any more. `Game` runs the loop, sends every message to the store and switches
259
+ screens; a game writes actors, screens and a reducer, never the loop.
260
+
254
- Master's std/game has `Game(scene, state)`, `Scene(stage)` (one screen: title, play, game
261
+ This brings over master's std/game (`Game(scene, state)`, `Scene(stage)`, `Stage`,
255
- over), `Stage` (the actor tree), `State.reduce` with `Actions` (a Redux dispatcher) and
262
+ `State.reduce` with `Actions`, `Restart`, `Exit`, `GoTo`) on traits instead of struct
256
- std's `Restart`, `Exit` and `GoTo(scene)`. The branch already has the fixed-tick `Clock`
257
- (std/game/input) and record and replay (std/game/Replay), ported by plum-63. Master's code
258
- is built on struct embedding and `&self` mutation, which the branch doesn't have, so the
263
+ embedding and `&self` mutation, which the branch doesn't have. The branch already has the
259
- ideas come over on traits, not the code. Names: a screen is a `Screen` and the actor tree
264
+ fixed-tick `Clock` (std/game/input) and record and replay (std/game/Replay). Names: a
260
- is a `SceneTree` (strictly a tree: one parent per node, no sharing), so "scene" alone names
265
+ screen is a `Screen` and the actor tree is a `SceneTree` (strictly a tree), so "scene"
261
- neither:
266
+ alone names neither.
262
267
 
263
268
  | master | branch |
264
269
  |---|---|
265
- | `Stage` (actor tree) | `SceneTree` on `FRoseTree[Actor]` (this doc's retained tree) |
270
+ | `Stage` (actor tree) | `SceneTree[M]` on `FRoseTree[Actor[M]]` |
271
+ | `Actor` (base struct) | `trait Actor[M]` |
266
- | `Scene` (one screen, subclassed) | `trait Screen[S, M]`: `update(self, io, input, dt, store) -> (Self, Store[S, M])`, optional `draw(self, io)` defaulting to drawing its tree |
272
+ | `Scene` (one screen, subclassed) | `trait Screen[S, M]` |
267
- | `State` + `reduce` + `Actions` | `Reducer[M]` + `Store[S, M]` (`dispatch` reduces at once, effects after the tick, as master) |
273
+ | `State` + `reduce` + `Actions` | `Reducer[M]` + `Store[S, M]`, driven by `Game` only |
268
- | `Restart`, `Exit`, `GoTo(scene)` | std messages `Restart`, `Exit`, `GoTo(screen)` that the `Game` applies after the tick, before the game's `handle` |
274
+ | `Restart`, `Exit`, `GoTo(scene)` | `Cmd`s `Restart` and `Exit`; `Screen.next` for a switch |
269
- | `Game(scene, state)` | `Game[S, M](screen: Screen[S, M], store: Store[S, M])`, a trait object for the current screen |
275
+ | `Game(scene, state)` | `Game[S, M]`, made only by `Game.start(screen, state)` |
276
+
277
+ ```plum
278
+ # std/game/scene
279
+ trait Actor[M] =
280
+ fun key(self) -> Int
281
+ fun place(self) -> Place
282
+ fun step(self, ctx: Ctx[M], dt: Float) -> Self = self # every tick
283
+ fun input(self, ctx: Ctx[M], e: InputEvent) -> Self = self # a pointer event on it
284
+ fun paint(self, io: Io, m: Affine, alpha: Float) = ()
285
+ fun hit(self, p: Vec2) -> Bool = false
286
+ fun shown(self) -> Bool = true
287
+ fun gone(self) -> Bool = false
288
+ fun opacity(self) -> Float = 1.0
289
+
290
+ enum Cmd[M] =
291
+ | Spawn(parent: Int, node: FRoseTree[Actor[M]])
292
+ | Kill(id: Int)
293
+ | Send(msg: M) # to the store
294
+ | Restart # the game's first screen and state again
295
+ | Exit
296
+
297
+ # The parent's transform and opacity, the tick's input (held keys, pointer) and the
298
+ # commands. `ctx.send(m)` is `ctx.out.push(Send(msg: m))`.
299
+ struct Ctx[M](up: Affine, alpha: Float, input: Input, out: Slice[Cmd[M]])
300
+
301
+ struct SceneTree[M](root: FRoseTree[Actor[M]], camX: Float, camY: Float, camScale: Float)
302
+
303
+ # std actors are generic in M so any game's tree takes them.
304
+ struct Group[M](id: Int, at: Place) is Actor[M]
305
+ struct Box[M](id: Int, at: Place, w: Float, h: Float, color: Color) is Actor[M]
306
+ struct Button[M](id: Int, at: Place, label: Str, onClick: M) is Actor[M] # std/game/ui
307
+
308
+ # std/game/Store.plum
309
+ trait Reducer[M] =
310
+ fun update(self, msg: M) -> Self # the rules: pure, no Io
311
+ fun handle(self, io: Io, msg: M) = () # effects (sound, save), after the tick
312
+
313
+ # std/game/Game.plum
314
+ trait Screen[S, M] =
315
+ fun tree(self) -> SceneTree[M]
316
+ fun withTree(self, t: SceneTree[M]) -> Self
317
+ # Rules over the whole screen, after its actors stepped (collisions): sends messages.
318
+ fun rules(self, ctx: Ctx[M], state: S, dt: Float) -> Self = self
319
+ # After each message is reduced: show the new state (a HUD label).
320
+ fun react(self, state: S, msg: M) -> Self = self
321
+ # The screen to switch to after this message, if any (title to play to game over).
322
+ fun next(self, state: S, msg: M) -> Option[Screen[S, M]] = None
323
+ fun draw(self, io: Io) = self.tree().draw(io)
324
+
325
+ struct Game[S: Reducer[M], M] =
326
+ fun start(screen: Screen[S, M], state: S) -> Game[S, M] # the only way to make one
327
+ ```
328
+
329
+ A game:
330
+
331
+ ```plum
332
+ enum Msg = Collected(id: Int) | Hurt | StartPressed
333
+
334
+ struct RunState(score: Int, lives: Int, rng: Random) is Reducer[Msg] = ...
335
+ struct Title(t: SceneTree[Msg]) is Screen[RunState, Msg] = ... # next: StartPressed => Play
336
+ struct Play(t: SceneTree[Msg]) is Screen[RunState, Msg] = ... # rules: collisions
337
+ struct Over(t: SceneTree[Msg]) is Screen[RunState, Msg] = ...
338
+
339
+ fun main(io: Io) -> Game[RunState, Msg] =
340
+ Game.start(Title.create(), RunState.create(io.randomSeed()))
341
+ ```
342
+
343
+ One tick, always in this order (std's code, not the game's):
344
+
345
+ 1. Each pointer event goes to the topmost actor under it (`hit`), then its parents, through
346
+ `input`, as master's stage does. Held keys and the pointer are in `ctx.input`.
347
+ 2. The tree steps (`step`), then the screen's `rules`.
348
+ 3. `Spawn` and `Kill` apply as now. Each `Send(msg)`, in order: the store reduces it
349
+ (`update`), then the screen `react`s to it and is asked for `next`. The first `next`
350
+ that gives a screen wins.
351
+ 4. After the tick: `handle(io, msg)` for every message in order, then the switch, then
352
+ `Restart` or `Exit`.
353
+
354
+ `draw` calls the current screen's `draw`. Randomness lives in `S` and changes only in
355
+ `update`, so the same seed and inputs replay the same game.
270
356
 
271
- A game's `main` returns a `Game`; the host drives it as now. Screens switch only between
357
+ The compiler's game detection (compiler/core/src/game_entry.rs) recognises a game as a
272
- ticks. A game with one screen may keep its own `update` and skip `Game`.
358
+ `main` returning `Game[S, M]` and nothing else; a type with its own `update` and `draw` is
359
+ no longer a game, and the error says to use `Game.start`.
273
360
 
274
361
  ## The port
275
362
 
@@ -298,15 +385,19 @@ ticks. A game with one screen may keep its own `update` and skip `Game`.
298
385
  4. Uniqueness and FBIP through dispatch and downcasts, with tests that show reuse.
299
386
  5. Docs: docs/language.md's section on traits; examples/traits.plum. std/game/Store.plum
300
387
  (`Reducer[M]`, `Store[S, M]`) with tests.
301
- 6. std/game_enum_tree on the trait: `Place`, `Actor`, `Group`, `Box`, `ECtx`, `ECmd`,
388
+ 6. std/game_enum_tree on the trait: `Place`, `Actor[M]`, `Group[M]`, `Box[M]`, `ECtx[M]`,
302
- `EStage` (with `updateAs` and `findAs`), with the tests ported.
389
+ `ECmd[M]` (with `Send`), `EStage[M]` (with `updateAs` and `findAs`), with the tests
390
+ ported.
303
391
  7. The port of orb_run_tree.plum and the benchmark, with the headless comparison.
304
392
  8. One scene system: the new scene becomes `std/game/scene`; ui.plum, orb_run,
305
393
  balloon_pop, the web-test fixtures and docs/game.md move to it; `SceneNode`, its
306
394
  fusion, and std/game_enum_tree are deleted. The tree type is named `SceneTree`.
307
- 9. Game and screens: std/game/Game.plum with `trait Screen[S, M]`, `Game[S, M]`, `Restart`,
395
+ 9. One way to make a game: std/game/Game.plum (`trait Screen[S, M]`, `Game[S, M]`,
396
+ `Game.start`), `Actor[M]` with `input`, `Ctx[M]` with `input` and `send`, `Cmd`'s `Send`,
308
- `Exit` and `GoTo`, as in "Game and screens (from master)"; orb_run's title, play and
397
+ `Restart` and `Exit`, as in "One way to make a game". game_entry.rs accepts only `Game`.
398
+ Every game moves to it: orb_run (title, play, game over screens), balloon_pop, and every
399
+ web-test fixture with a game `main` (text_sizes, stress*, main_game, interleave,
309
- game over become screens; docs/game.md and tests.
400
+ rendertarget). docs/game.md and tests.
310
401
 
311
402
  Tasks 1 to 4 are compiler work. Agree them with the session porting master's changes
312
403
  (plum-63) before starting, since it edits the parser and checker.