Fix unreachable code warning due to 83e5661ae59addac315e6754013b3887b477570f
[sbcl.git] / tests / define-compiler-macro.impure.lisp
blobd0b61a3df3230008cb352a876535efdb9125bed9
1 ;;;; Compiler-macro tests
3 ;;; taken from CLHS example
4 (defun square (x)
5 (expt x 2))
7 (define-compiler-macro square (&whole form arg)
8 (if (atom arg)
9 `(expt ,arg 2)
10 (case (car arg)
11 (square (if (= (length arg) 2)
12 `(expt ,(nth 1 arg) 4)
13 form))
14 (expt (if (= (length arg) 3)
15 (if (numberp (nth 2 arg))
16 `(expt ,(nth 1 arg) ,(* 2 (nth 2 arg)))
17 `(expt ,(nth 1 arg) (* 2 ,(nth 2 arg))))
18 form))
19 (otherwise `(expt ,arg 2)))))
21 (assert (eql 81 (square (square 3))))
23 (multiple-value-bind (expansion expanded-p) (macroexpand '(square x))
24 (assert (equal '(square x) expansion))
25 (assert (not expanded-p)))
27 (assert (equal '(expt x 2)
28 (funcall (compiler-macro-function 'square)
29 '(square x)
30 nil)))
32 (assert (equal '(expt x 4)
33 (funcall (compiler-macro-function 'square)
34 '(square (square x))
35 nil)))
37 (assert (equal '(expt x 2)
38 (funcall (compiler-macro-function 'square)
39 '(funcall #'square x)
40 nil)))