3 ;;;; Copyright (C) 2001, 2003, 2006, 2009, 2010, 2011,
4 ;;;; 2012 Free Software Foundation, Inc.
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.
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.
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
22 ;;; Portable implementation of syntax-case
23 ;;; Originally extracted from Chez Scheme Version 5.9f
24 ;;; Authors: R. Kent Dybvig, Oscar Waddell, Bob Hieb, Carl Bruggeman
26 ;;; Copyright (c) 1992-1997 Cadence Research Systems
27 ;;; Permission to copy this software, in whole or in part, to use this
28 ;;; software for any lawful purpose, and to redistribute this software
29 ;;; is granted subject to the restriction that all copies made of this
30 ;;; software must include this copyright notice in full. This software
31 ;;; is provided AS IS, with NO WARRANTY, EITHER EXPRESS OR IMPLIED,
32 ;;; INCLUDING BUT NOT LIMITED TO IMPLIED WARRANTIES OF MERCHANTABILITY
33 ;;; OR FITNESS FOR ANY PARTICULAR PURPOSE. IN NO EVENT SHALL THE
34 ;;; AUTHORS BE LIABLE FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES OF ANY
35 ;;; NATURE WHATSOEVER.
37 ;;; Modified by Mikael Djurfeldt <djurfeldt@nada.kth.se> according
38 ;;; to the ChangeLog distributed in the same directory as this file:
39 ;;; 1997-08-19, 1997-09-03, 1997-09-10, 2000-08-13, 2000-08-24,
40 ;;; 2000-09-12, 2001-03-08
42 ;;; Modified by Andy Wingo <wingo@pobox.com> according to the Git
43 ;;; revision control logs corresponding to this file: 2009, 2010.
46 ;;; This file defines the syntax-case expander, macroexpand, and a set
47 ;;; of associated syntactic forms and procedures. Of these, the
48 ;;; following are documented in The Scheme Programming Language,
49 ;;; Fourth Edition (R. Kent Dybvig, MIT Press, 2009), and in the
52 ;;; bound-identifier=?
55 ;;; syntax-parameterize
57 ;;; generate-temporaries
68 ;;; Additionally, the expander provides definitions for a number of core
69 ;;; Scheme syntactic bindings, such as `let', `lambda', and the like.
71 ;;; The remaining exports are listed below:
73 ;;; (macroexpand datum)
74 ;;; if datum represents a valid expression, macroexpand returns an
75 ;;; expanded version of datum in a core language that includes no
76 ;;; syntactic abstractions. The core language includes begin,
77 ;;; define, if, lambda, letrec, quote, and set!.
78 ;;; (eval-when situations expr ...)
79 ;;; conditionally evaluates expr ... at compile-time or run-time
80 ;;; depending upon situations (see the Chez Scheme System Manual,
81 ;;; Revision 3, for a complete description)
82 ;;; (syntax-violation who message form [subform])
83 ;;; used to report errors found during expansion
84 ;;; ($sc-dispatch e p)
85 ;;; used by expanded code to handle syntax-case matching
87 ;;; This file is shipped along with an expanded version of itself,
88 ;;; psyntax-pp.scm, which is loaded when psyntax.scm has not yet been
89 ;;; compiled. In this way, psyntax bootstraps off of an expanded
90 ;;; version of itself.
92 ;;; This implementation of the expander sometimes uses syntactic
93 ;;; abstractions when procedural abstractions would suffice. For
94 ;;; example, we define top-wrap and top-marked? as
96 ;;; (define-syntax top-wrap (identifier-syntax '((top))))
97 ;;; (define-syntax top-marked?
99 ;;; ((_ w) (memq 'top (wrap-marks w)))))
103 ;;; (define top-wrap '((top)))
104 ;;; (define top-marked?
105 ;;; (lambda (w) (memq 'top (wrap-marks w))))
107 ;;; On the other hand, we don't do this consistently; we define
108 ;;; make-wrap, wrap-marks, and wrap-subst simply as
110 ;;; (define make-wrap cons)
111 ;;; (define wrap-marks car)
112 ;;; (define wrap-subst cdr)
114 ;;; In Chez Scheme, the syntactic and procedural forms of these
115 ;;; abstractions are equivalent, since the optimizer consistently
116 ;;; integrates constants and small procedures. This will be true of
117 ;;; Guile as well, once we implement a proper inliner.
120 ;;; Implementation notes:
122 ;;; Objects with no standard print syntax, including objects containing
123 ;;; cycles and syntax object, are allowed in quoted data as long as they
124 ;;; are contained within a syntax form or produced by datum->syntax.
125 ;;; Such objects are never copied.
127 ;;; All identifiers that don't have macro definitions and are not bound
128 ;;; lexically are assumed to be global variables.
130 ;;; Top-level definitions of macro-introduced identifiers are allowed.
131 ;;; This may not be appropriate for implementations in which the
132 ;;; model is that bindings are created by definitions, as opposed to
133 ;;; one in which initial values are assigned by definitions.
135 ;;; Identifiers and syntax objects are implemented as vectors for
136 ;;; portability. As a result, it is possible to "forge" syntax objects.
138 ;;; The implementation of generate-temporaries assumes that it is
139 ;;; possible to generate globally unique symbols (gensyms).
141 ;;; The source location associated with incoming expressions is tracked
142 ;;; via the source-properties mechanism, a weak map from expression to
143 ;;; source information. At times the source is separated from the
144 ;;; expression; see the note below about "efficiency and confusion".
149 ;;; When changing syntax-object representations, it is necessary to support
150 ;;; both old and new syntax-object representations in id-var-name. It
151 ;;; should be sufficient to recognize old representations and treat
152 ;;; them as not lexically bound.
157 (set-current-module (resolve-module '(guile))))
160 (define-syntax define-expansion-constructors
164 (let lp ((n 0) (out '()))
165 (if (< n (vector-length %expanded-vtables))
167 (let* ((vtable (vector-ref %expanded-vtables n))
168 (stem (struct-ref vtable (+ vtable-offset-user 0)))
169 (fields (struct-ref vtable (+ vtable-offset-user 2)))
170 (sfields (map (lambda (f) (datum->syntax x f)) fields))
171 (ctor (datum->syntax x (symbol-append 'make- stem))))
172 (cons #`(define (#,ctor #,@sfields)
173 (make-struct (vector-ref %expanded-vtables #,n) 0
176 #`(begin #,@(reverse out))))))))
178 (define-syntax define-expansion-accessors
183 (let ((vtable (vector-ref %expanded-vtables n))
184 (stem (syntax->datum #'stem)))
185 (if (eq? (struct-ref vtable (+ vtable-offset-user 0)) stem)
187 (define (#,(datum->syntax x (symbol-append stem '?)) x)
189 (eq? (struct-vtable x)
190 (vector-ref %expanded-vtables #,n))))
193 (let ((get (datum->syntax x (symbol-append stem '- f)))
194 (set (datum->syntax x (symbol-append 'set- stem '- f '!)))
195 (idx (list-index (struct-ref vtable
196 (+ vtable-offset-user 2))
200 (struct-ref x #,idx))
202 (struct-set! x #,idx v)))))
203 (syntax->datum #'(field ...))))
206 (define-syntax define-structure
208 (define construct-name
209 (lambda (template-identifier . args)
217 (symbol->string (syntax->datum x))))
221 (and-map identifier? #'(name id1 ...))
223 ((constructor (construct-name #'name "make-" #'name))
224 (predicate (construct-name #'name #'name "?"))
226 (map (lambda (x) (construct-name x #'name "-" x))
230 (construct-name x "set-" #'name "-" x "!"))
233 (+ (length #'(id1 ...)) 1))
235 (let f ((i 1) (ids #'(id1 ...)))
238 (cons i (f (+ i 1) (cdr ids)))))))
242 (vector 'name id1 ... )))
246 (= (vector-length x) structure-length)
247 (eq? (vector-ref x 0) 'name))))
250 (vector-ref x index)))
254 (vector-set! x index update)))
258 (define-expansion-constructors)
259 (define-expansion-accessors lambda meta)
261 ;; hooks to nonportable run-time helpers
263 (define-syntax fx+ (identifier-syntax +))
264 (define-syntax fx- (identifier-syntax -))
265 (define-syntax fx= (identifier-syntax =))
266 (define-syntax fx< (identifier-syntax <))
268 (define top-level-eval-hook
272 (define local-eval-hook
276 ;; Capture syntax-session-id before we shove it off into a module.
278 (let ((v (module-variable (current-module) 'syntax-session-id)))
280 ((variable-ref v)))))
282 (define put-global-definition-hook
283 (lambda (symbol type val)
284 (module-define! (current-module)
286 (make-syntax-transformer symbol type val))))
288 (define get-global-definition-hook
289 (lambda (symbol module)
290 (if (and (not module) (current-module))
291 (warn "module system is booted, we should have a module" symbol))
292 (let ((v (module-variable (if module
293 (resolve-module (cdr module))
296 (and v (variable-bound? v)
297 (let ((val (variable-ref v)))
298 (and (macro? val) (macro-type val)
299 (cons (macro-type val)
300 (macro-binding val)))))))))
303 (define (decorate-source e s)
304 (if (and s (supports-source-properties? e))
305 (set-source-properties! e s))
308 (define (maybe-name-value! name val)
310 (let ((meta (lambda-meta val)))
311 (if (not (assq 'name meta))
312 (set-lambda-meta! val (acons 'name name meta))))))
314 ;; output constructors
319 (define build-application
320 (lambda (source fun-exp arg-exps)
321 (make-application source fun-exp arg-exps)))
323 (define build-conditional
324 (lambda (source test-exp then-exp else-exp)
325 (make-conditional source test-exp then-exp else-exp)))
328 (lambda (source fluids vals body)
329 (make-dynlet source fluids vals body)))
331 (define build-lexical-reference
332 (lambda (type source name var)
333 (make-lexical-ref source name var)))
335 (define build-lexical-assignment
336 (lambda (source name var exp)
337 (maybe-name-value! name exp)
338 (make-lexical-set source name var exp)))
340 (define (analyze-variable mod var modref-cont bare-cont)
343 (let ((kind (car mod))
346 ((public) (modref-cont mod var #t))
347 ((private) (if (not (equal? mod (module-name (current-module))))
348 (modref-cont mod var #f)
350 ((bare) (bare-cont var))
351 ((hygiene) (if (and (not (equal? mod (module-name (current-module))))
352 (module-variable (resolve-module mod) var))
353 (modref-cont mod var #f)
355 (else (syntax-violation #f "bad module kind" var mod))))))
357 (define build-global-reference
358 (lambda (source var mod)
361 (lambda (mod var public?)
362 (make-module-ref source mod var public?))
364 (make-toplevel-ref source var)))))
366 (define build-global-assignment
367 (lambda (source var exp mod)
368 (maybe-name-value! var exp)
371 (lambda (mod var public?)
372 (make-module-set source mod var public? exp))
374 (make-toplevel-set source var exp)))))
376 (define build-global-definition
377 (lambda (source var exp)
378 (maybe-name-value! var exp)
379 (make-toplevel-define source var exp)))
381 (define build-simple-lambda
382 (lambda (src req rest vars meta exp)
385 ;; hah, a case in which kwargs would be nice.
387 ;; src req opt rest kw inits vars body else
388 src req #f rest #f '() vars exp #f))))
390 (define build-case-lambda
391 (lambda (src meta body)
392 (make-lambda src meta body)))
394 (define build-lambda-case
396 ;; opt := (name ...) | #f
398 ;; kw := (allow-other-keys? (keyword name var) ...) | #f
401 ;; vars map to named arguments in the following order:
402 ;; required, optional (positional), rest, keyword.
403 ;; the body of a lambda: anything, already expanded
404 ;; else: lambda-case | #f
405 (lambda (src req opt rest kw inits vars body else-case)
406 (make-lambda-case src req opt rest kw inits vars body else-case)))
408 (define build-primref
410 (if (equal? (module-name (current-module)) '(guile))
411 (make-toplevel-ref src name)
412 (make-module-ref src '(guile) name #f))))
414 (define (build-data src exp)
415 (make-const src exp))
417 (define build-sequence
419 (if (null? (cdr exps))
421 (make-sequence src exps))))
424 (lambda (src ids vars val-exps body-exp)
425 (for-each maybe-name-value! ids val-exps)
428 (make-let src ids vars val-exps body-exp))))
430 (define build-named-let
431 (lambda (src ids vars val-exps body-exp)
436 (let ((proc (build-simple-lambda src ids #f vars '() body-exp)))
437 (maybe-name-value! f-name proc)
438 (for-each maybe-name-value! ids val-exps)
441 (list f-name) (list f) (list proc)
442 (build-application src (build-lexical-reference 'fun src f-name f)
446 (lambda (src in-order? ids vars val-exps body-exp)
450 (for-each maybe-name-value! ids val-exps)
451 (make-letrec src in-order? ids vars val-exps body-exp)))))
454 ;; FIXME: use a faster gensym
455 (define-syntax-rule (build-lexical-var src id)
456 (gensym (string-append (symbol->string id) "-")))
458 (define-structure (syntax-object expression wrap module))
460 (define-syntax no-source (identifier-syntax #f))
462 (define source-annotation
464 (let ((props (source-properties
465 (if (syntax-object? x)
466 (syntax-object-expression x)
468 (and (pair? props) props))))
470 (define-syntax-rule (arg-check pred? e who)
472 (if (not (pred? x)) (syntax-violation who "invalid argument" x))))
474 ;; compile-time environments
476 ;; wrap and environment comprise two level mapping.
477 ;; wrap : id --> label
478 ;; env : label --> <element>
480 ;; environments are represented in two parts: a lexical part and a global
481 ;; part. The lexical part is a simple list of associations from labels
482 ;; to bindings. The global part is implemented by
483 ;; {put,get}-global-definition-hook and associates symbols with
486 ;; global (assumed global variable) and displaced-lexical (see below)
487 ;; do not show up in any environment; instead, they are fabricated by
488 ;; lookup when it finds no other bindings.
490 ;; <environment> ::= ((<label> . <binding>)*)
492 ;; identifier bindings include a type and a value
494 ;; <binding> ::= (macro . <procedure>) macros
495 ;; (core . <procedure>) core forms
496 ;; (module-ref . <procedure>) @ or @@
499 ;; (define-syntax) define-syntax
500 ;; (define-syntax-parameter) define-syntax-parameter
501 ;; (local-syntax . rec?) let-syntax/letrec-syntax
502 ;; (eval-when) eval-when
503 ;; (syntax . (<var> . <level>)) pattern variables
504 ;; (global) assumed global variable
505 ;; (lexical . <var>) lexical variables
506 ;; (displaced-lexical) displaced lexicals
507 ;; <level> ::= <nonnegative integer>
508 ;; <var> ::= variable returned by build-lexical-var
510 ;; a macro is a user-defined syntactic-form. a core is a
511 ;; system-defined syntactic form. begin, define, define-syntax,
512 ;; define-syntax-parameter, and eval-when are treated specially
513 ;; since they are sensitive to whether the form is at top-level and
514 ;; (except for eval-when) can denote valid internal definitions.
516 ;; a pattern variable is a variable introduced by syntax-case and can
517 ;; be referenced only within a syntax form.
519 ;; any identifier for which no top-level syntax definition or local
520 ;; binding of any kind has been seen is assumed to be a global
523 ;; a lexical variable is a lambda- or letrec-bound variable.
525 ;; a displaced-lexical identifier is a lexical identifier removed from
526 ;; it's scope by the return of a syntax object containing the identifier.
527 ;; a displaced lexical can also appear when a letrec-syntax-bound
528 ;; keyword is referenced on the rhs of one of the letrec-syntax clauses.
529 ;; a displaced lexical should never occur with properly written macros.
531 (define-syntax make-binding
532 (syntax-rules (quote)
533 ((_ type value) (cons type value))
535 ((_ type) (cons type '()))))
536 (define-syntax-rule (binding-type x)
538 (define-syntax-rule (binding-value x)
541 (define-syntax null-env (identifier-syntax '()))
544 (lambda (labels bindings r)
547 (extend-env (cdr labels) (cdr bindings)
548 (cons (cons (car labels) (car bindings)) r)))))
550 (define extend-var-env
551 ;; variant of extend-env that forms "lexical" binding
552 (lambda (labels vars r)
555 (extend-var-env (cdr labels) (cdr vars)
556 (cons (cons (car labels) (make-binding 'lexical (car vars))) r)))))
558 ;; we use a "macros only" environment in expansion of local macro
559 ;; definitions so that their definitions can use local macros without
560 ;; attempting to use other lexical identifiers.
561 (define macros-only-env
566 (if (eq? (cadr a) 'macro)
567 (cons a (macros-only-env (cdr r)))
568 (macros-only-env (cdr r)))))))
571 ;; x may be a label or a symbol
572 ;; although symbols are usually global, we check the environment first
573 ;; anyway because a temporary binding may have been established by
579 (or (get-global-definition-hook x mod) (make-binding 'global)))
580 (else (make-binding 'displaced-lexical)))))
582 (define global-extend
583 (lambda (type sym val)
584 (put-global-definition-hook sym type val)))
587 ;; Conceptually, identifiers are always syntax objects. Internally,
588 ;; however, the wrap is sometimes maintained separately (a source of
589 ;; efficiency and confusion), so that symbols are also considered
590 ;; identifiers by id?. Externally, they are always wrapped.
592 (define nonsymbol-id?
594 (and (syntax-object? x)
595 (symbol? (syntax-object-expression x)))))
601 ((syntax-object? x) (symbol? (syntax-object-expression x)))
604 (define-syntax-rule (id-sym-name e)
606 (if (syntax-object? x)
607 (syntax-object-expression x)
610 (define id-sym-name&marks
612 (if (syntax-object? x)
614 (syntax-object-expression x)
615 (join-marks (wrap-marks w) (wrap-marks (syntax-object-wrap x))))
616 (values x (wrap-marks w)))))
618 ;; syntax object wraps
620 ;; <wrap> ::= ((<mark> ...) . (<subst> ...))
621 ;; <subst> ::= <shift> | <subs>
622 ;; <subs> ::= #(<old name> <label> (<mark> ...))
623 ;; <shift> ::= positive fixnum
625 (define-syntax make-wrap (identifier-syntax cons))
626 (define-syntax wrap-marks (identifier-syntax car))
627 (define-syntax wrap-subst (identifier-syntax cdr))
629 (define-syntax subst-rename? (identifier-syntax vector?))
630 (define-syntax-rule (rename-old x) (vector-ref x 0))
631 (define-syntax-rule (rename-new x) (vector-ref x 1))
632 (define-syntax-rule (rename-marks x) (vector-ref x 2))
633 (define-syntax-rule (make-rename old new marks)
634 (vector old new marks))
636 ;; labels must be comparable with "eq?", have read-write invariance,
637 ;; and distinct from symbols.
639 (string-append "l-" (session-id) (symbol->string (gensym "-"))))
645 (cons (gen-label) (gen-labels (cdr ls))))))
647 (define-structure (ribcage symnames marks labels))
649 (define-syntax empty-wrap (identifier-syntax '(())))
651 (define-syntax top-wrap (identifier-syntax '((top))))
653 (define-syntax-rule (top-marked? w)
654 (memq 'top (wrap-marks w)))
656 ;; Marks must be comparable with "eq?" and distinct from pairs and
657 ;; the symbol top. We do not use integers so that marks will remain
658 ;; unique even across file compiles.
660 (define-syntax the-anti-mark (identifier-syntax #f))
664 (make-wrap (cons the-anti-mark (wrap-marks w))
665 (cons 'shift (wrap-subst w)))))
667 (define-syntax-rule (new-mark)
668 (gensym (string-append "m-" (session-id) "-")))
670 ;; make-empty-ribcage and extend-ribcage maintain list-based ribcages for
671 ;; internal definitions, in which the ribcages are built incrementally
672 (define-syntax-rule (make-empty-ribcage)
673 (make-ribcage '() '() '()))
675 (define extend-ribcage!
676 ;; must receive ids with complete wraps
677 (lambda (ribcage id label)
678 (set-ribcage-symnames! ribcage
679 (cons (syntax-object-expression id)
680 (ribcage-symnames ribcage)))
681 (set-ribcage-marks! ribcage
682 (cons (wrap-marks (syntax-object-wrap id))
683 (ribcage-marks ribcage)))
684 (set-ribcage-labels! ribcage
685 (cons label (ribcage-labels ribcage)))))
687 ;; make-binding-wrap creates vector-based ribcages
688 (define make-binding-wrap
689 (lambda (ids labels w)
695 (let ((labelvec (list->vector labels)))
696 (let ((n (vector-length labelvec)))
697 (let ((symnamevec (make-vector n)) (marksvec (make-vector n)))
698 (let f ((ids ids) (i 0))
699 (if (not (null? ids))
701 (lambda () (id-sym-name&marks (car ids) w))
702 (lambda (symname marks)
703 (vector-set! symnamevec i symname)
704 (vector-set! marksvec i marks)
705 (f (cdr ids) (fx+ i 1))))))
706 (make-ribcage symnamevec marksvec labelvec))))
717 (let ((m1 (wrap-marks w1)) (s1 (wrap-subst w1)))
723 (smart-append s1 (wrap-subst w2))))
725 (smart-append m1 (wrap-marks w2))
726 (smart-append s1 (wrap-subst w2)))))))
730 (smart-append m1 m2)))
737 (eq? (car x) (car y))
738 (same-marks? (cdr x) (cdr y))))))
742 (define-syntax-rule (first e)
743 ;; Rely on Guile's multiple-values truncation.
746 (lambda (sym subst marks)
749 (let ((fst (car subst)))
751 (search sym (cdr subst) (cdr marks))
752 (let ((symnames (ribcage-symnames fst)))
753 (if (vector? symnames)
754 (search-vector-rib sym subst marks symnames fst)
755 (search-list-rib sym subst marks symnames fst))))))))
756 (define search-list-rib
757 (lambda (sym subst marks symnames ribcage)
758 (let f ((symnames symnames) (i 0))
760 ((null? symnames) (search sym (cdr subst) marks))
761 ((and (eq? (car symnames) sym)
762 (same-marks? marks (list-ref (ribcage-marks ribcage) i)))
763 (values (list-ref (ribcage-labels ribcage) i) marks))
764 (else (f (cdr symnames) (fx+ i 1)))))))
765 (define search-vector-rib
766 (lambda (sym subst marks symnames ribcage)
767 (let ((n (vector-length symnames)))
770 ((fx= i n) (search sym (cdr subst) marks))
771 ((and (eq? (vector-ref symnames i) sym)
772 (same-marks? marks (vector-ref (ribcage-marks ribcage) i)))
773 (values (vector-ref (ribcage-labels ribcage) i) marks))
774 (else (f (fx+ i 1))))))))
777 (or (first (search id (wrap-subst w) (wrap-marks w))) id))
779 (let ((id (syntax-object-expression id))
780 (w1 (syntax-object-wrap id)))
781 (let ((marks (join-marks (wrap-marks w) (wrap-marks w1))))
782 (call-with-values (lambda () (search id (wrap-subst w) marks))
783 (lambda (new-id marks)
785 (first (search id (wrap-subst w1) marks))
787 (else (syntax-violation 'id-var-name "invalid id" id)))))
789 ;; A helper procedure for syntax-locally-bound-identifiers, which
790 ;; itself is a helper for transformer procedures.
791 ;; `locally-bound-identifiers' returns a list of all bindings
792 ;; visible to a syntax object with the given wrap. They are in
793 ;; order from outer to inner.
795 ;; The purpose of this procedure is to give a transformer procedure
796 ;; references on bound identifiers, that the transformer can then
797 ;; introduce some of them in its output. As such, the identifiers
798 ;; are anti-marked, so that rebuild-macro-output doesn't apply new
801 (define locally-bound-identifiers
804 (lambda (subst results)
807 (let ((fst (car subst)))
809 (scan (cdr subst) results)
810 (let ((symnames (ribcage-symnames fst))
811 (marks (ribcage-marks fst)))
812 (if (vector? symnames)
813 (scan-vector-rib subst symnames marks results)
814 (scan-list-rib subst symnames marks results))))))))
815 (define scan-list-rib
816 (lambda (subst symnames marks results)
817 (let f ((symnames symnames) (marks marks) (results results))
819 (scan (cdr subst) results)
820 (f (cdr symnames) (cdr marks)
821 (cons (wrap (car symnames)
822 (anti-mark (make-wrap (car marks) subst))
825 (define scan-vector-rib
826 (lambda (subst symnames marks results)
827 (let ((n (vector-length symnames)))
828 (let f ((i 0) (results results))
830 (scan (cdr subst) results)
832 (cons (wrap (vector-ref symnames i)
833 (anti-mark (make-wrap (vector-ref marks i) subst))
836 (scan (wrap-subst w) '())))
838 ;; Returns three values: binding type, binding value, the module (for
839 ;; resolving toplevel vars).
840 (define (resolve-identifier id w r mod)
841 (define (resolve-global var mod)
842 (let ((b (or (get-global-definition-hook var mod)
843 (make-binding 'global))))
844 (if (eq? (binding-type b) 'global)
845 (values 'global var mod)
846 (values (binding-type b) (binding-value b) mod))))
847 (define (resolve-lexical label mod)
848 (let ((b (or (assq-ref r label)
849 (make-binding 'displaced-lexical))))
850 (values (binding-type b) (binding-value b) mod)))
851 (let ((n (id-var-name id w)))
854 (resolve-global n (if (syntax-object? id)
855 (syntax-object-module id)
858 (resolve-lexical n (if (syntax-object? id)
859 (syntax-object-module id)
862 (error "unexpected id-var-name" id w n)))))
864 (define transformer-environment
867 (error "called outside the dynamic extent of a syntax transformer"))))
869 (define (with-transformer-environment k)
870 ((fluid-ref transformer-environment) k))
872 ;; free-id=? must be passed fully wrapped ids since (free-id=? x y)
873 ;; may be true even if (free-id=? (wrap x w) (wrap y w)) is not.
877 (and (eq? (id-sym-name i) (id-sym-name j)) ; accelerator
878 (eq? (id-var-name i empty-wrap) (id-var-name j empty-wrap)))))
880 ;; bound-id=? may be passed unwrapped (or partially wrapped) ids as
881 ;; long as the missing portion of the wrap is common to both of the ids
882 ;; since (bound-id=? x y) iff (bound-id=? (wrap x w) (wrap y w))
886 (if (and (syntax-object? i) (syntax-object? j))
887 (and (eq? (syntax-object-expression i)
888 (syntax-object-expression j))
889 (same-marks? (wrap-marks (syntax-object-wrap i))
890 (wrap-marks (syntax-object-wrap j))))
893 ;; "valid-bound-ids?" returns #t if it receives a list of distinct ids.
894 ;; valid-bound-ids? may be passed unwrapped (or partially wrapped) ids
895 ;; as long as the missing portion of the wrap is common to all of the
898 (define valid-bound-ids?
900 (and (let all-ids? ((ids ids))
903 (all-ids? (cdr ids)))))
904 (distinct-bound-ids? ids))))
906 ;; distinct-bound-ids? expects a list of ids and returns #t if there are
907 ;; no duplicates. It is quadratic on the length of the id list; long
908 ;; lists could be sorted to make it more efficient. distinct-bound-ids?
909 ;; may be passed unwrapped (or partially wrapped) ids as long as the
910 ;; missing portion of the wrap is common to all of the ids.
912 (define distinct-bound-ids?
914 (let distinct? ((ids ids))
916 (and (not (bound-id-member? (car ids) (cdr ids)))
917 (distinct? (cdr ids)))))))
919 (define bound-id-member?
921 (and (not (null? list))
922 (or (bound-id=? x (car list))
923 (bound-id-member? x (cdr list))))))
925 ;; wrapping expressions and identifiers
930 ((and (null? (wrap-marks w)) (null? (wrap-subst w))) x)
933 (syntax-object-expression x)
934 (join-wraps w (syntax-object-wrap x))
935 (syntax-object-module x)))
937 (else (make-syntax-object x w defmod)))))
940 (lambda (x w s defmod)
941 (wrap (decorate-source x s) w defmod)))
945 (define expand-sequence
946 (lambda (body r w s mod)
948 (let dobody ((body body) (r r) (w w) (mod mod))
951 (let ((first (expand (car body) r w mod)))
952 (cons first (dobody (cdr body) r w mod))))))))
954 ;; At top-level, we allow mixed definitions and expressions. Like
955 ;; expand-body we expand in two passes.
957 ;; First, from left to right, we expand just enough to know what
958 ;; expressions are definitions, syntax definitions, and splicing
959 ;; statements (`begin'). If we anything needs evaluating at
960 ;; expansion-time, it is expanded directly.
962 ;; Otherwise we collect expressions to expand, in thunks, and then
963 ;; expand them all at the end. This allows all syntax expanders
964 ;; visible in a toplevel sequence to be visible during the
965 ;; expansions of all normal definitions and expressions in the
968 (define expand-top-sequence
969 (lambda (body r w s m esew mod)
970 (define (scan body r w s m esew mod exps)
980 (let ((e (car body)))
981 (syntax-type e r w (or (source-annotation e) s) #f mod #f)))
982 (lambda (type value form e w s mod)
988 (scan #'(e1 e2 ...) r w s m esew mod exps))))
990 (expand-local-syntax value e r w s mod
991 (lambda (body r w s mod)
992 (scan body r w s m esew mod exps))))
995 ((_ (x ...) e1 e2 ...)
996 (let ((when-list (parse-when-list e #'(x ...)))
997 (body #'(e1 e2 ...)))
1000 (if (memq 'eval when-list)
1002 (if (memq 'expand when-list) 'c&e 'e)
1006 (if (memq 'expand when-list)
1007 (top-level-eval-hook
1008 (expand-top-sequence body r w s 'e '(eval) mod)
1011 ((memq 'load when-list)
1012 (if (or (memq 'compile when-list)
1013 (memq 'expand when-list)
1014 (and (eq? m 'c&e) (memq 'eval when-list)))
1015 (scan body r w s 'c&e '(compile load) mod exps)
1016 (if (memq m '(c c&e))
1017 (scan body r w s 'c '(load) mod exps)
1019 ((or (memq 'compile when-list)
1020 (memq 'expand when-list)
1021 (and (eq? m 'c&e) (memq 'eval when-list)))
1022 (top-level-eval-hook
1023 (expand-top-sequence body r w s 'e '(eval) mod)
1028 ((define-syntax-form define-syntax-parameter-form)
1029 (let ((n (id-var-name value w)) (r (macros-only-env r)))
1032 (if (memq 'compile esew)
1033 (let ((e (expand-install-global n (expand e r w mod))))
1034 (top-level-eval-hook e mod)
1035 (if (memq 'load esew)
1036 (values (cons e exps))
1038 (if (memq 'load esew)
1039 (values (cons (expand-install-global n (expand e r w mod))
1043 (let ((e (expand-install-global n (expand e r w mod))))
1044 (top-level-eval-hook e mod)
1045 (values (cons e exps))))
1047 (if (memq 'eval esew)
1048 (top-level-eval-hook
1049 (expand-install-global n (expand e r w mod))
1053 (let* ((n (id-var-name value w))
1054 ;; Lookup the name in the module of the define form.
1055 (type (binding-type (lookup n r mod))))
1057 ((global core macro module-ref)
1058 ;; affect compile-time environment (once we have booted)
1059 (if (and (memq m '(c c&e))
1060 (not (module-local-variable (current-module) n))
1062 (let ((old (module-variable (current-module) n)))
1063 ;; use value of the same-named imported variable, if
1065 (if (and (variable? old) (variable-bound? old))
1066 (module-define! (current-module) n (variable-ref old))
1067 (module-add! (current-module) n (make-undefined-variable)))))
1071 (let ((x (build-global-definition s n (expand e r w mod))))
1072 (top-level-eval-hook x mod)
1075 (build-global-definition s n (expand e r w mod))))
1077 ((displaced-lexical)
1078 (syntax-violation #f "identifier out of context"
1079 (source-wrap form w s mod)
1080 (wrap value w mod)))
1082 (syntax-violation #f "cannot define keyword at top level"
1083 (source-wrap form w s mod)
1084 (wrap value w mod))))))
1088 (let ((x (expand-expr type value form e r w s mod)))
1089 (top-level-eval-hook x mod)
1092 (expand-expr type value form e r w s mod)))
1095 (scan (cdr body) r w s m esew mod exps))))))
1097 (call-with-values (lambda ()
1098 (scan body r w s m esew mod '()))
1104 (let lp ((in exps) (out '()))
1108 (cons (if (procedure? e) (e) e) out)))))))))))
1110 (define expand-install-global
1112 (build-global-definition
1117 (build-primref no-source 'make-syntax-transformer)
1118 (list (build-data no-source name)
1119 (build-data no-source 'macro)
1122 (define parse-when-list
1123 (lambda (e when-list)
1124 ;; when-list is syntax'd version of list of situations
1125 (let ((result (strip when-list empty-wrap)))
1126 (let lp ((l result))
1129 (if (memq (car l) '(compile load eval expand))
1131 (syntax-violation 'eval-when "invalid situation" e
1134 ;; syntax-type returns seven values: type, value, form, e, w, s, and
1135 ;; mod. The first two are described in the table below.
1137 ;; type value explanation
1138 ;; -------------------------------------------------------------------
1139 ;; core procedure core singleton
1140 ;; core-form procedure core form
1141 ;; module-ref procedure @ or @@ singleton
1142 ;; lexical name lexical variable reference
1143 ;; global name global variable reference
1144 ;; begin none begin keyword
1145 ;; define none define keyword
1146 ;; define-syntax none define-syntax keyword
1147 ;; define-syntax-parameter none define-syntax-parameter keyword
1148 ;; local-syntax rec? letrec-syntax/let-syntax keyword
1149 ;; eval-when none eval-when keyword
1150 ;; syntax level pattern variable
1151 ;; displaced-lexical none displaced lexical identifier
1152 ;; lexical-call name call to lexical variable
1153 ;; global-call name call to global variable
1154 ;; call none any other call
1155 ;; begin-form none begin expression
1156 ;; define-form id variable definition
1157 ;; define-syntax-form id syntax definition
1158 ;; define-syntax-parameter-form id syntax parameter definition
1159 ;; local-syntax-form rec? syntax definition
1160 ;; eval-when-form none eval-when form
1161 ;; constant none self-evaluating datum
1162 ;; other none anything else
1164 ;; form is the entire form. For definition forms (define-form,
1165 ;; define-syntax-form, and define-syntax-parameter-form), e is the
1166 ;; rhs expression. For all others, e is the entire form. w is the
1167 ;; wrap for both form and e. s is the source for the entire form.
1168 ;; mod is the module for both form and e.
1170 ;; syntax-type expands macros and unwraps as necessary to get to one
1171 ;; of the forms above. It also parses definition forms, although
1172 ;; perhaps this should be done by the consumer.
1175 (lambda (e r w s rib mod for-car?)
1178 (let* ((n (id-var-name e w))
1179 (b (lookup n r mod))
1180 (type (binding-type b)))
1182 ((lexical) (values type (binding-value b) e e w s mod))
1183 ((global) (values type n e e w s mod))
1186 (values type (binding-value b) e e w s mod)
1187 (syntax-type (expand-macro (binding-value b) e r w s rib mod)
1188 r empty-wrap s rib mod #f)))
1189 (else (values type (binding-value b) e e w s mod)))))
1191 (let ((first (car e)))
1193 (lambda () (syntax-type first r w s rib mod #t))
1194 (lambda (ftype fval fform fe fw fs fmod)
1197 (values 'lexical-call fval e e w s mod))
1199 ;; If we got here via an (@@ ...) expansion, we need to
1200 ;; make sure the fmod information is propagated back
1201 ;; correctly -- hence this consing.
1202 (values 'global-call (make-syntax-object fval w fmod)
1205 (syntax-type (expand-macro fval e r w s rib mod)
1206 r empty-wrap s rib mod for-car?))
1208 (call-with-values (lambda () (fval e r w))
1209 (lambda (e r w s mod)
1210 (syntax-type e r w s rib mod for-car?))))
1212 (values 'core-form fval e e w s mod))
1214 (values 'local-syntax-form fval e e w s mod))
1216 (values 'begin-form #f e e w s mod))
1218 (values 'eval-when-form #f e e w s mod))
1223 (values 'define-form #'name e #'val w s mod))
1224 ((_ (name . args) e1 e2 ...)
1226 (valid-bound-ids? (lambda-var-list #'args)))
1227 ;; need lambda here...
1228 (values 'define-form (wrap #'name w mod)
1231 (cons #'lambda (wrap #'(args e1 e2 ...) w mod))
1236 (values 'define-form (wrap #'name w mod)
1239 empty-wrap s mod))))
1244 (values 'define-syntax-form #'name e #'val w s mod))))
1245 ((define-syntax-parameter)
1249 (values 'define-syntax-parameter-form #'name e #'val w s mod))))
1251 (values 'call #f e e w s mod)))))))
1253 (syntax-type (syntax-object-expression e)
1255 (join-wraps w (syntax-object-wrap e))
1256 (or (source-annotation e) s) rib
1257 (or (syntax-object-module e) mod) for-car?))
1258 ((self-evaluating? e) (values 'constant #f e e w s mod))
1259 (else (values 'other #f e e w s mod)))))
1264 (lambda () (syntax-type e r w (source-annotation e) #f mod #f))
1265 (lambda (type value form e w s mod)
1266 (expand-expr type value form e r w s mod)))))
1269 (lambda (type value form e r w s mod)
1272 (build-lexical-reference 'value s e value))
1274 ;; apply transformer
1275 (value e r w s mod))
1277 (call-with-values (lambda () (value e r w))
1278 (lambda (e r w s mod)
1279 (expand e r w mod))))
1283 (build-lexical-reference 'fun (source-annotation id)
1284 (if (syntax-object? id)
1291 (build-global-reference (source-annotation (car e))
1292 (if (syntax-object? value)
1293 (syntax-object-expression value)
1295 (if (syntax-object? value)
1296 (syntax-object-module value)
1299 ((constant) (build-data s (strip (source-wrap e w s mod) empty-wrap)))
1300 ((global) (build-global-reference s value mod))
1301 ((call) (expand-application (expand (car e) r w mod) e r w s mod))
1304 ((_ e1 e2 ...) (expand-sequence #'(e1 e2 ...) r w s mod))
1306 (if (include-deprecated-features)
1308 (issue-deprecation-warning
1309 "Sequences of zero expressions are deprecated. Use *unspecified*.")
1311 (syntax-violation #f "sequence of zero expressions"
1312 (source-wrap e w s mod))))))
1313 ((local-syntax-form)
1314 (expand-local-syntax value e r w s mod expand-sequence))
1317 ((_ (x ...) e1 e2 ...)
1318 (let ((when-list (parse-when-list e #'(x ...))))
1319 (if (memq 'eval when-list)
1320 (expand-sequence #'(e1 e2 ...) r w s mod)
1322 ((define-form define-syntax-form define-syntax-parameter-form)
1323 (syntax-violation #f "definition in expression context, where definitions are not allowed,"
1324 (source-wrap form w s mod)))
1326 (syntax-violation #f "reference to pattern variable outside syntax form"
1327 (source-wrap e w s mod)))
1328 ((displaced-lexical)
1329 (syntax-violation #f "reference to identifier outside its scope"
1330 (source-wrap e w s mod)))
1331 (else (syntax-violation #f "unexpected syntax"
1332 (source-wrap e w s mod))))))
1334 (define expand-application
1335 (lambda (x e r w s mod)
1338 (build-application s x
1339 (map (lambda (e) (expand e r w mod)) #'(e1 ...)))))))
1341 ;; (What follows is my interpretation of what's going on here -- Andy)
1343 ;; A macro takes an expression, a tree, the leaves of which are identifiers
1344 ;; and datums. Identifiers are symbols along with a wrap and a module. For
1345 ;; efficiency, subtrees that share wraps and modules may be grouped as one
1348 ;; Going into the expansion, the expression is given an anti-mark, which
1349 ;; logically propagates to all leaves. Then, in the new expression returned
1350 ;; from the transfomer, if we see an expression with an anti-mark, we know it
1351 ;; pertains to the original expression; conversely, expressions without the
1352 ;; anti-mark are known to be introduced by the transformer.
1354 ;; OK, good until now. We know this algorithm does lexical scoping
1355 ;; appropriately because it's widely known in the literature, and psyntax is
1356 ;; widely used. But what about modules? Here we're on our own. What we do is
1357 ;; to mark the module of expressions produced by a macro as pertaining to the
1358 ;; module that was current when the macro was defined -- that is, free
1359 ;; identifiers introduced by a macro are scoped in the macro's module, not in
1360 ;; the expansion's module. Seems to work well.
1362 ;; The only wrinkle is when we want a macro to expand to code in another
1363 ;; module, as is the case for the r6rs `library' form -- the body expressions
1364 ;; should be scoped relative the new module, the one defined by the macro.
1365 ;; For that, use `(@@ mod-name body)'.
1367 ;; Part of the macro output will be from the site of the macro use and part
1368 ;; from the macro definition. We allow source information from the macro use
1369 ;; to pass through, but we annotate the parts coming from the macro with the
1370 ;; source location information corresponding to the macro use. It would be
1371 ;; really nice if we could also annotate introduced expressions with the
1372 ;; locations corresponding to the macro definition, but that is not yet
1374 (define expand-macro
1375 (lambda (p e r w s rib mod)
1376 (define rebuild-macro-output
1380 (cons (rebuild-macro-output (car x) m)
1381 (rebuild-macro-output (cdr x) m))
1384 (let ((w (syntax-object-wrap x)))
1385 (let ((ms (wrap-marks w)) (ss (wrap-subst w)))
1386 (if (and (pair? ms) (eq? (car ms) the-anti-mark))
1387 ;; output is from original text
1389 (syntax-object-expression x)
1390 (make-wrap (cdr ms) (if rib (cons rib (cdr ss)) (cdr ss)))
1391 (syntax-object-module x))
1392 ;; output introduced by macro
1394 (decorate-source (syntax-object-expression x) s)
1395 (make-wrap (cons m ms)
1397 (cons rib (cons 'shift ss))
1399 (syntax-object-module x))))))
1402 (let* ((n (vector-length x))
1403 (v (decorate-source (make-vector n) s)))
1404 (do ((i 0 (fx+ i 1)))
1407 (rebuild-macro-output (vector-ref x i) m)))))
1409 (syntax-violation #f "encountered raw symbol in macro output"
1410 (source-wrap e w (wrap-subst w) mod) x))
1411 (else (decorate-source x s)))))
1412 (with-fluids ((transformer-environment
1413 (lambda (k) (k e r w s rib mod))))
1414 (rebuild-macro-output (p (source-wrap e (anti-mark w) s mod))
1418 ;; In processing the forms of the body, we create a new, empty wrap.
1419 ;; This wrap is augmented (destructively) each time we discover that
1420 ;; the next form is a definition. This is done:
1422 ;; (1) to allow the first nondefinition form to be a call to
1423 ;; one of the defined ids even if the id previously denoted a
1424 ;; definition keyword or keyword for a macro expanding into a
1426 ;; (2) to prevent subsequent definition forms (but unfortunately
1427 ;; not earlier ones) and the first nondefinition form from
1428 ;; confusing one of the bound identifiers for an auxiliary
1430 ;; (3) so that we do not need to restart the expansion of the
1431 ;; first nondefinition form, which is problematic anyway
1432 ;; since it might be the first element of a begin that we
1433 ;; have just spliced into the body (meaning if we restarted,
1434 ;; we'd really need to restart with the begin or the macro
1435 ;; call that expanded into the begin, and we'd have to give
1436 ;; up allowing (begin <defn>+ <expr>+), which is itself
1437 ;; problematic since we don't know if a begin contains only
1438 ;; definitions until we've expanded it).
1440 ;; Before processing the body, we also create a new environment
1441 ;; containing a placeholder for the bindings we will add later and
1442 ;; associate this environment with each form. In processing a
1443 ;; let-syntax or letrec-syntax, the associated environment may be
1444 ;; augmented with local keyword bindings, so the environment may
1445 ;; be different for different forms in the body. Once we have
1446 ;; gathered up all of the definitions, we evaluate the transformer
1447 ;; expressions and splice into r at the placeholder the new variable
1448 ;; and keyword bindings. This allows let-syntax or letrec-syntax
1449 ;; forms local to a portion or all of the body to shadow the
1450 ;; definition bindings.
1452 ;; Subforms of a begin, let-syntax, or letrec-syntax are spliced
1455 ;; outer-form is fully wrapped w/source
1456 (lambda (body outer-form r w mod)
1457 (let* ((r (cons '("placeholder" . (placeholder)) r))
1458 (ribcage (make-empty-ribcage))
1459 (w (make-wrap (wrap-marks w) (cons ribcage (wrap-subst w)))))
1460 (let parse ((body (map (lambda (x) (cons r (wrap x w mod))) body))
1461 (ids '()) (labels '())
1462 (var-ids '()) (vars '()) (vals '()) (bindings '()))
1464 (syntax-violation #f "no expressions in body" outer-form)
1465 (let ((e (cdar body)) (er (caar body)))
1467 (lambda () (syntax-type e er empty-wrap (source-annotation er) ribcage mod #f))
1468 (lambda (type value form e w s mod)
1471 (let ((id (wrap value w mod)) (label (gen-label)))
1472 (let ((var (gen-var id)))
1473 (extend-ribcage! ribcage id label)
1475 (cons id ids) (cons label labels)
1477 (cons var vars) (cons (cons er (wrap e w mod)) vals)
1478 (cons (make-binding 'lexical var) bindings)))))
1479 ((define-syntax-form define-syntax-parameter-form)
1480 (let ((id (wrap value w mod)) (label (gen-label)))
1481 (extend-ribcage! ribcage id label)
1483 (cons id ids) (cons label labels)
1485 (cons (make-binding 'macro (cons er (wrap e w mod)))
1490 (parse (let f ((forms #'(e1 ...)))
1493 (cons (cons er (wrap (car forms) w mod))
1495 ids labels var-ids vars vals bindings))))
1496 ((local-syntax-form)
1497 (expand-local-syntax value e er w s mod
1498 (lambda (forms er w s mod)
1499 (parse (let f ((forms forms))
1502 (cons (cons er (wrap (car forms) w mod))
1504 ids labels var-ids vars vals bindings))))
1505 (else ; found a non-definition
1507 (build-sequence no-source
1509 (expand (cdr x) (car x) empty-wrap mod))
1510 (cons (cons er (source-wrap e w s mod))
1513 (if (not (valid-bound-ids? ids))
1515 #f "invalid or duplicate identifier in definition"
1517 (let loop ((bs bindings) (er-cache #f) (r-cache #f))
1518 (if (not (null? bs))
1519 (let* ((b (car bs)))
1520 (if (eq? (car b) 'macro)
1521 (let* ((er (cadr b))
1523 (if (eq? er er-cache)
1525 (macros-only-env er))))
1527 (eval-local-transformer
1528 (expand (cddr b) r-cache empty-wrap mod)
1530 (loop (cdr bs) er r-cache))
1531 (loop (cdr bs) er-cache r-cache)))))
1532 (set-cdr! r (extend-env labels bindings (cdr r)))
1533 (build-letrec no-source #t
1534 (reverse (map syntax->datum var-ids))
1537 (expand (cdr x) (car x) empty-wrap mod))
1539 (build-sequence no-source
1541 (expand (cdr x) (car x) empty-wrap mod))
1542 (cons (cons er (source-wrap e w s mod))
1543 (cdr body)))))))))))))))))
1545 (define expand-local-syntax
1546 (lambda (rec? e r w s mod k)
1548 ((_ ((id val) ...) e1 e2 ...)
1549 (let ((ids #'(id ...)))
1550 (if (not (valid-bound-ids? ids))
1551 (syntax-violation #f "duplicate bound keyword" e)
1552 (let ((labels (gen-labels ids)))
1553 (let ((new-w (make-binding-wrap ids labels w)))
1557 (let ((w (if rec? new-w w))
1558 (trans-r (macros-only-env r)))
1560 (make-binding 'macro
1561 (eval-local-transformer
1562 (expand x trans-r w mod)
1569 (_ (syntax-violation #f "bad local syntax definition"
1570 (source-wrap e w s mod))))))
1572 (define eval-local-transformer
1573 (lambda (expanded mod)
1574 (let ((p (local-eval-hook expanded mod)))
1577 (syntax-violation #f "nonprocedure transformer" p)))))
1581 (build-void no-source)))
1585 (and (nonsymbol-id? x)
1586 (free-id=? x #'(... ...)))))
1588 (define lambda-formals
1590 (define (req args rreq)
1591 (syntax-case args ()
1593 (check (reverse rreq) #f))
1595 (req #'b (cons #'a rreq)))
1597 (check (reverse rreq) #'r))
1599 (syntax-violation 'lambda "invalid argument list" orig-args args))))
1600 (define (check req rest)
1602 ((distinct-bound-ids? (if rest (cons rest req) req))
1603 (values req #f rest #f))
1605 (syntax-violation 'lambda "duplicate identifier in argument list"
1607 (req orig-args '())))
1609 (define expand-simple-lambda
1610 (lambda (e r w s mod req rest meta body)
1611 (let* ((ids (if rest (append req (list rest)) req))
1612 (vars (map gen-var ids))
1613 (labels (gen-labels ids)))
1614 (build-simple-lambda
1616 (map syntax->datum req) (and rest (syntax->datum rest)) vars
1618 (expand-body body (source-wrap e w s mod)
1619 (extend-var-env labels vars r)
1620 (make-binding-wrap ids labels w)
1623 (define lambda*-formals
1625 (define (req args rreq)
1626 (syntax-case args ()
1628 (check (reverse rreq) '() #f '()))
1630 (req #'b (cons #'a rreq)))
1631 ((a . b) (eq? (syntax->datum #'a) #:optional)
1632 (opt #'b (reverse rreq) '()))
1633 ((a . b) (eq? (syntax->datum #'a) #:key)
1634 (key #'b (reverse rreq) '() '()))
1635 ((a b) (eq? (syntax->datum #'a) #:rest)
1636 (rest #'b (reverse rreq) '() '()))
1638 (rest #'r (reverse rreq) '() '()))
1640 (syntax-violation 'lambda* "invalid argument list" orig-args args))))
1641 (define (opt args req ropt)
1642 (syntax-case args ()
1644 (check req (reverse ropt) #f '()))
1646 (opt #'b req (cons #'(a #f) ropt)))
1647 (((a init) . b) (id? #'a)
1648 (opt #'b req (cons #'(a init) ropt)))
1649 ((a . b) (eq? (syntax->datum #'a) #:key)
1650 (key #'b req (reverse ropt) '()))
1651 ((a b) (eq? (syntax->datum #'a) #:rest)
1652 (rest #'b req (reverse ropt) '()))
1654 (rest #'r req (reverse ropt) '()))
1656 (syntax-violation 'lambda* "invalid optional argument list"
1658 (define (key args req opt rkey)
1659 (syntax-case args ()
1661 (check req opt #f (cons #f (reverse rkey))))
1663 (with-syntax ((k (symbol->keyword (syntax->datum #'a))))
1664 (key #'b req opt (cons #'(k a #f) rkey))))
1665 (((a init) . b) (id? #'a)
1666 (with-syntax ((k (symbol->keyword (syntax->datum #'a))))
1667 (key #'b req opt (cons #'(k a init) rkey))))
1668 (((a init k) . b) (and (id? #'a)
1669 (keyword? (syntax->datum #'k)))
1670 (key #'b req opt (cons #'(k a init) rkey)))
1671 ((aok) (eq? (syntax->datum #'aok) #:allow-other-keys)
1672 (check req opt #f (cons #t (reverse rkey))))
1673 ((aok a b) (and (eq? (syntax->datum #'aok) #:allow-other-keys)
1674 (eq? (syntax->datum #'a) #:rest))
1675 (rest #'b req opt (cons #t (reverse rkey))))
1676 ((aok . r) (and (eq? (syntax->datum #'aok) #:allow-other-keys)
1678 (rest #'r req opt (cons #t (reverse rkey))))
1679 ((a b) (eq? (syntax->datum #'a) #:rest)
1680 (rest #'b req opt (cons #f (reverse rkey))))
1682 (rest #'r req opt (cons #f (reverse rkey))))
1684 (syntax-violation 'lambda* "invalid keyword argument list"
1686 (define (rest args req opt kw)
1687 (syntax-case args ()
1689 (check req opt #'r kw))
1691 (syntax-violation 'lambda* "invalid rest argument"
1693 (define (check req opt rest kw)
1695 ((distinct-bound-ids?
1696 (append req (map car opt) (if rest (list rest) '())
1697 (if (pair? kw) (map cadr (cdr kw)) '())))
1698 (values req opt rest kw))
1700 (syntax-violation 'lambda* "duplicate identifier in argument list"
1702 (req orig-args '())))
1704 (define expand-lambda-case
1705 (lambda (e r w s mod get-formals clauses)
1706 (define (parse-req req opt rest kw body)
1707 (let ((vars (map gen-var req))
1708 (labels (gen-labels req)))
1709 (let ((r* (extend-var-env labels vars r))
1710 (w* (make-binding-wrap req labels w)))
1711 (parse-opt (map syntax->datum req)
1712 opt rest kw body (reverse vars) r* w* '() '()))))
1713 (define (parse-opt req opt rest kw body vars r* w* out inits)
1716 (syntax-case (car opt) ()
1718 (let* ((v (gen-var #'id))
1719 (l (gen-labels (list v)))
1720 (r** (extend-var-env l (list v) r*))
1721 (w** (make-binding-wrap (list #'id) l w*)))
1722 (parse-opt req (cdr opt) rest kw body (cons v vars)
1723 r** w** (cons (syntax->datum #'id) out)
1724 (cons (expand #'i r* w* mod) inits))))))
1726 (let* ((v (gen-var rest))
1727 (l (gen-labels (list v)))
1728 (r* (extend-var-env l (list v) r*))
1729 (w* (make-binding-wrap (list rest) l w*)))
1730 (parse-kw req (if (pair? out) (reverse out) #f)
1731 (syntax->datum rest)
1732 (if (pair? kw) (cdr kw) kw)
1733 body (cons v vars) r* w*
1734 (if (pair? kw) (car kw) #f)
1737 (parse-kw req (if (pair? out) (reverse out) #f) #f
1738 (if (pair? kw) (cdr kw) kw)
1740 (if (pair? kw) (car kw) #f)
1742 (define (parse-kw req opt rest kw body vars r* w* aok out inits)
1745 (syntax-case (car kw) ()
1747 (let* ((v (gen-var #'id))
1748 (l (gen-labels (list v)))
1749 (r** (extend-var-env l (list v) r*))
1750 (w** (make-binding-wrap (list #'id) l w*)))
1751 (parse-kw req opt rest (cdr kw) body (cons v vars)
1753 (cons (list (syntax->datum #'k)
1754 (syntax->datum #'id)
1757 (cons (expand #'i r* w* mod) inits))))))
1759 (parse-body req opt rest
1760 (if (or aok (pair? out)) (cons aok (reverse out)) #f)
1761 body (reverse vars) r* w* (reverse inits) '()))))
1762 (define (parse-body req opt rest kw body vars r* w* inits meta)
1763 (syntax-case body ()
1764 ((docstring e1 e2 ...) (string? (syntax->datum #'docstring))
1765 (parse-body req opt rest kw #'(e1 e2 ...) vars r* w* inits
1768 . ,(syntax->datum #'docstring))))))
1769 ((#((k . v) ...) e1 e2 ...)
1770 (parse-body req opt rest kw #'(e1 e2 ...) vars r* w* inits
1771 (append meta (syntax->datum #'((k . v) ...)))))
1773 (values meta req opt rest kw inits vars
1774 (expand-body #'(e1 e2 ...) (source-wrap e w s mod)
1777 (syntax-case clauses ()
1778 (() (values '() #f))
1779 (((args e1 e2 ...) (args* e1* e2* ...) ...)
1780 (call-with-values (lambda () (get-formals #'args))
1781 (lambda (req opt rest kw)
1782 (call-with-values (lambda ()
1783 (parse-req req opt rest kw #'(e1 e2 ...)))
1784 (lambda (meta req opt rest kw inits vars body)
1787 (expand-lambda-case e r w s mod get-formals
1788 #'((args* e1* e2* ...) ...)))
1789 (lambda (meta* else*)
1792 (build-lambda-case s req opt rest kw inits vars
1793 body else*))))))))))))
1797 ;; strips syntax-objects down to top-wrap
1799 ;; since only the head of a list is annotated by the reader, not each pair
1800 ;; in the spine, we also check for pairs whose cars are annotated in case
1801 ;; we've been passed the cdr of an annotated list
1810 (strip (syntax-object-expression x) (syntax-object-wrap x)))
1812 (let ((a (f (car x))) (d (f (cdr x))))
1813 (if (and (eq? a (car x)) (eq? d (cdr x)))
1817 (let ((old (vector->list x)))
1818 (let ((new (map f old)))
1819 ;; inlined and-map with two args
1820 (let lp ((l1 old) (l2 new))
1823 (if (eq? (car l1) (car l2))
1824 (lp (cdr l1) (cdr l2))
1825 (list->vector new)))))))
1828 ;; lexical variables
1832 (let ((id (if (syntax-object? id) (syntax-object-expression id) id)))
1833 (build-lexical-var no-source id))))
1835 ;; appears to return a reversed list
1836 (define lambda-var-list
1838 (let lvl ((vars vars) (ls '()) (w empty-wrap))
1840 ((pair? vars) (lvl (cdr vars) (cons (wrap (car vars) w #f) ls) w))
1841 ((id? vars) (cons (wrap vars w #f) ls))
1843 ((syntax-object? vars)
1844 (lvl (syntax-object-expression vars)
1846 (join-wraps w (syntax-object-wrap vars))))
1847 ;; include anything else to be caught by subsequent error
1849 (else (cons vars ls))))))
1851 ;; core transformers
1853 (global-extend 'local-syntax 'letrec-syntax #t)
1854 (global-extend 'local-syntax 'let-syntax #f)
1856 (global-extend 'core 'syntax-parameterize
1857 (lambda (e r w s mod)
1859 ((_ ((var val) ...) e1 e2 ...)
1860 (valid-bound-ids? #'(var ...))
1861 (let ((names (map (lambda (x) (id-var-name x w)) #'(var ...))))
1864 (case (binding-type (lookup n r mod))
1865 ((displaced-lexical)
1866 (syntax-violation 'syntax-parameterize
1867 "identifier out of context"
1869 (source-wrap id w s mod)))))
1874 (source-wrap e w s mod)
1877 (let ((trans-r (macros-only-env r)))
1879 (make-binding 'macro
1880 (eval-local-transformer (expand x trans-r w mod)
1886 (_ (syntax-violation 'syntax-parameterize "bad syntax"
1887 (source-wrap e w s mod))))))
1889 (global-extend 'core 'quote
1890 (lambda (e r w s mod)
1892 ((_ e) (build-data s (strip #'e w)))
1893 (_ (syntax-violation 'quote "bad syntax"
1894 (source-wrap e w s mod))))))
1896 (global-extend 'core 'syntax
1899 (lambda (src e r maps ellipsis? mod)
1901 (let ((label (id-var-name e empty-wrap)))
1902 ;; Mod does not matter, we are looking to see if
1903 ;; the id is lexical syntax.
1904 (let ((b (lookup label r mod)))
1905 (if (eq? (binding-type b) 'syntax)
1908 (let ((var.lev (binding-value b)))
1909 (gen-ref src (car var.lev) (cdr var.lev) maps)))
1910 (lambda (var maps) (values `(ref ,var) maps)))
1912 (syntax-violation 'syntax "misplaced ellipsis" src)
1913 (values `(quote ,e) maps)))))
1917 (gen-syntax src #'e r maps (lambda (x) #f) mod))
1919 ;; this could be about a dozen lines of code, except that we
1920 ;; choose to handle #'(x ... ...) forms
1926 (gen-syntax src #'x r
1927 (cons '() maps) ellipsis? mod))
1929 (if (null? (car maps))
1930 (syntax-violation 'syntax "extra ellipsis"
1932 (values (gen-map x (car maps))
1940 (lambda () (k (cons '() maps)))
1942 (if (null? (car maps))
1943 (syntax-violation 'syntax "extra ellipsis" src)
1944 (values (gen-mappend x (car maps))
1946 (_ (call-with-values
1947 (lambda () (gen-syntax src y r maps ellipsis? mod))
1950 (lambda () (k maps))
1952 (values (gen-append x y) maps)))))))))
1955 (lambda () (gen-syntax src #'x r maps ellipsis? mod))
1958 (lambda () (gen-syntax src #'y r maps ellipsis? mod))
1959 (lambda (y maps) (values (gen-cons x y) maps))))))
1963 (gen-syntax src #'(e1 e2 ...) r maps ellipsis? mod))
1964 (lambda (e maps) (values (gen-vector e) maps))))
1965 (_ (values `(quote ,e) maps))))))
1968 (lambda (src var level maps)
1972 (syntax-violation 'syntax "missing ellipsis" src)
1974 (lambda () (gen-ref src var (fx- level 1) (cdr maps)))
1975 (lambda (outer-var outer-maps)
1976 (let ((b (assq outer-var (car maps))))
1978 (values (cdr b) maps)
1979 (let ((inner-var (gen-var 'tmp)))
1981 (cons (cons (cons outer-var inner-var)
1983 outer-maps)))))))))))
1987 `(apply (primitive append) ,(gen-map e map-env))))
1991 (let ((formals (map cdr map-env))
1992 (actuals (map (lambda (x) `(ref ,(car x))) map-env)))
1995 ;; identity map equivalence:
1996 ;; (map (lambda (x) x) y) == y
1999 (lambda (x) (and (eq? (car x) 'ref) (memq (cadr x) formals)))
2001 ;; eta map equivalence:
2002 ;; (map (lambda (x ...) (f x ...)) y ...) == (map f y ...)
2003 `(map (primitive ,(car e))
2004 ,@(map (let ((r (map cons formals actuals)))
2005 (lambda (x) (cdr (assq (cadr x) r))))
2007 (else `(map (lambda ,formals ,e) ,@actuals))))))
2013 (if (eq? (car x) 'quote)
2014 `(quote (,(cadr x) . ,(cadr y)))
2015 (if (eq? (cadr y) '())
2018 ((list) `(list ,x ,@(cdr y)))
2019 (else `(cons ,x ,y)))))
2023 (if (equal? y '(quote ()))
2030 ((eq? (car x) 'list) `(vector ,@(cdr x)))
2031 ((eq? (car x) 'quote) `(quote #(,@(cadr x))))
2032 (else `(list->vector ,x)))))
2038 ((ref) (build-lexical-reference 'value no-source (cadr x) (cadr x)))
2039 ((primitive) (build-primref no-source (cadr x)))
2040 ((quote) (build-data no-source (cadr x)))
2042 (if (list? (cadr x))
2043 (build-simple-lambda no-source (cadr x) #f (cadr x) '() (regen (caddr x)))
2044 (error "how did we get here" x)))
2045 (else (build-application no-source
2046 (build-primref no-source (car x))
2047 (map regen (cdr x)))))))
2049 (lambda (e r w s mod)
2050 (let ((e (source-wrap e w s mod)))
2054 (lambda () (gen-syntax e #'x r '() ellipsis? mod))
2055 (lambda (e maps) (regen e))))
2056 (_ (syntax-violation 'syntax "bad `syntax' form" e)))))))
2058 (global-extend 'core 'lambda
2059 (lambda (e r w s mod)
2062 (call-with-values (lambda () (lambda-formals #'args))
2063 (lambda (req opt rest kw)
2064 (let lp ((body #'(e1 e2 ...)) (meta '()))
2065 (syntax-case body ()
2066 ((docstring e1 e2 ...) (string? (syntax->datum #'docstring))
2070 . ,(syntax->datum #'docstring))))))
2071 ((#((k . v) ...) e1 e2 ...)
2073 (append meta (syntax->datum #'((k . v) ...)))))
2074 (_ (expand-simple-lambda e r w s mod req rest meta body)))))))
2075 (_ (syntax-violation 'lambda "bad lambda" e)))))
2077 (global-extend 'core 'lambda*
2078 (lambda (e r w s mod)
2083 (expand-lambda-case e r w s mod
2084 lambda*-formals #'((args e1 e2 ...))))
2085 (lambda (meta lcase)
2086 (build-case-lambda s meta lcase))))
2087 (_ (syntax-violation 'lambda "bad lambda*" e)))))
2089 (global-extend 'core 'case-lambda
2090 (lambda (e r w s mod)
2092 ((_ (args e1 e2 ...) (args* e1* e2* ...) ...)
2095 (expand-lambda-case e r w s mod
2097 #'((args e1 e2 ...) (args* e1* e2* ...) ...)))
2098 (lambda (meta lcase)
2099 (build-case-lambda s meta lcase))))
2100 (_ (syntax-violation 'case-lambda "bad case-lambda" e)))))
2102 (global-extend 'core 'case-lambda*
2103 (lambda (e r w s mod)
2105 ((_ (args e1 e2 ...) (args* e1* e2* ...) ...)
2108 (expand-lambda-case e r w s mod
2110 #'((args e1 e2 ...) (args* e1* e2* ...) ...)))
2111 (lambda (meta lcase)
2112 (build-case-lambda s meta lcase))))
2113 (_ (syntax-violation 'case-lambda "bad case-lambda*" e)))))
2115 (global-extend 'core 'let
2117 (define (expand-let e r w s mod constructor ids vals exps)
2118 (if (not (valid-bound-ids? ids))
2119 (syntax-violation 'let "duplicate bound variable" e)
2120 (let ((labels (gen-labels ids))
2121 (new-vars (map gen-var ids)))
2122 (let ((nw (make-binding-wrap ids labels w))
2123 (nr (extend-var-env labels new-vars r)))
2125 (map syntax->datum ids)
2127 (map (lambda (x) (expand x r w mod)) vals)
2128 (expand-body exps (source-wrap e nw s mod)
2130 (lambda (e r w s mod)
2132 ((_ ((id val) ...) e1 e2 ...)
2133 (and-map id? #'(id ...))
2134 (expand-let e r w s mod
2139 ((_ f ((id val) ...) e1 e2 ...)
2140 (and (id? #'f) (and-map id? #'(id ...)))
2141 (expand-let e r w s mod
2146 (_ (syntax-violation 'let "bad let" (source-wrap e w s mod)))))))
2149 (global-extend 'core 'letrec
2150 (lambda (e r w s mod)
2152 ((_ ((id val) ...) e1 e2 ...)
2153 (and-map id? #'(id ...))
2154 (let ((ids #'(id ...)))
2155 (if (not (valid-bound-ids? ids))
2156 (syntax-violation 'letrec "duplicate bound variable" e)
2157 (let ((labels (gen-labels ids))
2158 (new-vars (map gen-var ids)))
2159 (let ((w (make-binding-wrap ids labels w))
2160 (r (extend-var-env labels new-vars r)))
2162 (map syntax->datum ids)
2164 (map (lambda (x) (expand x r w mod)) #'(val ...))
2165 (expand-body #'(e1 e2 ...)
2166 (source-wrap e w s mod) r w mod)))))))
2167 (_ (syntax-violation 'letrec "bad letrec" (source-wrap e w s mod))))))
2170 (global-extend 'core 'letrec*
2171 (lambda (e r w s mod)
2173 ((_ ((id val) ...) e1 e2 ...)
2174 (and-map id? #'(id ...))
2175 (let ((ids #'(id ...)))
2176 (if (not (valid-bound-ids? ids))
2177 (syntax-violation 'letrec* "duplicate bound variable" e)
2178 (let ((labels (gen-labels ids))
2179 (new-vars (map gen-var ids)))
2180 (let ((w (make-binding-wrap ids labels w))
2181 (r (extend-var-env labels new-vars r)))
2183 (map syntax->datum ids)
2185 (map (lambda (x) (expand x r w mod)) #'(val ...))
2186 (expand-body #'(e1 e2 ...)
2187 (source-wrap e w s mod) r w mod)))))))
2188 (_ (syntax-violation 'letrec* "bad letrec*" (source-wrap e w s mod))))))
2191 (global-extend 'core 'set!
2192 (lambda (e r w s mod)
2196 (let ((n (id-var-name #'id w))
2197 ;; Lookup id in its module
2198 (id-mod (if (syntax-object? #'id)
2199 (syntax-object-module #'id)
2201 (let ((b (lookup n r id-mod)))
2202 (case (binding-type b)
2204 (build-lexical-assignment s
2205 (syntax->datum #'id)
2207 (expand #'val r w mod)))
2209 (build-global-assignment s n (expand #'val r w mod) id-mod))
2211 (let ((p (binding-value b)))
2212 (if (procedure-property p 'variable-transformer)
2213 ;; As syntax-type does, call expand-macro with
2214 ;; the mod of the expression. Hmm.
2215 (expand (expand-macro p e r w s #f mod) r empty-wrap mod)
2216 (syntax-violation 'set! "not a variable transformer"
2218 (wrap #'id w id-mod)))))
2219 ((displaced-lexical)
2220 (syntax-violation 'set! "identifier out of context"
2222 (else (syntax-violation 'set! "bad set!"
2223 (source-wrap e w s mod)))))))
2224 ((_ (head tail ...) val)
2226 (lambda () (syntax-type #'head r empty-wrap no-source #f mod #t))
2227 (lambda (type value formform ee ww ss modmod)
2230 (let ((val (expand #'val r w mod)))
2231 (call-with-values (lambda () (value #'(head tail ...) r w))
2232 (lambda (e r w s* mod)
2235 (build-global-assignment s (syntax->datum #'e)
2238 (build-application s
2239 (expand #'(setter head) r w mod)
2240 (map (lambda (e) (expand e r w mod))
2241 #'(tail ... val))))))))
2242 (_ (syntax-violation 'set! "bad set!" (source-wrap e w s mod))))))
2244 (global-extend 'module-ref '@
2248 (and (and-map id? #'(mod ...)) (id? #'id))
2249 (values (syntax->datum #'id) r w #f
2251 #'(public mod ...)))))))
2253 (global-extend 'module-ref '@@
2258 (cons (remodulate (car x) mod)
2259 (remodulate (cdr x) mod)))
2262 (remodulate (syntax-object-expression x) mod)
2263 (syntax-object-wrap x)
2264 ;; hither the remodulation
2267 (let* ((n (vector-length x)) (v (make-vector n)))
2268 (do ((i 0 (fx+ i 1)))
2270 (vector-set! v i (remodulate (vector-ref x i) mod)))))
2274 (and-map id? #'(mod ...))
2275 (let ((mod (syntax->datum #'(private mod ...))))
2276 (values (remodulate #'exp mod)
2277 r w (source-annotation #'exp)
2280 (global-extend 'core 'if
2281 (lambda (e r w s mod)
2286 (expand #'test r w mod)
2287 (expand #'then r w mod)
2288 (build-void no-source)))
2292 (expand #'test r w mod)
2293 (expand #'then r w mod)
2294 (expand #'else r w mod))))))
2296 (global-extend 'core 'with-fluids
2297 (lambda (e r w s mod)
2299 ((_ ((fluid val) ...) b b* ...)
2302 (map (lambda (x) (expand x r w mod)) #'(fluid ...))
2303 (map (lambda (x) (expand x r w mod)) #'(val ...))
2304 (expand-body #'(b b* ...)
2305 (source-wrap e w s mod) r w mod))))))
2307 (global-extend 'begin 'begin '())
2309 (global-extend 'define 'define '())
2311 (global-extend 'define-syntax 'define-syntax '())
2312 (global-extend 'define-syntax-parameter 'define-syntax-parameter '())
2314 (global-extend 'eval-when 'eval-when '())
2316 (global-extend 'core 'syntax-case
2318 (define convert-pattern
2319 ;; accepts pattern & keys
2320 ;; returns $sc-dispatch pattern & ids
2321 (lambda (pattern keys)
2324 (if (not (pair? p*))
2327 (lambda () (cvt* (cdr p*) n ids))
2330 (lambda () (cvt (car p*) n ids))
2332 (values (cons x y) ids))))))))
2334 (define (v-reverse x)
2335 (let loop ((r '()) (x x))
2338 (loop (cons (car x) r) (cdr x)))))
2344 ((bound-id-member? p keys)
2345 (values (vector 'free-id p) ids))
2349 (values 'any (cons (cons p n) ids))))
2352 (ellipsis? (syntax dots))
2354 (lambda () (cvt (syntax x) (fx+ n 1) ids))
2356 (values (if (eq? p 'any) 'each-any (vector 'each p))
2359 (ellipsis? (syntax dots))
2361 (lambda () (cvt* (syntax ys) n ids))
2364 (lambda () (cvt (syntax x) (+ n 1) ids))
2367 (lambda () (v-reverse ys))
2369 (values `#(each+ ,x ,ys ,e)
2373 (lambda () (cvt (syntax y) n ids))
2376 (lambda () (cvt (syntax x) n ids))
2378 (values (cons x y) ids))))))
2379 (() (values '() ids))
2382 (lambda () (cvt (syntax (x ...)) n ids))
2383 (lambda (p ids) (values (vector 'vector p) ids))))
2384 (x (values (vector 'atom (strip p empty-wrap)) ids))))))
2385 (cvt pattern 0 '())))
2387 (define build-dispatch-call
2388 (lambda (pvars exp y r mod)
2389 (let ((ids (map car pvars)) (levels (map cdr pvars)))
2390 (let ((labels (gen-labels ids)) (new-vars (map gen-var ids)))
2391 (build-application no-source
2392 (build-primref no-source 'apply)
2393 (list (build-simple-lambda no-source (map syntax->datum ids) #f new-vars '()
2397 (map (lambda (var level)
2398 (make-binding 'syntax `(,var . ,level)))
2402 (make-binding-wrap ids labels empty-wrap)
2407 (lambda (x keys clauses r pat fender exp mod)
2409 (lambda () (convert-pattern pat keys))
2412 ((not (distinct-bound-ids? (map car pvars)))
2413 (syntax-violation 'syntax-case "duplicate pattern variable" pat))
2414 ((not (and-map (lambda (x) (not (ellipsis? (car x)))) pvars))
2415 (syntax-violation 'syntax-case "misplaced ellipsis" pat))
2417 (let ((y (gen-var 'tmp)))
2418 ;; fat finger binding and references to temp variable y
2419 (build-application no-source
2420 (build-simple-lambda no-source (list 'tmp) #f (list y) '()
2421 (let ((y (build-lexical-reference 'value no-source
2423 (build-conditional no-source
2424 (syntax-case fender ()
2426 (_ (build-conditional no-source
2428 (build-dispatch-call pvars fender y r mod)
2429 (build-data no-source #f))))
2430 (build-dispatch-call pvars exp y r mod)
2431 (gen-syntax-case x keys clauses r mod))))
2432 (list (if (eq? p 'any)
2433 (build-application no-source
2434 (build-primref no-source 'list)
2436 (build-application no-source
2437 (build-primref no-source '$sc-dispatch)
2438 (list x (build-data no-source p)))))))))))))
2440 (define gen-syntax-case
2441 (lambda (x keys clauses r mod)
2443 (build-application no-source
2444 (build-primref no-source 'syntax-violation)
2445 (list (build-data no-source #f)
2446 (build-data no-source
2447 "source expression failed to match any pattern")
2449 (syntax-case (car clauses) ()
2451 (if (and (id? #'pat)
2452 (and-map (lambda (x) (not (free-id=? #'pat x)))
2453 (cons #'(... ...) keys)))
2454 (if (free-id=? #'pat #'_)
2455 (expand #'exp r empty-wrap mod)
2456 (let ((labels (list (gen-label)))
2457 (var (gen-var #'pat)))
2458 (build-application no-source
2459 (build-simple-lambda
2460 no-source (list (syntax->datum #'pat)) #f (list var)
2464 (list (make-binding 'syntax `(,var . 0)))
2466 (make-binding-wrap #'(pat)
2470 (gen-clause x keys (cdr clauses) r
2471 #'pat #t #'exp mod)))
2473 (gen-clause x keys (cdr clauses) r
2474 #'pat #'fender #'exp mod))
2475 (_ (syntax-violation 'syntax-case "invalid clause"
2478 (lambda (e r w s mod)
2479 (let ((e (source-wrap e w s mod)))
2481 ((_ val (key ...) m ...)
2482 (if (and-map (lambda (x) (and (id? x) (not (ellipsis? x))))
2484 (let ((x (gen-var 'tmp)))
2485 ;; fat finger binding and references to temp variable x
2486 (build-application s
2487 (build-simple-lambda no-source (list 'tmp) #f (list x) '()
2488 (gen-syntax-case (build-lexical-reference 'value no-source
2490 #'(key ...) #'(m ...)
2493 (list (expand #'val r empty-wrap mod))))
2494 (syntax-violation 'syntax-case "invalid literals list" e))))))))
2496 ;; The portable macroexpand seeds expand-top's mode m with 'e (for
2497 ;; evaluating) and esew (which stands for "eval syntax expanders
2498 ;; when") with '(eval). In Chez Scheme, m is set to 'c instead of e
2499 ;; if we are compiling a file, and esew is set to
2500 ;; (eval-syntactic-expanders-when), which defaults to the list
2501 ;; '(compile load eval). This means that, by default, top-level
2502 ;; syntactic definitions are evaluated immediately after they are
2503 ;; expanded, and the expanded definitions are also residualized into
2504 ;; the object file if we are compiling a file.
2506 (lambda* (x #:optional (m 'e) (esew '(eval)))
2507 (expand-top-sequence (list x) null-env top-wrap #f m esew
2508 (cons 'hygiene (module-name (current-module))))))
2516 (make-syntax-object datum (syntax-object-wrap id)
2517 (syntax-object-module id))))
2520 ;; accepts any object, since syntax objects may consist partially
2521 ;; or entirely of unwrapped, nonsymbolic data
2523 (strip x empty-wrap)))
2526 (lambda (x) (source-annotation x)))
2528 (set! generate-temporaries
2530 (arg-check list? ls 'generate-temporaries)
2531 (let ((mod (cons 'hygiene (module-name (current-module)))))
2532 (map (lambda (x) (wrap (gensym "t-") top-wrap mod)) ls))))
2534 (set! free-identifier=?
2536 (arg-check nonsymbol-id? x 'free-identifier=?)
2537 (arg-check nonsymbol-id? y 'free-identifier=?)
2540 (set! bound-identifier=?
2542 (arg-check nonsymbol-id? x 'bound-identifier=?)
2543 (arg-check nonsymbol-id? y 'bound-identifier=?)
2546 (set! syntax-violation
2547 (lambda* (who message form #:optional subform)
2548 (arg-check (lambda (x) (or (not x) (string? x) (symbol? x)))
2549 who 'syntax-violation)
2550 (arg-check string? message 'syntax-violation)
2551 (throw 'syntax-error who message
2552 (or (source-annotation subform)
2553 (source-annotation form))
2554 (strip form empty-wrap)
2555 (and subform (strip subform empty-wrap)))))
2558 (define (syntax-module id)
2559 (arg-check nonsymbol-id? id 'syntax-module)
2560 (cdr (syntax-object-module id)))
2562 (define (syntax-local-binding id)
2563 (arg-check nonsymbol-id? id 'syntax-local-binding)
2564 (with-transformer-environment
2565 (lambda (e r w s rib mod)
2566 (define (strip-anti-mark w)
2567 (let ((ms (wrap-marks w)) (s (wrap-subst w)))
2568 (if (and (pair? ms) (eq? (car ms) the-anti-mark))
2569 ;; output is from original text
2570 (make-wrap (cdr ms) (if rib (cons rib (cdr s)) (cdr s)))
2571 ;; output introduced by macro
2572 (make-wrap ms (if rib (cons rib s) s)))))
2573 (call-with-values (lambda ()
2575 (syntax-object-expression id)
2576 (strip-anti-mark (syntax-object-wrap id))
2578 (syntax-object-module id)))
2579 (lambda (type value mod)
2581 ((lexical) (values 'lexical value))
2582 ((macro) (values 'macro value))
2583 ((syntax) (values 'pattern-variable value))
2584 ((displaced-lexical) (values 'displaced-lexical #f))
2585 ((global) (values 'global (cons value (cdr mod))))
2586 (else (values 'other #f))))))))
2588 (define (syntax-locally-bound-identifiers id)
2589 (arg-check nonsymbol-id? id 'syntax-locally-bound-identifiers)
2590 (locally-bound-identifiers (syntax-object-wrap id)
2591 (syntax-object-module id)))
2593 ;; Using define! instead of set! to avoid warnings at
2594 ;; compile-time, after the variables are stolen away into (system
2595 ;; syntax). See the end of boot-9.scm.
2597 (define! 'syntax-module syntax-module)
2598 (define! 'syntax-local-binding syntax-local-binding)
2599 (define! 'syntax-locally-bound-identifiers syntax-locally-bound-identifiers))
2601 ;; $sc-dispatch expects an expression and a pattern. If the expression
2602 ;; matches the pattern a list of the matching expressions for each
2603 ;; "any" is returned. Otherwise, #f is returned. (This use of #f will
2604 ;; not work on r4rs implementations that violate the ieee requirement
2605 ;; that #f and () be distinct.)
2607 ;; The expression is matched with the pattern as follows:
2609 ;; pattern: matches:
2612 ;; (<pattern>1 . <pattern>2) (<pattern>1 . <pattern>2)
2614 ;; #(free-id <key>) <key> with free-identifier=?
2615 ;; #(each <pattern>) (<pattern>*)
2616 ;; #(each+ p1 (p2_1 ... p2_n) p3) (p1* (p2_n ... p2_1) . p3)
2617 ;; #(vector <pattern>) (list->vector <pattern>)
2618 ;; #(atom <object>) <object> with "equal?"
2620 ;; Vector cops out to pair under assumption that vectors are rare. If
2621 ;; not, should convert to:
2622 ;; #(vector <pattern>*) #(<pattern>*)
2630 (let ((first (match (car e) p w '() mod)))
2632 (let ((rest (match-each (cdr e) p w mod)))
2633 (and rest (cons first rest))))))
2636 (match-each (syntax-object-expression e)
2638 (join-wraps w (syntax-object-wrap e))
2639 (syntax-object-module e)))
2643 (lambda (e x-pat y-pat z-pat w r mod)
2644 (let f ((e e) (w w))
2647 (call-with-values (lambda () (f (cdr e) w))
2648 (lambda (xr* y-pat r)
2651 (let ((xr (match (car e) x-pat w '() mod)))
2653 (values (cons xr xr*) y-pat r)
2658 (match (car e) (car y-pat) w r mod)))
2659 (values #f #f #f)))))
2661 (f (syntax-object-expression e) (join-wraps w e)))
2663 (values '() y-pat (match e z-pat w r mod)))))))
2665 (define match-each-any
2669 (let ((l (match-each-any (cdr e) w mod)))
2670 (and l (cons (wrap (car e) w mod) l))))
2673 (match-each-any (syntax-object-expression e)
2674 (join-wraps w (syntax-object-wrap e))
2683 ((eq? p 'any) (cons '() r))
2684 ((pair? p) (match-empty (car p) (match-empty (cdr p) r)))
2685 ((eq? p 'each-any) (cons '() r))
2687 (case (vector-ref p 0)
2688 ((each) (match-empty (vector-ref p 1) r))
2689 ((each+) (match-empty (vector-ref p 1)
2691 (reverse (vector-ref p 2))
2692 (match-empty (vector-ref p 3) r))))
2694 ((vector) (match-empty (vector-ref p 1) r)))))))
2698 (if (null? (car r*))
2700 (cons (map car r*) (combine (map cdr r*) r)))))
2703 (lambda (e p w r mod)
2705 ((null? p) (and (null? e) r))
2707 (and (pair? e) (match (car e) (car p) w
2708 (match (cdr e) (cdr p) w r mod)
2711 (let ((l (match-each-any e w mod))) (and l (cons l r))))
2713 (case (vector-ref p 0)
2716 (match-empty (vector-ref p 1) r)
2717 (let ((l (match-each e (vector-ref p 1) w mod)))
2719 (let collect ((l l))
2722 (cons (map car l) (collect (map cdr l)))))))))
2726 (match-each+ e (vector-ref p 1) (vector-ref p 2) (vector-ref p 3) w r mod))
2727 (lambda (xr* y-pat r)
2731 (match-empty (vector-ref p 1) r)
2732 (combine xr* r))))))
2733 ((free-id) (and (id? e) (free-id=? (wrap e w mod) (vector-ref p 1)) r))
2734 ((atom) (and (equal? (vector-ref p 1) (strip e w)) r))
2737 (match (vector->list e) (vector-ref p 1) w r mod))))))))
2740 (lambda (e p w r mod)
2744 ((eq? p 'any) (cons (wrap e w mod) r))
2747 (syntax-object-expression e)
2749 (join-wraps w (syntax-object-wrap e))
2751 (syntax-object-module e)))
2752 (else (match* e p w r mod)))))
2757 ((eq? p 'any) (list e))
2760 (match* (syntax-object-expression e)
2761 p (syntax-object-wrap e) '() (syntax-object-module e)))
2762 (else (match* e p empty-wrap '() #f))))))))
2765 (define-syntax with-syntax
2769 #'(let () e1 e2 ...))
2770 ((_ ((out in)) e1 e2 ...)
2771 #'(syntax-case in ()
2772 (out (let () e1 e2 ...))))
2773 ((_ ((out in) ...) e1 e2 ...)
2774 #'(syntax-case (list in ...) ()
2775 ((out ...) (let () e1 e2 ...)))))))
2777 (define-syntax syntax-rules
2780 ((_ (k ...) ((keyword . pattern) template) ...)
2782 ;; embed patterns as procedure metadata
2783 #((macro-type . syntax-rules)
2784 (patterns pattern ...))
2785 (syntax-case x (k ...)
2786 ((dummy . pattern) #'template)
2788 ((_ (k ...) docstring ((keyword . pattern) template) ...)
2789 (string? (syntax->datum #'docstring))
2791 ;; the same, but allow a docstring
2793 #((macro-type . syntax-rules)
2794 (patterns pattern ...))
2795 (syntax-case x (k ...)
2796 ((dummy . pattern) #'template)
2799 (define-syntax define-syntax-rule
2802 ((_ (name . pattern) template)
2803 #'(define-syntax name
2805 ((_ . pattern) template))))
2806 ((_ (name . pattern) docstring template)
2807 (string? (syntax->datum #'docstring))
2808 #'(define-syntax name
2811 ((_ . pattern) template)))))))
2816 ((let* ((x v) ...) e1 e2 ...)
2817 (and-map identifier? #'(x ...))
2818 (let f ((bindings #'((x v) ...)))
2819 (if (null? bindings)
2820 #'(let () e1 e2 ...)
2821 (with-syntax ((body (f (cdr bindings)))
2822 (binding (car bindings)))
2823 #'(let (binding) body))))))))
2827 (syntax-case orig-x ()
2828 ((_ ((var init . step) ...) (e0 e1 ...) c ...)
2829 (with-syntax (((step ...)
2834 (_ (syntax-violation
2835 'do "bad step expression"
2839 (syntax-case #'(e1 ...) ()
2840 (() #'(let doloop ((var init) ...)
2842 (begin c ... (doloop step ...)))))
2844 #'(let doloop ((var init) ...)
2847 (begin c ... (doloop step ...)))))))))))
2849 (define-syntax quasiquote
2851 (define (quasi p lev)
2852 (syntax-case p (unquote quasiquote)
2856 (quasicons #'("quote" unquote) (quasi #'(p) (- lev 1)))))
2857 ((quasiquote p) (quasicons #'("quote" quasiquote) (quasi #'(p) (+ lev 1))))
2859 (syntax-case #'p (unquote unquote-splicing)
2862 (quasilist* #'(("value" p) ...) (quasi #'q lev))
2864 (quasicons #'("quote" unquote) (quasi #'(p ...) (- lev 1)))
2866 ((unquote-splicing p ...)
2868 (quasiappend #'(("value" p) ...) (quasi #'q lev))
2870 (quasicons #'("quote" unquote-splicing) (quasi #'(p ...) (- lev 1)))
2872 (_ (quasicons (quasi #'p lev) (quasi #'q lev)))))
2873 (#(x ...) (quasivector (vquasi #'(x ...) lev)))
2875 (define (vquasi p lev)
2878 (syntax-case #'p (unquote unquote-splicing)
2881 (quasilist* #'(("value" p) ...) (vquasi #'q lev))
2883 (quasicons #'("quote" unquote) (quasi #'(p ...) (- lev 1)))
2885 ((unquote-splicing p ...)
2887 (quasiappend #'(("value" p) ...) (vquasi #'q lev))
2890 #'("quote" unquote-splicing)
2891 (quasi #'(p ...) (- lev 1)))
2893 (_ (quasicons (quasi #'p lev) (vquasi #'q lev)))))
2894 (() #'("quote" ()))))
2895 (define (quasicons x y)
2896 (with-syntax ((x x) (y y))
2900 (("quote" dx) #'("quote" (dx . dy)))
2901 (_ (if (null? #'dy) #'("list" x) #'("list*" x y)))))
2902 (("list" . stuff) #'("list" x . stuff))
2903 (("list*" . stuff) #'("list*" x . stuff))
2904 (_ #'("list*" x y)))))
2905 (define (quasiappend x y)
2909 ((null? x) #'("quote" ()))
2910 ((null? (cdr x)) (car x))
2911 (else (with-syntax (((p ...) x)) #'("append" p ...)))))
2915 (else (with-syntax (((p ...) x) (y y)) #'("append" p ... y)))))))
2916 (define (quasilist* x y)
2920 (quasicons (car x) (f (cdr x))))))
2921 (define (quasivector x)
2923 (("quote" (x ...)) #'("quote" #(x ...)))
2925 (let f ((y x) (k (lambda (ls) #`("vector" #,@ls))))
2927 (("quote" (y ...)) (k #'(("quote" y) ...)))
2928 (("list" y ...) (k #'(y ...)))
2929 (("list*" y ... z) (f #'z (lambda (ls) (k (append #'(y ...) ls)))))
2930 (else #`("list->vector" #,x)))))))
2934 (("list" x ...) #`(list #,@(map emit #'(x ...))))
2935 ;; could emit list* for 3+ arguments if implementation supports
2938 (let f ((x* #'(x ...)))
2941 #`(cons #,(emit (car x*)) #,(f (cdr x*))))))
2942 (("append" x ...) #`(append #,@(map emit #'(x ...))))
2943 (("vector" x ...) #`(vector #,@(map emit #'(x ...))))
2944 (("list->vector" x) #`(list->vector #,(emit #'x)))
2948 ;; convert to intermediate language, combining introduced (but
2949 ;; not unquoted source) quote expressions where possible and
2950 ;; choosing optimal construction code otherwise, then emit
2951 ;; Scheme code corresponding to the intermediate language forms.
2952 ((_ e) (emit (quasi #'e 0)))))))
2954 (define-syntax include
2958 (let ((p (open-input-file fn)))
2959 (let f ((x (read p))
2963 (close-input-port p)
2966 (cons (datum->syntax k x) result)))))))
2969 (let ((fn (syntax->datum #'filename)))
2970 (with-syntax (((exp ...) (read-file fn #'filename)))
2971 #'(begin exp ...)))))))
2973 (define-syntax include-from-path
2977 (let ((fn (syntax->datum #'filename)))
2978 (with-syntax ((fn (datum->syntax
2980 (or (%search-load-path fn)
2981 (syntax-violation 'include-from-path
2982 "file not found in path"
2984 #'(include fn)))))))
2986 (define-syntax unquote
2988 (syntax-violation 'unquote
2989 "expression not valid outside of quasiquote"
2992 (define-syntax unquote-splicing
2994 (syntax-violation 'unquote-splicing
2995 "expression not valid outside of quasiquote"
3003 ((body (let f ((clause #'m1) (clauses #'(m2 ...)))
3005 (syntax-case clause (else)
3006 ((else e1 e2 ...) #'(begin e1 e2 ...))
3007 (((k ...) e1 e2 ...)
3008 #'(if (memv t '(k ...)) (begin e1 e2 ...)))
3009 (_ (syntax-violation 'case "bad clause" x clause)))
3010 (with-syntax ((rest (f (car clauses) (cdr clauses))))
3011 (syntax-case clause (else)
3012 (((k ...) e1 e2 ...)
3013 #'(if (memv t '(k ...))
3016 (_ (syntax-violation 'case "bad clause" x
3018 #'(let ((t e)) body))))))
3020 (define (make-variable-transformer proc)
3021 (if (procedure? proc)
3022 (let ((trans (lambda (x)
3023 #((macro-type . variable-transformer))
3025 (set-procedure-property! trans 'variable-transformer #t)
3027 (error "variable transformer not a procedure" proc)))
3029 (define-syntax identifier-syntax
3031 (syntax-case x (set!)
3034 #((macro-type . identifier-syntax))
3040 #'(e x (... ...))))))
3041 ((_ (id exp1) ((set! var val) exp2))
3042 (and (identifier? #'id) (identifier? #'var))
3043 #'(make-variable-transformer
3045 #((macro-type . variable-transformer))
3046 (syntax-case x (set!)
3047 ((set! var val) #'exp2)
3048 ((id x (... ...)) #'(exp1 x (... ...)))
3049 (id (identifier? #'id) #'exp1))))))))
3051 (define-syntax define*
3054 ((_ (id . args) b0 b1 ...)
3055 #'(define id (lambda* args b0 b1 ...)))
3056 ((_ id val) (identifier? #'id)
3057 #'(define id val)))))