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.
45 ;;; Modified by Mark H Weaver <mhw@netris.org> according to the Git
46 ;;; revision control logs corresponding to this file: 2012, 2013.
49 ;;; This code is based on "Syntax Abstraction in Scheme"
50 ;;; by R. Kent Dybvig, Robert Hieb, and Carl Bruggeman.
51 ;;; Lisp and Symbolic Computation 5:4, 295-326, 1992.
52 ;;; <http://www.cs.indiana.edu/~dyb/pubs/LaSC-5-4-pp295-326.pdf>
55 ;;; This file defines the syntax-case expander, macroexpand, and a set
56 ;;; of associated syntactic forms and procedures. Of these, the
57 ;;; following are documented in The Scheme Programming Language,
58 ;;; Fourth Edition (R. Kent Dybvig, MIT Press, 2009), and in the
61 ;;; bound-identifier=?
64 ;;; syntax-parameterize
66 ;;; generate-temporaries
77 ;;; Additionally, the expander provides definitions for a number of core
78 ;;; Scheme syntactic bindings, such as `let', `lambda', and the like.
80 ;;; The remaining exports are listed below:
82 ;;; (macroexpand datum)
83 ;;; if datum represents a valid expression, macroexpand returns an
84 ;;; expanded version of datum in a core language that includes no
85 ;;; syntactic abstractions. The core language includes begin,
86 ;;; define, if, lambda, letrec, quote, and set!.
87 ;;; (eval-when situations expr ...)
88 ;;; conditionally evaluates expr ... at compile-time or run-time
89 ;;; depending upon situations (see the Chez Scheme System Manual,
90 ;;; Revision 3, for a complete description)
91 ;;; (syntax-violation who message form [subform])
92 ;;; used to report errors found during expansion
93 ;;; ($sc-dispatch e p)
94 ;;; used by expanded code to handle syntax-case matching
96 ;;; This file is shipped along with an expanded version of itself,
97 ;;; psyntax-pp.scm, which is loaded when psyntax.scm has not yet been
98 ;;; compiled. In this way, psyntax bootstraps off of an expanded
99 ;;; version of itself.
101 ;;; This implementation of the expander sometimes uses syntactic
102 ;;; abstractions when procedural abstractions would suffice. For
103 ;;; example, we define top-wrap and top-marked? as
105 ;;; (define-syntax top-wrap (identifier-syntax '((top))))
106 ;;; (define-syntax top-marked?
108 ;;; ((_ w) (memq 'top (wrap-marks w)))))
112 ;;; (define top-wrap '((top)))
113 ;;; (define top-marked?
114 ;;; (lambda (w) (memq 'top (wrap-marks w))))
116 ;;; On the other hand, we don't do this consistently; we define
117 ;;; make-wrap, wrap-marks, and wrap-subst simply as
119 ;;; (define make-wrap cons)
120 ;;; (define wrap-marks car)
121 ;;; (define wrap-subst cdr)
123 ;;; In Chez Scheme, the syntactic and procedural forms of these
124 ;;; abstractions are equivalent, since the optimizer consistently
125 ;;; integrates constants and small procedures. This will be true of
126 ;;; Guile as well, once we implement a proper inliner.
129 ;;; Implementation notes:
131 ;;; Objects with no standard print syntax, including objects containing
132 ;;; cycles and syntax object, are allowed in quoted data as long as they
133 ;;; are contained within a syntax form or produced by datum->syntax.
134 ;;; Such objects are never copied.
136 ;;; All identifiers that don't have macro definitions and are not bound
137 ;;; lexically are assumed to be global variables.
139 ;;; Top-level definitions of macro-introduced identifiers are allowed.
140 ;;; This may not be appropriate for implementations in which the
141 ;;; model is that bindings are created by definitions, as opposed to
142 ;;; one in which initial values are assigned by definitions.
144 ;;; Identifiers and syntax objects are implemented as vectors for
145 ;;; portability. As a result, it is possible to "forge" syntax objects.
147 ;;; The implementation of generate-temporaries assumes that it is
148 ;;; possible to generate globally unique symbols (gensyms).
150 ;;; The source location associated with incoming expressions is tracked
151 ;;; via the source-properties mechanism, a weak map from expression to
152 ;;; source information. At times the source is separated from the
153 ;;; expression; see the note below about "efficiency and confusion".
158 ;;; When changing syntax-object representations, it is necessary to support
159 ;;; both old and new syntax-object representations in id-var-name. It
160 ;;; should be sufficient to recognize old representations and treat
161 ;;; them as not lexically bound.
166 (set-current-module (resolve-module '(guile))))
169 (define-syntax define-expansion-constructors
173 (let lp ((n 0) (out '()))
174 (if (< n (vector-length %expanded-vtables))
176 (let* ((vtable (vector-ref %expanded-vtables n))
177 (stem (struct-ref vtable (+ vtable-offset-user 0)))
178 (fields (struct-ref vtable (+ vtable-offset-user 2)))
179 (sfields (map (lambda (f) (datum->syntax x f)) fields))
180 (ctor (datum->syntax x (symbol-append 'make- stem))))
181 (cons #`(define (#,ctor #,@sfields)
182 (make-struct (vector-ref %expanded-vtables #,n) 0
185 #`(begin #,@(reverse out))))))))
187 (define-syntax define-expansion-accessors
192 (let ((vtable (vector-ref %expanded-vtables n))
193 (stem (syntax->datum #'stem)))
194 (if (eq? (struct-ref vtable (+ vtable-offset-user 0)) stem)
196 (define (#,(datum->syntax x (symbol-append stem '?)) x)
198 (eq? (struct-vtable x)
199 (vector-ref %expanded-vtables #,n))))
202 (let ((get (datum->syntax x (symbol-append stem '- f)))
203 (set (datum->syntax x (symbol-append 'set- stem '- f '!)))
204 (idx (list-index (struct-ref vtable
205 (+ vtable-offset-user 2))
209 (struct-ref x #,idx))
211 (struct-set! x #,idx v)))))
212 (syntax->datum #'(field ...))))
215 (define-syntax define-structure
217 (define construct-name
218 (lambda (template-identifier . args)
226 (symbol->string (syntax->datum x))))
230 (and-map identifier? #'(name id1 ...))
232 ((constructor (construct-name #'name "make-" #'name))
233 (predicate (construct-name #'name #'name "?"))
235 (map (lambda (x) (construct-name x #'name "-" x))
239 (construct-name x "set-" #'name "-" x "!"))
242 (+ (length #'(id1 ...)) 1))
244 (let f ((i 1) (ids #'(id1 ...)))
247 (cons i (f (+ i 1) (cdr ids)))))))
251 (vector 'name id1 ... )))
255 (= (vector-length x) structure-length)
256 (eq? (vector-ref x 0) 'name))))
259 (vector-ref x index)))
263 (vector-set! x index update)))
267 (define-expansion-constructors)
268 (define-expansion-accessors lambda meta)
270 ;; hooks to nonportable run-time helpers
272 (define-syntax fx+ (identifier-syntax +))
273 (define-syntax fx- (identifier-syntax -))
274 (define-syntax fx= (identifier-syntax =))
275 (define-syntax fx< (identifier-syntax <))
277 (define top-level-eval-hook
281 (define local-eval-hook
285 ;; Capture syntax-session-id before we shove it off into a module.
287 (let ((v (module-variable (current-module) 'syntax-session-id)))
289 ((variable-ref v)))))
291 (define put-global-definition-hook
292 (lambda (symbol type val)
293 (module-define! (current-module)
295 (make-syntax-transformer symbol type val))))
297 (define get-global-definition-hook
298 (lambda (symbol module)
299 (if (and (not module) (current-module))
300 (warn "module system is booted, we should have a module" symbol))
301 (and (not (equal? module '(primitive)))
302 (let ((v (module-variable (if module
303 (resolve-module (cdr module))
306 (and v (variable-bound? v)
307 (let ((val (variable-ref v)))
308 (and (macro? val) (macro-type val)
309 (cons (macro-type val)
310 (macro-binding val))))))))))
313 (define (decorate-source e s)
314 (if (and s (supports-source-properties? e))
315 (set-source-properties! e s))
318 (define (maybe-name-value! name val)
320 (let ((meta (lambda-meta val)))
321 (if (not (assq 'name meta))
322 (set-lambda-meta! val (acons 'name name meta))))))
324 ;; output constructors
330 (lambda (source fun-exp arg-exps)
331 (make-call source fun-exp arg-exps)))
333 (define build-conditional
334 (lambda (source test-exp then-exp else-exp)
335 (make-conditional source test-exp then-exp else-exp)))
337 (define build-lexical-reference
338 (lambda (type source name var)
339 (make-lexical-ref source name var)))
341 (define build-lexical-assignment
342 (lambda (source name var exp)
343 (maybe-name-value! name exp)
344 (make-lexical-set source name var exp)))
346 (define (analyze-variable mod var modref-cont bare-cont)
349 (let ((kind (car mod))
352 ((public) (modref-cont mod var #t))
353 ((private) (if (not (equal? mod (module-name (current-module))))
354 (modref-cont mod var #f)
356 ((bare) (bare-cont var))
357 ((hygiene) (if (and (not (equal? mod (module-name (current-module))))
358 (module-variable (resolve-module mod) var))
359 (modref-cont mod var #f)
362 (syntax-violation #f "primitive not in operator position" var))
363 (else (syntax-violation #f "bad module kind" var mod))))))
365 (define build-global-reference
366 (lambda (source var mod)
369 (lambda (mod var public?)
370 (make-module-ref source mod var public?))
372 (make-toplevel-ref source var)))))
374 (define build-global-assignment
375 (lambda (source var exp mod)
376 (maybe-name-value! var exp)
379 (lambda (mod var public?)
380 (make-module-set source mod var public? exp))
382 (make-toplevel-set source var exp)))))
384 (define build-global-definition
385 (lambda (source var exp)
386 (maybe-name-value! var exp)
387 (make-toplevel-define source var exp)))
389 (define build-simple-lambda
390 (lambda (src req rest vars meta exp)
393 ;; hah, a case in which kwargs would be nice.
395 ;; src req opt rest kw inits vars body else
396 src req #f rest #f '() vars exp #f))))
398 (define build-case-lambda
399 (lambda (src meta body)
400 (make-lambda src meta body)))
402 (define build-lambda-case
404 ;; opt := (name ...) | #f
406 ;; kw := (allow-other-keys? (keyword name var) ...) | #f
409 ;; vars map to named arguments in the following order:
410 ;; required, optional (positional), rest, keyword.
411 ;; the body of a lambda: anything, already expanded
412 ;; else: lambda-case | #f
413 (lambda (src req opt rest kw inits vars body else-case)
414 (make-lambda-case src req opt rest kw inits vars body else-case)))
416 (define build-primcall
417 (lambda (src name args)
418 (make-primcall src name args)))
420 (define build-primref
422 (make-primitive-ref src name)))
424 (define (build-data src exp)
425 (make-const src exp))
427 (define build-sequence
429 (if (null? (cdr exps))
431 (make-seq src (car exps) (build-sequence #f (cdr exps))))))
434 (lambda (src ids vars val-exps body-exp)
435 (for-each maybe-name-value! ids val-exps)
438 (make-let src ids vars val-exps body-exp))))
440 (define build-named-let
441 (lambda (src ids vars val-exps body-exp)
446 (let ((proc (build-simple-lambda src ids #f vars '() body-exp)))
447 (maybe-name-value! f-name proc)
448 (for-each maybe-name-value! ids val-exps)
451 (list f-name) (list f) (list proc)
452 (build-call src (build-lexical-reference 'fun src f-name f)
456 (lambda (src in-order? ids vars val-exps body-exp)
460 (for-each maybe-name-value! ids val-exps)
461 (make-letrec src in-order? ids vars val-exps body-exp)))))
464 ;; FIXME: use a faster gensym
465 (define-syntax-rule (build-lexical-var src id)
466 (gensym (string-append (symbol->string id) "-")))
468 (define-structure (syntax-object expression wrap module))
470 (define-syntax no-source (identifier-syntax #f))
472 (define source-annotation
474 (let ((props (source-properties
475 (if (syntax-object? x)
476 (syntax-object-expression x)
478 (and (pair? props) props))))
480 (define-syntax-rule (arg-check pred? e who)
482 (if (not (pred? x)) (syntax-violation who "invalid argument" x))))
484 ;; compile-time environments
486 ;; wrap and environment comprise two level mapping.
487 ;; wrap : id --> label
488 ;; env : label --> <element>
490 ;; environments are represented in two parts: a lexical part and a global
491 ;; part. The lexical part is a simple list of associations from labels
492 ;; to bindings. The global part is implemented by
493 ;; {put,get}-global-definition-hook and associates symbols with
496 ;; global (assumed global variable) and displaced-lexical (see below)
497 ;; do not show up in any environment; instead, they are fabricated by
498 ;; resolve-identifier when it finds no other bindings.
500 ;; <environment> ::= ((<label> . <binding>)*)
502 ;; identifier bindings include a type and a value
504 ;; <binding> ::= (macro . <procedure>) macros
505 ;; (syntax-parameter . (<procedure>)) syntax parameters
506 ;; (core . <procedure>) core forms
507 ;; (module-ref . <procedure>) @ or @@
510 ;; (define-syntax) define-syntax
511 ;; (define-syntax-parameter) define-syntax-parameter
512 ;; (local-syntax . rec?) let-syntax/letrec-syntax
513 ;; (eval-when) eval-when
514 ;; (syntax . (<var> . <level>)) pattern variables
515 ;; (global) assumed global variable
516 ;; (lexical . <var>) lexical variables
517 ;; (ellipsis . <identifier>) custom ellipsis
518 ;; (displaced-lexical) displaced lexicals
519 ;; <level> ::= <nonnegative integer>
520 ;; <var> ::= variable returned by build-lexical-var
522 ;; a macro is a user-defined syntactic-form. a core is a
523 ;; system-defined syntactic form. begin, define, define-syntax,
524 ;; define-syntax-parameter, and eval-when are treated specially
525 ;; since they are sensitive to whether the form is at top-level and
526 ;; (except for eval-when) can denote valid internal definitions.
528 ;; a pattern variable is a variable introduced by syntax-case and can
529 ;; be referenced only within a syntax form.
531 ;; any identifier for which no top-level syntax definition or local
532 ;; binding of any kind has been seen is assumed to be a global
535 ;; a lexical variable is a lambda- or letrec-bound variable.
537 ;; an ellipsis binding is introduced by the 'with-ellipsis' special
540 ;; a displaced-lexical identifier is a lexical identifier removed from
541 ;; it's scope by the return of a syntax object containing the identifier.
542 ;; a displaced lexical can also appear when a letrec-syntax-bound
543 ;; keyword is referenced on the rhs of one of the letrec-syntax clauses.
544 ;; a displaced lexical should never occur with properly written macros.
546 (define-syntax make-binding
547 (syntax-rules (quote)
548 ((_ type value) (cons type value))
550 ((_ type) (cons type '()))))
551 (define-syntax-rule (binding-type x)
553 (define-syntax-rule (binding-value x)
556 (define-syntax null-env (identifier-syntax '()))
559 (lambda (labels bindings r)
562 (extend-env (cdr labels) (cdr bindings)
563 (cons (cons (car labels) (car bindings)) r)))))
565 (define extend-var-env
566 ;; variant of extend-env that forms "lexical" binding
567 (lambda (labels vars r)
570 (extend-var-env (cdr labels) (cdr vars)
571 (cons (cons (car labels) (make-binding 'lexical (car vars))) r)))))
573 ;; we use a "macros only" environment in expansion of local macro
574 ;; definitions so that their definitions can use local macros without
575 ;; attempting to use other lexical identifiers.
576 (define macros-only-env
581 (if (memq (cadr a) '(macro syntax-parameter ellipsis))
582 (cons a (macros-only-env (cdr r)))
583 (macros-only-env (cdr r)))))))
585 (define global-extend
586 (lambda (type sym val)
587 (put-global-definition-hook sym type val)))
590 ;; Conceptually, identifiers are always syntax objects. Internally,
591 ;; however, the wrap is sometimes maintained separately (a source of
592 ;; efficiency and confusion), so that symbols are also considered
593 ;; identifiers by id?. Externally, they are always wrapped.
595 (define nonsymbol-id?
597 (and (syntax-object? x)
598 (symbol? (syntax-object-expression x)))))
604 ((syntax-object? x) (symbol? (syntax-object-expression x)))
607 (define-syntax-rule (id-sym-name e)
609 (if (syntax-object? x)
610 (syntax-object-expression x)
613 (define id-sym-name&marks
615 (if (syntax-object? x)
617 (syntax-object-expression x)
618 (join-marks (wrap-marks w) (wrap-marks (syntax-object-wrap x))))
619 (values x (wrap-marks w)))))
621 ;; syntax object wraps
623 ;; <wrap> ::= ((<mark> ...) . (<subst> ...))
624 ;; <subst> ::= shift | <subs>
625 ;; <subs> ::= #(ribcage #(<sym> ...) #(<mark> ...) #(<label> ...))
626 ;; | #(ribcage (<sym> ...) (<mark> ...) (<label> ...))
628 (define-syntax make-wrap (identifier-syntax cons))
629 (define-syntax wrap-marks (identifier-syntax car))
630 (define-syntax wrap-subst (identifier-syntax cdr))
632 ;; labels must be comparable with "eq?", have read-write invariance,
633 ;; and distinct from symbols.
635 (string-append "l-" (session-id) (symbol->string (gensym "-"))))
641 (cons (gen-label) (gen-labels (cdr ls))))))
643 (define-structure (ribcage symnames marks labels))
645 (define-syntax empty-wrap (identifier-syntax '(())))
647 (define-syntax top-wrap (identifier-syntax '((top))))
649 (define-syntax-rule (top-marked? w)
650 (memq 'top (wrap-marks w)))
652 ;; Marks must be comparable with "eq?" and distinct from pairs and
653 ;; the symbol top. We do not use integers so that marks will remain
654 ;; unique even across file compiles.
656 (define-syntax the-anti-mark (identifier-syntax #f))
660 (make-wrap (cons the-anti-mark (wrap-marks w))
661 (cons 'shift (wrap-subst w)))))
663 (define-syntax-rule (new-mark)
664 (gensym (string-append "m-" (session-id) "-")))
666 ;; make-empty-ribcage and extend-ribcage maintain list-based ribcages for
667 ;; internal definitions, in which the ribcages are built incrementally
668 (define-syntax-rule (make-empty-ribcage)
669 (make-ribcage '() '() '()))
671 (define extend-ribcage!
672 ;; must receive ids with complete wraps
673 (lambda (ribcage id label)
674 (set-ribcage-symnames! ribcage
675 (cons (syntax-object-expression id)
676 (ribcage-symnames ribcage)))
677 (set-ribcage-marks! ribcage
678 (cons (wrap-marks (syntax-object-wrap id))
679 (ribcage-marks ribcage)))
680 (set-ribcage-labels! ribcage
681 (cons label (ribcage-labels ribcage)))))
683 ;; make-binding-wrap creates vector-based ribcages
684 (define make-binding-wrap
685 (lambda (ids labels w)
691 (let ((labelvec (list->vector labels)))
692 (let ((n (vector-length labelvec)))
693 (let ((symnamevec (make-vector n)) (marksvec (make-vector n)))
694 (let f ((ids ids) (i 0))
695 (if (not (null? ids))
697 (lambda () (id-sym-name&marks (car ids) w))
698 (lambda (symname marks)
699 (vector-set! symnamevec i symname)
700 (vector-set! marksvec i marks)
701 (f (cdr ids) (fx+ i 1))))))
702 (make-ribcage symnamevec marksvec labelvec))))
713 (let ((m1 (wrap-marks w1)) (s1 (wrap-subst w1)))
719 (smart-append s1 (wrap-subst w2))))
721 (smart-append m1 (wrap-marks w2))
722 (smart-append s1 (wrap-subst w2)))))))
726 (smart-append m1 m2)))
733 (eq? (car x) (car y))
734 (same-marks? (cdr x) (cdr y))))))
737 ;; Syntax objects use wraps to associate names with marked
738 ;; identifiers. This function returns the name corresponding to
739 ;; the given identifier and wrap, or the original identifier if no
740 ;; corresponding name was found.
742 ;; The name may be a string created by gen-label, indicating a
743 ;; lexical binding, or another syntax object, indicating a
744 ;; reference to a top-level definition created during a previous
747 ;; For lexical variables, finding a label simply amounts to
748 ;; looking for an entry with the same symbolic name and the same
749 ;; marks. Finding a toplevel definition is the same, except we
750 ;; also have to compare modules, hence the `mod' parameter.
751 ;; Instead of adding a separate entry in the ribcage for modules,
752 ;; which wouldn't be used for lexicals, we arrange for the entry
753 ;; for the name entry to be a pair with the module in its car, and
754 ;; the name itself in the cdr. So if the name that we find is a
755 ;; pair, we have to check modules.
757 ;; The identifer may be passed in wrapped or unwrapped. In any
758 ;; case, this routine returns either a symbol, a syntax object, or
762 (define-syntax-rule (first e)
763 ;; Rely on Guile's multiple-values truncation.
766 (lambda (sym subst marks mod)
769 (let ((fst (car subst)))
771 (search sym (cdr subst) (cdr marks) mod)
772 (let ((symnames (ribcage-symnames fst)))
773 (if (vector? symnames)
774 (search-vector-rib sym subst marks symnames fst mod)
775 (search-list-rib sym subst marks symnames fst mod))))))))
776 (define search-list-rib
777 (lambda (sym subst marks symnames ribcage mod)
778 (let f ((symnames symnames) (i 0))
780 ((null? symnames) (search sym (cdr subst) marks mod))
781 ((and (eq? (car symnames) sym)
782 (same-marks? marks (list-ref (ribcage-marks ribcage) i)))
783 (let ((n (list-ref (ribcage-labels ribcage) i)))
785 (if (equal? mod (car n))
786 (values (cdr n) marks)
787 (f (cdr symnames) (fx+ i 1)))
789 (else (f (cdr symnames) (fx+ i 1)))))))
790 (define search-vector-rib
791 (lambda (sym subst marks symnames ribcage mod)
792 (let ((n (vector-length symnames)))
795 ((fx= i n) (search sym (cdr subst) marks mod))
796 ((and (eq? (vector-ref symnames i) sym)
797 (same-marks? marks (vector-ref (ribcage-marks ribcage) i)))
798 (let ((n (vector-ref (ribcage-labels ribcage) i)))
800 (if (equal? mod (car n))
801 (values (cdr n) marks)
804 (else (f (fx+ i 1))))))))
807 (or (first (search id (wrap-subst w) (wrap-marks w) mod)) id))
809 (let ((id (syntax-object-expression id))
810 (w1 (syntax-object-wrap id))
811 (mod (syntax-object-module id)))
812 (let ((marks (join-marks (wrap-marks w) (wrap-marks w1))))
813 (call-with-values (lambda () (search id (wrap-subst w) marks mod))
814 (lambda (new-id marks)
816 (first (search id (wrap-subst w1) marks mod))
818 (else (syntax-violation 'id-var-name "invalid id" id)))))
820 ;; A helper procedure for syntax-locally-bound-identifiers, which
821 ;; itself is a helper for transformer procedures.
822 ;; `locally-bound-identifiers' returns a list of all bindings
823 ;; visible to a syntax object with the given wrap. They are in
824 ;; order from outer to inner.
826 ;; The purpose of this procedure is to give a transformer procedure
827 ;; references on bound identifiers, that the transformer can then
828 ;; introduce some of them in its output. As such, the identifiers
829 ;; are anti-marked, so that rebuild-macro-output doesn't apply new
832 (define locally-bound-identifiers
835 (lambda (subst results)
838 (let ((fst (car subst)))
840 (scan (cdr subst) results)
841 (let ((symnames (ribcage-symnames fst))
842 (marks (ribcage-marks fst)))
843 (if (vector? symnames)
844 (scan-vector-rib subst symnames marks results)
845 (scan-list-rib subst symnames marks results))))))))
846 (define scan-list-rib
847 (lambda (subst symnames marks results)
848 (let f ((symnames symnames) (marks marks) (results results))
850 (scan (cdr subst) results)
851 (f (cdr symnames) (cdr marks)
852 (cons (wrap (car symnames)
853 (anti-mark (make-wrap (car marks) subst))
856 (define scan-vector-rib
857 (lambda (subst symnames marks results)
858 (let ((n (vector-length symnames)))
859 (let f ((i 0) (results results))
861 (scan (cdr subst) results)
863 (cons (wrap (vector-ref symnames i)
864 (anti-mark (make-wrap (vector-ref marks i) subst))
867 (scan (wrap-subst w) '())))
869 ;; Returns three values: binding type, binding value, the module (for
870 ;; resolving toplevel vars).
871 (define (resolve-identifier id w r mod resolve-syntax-parameters?)
872 (define (resolve-syntax-parameters b)
873 (if (and resolve-syntax-parameters?
874 (eq? (binding-type b) 'syntax-parameter))
875 (or (assq-ref r (binding-value b))
876 (make-binding 'macro (car (binding-value b))))
878 (define (resolve-global var mod)
879 (let ((b (resolve-syntax-parameters
880 (or (get-global-definition-hook var mod)
881 (make-binding 'global)))))
882 (if (eq? (binding-type b) 'global)
883 (values 'global var mod)
884 (values (binding-type b) (binding-value b) mod))))
885 (define (resolve-lexical label mod)
886 (let ((b (resolve-syntax-parameters
887 (or (assq-ref r label)
888 (make-binding 'displaced-lexical)))))
889 (values (binding-type b) (binding-value b) mod)))
890 (let ((n (id-var-name id w mod)))
893 ;; Recursing allows syntax-parameterize to override
894 ;; macro-introduced syntax parameters.
895 (resolve-identifier n w r mod resolve-syntax-parameters?))
897 (resolve-global n (if (syntax-object? id)
898 (syntax-object-module id)
901 (resolve-lexical n (if (syntax-object? id)
902 (syntax-object-module id)
905 (error "unexpected id-var-name" id w n)))))
907 (define transformer-environment
910 (error "called outside the dynamic extent of a syntax transformer"))))
912 (define (with-transformer-environment k)
913 ((fluid-ref transformer-environment) k))
915 ;; free-id=? must be passed fully wrapped ids since (free-id=? x y)
916 ;; may be true even if (free-id=? (wrap x w) (wrap y w)) is not.
920 (let* ((mi (and (syntax-object? i) (syntax-object-module i)))
921 (mj (and (syntax-object? j) (syntax-object-module j)))
922 (ni (id-var-name i empty-wrap mi))
923 (nj (id-var-name j empty-wrap mj)))
924 (define (id-module-binding id mod)
928 (resolve-module (cdr mod))
929 ;; Either modules have not been booted, or we have a
930 ;; raw symbol coming in, which is possible.
934 ((syntax-object? ni) (free-id=? ni j))
935 ((syntax-object? nj) (free-id=? i nj))
937 ;; `i' is not lexically bound. Assert that `j' is free,
938 ;; and if so, compare their bindings, that they are either
939 ;; bound to the same variable, or both unbound and have
941 (and (eq? nj (id-sym-name j))
942 (let ((bi (id-module-binding i mi)))
944 (eq? bi (id-module-binding j mj))
945 (and (not (id-module-binding j mj))
947 (eq? (id-module-binding i mi) (id-module-binding j mj))))
949 ;; Otherwise `i' is bound, so check that `j' is bound, and
950 ;; bound to the same thing.
953 ;; bound-id=? may be passed unwrapped (or partially wrapped) ids as
954 ;; long as the missing portion of the wrap is common to both of the ids
955 ;; since (bound-id=? x y) iff (bound-id=? (wrap x w) (wrap y w))
959 (if (and (syntax-object? i) (syntax-object? j))
960 (and (eq? (syntax-object-expression i)
961 (syntax-object-expression j))
962 (same-marks? (wrap-marks (syntax-object-wrap i))
963 (wrap-marks (syntax-object-wrap j))))
966 ;; "valid-bound-ids?" returns #t if it receives a list of distinct ids.
967 ;; valid-bound-ids? may be passed unwrapped (or partially wrapped) ids
968 ;; as long as the missing portion of the wrap is common to all of the
971 (define valid-bound-ids?
973 (and (let all-ids? ((ids ids))
976 (all-ids? (cdr ids)))))
977 (distinct-bound-ids? ids))))
979 ;; distinct-bound-ids? expects a list of ids and returns #t if there are
980 ;; no duplicates. It is quadratic on the length of the id list; long
981 ;; lists could be sorted to make it more efficient. distinct-bound-ids?
982 ;; may be passed unwrapped (or partially wrapped) ids as long as the
983 ;; missing portion of the wrap is common to all of the ids.
985 (define distinct-bound-ids?
987 (let distinct? ((ids ids))
989 (and (not (bound-id-member? (car ids) (cdr ids)))
990 (distinct? (cdr ids)))))))
992 (define bound-id-member?
994 (and (not (null? list))
995 (or (bound-id=? x (car list))
996 (bound-id-member? x (cdr list))))))
998 ;; wrapping expressions and identifiers
1001 (lambda (x w defmod)
1003 ((and (null? (wrap-marks w)) (null? (wrap-subst w))) x)
1006 (syntax-object-expression x)
1007 (join-wraps w (syntax-object-wrap x))
1008 (syntax-object-module x)))
1010 (else (make-syntax-object x w defmod)))))
1013 (lambda (x w s defmod)
1014 (wrap (decorate-source x s) w defmod)))
1018 (define expand-sequence
1019 (lambda (body r w s mod)
1021 (let dobody ((body body) (r r) (w w) (mod mod))
1024 (let ((first (expand (car body) r w mod)))
1025 (cons first (dobody (cdr body) r w mod))))))))
1027 ;; At top-level, we allow mixed definitions and expressions. Like
1028 ;; expand-body we expand in two passes.
1030 ;; First, from left to right, we expand just enough to know what
1031 ;; expressions are definitions, syntax definitions, and splicing
1032 ;; statements (`begin'). If we anything needs evaluating at
1033 ;; expansion-time, it is expanded directly.
1035 ;; Otherwise we collect expressions to expand, in thunks, and then
1036 ;; expand them all at the end. This allows all syntax expanders
1037 ;; visible in a toplevel sequence to be visible during the
1038 ;; expansions of all normal definitions and expressions in the
1041 (define expand-top-sequence
1042 (lambda (body r w s m esew mod)
1043 (let* ((r (cons '("placeholder" . (placeholder)) r))
1044 (ribcage (make-empty-ribcage))
1045 (w (make-wrap (wrap-marks w) (cons ribcage (wrap-subst w)))))
1046 (define (record-definition! id var)
1047 (let ((mod (cons 'hygiene (module-name (current-module)))))
1048 ;; Ribcages map symbol+marks to names, mostly for
1049 ;; resolving lexicals. Here to add a mapping for toplevel
1050 ;; definitions we also need to match the module. So, we
1051 ;; put it in the name instead, and make id-var-name handle
1052 ;; the special case of names that are pairs. See the
1053 ;; comments in id-var-name for more.
1054 (extend-ribcage! ribcage id
1055 (cons (syntax-object-module id)
1056 (wrap var top-wrap mod)))))
1057 (define (macro-introduced-identifier? id)
1058 (not (equal? (wrap-marks (syntax-object-wrap id)) '(top))))
1059 (define (fresh-derived-name id orig-form)
1061 (syntax-object-expression id)
1064 ;; FIXME: `hash' currently stops descending into nested
1065 ;; data at some point, so it's less unique than we would
1066 ;; like. Also this encodes hash values into the ABI of
1067 ;; compiled modules; a problem?
1069 (hash (syntax->datum orig-form) most-positive-fixnum)
1071 (define (parse body r w s m esew mod)
1072 (let lp ((body body) (exps '()))
1076 (append (parse1 (car body) r w s m esew mod)
1078 (define (parse1 x r w s m esew mod)
1081 (syntax-type x r w (source-annotation x) ribcage mod #f))
1082 (lambda (type value form e w s mod)
1085 (let* ((id (wrap value w mod))
1087 (var (if (macro-introduced-identifier? id)
1088 (fresh-derived-name id x)
1089 (syntax-object-expression id))))
1090 (record-definition! id var)
1093 (let ((x (build-global-definition s var (expand e r w mod))))
1094 (top-level-eval-hook x mod)
1097 (lambda () (resolve-identifier id empty-wrap r mod #t))
1098 (lambda (type* value* mod*)
1099 ;; If the identifier to be bound is currently bound to a
1100 ;; macro, then immediately discard that binding.
1101 (if (eq? type* 'macro)
1102 (top-level-eval-hook (build-global-definition
1103 s var (build-void s))
1106 (build-global-definition s var (expand e r w mod)))))))))
1107 ((define-syntax-form define-syntax-parameter-form)
1108 (let* ((id (wrap value w mod))
1110 (var (if (macro-introduced-identifier? id)
1111 (fresh-derived-name id x)
1112 (syntax-object-expression id))))
1113 (record-definition! id var)
1117 ((memq 'compile esew)
1118 (let ((e (expand-install-global var type (expand e r w mod))))
1119 (top-level-eval-hook e mod)
1120 (if (memq 'load esew)
1121 (list (lambda () e))
1125 (expand-install-global var type (expand e r w mod)))))
1128 (let ((e (expand-install-global var type (expand e r w mod))))
1129 (top-level-eval-hook e mod)
1130 (list (lambda () e))))
1132 (if (memq 'eval esew)
1133 (top-level-eval-hook
1134 (expand-install-global var type (expand e r w mod))
1140 (parse #'(e1 ...) r w s m esew mod))))
1141 ((local-syntax-form)
1142 (expand-local-syntax value e r w s mod
1143 (lambda (forms r w s mod)
1144 (parse forms r w s m esew mod))))
1147 ((_ (x ...) e1 e2 ...)
1148 (let ((when-list (parse-when-list e #'(x ...)))
1149 (body #'(e1 e2 ...)))
1150 (define (recurse m esew)
1151 (parse body r w s m esew mod))
1154 (if (memq 'eval when-list)
1155 (recurse (if (memq 'expand when-list) 'c&e 'e)
1158 (if (memq 'expand when-list)
1159 (top-level-eval-hook
1160 (expand-top-sequence body r w s 'e '(eval) mod)
1163 ((memq 'load when-list)
1164 (if (or (memq 'compile when-list)
1165 (memq 'expand when-list)
1166 (and (eq? m 'c&e) (memq 'eval when-list)))
1167 (recurse 'c&e '(compile load))
1168 (if (memq m '(c c&e))
1169 (recurse 'c '(load))
1171 ((or (memq 'compile when-list)
1172 (memq 'expand when-list)
1173 (and (eq? m 'c&e) (memq 'eval when-list)))
1174 (top-level-eval-hook
1175 (expand-top-sequence body r w s 'e '(eval) mod)
1183 (let ((x (expand-expr type value form e r w s mod)))
1184 (top-level-eval-hook x mod)
1187 (expand-expr type value form e r w s mod)))))))))
1188 (let ((exps (map (lambda (x) (x))
1189 (reverse (parse body r w s m esew mod)))))
1192 (build-sequence s exps))))))
1194 (define expand-install-global
1195 (lambda (name type e)
1196 (build-global-definition
1201 'make-syntax-transformer
1202 (if (eq? type 'define-syntax-parameter-form)
1203 (list (build-data no-source name)
1204 (build-data no-source 'syntax-parameter)
1205 (build-primcall no-source 'list (list e)))
1206 (list (build-data no-source name)
1207 (build-data no-source 'macro)
1210 (define parse-when-list
1211 (lambda (e when-list)
1212 ;; `when-list' is syntax'd version of list of situations. We
1213 ;; could match these keywords lexically, via free-id=?, but then
1214 ;; we twingle the definition of eval-when to the bindings of
1215 ;; eval, load, expand, and compile, which is totally unintended.
1216 ;; So do a symbolic match instead.
1217 (let ((result (strip when-list empty-wrap)))
1218 (let lp ((l result))
1221 (if (memq (car l) '(compile load eval expand))
1223 (syntax-violation 'eval-when "invalid situation" e
1226 ;; syntax-type returns seven values: type, value, form, e, w, s, and
1227 ;; mod. The first two are described in the table below.
1229 ;; type value explanation
1230 ;; -------------------------------------------------------------------
1231 ;; core procedure core singleton
1232 ;; core-form procedure core form
1233 ;; module-ref procedure @ or @@ singleton
1234 ;; lexical name lexical variable reference
1235 ;; global name global variable reference
1236 ;; begin none begin keyword
1237 ;; define none define keyword
1238 ;; define-syntax none define-syntax keyword
1239 ;; define-syntax-parameter none define-syntax-parameter keyword
1240 ;; local-syntax rec? letrec-syntax/let-syntax keyword
1241 ;; eval-when none eval-when keyword
1242 ;; syntax level pattern variable
1243 ;; displaced-lexical none displaced lexical identifier
1244 ;; lexical-call name call to lexical variable
1245 ;; global-call name call to global variable
1246 ;; primitive-call name call to primitive
1247 ;; call none any other call
1248 ;; begin-form none begin expression
1249 ;; define-form id variable definition
1250 ;; define-syntax-form id syntax definition
1251 ;; define-syntax-parameter-form id syntax parameter definition
1252 ;; local-syntax-form rec? syntax definition
1253 ;; eval-when-form none eval-when form
1254 ;; constant none self-evaluating datum
1255 ;; other none anything else
1257 ;; form is the entire form. For definition forms (define-form,
1258 ;; define-syntax-form, and define-syntax-parameter-form), e is the
1259 ;; rhs expression. For all others, e is the entire form. w is the
1260 ;; wrap for both form and e. s is the source for the entire form.
1261 ;; mod is the module for both form and e.
1263 ;; syntax-type expands macros and unwraps as necessary to get to one
1264 ;; of the forms above. It also parses definition forms, although
1265 ;; perhaps this should be done by the consumer.
1268 (lambda (e r w s rib mod for-car?)
1271 (call-with-values (lambda () (resolve-identifier e w r mod #t))
1272 (lambda (type value mod*)
1276 (values type value e e w s mod)
1277 (syntax-type (expand-macro value e r w s rib mod)
1278 r empty-wrap s rib mod #f)))
1280 ;; Toplevel definitions may resolve to bindings with
1281 ;; different names or in different modules.
1282 (values type value e value w s mod*))
1283 (else (values type value e e w s mod))))))
1285 (let ((first (car e)))
1287 (lambda () (syntax-type first r w s rib mod #t))
1288 (lambda (ftype fval fform fe fw fs fmod)
1291 (values 'lexical-call fval e e w s mod))
1293 (if (equal? fmod '(primitive))
1294 (values 'primitive-call fval e e w s mod)
1295 ;; If we got here via an (@@ ...) expansion, we
1296 ;; need to make sure the fmod information is
1297 ;; propagated back correctly -- hence this
1299 (values 'global-call (make-syntax-object fval w fmod)
1302 (syntax-type (expand-macro fval e r w s rib mod)
1303 r empty-wrap s rib mod for-car?))
1305 (call-with-values (lambda () (fval e r w mod))
1306 (lambda (e r w s mod)
1307 (syntax-type e r w s rib mod for-car?))))
1309 (values 'core-form fval e e w s mod))
1311 (values 'local-syntax-form fval e e w s mod))
1313 (values 'begin-form #f e e w s mod))
1315 (values 'eval-when-form #f e e w s mod))
1320 (values 'define-form #'name e #'val w s mod))
1321 ((_ (name . args) e1 e2 ...)
1323 (valid-bound-ids? (lambda-var-list #'args)))
1324 ;; need lambda here...
1325 (values 'define-form (wrap #'name w mod)
1328 (cons #'lambda (wrap #'(args e1 e2 ...) w mod))
1333 (values 'define-form (wrap #'name w mod)
1336 empty-wrap s mod))))
1341 (values 'define-syntax-form #'name e #'val w s mod))))
1342 ((define-syntax-parameter)
1346 (values 'define-syntax-parameter-form #'name e #'val w s mod))))
1348 (values 'call #f e e w s mod)))))))
1350 (syntax-type (syntax-object-expression e)
1352 (join-wraps w (syntax-object-wrap e))
1353 (or (source-annotation e) s) rib
1354 (or (syntax-object-module e) mod) for-car?))
1355 ((self-evaluating? e) (values 'constant #f e e w s mod))
1356 (else (values 'other #f e e w s mod)))))
1361 (lambda () (syntax-type e r w (source-annotation e) #f mod #f))
1362 (lambda (type value form e w s mod)
1363 (expand-expr type value form e r w s mod)))))
1366 (lambda (type value form e r w s mod)
1369 (build-lexical-reference 'value s e value))
1371 ;; apply transformer
1372 (value e r w s mod))
1374 (call-with-values (lambda () (value e r w mod))
1375 (lambda (e r w s mod)
1376 (expand e r w mod))))
1380 (build-lexical-reference 'fun (source-annotation id)
1381 (if (syntax-object? id)
1388 (build-global-reference (source-annotation (car e))
1389 (if (syntax-object? value)
1390 (syntax-object-expression value)
1392 (if (syntax-object? value)
1393 (syntax-object-module value)
1401 (map (lambda (e) (expand e r w mod))
1403 ((constant) (build-data s (strip (source-wrap e w s mod) empty-wrap)))
1404 ((global) (build-global-reference s value mod))
1405 ((call) (expand-call (expand (car e) r w mod) e r w s mod))
1408 ((_ e1 e2 ...) (expand-sequence #'(e1 e2 ...) r w s mod))
1410 (syntax-violation #f "sequence of zero expressions"
1411 (source-wrap e w s mod)))))
1412 ((local-syntax-form)
1413 (expand-local-syntax value e r w s mod expand-sequence))
1416 ((_ (x ...) e1 e2 ...)
1417 (let ((when-list (parse-when-list e #'(x ...))))
1418 (if (memq 'eval when-list)
1419 (expand-sequence #'(e1 e2 ...) r w s mod)
1421 ((define-form define-syntax-form define-syntax-parameter-form)
1422 (syntax-violation #f "definition in expression context, where definitions are not allowed,"
1423 (source-wrap form w s mod)))
1425 (syntax-violation #f "reference to pattern variable outside syntax form"
1426 (source-wrap e w s mod)))
1427 ((displaced-lexical)
1428 (syntax-violation #f "reference to identifier outside its scope"
1429 (source-wrap e w s mod)))
1430 (else (syntax-violation #f "unexpected syntax"
1431 (source-wrap e w s mod))))))
1434 (lambda (x e r w s mod)
1438 (map (lambda (e) (expand e r w mod)) #'(e1 ...)))))))
1440 ;; (What follows is my interpretation of what's going on here -- Andy)
1442 ;; A macro takes an expression, a tree, the leaves of which are identifiers
1443 ;; and datums. Identifiers are symbols along with a wrap and a module. For
1444 ;; efficiency, subtrees that share wraps and modules may be grouped as one
1447 ;; Going into the expansion, the expression is given an anti-mark, which
1448 ;; logically propagates to all leaves. Then, in the new expression returned
1449 ;; from the transfomer, if we see an expression with an anti-mark, we know it
1450 ;; pertains to the original expression; conversely, expressions without the
1451 ;; anti-mark are known to be introduced by the transformer.
1453 ;; OK, good until now. We know this algorithm does lexical scoping
1454 ;; appropriately because it's widely known in the literature, and psyntax is
1455 ;; widely used. But what about modules? Here we're on our own. What we do is
1456 ;; to mark the module of expressions produced by a macro as pertaining to the
1457 ;; module that was current when the macro was defined -- that is, free
1458 ;; identifiers introduced by a macro are scoped in the macro's module, not in
1459 ;; the expansion's module. Seems to work well.
1461 ;; The only wrinkle is when we want a macro to expand to code in another
1462 ;; module, as is the case for the r6rs `library' form -- the body expressions
1463 ;; should be scoped relative the the new module, the one defined by the macro.
1464 ;; For that, use `(@@ mod-name body)'.
1466 ;; Part of the macro output will be from the site of the macro use and part
1467 ;; from the macro definition. We allow source information from the macro use
1468 ;; to pass through, but we annotate the parts coming from the macro with the
1469 ;; source location information corresponding to the macro use. It would be
1470 ;; really nice if we could also annotate introduced expressions with the
1471 ;; locations corresponding to the macro definition, but that is not yet
1473 (define expand-macro
1474 (lambda (p e r w s rib mod)
1475 (define rebuild-macro-output
1479 (cons (rebuild-macro-output (car x) m)
1480 (rebuild-macro-output (cdr x) m))
1483 (let ((w (syntax-object-wrap x)))
1484 (let ((ms (wrap-marks w)) (ss (wrap-subst w)))
1485 (if (and (pair? ms) (eq? (car ms) the-anti-mark))
1486 ;; output is from original text
1488 (syntax-object-expression x)
1489 (make-wrap (cdr ms) (if rib (cons rib (cdr ss)) (cdr ss)))
1490 (syntax-object-module x))
1491 ;; output introduced by macro
1493 (decorate-source (syntax-object-expression x) s)
1494 (make-wrap (cons m ms)
1496 (cons rib (cons 'shift ss))
1498 (syntax-object-module x))))))
1501 (let* ((n (vector-length x))
1502 (v (decorate-source (make-vector n) s)))
1503 (do ((i 0 (fx+ i 1)))
1506 (rebuild-macro-output (vector-ref x i) m)))))
1508 (syntax-violation #f "encountered raw symbol in macro output"
1509 (source-wrap e w (wrap-subst w) mod) x))
1510 (else (decorate-source x s)))))
1511 (with-fluids ((transformer-environment
1512 (lambda (k) (k e r w s rib mod))))
1513 (rebuild-macro-output (p (source-wrap e (anti-mark w) s mod))
1517 ;; In processing the forms of the body, we create a new, empty wrap.
1518 ;; This wrap is augmented (destructively) each time we discover that
1519 ;; the next form is a definition. This is done:
1521 ;; (1) to allow the first nondefinition form to be a call to
1522 ;; one of the defined ids even if the id previously denoted a
1523 ;; definition keyword or keyword for a macro expanding into a
1525 ;; (2) to prevent subsequent definition forms (but unfortunately
1526 ;; not earlier ones) and the first nondefinition form from
1527 ;; confusing one of the bound identifiers for an auxiliary
1529 ;; (3) so that we do not need to restart the expansion of the
1530 ;; first nondefinition form, which is problematic anyway
1531 ;; since it might be the first element of a begin that we
1532 ;; have just spliced into the body (meaning if we restarted,
1533 ;; we'd really need to restart with the begin or the macro
1534 ;; call that expanded into the begin, and we'd have to give
1535 ;; up allowing (begin <defn>+ <expr>+), which is itself
1536 ;; problematic since we don't know if a begin contains only
1537 ;; definitions until we've expanded it).
1539 ;; Before processing the body, we also create a new environment
1540 ;; containing a placeholder for the bindings we will add later and
1541 ;; associate this environment with each form. In processing a
1542 ;; let-syntax or letrec-syntax, the associated environment may be
1543 ;; augmented with local keyword bindings, so the environment may
1544 ;; be different for different forms in the body. Once we have
1545 ;; gathered up all of the definitions, we evaluate the transformer
1546 ;; expressions and splice into r at the placeholder the new variable
1547 ;; and keyword bindings. This allows let-syntax or letrec-syntax
1548 ;; forms local to a portion or all of the body to shadow the
1549 ;; definition bindings.
1551 ;; Subforms of a begin, let-syntax, or letrec-syntax are spliced
1554 ;; outer-form is fully wrapped w/source
1555 (lambda (body outer-form r w mod)
1556 (let* ((r (cons '("placeholder" . (placeholder)) r))
1557 (ribcage (make-empty-ribcage))
1558 (w (make-wrap (wrap-marks w) (cons ribcage (wrap-subst w)))))
1559 (let parse ((body (map (lambda (x) (cons r (wrap x w mod))) body))
1560 (ids '()) (labels '())
1561 (var-ids '()) (vars '()) (vals '()) (bindings '()))
1563 (syntax-violation #f "no expressions in body" outer-form)
1564 (let ((e (cdar body)) (er (caar body)))
1566 (lambda () (syntax-type e er empty-wrap (source-annotation e) ribcage mod #f))
1567 (lambda (type value form e w s mod)
1570 (let ((id (wrap value w mod)) (label (gen-label)))
1571 (let ((var (gen-var id)))
1572 (extend-ribcage! ribcage id label)
1574 (cons id ids) (cons label labels)
1576 (cons var vars) (cons (cons er (wrap e w mod)) vals)
1577 (cons (make-binding 'lexical var) bindings)))))
1578 ((define-syntax-form)
1579 (let ((id (wrap value w mod))
1581 (trans-r (macros-only-env er)))
1582 (extend-ribcage! ribcage id label)
1583 ;; As required by R6RS, evaluate the right-hand-sides of internal
1584 ;; syntax definition forms and add their transformers to the
1585 ;; compile-time environment immediately, so that the newly-defined
1586 ;; keywords may be used in definition context within the same
1588 (set-cdr! r (extend-env
1592 (eval-local-transformer
1593 (expand e trans-r w mod)
1596 (parse (cdr body) (cons id ids) labels var-ids vars vals bindings)))
1597 ((define-syntax-parameter-form)
1598 ;; Same as define-syntax-form, but different format of the binding.
1599 (let ((id (wrap value w mod))
1601 (trans-r (macros-only-env er)))
1602 (extend-ribcage! ribcage id label)
1603 (set-cdr! r (extend-env
1607 (list (eval-local-transformer
1608 (expand e trans-r w mod)
1611 (parse (cdr body) (cons id ids) labels var-ids vars vals bindings)))
1615 (parse (let f ((forms #'(e1 ...)))
1618 (cons (cons er (wrap (car forms) w mod))
1620 ids labels var-ids vars vals bindings))))
1621 ((local-syntax-form)
1622 (expand-local-syntax value e er w s mod
1623 (lambda (forms er w s mod)
1624 (parse (let f ((forms forms))
1627 (cons (cons er (wrap (car forms) w mod))
1629 ids labels var-ids vars vals bindings))))
1630 (else ; found a non-definition
1632 (build-sequence no-source
1634 (expand (cdr x) (car x) empty-wrap mod))
1635 (cons (cons er (source-wrap e w s mod))
1638 (if (not (valid-bound-ids? ids))
1640 #f "invalid or duplicate identifier in definition"
1642 (set-cdr! r (extend-env labels bindings (cdr r)))
1643 (build-letrec no-source #t
1644 (reverse (map syntax->datum var-ids))
1647 (expand (cdr x) (car x) empty-wrap mod))
1649 (build-sequence no-source
1651 (expand (cdr x) (car x) empty-wrap mod))
1652 (cons (cons er (source-wrap e w s mod))
1653 (cdr body)))))))))))))))))
1655 (define expand-local-syntax
1656 (lambda (rec? e r w s mod k)
1658 ((_ ((id val) ...) e1 e2 ...)
1659 (let ((ids #'(id ...)))
1660 (if (not (valid-bound-ids? ids))
1661 (syntax-violation #f "duplicate bound keyword" e)
1662 (let ((labels (gen-labels ids)))
1663 (let ((new-w (make-binding-wrap ids labels w)))
1667 (let ((w (if rec? new-w w))
1668 (trans-r (macros-only-env r)))
1670 (make-binding 'macro
1671 (eval-local-transformer
1672 (expand x trans-r w mod)
1679 (_ (syntax-violation #f "bad local syntax definition"
1680 (source-wrap e w s mod))))))
1682 (define eval-local-transformer
1683 (lambda (expanded mod)
1684 (let ((p (local-eval-hook expanded mod)))
1687 (syntax-violation #f "nonprocedure transformer" p)))))
1691 (build-void no-source)))
1695 (and (nonsymbol-id? e)
1696 ;; If there is a binding for the special identifier
1697 ;; #{ $sc-ellipsis }# in the lexical environment of E,
1698 ;; and if the associated binding type is 'ellipsis',
1699 ;; then the binding's value specifies the custom ellipsis
1700 ;; identifier within that lexical environment, and the
1701 ;; comparison is done using 'bound-id=?'.
1703 (lambda () (resolve-identifier
1704 (make-syntax-object '#{ $sc-ellipsis }#
1705 (syntax-object-wrap e)
1706 (syntax-object-module e))
1707 empty-wrap r mod #f))
1708 (lambda (type value mod)
1709 (if (eq? type 'ellipsis)
1710 (bound-id=? e value)
1711 (free-id=? e #'(... ...))))))))
1713 (define lambda-formals
1715 (define (req args rreq)
1716 (syntax-case args ()
1718 (check (reverse rreq) #f))
1720 (req #'b (cons #'a rreq)))
1722 (check (reverse rreq) #'r))
1724 (syntax-violation 'lambda "invalid argument list" orig-args args))))
1725 (define (check req rest)
1727 ((distinct-bound-ids? (if rest (cons rest req) req))
1728 (values req #f rest #f))
1730 (syntax-violation 'lambda "duplicate identifier in argument list"
1732 (req orig-args '())))
1734 (define expand-simple-lambda
1735 (lambda (e r w s mod req rest meta body)
1736 (let* ((ids (if rest (append req (list rest)) req))
1737 (vars (map gen-var ids))
1738 (labels (gen-labels ids)))
1739 (build-simple-lambda
1741 (map syntax->datum req) (and rest (syntax->datum rest)) vars
1743 (expand-body body (source-wrap e w s mod)
1744 (extend-var-env labels vars r)
1745 (make-binding-wrap ids labels w)
1748 (define lambda*-formals
1750 (define (req args rreq)
1751 (syntax-case args ()
1753 (check (reverse rreq) '() #f '()))
1755 (req #'b (cons #'a rreq)))
1756 ((a . b) (eq? (syntax->datum #'a) #:optional)
1757 (opt #'b (reverse rreq) '()))
1758 ((a . b) (eq? (syntax->datum #'a) #:key)
1759 (key #'b (reverse rreq) '() '()))
1760 ((a b) (eq? (syntax->datum #'a) #:rest)
1761 (rest #'b (reverse rreq) '() '()))
1763 (rest #'r (reverse rreq) '() '()))
1765 (syntax-violation 'lambda* "invalid argument list" orig-args args))))
1766 (define (opt args req ropt)
1767 (syntax-case args ()
1769 (check req (reverse ropt) #f '()))
1771 (opt #'b req (cons #'(a #f) ropt)))
1772 (((a init) . b) (id? #'a)
1773 (opt #'b req (cons #'(a init) ropt)))
1774 ((a . b) (eq? (syntax->datum #'a) #:key)
1775 (key #'b req (reverse ropt) '()))
1776 ((a b) (eq? (syntax->datum #'a) #:rest)
1777 (rest #'b req (reverse ropt) '()))
1779 (rest #'r req (reverse ropt) '()))
1781 (syntax-violation 'lambda* "invalid optional argument list"
1783 (define (key args req opt rkey)
1784 (syntax-case args ()
1786 (check req opt #f (cons #f (reverse rkey))))
1788 (with-syntax ((k (symbol->keyword (syntax->datum #'a))))
1789 (key #'b req opt (cons #'(k a #f) rkey))))
1790 (((a init) . b) (id? #'a)
1791 (with-syntax ((k (symbol->keyword (syntax->datum #'a))))
1792 (key #'b req opt (cons #'(k a init) rkey))))
1793 (((a init k) . b) (and (id? #'a)
1794 (keyword? (syntax->datum #'k)))
1795 (key #'b req opt (cons #'(k a init) rkey)))
1796 ((aok) (eq? (syntax->datum #'aok) #:allow-other-keys)
1797 (check req opt #f (cons #t (reverse rkey))))
1798 ((aok a b) (and (eq? (syntax->datum #'aok) #:allow-other-keys)
1799 (eq? (syntax->datum #'a) #:rest))
1800 (rest #'b req opt (cons #t (reverse rkey))))
1801 ((aok . r) (and (eq? (syntax->datum #'aok) #:allow-other-keys)
1803 (rest #'r req opt (cons #t (reverse rkey))))
1804 ((a b) (eq? (syntax->datum #'a) #:rest)
1805 (rest #'b req opt (cons #f (reverse rkey))))
1807 (rest #'r req opt (cons #f (reverse rkey))))
1809 (syntax-violation 'lambda* "invalid keyword argument list"
1811 (define (rest args req opt kw)
1812 (syntax-case args ()
1814 (check req opt #'r kw))
1816 (syntax-violation 'lambda* "invalid rest argument"
1818 (define (check req opt rest kw)
1820 ((distinct-bound-ids?
1821 (append req (map car opt) (if rest (list rest) '())
1822 (if (pair? kw) (map cadr (cdr kw)) '())))
1823 (values req opt rest kw))
1825 (syntax-violation 'lambda* "duplicate identifier in argument list"
1827 (req orig-args '())))
1829 (define expand-lambda-case
1830 (lambda (e r w s mod get-formals clauses)
1831 (define (parse-req req opt rest kw body)
1832 (let ((vars (map gen-var req))
1833 (labels (gen-labels req)))
1834 (let ((r* (extend-var-env labels vars r))
1835 (w* (make-binding-wrap req labels w)))
1836 (parse-opt (map syntax->datum req)
1837 opt rest kw body (reverse vars) r* w* '() '()))))
1838 (define (parse-opt req opt rest kw body vars r* w* out inits)
1841 (syntax-case (car opt) ()
1843 (let* ((v (gen-var #'id))
1844 (l (gen-labels (list v)))
1845 (r** (extend-var-env l (list v) r*))
1846 (w** (make-binding-wrap (list #'id) l w*)))
1847 (parse-opt req (cdr opt) rest kw body (cons v vars)
1848 r** w** (cons (syntax->datum #'id) out)
1849 (cons (expand #'i r* w* mod) inits))))))
1851 (let* ((v (gen-var rest))
1852 (l (gen-labels (list v)))
1853 (r* (extend-var-env l (list v) r*))
1854 (w* (make-binding-wrap (list rest) l w*)))
1855 (parse-kw req (if (pair? out) (reverse out) #f)
1856 (syntax->datum rest)
1857 (if (pair? kw) (cdr kw) kw)
1858 body (cons v vars) r* w*
1859 (if (pair? kw) (car kw) #f)
1862 (parse-kw req (if (pair? out) (reverse out) #f) #f
1863 (if (pair? kw) (cdr kw) kw)
1865 (if (pair? kw) (car kw) #f)
1867 (define (parse-kw req opt rest kw body vars r* w* aok out inits)
1870 (syntax-case (car kw) ()
1872 (let* ((v (gen-var #'id))
1873 (l (gen-labels (list v)))
1874 (r** (extend-var-env l (list v) r*))
1875 (w** (make-binding-wrap (list #'id) l w*)))
1876 (parse-kw req opt rest (cdr kw) body (cons v vars)
1878 (cons (list (syntax->datum #'k)
1879 (syntax->datum #'id)
1882 (cons (expand #'i r* w* mod) inits))))))
1884 (parse-body req opt rest
1885 (if (or aok (pair? out)) (cons aok (reverse out)) #f)
1886 body (reverse vars) r* w* (reverse inits) '()))))
1887 (define (parse-body req opt rest kw body vars r* w* inits meta)
1888 (syntax-case body ()
1889 ((docstring e1 e2 ...) (string? (syntax->datum #'docstring))
1890 (parse-body req opt rest kw #'(e1 e2 ...) vars r* w* inits
1893 . ,(syntax->datum #'docstring))))))
1894 ((#((k . v) ...) e1 e2 ...)
1895 (parse-body req opt rest kw #'(e1 e2 ...) vars r* w* inits
1896 (append meta (syntax->datum #'((k . v) ...)))))
1898 (values meta req opt rest kw inits vars
1899 (expand-body #'(e1 e2 ...) (source-wrap e w s mod)
1902 (syntax-case clauses ()
1903 (() (values '() #f))
1904 (((args e1 e2 ...) (args* e1* e2* ...) ...)
1905 (call-with-values (lambda () (get-formals #'args))
1906 (lambda (req opt rest kw)
1907 (call-with-values (lambda ()
1908 (parse-req req opt rest kw #'(e1 e2 ...)))
1909 (lambda (meta req opt rest kw inits vars body)
1912 (expand-lambda-case e r w s mod get-formals
1913 #'((args* e1* e2* ...) ...)))
1914 (lambda (meta* else*)
1917 (build-lambda-case s req opt rest kw inits vars
1918 body else*))))))))))))
1922 ;; strips syntax-objects down to top-wrap
1924 ;; since only the head of a list is annotated by the reader, not each pair
1925 ;; in the spine, we also check for pairs whose cars are annotated in case
1926 ;; we've been passed the cdr of an annotated list
1935 (strip (syntax-object-expression x) (syntax-object-wrap x)))
1937 (let ((a (f (car x))) (d (f (cdr x))))
1938 (if (and (eq? a (car x)) (eq? d (cdr x)))
1942 (let ((old (vector->list x)))
1943 (let ((new (map f old)))
1944 ;; inlined and-map with two args
1945 (let lp ((l1 old) (l2 new))
1948 (if (eq? (car l1) (car l2))
1949 (lp (cdr l1) (cdr l2))
1950 (list->vector new)))))))
1953 ;; lexical variables
1957 (let ((id (if (syntax-object? id) (syntax-object-expression id) id)))
1958 (build-lexical-var no-source id))))
1960 ;; appears to return a reversed list
1961 (define lambda-var-list
1963 (let lvl ((vars vars) (ls '()) (w empty-wrap))
1965 ((pair? vars) (lvl (cdr vars) (cons (wrap (car vars) w #f) ls) w))
1966 ((id? vars) (cons (wrap vars w #f) ls))
1968 ((syntax-object? vars)
1969 (lvl (syntax-object-expression vars)
1971 (join-wraps w (syntax-object-wrap vars))))
1972 ;; include anything else to be caught by subsequent error
1974 (else (cons vars ls))))))
1976 ;; core transformers
1978 (global-extend 'local-syntax 'letrec-syntax #t)
1979 (global-extend 'local-syntax 'let-syntax #f)
1982 'core 'syntax-parameterize
1983 (lambda (e r w s mod)
1985 ((_ ((var val) ...) e1 e2 ...)
1986 (valid-bound-ids? #'(var ...))
1990 (lambda () (resolve-identifier x w r mod #f))
1991 (lambda (type value mod)
1993 ((displaced-lexical)
1994 (syntax-violation 'syntax-parameterize
1995 "identifier out of context"
1997 (source-wrap x w s mod)))
2001 (syntax-violation 'syntax-parameterize
2002 "invalid syntax parameter"
2004 (source-wrap x w s mod)))))))
2007 (let ((trans-r (macros-only-env r)))
2011 (eval-local-transformer (expand x trans-r w mod) mod)))
2013 (expand-body #'(e1 e2 ...)
2014 (source-wrap e w s mod)
2015 (extend-env names bindings r)
2018 (_ (syntax-violation 'syntax-parameterize "bad syntax"
2019 (source-wrap e w s mod))))))
2021 (global-extend 'core 'quote
2022 (lambda (e r w s mod)
2024 ((_ e) (build-data s (strip #'e w)))
2025 (_ (syntax-violation 'quote "bad syntax"
2026 (source-wrap e w s mod))))))
2032 (lambda (src e r maps ellipsis? mod)
2034 (call-with-values (lambda ()
2035 (resolve-identifier e empty-wrap r mod #f))
2036 (lambda (type value mod)
2040 (lambda () (gen-ref src (car value) (cdr value) maps))
2042 (values `(ref ,var) maps))))
2044 (if (ellipsis? e r mod)
2045 (syntax-violation 'syntax "misplaced ellipsis" src)
2046 (values `(quote ,e) maps))))))
2049 (ellipsis? #'dots r mod)
2050 (gen-syntax src #'e r maps (lambda (e r mod) #f) mod))
2052 ;; this could be about a dozen lines of code, except that we
2053 ;; choose to handle #'(x ... ...) forms
2054 (ellipsis? #'dots r mod)
2059 (gen-syntax src #'x r
2060 (cons '() maps) ellipsis? mod))
2062 (if (null? (car maps))
2063 (syntax-violation 'syntax "extra ellipsis"
2065 (values (gen-map x (car maps))
2069 (ellipsis? #'dots r mod)
2073 (lambda () (k (cons '() maps)))
2075 (if (null? (car maps))
2076 (syntax-violation 'syntax "extra ellipsis" src)
2077 (values (gen-mappend x (car maps))
2079 (_ (call-with-values
2080 (lambda () (gen-syntax src y r maps ellipsis? mod))
2083 (lambda () (k maps))
2085 (values (gen-append x y) maps)))))))))
2088 (lambda () (gen-syntax src #'x r maps ellipsis? mod))
2091 (lambda () (gen-syntax src #'y r maps ellipsis? mod))
2092 (lambda (y maps) (values (gen-cons x y) maps))))))
2096 (gen-syntax src #'(e1 e2 ...) r maps ellipsis? mod))
2097 (lambda (e maps) (values (gen-vector e) maps))))
2098 (_ (values `(quote ,e) maps))))))
2101 (lambda (src var level maps)
2105 (syntax-violation 'syntax "missing ellipsis" src)
2107 (lambda () (gen-ref src var (fx- level 1) (cdr maps)))
2108 (lambda (outer-var outer-maps)
2109 (let ((b (assq outer-var (car maps))))
2111 (values (cdr b) maps)
2112 (let ((inner-var (gen-var 'tmp)))
2114 (cons (cons (cons outer-var inner-var)
2116 outer-maps)))))))))))
2120 `(apply (primitive append) ,(gen-map e map-env))))
2124 (let ((formals (map cdr map-env))
2125 (actuals (map (lambda (x) `(ref ,(car x))) map-env)))
2128 ;; identity map equivalence:
2129 ;; (map (lambda (x) x) y) == y
2132 (lambda (x) (and (eq? (car x) 'ref) (memq (cadr x) formals)))
2134 ;; eta map equivalence:
2135 ;; (map (lambda (x ...) (f x ...)) y ...) == (map f y ...)
2136 `(map (primitive ,(car e))
2137 ,@(map (let ((r (map cons formals actuals)))
2138 (lambda (x) (cdr (assq (cadr x) r))))
2140 (else `(map (lambda ,formals ,e) ,@actuals))))))
2146 (if (eq? (car x) 'quote)
2147 `(quote (,(cadr x) . ,(cadr y)))
2148 (if (eq? (cadr y) '())
2151 ((list) `(list ,x ,@(cdr y)))
2152 (else `(cons ,x ,y)))))
2156 (if (equal? y '(quote ()))
2163 ((eq? (car x) 'list) `(vector ,@(cdr x)))
2164 ((eq? (car x) 'quote) `(quote #(,@(cadr x))))
2165 (else `(list->vector ,x)))))
2171 ((ref) (build-lexical-reference 'value no-source (cadr x) (cadr x)))
2172 ((primitive) (build-primref no-source (cadr x)))
2173 ((quote) (build-data no-source (cadr x)))
2175 (if (list? (cadr x))
2176 (build-simple-lambda no-source (cadr x) #f (cadr x) '() (regen (caddr x)))
2177 (error "how did we get here" x)))
2178 (else (build-primcall no-source (car x) (map regen (cdr x)))))))
2180 (lambda (e r w s mod)
2181 (let ((e (source-wrap e w s mod)))
2185 (lambda () (gen-syntax e #'x r '() ellipsis? mod))
2186 (lambda (e maps) (regen e))))
2187 (_ (syntax-violation 'syntax "bad `syntax' form" e)))))))
2189 (global-extend 'core 'lambda
2190 (lambda (e r w s mod)
2193 (call-with-values (lambda () (lambda-formals #'args))
2194 (lambda (req opt rest kw)
2195 (let lp ((body #'(e1 e2 ...)) (meta '()))
2196 (syntax-case body ()
2197 ((docstring e1 e2 ...) (string? (syntax->datum #'docstring))
2201 . ,(syntax->datum #'docstring))))))
2202 ((#((k . v) ...) e1 e2 ...)
2204 (append meta (syntax->datum #'((k . v) ...)))))
2205 (_ (expand-simple-lambda e r w s mod req rest meta body)))))))
2206 (_ (syntax-violation 'lambda "bad lambda" e)))))
2208 (global-extend 'core 'lambda*
2209 (lambda (e r w s mod)
2214 (expand-lambda-case e r w s mod
2215 lambda*-formals #'((args e1 e2 ...))))
2216 (lambda (meta lcase)
2217 (build-case-lambda s meta lcase))))
2218 (_ (syntax-violation 'lambda "bad lambda*" e)))))
2220 (global-extend 'core 'case-lambda
2221 (lambda (e r w s mod)
2222 (define (build-it meta clauses)
2225 (expand-lambda-case e r w s mod
2228 (lambda (meta* lcase)
2229 (build-case-lambda s (append meta meta*) lcase))))
2231 ((_ (args e1 e2 ...) ...)
2232 (build-it '() #'((args e1 e2 ...) ...)))
2233 ((_ docstring (args e1 e2 ...) ...)
2234 (string? (syntax->datum #'docstring))
2235 (build-it `((documentation
2236 . ,(syntax->datum #'docstring)))
2237 #'((args e1 e2 ...) ...)))
2238 (_ (syntax-violation 'case-lambda "bad case-lambda" e)))))
2240 (global-extend 'core 'case-lambda*
2241 (lambda (e r w s mod)
2242 (define (build-it meta clauses)
2245 (expand-lambda-case e r w s mod
2248 (lambda (meta* lcase)
2249 (build-case-lambda s (append meta meta*) lcase))))
2251 ((_ (args e1 e2 ...) ...)
2252 (build-it '() #'((args e1 e2 ...) ...)))
2253 ((_ docstring (args e1 e2 ...) ...)
2254 (string? (syntax->datum #'docstring))
2255 (build-it `((documentation
2256 . ,(syntax->datum #'docstring)))
2257 #'((args e1 e2 ...) ...)))
2258 (_ (syntax-violation 'case-lambda "bad case-lambda*" e)))))
2260 (global-extend 'core 'with-ellipsis
2261 (lambda (e r w s mod)
2265 (let ((id (if (symbol? #'dots)
2267 (make-syntax-object '#{ $sc-ellipsis }#
2268 (syntax-object-wrap #'dots)
2269 (syntax-object-module #'dots)))))
2270 (let ((ids (list id))
2271 (labels (list (gen-label)))
2272 (bindings (list (make-binding 'ellipsis (source-wrap #'dots w s mod)))))
2273 (let ((nw (make-binding-wrap ids labels w))
2274 (nr (extend-env labels bindings r)))
2275 (expand-body #'(e1 e2 ...) (source-wrap e nw s mod) nr nw mod)))))
2276 (_ (syntax-violation 'with-ellipsis "bad syntax"
2277 (source-wrap e w s mod))))))
2279 (global-extend 'core 'let
2281 (define (expand-let e r w s mod constructor ids vals exps)
2282 (if (not (valid-bound-ids? ids))
2283 (syntax-violation 'let "duplicate bound variable" e)
2284 (let ((labels (gen-labels ids))
2285 (new-vars (map gen-var ids)))
2286 (let ((nw (make-binding-wrap ids labels w))
2287 (nr (extend-var-env labels new-vars r)))
2289 (map syntax->datum ids)
2291 (map (lambda (x) (expand x r w mod)) vals)
2292 (expand-body exps (source-wrap e nw s mod)
2294 (lambda (e r w s mod)
2296 ((_ ((id val) ...) e1 e2 ...)
2297 (and-map id? #'(id ...))
2298 (expand-let e r w s mod
2303 ((_ f ((id val) ...) e1 e2 ...)
2304 (and (id? #'f) (and-map id? #'(id ...)))
2305 (expand-let e r w s mod
2310 (_ (syntax-violation 'let "bad let" (source-wrap e w s mod)))))))
2313 (global-extend 'core 'letrec
2314 (lambda (e r w s mod)
2316 ((_ ((id val) ...) e1 e2 ...)
2317 (and-map id? #'(id ...))
2318 (let ((ids #'(id ...)))
2319 (if (not (valid-bound-ids? ids))
2320 (syntax-violation 'letrec "duplicate bound variable" e)
2321 (let ((labels (gen-labels ids))
2322 (new-vars (map gen-var ids)))
2323 (let ((w (make-binding-wrap ids labels w))
2324 (r (extend-var-env labels new-vars r)))
2326 (map syntax->datum ids)
2328 (map (lambda (x) (expand x r w mod)) #'(val ...))
2329 (expand-body #'(e1 e2 ...)
2330 (source-wrap e w s mod) r w mod)))))))
2331 (_ (syntax-violation 'letrec "bad letrec" (source-wrap e w s mod))))))
2334 (global-extend 'core 'letrec*
2335 (lambda (e r w s mod)
2337 ((_ ((id val) ...) e1 e2 ...)
2338 (and-map id? #'(id ...))
2339 (let ((ids #'(id ...)))
2340 (if (not (valid-bound-ids? ids))
2341 (syntax-violation 'letrec* "duplicate bound variable" e)
2342 (let ((labels (gen-labels ids))
2343 (new-vars (map gen-var ids)))
2344 (let ((w (make-binding-wrap ids labels w))
2345 (r (extend-var-env labels new-vars r)))
2347 (map syntax->datum ids)
2349 (map (lambda (x) (expand x r w mod)) #'(val ...))
2350 (expand-body #'(e1 e2 ...)
2351 (source-wrap e w s mod) r w mod)))))))
2352 (_ (syntax-violation 'letrec* "bad letrec*" (source-wrap e w s mod))))))
2357 (lambda (e r w s mod)
2362 (lambda () (resolve-identifier #'id w r mod #t))
2363 (lambda (type value id-mod)
2366 (build-lexical-assignment s (syntax->datum #'id) value
2367 (expand #'val r w mod)))
2369 (build-global-assignment s value (expand #'val r w mod) id-mod))
2371 (if (procedure-property value 'variable-transformer)
2372 ;; As syntax-type does, call expand-macro with
2373 ;; the mod of the expression. Hmm.
2374 (expand (expand-macro value e r w s #f mod) r empty-wrap mod)
2375 (syntax-violation 'set! "not a variable transformer"
2377 (wrap #'id w id-mod))))
2378 ((displaced-lexical)
2379 (syntax-violation 'set! "identifier out of context"
2382 (syntax-violation 'set! "bad set!" (source-wrap e w s mod)))))))
2383 ((_ (head tail ...) val)
2385 (lambda () (syntax-type #'head r empty-wrap no-source #f mod #t))
2386 (lambda (type value ee* ee ww ss modmod)
2389 (let ((val (expand #'val r w mod)))
2390 (call-with-values (lambda () (value #'(head tail ...) r w mod))
2391 (lambda (e r w s* mod)
2394 (build-global-assignment s (syntax->datum #'e)
2398 (expand #'(setter head) r w mod)
2399 (map (lambda (e) (expand e r w mod))
2400 #'(tail ... val))))))))
2401 (_ (syntax-violation 'set! "bad set!" (source-wrap e w s mod))))))
2403 (global-extend 'module-ref '@
2407 (and (and-map id? #'(mod ...)) (id? #'id))
2408 ;; Strip the wrap from the identifier and return top-wrap
2409 ;; so that the identifier will not be captured by lexicals.
2410 (values (syntax->datum #'id) r top-wrap #f
2412 #'(public mod ...)))))))
2414 (global-extend 'module-ref '@@
2419 (cons (remodulate (car x) mod)
2420 (remodulate (cdr x) mod)))
2423 (remodulate (syntax-object-expression x) mod)
2424 (syntax-object-wrap x)
2425 ;; hither the remodulation
2428 (let* ((n (vector-length x)) (v (make-vector n)))
2429 (do ((i 0 (fx+ i 1)))
2431 (vector-set! v i (remodulate (vector-ref x i) mod)))))
2433 (syntax-case e (@@ primitive)
2436 (equal? (cdr (if (syntax-object? #'id)
2437 (syntax-object-module #'id)
2440 ;; Strip the wrap from the identifier and return top-wrap
2441 ;; so that the identifier will not be captured by lexicals.
2442 (values (syntax->datum #'id) r top-wrap #f '(primitive)))
2444 (and (and-map id? #'(mod ...)) (id? #'id))
2445 ;; Strip the wrap from the identifier and return top-wrap
2446 ;; so that the identifier will not be captured by lexicals.
2447 (values (syntax->datum #'id) r top-wrap #f
2449 #'(private mod ...))))
2450 ((_ @@ (mod ...) exp)
2451 (and-map id? #'(mod ...))
2452 ;; This is a special syntax used to support R6RS library forms.
2453 ;; Unlike the syntax above, the last item is not restricted to
2454 ;; be a single identifier, and the syntax objects are kept
2455 ;; intact, with only their module changed.
2456 (let ((mod (syntax->datum #'(private mod ...))))
2457 (values (remodulate #'exp mod)
2458 r w (source-annotation #'exp)
2461 (global-extend 'core 'if
2462 (lambda (e r w s mod)
2467 (expand #'test r w mod)
2468 (expand #'then r w mod)
2469 (build-void no-source)))
2473 (expand #'test r w mod)
2474 (expand #'then r w mod)
2475 (expand #'else r w mod))))))
2477 (global-extend 'begin 'begin '())
2479 (global-extend 'define 'define '())
2481 (global-extend 'define-syntax 'define-syntax '())
2482 (global-extend 'define-syntax-parameter 'define-syntax-parameter '())
2484 (global-extend 'eval-when 'eval-when '())
2486 (global-extend 'core 'syntax-case
2488 (define convert-pattern
2489 ;; accepts pattern & keys
2490 ;; returns $sc-dispatch pattern & ids
2491 (lambda (pattern keys ellipsis?)
2497 (lambda () (cvt* #'y n ids))
2500 (lambda () (cvt #'x n ids))
2502 (values (cons x y) ids))))))
2503 (_ (cvt p* n ids)))))
2505 (define (v-reverse x)
2506 (let loop ((r '()) (x x))
2509 (loop (cons (car x) r) (cdr x)))))
2515 ((bound-id-member? p keys)
2516 (values (vector 'free-id p) ids))
2520 (values 'any (cons (cons p n) ids))))
2523 (ellipsis? (syntax dots))
2525 (lambda () (cvt (syntax x) (fx+ n 1) ids))
2527 (values (if (eq? p 'any) 'each-any (vector 'each p))
2530 (ellipsis? (syntax dots))
2532 (lambda () (cvt* (syntax ys) n ids))
2535 (lambda () (cvt (syntax x) (+ n 1) ids))
2538 (lambda () (v-reverse ys))
2540 (values `#(each+ ,x ,ys ,e)
2544 (lambda () (cvt (syntax y) n ids))
2547 (lambda () (cvt (syntax x) n ids))
2549 (values (cons x y) ids))))))
2550 (() (values '() ids))
2553 (lambda () (cvt (syntax (x ...)) n ids))
2554 (lambda (p ids) (values (vector 'vector p) ids))))
2555 (x (values (vector 'atom (strip p empty-wrap)) ids))))))
2556 (cvt pattern 0 '())))
2558 (define build-dispatch-call
2559 (lambda (pvars exp y r mod)
2560 (let ((ids (map car pvars)) (levels (map cdr pvars)))
2561 (let ((labels (gen-labels ids)) (new-vars (map gen-var ids)))
2565 (list (build-simple-lambda no-source (map syntax->datum ids) #f new-vars '()
2569 (map (lambda (var level)
2570 (make-binding 'syntax `(,var . ,level)))
2574 (make-binding-wrap ids labels empty-wrap)
2579 (lambda (x keys clauses r pat fender exp mod)
2581 (lambda () (convert-pattern pat keys (lambda (e) (ellipsis? e r mod))))
2584 ((not (and-map (lambda (x) (not (ellipsis? (car x) r mod))) pvars))
2585 (syntax-violation 'syntax-case "misplaced ellipsis" pat))
2586 ((not (distinct-bound-ids? (map car pvars)))
2587 (syntax-violation 'syntax-case "duplicate pattern variable" pat))
2589 (let ((y (gen-var 'tmp)))
2590 ;; fat finger binding and references to temp variable y
2591 (build-call no-source
2592 (build-simple-lambda no-source (list 'tmp) #f (list y) '()
2593 (let ((y (build-lexical-reference 'value no-source
2595 (build-conditional no-source
2596 (syntax-case fender ()
2598 (_ (build-conditional no-source
2600 (build-dispatch-call pvars fender y r mod)
2601 (build-data no-source #f))))
2602 (build-dispatch-call pvars exp y r mod)
2603 (gen-syntax-case x keys clauses r mod))))
2604 (list (if (eq? p 'any)
2605 (build-primcall no-source 'list (list x))
2606 (build-primcall no-source '$sc-dispatch
2607 (list x (build-data no-source p)))))))))))))
2609 (define gen-syntax-case
2610 (lambda (x keys clauses r mod)
2612 (build-primcall no-source 'syntax-violation
2613 (list (build-data no-source #f)
2614 (build-data no-source
2615 "source expression failed to match any pattern")
2617 (syntax-case (car clauses) ()
2619 (if (and (id? #'pat)
2620 (and-map (lambda (x) (not (free-id=? #'pat x)))
2621 (cons #'(... ...) keys)))
2622 (if (free-id=? #'pat #'_)
2623 (expand #'exp r empty-wrap mod)
2624 (let ((labels (list (gen-label)))
2625 (var (gen-var #'pat)))
2626 (build-call no-source
2627 (build-simple-lambda
2628 no-source (list (syntax->datum #'pat)) #f (list var)
2632 (list (make-binding 'syntax `(,var . 0)))
2634 (make-binding-wrap #'(pat)
2638 (gen-clause x keys (cdr clauses) r
2639 #'pat #t #'exp mod)))
2641 (gen-clause x keys (cdr clauses) r
2642 #'pat #'fender #'exp mod))
2643 (_ (syntax-violation 'syntax-case "invalid clause"
2646 (lambda (e r w s mod)
2647 (let ((e (source-wrap e w s mod)))
2649 ((_ val (key ...) m ...)
2650 (if (and-map (lambda (x) (and (id? x) (not (ellipsis? x r mod))))
2652 (let ((x (gen-var 'tmp)))
2653 ;; fat finger binding and references to temp variable x
2655 (build-simple-lambda no-source (list 'tmp) #f (list x) '()
2656 (gen-syntax-case (build-lexical-reference 'value no-source
2658 #'(key ...) #'(m ...)
2661 (list (expand #'val r empty-wrap mod))))
2662 (syntax-violation 'syntax-case "invalid literals list" e))))))))
2664 ;; The portable macroexpand seeds expand-top's mode m with 'e (for
2665 ;; evaluating) and esew (which stands for "eval syntax expanders
2666 ;; when") with '(eval). In Chez Scheme, m is set to 'c instead of e
2667 ;; if we are compiling a file, and esew is set to
2668 ;; (eval-syntactic-expanders-when), which defaults to the list
2669 ;; '(compile load eval). This means that, by default, top-level
2670 ;; syntactic definitions are evaluated immediately after they are
2671 ;; expanded, and the expanded definitions are also residualized into
2672 ;; the object file if we are compiling a file.
2674 (lambda* (x #:optional (m 'e) (esew '(eval)))
2675 (expand-top-sequence (list x) null-env top-wrap #f m esew
2676 (cons 'hygiene (module-name (current-module))))))
2684 (make-syntax-object datum (syntax-object-wrap id)
2685 (syntax-object-module id))))
2688 ;; accepts any object, since syntax objects may consist partially
2689 ;; or entirely of unwrapped, nonsymbolic data
2691 (strip x empty-wrap)))
2694 (lambda (x) (source-annotation x)))
2696 (set! generate-temporaries
2698 (arg-check list? ls 'generate-temporaries)
2699 (let ((mod (cons 'hygiene (module-name (current-module)))))
2700 (map (lambda (x) (wrap (gensym "t-") top-wrap mod)) ls))))
2702 (set! free-identifier=?
2704 (arg-check nonsymbol-id? x 'free-identifier=?)
2705 (arg-check nonsymbol-id? y 'free-identifier=?)
2708 (set! bound-identifier=?
2710 (arg-check nonsymbol-id? x 'bound-identifier=?)
2711 (arg-check nonsymbol-id? y 'bound-identifier=?)
2714 (set! syntax-violation
2715 (lambda* (who message form #:optional subform)
2716 (arg-check (lambda (x) (or (not x) (string? x) (symbol? x)))
2717 who 'syntax-violation)
2718 (arg-check string? message 'syntax-violation)
2719 (throw 'syntax-error who message
2720 (or (source-annotation subform)
2721 (source-annotation form))
2722 (strip form empty-wrap)
2723 (and subform (strip subform empty-wrap)))))
2726 (define (syntax-module id)
2727 (arg-check nonsymbol-id? id 'syntax-module)
2728 (let ((mod (syntax-object-module id)))
2729 (and (not (equal? mod '(primitive)))
2732 (define* (syntax-local-binding id #:key (resolve-syntax-parameters? #t))
2733 (arg-check nonsymbol-id? id 'syntax-local-binding)
2734 (with-transformer-environment
2735 (lambda (e r w s rib mod)
2736 (define (strip-anti-mark w)
2737 (let ((ms (wrap-marks w)) (s (wrap-subst w)))
2738 (if (and (pair? ms) (eq? (car ms) the-anti-mark))
2739 ;; output is from original text
2740 (make-wrap (cdr ms) (if rib (cons rib (cdr s)) (cdr s)))
2741 ;; output introduced by macro
2742 (make-wrap ms (if rib (cons rib s) s)))))
2743 (call-with-values (lambda ()
2745 (syntax-object-expression id)
2746 (strip-anti-mark (syntax-object-wrap id))
2748 (syntax-object-module id)
2749 resolve-syntax-parameters?))
2750 (lambda (type value mod)
2752 ((lexical) (values 'lexical value))
2753 ((macro) (values 'macro value))
2754 ((syntax-parameter) (values 'syntax-parameter (car value)))
2755 ((syntax) (values 'pattern-variable value))
2756 ((displaced-lexical) (values 'displaced-lexical #f))
2758 (if (equal? mod '(primitive))
2759 (values 'primitive value)
2760 (values 'global (cons value (cdr mod)))))
2763 (make-syntax-object (syntax-object-expression value)
2764 (anti-mark (syntax-object-wrap value))
2765 (syntax-object-module value))))
2766 (else (values 'other #f))))))))
2768 (define (syntax-locally-bound-identifiers id)
2769 (arg-check nonsymbol-id? id 'syntax-locally-bound-identifiers)
2770 (locally-bound-identifiers (syntax-object-wrap id)
2771 (syntax-object-module id)))
2773 ;; Using define! instead of set! to avoid warnings at
2774 ;; compile-time, after the variables are stolen away into (system
2775 ;; syntax). See the end of boot-9.scm.
2777 (define! 'syntax-module syntax-module)
2778 (define! 'syntax-local-binding syntax-local-binding)
2779 (define! 'syntax-locally-bound-identifiers syntax-locally-bound-identifiers))
2781 ;; $sc-dispatch expects an expression and a pattern. If the expression
2782 ;; matches the pattern a list of the matching expressions for each
2783 ;; "any" is returned. Otherwise, #f is returned. (This use of #f will
2784 ;; not work on r4rs implementations that violate the ieee requirement
2785 ;; that #f and () be distinct.)
2787 ;; The expression is matched with the pattern as follows:
2789 ;; pattern: matches:
2792 ;; (<pattern>1 . <pattern>2) (<pattern>1 . <pattern>2)
2794 ;; #(free-id <key>) <key> with free-identifier=?
2795 ;; #(each <pattern>) (<pattern>*)
2796 ;; #(each+ p1 (p2_1 ... p2_n) p3) (p1* (p2_n ... p2_1) . p3)
2797 ;; #(vector <pattern>) (list->vector <pattern>)
2798 ;; #(atom <object>) <object> with "equal?"
2800 ;; Vector cops out to pair under assumption that vectors are rare. If
2801 ;; not, should convert to:
2802 ;; #(vector <pattern>*) #(<pattern>*)
2810 (let ((first (match (car e) p w '() mod)))
2812 (let ((rest (match-each (cdr e) p w mod)))
2813 (and rest (cons first rest))))))
2816 (match-each (syntax-object-expression e)
2818 (join-wraps w (syntax-object-wrap e))
2819 (syntax-object-module e)))
2823 (lambda (e x-pat y-pat z-pat w r mod)
2824 (let f ((e e) (w w))
2827 (call-with-values (lambda () (f (cdr e) w))
2828 (lambda (xr* y-pat r)
2831 (let ((xr (match (car e) x-pat w '() mod)))
2833 (values (cons xr xr*) y-pat r)
2838 (match (car e) (car y-pat) w r mod)))
2839 (values #f #f #f)))))
2841 (f (syntax-object-expression e) (join-wraps w e)))
2843 (values '() y-pat (match e z-pat w r mod)))))))
2845 (define match-each-any
2849 (let ((l (match-each-any (cdr e) w mod)))
2850 (and l (cons (wrap (car e) w mod) l))))
2853 (match-each-any (syntax-object-expression e)
2854 (join-wraps w (syntax-object-wrap e))
2863 ((eq? p 'any) (cons '() r))
2864 ((pair? p) (match-empty (car p) (match-empty (cdr p) r)))
2865 ((eq? p 'each-any) (cons '() r))
2867 (case (vector-ref p 0)
2868 ((each) (match-empty (vector-ref p 1) r))
2869 ((each+) (match-empty (vector-ref p 1)
2871 (reverse (vector-ref p 2))
2872 (match-empty (vector-ref p 3) r))))
2874 ((vector) (match-empty (vector-ref p 1) r)))))))
2878 (if (null? (car r*))
2880 (cons (map car r*) (combine (map cdr r*) r)))))
2883 (lambda (e p w r mod)
2885 ((null? p) (and (null? e) r))
2887 (and (pair? e) (match (car e) (car p) w
2888 (match (cdr e) (cdr p) w r mod)
2891 (let ((l (match-each-any e w mod))) (and l (cons l r))))
2893 (case (vector-ref p 0)
2896 (match-empty (vector-ref p 1) r)
2897 (let ((l (match-each e (vector-ref p 1) w mod)))
2899 (let collect ((l l))
2902 (cons (map car l) (collect (map cdr l)))))))))
2906 (match-each+ e (vector-ref p 1) (vector-ref p 2) (vector-ref p 3) w r mod))
2907 (lambda (xr* y-pat r)
2911 (match-empty (vector-ref p 1) r)
2912 (combine xr* r))))))
2913 ((free-id) (and (id? e) (free-id=? (wrap e w mod) (vector-ref p 1)) r))
2914 ((atom) (and (equal? (vector-ref p 1) (strip e w)) r))
2917 (match (vector->list e) (vector-ref p 1) w r mod))))))))
2920 (lambda (e p w r mod)
2924 ((eq? p 'any) (cons (wrap e w mod) r))
2927 (syntax-object-expression e)
2929 (join-wraps w (syntax-object-wrap e))
2931 (syntax-object-module e)))
2932 (else (match* e p w r mod)))))
2937 ((eq? p 'any) (list e))
2940 (match* (syntax-object-expression e)
2941 p (syntax-object-wrap e) '() (syntax-object-module e)))
2942 (else (match* e p empty-wrap '() #f))))))))
2945 (define-syntax with-syntax
2949 #'(let () e1 e2 ...))
2950 ((_ ((out in)) e1 e2 ...)
2951 #'(syntax-case in ()
2952 (out (let () e1 e2 ...))))
2953 ((_ ((out in) ...) e1 e2 ...)
2954 #'(syntax-case (list in ...) ()
2955 ((out ...) (let () e1 e2 ...)))))))
2957 (define-syntax syntax-error
2960 ;; Extended internal syntax which provides the original form
2961 ;; as the first operand, for improved error reporting.
2962 ((_ (keyword . operands) message arg ...)
2963 (string? (syntax->datum #'message))
2964 (syntax-violation (syntax->datum #'keyword)
2965 (string-join (cons (syntax->datum #'message)
2970 (and (syntax->datum #'keyword)
2971 #'(keyword . operands))))
2972 ;; Standard R7RS syntax
2973 ((_ message arg ...)
2974 (string? (syntax->datum #'message))
2975 #'(syntax-error (#f) message arg ...)))))
2977 (define-syntax syntax-rules
2979 (define (expand-clause clause)
2980 ;; Convert a 'syntax-rules' clause into a 'syntax-case' clause.
2981 (syntax-case clause (syntax-error)
2982 ;; If the template is a 'syntax-error' form, use the extended
2983 ;; internal syntax, which adds the original form as the first
2984 ;; operand for improved error reporting.
2985 (((keyword . pattern) (syntax-error message arg ...))
2986 (string? (syntax->datum #'message))
2987 #'((dummy . pattern) #'(syntax-error (dummy . pattern) message arg ...)))
2989 (((keyword . pattern) template)
2990 #'((dummy . pattern) #'template))))
2991 (define (expand-syntax-rules dots keys docstrings clauses)
2994 ((docstring ...) docstrings)
2995 ((((keyword . pattern) template) ...) clauses)
2996 ((clause ...) (map expand-clause clauses)))
2998 ((form #'(lambda (x)
2999 docstring ... ; optional docstring
3000 #((macro-type . syntax-rules)
3001 (patterns pattern ...)) ; embed patterns as procedure metadata
3002 (syntax-case x (k ...)
3005 (with-syntax ((dots dots))
3006 #'(with-ellipsis dots form))
3009 ((_ (k ...) ((keyword . pattern) template) ...)
3010 (expand-syntax-rules #f #'(k ...) #'() #'(((keyword . pattern) template) ...)))
3011 ((_ (k ...) docstring ((keyword . pattern) template) ...)
3012 (string? (syntax->datum #'docstring))
3013 (expand-syntax-rules #f #'(k ...) #'(docstring) #'(((keyword . pattern) template) ...)))
3014 ((_ dots (k ...) ((keyword . pattern) template) ...)
3015 (identifier? #'dots)
3016 (expand-syntax-rules #'dots #'(k ...) #'() #'(((keyword . pattern) template) ...)))
3017 ((_ dots (k ...) docstring ((keyword . pattern) template) ...)
3018 (and (identifier? #'dots) (string? (syntax->datum #'docstring)))
3019 (expand-syntax-rules #'dots #'(k ...) #'(docstring) #'(((keyword . pattern) template) ...))))))
3021 (define-syntax define-syntax-rule
3024 ((_ (name . pattern) template)
3025 #'(define-syntax name
3027 ((_ . pattern) template))))
3028 ((_ (name . pattern) docstring template)
3029 (string? (syntax->datum #'docstring))
3030 #'(define-syntax name
3033 ((_ . pattern) template)))))))
3038 ((let* ((x v) ...) e1 e2 ...)
3039 (and-map identifier? #'(x ...))
3040 (let f ((bindings #'((x v) ...)))
3041 (if (null? bindings)
3042 #'(let () e1 e2 ...)
3043 (with-syntax ((body (f (cdr bindings)))
3044 (binding (car bindings)))
3045 #'(let (binding) body))))))))
3047 (define-syntax quasiquote
3049 (define (quasi p lev)
3050 (syntax-case p (unquote quasiquote)
3054 (quasicons #'("quote" unquote) (quasi #'(p) (- lev 1)))))
3055 ((quasiquote p) (quasicons #'("quote" quasiquote) (quasi #'(p) (+ lev 1))))
3057 (syntax-case #'p (unquote unquote-splicing)
3060 (quasilist* #'(("value" p) ...) (quasi #'q lev))
3062 (quasicons #'("quote" unquote) (quasi #'(p ...) (- lev 1)))
3064 ((unquote-splicing p ...)
3066 (quasiappend #'(("value" p) ...) (quasi #'q lev))
3068 (quasicons #'("quote" unquote-splicing) (quasi #'(p ...) (- lev 1)))
3070 (_ (quasicons (quasi #'p lev) (quasi #'q lev)))))
3071 (#(x ...) (quasivector (vquasi #'(x ...) lev)))
3073 (define (vquasi p lev)
3076 (syntax-case #'p (unquote unquote-splicing)
3079 (quasilist* #'(("value" p) ...) (vquasi #'q lev))
3081 (quasicons #'("quote" unquote) (quasi #'(p ...) (- lev 1)))
3083 ((unquote-splicing p ...)
3085 (quasiappend #'(("value" p) ...) (vquasi #'q lev))
3088 #'("quote" unquote-splicing)
3089 (quasi #'(p ...) (- lev 1)))
3091 (_ (quasicons (quasi #'p lev) (vquasi #'q lev)))))
3092 (() #'("quote" ()))))
3093 (define (quasicons x y)
3094 (with-syntax ((x x) (y y))
3098 (("quote" dx) #'("quote" (dx . dy)))
3099 (_ (if (null? #'dy) #'("list" x) #'("list*" x y)))))
3100 (("list" . stuff) #'("list" x . stuff))
3101 (("list*" . stuff) #'("list*" x . stuff))
3102 (_ #'("list*" x y)))))
3103 (define (quasiappend x y)
3107 ((null? x) #'("quote" ()))
3108 ((null? (cdr x)) (car x))
3109 (else (with-syntax (((p ...) x)) #'("append" p ...)))))
3113 (else (with-syntax (((p ...) x) (y y)) #'("append" p ... y)))))))
3114 (define (quasilist* x y)
3118 (quasicons (car x) (f (cdr x))))))
3119 (define (quasivector x)
3121 (("quote" (x ...)) #'("quote" #(x ...)))
3123 (let f ((y x) (k (lambda (ls) #`("vector" #,@ls))))
3125 (("quote" (y ...)) (k #'(("quote" y) ...)))
3126 (("list" y ...) (k #'(y ...)))
3127 (("list*" y ... z) (f #'z (lambda (ls) (k (append #'(y ...) ls)))))
3128 (else #`("list->vector" #,x)))))))
3132 (("list" x ...) #`(list #,@(map emit #'(x ...))))
3133 ;; could emit list* for 3+ arguments if implementation supports
3136 (let f ((x* #'(x ...)))
3139 #`(cons #,(emit (car x*)) #,(f (cdr x*))))))
3140 (("append" x ...) #`(append #,@(map emit #'(x ...))))
3141 (("vector" x ...) #`(vector #,@(map emit #'(x ...))))
3142 (("list->vector" x) #`(list->vector #,(emit #'x)))
3146 ;; convert to intermediate language, combining introduced (but
3147 ;; not unquoted source) quote expressions where possible and
3148 ;; choosing optimal construction code otherwise, then emit
3149 ;; Scheme code corresponding to the intermediate language forms.
3150 ((_ e) (emit (quasi #'e 0)))))))
3152 (define-syntax include
3156 (let* ((p (open-input-file
3157 (cond ((absolute-file-name? fn)
3160 (in-vicinity dir fn))
3164 "relative file name only allowed when the include form is in a file"
3166 (enc (file-encoding p)))
3168 ;; Choose the input encoding deterministically.
3169 (set-port-encoding! p (or enc "UTF-8"))
3171 (let f ((x (read p))
3175 (close-input-port p)
3178 (cons (datum->syntax k x) result)))))))
3179 (let* ((src (syntax-source x))
3180 (file (and src (assq-ref src 'filename)))
3181 (dir (and (string? file) (dirname file))))
3184 (let ((fn (syntax->datum #'filename)))
3185 (with-syntax (((exp ...) (read-file fn dir #'filename)))
3186 #'(begin exp ...))))))))
3188 (define-syntax include-from-path
3192 (let ((fn (syntax->datum #'filename)))
3193 (with-syntax ((fn (datum->syntax
3195 (or (%search-load-path fn)
3196 (syntax-violation 'include-from-path
3197 "file not found in path"
3199 #'(include fn)))))))
3201 (define-syntax unquote
3203 (syntax-violation 'unquote
3204 "expression not valid outside of quasiquote"
3207 (define-syntax unquote-splicing
3209 (syntax-violation 'unquote-splicing
3210 "expression not valid outside of quasiquote"
3213 (define (make-variable-transformer proc)
3214 (if (procedure? proc)
3215 (let ((trans (lambda (x)
3216 #((macro-type . variable-transformer))
3218 (set-procedure-property! trans 'variable-transformer #t)
3220 (error "variable transformer not a procedure" proc)))
3222 (define-syntax identifier-syntax
3224 (syntax-case xx (set!)
3227 #((macro-type . identifier-syntax))
3233 #'(e x (... ...))))))
3234 ((_ (id exp1) ((set! var val) exp2))
3235 (and (identifier? #'id) (identifier? #'var))
3236 #'(make-variable-transformer
3238 #((macro-type . variable-transformer))
3239 (syntax-case x (set!)
3240 ((set! var val) #'exp2)
3241 ((id x (... ...)) #'(exp1 x (... ...)))
3242 (id (identifier? #'id) #'exp1))))))))
3244 (define-syntax define*
3247 ((_ (id . args) b0 b1 ...)
3248 #'(define id (lambda* args b0 b1 ...)))
3249 ((_ id val) (identifier? #'id)
3250 #'(define id val)))))