From 481884fb892f949478dad8d801ced704baea986c Mon Sep 17 00:00:00 2001 From: tomsmeding Date: Mon, 18 Nov 2019 18:36:57 +0100 Subject: Automatic testing of examples --- tests/fibo.lisp | 156 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 156 insertions(+) create mode 100644 tests/fibo.lisp (limited to 'tests/fibo.lisp') diff --git a/tests/fibo.lisp b/tests/fibo.lisp new file mode 100644 index 0000000..3beae63 --- /dev/null +++ b/tests/fibo.lisp @@ -0,0 +1,156 @@ +(define fibo1 (n) + (if (<= n 0) 0 + (if (<= n 2) 1 + (+ (fibo1 (- n 1)) (fibo1 (- n 2)))))) + +; == main lambda == +; Params: 1 +; B0: +; t0 <- param 0 +; t1 <- callf "<=" [t0 0] +; br t1 B1 B2 +; B1: +; t2 <- assign 0 +; jmp B5 +; B2: +; t3 <- param 0 +; t4 <- callf "<=" [t3 2] +; br t4 B3 B4 +; B3: +; t2 <- assign 1 +; jmp B5 +; B4: +; t5 <- param 0 +; t6 <- callf "-" [t5 1] +; t7 <- callf "fibo1" [t6] ; patched up (statically known address, afterwards) +; t8 <- param 0 +; t9 <- callf "-" [t8 2] +; t10 <- callf "fibo1" [t9] ; patched up +; t2 <- callf "+" [t7 t10] +; jmp B5 +; B5: +; return t2 + + +(define fibo2help (a b n) + (if (<= n 0) b + (fibo2help b (+ a b) (- n 1)))) + +(define fibo2 (n) + (if (<= n 0) 0 + (if (<= n 2) 1 + (fibo2help 1 1 (- n 2))))) + + +(define fibo3 (n) + ((lambda (helper) + (if (<= n 0) 0 + (if (<= n 2) 1 + (helper helper 1 1 (- n 2))))) + (lambda (recur a b n) + (if (<= n 0) b + (recur recur b (+ a b) (- n 1)))))) + +; == L1 == +; Params: 1 +; Closure slots: 1 +; B0: +; t0 <- closure 0 +; t1 <- callf "<=" [t0 0] +; br t1 B1 B2 +; B1: +; t2 <- assign 0 +; jmp B5 +; B2: +; t3 <- closure 0 +; t4 <- callf "<=" [t3 2] +; br t4 B3 B4 +; B3: +; t2 <- assign 1 +; jmp B5 +; B4: +; t5 <- param 0 +; t6 <- param 0 +; t7 <- closure 0 +; t8 <- callf "-" [t7 2] +; t2 <- callc t5 [t6 1 1 t8] +; jmp B5 +; B5: +; return t2 +; +; == L2 == +; Params: 4 +; B0: +; t0 <- param 3 +; t1 <- callf "<=" [t0 0] +; br t1 B1 B2 +; B1: +; t2 <- param 2 +; jmp B3 +; B2: +; t3 <- param 0 +; t4 <- param 0 +; t5 <- param 2 +; t6 <- param 1 +; t7 <- param 2 +; t8 <- callf "+" [t6 t7] +; t9 <- param 3 +; t10 <- callf "-" [t9 1] +; t2 <- callc t3 [t4 t5 t8 t10] +; jmp B3 +; B3: +; return t2 +; +; == main lambda == +; Params: 1 +; B0: +; t0 <- param 0 +; t1 <- alloc-closure L1 [t0] +; t2 <- callc t1 [C(L2)] +; free-closure t1 +; return t2 + + +(define for (start end f) + (if (<= start end) + (do (f start) (for (+ start 1) end f)) + '())) + +; == main lambda == +; Params: 3 +; Data table: +; 0: () +; B0: +; t0 <- param 0 +; t1 <- param 1 +; t2 <- callf "<=" [t0 t1] +; br t2 B1 B2 +; B1: +; t3 <- param 2 +; t4 <- param 0 +; _ <- callc t3 [t4] +; t5 <- param 0 +; t6 <- callf "+" [t5 1] +; t7 <- param 1 +; t8 <- param 2 +; t9 <- callf "for" [t6 t7 t8] ; patched up +; jmp B3 +; B2: +; t9 <- data 0 +; jmp B3 +; B3: +; return t9 + +(for 1 25 (lambda (n) (print (fibo3 n)))) + +; == L1 == +; Params: 1 +; B0: +; t0 <- param 0 +; t1 <- callf "fibo3" [t0] +; t2 <- callf "print" [t1] +; return t2 +; +; == global code == +; B0: +; _ <- callf "for" [1 25 C(L1)] -- cgit v1.2.3-54-g00ecf