works albeit comically inefficiently

This commit is contained in:
2026-08-30 02:12:16 -06:00
parent e7c0ae9161
commit a09c00badd
8 changed files with 185 additions and 129 deletions
+6 -8
View File
@@ -76,8 +76,7 @@ test_root = testGroup "stack machine"
(tail-call 1))
(define $square
(pop! %x)
(prim %x2 (* %x %x))
(push! %x2)
(prim (* %x %x))
(return 1))
|]
, testGroup "factorial"
@@ -90,20 +89,19 @@ test_root = testGroup "stack machine"
(tail-call 1))
(define $fac
(load %n 0)
(prim %x0 (zero? %n))
(prim (zero? %n))
(pop! %x0)
(if %x0
(then (push! 1)
(return 1))
(else (prim %x1 (- %n 1))
(push! $fac-c0)
(else (push! $fac-c0)
(push! $fac)
(push! %x1)
(prim (- %n 1))
(call 1))))
(define $fac-c0
(pop! %x2)
(pop! %n)
(prim %x3 (* %n %x2))
(push! %x3)
(prim (* %n %x2))
(return 1))
|]
mkcase n = testCase [i|#{n}|] do