2
;;;; Author: Paul Dietz
3
;;;; Created: Wed Oct 9 19:41:24 2002
4
;;;; Contains: Tests of MACROLET
10
(macrolet ((%m (x) `(car ,x)))
12
(values (%m y) (%m z)))))
17
(macrolet ((%m (x) `(car ,x)))
26
;;; Inner definitions shadow outer ones
28
(macrolet ((%m (w) `(cadr ,w)))
30
(macrolet ((%m (x) `(car ,x)))
42
(macrolet ((%m (&whole w arg)
43
`(progn (setq x (quote ,w))
48
;;; &whole parameter (nested, destructuring; see section 3.4.4)
51
(macrolet ((%m ((&whole w arg))
52
`(progn (setq x (quote ,w))
60
(macrolet ((%m (&key (a 'xxx) b)
61
`(setq x (quote ,a))))
67
;;; nested key parameters
70
(macrolet ((%m ((&key a b))
71
`(setq x (quote ,a))))
73
(values (%m (:a foo)) x
77
;;; nested key parameters
80
(macrolet ((%m ((&key (a 10) b))
81
`(setq x (quote ,a))))
83
(values (%m (:a foo)) x
87
;;; keyword parameter with supplied-p parameter
90
(macrolet ((%m (&key (a 'xxx a-p) b)
91
`(setq x (quote ,(list a (not (not a-p)))))))
95
(foo t) (foo t) (xxx nil) (xxx nil))
101
(macrolet ((%m (b &rest a)
102
`(setq x (quote ,a))))
103
(values (%m a1 a2) x)))
106
;;; rest parameter w. destructuring
109
(macrolet ((%m ((b &rest a))
110
`(setq x (quote ,a))))
111
(values (%m (a1 a2)) x)))
114
;;; rest parameter w. whole
117
(macrolet ((%m (&whole w b &rest a)
118
`(setq x (quote ,(list a w)))))
119
(values (%m a1 a2) x)))
123
;;; Interaction with symbol-macrolet
126
(symbol-macrolet ((a b))
127
(macrolet ((foo (x &environment env)
128
(let ((y (macroexpand x env)))
129
(if (eq y 'a) 1 2))))
134
(symbol-macrolet ((a b))
135
(macrolet ((foo (x &environment env)
136
(let ((y (macroexpand-1 x env)))
137
(if (eq y 'a) 1 2))))
142
(macrolet ((nil () ''a))
147
(loop for s in *cl-non-function-macro-special-operator-symbols*
148
for form = `(ignore-errors (macrolet ((,s () ''a)) (,s)))
149
unless (eq (eval form) 'a)
154
(macrolet ((%m (&key (a t)) `(quote ,a)))
159
(macrolet ((%m (&key (a t a-p)) `(quote (,a ,(notnot a-p)))))
164
(macrolet ((%m (x &optional y) `(quote (,x ,y))))
165
(values (%m 1) (%m 2 3)))
170
(macrolet ((%m (x &optional (y 'a)) `(quote (,x ,y))))
171
(values (%m 1) (%m 2 3)))
175
;;; Note -- the supplied-p parameter in a macrolet &optional
176
;;; is required to be T (not just true) if the parameter is present.
177
;;; See section 3.4.4.1.2
179
(macrolet ((%m (x &optional (y 'a y-p)) `(quote (,x ,y ,y-p))))
180
(values (%m 1) (%m 2 3)))
185
(macrolet ((%m (x &optional ((y z) '(2 3))) `(quote (,x ,y ,z))))
192
(deftest macrolet.22a
193
(macrolet ((%m (x &optional ((y z) '(2 3) y-z-p))
194
`(quote (,x ,y ,z ,y-z-p))))
202
(macrolet ((%m (&rest y) `(quote ,y)))
206
;;; According to 3.4.4.1.2, the entity following &rest is
207
;;; 'a destructuring pattern that matches the rest of the list.'
210
(macrolet ((%m (&rest (x y z)) `(quote (,x ,y ,z))))
215
(macrolet ((%m (&body (x y z)) `(quote (,x ,y ,z))))
219
;;; More key parameters
222
(macrolet ((%m (&key ((:a b))) `(quote ,b)))
229
(macrolet ((%m (&key ((:a (b c)))) `(quote (,c ,b))))
234
(macrolet ((%m (&key ((:a (b c)) '(3 4))) `(quote (,c ,b))))
235
(values (%m :a (1 2))
236
(%m :a (1 2) :a (10 11))
243
(macrolet ((%m (&key a (b a)) `(quote (,a ,b))))
251
(%m :a 10 :b nil :b 11)))
262
(macrolet ((%m ((&key a) &key (b a)) `(quote (,a ,b))))
269
(%m (:a 10) :b nil :b 11)))
279
(macrolet ((%m (&key ((:a (b c)) '(3 4) a-p))
280
`(quote (,(notnot a-p) ,c ,b))))
281
(values (%m :a (1 2))
282
(%m :a (1 2) :a (10 11))
288
;;; Allow-other-keys tests
291
(macrolet ((%m (&key a b c) `(quote (,a ,b ,c))))
293
(%m :allow-other-keys nil)
294
(%m :a 1 :allow-other-keys nil)
295
(%m :allow-other-keys t)
296
(%m :allow-other-keys t :allow-other-keys nil :foo t)
297
(%m :allow-other-keys t :c 1 :b 2 :a 3)
298
(%m :allow-other-keys nil :c 1 :b 2 :a 3)))
307
(macrolet ((%m (&key allow-other-keys) `(quote ,allow-other-keys)))
310
(%m :allow-other-keys nil)
311
(%m :allow-other-keys t :foo t)))
317
(macrolet ((%m (&key &allow-other-keys) :good))
321
(%m :allow-other-keys nil :foo t)))
327
(macrolet ((%m (&key a b &allow-other-keys) `(quote (,a ,b))))
331
(%m :allow-other-keys nil :a 1 :foo t :b 2)))
336
;;; &whole is followed by a destructuring pattern (see 3.4.4.1.2)
338
(macrolet ((%m (&whole (m a b) c d) `(quote (,m ,a ,b ,c ,d))))
342
;;; Macro names are shadowed by local functions
345
(macrolet ((%f () :bad))
346
(flet ((%f () :good))
350
;;; The &environment parameter is bound first
353
(macrolet ((foo () 1))
354
(macrolet ((%f (&optional (x (macroexpand '(foo) env)) &environment env)
359
;;; Test for bug that showed up in sbcl
362
(macrolet ((%m (()) :good)) (%m ()))
365
;;; Test that macrolets accept declarations
368
(macrolet ((%x () t))
369
(declare (optimize)))
373
(macrolet ((%x () t))
375
(declare (notinline identity)))
379
(macrolet ((%x () t))
384
;;; Symbol-macrolet tests
386
(deftest symbol-macrolet.1
387
(loop for s in *cl-non-variable-constant-symbols*
388
for form = `(ignore-errors (symbol-macrolet ((,s 17)) ,s))
389
unless (eql (eval form) 17)
393
(deftest symbol-macrolet.2
397
(deftest symbol-macrolet.3
398
(symbol-macrolet () (declare (optimize)))
401
(deftest symbol-macrolet.4
402
(symbol-macrolet ((x 1))
403
(symbol-macrolet ((x 2))
407
(deftest symbol-macrolet.5
409
(symbol-macrolet ((y x))
417
(10 10 20 10 50 10 10))
419
(deftest symbol-macrolet.6
420
(symbol-macrolet () (values)))
422
(deftest symbol-macrolet.7
423
(symbol-macrolet () (values 'a 'b 'c 'd 'e))
427
(deftest symbol-macrolet.error.1
429
(symbol-macrolet ((x 10))
430
(declare (special x))
435
(deftest symbol-macrolet.error.2
436
(signals-error (symbol-macrolet ((t 'a)) t)
440
(deftest symbol-macrolet.error.3
441
(signals-error (symbol-macrolet ((*pathnames* 19)) *pathnames*)
b'\\ No newline at end of file'