plum
git clone https://git.pyrossh.dev/plum
A statically typed, imperative programming language inspired by rust, python
18c860c
— Peter John
2026-07-24T07:42:28+05:30
docs: add implementation plan for wiring up List's methods
docs/superpowers/plans/2026-07-24-list-methods.md
ADDED
|
@@ -0,0 +1,1155 @@
|
|
|
1
|
+
# List Methods Implementation Plan
|
|
2
|
+
|
|
3
|
+
> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking.
|
|
4
|
+
|
|
5
|
+
**Goal:** Wire up `libs/std/list.plum`'s remaining `todo` methods (`add`, `set`, `removeAt`, `remove`, `clear`, `reverse`) plus a rewritten `join`, using the mutable `Node`/`head`/`tail`/`size` design already in place.
|
|
6
|
+
|
|
7
|
+
**Architecture:** Each method is implemented directly against the existing `Node(a)`/`List(a)` shapes using field assignment (`self.head = ...`, `node.next = ...`) and, for `add`, variadic iteration (`for v in values`). No type/grammar changes. Tests use small, self-contained inline fixtures (a minimal `Option`/`Node`/`List` trio plus whichever methods a test needs) — matching this project's established codegen-test convention — rather than depending on cross-file loading of the real `libs/std` files, which is a separate concern.
|
|
8
|
+
|
|
9
|
+
**Tech Stack:** Rust, the existing `plum-checker`/`plum-wasm-codegen` pipeline. No new dependencies.
|
|
10
|
+
|
|
11
|
+
## Global Constraints
|
|
12
|
+
|
|
13
|
+
- Spec: `docs/superpowers/specs/2026-07-24-list-methods-design.md`
|
|
14
|
+
- `add` appends to the tail (fixing the stale "to the start" doc comment); `reverse<List>(self) -> List` drops its predicate parameter.
|
|
15
|
+
- Every fresh-empty-list construction (including the PRE-EXISTING `init<List>`/`map<List>` methods' bare `List()` calls) must use explicit fields: `List(head: None, tail: None, size: 0)` — bare `List()` leaves `head`/`tail` as zero-filled memory, which likely decodes as a garbage `Some(...)` rather than `None` (see spec's "Additional fix" section).
|
|
16
|
+
- Default parameter values (`sep: Str = ","`) are parsed but NOT consulted at call sites for arity checking or substitution anywhere in the checker/codegen today (confirmed: no code path reads `Param.default` except monomorphization's structural clone) — this is a separate, un-scoped gap. Every test in this plan that calls a method with a defaulted param passes the argument explicitly; do not rely on omitting it.
|
|
17
|
+
- Out of scope: `sort`, `find`, `contains`, `flatMap`, `retain`, `reject`, `any`, `every`, `reduce`, `sublist`, `take`, `skip`, `drop`, `sample`, `shuffle`, `partition`, `chunk`, `groupBy`, and anything in `Map`.
|
|
18
|
+
- Run `cargo test --workspace` after every task — all pre-existing tests must keep passing throughout.
|
|
19
|
+
|
|
20
|
+
---
|
|
21
|
+
|
|
22
|
+
### Task 1: `add<List>` (+ fix `init`/`map`'s bare `List()`)
|
|
23
|
+
|
|
24
|
+
**Files:**
|
|
25
|
+
- Test: `plum-wasm-codegen/tests/codegen_tests.rs`
|
|
26
|
+
- Modify: `libs/std/list.plum` (`init`, `add`, `map`)
|
|
27
|
+
|
|
28
|
+
**Interfaces:**
|
|
29
|
+
- Consumes: field assignment (`self.field = ...`, `obj.field = ...`), variadic `for v in values` iteration — both already implemented.
|
|
30
|
+
- Produces: `add<List>(self, values: ...a)` — appends each value to the tail, in call order, updating `self.head`/`self.tail`/`self.size`. Later tasks' tests build lists via `add` (or via `List(head: None, tail: None, size: 0)` directly, as needed).
|
|
31
|
+
|
|
32
|
+
This is the riskiest task in the plan: `add` constructs a `Node(a)` instance (a class literal for one generic class) *inside* a method of a *different* generic class (`List`'s own `add<List>`), with `List`'s type parameter needing to flow into `Node`'s specialization. No existing test covers this exact shape (existing generic tests cover a class's method reading its own field, or a generic function call chain, not nested generic-class construction across two classes). If the RED step below fails with something other than a straightforward "method not implemented" trap — e.g. a monomorphization error about `Node`'s type parameter, or a codegen panic — **stop and report BLOCKED** rather than trying to work around it; that would mean this plan's risk assessment was right and the controller needs to decide how to proceed (fix a compiler gap first, or reshape the approach), not something to paper over inside this task.
|
|
33
|
+
|
|
34
|
+
- [ ] **Step 1: Write the failing test**
|
|
35
|
+
|
|
36
|
+
Add to `plum-wasm-codegen/tests/codegen_tests.rs`:
|
|
37
|
+
|
|
38
|
+
```rust
|
|
39
|
+
#[test]
|
|
40
|
+
fn list_add_appends_values_in_order_runs_correctly() {
|
|
41
|
+
let src = "\
|
|
42
|
+
enum Option =
|
|
43
|
+
| Some(a)
|
|
44
|
+
| None
|
|
45
|
+
|
|
46
|
+
type Node(a) =
|
|
47
|
+
value: a
|
|
48
|
+
prev: Option[Node]
|
|
49
|
+
next: Option[Node]
|
|
50
|
+
|
|
51
|
+
type List(a) =
|
|
52
|
+
head: Option[Node]
|
|
53
|
+
tail: Option[Node]
|
|
54
|
+
size: Int
|
|
55
|
+
|
|
56
|
+
get<List>(self, i: Int) -> Option(a) =
|
|
57
|
+
current = self.head
|
|
58
|
+
index = 0
|
|
59
|
+
while current != None
|
|
60
|
+
match current
|
|
61
|
+
Some(node) =>
|
|
62
|
+
if index == i
|
|
63
|
+
return Some(node.value)
|
|
64
|
+
current = node.next
|
|
65
|
+
index = index + 1
|
|
66
|
+
None =>
|
|
67
|
+
break
|
|
68
|
+
None
|
|
69
|
+
|
|
70
|
+
length<List>(self) -> Int =
|
|
71
|
+
self.size
|
|
72
|
+
|
|
73
|
+
add<List>(self, values: ...a) =
|
|
74
|
+
for v in values
|
|
75
|
+
newNode = Node(value: v, prev: self.tail, next: None)
|
|
76
|
+
match self.tail
|
|
77
|
+
Some(oldTail) =>
|
|
78
|
+
oldTail.next = Some(newNode)
|
|
79
|
+
None =>
|
|
80
|
+
self.head = Some(newNode)
|
|
81
|
+
self.tail = Some(newNode)
|
|
82
|
+
self.size = self.size + 1
|
|
83
|
+
|
|
84
|
+
unwrapOr(o: Option, default: Int) -> Int =
|
|
85
|
+
match o
|
|
86
|
+
Some(v) => v
|
|
87
|
+
None => default
|
|
88
|
+
|
|
89
|
+
main() -> Int =
|
|
90
|
+
l = List(head: None, tail: None, size: 0)
|
|
91
|
+
l.add(1, 2, 3)
|
|
92
|
+
a = unwrapOr(l.get(0), -1)
|
|
93
|
+
b = unwrapOr(l.get(1), -1)
|
|
94
|
+
c = unwrapOr(l.get(2), -1)
|
|
95
|
+
d = l.length()
|
|
96
|
+
a * 1000 + b * 100 + c * 10 + d
|
|
97
|
+
";
|
|
98
|
+
let source = parse(src);
|
|
99
|
+
let bytes = compile_source(&source).expect("compile failed");
|
|
100
|
+
assert_eq!(run_main(&bytes), 1233);
|
|
101
|
+
}
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
- [ ] **Step 2: Run the test to verify it fails**
|
|
105
|
+
|
|
106
|
+
Run: `cargo test -p plum-wasm-codegen list_add_appends_values_in_order 2>&1 | tail -60`
|
|
107
|
+
Expected: FAIL — `add`'s body in this inline test source already has the real implementation (Step 1 writes the test with the real `add` body directly in the test source, since this test doesn't depend on `libs/std/list.plum` at all), so this run is really the first real exercise of the nested-generic-construction risk described above. If it fails with a clear codegen/checker error about `Node`'s type resolution, that confirms the risk; if it fails only because you haven't run it yet (trivial), re-check you actually ran the command.
|
|
108
|
+
|
|
109
|
+
- [ ] **Step 3: If it passes (or fails only for a benign reason), apply the same `add` implementation to `libs/std/list.plum`**
|
|
110
|
+
|
|
111
|
+
In `libs/std/list.plum`, replace:
|
|
112
|
+
|
|
113
|
+
```
|
|
114
|
+
init<List>(self, values: ...a) -> List =
|
|
115
|
+
List().add(values)
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
with:
|
|
119
|
+
|
|
120
|
+
```
|
|
121
|
+
init<List>(self, values: ...a) -> List =
|
|
122
|
+
List(head: None, tail: None, size: 0).add(values)
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
Replace:
|
|
126
|
+
|
|
127
|
+
```
|
|
128
|
+
# adds the specified elements to the start of the list
|
|
129
|
+
add<List>(self, values: ...a) =
|
|
130
|
+
todo
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
with:
|
|
134
|
+
|
|
135
|
+
```
|
|
136
|
+
# adds the specified elements to the end of the list
|
|
137
|
+
add<List>(self, values: ...a) =
|
|
138
|
+
for v in values
|
|
139
|
+
newNode = Node(value: v, prev: self.tail, next: None)
|
|
140
|
+
match self.tail
|
|
141
|
+
Some(oldTail) =>
|
|
142
|
+
oldTail.next = Some(newNode)
|
|
143
|
+
None =>
|
|
144
|
+
self.head = Some(newNode)
|
|
145
|
+
self.tail = Some(newNode)
|
|
146
|
+
self.size = self.size + 1
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
And in `map<List>`, replace:
|
|
150
|
+
|
|
151
|
+
```
|
|
152
|
+
map<List>(self, cb: fn(a) -> b) -> List(b) =
|
|
153
|
+
nl = List()
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
with:
|
|
157
|
+
|
|
158
|
+
```
|
|
159
|
+
map<List>(self, cb: fn(a) -> b) -> List(b) =
|
|
160
|
+
nl = List(head: None, tail: None, size: 0)
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
(Leave the rest of `map`'s body unchanged.)
|
|
164
|
+
|
|
165
|
+
- [ ] **Step 4: Run the test to verify it passes**
|
|
166
|
+
|
|
167
|
+
Run: `cargo test -p plum-wasm-codegen list_add_appends_values_in_order 2>&1 | tail -60`
|
|
168
|
+
Expected: PASS (`1233`).
|
|
169
|
+
|
|
170
|
+
- [ ] **Step 5: Run the full workspace test suite**
|
|
171
|
+
|
|
172
|
+
Run: `cargo test --workspace 2>&1 | tail -100`
|
|
173
|
+
Expected: all tests PASS (no existing test compiles the real `libs/std/list.plum`, so the `init`/`add`/`map` edits there have no effect on the Rust test suite — this step exists to catch the unexpected case where something does reference it).
|
|
174
|
+
|
|
175
|
+
- [ ] **Step 6: Commit**
|
|
176
|
+
|
|
177
|
+
```bash
|
|
178
|
+
git add plum-wasm-codegen/tests/codegen_tests.rs libs/std/list.plum
|
|
179
|
+
git commit -m "feat(libs/std): implement List.add, fix List() zero-field construction in init/map"
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
---
|
|
183
|
+
|
|
184
|
+
### Task 2: `set<List>`
|
|
185
|
+
|
|
186
|
+
**Files:**
|
|
187
|
+
- Test: `plum-wasm-codegen/tests/codegen_tests.rs`
|
|
188
|
+
- Modify: `libs/std/list.plum` (`set`)
|
|
189
|
+
|
|
190
|
+
**Interfaces:**
|
|
191
|
+
- Consumes: `add<List>` (Task 1, for building test fixtures), field assignment on a match-bound local (`node.value = v`).
|
|
192
|
+
- Produces: `set<List>(self, i: Int, v: a) -> Option(a)` — replaces the value at index `i`, returning the old value wrapped in `Some`, or `None` if `i` is out of range.
|
|
193
|
+
|
|
194
|
+
- [ ] **Step 1: Write the failing tests**
|
|
195
|
+
|
|
196
|
+
Add to `plum-wasm-codegen/tests/codegen_tests.rs` (reusing the same `Option`/`Node`/`List`/`get`/`length`/`add`/`unwrapOr` preamble as Task 1's test — repeated here in full since each test source is self-contained):
|
|
197
|
+
|
|
198
|
+
```rust
|
|
199
|
+
#[test]
|
|
200
|
+
fn list_set_in_range_replaces_value_and_returns_old_runs_correctly() {
|
|
201
|
+
let src = "\
|
|
202
|
+
enum Option =
|
|
203
|
+
| Some(a)
|
|
204
|
+
| None
|
|
205
|
+
|
|
206
|
+
type Node(a) =
|
|
207
|
+
value: a
|
|
208
|
+
prev: Option[Node]
|
|
209
|
+
next: Option[Node]
|
|
210
|
+
|
|
211
|
+
type List(a) =
|
|
212
|
+
head: Option[Node]
|
|
213
|
+
tail: Option[Node]
|
|
214
|
+
size: Int
|
|
215
|
+
|
|
216
|
+
get<List>(self, i: Int) -> Option(a) =
|
|
217
|
+
current = self.head
|
|
218
|
+
index = 0
|
|
219
|
+
while current != None
|
|
220
|
+
match current
|
|
221
|
+
Some(node) =>
|
|
222
|
+
if index == i
|
|
223
|
+
return Some(node.value)
|
|
224
|
+
current = node.next
|
|
225
|
+
index = index + 1
|
|
226
|
+
None =>
|
|
227
|
+
break
|
|
228
|
+
None
|
|
229
|
+
|
|
230
|
+
length<List>(self) -> Int =
|
|
231
|
+
self.size
|
|
232
|
+
|
|
233
|
+
add<List>(self, values: ...a) =
|
|
234
|
+
for v in values
|
|
235
|
+
newNode = Node(value: v, prev: self.tail, next: None)
|
|
236
|
+
match self.tail
|
|
237
|
+
Some(oldTail) =>
|
|
238
|
+
oldTail.next = Some(newNode)
|
|
239
|
+
None =>
|
|
240
|
+
self.head = Some(newNode)
|
|
241
|
+
self.tail = Some(newNode)
|
|
242
|
+
self.size = self.size + 1
|
|
243
|
+
|
|
244
|
+
set<List>(self, i: Int, v: a) -> Option(a) =
|
|
245
|
+
current = self.head
|
|
246
|
+
index = 0
|
|
247
|
+
while current != None
|
|
248
|
+
match current
|
|
249
|
+
Some(node) =>
|
|
250
|
+
if index == i
|
|
251
|
+
oldValue = node.value
|
|
252
|
+
node.value = v
|
|
253
|
+
return Some(oldValue)
|
|
254
|
+
current = node.next
|
|
255
|
+
index = index + 1
|
|
256
|
+
None =>
|
|
257
|
+
break
|
|
258
|
+
None
|
|
259
|
+
|
|
260
|
+
unwrapOr(o: Option, default: Int) -> Int =
|
|
261
|
+
match o
|
|
262
|
+
Some(v) => v
|
|
263
|
+
None => default
|
|
264
|
+
|
|
265
|
+
main() -> Int =
|
|
266
|
+
l = List(head: None, tail: None, size: 0)
|
|
267
|
+
l.add(1, 2, 3)
|
|
268
|
+
old = unwrapOr(l.set(1, 99), -1)
|
|
269
|
+
new = unwrapOr(l.get(1), -1)
|
|
270
|
+
old * 1000 + new
|
|
271
|
+
";
|
|
272
|
+
let source = parse(src);
|
|
273
|
+
let bytes = compile_source(&source).expect("compile failed");
|
|
274
|
+
assert_eq!(run_main(&bytes), 2099);
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
#[test]
|
|
278
|
+
fn list_set_out_of_range_returns_none_runs_correctly() {
|
|
279
|
+
let src = "\
|
|
280
|
+
enum Option =
|
|
281
|
+
| Some(a)
|
|
282
|
+
| None
|
|
283
|
+
|
|
284
|
+
type Node(a) =
|
|
285
|
+
value: a
|
|
286
|
+
prev: Option[Node]
|
|
287
|
+
next: Option[Node]
|
|
288
|
+
|
|
289
|
+
type List(a) =
|
|
290
|
+
head: Option[Node]
|
|
291
|
+
tail: Option[Node]
|
|
292
|
+
size: Int
|
|
293
|
+
|
|
294
|
+
length<List>(self) -> Int =
|
|
295
|
+
self.size
|
|
296
|
+
|
|
297
|
+
add<List>(self, values: ...a) =
|
|
298
|
+
for v in values
|
|
299
|
+
newNode = Node(value: v, prev: self.tail, next: None)
|
|
300
|
+
match self.tail
|
|
301
|
+
Some(oldTail) =>
|
|
302
|
+
oldTail.next = Some(newNode)
|
|
303
|
+
None =>
|
|
304
|
+
self.head = Some(newNode)
|
|
305
|
+
self.tail = Some(newNode)
|
|
306
|
+
self.size = self.size + 1
|
|
307
|
+
|
|
308
|
+
set<List>(self, i: Int, v: a) -> Option(a) =
|
|
309
|
+
current = self.head
|
|
310
|
+
index = 0
|
|
311
|
+
while current != None
|
|
312
|
+
match current
|
|
313
|
+
Some(node) =>
|
|
314
|
+
if index == i
|
|
315
|
+
oldValue = node.value
|
|
316
|
+
node.value = v
|
|
317
|
+
return Some(oldValue)
|
|
318
|
+
current = node.next
|
|
319
|
+
index = index + 1
|
|
320
|
+
None =>
|
|
321
|
+
break
|
|
322
|
+
None
|
|
323
|
+
|
|
324
|
+
main() -> Int =
|
|
325
|
+
l = List(head: None, tail: None, size: 0)
|
|
326
|
+
l.add(1, 2, 3)
|
|
327
|
+
result = l.set(10, 99)
|
|
328
|
+
match result
|
|
329
|
+
Some(v) =>
|
|
330
|
+
1
|
|
331
|
+
None =>
|
|
332
|
+
0
|
|
333
|
+
";
|
|
334
|
+
let source = parse(src);
|
|
335
|
+
let bytes = compile_source(&source).expect("compile failed");
|
|
336
|
+
assert_eq!(run_main(&bytes), 0);
|
|
337
|
+
}
|
|
338
|
+
```
|
|
339
|
+
|
|
340
|
+
- [ ] **Step 2: Run the tests to verify they fail**
|
|
341
|
+
|
|
342
|
+
Run: `cargo test -p plum-wasm-codegen list_set_ 2>&1 | tail -80`
|
|
343
|
+
Expected: FAIL to compile (crate-internal `set`'s body is already written directly in the test source in Step 1, so this exercises the real implementation immediately — same reasoning as Task 1). If it fails for a reason other than "these tests don't exist yet before you add them," re-check.
|
|
344
|
+
|
|
345
|
+
- [ ] **Step 3: Apply the same `set` implementation to `libs/std/list.plum`**
|
|
346
|
+
|
|
347
|
+
Replace:
|
|
348
|
+
|
|
349
|
+
```
|
|
350
|
+
# sets the element at i'th index of the list
|
|
351
|
+
set<List>(self, i: Int, v: a) -> Option(a) =
|
|
352
|
+
todo
|
|
353
|
+
```
|
|
354
|
+
|
|
355
|
+
with:
|
|
356
|
+
|
|
357
|
+
```
|
|
358
|
+
# sets the element at i'th index of the list
|
|
359
|
+
set<List>(self, i: Int, v: a) -> Option(a) =
|
|
360
|
+
current = self.head
|
|
361
|
+
index = 0
|
|
362
|
+
while current != None
|
|
363
|
+
match current
|
|
364
|
+
Some(node) =>
|
|
365
|
+
if index == i
|
|
366
|
+
oldValue = node.value
|
|
367
|
+
node.value = v
|
|
368
|
+
return Some(oldValue)
|
|
369
|
+
current = node.next
|
|
370
|
+
index = index + 1
|
|
371
|
+
None =>
|
|
372
|
+
break
|
|
373
|
+
None
|
|
374
|
+
```
|
|
375
|
+
|
|
376
|
+
- [ ] **Step 4: Run the tests to verify they pass**
|
|
377
|
+
|
|
378
|
+
Run: `cargo test -p plum-wasm-codegen list_set_ 2>&1 | tail -80`
|
|
379
|
+
Expected: both PASS (`2099`, `0`).
|
|
380
|
+
|
|
381
|
+
- [ ] **Step 5: Run the full workspace test suite**
|
|
382
|
+
|
|
383
|
+
Run: `cargo test --workspace 2>&1 | tail -100`
|
|
384
|
+
Expected: all tests PASS.
|
|
385
|
+
|
|
386
|
+
- [ ] **Step 6: Commit**
|
|
387
|
+
|
|
388
|
+
```bash
|
|
389
|
+
git add plum-wasm-codegen/tests/codegen_tests.rs libs/std/list.plum
|
|
390
|
+
git commit -m "feat(libs/std): implement List.set"
|
|
391
|
+
```
|
|
392
|
+
|
|
393
|
+
---
|
|
394
|
+
|
|
395
|
+
### Task 3: `removeAt<List>` and `remove<List>`
|
|
396
|
+
|
|
397
|
+
**Files:**
|
|
398
|
+
- Test: `plum-wasm-codegen/tests/codegen_tests.rs`
|
|
399
|
+
- Modify: `libs/std/list.plum` (`removeAt`, `remove`)
|
|
400
|
+
|
|
401
|
+
**Interfaces:**
|
|
402
|
+
- Consumes: `add<List>` (Task 1, for building test fixtures).
|
|
403
|
+
- Produces: `removeAt<List>(self, i: Int)` and `remove<List>(self, v: a)` — both unlink the target node (by index / by value) from the doubly-linked chain, fixing up `self.head`/`self.tail`/neighboring `prev`/`next`, and decrement `self.size`. Both are a no-op if no matching node is found (no error), matching `get`'s existing out-of-range convention.
|
|
404
|
+
|
|
405
|
+
- [ ] **Step 1: Write the failing tests**
|
|
406
|
+
|
|
407
|
+
Add to `plum-wasm-codegen/tests/codegen_tests.rs`:
|
|
408
|
+
|
|
409
|
+
```rust
|
|
410
|
+
#[test]
|
|
411
|
+
fn list_remove_at_head_updates_list_correctly() {
|
|
412
|
+
let src = "\
|
|
413
|
+
enum Option =
|
|
414
|
+
| Some(a)
|
|
415
|
+
| None
|
|
416
|
+
|
|
417
|
+
type Node(a) =
|
|
418
|
+
value: a
|
|
419
|
+
prev: Option[Node]
|
|
420
|
+
next: Option[Node]
|
|
421
|
+
|
|
422
|
+
type List(a) =
|
|
423
|
+
head: Option[Node]
|
|
424
|
+
tail: Option[Node]
|
|
425
|
+
size: Int
|
|
426
|
+
|
|
427
|
+
get<List>(self, i: Int) -> Option(a) =
|
|
428
|
+
current = self.head
|
|
429
|
+
index = 0
|
|
430
|
+
while current != None
|
|
431
|
+
match current
|
|
432
|
+
Some(node) =>
|
|
433
|
+
if index == i
|
|
434
|
+
return Some(node.value)
|
|
435
|
+
current = node.next
|
|
436
|
+
index = index + 1
|
|
437
|
+
None =>
|
|
438
|
+
break
|
|
439
|
+
None
|
|
440
|
+
|
|
441
|
+
length<List>(self) -> Int =
|
|
442
|
+
self.size
|
|
443
|
+
|
|
444
|
+
add<List>(self, values: ...a) =
|
|
445
|
+
for v in values
|
|
446
|
+
newNode = Node(value: v, prev: self.tail, next: None)
|
|
447
|
+
match self.tail
|
|
448
|
+
Some(oldTail) =>
|
|
449
|
+
oldTail.next = Some(newNode)
|
|
450
|
+
None =>
|
|
451
|
+
self.head = Some(newNode)
|
|
452
|
+
self.tail = Some(newNode)
|
|
453
|
+
self.size = self.size + 1
|
|
454
|
+
|
|
455
|
+
removeAt<List>(self, i: Int) =
|
|
456
|
+
current = self.head
|
|
457
|
+
index = 0
|
|
458
|
+
while current != None
|
|
459
|
+
match current
|
|
460
|
+
Some(node) =>
|
|
461
|
+
if index == i
|
|
462
|
+
match node.prev
|
|
463
|
+
Some(p) =>
|
|
464
|
+
p.next = node.next
|
|
465
|
+
None =>
|
|
466
|
+
self.head = node.next
|
|
467
|
+
match node.next
|
|
468
|
+
Some(n) =>
|
|
469
|
+
n.prev = node.prev
|
|
470
|
+
None =>
|
|
471
|
+
self.tail = node.prev
|
|
472
|
+
self.size = self.size - 1
|
|
473
|
+
return
|
|
474
|
+
current = node.next
|
|
475
|
+
index = index + 1
|
|
476
|
+
None =>
|
|
477
|
+
break
|
|
478
|
+
|
|
479
|
+
unwrapOr(o: Option, default: Int) -> Int =
|
|
480
|
+
match o
|
|
481
|
+
Some(v) => v
|
|
482
|
+
None => default
|
|
483
|
+
|
|
484
|
+
main() -> Int =
|
|
485
|
+
l = List(head: None, tail: None, size: 0)
|
|
486
|
+
l.add(1, 2, 3)
|
|
487
|
+
l.removeAt(0)
|
|
488
|
+
a = unwrapOr(l.get(0), -1)
|
|
489
|
+
b = unwrapOr(l.get(1), -1)
|
|
490
|
+
c = l.length()
|
|
491
|
+
a * 100 + b * 10 + c
|
|
492
|
+
";
|
|
493
|
+
let source = parse(src);
|
|
494
|
+
let bytes = compile_source(&source).expect("compile failed");
|
|
495
|
+
assert_eq!(run_main(&bytes), 232);
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
#[test]
|
|
499
|
+
fn list_remove_at_tail_relinks_tail_pointer_correctly() {
|
|
500
|
+
let src = "\
|
|
501
|
+
enum Option =
|
|
502
|
+
| Some(a)
|
|
503
|
+
| None
|
|
504
|
+
|
|
505
|
+
type Node(a) =
|
|
506
|
+
value: a
|
|
507
|
+
prev: Option[Node]
|
|
508
|
+
next: Option[Node]
|
|
509
|
+
|
|
510
|
+
type List(a) =
|
|
511
|
+
head: Option[Node]
|
|
512
|
+
tail: Option[Node]
|
|
513
|
+
size: Int
|
|
514
|
+
|
|
515
|
+
get<List>(self, i: Int) -> Option(a) =
|
|
516
|
+
current = self.head
|
|
517
|
+
index = 0
|
|
518
|
+
while current != None
|
|
519
|
+
match current
|
|
520
|
+
Some(node) =>
|
|
521
|
+
if index == i
|
|
522
|
+
return Some(node.value)
|
|
523
|
+
current = node.next
|
|
524
|
+
index = index + 1
|
|
525
|
+
None =>
|
|
526
|
+
break
|
|
527
|
+
None
|
|
528
|
+
|
|
529
|
+
length<List>(self) -> Int =
|
|
530
|
+
self.size
|
|
531
|
+
|
|
532
|
+
add<List>(self, values: ...a) =
|
|
533
|
+
for v in values
|
|
534
|
+
newNode = Node(value: v, prev: self.tail, next: None)
|
|
535
|
+
match self.tail
|
|
536
|
+
Some(oldTail) =>
|
|
537
|
+
oldTail.next = Some(newNode)
|
|
538
|
+
None =>
|
|
539
|
+
self.head = Some(newNode)
|
|
540
|
+
self.tail = Some(newNode)
|
|
541
|
+
self.size = self.size + 1
|
|
542
|
+
|
|
543
|
+
removeAt<List>(self, i: Int) =
|
|
544
|
+
current = self.head
|
|
545
|
+
index = 0
|
|
546
|
+
while current != None
|
|
547
|
+
match current
|
|
548
|
+
Some(node) =>
|
|
549
|
+
if index == i
|
|
550
|
+
match node.prev
|
|
551
|
+
Some(p) =>
|
|
552
|
+
p.next = node.next
|
|
553
|
+
None =>
|
|
554
|
+
self.head = node.next
|
|
555
|
+
match node.next
|
|
556
|
+
Some(n) =>
|
|
557
|
+
n.prev = node.prev
|
|
558
|
+
None =>
|
|
559
|
+
self.tail = node.prev
|
|
560
|
+
self.size = self.size - 1
|
|
561
|
+
return
|
|
562
|
+
current = node.next
|
|
563
|
+
index = index + 1
|
|
564
|
+
None =>
|
|
565
|
+
break
|
|
566
|
+
|
|
567
|
+
unwrapOr(o: Option, default: Int) -> Int =
|
|
568
|
+
match o
|
|
569
|
+
Some(v) => v
|
|
570
|
+
None => default
|
|
571
|
+
|
|
572
|
+
main() -> Int =
|
|
573
|
+
l = List(head: None, tail: None, size: 0)
|
|
574
|
+
l.add(1, 2, 3)
|
|
575
|
+
l.removeAt(2)
|
|
576
|
+
l.add(4)
|
|
577
|
+
a = unwrapOr(l.get(0), -1)
|
|
578
|
+
b = unwrapOr(l.get(1), -1)
|
|
579
|
+
c = unwrapOr(l.get(2), -1)
|
|
580
|
+
d = l.length()
|
|
581
|
+
a * 1000 + b * 100 + c * 10 + d
|
|
582
|
+
";
|
|
583
|
+
let source = parse(src);
|
|
584
|
+
let bytes = compile_source(&source).expect("compile failed");
|
|
585
|
+
assert_eq!(run_main(&bytes), 1243);
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
#[test]
|
|
589
|
+
fn list_remove_by_value_removes_middle_element_runs_correctly() {
|
|
590
|
+
let src = "\
|
|
591
|
+
enum Option =
|
|
592
|
+
| Some(a)
|
|
593
|
+
| None
|
|
594
|
+
|
|
595
|
+
type Node(a) =
|
|
596
|
+
value: a
|
|
597
|
+
prev: Option[Node]
|
|
598
|
+
next: Option[Node]
|
|
599
|
+
|
|
600
|
+
type List(a) =
|
|
601
|
+
head: Option[Node]
|
|
602
|
+
tail: Option[Node]
|
|
603
|
+
size: Int
|
|
604
|
+
|
|
605
|
+
get<List>(self, i: Int) -> Option(a) =
|
|
606
|
+
current = self.head
|
|
607
|
+
index = 0
|
|
608
|
+
while current != None
|
|
609
|
+
match current
|
|
610
|
+
Some(node) =>
|
|
611
|
+
if index == i
|
|
612
|
+
return Some(node.value)
|
|
613
|
+
current = node.next
|
|
614
|
+
index = index + 1
|
|
615
|
+
None =>
|
|
616
|
+
break
|
|
617
|
+
None
|
|
618
|
+
|
|
619
|
+
length<List>(self) -> Int =
|
|
620
|
+
self.size
|
|
621
|
+
|
|
622
|
+
add<List>(self, values: ...a) =
|
|
623
|
+
for v in values
|
|
624
|
+
newNode = Node(value: v, prev: self.tail, next: None)
|
|
625
|
+
match self.tail
|
|
626
|
+
Some(oldTail) =>
|
|
627
|
+
oldTail.next = Some(newNode)
|
|
628
|
+
None =>
|
|
629
|
+
self.head = Some(newNode)
|
|
630
|
+
self.tail = Some(newNode)
|
|
631
|
+
self.size = self.size + 1
|
|
632
|
+
|
|
633
|
+
remove<List>(self, v: a) =
|
|
634
|
+
current = self.head
|
|
635
|
+
while current != None
|
|
636
|
+
match current
|
|
637
|
+
Some(node) =>
|
|
638
|
+
if node.value == v
|
|
639
|
+
match node.prev
|
|
640
|
+
Some(p) =>
|
|
641
|
+
p.next = node.next
|
|
642
|
+
None =>
|
|
643
|
+
self.head = node.next
|
|
644
|
+
match node.next
|
|
645
|
+
Some(n) =>
|
|
646
|
+
n.prev = node.prev
|
|
647
|
+
None =>
|
|
648
|
+
self.tail = node.prev
|
|
649
|
+
self.size = self.size - 1
|
|
650
|
+
return
|
|
651
|
+
current = node.next
|
|
652
|
+
None =>
|
|
653
|
+
break
|
|
654
|
+
|
|
655
|
+
unwrapOr(o: Option, default: Int) -> Int =
|
|
656
|
+
match o
|
|
657
|
+
Some(v) => v
|
|
658
|
+
None => default
|
|
659
|
+
|
|
660
|
+
main() -> Int =
|
|
661
|
+
l = List(head: None, tail: None, size: 0)
|
|
662
|
+
l.add(1, 2, 3)
|
|
663
|
+
l.remove(2)
|
|
664
|
+
a = unwrapOr(l.get(0), -1)
|
|
665
|
+
b = unwrapOr(l.get(1), -1)
|
|
666
|
+
c = l.length()
|
|
667
|
+
a * 100 + b * 10 + c
|
|
668
|
+
";
|
|
669
|
+
let source = parse(src);
|
|
670
|
+
let bytes = compile_source(&source).expect("compile failed");
|
|
671
|
+
assert_eq!(run_main(&bytes), 132);
|
|
672
|
+
}
|
|
673
|
+
```
|
|
674
|
+
|
|
675
|
+
- [ ] **Step 2: Run the tests to verify they fail**
|
|
676
|
+
|
|
677
|
+
Run: `cargo test -p plum-wasm-codegen list_remove 2>&1 | tail -100`
|
|
678
|
+
Expected: FAIL to compile (the tests don't exist before you add them; their bodies already contain the real implementation, matching Task 1/2's pattern).
|
|
679
|
+
|
|
680
|
+
- [ ] **Step 3: Apply the same implementations to `libs/std/list.plum`**
|
|
681
|
+
|
|
682
|
+
Replace:
|
|
683
|
+
|
|
684
|
+
```
|
|
685
|
+
# removes the element at i'th index of the list
|
|
686
|
+
removeAt<List>(self, i: Int) =
|
|
687
|
+
todo
|
|
688
|
+
```
|
|
689
|
+
|
|
690
|
+
with:
|
|
691
|
+
|
|
692
|
+
```
|
|
693
|
+
# removes the element at i'th index of the list
|
|
694
|
+
removeAt<List>(self, i: Int) =
|
|
695
|
+
current = self.head
|
|
696
|
+
index = 0
|
|
697
|
+
while current != None
|
|
698
|
+
match current
|
|
699
|
+
Some(node) =>
|
|
700
|
+
if index == i
|
|
701
|
+
match node.prev
|
|
702
|
+
Some(p) =>
|
|
703
|
+
p.next = node.next
|
|
704
|
+
None =>
|
|
705
|
+
self.head = node.next
|
|
706
|
+
match node.next
|
|
707
|
+
Some(n) =>
|
|
708
|
+
n.prev = node.prev
|
|
709
|
+
None =>
|
|
710
|
+
self.tail = node.prev
|
|
711
|
+
self.size = self.size - 1
|
|
712
|
+
return
|
|
713
|
+
current = node.next
|
|
714
|
+
index = index + 1
|
|
715
|
+
None =>
|
|
716
|
+
break
|
|
717
|
+
```
|
|
718
|
+
|
|
719
|
+
Replace:
|
|
720
|
+
|
|
721
|
+
```
|
|
722
|
+
# removes the element v from list
|
|
723
|
+
remove<List>(self, v: a) =
|
|
724
|
+
todo
|
|
725
|
+
```
|
|
726
|
+
|
|
727
|
+
with:
|
|
728
|
+
|
|
729
|
+
```
|
|
730
|
+
# removes the element v from list
|
|
731
|
+
remove<List>(self, v: a) =
|
|
732
|
+
current = self.head
|
|
733
|
+
while current != None
|
|
734
|
+
match current
|
|
735
|
+
Some(node) =>
|
|
736
|
+
if node.value == v
|
|
737
|
+
match node.prev
|
|
738
|
+
Some(p) =>
|
|
739
|
+
p.next = node.next
|
|
740
|
+
None =>
|
|
741
|
+
self.head = node.next
|
|
742
|
+
match node.next
|
|
743
|
+
Some(n) =>
|
|
744
|
+
n.prev = node.prev
|
|
745
|
+
None =>
|
|
746
|
+
self.tail = node.prev
|
|
747
|
+
self.size = self.size - 1
|
|
748
|
+
return
|
|
749
|
+
current = node.next
|
|
750
|
+
None =>
|
|
751
|
+
break
|
|
752
|
+
```
|
|
753
|
+
|
|
754
|
+
- [ ] **Step 4: Run the tests to verify they pass**
|
|
755
|
+
|
|
756
|
+
Run: `cargo test -p plum-wasm-codegen list_remove 2>&1 | tail -100`
|
|
757
|
+
Expected: all 3 PASS (`232`, `1243`, `132`).
|
|
758
|
+
|
|
759
|
+
- [ ] **Step 5: Run the full workspace test suite**
|
|
760
|
+
|
|
761
|
+
Run: `cargo test --workspace 2>&1 | tail -100`
|
|
762
|
+
Expected: all tests PASS.
|
|
763
|
+
|
|
764
|
+
- [ ] **Step 6: Commit**
|
|
765
|
+
|
|
766
|
+
```bash
|
|
767
|
+
git add plum-wasm-codegen/tests/codegen_tests.rs libs/std/list.plum
|
|
768
|
+
git commit -m "feat(libs/std): implement List.removeAt and List.remove"
|
|
769
|
+
```
|
|
770
|
+
|
|
771
|
+
---
|
|
772
|
+
|
|
773
|
+
### Task 4: `clear<List>` and `reverse<List>`
|
|
774
|
+
|
|
775
|
+
**Files:**
|
|
776
|
+
- Test: `plum-wasm-codegen/tests/codegen_tests.rs`
|
|
777
|
+
- Modify: `libs/std/list.plum` (`clear`, `reverse`)
|
|
778
|
+
|
|
779
|
+
**Interfaces:**
|
|
780
|
+
- Consumes: `add<List>` (Task 1).
|
|
781
|
+
- Produces: `clear<List>(self)` — resets `head`/`tail` to `None`, `size` to `0`. `reverse<List>(self) -> List` (predicate parameter dropped, per the spec) — returns a NEW list with elements in reverse order, built via `add`, leaving `self` unmodified.
|
|
782
|
+
|
|
783
|
+
- [ ] **Step 1: Write the failing tests**
|
|
784
|
+
|
|
785
|
+
Add to `plum-wasm-codegen/tests/codegen_tests.rs`:
|
|
786
|
+
|
|
787
|
+
```rust
|
|
788
|
+
#[test]
|
|
789
|
+
fn list_clear_resets_list_to_empty_runs_correctly() {
|
|
790
|
+
let src = "\
|
|
791
|
+
enum Option =
|
|
792
|
+
| Some(a)
|
|
793
|
+
| None
|
|
794
|
+
|
|
795
|
+
type Node(a) =
|
|
796
|
+
value: a
|
|
797
|
+
prev: Option[Node]
|
|
798
|
+
next: Option[Node]
|
|
799
|
+
|
|
800
|
+
type List(a) =
|
|
801
|
+
head: Option[Node]
|
|
802
|
+
tail: Option[Node]
|
|
803
|
+
size: Int
|
|
804
|
+
|
|
805
|
+
get<List>(self, i: Int) -> Option(a) =
|
|
806
|
+
current = self.head
|
|
807
|
+
index = 0
|
|
808
|
+
while current != None
|
|
809
|
+
match current
|
|
810
|
+
Some(node) =>
|
|
811
|
+
if index == i
|
|
812
|
+
return Some(node.value)
|
|
813
|
+
current = node.next
|
|
814
|
+
index = index + 1
|
|
815
|
+
None =>
|
|
816
|
+
break
|
|
817
|
+
None
|
|
818
|
+
|
|
819
|
+
length<List>(self) -> Int =
|
|
820
|
+
self.size
|
|
821
|
+
|
|
822
|
+
add<List>(self, values: ...a) =
|
|
823
|
+
for v in values
|
|
824
|
+
newNode = Node(value: v, prev: self.tail, next: None)
|
|
825
|
+
match self.tail
|
|
826
|
+
Some(oldTail) =>
|
|
827
|
+
oldTail.next = Some(newNode)
|
|
828
|
+
None =>
|
|
829
|
+
self.head = Some(newNode)
|
|
830
|
+
self.tail = Some(newNode)
|
|
831
|
+
self.size = self.size + 1
|
|
832
|
+
|
|
833
|
+
clear<List>(self) =
|
|
834
|
+
self.head = None
|
|
835
|
+
self.tail = None
|
|
836
|
+
self.size = 0
|
|
837
|
+
|
|
838
|
+
unwrapOr(o: Option, default: Int) -> Int =
|
|
839
|
+
match o
|
|
840
|
+
Some(v) => v
|
|
841
|
+
None => default
|
|
842
|
+
|
|
843
|
+
main() -> Int =
|
|
844
|
+
l = List(head: None, tail: None, size: 0)
|
|
845
|
+
l.add(1, 2, 3)
|
|
846
|
+
l.clear()
|
|
847
|
+
a = l.length()
|
|
848
|
+
b = unwrapOr(l.get(0), -1)
|
|
849
|
+
a * 100 + b
|
|
850
|
+
";
|
|
851
|
+
let source = parse(src);
|
|
852
|
+
let bytes = compile_source(&source).expect("compile failed");
|
|
853
|
+
assert_eq!(run_main(&bytes), -1);
|
|
854
|
+
}
|
|
855
|
+
|
|
856
|
+
#[test]
|
|
857
|
+
fn list_reverse_returns_new_reversed_list_and_leaves_original_unchanged() {
|
|
858
|
+
let src = "\
|
|
859
|
+
enum Option =
|
|
860
|
+
| Some(a)
|
|
861
|
+
| None
|
|
862
|
+
|
|
863
|
+
type Node(a) =
|
|
864
|
+
value: a
|
|
865
|
+
prev: Option[Node]
|
|
866
|
+
next: Option[Node]
|
|
867
|
+
|
|
868
|
+
type List(a) =
|
|
869
|
+
head: Option[Node]
|
|
870
|
+
tail: Option[Node]
|
|
871
|
+
size: Int
|
|
872
|
+
|
|
873
|
+
get<List>(self, i: Int) -> Option(a) =
|
|
874
|
+
current = self.head
|
|
875
|
+
index = 0
|
|
876
|
+
while current != None
|
|
877
|
+
match current
|
|
878
|
+
Some(node) =>
|
|
879
|
+
if index == i
|
|
880
|
+
return Some(node.value)
|
|
881
|
+
current = node.next
|
|
882
|
+
index = index + 1
|
|
883
|
+
None =>
|
|
884
|
+
break
|
|
885
|
+
None
|
|
886
|
+
|
|
887
|
+
length<List>(self) -> Int =
|
|
888
|
+
self.size
|
|
889
|
+
|
|
890
|
+
add<List>(self, values: ...a) =
|
|
891
|
+
for v in values
|
|
892
|
+
newNode = Node(value: v, prev: self.tail, next: None)
|
|
893
|
+
match self.tail
|
|
894
|
+
Some(oldTail) =>
|
|
895
|
+
oldTail.next = Some(newNode)
|
|
896
|
+
None =>
|
|
897
|
+
self.head = Some(newNode)
|
|
898
|
+
self.tail = Some(newNode)
|
|
899
|
+
self.size = self.size + 1
|
|
900
|
+
|
|
901
|
+
reverse<List>(self) -> List =
|
|
902
|
+
nl = List(head: None, tail: None, size: 0)
|
|
903
|
+
current = self.tail
|
|
904
|
+
while current != None
|
|
905
|
+
match current
|
|
906
|
+
Some(node) =>
|
|
907
|
+
nl.add(node.value)
|
|
908
|
+
current = node.prev
|
|
909
|
+
None =>
|
|
910
|
+
break
|
|
911
|
+
nl
|
|
912
|
+
|
|
913
|
+
unwrapOr(o: Option, default: Int) -> Int =
|
|
914
|
+
match o
|
|
915
|
+
Some(v) => v
|
|
916
|
+
None => default
|
|
917
|
+
|
|
918
|
+
main() -> Int =
|
|
919
|
+
l = List(head: None, tail: None, size: 0)
|
|
920
|
+
l.add(1, 2, 3)
|
|
921
|
+
r = l.reverse()
|
|
922
|
+
ra = unwrapOr(r.get(0), -1)
|
|
923
|
+
rb = unwrapOr(r.get(1), -1)
|
|
924
|
+
rc = unwrapOr(r.get(2), -1)
|
|
925
|
+
rlen = r.length()
|
|
926
|
+
oa = unwrapOr(l.get(0), -1)
|
|
927
|
+
olen = l.length()
|
|
928
|
+
ra * 100000 + rb * 10000 + rc * 1000 + rlen * 100 + oa * 10 + olen
|
|
929
|
+
";
|
|
930
|
+
let source = parse(src);
|
|
931
|
+
let bytes = compile_source(&source).expect("compile failed");
|
|
932
|
+
assert_eq!(run_main(&bytes), 321313);
|
|
933
|
+
}
|
|
934
|
+
```
|
|
935
|
+
|
|
936
|
+
- [ ] **Step 2: Run the tests to verify they fail**
|
|
937
|
+
|
|
938
|
+
Run: `cargo test -p plum-wasm-codegen list_clear list_reverse 2>&1 | tail -80`
|
|
939
|
+
Expected: FAIL to compile (tests don't exist yet).
|
|
940
|
+
|
|
941
|
+
- [ ] **Step 3: Apply the same implementations to `libs/std/list.plum`**
|
|
942
|
+
|
|
943
|
+
Replace:
|
|
944
|
+
|
|
945
|
+
```
|
|
946
|
+
# removes all objects from this list
|
|
947
|
+
clear<List>(self) =
|
|
948
|
+
todo
|
|
949
|
+
```
|
|
950
|
+
|
|
951
|
+
with:
|
|
952
|
+
|
|
953
|
+
```
|
|
954
|
+
# removes all objects from this list
|
|
955
|
+
clear<List>(self) =
|
|
956
|
+
self.head = None
|
|
957
|
+
self.tail = None
|
|
958
|
+
self.size = 0
|
|
959
|
+
```
|
|
960
|
+
|
|
961
|
+
Replace:
|
|
962
|
+
|
|
963
|
+
```
|
|
964
|
+
# returns a new list with the elements in reverse order.
|
|
965
|
+
reverse<List>(self, v: fn(a) -> Bool) -> List =
|
|
966
|
+
todo
|
|
967
|
+
```
|
|
968
|
+
|
|
969
|
+
with:
|
|
970
|
+
|
|
971
|
+
```
|
|
972
|
+
# returns a new list with the elements in reverse order.
|
|
973
|
+
reverse<List>(self) -> List =
|
|
974
|
+
nl = List(head: None, tail: None, size: 0)
|
|
975
|
+
current = self.tail
|
|
976
|
+
while current != None
|
|
977
|
+
match current
|
|
978
|
+
Some(node) =>
|
|
979
|
+
nl.add(node.value)
|
|
980
|
+
current = node.prev
|
|
981
|
+
None =>
|
|
982
|
+
break
|
|
983
|
+
nl
|
|
984
|
+
```
|
|
985
|
+
|
|
986
|
+
- [ ] **Step 4: Run the tests to verify they pass**
|
|
987
|
+
|
|
988
|
+
Run: `cargo test -p plum-wasm-codegen list_clear list_reverse 2>&1 | tail -80`
|
|
989
|
+
Expected: both PASS (`-1`, `321313`).
|
|
990
|
+
|
|
991
|
+
- [ ] **Step 5: Run the full workspace test suite**
|
|
992
|
+
|
|
993
|
+
Run: `cargo test --workspace 2>&1 | tail -100`
|
|
994
|
+
Expected: all tests PASS.
|
|
995
|
+
|
|
996
|
+
- [ ] **Step 6: Commit**
|
|
997
|
+
|
|
998
|
+
```bash
|
|
999
|
+
git add plum-wasm-codegen/tests/codegen_tests.rs libs/std/list.plum
|
|
1000
|
+
git commit -m "feat(libs/std): implement List.clear and List.reverse"
|
|
1001
|
+
```
|
|
1002
|
+
|
|
1003
|
+
---
|
|
1004
|
+
|
|
1005
|
+
### Task 5: `join<List>` (rewritten, no `Buffer`)
|
|
1006
|
+
|
|
1007
|
+
**Files:**
|
|
1008
|
+
- Test: `plum-wasm-codegen/tests/codegen_tests.rs`
|
|
1009
|
+
- Modify: `libs/std/list.plum` (`join`)
|
|
1010
|
+
|
|
1011
|
+
**Interfaces:**
|
|
1012
|
+
- Consumes: `add<List>` (Task 1); the existing `run_main_str` test helper (already defined in `plum-wasm-codegen/tests/codegen_tests.rs`, used elsewhere in this file to read a `Str`-returning `main`'s result).
|
|
1013
|
+
- Produces: `join<List>(self, sep: Str = ",") -> Str` — concatenates each element's `Str` interpolation with `sep` after it (including after the last element — this matches the ORIGINAL `Buffer`-based version's existing behavior of always appending a trailing separator, not a regression).
|
|
1014
|
+
|
|
1015
|
+
- [ ] **Step 1: Write the failing test**
|
|
1016
|
+
|
|
1017
|
+
Add to `plum-wasm-codegen/tests/codegen_tests.rs`:
|
|
1018
|
+
|
|
1019
|
+
```rust
|
|
1020
|
+
#[test]
|
|
1021
|
+
fn list_join_concatenates_elements_with_separator_runs_correctly() {
|
|
1022
|
+
let src = "\
|
|
1023
|
+
enum Option =
|
|
1024
|
+
| Some(a)
|
|
1025
|
+
| None
|
|
1026
|
+
|
|
1027
|
+
type Node(a) =
|
|
1028
|
+
value: a
|
|
1029
|
+
prev: Option[Node]
|
|
1030
|
+
next: Option[Node]
|
|
1031
|
+
|
|
1032
|
+
type List(a) =
|
|
1033
|
+
head: Option[Node]
|
|
1034
|
+
tail: Option[Node]
|
|
1035
|
+
size: Int
|
|
1036
|
+
|
|
1037
|
+
add<List>(self, values: ...a) =
|
|
1038
|
+
for v in values
|
|
1039
|
+
newNode = Node(value: v, prev: self.tail, next: None)
|
|
1040
|
+
match self.tail
|
|
1041
|
+
Some(oldTail) =>
|
|
1042
|
+
oldTail.next = Some(newNode)
|
|
1043
|
+
None =>
|
|
1044
|
+
self.head = Some(newNode)
|
|
1045
|
+
self.tail = Some(newNode)
|
|
1046
|
+
self.size = self.size + 1
|
|
1047
|
+
|
|
1048
|
+
join<List>(self, sep: Str) -> Str =
|
|
1049
|
+
result = \"\"
|
|
1050
|
+
current = self.head
|
|
1051
|
+
while current != None
|
|
1052
|
+
match current
|
|
1053
|
+
Some(node) =>
|
|
1054
|
+
result = \"{result}{node.value}{sep}\"
|
|
1055
|
+
current = node.next
|
|
1056
|
+
None =>
|
|
1057
|
+
break
|
|
1058
|
+
result
|
|
1059
|
+
|
|
1060
|
+
main() -> Str =
|
|
1061
|
+
l = List(head: None, tail: None, size: 0)
|
|
1062
|
+
l.add(1, 2, 3)
|
|
1063
|
+
l.join(\",\")
|
|
1064
|
+
";
|
|
1065
|
+
let source = parse(src);
|
|
1066
|
+
let bytes = compile_source(&source).expect("compile failed");
|
|
1067
|
+
assert_eq!(run_main_str(&bytes), "1,2,3,");
|
|
1068
|
+
}
|
|
1069
|
+
```
|
|
1070
|
+
|
|
1071
|
+
(Note: this test declares `join<List>(self, sep: Str) -> Str` — no default value on `sep` — and always passes the separator explicitly at the call site, per this plan's Global Constraints: default parameter values aren't consulted at call sites anywhere in the checker/codegen today, so a test relying on omitting `sep` would fail for that unrelated, out-of-scope reason. `libs/std/list.plum`'s own declaration keeps its existing `sep: Str = ","` default in the signature — the default annotation is harmless to leave in place since nothing reads it, it's just never *usable* at a call site yet, which is not new or introduced by this task.)
|
|
1072
|
+
|
|
1073
|
+
- [ ] **Step 2: Run the test to verify it fails**
|
|
1074
|
+
|
|
1075
|
+
Run: `cargo test -p plum-wasm-codegen list_join_concatenates 2>&1 | tail -60`
|
|
1076
|
+
Expected: FAIL to compile (test doesn't exist yet).
|
|
1077
|
+
|
|
1078
|
+
- [ ] **Step 3: Apply the same implementation to `libs/std/list.plum`**
|
|
1079
|
+
|
|
1080
|
+
Replace:
|
|
1081
|
+
|
|
1082
|
+
```
|
|
1083
|
+
join<List>(self, sep: Str = ",") -> Str =
|
|
1084
|
+
res = Buffer()
|
|
1085
|
+
self.each(|v|
|
|
1086
|
+
res.write(v.toStr())
|
|
1087
|
+
res.write(sep)
|
|
1088
|
+
)
|
|
1089
|
+
res.toStr()
|
|
1090
|
+
```
|
|
1091
|
+
|
|
1092
|
+
with:
|
|
1093
|
+
|
|
1094
|
+
```
|
|
1095
|
+
join<List>(self, sep: Str = ",") -> Str =
|
|
1096
|
+
result = ""
|
|
1097
|
+
current = self.head
|
|
1098
|
+
while current != None
|
|
1099
|
+
match current
|
|
1100
|
+
Some(node) =>
|
|
1101
|
+
result = "{result}{node.value}{sep}"
|
|
1102
|
+
current = node.next
|
|
1103
|
+
None =>
|
|
1104
|
+
break
|
|
1105
|
+
result
|
|
1106
|
+
```
|
|
1107
|
+
|
|
1108
|
+
- [ ] **Step 4: Run the test to verify it passes**
|
|
1109
|
+
|
|
1110
|
+
Run: `cargo test -p plum-wasm-codegen list_join_concatenates 2>&1 | tail -60`
|
|
1111
|
+
Expected: PASS (`"1,2,3,"`).
|
|
1112
|
+
|
|
1113
|
+
- [ ] **Step 5: Run the full workspace test suite**
|
|
1114
|
+
|
|
1115
|
+
Run: `cargo test --workspace 2>&1 | tail -100`
|
|
1116
|
+
Expected: all tests PASS.
|
|
1117
|
+
|
|
1118
|
+
- [ ] **Step 6: Commit**
|
|
1119
|
+
|
|
1120
|
+
```bash
|
|
1121
|
+
git add plum-wasm-codegen/tests/codegen_tests.rs libs/std/list.plum
|
|
1122
|
+
git commit -m "feat(libs/std): rewrite List.join using string interpolation instead of Buffer"
|
|
1123
|
+
```
|
|
1124
|
+
|
|
1125
|
+
---
|
|
1126
|
+
|
|
1127
|
+
### Task 6: README — close the gap
|
|
1128
|
+
|
|
1129
|
+
**Files:**
|
|
1130
|
+
- Modify: `README.md` (the "Known gaps" section)
|
|
1131
|
+
|
|
1132
|
+
**Interfaces:**
|
|
1133
|
+
- Consumes: nothing.
|
|
1134
|
+
- Produces: nothing (docs only).
|
|
1135
|
+
|
|
1136
|
+
- [ ] **Step 1: Update the Known gaps bullet**
|
|
1137
|
+
|
|
1138
|
+
Run: `grep -n "List" README.md` to find the current bullet, which reads along the lines of:
|
|
1139
|
+
|
|
1140
|
+
```
|
|
1141
|
+
- `libs/std`'s actual `List`/`Map` still don't fully compile — cross-file `import` resolution now works (`import <path>` resolves against `--lib-path`, defaulting to `./libs`), and variadic parameters work, but `List`'s methods beyond `get`/`length` (`add`, `set`, `removeAt`, `remove`, `clear`, `reverse`) are still `todo`; separately, `List`'s own `join` method (and `Map`) reference a `Buffer` type and trait-bounded dispatch (`Stringable`) that don't exist yet — `plum-checker` doesn't process trait declarations at all currently
|
|
1142
|
+
```
|
|
1143
|
+
|
|
1144
|
+
Replace it with:
|
|
1145
|
+
|
|
1146
|
+
```
|
|
1147
|
+
- `libs/std/map.plum`'s `Map` still doesn't fully compile — it references a `Buffer` type that doesn't exist and relies on trait-bounded dispatch (`Stringable`) that `plum-checker` doesn't process at all currently. `List` (`libs/std/list.plum`) is otherwise fully wired up: `get`, `length`, `each`, `map`, `first`, `last`, `add`, `set`, `removeAt`, `remove`, `clear`, `reverse`, and `join` all compile and run (`join` was rewritten to use string interpolation instead of `Buffer`). `List`'s remaining extras (`sort`, `find`, `flatMap`, `retain`, and similar) are still `todo` — a much longer tail that was never part of this gap's original scope.
|
|
1148
|
+
```
|
|
1149
|
+
|
|
1150
|
+
- [ ] **Step 2: Commit**
|
|
1151
|
+
|
|
1152
|
+
```bash
|
|
1153
|
+
git add README.md
|
|
1154
|
+
git commit -m "docs: List's core methods are wired up; Map remains blocked on Buffer/traits"
|
|
1155
|
+
```
|