diff --git a/lib/smalltalk/parser.sx b/lib/smalltalk/parser.sx index c4a32c39..5938e46d 100644 --- a/lib/smalltalk/parser.sx +++ b/lib/smalltalk/parser.sx @@ -668,6 +668,10 @@ (else receiver))))) ;; Binary message: (binop )* + ;; A bare `|` is also a legitimate binary selector (logical or in + ;; some Smalltalks); the tokenizer emits it as the `bar` type so + ;; that block-param / temp-decl delimiters are easy to spot. + ;; In expression position, accept it as a binary operator. (define parse-binary-message (fn @@ -680,7 +684,7 @@ (fn () (when - (at-type? "binary") + (or (at-type? "binary") (at-type? "bar")) (let ((t (peek-tok))) (begin (advance-tok!) diff --git a/lib/smalltalk/tests/conditional.sx b/lib/smalltalk/tests/conditional.sx new file mode 100644 index 00000000..ad91c4ea --- /dev/null +++ b/lib/smalltalk/tests/conditional.sx @@ -0,0 +1,104 @@ +;; ifTrue: / ifFalse: / ifTrue:ifFalse: / ifFalse:ifTrue: tests. +;; +;; In Smalltalk these are *block sends* on Boolean. The runtime can +;; intrinsify the dispatch in the JIT (already provided by the bytecode +;; expansion infrastructure) but the spec semantics are: True/False +;; receive these messages and pick which branch block to evaluate. + +(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. ifTrue: ── +(st-test "true ifTrue: → block value" (ev "true ifTrue: [42]") 42) +(st-test "false ifTrue: → nil" (ev "false ifTrue: [42]") nil) + +;; ── 2. ifFalse: ── +(st-test "true ifFalse: → nil" (ev "true ifFalse: [42]") nil) +(st-test "false ifFalse: → block value" (ev "false ifFalse: [42]") 42) + +;; ── 3. ifTrue:ifFalse: ── +(st-test "true ifTrue:ifFalse:" (ev "true ifTrue: [1] ifFalse: [2]") 1) +(st-test "false ifTrue:ifFalse:" (ev "false ifTrue: [1] ifFalse: [2]") 2) + +;; ── 4. ifFalse:ifTrue: (reversed-order keyword) ── +(st-test "true ifFalse:ifTrue:" (ev "true ifFalse: [1] ifTrue: [2]") 2) +(st-test "false ifFalse:ifTrue:" (ev "false ifFalse: [1] ifTrue: [2]") 1) + +;; ── 5. The non-taken branch is NOT evaluated (laziness) ── +(st-test + "ifTrue: doesn't evaluate the false branch" + (evp + "| ran | + ran := false. + true ifTrue: [99] ifFalse: [ran := true. 0]. + ^ ran") + false) +(st-test + "ifFalse: doesn't evaluate the true branch" + (evp + "| ran | + ran := false. + false ifTrue: [ran := true. 99] ifFalse: [0]. + ^ ran") + false) + +;; ── 6. Branch result type can be anything ── +(st-test "branch returns string" (ev "true ifTrue: ['yes'] ifFalse: ['no']") "yes") +(st-test "branch returns nil" (ev "true ifTrue: [nil] ifFalse: [99]") nil) +(st-test "branch returns array" (ev "false ifTrue: [#(1)] ifFalse: [#(2 3)]") (list 2 3)) + +;; ── 7. Nested if ── +(st-test + "nested ifTrue:ifFalse:" + (evp + "| x | + x := 5. + ^ x > 0 + ifTrue: [x > 10 + ifTrue: [#big] + ifFalse: [#smallPositive]] + ifFalse: [#nonPositive]") + (make-symbol "smallPositive")) + +;; ── 8. Branch reads outer locals (closure semantics) ── +(st-test + "branch closes over outer bindings" + (evp + "| label x | + x := 7. + label := x > 0 + ifTrue: [#positive] + ifFalse: [#nonPositive]. + ^ label") + (make-symbol "positive")) + +;; ── 9. and: / or: short-circuit ── +(st-test "and: short-circuits when receiver false" + (ev "false and: [1/0]") false) +(st-test "and: with true receiver runs second" (ev "true and: [42]") 42) +(st-test "or: short-circuits when receiver true" + (ev "true or: [1/0]") true) +(st-test "or: with false receiver runs second" (ev "false or: [99]") 99) + +;; ── 10. & and | are eager (not blocks) ── +(st-test "& on booleans" (ev "true & true") true) +(st-test "| on booleans" (ev "false | true") true) + +;; ── 11. Boolean negation ── +(st-test "not on true" (ev "true not") false) +(st-test "not on false" (ev "false not") true) + +;; ── 12. Real-world idiom: max via ifTrue:ifFalse: in a method ── +(st-class-define! "Mathy" "Object" (list)) +(st-class-add-method! "Mathy" "myMax:and:" + (st-parse-method "myMax: a and: b ^ a > b ifTrue: [a] ifFalse: [b]")) + +(st-test "method using ifTrue:ifFalse: returns max" (evp "^ Mathy new myMax: 3 and: 7") 7) +(st-test "method using ifTrue:ifFalse: returns max sym" (evp "^ Mathy new myMax: 9 and: 4") 9) + +(list st-test-pass st-test-fail) diff --git a/plans/smalltalk-on-sx.md b/plans/smalltalk-on-sx.md index 06a3a16e..982db049 100644 --- a/plans/smalltalk-on-sx.md +++ b/plans/smalltalk-on-sx.md @@ -68,7 +68,7 @@ Core mapping: - [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`. - [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 +- [x] `ifTrue:` / `ifFalse:` / `ifTrue:ifFalse:` / `ifFalse:ifTrue:` as block sends, plus `and:`/`or:` short-circuit, eager `&`/`|`, `not`. Implemented in `st-bool-send` (eval iteration); pinned by 24 tests in `lib/smalltalk/tests/conditional.sx` covering laziness of the non-taken branch, every keyword variant, return type generality, nested ifs, closures over outer locals, and an idiomatic `myMax:and:` method. Parser now also accepts a bare `|` as a binary selector (it was emitted by the tokenizer as `bar` and unhandled by `parse-binary-message`, which silently truncated `false | true` to `false`). - [ ] Escape past returned-from method raises `BlockContext>>cannotReturn:` - [ ] Classic programs in `lib/smalltalk/tests/programs/`: - [ ] `eight-queens.st` @@ -108,6 +108,7 @@ Core mapping: _Newest first. Agent appends on every commit._ +- 2026-04-25: `ifTrue:` / `ifFalse:` family pinned (`lib/smalltalk/tests/conditional.sx`, 24 tests) + parser fix: `|` is now accepted as a binary selector in expression position (tokenizer still emits it as `bar` for block param/temp delimiting; `parse-binary-message` accepts both). Caught by `false | true` truncating silently to `false`. 359/359 total. - 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.