3 ;;;; Copyright (C) 2001, 2003, 2006, 2009, 2010, 2011,
4 ;;;; 2012, 2013 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> ::= #(ribcage #(<sym> ...) #(<mark> ...) #(<label> ...))
623 ;; | #(ribcage (<sym> ...) (<mark> ...) (<label> ...))
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 ;; labels must be comparable with "eq?", have read-write invariance,
630 ;; and distinct from symbols.
632 (string-append "l-" (session-id) (symbol->string (gensym "-"))))
638 (cons (gen-label) (gen-labels (cdr ls))))))
640 (define-structure (ribcage symnames marks labels))
642 (define-syntax empty-wrap (identifier-syntax '(())))
644 (define-syntax top-wrap (identifier-syntax '((top))))
646 (define-syntax-rule (top-marked? w)
647 (memq 'top (wrap-marks w)))
649 ;; Marks must be comparable with "eq?" and distinct from pairs and
650 ;; the symbol top. We do not use integers so that marks will remain
651 ;; unique even across file compiles.
653 (define-syntax the-anti-mark (identifier-syntax #f))
657 (make-wrap (cons the-anti-mark (wrap-marks w))
658 (cons 'shift (wrap-subst w)))))
660 (define-syntax-rule (new-mark)
661 (gensym (string-append "m-" (session-id) "-")))
663 ;; make-empty-ribcage and extend-ribcage maintain list-based ribcages for
664 ;; internal definitions, in which the ribcages are built incrementally
665 (define-syntax-rule (make-empty-ribcage)
666 (make-ribcage '() '() '()))
668 (define extend-ribcage!
669 ;; must receive ids with complete wraps
670 (lambda (ribcage id label)
671 (set-ribcage-symnames! ribcage
672 (cons (syntax-object-expression id)
673 (ribcage-symnames ribcage)))
674 (set-ribcage-marks! ribcage
675 (cons (wrap-marks (syntax-object-wrap id))
676 (ribcage-marks ribcage)))
677 (set-ribcage-labels! ribcage
678 (cons label (ribcage-labels ribcage)))))
680 ;; make-binding-wrap creates vector-based ribcages
681 (define make-binding-wrap
682 (lambda (ids labels w)
688 (let ((labelvec (list->vector labels)))
689 (let ((n (vector-length labelvec)))
690 (let ((symnamevec (make-vector n)) (marksvec (make-vector n)))
691 (let f ((ids ids) (i 0))
692 (if (not (null? ids))
694 (lambda () (id-sym-name&marks (car ids) w))
695 (lambda (symname marks)
696 (vector-set! symnamevec i symname)
697 (vector-set! marksvec i marks)
698 (f (cdr ids) (fx+ i 1))))))
699 (make-ribcage symnamevec marksvec labelvec))))
710 (let ((m1 (wrap-marks w1)) (s1 (wrap-subst w1)))
716 (smart-append s1 (wrap-subst w2))))
718 (smart-append m1 (wrap-marks w2))
719 (smart-append s1 (wrap-subst w2)))))))
723 (smart-append m1 m2)))
730 (eq? (car x) (car y))
731 (same-marks? (cdr x) (cdr y))))))
735 (define-syntax-rule (first e)
736 ;; Rely on Guile's multiple-values truncation.
739 (lambda (sym subst marks)
742 (let ((fst (car subst)))
744 (search sym (cdr subst) (cdr marks))
745 (let ((symnames (ribcage-symnames fst)))
746 (if (vector? symnames)
747 (search-vector-rib sym subst marks symnames fst)
748 (search-list-rib sym subst marks symnames fst))))))))
749 (define search-list-rib
750 (lambda (sym subst marks symnames ribcage)
751 (let f ((symnames symnames) (i 0))
753 ((null? symnames) (search sym (cdr subst) marks))
754 ((and (eq? (car symnames) sym)
755 (same-marks? marks (list-ref (ribcage-marks ribcage) i)))
756 (values (list-ref (ribcage-labels ribcage) i) marks))
757 (else (f (cdr symnames) (fx+ i 1)))))))
758 (define search-vector-rib
759 (lambda (sym subst marks symnames ribcage)
760 (let ((n (vector-length symnames)))
763 ((fx= i n) (search sym (cdr subst) marks))
764 ((and (eq? (vector-ref symnames i) sym)
765 (same-marks? marks (vector-ref (ribcage-marks ribcage) i)))
766 (values (vector-ref (ribcage-labels ribcage) i) marks))
767 (else (f (fx+ i 1))))))))
770 (or (first (search id (wrap-subst w) (wrap-marks w))) id))
772 (let ((id (syntax-object-expression id))
773 (w1 (syntax-object-wrap id)))
774 (let ((marks (join-marks (wrap-marks w) (wrap-marks w1))))
775 (call-with-values (lambda () (search id (wrap-subst w) marks))
776 (lambda (new-id marks)
778 (first (search id (wrap-subst w1) marks))
780 (else (syntax-violation 'id-var-name "invalid id" id)))))
782 ;; A helper procedure for syntax-locally-bound-identifiers, which
783 ;; itself is a helper for transformer procedures.
784 ;; `locally-bound-identifiers' returns a list of all bindings
785 ;; visible to a syntax object with the given wrap. They are in
786 ;; order from outer to inner.
788 ;; The purpose of this procedure is to give a transformer procedure
789 ;; references on bound identifiers, that the transformer can then
790 ;; introduce some of them in its output. As such, the identifiers
791 ;; are anti-marked, so that rebuild-macro-output doesn't apply new
794 (define locally-bound-identifiers
797 (lambda (subst results)
800 (let ((fst (car subst)))
802 (scan (cdr subst) results)
803 (let ((symnames (ribcage-symnames fst))
804 (marks (ribcage-marks fst)))
805 (if (vector? symnames)
806 (scan-vector-rib subst symnames marks results)
807 (scan-list-rib subst symnames marks results))))))))
808 (define scan-list-rib
809 (lambda (subst symnames marks results)
810 (let f ((symnames symnames) (marks marks) (results results))
812 (scan (cdr subst) results)
813 (f (cdr symnames) (cdr marks)
814 (cons (wrap (car symnames)
815 (anti-mark (make-wrap (car marks) subst))
818 (define scan-vector-rib
819 (lambda (subst symnames marks results)
820 (let ((n (vector-length symnames)))
821 (let f ((i 0) (results results))
823 (scan (cdr subst) results)
825 (cons (wrap (vector-ref symnames i)
826 (anti-mark (make-wrap (vector-ref marks i) subst))
829 (scan (wrap-subst w) '())))
831 ;; Returns three values: binding type, binding value, the module (for
832 ;; resolving toplevel vars).
833 (define (resolve-identifier id w r mod)
834 (define (resolve-global var mod)
835 (let ((b (or (get-global-definition-hook var mod)
836 (make-binding 'global))))
837 (if (eq? (binding-type b) 'global)
838 (values 'global var mod)
839 (values (binding-type b) (binding-value b) mod))))
840 (define (resolve-lexical label mod)
841 (let ((b (or (assq-ref r label)
842 (make-binding 'displaced-lexical))))
843 (values (binding-type b) (binding-value b) mod)))
844 (let ((n (id-var-name id w)))
847 (resolve-global n (if (syntax-object? id)
848 (syntax-object-module id)
851 (resolve-lexical n (if (syntax-object? id)
852 (syntax-object-module id)
855 (error "unexpected id-var-name" id w n)))))
857 (define transformer-environment
860 (error "called outside the dynamic extent of a syntax transformer"))))
862 (define (with-transformer-environment k)
863 ((fluid-ref transformer-environment) k))
865 ;; free-id=? must be passed fully wrapped ids since (free-id=? x y)
866 ;; may be true even if (free-id=? (wrap x w) (wrap y w)) is not.
870 (and (eq? (id-sym-name i) (id-sym-name j)) ; accelerator
871 (eq? (id-var-name i empty-wrap) (id-var-name j empty-wrap)))))
873 ;; bound-id=? may be passed unwrapped (or partially wrapped) ids as
874 ;; long as the missing portion of the wrap is common to both of the ids
875 ;; since (bound-id=? x y) iff (bound-id=? (wrap x w) (wrap y w))
879 (if (and (syntax-object? i) (syntax-object? j))
880 (and (eq? (syntax-object-expression i)
881 (syntax-object-expression j))
882 (same-marks? (wrap-marks (syntax-object-wrap i))
883 (wrap-marks (syntax-object-wrap j))))
886 ;; "valid-bound-ids?" returns #t if it receives a list of distinct ids.
887 ;; valid-bound-ids? may be passed unwrapped (or partially wrapped) ids
888 ;; as long as the missing portion of the wrap is common to all of the
891 (define valid-bound-ids?
893 (and (let all-ids? ((ids ids))
896 (all-ids? (cdr ids)))))
897 (distinct-bound-ids? ids))))
899 ;; distinct-bound-ids? expects a list of ids and returns #t if there are
900 ;; no duplicates. It is quadratic on the length of the id list; long
901 ;; lists could be sorted to make it more efficient. distinct-bound-ids?
902 ;; may be passed unwrapped (or partially wrapped) ids as long as the
903 ;; missing portion of the wrap is common to all of the ids.
905 (define distinct-bound-ids?
907 (let distinct? ((ids ids))
909 (and (not (bound-id-member? (car ids) (cdr ids)))
910 (distinct? (cdr ids)))))))
912 (define bound-id-member?
914 (and (not (null? list))
915 (or (bound-id=? x (car list))
916 (bound-id-member? x (cdr list))))))
918 ;; wrapping expressions and identifiers
923 ((and (null? (wrap-marks w)) (null? (wrap-subst w))) x)
926 (syntax-object-expression x)
927 (join-wraps w (syntax-object-wrap x))
928 (syntax-object-module x)))
930 (else (make-syntax-object x w defmod)))))
933 (lambda (x w s defmod)
934 (wrap (decorate-source x s) w defmod)))
938 (define expand-sequence
939 (lambda (body r w s mod)
941 (let dobody ((body body) (r r) (w w) (mod mod))
944 (let ((first (expand (car body) r w mod)))
945 (cons first (dobody (cdr body) r w mod))))))))
947 ;; At top-level, we allow mixed definitions and expressions. Like
948 ;; expand-body we expand in two passes.
950 ;; First, from left to right, we expand just enough to know what
951 ;; expressions are definitions, syntax definitions, and splicing
952 ;; statements (`begin'). If we anything needs evaluating at
953 ;; expansion-time, it is expanded directly.
955 ;; Otherwise we collect expressions to expand, in thunks, and then
956 ;; expand them all at the end. This allows all syntax expanders
957 ;; visible in a toplevel sequence to be visible during the
958 ;; expansions of all normal definitions and expressions in the
961 (define expand-top-sequence
962 (lambda (body r w s m esew mod)
963 (define (scan body r w s m esew mod exps)
973 (let ((e (car body)))
974 (syntax-type e r w (or (source-annotation e) s) #f mod #f)))
975 (lambda (type value form e w s mod)
981 (scan #'(e1 e2 ...) r w s m esew mod exps))))
983 (expand-local-syntax value e r w s mod
984 (lambda (body r w s mod)
985 (scan body r w s m esew mod exps))))
988 ((_ (x ...) e1 e2 ...)
989 (let ((when-list (parse-when-list e #'(x ...)))
990 (body #'(e1 e2 ...)))
993 (if (memq 'eval when-list)
995 (if (memq 'expand when-list) 'c&e 'e)
999 (if (memq 'expand when-list)
1000 (top-level-eval-hook
1001 (expand-top-sequence body r w s 'e '(eval) mod)
1004 ((memq 'load when-list)
1005 (if (or (memq 'compile when-list)
1006 (memq 'expand when-list)
1007 (and (eq? m 'c&e) (memq 'eval when-list)))
1008 (scan body r w s 'c&e '(compile load) mod exps)
1009 (if (memq m '(c c&e))
1010 (scan body r w s 'c '(load) mod exps)
1012 ((or (memq 'compile when-list)
1013 (memq 'expand when-list)
1014 (and (eq? m 'c&e) (memq 'eval when-list)))
1015 (top-level-eval-hook
1016 (expand-top-sequence body r w s 'e '(eval) mod)
1021 ((define-syntax-form define-syntax-parameter-form)
1022 (let ((n (id-var-name value w)) (r (macros-only-env r)))
1025 (if (memq 'compile esew)
1026 (let ((e (expand-install-global n (expand e r w mod))))
1027 (top-level-eval-hook e mod)
1028 (if (memq 'load esew)
1029 (values (cons e exps))
1031 (if (memq 'load esew)
1032 (values (cons (expand-install-global n (expand e r w mod))
1036 (let ((e (expand-install-global n (expand e r w mod))))
1037 (top-level-eval-hook e mod)
1038 (values (cons e exps))))
1040 (if (memq 'eval esew)
1041 (top-level-eval-hook
1042 (expand-install-global n (expand e r w mod))
1046 (let* ((n (id-var-name value w))
1047 ;; Lookup the name in the module of the define form.
1048 (type (binding-type (lookup n r mod))))
1050 ((global core macro module-ref)
1051 ;; affect compile-time environment (once we have booted)
1052 (if (and (memq m '(c c&e))
1053 (not (module-local-variable (current-module) n))
1055 (let ((old (module-variable (current-module) n)))
1056 ;; use value of the same-named imported variable, if
1058 (if (and (variable? old) (variable-bound? old))
1059 (module-define! (current-module) n (variable-ref old))
1060 (module-add! (current-module) n (make-undefined-variable)))))
1064 (let ((x (build-global-definition s n (expand e r w mod))))
1065 (top-level-eval-hook x mod)
1068 (build-global-definition s n (expand e r w mod))))
1070 ((displaced-lexical)
1071 (syntax-violation #f "identifier out of context"
1072 (source-wrap form w s mod)
1073 (wrap value w mod)))
1075 (syntax-violation #f "cannot define keyword at top level"
1076 (source-wrap form w s mod)
1077 (wrap value w mod))))))
1081 (let ((x (expand-expr type value form e r w s mod)))
1082 (top-level-eval-hook x mod)
1085 (expand-expr type value form e r w s mod)))
1088 (scan (cdr body) r w s m esew mod exps))))))
1090 (call-with-values (lambda ()
1091 (scan body r w s m esew mod '()))
1097 (let lp ((in exps) (out '()))
1101 (cons (if (procedure? e) (e) e) out)))))))))))
1103 (define expand-install-global
1105 (build-global-definition
1110 (build-primref no-source 'make-syntax-transformer)
1111 (list (build-data no-source name)
1112 (build-data no-source 'macro)
1115 (define parse-when-list
1116 (lambda (e when-list)
1117 ;; when-list is syntax'd version of list of situations
1118 (let ((result (strip when-list empty-wrap)))
1119 (let lp ((l result))
1122 (if (memq (car l) '(compile load eval expand))
1124 (syntax-violation 'eval-when "invalid situation" e
1127 ;; syntax-type returns seven values: type, value, form, e, w, s, and
1128 ;; mod. The first two are described in the table below.
1130 ;; type value explanation
1131 ;; -------------------------------------------------------------------
1132 ;; core procedure core singleton
1133 ;; core-form procedure core form
1134 ;; module-ref procedure @ or @@ singleton
1135 ;; lexical name lexical variable reference
1136 ;; global name global variable reference
1137 ;; begin none begin keyword
1138 ;; define none define keyword
1139 ;; define-syntax none define-syntax keyword
1140 ;; define-syntax-parameter none define-syntax-parameter keyword
1141 ;; local-syntax rec? letrec-syntax/let-syntax keyword
1142 ;; eval-when none eval-when keyword
1143 ;; syntax level pattern variable
1144 ;; displaced-lexical none displaced lexical identifier
1145 ;; lexical-call name call to lexical variable
1146 ;; global-call name call to global variable
1147 ;; call none any other call
1148 ;; begin-form none begin expression
1149 ;; define-form id variable definition
1150 ;; define-syntax-form id syntax definition
1151 ;; define-syntax-parameter-form id syntax parameter definition
1152 ;; local-syntax-form rec? syntax definition
1153 ;; eval-when-form none eval-when form
1154 ;; constant none self-evaluating datum
1155 ;; other none anything else
1157 ;; form is the entire form. For definition forms (define-form,
1158 ;; define-syntax-form, and define-syntax-parameter-form), e is the
1159 ;; rhs expression. For all others, e is the entire form. w is the
1160 ;; wrap for both form and e. s is the source for the entire form.
1161 ;; mod is the module for both form and e.
1163 ;; syntax-type expands macros and unwraps as necessary to get to one
1164 ;; of the forms above. It also parses definition forms, although
1165 ;; perhaps this should be done by the consumer.
1168 (lambda (e r w s rib mod for-car?)
1171 (let* ((n (id-var-name e w))
1172 (b (lookup n r mod))
1173 (type (binding-type b)))
1175 ((lexical) (values type (binding-value b) e e w s mod))
1176 ((global) (values type n e e w s mod))
1179 (values type (binding-value b) e e w s mod)
1180 (syntax-type (expand-macro (binding-value b) e r w s rib mod)
1181 r empty-wrap s rib mod #f)))
1182 (else (values type (binding-value b) e e w s mod)))))
1184 (let ((first (car e)))
1186 (lambda () (syntax-type first r w s rib mod #t))
1187 (lambda (ftype fval fform fe fw fs fmod)
1190 (values 'lexical-call fval e e w s mod))
1192 ;; If we got here via an (@@ ...) expansion, we need to
1193 ;; make sure the fmod information is propagated back
1194 ;; correctly -- hence this consing.
1195 (values 'global-call (make-syntax-object fval w fmod)
1198 (syntax-type (expand-macro fval e r w s rib mod)
1199 r empty-wrap s rib mod for-car?))
1201 (call-with-values (lambda () (fval e r w))
1202 (lambda (e r w s mod)
1203 (syntax-type e r w s rib mod for-car?))))
1205 (values 'core-form fval e e w s mod))
1207 (values 'local-syntax-form fval e e w s mod))
1209 (values 'begin-form #f e e w s mod))
1211 (values 'eval-when-form #f e e w s mod))
1216 (values 'define-form #'name e #'val w s mod))
1217 ((_ (name . args) e1 e2 ...)
1219 (valid-bound-ids? (lambda-var-list #'args)))
1220 ;; need lambda here...
1221 (values 'define-form (wrap #'name w mod)
1224 (cons #'lambda (wrap #'(args e1 e2 ...) w mod))
1229 (values 'define-form (wrap #'name w mod)
1232 empty-wrap s mod))))
1237 (values 'define-syntax-form #'name e #'val w s mod))))
1238 ((define-syntax-parameter)
1242 (values 'define-syntax-parameter-form #'name e #'val w s mod))))
1244 (values 'call #f e e w s mod)))))))
1246 (syntax-type (syntax-object-expression e)
1248 (join-wraps w (syntax-object-wrap e))
1249 (or (source-annotation e) s) rib
1250 (or (syntax-object-module e) mod) for-car?))
1251 ((self-evaluating? e) (values 'constant #f e e w s mod))
1252 (else (values 'other #f e e w s mod)))))
1257 (lambda () (syntax-type e r w (source-annotation e) #f mod #f))
1258 (lambda (type value form e w s mod)
1259 (expand-expr type value form e r w s mod)))))
1262 (lambda (type value form e r w s mod)
1265 (build-lexical-reference 'value s e value))
1267 ;; apply transformer
1268 (value e r w s mod))
1270 (call-with-values (lambda () (value e r w))
1271 (lambda (e r w s mod)
1272 (expand e r w mod))))
1276 (build-lexical-reference 'fun (source-annotation id)
1277 (if (syntax-object? id)
1284 (build-global-reference (source-annotation (car e))
1285 (if (syntax-object? value)
1286 (syntax-object-expression value)
1288 (if (syntax-object? value)
1289 (syntax-object-module value)
1292 ((constant) (build-data s (strip (source-wrap e w s mod) empty-wrap)))
1293 ((global) (build-global-reference s value mod))
1294 ((call) (expand-application (expand (car e) r w mod) e r w s mod))
1297 ((_ e1 e2 ...) (expand-sequence #'(e1 e2 ...) r w s mod))
1299 (if (include-deprecated-features)
1301 (issue-deprecation-warning
1302 "Sequences of zero expressions are deprecated. Use *unspecified*.")
1304 (syntax-violation #f "sequence of zero expressions"
1305 (source-wrap e w s mod))))))
1306 ((local-syntax-form)
1307 (expand-local-syntax value e r w s mod expand-sequence))
1310 ((_ (x ...) e1 e2 ...)
1311 (let ((when-list (parse-when-list e #'(x ...))))
1312 (if (memq 'eval when-list)
1313 (expand-sequence #'(e1 e2 ...) r w s mod)
1315 ((define-form define-syntax-form define-syntax-parameter-form)
1316 (syntax-violation #f "definition in expression context, where definitions are not allowed,"
1317 (source-wrap form w s mod)))
1319 (syntax-violation #f "reference to pattern variable outside syntax form"
1320 (source-wrap e w s mod)))
1321 ((displaced-lexical)
1322 (syntax-violation #f "reference to identifier outside its scope"
1323 (source-wrap e w s mod)))
1324 (else (syntax-violation #f "unexpected syntax"
1325 (source-wrap e w s mod))))))
1327 (define expand-application
1328 (lambda (x e r w s mod)
1331 (build-application s x
1332 (map (lambda (e) (expand e r w mod)) #'(e1 ...)))))))
1334 ;; (What follows is my interpretation of what's going on here -- Andy)
1336 ;; A macro takes an expression, a tree, the leaves of which are identifiers
1337 ;; and datums. Identifiers are symbols along with a wrap and a module. For
1338 ;; efficiency, subtrees that share wraps and modules may be grouped as one
1341 ;; Going into the expansion, the expression is given an anti-mark, which
1342 ;; logically propagates to all leaves. Then, in the new expression returned
1343 ;; from the transfomer, if we see an expression with an anti-mark, we know it
1344 ;; pertains to the original expression; conversely, expressions without the
1345 ;; anti-mark are known to be introduced by the transformer.
1347 ;; OK, good until now. We know this algorithm does lexical scoping
1348 ;; appropriately because it's widely known in the literature, and psyntax is
1349 ;; widely used. But what about modules? Here we're on our own. What we do is
1350 ;; to mark the module of expressions produced by a macro as pertaining to the
1351 ;; module that was current when the macro was defined -- that is, free
1352 ;; identifiers introduced by a macro are scoped in the macro's module, not in
1353 ;; the expansion's module. Seems to work well.
1355 ;; The only wrinkle is when we want a macro to expand to code in another
1356 ;; module, as is the case for the r6rs `library' form -- the body expressions
1357 ;; should be scoped relative the new module, the one defined by the macro.
1358 ;; For that, use `(@@ mod-name body)'.
1360 ;; Part of the macro output will be from the site of the macro use and part
1361 ;; from the macro definition. We allow source information from the macro use
1362 ;; to pass through, but we annotate the parts coming from the macro with the
1363 ;; source location information corresponding to the macro use. It would be
1364 ;; really nice if we could also annotate introduced expressions with the
1365 ;; locations corresponding to the macro definition, but that is not yet
1367 (define expand-macro
1368 (lambda (p e r w s rib mod)
1369 (define rebuild-macro-output
1373 (cons (rebuild-macro-output (car x) m)
1374 (rebuild-macro-output (cdr x) m))
1377 (let ((w (syntax-object-wrap x)))
1378 (let ((ms (wrap-marks w)) (ss (wrap-subst w)))
1379 (if (and (pair? ms) (eq? (car ms) the-anti-mark))
1380 ;; output is from original text
1382 (syntax-object-expression x)
1383 (make-wrap (cdr ms) (if rib (cons rib (cdr ss)) (cdr ss)))
1384 (syntax-object-module x))
1385 ;; output introduced by macro
1387 (decorate-source (syntax-object-expression x) s)
1388 (make-wrap (cons m ms)
1390 (cons rib (cons 'shift ss))
1392 (syntax-object-module x))))))
1395 (let* ((n (vector-length x))
1396 (v (decorate-source (make-vector n) s)))
1397 (do ((i 0 (fx+ i 1)))
1400 (rebuild-macro-output (vector-ref x i) m)))))
1402 (syntax-violation #f "encountered raw symbol in macro output"
1403 (source-wrap e w (wrap-subst w) mod) x))
1404 (else (decorate-source x s)))))
1405 (with-fluids ((transformer-environment
1406 (lambda (k) (k e r w s rib mod))))
1407 (rebuild-macro-output (p (source-wrap e (anti-mark w) s mod))
1411 ;; In processing the forms of the body, we create a new, empty wrap.
1412 ;; This wrap is augmented (destructively) each time we discover that
1413 ;; the next form is a definition. This is done:
1415 ;; (1) to allow the first nondefinition form to be a call to
1416 ;; one of the defined ids even if the id previously denoted a
1417 ;; definition keyword or keyword for a macro expanding into a
1419 ;; (2) to prevent subsequent definition forms (but unfortunately
1420 ;; not earlier ones) and the first nondefinition form from
1421 ;; confusing one of the bound identifiers for an auxiliary
1423 ;; (3) so that we do not need to restart the expansion of the
1424 ;; first nondefinition form, which is problematic anyway
1425 ;; since it might be the first element of a begin that we
1426 ;; have just spliced into the body (meaning if we restarted,
1427 ;; we'd really need to restart with the begin or the macro
1428 ;; call that expanded into the begin, and we'd have to give
1429 ;; up allowing (begin <defn>+ <expr>+), which is itself
1430 ;; problematic since we don't know if a begin contains only
1431 ;; definitions until we've expanded it).
1433 ;; Before processing the body, we also create a new environment
1434 ;; containing a placeholder for the bindings we will add later and
1435 ;; associate this environment with each form. In processing a
1436 ;; let-syntax or letrec-syntax, the associated environment may be
1437 ;; augmented with local keyword bindings, so the environment may
1438 ;; be different for different forms in the body. Once we have
1439 ;; gathered up all of the definitions, we evaluate the transformer
1440 ;; expressions and splice into r at the placeholder the new variable
1441 ;; and keyword bindings. This allows let-syntax or letrec-syntax
1442 ;; forms local to a portion or all of the body to shadow the
1443 ;; definition bindings.
1445 ;; Subforms of a begin, let-syntax, or letrec-syntax are spliced
1448 ;; outer-form is fully wrapped w/source
1449 (lambda (body outer-form r w mod)
1450 (let* ((r (cons '("placeholder" . (placeholder)) r))
1451 (ribcage (make-empty-ribcage))
1452 (w (make-wrap (wrap-marks w) (cons ribcage (wrap-subst w)))))
1453 (let parse ((body (map (lambda (x) (cons r (wrap x w mod))) body))
1454 (ids '()) (labels '())
1455 (var-ids '()) (vars '()) (vals '()) (bindings '()))
1457 (syntax-violation #f "no expressions in body" outer-form)
1458 (let ((e (cdar body)) (er (caar body)))
1460 (lambda () (syntax-type e er empty-wrap (source-annotation e) ribcage mod #f))
1461 (lambda (type value form e w s mod)
1464 (let ((id (wrap value w mod)) (label (gen-label)))
1465 (let ((var (gen-var id)))
1466 (extend-ribcage! ribcage id label)
1468 (cons id ids) (cons label labels)
1470 (cons var vars) (cons (cons er (wrap e w mod)) vals)
1471 (cons (make-binding 'lexical var) bindings)))))
1472 ((define-syntax-form define-syntax-parameter-form)
1473 (let ((id (wrap value w mod))
1475 (trans-r (macros-only-env er)))
1476 (extend-ribcage! ribcage id label)
1477 ;; As required by R6RS, evaluate the right-hand-sides of internal
1478 ;; syntax definition forms and add their transformers to the
1479 ;; compile-time environment immediately, so that the newly-defined
1480 ;; keywords may be used in definition context within the same
1482 (set-cdr! r (extend-env (list label)
1483 (list (make-binding 'macro
1484 (eval-local-transformer
1485 (expand e trans-r w mod)
1488 (parse (cdr body) (cons id ids) labels var-ids vars vals bindings)))
1492 (parse (let f ((forms #'(e1 ...)))
1495 (cons (cons er (wrap (car forms) w mod))
1497 ids labels var-ids vars vals bindings))))
1498 ((local-syntax-form)
1499 (expand-local-syntax value e er w s mod
1500 (lambda (forms er w s mod)
1501 (parse (let f ((forms forms))
1504 (cons (cons er (wrap (car forms) w mod))
1506 ids labels var-ids vars vals bindings))))
1507 (else ; found a non-definition
1509 (build-sequence no-source
1511 (expand (cdr x) (car x) empty-wrap mod))
1512 (cons (cons er (source-wrap e w s mod))
1515 (if (not (valid-bound-ids? ids))
1517 #f "invalid or duplicate identifier in definition"
1519 (set-cdr! r (extend-env labels bindings (cdr r)))
1520 (build-letrec no-source #t
1521 (reverse (map syntax->datum var-ids))
1524 (expand (cdr x) (car x) empty-wrap mod))
1526 (build-sequence no-source
1528 (expand (cdr x) (car x) empty-wrap mod))
1529 (cons (cons er (source-wrap e w s mod))
1530 (cdr body)))))))))))))))))
1532 (define expand-local-syntax
1533 (lambda (rec? e r w s mod k)
1535 ((_ ((id val) ...) e1 e2 ...)
1536 (let ((ids #'(id ...)))
1537 (if (not (valid-bound-ids? ids))
1538 (syntax-violation #f "duplicate bound keyword" e)
1539 (let ((labels (gen-labels ids)))
1540 (let ((new-w (make-binding-wrap ids labels w)))
1544 (let ((w (if rec? new-w w))
1545 (trans-r (macros-only-env r)))
1547 (make-binding 'macro
1548 (eval-local-transformer
1549 (expand x trans-r w mod)
1556 (_ (syntax-violation #f "bad local syntax definition"
1557 (source-wrap e w s mod))))))
1559 (define eval-local-transformer
1560 (lambda (expanded mod)
1561 (let ((p (local-eval-hook expanded mod)))
1564 (syntax-violation #f "nonprocedure transformer" p)))))
1568 (build-void no-source)))
1572 (and (nonsymbol-id? x)
1573 (free-id=? x #'(... ...)))))
1575 (define lambda-formals
1577 (define (req args rreq)
1578 (syntax-case args ()
1580 (check (reverse rreq) #f))
1582 (req #'b (cons #'a rreq)))
1584 (check (reverse rreq) #'r))
1586 (syntax-violation 'lambda "invalid argument list" orig-args args))))
1587 (define (check req rest)
1589 ((distinct-bound-ids? (if rest (cons rest req) req))
1590 (values req #f rest #f))
1592 (syntax-violation 'lambda "duplicate identifier in argument list"
1594 (req orig-args '())))
1596 (define expand-simple-lambda
1597 (lambda (e r w s mod req rest meta body)
1598 (let* ((ids (if rest (append req (list rest)) req))
1599 (vars (map gen-var ids))
1600 (labels (gen-labels ids)))
1601 (build-simple-lambda
1603 (map syntax->datum req) (and rest (syntax->datum rest)) vars
1605 (expand-body body (source-wrap e w s mod)
1606 (extend-var-env labels vars r)
1607 (make-binding-wrap ids labels w)
1610 (define lambda*-formals
1612 (define (req args rreq)
1613 (syntax-case args ()
1615 (check (reverse rreq) '() #f '()))
1617 (req #'b (cons #'a rreq)))
1618 ((a . b) (eq? (syntax->datum #'a) #:optional)
1619 (opt #'b (reverse rreq) '()))
1620 ((a . b) (eq? (syntax->datum #'a) #:key)
1621 (key #'b (reverse rreq) '() '()))
1622 ((a b) (eq? (syntax->datum #'a) #:rest)
1623 (rest #'b (reverse rreq) '() '()))
1625 (rest #'r (reverse rreq) '() '()))
1627 (syntax-violation 'lambda* "invalid argument list" orig-args args))))
1628 (define (opt args req ropt)
1629 (syntax-case args ()
1631 (check req (reverse ropt) #f '()))
1633 (opt #'b req (cons #'(a #f) ropt)))
1634 (((a init) . b) (id? #'a)
1635 (opt #'b req (cons #'(a init) ropt)))
1636 ((a . b) (eq? (syntax->datum #'a) #:key)
1637 (key #'b req (reverse ropt) '()))
1638 ((a b) (eq? (syntax->datum #'a) #:rest)
1639 (rest #'b req (reverse ropt) '()))
1641 (rest #'r req (reverse ropt) '()))
1643 (syntax-violation 'lambda* "invalid optional argument list"
1645 (define (key args req opt rkey)
1646 (syntax-case args ()
1648 (check req opt #f (cons #f (reverse rkey))))
1650 (with-syntax ((k (symbol->keyword (syntax->datum #'a))))
1651 (key #'b req opt (cons #'(k a #f) rkey))))
1652 (((a init) . b) (id? #'a)
1653 (with-syntax ((k (symbol->keyword (syntax->datum #'a))))
1654 (key #'b req opt (cons #'(k a init) rkey))))
1655 (((a init k) . b) (and (id? #'a)
1656 (keyword? (syntax->datum #'k)))
1657 (key #'b req opt (cons #'(k a init) rkey)))
1658 ((aok) (eq? (syntax->datum #'aok) #:allow-other-keys)
1659 (check req opt #f (cons #t (reverse rkey))))
1660 ((aok a b) (and (eq? (syntax->datum #'aok) #:allow-other-keys)
1661 (eq? (syntax->datum #'a) #:rest))
1662 (rest #'b req opt (cons #t (reverse rkey))))
1663 ((aok . r) (and (eq? (syntax->datum #'aok) #:allow-other-keys)
1665 (rest #'r req opt (cons #t (reverse rkey))))
1666 ((a b) (eq? (syntax->datum #'a) #:rest)
1667 (rest #'b req opt (cons #f (reverse rkey))))
1669 (rest #'r req opt (cons #f (reverse rkey))))
1671 (syntax-violation 'lambda* "invalid keyword argument list"
1673 (define (rest args req opt kw)
1674 (syntax-case args ()
1676 (check req opt #'r kw))
1678 (syntax-violation 'lambda* "invalid rest argument"
1680 (define (check req opt rest kw)
1682 ((distinct-bound-ids?
1683 (append req (map car opt) (if rest (list rest) '())
1684 (if (pair? kw) (map cadr (cdr kw)) '())))
1685 (values req opt rest kw))
1687 (syntax-violation 'lambda* "duplicate identifier in argument list"
1689 (req orig-args '())))
1691 (define expand-lambda-case
1692 (lambda (e r w s mod get-formals clauses)
1693 (define (parse-req req opt rest kw body)
1694 (let ((vars (map gen-var req))
1695 (labels (gen-labels req)))
1696 (let ((r* (extend-var-env labels vars r))
1697 (w* (make-binding-wrap req labels w)))
1698 (parse-opt (map syntax->datum req)
1699 opt rest kw body (reverse vars) r* w* '() '()))))
1700 (define (parse-opt req opt rest kw body vars r* w* out inits)
1703 (syntax-case (car opt) ()
1705 (let* ((v (gen-var #'id))
1706 (l (gen-labels (list v)))
1707 (r** (extend-var-env l (list v) r*))
1708 (w** (make-binding-wrap (list #'id) l w*)))
1709 (parse-opt req (cdr opt) rest kw body (cons v vars)
1710 r** w** (cons (syntax->datum #'id) out)
1711 (cons (expand #'i r* w* mod) inits))))))
1713 (let* ((v (gen-var rest))
1714 (l (gen-labels (list v)))
1715 (r* (extend-var-env l (list v) r*))
1716 (w* (make-binding-wrap (list rest) l w*)))
1717 (parse-kw req (if (pair? out) (reverse out) #f)
1718 (syntax->datum rest)
1719 (if (pair? kw) (cdr kw) kw)
1720 body (cons v vars) r* w*
1721 (if (pair? kw) (car kw) #f)
1724 (parse-kw req (if (pair? out) (reverse out) #f) #f
1725 (if (pair? kw) (cdr kw) kw)
1727 (if (pair? kw) (car kw) #f)
1729 (define (parse-kw req opt rest kw body vars r* w* aok out inits)
1732 (syntax-case (car kw) ()
1734 (let* ((v (gen-var #'id))
1735 (l (gen-labels (list v)))
1736 (r** (extend-var-env l (list v) r*))
1737 (w** (make-binding-wrap (list #'id) l w*)))
1738 (parse-kw req opt rest (cdr kw) body (cons v vars)
1740 (cons (list (syntax->datum #'k)
1741 (syntax->datum #'id)
1744 (cons (expand #'i r* w* mod) inits))))))
1746 (parse-body req opt rest
1747 (if (or aok (pair? out)) (cons aok (reverse out)) #f)
1748 body (reverse vars) r* w* (reverse inits) '()))))
1749 (define (parse-body req opt rest kw body vars r* w* inits meta)
1750 (syntax-case body ()
1751 ((docstring e1 e2 ...) (string? (syntax->datum #'docstring))
1752 (parse-body req opt rest kw #'(e1 e2 ...) vars r* w* inits
1755 . ,(syntax->datum #'docstring))))))
1756 ((#((k . v) ...) e1 e2 ...)
1757 (parse-body req opt rest kw #'(e1 e2 ...) vars r* w* inits
1758 (append meta (syntax->datum #'((k . v) ...)))))
1760 (values meta req opt rest kw inits vars
1761 (expand-body #'(e1 e2 ...) (source-wrap e w s mod)
1764 (syntax-case clauses ()
1765 (() (values '() #f))
1766 (((args e1 e2 ...) (args* e1* e2* ...) ...)
1767 (call-with-values (lambda () (get-formals #'args))
1768 (lambda (req opt rest kw)
1769 (call-with-values (lambda ()
1770 (parse-req req opt rest kw #'(e1 e2 ...)))
1771 (lambda (meta req opt rest kw inits vars body)
1774 (expand-lambda-case e r w s mod get-formals
1775 #'((args* e1* e2* ...) ...)))
1776 (lambda (meta* else*)
1779 (build-lambda-case s req opt rest kw inits vars
1780 body else*))))))))))))
1784 ;; strips syntax-objects down to top-wrap
1786 ;; since only the head of a list is annotated by the reader, not each pair
1787 ;; in the spine, we also check for pairs whose cars are annotated in case
1788 ;; we've been passed the cdr of an annotated list
1797 (strip (syntax-object-expression x) (syntax-object-wrap x)))
1799 (let ((a (f (car x))) (d (f (cdr x))))
1800 (if (and (eq? a (car x)) (eq? d (cdr x)))
1804 (let ((old (vector->list x)))
1805 (let ((new (map f old)))
1806 ;; inlined and-map with two args
1807 (let lp ((l1 old) (l2 new))
1810 (if (eq? (car l1) (car l2))
1811 (lp (cdr l1) (cdr l2))
1812 (list->vector new)))))))
1815 ;; lexical variables
1819 (let ((id (if (syntax-object? id) (syntax-object-expression id) id)))
1820 (build-lexical-var no-source id))))
1822 ;; appears to return a reversed list
1823 (define lambda-var-list
1825 (let lvl ((vars vars) (ls '()) (w empty-wrap))
1827 ((pair? vars) (lvl (cdr vars) (cons (wrap (car vars) w #f) ls) w))
1828 ((id? vars) (cons (wrap vars w #f) ls))
1830 ((syntax-object? vars)
1831 (lvl (syntax-object-expression vars)
1833 (join-wraps w (syntax-object-wrap vars))))
1834 ;; include anything else to be caught by subsequent error
1836 (else (cons vars ls))))))
1838 ;; core transformers
1840 (global-extend 'local-syntax 'letrec-syntax #t)
1841 (global-extend 'local-syntax 'let-syntax #f)
1843 (global-extend 'core 'syntax-parameterize
1844 (lambda (e r w s mod)
1846 ((_ ((var val) ...) e1 e2 ...)
1847 (valid-bound-ids? #'(var ...))
1848 (let ((names (map (lambda (x) (id-var-name x w)) #'(var ...))))
1851 (case (binding-type (lookup n r mod))
1852 ((displaced-lexical)
1853 (syntax-violation 'syntax-parameterize
1854 "identifier out of context"
1856 (source-wrap id w s mod)))))
1861 (source-wrap e w s mod)
1864 (let ((trans-r (macros-only-env r)))
1866 (make-binding 'macro
1867 (eval-local-transformer (expand x trans-r w mod)
1873 (_ (syntax-violation 'syntax-parameterize "bad syntax"
1874 (source-wrap e w s mod))))))
1876 (global-extend 'core 'quote
1877 (lambda (e r w s mod)
1879 ((_ e) (build-data s (strip #'e w)))
1880 (_ (syntax-violation 'quote "bad syntax"
1881 (source-wrap e w s mod))))))
1883 (global-extend 'core 'syntax
1886 (lambda (src e r maps ellipsis? mod)
1888 (let ((label (id-var-name e empty-wrap)))
1889 ;; Mod does not matter, we are looking to see if
1890 ;; the id is lexical syntax.
1891 (let ((b (lookup label r mod)))
1892 (if (eq? (binding-type b) 'syntax)
1895 (let ((var.lev (binding-value b)))
1896 (gen-ref src (car var.lev) (cdr var.lev) maps)))
1897 (lambda (var maps) (values `(ref ,var) maps)))
1899 (syntax-violation 'syntax "misplaced ellipsis" src)
1900 (values `(quote ,e) maps)))))
1904 (gen-syntax src #'e r maps (lambda (x) #f) mod))
1906 ;; this could be about a dozen lines of code, except that we
1907 ;; choose to handle #'(x ... ...) forms
1913 (gen-syntax src #'x r
1914 (cons '() maps) ellipsis? mod))
1916 (if (null? (car maps))
1917 (syntax-violation 'syntax "extra ellipsis"
1919 (values (gen-map x (car maps))
1927 (lambda () (k (cons '() maps)))
1929 (if (null? (car maps))
1930 (syntax-violation 'syntax "extra ellipsis" src)
1931 (values (gen-mappend x (car maps))
1933 (_ (call-with-values
1934 (lambda () (gen-syntax src y r maps ellipsis? mod))
1937 (lambda () (k maps))
1939 (values (gen-append x y) maps)))))))))
1942 (lambda () (gen-syntax src #'x r maps ellipsis? mod))
1945 (lambda () (gen-syntax src #'y r maps ellipsis? mod))
1946 (lambda (y maps) (values (gen-cons x y) maps))))))
1950 (gen-syntax src #'(e1 e2 ...) r maps ellipsis? mod))
1951 (lambda (e maps) (values (gen-vector e) maps))))
1952 (_ (values `(quote ,e) maps))))))
1955 (lambda (src var level maps)
1959 (syntax-violation 'syntax "missing ellipsis" src)
1961 (lambda () (gen-ref src var (fx- level 1) (cdr maps)))
1962 (lambda (outer-var outer-maps)
1963 (let ((b (assq outer-var (car maps))))
1965 (values (cdr b) maps)
1966 (let ((inner-var (gen-var 'tmp)))
1968 (cons (cons (cons outer-var inner-var)
1970 outer-maps)))))))))))
1974 `(apply (primitive append) ,(gen-map e map-env))))
1978 (let ((formals (map cdr map-env))
1979 (actuals (map (lambda (x) `(ref ,(car x))) map-env)))
1982 ;; identity map equivalence:
1983 ;; (map (lambda (x) x) y) == y
1986 (lambda (x) (and (eq? (car x) 'ref) (memq (cadr x) formals)))
1988 ;; eta map equivalence:
1989 ;; (map (lambda (x ...) (f x ...)) y ...) == (map f y ...)
1990 `(map (primitive ,(car e))
1991 ,@(map (let ((r (map cons formals actuals)))
1992 (lambda (x) (cdr (assq (cadr x) r))))
1994 (else `(map (lambda ,formals ,e) ,@actuals))))))
2000 (if (eq? (car x) 'quote)
2001 `(quote (,(cadr x) . ,(cadr y)))
2002 (if (eq? (cadr y) '())
2005 ((list) `(list ,x ,@(cdr y)))
2006 (else `(cons ,x ,y)))))
2010 (if (equal? y '(quote ()))
2017 ((eq? (car x) 'list) `(vector ,@(cdr x)))
2018 ((eq? (car x) 'quote) `(quote #(,@(cadr x))))
2019 (else `(list->vector ,x)))))
2025 ((ref) (build-lexical-reference 'value no-source (cadr x) (cadr x)))
2026 ((primitive) (build-primref no-source (cadr x)))
2027 ((quote) (build-data no-source (cadr x)))
2029 (if (list? (cadr x))
2030 (build-simple-lambda no-source (cadr x) #f (cadr x) '() (regen (caddr x)))
2031 (error "how did we get here" x)))
2032 (else (build-application no-source
2033 (build-primref no-source (car x))
2034 (map regen (cdr x)))))))
2036 (lambda (e r w s mod)
2037 (let ((e (source-wrap e w s mod)))
2041 (lambda () (gen-syntax e #'x r '() ellipsis? mod))
2042 (lambda (e maps) (regen e))))
2043 (_ (syntax-violation 'syntax "bad `syntax' form" e)))))))
2045 (global-extend 'core 'lambda
2046 (lambda (e r w s mod)
2049 (call-with-values (lambda () (lambda-formals #'args))
2050 (lambda (req opt rest kw)
2051 (let lp ((body #'(e1 e2 ...)) (meta '()))
2052 (syntax-case body ()
2053 ((docstring e1 e2 ...) (string? (syntax->datum #'docstring))
2057 . ,(syntax->datum #'docstring))))))
2058 ((#((k . v) ...) e1 e2 ...)
2060 (append meta (syntax->datum #'((k . v) ...)))))
2061 (_ (expand-simple-lambda e r w s mod req rest meta body)))))))
2062 (_ (syntax-violation 'lambda "bad lambda" e)))))
2064 (global-extend 'core 'lambda*
2065 (lambda (e r w s mod)
2070 (expand-lambda-case e r w s mod
2071 lambda*-formals #'((args e1 e2 ...))))
2072 (lambda (meta lcase)
2073 (build-case-lambda s meta lcase))))
2074 (_ (syntax-violation 'lambda "bad lambda*" e)))))
2076 (global-extend 'core 'case-lambda
2077 (lambda (e r w s mod)
2078 (define (build-it meta clauses)
2081 (expand-lambda-case e r w s mod
2084 (lambda (meta* lcase)
2085 (build-case-lambda s (append meta meta*) lcase))))
2087 ((_ (args e1 e2 ...) ...)
2088 (build-it '() #'((args e1 e2 ...) ...)))
2089 ((_ docstring (args e1 e2 ...) ...)
2090 (string? (syntax->datum #'docstring))
2091 (build-it `((documentation
2092 . ,(syntax->datum #'docstring)))
2093 #'((args e1 e2 ...) ...)))
2094 (_ (syntax-violation 'case-lambda "bad case-lambda" e)))))
2096 (global-extend 'core 'case-lambda*
2097 (lambda (e r w s mod)
2098 (define (build-it meta clauses)
2101 (expand-lambda-case e r w s mod
2104 (lambda (meta* lcase)
2105 (build-case-lambda s (append meta meta*) lcase))))
2107 ((_ (args e1 e2 ...) ...)
2108 (build-it '() #'((args e1 e2 ...) ...)))
2109 ((_ docstring (args e1 e2 ...) ...)
2110 (string? (syntax->datum #'docstring))
2111 (build-it `((documentation
2112 . ,(syntax->datum #'docstring)))
2113 #'((args e1 e2 ...) ...)))
2114 (_ (syntax-violation 'case-lambda "bad case-lambda*" e)))))
2116 (global-extend 'core 'let
2118 (define (expand-let e r w s mod constructor ids vals exps)
2119 (if (not (valid-bound-ids? ids))
2120 (syntax-violation 'let "duplicate bound variable" e)
2121 (let ((labels (gen-labels ids))
2122 (new-vars (map gen-var ids)))
2123 (let ((nw (make-binding-wrap ids labels w))
2124 (nr (extend-var-env labels new-vars r)))
2126 (map syntax->datum ids)
2128 (map (lambda (x) (expand x r w mod)) vals)
2129 (expand-body exps (source-wrap e nw s mod)
2131 (lambda (e r w s mod)
2133 ((_ ((id val) ...) e1 e2 ...)
2134 (and-map id? #'(id ...))
2135 (expand-let e r w s mod
2140 ((_ f ((id val) ...) e1 e2 ...)
2141 (and (id? #'f) (and-map id? #'(id ...)))
2142 (expand-let e r w s mod
2147 (_ (syntax-violation 'let "bad let" (source-wrap e w s mod)))))))
2150 (global-extend 'core 'letrec
2151 (lambda (e r w s mod)
2153 ((_ ((id val) ...) e1 e2 ...)
2154 (and-map id? #'(id ...))
2155 (let ((ids #'(id ...)))
2156 (if (not (valid-bound-ids? ids))
2157 (syntax-violation 'letrec "duplicate bound variable" e)
2158 (let ((labels (gen-labels ids))
2159 (new-vars (map gen-var ids)))
2160 (let ((w (make-binding-wrap ids labels w))
2161 (r (extend-var-env labels new-vars r)))
2163 (map syntax->datum ids)
2165 (map (lambda (x) (expand x r w mod)) #'(val ...))
2166 (expand-body #'(e1 e2 ...)
2167 (source-wrap e w s mod) r w mod)))))))
2168 (_ (syntax-violation 'letrec "bad letrec" (source-wrap e w s mod))))))
2171 (global-extend 'core 'letrec*
2172 (lambda (e r w s mod)
2174 ((_ ((id val) ...) e1 e2 ...)
2175 (and-map id? #'(id ...))
2176 (let ((ids #'(id ...)))
2177 (if (not (valid-bound-ids? ids))
2178 (syntax-violation 'letrec* "duplicate bound variable" e)
2179 (let ((labels (gen-labels ids))
2180 (new-vars (map gen-var ids)))
2181 (let ((w (make-binding-wrap ids labels w))
2182 (r (extend-var-env labels new-vars r)))
2184 (map syntax->datum ids)
2186 (map (lambda (x) (expand x r w mod)) #'(val ...))
2187 (expand-body #'(e1 e2 ...)
2188 (source-wrap e w s mod) r w mod)))))))
2189 (_ (syntax-violation 'letrec* "bad letrec*" (source-wrap e w s mod))))))
2192 (global-extend 'core 'set!
2193 (lambda (e r w s mod)
2197 (let ((n (id-var-name #'id w))
2198 ;; Lookup id in its module
2199 (id-mod (if (syntax-object? #'id)
2200 (syntax-object-module #'id)
2202 (let ((b (lookup n r id-mod)))
2203 (case (binding-type b)
2205 (build-lexical-assignment s
2206 (syntax->datum #'id)
2208 (expand #'val r w mod)))
2210 (build-global-assignment s n (expand #'val r w mod) id-mod))
2212 (let ((p (binding-value b)))
2213 (if (procedure-property p 'variable-transformer)
2214 ;; As syntax-type does, call expand-macro with
2215 ;; the mod of the expression. Hmm.
2216 (expand (expand-macro p e r w s #f mod) r empty-wrap mod)
2217 (syntax-violation 'set! "not a variable transformer"
2219 (wrap #'id w id-mod)))))
2220 ((displaced-lexical)
2221 (syntax-violation 'set! "identifier out of context"
2223 (else (syntax-violation 'set! "bad set!"
2224 (source-wrap e w s mod)))))))
2225 ((_ (head tail ...) val)
2227 (lambda () (syntax-type #'head r empty-wrap no-source #f mod #t))
2228 (lambda (type value formform ee ww ss modmod)
2231 (let ((val (expand #'val r w mod)))
2232 (call-with-values (lambda () (value #'(head tail ...) r w))
2233 (lambda (e r w s* mod)
2236 (build-global-assignment s (syntax->datum #'e)
2239 (build-application s
2240 (expand #'(setter head) r w mod)
2241 (map (lambda (e) (expand e r w mod))
2242 #'(tail ... val))))))))
2243 (_ (syntax-violation 'set! "bad set!" (source-wrap e w s mod))))))
2245 (global-extend 'module-ref '@
2249 (and (and-map id? #'(mod ...)) (id? #'id))
2250 ;; Strip the wrap from the identifier and return top-wrap
2251 ;; so that the identifier will not be captured by lexicals.
2252 (values (syntax->datum #'id) r top-wrap #f
2254 #'(public mod ...)))))))
2256 (global-extend 'module-ref '@@
2261 (cons (remodulate (car x) mod)
2262 (remodulate (cdr x) mod)))
2265 (remodulate (syntax-object-expression x) mod)
2266 (syntax-object-wrap x)
2267 ;; hither the remodulation
2270 (let* ((n (vector-length x)) (v (make-vector n)))
2271 (do ((i 0 (fx+ i 1)))
2273 (vector-set! v i (remodulate (vector-ref x i) mod)))))
2277 (and (and-map id? #'(mod ...)) (id? #'id))
2278 ;; Strip the wrap from the identifier and return top-wrap
2279 ;; so that the identifier will not be captured by lexicals.
2280 (values (syntax->datum #'id) r top-wrap #f
2282 #'(private mod ...))))
2283 ((_ @@ (mod ...) exp)
2284 (and-map id? #'(mod ...))
2285 ;; This is a special syntax used to support R6RS library forms.
2286 ;; Unlike the syntax above, the last item is not restricted to
2287 ;; be a single identifier, and the syntax objects are kept
2288 ;; intact, with only their module changed.
2289 (let ((mod (syntax->datum #'(private mod ...))))
2290 (values (remodulate #'exp mod)
2291 r w (source-annotation #'exp)
2294 (global-extend 'core 'if
2295 (lambda (e r w s mod)
2300 (expand #'test r w mod)
2301 (expand #'then r w mod)
2302 (build-void no-source)))
2306 (expand #'test r w mod)
2307 (expand #'then r w mod)
2308 (expand #'else r w mod))))))
2310 (global-extend 'core 'with-fluids
2311 (lambda (e r w s mod)
2313 ((_ ((fluid val) ...) b b* ...)
2316 (map (lambda (x) (expand x r w mod)) #'(fluid ...))
2317 (map (lambda (x) (expand x r w mod)) #'(val ...))
2318 (expand-body #'(b b* ...)
2319 (source-wrap e w s mod) r w mod))))))
2321 (global-extend 'begin 'begin '())
2323 (global-extend 'define 'define '())
2325 (global-extend 'define-syntax 'define-syntax '())
2326 (global-extend 'define-syntax-parameter 'define-syntax-parameter '())
2328 (global-extend 'eval-when 'eval-when '())
2330 (global-extend 'core 'syntax-case
2332 (define convert-pattern
2333 ;; accepts pattern & keys
2334 ;; returns $sc-dispatch pattern & ids
2335 (lambda (pattern keys)
2338 (if (not (pair? p*))
2341 (lambda () (cvt* (cdr p*) n ids))
2344 (lambda () (cvt (car p*) n ids))
2346 (values (cons x y) ids))))))))
2348 (define (v-reverse x)
2349 (let loop ((r '()) (x x))
2352 (loop (cons (car x) r) (cdr x)))))
2358 ((bound-id-member? p keys)
2359 (values (vector 'free-id p) ids))
2363 (values 'any (cons (cons p n) ids))))
2366 (ellipsis? (syntax dots))
2368 (lambda () (cvt (syntax x) (fx+ n 1) ids))
2370 (values (if (eq? p 'any) 'each-any (vector 'each p))
2373 (ellipsis? (syntax dots))
2375 (lambda () (cvt* (syntax ys) n ids))
2378 (lambda () (cvt (syntax x) (+ n 1) ids))
2381 (lambda () (v-reverse ys))
2383 (values `#(each+ ,x ,ys ,e)
2387 (lambda () (cvt (syntax y) n ids))
2390 (lambda () (cvt (syntax x) n ids))
2392 (values (cons x y) ids))))))
2393 (() (values '() ids))
2396 (lambda () (cvt (syntax (x ...)) n ids))
2397 (lambda (p ids) (values (vector 'vector p) ids))))
2398 (x (values (vector 'atom (strip p empty-wrap)) ids))))))
2399 (cvt pattern 0 '())))
2401 (define build-dispatch-call
2402 (lambda (pvars exp y r mod)
2403 (let ((ids (map car pvars)) (levels (map cdr pvars)))
2404 (let ((labels (gen-labels ids)) (new-vars (map gen-var ids)))
2405 (build-application no-source
2406 (build-primref no-source 'apply)
2407 (list (build-simple-lambda no-source (map syntax->datum ids) #f new-vars '()
2411 (map (lambda (var level)
2412 (make-binding 'syntax `(,var . ,level)))
2416 (make-binding-wrap ids labels empty-wrap)
2421 (lambda (x keys clauses r pat fender exp mod)
2423 (lambda () (convert-pattern pat keys))
2426 ((not (distinct-bound-ids? (map car pvars)))
2427 (syntax-violation 'syntax-case "duplicate pattern variable" pat))
2428 ((not (and-map (lambda (x) (not (ellipsis? (car x)))) pvars))
2429 (syntax-violation 'syntax-case "misplaced ellipsis" pat))
2431 (let ((y (gen-var 'tmp)))
2432 ;; fat finger binding and references to temp variable y
2433 (build-application no-source
2434 (build-simple-lambda no-source (list 'tmp) #f (list y) '()
2435 (let ((y (build-lexical-reference 'value no-source
2437 (build-conditional no-source
2438 (syntax-case fender ()
2440 (_ (build-conditional no-source
2442 (build-dispatch-call pvars fender y r mod)
2443 (build-data no-source #f))))
2444 (build-dispatch-call pvars exp y r mod)
2445 (gen-syntax-case x keys clauses r mod))))
2446 (list (if (eq? p 'any)
2447 (build-application no-source
2448 (build-primref no-source 'list)
2450 (build-application no-source
2451 (build-primref no-source '$sc-dispatch)
2452 (list x (build-data no-source p)))))))))))))
2454 (define gen-syntax-case
2455 (lambda (x keys clauses r mod)
2457 (build-application no-source
2458 (build-primref no-source 'syntax-violation)
2459 (list (build-data no-source #f)
2460 (build-data no-source
2461 "source expression failed to match any pattern")
2463 (syntax-case (car clauses) ()
2465 (if (and (id? #'pat)
2466 (and-map (lambda (x) (not (free-id=? #'pat x)))
2467 (cons #'(... ...) keys)))
2468 (if (free-id=? #'pat #'_)
2469 (expand #'exp r empty-wrap mod)
2470 (let ((labels (list (gen-label)))
2471 (var (gen-var #'pat)))
2472 (build-application no-source
2473 (build-simple-lambda
2474 no-source (list (syntax->datum #'pat)) #f (list var)
2478 (list (make-binding 'syntax `(,var . 0)))
2480 (make-binding-wrap #'(pat)
2484 (gen-clause x keys (cdr clauses) r
2485 #'pat #t #'exp mod)))
2487 (gen-clause x keys (cdr clauses) r
2488 #'pat #'fender #'exp mod))
2489 (_ (syntax-violation 'syntax-case "invalid clause"
2492 (lambda (e r w s mod)
2493 (let ((e (source-wrap e w s mod)))
2495 ((_ val (key ...) m ...)
2496 (if (and-map (lambda (x) (and (id? x) (not (ellipsis? x))))
2498 (let ((x (gen-var 'tmp)))
2499 ;; fat finger binding and references to temp variable x
2500 (build-application s
2501 (build-simple-lambda no-source (list 'tmp) #f (list x) '()
2502 (gen-syntax-case (build-lexical-reference 'value no-source
2504 #'(key ...) #'(m ...)
2507 (list (expand #'val r empty-wrap mod))))
2508 (syntax-violation 'syntax-case "invalid literals list" e))))))))
2510 ;; The portable macroexpand seeds expand-top's mode m with 'e (for
2511 ;; evaluating) and esew (which stands for "eval syntax expanders
2512 ;; when") with '(eval). In Chez Scheme, m is set to 'c instead of e
2513 ;; if we are compiling a file, and esew is set to
2514 ;; (eval-syntactic-expanders-when), which defaults to the list
2515 ;; '(compile load eval). This means that, by default, top-level
2516 ;; syntactic definitions are evaluated immediately after they are
2517 ;; expanded, and the expanded definitions are also residualized into
2518 ;; the object file if we are compiling a file.
2520 (lambda* (x #:optional (m 'e) (esew '(eval)))
2521 (expand-top-sequence (list x) null-env top-wrap #f m esew
2522 (cons 'hygiene (module-name (current-module))))))
2530 (make-syntax-object datum (syntax-object-wrap id)
2531 (syntax-object-module id))))
2534 ;; accepts any object, since syntax objects may consist partially
2535 ;; or entirely of unwrapped, nonsymbolic data
2537 (strip x empty-wrap)))
2540 (lambda (x) (source-annotation x)))
2542 (set! generate-temporaries
2544 (arg-check list? ls 'generate-temporaries)
2545 (let ((mod (cons 'hygiene (module-name (current-module)))))
2546 (map (lambda (x) (wrap (gensym "t-") top-wrap mod)) ls))))
2548 (set! free-identifier=?
2550 (arg-check nonsymbol-id? x 'free-identifier=?)
2551 (arg-check nonsymbol-id? y 'free-identifier=?)
2554 (set! bound-identifier=?
2556 (arg-check nonsymbol-id? x 'bound-identifier=?)
2557 (arg-check nonsymbol-id? y 'bound-identifier=?)
2560 (set! syntax-violation
2561 (lambda* (who message form #:optional subform)
2562 (arg-check (lambda (x) (or (not x) (string? x) (symbol? x)))
2563 who 'syntax-violation)
2564 (arg-check string? message 'syntax-violation)
2565 (throw 'syntax-error who message
2566 (or (source-annotation subform)
2567 (source-annotation form))
2568 (strip form empty-wrap)
2569 (and subform (strip subform empty-wrap)))))
2572 (define (syntax-module id)
2573 (arg-check nonsymbol-id? id 'syntax-module)
2574 (cdr (syntax-object-module id)))
2576 (define (syntax-local-binding id)
2577 (arg-check nonsymbol-id? id 'syntax-local-binding)
2578 (with-transformer-environment
2579 (lambda (e r w s rib mod)
2580 (define (strip-anti-mark w)
2581 (let ((ms (wrap-marks w)) (s (wrap-subst w)))
2582 (if (and (pair? ms) (eq? (car ms) the-anti-mark))
2583 ;; output is from original text
2584 (make-wrap (cdr ms) (if rib (cons rib (cdr s)) (cdr s)))
2585 ;; output introduced by macro
2586 (make-wrap ms (if rib (cons rib s) s)))))
2587 (call-with-values (lambda ()
2589 (syntax-object-expression id)
2590 (strip-anti-mark (syntax-object-wrap id))
2592 (syntax-object-module id)))
2593 (lambda (type value mod)
2595 ((lexical) (values 'lexical value))
2596 ((macro) (values 'macro value))
2597 ((syntax) (values 'pattern-variable value))
2598 ((displaced-lexical) (values 'displaced-lexical #f))
2599 ((global) (values 'global (cons value (cdr mod))))
2600 (else (values 'other #f))))))))
2602 (define (syntax-locally-bound-identifiers id)
2603 (arg-check nonsymbol-id? id 'syntax-locally-bound-identifiers)
2604 (locally-bound-identifiers (syntax-object-wrap id)
2605 (syntax-object-module id)))
2607 ;; Using define! instead of set! to avoid warnings at
2608 ;; compile-time, after the variables are stolen away into (system
2609 ;; syntax). See the end of boot-9.scm.
2611 (define! 'syntax-module syntax-module)
2612 (define! 'syntax-local-binding syntax-local-binding)
2613 (define! 'syntax-locally-bound-identifiers syntax-locally-bound-identifiers))
2615 ;; $sc-dispatch expects an expression and a pattern. If the expression
2616 ;; matches the pattern a list of the matching expressions for each
2617 ;; "any" is returned. Otherwise, #f is returned. (This use of #f will
2618 ;; not work on r4rs implementations that violate the ieee requirement
2619 ;; that #f and () be distinct.)
2621 ;; The expression is matched with the pattern as follows:
2623 ;; pattern: matches:
2626 ;; (<pattern>1 . <pattern>2) (<pattern>1 . <pattern>2)
2628 ;; #(free-id <key>) <key> with free-identifier=?
2629 ;; #(each <pattern>) (<pattern>*)
2630 ;; #(each+ p1 (p2_1 ... p2_n) p3) (p1* (p2_n ... p2_1) . p3)
2631 ;; #(vector <pattern>) (list->vector <pattern>)
2632 ;; #(atom <object>) <object> with "equal?"
2634 ;; Vector cops out to pair under assumption that vectors are rare. If
2635 ;; not, should convert to:
2636 ;; #(vector <pattern>*) #(<pattern>*)
2644 (let ((first (match (car e) p w '() mod)))
2646 (let ((rest (match-each (cdr e) p w mod)))
2647 (and rest (cons first rest))))))
2650 (match-each (syntax-object-expression e)
2652 (join-wraps w (syntax-object-wrap e))
2653 (syntax-object-module e)))
2657 (lambda (e x-pat y-pat z-pat w r mod)
2658 (let f ((e e) (w w))
2661 (call-with-values (lambda () (f (cdr e) w))
2662 (lambda (xr* y-pat r)
2665 (let ((xr (match (car e) x-pat w '() mod)))
2667 (values (cons xr xr*) y-pat r)
2672 (match (car e) (car y-pat) w r mod)))
2673 (values #f #f #f)))))
2675 (f (syntax-object-expression e) (join-wraps w e)))
2677 (values '() y-pat (match e z-pat w r mod)))))))
2679 (define match-each-any
2683 (let ((l (match-each-any (cdr e) w mod)))
2684 (and l (cons (wrap (car e) w mod) l))))
2687 (match-each-any (syntax-object-expression e)
2688 (join-wraps w (syntax-object-wrap e))
2697 ((eq? p 'any) (cons '() r))
2698 ((pair? p) (match-empty (car p) (match-empty (cdr p) r)))
2699 ((eq? p 'each-any) (cons '() r))
2701 (case (vector-ref p 0)
2702 ((each) (match-empty (vector-ref p 1) r))
2703 ((each+) (match-empty (vector-ref p 1)
2705 (reverse (vector-ref p 2))
2706 (match-empty (vector-ref p 3) r))))
2708 ((vector) (match-empty (vector-ref p 1) r)))))))
2712 (if (null? (car r*))
2714 (cons (map car r*) (combine (map cdr r*) r)))))
2717 (lambda (e p w r mod)
2719 ((null? p) (and (null? e) r))
2721 (and (pair? e) (match (car e) (car p) w
2722 (match (cdr e) (cdr p) w r mod)
2725 (let ((l (match-each-any e w mod))) (and l (cons l r))))
2727 (case (vector-ref p 0)
2730 (match-empty (vector-ref p 1) r)
2731 (let ((l (match-each e (vector-ref p 1) w mod)))
2733 (let collect ((l l))
2736 (cons (map car l) (collect (map cdr l)))))))))
2740 (match-each+ e (vector-ref p 1) (vector-ref p 2) (vector-ref p 3) w r mod))
2741 (lambda (xr* y-pat r)
2745 (match-empty (vector-ref p 1) r)
2746 (combine xr* r))))))
2747 ((free-id) (and (id? e) (free-id=? (wrap e w mod) (vector-ref p 1)) r))
2748 ((atom) (and (equal? (vector-ref p 1) (strip e w)) r))
2751 (match (vector->list e) (vector-ref p 1) w r mod))))))))
2754 (lambda (e p w r mod)
2758 ((eq? p 'any) (cons (wrap e w mod) r))
2761 (syntax-object-expression e)
2763 (join-wraps w (syntax-object-wrap e))
2765 (syntax-object-module e)))
2766 (else (match* e p w r mod)))))
2771 ((eq? p 'any) (list e))
2774 (match* (syntax-object-expression e)
2775 p (syntax-object-wrap e) '() (syntax-object-module e)))
2776 (else (match* e p empty-wrap '() #f))))))))
2779 (define-syntax with-syntax
2783 #'(let () e1 e2 ...))
2784 ((_ ((out in)) e1 e2 ...)
2785 #'(syntax-case in ()
2786 (out (let () e1 e2 ...))))
2787 ((_ ((out in) ...) e1 e2 ...)
2788 #'(syntax-case (list in ...) ()
2789 ((out ...) (let () e1 e2 ...)))))))
2791 (define-syntax syntax-rules
2794 ((_ (k ...) ((keyword . pattern) template) ...)
2796 ;; embed patterns as procedure metadata
2797 #((macro-type . syntax-rules)
2798 (patterns pattern ...))
2799 (syntax-case x (k ...)
2800 ((dummy . pattern) #'template)
2802 ((_ (k ...) docstring ((keyword . pattern) template) ...)
2803 (string? (syntax->datum #'docstring))
2805 ;; the same, but allow a docstring
2807 #((macro-type . syntax-rules)
2808 (patterns pattern ...))
2809 (syntax-case x (k ...)
2810 ((dummy . pattern) #'template)
2813 (define-syntax define-syntax-rule
2816 ((_ (name . pattern) template)
2817 #'(define-syntax name
2819 ((_ . pattern) template))))
2820 ((_ (name . pattern) docstring template)
2821 (string? (syntax->datum #'docstring))
2822 #'(define-syntax name
2825 ((_ . pattern) template)))))))
2830 ((let* ((x v) ...) e1 e2 ...)
2831 (and-map identifier? #'(x ...))
2832 (let f ((bindings #'((x v) ...)))
2833 (if (null? bindings)
2834 #'(let () e1 e2 ...)
2835 (with-syntax ((body (f (cdr bindings)))
2836 (binding (car bindings)))
2837 #'(let (binding) body))))))))
2839 (define-syntax quasiquote
2841 (define (quasi p lev)
2842 (syntax-case p (unquote quasiquote)
2846 (quasicons #'("quote" unquote) (quasi #'(p) (- lev 1)))))
2847 ((quasiquote p) (quasicons #'("quote" quasiquote) (quasi #'(p) (+ lev 1))))
2849 (syntax-case #'p (unquote unquote-splicing)
2852 (quasilist* #'(("value" p) ...) (quasi #'q lev))
2854 (quasicons #'("quote" unquote) (quasi #'(p ...) (- lev 1)))
2856 ((unquote-splicing p ...)
2858 (quasiappend #'(("value" p) ...) (quasi #'q lev))
2860 (quasicons #'("quote" unquote-splicing) (quasi #'(p ...) (- lev 1)))
2862 (_ (quasicons (quasi #'p lev) (quasi #'q lev)))))
2863 (#(x ...) (quasivector (vquasi #'(x ...) lev)))
2865 (define (vquasi p lev)
2868 (syntax-case #'p (unquote unquote-splicing)
2871 (quasilist* #'(("value" p) ...) (vquasi #'q lev))
2873 (quasicons #'("quote" unquote) (quasi #'(p ...) (- lev 1)))
2875 ((unquote-splicing p ...)
2877 (quasiappend #'(("value" p) ...) (vquasi #'q lev))
2880 #'("quote" unquote-splicing)
2881 (quasi #'(p ...) (- lev 1)))
2883 (_ (quasicons (quasi #'p lev) (vquasi #'q lev)))))
2884 (() #'("quote" ()))))
2885 (define (quasicons x y)
2886 (with-syntax ((x x) (y y))
2890 (("quote" dx) #'("quote" (dx . dy)))
2891 (_ (if (null? #'dy) #'("list" x) #'("list*" x y)))))
2892 (("list" . stuff) #'("list" x . stuff))
2893 (("list*" . stuff) #'("list*" x . stuff))
2894 (_ #'("list*" x y)))))
2895 (define (quasiappend x y)
2899 ((null? x) #'("quote" ()))
2900 ((null? (cdr x)) (car x))
2901 (else (with-syntax (((p ...) x)) #'("append" p ...)))))
2905 (else (with-syntax (((p ...) x) (y y)) #'("append" p ... y)))))))
2906 (define (quasilist* x y)
2910 (quasicons (car x) (f (cdr x))))))
2911 (define (quasivector x)
2913 (("quote" (x ...)) #'("quote" #(x ...)))
2915 (let f ((y x) (k (lambda (ls) #`("vector" #,@ls))))
2917 (("quote" (y ...)) (k #'(("quote" y) ...)))
2918 (("list" y ...) (k #'(y ...)))
2919 (("list*" y ... z) (f #'z (lambda (ls) (k (append #'(y ...) ls)))))
2920 (else #`("list->vector" #,x)))))))
2924 (("list" x ...) #`(list #,@(map emit #'(x ...))))
2925 ;; could emit list* for 3+ arguments if implementation supports
2928 (let f ((x* #'(x ...)))
2931 #`(cons #,(emit (car x*)) #,(f (cdr x*))))))
2932 (("append" x ...) #`(append #,@(map emit #'(x ...))))
2933 (("vector" x ...) #`(vector #,@(map emit #'(x ...))))
2934 (("list->vector" x) #`(list->vector #,(emit #'x)))
2938 ;; convert to intermediate language, combining introduced (but
2939 ;; not unquoted source) quote expressions where possible and
2940 ;; choosing optimal construction code otherwise, then emit
2941 ;; Scheme code corresponding to the intermediate language forms.
2942 ((_ e) (emit (quasi #'e 0)))))))
2944 (define-syntax include
2948 (let* ((p (open-input-file
2949 (if (absolute-file-name? fn)
2951 (in-vicinity dir fn))))
2952 (enc (file-encoding p)))
2954 ;; Choose the input encoding deterministically.
2955 (set-port-encoding! p (or enc "UTF-8"))
2957 (let f ((x (read p))
2961 (close-input-port p)
2964 (cons (datum->syntax k x) result)))))))
2965 (let* ((src (syntax-source x))
2966 (file (and src (assq-ref src 'filename)))
2967 (dir (and (string? file) (dirname file))))
2970 (let ((fn (syntax->datum #'filename)))
2971 (with-syntax (((exp ...) (read-file fn dir #'filename)))
2972 #'(begin exp ...))))))))
2974 (define-syntax include-from-path
2978 (let ((fn (syntax->datum #'filename)))
2979 (with-syntax ((fn (datum->syntax
2981 (or (%search-load-path fn)
2982 (syntax-violation 'include-from-path
2983 "file not found in path"
2985 #'(include fn)))))))
2987 (define-syntax unquote
2989 (syntax-violation 'unquote
2990 "expression not valid outside of quasiquote"
2993 (define-syntax unquote-splicing
2995 (syntax-violation 'unquote-splicing
2996 "expression not valid outside of quasiquote"
2999 (define (make-variable-transformer proc)
3000 (if (procedure? proc)
3001 (let ((trans (lambda (x)
3002 #((macro-type . variable-transformer))
3004 (set-procedure-property! trans 'variable-transformer #t)
3006 (error "variable transformer not a procedure" proc)))
3008 (define-syntax identifier-syntax
3010 (syntax-case xx (set!)
3013 #((macro-type . identifier-syntax))
3019 #'(e x (... ...))))))
3020 ((_ (id exp1) ((set! var val) exp2))
3021 (and (identifier? #'id) (identifier? #'var))
3022 #'(make-variable-transformer
3024 #((macro-type . variable-transformer))
3025 (syntax-case x (set!)
3026 ((set! var val) #'exp2)
3027 ((id x (... ...)) #'(exp1 x (... ...)))
3028 (id (identifier? #'id) #'exp1))))))))
3030 (define-syntax define*
3033 ((_ (id . args) b0 b1 ...)
3034 #'(define id (lambda* args b0 b1 ...)))
3035 ((_ id val) (identifier? #'id)
3036 #'(define id val)))))