1 ;;;; syntax.test --- test suite for Guile's syntactic forms -*- scheme -*-
3 ;;;; Copyright (C) 2001,2003,2004, 2005, 2006, 2009, 2010, 2011 Free Software Foundation, Inc.
5 ;;;; This library is free software; you can redistribute it and/or
6 ;;;; modify it under the terms of the GNU Lesser General Public
7 ;;;; License as published by the Free Software Foundation; either
8 ;;;; version 3 of the License, or (at your option) any later version.
10 ;;;; This library is distributed in the hope that it will be useful,
11 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ;;;; Lesser General Public License for more details.
15 ;;;; You should have received a copy of the GNU Lesser General Public
16 ;;;; License along with this library; if not, write to the Free Software
17 ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 (define-module (test-suite test-syntax)
20 #:use-module (ice-9 regex)
21 #:use-module (test-suite lib))
24 (define exception:generic-syncase-error
25 "source expression failed to match")
26 (define exception:unexpected-syntax
29 (define exception:bad-expression
32 (define exception:missing/extra-expr
33 "Missing or extra expression")
34 (define exception:missing-expr
36 (define exception:missing-body-expr
37 "no expressions in body")
38 (define exception:extra-expr
40 (define exception:illegal-empty-combination
41 "Illegal empty combination")
43 (define exception:bad-lambda
45 (define exception:bad-let
47 (define exception:bad-letrec
49 (define exception:bad-letrec*
51 (define exception:bad-set!
53 (define exception:bad-quote
54 '(quote . "bad syntax"))
55 (define exception:bad-bindings
57 (define exception:bad-binding
59 (define exception:duplicate-binding
60 "duplicate bound variable")
61 (define exception:bad-body
63 (define exception:bad-formals
64 "invalid argument list")
65 (define exception:bad-formal
67 (define exception:duplicate-formals
68 "duplicate identifier in argument list")
70 (define exception:missing-clauses
72 (define exception:misplaced-else-clause
73 "Misplaced else clause")
74 (define exception:bad-case-clause
76 (define exception:bad-case-labels
78 (define exception:bad-cond-clause
81 (define exception:too-many-args
85 ;; (put 'pass-if-syntax-error 'scheme-indent-function 1)
86 (define-syntax pass-if-syntax-error
91 (lambda () exp (error "expected uri-error exception"))
92 (lambda (k who what where form . maybe-subform)
94 (and (eq? who (car pat))
95 (string-match (cdr pat) what))
96 (string-match pat what))
98 (error "unexpected syntax-error exception" what pat))))))))
100 (with-test-prefix "expressions"
102 (with-test-prefix "Bad argument list"
104 (pass-if-syntax-error "improper argument list of length 1"
105 exception:generic-syncase-error
106 (eval '(let ((foo (lambda (x y) #t)))
108 (interaction-environment)))
110 (pass-if-syntax-error "improper argument list of length 2"
111 exception:generic-syncase-error
112 (eval '(let ((foo (lambda (x y) #t)))
114 (interaction-environment))))
116 (with-test-prefix "missing or extra expression"
119 ;; *Note:* In many dialects of Lisp, the empty combination, (),
120 ;; is a legitimate expression. In Scheme, combinations must
121 ;; have at least one subexpression, so () is not a syntactically
125 (pass-if-syntax-error "empty parentheses \"()\""
126 exception:unexpected-syntax
128 (interaction-environment)))))
130 (with-test-prefix "quote"
133 (with-test-prefix "quasiquote"
135 (with-test-prefix "unquote"
137 (pass-if "repeated execution"
138 (let ((foo (let ((i 0)) (lambda () (set! i (+ i 1)) `(,i)))))
139 (and (equal? (foo) '(1)) (equal? (foo) '(2))))))
141 (with-test-prefix "unquote-splicing"
143 (pass-if "extra arguments"
144 (equal? (eval '(quasiquote ((unquote-splicing (list 1 2) (list 3 4))))
145 (interaction-environment))
148 (with-test-prefix "begin"
150 (pass-if "legal (begin)"
151 (eval '(begin (begin) #t) (interaction-environment)))
153 (pass-if-syntax-error "illegal (begin)"
154 exception:generic-syncase-error
155 (eval '(begin (if #t (begin)) #t) (interaction-environment))))
157 (define-syntax matches?
159 ((_ (op arg ...) pat) (let ((x (op arg ...)))
162 ((_ x (a . b)) (and (pair? x)
164 (matches? (cdr x) b)))
166 ((_ x pat) (equal? x 'pat))))
168 (with-test-prefix "lambda"
170 (with-test-prefix "bad formals"
172 (pass-if-syntax-error "(lambda)"
175 (interaction-environment)))
177 (pass-if-syntax-error "(lambda . \"foo\")"
179 (eval '(lambda . "foo")
180 (interaction-environment)))
182 (pass-if-syntax-error "(lambda \"foo\")"
184 (eval '(lambda "foo")
185 (interaction-environment)))
187 (pass-if-syntax-error "(lambda \"foo\" #f)"
188 exception:bad-formals
189 (eval '(lambda "foo" #f)
190 (interaction-environment)))
192 (pass-if-syntax-error "(lambda (x 1) 2)"
193 exception:bad-formals
194 (eval '(lambda (x 1) 2)
195 (interaction-environment)))
197 (pass-if-syntax-error "(lambda (1 x) 2)"
198 exception:bad-formals
199 (eval '(lambda (1 x) 2)
200 (interaction-environment)))
202 (pass-if-syntax-error "(lambda (x \"a\") 2)"
203 exception:bad-formals
204 (eval '(lambda (x "a") 2)
205 (interaction-environment)))
207 (pass-if-syntax-error "(lambda (\"a\" x) 2)"
208 exception:bad-formals
209 (eval '(lambda ("a" x) 2)
210 (interaction-environment))))
212 (with-test-prefix "duplicate formals"
215 (pass-if-syntax-error "(lambda (x x) 1)"
216 exception:duplicate-formals
217 (eval '(lambda (x x) 1)
218 (interaction-environment)))
221 (pass-if-syntax-error "(lambda (x x x) 1)"
222 exception:duplicate-formals
223 (eval '(lambda (x x x) 1)
224 (interaction-environment))))
226 (with-test-prefix "bad body"
228 (pass-if-syntax-error "(lambda ())"
231 (interaction-environment)))))
233 (with-test-prefix "let"
235 (with-test-prefix "bindings"
237 (pass-if-exception "late binding"
238 exception:unbound-var
239 (let ((x 1) (y x)) y)))
241 (with-test-prefix "bad bindings"
243 (pass-if-syntax-error "(let)"
246 (interaction-environment)))
248 (pass-if-syntax-error "(let 1)"
251 (interaction-environment)))
253 (pass-if-syntax-error "(let (x))"
256 (interaction-environment)))
258 (pass-if-syntax-error "(let ((x)))"
261 (interaction-environment)))
263 (pass-if-syntax-error "(let (x) 1)"
266 (interaction-environment)))
268 (pass-if-syntax-error "(let ((x)) 3)"
271 (interaction-environment)))
273 (pass-if-syntax-error "(let ((x 1) y) x)"
275 (eval '(let ((x 1) y) x)
276 (interaction-environment)))
278 (pass-if-syntax-error "(let ((1 2)) 3)"
280 (eval '(let ((1 2)) 3)
281 (interaction-environment))))
283 (with-test-prefix "duplicate bindings"
285 (pass-if-syntax-error "(let ((x 1) (x 2)) x)"
286 exception:duplicate-binding
287 (eval '(let ((x 1) (x 2)) x)
288 (interaction-environment))))
290 (with-test-prefix "bad body"
292 (pass-if-syntax-error "(let ())"
295 (interaction-environment)))
297 (pass-if-syntax-error "(let ((x 1)))"
300 (interaction-environment)))))
302 (with-test-prefix "named let"
304 (with-test-prefix "initializers"
306 (pass-if "evaluated in outer environment"
308 (eqv? (let f ((n (f 1))) n) -1))))
310 (with-test-prefix "bad bindings"
312 (pass-if-syntax-error "(let x (y))"
315 (interaction-environment))))
317 (with-test-prefix "bad body"
319 (pass-if-syntax-error "(let x ())"
322 (interaction-environment)))
324 (pass-if-syntax-error "(let x ((y 1)))"
326 (eval '(let x ((y 1)))
327 (interaction-environment)))))
329 (with-test-prefix "let*"
331 (with-test-prefix "bindings"
333 (pass-if "(let* ((x 1) (x 2)) ...)"
337 (pass-if "(let* ((x 1) (x x)) ...)"
341 (pass-if "(let ((x 1) (y 2)) (let* () ...))"
344 (and (= x 1) (= y 2))))))
346 (with-test-prefix "bad bindings"
348 (pass-if-syntax-error "(let*)"
349 exception:generic-syncase-error
351 (interaction-environment)))
353 (pass-if-syntax-error "(let* 1)"
354 exception:generic-syncase-error
356 (interaction-environment)))
358 (pass-if-syntax-error "(let* (x))"
359 exception:generic-syncase-error
361 (interaction-environment)))
363 (pass-if-syntax-error "(let* (x) 1)"
364 exception:generic-syncase-error
366 (interaction-environment)))
368 (pass-if-syntax-error "(let* ((x)) 3)"
369 exception:generic-syncase-error
370 (eval '(let* ((x)) 3)
371 (interaction-environment)))
373 (pass-if-syntax-error "(let* ((x 1) y) x)"
374 exception:generic-syncase-error
375 (eval '(let* ((x 1) y) x)
376 (interaction-environment)))
378 (pass-if-syntax-error "(let* x ())"
379 exception:generic-syncase-error
381 (interaction-environment)))
383 (pass-if-syntax-error "(let* x (y))"
384 exception:generic-syncase-error
386 (interaction-environment)))
388 (pass-if-syntax-error "(let* ((1 2)) 3)"
389 exception:generic-syncase-error
390 (eval '(let* ((1 2)) 3)
391 (interaction-environment))))
393 (with-test-prefix "bad body"
395 (pass-if-syntax-error "(let* ())"
396 exception:generic-syncase-error
398 (interaction-environment)))
400 (pass-if-syntax-error "(let* ((x 1)))"
401 exception:generic-syncase-error
402 (eval '(let* ((x 1)))
403 (interaction-environment)))))
405 (with-test-prefix "letrec"
407 (with-test-prefix "bindings"
409 (pass-if-syntax-error "initial bindings are undefined"
410 exception:used-before-defined
412 ;; FIXME: the memoizer does initialize the var to undefined, but
413 ;; the Scheme evaluator has no way of checking what's an
414 ;; undefined value. Not sure how to do this.
416 (letrec ((x 1) (y x)) y))))
418 (with-test-prefix "bad bindings"
420 (pass-if-syntax-error "(letrec)"
423 (interaction-environment)))
425 (pass-if-syntax-error "(letrec 1)"
428 (interaction-environment)))
430 (pass-if-syntax-error "(letrec (x))"
433 (interaction-environment)))
435 (pass-if-syntax-error "(letrec (x) 1)"
437 (eval '(letrec (x) 1)
438 (interaction-environment)))
440 (pass-if-syntax-error "(letrec ((x)) 3)"
442 (eval '(letrec ((x)) 3)
443 (interaction-environment)))
445 (pass-if-syntax-error "(letrec ((x 1) y) x)"
447 (eval '(letrec ((x 1) y) x)
448 (interaction-environment)))
450 (pass-if-syntax-error "(letrec x ())"
453 (interaction-environment)))
455 (pass-if-syntax-error "(letrec x (y))"
457 (eval '(letrec x (y))
458 (interaction-environment)))
460 (pass-if-syntax-error "(letrec ((1 2)) 3)"
462 (eval '(letrec ((1 2)) 3)
463 (interaction-environment))))
465 (with-test-prefix "duplicate bindings"
467 (pass-if-syntax-error "(letrec ((x 1) (x 2)) x)"
468 exception:duplicate-binding
469 (eval '(letrec ((x 1) (x 2)) x)
470 (interaction-environment))))
472 (with-test-prefix "bad body"
474 (pass-if-syntax-error "(letrec ())"
477 (interaction-environment)))
479 (pass-if-syntax-error "(letrec ((x 1)))"
481 (eval '(letrec ((x 1)))
482 (interaction-environment)))))
484 (with-test-prefix "letrec*"
486 (with-test-prefix "bindings"
488 (pass-if-syntax-error "initial bindings are undefined"
489 exception:used-before-defined
491 ;; FIXME: the memoizer does initialize the var to undefined, but
492 ;; the Scheme evaluator has no way of checking what's an
493 ;; undefined value. Not sure how to do this.
495 (letrec* ((x y) (y 1)) y))))
497 (with-test-prefix "bad bindings"
499 (pass-if-syntax-error "(letrec*)"
500 exception:bad-letrec*
502 (interaction-environment)))
504 (pass-if-syntax-error "(letrec* 1)"
505 exception:bad-letrec*
507 (interaction-environment)))
509 (pass-if-syntax-error "(letrec* (x))"
510 exception:bad-letrec*
512 (interaction-environment)))
514 (pass-if-syntax-error "(letrec* (x) 1)"
515 exception:bad-letrec*
516 (eval '(letrec* (x) 1)
517 (interaction-environment)))
519 (pass-if-syntax-error "(letrec* ((x)) 3)"
520 exception:bad-letrec*
521 (eval '(letrec* ((x)) 3)
522 (interaction-environment)))
524 (pass-if-syntax-error "(letrec* ((x 1) y) x)"
525 exception:bad-letrec*
526 (eval '(letrec* ((x 1) y) x)
527 (interaction-environment)))
529 (pass-if-syntax-error "(letrec* x ())"
530 exception:bad-letrec*
531 (eval '(letrec* x ())
532 (interaction-environment)))
534 (pass-if-syntax-error "(letrec* x (y))"
535 exception:bad-letrec*
536 (eval '(letrec* x (y))
537 (interaction-environment)))
539 (pass-if-syntax-error "(letrec* ((1 2)) 3)"
540 exception:bad-letrec*
541 (eval '(letrec* ((1 2)) 3)
542 (interaction-environment))))
544 (with-test-prefix "duplicate bindings"
546 (pass-if-syntax-error "(letrec* ((x 1) (x 2)) x)"
547 exception:duplicate-binding
548 (eval '(letrec* ((x 1) (x 2)) x)
549 (interaction-environment))))
551 (with-test-prefix "bad body"
553 (pass-if-syntax-error "(letrec* ())"
554 exception:bad-letrec*
556 (interaction-environment)))
558 (pass-if-syntax-error "(letrec* ((x 1)))"
559 exception:bad-letrec*
560 (eval '(letrec* ((x 1)))
561 (interaction-environment))))
563 (with-test-prefix "referencing previous values"
564 (pass-if (equal? (letrec ((a (cons 'foo 'bar))
568 (pass-if (equal? (let ()
569 (define a (cons 'foo 'bar))
574 (with-test-prefix "if"
576 (with-test-prefix "missing or extra expressions"
578 (pass-if-syntax-error "(if)"
579 exception:generic-syncase-error
581 (interaction-environment)))
583 (pass-if-syntax-error "(if 1 2 3 4)"
584 exception:generic-syncase-error
586 (interaction-environment)))))
588 (with-test-prefix "cond"
590 (with-test-prefix "cond is hygienic"
592 (pass-if "bound 'else is handled correctly"
593 (eq? (let ((else 'ok)) (cond (else))) 'ok))
595 (with-test-prefix "bound '=> is handled correctly"
599 (eq? (cond (#t => 'ok)) 'ok)))
603 (eq? (cond (else =>)) 'foo)))
605 (pass-if "else => identity"
607 (eq? (cond (else => identity)) identity)))))
609 (with-test-prefix "SRFI-61"
611 (pass-if "always available"
612 (cond-expand (srfi-61 #t) (else #f)))
614 (pass-if "single value consequent"
615 (eq? 'ok (cond (#t identity => (lambda (x) 'ok)) (else #f))))
617 (pass-if "single value alternate"
618 (eq? 'ok (cond (#t not => (lambda (x) #f)) (else 'ok))))
620 (pass-if-exception "doesn't affect standard =>"
621 exception:wrong-num-args
622 (cond ((values 1 2) => (lambda (x y) #t))))
624 (pass-if "multiple values consequent"
625 (equal? '(2 1) (cond ((values 1 2)
627 (and (= 1 one) (= 2 two))) =>
628 (lambda (one two) (list two one)))
631 (pass-if "multiple values alternate"
632 (eq? 'ok (cond ((values 2 3 4)
633 (lambda args (equal? '(1 2 3) args)) =>
637 (pass-if "zero values"
638 (eq? 'ok (cond ((values) (lambda () #t) => (lambda () 'ok))
641 (pass-if "bound => is handled correctly"
643 (eq? 'ok (cond (#t identity =>) (else #f)))))
645 (pass-if-syntax-error "missing recipient"
646 '(cond . "wrong number of receiver expressions")
647 (cond (#t identity =>)))
649 (pass-if-syntax-error "extra recipient"
650 '(cond . "wrong number of receiver expressions")
651 (cond (#t identity => identity identity))))
653 (with-test-prefix "bad or missing clauses"
655 (pass-if-syntax-error "(cond)"
656 exception:generic-syncase-error
658 (interaction-environment)))
660 (pass-if-syntax-error "(cond #t)"
661 exception:generic-syncase-error
663 (interaction-environment)))
665 (pass-if-syntax-error "(cond 1)"
666 exception:generic-syncase-error
668 (interaction-environment)))
670 (pass-if-syntax-error "(cond 1 2)"
671 exception:generic-syncase-error
673 (interaction-environment)))
675 (pass-if-syntax-error "(cond 1 2 3)"
676 exception:generic-syncase-error
678 (interaction-environment)))
680 (pass-if-syntax-error "(cond 1 2 3 4)"
681 exception:generic-syncase-error
682 (eval '(cond 1 2 3 4)
683 (interaction-environment)))
685 (pass-if-syntax-error "(cond ())"
686 exception:generic-syncase-error
688 (interaction-environment)))
690 (pass-if-syntax-error "(cond () 1)"
691 exception:generic-syncase-error
693 (interaction-environment)))
695 (pass-if-syntax-error "(cond (1) 1)"
696 exception:generic-syncase-error
698 (interaction-environment))))
700 (with-test-prefix "wrong number of arguments"
702 (pass-if-exception "=> (lambda (x y) #t)"
703 exception:wrong-num-args
704 (cond (1 => (lambda (x y) #t))))))
706 (with-test-prefix "case"
708 (pass-if "clause with empty labels list"
709 (case 1 (() #f) (else #t)))
711 (with-test-prefix "case is hygienic"
713 (pass-if-syntax-error "bound 'else is handled correctly"
714 exception:generic-syncase-error
715 (eval '(let ((else #f)) (case 1 (else #f)))
716 (interaction-environment))))
718 (with-test-prefix "bad or missing clauses"
720 (pass-if-syntax-error "(case)"
721 exception:generic-syncase-error
723 (interaction-environment)))
725 (pass-if-syntax-error "(case . \"foo\")"
726 exception:generic-syncase-error
727 (eval '(case . "foo")
728 (interaction-environment)))
730 (pass-if-syntax-error "(case 1)"
731 exception:generic-syncase-error
733 (interaction-environment)))
735 (pass-if-syntax-error "(case 1 . \"foo\")"
736 exception:generic-syncase-error
737 (eval '(case 1 . "foo")
738 (interaction-environment)))
740 (pass-if-syntax-error "(case 1 \"foo\")"
741 exception:generic-syncase-error
742 (eval '(case 1 "foo")
743 (interaction-environment)))
745 (pass-if-syntax-error "(case 1 ())"
746 exception:generic-syncase-error
748 (interaction-environment)))
750 (pass-if-syntax-error "(case 1 (\"foo\"))"
751 exception:generic-syncase-error
752 (eval '(case 1 ("foo"))
753 (interaction-environment)))
755 (pass-if-syntax-error "(case 1 (\"foo\" \"bar\"))"
756 exception:generic-syncase-error
757 (eval '(case 1 ("foo" "bar"))
758 (interaction-environment)))
760 (pass-if-syntax-error "(case 1 ((2) \"bar\") . \"foo\")"
761 exception:generic-syncase-error
762 (eval '(case 1 ((2) "bar") . "foo")
763 (interaction-environment)))
765 (pass-if-syntax-error "(case 1 ((2) \"bar\") (else))"
766 exception:generic-syncase-error
767 (eval '(case 1 ((2) "bar") (else))
768 (interaction-environment)))
770 (pass-if-syntax-error "(case 1 (else #f) . \"foo\")"
771 exception:generic-syncase-error
772 (eval '(case 1 (else #f) . "foo")
773 (interaction-environment)))
775 (pass-if-syntax-error "(case 1 (else #f) ((1) #t))"
776 exception:generic-syncase-error
777 (eval '(case 1 (else #f) ((1) #t))
778 (interaction-environment)))))
780 (with-test-prefix "top-level define"
782 (pass-if "redefinition"
783 (let ((m (make-module)))
784 (beautify-user-module! m)
786 ;; The previous value of `round' must still be visible at the time the
787 ;; new `round' is defined. According to R5RS (Section 5.2.1), `define'
788 ;; should behave like `set!' in this case (except that in the case of
789 ;; Guile, we respect module boundaries).
790 (eval '(define round round) m)
791 (eq? (module-ref m 'round) round)))
793 (with-test-prefix "missing or extra expressions"
795 (pass-if-syntax-error "(define)"
796 exception:generic-syncase-error
798 (interaction-environment))))
800 (pass-if "module scoping"
804 (define-module (top-level-define/module-scoping-1)
805 #:export (define-10))
806 (define-syntax-rule (define-10 name)
810 (define-module (top-level-define/module-scoping-2)
811 #:use-module (top-level-define/module-scoping-1))
817 (pass-if "module scoping, same symbolic name"
821 (define-module (top-level-define/module-scoping-3))
823 (define-module (top-level-define/module-scoping-4)
824 #:use-module (top-level-define/module-scoping-3))
825 (define a (@@ (top-level-define/module-scoping-3) a))
830 (pass-if "module scoping, introduced names"
834 (define-module (top-level-define/module-scoping-5)
835 #:export (define-constant))
836 (define-syntax-rule (define-constant name val)
840 (define-module (top-level-define/module-scoping-6)
841 #:use-module (top-level-define/module-scoping-5))
842 (define-constant foo 10)
843 (define-constant bar 20)
848 (pass-if "module scoping, duplicate introduced name"
852 (define-module (top-level-define/module-scoping-7)
853 #:export (define-constant))
854 (define-syntax-rule (define-constant name val)
858 (define-module (top-level-define/module-scoping-8)
859 #:use-module (top-level-define/module-scoping-7))
860 (define-constant foo 10)
861 (define-constant foo 20)
866 (with-test-prefix "internal define"
868 (pass-if "internal defines become letrec"
869 (eval '(let ((a identity) (b identity) (c identity))
870 (define (a x) (if (= x 0) 'a (b (- x 1))))
871 (define (b x) (if (= x 0) 'b (c (- x 1))))
872 (define (c x) (if (= x 0) 'c (a (- x 1))))
873 (and (eq? 'a (a 0) (a 3))
875 (eq? 'c (a 2) (a 5))))
876 (interaction-environment)))
878 (pass-if "binding is created before expression is evaluated"
879 ;; Internal defines are equivalent to `letrec' (R5RS, Section 5.2.2).
886 (interaction-environment))
889 (pass-if "internal defines with begin"
891 (eval '(let ((a identity) (b identity) (c identity))
892 (define (a x) (if (= x 0) 'a (b (- x 1))))
894 (define (b x) (if (= x 0) 'b (c (- x 1)))))
895 (define (c x) (if (= x 0) 'c (a (- x 1))))
896 (and (eq? 'a (a 0) (a 3))
898 (eq? 'c (a 2) (a 5))))
899 (interaction-environment))))
901 (pass-if "internal defines with empty begin"
903 (eval '(let ((a identity) (b identity) (c identity))
904 (define (a x) (if (= x 0) 'a (b (- x 1))))
906 (define (b x) (if (= x 0) 'b (c (- x 1))))
907 (define (c x) (if (= x 0) 'c (a (- x 1))))
908 (and (eq? 'a (a 0) (a 3))
910 (eq? 'c (a 2) (a 5))))
911 (interaction-environment))))
913 (pass-if "internal defines with macro application"
916 (defmacro my-define forms
917 (cons 'define forms))
918 (let ((a identity) (b identity) (c identity))
919 (define (a x) (if (= x 0) 'a (b (- x 1))))
920 (my-define (b x) (if (= x 0) 'b (c (- x 1))))
921 (define (c x) (if (= x 0) 'c (a (- x 1))))
922 (and (eq? 'a (a 0) (a 3))
924 (eq? 'c (a 2) (a 5)))))
925 (interaction-environment))))
927 (pass-if-syntax-error "missing body expression"
928 exception:missing-body-expr
929 (eval '(let () (define x #t))
930 (interaction-environment))))
932 (with-test-prefix "set!"
934 (with-test-prefix "missing or extra expressions"
936 (pass-if-syntax-error "(set!)"
939 (interaction-environment)))
941 (pass-if-syntax-error "(set! 1)"
944 (interaction-environment)))
946 (pass-if-syntax-error "(set! 1 2 3)"
949 (interaction-environment))))
951 (with-test-prefix "bad variable"
953 (pass-if-syntax-error "(set! \"\" #t)"
956 (interaction-environment)))
958 (pass-if-syntax-error "(set! 1 #t)"
961 (interaction-environment)))
963 (pass-if-syntax-error "(set! #t #f)"
966 (interaction-environment)))
968 (pass-if-syntax-error "(set! #f #t)"
971 (interaction-environment)))
973 (pass-if-syntax-error "(set! #\\space #f)"
975 (eval '(set! #\space #f)
976 (interaction-environment)))))
978 (with-test-prefix "quote"
980 (with-test-prefix "missing or extra expression"
982 (pass-if-syntax-error "(quote)"
985 (interaction-environment)))
987 (pass-if-syntax-error "(quote a b)"
990 (interaction-environment)))))
992 (with-test-prefix "while"
994 (define (unreachable)
995 (error "unreachable code has been reached!"))
997 ;; Return a new procedure COND which when called (COND) will return #t the
998 ;; first N times, then #f, then any further call is an error. N=0 is
999 ;; allowed, in which case #f is returned by the first call.
1000 (define (make-iterations-cond n)
1003 (error "oops, condition re-tested after giving false"))
1012 (pass-if-syntax-error "too few args" exception:generic-syncase-error
1013 (eval '(while) (interaction-environment)))
1015 (with-test-prefix "empty body"
1019 (eval `(letrec ((make-iterations-cond
1023 (error "oops, condition re-tested after giving false"))
1030 (let ((cond (make-iterations-cond ,n)))
1033 (interaction-environment)))))
1035 (pass-if "initially false"
1040 (with-test-prefix "iterations"
1044 (let ((cond (make-iterations-cond n))
1050 (with-test-prefix "break"
1052 (pass-if "normal return"
1053 (not (while #f (error "not reached"))))
1058 (pass-if "multiple values"
1061 (lambda () (while #t (break 1 2 3)))
1064 (with-test-prefix "from cond"
1075 (let ((cond (make-iterations-cond n))
1085 (with-test-prefix "from body"
1095 (let ((cond (make-iterations-cond n))
1105 (pass-if "from nested"
1107 (let ((outer-break break))
1114 (pass-if "from recursive"
1115 (let ((outer-break #f))
1120 (set! outer-break break)
1126 (error "broke only from inner loop")))
1130 (with-test-prefix "continue"
1132 (pass-if-syntax-error "too many args" exception:too-many-args
1135 (interaction-environment)))
1137 (with-test-prefix "from cond"
1141 (let ((cond (make-iterations-cond n))
1152 (with-test-prefix "from body"
1156 (let ((cond (make-iterations-cond n))
1164 (pass-if "from nested"
1165 (let ((cond (make-iterations-cond 3)))
1167 (let ((outer-continue continue))
1173 (pass-if "from recursive"
1174 (let ((outer-continue #f))
1176 (let ((cond (make-iterations-cond 3))
1179 (if (and (not first)
1181 (error "continued only to inner loop"))
1186 (set! outer-continue continue)