Commit f659790
feat(vm): Result constructors from surface syntax + closures (#93)
## What
The next two VM-completion follow-ups from #92's roadmap: **Result
constructors from surface syntax** and **closures**. Two commits.
### 1. Result constructors + built-ins from surface syntax (`da10ac5`)
`Okay(x)`/`Oops(x)` parse as ordinary **calls**, so they were
unreachable on the VM (`UndefinedFunction`). The compiler's `Expr::Call`
now recognises them — and a few stdlib-style ops the VM already has
opcodes for — before the user-function lookup, mirroring the
interpreter's inline built-in dispatch:
`Okay`→`MakeOkay`, `Oops`→`MakeOops`, `isOkay`→`IsOkay`,
`isOops`→`IsOkay;Not`, `unwrap`→`TryUnwrap`, `len`→`Len`,
`toString`→`ToString`, `print`/`printInline`→`Print`. Also fixed
`OpCode::Print` to use `Display` (not `Debug`), matching the
interpreter.
### 2. Non-capturing closures (`5d958df`)
The compiler emitted `MakeClosure`/`Call` for lambdas, but the machine
never implemented `MakeClosure` and `CallExpr` used a broken
`Call(args.len())` (`Call` expects a *function index*, not an arg
count). Now immediately-invoked lambdas run:
- `Value` gains a `VmClosure(func_idx)` variant (non-capturing — uses
params + globals, not enclosing locals); the 5 exhaustive `Value`
matches updated (`is_truthy`, `Display`, `value_to_string`, repl, json).
- New `OpCode::CallValue(argc)`; `MakeClosure` pushes a `VmClosure`;
`CallValue` removes the closure from below its args and enters its frame
(args become locals `0..argc`). The opcode match is now exhaustive — the
`Unimplemented opcode` catch-all is gone.
- `CallExpr` emits `CallValue` instead of the buggy `Call`.
Closures are invoked via `CallExpr` (e.g. `(|x| x*2)(21)`); the parser
lowers `identifier(args)` to a named `Call` (top-level functions only),
so this is **at parity with the interpreter**, which also calls closures
only via `CallExpr`.
## Verification
- **Full lib suite green: 186 passed, 0 failed** (+5 new VM tests).
- New tests: `Okay`/`Oops`/`isOkay`/`isOops`/`unwrap`/`len`/`toString`
from source; `(|x| x*2)(21)` and `(|a,b| a+b)(15,27)`.
- `rustfmt` + `clippy` (CI mode) clean.
## Deferred follow-ups
- **Upvalue capture** for closures (enclosing locals) — needs a capture
mechanism + upvalues on `VmClosure`.
- Consent grant sources (`superpower` decls), worker statements, `.wbc`
serialization, interpreter↔VM parity harness (from #92).
🤖 Generated with [Claude Code](https://claude.com/claude-code)
https://claude.ai/code/session_013wg3Mtq2QFhYi4XVw1Z6z7
---
_Generated by [Claude
Code](https://claude.ai/code/session_013wg3Mtq2QFhYi4XVw1Z6z7)_
---------
Co-authored-by: Claude <noreply@anthropic.com>1 parent 6a050bd commit f659790
8 files changed
Lines changed: 143 additions & 9 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1056 | 1056 | | |
1057 | 1057 | | |
1058 | 1058 | | |
| 1059 | + | |
1059 | 1060 | | |
1060 | 1061 | | |
1061 | 1062 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
223 | 223 | | |
224 | 224 | | |
225 | 225 | | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
226 | 229 | | |
227 | 230 | | |
228 | 231 | | |
| |||
240 | 243 | | |
241 | 244 | | |
242 | 245 | | |
| 246 | + | |
243 | 247 | | |
244 | 248 | | |
245 | 249 | | |
| |||
304 | 308 | | |
305 | 309 | | |
306 | 310 | | |
| 311 | + | |
307 | 312 | | |
308 | 313 | | |
309 | 314 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
85 | 85 | | |
86 | 86 | | |
87 | 87 | | |
| 88 | + | |
88 | 89 | | |
89 | 90 | | |
90 | 91 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
147 | 147 | | |
148 | 148 | | |
149 | 149 | | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
150 | 153 | | |
151 | 154 | | |
152 | 155 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
69 | 69 | | |
70 | 70 | | |
71 | 71 | | |
72 | | - | |
| 72 | + | |
73 | 73 | | |
| 74 | + | |
| 75 | + | |
74 | 76 | | |
75 | 77 | | |
76 | 78 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
570 | 570 | | |
571 | 571 | | |
572 | 572 | | |
| 573 | + | |
| 574 | + | |
| 575 | + | |
| 576 | + | |
| 577 | + | |
| 578 | + | |
| 579 | + | |
| 580 | + | |
| 581 | + | |
| 582 | + | |
| 583 | + | |
| 584 | + | |
| 585 | + | |
| 586 | + | |
| 587 | + | |
| 588 | + | |
| 589 | + | |
| 590 | + | |
| 591 | + | |
| 592 | + | |
| 593 | + | |
| 594 | + | |
| 595 | + | |
| 596 | + | |
| 597 | + | |
| 598 | + | |
| 599 | + | |
| 600 | + | |
| 601 | + | |
573 | 602 | | |
574 | 603 | | |
575 | 604 | | |
| |||
642 | 671 | | |
643 | 672 | | |
644 | 673 | | |
645 | | - | |
646 | | - | |
| 674 | + | |
| 675 | + | |
647 | 676 | | |
648 | 677 | | |
649 | 678 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
333 | 333 | | |
334 | 334 | | |
335 | 335 | | |
| 336 | + | |
336 | 337 | | |
337 | | - | |
| 338 | + | |
338 | 339 | | |
339 | 340 | | |
340 | 341 | | |
| |||
468 | 469 | | |
469 | 470 | | |
470 | 471 | | |
471 | | - | |
472 | | - | |
473 | | - | |
474 | | - | |
475 | | - | |
| 472 | + | |
| 473 | + | |
| 474 | + | |
| 475 | + | |
| 476 | + | |
| 477 | + | |
| 478 | + | |
| 479 | + | |
| 480 | + | |
| 481 | + | |
| 482 | + | |
| 483 | + | |
| 484 | + | |
| 485 | + | |
| 486 | + | |
| 487 | + | |
| 488 | + | |
| 489 | + | |
| 490 | + | |
| 491 | + | |
| 492 | + | |
| 493 | + | |
| 494 | + | |
| 495 | + | |
| 496 | + | |
| 497 | + | |
| 498 | + | |
| 499 | + | |
| 500 | + | |
| 501 | + | |
| 502 | + | |
| 503 | + | |
476 | 504 | | |
477 | 505 | | |
478 | 506 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
246 | 246 | | |
247 | 247 | | |
248 | 248 | | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
249 | 314 | | |
0 commit comments