plum

#treesitter#compiler#wasm

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

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


6eaffb8 — Peter John 2026-11-12T22:52:24+05:30
benchmarks: screen_views, Orbit Runner per tick and 10,000 actors by view or by hand, as master's; master_vs_simpler on the new games
benchmarks/master_vs_simpler/run.py CHANGED
@@ -17,18 +17,18 @@ No program compiles unchanged on both branches (master needs `return` in a block
17
17
  rejects `fun f() = value`; the branch needs `= value` and has no `return`), so every
18
18
  program is a port: master/X.plum and branch/X.plum run the same algorithm on the same
19
19
  data and print the same checksum. The two games are drivers written around each branch's
20
- own game (master's examples/games/orbitrunner and balloonpop, the branch's
20
+ own game (examples/games/orbitrunner and balloonpop on both): a headless `main` that steps
21
- examples/games/orb_run and kids/balloon_pop): this script copies the game without its
22
- `main` and tests and appends a headless `main` that steps it a fixed number of ticks with
21
+ the game a fixed number of ticks with inputs that are a function of the seed and tick only.
22
+ The branch's driver imports the game's modules by path (from the repository root, the lib
23
- inputs that are a function of the seed and tick only. On master (20ae0ea0 on) a game is a
23
+ path); master's is appended to a copy of the game, as below. On master (20ae0ea0 on) a game is a
24
24
  folder; the whole folder is copied into `--out` and the driver replaces `main` in the copy
25
25
  of its entry file (OrbitRunner.plum, BalloonPop.plum), so the game's other modules are
26
26
  master's own. Master's micro ports are written for 20ae0ea0's syntax (`<>` generics, `T?`
27
27
  and `?:`, grouped imports, no unused import); `cons` and `cons_rec` need master's
28
- `LinkedList`, removed in ed6fec37, and no longer build there. The branch's Orb Run is given
28
+ `LinkedList`, removed in ed6fec37, and no longer build there. Since sl/view the branch's games
29
- master's restart rule (`ORB_BRANCH_AGAIN`), so the two play the same games; it is given it
29
+ are master's screens (one `PlayingScreen`, one `PlaygroundScreen`) in the functional
30
- without `--master` too, so the checksums stay comparable from run to run (at the default
31
- ticks Orb Run prints checksum 871028980, Balloon Pop checksum 234038249 with 3975 pops).
30
+ dialect; the checksums printed before it (Orb Run 871028980, Balloon Pop 234038249 with
31
+ 3975 pops) were of the branch's older games.
32
32
  `--orb-ticks` and `--bp-ticks` make a quick run (`--orb-ticks 300 --bp-ticks 1000 --runs 1`).
33
33
 
34
34
  For each program: `plum compile` --runs times on each branch (median ms, wasm bytes),
@@ -129,6 +129,21 @@ fun main(io: Io)
129
129
  '''
130
130
 
131
131
  ORB_BRANCH = '''
132
+ import std/Int
133
+ import std/Float
134
+ import std/Bool
135
+ import std/Str
136
+ import std/Io
137
+ import std/Slice
138
+ import std/collections/List
139
+ import std/math/Vec2
140
+ import std/math/Random
141
+ import std/game/input
142
+ import std/game/Screen
143
+ import std/game/Game
144
+ import examples/games/orbitrunner/OrbitRunner
145
+ import examples/games/orbitrunner/screens/PlayingScreen
146
+
132
147
  fun steerX(seed: Int, t: Int) -> Float = Float({{seed * 7 + t / 9} * 2654435761 % 1000003} % 3 - 1)
133
148
 
134
149
  fun steerY(seed: Int, t: Int) -> Float = Float({{seed * 13 + t / 13} * 40503 % 1000033} % 3 - 1)
@@ -163,37 +178,30 @@ fun inputAt(seed: Int, t: Int, over: Bool) -> Input =
163
178
 
164
179
  struct Tally(check: Int, overs: Int, scores: Int, mines: Int)
165
180
 
181
+ fun playing(g: Game) -> PlayingScreen = match g.screen()
182
+ PlayingScreen as p => p
183
+ _ => PlayingScreen.start(Random.seeded(0), Assets.none())
184
+
166
- fun run(io: Io, seed: Int, g: Game<State, Msg>, t: Int, was: Bool, acc: Tally) -> Tally =
185
+ fun run(io: Io, seed: Int, g: Game, t: Int, was: Bool, acc: Tally) -> Tally =
167
186
  if t >= TICKS
168
- Tally(..acc, mines: acc.mines + g.state().mines)
187
+ Tally(..acc, mines: acc.mines + playing(g).mines.length())
169
188
  else
170
- n := g.step(io, inputAt(seed, t, g.state().over), 1.0 / 60.0, Vec2(x: 800.0, y: 600.0))
189
+ n := g.step(io, inputAt(seed, t, playing(g).over), 1.0 / 60.0, Vec2(x: 800.0, y: 600.0))
171
- r := n.state()
190
+ r := playing(n)
172
191
  fresh := r.over && !was
173
192
  a := Tally(check: {acc.check * 31 + r.score + r.lives} % 1000000007, overs: if fresh then acc.overs + 1 else acc.overs, scores: if fresh then acc.scores + r.score else acc.scores, mines: acc.mines)
174
193
  run(io, seed, n, t + 1, r.over, a)
175
194
 
176
195
  fun main(io: Io) =
177
196
  t0 := io.nowMicros()
178
- enter := Input(held: Held.none(), events: [KeyDown(key: Key.Enter)].toSlice(), pos: Vec2(x: 0.0, y: 0.0), wheel: 0.0)
179
197
  tally := SEEDS.fold(Tally(check: 0, overs: 0, scores: 0, mines: 0), |acc, seed|
180
- g: Game<State, Msg> := Game.start(gameTree(), State.create(seed, Texture(id: -1), Sounds.silent()))
198
+ only: Screen := PlayingScreen.start(Random.seeded(seed), Assets.none())
181
- run(io, seed, g.step(io, enter, 1.0 / 60.0, Vec2(x: 800.0, y: 600.0)), 0, false, acc))
199
+ run(io, seed, Game(screens: [only]), 0, false, acc))
182
200
  t1 := io.nowMicros()
183
201
  el := {t1 - t0} / 1000.0
184
202
  io.printLn("ms {el} checksum {tally.check} overs {tally.overs} scoreAtOver {tally.scores} minesAtEnd {tally.mines}")
185
203
  '''
186
204
 
187
- # Master's restart keeps the world's random stream (`World.restart` places the orbs, then
188
- # the mines, from where the stream is); the branch's `Again` seeds a new run with the
189
- # stream's next Int. Both games place a fresh run the same way (three orbs, then three
190
- # mines, around the middle), so the branch's game is given master's rule: its next run
191
- # goes on from the stream (`Random.seeded(rng.seed)` is `rng`). Without this the two runs
192
- # differ after the first game over (seed 13 at tick 2100, seen at 2695 as a hit).
193
- ORB_BRANCH_AGAIN = (
194
- "Again => if self.over then self.run(self.rng.nextInt().value, self.best.max(self.score)) else self",
195
- "Again => if self.over then self.run(self.rng.seed, self.best.max(self.score)) else self")
196
-
197
205
  BP_MASTER = '''
198
206
  fun main(io: Io)
199
207
  t0 := io.nowMicros()
@@ -214,24 +222,40 @@ fun main(io: Io)
214
222
  '''
215
223
 
216
224
  BP_BRANCH = '''
225
+ import std/Int
226
+ import std/Float
227
+ import std/Str
228
+ import std/Io
229
+ import std/Slice
230
+ import std/collections/List
231
+ import std/math/Vec2
232
+ import std/game/input
233
+ import std/game/Screen
234
+ import std/game/Game
235
+ import examples/games/balloonpop/BalloonPop
236
+ import examples/games/balloonpop/screens/PlaygroundScreen
237
+
217
238
  struct Tally(check: Int, pops: Int)
218
239
 
240
+ fun field(g: Game) -> PlaygroundScreen = match g.screen()
241
+ PlaygroundScreen as p => p
242
+ _ => PlaygroundScreen.seeded(0, Assets.none())
243
+
219
- fun run(io: Io, g: Game<State, Msg>, t: Int, acc: Tally) -> Tally =
244
+ fun run(io: Io, g: Game, t: Int, acc: Tally) -> Tally =
220
245
  if t >= TICKS
221
- Tally(..acc, pops: acc.pops + g.state().pops)
246
+ Tally(..acc, pops: acc.pops + field(g).pops)
222
247
  else
223
248
  events := Slice<InputEvent>()
224
249
  events.push(PointerDown(x: Float({t * 97} % 800), y: Float({t * 61} % 600)))
225
250
  inp := Input(held: Held.none(), events: events, pos: Vec2(x: 0.0, y: 0.0), wheel: 0.0)
226
251
  n := g.step(io, inp, 1.0 / 60.0, Vec2(x: 800.0, y: 600.0))
227
- run(io, n, t + 1, Tally(..acc, check: {acc.check * 31 + n.state().pops} % 1000000007))
252
+ run(io, n, t + 1, Tally(..acc, check: {acc.check * 31 + field(n).pops} % 1000000007))
228
253
 
229
254
  fun main(io: Io) =
230
255
  t0 := io.nowMicros()
231
256
  tally := SEEDS.fold(Tally(check: 0, pops: 0), |acc, seed|
232
- play := SkyScreen.start(State.start(seed, Sound.Silent, Sound.Silent))
257
+ only: Screen := PlaygroundScreen.seeded(seed, Assets.none())
233
- g: Game<State, Msg> := Game.start(play.tree, play.state)
234
- run(io, g, 0, acc))
258
+ run(io, Game(screens: [only]), 0, acc))
235
259
  t1 := io.nowMicros()
236
260
  el := {t1 - t0} / 1000.0
237
261
  io.printLn("ms {el} checksum {tally.check} pops {tally.pops}")
@@ -271,21 +295,17 @@ def write_games(master, out, orb_ticks, bp_ticks, only):
271
295
  games = [
272
296
  ("orb_run", "/examples/games/orbitrunner", "OrbitRunner.plum", "fun main(io: Io) -> Game",
273
297
  ORB_MASTER, ["std/game/input", "std/math/Vec2"],
274
- "examples/games/orb_run.plum", "fun main(io: Io) -> Game<State, Msg>", ORB_BRANCH, orb_ticks, ORB_SEEDS),
298
+ ORB_BRANCH, orb_ticks, ORB_SEEDS),
275
299
  ("balloon_pop", "/examples/games/balloonpop", "BalloonPop.plum", "fun main(io: Io) -> Game",
276
300
  BP_MASTER, ["std/game/input", "std/math/Vec2"],
277
- "examples/games/kids/balloon_pop.plum", "fun main(io: Io) -> Game<State, Msg>", BP_BRANCH, bp_ticks, BP_SEEDS),
301
+ BP_BRANCH, bp_ticks, BP_SEEDS),
278
302
  ]
279
303
  progs = []
280
- for name, mfolder, mentry, msig, mdrv, mimports, bsrc, bsig, bdrv, ticks, seeds in games:
304
+ for name, mfolder, mentry, msig, mdrv, mimports, bdrv, ticks, seeds in games:
281
305
  if only and name not in only:
282
306
  continue
283
307
  mp, bp = None, f"{out}/{name}_branch.plum"
284
- game = strip_main(bsrc, bsig)
285
- if name == "orb_run":
286
- assert ORB_BRANCH_AGAIN[0] in game, "orb_run's Again changed: update ORB_BRANCH_AGAIN"
287
- game = game.replace(*ORB_BRANCH_AGAIN)
288
- open(bp, "w").write(game + bdrv.replace("SEEDS", seeds).replace("TICKS", str(ticks)))
308
+ open(bp, "w").write(bdrv.replace("SEEDS", seeds).replace("TICKS", str(ticks)).lstrip("\n"))
289
309
  if master:
290
310
  mp = master_game(master, out, name, mfolder, mentry, msig,
291
311
  mdrv.replace("SEEDS", seeds).replace("TICKS", str(ticks)), mimports)
benchmarks/screen_views/orbit.plum ADDED
@@ -0,0 +1,80 @@
1
+ # Orbit Runner, timed per tick: seeds 1, 7, 42 and 1234, 3600 ticks each, the scripted
2
+ # input of benchmarks/screen_views/timing (as master's benchmarks/screen_views/orbit.plum),
3
+ # restarts on.
4
+ #
5
+ # update each tick through the game (`Game.update`: the screen's mapping, its reducer,
6
+ # the effects)
7
+ # paint each tick, then the frame (`Game.draw`: the clear, the viewport, the screen's
8
+ # view made and drawn). Run with PLUM_NULL_GFX=1: there is no host, and the draw
9
+ # calls reach nothing.
10
+ #
11
+ # It prints `us <total> ticks <n> checksum <c>`: the microseconds of every tick (and frame)
12
+ # of the four seeds, the ticks, and the state checksum (the run, the stream, the ship,
13
+ # each orb and each mine at the end of each seed), computed as master's driver does.
14
+ #
15
+ # plum build benchmarks/screen_views/orbit.plum -o orbit
16
+ # PLUM_NULL_GFX=1 ./orbit update
17
+ # PLUM_NULL_GFX=1 ./orbit paint
18
+ module orbit_bench
19
+
20
+ import std/Int
21
+ import std/Float
22
+ import std/Bool
23
+ import std/Str
24
+ import std/Io
25
+ import std/Slice
26
+ import std/collections/List
27
+ import std/math/Random
28
+ import std/game/input
29
+ import std/math/Vec2
30
+ import std/game/Screen
31
+ import std/game/Game
32
+ import benchmarks/screen_views/timing
33
+ import examples/games/orbitrunner/OrbitRunner
34
+ import examples/games/orbitrunner/screens/PlayingScreen
35
+
36
+ fun make(seed: Int) -> PlayingScreen = PlayingScreen.at(Random.seeded(seed), Assets.none(), 0, 3)
37
+
38
+ fun gameOf(seed: Int) -> Game =
39
+ only: Screen := make(seed)
40
+ Game(screens: [only])
41
+
42
+ fun playingOf(g: Game) -> PlayingScreen = match g.screen()
43
+ PlayingScreen as p => p
44
+ _ => make(-1)
45
+
46
+ # The run's state: the run, the stream, the ship, each orb and each mine.
47
+ fun state(p: PlayingScreen) -> Int =
48
+ head := [p.score, p.best, p.lives, timing.flag(p.over), p.rng.seed, p.ship.at.x.toBits(), p.ship.at.y.toBits(), p.ship.vx.toBits(), p.ship.vy.toBits(), p.orbs.length(), p.mines.length()]
49
+ none: List<Int> := Nil
50
+ orbs := p.orbs.fold(none, |acc, o| Cons(o.at.y.toBits(), Cons(o.at.x.toBits(), acc)))
51
+ mines := p.mines.fold(none, |acc, m| Cons(m.at.rotation.toBits(), Cons(m.at.x.toBits(), acc)))
52
+ timing.hash(head.append(orbs.reverse()).append(mines.reverse()))
53
+
54
+ fun tickOnce(io: Io, g: Game, inp: Input, paint: Bool) -> Game =
55
+ next := g.update(io, inp, Clock.Tick)
56
+ if paint
57
+ next.draw(io)
58
+ next
59
+
60
+ fun play(io: Io, seed: Int, paint: Bool, ticks: Int) -> Int =
61
+ g := 0.until(ticks).fold(gameOf(seed), |cur, t| tickOnce(io, cur, timing.script(seed, t), paint))
62
+ timing.hash([state(playingOf(g)), 0])
63
+
64
+ struct Timed(us: Float, sums: List<Int>)
65
+
66
+ fun main(io: Io) =
67
+ args := io.args()
68
+ paint := args.length() > 0 && args.at(0) == "paint"
69
+ seeds := [1, 7, 42, 1234]
70
+ timed := seeds.fold(Timed(us: 0.0, sums: Nil), |acc, seed|
71
+ t0 := io.nowMicros()
72
+ sum := play(io, seed, paint, 3600)
73
+ Timed(us: acc.us + {io.nowMicros() - t0}, sums: acc.sums.append([sum])))
74
+ io.printLn("us {Int(timed.us)} ticks {3600 * seeds.length()} checksum {timing.hash(timed.sums)}")
75
+
76
+ test "the driver's run is the game's: the same seed reaches the same state"
77
+ a := 0.until(600).fold(gameOf(7), |g, t| g.step(io, timing.script(7, t), Clock.Tick, Vec2(x: 800.0, y: 600.0)))
78
+ b := 0.until(600).fold(gameOf(7), |g, t| g.step(io, timing.script(7, t), Clock.Tick, Vec2(x: 800.0, y: 600.0)))
79
+ assert state(playingOf(a)) == state(playingOf(b))
80
+ assert state(playingOf(a)) != state(make(7))
benchmarks/screen_views/scale.plum ADDED
@@ -0,0 +1,130 @@
1
+ # 10,000 actors on one screen, each moving its clock each tick, drawn by the screen's
2
+ # view (as master's benchmarks/screen_views/scale.plum): with `view`, actors whose view is
3
+ # one `Sprite`, made again at each draw; with `hand`, the same actors drawing the same
4
+ # sprite with a hand-written `paint` (`sprite.drawSpriteFrame`). The screen's tick action
5
+ # steps every actor (its `stepped`, which returns the actor's next version). 20 ticks to
6
+ # warm up, then 200 timed.
7
+ #
8
+ # update each tick through the game (`Game.update`)
9
+ # paint each tick, then the frame (`Game.draw`). Run with PLUM_NULL_GFX=1: there is no
10
+ # host, and the draw calls reach nothing.
11
+ #
12
+ # It prints `us <total> ticks <n> checksum <c>`, the checksum over every actor's frame at
13
+ # the end.
14
+ #
15
+ # plum build benchmarks/screen_views/scale.plum -o scale
16
+ # PLUM_NULL_GFX=1 ./scale update view
17
+ # PLUM_NULL_GFX=1 ./scale paint view
18
+ # PLUM_NULL_GFX=1 ./scale paint hand
19
+ module scale_bench
20
+
21
+ import std/Int
22
+ import std/Float
23
+ import std/Bool
24
+ import std/Str
25
+ import std/Io
26
+ import std/Option
27
+ import std/Slice
28
+ import std/collections/List
29
+ import std/math/Affine
30
+ import std/math/Vec2
31
+ import std/math/Aabb
32
+ import std/game/Color
33
+ import std/game/input
34
+ import std/game/Place
35
+ import std/game/Actor
36
+ import std/game/Sprite
37
+ import std/game/Screen
38
+ import std/game/ScreenSlice
39
+ import std/game/Game
40
+ import std/game/headless
41
+ import benchmarks/screen_views/timing
42
+
43
+ Count = 10000
44
+
45
+ struct ViewDot[Actor](i: Int, at: Place, clock: Float) =
46
+ fun of(i: Int) -> ViewDot = ViewDot(i: i, at: place.at(Float(i % 100) * 8.0, Float(i / 100) * 6.0), clock: 0.0)
47
+
48
+ fun place(self) -> Place = self.at
49
+
50
+ fun stepped(self, dt: Float) -> ViewDot = ViewDot(..self, clock: self.clock + dt)
51
+
52
+ fun view(self) -> List<Actor> =
53
+ [Sprite(x: 0.0, y: 0.0, tex: Texture(id: 1), w: 16.0, h: 16.0, srcX: 0.0, srcY: 0.0, srcW: 16.0, srcH: 16.0, frame: Int(self.clock * 8.0) % 4, tint: Color.White, touch: Disabled)]
54
+
55
+ struct HandDot[Actor](i: Int, at: Place, clock: Float) =
56
+ fun of(i: Int) -> HandDot = HandDot(i: i, at: place.at(Float(i % 100) * 8.0, Float(i / 100) * 6.0), clock: 0.0)
57
+
58
+ fun place(self) -> Place = self.at
59
+
60
+ fun stepped(self, dt: Float) -> HandDot = HandDot(..self, clock: self.clock + dt)
61
+
62
+ fun paint(self, io: Io, m: Affine, alpha: Float) =
63
+ frame := Int(self.clock * 8.0) % 4
64
+ sprite.drawSpriteFrame(io, m, alpha, 16.0, 16.0, Color.White, Texture(id: 1), Float(frame) * 16.0, 0.0, 16.0, 16.0)
65
+
66
+ enum CrowdAct = CrowdTick(dt: Float)
67
+
68
+ struct CrowdScreen[ScreenSlice<CrowdAct>](hand: Bool, dots: List<Actor>) =
69
+ fun of(hand: Bool, n: Int) -> CrowdScreen = CrowdScreen(hand: hand, dots: dotsFrom(hand, 0, n))
70
+
71
+ fun name(self) -> Str = "crowd"
72
+
73
+ fun view(self) -> List<Actor> = self.dots
74
+
75
+ fun tick(self, dt: Float) -> Option<CrowdAct> = Some(CrowdTick(dt: dt))
76
+
77
+ fun reduce(self, a: CrowdAct) -> CrowdScreen = match a
78
+ CrowdTick(dt) => CrowdScreen(..self, dots: self.dots.map(|d| stepDot(d, dt)))
79
+
80
+ fun frameSum(self) -> Int = timing.hash(self.dots.map(|d| frameOf(d)))
81
+
82
+ fun dotsFrom(hand: Bool, i: Int, n: Int) -> List<Actor> =
83
+ if i >= n
84
+ Nil
85
+ else
86
+ d: Actor := if hand then HandDot.of(i) else ViewDot.of(i)
87
+ Cons(d, dotsFrom(hand, i + 1, n))
88
+
89
+ fun stepDot(d: Actor, dt: Float) -> Actor = match d
90
+ ViewDot as v => v.stepped(dt)
91
+ HandDot as h => h.stepped(dt)
92
+ _ => d
93
+
94
+ fun frameOf(d: Actor) -> Int = match d
95
+ ViewDot as v => Int(v.clock * 8.0) % 4
96
+ HandDot as h => Int(h.clock * 8.0) % 4
97
+ _ => 0
98
+
99
+ fun crowdOf(g: Game) -> CrowdScreen = match g.screen()
100
+ CrowdScreen as c => c
101
+ _ => CrowdScreen(hand: false, dots: Nil)
102
+
103
+ fun tickOnce(io: Io, g: Game, paint: Bool) -> Game =
104
+ next := g.update(io, Input.create(), Clock.Tick)
105
+ if paint
106
+ next.draw(io)
107
+ next
108
+
109
+ # `ticks` ticks of `s` on an 800 by 600 screen, with no host (a test's).
110
+ fun run(io: Io, s: CrowdScreen, ticks: Int) -> CrowdScreen =
111
+ only: Screen := s
112
+ crowdOf(0.until(ticks).fold(Game(screens: [only]), |g, t| g.step(io, Input.create(), Clock.Tick, Vec2(x: 800.0, y: 600.0))))
113
+
114
+ fun main(io: Io) =
115
+ args := io.args()
116
+ paint := args.length() > 0 && args.at(0) == "paint"
117
+ hand := args.length() > 1 && args.at(1) == "hand"
118
+ only: Screen := CrowdScreen.of(hand, Count)
119
+ warm := 0.until(20).fold(Game(screens: [only]), |g, t| tickOnce(io, g, paint))
120
+ t0 := io.nowMicros()
121
+ done := 0.until(200).fold(warm, |g, t| tickOnce(io, g, paint))
122
+ total := io.nowMicros() - t0
123
+ io.printLn("us {Int(total)} ticks 200 checksum {crowdOf(done).frameSum()}")
124
+
125
+ test "both kinds of actor step and show the same frames"
126
+ v := run(io, CrowdScreen.of(false, 50), 30)
127
+ h := run(io, CrowdScreen.of(true, 50), 30)
128
+ assert v.frameSum() == h.frameSum()
129
+ assert headless.mount(io, v).drawn().length() == 100
130
+ assert headless.mount(io, h).drawn().length() == 50
benchmarks/screen_views/timing.plum ADDED
@@ -0,0 +1,47 @@
1
+ # What the drivers of benchmarks/screen_views share: Orbit Runner's scripted input
2
+ # (master's benchmarks/screen_views/timing.plum, benchmarks/engine_stages's, restarts on)
3
+ # and the checksum.
4
+ module timing
5
+
6
+ import std/Int
7
+ import std/Float
8
+ import std/Bool
9
+ import std/Slice
10
+ import std/collections/List
11
+ import std/math/Vec2
12
+ import std/game/input
13
+
14
+ # One of nine ways (or none) held for 20 ticks at a time, picked by the seed and the tick
15
+ # alone, and Enter going down every 150 ticks.
16
+ fun script(seed: Int, tick: Int) -> Input =
17
+ x0 := {seed * 1103515245 + {tick / 20} * 12345 + 12345} % 2147483648
18
+ x := {x0 * 1103515245 + 12345} % 2147483648
19
+ way := {x / 65536} % 9
20
+ keys := Slice<Int>()
21
+ if way % 3 == 0
22
+ keys.push(Key.Left)
23
+ if way % 3 == 2
24
+ keys.push(Key.KeyD)
25
+ if way / 3 == 0
26
+ keys.push(Key.KeyW)
27
+ if way / 3 == 2
28
+ keys.push(Key.Down)
29
+ events := Slice<InputEvent>()
30
+ if tick % 150 == 149
31
+ events.push(KeyDown(key: Key.Enter))
32
+ Input(held: Held(mouse: false, keys: keys), events: events, pos: Vec2(x: 0.0, y: 0.0), wheel: 0.0)
33
+
34
+ fun hash(xs: List<Int>) -> Int =
35
+ xs.fold(17, |h, x|
36
+ v0 := x % 1000000007
37
+ v := if v0 < 0 then v0 + 1000000007 else v0
38
+ {h * 31 + v} % 1000000007)
39
+
40
+ fun flag(b: Bool) -> Int = if b then 1 else 0
41
+
42
+ test "the script holds a way for 20 ticks and presses Enter every 150"
43
+ assert script(1, 0).held.same(script(1, 19).held)
44
+ assert script(1, 149).events.length() == 1
45
+ assert script(1, 148).events.isEmpty()
46
+ assert hash([1, 2]) == hash([1, 2])
47
+ assert hash([1, 2]) != hash([2, 1])