plum

#treesitter#compiler#wasm

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

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


69bec11 — Peter John 2026-11-01T22:47:00+05:30
test: struct-of-arrays probe tests from review
Files changed (1) hide show
  1. plum-examples/struct_arrays.plum +79 -0
plum-examples/struct_arrays.plum CHANGED
@@ -20,6 +20,21 @@ struct Tile(id: Int, solid: Bool, weight: Float)
20
20
 
21
21
  record World(points: List[Vec2], name: Str)
22
22
 
23
+ struct One(n: Int)
24
+ struct Flags(a: Bool, b: Bool, c: Int)
25
+ struct L3(r: Rect, k: Int)
26
+ record Holder(arr: Array[Vec2])
27
+ record Named(label: Str, v: Vec2)
28
+
29
+ fun bump(h: Holder) =
30
+ h.arr.set(0, Vec2(x: 77.0, y: 88.0))
31
+
32
+ fun first(a: Array[T]) -> T =
33
+ a.get(0)
34
+
35
+ fun mk() -> !Array[Vec2] =
36
+ Array[Vec2]().push(Vec2(x: 1.0, y: 2.0))
37
+
23
38
  test "List[Vec2] push/get/set round-trips across growth"
24
39
  l := List[Vec2]()
25
40
  for i := range 100
@@ -108,3 +123,67 @@ test "a List[Vec2] inside a record field"
108
123
  w.points.set(1, Vec2(x: 30.0, y: 40.0))
109
124
  assert w.points.get(1)!.x == 30.0
110
125
  assert w.points.get(0)!.y == 2.0
126
+
127
+ test "probe push growth, shrink, single leaf"
128
+ a := Array[One]()
129
+ for i := range 50
130
+ a = a.push(One(n: i))
131
+ assert a.length() == 50
132
+ assert a.get(49).n == 49
133
+ b := a.resize(10)
134
+ assert b.length() == 10
135
+ assert b.get(9).n == 9
136
+ c := b.resize(0)
137
+ assert c.length() == 0
138
+
139
+ test "probe Byte/Bool leaves"
140
+ a := Array[Flags]()
141
+ a = a.push(Flags(a: false, b: true, c: 3))
142
+ assert a.get(0).b
143
+ assert a.get(0).c == 3
144
+
145
+ test "probe three-level nested"
146
+ a := Array[L3]()
147
+ a = a.push(L3(r: Rect(pos: Vec2(x: 1.0, y: 2.0), size: Vec2(x: 3.0, y: 4.0)), k: 9))
148
+ assert a.get(0).r.size.y == 4.0
149
+ assert a.get(0).k == 9
150
+ assert a.get(0).r.area() == 12.0
151
+
152
+ test "probe array field reference semantics"
153
+ h := Holder(arr: Array[Vec2]().push(Vec2(x: 1.0, y: 1.0)))
154
+ bump(h)
155
+ assert h.arr.get(0).x == 77.0
156
+ alias := h.arr
157
+ alias.set(0, Vec2(x: 5.0, y: 5.0))
158
+ assert h.arr.get(0).x == 5.0
159
+
160
+ test "probe generic first, equality"
161
+ a := Array[Vec2]().push(Vec2(x: 1.0, y: 2.0)).push(Vec2(x: 1.0, y: 2.0))
162
+ assert first(a).y == 2.0
163
+ eq := a.get(0) == a.get(1)
164
+ assert eq
165
+
166
+ test "probe nullable and list and error"
167
+ n: Array[Vec2]? := Array[Vec2]().push(Vec2(x: 3.0, y: 3.0))
168
+ assert n!.get(0).x == 3.0
169
+ l := List[Array[Vec2]]()
170
+ l.push(Array[Vec2]().push(Vec2(x: 4.0, y: 4.0)))
171
+ assert l.get(0)!.get(0).x == 4.0
172
+ r := mk()!
173
+ assert r.get(0).y == 2.0
174
+
175
+ test "probe break/continue"
176
+ a := Array[Vec2]().push(Vec2(x: 1.0, y: 0.0)).push(Vec2(x: 2.0, y: 0.0)).push(Vec2(x: 3.0, y: 0.0)).push(Vec2(x: 4.0, y: 0.0))
177
+ s := 0.0
178
+ for v := range a
179
+ if v.x == 2.0
180
+ continue
181
+ if v.x == 4.0
182
+ break
183
+ s = s + v.x
184
+ assert s == 4.0
185
+
186
+ test "probe Str leaf"
187
+ a := Array[Named]()
188
+ a = a.push(Named(label: "hi", v: Vec2(x: 1.0, y: 2.0)))
189
+ assert a.get(0).label == "hi"