Debian Codegraph @

test-suite/tests/syntax.test

raw
1 ;;;; syntax.test --- test suite for Guile's syntactic forms -*- scheme -*-
2 ;;;;
3 ;;;; Copyright (C) 2001, 2003, 2004, 2005, 2006, 2009, 2010,
4 ;;;; 2011, 2012, 2013, 2014, 2021, 2024 Free Software Foundation, Inc.
5 ;;;;
6 ;;;; This library is free software; you can redistribute it and/or
7 ;;;; modify it under the terms of the GNU Lesser General Public
8 ;;;; License as published by the Free Software Foundation; either
9 ;;;; version 3 of the License, or (at your option) any later version.
10 ;;;;
11 ;;;; This library is distributed in the hope that it will be useful,
12 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 ;;;; Lesser General Public License for more details.
15 ;;;;
16 ;;;; You should have received a copy of the GNU Lesser General Public
17 ;;;; License along with this library; if not, write to the Free Software
18 ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
20 (define-module (test-suite test-syntax)
21 #:use-module (ice-9 regex)
22 #:use-module (ice-9 local-eval)
23 #:use-module ((system syntax) #:select (syntax?))
24 #:use-module (test-suite lib))
25
26
27 (define exception:generic-syncase-error
28 "source expression failed to match")
29 (define exception:unexpected-syntax
30 "unexpected syntax")
31
32 (define exception:bad-expression
33 "Bad expression")
34
35 (define exception:missing/extra-expr
36 "Missing or extra expression")
37 (define exception:missing-expr
38 "Missing expression")
39 (define exception:empty-body
40 "empty body")
41 (define exception:body-should-end-with-expr
42 "body should end with an expression")
43 (define exception:extra-expr
44 "Extra expression")
45 (define exception:illegal-empty-combination
46 "Illegal empty combination")
47
48 (define exception:bad-lambda
49 "bad lambda")
50 (define exception:bad-let
51 "bad let$")
52 (define exception:bad-letrec
53 "bad letrec$")
54 (define exception:bad-letrec*
55 "bad letrec\\*$")
56 (define exception:bad-set!
57 "bad set!")
58 (define exception:bad-quote
59 '(quote . "bad syntax"))
60 (define exception:bad-bindings
61 "Bad bindings")
62 (define exception:bad-binding
63 "Bad binding")
64 (define exception:duplicate-binding
65 "duplicate bound variable")
66 (define exception:bad-body
67 "^bad body")
68 (define exception:bad-formals
69 "invalid argument list")
70 (define exception:bad-formal
71 "Bad formal")
72 (define exception:duplicate-formals
73 "duplicate identifier in argument list")
74
75 (define exception:missing-clauses
76 "Missing clauses")
77 (define exception:misplaced-else-clause
78 "Misplaced else clause")
79 (define exception:bad-case-clause
80 "Bad case clause")
81 (define exception:bad-case-labels
82 "Bad case labels")
83 (define exception:bad-cond-clause
84 "Bad cond clause")
85
86 (define exception:too-many-args
87 "too many arguments")
88 (define exception:wrong-number-of-values
89 '(wrong-number-of-args . "number of (values)|(arguments)"))
90 (define exception:zero-expression-sequence
91 "sequence of zero expressions")
92
93 (define exception:variable-ref
94 '(misc-error . "Unbound variable"))
95
96 ;; (put 'pass-if-syntax-error 'scheme-indent-function 1)
97 (define-syntax pass-if-syntax-error
98 (syntax-rules ()
99 ((_ name pat exp)
100 (pass-if name
101 (catch 'syntax-error
102 (lambda () exp (error "expected syntax-error exception"))
103 (lambda (k who what where form . maybe-subform)
104 (if (if (pair? pat)
105 (and (eq? who (car pat))
106 (string-match (cdr pat) what))
107 (string-match pat what))
108 #t
109 (error "unexpected syntax-error exception" what pat))))))))
110
111 (with-test-prefix "expressions"
112
113 (with-test-prefix "Bad argument list"
114
115 (pass-if-equal "syntax-error location"
116 '((filename . "example.scm") (line . 1) (column . 2))
117 (catch 'syntax-error
118 (lambda ()
119 (eval (call-with-input-string "\n (let foo bar)"
120 (lambda (port)
121 (set-port-filename! port "example.scm")
122 (read port)))
123 (interaction-environment)))
124 (lambda (key proc message properties form subform . rest)
125 properties)))
126
127 (pass-if-syntax-error "improper argument list of length 1"
128 exception:generic-syncase-error
129 (eval '(let ((foo (lambda (x y) #t)))
130 (foo . 1))
131 (interaction-environment)))
132
133 (pass-if-syntax-error "improper argument list of length 2"
134 exception:generic-syncase-error
135 (eval '(let ((foo (lambda (x y) #t)))
136 (foo 1 . 2))
137 (interaction-environment))))
138
139 (with-test-prefix "missing or extra expression"
140
141 ;; R5RS says:
142 ;; *Note:* In many dialects of Lisp, the empty combination, (),
143 ;; is a legitimate expression. In Scheme, combinations must
144 ;; have at least one subexpression, so () is not a syntactically
145 ;; valid expression.
146
147 ;; Fixed on 2001-3-3
148 (pass-if-syntax-error "empty parentheses \"()\""
149 exception:unexpected-syntax
150 (eval '()
151 (interaction-environment)))))
152
153 (with-test-prefix "quote"
154 #t)
155
156 (with-test-prefix "quasiquote"
157
158 (with-test-prefix "unquote"
159
160 (pass-if "repeated execution"
161 (let ((foo (let ((i 0)) (lambda () (set! i (+ i 1)) `(,i)))))
162 (and (equal? (foo) '(1)) (equal? (foo) '(2))))))
163
164 (with-test-prefix "unquote-splicing"
165
166 (pass-if "extra arguments"
167 (equal? (eval '(quasiquote ((unquote-splicing (list 1 2) (list 3 4))))
168 (interaction-environment))
169 '(1 2 3 4)))))
170
171 (with-test-prefix "begin"
172
173 (pass-if "valid (begin)"
174 (eval '(begin (begin) #t) (interaction-environment)))
175
176 (if (not (include-deprecated-features))
177 (pass-if-syntax-error "invalid (begin)"
178 exception:zero-expression-sequence
179 (eval '(begin (if #t (begin)) #t) (interaction-environment)))))
180
181 (define-syntax matches?
182 (syntax-rules (<>)
183 ((_ (op arg ...) pat) (let ((x (op arg ...)))
184 (matches? x pat)))
185 ((_ x ()) (null? x))
186 ((_ x (a . b)) (and (pair? x)
187 (matches? (car x) a)
188 (matches? (cdr x) b)))
189 ((_ x <>) #t)
190 ((_ x pat) (equal? x 'pat))))
191
192 (with-test-prefix "lambda"
193
194 (with-test-prefix "bad formals"
195
196 (pass-if-syntax-error "(lambda)"
197 exception:bad-lambda
198 (eval '(lambda)
199 (interaction-environment)))
200
201 (pass-if-syntax-error "(lambda . \"foo\")"
202 exception:bad-lambda
203 (eval '(lambda . "foo")
204 (interaction-environment)))
205
206 (pass-if-syntax-error "(lambda \"foo\")"
207 exception:bad-lambda
208 (eval '(lambda "foo")
209 (interaction-environment)))
210
211 (pass-if-syntax-error "(lambda \"foo\" #f)"
212 exception:bad-formals
213 (eval '(lambda "foo" #f)
214 (interaction-environment)))
215
216 (pass-if-syntax-error "(lambda (x 1) 2)"
217 exception:bad-formals
218 (eval '(lambda (x 1) 2)
219 (interaction-environment)))
220
221 (pass-if-syntax-error "(lambda (1 x) 2)"
222 exception:bad-formals
223 (eval '(lambda (1 x) 2)
224 (interaction-environment)))
225
226 (pass-if-syntax-error "(lambda (x \"a\") 2)"
227 exception:bad-formals
228 (eval '(lambda (x "a") 2)
229 (interaction-environment)))
230
231 (pass-if-syntax-error "(lambda (\"a\" x) 2)"
232 exception:bad-formals
233 (eval '(lambda ("a" x) 2)
234 (interaction-environment))))
235
236 (with-test-prefix "duplicate formals"
237
238 ;; Fixed on 2001-3-3
239 (pass-if-syntax-error "(lambda (x x) 1)"
240 exception:duplicate-formals
241 (eval '(lambda (x x) 1)
242 (interaction-environment)))
243
244 ;; Fixed on 2001-3-3
245 (pass-if-syntax-error "(lambda (x x x) 1)"
246 exception:duplicate-formals
247 (eval '(lambda (x x x) 1)
248 (interaction-environment))))
249
250 (with-test-prefix "bad body"
251
252 (pass-if-syntax-error "(lambda ())"
253 exception:bad-lambda
254 (eval '(lambda ())
255 (interaction-environment)))))
256
257 (with-test-prefix "let"
258
259 (with-test-prefix "bindings"
260
261 (pass-if-exception "late binding"
262 exception:unbound-var
263 (let ((x 1) (y x)) y)))
264
265 (with-test-prefix "bad bindings"
266
267 (pass-if-syntax-error "(let)"
268 exception:bad-let
269 (eval '(let)
270 (interaction-environment)))
271
272 (pass-if-syntax-error "(let 1)"
273 exception:bad-let
274 (eval '(let 1)
275 (interaction-environment)))
276
277 (pass-if-syntax-error "(let (x))"
278 exception:bad-let
279 (eval '(let (x))
280 (interaction-environment)))
281
282 (pass-if-syntax-error "(let ((x)))"
283 exception:bad-let
284 (eval '(let ((x)))
285 (interaction-environment)))
286
287 (pass-if-syntax-error "(let (x) 1)"
288 exception:bad-let
289 (eval '(let (x) 1)
290 (interaction-environment)))
291
292 (pass-if-syntax-error "(let ((x)) 3)"
293 exception:bad-let
294 (eval '(let ((x)) 3)
295 (interaction-environment)))
296
297 (pass-if-syntax-error "(let ((x 1) y) x)"
298 exception:bad-let
299 (eval '(let ((x 1) y) x)
300 (interaction-environment)))
301
302 (pass-if-syntax-error "(let ((1 2)) 3)"
303 exception:bad-let
304 (eval '(let ((1 2)) 3)
305 (interaction-environment))))
306
307 (with-test-prefix "duplicate bindings"
308
309 (pass-if-syntax-error "(let ((x 1) (x 2)) x)"
310 exception:duplicate-binding
311 (eval '(let ((x 1) (x 2)) x)
312 (interaction-environment))))
313
314 (with-test-prefix "bad body"
315
316 (pass-if-syntax-error "(let ())"
317 exception:bad-let
318 (eval '(let ())
319 (interaction-environment)))
320
321 (pass-if-syntax-error "(let ((x 1)))"
322 exception:bad-let
323 (eval '(let ((x 1)))
324 (interaction-environment)))))
325
326 (with-test-prefix "named let"
327
328 (with-test-prefix "initializers"
329
330 (pass-if "evaluated in outer environment"
331 (let ((f -))
332 (eqv? (let f ((n (f 1))) n) -1))))
333
334 (with-test-prefix "bad bindings"
335
336 (pass-if-syntax-error "(let x (y))"
337 exception:bad-let
338 (eval '(let x (y))
339 (interaction-environment))))
340
341 (with-test-prefix "bad body"
342
343 (pass-if-syntax-error "(let x ())"
344 exception:bad-let
345 (eval '(let x ())
346 (interaction-environment)))
347
348 (pass-if-syntax-error "(let x ((y 1)))"
349 exception:bad-let
350 (eval '(let x ((y 1)))
351 (interaction-environment)))))
352
353 (with-test-prefix "let*"
354
355 (with-test-prefix "bindings"
356
357 (pass-if "(let* ((x 1) (x 2)) ...)"
358 (let* ((x 1) (x 2))
359 (= x 2)))
360
361 (pass-if "(let* ((x 1) (x x)) ...)"
362 (let* ((x 1) (x x))
363 (= x 1)))
364
365 (pass-if "(let ((x 1) (y 2)) (let* () ...))"
366 (let ((x 1) (y 2))
367 (let* ()
368 (and (= x 1) (= y 2))))))
369
370 (with-test-prefix "bad bindings"
371
372 (pass-if-syntax-error "(let*)"
373 exception:generic-syncase-error
374 (eval '(let*)
375 (interaction-environment)))
376
377 (pass-if-syntax-error "(let* 1)"
378 exception:generic-syncase-error
379 (eval '(let* 1)
380 (interaction-environment)))
381
382 (pass-if-syntax-error "(let* (x))"
383 exception:generic-syncase-error
384 (eval '(let* (x))
385 (interaction-environment)))
386
387 (pass-if-syntax-error "(let* (x) 1)"
388 exception:generic-syncase-error
389 (eval '(let* (x) 1)
390 (interaction-environment)))
391
392 (pass-if-syntax-error "(let* ((x)) 3)"
393 exception:generic-syncase-error
394 (eval '(let* ((x)) 3)
395 (interaction-environment)))
396
397 (pass-if-syntax-error "(let* ((x 1) y) x)"
398 exception:generic-syncase-error
399 (eval '(let* ((x 1) y) x)
400 (interaction-environment)))
401
402 (pass-if-syntax-error "(let* x ())"
403 exception:generic-syncase-error
404 (eval '(let* x ())
405 (interaction-environment)))
406
407 (pass-if-syntax-error "(let* x (y))"
408 exception:generic-syncase-error
409 (eval '(let* x (y))
410 (interaction-environment)))
411
412 (pass-if-syntax-error "(let* ((1 2)) 3)"
413 exception:generic-syncase-error
414 (eval '(let* ((1 2)) 3)
415 (interaction-environment))))
416
417 (with-test-prefix "bad body"
418
419 (pass-if-syntax-error "(let* ())"
420 exception:generic-syncase-error
421 (eval '(let* ())
422 (interaction-environment)))
423
424 (pass-if-syntax-error "(let* ((x 1)))"
425 exception:generic-syncase-error
426 (eval '(let* ((x 1)))
427 (interaction-environment)))))
428
429 (with-test-prefix "letrec"
430
431 (with-test-prefix "bindings"
432
433 (pass-if-exception "initial bindings are undefined"
434 exception:variable-ref
435 (eval '(let ((x 1))
436 (letrec ((x 1) (y x)) y))
437 (interaction-environment))))
438
439 (with-test-prefix "bad bindings"
440
441 (pass-if-syntax-error "(letrec)"
442 exception:bad-letrec
443 (eval '(letrec)
444 (interaction-environment)))
445
446 (pass-if-syntax-error "(letrec 1)"
447 exception:bad-letrec
448 (eval '(letrec 1)
449 (interaction-environment)))
450
451 (pass-if-syntax-error "(letrec (x))"
452 exception:bad-letrec
453 (eval '(letrec (x))
454 (interaction-environment)))
455
456 (pass-if-syntax-error "(letrec (x) 1)"
457 exception:bad-letrec
458 (eval '(letrec (x) 1)
459 (interaction-environment)))
460
461 (pass-if-syntax-error "(letrec ((x)) 3)"
462 exception:bad-letrec
463 (eval '(letrec ((x)) 3)
464 (interaction-environment)))
465
466 (pass-if-syntax-error "(letrec ((x 1) y) x)"
467 exception:bad-letrec
468 (eval '(letrec ((x 1) y) x)
469 (interaction-environment)))
470
471 (pass-if-syntax-error "(letrec x ())"
472 exception:bad-letrec
473 (eval '(letrec x ())
474 (interaction-environment)))
475
476 (pass-if-syntax-error "(letrec x (y))"
477 exception:bad-letrec
478 (eval '(letrec x (y))
479 (interaction-environment)))
480
481 (pass-if-syntax-error "(letrec ((1 2)) 3)"
482 exception:bad-letrec
483 (eval '(letrec ((1 2)) 3)
484 (interaction-environment))))
485
486 (with-test-prefix "duplicate bindings"
487
488 (pass-if-syntax-error "(letrec ((x 1) (x 2)) x)"
489 exception:duplicate-binding
490 (eval '(letrec ((x 1) (x 2)) x)
491 (interaction-environment))))
492
493 (with-test-prefix "bad body"
494
495 (pass-if-syntax-error "(letrec ())"
496 exception:bad-letrec
497 (eval '(letrec ())
498 (interaction-environment)))
499
500 (pass-if-syntax-error "(letrec ((x 1)))"
501 exception:bad-letrec
502 (eval '(letrec ((x 1)))
503 (interaction-environment)))))
504
505 (with-test-prefix "letrec*"
506
507 (with-test-prefix "bindings"
508
509 (pass-if-exception "initial bindings are undefined"
510 exception:variable-ref
511 (eval '(letrec* ((x y) (y 1)) y)
512 (interaction-environment))))
513
514 (with-test-prefix "bad bindings"
515
516 (pass-if-syntax-error "(letrec*)"
517 exception:bad-letrec*
518 (eval '(letrec*)
519 (interaction-environment)))
520
521 (pass-if-syntax-error "(letrec* 1)"
522 exception:bad-letrec*
523 (eval '(letrec* 1)
524 (interaction-environment)))
525
526 (pass-if-syntax-error "(letrec* (x))"
527 exception:bad-letrec*
528 (eval '(letrec* (x))
529 (interaction-environment)))
530
531 (pass-if-syntax-error "(letrec* (x) 1)"
532 exception:bad-letrec*
533 (eval '(letrec* (x) 1)
534 (interaction-environment)))
535
536 (pass-if-syntax-error "(letrec* ((x)) 3)"
537 exception:bad-letrec*
538 (eval '(letrec* ((x)) 3)
539 (interaction-environment)))
540
541 (pass-if-syntax-error "(letrec* ((x 1) y) x)"
542 exception:bad-letrec*
543 (eval '(letrec* ((x 1) y) x)
544 (interaction-environment)))
545
546 (pass-if-syntax-error "(letrec* x ())"
547 exception:bad-letrec*
548 (eval '(letrec* x ())
549 (interaction-environment)))
550
551 (pass-if-syntax-error "(letrec* x (y))"
552 exception:bad-letrec*
553 (eval '(letrec* x (y))
554 (interaction-environment)))
555
556 (pass-if-syntax-error "(letrec* ((1 2)) 3)"
557 exception:bad-letrec*
558 (eval '(letrec* ((1 2)) 3)
559 (interaction-environment))))
560
561 (with-test-prefix "duplicate bindings"
562
563 (pass-if-syntax-error "(letrec* ((x 1) (x 2)) x)"
564 exception:duplicate-binding
565 (eval '(letrec* ((x 1) (x 2)) x)
566 (interaction-environment))))
567
568 (with-test-prefix "bad body"
569
570 (pass-if-syntax-error "(letrec* ())"
571 exception:bad-letrec*
572 (eval '(letrec* ())
573 (interaction-environment)))
574
575 (pass-if-syntax-error "(letrec* ((x 1)))"
576 exception:bad-letrec*
577 (eval '(letrec* ((x 1)))
578 (interaction-environment))))
579
580 (with-test-prefix "referencing previous values"
581 (pass-if (equal? (letrec* ((a (cons 'foo 'bar))
582 (b a))
583 b)
584 '(foo . bar)))
585 (pass-if (equal? (let ()
586 (define a (cons 'foo 'bar))
587 (define b a)
588 b)
589 '(foo . bar)))))
590
591 (with-test-prefix "if"
592
593 (with-test-prefix "missing or extra expressions"
594
595 (pass-if-syntax-error "(if)"
596 exception:generic-syncase-error
597 (eval '(if)
598 (interaction-environment)))
599
600 (pass-if-syntax-error "(if 1 2 3 4)"
601 exception:generic-syncase-error
602 (eval '(if 1 2 3 4)
603 (interaction-environment)))))
604
605 (with-test-prefix "cond"
606
607 (with-test-prefix "cond is hygienic"
608
609 (pass-if "bound 'else is handled correctly"
610 (eq? (let ((else 'ok)) (cond (else))) 'ok))
611
612 (with-test-prefix "bound '=> is handled correctly"
613
614 (pass-if "#t => 'ok"
615 (let ((=> 'foo))
616 (eq? (cond (#t => 'ok)) 'ok)))
617
618 (pass-if "else =>"
619 (let ((=> 'foo))
620 (eq? (cond (else =>)) 'foo)))
621
622 (pass-if "else => identity"
623 (let ((=> 'foo))
624 (eq? (cond (else => identity)) identity)))))
625
626 (with-test-prefix "SRFI-61"
627
628 (pass-if "always available"
629 (cond-expand (srfi-61 #t) (else #f)))
630
631 (pass-if "single value consequent"
632 (eq? 'ok (cond (#t identity => (lambda (x) 'ok)) (else #f))))
633
634 (pass-if "single value alternate"
635 (eq? 'ok (cond (#t not => (lambda (x) #f)) (else 'ok))))
636
637 (pass-if-exception "doesn't affect standard =>"
638 exception:wrong-num-args
639 (cond ((values 1 2) => (lambda (x y) #t))))
640
641 (pass-if "multiple values consequent"
642 (equal? '(2 1) (cond ((values 1 2)
643 (lambda (one two)
644 (and (= 1 one) (= 2 two))) =>
645 (lambda (one two) (list two one)))
646 (else #f))))
647
648 (pass-if "multiple values alternate"
649 (eq? 'ok (cond ((values 2 3 4)
650 (lambda args (equal? '(1 2 3) args)) =>
651 (lambda (x y z) #f))
652 (else 'ok))))
653
654 (pass-if "zero values"
655 (eq? 'ok (cond ((values) (lambda () #t) => (lambda () 'ok))
656 (else #f))))
657
658 (pass-if "bound => is handled correctly"
659 (let ((=> 'ok))
660 (eq? 'ok (cond (#t identity =>) (else #f)))))
661
662 (pass-if-syntax-error "missing recipient"
663 '(cond . "wrong number of receiver expressions")
664 (eval '(cond (#t identity =>))
665 (interaction-environment)))
666
667 (pass-if-syntax-error "extra recipient"
668 '(cond . "wrong number of receiver expressions")
669 (eval '(cond (#t identity => identity identity))
670 (interaction-environment))))
671
672 (with-test-prefix "bad or missing clauses"
673
674 (pass-if-syntax-error "(cond)"
675 exception:generic-syncase-error
676 (eval '(cond)
677 (interaction-environment)))
678
679 (pass-if-syntax-error "(cond #t)"
680 '(cond . "invalid clause")
681 (eval '(cond #t)
682 (interaction-environment)))
683
684 (pass-if-syntax-error "(cond 1)"
685 '(cond . "invalid clause")
686 (eval '(cond 1)
687 (interaction-environment)))
688
689 (pass-if-syntax-error "(cond 1 2)"
690 '(cond . "invalid clause")
691 (eval '(cond 1 2)
692 (interaction-environment)))
693
694 (pass-if-syntax-error "(cond 1 2 3)"
695 '(cond . "invalid clause")
696 (eval '(cond 1 2 3)
697 (interaction-environment)))
698
699 (pass-if-syntax-error "(cond 1 2 3 4)"
700 '(cond . "invalid clause")
701 (eval '(cond 1 2 3 4)
702 (interaction-environment)))
703
704 (pass-if-syntax-error "(cond ())"
705 '(cond . "invalid clause")
706 (eval '(cond ())
707 (interaction-environment)))
708
709 (pass-if-syntax-error "(cond () 1)"
710 '(cond . "invalid clause")
711 (eval '(cond () 1)
712 (interaction-environment)))
713
714 (pass-if-syntax-error "(cond (1) 1)"
715 '(cond . "invalid clause")
716 (eval '(cond (1) 1)
717 (interaction-environment)))
718
719 (pass-if-syntax-error "(cond (else #f) (#t #t))"
720 '(cond . "else must be the last clause")
721 (eval '(cond (else #f) (#t #t))
722 (interaction-environment))))
723
724 (with-test-prefix "wrong number of arguments"
725
726 (pass-if-exception "=> (lambda (x y) #t)"
727 exception:wrong-num-args
728 (cond (1 => (lambda (x y) #t))))))
729
730 (with-test-prefix "case"
731
732 (pass-if "clause with empty labels list"
733 (case 1 (() #f) (else #t)))
734
735 (with-test-prefix "case handles '=> correctly"
736
737 (pass-if "(1 2 3) => list"
738 (equal? (case 1 ((1 2 3) => list))
739 '(1)))
740
741 (pass-if "else => list"
742 (equal? (case 6
743 ((1 2 3) 'wrong)
744 (else => list))
745 '(6)))
746
747 (with-test-prefix "bound '=> is handled correctly"
748
749 (pass-if "(1) => 'ok"
750 (let ((=> 'foo))
751 (eq? (case 1 ((1) => 'ok)) 'ok)))
752
753 (pass-if "else =>"
754 (let ((=> 'foo))
755 (eq? (case 1 (else =>)) 'foo)))
756
757 (pass-if "else => list"
758 (let ((=> 'foo))
759 (eq? (case 1 (else => identity)) identity))))
760
761 (pass-if-syntax-error "missing recipient"
762 '(case . "wrong number of receiver expressions")
763 (eval '(case 1 ((1) =>))
764 (interaction-environment)))
765
766 (pass-if-syntax-error "extra recipient"
767 '(case . "wrong number of receiver expressions")
768 (eval '(case 1 ((1) => identity identity))
769 (interaction-environment))))
770
771 (with-test-prefix "case is hygienic"
772
773 (pass-if-syntax-error "bound 'else is handled correctly"
774 '(case . "invalid clause")
775 (eval '(let ((else #f)) (case 1 (else #f)))
776 (interaction-environment))))
777
778 (with-test-prefix "bad or missing clauses"
779
780 (pass-if-syntax-error "(case)"
781 exception:generic-syncase-error
782 (eval '(case)
783 (interaction-environment)))
784
785 (pass-if-syntax-error "(case . \"foo\")"
786 exception:generic-syncase-error
787 (eval '(case . "foo")
788 (interaction-environment)))
789
790 (pass-if-syntax-error "(case 1)"
791 exception:generic-syncase-error
792 (eval '(case 1)
793 (interaction-environment)))
794
795 (pass-if-syntax-error "(case 1 . \"foo\")"
796 exception:generic-syncase-error
797 (eval '(case 1 . "foo")
798 (interaction-environment)))
799
800 (pass-if-syntax-error "(case 1 \"foo\")"
801 '(case . "invalid clause")
802 (eval '(case 1 "foo")
803 (interaction-environment)))
804
805 (pass-if-syntax-error "(case 1 ())"
806 '(case . "invalid clause")
807 (eval '(case 1 ())
808 (interaction-environment)))
809
810 (pass-if-syntax-error "(case 1 (\"foo\"))"
811 '(case . "invalid clause")
812 (eval '(case 1 ("foo"))
813 (interaction-environment)))
814
815 (pass-if-syntax-error "(case 1 (\"foo\" \"bar\"))"
816 '(case . "invalid clause")
817 (eval '(case 1 ("foo" "bar"))
818 (interaction-environment)))
819
820 (pass-if-syntax-error "(case 1 ((2) \"bar\") . \"foo\")"
821 exception:generic-syncase-error
822 (eval '(case 1 ((2) "bar") . "foo")
823 (interaction-environment)))
824
825 (pass-if-syntax-error "(case 1 ((2) \"bar\") (else))"
826 '(case . "invalid clause")
827 (eval '(case 1 ((2) "bar") (else))
828 (interaction-environment)))
829
830 (pass-if-syntax-error "(case 1 (else #f) . \"foo\")"
831 exception:generic-syncase-error
832 (eval '(case 1 (else #f) . "foo")
833 (interaction-environment)))
834
835 (pass-if-syntax-error "(case 1 (else #f) ((1) #t))"
836 '(case . "else must be the last clause")
837 (eval '(case 1 (else #f) ((1) #t))
838 (interaction-environment)))))
839
840 (with-test-prefix "top-level define"
841
842 (pass-if "redefinition"
843 (let ((m (make-module)))
844 (beautify-user-module! m)
845
846 ;; The previous value of `round' must still be visible at the time the
847 ;; new `round' is defined. According to R5RS (Section 5.2.1), `define'
848 ;; should behave like `set!' in this case (except that in the case of
849 ;; Guile, we respect module boundaries).
850 (eval '(define round round) m)
851 (eq? (module-ref m 'round) round)))
852
853 (with-test-prefix "missing or extra expressions"
854
855 (pass-if-syntax-error "(define)"
856 exception:generic-syncase-error
857 (eval '(define)
858 (interaction-environment))))
859
860 (pass-if "module scoping"
861 (equal?
862 (eval
863 '(begin
864 (define-module (top-level-define/module-scoping-1)
865 #:export (define-10))
866 (define-syntax-rule (define-10 name)
867 (begin
868 (define t 10)
869 (define (name) t)))
870 (define-module (top-level-define/module-scoping-2)
871 #:use-module (top-level-define/module-scoping-1))
872 (define-10 foo)
873 (foo))
874 (current-module))
875 10))
876
877 (pass-if "module scoping, same symbolic name"
878 (equal?
879 (eval
880 '(begin
881 (define-module (top-level-define/module-scoping-3))
882 (define a 10)
883 (define-module (top-level-define/module-scoping-4)
884 #:use-module (top-level-define/module-scoping-3))
885 (define a (@@ (top-level-define/module-scoping-3) a))
886 a)
887 (current-module))
888 10))
889
890 (pass-if "module scoping, introduced names"
891 (equal?
892 (eval
893 '(begin
894 (define-module (top-level-define/module-scoping-5)
895 #:export (define-constant))
896 (define-syntax-rule (define-constant name val)
897 (begin
898 (define t val)
899 (define (name) t)))
900 (define-module (top-level-define/module-scoping-6)
901 #:use-module (top-level-define/module-scoping-5))
902 (define-constant foo 10)
903 (define-constant bar 20)
904 (foo))
905 (current-module))
906 10))
907
908 (pass-if "module scoping, duplicate introduced name"
909 (equal?
910 (eval
911 '(begin
912 (define-module (top-level-define/module-scoping-7)
913 #:export (define-constant))
914 (define-syntax-rule (define-constant name val)
915 (begin
916 (define t val)
917 (define (name) t)))
918 (define-module (top-level-define/module-scoping-8)
919 #:use-module (top-level-define/module-scoping-7))
920 (define-constant foo 10)
921 (define-constant foo 20)
922 (foo))
923 (current-module))
924 20)))
925
926 (with-test-prefix "internal define"
927
928 (pass-if "internal defines become letrec"
929 (eval '(let ((a identity) (b identity) (c identity))
930 (define (a x) (if (= x 0) 'a (b (- x 1))))
931 (define (b x) (if (= x 0) 'b (c (- x 1))))
932 (define (c x) (if (= x 0) 'c (a (- x 1))))
933 (and (eq? 'a (a 0) (a 3))
934 (eq? 'b (a 1) (a 4))
935 (eq? 'c (a 2) (a 5))))
936 (interaction-environment)))
937
938 (pass-if "binding is created before expression is evaluated"
939 ;; Internal defines are equivalent to `letrec' (R5RS, Section 5.2.2).
940 (= (eval '(let ()
941 (define foo
942 (begin
943 (set! foo 1)
944 (+ foo 1)))
945 foo)
946 (interaction-environment))
947 2))
948
949 (pass-if "internal defines with begin"
950 (false-if-exception
951 (eval '(let ((a identity) (b identity) (c identity))
952 (define (a x) (if (= x 0) 'a (b (- x 1))))
953 (begin
954 (define (b x) (if (= x 0) 'b (c (- x 1)))))
955 (define (c x) (if (= x 0) 'c (a (- x 1))))
956 (and (eq? 'a (a 0) (a 3))
957 (eq? 'b (a 1) (a 4))
958 (eq? 'c (a 2) (a 5))))
959 (interaction-environment))))
960
961 (pass-if "internal defines with empty begin"
962 (false-if-exception
963 (eval '(let ((a identity) (b identity) (c identity))
964 (define (a x) (if (= x 0) 'a (b (- x 1))))
965 (begin)
966 (define (b x) (if (= x 0) 'b (c (- x 1))))
967 (define (c x) (if (= x 0) 'c (a (- x 1))))
968 (and (eq? 'a (a 0) (a 3))
969 (eq? 'b (a 1) (a 4))
970 (eq? 'c (a 2) (a 5))))
971 (interaction-environment))))
972
973 (pass-if "internal defines with macro application"
974 (false-if-exception
975 (eval '(begin
976 (defmacro my-define forms
977 (cons 'define forms))
978 (let ((a identity) (b identity) (c identity))
979 (define (a x) (if (= x 0) 'a (b (- x 1))))
980 (my-define (b x) (if (= x 0) 'b (c (- x 1))))
981 (define (c x) (if (= x 0) 'c (a (- x 1))))
982 (and (eq? 'a (a 0) (a 3))
983 (eq? 'b (a 1) (a 4))
984 (eq? 'c (a 2) (a 5)))))
985 (interaction-environment))))
986
987 (pass-if-syntax-error "empty body"
988 exception:empty-body
989 (eval '(let () (begin))
990 (interaction-environment)))
991
992 (pass-if-syntax-error "body should end with expression"
993 exception:body-should-end-with-expr
994 (eval '(let () (define x #t))
995 (interaction-environment)))
996
997 (pass-if-equal "mixed definitions and expressions" 256
998 ((eval '(lambda (x)
999 (unless (number? x) (error "not a number" x))
1000 (define (square x) (* x x))
1001 (square (square x)))
1002 (interaction-environment))
1003 4))
1004
1005 (pass-if-equal "mixed definitions and expressions 2" 42
1006 (eval '(let ()
1007 (define (foo) (bar))
1008 1
1009 (define (bar) 42)
1010 (foo))
1011 (interaction-environment))))
1012
1013 (with-test-prefix "top-level define-values"
1014
1015 (pass-if "zero values"
1016 (eval '(begin (define-values () (values))
1017 #t)
1018 (interaction-environment)))
1019
1020 (pass-if-equal "one value"
1021 1
1022 (eval '(begin (define-values (x) 1)
1023 x)
1024 (interaction-environment)))
1025
1026 (pass-if-equal "two values"
1027 '(2 3)
1028 (eval '(begin (define-values (x y) (values 2 3))
1029 (list x y))
1030 (interaction-environment)))
1031
1032 (pass-if-equal "three values"
1033 '(4 5 6)
1034 (eval '(begin (define-values (x y z) (values 4 5 6))
1035 (list x y z))
1036 (interaction-environment)))
1037
1038 (pass-if-equal "one value with tail"
1039 '(a (b c d))
1040 (eval '(begin (define-values (x . y) (values 'a 'b 'c 'd))
1041 (list x y))
1042 (interaction-environment)))
1043
1044 (pass-if-equal "two values with tail"
1045 '(x y (z w))
1046 (eval '(begin (define-values (x y . z) (values 'x 'y 'z 'w))
1047 (list x y z))
1048 (interaction-environment)))
1049
1050 (pass-if-equal "just tail"
1051 '(1 2 3)
1052 (eval '(begin (define-values x (values 1 2 3))
1053 x)
1054 (interaction-environment)))
1055
1056 (pass-if-exception "expected 0 values, got 1"
1057 exception:wrong-number-of-values
1058 (eval '(define-values () 1)
1059 (interaction-environment)))
1060
1061 (pass-if-exception "expected 1 value, got 0"
1062 exception:wrong-number-of-values
1063 (eval '(define-values (x) (values))
1064 (interaction-environment)))
1065
1066 (pass-if-exception "expected 1 value, got 2"
1067 exception:wrong-number-of-values
1068 (eval '(define-values (x) (values 1 2))
1069 (interaction-environment)))
1070
1071 (pass-if-exception "expected 1 value with tail, got 0"
1072 exception:wrong-number-of-values
1073 (eval '(define-values (x . y) (values))
1074 (interaction-environment)))
1075
1076 (pass-if-exception "expected 2 value with tail, got 1"
1077 exception:wrong-number-of-values
1078 (eval '(define-values (x y . z) 1)
1079 (interaction-environment)))
1080
1081 (pass-if "redefinition"
1082 (let ((m (make-module)))
1083 (beautify-user-module! m)
1084
1085 ;; The previous values of `floor' and `round' must still be
1086 ;; visible at the time the new `floor' and `round' are defined.
1087 (eval '(define-values (floor round) (values floor round)) m)
1088 (and (eq? (module-ref m 'floor) floor)
1089 (eq? (module-ref m 'round) round))))
1090
1091 (with-test-prefix "missing expression"
1092
1093 (pass-if-syntax-error "(define-values)"
1094 exception:generic-syncase-error
1095 (eval '(define-values)
1096 (interaction-environment)))))
1097
1098 (with-test-prefix "internal define-values"
1099
1100 (pass-if "zero values"
1101 (let ()
1102 (define-values () (values))
1103 #t))
1104
1105 (pass-if-equal "one value"
1106 1
1107 (let ()
1108 (define-values (x) 1)
1109 x))
1110
1111 (pass-if-equal "two values"
1112 '(2 3)
1113 (let ()
1114 (define-values (x y) (values 2 3))
1115 (list x y)))
1116
1117 (pass-if-equal "three values"
1118 '(4 5 6)
1119 (let ()
1120 (define-values (x y z) (values 4 5 6))
1121 (list x y z)))
1122
1123 (pass-if-equal "one value with tail"
1124 '(a (b c d))
1125 (let ()
1126 (define-values (x . y) (values 'a 'b 'c 'd))
1127 (list x y)))
1128
1129 (pass-if-equal "two values with tail"
1130 '(x y (z w))
1131 (let ()
1132 (define-values (x y . z) (values 'x 'y 'z 'w))
1133 (list x y z)))
1134
1135 (pass-if-equal "just tail"
1136 '(1 2 3)
1137 (let ()
1138 (define-values x (values 1 2 3))
1139 x))
1140
1141 (pass-if-exception "expected 0 values, got 1"
1142 exception:wrong-number-of-values
1143 (eval '(let ()
1144 (define-values () 1)
1145 #f)
1146 (interaction-environment)))
1147
1148 (pass-if-exception "expected 1 value, got 0"
1149 exception:wrong-number-of-values
1150 (eval '(let ()
1151 (define-values (x) (values))
1152 #f)
1153 (interaction-environment)))
1154
1155 (pass-if-exception "expected 1 value, got 2"
1156 exception:wrong-number-of-values
1157 (eval '(let ()
1158 (define-values (x) (values 1 2))
1159 #f)
1160 (interaction-environment)))
1161
1162 (pass-if-exception "expected 1 value with tail, got 0"
1163 exception:wrong-number-of-values
1164 (eval '(let ()
1165 (define-values (x . y) (values))
1166 #f)
1167 (interaction-environment)))
1168
1169 (pass-if-exception "expected 2 value with tail, got 1"
1170 exception:wrong-number-of-values
1171 (eval '(let ()
1172 (define-values (x y . z) 1)
1173 #f)
1174 (interaction-environment)))
1175
1176 (with-test-prefix "missing expression"
1177
1178 (pass-if-syntax-error "(define-values)"
1179 exception:generic-syncase-error
1180 (eval '(let ()
1181 (define-values)
1182 #f)
1183 (interaction-environment)))))
1184
1185 (with-test-prefix "set!"
1186
1187 (with-test-prefix "missing or extra expressions"
1188
1189 (pass-if-syntax-error "(set!)"
1190 exception:bad-set!
1191 (eval '(set!)
1192 (interaction-environment)))
1193
1194 (pass-if-syntax-error "(set! 1)"
1195 exception:bad-set!
1196 (eval '(set! 1)
1197 (interaction-environment)))
1198
1199 (pass-if-syntax-error "(set! 1 2 3)"
1200 exception:bad-set!
1201 (eval '(set! 1 2 3)
1202 (interaction-environment))))
1203
1204 (with-test-prefix "bad variable"
1205
1206 (pass-if-syntax-error "(set! \"\" #t)"
1207 exception:bad-set!
1208 (eval '(set! "" #t)
1209 (interaction-environment)))
1210
1211 (pass-if-syntax-error "(set! 1 #t)"
1212 exception:bad-set!
1213 (eval '(set! 1 #t)
1214 (interaction-environment)))
1215
1216 (pass-if-syntax-error "(set! #t #f)"
1217 exception:bad-set!
1218 (eval '(set! #t #f)
1219 (interaction-environment)))
1220
1221 (pass-if-syntax-error "(set! #f #t)"
1222 exception:bad-set!
1223 (eval '(set! #f #t)
1224 (interaction-environment)))
1225
1226 (pass-if-syntax-error "(set! #\\space #f)"
1227 exception:bad-set!
1228 (eval '(set! #\space #f)
1229 (interaction-environment)))))
1230
1231 (with-test-prefix "quote"
1232
1233 (with-test-prefix "missing or extra expression"
1234
1235 (pass-if-syntax-error "(quote)"
1236 exception:bad-quote
1237 (eval '(quote)
1238 (interaction-environment)))
1239
1240 (pass-if-syntax-error "(quote a b)"
1241 exception:bad-quote
1242 (eval '(quote a b)
1243 (interaction-environment)))))
1244
1245 (with-test-prefix "while"
1246
1247 (define (unreachable)
1248 (error "unreachable code has been reached!"))
1249
1250 ;; Return a new procedure COND which when called (COND) will return #t the
1251 ;; first N times, then #f, then any further call is an error. N=0 is
1252 ;; allowed, in which case #f is returned by the first call.
1253 (define (make-iterations-cond n)
1254 (lambda ()
1255 (cond ((not n)
1256 (error "oops, condition re-tested after giving false"))
1257 ((= 0 n)
1258 (set! n #f)
1259 #f)
1260 (else
1261 (set! n (1- n))
1262 #t))))
1263
1264
1265 (pass-if-syntax-error "too few args" exception:generic-syncase-error
1266 (eval '(while) (interaction-environment)))
1267
1268 (with-test-prefix "empty body"
1269 (do ((n 0 (1+ n)))
1270 ((> n 5))
1271 (pass-if n
1272 (eval `(letrec ((make-iterations-cond
1273 (lambda (n)
1274 (lambda ()
1275 (cond ((not n)
1276 (error "oops, condition re-tested after giving false"))
1277 ((= 0 n)
1278 (set! n #f)
1279 #f)
1280 (else
1281 (set! n (1- n))
1282 #t))))))
1283 (let ((cond (make-iterations-cond ,n)))
1284 (while (cond))
1285 #t))
1286 (interaction-environment)))))
1287
1288 (pass-if "initially false"
1289 (while #f
1290 (unreachable))
1291 #t)
1292
1293 (with-test-prefix "iterations"
1294 (do ((n 0 (1+ n)))
1295 ((> n 5))
1296 (pass-if n
1297 (let ((cond (make-iterations-cond n))
1298 (i 0))
1299 (while (cond)
1300 (set! i (1+ i)))
1301 (= i n)))))
1302
1303 (with-test-prefix "break"
1304
1305 (pass-if "normal return"
1306 (not (while #f (error "not reached"))))
1307
1308 (pass-if "no args"
1309 (while #t (break)))
1310
1311 (pass-if "multiple values"
1312 (equal? '(1 2 3)
1313 (call-with-values
1314 (lambda () (while #t (break 1 2 3)))
1315 list)))
1316
1317 (with-test-prefix "from cond"
1318 (pass-if "first"
1319 (while (begin
1320 (break)
1321 (unreachable))
1322 (unreachable))
1323 #t)
1324
1325 (do ((n 0 (1+ n)))
1326 ((> n 5))
1327 (pass-if n
1328 (let ((cond (make-iterations-cond n))
1329 (i 0))
1330 (while (if (cond)
1331 #t
1332 (begin
1333 (break)
1334 (unreachable)))
1335 (set! i (1+ i)))
1336 (= i n)))))
1337
1338 (with-test-prefix "from body"
1339 (pass-if "first"
1340 (while #t
1341 (break)
1342 (unreachable))
1343 #t)
1344
1345 (do ((n 0 (1+ n)))
1346 ((> n 5))
1347 (pass-if n
1348 (let ((cond (make-iterations-cond n))
1349 (i 0))
1350 (while #t
1351 (if (not (cond))
1352 (begin
1353 (break)
1354 (unreachable)))
1355 (set! i (1+ i)))
1356 (= i n)))))
1357
1358 (pass-if "from nested"
1359 (while #t
1360 (let ((outer-break break))
1361 (while #t
1362 (outer-break)
1363 (unreachable)))
1364 (unreachable))
1365 #t)
1366
1367 (pass-if "from recursive"
1368 (let ((outer-break #f))
1369 (define (r n)
1370 (while #t
1371 (if (eq? n 'outer)
1372 (begin
1373 (set! outer-break break)
1374 (r 'inner))
1375 (begin
1376 (outer-break)
1377 (unreachable))))
1378 (if (eq? n 'inner)
1379 (error "broke only from inner loop")))
1380 (r 'outer))
1381 #t))
1382
1383 (with-test-prefix "continue"
1384
1385 (pass-if-syntax-error "too many args" exception:too-many-args
1386 (eval '(while #t
1387 (continue 1))
1388 (interaction-environment)))
1389
1390 (with-test-prefix "from cond"
1391 (do ((n 0 (1+ n)))
1392 ((> n 5))
1393 (pass-if n
1394 (let ((cond (make-iterations-cond n))
1395 (i 0))
1396 (while (if (cond)
1397 (begin
1398 (set! i (1+ i))
1399 (continue)
1400 (unreachable))
1401 #f)
1402 (unreachable))
1403 (= i n)))))
1404
1405 (with-test-prefix "from body"
1406 (do ((n 0 (1+ n)))
1407 ((> n 5))
1408 (pass-if n
1409 (let ((cond (make-iterations-cond n))
1410 (i 0))
1411 (while (cond)
1412 (set! i (1+ i))
1413 (continue)
1414 (unreachable))
1415 (= i n)))))
1416
1417 (pass-if "from nested"
1418 (let ((cond (make-iterations-cond 3)))
1419 (while (cond)
1420 (let ((outer-continue continue))
1421 (while #t
1422 (outer-continue)
1423 (unreachable)))))
1424 #t)
1425
1426 (pass-if "from recursive"
1427 (let ((outer-continue #f))
1428 (define (r n)
1429 (let ((cond (make-iterations-cond 3))
1430 (first #t))
1431 (while (begin
1432 (if (and (not first)
1433 (eq? n 'inner))
1434 (error "continued only to inner loop"))
1435 (cond))
1436 (set! first #f)
1437 (if (eq? n 'outer)
1438 (begin
1439 (set! outer-continue continue)
1440 (r 'inner))
1441 (begin
1442 (outer-continue)
1443 (unreachable))))))
1444 (r 'outer))
1445 #t)))
1446
1447 (with-test-prefix "syntax-rules"
1448
1449 (pass-if-equal "custom ellipsis within normal ellipsis"
1450 '((((a x) (a y) (a …))
1451 ((b x) (b y) (b …))
1452 ((c x) (c y) (c …)))
1453 (((a x) (b x) (c x))
1454 ((a y) (b y) (c y))
1455 ((a …) (b …) (c …))))
1456 (let ()
1457 (define-syntax foo
1458 (syntax-rules ()
1459 ((_ y ...)
1460 (syntax-rules … ()
1461 ((_ x …)
1462 '((((x y) ...) …)
1463 (((x y) …) ...)))))))
1464 (define-syntax bar (foo x y …))
1465 (bar a b c)))
1466
1467 (pass-if-equal "normal ellipsis within custom ellipsis"
1468 '((((a x) (a y) (a z))
1469 ((b x) (b y) (b z))
1470 ((c x) (c y) (c z)))
1471 (((a x) (b x) (c x))
1472 ((a y) (b y) (c y))
1473 ((a z) (b z) (c z))))
1474 (let ()
1475 (define-syntax foo
1476 (syntax-rules … ()
1477 ((_ y …)
1478 (syntax-rules ()
1479 ((_ x ...)
1480 '((((x y) …) ...)
1481 (((x y) ...) …)))))))
1482 (define-syntax bar (foo x y z))
1483 (bar a b c)))
1484
1485 ;; This test is given in SRFI-46.
1486 (pass-if-equal "custom ellipsis is handled hygienically"
1487 '((1) 2 (3) (4))
1488 (let-syntax
1489 ((f (syntax-rules ()
1490 ((f ?e)
1491 (let-syntax
1492 ((g (syntax-rules --- ()
1493 ((g (??x ?e) (??y ---))
1494 '((??x) ?e (??y) ---)))))
1495 (g (1 2) (3 4)))))))
1496 (f ---))))
1497
1498 (with-test-prefix "syntax-error"
1499
1500 (pass-if-syntax-error "outside of macro without args"
1501 "test error"
1502 (eval '(syntax-error "test error")
1503 (interaction-environment)))
1504
1505 (pass-if-syntax-error "outside of macro with args"
1506 "test error x \\(y z\\)"
1507 (eval '(syntax-error "test error" x (y z))
1508 (interaction-environment)))
1509
1510 (pass-if-equal "within macro"
1511 '(simple-let
1512 "expected an identifier but got (z1 z2)"
1513 (simple-let ((y (* x x))
1514 ((z1 z2) (values x x)))
1515 (+ y 1)))
1516 (catch 'syntax-error
1517 (lambda ()
1518 (eval '(let ()
1519 (define-syntax simple-let
1520 (syntax-rules ()
1521 ((_ (head ... ((x . y) val) . tail)
1522 body1 body2 ...)
1523 (syntax-error
1524 "expected an identifier but got"
1525 (x . y)))
1526 ((_ ((name val) ...) body1 body2 ...)
1527 ((lambda (name ...) body1 body2 ...)
1528 val ...))))
1529 (define (foo x)
1530 (simple-let ((y (* x x))
1531 ((z1 z2) (values x x)))
1532 (+ y 1)))
1533 foo)
1534 (interaction-environment))
1535 (error "expected syntax-error exception"))
1536 (lambda (k who what where form . maybe-subform)
1537 (list who what form)))))
1538
1539 (with-test-prefix "syntax-case"
1540
1541 (pass-if-syntax-error "duplicate pattern variable"
1542 '(syntax-case . "duplicate pattern variable")
1543 (eval '(lambda (e)
1544 (syntax-case e ()
1545 ((a b c d e d f) #f)))
1546 (interaction-environment)))
1547
1548 (with-test-prefix "misplaced ellipses"
1549
1550 (pass-if-syntax-error "bare ellipsis"
1551 '(syntax-case . "misplaced ellipsis")
1552 (eval '(lambda (e)
1553 (syntax-case e ()
1554 (... #f)))
1555 (interaction-environment)))
1556
1557 (pass-if-syntax-error "ellipsis singleton"
1558 '(syntax-case . "misplaced ellipsis")
1559 (eval '(lambda (e)
1560 (syntax-case e ()
1561 ((...) #f)))
1562 (interaction-environment)))
1563
1564 (pass-if-syntax-error "ellipsis in car"
1565 '(syntax-case . "misplaced ellipsis")
1566 (eval '(lambda (e)
1567 (syntax-case e ()
1568 ((... . _) #f)))
1569 (interaction-environment)))
1570
1571 (pass-if-syntax-error "ellipsis in cdr"
1572 '(syntax-case . "misplaced ellipsis")
1573 (eval '(lambda (e)
1574 (syntax-case e ()
1575 ((_ . ...) #f)))
1576 (interaction-environment)))
1577
1578 (pass-if-syntax-error "two ellipses in the same list"
1579 '(syntax-case . "misplaced ellipsis")
1580 (eval '(lambda (e)
1581 (syntax-case e ()
1582 ((x ... y ...) #f)))
1583 (interaction-environment)))
1584
1585 (pass-if-syntax-error "three ellipses in the same list"
1586 '(syntax-case . "misplaced ellipsis")
1587 (eval '(lambda (e)
1588 (syntax-case e ()
1589 ((x ... y ... z ...) #f)))
1590 (interaction-environment)))))
1591
1592 (with-test-prefix "with-ellipsis"
1593
1594 (pass-if-equal "simple"
1595 '(a 1 2 3)
1596 (let ()
1597 (define-syntax define-quotation-macros
1598 (lambda (x)
1599 (syntax-case x ()
1600 ((_ (macro-name head-symbol) ...)
1601 #'(begin (define-syntax macro-name
1602 (lambda (x)
1603 (with-ellipsis …
1604 (syntax-case x ()
1605 ((_ x …)
1606 #'(quote (head-symbol x …)))))))
1607 ...)))))
1608 (define-quotation-macros (quote-a a) (quote-b b))
1609 (quote-a 1 2 3)))
1610
1611 (pass-if-equal "disables normal ellipsis"
1612 '(a ...)
1613 (let ()
1614 (define-syntax foo
1615 (lambda (x)
1616 (with-ellipsis …
1617 (syntax-case x ()
1618 ((_)
1619 #'(quote (a ...)))))))
1620 (foo)))
1621
1622 (pass-if-equal "doesn't affect ellipsis for generated code"
1623 '(a b c)
1624 (let ()
1625 (define-syntax quotation-macro
1626 (lambda (x)
1627 (with-ellipsis …
1628 (syntax-case x ()
1629 ((_)
1630 #'(lambda (x)
1631 (syntax-case x ()
1632 ((_ x ...)
1633 #'(quote (x ...))))))))))
1634 (define-syntax kwote (quotation-macro))
1635 (kwote a b c)))
1636
1637 (pass-if-equal "propagates into syntax binders"
1638 '(a b c)
1639 (let ()
1640 (with-ellipsis …
1641 (define-syntax kwote
1642 (lambda (x)
1643 (syntax-case x ()
1644 ((_ x …)
1645 #'(quote (x …))))))
1646 (kwote a b c))))
1647
1648 (pass-if-equal "works with local-eval"
1649 5
1650 (let ((env (with-ellipsis … (the-environment))))
1651 (local-eval '(syntax-case #'(a b c d e) ()
1652 ((x …)
1653 (length #'(x …))))
1654 env))))
1655
1656 (with-test-prefix "syntax objects"
1657 (let ((interpreted (eval '#'(foo bar baz) (current-module)))
1658 (interpreted-bis (eval '#'(foo bar baz) (current-module)))
1659 (compiled ((@ (system base compile) compile) '#'(foo bar baz)
1660 #:env (current-module))))
1661 ;; Guile's expander doesn't wrap lists.
1662 (pass-if "interpreted syntax object?"
1663 (and (list? interpreted)
1664 (and-map syntax? interpreted)))
1665 (pass-if "compiled syntax object?"
1666 (and (list? compiled)
1667 (and-map syntax? compiled)))
1668
1669 (pass-if "interpreted syntax objects are not vectors"
1670 (not (vector? interpreted)))
1671 (pass-if "compiled syntax objects are not vectors"
1672 (not (vector? compiled)))
1673
1674 (pass-if-equal "syntax objects comparable with equal? (eval/eval)"
1675 interpreted interpreted-bis)
1676 (pass-if-equal "syntax objects comparable with equal? (eval/compile)"
1677 interpreted compiled)
1678
1679 (pass-if-equal "syntax objects hash the same (eval/eval)"
1680 (hash interpreted most-positive-fixnum)
1681 (hash interpreted-bis most-positive-fixnum))
1682
1683 (pass-if-equal "syntax objects hash the same (eval/compile)"
1684 (hash interpreted most-positive-fixnum)
1685 (hash compiled most-positive-fixnum))))
1686
1687 (with-test-prefix "#nil in syntaxes"
1688 (pass-if-equal "does not crash"
1689 42
1690 (let ()
1691 (define-syntax foo
1692 (syntax-rules ()
1693 ;; In 3.0.7 this would crash with
1694 ;; unknown location: unexpected syntax in form ()
1695 ((_ x) (when (eq? x #nil) 42))))
1696 (foo #nil))))
1697
1698 (with-test-prefix "duplicate top-level introduced definitions"
1699 (pass-if-equal '(42 69)
1700 (primitive-eval
1701 (macroexpand
1702 '(begin
1703 (define-syntax-rule (defconst f val)
1704 (begin
1705 ;; The zeros cause a hash collision.
1706 (define t (begin 0 0 0 0 0 0 0 0 0 val))
1707 (define (f) t)))
1708 (defconst a 42)
1709 (defconst b 69)
1710 (list (a) (b)))))))
1711
1712 (pass-if-exception "macro-introduced cross-module unbound identifiers"
1713 exception:unbound-var
1714 (eval
1715 '(begin
1716 (define-module (foo) #:export (introduce-unbound))
1717 (define-syntax-rule (introduce-unbound)
1718 variable-bound-in-bar)
1719 (define-module (bar) #:use-module (foo))
1720 (define variable-bound-in-bar 42)
1721 (introduce-unbound))
1722 (interaction-environment)))
1723
1724 ;;; Local Variables:
1725 ;;; eval: (put 'pass-if-syntax-error 'scheme-indent-function 1)
1726 ;;; eval: (put 'with-ellipsis 'scheme-indent-function 1)
1727 ;;; End: