apl: inline assignment a ← rhs mid-expression (+5 tests)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 1m2s
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 1m2s
Parser: :name clause now detects 'name ← rhs' patterns inside expressions. When seen, consumes the remaining tokens as RHS, parses recursively, and emits a (:assign-expr name parsed-rhs) value segment. Eval-ast :dyad and :monad: when the right operand is an :assign-expr node, capture the binding into env before evaluating the left operand. This realises the primes idiom: apl-run "(2 = +⌿ 0 = a ∘.| a) / a ← ⍳ 30" → 2 3 5 7 11 13 17 19 23 29 Also: top-level x←5 now evaluates to scalar 5 (apl-eval-ast :assign just unwraps to its RHS value). Caveat: ⍵-rebinding (the original primes.apl uses '⍵←⍳⍵') is a :glyph-token; only :name-tokens are handled. A regular variable name (like 'a') works.
This commit is contained in:
@@ -270,6 +270,15 @@
|
||||
(collect-segments-loop tokens (+ i 1) (append acc {:kind "val" :node (list :str tv)})))
|
||||
((= tt :name)
|
||||
(cond
|
||||
((and (< (+ i 1) (len tokens)) (= (tok-type (nth tokens (+ i 1))) :assign))
|
||||
(let
|
||||
((rhs-tokens (slice tokens (+ i 2) (len tokens))))
|
||||
(let
|
||||
((rhs-expr (parse-apl-expr rhs-tokens)))
|
||||
(collect-segments-loop
|
||||
tokens
|
||||
(len tokens)
|
||||
(append acc {:kind "val" :node (list :assign-expr tv rhs-expr)})))))
|
||||
((some (fn (q) (= q tv)) apl-quad-fn-names)
|
||||
(let
|
||||
((op-result (collect-ops tokens (+ i 1))))
|
||||
|
||||
Reference in New Issue
Block a user