Files
rose-ash/lib/apl/tests/dfn.sx
giles 84d210b6b3
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 1m2s
apl: dfn foundation — transpile.sx + apl-eval-ast (+15 tests, 226/226)
2026-05-07 00:57:59 +00:00

108 lines
2.4 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
; Tests for apl-eval-ast and apl-call-dfn (manual AST construction).
(define rv (fn (arr) (get arr :ravel)))
(define sh (fn (arr) (get arr :shape)))
(define num (fn (n) (list :num n)))
(define name (fn (s) (list :name s)))
(define fnglyph (fn (g) (list :fn-glyph g)))
(define monad (fn (g a) (list :monad (fnglyph g) a)))
(define dyad (fn (g l r) (list :dyad (fnglyph g) l r)))
(define dfn (fn (body) (list :dfn body)))
(define prog (fn (stmts) (cons :program stmts)))
(apl-test
"eval :num literal"
(rv (apl-eval-ast (num 42) {}))
(list 42))
(apl-test
"eval :num literal shape"
(sh (apl-eval-ast (num 42) {}))
(list))
(apl-test
"eval :dyad +"
(rv (apl-eval-ast (dyad "+" (num 2) (num 3)) {}))
(list 5))
(apl-test
"eval :dyad ×"
(rv (apl-eval-ast (dyad "×" (num 6) (num 7)) {}))
(list 42))
(apl-test
"eval :monad - (negate)"
(rv (apl-eval-ast (monad "-" (num 7)) {}))
(list -7))
(apl-test
"eval :monad ⌊ (floor)"
(rv (apl-eval-ast (monad "⌊" (num 3)) {}))
(list 3))
(apl-test
"eval :name ⍵ from env"
(rv (apl-eval-ast (name "⍵") {:omega (apl-scalar 99) :alpha nil}))
(list 99))
(apl-test
"eval :name from env"
(rv (apl-eval-ast (name "") {:omega nil :alpha (apl-scalar 7)}))
(list 7))
(apl-test
"dfn {⍵+1} called monadic"
(rv
(apl-call-dfn-m (dfn (dyad "+" (name "⍵") (num 1))) (apl-scalar 5)))
(list 6))
(apl-test
"dfn {+⍵} called dyadic"
(rv
(apl-call-dfn
(dfn (dyad "+" (name "") (name "⍵")))
(apl-scalar 4)
(apl-scalar 9)))
(list 13))
(apl-test
"dfn {⍺×⍵} dyadic on vectors"
(rv
(apl-call-dfn
(dfn (dyad "×" (name "") (name "⍵")))
(make-array (list 3) (list 1 2 3))
(make-array (list 3) (list 10 20 30))))
(list 10 40 90))
(apl-test
"dfn {-⍵} monadic negate"
(rv
(apl-call-dfn-m
(dfn (monad "-" (name "⍵")))
(make-array (list 3) (list 1 2 3))))
(list -1 -2 -3))
(apl-test
"dfn {-⍵} dyadic subtract scalar"
(rv
(apl-call-dfn
(dfn (dyad "-" (name "") (name "⍵")))
(apl-scalar 10)
(apl-scalar 3)))
(list 7))
(apl-test
"dfn {⌈⍺,⍵} not used (just verify : missing) — ceiling of right"
(rv (apl-call-dfn-m (dfn (monad "⌈" (name "⍵"))) (apl-scalar 5)))
(list 5))
(apl-test
"dfn nested dyad"
(rv
(apl-call-dfn
(dfn (dyad "+" (name "") (dyad "×" (name "⍵") (num 2))))
(apl-scalar 1)
(apl-scalar 3)))
(list 7))