Debian Codegraph @

test/macro.scm

raw
1 ;;
2 ;; testing macro expansion
3 ;;
4
5 (use gauche.test)
6
7 (test-start "macro")
8
9 ;; strip off syntactic information from identifiers in the macro output.
10 (define (unident form)
11 (cond
12 ((identifier? form) (identifier->symbol form))
13 ((pair? form) (cons (unident (car form)) (unident (cdr form))))
14 ((vector? form)
15 (list->vector (map unident (vector->list form))))
16 (else form)))
17
18 (define-macro (test-macro msg expect form)
19 `(test ,msg ',expect (lambda () (unident (%macroexpand ,form)))))
20
21 ;;----------------------------------------------------------------------
22 ;;
23
24 (test-section "ER macro basics")
25
26 (define-syntax er-when
27 (er-macro-transformer
28 (^[f r c]
29 (let ([test (cadr f)]
30 [exprs (cddr f)])
31 `(,(r 'if) ,test (,(r 'begin) ,@exprs))))))
32
33 (test "when - basic" #t (^[] (let ((x #f)) (er-when #t (set! x #t)) x)))
34 (test "when - basic" #f (^[] (let ((x #f)) (er-when #f (set! x #t)) x)))
35
36 (test "when - hygene" 3
37 (^[] (let ([if list]
38 [begin list])
39 (er-when #t 1 2 3))))
40
41 (define-syntax er-aif
42 (er-macro-transformer
43 (^[f r c]
44 (let ([test (cadr f)]
45 [then (caddr f)]
46 [else (cadddr f)])
47 `(,(r 'let) ((it ,test))
48 (,(r 'if) it ,then ,else))))))
49
50 (test "aif - basic" 4 (^[] (er-aif (+ 1 2) (+ it 1) #f)))
51 (test "aif - basic" 5 (^[] (let ((it 999)) (er-aif (+ 1 2) (+ it 2) #f))))
52
53 (test "aif - hygene" 6
54 (^[] (let ((it 999)
55 (let list))
56 (er-aif (+ 1 2) (+ it 3) #f))))
57 (test "aif - nesting" #t
58 (^[] (let ([it 999])
59 (er-aif (+ 1 2) (er-aif (odd? it) it #f) #f))))
60
61 (test-section "ER macro local scope")
62
63 (let ([if list])
64 (let-syntax ([fake-if (er-macro-transformer
65 (^[f r c] `(,(r 'if) ,@(cdr f))))])
66 (test "fake-if" '(1 2 3) (^[] (fake-if 1 2 3)))
67 (let ([if +])
68 (test "fake-if" '(4 5 6) (^[] (fake-if 4 5 6))))))
69
70 (test-section "ER compare literals")
71
72 ;; from Clinger "Hygienic Macros Through Explicit Renaming"
73 (define-syntax er-cond
74 (er-macro-transformer
75 (^[f r c]
76 (let1 clauses (cdr f)
77 (if (null? clauses)
78 `(,(r 'quote) ,(r 'unspecified))
79 (let* ([first (car clauses)]
80 [rest (cdr clauses)]
81 [test (car first)])
82 (cond [(and (identifier? test)
83 (c test (r 'else)))
84 `(,(r 'begin) ,@(cdr first))]
85 [else `(,(r 'if) ,test
86 (,(r 'begin) ,@(cdr first))
87 (er-cond ,@rest))])))))))
88
89 (define (er-cond-tester1 x)
90 (er-cond [(odd? x) 'odd] [else 'even]))
91
92 (test "er-cond 1" '(even odd)
93 (^[] (list (er-cond-tester1 0) (er-cond-tester1 1))))
94
95 (let ([else #f])
96 (define (er-cond-tester2 x)
97 (er-cond [(odd? x) 'odd] [else 'even]))
98 (test "er-cond 2" '(unspecified odd)
99 (^[] (list (er-cond-tester2 0) (er-cond-tester2 1)))))
100
101 (define-module er-test-mod
102 (export er-cond2)
103 (define-syntax er-cond2
104 (er-macro-transformer
105 (^[f r c]
106 (let1 clauses (cdr f)
107 (if (null? clauses)
108 `(,(r 'quote) ,(r 'unspecified))
109 (let* ([first (car clauses)]
110 [rest (cdr clauses)]
111 [test (car first)])
112 (cond [(and (identifier? test)
113 (c test (r 'else)))
114 `(,(r 'begin) ,@(cdr first))]
115 [else `(,(r 'if) ,test
116 (,(r 'begin) ,@(cdr first))
117 (er-cond2 ,@rest))]))))))))
118
119 (define-module er-test-mod2
120 (use gauche.test)
121 (import er-test-mod)
122 (define (er-cond-tester1 x)
123 (er-cond2 [(odd? x) 'odd] [else 'even]))
124 (test "er-cond (cross-module)" '(even odd)
125 (^[] (list (er-cond-tester1 0) (er-cond-tester1 1)))))
126
127 ;; Introducing local bindings
128 (let ((x 3))
129 (let-syntax ([foo (er-macro-transformer
130 (^[f r c]
131 (let1 body (cdr f)
132 `(,(r 'let) ([,(r 'x) (,(r '+) ,(r 'x) 2)])
133 (,(r '+) ,(r 'x) ,@body)))))])
134 (let ((x -1))
135 (test* "er-macro introducing local bindings" 4
136 (foo x)))))
137
138 ;; er-macro and nested identifier
139 ;; cf. http://saito.hatenablog.jp/entry/2014/11/18/233209
140 (define (er-test-traverse proc obj)
141 (let loop ((obj obj))
142 (cond [(identifier? obj) (proc obj)]
143 [(pair? obj) (cons (loop (car obj)) (loop (cdr obj)))]
144 [(vector? obj) (vector-map loop obj)]
145 [else obj])))
146
147 (define-syntax er-test-let/scope
148 (er-macro-transformer
149 (lambda (form rename _)
150 (let ([scope (cadr form)]
151 [body (cddr form)])
152 `(let-syntax ((,scope
153 (,(rename 'er-macro-transformer)
154 (,(rename 'lambda) (f r _)
155 (,(rename 'let) ((form2 (,(rename 'cdr) f)))
156 (,(rename 'cons)
157 ',(rename 'begin)
158 (,(rename 'er-test-traverse) r form2)))))))
159 ,@body)))))
160
161 (test "er-macro and nested identifier"
162 '(2 2 3 4)
163 (lambda ()
164 (let ([x 1])
165 (er-test-let/scope scope-1
166 (let ([x 2])
167 (er-test-let/scope scope-2
168 (let ([x 3])
169 (er-test-let/scope scope-1
170 (let ([x 4])
171 (list (scope-2 (scope-1 x))
172 (scope-2 x)
173 (scope-1 x)
174 x))))))))))
175
176 ;; passing form rename procedure
177 (let ([a 1] [b 2])
178 (let-syntax ([foo (er-macro-transformer
179 (lambda (f r c)
180 (r '(cons (list a b) `#(,a ,b)))))])
181 (let ([a -1] [b -2] [list *])
182 (test* "list arg for rename procedure"
183 '((1 2) . #(1 2))
184 (foo)))))
185
186 ;; er-macro and with-module
187 ;; cf. https://github.com/shirok/Gauche/issues/250
188 (define er-macro-scope-test-a 'a)
189
190 (define-module er-macro-test-1
191 (define er-macro-scope-test-a 'b))
192
193 (with-module er-macro-test-1
194 (define-syntax er-macro-test-x
195 (er-macro-transformer
196 (^[f r c] (r 'er-macro-scope-test-a)))))
197
198 (test* "er-macro and with-module" 'b
199 ((with-module er-macro-test-1 er-macro-test-x)))
200
201 ;; er-macro and eval
202 (test* "er-macro and eval" 'b
203 (eval '(let-syntax ((m (er-macro-transformer
204 (^[f r c] (r 'er-macro-scope-test-a)))))
205 (m))
206 (find-module 'er-macro-test-1)))
207
208 ;; quasirename
209 (let ((unquote list)
210 (x 1)
211 (y 2))
212 (let-syntax ([foo (er-macro-transformer
213 (^[f r c]
214 (let ([a (cadr f)]
215 [b (caddr f)]
216 [all (cdr f)])
217 (quasirename r
218 `(list x ,a y ,b ,@all
219 '#(x ,a y ,b) ,@(reverse all))))))])
220 (let ((list vector)
221 (x 10)
222 (y 20))
223 (test* "er-macro and quasirename"
224 '(1 3 2 4 3 4 #(x 3 y 4) 4 3)
225 (foo 3 4)))))
226
227 ;; nested quasirename
228 (let ()
229 (define (add-prefix p)
230 (^s (symbol-append p s)))
231 (define a 1)
232 (define b 2)
233 (define c 3)
234 (test* "nested quasirename"
235 '(p:quasirename p:x
236 `(p:a ,p:b ,(p:quote 3) ,d))
237 (unwrap-syntax
238 (quasirename (add-prefix 'p:)
239 `(quasirename x
240 `(a ,b ,',c ,,'d))))))
241
242 (let-syntax ([def (er-macro-transformer
243 (^[f r c]
244 (quasirename r
245 `(define-syntax ,(cadr f)
246 (er-macro-transformer
247 ;; we need to protect ff from being renamed,
248 ;; for we have to refer to it inside quote
249 ;; in (cadr ff).
250 (^[,'ff rr cc]
251 (quasirename rr
252 `(define ,',(caddr f) ,,'(cadr ff)))))))))])
253 (test* "nested quasirename" 4
254 (let ()
255 (def foo bar)
256 (let ()
257 (foo 4)
258 bar))))
259
260 ;; Mixing syntax-rules and er-macro requires unhygienic identifiers to be
261 ;; explicitly "injected".
262 ;; (This does not work with the current compiler)
263
264 ;; (define-syntax eri-test-loop
265 ;; (eri-macro-transformer
266 ;; (lambda (x r c i)
267 ;; (let ((body (cdr x)))
268 ;; `(,(r 'call-with-current-continuation)
269 ;; (,(r 'lambda) (,(i 'exiit))
270 ;; (,(r 'let) ,(r 'f) () ,@body (,(r 'f)))))))))
271
272 ;; (define-syntax eri-test-foo
273 ;; (syntax-rules ()
274 ;; ((_ x) (eri-test-loop (exiit x)))))
275
276 ;; (test* "Mixing syntax-rules and eri-macro" 'yot
277 ;; (let ((exiit 42))
278 ;; (eri-test-foo exiit)))
279
280 ;;----------------------------------------------------------------------
281 ;; er identifier macros
282 ;; EXPERIMENTAL - The interface of er-macro-transformer may change
283
284 (test-section "er identifier macros")
285
286 ;; global
287 (define-module id-macro-test-er
288 (use gauche.test)
289 (define p (cons 4 5))
290 (define-syntax p.car
291 (make-id-transformer
292 (er-macro-transformer
293 (lambda (f r c)
294 (cond [(identifier? f) (quasirename r `(car p))]
295 [(c (car f) (r'set!))
296 (quasirename r `(set-car! p ,(caddr f)))]
297 [else (error "bad id-macro call:" f)])))))
298 (test "er global identifier macro" 4 (lambda () p.car))
299 (set! p.car 15)
300 (test "er global identifier macro" 15 (lambda () p.car))
301 (test "er global identifier macro" '(15 . 5) (lambda () p))
302
303 (let ((p (cons 6 7)))
304 (test "er global identifier macro hygiene" 15 (lambda () p.car))
305 (set! p.car 99)
306 (test "er global identifier macro hygiene" 99 (lambda () p.car))
307 (test "er global identifier macro hygiene" '(6 . 7) (lambda () p)))
308
309 (set! p.car list)
310 (test "er global identifier macro in head" '(1 2 3) (lambda () (p.car 1 2 3)))
311 )
312
313 (define-module id-macro-test-sr
314 (use gauche.test)
315 (define p (cons 4 5))
316 (define-syntax p.car
317 (make-id-transformer
318 (syntax-rules (set!)
319 [(set! _ expr) (set-car! p expr)]
320 [_ (car p)])))
321
322 (test "synrule global identifier macro" 4 (lambda () p.car))
323 (set! p.car 15)
324 (test "synrule global identifier macro" 15 (lambda () p.car))
325 (test "synrule global identifier macro" '(15 . 5) (lambda () p))
326
327 (let ((p (cons 6 7)))
328 (test "synrule global identifier macro hygiene" 15 (lambda () p.car))
329 (set! p.car 99)
330 (test "synrule global identifier macro hygiene" 99 (lambda () p.car))
331 (test "synrule global identifier macro hygiene" '(6 . 7) (lambda () p)))
332 )
333
334 ;; local
335 (let ((p (cons 4 5)))
336 (let-syntax ((p.car
337 (make-id-transformer
338 (er-macro-transformer
339 (lambda (f r c)
340 (cond [(identifier? f) (quasirename r `(car p))]
341 [(c (car f) (r'set!))
342 (quasirename r `(set-car! p ,(caddr f)))]
343 [else (error "bad id-macro call:" f)]))))))
344 (test "er local identifier macro" 4 (lambda () p.car))
345 (set! p.car 15)
346 (test "er local identifier macro" 15 (lambda () p.car))
347 (test "er local identifier macro" '(15 . 5) (lambda () p))
348
349 (let ((p (cons 6 7)))
350 (test "er local identifier macro hygiene" 15 (lambda () p.car))
351 (set! p.car 99)
352 (test "er local identifier macro hygiene" 99 (lambda () p.car))
353 (test "er local identifier macro hygiene" '(6 . 7) (lambda () p)))
354
355 (set! p.car list)
356 (test "er local identifier macro in head" '(1 2 3) (lambda () (p.car 1 2 3)))
357 ))
358
359 ;;----------------------------------------------------------------------
360 ;; basic tests
361
362 (test-section "basic expansion")
363
364 (define-syntax simple (syntax-rules ()
365 ((_ "a" ?a) (a ?a))
366 ((_ "b" ?a) (b ?a))
367 ((_ #f ?a) (c ?a))
368 ((_ (#\a #\b) ?a) (d ?a))
369 ((_ #(1 2) ?a) (e ?a))
370 ((_ ?b ?a) (f ?a ?b))))
371
372 (test-macro "simple" (a z) (simple "a" z))
373 (test-macro "simple" (b z) (simple "b" z))
374 (test-macro "simple" (c z) (simple #f z))
375 (test-macro "simple" (d z) (simple (#\a #\b) z))
376 (test-macro "simple" (e z) (simple #(1 2) z))
377 (test-macro "simple" (f z #(1.0 2.0)) (simple #(1.0 2.0) z))
378 (test-macro "simple" (f z (#\b #\a)) (simple (#\b #\a) z))
379 (test-macro "simple" (f z #(2 1)) (simple #(2 1) z))
380
381 (define-syntax underbar (syntax-rules ()
382 [(_) 0]
383 [(_ _) 1]
384 [(_ _ _) 2]
385 [(_ _ _ _) 3]
386 [(_ _ _ _ . _) many]))
387 (test-macro "underbar" 0 (underbar))
388 (test-macro "underbar" 1 (underbar a))
389 (test-macro "underbar" 2 (underbar a b))
390 (test-macro "underbar" 3 (underbar a b c))
391 (test-macro "underbar" many (underbar a b c d))
392
393 (define-syntax repeat (syntax-rules ()
394 ((_ 0 (?a ?b) ...) ((?a ...) (?b ...)))
395 ((_ 1 (?a ?b) ...) (?a ... ?b ...))
396 ((_ 2 (?a ?b) ...) (?a ... ?b ... ?a ...))
397 ((_ 0 (?a ?b ?c) ...) ((?a ...) (?b ?c) ...))
398 ((_ 1 (?a ?b ?c) ...) (?a ... (?c 8 ?b) ...))
399 ))
400
401 (test-macro "repeat" ((a c e) (b d f))
402 (repeat 0 (a b) (c d) (e f)))
403 (test-macro "repeat" (a c e b d f)
404 (repeat 1 (a b) (c d) (e f)))
405 (test-macro "repeat" (a c e b d f a c e)
406 (repeat 2 (a b) (c d) (e f)))
407 (test-macro "repeat" ((a d g) (b c) (e f) (h i))
408 (repeat 0 (a b c) (d e f) (g h i)))
409 (test-macro "repeat" (a d g (c 8 b) (f 8 e) (i 8 h))
410 (repeat 1 (a b c) (d e f) (g h i)))
411
412 (define-syntax repeat2 (syntax-rules () ;r7rs
413 ((_ 0 (?a ?b ... ?c)) (?a (?b ...) ?c))
414 ((_ 1 (?a ?b ... ?c ?d)) (?a (?b ...) ?c ?d))
415 ((_ 2 (?a ?b ... . ?c)) (?a (?b ...) ?c))
416 ((_ 3 (?a ?b ... ?c ?d . ?e)) (?a (?b ...) ?c ?d ?e))
417 ((_ ?x ?y) ho)))
418
419 (test-macro "repeat2" (a (b c d e f) g)
420 (repeat2 0 (a b c d e f g)))
421 (test-macro "repeat2" (a () b)
422 (repeat2 0 (a b)))
423 (test-macro "repeat2" ho
424 (repeat2 0 (a)))
425 (test-macro "repeat2" (a (b c d e) f g)
426 (repeat2 1 (a b c d e f g)))
427 (test-macro "repeat2" (a () b c)
428 (repeat2 1 (a b c)))
429 (test-macro "repeat2" ho
430 (repeat2 1 (a b)))
431 (test-macro "repeat2" (a (b c d e f g) ())
432 (repeat2 2 (a b c d e f g)))
433 (test-macro "repeat2" (a (b c d e) f g ())
434 (repeat2 3 (a b c d e f g)))
435 (test-macro "repeat2" (a (b c d) e)
436 (repeat2 2 (a b c d . e)))
437 (test-macro "repeat2" (a (b) c d e)
438 (repeat2 3 (a b c d . e)))
439
440 (define-syntax nest1 (syntax-rules ()
441 ((_ (?a ...) ...) ((?a ... z) ...))))
442
443 (test-macro "nest1" ((a z) (b c d z) (e f g h i z) (z) (j z))
444 (nest1 (a) (b c d) (e f g h i) () (j)))
445
446 (define-syntax nest2 (syntax-rules ()
447 ((_ ((?a ?b) ...) ...) ((?a ... ?b ...) ...))))
448
449 (test-macro "nest2" ((a c b d) () (e g i f h j))
450 (nest2 ((a b) (c d)) () ((e f) (g h) (i j))))
451
452 (define-syntax nest3 (syntax-rules ()
453 ((_ ((?a ?b ...) ...) ...) ((((?b ...) ...) ...)
454 ((?a ...) ...)))))
455
456 (test-macro "nest3" ((((b c d e) (g h i)) (() (l m n) (p)) () ((r)))
457 ((a f) (j k o) () (q)))
458 (nest3 ((a b c d e) (f g h i)) ((j) (k l m n) (o p)) () ((q r))))
459
460 (define-syntax nest4 (syntax-rules () ; r7rs
461 ((_ ((?a ?b ... ?c) ... ?d))
462 ((?a ...) ((?b ...) ...) (?c ...) ?d))))
463
464 (test-macro "nest4"((a d f)
465 ((b) () (g h i))
466 (c e j)
467 (k l m))
468 (nest4 ((a b c) (d e) (f g h i j) (k l m))))
469
470 (define-syntax nest5 (syntax-rules () ; r7rs
471 ((_ (?a (?b ... ?c ?d) ... . ?e))
472 (?a ((?b ...) ...) (?c ...) (?d ...) ?e))))
473 (test-macro "nest5" (z
474 ((a) (d e) ())
475 (b f h)
476 (c g i)
477 j)
478 (nest5 (z (a b c) (d e f g) (h i) . j)))
479
480 (define-syntax nest6 (syntax-rules ()
481 ((_ (?a ...) ...)
482 (?a ... ...)))) ;SRFI-149
483 (test-macro "nest6" (a b c d e f g h i j)
484 (nest6 (a b c d) (e f g) (h i) (j)))
485 (test-macro "nest6" (a b c d e f g)
486 (nest6 (a b c d) () (e) () (f g)))
487
488 (define-syntax nest7 (syntax-rules ()
489 ((_ (?a ...) ...)
490 (?a ... ... z ?a ... ...)))) ;SRFI-149
491 (test-macro "nest7" (a b c d e f g h i j z a b c d e f g h i j)
492 (nest7 (a b c d) (e f g) (h i) (j)))
493 (test-macro "nest7" (a b c d e f g z a b c d e f g)
494 (nest7 (a b c d) () (e) () (f g)))
495
496 (define-syntax nest8 (syntax-rules ()
497 ((_ ((?a ...) ...) ...)
498 (?a ... ... ... z)))) ;SRFI-149
499 (test-macro "nest8" (a b c d e f g h i j z)
500 (nest8 ((a b c d) (e f g)) ((h i) (j))))
501 (test-macro "nest8" (a b c d e f g h i j z)
502 (nest8 ((a b c d) () (e f g)) () ((h i) () (j) ())))
503
504 ;; mixlevel is allowed by SRFI-149
505 (define-syntax mixlevel1 (syntax-rules ()
506 ((_ (?a ?b ...)) ((?a ?b) ...))))
507
508 (test-macro "mixlevel1" ((1 2) (1 3) (1 4) (1 5) (1 6))
509 (mixlevel1 (1 2 3 4 5 6)))
510 (test-macro "mixlevel1" ()
511 (mixlevel1 (1)))
512
513 (define-syntax mixlevel2 (syntax-rules ()
514 ((_ (?a ?b ...) ...)
515 (((?a ?b) ...) ...))))
516
517 (test-macro "mixlevel2" (((1 2) (1 3) (1 4)) ((2 3) (2 4) (2 5) (2 6)))
518 (mixlevel2 (1 2 3 4) (2 3 4 5 6)))
519
520 (define-syntax mixlevel3 (syntax-rules ()
521 ((_ ?a (?b ?c ...) ...)
522 (((?a ?b ?c) ...) ...))))
523
524 (test-macro "mixlevel3" (((1 2 3) (1 2 4) (1 2 5) (1 2 6))
525 ((1 7 8) (1 7 9) (1 7 10)))
526 (mixlevel3 1 (2 3 4 5 6) (7 8 9 10)))
527
528 ;; test that wrong usage of ellipsis is correctly identified
529 (test "bad ellipsis 1" (test-error)
530 (lambda ()
531 (eval '(define-syntax badellipsis
532 (syntax-rules () [(t) (3 ...)]))
533 (interaction-environment))))
534 (test "bad ellipsis 2" (test-error)
535 (lambda ()
536 (eval '(define-syntax badellipsis
537 (syntax-rules () [(t a) (a ...)]))
538 (interaction-environment))))
539 (test "bad ellipsis 3" (test-error)
540 (lambda ()
541 (eval '(define-syntax badellipsis
542 (syntax-rules () [(t a b ...) (a ...)]))
543 (interaction-environment))))
544 (test "bad ellipsis 4" (test-error)
545 (lambda ()
546 (eval '(define-syntax badellipsis
547 (syntax-rules () [(t a ...) ((a ...) ...)]))
548 (interaction-environment))))
549
550 (test "bad ellipsis 5" (test-error)
551 (lambda ()
552 (eval '(define-syntax badellipsis
553 (syntax-rules () [(t (a ... b ...)) ((a ...) (b ...))]))
554 (interaction-environment))))
555 (test "bad ellipsis 6" (test-error)
556 (lambda ()
557 (eval '(define-syntax badellipsis
558 (syntax-rules () [(t (... a b)) (... a b )]))
559 (interaction-environment))))
560
561 (define-syntax hygiene (syntax-rules ()
562 ((_ ?a) (+ ?a 1))))
563 (test "hygiene" 3
564 (lambda () (let ((+ *)) (hygiene 2))))
565
566 (define-syntax vect1 (syntax-rules ()
567 ((_ #(?a ...)) (?a ...))
568 ((_ (?a ...)) #(?a ...))))
569 (test-macro "vect1" (1 2 3 4 5) (vect1 #(1 2 3 4 5)))
570 (test-macro "vect1" #(1 2 3 4 5) (vect1 (1 2 3 4 5)))
571
572 (define-syntax vect2 (syntax-rules ()
573 ((_ #(#(?a ?b) ...)) #(?a ... ?b ...))
574 ((_ #((?a ?b) ...)) (?a ... ?b ...))
575 ((_ (#(?a ?b) ...)) (#(?a ...) #(?b ...)))))
576
577 (test-macro "vect2" #(a c e b d f) (vect2 #(#(a b) #(c d) #(e f))))
578 (test-macro "vect2" (a c e b d f) (vect2 #((a b) (c d) (e f))))
579 (test-macro "vect2" (#(a c e) #(b d f)) (vect2 (#(a b) #(c d) #(e f))))
580
581 (define-syntax vect3 (syntax-rules ()
582 ((_ 0 #(?a ... ?b)) ((?a ...) ?b))
583 ((_ 0 ?x) ho)
584 ((_ 1 #(?a ?b ... ?c ?d ?e)) (?a (?b ...) ?c ?d ?e))
585 ((_ 1 ?x) ho)))
586
587 (test-macro "vect3" ((a b c d e) f)
588 (vect3 0 #(a b c d e f)))
589 (test-macro "vect3" (() a)
590 (vect3 0 #(a)))
591 (test-macro "vect3" ho
592 (vect3 0 #()))
593 (test-macro "vect3" (a (b c) d e f)
594 (vect3 1 #(a b c d e f)))
595 (test-macro "vect3" (a () b c d)
596 (vect3 1 #(a b c d)))
597 (test-macro "vect3" ho
598 (vect3 1 #(a b c)))
599
600 (define-syntax dot1 (syntax-rules ()
601 ((_ (?a . ?b)) (?a ?b))
602 ((_ ?loser) #f)))
603 (test-macro "dot1" (1 2) (dot1 (1 . 2)))
604 (test-macro "dot1" (1 (2)) (dot1 (1 2)))
605 (test-macro "dot1" (1 ()) (dot1 (1)))
606 (test-macro "dot1" (1 (2 3)) (dot1 (1 2 3)))
607 (test-macro "dot1" #f (dot1 ()))
608
609 (define-syntax dot2 (syntax-rules ()
610 ((_ ?a . ?b) (?b . ?a))
611 ((_ . ?loser) #f)))
612 (test-macro "dot2" (2 . 1) (dot2 1 . 2))
613 (test-macro "dot2" ((2) . 1) (dot2 1 2))
614 (test-macro "dot2" (() . 1) (dot2 1))
615 (test-macro "dot2" ((2 3) . 1) (dot2 1 2 3))
616 (test-macro "dot2" #f (dot2))
617
618 ;; pattern to yield (. x) => x
619 (define-syntax dot3 (syntax-rules ()
620 ((_ (?a ...) ?b) (?a ... . ?b))))
621 (test-macro "dot3" (1 2 . 3) (dot3 (1 2) 3))
622 (test-macro "dot3" 3 (dot3 () 3))
623
624 ;; see if effective quote introduced by quasiquote properly unwrap
625 ;; syntactic environment.
626 (define-syntax unwrap1 (syntax-rules ()
627 ((_ x) `(a ,x))))
628 (test "unwrap1" '(a 3) (lambda () (unwrap1 3))
629 (lambda (x y) (and (eq? (car x) (car y)) (eq? (cadr x) (cadr y)))))
630 (test "unwrap1" '(a 4) (lambda () (let ((a 4)) (unwrap1 a)))
631 (lambda (x y) (and (eq? (car x) (car y)) (eq? (cadr x) (cadr y)))))
632
633 ;; regression check for quasiquote hygienty handling code
634 (define-syntax qq1 (syntax-rules ()
635 ((_ a) `(,@a))))
636 (define-syntax qq2 (syntax-rules ()
637 ((_ a) `#(,@a))))
638
639 (test "qq1" '() (lambda () (qq1 '())))
640 (test "qq2" '#() (lambda () (qq2 '())))
641
642 ;; R7RS style alternative ellipsis
643 (test-section "alternative ellipsis")
644
645 (define-syntax alt-elli1
646 (syntax-rules ooo ()
647 [(_ ... ooo) '((... ...) ooo)]))
648
649 (test "alt-elli1" '((a a) (b b) (c c)) (lambda () (alt-elli1 a b c)))
650
651 (define-syntax alt-elli2
652 (syntax-rules ::: ()
653 [(_ ... :::) '((... ...) :::)]))
654
655 (test "alt-elli2" '((a a) (b b) (c c)) (lambda () (alt-elli2 a b c)))
656
657 ;; https://srfi-email.schemers.org/srfi-148/msg/6115633
658 (define-syntax alt-elli3
659 (syntax-rules ... (...)
660 [(m x y ...) 'ellipsis]
661 [(m x ...) 'literal]))
662
663 (test "alt-elli3" 'literal (lambda () (alt-elli3 x ...)))
664
665 ;;----------------------------------------------------------------------
666 ;; cond, taken from R5RS section 7.3
667
668 (test-section "recursive expansion")
669
670 (define-syntax %cond
671 (syntax-rules (else =>)
672 ((cond (else result1 result2 ...))
673 (begin result1 result2 ...))
674 ((cond (test => result))
675 (let ((temp test))
676 (if temp (result temp))))
677 ((cond (test => result) clause1 clause2 ...)
678 (let ((temp test))
679 (if temp
680 (result temp)
681 (%cond clause1 clause2 ...))))
682 ((cond (test)) test)
683 ((cond (test) clause1 clause2 ...)
684 (let ((temp test))
685 (if temp temp (%cond clause1 clause2 ...))))
686 ((cond (test result1 result2 ...))
687 (if test (begin result1 result2 ...)))
688 ((cond (test result1 result2 ...) clause1 clause2 ...)
689 (if test (begin result1 result2 ...) (%cond clause1 clause2 ...)))
690 ))
691
692 (test-macro "%cond" (begin a) (%cond (else a)))
693 (test-macro "%cond" (begin a b c) (%cond (else a b c)))
694 (test-macro "%cond" (let ((temp a)) (if temp (b temp))) (%cond (a => b)))
695 (test-macro "%cond" (let ((temp a)) (if temp (b temp) (%cond c))) (%cond (a => b) c))
696 (test-macro "%cond" (let ((temp a)) (if temp (b temp) (%cond c d))) (%cond (a => b) c d))
697 (test-macro "%cond" (let ((temp a)) (if temp (b temp) (%cond c d e))) (%cond (a => b) c d e))
698 (test-macro "%cond" a (%cond (a)))
699 (test-macro "%cond" (let ((temp a)) (if temp temp (%cond b))) (%cond (a) b))
700 (test-macro "%cond" (let ((temp a)) (if temp temp (%cond b c))) (%cond (a) b c))
701 (test-macro "%cond" (if a (begin b)) (%cond (a b)))
702 (test-macro "%cond" (if a (begin b c d)) (%cond (a b c d)))
703 (test-macro "%cond" (if a (begin b c d) (%cond e f g)) (%cond (a b c d) e f g))
704
705 ;; test for higiene
706 (test "%cond" '(if a (begin => b))
707 (lambda () (let ((=> #f)) (unident (%macroexpand (%cond (a => b)))))))
708 (test "%cond" '(if else (begin z))
709 (lambda () (let ((else #t)) (unident (%macroexpand (%cond (else z)))))))
710
711 ;;----------------------------------------------------------------------
712 ;; letrec, taken from R5RS section 7.3
713 (define-syntax %letrec
714 (syntax-rules ()
715 ((_ ((var1 init1) ...) body ...)
716 (%letrec "generate_temp_names"
717 (var1 ...)
718 ()
719 ((var1 init1) ...)
720 body ...))
721 ((_ "generate_temp_names" () (temp1 ...) ((var1 init1) ...) body ...)
722 (let ((var1 :undefined) ...)
723 (let ((temp1 init1) ...)
724 (set! var1 temp1) ...
725 body ...)))
726 ((_ "generate_temp_names" (x y ...) (temp ...) ((var1 init1) ...) body ...)
727 (%letrec "generate_temp_names"
728 (y ...)
729 (newtemp temp ...)
730 ((var1 init1) ...)
731 body ...))))
732
733 ;; Note: if you "unident" the expansion result of %letrec, you see a symbol
734 ;; "newtemp" appears repeatedly in the let binding, seemingly expanding
735 ;; into invalid syntax. Internally, however, those symbols are treated
736 ;; as identifiers with the correct identity, so the expanded code works
737 ;; fine (as tested in the second test).
738 (test-macro "%letrec"
739 (let ((a :undefined)
740 (c :undefined))
741 (let ((newtemp b)
742 (newtemp d))
743 (set! a newtemp)
744 (set! c newtemp)
745 e f g))
746 (%letrec ((a b) (c d)) e f g))
747 (test "%letrec" '(1 2 3)
748 (lambda () (%letrec ((a 1) (b 2) (c 3)) (list a b c))))
749
750 ;;----------------------------------------------------------------------
751 ;; do, taken from R5RS section 7.3
752 (define-syntax %do
753 (syntax-rules ()
754 ((_ ((var init step ...) ...)
755 (test expr ...)
756 command ...)
757 (letrec
758 ((loop
759 (lambda (var ...)
760 (if test
761 (begin
762 (if #f #f)
763 expr ...)
764 (begin
765 command
766 ...
767 (loop (%do "step" var step ...)
768 ...))))))
769 (loop init ...)))
770 ((_ "step" x)
771 x)
772 ((_ "step" x y)
773 y)))
774
775 (test-macro "%do"
776 (letrec ((loop (lambda (x y)
777 (if (>= x 10)
778 (begin (if #f #f) y)
779 (begin (loop (%do "step" x (+ x 1))
780 (%do "step" y (* y 2))))))))
781 (loop 0 1))
782 (%do ((x 0 (+ x 1))
783 (y 1 (* y 2)))
784 ((>= x 10) y)))
785 (test "%do" 1024
786 (lambda () (%do ((x 0 (+ x 1))
787 (y 1 (* y 2)))
788 ((>= x 10) y))))
789
790 (test-macro "%do"
791 (letrec ((loop (lambda (y x)
792 (if (>= x 10)
793 (begin (if #f #f) y)
794 (begin (set! y (* y 2))
795 (loop (%do "step" y)
796 (%do "step" x (+ x 1))))))))
797 (loop 1 0))
798 (%do ((y 1)
799 (x 0 (+ x 1)))
800 ((>= x 10) y)
801 (set! y (* y 2))))
802 (test "%do" 1024
803 (lambda () (%do ((y 1)
804 (x 0 (+ x 1)))
805 ((>= x 10) y)
806 (set! y (* y 2)))))
807
808 ;;----------------------------------------------------------------------
809 ;; non-syntax-rule transformers
810
811 (test-section "transformers other than syntax-rules")
812
813 (define-syntax xif if)
814 (test "xif" 'ok (lambda () (xif #f 'ng 'ok)))
815
816 (define-syntax fi (syntax-rules () [(_ a b c) (xif a c b)]))
817 (define-syntax xfi fi)
818 (test "xfi" 'ok (lambda () (xfi #f 'ok 'ng)))
819
820 ;;----------------------------------------------------------------------
821 ;; local syntactic bindings.
822
823 (test-section "local syntactic bindings")
824
825 (test "let-syntax" ; R5RS 4.3.1
826 'now
827 (lambda ()
828 (let-syntax ((%when (syntax-rules ()
829 ((_ test stmt1 stmt2 ...)
830 (if test (begin stmt1 stmt2 ...))))))
831 (let ((if #t))
832 (%when if (set! if 'now))
833 if))))
834
835 (test "let-syntax" ; R5RS 4.3.1
836 'outer
837 (lambda ()
838 (let ((x 'outer))
839 (let-syntax ((m (syntax-rules () ((m) x))))
840 (let ((x 'inner))
841 (m))))))
842
843 (test "let-syntax (multi)"
844 81
845 (lambda ()
846 (let ((+ *))
847 (let-syntax ((a (syntax-rules () ((_ ?x) (+ ?x ?x))))
848 (b (syntax-rules () ((_ ?x) (* ?x ?x)))))
849 (let ((* -)
850 (+ /))
851 (a (b 3)))))))
852
853 (test "let-syntax (nest)"
854 19
855 (lambda ()
856 (let-syntax ((a (syntax-rules () ((_ ?x ...) (+ ?x ...)))))
857 (let-syntax ((a (syntax-rules ()
858 ((_ ?x ?y ...) (a ?y ...))
859 ((_) 2))))
860 (a 8 9 10)))))
861
862 (test "let-syntax (nest)"
863 '(-6 11)
864 (lambda ()
865 (let-syntax ((a (syntax-rules () ((_ ?x) (+ ?x 8))))
866 (b (syntax-rules () ((_ ?x) (- ?x 8)))))
867 (let-syntax ((a (syntax-rules () ((_ ?x) (b 2))))
868 (b (syntax-rules () ((_ ?x) (a 3)))))
869 (list (a 7) (b 8))))))
870
871 (test "letrec-syntax" ; R5RS 4.3.1
872 7
873 (lambda ()
874 (letrec-syntax ((%or (syntax-rules ()
875 ((_) #f)
876 ((_ e) e)
877 ((_ e f ...)
878 (let ((temp e))
879 (if temp temp (%or f ...)))))))
880 (let ((x #f)
881 (y 7)
882 (temp 8)
883 (let odd?)
884 (if even?))
885 (%or x (let temp) (if y) y)))))
886
887 (test "letrec-syntax (nest)"
888 2
889 (lambda ()
890 (letrec-syntax ((a (syntax-rules () ((_ ?x ...) (+ ?x ...)))))
891 (letrec-syntax ((a (syntax-rules ()
892 ((_ ?x ?y ...) (a ?y ...))
893 ((_) 2))))
894 (a 8 9 10)))))
895
896 (test "letrec-syntax (nest)"
897 '(9 11)
898 (lambda ()
899 (letrec-syntax ((a (syntax-rules () ((_ ?x) (+ ?x 8))))
900 (b (syntax-rules () ((_ ?x) (- ?x 8)))))
901 (letrec-syntax ((a (syntax-rules ()
902 ((_ ?x) (b ?x 2))
903 ((_ ?x ?y) (+ ?x ?y))))
904 (b (syntax-rules ()
905 ((_ ?x) (a ?x 3))
906 ((_ ?x ?y) (+ ?x ?y)))))
907 (list (a 7) (b 8))))))
908
909 (test "letrec-syntax (recursive)"
910 #t
911 (lambda ()
912 (letrec-syntax ((o? (syntax-rules ()
913 ((o? ()) #f)
914 ((o? (x . xs)) (e? xs))))
915 (e? (syntax-rules ()
916 ((e? ()) #t)
917 ((e? (x . xs)) (o? xs)))))
918 (e? '(a a a a)))))
919
920 ;; shadowing variable binding with syntactic binding
921 ;; it was allowed up to 0.9.10, but no longer.
922 (test "shadowing" (test-error <error> #/Non-identifier-macro can't appear/)
923 (lambda ()
924 (eval '(let ((x 0))
925 (let-syntax ((x (syntax-rules () ((_) 1))))
926 (list x)))
927 (current-module))))
928
929 ;; This is from comp.lang.scheme posting by Antti Huima
930 ;; http://groups.google.com/groups?hl=ja&selm=7qpu5ncg2l.fsf%40divergence.tcs.hut.fi
931 (test "let-syntax (huima)" '(1 3 5 9)
932 (lambda ()
933 (define the-procedure
934 (let-syntax((l(syntax-rules()((l((x(y ...))...)b ...)(let-syntax((x (syntax-rules()y ...))...) b ...)))))(l('(('(a b ...)(lambda a b ...)))`((`(a b c)(if a b c))(`(a)(car a))),((,(a b)(set! a b))(,(a)(cdr a))),@((,@z(call-with-current-continuation z))))'((ls)('((s)('((i) ('((d)('((j)('((c)('((p)('((l)('(()(l l))))'((k)`((pair?,(p))('((c) ,(p(append,(,(p))(d c)))(k k))(c`(p)`(,(p))c))`(p)))))(cons(d)(map d ls))))'((x y c),@'((-)(s x y null? - s)(j x y c)))))'((x y c)('((q)('((f)(cons`(q)(c((f x)x)((f y)y)c)))'((h)`((eq? q h)'((x),(x)) i)))),@'((-)(s x y'((z)(>=`(z)(sqrt(*`(x)`(y)))))- s))))))list)) '((z)z)))'((x y p k l),@'((-)`((p x)(k y)(l y x'((z)`((p z)-(- #f)))k l)))))))))
935 (the-procedure '(5 1 9 3))))
936
937
938 (test "let-syntax, rebinding syntax" 'ok
939 (lambda ()
940 (let-syntax ([xif if] [if when]) (xif #f 'ng 'ok))))
941
942 (test "let-syntax, rebinding macro" 'ok
943 (lambda ()
944 (let-syntax ([if fi]) (if #f 'ok 'ng))))
945
946 ;; Macro-generating-macro scoping
947 ;; Currently it's not working.
948 (define-syntax mgm-bar
949 (syntax-rules ()
950 ((_ . xs) '(bad . xs))))
951
952 (define-syntax mgm-foo
953 (syntax-rules ()
954 ((_ xs)
955 (letrec-syntax ((mgm-bar
956 (syntax-rules ()
957 ((_ (%x . %xs) %ys)
958 (mgm-bar %xs (%x . %ys)))
959 ((_ () %ys)
960 '%ys))))
961 (mgm-bar xs ())))))
962
963 (test "macro-generating-macro scope" '(z y x)
964 (lambda () (mgm-foo (x y z))))
965
966 ;;----------------------------------------------------------------------
967 ;; macro and internal define
968
969 (test-section "macro and internal define")
970
971 (define-macro (gen-idef-1 x)
972 `(define foo ,x))
973
974 (test "define foo (legacy)" 3
975 (lambda ()
976 (gen-idef-1 3)
977 foo))
978 (test "define foo (legacy)" '(3 5)
979 (lambda ()
980 (let ((foo 5))
981 (list (let () (gen-idef-1 3) foo)
982 foo))))
983 (define foo 10)
984 (test "define foo (legacy)" '(3 10)
985 (lambda ()
986 (list (let () (gen-idef-1 3) foo) foo)))
987 (test "define foo (legacy)" '(4 5)
988 (lambda ()
989 (gen-idef-1 4)
990 (define bar 5)
991 (list foo bar)))
992 (test "define foo (legacy)" '(4 5)
993 (lambda ()
994 (define bar 5)
995 (gen-idef-1 4)
996 (list foo bar)))
997
998 (test "define foo (error)" (test-error)
999 (lambda ()
1000 (eval '(let ()
1001 (list 3 4)
1002 (gen-idef-1 5)))))
1003 (test "define foo (error)" (test-error)
1004 (lambda ()
1005 (eval '(let ()
1006 (gen-idef-1 5)))))
1007
1008 (test "define foo (shadow)" 10
1009 (lambda ()
1010 (let ((gen-idef-1 -))
1011 (gen-idef-1 5)
1012 foo)))
1013
1014 (define-macro (gen-idef-2 x y)
1015 `(begin (define foo ,x) (define bar ,y)))
1016
1017 (test "define foo, bar (legacy)" '((0 1) 10)
1018 (lambda ()
1019 (let ((l (let () (gen-idef-2 0 1) (list foo bar))))
1020 (list l foo))))
1021 (test "define foo, bar (legacy)" '(-1 -2 20)
1022 (lambda ()
1023 (define baz 20)
1024 (gen-idef-2 -1 -2)
1025 (list foo bar baz)))
1026 (test "define foo, bar (legacy)" '(-1 -2 20)
1027 (lambda ()
1028 (gen-idef-2 -1 -2)
1029 (define baz 20)
1030 (list foo bar baz)))
1031 (test "define foo, bar (legacy)" '(3 4 20 -10)
1032 (lambda ()
1033 (begin
1034 (define biz -10)
1035 (gen-idef-2 3 4)
1036 (define baz 20))
1037 (list foo bar baz biz)))
1038 (test "define foo, bar (legacy)" '(3 4 20 -10)
1039 (lambda ()
1040 (define biz -10)
1041 (begin
1042 (gen-idef-2 3 4)
1043 (define baz 20)
1044 (list foo bar baz biz))))
1045 (test "define foo, bar (legacy)" '(3 4 20 -10)
1046 (lambda ()
1047 (begin
1048 (define biz -10))
1049 (begin
1050 (gen-idef-2 3 4))
1051 (define baz 20)
1052 (list foo bar baz biz)))
1053 (test "define foo, bar (error)" (test-error)
1054 (lambda ()
1055 (eval '(let ()
1056 (list 3)
1057 (gen-idef-2 -1 -2)
1058 (list foo bar)))))
1059 (test "define foo, bar (error)" (test-error)
1060 (lambda ()
1061 (eval '(let ()
1062 (gen-idef-2 -1 -2)))))
1063
1064 (define-syntax gen-idef-3
1065 (syntax-rules ()
1066 ((gen-idef-3 x y)
1067 (begin (define x y)))))
1068
1069 (test "define boo (r5rs)" 3
1070 (lambda ()
1071 (gen-idef-3 boo 3)
1072 boo))
1073 (test "define boo (r5rs)" '(3 10)
1074 (lambda ()
1075 (let ((l (let () (gen-idef-3 foo 3) foo)))
1076 (list l foo))))
1077
1078 (define-syntax gen-idef-4
1079 (syntax-rules ()
1080 ((gen-idef-4 x y)
1081 (begin (define x y) (+ x x)))))
1082
1083 (test "define poo (r5rs)" 6
1084 (lambda ()
1085 (gen-idef-4 poo 3)))
1086
1087 (test "define poo (r5rs)" 3
1088 (lambda ()
1089 (gen-idef-4 poo 3) poo))
1090
1091 (define-macro (gen-idef-5 o e)
1092 `(begin
1093 (define (,o n)
1094 (if (= n 0) #f (,e (- n 1))))
1095 (define (,e n)
1096 (if (= n 0) #t (,o (- n 1))))))
1097
1098 (test "define (legacy, mutually-recursive)" '(#t #f)
1099 (lambda ()
1100 (gen-idef-5 ooo? eee?)
1101 (list (ooo? 5) (eee? 7))))
1102
1103
1104 (define-syntax gen-idef-6
1105 (syntax-rules ()
1106 ((gen-idef-6 o e)
1107 (begin
1108 (define (o n) (if (= n 0) #f (e (- n 1))))
1109 (define (e n) (if (= n 0) #t (o (- n 1))))))))
1110
1111 (test "define (r5rs, mutually-recursive)" '(#t #f)
1112 (lambda ()
1113 (gen-idef-5 ooo? eee?)
1114 (list (ooo? 5) (eee? 7))))
1115
1116 ;; crazy case when define is redefined
1117 (define-module mac-idef
1118 (export (rename my-define define))
1119 (define (my-define . args) args))
1120
1121 (define-module mac-idef.user
1122 (import mac-idef))
1123
1124 (test "define (redefined)" '(5 2)
1125 (lambda ()
1126 (with-module mac-idef.user
1127 (let ((a 5)) (define a 2)))))
1128
1129 (define-module mac-idef2
1130 (export (rename my-define define))
1131 (define-syntax my-define
1132 (syntax-rules ()
1133 [(_ var expr) (define (var) expr)])))
1134
1135 (define-module mac-idef2.user
1136 (import mac-idef2))
1137
1138 (test "define (redefined2)" 5
1139 (lambda ()
1140 (with-module mac-idef2.user
1141 (let ((a 5)) (define x a) (x)))))
1142
1143 (test "internal define-syntax and scope 1" 'inner
1144 (let ((x 'outer))
1145 (lambda ()
1146 (define x 'inner)
1147 (define-syntax foo
1148 (syntax-rules ()
1149 [(_) x]))
1150 (foo))))
1151
1152 (test "internal define-syntax and scope 2" 'inner
1153 (let ((x 'outer))
1154 (lambda ()
1155 (define-syntax foo
1156 (syntax-rules ()
1157 [(_) x]))
1158 (define x 'inner)
1159 (foo))))
1160
1161 (test "internal define-syntax and scope 3" '(inner inner)
1162 (let ((x 'outer))
1163 (lambda ()
1164 (define-syntax def
1165 (syntax-rules ()
1166 [(_ v) (define v x)]))
1167 (define x 'inner)
1168 (def y)
1169 (list x y))))
1170
1171 (test "internal define-syntax and scope 4" '(inner inner)
1172 (let ((x 'outer))
1173 (lambda ()
1174 (define-syntax def
1175 (syntax-rules ()
1176 [(_ v) (define v (lambda () x))]))
1177 (def y)
1178 (define x 'inner)
1179 (list x (y)))))
1180
1181 (test "internal define-syntax and scope 5" '(inner (inner . innermost))
1182 (let ((x 'outer))
1183 (lambda ()
1184 (define-syntax def1
1185 (syntax-rules ()
1186 [(_ v) (def2 v x)]))
1187 (define-syntax def2
1188 (syntax-rules ()
1189 [(_ v y) (define v (let ((x 'innermost))
1190 (lambda () (cons y x))))]))
1191 (def1 z)
1192 (define x 'inner)
1193 (list x (z)))))
1194
1195 ;;----------------------------------------------------------------------
1196 ;; macro defining macros
1197
1198 (test-section "macro defining macros")
1199
1200 (define-syntax mdm-foo1
1201 (syntax-rules ()
1202 ((mdm-foo1 x y)
1203 (define-syntax x
1204 (syntax-rules ()
1205 ((x z) (cons z y)))))
1206 ))
1207
1208 (mdm-foo1 mdm-cons 0)
1209
1210 (test "define-syntax - define-syntax" '(1 . 0)
1211 (lambda () (mdm-cons 1)))
1212
1213 (define-syntax mdm-foo2
1214 (syntax-rules ()
1215 ((mdm-foo2 x y)
1216 (let-syntax ((x (syntax-rules ()
1217 ((x z) (cons z y)))))
1218 (x 1)))))
1219
1220 (test "define-syntax - let-syntax" '(1 . 0)
1221 (lambda () (mdm-foo2 cons 0)))
1222
1223 (test "let-syntax - let-syntax" '(4 . 3)
1224 (lambda ()
1225 (let-syntax ((mdm-foo3 (syntax-rules ()
1226 ((mdm-foo3 x y body)
1227 (let-syntax ((x (syntax-rules ()
1228 ((x z) (cons z y)))))
1229 body)))))
1230 (mdm-foo3 list 3 (list 4)))))
1231
1232 (test "letrec-syntax - let-syntax" 3
1233 (lambda ()
1234 (letrec-syntax ((mdm-foo4
1235 (syntax-rules ()
1236 ((mdm-foo4 () n) n)
1237 ((mdm-foo4 (x . xs) n)
1238 (let-syntax ((mdm-foo5
1239 (syntax-rules ()
1240 ((mdm-foo5)
1241 (mdm-foo4 xs (+ n 1))))))
1242 (mdm-foo5))))))
1243 (mdm-foo4 (#f #f #f) 0))))
1244
1245 (define-syntax mdm-foo3
1246 (syntax-rules ()
1247 ((mdm-foo3 y)
1248 (letrec-syntax ((o? (syntax-rules ()
1249 ((o? ()) #f)
1250 ((o? (x . xs)) (e? xs))))
1251 (e? (syntax-rules ()
1252 ((e? ()) #t)
1253 ((e? (x . xs)) (o? xs)))))
1254 (e? y)))))
1255
1256 (test "define-syntax - letrec-syntax" #t
1257 (lambda () (mdm-foo3 (a b c d))))
1258
1259 ;; Examples from "Two pitfalls in programming nested R5RS macros"
1260 ;; by Oleg Kiselyov
1261 ;; http://pobox.com/~oleg/ftp/Scheme/r5rs-macros-pitfalls.txt
1262
1263 (define-syntax mdm-bar-m
1264 (syntax-rules ()
1265 ((_ x y)
1266 (let-syntax
1267 ((helper
1268 (syntax-rules ()
1269 ((_ u) (+ x u)))))
1270 (helper y)))))
1271
1272 (test "lexical scope" 5
1273 (lambda () (mdm-bar-m 4 1)))
1274
1275 (define-syntax mdm-bar-m1
1276 (syntax-rules ()
1277 ((_ var body)
1278 (let-syntax
1279 ((helper
1280 (syntax-rules ()
1281 ((_) (lambda (var) body)))))
1282 (helper)))))
1283
1284 (test "lexical scope" 5
1285 (lambda () ((mdm-bar-m1 z (+ z 1)) 4)))
1286
1287 (define-syntax mdm-bar-m3
1288 (syntax-rules ()
1289 ((_ var body)
1290 (let-syntax
1291 ((helper
1292 (syntax-rules ()
1293 ((_ vvar bbody) (lambda (vvar) bbody)))))
1294 (helper var body)))))
1295
1296 (test "passing by parameters" 5
1297 (lambda () ((mdm-bar-m3 z (+ z 1)) 4)))
1298
1299 ;; Macro defining toplevel macros.
1300 (define-syntax defMyQuote
1301 (syntax-rules ()
1302 ((_ name)
1303 (begin
1304 (define-syntax TEMP
1305 (syntax-rules ()
1306 ((_ arg)
1307 `arg)))
1308 (define-syntax name
1309 (syntax-rules ()
1310 ((_ arg)
1311 (TEMP arg))))))))
1312
1313 (defMyQuote MyQuote)
1314
1315 (test "macro defining a toplevel macro" '(1 2 3)
1316 (lambda () (MyQuote (1 2 3))))
1317
1318 ;; Macro inserting toplevel identifier
1319 (define-module defFoo-test
1320 (export defFoo)
1321 (define-syntax defFoo
1322 (syntax-rules ()
1323 [(_ accessor)
1324 (begin
1325 (define foo-toplevel 42)
1326 (define (accessor) foo-toplevel))])))
1327
1328 (import defFoo-test)
1329 (defFoo get-foo)
1330
1331 (test "macro injecting toplevel definition" '(#f #f 42)
1332 (lambda ()
1333 (list (module-binding-ref (current-module) 'foo-toplevel #f)
1334 (module-binding-ref 'defFoo-test 'foo-toplevel #f)
1335 (get-foo))))
1336
1337 ;; recursive reference in macro-defined-macro
1338 ;; https://gist.github.com/ktakashi/03ae059f804a723a9589
1339 (define-syntax assocm
1340 (syntax-rules ()
1341 ((_ key (alist ...))
1342 (letrec-syntax ((fooj (syntax-rules (key)
1343 ((_ (key . e) res (... ...)) '(key . e))
1344 ((_ (a . d) res (... ...)) (fooj res (... ...))))))
1345 (fooj alist ...)))))
1346
1347 (test "recursive reference in macro-defined-macro" '(c . d)
1348 (lambda () (assocm c ((a . b) (b . d) (c . d) (d . d)))))
1349
1350 ;; literal identifier comparison with renamed identifier
1351 ;; https://gist.github.com/ktakashi/fa4ee23da88151536619
1352 (define-module literal-id-test-sub
1353 (export car))
1354
1355 (define-module literal-id-test
1356 (use gauche.test)
1357 (import (literal-id-test-sub :rename ((car car-alias))))
1358
1359 (define-syntax free-identifier=??
1360 (syntax-rules ()
1361 ((_ a b)
1362 (let-syntax ((foo (syntax-rules (a)
1363 ((_ a) #t)
1364 ((_ _) #f))))
1365 (foo b)))))
1366
1367 (test "literal identifier comparison a a" #t
1368 (lambda () (free-identifier=?? a a)))
1369 (test "literal identifier comparison b a" #f
1370 (lambda () (free-identifier=?? b a)))
1371 (test "literal identifier comparison car car-alias" #t
1372 (lambda () (free-identifier=?? car car-alias))))
1373
1374 ;; macro defining macro from other module
1375 ;; https://github.com/shirok/Gauche/issues/532
1376
1377 (define-module macro-defining-macro-toplevel
1378 (export x1)
1379 (define-syntax x1
1380 (syntax-rules ()
1381 ((x1 y1)
1382 (x2 x3 y1))))
1383
1384 (define-syntax x2
1385 (syntax-rules ()
1386 ((x2 x3 y1)
1387 (begin
1388 (define-syntax x3
1389 (syntax-rules ()
1390 ((x3 x4) x4)))
1391 (define-syntax y1
1392 (syntax-rules ()
1393 ((y1 y2) (x3 y2)))))))))
1394
1395 (define-module macro-defining-macro-toplevel-user
1396 (use gauche.test)
1397 (import macro-defining-macro-toplevel)
1398 (x1 bar)
1399 ;; without fix, (bar 1) fails with "unbound variable: #<identifier ... x3>"
1400 (test "macro defining macro in other module" 1
1401 (lambda () (eval '(bar 1) (current-module)))))
1402
1403 ;;----------------------------------------------------------------------
1404 ;; identifier comparison
1405
1406 (test-section "identifier comparison")
1407
1408 ;; This is EXPERIMENTAL: may be changed in later release.
1409 (define-syntax expand-id-compare (syntax-rules () ((hoge foo ...) (cdr b))))
1410 (test "comparison of identifiers" '(cdr b)
1411 (lambda () (macroexpand '(expand-id-compare bar) #t)))
1412 (test "comparison of identifiers" (macroexpand '(expand-id-compare bar) #t)
1413 (lambda () (macroexpand '(expand-id-compare bar) #t)))
1414
1415 ;;----------------------------------------------------------------------
1416 ;; keyword and extended lambda list
1417
1418 (test-section "keyword inserted by macro")
1419
1420 (define-syntax define-extended-1
1421 (syntax-rules ()
1422 [(_ name)
1423 (define (name a :key (b #f))
1424 (list a b))]))
1425
1426 (define-extended-1 extended-1)
1427 (test "macro expands to extended lambda list" '(1 2)
1428 (lambda () (extended-1 1 :b 2)))
1429
1430 (define-syntax define-extended-2
1431 (syntax-rules ()
1432 [(_ name)
1433 (define (name a :key ((:b boo) #f))
1434 (list a boo))]))
1435 (define-extended-2 extended-2)
1436 (test "macro expands to extended lambda list" '(3 4)
1437 (lambda () (extended-2 3 :b 4)))
1438
1439 ;;----------------------------------------------------------------------
1440 ;; common-macros
1441
1442 (test-section "common-macros utilities")
1443
1444 (test "push!" '(1 2 3)
1445 (lambda ()
1446 (let ((a '()))
1447 (push! a 3) (push! a 2) (push! a 1)
1448 a)))
1449
1450 (test "push!" '(0 1 2 3)
1451 (lambda ()
1452 (let ((a (list 0)))
1453 (push! (cdr a) 3) (push! (cdr a) 2) (push! (cdr a) 1)
1454 a)))
1455
1456 (test "push!" '#((1 2) (3 . 0))
1457 (lambda ()
1458 (let ((a (vector '() 0)))
1459 (push! (vector-ref a 0) 2)
1460 (push! (vector-ref a 0) 1)
1461 (push! (vector-ref a 1) 3)
1462 a)))
1463
1464 (test "push-unique!" '(3 2 1)
1465 (lambda ()
1466 (let ((a '(1)))
1467 (push-unique! a 1)
1468 (push-unique! a 2)
1469 (push-unique! a 3)
1470 (push-unique! a 2)
1471 (push-unique! a 1)
1472 (push-unique! a 3)
1473 a)))
1474
1475 (test "push-unique!" '#((3 2 1) (3 1 2))
1476 (lambda ()
1477 (let ((a (vector '() '())))
1478 (push-unique! (vector-ref a 0) 1)
1479 (push-unique! (vector-ref a 0) 2)
1480 (push-unique! (vector-ref a 0) 3)
1481 (push-unique! (vector-ref a 0) 2)
1482 (push-unique! (vector-ref a 0) 1)
1483 (push-unique! (vector-ref a 0) 3)
1484 (push-unique! (vector-ref a 1) 2)
1485 (push-unique! (vector-ref a 1) 1)
1486 (push-unique! (vector-ref a 1) 3)
1487 (push-unique! (vector-ref a 1) 1)
1488 a)))
1489
1490 (test "push-unique!" '("a" "B" "c")
1491 (lambda ()
1492 (let ((a '("c")))
1493 (push-unique! a "B" string-ci=?)
1494 (push-unique! a "C" string-ci=?)
1495 (push-unique! a "a" string-ci=?)
1496 (push-unique! a "b" string-ci=?)
1497 (push-unique! a "A" string-ci=?)
1498 a)))
1499
1500 (test "push-unique!" '(("a" "B" "c"))
1501 (lambda ()
1502 (let ((a (list '("c"))))
1503 (push-unique! (car a) "B" string-ci=?)
1504 (push-unique! (car a) "C" string-ci=?)
1505 (push-unique! (car a) "a" string-ci=?)
1506 (push-unique! (car a) "b" string-ci=?)
1507 (push-unique! (car a) "A" string-ci=?)
1508 a)))
1509
1510 (test "pop!" '((2 3) . 1)
1511 (lambda ()
1512 (let* ((a (list 1 2 3))
1513 (b (pop! a)))
1514 (cons a b))))
1515
1516 (test "pop!" '((1 3) . 2)
1517 (lambda ()
1518 (let* ((a (list 1 2 3))
1519 (b (pop! (cdr a))))
1520 (cons a b))))
1521
1522 (test "pop!" '(#((2)) . 1)
1523 (lambda ()
1524 (let* ((a (vector (list 1 2)))
1525 (b (pop! (vector-ref a 0))))
1526 (cons a b))))
1527
1528 (test "push!, pop!" '((2 3) (4 1))
1529 (lambda ()
1530 (let ((a (list 1 2 3))
1531 (b (list 4)))
1532 (push! (cdr b) (pop! a))
1533 (list a b))))
1534
1535 (test "inc!" 3
1536 (lambda () (let ((x 2)) (inc! x) x)))
1537 (test "inc!" 4
1538 (lambda () (let ((x 2)) (inc! x 2) x)))
1539 (test "inc!" '(4 . 1)
1540 (lambda ()
1541 (let ((x (cons 3 1)))
1542 (inc! (car x)) x)))
1543 (test "inc!" '(1 . 1)
1544 (lambda ()
1545 (let ((x (cons 3 1)))
1546 (inc! (car x) -2) x)))
1547 (test "inc!" '((4 . 1) 1)
1548 (lambda ()
1549 (let ((x (cons 3 1))
1550 (y 0))
1551 (define (zz) (inc! y) car)
1552 (inc! ((zz) x))
1553 (list x y))))
1554 (test "dec!" 1
1555 (lambda () (let ((x 2)) (dec! x) x)))
1556 (test "dec!" 0
1557 (lambda () (let ((x 2)) (dec! x 2) x)))
1558 (test "dec!" '(2 . 1)
1559 (lambda ()
1560 (let ((x (cons 3 1)))
1561 (dec! (car x)) x)))
1562 (test "dec!" '(5 . 1)
1563 (lambda ()
1564 (let ((x (cons 3 1)))
1565 (dec! (car x) -2) x)))
1566 (test "dec!" '((2 . 1) -1)
1567 (lambda ()
1568 (let ((x (cons 3 1))
1569 (y 0))
1570 (define (zz) (dec! y) car)
1571 (dec! ((zz) x))
1572 (list x y))))
1573
1574 (test "rotate!" '(2 1)
1575 (lambda ()
1576 (let ((a 1)
1577 (b 2))
1578 (rotate! a b)
1579 (list a b))))
1580 (test "rotate!" '(2 3 1)
1581 (lambda ()
1582 (let ((a 1)
1583 (b 2)
1584 (c 3))
1585 (rotate! a b c)
1586 (list a b c))))
1587 (test "rotate!" '(2 3 1)
1588 (lambda ()
1589 (let ((vals (list 1 2 3)))
1590 (rotate! (car vals) (cadr vals) (list-ref vals 2))
1591 vals)))
1592
1593 (test "dotimes" '(0 1 2 3 4 5 6 7 8 9)
1594 (lambda ()
1595 (let ((m '()))
1596 (dotimes (n 10) (push! m n))
1597 (reverse m))))
1598 (test "dotimes" '(0 1 2 3 4 5 6 7 8 9)
1599 (lambda ()
1600 (let ((m '()))
1601 (dotimes (n 10 (reverse m)) (push! m n)))))
1602 (test "dotimes" '(0 1 2 3 4 5 6 7 8 9)
1603 (lambda ()
1604 (let ((m '()))
1605 (dotimes (n (if (null? m) 10 (error "Boom!")) (reverse m))
1606 (push! m n)))))
1607
1608 (test "while" 9
1609 (lambda ()
1610 (let ((a 10)
1611 (b 0))
1612 (while (positive? (dec! a))
1613 (inc! b))
1614 b)))
1615 (test "while" 0
1616 (lambda ()
1617 (let ((a -1)
1618 (b 0))
1619 (while (positive? (dec! a))
1620 (inc! b))
1621 b)))
1622
1623 (test "while =>" 6
1624 (lambda ()
1625 (let ((a '(1 2 3 #f))
1626 (b 0))
1627 (while (pop! a)
1628 => val
1629 (inc! b val))
1630 b)))
1631
1632 (test "while => guard" 45
1633 (lambda ()
1634 (let ((a 10)
1635 (b 0))
1636 (while (dec! a)
1637 positive? => val
1638 (inc! b a))
1639 b)))
1640
1641 (test "until" 10
1642 (lambda ()
1643 (let ((a 10) (b 0))
1644 (until (negative? (dec! a))
1645 (inc! b))
1646 b)))
1647 (test "until => guard" 45
1648 (lambda ()
1649 (let ((a 10) (b 0))
1650 (until (dec! a)
1651 negative? => val
1652 (inc! b a))
1653 b)))
1654
1655 (test "values-ref" 3
1656 (lambda ()
1657 (values-ref (quotient&remainder 10 3) 0)))
1658 (test "values-ref" 1
1659 (lambda ()
1660 (values-ref (quotient&remainder 10 3) 1)))
1661 (test "values-ref" 'e
1662 (lambda ()
1663 (values-ref (values 'a 'b 'c 'd 'e) 4)))
1664 (test "values-ref" '(d b)
1665 (lambda ()
1666 (receive r
1667 (values-ref (values 'a 'b 'c 'd 'e) 3 1)
1668 r)))
1669 (test "values-ref" '(d a b)
1670 (lambda ()
1671 (receive r
1672 (values-ref (values 'a 'b 'c 'd 'e) 3 0 1)
1673 r)))
1674 (test "values-ref" '(e d c b a)
1675 (lambda ()
1676 (receive r
1677 (values-ref (values 'a 'b 'c 'd 'e) 4 3 2 1 0)
1678 r)))
1679
1680 (test "values->list" '(3 1)
1681 (lambda () (values->list (quotient&remainder 10 3))))
1682 (test "values->list" '(1)
1683 (lambda () (values->list 1)))
1684 (test "values->list" '()
1685 (lambda () (values->list (values))))
1686
1687 (test "let1" '(2 2 2)
1688 (lambda () (let1 x (+ 1 1) (list x x x))))
1689 (test "let1" '(2 4)
1690 (lambda () (let1 x (+ 1 1) (list x (let1 x (+ x x) x)))))
1691
1692 (test "rlet1" 1 (lambda () (rlet1 x (/ 2 2) (+ x x))))
1693
1694 (test "if-let1" 4
1695 (lambda () (if-let1 it (+ 1 1) (* it 2))))
1696 (test "if-let1" 'bar
1697 (lambda () (if-let1 it (memq 'a '(b c d)) 'boo 'bar)))
1698
1699 (test "let-values" '(2 1 1 (2) (2 1))
1700 (lambda () (let ([a 1] [b 2])
1701 (let-values ([(a b) (values b a)]
1702 [(c . d) (values a b)]
1703 [e (values b a)])
1704 (list a b c d e)))))
1705
1706 (test "let*-values" '(2 1 2 (1) (1 2))
1707 (lambda () (let ([a 1] [b 2])
1708 (let*-values ([(a b) (values b a)]
1709 [(c . d) (values a b)]
1710 [e (values b a)])
1711 (list a b c d e)))))
1712
1713 (test "ecase" 'b
1714 (lambda () (ecase 3 ((1) 'a) ((2 3) 'b) ((4) 'c))))
1715 (test "ecase" (test-error)
1716 (lambda () (ecase 5 ((1) 'a) ((2 3) 'b) ((4) 'c))))
1717 (test "ecase" 'd
1718 (lambda () (ecase 5 ((1) 'a) ((2 3) 'b) ((4) 'c) (else 'd))))
1719
1720 (test "$" '(0 1)
1721 (lambda () ($ list 0 1)))
1722 (test "$" '(0 1 (2 3 (4 5 (6 7))))
1723 (lambda () ($ list 0 1 $ list 2 3 $ list 4 5 $ list 6 7)))
1724 (test "$ - $*" '(0 1 (2 3 4 5 6 7))
1725 (lambda () ($ list 0 1 $ list 2 3 $* list 4 5 $* list 6 7)))
1726 (test "$ - $*" '(0 1 2 3 (4 5 6 7))
1727 (lambda () ($ list 0 1 $* list 2 3 $ list 4 5 $* list 6 7)))
1728 (test "$ - $*" '(0 1 2 3 4 5 (6 7))
1729 (lambda () ($ list 0 1 $* list 2 3 $* list 4 5 $ list 6 7)))
1730 (test "$ - partial" '(0 1 (2 3 (4 5 a)))
1731 (lambda () (($ list 0 1 $ list 2 3 $ list 4 5 $) 'a)))
1732 (test "$ - $* - partial" '(0 1 2 3 4 5 a)
1733 (lambda () (($ list 0 1 $* list 2 3 $* list 4 5 $) 'a)))
1734 (test "$ - $* - partial" '(0 1 (2 3 (4 5 a b)))
1735 (lambda () (($ list 0 1 $ list 2 3 $ list 4 5 $*) 'a 'b)))
1736
1737 (test "$ - hygienty" `(0 1 a ,list 2 3 b ,list 4 5)
1738 (lambda ()
1739 (let-syntax ([$$ (syntax-rules ()
1740 [($$ . xs) ($ . xs)])])
1741 (let ([$ 'a] [$* 'b])
1742 ($$ list 0 1 $ list 2 3 $* list 4 5)))))
1743
1744 (test* "cond-list" '() (cond-list))
1745 (test* "cond-list" '(a) (cond-list ('a)))
1746 (test* "cond-list" '(a) (cond-list (#t 'a) (#f 'b)))
1747 (test* "cond-list" '(b) (cond-list (#f 'a) (#t 'b)))
1748 (test* "cond-list" '(a b d) (cond-list (#t 'a) (#t 'b) (#f 'c) (#t 'd)))
1749 (test* "cond-list" '((b)) (cond-list (#f 'a) ('b => list)))
1750 (test* "cond-list" '(a b c d x)
1751 (cond-list (#t @ '(a b)) (#t @ '(c d)) (#f @ '(e f))
1752 ('x => @ list)))
1753
1754 ;;----------------------------------------------------------------------
1755 ;; macro-expand
1756
1757 (test-section "macroexpand")
1758
1759 (define-macro (foo x) `(bar ,x ,x))
1760 (define-macro (bar x y) `(list ,x ,x ,y ,y))
1761
1762 (test "macroexpand" '(list 1 1 1 1)
1763 (lambda () (macroexpand '(foo 1))))
1764 (test "macroexpand-1" '(bar 1 1)
1765 (lambda () (macroexpand-1 '(foo 1))))
1766
1767 ;;----------------------------------------------------------------------
1768 ;; not allowing first-class macro
1769
1770 (test-section "failure cases")
1771
1772 (define-macro (bad-if a b c) `(,if ,a ,b ,c))
1773 (test "reject first-class syntax usage" (test-error)
1774 (lambda () (bad-if #t 'a 'b)))
1775
1776 (define-macro (bad-fi a b c) `(,fi ,a ,b ,c))
1777 (test "reject first-class macro usage" (test-error)
1778 (lambda () (bad-fi #t 'a 'b)))
1779
1780 ;;----------------------------------------------------------------------
1781 ;; compiler macros
1782
1783 (test-section "define-hybrid-syntax")
1784
1785 (define-hybrid-syntax cpm
1786 (lambda (a b) (+ a b))
1787 (er-macro-transformer
1788 (lambda (f r c) `(,(r '*) ,(cadr f) ,(caddr f)))))
1789 (test "compiler macro" '(6 5 6)
1790 (lambda ()
1791 (list (cpm 2 3)
1792 (apply cpm '(2 3))
1793 (let ((* -)) (cpm 2 3)))))
1794
1795 ;;----------------------------------------------------------------------
1796 ;; syntax error
1797
1798 (test-section "syntax-error")
1799
1800 (define-syntax test-syntax-error
1801 (syntax-rules ()
1802 [(_ a) 'ok]
1803 [(_ a b) (syntax-errorf "bad {~a ~a}" a b)]
1804 [(_ x ...) (syntax-error "bad number of arguments" x ...)]))
1805
1806 ;; NB: These tests depends on the fact that the compile "wraps"
1807 ;; the error by <compile-error-mixin> in order. If the compiler changes
1808 ;; the error handling, adjust the tests accordingly.
1809 ;; Our purpose here is to make sure syntax-error preserves the offending macro
1810 ;; call (test-syntax-error ...).
1811 (test "syntax-error"
1812 '("bad number of arguments x y z"
1813 (test-syntax-error x y z)
1814 (list (test-syntax-error x y z)))
1815 (lambda ()
1816 (guard [e (else (let1 xs (filter <compile-error-mixin>
1817 (slot-ref e '%conditions))
1818 (cons (condition-message e e)
1819 (map (lambda (x) (slot-ref x 'expr)) xs))))]
1820 (eval '(list (test-syntax-error x y z))
1821 (interaction-environment)))))
1822 (test "syntax-errorf"
1823 '("bad {x y}"
1824 (test-syntax-error x y)
1825 (list (test-syntax-error x y)))
1826 (lambda ()
1827 (guard [e (else (let1 xs (filter <compile-error-mixin>
1828 (slot-ref e '%conditions))
1829 (cons (condition-message e e)
1830 (map (lambda (x) (slot-ref x 'expr)) xs))))]
1831 (eval '(list (test-syntax-error x y))
1832 (interaction-environment)))))
1833
1834 ;;----------------------------------------------------------------------
1835 ;; 'compare-ellipsis-1' test should output the following error.
1836 ;;
1837 ;; *** ERROR: in definition of macro mac-sub1:
1838 ;; template's ellipsis nesting is deeper than pattern's:
1839 ;; (#<identifier user#list.2d80660> #<identifier user#x.2d80690>
1840 ;; #<identifier user#ooo.2d806f0>)
1841 ;;
1842 ;; 'compare-ellipsis-2' test should output the following error.
1843 ;;
1844 ;; *** ERROR: in definition of macro mac-sub1:
1845 ;; template's ellipsis nesting is deeper than pattern's:
1846 ;; (#<identifier user#list.2969870> #<identifier user#x.29698a0>
1847 ;; #<identifier user#ooo.2969900>)
1848
1849 (test-section "compare ellipsis")
1850
1851 (define-syntax ell-test
1852 (syntax-rules (ooo)
1853 ((_ zzz)
1854 (let-syntax
1855 ((mac-sub1
1856 (syntax-rules ooo ()
1857 ((_ x zzz)
1858 (list x ooo)))))
1859 (mac-sub1 1 2 3)))))
1860
1861 (test* "compare-ellipsis-1"
1862 (test-error <error> #/^in definition of macro/)
1863 (eval
1864 '(ell-test ooo)
1865 (interaction-environment)))
1866
1867 (test* "compare-ellipsis-2"
1868 (test-error <error> #/^in definition of macro/)
1869 (eval
1870 '(let ((ooo 'yyy)) (ell-test ooo))
1871 (interaction-environment)))
1872
1873 ;;----------------------------------------------------------------------
1874 ;; 'compare-literals-2' test should output the following error.
1875 ;;
1876 ;; *** ERROR: malformed #<identifier user#lit-test-2.29d4060>:
1877 ;; (#<identifier user#lit-test-2.29d4060> #<identifier user#temp.29d40c0>)
1878 ;; While compiling: (lit-test-2 temp 1)
1879
1880 (test-section "compare literals")
1881
1882 (define-syntax lit-test-1
1883 (syntax-rules (temp)
1884 ((_ temp x)
1885 (lit-test-1 temp))
1886 ((_ temp)
1887 'passed)))
1888
1889 (test* "compare-literals-1" 'passed (lit-test-1 temp 1))
1890
1891 (define-syntax lit-test-2
1892 (syntax-rules (temp)
1893 ((_ temp x)
1894 (let ((temp 100))
1895 (lit-test-2 temp)))
1896 ((_ temp)
1897 'failed)))
1898
1899 (test* "compare-literals-2"
1900 (test-error <error> #/^malformed/)
1901 (eval '(lit-test-2 temp 1) (interaction-environment)))
1902
1903 ;;----------------------------------------------------------------------
1904 ;; 'generate-underbar-1' inserts global underbar into the macro output.
1905 ;; It shouldn't be regarded as a pattern variable, so the underbar in
1906 ;; the template refers to the global binding of '_'.
1907
1908 (test-section "generate underbar")
1909
1910 (define-syntax gen-underbar
1911 (syntax-rules (_)
1912 ((gen-underbar)
1913 (let-syntax
1914 ((mac-sub1
1915 (syntax-rules ()
1916 ((mac-sub1 _)
1917 _))))
1918 (mac-sub1 'failed)))))
1919
1920 (test* "generate-underbar-1" _
1921 (gen-underbar))
1922
1923 ;;----------------------------------------------------------------------
1924 ;; 'pattern-variables-1' test should output the following error.
1925 ;;
1926 ;; *** ERROR: too many pattern variables in the macro definition of pat-vars
1927 ;; While compiling: (syntax-rules () ((_ (z1 (x1 x2 x3 x4 x5 x6 x7 x8 x9 x10
1928 ;; x11 x12 x13 x14 x15 x16 x17 x ...
1929 ;; While compiling: (define-syntax pat-vars (syntax-rules () ((_ (z1 (x1 x2 x3
1930 ;; x4 x5 x6 x7 x8 x9 x10 x11 x ...
1931
1932 (test-section "pattern variables check")
1933
1934 (test* "pattern-variables-1"
1935 (test-error <error> #/^Too many pattern variables/)
1936 (eval
1937 '(define-syntax pat-vars
1938 (syntax-rules ()
1939 ((_ (z1 (x1 x2 x3 x4 x5 x6 x7 x8 x9 x10
1940 x11 x12 x13 x14 x15 x16 x17 x18 x19 x20
1941 x21 x22 x23 x24 x25 x26 x27 x28 x29 x30
1942 x31 x32 x33 x34 x35 x36 x37 x38 x39 x40
1943 x41 x42 x43 x44 x45 x46 x47 x48 x49 x50
1944 x51 x52 x53 x54 x55 x56 x57 x58 x59 x60
1945 x61 x62 x63 x64 x65 x66 x67 x68 x69 x70
1946 x71 x72 x73 x74 x75 x76 x77 x78 x79 x80
1947 x81 x82 x83 x84 x85 x86 x87 x88 x89 x90
1948 x91 x92 x93 x94 x95 x96 x97 x98 x99 x100
1949 x101 x102 x103 x104 x105 x106 x107 x108 x109 x110
1950 x111 x112 x113 x114 x115 x116 x117 x118 x119 x120
1951 x121 x122 x123 x124 x125 x126 x127 x128 x129 x130
1952 x131 x132 x133 x134 x135 x136 x137 x138 x139 x140
1953 x141 x142 x143 x144 x145 x146 x147 x148 x149 x150
1954 x151 x152 x153 x154 x155 x156 x157 x158 x159 x160
1955 x161 x162 x163 x164 x165 x166 x167 x168 x169 x170
1956 x171 x172 x173 x174 x175 x176 x177 x178 x179 x180
1957 x181 x182 x183 x184 x185 x186 x187 x188 x189 x190
1958 x191 x192 x193 x194 x195 x196 x197 x198 x199 x200
1959 x201 x202 x203 x204 x205 x206 x207 x208 x209 x210
1960 x211 x212 x213 x214 x215 x216 x217 x218 x219 x220
1961 x221 x222 x223 x224 x225 x226 x227 x228 x229 x230
1962 x231 x232 x233 x234 x235 x236 x237 x238 x239 x240
1963 x241 x242 x243 x244 x245 x246 x247 x248 x249 x250
1964 x251 x252 x253 x254 x255 x256)))
1965 (print z1 " " x255 " " x256))))
1966 (interaction-environment)))
1967
1968 (test* "pattern-variables-2"
1969 (test-error <error> #/^Pattern levels too deeply nested/)
1970 (let ()
1971 (define (build-deep-nested-pattern n f)
1972 (if (= n 0)
1973 `(define-syntax pat-vars
1974 (syntax-rules ()
1975 ((_ ,f)
1976 (quote ,f))))
1977 (build-deep-nested-pattern (- n 1) `(,f ...))))
1978 (eval
1979 (build-deep-nested-pattern 256 'x)
1980 (interaction-environment))))
1981
1982 ;;----------------------------------------------------------------------
1983 ;; let-keyword* hygienic expansion
1984 ;;
1985
1986 (test-section "hygienic extened-lambda expansion")
1987 (define-module let-keyword-hygiene-def
1988 (use gauche.base)
1989 (use util.match)
1990 (export klambda)
1991 (extend scheme)
1992 (define-syntax klambda
1993 (er-macro-transformer
1994 (^[f r c]
1995 (match f
1996 [(_ formals&keys . body)
1997 (quasirename r
1998 `(lambda (,@(drop-right formals&keys 1)
1999 ,(make-keyword 'key)
2000 ,@(map (^s `(,s #f)) (last formals&keys)))
2001 ,@body))])))))
2002
2003 (define-module let-keyword-hygeiene-use
2004 (import let-keyword-hygiene-def)
2005 (import gauche.keyword)
2006 (export call-klambda)
2007 (extend scheme)
2008 (define (call-klambda a b c d)
2009 ((klambda (a b (x y)) (list a b x y))
2010 a b :x c :y d)))
2011
2012 (test* "hygienic let-keyword expansion" '(1 2 3 4)
2013 ((with-module let-keyword-hygeiene-use call-klambda) 1 2 3 4))
2014
2015 ;; Cf. http://chaton.practical-scheme.net/gauche/a/2020/11/05#entry-5fa3ba50-dc7d3
2016 (define-syntax let-keywords-hygiene-test-1-inner
2017 (er-macro-transformer
2018 (^[f r c]
2019 (let-keywords (cdr f) ([a 1]
2020 [b 2])
2021 (quasirename r `(+ ,a ,b))))))
2022 (define-syntax let-keywords-hygiene-test-1-outer
2023 (syntax-rules ()
2024 [(_ x) (let-keywords-hygiene-test-1-inner :b x)]))
2025
2026 (test* "hygienic let-keyword match" 10
2027 (let-keywords-hygiene-test-1-outer 9))
2028
2029
2030 ;;----------------------------------------------------------------------
2031 ;; SRFI-147 begin
2032 ;; (not yest supported)
2033
2034 '(test-section "SRFI-147 begin")
2035
2036 '(test "SRFI-147 begin (internal) 1"
2037 '(yes no)
2038 (lambda ()
2039 (define-syntax foo
2040 (begin (define-syntax bar if)
2041 (syntax-rules ()
2042 [(_ x y z) (bar z x y)])))
2043 (list (foo 'yes 'no (zero? 0))
2044 (foo 'yes 'no (zero? 1)))))
2045
2046 '(test "SRFI-147 begin (internal) 2"
2047 11
2048 (lambda ()
2049 (let-syntax ([foo (syntax-rules ()
2050 [(_ a) (begin (define x (* a 2))
2051 (syntax-rules ()
2052 [(_ b) (+ b x)]))])])
2053 (define-syntax bar (foo 3))
2054 (bar 5))))
2055
2056 (test-end)