smalltalk: whileTrue:/whileFalse: family pinned + 14 tests
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Has been cancelled
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Has been cancelled
This commit is contained in:
@@ -446,27 +446,29 @@
|
||||
temps)
|
||||
(st-eval-seq body frame))))))))
|
||||
|
||||
;; whileTrue: / whileTrue / whileFalse: / whileFalse — the receiver is the
|
||||
;; condition block; the optional argument is the body block. Per ANSI / Pharo
|
||||
;; convention, the loop returns nil regardless of how many iterations ran.
|
||||
(define
|
||||
st-block-while
|
||||
(fn
|
||||
(cond-block body-block target)
|
||||
(let ((last nil))
|
||||
(begin
|
||||
(define
|
||||
wh-loop
|
||||
(fn
|
||||
()
|
||||
(let
|
||||
((c (st-block-apply cond-block (list))))
|
||||
(when
|
||||
(= c target)
|
||||
(begin
|
||||
(cond
|
||||
((not (= body-block nil))
|
||||
(set! last (st-block-apply body-block (list)))))
|
||||
(wh-loop))))))
|
||||
(wh-loop)
|
||||
last))))
|
||||
(begin
|
||||
(define
|
||||
wh-loop
|
||||
(fn
|
||||
()
|
||||
(let
|
||||
((c (st-block-apply cond-block (list))))
|
||||
(when
|
||||
(= c target)
|
||||
(begin
|
||||
(cond
|
||||
((not (= body-block nil))
|
||||
(st-block-apply body-block (list))))
|
||||
(wh-loop))))))
|
||||
(wh-loop)
|
||||
nil)))
|
||||
|
||||
;; ── Primitive method table for native receivers ────────────────────────
|
||||
;; Returns the result, or the sentinel :unhandled if no primitive matches —
|
||||
|
||||
145
lib/smalltalk/tests/while.sx
Normal file
145
lib/smalltalk/tests/while.sx
Normal file
@@ -0,0 +1,145 @@
|
||||
;; whileTrue: / whileTrue / whileFalse: / whileFalse tests.
|
||||
;;
|
||||
;; In Smalltalk these are *ordinary* messages sent to the condition block.
|
||||
;; No special-form magic — just block sends. The runtime can intrinsify
|
||||
;; them later in the JIT (Tier 1 of bytecode expansion) but the spec-level
|
||||
;; semantics are what's pinned here.
|
||||
|
||||
(set! st-test-pass 0)
|
||||
(set! st-test-fail 0)
|
||||
(set! st-test-fails (list))
|
||||
|
||||
(st-bootstrap-classes!)
|
||||
(define ev (fn (src) (smalltalk-eval src)))
|
||||
(define evp (fn (src) (smalltalk-eval-program src)))
|
||||
|
||||
;; ── 1. whileTrue: with body — basic counter ──
|
||||
(st-test
|
||||
"whileTrue: counts down"
|
||||
(evp "| n | n := 5. [n > 0] whileTrue: [n := n - 1]. ^ n")
|
||||
0)
|
||||
|
||||
(st-test
|
||||
"whileTrue: returns nil"
|
||||
(evp "| n | n := 3. ^ [n > 0] whileTrue: [n := n - 1]")
|
||||
nil)
|
||||
|
||||
(st-test
|
||||
"whileTrue: zero iterations is fine"
|
||||
(evp "| n | n := 0. [n > 0] whileTrue: [n := n + 1]. ^ n")
|
||||
0)
|
||||
|
||||
;; ── 2. whileFalse: with body ──
|
||||
(st-test
|
||||
"whileFalse: counts down (cond becomes true)"
|
||||
(evp "| n | n := 5. [n <= 0] whileFalse: [n := n - 1]. ^ n")
|
||||
0)
|
||||
|
||||
(st-test
|
||||
"whileFalse: returns nil"
|
||||
(evp "| n | n := 3. ^ [n <= 0] whileFalse: [n := n - 1]")
|
||||
nil)
|
||||
|
||||
;; ── 3. whileTrue (no arg) — body-less side-effect loop ──
|
||||
(st-test
|
||||
"whileTrue without argument runs cond-only loop"
|
||||
(evp
|
||||
"| n decrement |
|
||||
n := 5.
|
||||
decrement := [n := n - 1. n > 0].
|
||||
decrement whileTrue.
|
||||
^ n")
|
||||
0)
|
||||
|
||||
;; ── 4. whileFalse (no arg) ──
|
||||
(st-test
|
||||
"whileFalse without argument"
|
||||
(evp
|
||||
"| n inc |
|
||||
n := 0.
|
||||
inc := [n := n + 1. n >= 3].
|
||||
inc whileFalse.
|
||||
^ n")
|
||||
3)
|
||||
|
||||
;; ── 5. Cond block evaluated each iteration (not cached) ──
|
||||
(st-test
|
||||
"whileTrue: re-evaluates cond on every iter"
|
||||
(evp
|
||||
"| n stop |
|
||||
n := 0. stop := false.
|
||||
[stop] whileFalse: [
|
||||
n := n + 1.
|
||||
n >= 4 ifTrue: [stop := true]].
|
||||
^ n")
|
||||
4)
|
||||
|
||||
;; ── 6. Body block sees outer locals ──
|
||||
(st-test
|
||||
"whileTrue: body reads + writes captured locals"
|
||||
(evp
|
||||
"| acc i |
|
||||
acc := 0. i := 1.
|
||||
[i <= 10] whileTrue: [acc := acc + i. i := i + 1].
|
||||
^ acc")
|
||||
55)
|
||||
|
||||
;; ── 7. Nested while loops ──
|
||||
(st-test
|
||||
"nested whileTrue: produces flat sum"
|
||||
(evp
|
||||
"| total i j |
|
||||
total := 0. i := 0.
|
||||
[i < 3] whileTrue: [
|
||||
j := 0.
|
||||
[j < 4] whileTrue: [total := total + 1. j := j + 1].
|
||||
i := i + 1].
|
||||
^ total")
|
||||
12)
|
||||
|
||||
;; ── 8. ^ inside whileTrue: short-circuits the surrounding method ──
|
||||
(st-class-define! "WhileEscape" "Object" (list))
|
||||
(st-class-add-method! "WhileEscape" "firstOver:in:"
|
||||
(st-parse-method
|
||||
"firstOver: limit in: arr
|
||||
| i |
|
||||
i := 1.
|
||||
[i <= arr size] whileTrue: [
|
||||
(arr at: i) > limit ifTrue: [^ arr at: i].
|
||||
i := i + 1].
|
||||
^ nil"))
|
||||
|
||||
(st-test
|
||||
"early ^ from whileTrue: body"
|
||||
(evp "^ WhileEscape new firstOver: 5 in: #(1 3 5 7 9)")
|
||||
7)
|
||||
|
||||
(st-test
|
||||
"whileTrue: completes when nothing matches"
|
||||
(evp "^ WhileEscape new firstOver: 100 in: #(1 2 3)")
|
||||
nil)
|
||||
|
||||
;; ── 9. whileTrue: invocations independent across calls ──
|
||||
(st-class-define! "Counter2" "Object" (list "n"))
|
||||
(st-class-add-method! "Counter2" "init"
|
||||
(st-parse-method "init n := 0. ^ self"))
|
||||
(st-class-add-method! "Counter2" "n"
|
||||
(st-parse-method "n ^ n"))
|
||||
(st-class-add-method! "Counter2" "tick:"
|
||||
(st-parse-method "tick: count [count > 0] whileTrue: [n := n + 1. count := count - 1]. ^ self"))
|
||||
|
||||
(st-test
|
||||
"instance state survives whileTrue: invocations"
|
||||
(evp
|
||||
"| c | c := Counter2 new init.
|
||||
c tick: 3. c tick: 4.
|
||||
^ c n")
|
||||
7)
|
||||
|
||||
;; ── 10. Timing: whileTrue: on a never-true cond runs zero times ──
|
||||
(st-test
|
||||
"whileTrue: with always-false cond"
|
||||
(evp "| ran | ran := false. [false] whileTrue: [ran := true]. ^ ran")
|
||||
false)
|
||||
|
||||
(list st-test-pass st-test-fail)
|
||||
@@ -67,7 +67,7 @@ Core mapping:
|
||||
- [x] Method invocation captures a `^k` (the return continuation) and binds it as the block's escape. `st-invoke` wraps body in `(call/cc (fn (k) ...))`; the frame's `:return-k` is set to k. Block creation copies `(get frame :return-k)` onto the block. Block invocation sets the new frame's `:return-k` to the block's saved one — so non-local return reaches *back through* any number of intermediate block invocations.
|
||||
- [x] `^expr` from inside a block invokes that captured `^k`. The "return" AST type evaluates the expression then calls `(k v)` on the frame's :return-k. Verified: `detect:in:` style early-exit, multi-level nested blocks, ^ from inside `to:do:`/`whileTrue:`, ^ from a block passed to a *different* method (Caller→Helper) returns from Caller.
|
||||
- [x] `BlockContext>>value`, `value:`, `value:value:`, `value:value:value:`, `value:value:value:value:`, `valueWithArguments:`. Implemented in `st-block-dispatch` + `st-block-apply` (eval iteration); pinned by 19 dedicated tests in `lib/smalltalk/tests/blocks.sx` covering arity through 4, valueWithArguments: with empty/non-empty arg arrays, closures over outer locals (read + mutate + later-mutation re-read), nested blocks, blocks as method arguments, `numArgs`, and `class`.
|
||||
- [ ] `whileTrue:` / `whileTrue` / `whileFalse:` / `whileFalse` as ordinary block sends — runtime intrinsifies the loop in the bytecode JIT
|
||||
- [x] `whileTrue:` / `whileTrue` / `whileFalse:` / `whileFalse` as ordinary block sends. `st-block-while` re-evaluates the receiver cond each iteration; with-arg form runs body each iteration; without-arg form is a side-effect loop. Now returns `nil` per ANSI/Pharo. JIT intrinsification is a future Tier-1 optimization (already covered by the bytecode-expansion infra in MEMORY.md). 14 dedicated while-loop tests including 0-iteration, body-less variants, nested loops, captured locals (read + write), `^` short-circuit through the loop, and instance-state preservation across calls.
|
||||
- [ ] `ifTrue:` / `ifFalse:` / `ifTrue:ifFalse:` as block sends, similarly intrinsified
|
||||
- [ ] Escape past returned-from method raises `BlockContext>>cannotReturn:`
|
||||
- [ ] Classic programs in `lib/smalltalk/tests/programs/`:
|
||||
@@ -108,6 +108,7 @@ Core mapping:
|
||||
|
||||
_Newest first. Agent appends on every commit._
|
||||
|
||||
- 2026-04-25: `whileTrue:` / `whileFalse:` / no-arg variants pinned (`lib/smalltalk/tests/while.sx`, 14 tests). `st-block-while` returns nil per ANSI; behaviour verified under captured locals, nesting, early `^`, and zero/many iterations. 334/334 total.
|
||||
- 2026-04-25: BlockContext value family pinned (`lib/smalltalk/tests/blocks.sx`, 19 tests). Each value/valueN/valueWithArguments: variant verified plus closure semantics (read, write, later-mutation re-read), nested blocks, and block-as-arg. 320/320 total.
|
||||
- 2026-04-25: **THE SHOWCASE** — non-local return via captured method-return continuations + 14 NLR tests (`lib/smalltalk/tests/nlr.sx`). `st-invoke` wraps body in `call/cc`; blocks copy creating method's `^k`; `^expr` invokes that k. Verified across nested blocks, `to:do:` / `whileTrue:`, blocks passed to different methods (Caller→Helper escapes back to Caller), inner-vs-outer method nesting. Sentinel-based return removed. 301/301 total.
|
||||
- 2026-04-25: `super` send + 9 tests (`lib/smalltalk/tests/super.sx`). `st-super-send` walks from defining-class's superclass; class-side aware; primitives → DNU fallback. Also fixed top-level `| temps |` parsing in `st-parse` (the absence of which was silently aborting earlier eval/dnu tests — counts go from 274 → 287, with previously-skipped tests now actually running).
|
||||
|
||||
Reference in New Issue
Block a user