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 (and (not (equal? module '(primitive)))
293 (let ((v (module-variable (if module
294 (resolve-module (cdr module))
297 (and v (variable-bound? v)
298 (let ((val (variable-ref v)))
299 (and (macro? val) (macro-type val)
300 (cons (macro-type val)
301 (macro-binding val))))))))))
304 (define (decorate-source e s)
305 (if (and s (supports-source-properties? e))
306 (set-source-properties! e s))
309 (define (maybe-name-value! name val)
311 (let ((meta (lambda-meta val)))
312 (if (not (assq 'name meta))
313 (set-lambda-meta! val (acons 'name name meta))))))
315 ;; output constructors
321 (lambda (source fun-exp arg-exps)
322 (make-call source fun-exp arg-exps)))
324 (define build-conditional
325 (lambda (source test-exp then-exp else-exp)
326 (make-conditional source test-exp then-exp else-exp)))
329 (lambda (source fluids vals body)
330 (make-dynlet source fluids vals body)))
332 (define build-lexical-reference
333 (lambda (type source name var)
334 (make-lexical-ref source name var)))
336 (define build-lexical-assignment
337 (lambda (source name var exp)
338 (maybe-name-value! name exp)
339 (make-lexical-set source name var exp)))
341 (define (analyze-variable mod var modref-cont bare-cont)
344 (let ((kind (car mod))
347 ((public) (modref-cont mod var #t))
348 ((private) (if (not (equal? mod (module-name (current-module))))
349 (modref-cont mod var #f)
351 ((bare) (bare-cont var))
352 ((hygiene) (if (and (not (equal? mod (module-name (current-module))))
353 (module-variable (resolve-module mod) var))
354 (modref-cont mod var #f)
357 (syntax-violation #f "primitive not in operator position" var))
358 (else (syntax-violation #f "bad module kind" var mod))))))
360 (define build-global-reference
361 (lambda (source var mod)
364 (lambda (mod var public?)
365 (make-module-ref source mod var public?))
367 (make-toplevel-ref source var)))))
369 (define build-global-assignment
370 (lambda (source var exp mod)
371 (maybe-name-value! var exp)
374 (lambda (mod var public?)
375 (make-module-set source mod var public? exp))
377 (make-toplevel-set source var exp)))))
379 (define build-global-definition
380 (lambda (source var exp)
381 (maybe-name-value! var exp)
382 (make-toplevel-define source var exp)))
384 (define build-simple-lambda
385 (lambda (src req rest vars meta exp)
388 ;; hah, a case in which kwargs would be nice.
390 ;; src req opt rest kw inits vars body else
391 src req #f rest #f '() vars exp #f))))
393 (define build-case-lambda
394 (lambda (src meta body)
395 (make-lambda src meta body)))
397 (define build-lambda-case
399 ;; opt := (name ...) | #f
401 ;; kw := (allow-other-keys? (keyword name var) ...) | #f
404 ;; vars map to named arguments in the following order:
405 ;; required, optional (positional), rest, keyword.
406 ;; the body of a lambda: anything, already expanded
407 ;; else: lambda-case | #f
408 (lambda (src req opt rest kw inits vars body else-case)
409 (make-lambda-case src req opt rest kw inits vars body else-case)))
411 (define build-primcall
412 (lambda (src name args)
413 (make-primcall src name args)))
415 (define build-primref
417 (make-primitive-ref src name)))
419 (define (build-data src exp)
420 (make-const src exp))
422 (define build-sequence
424 (if (null? (cdr exps))
426 (make-seq src (car exps) (build-sequence #f (cdr exps))))))
429 (lambda (src ids vars val-exps body-exp)
430 (for-each maybe-name-value! ids val-exps)
433 (make-let src ids vars val-exps body-exp))))
435 (define build-named-let
436 (lambda (src ids vars val-exps body-exp)
441 (let ((proc (build-simple-lambda src ids #f vars '() body-exp)))
442 (maybe-name-value! f-name proc)
443 (for-each maybe-name-value! ids val-exps)
446 (list f-name) (list f) (list proc)
447 (build-call src (build-lexical-reference 'fun src f-name f)
451 (lambda (src in-order? ids vars val-exps body-exp)
455 (for-each maybe-name-value! ids val-exps)
456 (make-letrec src in-order? ids vars val-exps body-exp)))))
459 ;; FIXME: use a faster gensym
460 (define-syntax-rule (build-lexical-var src id)
461 (gensym (string-append (symbol->string id) "-")))
463 (define-structure (syntax-object expression wrap module))
465 (define-syntax no-source (identifier-syntax #f))
467 (define source-annotation
469 (let ((props (source-properties
470 (if (syntax-object? x)
471 (syntax-object-expression x)
473 (and (pair? props) props))))
475 (define-syntax-rule (arg-check pred? e who)
477 (if (not (pred? x)) (syntax-violation who "invalid argument" x))))
479 ;; compile-time environments
481 ;; wrap and environment comprise two level mapping.
482 ;; wrap : id --> label
483 ;; env : label --> <element>
485 ;; environments are represented in two parts: a lexical part and a global
486 ;; part. The lexical part is a simple list of associations from labels
487 ;; to bindings. The global part is implemented by
488 ;; {put,get}-global-definition-hook and associates symbols with
491 ;; global (assumed global variable) and displaced-lexical (see below)
492 ;; do not show up in any environment; instead, they are fabricated by
493 ;; resolve-identifier when it finds no other bindings.
495 ;; <environment> ::= ((<label> . <binding>)*)
497 ;; identifier bindings include a type and a value
499 ;; <binding> ::= (macro . <procedure>) macros
500 ;; (syntax-parameter . (<procedure>)) syntax parameters
501 ;; (core . <procedure>) core forms
502 ;; (module-ref . <procedure>) @ or @@
505 ;; (define-syntax) define-syntax
506 ;; (define-syntax-parameter) define-syntax-parameter
507 ;; (local-syntax . rec?) let-syntax/letrec-syntax
508 ;; (eval-when) eval-when
509 ;; (syntax . (<var> . <level>)) pattern variables
510 ;; (global) assumed global variable
511 ;; (lexical . <var>) lexical variables
512 ;; (displaced-lexical) displaced lexicals
513 ;; <level> ::= <nonnegative integer>
514 ;; <var> ::= variable returned by build-lexical-var
516 ;; a macro is a user-defined syntactic-form. a core is a
517 ;; system-defined syntactic form. begin, define, define-syntax,
518 ;; define-syntax-parameter, and eval-when are treated specially
519 ;; since they are sensitive to whether the form is at top-level and
520 ;; (except for eval-when) can denote valid internal definitions.
522 ;; a pattern variable is a variable introduced by syntax-case and can
523 ;; be referenced only within a syntax form.
525 ;; any identifier for which no top-level syntax definition or local
526 ;; binding of any kind has been seen is assumed to be a global
529 ;; a lexical variable is a lambda- or letrec-bound variable.
531 ;; a displaced-lexical identifier is a lexical identifier removed from
532 ;; it's scope by the return of a syntax object containing the identifier.
533 ;; a displaced lexical can also appear when a letrec-syntax-bound
534 ;; keyword is referenced on the rhs of one of the letrec-syntax clauses.
535 ;; a displaced lexical should never occur with properly written macros.
537 (define-syntax make-binding
538 (syntax-rules (quote)
539 ((_ type value) (cons type value))
541 ((_ type) (cons type '()))))
542 (define-syntax-rule (binding-type x)
544 (define-syntax-rule (binding-value x)
547 (define-syntax null-env (identifier-syntax '()))
550 (lambda (labels bindings r)
553 (extend-env (cdr labels) (cdr bindings)
554 (cons (cons (car labels) (car bindings)) r)))))
556 (define extend-var-env
557 ;; variant of extend-env that forms "lexical" binding
558 (lambda (labels vars r)
561 (extend-var-env (cdr labels) (cdr vars)
562 (cons (cons (car labels) (make-binding 'lexical (car vars))) r)))))
564 ;; we use a "macros only" environment in expansion of local macro
565 ;; definitions so that their definitions can use local macros without
566 ;; attempting to use other lexical identifiers.
567 (define macros-only-env
572 (if (memq (cadr a) '(macro syntax-parameter))
573 (cons a (macros-only-env (cdr r)))
574 (macros-only-env (cdr r)))))))
576 (define global-extend
577 (lambda (type sym val)
578 (put-global-definition-hook sym type val)))
581 ;; Conceptually, identifiers are always syntax objects. Internally,
582 ;; however, the wrap is sometimes maintained separately (a source of
583 ;; efficiency and confusion), so that symbols are also considered
584 ;; identifiers by id?. Externally, they are always wrapped.
586 (define nonsymbol-id?
588 (and (syntax-object? x)
589 (symbol? (syntax-object-expression x)))))
595 ((syntax-object? x) (symbol? (syntax-object-expression x)))
598 (define-syntax-rule (id-sym-name e)
600 (if (syntax-object? x)
601 (syntax-object-expression x)
604 (define id-sym-name&marks
606 (if (syntax-object? x)
608 (syntax-object-expression x)
609 (join-marks (wrap-marks w) (wrap-marks (syntax-object-wrap x))))
610 (values x (wrap-marks w)))))
612 ;; syntax object wraps
614 ;; <wrap> ::= ((<mark> ...) . (<subst> ...))
615 ;; <subst> ::= shift | <subs>
616 ;; <subs> ::= #(ribcage #(<sym> ...) #(<mark> ...) #(<label> ...))
617 ;; | #(ribcage (<sym> ...) (<mark> ...) (<label> ...))
619 (define-syntax make-wrap (identifier-syntax cons))
620 (define-syntax wrap-marks (identifier-syntax car))
621 (define-syntax wrap-subst (identifier-syntax cdr))
623 ;; labels must be comparable with "eq?", have read-write invariance,
624 ;; and distinct from symbols.
626 (string-append "l-" (session-id) (symbol->string (gensym "-"))))
632 (cons (gen-label) (gen-labels (cdr ls))))))
634 (define-structure (ribcage symnames marks labels))
636 (define-syntax empty-wrap (identifier-syntax '(())))
638 (define-syntax top-wrap (identifier-syntax '((top))))
640 (define-syntax-rule (top-marked? w)
641 (memq 'top (wrap-marks w)))
643 ;; Marks must be comparable with "eq?" and distinct from pairs and
644 ;; the symbol top. We do not use integers so that marks will remain
645 ;; unique even across file compiles.
647 (define-syntax the-anti-mark (identifier-syntax #f))
651 (make-wrap (cons the-anti-mark (wrap-marks w))
652 (cons 'shift (wrap-subst w)))))
654 (define-syntax-rule (new-mark)
655 (gensym (string-append "m-" (session-id) "-")))
657 ;; make-empty-ribcage and extend-ribcage maintain list-based ribcages for
658 ;; internal definitions, in which the ribcages are built incrementally
659 (define-syntax-rule (make-empty-ribcage)
660 (make-ribcage '() '() '()))
662 (define extend-ribcage!
663 ;; must receive ids with complete wraps
664 (lambda (ribcage id label)
665 (set-ribcage-symnames! ribcage
666 (cons (syntax-object-expression id)
667 (ribcage-symnames ribcage)))
668 (set-ribcage-marks! ribcage
669 (cons (wrap-marks (syntax-object-wrap id))
670 (ribcage-marks ribcage)))
671 (set-ribcage-labels! ribcage
672 (cons label (ribcage-labels ribcage)))))
674 ;; make-binding-wrap creates vector-based ribcages
675 (define make-binding-wrap
676 (lambda (ids labels w)
682 (let ((labelvec (list->vector labels)))
683 (let ((n (vector-length labelvec)))
684 (let ((symnamevec (make-vector n)) (marksvec (make-vector n)))
685 (let f ((ids ids) (i 0))
686 (if (not (null? ids))
688 (lambda () (id-sym-name&marks (car ids) w))
689 (lambda (symname marks)
690 (vector-set! symnamevec i symname)
691 (vector-set! marksvec i marks)
692 (f (cdr ids) (fx+ i 1))))))
693 (make-ribcage symnamevec marksvec labelvec))))
704 (let ((m1 (wrap-marks w1)) (s1 (wrap-subst w1)))
710 (smart-append s1 (wrap-subst w2))))
712 (smart-append m1 (wrap-marks w2))
713 (smart-append s1 (wrap-subst w2)))))))
717 (smart-append m1 m2)))
724 (eq? (car x) (car y))
725 (same-marks? (cdr x) (cdr y))))))
728 ;; Syntax objects use wraps to associate names with marked
729 ;; identifiers. This function returns the name corresponding to
730 ;; the given identifier and wrap, or the original identifier if no
731 ;; corresponding name was found.
733 ;; The name may be a string created by gen-label, indicating a
734 ;; lexical binding, or another syntax object, indicating a
735 ;; reference to a top-level definition created during a previous
738 ;; For lexical variables, finding a label simply amounts to
739 ;; looking for an entry with the same symbolic name and the same
740 ;; marks. Finding a toplevel definition is the same, except we
741 ;; also have to compare modules, hence the `mod' parameter.
742 ;; Instead of adding a separate entry in the ribcage for modules,
743 ;; which wouldn't be used for lexicals, we arrange for the entry
744 ;; for the name entry to be a pair with the module in its car, and
745 ;; the name itself in the cdr. So if the name that we find is a
746 ;; pair, we have to check modules.
748 ;; The identifer may be passed in wrapped or unwrapped. In any
749 ;; case, this routine returns either a symbol, a syntax object, or
753 (define-syntax-rule (first e)
754 ;; Rely on Guile's multiple-values truncation.
757 (lambda (sym subst marks mod)
760 (let ((fst (car subst)))
762 (search sym (cdr subst) (cdr marks) mod)
763 (let ((symnames (ribcage-symnames fst)))
764 (if (vector? symnames)
765 (search-vector-rib sym subst marks symnames fst mod)
766 (search-list-rib sym subst marks symnames fst mod))))))))
767 (define search-list-rib
768 (lambda (sym subst marks symnames ribcage mod)
769 (let f ((symnames symnames) (i 0))
771 ((null? symnames) (search sym (cdr subst) marks mod))
772 ((and (eq? (car symnames) sym)
773 (same-marks? marks (list-ref (ribcage-marks ribcage) i)))
774 (let ((n (list-ref (ribcage-labels ribcage) i)))
776 (if (equal? mod (car n))
777 (values (cdr n) marks)
778 (f (cdr symnames) (fx+ i 1)))
780 (else (f (cdr symnames) (fx+ i 1)))))))
781 (define search-vector-rib
782 (lambda (sym subst marks symnames ribcage mod)
783 (let ((n (vector-length symnames)))
786 ((fx= i n) (search sym (cdr subst) marks mod))
787 ((and (eq? (vector-ref symnames i) sym)
788 (same-marks? marks (vector-ref (ribcage-marks ribcage) i)))
789 (let ((n (vector-ref (ribcage-labels ribcage) i)))
791 (if (equal? mod (car n))
792 (values (cdr n) marks)
795 (else (f (fx+ i 1))))))))
798 (or (first (search id (wrap-subst w) (wrap-marks w) mod)) id))
800 (let ((id (syntax-object-expression id))
801 (w1 (syntax-object-wrap id))
802 (mod (syntax-object-module id)))
803 (let ((marks (join-marks (wrap-marks w) (wrap-marks w1))))
804 (call-with-values (lambda () (search id (wrap-subst w) marks mod))
805 (lambda (new-id marks)
807 (first (search id (wrap-subst w1) marks mod))
809 (else (syntax-violation 'id-var-name "invalid id" id)))))
811 ;; A helper procedure for syntax-locally-bound-identifiers, which
812 ;; itself is a helper for transformer procedures.
813 ;; `locally-bound-identifiers' returns a list of all bindings
814 ;; visible to a syntax object with the given wrap. They are in
815 ;; order from outer to inner.
817 ;; The purpose of this procedure is to give a transformer procedure
818 ;; references on bound identifiers, that the transformer can then
819 ;; introduce some of them in its output. As such, the identifiers
820 ;; are anti-marked, so that rebuild-macro-output doesn't apply new
823 (define locally-bound-identifiers
826 (lambda (subst results)
829 (let ((fst (car subst)))
831 (scan (cdr subst) results)
832 (let ((symnames (ribcage-symnames fst))
833 (marks (ribcage-marks fst)))
834 (if (vector? symnames)
835 (scan-vector-rib subst symnames marks results)
836 (scan-list-rib subst symnames marks results))))))))
837 (define scan-list-rib
838 (lambda (subst symnames marks results)
839 (let f ((symnames symnames) (marks marks) (results results))
841 (scan (cdr subst) results)
842 (f (cdr symnames) (cdr marks)
843 (cons (wrap (car symnames)
844 (anti-mark (make-wrap (car marks) subst))
847 (define scan-vector-rib
848 (lambda (subst symnames marks results)
849 (let ((n (vector-length symnames)))
850 (let f ((i 0) (results results))
852 (scan (cdr subst) results)
854 (cons (wrap (vector-ref symnames i)
855 (anti-mark (make-wrap (vector-ref marks i) subst))
858 (scan (wrap-subst w) '())))
860 ;; Returns three values: binding type, binding value, the module (for
861 ;; resolving toplevel vars).
862 (define (resolve-identifier id w r mod resolve-syntax-parameters?)
863 (define (resolve-syntax-parameters b)
864 (if (and resolve-syntax-parameters?
865 (eq? (binding-type b) 'syntax-parameter))
866 (or (assq-ref r (binding-value b))
867 (make-binding 'macro (car (binding-value b))))
869 (define (resolve-global var mod)
870 (let ((b (resolve-syntax-parameters
871 (or (get-global-definition-hook var mod)
872 (make-binding 'global)))))
873 (if (eq? (binding-type b) 'global)
874 (values 'global var mod)
875 (values (binding-type b) (binding-value b) mod))))
876 (define (resolve-lexical label mod)
877 (let ((b (resolve-syntax-parameters
878 (or (assq-ref r label)
879 (make-binding 'displaced-lexical)))))
880 (values (binding-type b) (binding-value b) mod)))
881 (let ((n (id-var-name id w mod)))
884 ;; Recursing allows syntax-parameterize to override
885 ;; macro-introduced syntax parameters.
886 (resolve-identifier n w r mod resolve-syntax-parameters?))
888 (resolve-global n (if (syntax-object? id)
889 (syntax-object-module id)
892 (resolve-lexical n (if (syntax-object? id)
893 (syntax-object-module id)
896 (error "unexpected id-var-name" id w n)))))
898 (define transformer-environment
901 (error "called outside the dynamic extent of a syntax transformer"))))
903 (define (with-transformer-environment k)
904 ((fluid-ref transformer-environment) k))
906 ;; free-id=? must be passed fully wrapped ids since (free-id=? x y)
907 ;; may be true even if (free-id=? (wrap x w) (wrap y w)) is not.
911 (let* ((mi (and (syntax-object? i) (syntax-object-module i)))
912 (mj (and (syntax-object? j) (syntax-object-module j)))
913 (ni (id-var-name i empty-wrap mi))
914 (nj (id-var-name j empty-wrap mj)))
915 (define (id-module-binding id mod)
919 (resolve-module (cdr mod))
920 ;; Either modules have not been booted, or we have a
921 ;; raw symbol coming in, which is possible.
925 ((syntax-object? ni) (free-id=? ni j))
926 ((syntax-object? nj) (free-id=? i nj))
928 ;; `i' is not lexically bound. Assert that `j' is free,
929 ;; and if so, compare their bindings, that they are either
930 ;; bound to the same variable, or both unbound and have
932 (and (eq? nj (id-sym-name j))
933 (let ((bi (id-module-binding i mi)))
935 (eq? bi (id-module-binding j mj))
936 (and (not (id-module-binding j mj))
938 (eq? (id-module-binding i mi) (id-module-binding j mj))))
940 ;; Otherwise `i' is bound, so check that `j' is bound, and
941 ;; bound to the same thing.
944 ;; bound-id=? may be passed unwrapped (or partially wrapped) ids as
945 ;; long as the missing portion of the wrap is common to both of the ids
946 ;; since (bound-id=? x y) iff (bound-id=? (wrap x w) (wrap y w))
950 (if (and (syntax-object? i) (syntax-object? j))
951 (and (eq? (syntax-object-expression i)
952 (syntax-object-expression j))
953 (same-marks? (wrap-marks (syntax-object-wrap i))
954 (wrap-marks (syntax-object-wrap j))))
957 ;; "valid-bound-ids?" returns #t if it receives a list of distinct ids.
958 ;; valid-bound-ids? may be passed unwrapped (or partially wrapped) ids
959 ;; as long as the missing portion of the wrap is common to all of the
962 (define valid-bound-ids?
964 (and (let all-ids? ((ids ids))
967 (all-ids? (cdr ids)))))
968 (distinct-bound-ids? ids))))
970 ;; distinct-bound-ids? expects a list of ids and returns #t if there are
971 ;; no duplicates. It is quadratic on the length of the id list; long
972 ;; lists could be sorted to make it more efficient. distinct-bound-ids?
973 ;; may be passed unwrapped (or partially wrapped) ids as long as the
974 ;; missing portion of the wrap is common to all of the ids.
976 (define distinct-bound-ids?
978 (let distinct? ((ids ids))
980 (and (not (bound-id-member? (car ids) (cdr ids)))
981 (distinct? (cdr ids)))))))
983 (define bound-id-member?
985 (and (not (null? list))
986 (or (bound-id=? x (car list))
987 (bound-id-member? x (cdr list))))))
989 ;; wrapping expressions and identifiers
994 ((and (null? (wrap-marks w)) (null? (wrap-subst w))) x)
997 (syntax-object-expression x)
998 (join-wraps w (syntax-object-wrap x))
999 (syntax-object-module x)))
1001 (else (make-syntax-object x w defmod)))))
1004 (lambda (x w s defmod)
1005 (wrap (decorate-source x s) w defmod)))
1009 (define expand-sequence
1010 (lambda (body r w s mod)
1012 (let dobody ((body body) (r r) (w w) (mod mod))
1015 (let ((first (expand (car body) r w mod)))
1016 (cons first (dobody (cdr body) r w mod))))))))
1018 ;; At top-level, we allow mixed definitions and expressions. Like
1019 ;; expand-body we expand in two passes.
1021 ;; First, from left to right, we expand just enough to know what
1022 ;; expressions are definitions, syntax definitions, and splicing
1023 ;; statements (`begin'). If we anything needs evaluating at
1024 ;; expansion-time, it is expanded directly.
1026 ;; Otherwise we collect expressions to expand, in thunks, and then
1027 ;; expand them all at the end. This allows all syntax expanders
1028 ;; visible in a toplevel sequence to be visible during the
1029 ;; expansions of all normal definitions and expressions in the
1032 (define expand-top-sequence
1033 (lambda (body r w s m esew mod)
1034 (let* ((r (cons '("placeholder" . (placeholder)) r))
1035 (ribcage (make-empty-ribcage))
1036 (w (make-wrap (wrap-marks w) (cons ribcage (wrap-subst w)))))
1037 (define (record-definition! id var)
1038 (let ((mod (cons 'hygiene (module-name (current-module)))))
1039 ;; Ribcages map symbol+marks to names, mostly for
1040 ;; resolving lexicals. Here to add a mapping for toplevel
1041 ;; definitions we also need to match the module. So, we
1042 ;; put it in the name instead, and make id-var-name handle
1043 ;; the special case of names that are pairs. See the
1044 ;; comments in id-var-name for more.
1045 (extend-ribcage! ribcage id
1046 (cons (syntax-object-module id)
1047 (wrap var top-wrap mod)))))
1048 (define (macro-introduced-identifier? id)
1049 (not (equal? (wrap-marks (syntax-object-wrap id)) '(top))))
1050 (define (fresh-derived-name id orig-form)
1052 (syntax-object-expression id)
1055 ;; FIXME: `hash' currently stops descending into nested
1056 ;; data at some point, so it's less unique than we would
1057 ;; like. Also this encodes hash values into the ABI of
1058 ;; compiled modules; a problem?
1060 (hash (syntax->datum orig-form) most-positive-fixnum)
1062 (define (parse body r w s m esew mod)
1063 (let lp ((body body) (exps '()))
1067 (append (parse1 (car body) r w s m esew mod)
1069 (define (parse1 x r w s m esew mod)
1072 (syntax-type x r w (source-annotation x) ribcage mod #f))
1073 (lambda (type value form e w s mod)
1076 (let* ((id (wrap value w mod))
1078 (var (if (macro-introduced-identifier? id)
1079 (fresh-derived-name id x)
1080 (syntax-object-expression id))))
1081 (record-definition! id var)
1084 (let ((x (build-global-definition s var (expand e r w mod))))
1085 (top-level-eval-hook x mod)
1088 (build-global-definition s var (expand e r w mod)))))))
1089 ((define-syntax-form define-syntax-parameter-form)
1090 (let* ((id (wrap value w mod))
1092 (var (if (macro-introduced-identifier? id)
1093 (fresh-derived-name id x)
1094 (syntax-object-expression id))))
1095 (record-definition! id var)
1099 ((memq 'compile esew)
1100 (let ((e (expand-install-global var type (expand e r w mod))))
1101 (top-level-eval-hook e mod)
1102 (if (memq 'load esew)
1103 (list (lambda () e))
1107 (expand-install-global var type (expand e r w mod)))))
1110 (let ((e (expand-install-global var type (expand e r w mod))))
1111 (top-level-eval-hook e mod)
1112 (list (lambda () e))))
1114 (if (memq 'eval esew)
1115 (top-level-eval-hook
1116 (expand-install-global var type (expand e r w mod))
1122 (parse #'(e1 ...) r w s m esew mod))))
1123 ((local-syntax-form)
1124 (expand-local-syntax value e r w s mod
1125 (lambda (forms r w s mod)
1126 (parse forms r w s m esew mod))))
1129 ((_ (x ...) e1 e2 ...)
1130 (let ((when-list (parse-when-list e #'(x ...)))
1131 (body #'(e1 e2 ...)))
1132 (define (recurse m esew)
1133 (parse body r w s m esew mod))
1136 (if (memq 'eval when-list)
1137 (recurse (if (memq 'expand when-list) 'c&e 'e)
1140 (if (memq 'expand when-list)
1141 (top-level-eval-hook
1142 (expand-top-sequence body r w s 'e '(eval) mod)
1145 ((memq 'load when-list)
1146 (if (or (memq 'compile when-list)
1147 (memq 'expand when-list)
1148 (and (eq? m 'c&e) (memq 'eval when-list)))
1149 (recurse 'c&e '(compile load))
1150 (if (memq m '(c c&e))
1151 (recurse 'c '(load))
1153 ((or (memq 'compile when-list)
1154 (memq 'expand when-list)
1155 (and (eq? m 'c&e) (memq 'eval when-list)))
1156 (top-level-eval-hook
1157 (expand-top-sequence body r w s 'e '(eval) mod)
1165 (let ((x (expand-expr type value form e r w s mod)))
1166 (top-level-eval-hook x mod)
1169 (expand-expr type value form e r w s mod)))))))))
1170 (let ((exps (map (lambda (x) (x))
1171 (reverse (parse body r w s m esew mod)))))
1174 (build-sequence s exps))))))
1176 (define expand-install-global
1177 (lambda (name type e)
1178 (build-global-definition
1183 'make-syntax-transformer
1184 (if (eq? type 'define-syntax-parameter-form)
1185 (list (build-data no-source name)
1186 (build-data no-source 'syntax-parameter)
1187 (build-primcall no-source 'list (list e)))
1188 (list (build-data no-source name)
1189 (build-data no-source 'macro)
1192 (define parse-when-list
1193 (lambda (e when-list)
1194 ;; `when-list' is syntax'd version of list of situations. We
1195 ;; could match these keywords lexically, via free-id=?, but then
1196 ;; we twingle the definition of eval-when to the bindings of
1197 ;; eval, load, expand, and compile, which is totally unintended.
1198 ;; So do a symbolic match instead.
1199 (let ((result (strip when-list empty-wrap)))
1200 (let lp ((l result))
1203 (if (memq (car l) '(compile load eval expand))
1205 (syntax-violation 'eval-when "invalid situation" e
1208 ;; syntax-type returns seven values: type, value, form, e, w, s, and
1209 ;; mod. The first two are described in the table below.
1211 ;; type value explanation
1212 ;; -------------------------------------------------------------------
1213 ;; core procedure core singleton
1214 ;; core-form procedure core form
1215 ;; module-ref procedure @ or @@ singleton
1216 ;; lexical name lexical variable reference
1217 ;; global name global variable reference
1218 ;; begin none begin keyword
1219 ;; define none define keyword
1220 ;; define-syntax none define-syntax keyword
1221 ;; define-syntax-parameter none define-syntax-parameter keyword
1222 ;; local-syntax rec? letrec-syntax/let-syntax keyword
1223 ;; eval-when none eval-when keyword
1224 ;; syntax level pattern variable
1225 ;; displaced-lexical none displaced lexical identifier
1226 ;; lexical-call name call to lexical variable
1227 ;; global-call name call to global variable
1228 ;; primitive-call name call to primitive
1229 ;; call none any other call
1230 ;; begin-form none begin expression
1231 ;; define-form id variable definition
1232 ;; define-syntax-form id syntax definition
1233 ;; define-syntax-parameter-form id syntax parameter definition
1234 ;; local-syntax-form rec? syntax definition
1235 ;; eval-when-form none eval-when form
1236 ;; constant none self-evaluating datum
1237 ;; other none anything else
1239 ;; form is the entire form. For definition forms (define-form,
1240 ;; define-syntax-form, and define-syntax-parameter-form), e is the
1241 ;; rhs expression. For all others, e is the entire form. w is the
1242 ;; wrap for both form and e. s is the source for the entire form.
1243 ;; mod is the module for both form and e.
1245 ;; syntax-type expands macros and unwraps as necessary to get to one
1246 ;; of the forms above. It also parses definition forms, although
1247 ;; perhaps this should be done by the consumer.
1250 (lambda (e r w s rib mod for-car?)
1253 (call-with-values (lambda () (resolve-identifier e w r mod #t))
1254 (lambda (type value mod*)
1258 (values type value e e w s mod)
1259 (syntax-type (expand-macro value e r w s rib mod)
1260 r empty-wrap s rib mod #f)))
1262 ;; Toplevel definitions may resolve to bindings with
1263 ;; different names or in different modules.
1264 (values type value e value w s mod*))
1265 (else (values type value e e w s mod))))))
1267 (let ((first (car e)))
1269 (lambda () (syntax-type first r w s rib mod #t))
1270 (lambda (ftype fval fform fe fw fs fmod)
1273 (values 'lexical-call fval e e w s mod))
1275 (if (equal? fmod '(primitive))
1276 (values 'primitive-call fval e e w s mod)
1277 ;; If we got here via an (@@ ...) expansion, we
1278 ;; need to make sure the fmod information is
1279 ;; propagated back correctly -- hence this
1281 (values 'global-call (make-syntax-object fval w fmod)
1284 (syntax-type (expand-macro fval e r w s rib mod)
1285 r empty-wrap s rib mod for-car?))
1287 (call-with-values (lambda () (fval e r w mod))
1288 (lambda (e r w s mod)
1289 (syntax-type e r w s rib mod for-car?))))
1291 (values 'core-form fval e e w s mod))
1293 (values 'local-syntax-form fval e e w s mod))
1295 (values 'begin-form #f e e w s mod))
1297 (values 'eval-when-form #f e e w s mod))
1302 (values 'define-form #'name e #'val w s mod))
1303 ((_ (name . args) e1 e2 ...)
1305 (valid-bound-ids? (lambda-var-list #'args)))
1306 ;; need lambda here...
1307 (values 'define-form (wrap #'name w mod)
1310 (cons #'lambda (wrap #'(args e1 e2 ...) w mod))
1315 (values 'define-form (wrap #'name w mod)
1318 empty-wrap s mod))))
1323 (values 'define-syntax-form #'name e #'val w s mod))))
1324 ((define-syntax-parameter)
1328 (values 'define-syntax-parameter-form #'name e #'val w s mod))))
1330 (values 'call #f e e w s mod)))))))
1332 (syntax-type (syntax-object-expression e)
1334 (join-wraps w (syntax-object-wrap e))
1335 (or (source-annotation e) s) rib
1336 (or (syntax-object-module e) mod) for-car?))
1337 ((self-evaluating? e) (values 'constant #f e e w s mod))
1338 (else (values 'other #f e e w s mod)))))
1343 (lambda () (syntax-type e r w (source-annotation e) #f mod #f))
1344 (lambda (type value form e w s mod)
1345 (expand-expr type value form e r w s mod)))))
1348 (lambda (type value form e r w s mod)
1351 (build-lexical-reference 'value s e value))
1353 ;; apply transformer
1354 (value e r w s mod))
1356 (call-with-values (lambda () (value e r w mod))
1357 (lambda (e r w s mod)
1358 (expand e r w mod))))
1362 (build-lexical-reference 'fun (source-annotation id)
1363 (if (syntax-object? id)
1370 (build-global-reference (source-annotation (car e))
1371 (if (syntax-object? value)
1372 (syntax-object-expression value)
1374 (if (syntax-object? value)
1375 (syntax-object-module value)
1383 (map (lambda (e) (expand e r w mod))
1385 ((constant) (build-data s (strip (source-wrap e w s mod) empty-wrap)))
1386 ((global) (build-global-reference s value mod))
1387 ((call) (expand-call (expand (car e) r w mod) e r w s mod))
1390 ((_ e1 e2 ...) (expand-sequence #'(e1 e2 ...) r w s mod))
1392 (syntax-violation #f "sequence of zero expressions"
1393 (source-wrap e w s mod)))))
1394 ((local-syntax-form)
1395 (expand-local-syntax value e r w s mod expand-sequence))
1398 ((_ (x ...) e1 e2 ...)
1399 (let ((when-list (parse-when-list e #'(x ...))))
1400 (if (memq 'eval when-list)
1401 (expand-sequence #'(e1 e2 ...) r w s mod)
1403 ((define-form define-syntax-form define-syntax-parameter-form)
1404 (syntax-violation #f "definition in expression context, where definitions are not allowed,"
1405 (source-wrap form w s mod)))
1407 (syntax-violation #f "reference to pattern variable outside syntax form"
1408 (source-wrap e w s mod)))
1409 ((displaced-lexical)
1410 (syntax-violation #f "reference to identifier outside its scope"
1411 (source-wrap e w s mod)))
1412 (else (syntax-violation #f "unexpected syntax"
1413 (source-wrap e w s mod))))))
1416 (lambda (x e r w s mod)
1420 (map (lambda (e) (expand e r w mod)) #'(e1 ...)))))))
1422 ;; (What follows is my interpretation of what's going on here -- Andy)
1424 ;; A macro takes an expression, a tree, the leaves of which are identifiers
1425 ;; and datums. Identifiers are symbols along with a wrap and a module. For
1426 ;; efficiency, subtrees that share wraps and modules may be grouped as one
1429 ;; Going into the expansion, the expression is given an anti-mark, which
1430 ;; logically propagates to all leaves. Then, in the new expression returned
1431 ;; from the transfomer, if we see an expression with an anti-mark, we know it
1432 ;; pertains to the original expression; conversely, expressions without the
1433 ;; anti-mark are known to be introduced by the transformer.
1435 ;; OK, good until now. We know this algorithm does lexical scoping
1436 ;; appropriately because it's widely known in the literature, and psyntax is
1437 ;; widely used. But what about modules? Here we're on our own. What we do is
1438 ;; to mark the module of expressions produced by a macro as pertaining to the
1439 ;; module that was current when the macro was defined -- that is, free
1440 ;; identifiers introduced by a macro are scoped in the macro's module, not in
1441 ;; the expansion's module. Seems to work well.
1443 ;; The only wrinkle is when we want a macro to expand to code in another
1444 ;; module, as is the case for the r6rs `library' form -- the body expressions
1445 ;; should be scoped relative the the new module, the one defined by the macro.
1446 ;; For that, use `(@@ mod-name body)'.
1448 ;; Part of the macro output will be from the site of the macro use and part
1449 ;; from the macro definition. We allow source information from the macro use
1450 ;; to pass through, but we annotate the parts coming from the macro with the
1451 ;; source location information corresponding to the macro use. It would be
1452 ;; really nice if we could also annotate introduced expressions with the
1453 ;; locations corresponding to the macro definition, but that is not yet
1455 (define expand-macro
1456 (lambda (p e r w s rib mod)
1457 (define rebuild-macro-output
1461 (cons (rebuild-macro-output (car x) m)
1462 (rebuild-macro-output (cdr x) m))
1465 (let ((w (syntax-object-wrap x)))
1466 (let ((ms (wrap-marks w)) (ss (wrap-subst w)))
1467 (if (and (pair? ms) (eq? (car ms) the-anti-mark))
1468 ;; output is from original text
1470 (syntax-object-expression x)
1471 (make-wrap (cdr ms) (if rib (cons rib (cdr ss)) (cdr ss)))
1472 (syntax-object-module x))
1473 ;; output introduced by macro
1475 (decorate-source (syntax-object-expression x) s)
1476 (make-wrap (cons m ms)
1478 (cons rib (cons 'shift ss))
1480 (syntax-object-module x))))))
1483 (let* ((n (vector-length x))
1484 (v (decorate-source (make-vector n) s)))
1485 (do ((i 0 (fx+ i 1)))
1488 (rebuild-macro-output (vector-ref x i) m)))))
1490 (syntax-violation #f "encountered raw symbol in macro output"
1491 (source-wrap e w (wrap-subst w) mod) x))
1492 (else (decorate-source x s)))))
1493 (with-fluids ((transformer-environment
1494 (lambda (k) (k e r w s rib mod))))
1495 (rebuild-macro-output (p (source-wrap e (anti-mark w) s mod))
1499 ;; In processing the forms of the body, we create a new, empty wrap.
1500 ;; This wrap is augmented (destructively) each time we discover that
1501 ;; the next form is a definition. This is done:
1503 ;; (1) to allow the first nondefinition form to be a call to
1504 ;; one of the defined ids even if the id previously denoted a
1505 ;; definition keyword or keyword for a macro expanding into a
1507 ;; (2) to prevent subsequent definition forms (but unfortunately
1508 ;; not earlier ones) and the first nondefinition form from
1509 ;; confusing one of the bound identifiers for an auxiliary
1511 ;; (3) so that we do not need to restart the expansion of the
1512 ;; first nondefinition form, which is problematic anyway
1513 ;; since it might be the first element of a begin that we
1514 ;; have just spliced into the body (meaning if we restarted,
1515 ;; we'd really need to restart with the begin or the macro
1516 ;; call that expanded into the begin, and we'd have to give
1517 ;; up allowing (begin <defn>+ <expr>+), which is itself
1518 ;; problematic since we don't know if a begin contains only
1519 ;; definitions until we've expanded it).
1521 ;; Before processing the body, we also create a new environment
1522 ;; containing a placeholder for the bindings we will add later and
1523 ;; associate this environment with each form. In processing a
1524 ;; let-syntax or letrec-syntax, the associated environment may be
1525 ;; augmented with local keyword bindings, so the environment may
1526 ;; be different for different forms in the body. Once we have
1527 ;; gathered up all of the definitions, we evaluate the transformer
1528 ;; expressions and splice into r at the placeholder the new variable
1529 ;; and keyword bindings. This allows let-syntax or letrec-syntax
1530 ;; forms local to a portion or all of the body to shadow the
1531 ;; definition bindings.
1533 ;; Subforms of a begin, let-syntax, or letrec-syntax are spliced
1536 ;; outer-form is fully wrapped w/source
1537 (lambda (body outer-form r w mod)
1538 (let* ((r (cons '("placeholder" . (placeholder)) r))
1539 (ribcage (make-empty-ribcage))
1540 (w (make-wrap (wrap-marks w) (cons ribcage (wrap-subst w)))))
1541 (let parse ((body (map (lambda (x) (cons r (wrap x w mod))) body))
1542 (ids '()) (labels '())
1543 (var-ids '()) (vars '()) (vals '()) (bindings '()))
1545 (syntax-violation #f "no expressions in body" outer-form)
1546 (let ((e (cdar body)) (er (caar body)))
1548 (lambda () (syntax-type e er empty-wrap (source-annotation e) ribcage mod #f))
1549 (lambda (type value form e w s mod)
1552 (let ((id (wrap value w mod)) (label (gen-label)))
1553 (let ((var (gen-var id)))
1554 (extend-ribcage! ribcage id label)
1556 (cons id ids) (cons label labels)
1558 (cons var vars) (cons (cons er (wrap e w mod)) vals)
1559 (cons (make-binding 'lexical var) bindings)))))
1560 ((define-syntax-form)
1561 (let ((id (wrap value w mod))
1563 (trans-r (macros-only-env er)))
1564 (extend-ribcage! ribcage id label)
1565 ;; As required by R6RS, evaluate the right-hand-sides of internal
1566 ;; syntax definition forms and add their transformers to the
1567 ;; compile-time environment immediately, so that the newly-defined
1568 ;; keywords may be used in definition context within the same
1570 (set-cdr! r (extend-env
1574 (eval-local-transformer
1575 (expand e trans-r w mod)
1578 (parse (cdr body) (cons id ids) labels var-ids vars vals bindings)))
1579 ((define-syntax-parameter-form)
1580 ;; Same as define-syntax-form, but different format of the binding.
1581 (let ((id (wrap value w mod))
1583 (trans-r (macros-only-env er)))
1584 (extend-ribcage! ribcage id label)
1585 (set-cdr! r (extend-env
1589 (list (eval-local-transformer
1590 (expand e trans-r w mod)
1593 (parse (cdr body) (cons id ids) labels var-ids vars vals bindings)))
1597 (parse (let f ((forms #'(e1 ...)))
1600 (cons (cons er (wrap (car forms) w mod))
1602 ids labels var-ids vars vals bindings))))
1603 ((local-syntax-form)
1604 (expand-local-syntax value e er w s mod
1605 (lambda (forms er w s mod)
1606 (parse (let f ((forms forms))
1609 (cons (cons er (wrap (car forms) w mod))
1611 ids labels var-ids vars vals bindings))))
1612 (else ; found a non-definition
1614 (build-sequence no-source
1616 (expand (cdr x) (car x) empty-wrap mod))
1617 (cons (cons er (source-wrap e w s mod))
1620 (if (not (valid-bound-ids? ids))
1622 #f "invalid or duplicate identifier in definition"
1624 (set-cdr! r (extend-env labels bindings (cdr r)))
1625 (build-letrec no-source #t
1626 (reverse (map syntax->datum var-ids))
1629 (expand (cdr x) (car x) empty-wrap mod))
1631 (build-sequence no-source
1633 (expand (cdr x) (car x) empty-wrap mod))
1634 (cons (cons er (source-wrap e w s mod))
1635 (cdr body)))))))))))))))))
1637 (define expand-local-syntax
1638 (lambda (rec? e r w s mod k)
1640 ((_ ((id val) ...) e1 e2 ...)
1641 (let ((ids #'(id ...)))
1642 (if (not (valid-bound-ids? ids))
1643 (syntax-violation #f "duplicate bound keyword" e)
1644 (let ((labels (gen-labels ids)))
1645 (let ((new-w (make-binding-wrap ids labels w)))
1649 (let ((w (if rec? new-w w))
1650 (trans-r (macros-only-env r)))
1652 (make-binding 'macro
1653 (eval-local-transformer
1654 (expand x trans-r w mod)
1661 (_ (syntax-violation #f "bad local syntax definition"
1662 (source-wrap e w s mod))))))
1664 (define eval-local-transformer
1665 (lambda (expanded mod)
1666 (let ((p (local-eval-hook expanded mod)))
1669 (syntax-violation #f "nonprocedure transformer" p)))))
1673 (build-void no-source)))
1677 (and (nonsymbol-id? x)
1678 (free-id=? x #'(... ...)))))
1680 (define lambda-formals
1682 (define (req args rreq)
1683 (syntax-case args ()
1685 (check (reverse rreq) #f))
1687 (req #'b (cons #'a rreq)))
1689 (check (reverse rreq) #'r))
1691 (syntax-violation 'lambda "invalid argument list" orig-args args))))
1692 (define (check req rest)
1694 ((distinct-bound-ids? (if rest (cons rest req) req))
1695 (values req #f rest #f))
1697 (syntax-violation 'lambda "duplicate identifier in argument list"
1699 (req orig-args '())))
1701 (define expand-simple-lambda
1702 (lambda (e r w s mod req rest meta body)
1703 (let* ((ids (if rest (append req (list rest)) req))
1704 (vars (map gen-var ids))
1705 (labels (gen-labels ids)))
1706 (build-simple-lambda
1708 (map syntax->datum req) (and rest (syntax->datum rest)) vars
1710 (expand-body body (source-wrap e w s mod)
1711 (extend-var-env labels vars r)
1712 (make-binding-wrap ids labels w)
1715 (define lambda*-formals
1717 (define (req args rreq)
1718 (syntax-case args ()
1720 (check (reverse rreq) '() #f '()))
1722 (req #'b (cons #'a rreq)))
1723 ((a . b) (eq? (syntax->datum #'a) #:optional)
1724 (opt #'b (reverse rreq) '()))
1725 ((a . b) (eq? (syntax->datum #'a) #:key)
1726 (key #'b (reverse rreq) '() '()))
1727 ((a b) (eq? (syntax->datum #'a) #:rest)
1728 (rest #'b (reverse rreq) '() '()))
1730 (rest #'r (reverse rreq) '() '()))
1732 (syntax-violation 'lambda* "invalid argument list" orig-args args))))
1733 (define (opt args req ropt)
1734 (syntax-case args ()
1736 (check req (reverse ropt) #f '()))
1738 (opt #'b req (cons #'(a #f) ropt)))
1739 (((a init) . b) (id? #'a)
1740 (opt #'b req (cons #'(a init) ropt)))
1741 ((a . b) (eq? (syntax->datum #'a) #:key)
1742 (key #'b req (reverse ropt) '()))
1743 ((a b) (eq? (syntax->datum #'a) #:rest)
1744 (rest #'b req (reverse ropt) '()))
1746 (rest #'r req (reverse ropt) '()))
1748 (syntax-violation 'lambda* "invalid optional argument list"
1750 (define (key args req opt rkey)
1751 (syntax-case args ()
1753 (check req opt #f (cons #f (reverse rkey))))
1755 (with-syntax ((k (symbol->keyword (syntax->datum #'a))))
1756 (key #'b req opt (cons #'(k a #f) rkey))))
1757 (((a init) . b) (id? #'a)
1758 (with-syntax ((k (symbol->keyword (syntax->datum #'a))))
1759 (key #'b req opt (cons #'(k a init) rkey))))
1760 (((a init k) . b) (and (id? #'a)
1761 (keyword? (syntax->datum #'k)))
1762 (key #'b req opt (cons #'(k a init) rkey)))
1763 ((aok) (eq? (syntax->datum #'aok) #:allow-other-keys)
1764 (check req opt #f (cons #t (reverse rkey))))
1765 ((aok a b) (and (eq? (syntax->datum #'aok) #:allow-other-keys)
1766 (eq? (syntax->datum #'a) #:rest))
1767 (rest #'b req opt (cons #t (reverse rkey))))
1768 ((aok . r) (and (eq? (syntax->datum #'aok) #:allow-other-keys)
1770 (rest #'r req opt (cons #t (reverse rkey))))
1771 ((a b) (eq? (syntax->datum #'a) #:rest)
1772 (rest #'b req opt (cons #f (reverse rkey))))
1774 (rest #'r req opt (cons #f (reverse rkey))))
1776 (syntax-violation 'lambda* "invalid keyword argument list"
1778 (define (rest args req opt kw)
1779 (syntax-case args ()
1781 (check req opt #'r kw))
1783 (syntax-violation 'lambda* "invalid rest argument"
1785 (define (check req opt rest kw)
1787 ((distinct-bound-ids?
1788 (append req (map car opt) (if rest (list rest) '())
1789 (if (pair? kw) (map cadr (cdr kw)) '())))
1790 (values req opt rest kw))
1792 (syntax-violation 'lambda* "duplicate identifier in argument list"
1794 (req orig-args '())))
1796 (define expand-lambda-case
1797 (lambda (e r w s mod get-formals clauses)
1798 (define (parse-req req opt rest kw body)
1799 (let ((vars (map gen-var req))
1800 (labels (gen-labels req)))
1801 (let ((r* (extend-var-env labels vars r))
1802 (w* (make-binding-wrap req labels w)))
1803 (parse-opt (map syntax->datum req)
1804 opt rest kw body (reverse vars) r* w* '() '()))))
1805 (define (parse-opt req opt rest kw body vars r* w* out inits)
1808 (syntax-case (car opt) ()
1810 (let* ((v (gen-var #'id))
1811 (l (gen-labels (list v)))
1812 (r** (extend-var-env l (list v) r*))
1813 (w** (make-binding-wrap (list #'id) l w*)))
1814 (parse-opt req (cdr opt) rest kw body (cons v vars)
1815 r** w** (cons (syntax->datum #'id) out)
1816 (cons (expand #'i r* w* mod) inits))))))
1818 (let* ((v (gen-var rest))
1819 (l (gen-labels (list v)))
1820 (r* (extend-var-env l (list v) r*))
1821 (w* (make-binding-wrap (list rest) l w*)))
1822 (parse-kw req (if (pair? out) (reverse out) #f)
1823 (syntax->datum rest)
1824 (if (pair? kw) (cdr kw) kw)
1825 body (cons v vars) r* w*
1826 (if (pair? kw) (car kw) #f)
1829 (parse-kw req (if (pair? out) (reverse out) #f) #f
1830 (if (pair? kw) (cdr kw) kw)
1832 (if (pair? kw) (car kw) #f)
1834 (define (parse-kw req opt rest kw body vars r* w* aok out inits)
1837 (syntax-case (car kw) ()
1839 (let* ((v (gen-var #'id))
1840 (l (gen-labels (list v)))
1841 (r** (extend-var-env l (list v) r*))
1842 (w** (make-binding-wrap (list #'id) l w*)))
1843 (parse-kw req opt rest (cdr kw) body (cons v vars)
1845 (cons (list (syntax->datum #'k)
1846 (syntax->datum #'id)
1849 (cons (expand #'i r* w* mod) inits))))))
1851 (parse-body req opt rest
1852 (if (or aok (pair? out)) (cons aok (reverse out)) #f)
1853 body (reverse vars) r* w* (reverse inits) '()))))
1854 (define (parse-body req opt rest kw body vars r* w* inits meta)
1855 (syntax-case body ()
1856 ((docstring e1 e2 ...) (string? (syntax->datum #'docstring))
1857 (parse-body req opt rest kw #'(e1 e2 ...) vars r* w* inits
1860 . ,(syntax->datum #'docstring))))))
1861 ((#((k . v) ...) e1 e2 ...)
1862 (parse-body req opt rest kw #'(e1 e2 ...) vars r* w* inits
1863 (append meta (syntax->datum #'((k . v) ...)))))
1865 (values meta req opt rest kw inits vars
1866 (expand-body #'(e1 e2 ...) (source-wrap e w s mod)
1869 (syntax-case clauses ()
1870 (() (values '() #f))
1871 (((args e1 e2 ...) (args* e1* e2* ...) ...)
1872 (call-with-values (lambda () (get-formals #'args))
1873 (lambda (req opt rest kw)
1874 (call-with-values (lambda ()
1875 (parse-req req opt rest kw #'(e1 e2 ...)))
1876 (lambda (meta req opt rest kw inits vars body)
1879 (expand-lambda-case e r w s mod get-formals
1880 #'((args* e1* e2* ...) ...)))
1881 (lambda (meta* else*)
1884 (build-lambda-case s req opt rest kw inits vars
1885 body else*))))))))))))
1889 ;; strips syntax-objects down to top-wrap
1891 ;; since only the head of a list is annotated by the reader, not each pair
1892 ;; in the spine, we also check for pairs whose cars are annotated in case
1893 ;; we've been passed the cdr of an annotated list
1902 (strip (syntax-object-expression x) (syntax-object-wrap x)))
1904 (let ((a (f (car x))) (d (f (cdr x))))
1905 (if (and (eq? a (car x)) (eq? d (cdr x)))
1909 (let ((old (vector->list x)))
1910 (let ((new (map f old)))
1911 ;; inlined and-map with two args
1912 (let lp ((l1 old) (l2 new))
1915 (if (eq? (car l1) (car l2))
1916 (lp (cdr l1) (cdr l2))
1917 (list->vector new)))))))
1920 ;; lexical variables
1924 (let ((id (if (syntax-object? id) (syntax-object-expression id) id)))
1925 (build-lexical-var no-source id))))
1927 ;; appears to return a reversed list
1928 (define lambda-var-list
1930 (let lvl ((vars vars) (ls '()) (w empty-wrap))
1932 ((pair? vars) (lvl (cdr vars) (cons (wrap (car vars) w #f) ls) w))
1933 ((id? vars) (cons (wrap vars w #f) ls))
1935 ((syntax-object? vars)
1936 (lvl (syntax-object-expression vars)
1938 (join-wraps w (syntax-object-wrap vars))))
1939 ;; include anything else to be caught by subsequent error
1941 (else (cons vars ls))))))
1943 ;; core transformers
1945 (global-extend 'local-syntax 'letrec-syntax #t)
1946 (global-extend 'local-syntax 'let-syntax #f)
1949 'core 'syntax-parameterize
1950 (lambda (e r w s mod)
1952 ((_ ((var val) ...) e1 e2 ...)
1953 (valid-bound-ids? #'(var ...))
1957 (lambda () (resolve-identifier x w r mod #f))
1958 (lambda (type value mod)
1960 ((displaced-lexical)
1961 (syntax-violation 'syntax-parameterize
1962 "identifier out of context"
1964 (source-wrap x w s mod)))
1968 (syntax-violation 'syntax-parameterize
1969 "invalid syntax parameter"
1971 (source-wrap x w s mod)))))))
1974 (let ((trans-r (macros-only-env r)))
1978 (eval-local-transformer (expand x trans-r w mod) mod)))
1980 (expand-body #'(e1 e2 ...)
1981 (source-wrap e w s mod)
1982 (extend-env names bindings r)
1985 (_ (syntax-violation 'syntax-parameterize "bad syntax"
1986 (source-wrap e w s mod))))))
1988 (global-extend 'core 'quote
1989 (lambda (e r w s mod)
1991 ((_ e) (build-data s (strip #'e w)))
1992 (_ (syntax-violation 'quote "bad syntax"
1993 (source-wrap e w s mod))))))
1999 (lambda (src e r maps ellipsis? mod)
2001 (call-with-values (lambda ()
2002 (resolve-identifier e empty-wrap r mod #f))
2003 (lambda (type value mod)
2007 (lambda () (gen-ref src (car value) (cdr value) maps))
2009 (values `(ref ,var) maps))))
2012 (syntax-violation 'syntax "misplaced ellipsis" src)
2013 (values `(quote ,e) maps))))))
2017 (gen-syntax src #'e r maps (lambda (x) #f) mod))
2019 ;; this could be about a dozen lines of code, except that we
2020 ;; choose to handle #'(x ... ...) forms
2026 (gen-syntax src #'x r
2027 (cons '() maps) ellipsis? mod))
2029 (if (null? (car maps))
2030 (syntax-violation 'syntax "extra ellipsis"
2032 (values (gen-map x (car maps))
2040 (lambda () (k (cons '() maps)))
2042 (if (null? (car maps))
2043 (syntax-violation 'syntax "extra ellipsis" src)
2044 (values (gen-mappend x (car maps))
2046 (_ (call-with-values
2047 (lambda () (gen-syntax src y r maps ellipsis? mod))
2050 (lambda () (k maps))
2052 (values (gen-append x y) maps)))))))))
2055 (lambda () (gen-syntax src #'x r maps ellipsis? mod))
2058 (lambda () (gen-syntax src #'y r maps ellipsis? mod))
2059 (lambda (y maps) (values (gen-cons x y) maps))))))
2063 (gen-syntax src #'(e1 e2 ...) r maps ellipsis? mod))
2064 (lambda (e maps) (values (gen-vector e) maps))))
2065 (_ (values `(quote ,e) maps))))))
2068 (lambda (src var level maps)
2072 (syntax-violation 'syntax "missing ellipsis" src)
2074 (lambda () (gen-ref src var (fx- level 1) (cdr maps)))
2075 (lambda (outer-var outer-maps)
2076 (let ((b (assq outer-var (car maps))))
2078 (values (cdr b) maps)
2079 (let ((inner-var (gen-var 'tmp)))
2081 (cons (cons (cons outer-var inner-var)
2083 outer-maps)))))))))))
2087 `(apply (primitive append) ,(gen-map e map-env))))
2091 (let ((formals (map cdr map-env))
2092 (actuals (map (lambda (x) `(ref ,(car x))) map-env)))
2095 ;; identity map equivalence:
2096 ;; (map (lambda (x) x) y) == y
2099 (lambda (x) (and (eq? (car x) 'ref) (memq (cadr x) formals)))
2101 ;; eta map equivalence:
2102 ;; (map (lambda (x ...) (f x ...)) y ...) == (map f y ...)
2103 `(map (primitive ,(car e))
2104 ,@(map (let ((r (map cons formals actuals)))
2105 (lambda (x) (cdr (assq (cadr x) r))))
2107 (else `(map (lambda ,formals ,e) ,@actuals))))))
2113 (if (eq? (car x) 'quote)
2114 `(quote (,(cadr x) . ,(cadr y)))
2115 (if (eq? (cadr y) '())
2118 ((list) `(list ,x ,@(cdr y)))
2119 (else `(cons ,x ,y)))))
2123 (if (equal? y '(quote ()))
2130 ((eq? (car x) 'list) `(vector ,@(cdr x)))
2131 ((eq? (car x) 'quote) `(quote #(,@(cadr x))))
2132 (else `(list->vector ,x)))))
2138 ((ref) (build-lexical-reference 'value no-source (cadr x) (cadr x)))
2139 ((primitive) (build-primref no-source (cadr x)))
2140 ((quote) (build-data no-source (cadr x)))
2142 (if (list? (cadr x))
2143 (build-simple-lambda no-source (cadr x) #f (cadr x) '() (regen (caddr x)))
2144 (error "how did we get here" x)))
2145 (else (build-primcall no-source (car x) (map regen (cdr x)))))))
2147 (lambda (e r w s mod)
2148 (let ((e (source-wrap e w s mod)))
2152 (lambda () (gen-syntax e #'x r '() ellipsis? mod))
2153 (lambda (e maps) (regen e))))
2154 (_ (syntax-violation 'syntax "bad `syntax' form" e)))))))
2156 (global-extend 'core 'lambda
2157 (lambda (e r w s mod)
2160 (call-with-values (lambda () (lambda-formals #'args))
2161 (lambda (req opt rest kw)
2162 (let lp ((body #'(e1 e2 ...)) (meta '()))
2163 (syntax-case body ()
2164 ((docstring e1 e2 ...) (string? (syntax->datum #'docstring))
2168 . ,(syntax->datum #'docstring))))))
2169 ((#((k . v) ...) e1 e2 ...)
2171 (append meta (syntax->datum #'((k . v) ...)))))
2172 (_ (expand-simple-lambda e r w s mod req rest meta body)))))))
2173 (_ (syntax-violation 'lambda "bad lambda" e)))))
2175 (global-extend 'core 'lambda*
2176 (lambda (e r w s mod)
2181 (expand-lambda-case e r w s mod
2182 lambda*-formals #'((args e1 e2 ...))))
2183 (lambda (meta lcase)
2184 (build-case-lambda s meta lcase))))
2185 (_ (syntax-violation 'lambda "bad lambda*" e)))))
2187 (global-extend 'core 'case-lambda
2188 (lambda (e r w s mod)
2189 (define (build-it meta clauses)
2192 (expand-lambda-case e r w s mod
2195 (lambda (meta* lcase)
2196 (build-case-lambda s (append meta meta*) lcase))))
2198 ((_ (args e1 e2 ...) ...)
2199 (build-it '() #'((args e1 e2 ...) ...)))
2200 ((_ docstring (args e1 e2 ...) ...)
2201 (string? (syntax->datum #'docstring))
2202 (build-it `((documentation
2203 . ,(syntax->datum #'docstring)))
2204 #'((args e1 e2 ...) ...)))
2205 (_ (syntax-violation 'case-lambda "bad case-lambda" e)))))
2207 (global-extend 'core 'case-lambda*
2208 (lambda (e r w s mod)
2209 (define (build-it meta clauses)
2212 (expand-lambda-case e r w s mod
2215 (lambda (meta* lcase)
2216 (build-case-lambda s (append meta meta*) lcase))))
2218 ((_ (args e1 e2 ...) ...)
2219 (build-it '() #'((args e1 e2 ...) ...)))
2220 ((_ docstring (args e1 e2 ...) ...)
2221 (string? (syntax->datum #'docstring))
2222 (build-it `((documentation
2223 . ,(syntax->datum #'docstring)))
2224 #'((args e1 e2 ...) ...)))
2225 (_ (syntax-violation 'case-lambda "bad case-lambda*" e)))))
2227 (global-extend 'core 'let
2229 (define (expand-let e r w s mod constructor ids vals exps)
2230 (if (not (valid-bound-ids? ids))
2231 (syntax-violation 'let "duplicate bound variable" e)
2232 (let ((labels (gen-labels ids))
2233 (new-vars (map gen-var ids)))
2234 (let ((nw (make-binding-wrap ids labels w))
2235 (nr (extend-var-env labels new-vars r)))
2237 (map syntax->datum ids)
2239 (map (lambda (x) (expand x r w mod)) vals)
2240 (expand-body exps (source-wrap e nw s mod)
2242 (lambda (e r w s mod)
2244 ((_ ((id val) ...) e1 e2 ...)
2245 (and-map id? #'(id ...))
2246 (expand-let e r w s mod
2251 ((_ f ((id val) ...) e1 e2 ...)
2252 (and (id? #'f) (and-map id? #'(id ...)))
2253 (expand-let e r w s mod
2258 (_ (syntax-violation 'let "bad let" (source-wrap e w s mod)))))))
2261 (global-extend 'core 'letrec
2262 (lambda (e r w s mod)
2264 ((_ ((id val) ...) e1 e2 ...)
2265 (and-map id? #'(id ...))
2266 (let ((ids #'(id ...)))
2267 (if (not (valid-bound-ids? ids))
2268 (syntax-violation 'letrec "duplicate bound variable" e)
2269 (let ((labels (gen-labels ids))
2270 (new-vars (map gen-var ids)))
2271 (let ((w (make-binding-wrap ids labels w))
2272 (r (extend-var-env labels new-vars r)))
2274 (map syntax->datum ids)
2276 (map (lambda (x) (expand x r w mod)) #'(val ...))
2277 (expand-body #'(e1 e2 ...)
2278 (source-wrap e w s mod) r w mod)))))))
2279 (_ (syntax-violation 'letrec "bad letrec" (source-wrap e w s mod))))))
2282 (global-extend 'core 'letrec*
2283 (lambda (e r w s mod)
2285 ((_ ((id val) ...) e1 e2 ...)
2286 (and-map id? #'(id ...))
2287 (let ((ids #'(id ...)))
2288 (if (not (valid-bound-ids? ids))
2289 (syntax-violation 'letrec* "duplicate bound variable" e)
2290 (let ((labels (gen-labels ids))
2291 (new-vars (map gen-var ids)))
2292 (let ((w (make-binding-wrap ids labels w))
2293 (r (extend-var-env labels new-vars r)))
2295 (map syntax->datum ids)
2297 (map (lambda (x) (expand x r w mod)) #'(val ...))
2298 (expand-body #'(e1 e2 ...)
2299 (source-wrap e w s mod) r w mod)))))))
2300 (_ (syntax-violation 'letrec* "bad letrec*" (source-wrap e w s mod))))))
2305 (lambda (e r w s mod)
2310 (lambda () (resolve-identifier #'id w r mod #t))
2311 (lambda (type value id-mod)
2314 (build-lexical-assignment s (syntax->datum #'id) value
2315 (expand #'val r w mod)))
2317 (build-global-assignment s value (expand #'val r w mod) id-mod))
2319 (if (procedure-property value 'variable-transformer)
2320 ;; As syntax-type does, call expand-macro with
2321 ;; the mod of the expression. Hmm.
2322 (expand (expand-macro value e r w s #f mod) r empty-wrap mod)
2323 (syntax-violation 'set! "not a variable transformer"
2325 (wrap #'id w id-mod))))
2326 ((displaced-lexical)
2327 (syntax-violation 'set! "identifier out of context"
2330 (syntax-violation 'set! "bad set!" (source-wrap e w s mod)))))))
2331 ((_ (head tail ...) val)
2333 (lambda () (syntax-type #'head r empty-wrap no-source #f mod #t))
2334 (lambda (type value ee* ee ww ss modmod)
2337 (let ((val (expand #'val r w mod)))
2338 (call-with-values (lambda () (value #'(head tail ...) r w mod))
2339 (lambda (e r w s* mod)
2342 (build-global-assignment s (syntax->datum #'e)
2346 (expand #'(setter head) r w mod)
2347 (map (lambda (e) (expand e r w mod))
2348 #'(tail ... val))))))))
2349 (_ (syntax-violation 'set! "bad set!" (source-wrap e w s mod))))))
2351 (global-extend 'module-ref '@
2355 (and (and-map id? #'(mod ...)) (id? #'id))
2356 ;; Strip the wrap from the identifier and return top-wrap
2357 ;; so that the identifier will not be captured by lexicals.
2358 (values (syntax->datum #'id) r top-wrap #f
2360 #'(public mod ...)))))))
2362 (global-extend 'module-ref '@@
2367 (cons (remodulate (car x) mod)
2368 (remodulate (cdr x) mod)))
2371 (remodulate (syntax-object-expression x) mod)
2372 (syntax-object-wrap x)
2373 ;; hither the remodulation
2376 (let* ((n (vector-length x)) (v (make-vector n)))
2377 (do ((i 0 (fx+ i 1)))
2379 (vector-set! v i (remodulate (vector-ref x i) mod)))))
2381 (syntax-case e (@@ primitive)
2384 (equal? (cdr (if (syntax-object? #'id)
2385 (syntax-object-module #'id)
2388 ;; Strip the wrap from the identifier and return top-wrap
2389 ;; so that the identifier will not be captured by lexicals.
2390 (values (syntax->datum #'id) r top-wrap #f '(primitive)))
2392 (and (and-map id? #'(mod ...)) (id? #'id))
2393 ;; Strip the wrap from the identifier and return top-wrap
2394 ;; so that the identifier will not be captured by lexicals.
2395 (values (syntax->datum #'id) r top-wrap #f
2397 #'(private mod ...))))
2398 ((_ @@ (mod ...) exp)
2399 (and-map id? #'(mod ...))
2400 ;; This is a special syntax used to support R6RS library forms.
2401 ;; Unlike the syntax above, the last item is not restricted to
2402 ;; be a single identifier, and the syntax objects are kept
2403 ;; intact, with only their module changed.
2404 (let ((mod (syntax->datum #'(private mod ...))))
2405 (values (remodulate #'exp mod)
2406 r w (source-annotation #'exp)
2409 (global-extend 'core 'if
2410 (lambda (e r w s mod)
2415 (expand #'test r w mod)
2416 (expand #'then r w mod)
2417 (build-void no-source)))
2421 (expand #'test r w mod)
2422 (expand #'then r w mod)
2423 (expand #'else r w mod))))))
2425 (global-extend 'core 'with-fluids
2426 (lambda (e r w s mod)
2428 ((_ ((fluid val) ...) b b* ...)
2431 (map (lambda (x) (expand x r w mod)) #'(fluid ...))
2432 (map (lambda (x) (expand x r w mod)) #'(val ...))
2433 (expand-body #'(b b* ...)
2434 (source-wrap e w s mod) r w mod))))))
2436 (global-extend 'begin 'begin '())
2438 (global-extend 'define 'define '())
2440 (global-extend 'define-syntax 'define-syntax '())
2441 (global-extend 'define-syntax-parameter 'define-syntax-parameter '())
2443 (global-extend 'eval-when 'eval-when '())
2445 (global-extend 'core 'syntax-case
2447 (define convert-pattern
2448 ;; accepts pattern & keys
2449 ;; returns $sc-dispatch pattern & ids
2450 (lambda (pattern keys)
2453 (if (not (pair? p*))
2456 (lambda () (cvt* (cdr p*) n ids))
2459 (lambda () (cvt (car p*) n ids))
2461 (values (cons x y) ids))))))))
2463 (define (v-reverse x)
2464 (let loop ((r '()) (x x))
2467 (loop (cons (car x) r) (cdr x)))))
2473 ((bound-id-member? p keys)
2474 (values (vector 'free-id p) ids))
2478 (values 'any (cons (cons p n) ids))))
2481 (ellipsis? (syntax dots))
2483 (lambda () (cvt (syntax x) (fx+ n 1) ids))
2485 (values (if (eq? p 'any) 'each-any (vector 'each p))
2488 (ellipsis? (syntax dots))
2490 (lambda () (cvt* (syntax ys) n ids))
2493 (lambda () (cvt (syntax x) (+ n 1) ids))
2496 (lambda () (v-reverse ys))
2498 (values `#(each+ ,x ,ys ,e)
2502 (lambda () (cvt (syntax y) n ids))
2505 (lambda () (cvt (syntax x) n ids))
2507 (values (cons x y) ids))))))
2508 (() (values '() ids))
2511 (lambda () (cvt (syntax (x ...)) n ids))
2512 (lambda (p ids) (values (vector 'vector p) ids))))
2513 (x (values (vector 'atom (strip p empty-wrap)) ids))))))
2514 (cvt pattern 0 '())))
2516 (define build-dispatch-call
2517 (lambda (pvars exp y r mod)
2518 (let ((ids (map car pvars)) (levels (map cdr pvars)))
2519 (let ((labels (gen-labels ids)) (new-vars (map gen-var ids)))
2523 (list (build-simple-lambda no-source (map syntax->datum ids) #f new-vars '()
2527 (map (lambda (var level)
2528 (make-binding 'syntax `(,var . ,level)))
2532 (make-binding-wrap ids labels empty-wrap)
2537 (lambda (x keys clauses r pat fender exp mod)
2539 (lambda () (convert-pattern pat keys))
2542 ((not (distinct-bound-ids? (map car pvars)))
2543 (syntax-violation 'syntax-case "duplicate pattern variable" pat))
2544 ((not (and-map (lambda (x) (not (ellipsis? (car x)))) pvars))
2545 (syntax-violation 'syntax-case "misplaced ellipsis" pat))
2547 (let ((y (gen-var 'tmp)))
2548 ;; fat finger binding and references to temp variable y
2549 (build-call no-source
2550 (build-simple-lambda no-source (list 'tmp) #f (list y) '()
2551 (let ((y (build-lexical-reference 'value no-source
2553 (build-conditional no-source
2554 (syntax-case fender ()
2556 (_ (build-conditional no-source
2558 (build-dispatch-call pvars fender y r mod)
2559 (build-data no-source #f))))
2560 (build-dispatch-call pvars exp y r mod)
2561 (gen-syntax-case x keys clauses r mod))))
2562 (list (if (eq? p 'any)
2563 (build-primcall no-source 'list (list x))
2564 (build-primcall no-source '$sc-dispatch
2565 (list x (build-data no-source p)))))))))))))
2567 (define gen-syntax-case
2568 (lambda (x keys clauses r mod)
2570 (build-primcall no-source 'syntax-violation
2571 (list (build-data no-source #f)
2572 (build-data no-source
2573 "source expression failed to match any pattern")
2575 (syntax-case (car clauses) ()
2577 (if (and (id? #'pat)
2578 (and-map (lambda (x) (not (free-id=? #'pat x)))
2579 (cons #'(... ...) keys)))
2580 (if (free-id=? #'pat #'_)
2581 (expand #'exp r empty-wrap mod)
2582 (let ((labels (list (gen-label)))
2583 (var (gen-var #'pat)))
2584 (build-call no-source
2585 (build-simple-lambda
2586 no-source (list (syntax->datum #'pat)) #f (list var)
2590 (list (make-binding 'syntax `(,var . 0)))
2592 (make-binding-wrap #'(pat)
2596 (gen-clause x keys (cdr clauses) r
2597 #'pat #t #'exp mod)))
2599 (gen-clause x keys (cdr clauses) r
2600 #'pat #'fender #'exp mod))
2601 (_ (syntax-violation 'syntax-case "invalid clause"
2604 (lambda (e r w s mod)
2605 (let ((e (source-wrap e w s mod)))
2607 ((_ val (key ...) m ...)
2608 (if (and-map (lambda (x) (and (id? x) (not (ellipsis? x))))
2610 (let ((x (gen-var 'tmp)))
2611 ;; fat finger binding and references to temp variable x
2613 (build-simple-lambda no-source (list 'tmp) #f (list x) '()
2614 (gen-syntax-case (build-lexical-reference 'value no-source
2616 #'(key ...) #'(m ...)
2619 (list (expand #'val r empty-wrap mod))))
2620 (syntax-violation 'syntax-case "invalid literals list" e))))))))
2622 ;; The portable macroexpand seeds expand-top's mode m with 'e (for
2623 ;; evaluating) and esew (which stands for "eval syntax expanders
2624 ;; when") with '(eval). In Chez Scheme, m is set to 'c instead of e
2625 ;; if we are compiling a file, and esew is set to
2626 ;; (eval-syntactic-expanders-when), which defaults to the list
2627 ;; '(compile load eval). This means that, by default, top-level
2628 ;; syntactic definitions are evaluated immediately after they are
2629 ;; expanded, and the expanded definitions are also residualized into
2630 ;; the object file if we are compiling a file.
2632 (lambda* (x #:optional (m 'e) (esew '(eval)))
2633 (expand-top-sequence (list x) null-env top-wrap #f m esew
2634 (cons 'hygiene (module-name (current-module))))))
2642 (make-syntax-object datum (syntax-object-wrap id)
2643 (syntax-object-module id))))
2646 ;; accepts any object, since syntax objects may consist partially
2647 ;; or entirely of unwrapped, nonsymbolic data
2649 (strip x empty-wrap)))
2652 (lambda (x) (source-annotation x)))
2654 (set! generate-temporaries
2656 (arg-check list? ls 'generate-temporaries)
2657 (let ((mod (cons 'hygiene (module-name (current-module)))))
2658 (map (lambda (x) (wrap (gensym "t-") top-wrap mod)) ls))))
2660 (set! free-identifier=?
2662 (arg-check nonsymbol-id? x 'free-identifier=?)
2663 (arg-check nonsymbol-id? y 'free-identifier=?)
2666 (set! bound-identifier=?
2668 (arg-check nonsymbol-id? x 'bound-identifier=?)
2669 (arg-check nonsymbol-id? y 'bound-identifier=?)
2672 (set! syntax-violation
2673 (lambda* (who message form #:optional subform)
2674 (arg-check (lambda (x) (or (not x) (string? x) (symbol? x)))
2675 who 'syntax-violation)
2676 (arg-check string? message 'syntax-violation)
2677 (throw 'syntax-error who message
2678 (or (source-annotation subform)
2679 (source-annotation form))
2680 (strip form empty-wrap)
2681 (and subform (strip subform empty-wrap)))))
2684 (define (syntax-module id)
2685 (arg-check nonsymbol-id? id 'syntax-module)
2686 (let ((mod (syntax-object-module id)))
2687 (and (not (equal? mod '(primitive)))
2690 (define* (syntax-local-binding id #:key (resolve-syntax-parameters? #t))
2691 (arg-check nonsymbol-id? id 'syntax-local-binding)
2692 (with-transformer-environment
2693 (lambda (e r w s rib mod)
2694 (define (strip-anti-mark w)
2695 (let ((ms (wrap-marks w)) (s (wrap-subst w)))
2696 (if (and (pair? ms) (eq? (car ms) the-anti-mark))
2697 ;; output is from original text
2698 (make-wrap (cdr ms) (if rib (cons rib (cdr s)) (cdr s)))
2699 ;; output introduced by macro
2700 (make-wrap ms (if rib (cons rib s) s)))))
2701 (call-with-values (lambda ()
2703 (syntax-object-expression id)
2704 (strip-anti-mark (syntax-object-wrap id))
2706 (syntax-object-module id)
2707 resolve-syntax-parameters?))
2708 (lambda (type value mod)
2710 ((lexical) (values 'lexical value))
2711 ((macro) (values 'macro value))
2712 ((syntax-parameter) (values 'syntax-parameter (car value)))
2713 ((syntax) (values 'pattern-variable value))
2714 ((displaced-lexical) (values 'displaced-lexical #f))
2716 (if (equal? mod '(primitive))
2717 (values 'primitive value)
2718 (values 'global (cons value (cdr mod)))))
2719 (else (values 'other #f))))))))
2721 (define (syntax-locally-bound-identifiers id)
2722 (arg-check nonsymbol-id? id 'syntax-locally-bound-identifiers)
2723 (locally-bound-identifiers (syntax-object-wrap id)
2724 (syntax-object-module id)))
2726 ;; Using define! instead of set! to avoid warnings at
2727 ;; compile-time, after the variables are stolen away into (system
2728 ;; syntax). See the end of boot-9.scm.
2730 (define! 'syntax-module syntax-module)
2731 (define! 'syntax-local-binding syntax-local-binding)
2732 (define! 'syntax-locally-bound-identifiers syntax-locally-bound-identifiers))
2734 ;; $sc-dispatch expects an expression and a pattern. If the expression
2735 ;; matches the pattern a list of the matching expressions for each
2736 ;; "any" is returned. Otherwise, #f is returned. (This use of #f will
2737 ;; not work on r4rs implementations that violate the ieee requirement
2738 ;; that #f and () be distinct.)
2740 ;; The expression is matched with the pattern as follows:
2742 ;; pattern: matches:
2745 ;; (<pattern>1 . <pattern>2) (<pattern>1 . <pattern>2)
2747 ;; #(free-id <key>) <key> with free-identifier=?
2748 ;; #(each <pattern>) (<pattern>*)
2749 ;; #(each+ p1 (p2_1 ... p2_n) p3) (p1* (p2_n ... p2_1) . p3)
2750 ;; #(vector <pattern>) (list->vector <pattern>)
2751 ;; #(atom <object>) <object> with "equal?"
2753 ;; Vector cops out to pair under assumption that vectors are rare. If
2754 ;; not, should convert to:
2755 ;; #(vector <pattern>*) #(<pattern>*)
2763 (let ((first (match (car e) p w '() mod)))
2765 (let ((rest (match-each (cdr e) p w mod)))
2766 (and rest (cons first rest))))))
2769 (match-each (syntax-object-expression e)
2771 (join-wraps w (syntax-object-wrap e))
2772 (syntax-object-module e)))
2776 (lambda (e x-pat y-pat z-pat w r mod)
2777 (let f ((e e) (w w))
2780 (call-with-values (lambda () (f (cdr e) w))
2781 (lambda (xr* y-pat r)
2784 (let ((xr (match (car e) x-pat w '() mod)))
2786 (values (cons xr xr*) y-pat r)
2791 (match (car e) (car y-pat) w r mod)))
2792 (values #f #f #f)))))
2794 (f (syntax-object-expression e) (join-wraps w e)))
2796 (values '() y-pat (match e z-pat w r mod)))))))
2798 (define match-each-any
2802 (let ((l (match-each-any (cdr e) w mod)))
2803 (and l (cons (wrap (car e) w mod) l))))
2806 (match-each-any (syntax-object-expression e)
2807 (join-wraps w (syntax-object-wrap e))
2816 ((eq? p 'any) (cons '() r))
2817 ((pair? p) (match-empty (car p) (match-empty (cdr p) r)))
2818 ((eq? p 'each-any) (cons '() r))
2820 (case (vector-ref p 0)
2821 ((each) (match-empty (vector-ref p 1) r))
2822 ((each+) (match-empty (vector-ref p 1)
2824 (reverse (vector-ref p 2))
2825 (match-empty (vector-ref p 3) r))))
2827 ((vector) (match-empty (vector-ref p 1) r)))))))
2831 (if (null? (car r*))
2833 (cons (map car r*) (combine (map cdr r*) r)))))
2836 (lambda (e p w r mod)
2838 ((null? p) (and (null? e) r))
2840 (and (pair? e) (match (car e) (car p) w
2841 (match (cdr e) (cdr p) w r mod)
2844 (let ((l (match-each-any e w mod))) (and l (cons l r))))
2846 (case (vector-ref p 0)
2849 (match-empty (vector-ref p 1) r)
2850 (let ((l (match-each e (vector-ref p 1) w mod)))
2852 (let collect ((l l))
2855 (cons (map car l) (collect (map cdr l)))))))))
2859 (match-each+ e (vector-ref p 1) (vector-ref p 2) (vector-ref p 3) w r mod))
2860 (lambda (xr* y-pat r)
2864 (match-empty (vector-ref p 1) r)
2865 (combine xr* r))))))
2866 ((free-id) (and (id? e) (free-id=? (wrap e w mod) (vector-ref p 1)) r))
2867 ((atom) (and (equal? (vector-ref p 1) (strip e w)) r))
2870 (match (vector->list e) (vector-ref p 1) w r mod))))))))
2873 (lambda (e p w r mod)
2877 ((eq? p 'any) (cons (wrap e w mod) r))
2880 (syntax-object-expression e)
2882 (join-wraps w (syntax-object-wrap e))
2884 (syntax-object-module e)))
2885 (else (match* e p w r mod)))))
2890 ((eq? p 'any) (list e))
2893 (match* (syntax-object-expression e)
2894 p (syntax-object-wrap e) '() (syntax-object-module e)))
2895 (else (match* e p empty-wrap '() #f))))))))
2898 (define-syntax with-syntax
2902 #'(let () e1 e2 ...))
2903 ((_ ((out in)) e1 e2 ...)
2904 #'(syntax-case in ()
2905 (out (let () e1 e2 ...))))
2906 ((_ ((out in) ...) e1 e2 ...)
2907 #'(syntax-case (list in ...) ()
2908 ((out ...) (let () e1 e2 ...)))))))
2910 (define-syntax syntax-rules
2913 ((_ (k ...) ((keyword . pattern) template) ...)
2915 ;; embed patterns as procedure metadata
2916 #((macro-type . syntax-rules)
2917 (patterns pattern ...))
2918 (syntax-case x (k ...)
2919 ((_ . pattern) #'template)
2921 ((_ (k ...) docstring ((keyword . pattern) template) ...)
2922 (string? (syntax->datum #'docstring))
2924 ;; the same, but allow a docstring
2926 #((macro-type . syntax-rules)
2927 (patterns pattern ...))
2928 (syntax-case x (k ...)
2929 ((_ . pattern) #'template)
2932 (define-syntax define-syntax-rule
2935 ((_ (name . pattern) template)
2936 #'(define-syntax name
2938 ((_ . pattern) template))))
2939 ((_ (name . pattern) docstring template)
2940 (string? (syntax->datum #'docstring))
2941 #'(define-syntax name
2944 ((_ . pattern) template)))))))
2949 ((let* ((x v) ...) e1 e2 ...)
2950 (and-map identifier? #'(x ...))
2951 (let f ((bindings #'((x v) ...)))
2952 (if (null? bindings)
2953 #'(let () e1 e2 ...)
2954 (with-syntax ((body (f (cdr bindings)))
2955 (binding (car bindings)))
2956 #'(let (binding) body))))))))
2958 (define-syntax quasiquote
2960 (define (quasi p lev)
2961 (syntax-case p (unquote quasiquote)
2965 (quasicons #'("quote" unquote) (quasi #'(p) (- lev 1)))))
2966 ((quasiquote p) (quasicons #'("quote" quasiquote) (quasi #'(p) (+ lev 1))))
2968 (syntax-case #'p (unquote unquote-splicing)
2971 (quasilist* #'(("value" p) ...) (quasi #'q lev))
2973 (quasicons #'("quote" unquote) (quasi #'(p ...) (- lev 1)))
2975 ((unquote-splicing p ...)
2977 (quasiappend #'(("value" p) ...) (quasi #'q lev))
2979 (quasicons #'("quote" unquote-splicing) (quasi #'(p ...) (- lev 1)))
2981 (_ (quasicons (quasi #'p lev) (quasi #'q lev)))))
2982 (#(x ...) (quasivector (vquasi #'(x ...) lev)))
2984 (define (vquasi p lev)
2987 (syntax-case #'p (unquote unquote-splicing)
2990 (quasilist* #'(("value" p) ...) (vquasi #'q lev))
2992 (quasicons #'("quote" unquote) (quasi #'(p ...) (- lev 1)))
2994 ((unquote-splicing p ...)
2996 (quasiappend #'(("value" p) ...) (vquasi #'q lev))
2999 #'("quote" unquote-splicing)
3000 (quasi #'(p ...) (- lev 1)))
3002 (_ (quasicons (quasi #'p lev) (vquasi #'q lev)))))
3003 (() #'("quote" ()))))
3004 (define (quasicons x y)
3005 (with-syntax ((x x) (y y))
3009 (("quote" dx) #'("quote" (dx . dy)))
3010 (_ (if (null? #'dy) #'("list" x) #'("list*" x y)))))
3011 (("list" . stuff) #'("list" x . stuff))
3012 (("list*" . stuff) #'("list*" x . stuff))
3013 (_ #'("list*" x y)))))
3014 (define (quasiappend x y)
3018 ((null? x) #'("quote" ()))
3019 ((null? (cdr x)) (car x))
3020 (else (with-syntax (((p ...) x)) #'("append" p ...)))))
3024 (else (with-syntax (((p ...) x) (y y)) #'("append" p ... y)))))))
3025 (define (quasilist* x y)
3029 (quasicons (car x) (f (cdr x))))))
3030 (define (quasivector x)
3032 (("quote" (x ...)) #'("quote" #(x ...)))
3034 (let f ((y x) (k (lambda (ls) #`("vector" #,@ls))))
3036 (("quote" (y ...)) (k #'(("quote" y) ...)))
3037 (("list" y ...) (k #'(y ...)))
3038 (("list*" y ... z) (f #'z (lambda (ls) (k (append #'(y ...) ls)))))
3039 (else #`("list->vector" #,x)))))))
3043 (("list" x ...) #`(list #,@(map emit #'(x ...))))
3044 ;; could emit list* for 3+ arguments if implementation supports
3047 (let f ((x* #'(x ...)))
3050 #`(cons #,(emit (car x*)) #,(f (cdr x*))))))
3051 (("append" x ...) #`(append #,@(map emit #'(x ...))))
3052 (("vector" x ...) #`(vector #,@(map emit #'(x ...))))
3053 (("list->vector" x) #`(list->vector #,(emit #'x)))
3057 ;; convert to intermediate language, combining introduced (but
3058 ;; not unquoted source) quote expressions where possible and
3059 ;; choosing optimal construction code otherwise, then emit
3060 ;; Scheme code corresponding to the intermediate language forms.
3061 ((_ e) (emit (quasi #'e 0)))))))
3063 (define-syntax include
3067 (let* ((p (open-input-file
3068 (if (absolute-file-name? fn)
3070 (in-vicinity dir fn))))
3071 (enc (file-encoding p)))
3073 ;; Choose the input encoding deterministically.
3074 (set-port-encoding! p (or enc "UTF-8"))
3076 (let f ((x (read p))
3080 (close-input-port p)
3083 (cons (datum->syntax k x) result)))))))
3084 (let* ((src (syntax-source x))
3085 (file (and src (assq-ref src 'filename)))
3086 (dir (and (string? file) (dirname file))))
3089 (let ((fn (syntax->datum #'filename)))
3090 (with-syntax (((exp ...) (read-file fn dir #'filename)))
3091 #'(begin exp ...))))))))
3093 (define-syntax include-from-path
3097 (let ((fn (syntax->datum #'filename)))
3098 (with-syntax ((fn (datum->syntax
3100 (or (%search-load-path fn)
3101 (syntax-violation 'include-from-path
3102 "file not found in path"
3104 #'(include fn)))))))
3106 (define-syntax unquote
3108 (syntax-violation 'unquote
3109 "expression not valid outside of quasiquote"
3112 (define-syntax unquote-splicing
3114 (syntax-violation 'unquote-splicing
3115 "expression not valid outside of quasiquote"
3118 (define (make-variable-transformer proc)
3119 (if (procedure? proc)
3120 (let ((trans (lambda (x)
3121 #((macro-type . variable-transformer))
3123 (set-procedure-property! trans 'variable-transformer #t)
3125 (error "variable transformer not a procedure" proc)))
3127 (define-syntax identifier-syntax
3129 (syntax-case xx (set!)
3132 #((macro-type . identifier-syntax))
3138 #'(e x (... ...))))))
3139 ((_ (id exp1) ((set! var val) exp2))
3140 (and (identifier? #'id) (identifier? #'var))
3141 #'(make-variable-transformer
3143 #((macro-type . variable-transformer))
3144 (syntax-case x (set!)
3145 ((set! var val) #'exp2)
3146 ((id x (... ...)) #'(exp1 x (... ...)))
3147 (id (identifier? #'id) #'exp1))))))))
3149 (define-syntax define*
3152 ((_ (id . args) b0 b1 ...)
3153 #'(define id (lambda* args b0 b1 ...)))
3154 ((_ id val) (identifier? #'id)
3155 #'(define id val)))))