plum

#treesitter#compiler#wasm

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

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


fc03ef3 — Peter John 2026-11-10T22:01:10+05:30
Grid: get and set crash off the grid
Files changed (2) hide show
  1. docs/std.md +1 -1
  2. std/collections/Grid.plum +16 -3
docs/std.md CHANGED
@@ -98,7 +98,7 @@ fun main() =
98
98
 
99
99
  Renamed or removed: `Slice.head` (use `first`), `Set.toList` is now `toSlice`.
100
100
 
101
- **`Array`** has a fixed size: `Array.filled(n, value)`, `get`, `set`, `length`. **`Grid`** is a 2D array: `Grid.filled(w, h, value)`, `get(x, y)`, `set(x, y, value)`.
101
+ **`Array`** has a fixed size: `Array.filled(n, value)`, `get`, `set`, `length`. **`Grid`** is a 2D array: `Grid.filled(w, h, value)`, `get(x, y)`, `set(x, y, value)` (both crash with the cell and the size off the grid; `inBounds(x, y)` tells first).
102
102
 
103
103
  ## Collections
104
104
 
std/collections/Grid.plum CHANGED
@@ -3,13 +3,14 @@ module std
3
3
  import std/Array
4
4
  import std/Int
5
5
  import std/Float
6
+ import std/Str
6
7
 
7
8
  # A fixed-size 2D grid stored row-major in ONE flat `Array<T>` (so a
8
9
  # `Grid<Int>`/`Grid<Float>`/`Grid<Bool>` is a single native unboxed wasm
9
10
  # array). Cell `(x, y)` lives at `y * w + x`; `x` is the column in `[0, w)`,
10
- # `y` the row in `[0, h)`. `get`/`set` do no bounds check of their own beyond
11
+ # `y` the row in `[0, h)`. `get`/`set` crash with the cell and the size when `(x, y)` is
11
- # the underlying array's trap — use `inBounds` first when coordinates may stray
12
+ # off the grid (an `x` past the row would otherwise read the next row's cell): use
12
- # off the map (neighbour scans, pathfinding).
13
+ # `inBounds` first when coordinates may stray off the map (neighbour scans, pathfinding).
13
14
  struct Grid<T>(w: Int, h: Int, data: Array<T>) =
14
15
 
15
16
  # `Grid.filled(w, h, v)` — a `w` x `h` grid with every cell set to `v`
@@ -25,11 +26,19 @@ struct Grid<T>(w: Int, h: Int, data: Array<T>) =
25
26
  x >= 0 && y >= 0 && x < self.w && y < self.h
26
27
 
27
28
  fun get(self, x: Int, y: Int) -> T =
29
+ if !self.inBounds(x, y)
30
+ self.offGrid("get", x, y)
28
31
  self.data.get(y * self.w + x)
29
32
 
30
33
  fun set(self, x: Int, y: Int, v: T) -> Unit =
34
+ if !self.inBounds(x, y)
35
+ self.offGrid("set", x, y)
31
36
  self.data.set(y * self.w + x, v)
32
37
 
38
+ # Stops the program: `(x, y)` is not a cell of this grid.
39
+ fun offGrid(self, what: Str, x: Int, y: Int) -> Unit =
40
+ crash "Grid.{what}: cell ({x}, {y}) is off the {self.w} x {self.h} grid"
41
+
33
42
  test "Grid get/set address cells row-major and fill covers every cell"
34
43
  g := Grid.filled(4, 3, 0)
35
44
  assert g.w == 4
@@ -48,3 +57,7 @@ test "Grid inBounds rejects negative and too-large coordinates"
48
57
  assert g.inBounds(2, 0) == false
49
58
  assert g.inBounds(0, -1) == false
50
59
  assert g.get(1, 1) == 1.5
60
+
61
+ test "Grid get and set crash off the grid, a column past the row included"
62
+ g := Grid.filled(4, 3, 0)
63
+ assert g.get(3, 0) == 0 && g.get(0, 2) == 0