3 ;;;; Copyright (C) 2001, 2003, 2006, 2009, 2010, 2011,
4 ;;;; 2012, 2013, 2015 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)))
895 ;; This identifier aliased another; recurse to allow
896 ;; syntax-parameterize to override macro-introduced syntax
898 (resolve-identifier n w r mod resolve-syntax-parameters?))
900 ;; Resolved to a free variable that was introduced by this
901 ;; macro; continue to resolve this global by name.
902 (resolve-identifier (syntax-object-expression n)
903 (syntax-object-wrap n)
905 (syntax-object-module n)
906 resolve-syntax-parameters?))))
908 (resolve-global n (if (syntax-object? id)
909 (syntax-object-module id)
912 (resolve-lexical n (if (syntax-object? id)
913 (syntax-object-module id)
916 (error "unexpected id-var-name" id w n)))))
918 (define transformer-environment
921 (error "called outside the dynamic extent of a syntax transformer"))))
923 (define (with-transformer-environment k)
924 ((fluid-ref transformer-environment) k))
926 ;; free-id=? must be passed fully wrapped ids since (free-id=? x y)
927 ;; may be true even if (free-id=? (wrap x w) (wrap y w)) is not.
931 (let* ((mi (and (syntax-object? i) (syntax-object-module i)))
932 (mj (and (syntax-object? j) (syntax-object-module j)))
933 (ni (id-var-name i empty-wrap mi))
934 (nj (id-var-name j empty-wrap mj)))
935 (define (id-module-binding id mod)
939 (resolve-module (cdr mod))
940 ;; Either modules have not been booted, or we have a
941 ;; raw symbol coming in, which is possible.
945 ((syntax-object? ni) (free-id=? ni j))
946 ((syntax-object? nj) (free-id=? i nj))
948 ;; `i' is not lexically bound. Assert that `j' is free,
949 ;; and if so, compare their bindings, that they are either
950 ;; bound to the same variable, or both unbound and have
952 (and (eq? nj (id-sym-name j))
953 (let ((bi (id-module-binding i mi)))
955 (eq? bi (id-module-binding j mj))
956 (and (not (id-module-binding j mj))
958 (eq? (id-module-binding i mi) (id-module-binding j mj))))
960 ;; Otherwise `i' is bound, so check that `j' is bound, and
961 ;; bound to the same thing.
964 ;; bound-id=? may be passed unwrapped (or partially wrapped) ids as
965 ;; long as the missing portion of the wrap is common to both of the ids
966 ;; since (bound-id=? x y) iff (bound-id=? (wrap x w) (wrap y w))
970 (if (and (syntax-object? i) (syntax-object? j))
971 (and (eq? (syntax-object-expression i)
972 (syntax-object-expression j))
973 (same-marks? (wrap-marks (syntax-object-wrap i))
974 (wrap-marks (syntax-object-wrap j))))
977 ;; "valid-bound-ids?" returns #t if it receives a list of distinct ids.
978 ;; valid-bound-ids? may be passed unwrapped (or partially wrapped) ids
979 ;; as long as the missing portion of the wrap is common to all of the
982 (define valid-bound-ids?
984 (and (let all-ids? ((ids ids))
987 (all-ids? (cdr ids)))))
988 (distinct-bound-ids? ids))))
990 ;; distinct-bound-ids? expects a list of ids and returns #t if there are
991 ;; no duplicates. It is quadratic on the length of the id list; long
992 ;; lists could be sorted to make it more efficient. distinct-bound-ids?
993 ;; may be passed unwrapped (or partially wrapped) ids as long as the
994 ;; missing portion of the wrap is common to all of the ids.
996 (define distinct-bound-ids?
998 (let distinct? ((ids ids))
1000 (and (not (bound-id-member? (car ids) (cdr ids)))
1001 (distinct? (cdr ids)))))))
1003 (define bound-id-member?
1005 (and (not (null? list))
1006 (or (bound-id=? x (car list))
1007 (bound-id-member? x (cdr list))))))
1009 ;; wrapping expressions and identifiers
1012 (lambda (x w defmod)
1014 ((and (null? (wrap-marks w)) (null? (wrap-subst w))) x)
1017 (syntax-object-expression x)
1018 (join-wraps w (syntax-object-wrap x))
1019 (syntax-object-module x)))
1021 (else (make-syntax-object x w defmod)))))
1024 (lambda (x w s defmod)
1025 (wrap (decorate-source x s) w defmod)))
1029 (define expand-sequence
1030 (lambda (body r w s mod)
1032 (let dobody ((body body) (r r) (w w) (mod mod))
1035 (let ((first (expand (car body) r w mod)))
1036 (cons first (dobody (cdr body) r w mod))))))))
1038 ;; At top-level, we allow mixed definitions and expressions. Like
1039 ;; expand-body we expand in two passes.
1041 ;; First, from left to right, we expand just enough to know what
1042 ;; expressions are definitions, syntax definitions, and splicing
1043 ;; statements (`begin'). If we anything needs evaluating at
1044 ;; expansion-time, it is expanded directly.
1046 ;; Otherwise we collect expressions to expand, in thunks, and then
1047 ;; expand them all at the end. This allows all syntax expanders
1048 ;; visible in a toplevel sequence to be visible during the
1049 ;; expansions of all normal definitions and expressions in the
1052 (define expand-top-sequence
1053 (lambda (body r w s m esew mod)
1054 (let* ((r (cons '("placeholder" . (placeholder)) r))
1055 (ribcage (make-empty-ribcage))
1056 (w (make-wrap (wrap-marks w) (cons ribcage (wrap-subst w)))))
1057 (define (record-definition! id var)
1058 (let ((mod (cons 'hygiene (module-name (current-module)))))
1059 ;; Ribcages map symbol+marks to names, mostly for
1060 ;; resolving lexicals. Here to add a mapping for toplevel
1061 ;; definitions we also need to match the module. So, we
1062 ;; put it in the name instead, and make id-var-name handle
1063 ;; the special case of names that are pairs. See the
1064 ;; comments in id-var-name for more.
1065 (extend-ribcage! ribcage id
1066 (cons (syntax-object-module id)
1067 (wrap var top-wrap mod)))))
1068 (define (macro-introduced-identifier? id)
1069 (not (equal? (wrap-marks (syntax-object-wrap id)) '(top))))
1070 (define (fresh-derived-name id orig-form)
1072 (syntax-object-expression id)
1075 ;; FIXME: `hash' currently stops descending into nested
1076 ;; data at some point, so it's less unique than we would
1077 ;; like. Also this encodes hash values into the ABI of
1078 ;; compiled modules; a problem?
1080 (hash (syntax->datum orig-form) most-positive-fixnum)
1082 (define (parse body r w s m esew mod)
1083 (let lp ((body body) (exps '()))
1087 (append (parse1 (car body) r w s m esew mod)
1089 (define (parse1 x r w s m esew mod)
1092 (syntax-type x r w (source-annotation x) ribcage mod #f))
1093 (lambda (type value form e w s mod)
1096 (let* ((id (wrap value w mod))
1098 (var (if (macro-introduced-identifier? id)
1099 (fresh-derived-name id x)
1100 (syntax-object-expression id))))
1101 (record-definition! id var)
1104 (let ((x (build-global-definition s var (expand e r w mod))))
1105 (top-level-eval-hook x mod)
1108 (lambda () (resolve-identifier id empty-wrap r mod #t))
1109 (lambda (type* value* mod*)
1110 ;; If the identifier to be bound is currently bound to a
1111 ;; macro, then immediately discard that binding.
1112 (if (eq? type* 'macro)
1113 (top-level-eval-hook (build-global-definition
1114 s var (build-void s))
1117 (build-global-definition s var (expand e r w mod)))))))))
1118 ((define-syntax-form define-syntax-parameter-form)
1119 (let* ((id (wrap value w mod))
1121 (var (if (macro-introduced-identifier? id)
1122 (fresh-derived-name id x)
1123 (syntax-object-expression id))))
1124 (record-definition! id var)
1128 ((memq 'compile esew)
1129 (let ((e (expand-install-global var type (expand e r w mod))))
1130 (top-level-eval-hook e mod)
1131 (if (memq 'load esew)
1132 (list (lambda () e))
1136 (expand-install-global var type (expand e r w mod)))))
1139 (let ((e (expand-install-global var type (expand e r w mod))))
1140 (top-level-eval-hook e mod)
1141 (list (lambda () e))))
1143 (if (memq 'eval esew)
1144 (top-level-eval-hook
1145 (expand-install-global var type (expand e r w mod))
1151 (parse #'(e1 ...) r w s m esew mod))))
1152 ((local-syntax-form)
1153 (expand-local-syntax value e r w s mod
1154 (lambda (forms r w s mod)
1155 (parse forms r w s m esew mod))))
1158 ((_ (x ...) e1 e2 ...)
1159 (let ((when-list (parse-when-list e #'(x ...)))
1160 (body #'(e1 e2 ...)))
1161 (define (recurse m esew)
1162 (parse body r w s m esew mod))
1165 (if (memq 'eval when-list)
1166 (recurse (if (memq 'expand when-list) 'c&e 'e)
1169 (if (memq 'expand when-list)
1170 (top-level-eval-hook
1171 (expand-top-sequence body r w s 'e '(eval) mod)
1174 ((memq 'load when-list)
1175 (if (or (memq 'compile when-list)
1176 (memq 'expand when-list)
1177 (and (eq? m 'c&e) (memq 'eval when-list)))
1178 (recurse 'c&e '(compile load))
1179 (if (memq m '(c c&e))
1180 (recurse 'c '(load))
1182 ((or (memq 'compile when-list)
1183 (memq 'expand when-list)
1184 (and (eq? m 'c&e) (memq 'eval when-list)))
1185 (top-level-eval-hook
1186 (expand-top-sequence body r w s 'e '(eval) mod)
1194 (let ((x (expand-expr type value form e r w s mod)))
1195 (top-level-eval-hook x mod)
1198 (expand-expr type value form e r w s mod)))))))))
1199 (let ((exps (map (lambda (x) (x))
1200 (reverse (parse body r w s m esew mod)))))
1203 (build-sequence s exps))))))
1205 (define expand-install-global
1206 (lambda (name type e)
1207 (build-global-definition
1212 'make-syntax-transformer
1213 (if (eq? type 'define-syntax-parameter-form)
1214 (list (build-data no-source name)
1215 (build-data no-source 'syntax-parameter)
1216 (build-primcall no-source 'list (list e)))
1217 (list (build-data no-source name)
1218 (build-data no-source 'macro)
1221 (define parse-when-list
1222 (lambda (e when-list)
1223 ;; `when-list' is syntax'd version of list of situations. We
1224 ;; could match these keywords lexically, via free-id=?, but then
1225 ;; we twingle the definition of eval-when to the bindings of
1226 ;; eval, load, expand, and compile, which is totally unintended.
1227 ;; So do a symbolic match instead.
1228 (let ((result (strip when-list empty-wrap)))
1229 (let lp ((l result))
1232 (if (memq (car l) '(compile load eval expand))
1234 (syntax-violation 'eval-when "invalid situation" e
1237 ;; syntax-type returns seven values: type, value, form, e, w, s, and
1238 ;; mod. The first two are described in the table below.
1240 ;; type value explanation
1241 ;; -------------------------------------------------------------------
1242 ;; core procedure core singleton
1243 ;; core-form procedure core form
1244 ;; module-ref procedure @ or @@ singleton
1245 ;; lexical name lexical variable reference
1246 ;; global name global variable reference
1247 ;; begin none begin keyword
1248 ;; define none define keyword
1249 ;; define-syntax none define-syntax keyword
1250 ;; define-syntax-parameter none define-syntax-parameter keyword
1251 ;; local-syntax rec? letrec-syntax/let-syntax keyword
1252 ;; eval-when none eval-when keyword
1253 ;; syntax level pattern variable
1254 ;; displaced-lexical none displaced lexical identifier
1255 ;; lexical-call name call to lexical variable
1256 ;; global-call name call to global variable
1257 ;; primitive-call name call to primitive
1258 ;; call none any other call
1259 ;; begin-form none begin expression
1260 ;; define-form id variable definition
1261 ;; define-syntax-form id syntax definition
1262 ;; define-syntax-parameter-form id syntax parameter definition
1263 ;; local-syntax-form rec? syntax definition
1264 ;; eval-when-form none eval-when form
1265 ;; constant none self-evaluating datum
1266 ;; other none anything else
1268 ;; form is the entire form. For definition forms (define-form,
1269 ;; define-syntax-form, and define-syntax-parameter-form), e is the
1270 ;; rhs expression. For all others, e is the entire form. w is the
1271 ;; wrap for both form and e. s is the source for the entire form.
1272 ;; mod is the module for both form and e.
1274 ;; syntax-type expands macros and unwraps as necessary to get to one
1275 ;; of the forms above. It also parses definition forms, although
1276 ;; perhaps this should be done by the consumer.
1279 (lambda (e r w s rib mod for-car?)
1282 (call-with-values (lambda () (resolve-identifier e w r mod #t))
1283 (lambda (type value mod*)
1287 (values type value e e w s mod)
1288 (syntax-type (expand-macro value e r w s rib mod)
1289 r empty-wrap s rib mod #f)))
1291 ;; Toplevel definitions may resolve to bindings with
1292 ;; different names or in different modules.
1293 (values type value e value w s mod*))
1294 (else (values type value e e w s mod))))))
1296 (let ((first (car e)))
1298 (lambda () (syntax-type first r w s rib mod #t))
1299 (lambda (ftype fval fform fe fw fs fmod)
1302 (values 'lexical-call fval e e w s mod))
1304 (if (equal? fmod '(primitive))
1305 (values 'primitive-call fval e e w s mod)
1306 ;; If we got here via an (@@ ...) expansion, we
1307 ;; need to make sure the fmod information is
1308 ;; propagated back correctly -- hence this
1310 (values 'global-call (make-syntax-object fval w fmod)
1313 (syntax-type (expand-macro fval e r w s rib mod)
1314 r empty-wrap s rib mod for-car?))
1316 (call-with-values (lambda () (fval e r w mod))
1317 (lambda (e r w s mod)
1318 (syntax-type e r w s rib mod for-car?))))
1320 (values 'core-form fval e e w s mod))
1322 (values 'local-syntax-form fval e e w s mod))
1324 (values 'begin-form #f e e w s mod))
1326 (values 'eval-when-form #f e e w s mod))
1331 (values 'define-form #'name e #'val w s mod))
1332 ((_ (name . args) e1 e2 ...)
1334 (valid-bound-ids? (lambda-var-list #'args)))
1335 ;; need lambda here...
1336 (values 'define-form (wrap #'name w mod)
1339 (cons #'lambda (wrap #'(args e1 e2 ...) w mod))
1344 (values 'define-form (wrap #'name w mod)
1347 empty-wrap s mod))))
1352 (values 'define-syntax-form #'name e #'val w s mod))))
1353 ((define-syntax-parameter)
1357 (values 'define-syntax-parameter-form #'name e #'val w s mod))))
1359 (values 'call #f e e w s mod)))))))
1361 (syntax-type (syntax-object-expression e)
1363 (join-wraps w (syntax-object-wrap e))
1364 (or (source-annotation e) s) rib
1365 (or (syntax-object-module e) mod) for-car?))
1366 ((self-evaluating? e) (values 'constant #f e e w s mod))
1367 (else (values 'other #f e e w s mod)))))
1372 (lambda () (syntax-type e r w (source-annotation e) #f mod #f))
1373 (lambda (type value form e w s mod)
1374 (expand-expr type value form e r w s mod)))))
1377 (lambda (type value form e r w s mod)
1380 (build-lexical-reference 'value s e value))
1382 ;; apply transformer
1383 (value e r w s mod))
1385 (call-with-values (lambda () (value e r w mod))
1386 (lambda (e r w s mod)
1387 (expand e r w mod))))
1391 (build-lexical-reference 'fun (source-annotation id)
1392 (if (syntax-object? id)
1399 (build-global-reference (source-annotation (car e))
1400 (if (syntax-object? value)
1401 (syntax-object-expression value)
1403 (if (syntax-object? value)
1404 (syntax-object-module value)
1412 (map (lambda (e) (expand e r w mod))
1414 ((constant) (build-data s (strip (source-wrap e w s mod) empty-wrap)))
1415 ((global) (build-global-reference s value mod))
1416 ((call) (expand-call (expand (car e) r w mod) e r w s mod))
1419 ((_ e1 e2 ...) (expand-sequence #'(e1 e2 ...) r w s mod))
1421 (syntax-violation #f "sequence of zero expressions"
1422 (source-wrap e w s mod)))))
1423 ((local-syntax-form)
1424 (expand-local-syntax value e r w s mod expand-sequence))
1427 ((_ (x ...) e1 e2 ...)
1428 (let ((when-list (parse-when-list e #'(x ...))))
1429 (if (memq 'eval when-list)
1430 (expand-sequence #'(e1 e2 ...) r w s mod)
1432 ((define-form define-syntax-form define-syntax-parameter-form)
1433 (syntax-violation #f "definition in expression context, where definitions are not allowed,"
1434 (source-wrap form w s mod)))
1436 (syntax-violation #f "reference to pattern variable outside syntax form"
1437 (source-wrap e w s mod)))
1438 ((displaced-lexical)
1439 (syntax-violation #f "reference to identifier outside its scope"
1440 (source-wrap e w s mod)))
1441 (else (syntax-violation #f "unexpected syntax"
1442 (source-wrap e w s mod))))))
1445 (lambda (x e r w s mod)
1449 (map (lambda (e) (expand e r w mod)) #'(e1 ...)))))))
1451 ;; (What follows is my interpretation of what's going on here -- Andy)
1453 ;; A macro takes an expression, a tree, the leaves of which are identifiers
1454 ;; and datums. Identifiers are symbols along with a wrap and a module. For
1455 ;; efficiency, subtrees that share wraps and modules may be grouped as one
1458 ;; Going into the expansion, the expression is given an anti-mark, which
1459 ;; logically propagates to all leaves. Then, in the new expression returned
1460 ;; from the transfomer, if we see an expression with an anti-mark, we know it
1461 ;; pertains to the original expression; conversely, expressions without the
1462 ;; anti-mark are known to be introduced by the transformer.
1464 ;; OK, good until now. We know this algorithm does lexical scoping
1465 ;; appropriately because it's widely known in the literature, and psyntax is
1466 ;; widely used. But what about modules? Here we're on our own. What we do is
1467 ;; to mark the module of expressions produced by a macro as pertaining to the
1468 ;; module that was current when the macro was defined -- that is, free
1469 ;; identifiers introduced by a macro are scoped in the macro's module, not in
1470 ;; the expansion's module. Seems to work well.
1472 ;; The only wrinkle is when we want a macro to expand to code in another
1473 ;; module, as is the case for the r6rs `library' form -- the body expressions
1474 ;; should be scoped relative the the new module, the one defined by the macro.
1475 ;; For that, use `(@@ mod-name body)'.
1477 ;; Part of the macro output will be from the site of the macro use and part
1478 ;; from the macro definition. We allow source information from the macro use
1479 ;; to pass through, but we annotate the parts coming from the macro with the
1480 ;; source location information corresponding to the macro use. It would be
1481 ;; really nice if we could also annotate introduced expressions with the
1482 ;; locations corresponding to the macro definition, but that is not yet
1484 (define expand-macro
1485 (lambda (p e r w s rib mod)
1486 (define rebuild-macro-output
1490 (cons (rebuild-macro-output (car x) m)
1491 (rebuild-macro-output (cdr x) m))
1494 (let ((w (syntax-object-wrap x)))
1495 (let ((ms (wrap-marks w)) (ss (wrap-subst w)))
1496 (if (and (pair? ms) (eq? (car ms) the-anti-mark))
1497 ;; output is from original text
1499 (syntax-object-expression x)
1500 (make-wrap (cdr ms) (if rib (cons rib (cdr ss)) (cdr ss)))
1501 (syntax-object-module x))
1502 ;; output introduced by macro
1504 (decorate-source (syntax-object-expression x) s)
1505 (make-wrap (cons m ms)
1507 (cons rib (cons 'shift ss))
1509 (syntax-object-module x))))))
1512 (let* ((n (vector-length x))
1513 (v (decorate-source (make-vector n) s)))
1514 (do ((i 0 (fx+ i 1)))
1517 (rebuild-macro-output (vector-ref x i) m)))))
1519 (syntax-violation #f "encountered raw symbol in macro output"
1520 (source-wrap e w (wrap-subst w) mod) x))
1521 (else (decorate-source x s)))))
1522 (with-fluids ((transformer-environment
1523 (lambda (k) (k e r w s rib mod))))
1524 (rebuild-macro-output (p (source-wrap e (anti-mark w) s mod))
1528 ;; In processing the forms of the body, we create a new, empty wrap.
1529 ;; This wrap is augmented (destructively) each time we discover that
1530 ;; the next form is a definition. This is done:
1532 ;; (1) to allow the first nondefinition form to be a call to
1533 ;; one of the defined ids even if the id previously denoted a
1534 ;; definition keyword or keyword for a macro expanding into a
1536 ;; (2) to prevent subsequent definition forms (but unfortunately
1537 ;; not earlier ones) and the first nondefinition form from
1538 ;; confusing one of the bound identifiers for an auxiliary
1540 ;; (3) so that we do not need to restart the expansion of the
1541 ;; first nondefinition form, which is problematic anyway
1542 ;; since it might be the first element of a begin that we
1543 ;; have just spliced into the body (meaning if we restarted,
1544 ;; we'd really need to restart with the begin or the macro
1545 ;; call that expanded into the begin, and we'd have to give
1546 ;; up allowing (begin <defn>+ <expr>+), which is itself
1547 ;; problematic since we don't know if a begin contains only
1548 ;; definitions until we've expanded it).
1550 ;; Before processing the body, we also create a new environment
1551 ;; containing a placeholder for the bindings we will add later and
1552 ;; associate this environment with each form. In processing a
1553 ;; let-syntax or letrec-syntax, the associated environment may be
1554 ;; augmented with local keyword bindings, so the environment may
1555 ;; be different for different forms in the body. Once we have
1556 ;; gathered up all of the definitions, we evaluate the transformer
1557 ;; expressions and splice into r at the placeholder the new variable
1558 ;; and keyword bindings. This allows let-syntax or letrec-syntax
1559 ;; forms local to a portion or all of the body to shadow the
1560 ;; definition bindings.
1562 ;; Subforms of a begin, let-syntax, or letrec-syntax are spliced
1565 ;; outer-form is fully wrapped w/source
1566 (lambda (body outer-form r w mod)
1567 (let* ((r (cons '("placeholder" . (placeholder)) r))
1568 (ribcage (make-empty-ribcage))
1569 (w (make-wrap (wrap-marks w) (cons ribcage (wrap-subst w)))))
1570 (let parse ((body (map (lambda (x) (cons r (wrap x w mod))) body))
1571 (ids '()) (labels '())
1572 (var-ids '()) (vars '()) (vals '()) (bindings '()))
1574 (syntax-violation #f "no expressions in body" outer-form)
1575 (let ((e (cdar body)) (er (caar body)))
1577 (lambda () (syntax-type e er empty-wrap (source-annotation e) ribcage mod #f))
1578 (lambda (type value form e w s mod)
1581 (let ((id (wrap value w mod)) (label (gen-label)))
1582 (let ((var (gen-var id)))
1583 (extend-ribcage! ribcage id label)
1585 (cons id ids) (cons label labels)
1587 (cons var vars) (cons (cons er (wrap e w mod)) vals)
1588 (cons (make-binding 'lexical var) bindings)))))
1589 ((define-syntax-form)
1590 (let ((id (wrap value w mod))
1592 (trans-r (macros-only-env er)))
1593 (extend-ribcage! ribcage id label)
1594 ;; As required by R6RS, evaluate the right-hand-sides of internal
1595 ;; syntax definition forms and add their transformers to the
1596 ;; compile-time environment immediately, so that the newly-defined
1597 ;; keywords may be used in definition context within the same
1599 (set-cdr! r (extend-env
1603 (eval-local-transformer
1604 (expand e trans-r w mod)
1607 (parse (cdr body) (cons id ids) labels var-ids vars vals bindings)))
1608 ((define-syntax-parameter-form)
1609 ;; Same as define-syntax-form, but different format of the binding.
1610 (let ((id (wrap value w mod))
1612 (trans-r (macros-only-env er)))
1613 (extend-ribcage! ribcage id label)
1614 (set-cdr! r (extend-env
1618 (list (eval-local-transformer
1619 (expand e trans-r w mod)
1622 (parse (cdr body) (cons id ids) labels var-ids vars vals bindings)))
1626 (parse (let f ((forms #'(e1 ...)))
1629 (cons (cons er (wrap (car forms) w mod))
1631 ids labels var-ids vars vals bindings))))
1632 ((local-syntax-form)
1633 (expand-local-syntax value e er w s mod
1634 (lambda (forms er w s mod)
1635 (parse (let f ((forms forms))
1638 (cons (cons er (wrap (car forms) w mod))
1640 ids labels var-ids vars vals bindings))))
1641 (else ; found a non-definition
1643 (build-sequence no-source
1645 (expand (cdr x) (car x) empty-wrap mod))
1646 (cons (cons er (source-wrap e w s mod))
1649 (if (not (valid-bound-ids? ids))
1651 #f "invalid or duplicate identifier in definition"
1653 (set-cdr! r (extend-env labels bindings (cdr r)))
1654 (build-letrec no-source #t
1655 (reverse (map syntax->datum var-ids))
1658 (expand (cdr x) (car x) empty-wrap mod))
1660 (build-sequence no-source
1662 (expand (cdr x) (car x) empty-wrap mod))
1663 (cons (cons er (source-wrap e w s mod))
1664 (cdr body)))))))))))))))))
1666 (define expand-local-syntax
1667 (lambda (rec? e r w s mod k)
1669 ((_ ((id val) ...) e1 e2 ...)
1670 (let ((ids #'(id ...)))
1671 (if (not (valid-bound-ids? ids))
1672 (syntax-violation #f "duplicate bound keyword" e)
1673 (let ((labels (gen-labels ids)))
1674 (let ((new-w (make-binding-wrap ids labels w)))
1678 (let ((w (if rec? new-w w))
1679 (trans-r (macros-only-env r)))
1681 (make-binding 'macro
1682 (eval-local-transformer
1683 (expand x trans-r w mod)
1690 (_ (syntax-violation #f "bad local syntax definition"
1691 (source-wrap e w s mod))))))
1693 (define eval-local-transformer
1694 (lambda (expanded mod)
1695 (let ((p (local-eval-hook expanded mod)))
1698 (syntax-violation #f "nonprocedure transformer" p)))))
1702 (build-void no-source)))
1706 (and (nonsymbol-id? e)
1707 ;; If there is a binding for the special identifier
1708 ;; #{ $sc-ellipsis }# in the lexical environment of E,
1709 ;; and if the associated binding type is 'ellipsis',
1710 ;; then the binding's value specifies the custom ellipsis
1711 ;; identifier within that lexical environment, and the
1712 ;; comparison is done using 'bound-id=?'.
1714 (lambda () (resolve-identifier
1715 (make-syntax-object '#{ $sc-ellipsis }#
1716 (syntax-object-wrap e)
1717 (syntax-object-module e))
1718 empty-wrap r mod #f))
1719 (lambda (type value mod)
1720 (if (eq? type 'ellipsis)
1721 (bound-id=? e value)
1722 (free-id=? e #'(... ...))))))))
1724 (define lambda-formals
1726 (define (req args rreq)
1727 (syntax-case args ()
1729 (check (reverse rreq) #f))
1731 (req #'b (cons #'a rreq)))
1733 (check (reverse rreq) #'r))
1735 (syntax-violation 'lambda "invalid argument list" orig-args args))))
1736 (define (check req rest)
1738 ((distinct-bound-ids? (if rest (cons rest req) req))
1739 (values req #f rest #f))
1741 (syntax-violation 'lambda "duplicate identifier in argument list"
1743 (req orig-args '())))
1745 (define expand-simple-lambda
1746 (lambda (e r w s mod req rest meta body)
1747 (let* ((ids (if rest (append req (list rest)) req))
1748 (vars (map gen-var ids))
1749 (labels (gen-labels ids)))
1750 (build-simple-lambda
1752 (map syntax->datum req) (and rest (syntax->datum rest)) vars
1754 (expand-body body (source-wrap e w s mod)
1755 (extend-var-env labels vars r)
1756 (make-binding-wrap ids labels w)
1759 (define lambda*-formals
1761 (define (req args rreq)
1762 (syntax-case args ()
1764 (check (reverse rreq) '() #f '()))
1766 (req #'b (cons #'a rreq)))
1767 ((a . b) (eq? (syntax->datum #'a) #:optional)
1768 (opt #'b (reverse rreq) '()))
1769 ((a . b) (eq? (syntax->datum #'a) #:key)
1770 (key #'b (reverse rreq) '() '()))
1771 ((a b) (eq? (syntax->datum #'a) #:rest)
1772 (rest #'b (reverse rreq) '() '()))
1774 (rest #'r (reverse rreq) '() '()))
1776 (syntax-violation 'lambda* "invalid argument list" orig-args args))))
1777 (define (opt args req ropt)
1778 (syntax-case args ()
1780 (check req (reverse ropt) #f '()))
1782 (opt #'b req (cons #'(a #f) ropt)))
1783 (((a init) . b) (id? #'a)
1784 (opt #'b req (cons #'(a init) ropt)))
1785 ((a . b) (eq? (syntax->datum #'a) #:key)
1786 (key #'b req (reverse ropt) '()))
1787 ((a b) (eq? (syntax->datum #'a) #:rest)
1788 (rest #'b req (reverse ropt) '()))
1790 (rest #'r req (reverse ropt) '()))
1792 (syntax-violation 'lambda* "invalid optional argument list"
1794 (define (key args req opt rkey)
1795 (syntax-case args ()
1797 (check req opt #f (cons #f (reverse rkey))))
1799 (with-syntax ((k (symbol->keyword (syntax->datum #'a))))
1800 (key #'b req opt (cons #'(k a #f) rkey))))
1801 (((a init) . b) (id? #'a)
1802 (with-syntax ((k (symbol->keyword (syntax->datum #'a))))
1803 (key #'b req opt (cons #'(k a init) rkey))))
1804 (((a init k) . b) (and (id? #'a)
1805 (keyword? (syntax->datum #'k)))
1806 (key #'b req opt (cons #'(k a init) rkey)))
1807 ((aok) (eq? (syntax->datum #'aok) #:allow-other-keys)
1808 (check req opt #f (cons #t (reverse rkey))))
1809 ((aok a b) (and (eq? (syntax->datum #'aok) #:allow-other-keys)
1810 (eq? (syntax->datum #'a) #:rest))
1811 (rest #'b req opt (cons #t (reverse rkey))))
1812 ((aok . r) (and (eq? (syntax->datum #'aok) #:allow-other-keys)
1814 (rest #'r req opt (cons #t (reverse rkey))))
1815 ((a b) (eq? (syntax->datum #'a) #:rest)
1816 (rest #'b req opt (cons #f (reverse rkey))))
1818 (rest #'r req opt (cons #f (reverse rkey))))
1820 (syntax-violation 'lambda* "invalid keyword argument list"
1822 (define (rest args req opt kw)
1823 (syntax-case args ()
1825 (check req opt #'r kw))
1827 (syntax-violation 'lambda* "invalid rest argument"
1829 (define (check req opt rest kw)
1831 ((distinct-bound-ids?
1832 (append req (map car opt) (if rest (list rest) '())
1833 (if (pair? kw) (map cadr (cdr kw)) '())))
1834 (values req opt rest kw))
1836 (syntax-violation 'lambda* "duplicate identifier in argument list"
1838 (req orig-args '())))
1840 (define expand-lambda-case
1841 (lambda (e r w s mod get-formals clauses)
1842 (define (parse-req req opt rest kw body)
1843 (let ((vars (map gen-var req))
1844 (labels (gen-labels req)))
1845 (let ((r* (extend-var-env labels vars r))
1846 (w* (make-binding-wrap req labels w)))
1847 (parse-opt (map syntax->datum req)
1848 opt rest kw body (reverse vars) r* w* '() '()))))
1849 (define (parse-opt req opt rest kw body vars r* w* out inits)
1852 (syntax-case (car opt) ()
1854 (let* ((v (gen-var #'id))
1855 (l (gen-labels (list v)))
1856 (r** (extend-var-env l (list v) r*))
1857 (w** (make-binding-wrap (list #'id) l w*)))
1858 (parse-opt req (cdr opt) rest kw body (cons v vars)
1859 r** w** (cons (syntax->datum #'id) out)
1860 (cons (expand #'i r* w* mod) inits))))))
1862 (let* ((v (gen-var rest))
1863 (l (gen-labels (list v)))
1864 (r* (extend-var-env l (list v) r*))
1865 (w* (make-binding-wrap (list rest) l w*)))
1866 (parse-kw req (if (pair? out) (reverse out) #f)
1867 (syntax->datum rest)
1868 (if (pair? kw) (cdr kw) kw)
1869 body (cons v vars) r* w*
1870 (if (pair? kw) (car kw) #f)
1873 (parse-kw req (if (pair? out) (reverse out) #f) #f
1874 (if (pair? kw) (cdr kw) kw)
1876 (if (pair? kw) (car kw) #f)
1878 (define (parse-kw req opt rest kw body vars r* w* aok out inits)
1881 (syntax-case (car kw) ()
1883 (let* ((v (gen-var #'id))
1884 (l (gen-labels (list v)))
1885 (r** (extend-var-env l (list v) r*))
1886 (w** (make-binding-wrap (list #'id) l w*)))
1887 (parse-kw req opt rest (cdr kw) body (cons v vars)
1889 (cons (list (syntax->datum #'k)
1890 (syntax->datum #'id)
1893 (cons (expand #'i r* w* mod) inits))))))
1895 (parse-body req opt rest
1896 (if (or aok (pair? out)) (cons aok (reverse out)) #f)
1897 body (reverse vars) r* w* (reverse inits) '()))))
1898 (define (parse-body req opt rest kw body vars r* w* inits meta)
1899 (syntax-case body ()
1900 ((docstring e1 e2 ...) (string? (syntax->datum #'docstring))
1901 (parse-body req opt rest kw #'(e1 e2 ...) vars r* w* inits
1904 . ,(syntax->datum #'docstring))))))
1905 ((#((k . v) ...) e1 e2 ...)
1906 (parse-body req opt rest kw #'(e1 e2 ...) vars r* w* inits
1907 (append meta (syntax->datum #'((k . v) ...)))))
1909 (values meta req opt rest kw inits vars
1910 (expand-body #'(e1 e2 ...) (source-wrap e w s mod)
1913 (syntax-case clauses ()
1914 (() (values '() #f))
1915 (((args e1 e2 ...) (args* e1* e2* ...) ...)
1916 (call-with-values (lambda () (get-formals #'args))
1917 (lambda (req opt rest kw)
1918 (call-with-values (lambda ()
1919 (parse-req req opt rest kw #'(e1 e2 ...)))
1920 (lambda (meta req opt rest kw inits vars body)
1923 (expand-lambda-case e r w s mod get-formals
1924 #'((args* e1* e2* ...) ...)))
1925 (lambda (meta* else*)
1928 (build-lambda-case s req opt rest kw inits vars
1929 body else*))))))))))))
1933 ;; strips syntax-objects down to top-wrap
1935 ;; since only the head of a list is annotated by the reader, not each pair
1936 ;; in the spine, we also check for pairs whose cars are annotated in case
1937 ;; we've been passed the cdr of an annotated list
1946 (strip (syntax-object-expression x) (syntax-object-wrap x)))
1948 (let ((a (f (car x))) (d (f (cdr x))))
1949 (if (and (eq? a (car x)) (eq? d (cdr x)))
1953 (let ((old (vector->list x)))
1954 (let ((new (map f old)))
1955 ;; inlined and-map with two args
1956 (let lp ((l1 old) (l2 new))
1959 (if (eq? (car l1) (car l2))
1960 (lp (cdr l1) (cdr l2))
1961 (list->vector new)))))))
1964 ;; lexical variables
1968 (let ((id (if (syntax-object? id) (syntax-object-expression id) id)))
1969 (build-lexical-var no-source id))))
1971 ;; appears to return a reversed list
1972 (define lambda-var-list
1974 (let lvl ((vars vars) (ls '()) (w empty-wrap))
1976 ((pair? vars) (lvl (cdr vars) (cons (wrap (car vars) w #f) ls) w))
1977 ((id? vars) (cons (wrap vars w #f) ls))
1979 ((syntax-object? vars)
1980 (lvl (syntax-object-expression vars)
1982 (join-wraps w (syntax-object-wrap vars))))
1983 ;; include anything else to be caught by subsequent error
1985 (else (cons vars ls))))))
1987 ;; core transformers
1989 (global-extend 'local-syntax 'letrec-syntax #t)
1990 (global-extend 'local-syntax 'let-syntax #f)
1993 'core 'syntax-parameterize
1994 (lambda (e r w s mod)
1996 ((_ ((var val) ...) e1 e2 ...)
1997 (valid-bound-ids? #'(var ...))
2001 (lambda () (resolve-identifier x w r mod #f))
2002 (lambda (type value mod)
2004 ((displaced-lexical)
2005 (syntax-violation 'syntax-parameterize
2006 "identifier out of context"
2008 (source-wrap x w s mod)))
2012 (syntax-violation 'syntax-parameterize
2013 "invalid syntax parameter"
2015 (source-wrap x w s mod)))))))
2018 (let ((trans-r (macros-only-env r)))
2022 (eval-local-transformer (expand x trans-r w mod) mod)))
2024 (expand-body #'(e1 e2 ...)
2025 (source-wrap e w s mod)
2026 (extend-env names bindings r)
2029 (_ (syntax-violation 'syntax-parameterize "bad syntax"
2030 (source-wrap e w s mod))))))
2032 (global-extend 'core 'quote
2033 (lambda (e r w s mod)
2035 ((_ e) (build-data s (strip #'e w)))
2036 (_ (syntax-violation 'quote "bad syntax"
2037 (source-wrap e w s mod))))))
2043 (lambda (src e r maps ellipsis? mod)
2045 (call-with-values (lambda ()
2046 (resolve-identifier e empty-wrap r mod #f))
2047 (lambda (type value mod)
2051 (lambda () (gen-ref src (car value) (cdr value) maps))
2053 (values `(ref ,var) maps))))
2055 (if (ellipsis? e r mod)
2056 (syntax-violation 'syntax "misplaced ellipsis" src)
2057 (values `(quote ,e) maps))))))
2060 (ellipsis? #'dots r mod)
2061 (gen-syntax src #'e r maps (lambda (e r mod) #f) mod))
2063 ;; this could be about a dozen lines of code, except that we
2064 ;; choose to handle #'(x ... ...) forms
2065 (ellipsis? #'dots r mod)
2070 (gen-syntax src #'x r
2071 (cons '() maps) ellipsis? mod))
2073 (if (null? (car maps))
2074 (syntax-violation 'syntax "extra ellipsis"
2076 (values (gen-map x (car maps))
2080 (ellipsis? #'dots r mod)
2084 (lambda () (k (cons '() maps)))
2086 (if (null? (car maps))
2087 (syntax-violation 'syntax "extra ellipsis" src)
2088 (values (gen-mappend x (car maps))
2090 (_ (call-with-values
2091 (lambda () (gen-syntax src y r maps ellipsis? mod))
2094 (lambda () (k maps))
2096 (values (gen-append x y) maps)))))))))
2099 (lambda () (gen-syntax src #'x r maps ellipsis? mod))
2102 (lambda () (gen-syntax src #'y r maps ellipsis? mod))
2103 (lambda (y maps) (values (gen-cons x y) maps))))))
2107 (gen-syntax src #'(e1 e2 ...) r maps ellipsis? mod))
2108 (lambda (e maps) (values (gen-vector e) maps))))
2109 (_ (values `(quote ,e) maps))))))
2112 (lambda (src var level maps)
2116 (syntax-violation 'syntax "missing ellipsis" src)
2118 (lambda () (gen-ref src var (fx- level 1) (cdr maps)))
2119 (lambda (outer-var outer-maps)
2120 (let ((b (assq outer-var (car maps))))
2122 (values (cdr b) maps)
2123 (let ((inner-var (gen-var 'tmp)))
2125 (cons (cons (cons outer-var inner-var)
2127 outer-maps)))))))))))
2131 `(apply (primitive append) ,(gen-map e map-env))))
2135 (let ((formals (map cdr map-env))
2136 (actuals (map (lambda (x) `(ref ,(car x))) map-env)))
2139 ;; identity map equivalence:
2140 ;; (map (lambda (x) x) y) == y
2143 (lambda (x) (and (eq? (car x) 'ref) (memq (cadr x) formals)))
2145 ;; eta map equivalence:
2146 ;; (map (lambda (x ...) (f x ...)) y ...) == (map f y ...)
2147 `(map (primitive ,(car e))
2148 ,@(map (let ((r (map cons formals actuals)))
2149 (lambda (x) (cdr (assq (cadr x) r))))
2151 (else `(map (lambda ,formals ,e) ,@actuals))))))
2157 (if (eq? (car x) 'quote)
2158 `(quote (,(cadr x) . ,(cadr y)))
2159 (if (eq? (cadr y) '())
2162 ((list) `(list ,x ,@(cdr y)))
2163 (else `(cons ,x ,y)))))
2167 (if (equal? y '(quote ()))
2174 ((eq? (car x) 'list) `(vector ,@(cdr x)))
2175 ((eq? (car x) 'quote) `(quote #(,@(cadr x))))
2176 (else `(list->vector ,x)))))
2182 ((ref) (build-lexical-reference 'value no-source (cadr x) (cadr x)))
2183 ((primitive) (build-primref no-source (cadr x)))
2184 ((quote) (build-data no-source (cadr x)))
2186 (if (list? (cadr x))
2187 (build-simple-lambda no-source (cadr x) #f (cadr x) '() (regen (caddr x)))
2188 (error "how did we get here" x)))
2189 (else (build-primcall no-source (car x) (map regen (cdr x)))))))
2191 (lambda (e r w s mod)
2192 (let ((e (source-wrap e w s mod)))
2196 (lambda () (gen-syntax e #'x r '() ellipsis? mod))
2197 (lambda (e maps) (regen e))))
2198 (_ (syntax-violation 'syntax "bad `syntax' form" e)))))))
2200 (global-extend 'core 'lambda
2201 (lambda (e r w s mod)
2204 (call-with-values (lambda () (lambda-formals #'args))
2205 (lambda (req opt rest kw)
2206 (let lp ((body #'(e1 e2 ...)) (meta '()))
2207 (syntax-case body ()
2208 ((docstring e1 e2 ...) (string? (syntax->datum #'docstring))
2212 . ,(syntax->datum #'docstring))))))
2213 ((#((k . v) ...) e1 e2 ...)
2215 (append meta (syntax->datum #'((k . v) ...)))))
2216 (_ (expand-simple-lambda e r w s mod req rest meta body)))))))
2217 (_ (syntax-violation 'lambda "bad lambda" e)))))
2219 (global-extend 'core 'lambda*
2220 (lambda (e r w s mod)
2225 (expand-lambda-case e r w s mod
2226 lambda*-formals #'((args e1 e2 ...))))
2227 (lambda (meta lcase)
2228 (build-case-lambda s meta lcase))))
2229 (_ (syntax-violation 'lambda "bad lambda*" e)))))
2231 (global-extend 'core 'case-lambda
2232 (lambda (e r w s mod)
2233 (define (build-it meta clauses)
2236 (expand-lambda-case e r w s mod
2239 (lambda (meta* lcase)
2240 (build-case-lambda s (append meta meta*) lcase))))
2242 ((_ (args e1 e2 ...) ...)
2243 (build-it '() #'((args e1 e2 ...) ...)))
2244 ((_ docstring (args e1 e2 ...) ...)
2245 (string? (syntax->datum #'docstring))
2246 (build-it `((documentation
2247 . ,(syntax->datum #'docstring)))
2248 #'((args e1 e2 ...) ...)))
2249 (_ (syntax-violation 'case-lambda "bad case-lambda" e)))))
2251 (global-extend 'core 'case-lambda*
2252 (lambda (e r w s mod)
2253 (define (build-it meta clauses)
2256 (expand-lambda-case e r w s mod
2259 (lambda (meta* lcase)
2260 (build-case-lambda s (append meta meta*) lcase))))
2262 ((_ (args e1 e2 ...) ...)
2263 (build-it '() #'((args e1 e2 ...) ...)))
2264 ((_ docstring (args e1 e2 ...) ...)
2265 (string? (syntax->datum #'docstring))
2266 (build-it `((documentation
2267 . ,(syntax->datum #'docstring)))
2268 #'((args e1 e2 ...) ...)))
2269 (_ (syntax-violation 'case-lambda "bad case-lambda*" e)))))
2271 (global-extend 'core 'with-ellipsis
2272 (lambda (e r w s mod)
2276 (let ((id (if (symbol? #'dots)
2278 (make-syntax-object '#{ $sc-ellipsis }#
2279 (syntax-object-wrap #'dots)
2280 (syntax-object-module #'dots)))))
2281 (let ((ids (list id))
2282 (labels (list (gen-label)))
2283 (bindings (list (make-binding 'ellipsis (source-wrap #'dots w s mod)))))
2284 (let ((nw (make-binding-wrap ids labels w))
2285 (nr (extend-env labels bindings r)))
2286 (expand-body #'(e1 e2 ...) (source-wrap e nw s mod) nr nw mod)))))
2287 (_ (syntax-violation 'with-ellipsis "bad syntax"
2288 (source-wrap e w s mod))))))
2290 (global-extend 'core 'let
2292 (define (expand-let e r w s mod constructor ids vals exps)
2293 (if (not (valid-bound-ids? ids))
2294 (syntax-violation 'let "duplicate bound variable" e)
2295 (let ((labels (gen-labels ids))
2296 (new-vars (map gen-var ids)))
2297 (let ((nw (make-binding-wrap ids labels w))
2298 (nr (extend-var-env labels new-vars r)))
2300 (map syntax->datum ids)
2302 (map (lambda (x) (expand x r w mod)) vals)
2303 (expand-body exps (source-wrap e nw s mod)
2305 (lambda (e r w s mod)
2307 ((_ ((id val) ...) e1 e2 ...)
2308 (and-map id? #'(id ...))
2309 (expand-let e r w s mod
2314 ((_ f ((id val) ...) e1 e2 ...)
2315 (and (id? #'f) (and-map id? #'(id ...)))
2316 (expand-let e r w s mod
2321 (_ (syntax-violation 'let "bad let" (source-wrap e w s mod)))))))
2324 (global-extend 'core 'letrec
2325 (lambda (e r w s mod)
2327 ((_ ((id val) ...) e1 e2 ...)
2328 (and-map id? #'(id ...))
2329 (let ((ids #'(id ...)))
2330 (if (not (valid-bound-ids? ids))
2331 (syntax-violation 'letrec "duplicate bound variable" e)
2332 (let ((labels (gen-labels ids))
2333 (new-vars (map gen-var ids)))
2334 (let ((w (make-binding-wrap ids labels w))
2335 (r (extend-var-env labels new-vars r)))
2337 (map syntax->datum ids)
2339 (map (lambda (x) (expand x r w mod)) #'(val ...))
2340 (expand-body #'(e1 e2 ...)
2341 (source-wrap e w s mod) r w mod)))))))
2342 (_ (syntax-violation 'letrec "bad letrec" (source-wrap e w s mod))))))
2345 (global-extend 'core 'letrec*
2346 (lambda (e r w s mod)
2348 ((_ ((id val) ...) e1 e2 ...)
2349 (and-map id? #'(id ...))
2350 (let ((ids #'(id ...)))
2351 (if (not (valid-bound-ids? ids))
2352 (syntax-violation 'letrec* "duplicate bound variable" e)
2353 (let ((labels (gen-labels ids))
2354 (new-vars (map gen-var ids)))
2355 (let ((w (make-binding-wrap ids labels w))
2356 (r (extend-var-env labels new-vars r)))
2358 (map syntax->datum ids)
2360 (map (lambda (x) (expand x r w mod)) #'(val ...))
2361 (expand-body #'(e1 e2 ...)
2362 (source-wrap e w s mod) r w mod)))))))
2363 (_ (syntax-violation 'letrec* "bad letrec*" (source-wrap e w s mod))))))
2368 (lambda (e r w s mod)
2373 (lambda () (resolve-identifier #'id w r mod #t))
2374 (lambda (type value id-mod)
2377 (build-lexical-assignment s (syntax->datum #'id) value
2378 (expand #'val r w mod)))
2380 (build-global-assignment s value (expand #'val r w mod) id-mod))
2382 (if (procedure-property value 'variable-transformer)
2383 ;; As syntax-type does, call expand-macro with
2384 ;; the mod of the expression. Hmm.
2385 (expand (expand-macro value e r w s #f mod) r empty-wrap mod)
2386 (syntax-violation 'set! "not a variable transformer"
2388 (wrap #'id w id-mod))))
2389 ((displaced-lexical)
2390 (syntax-violation 'set! "identifier out of context"
2393 (syntax-violation 'set! "bad set!" (source-wrap e w s mod)))))))
2394 ((_ (head tail ...) val)
2396 (lambda () (syntax-type #'head r empty-wrap no-source #f mod #t))
2397 (lambda (type value ee* ee ww ss modmod)
2400 (let ((val (expand #'val r w mod)))
2401 (call-with-values (lambda () (value #'(head tail ...) r w mod))
2402 (lambda (e r w s* mod)
2405 (build-global-assignment s (syntax->datum #'e)
2409 (expand #'(setter head) r w mod)
2410 (map (lambda (e) (expand e r w mod))
2411 #'(tail ... val))))))))
2412 (_ (syntax-violation 'set! "bad set!" (source-wrap e w s mod))))))
2414 (global-extend 'module-ref '@
2418 (and (and-map id? #'(mod ...)) (id? #'id))
2419 ;; Strip the wrap from the identifier and return top-wrap
2420 ;; so that the identifier will not be captured by lexicals.
2421 (values (syntax->datum #'id) r top-wrap #f
2423 #'(public mod ...)))))))
2425 (global-extend 'module-ref '@@
2430 (cons (remodulate (car x) mod)
2431 (remodulate (cdr x) mod)))
2434 (remodulate (syntax-object-expression x) mod)
2435 (syntax-object-wrap x)
2436 ;; hither the remodulation
2439 (let* ((n (vector-length x)) (v (make-vector n)))
2440 (do ((i 0 (fx+ i 1)))
2442 (vector-set! v i (remodulate (vector-ref x i) mod)))))
2444 (syntax-case e (@@ primitive)
2447 (equal? (cdr (if (syntax-object? #'id)
2448 (syntax-object-module #'id)
2451 ;; Strip the wrap from the identifier and return top-wrap
2452 ;; so that the identifier will not be captured by lexicals.
2453 (values (syntax->datum #'id) r top-wrap #f '(primitive)))
2455 (and (and-map id? #'(mod ...)) (id? #'id))
2456 ;; Strip the wrap from the identifier and return top-wrap
2457 ;; so that the identifier will not be captured by lexicals.
2458 (values (syntax->datum #'id) r top-wrap #f
2460 #'(private mod ...))))
2461 ((_ @@ (mod ...) exp)
2462 (and-map id? #'(mod ...))
2463 ;; This is a special syntax used to support R6RS library forms.
2464 ;; Unlike the syntax above, the last item is not restricted to
2465 ;; be a single identifier, and the syntax objects are kept
2466 ;; intact, with only their module changed.
2467 (let ((mod (syntax->datum #'(private mod ...))))
2468 (values (remodulate #'exp mod)
2469 r w (source-annotation #'exp)
2472 (global-extend 'core 'if
2473 (lambda (e r w s mod)
2478 (expand #'test r w mod)
2479 (expand #'then r w mod)
2480 (build-void no-source)))
2484 (expand #'test r w mod)
2485 (expand #'then r w mod)
2486 (expand #'else r w mod))))))
2488 (global-extend 'begin 'begin '())
2490 (global-extend 'define 'define '())
2492 (global-extend 'define-syntax 'define-syntax '())
2493 (global-extend 'define-syntax-parameter 'define-syntax-parameter '())
2495 (global-extend 'eval-when 'eval-when '())
2497 (global-extend 'core 'syntax-case
2499 (define convert-pattern
2500 ;; accepts pattern & keys
2501 ;; returns $sc-dispatch pattern & ids
2502 (lambda (pattern keys ellipsis?)
2508 (lambda () (cvt* #'y n ids))
2511 (lambda () (cvt #'x n ids))
2513 (values (cons x y) ids))))))
2514 (_ (cvt p* n ids)))))
2516 (define (v-reverse x)
2517 (let loop ((r '()) (x x))
2520 (loop (cons (car x) r) (cdr x)))))
2526 ((bound-id-member? p keys)
2527 (values (vector 'free-id p) ids))
2531 (values 'any (cons (cons p n) ids))))
2534 (ellipsis? (syntax dots))
2536 (lambda () (cvt (syntax x) (fx+ n 1) ids))
2538 (values (if (eq? p 'any) 'each-any (vector 'each p))
2541 (ellipsis? (syntax dots))
2543 (lambda () (cvt* (syntax ys) n ids))
2546 (lambda () (cvt (syntax x) (+ n 1) ids))
2549 (lambda () (v-reverse ys))
2551 (values `#(each+ ,x ,ys ,e)
2555 (lambda () (cvt (syntax y) n ids))
2558 (lambda () (cvt (syntax x) n ids))
2560 (values (cons x y) ids))))))
2561 (() (values '() ids))
2564 (lambda () (cvt (syntax (x ...)) n ids))
2565 (lambda (p ids) (values (vector 'vector p) ids))))
2566 (x (values (vector 'atom (strip p empty-wrap)) ids))))))
2567 (cvt pattern 0 '())))
2569 (define build-dispatch-call
2570 (lambda (pvars exp y r mod)
2571 (let ((ids (map car pvars)) (levels (map cdr pvars)))
2572 (let ((labels (gen-labels ids)) (new-vars (map gen-var ids)))
2576 (list (build-simple-lambda no-source (map syntax->datum ids) #f new-vars '()
2580 (map (lambda (var level)
2581 (make-binding 'syntax `(,var . ,level)))
2585 (make-binding-wrap ids labels empty-wrap)
2590 (lambda (x keys clauses r pat fender exp mod)
2592 (lambda () (convert-pattern pat keys (lambda (e) (ellipsis? e r mod))))
2595 ((not (and-map (lambda (x) (not (ellipsis? (car x) r mod))) pvars))
2596 (syntax-violation 'syntax-case "misplaced ellipsis" pat))
2597 ((not (distinct-bound-ids? (map car pvars)))
2598 (syntax-violation 'syntax-case "duplicate pattern variable" pat))
2600 (let ((y (gen-var 'tmp)))
2601 ;; fat finger binding and references to temp variable y
2602 (build-call no-source
2603 (build-simple-lambda no-source (list 'tmp) #f (list y) '()
2604 (let ((y (build-lexical-reference 'value no-source
2606 (build-conditional no-source
2607 (syntax-case fender ()
2609 (_ (build-conditional no-source
2611 (build-dispatch-call pvars fender y r mod)
2612 (build-data no-source #f))))
2613 (build-dispatch-call pvars exp y r mod)
2614 (gen-syntax-case x keys clauses r mod))))
2615 (list (if (eq? p 'any)
2616 (build-primcall no-source 'list (list x))
2617 (build-primcall no-source '$sc-dispatch
2618 (list x (build-data no-source p)))))))))))))
2620 (define gen-syntax-case
2621 (lambda (x keys clauses r mod)
2623 (build-primcall no-source 'syntax-violation
2624 (list (build-data no-source #f)
2625 (build-data no-source
2626 "source expression failed to match any pattern")
2628 (syntax-case (car clauses) ()
2630 (if (and (id? #'pat)
2631 (and-map (lambda (x) (not (free-id=? #'pat x)))
2632 (cons #'(... ...) keys)))
2633 (if (free-id=? #'pat #'_)
2634 (expand #'exp r empty-wrap mod)
2635 (let ((labels (list (gen-label)))
2636 (var (gen-var #'pat)))
2637 (build-call no-source
2638 (build-simple-lambda
2639 no-source (list (syntax->datum #'pat)) #f (list var)
2643 (list (make-binding 'syntax `(,var . 0)))
2645 (make-binding-wrap #'(pat)
2649 (gen-clause x keys (cdr clauses) r
2650 #'pat #t #'exp mod)))
2652 (gen-clause x keys (cdr clauses) r
2653 #'pat #'fender #'exp mod))
2654 (_ (syntax-violation 'syntax-case "invalid clause"
2657 (lambda (e r w s mod)
2658 (let ((e (source-wrap e w s mod)))
2660 ((_ val (key ...) m ...)
2661 (if (and-map (lambda (x) (and (id? x) (not (ellipsis? x r mod))))
2663 (let ((x (gen-var 'tmp)))
2664 ;; fat finger binding and references to temp variable x
2666 (build-simple-lambda no-source (list 'tmp) #f (list x) '()
2667 (gen-syntax-case (build-lexical-reference 'value no-source
2669 #'(key ...) #'(m ...)
2672 (list (expand #'val r empty-wrap mod))))
2673 (syntax-violation 'syntax-case "invalid literals list" e))))))))
2675 ;; The portable macroexpand seeds expand-top's mode m with 'e (for
2676 ;; evaluating) and esew (which stands for "eval syntax expanders
2677 ;; when") with '(eval). In Chez Scheme, m is set to 'c instead of e
2678 ;; if we are compiling a file, and esew is set to
2679 ;; (eval-syntactic-expanders-when), which defaults to the list
2680 ;; '(compile load eval). This means that, by default, top-level
2681 ;; syntactic definitions are evaluated immediately after they are
2682 ;; expanded, and the expanded definitions are also residualized into
2683 ;; the object file if we are compiling a file.
2685 (lambda* (x #:optional (m 'e) (esew '(eval)))
2686 (expand-top-sequence (list x) null-env top-wrap #f m esew
2687 (cons 'hygiene (module-name (current-module))))))
2695 (make-syntax-object datum (syntax-object-wrap id)
2696 (syntax-object-module id))))
2699 ;; accepts any object, since syntax objects may consist partially
2700 ;; or entirely of unwrapped, nonsymbolic data
2702 (strip x empty-wrap)))
2705 (lambda (x) (source-annotation x)))
2707 (set! generate-temporaries
2709 (arg-check list? ls 'generate-temporaries)
2710 (let ((mod (cons 'hygiene (module-name (current-module)))))
2711 (map (lambda (x) (wrap (gensym "t-") top-wrap mod)) ls))))
2713 (set! free-identifier=?
2715 (arg-check nonsymbol-id? x 'free-identifier=?)
2716 (arg-check nonsymbol-id? y 'free-identifier=?)
2719 (set! bound-identifier=?
2721 (arg-check nonsymbol-id? x 'bound-identifier=?)
2722 (arg-check nonsymbol-id? y 'bound-identifier=?)
2725 (set! syntax-violation
2726 (lambda* (who message form #:optional subform)
2727 (arg-check (lambda (x) (or (not x) (string? x) (symbol? x)))
2728 who 'syntax-violation)
2729 (arg-check string? message 'syntax-violation)
2730 (throw 'syntax-error who message
2731 (or (source-annotation subform)
2732 (source-annotation form))
2733 (strip form empty-wrap)
2734 (and subform (strip subform empty-wrap)))))
2737 (define (syntax-module id)
2738 (arg-check nonsymbol-id? id 'syntax-module)
2739 (let ((mod (syntax-object-module id)))
2740 (and (not (equal? mod '(primitive)))
2743 (define* (syntax-local-binding id #:key (resolve-syntax-parameters? #t))
2744 (arg-check nonsymbol-id? id 'syntax-local-binding)
2745 (with-transformer-environment
2746 (lambda (e r w s rib mod)
2747 (define (strip-anti-mark w)
2748 (let ((ms (wrap-marks w)) (s (wrap-subst w)))
2749 (if (and (pair? ms) (eq? (car ms) the-anti-mark))
2750 ;; output is from original text
2751 (make-wrap (cdr ms) (if rib (cons rib (cdr s)) (cdr s)))
2752 ;; output introduced by macro
2753 (make-wrap ms (if rib (cons rib s) s)))))
2754 (call-with-values (lambda ()
2756 (syntax-object-expression id)
2757 (strip-anti-mark (syntax-object-wrap id))
2759 (syntax-object-module id)
2760 resolve-syntax-parameters?))
2761 (lambda (type value mod)
2763 ((lexical) (values 'lexical value))
2764 ((macro) (values 'macro value))
2765 ((syntax-parameter) (values 'syntax-parameter (car value)))
2766 ((syntax) (values 'pattern-variable value))
2767 ((displaced-lexical) (values 'displaced-lexical #f))
2769 (if (equal? mod '(primitive))
2770 (values 'primitive value)
2771 (values 'global (cons value (cdr mod)))))
2774 (make-syntax-object (syntax-object-expression value)
2775 (anti-mark (syntax-object-wrap value))
2776 (syntax-object-module value))))
2777 (else (values 'other #f))))))))
2779 (define (syntax-locally-bound-identifiers id)
2780 (arg-check nonsymbol-id? id 'syntax-locally-bound-identifiers)
2781 (locally-bound-identifiers (syntax-object-wrap id)
2782 (syntax-object-module id)))
2784 ;; Using define! instead of set! to avoid warnings at
2785 ;; compile-time, after the variables are stolen away into (system
2786 ;; syntax). See the end of boot-9.scm.
2788 (define! 'syntax-module syntax-module)
2789 (define! 'syntax-local-binding syntax-local-binding)
2790 (define! 'syntax-locally-bound-identifiers syntax-locally-bound-identifiers))
2792 ;; $sc-dispatch expects an expression and a pattern. If the expression
2793 ;; matches the pattern a list of the matching expressions for each
2794 ;; "any" is returned. Otherwise, #f is returned. (This use of #f will
2795 ;; not work on r4rs implementations that violate the ieee requirement
2796 ;; that #f and () be distinct.)
2798 ;; The expression is matched with the pattern as follows:
2800 ;; pattern: matches:
2803 ;; (<pattern>1 . <pattern>2) (<pattern>1 . <pattern>2)
2805 ;; #(free-id <key>) <key> with free-identifier=?
2806 ;; #(each <pattern>) (<pattern>*)
2807 ;; #(each+ p1 (p2_1 ... p2_n) p3) (p1* (p2_n ... p2_1) . p3)
2808 ;; #(vector <pattern>) (list->vector <pattern>)
2809 ;; #(atom <object>) <object> with "equal?"
2811 ;; Vector cops out to pair under assumption that vectors are rare. If
2812 ;; not, should convert to:
2813 ;; #(vector <pattern>*) #(<pattern>*)
2821 (let ((first (match (car e) p w '() mod)))
2823 (let ((rest (match-each (cdr e) p w mod)))
2824 (and rest (cons first rest))))))
2827 (match-each (syntax-object-expression e)
2829 (join-wraps w (syntax-object-wrap e))
2830 (syntax-object-module e)))
2834 (lambda (e x-pat y-pat z-pat w r mod)
2835 (let f ((e e) (w w))
2838 (call-with-values (lambda () (f (cdr e) w))
2839 (lambda (xr* y-pat r)
2842 (let ((xr (match (car e) x-pat w '() mod)))
2844 (values (cons xr xr*) y-pat r)
2849 (match (car e) (car y-pat) w r mod)))
2850 (values #f #f #f)))))
2852 (f (syntax-object-expression e) (join-wraps w e)))
2854 (values '() y-pat (match e z-pat w r mod)))))))
2856 (define match-each-any
2860 (let ((l (match-each-any (cdr e) w mod)))
2861 (and l (cons (wrap (car e) w mod) l))))
2864 (match-each-any (syntax-object-expression e)
2865 (join-wraps w (syntax-object-wrap e))
2874 ((eq? p 'any) (cons '() r))
2875 ((pair? p) (match-empty (car p) (match-empty (cdr p) r)))
2876 ((eq? p 'each-any) (cons '() r))
2878 (case (vector-ref p 0)
2879 ((each) (match-empty (vector-ref p 1) r))
2880 ((each+) (match-empty (vector-ref p 1)
2882 (reverse (vector-ref p 2))
2883 (match-empty (vector-ref p 3) r))))
2885 ((vector) (match-empty (vector-ref p 1) r)))))))
2889 (if (null? (car r*))
2891 (cons (map car r*) (combine (map cdr r*) r)))))
2894 (lambda (e p w r mod)
2896 ((null? p) (and (null? e) r))
2898 (and (pair? e) (match (car e) (car p) w
2899 (match (cdr e) (cdr p) w r mod)
2902 (let ((l (match-each-any e w mod))) (and l (cons l r))))
2904 (case (vector-ref p 0)
2907 (match-empty (vector-ref p 1) r)
2908 (let ((l (match-each e (vector-ref p 1) w mod)))
2910 (let collect ((l l))
2913 (cons (map car l) (collect (map cdr l)))))))))
2917 (match-each+ e (vector-ref p 1) (vector-ref p 2) (vector-ref p 3) w r mod))
2918 (lambda (xr* y-pat r)
2922 (match-empty (vector-ref p 1) r)
2923 (combine xr* r))))))
2924 ((free-id) (and (id? e) (free-id=? (wrap e w mod) (vector-ref p 1)) r))
2925 ((atom) (and (equal? (vector-ref p 1) (strip e w)) r))
2928 (match (vector->list e) (vector-ref p 1) w r mod))))))))
2931 (lambda (e p w r mod)
2935 ((eq? p 'any) (cons (wrap e w mod) r))
2938 (syntax-object-expression e)
2940 (join-wraps w (syntax-object-wrap e))
2942 (syntax-object-module e)))
2943 (else (match* e p w r mod)))))
2948 ((eq? p 'any) (list e))
2951 (match* (syntax-object-expression e)
2952 p (syntax-object-wrap e) '() (syntax-object-module e)))
2953 (else (match* e p empty-wrap '() #f))))))))
2956 (define-syntax with-syntax
2960 #'(let () e1 e2 ...))
2961 ((_ ((out in)) e1 e2 ...)
2962 #'(syntax-case in ()
2963 (out (let () e1 e2 ...))))
2964 ((_ ((out in) ...) e1 e2 ...)
2965 #'(syntax-case (list in ...) ()
2966 ((out ...) (let () e1 e2 ...)))))))
2968 (define-syntax syntax-error
2971 ;; Extended internal syntax which provides the original form
2972 ;; as the first operand, for improved error reporting.
2973 ((_ (keyword . operands) message arg ...)
2974 (string? (syntax->datum #'message))
2975 (syntax-violation (syntax->datum #'keyword)
2976 (string-join (cons (syntax->datum #'message)
2981 (and (syntax->datum #'keyword)
2982 #'(keyword . operands))))
2983 ;; Standard R7RS syntax
2984 ((_ message arg ...)
2985 (string? (syntax->datum #'message))
2986 #'(syntax-error (#f) message arg ...)))))
2988 (define-syntax syntax-rules
2990 (define (expand-clause clause)
2991 ;; Convert a 'syntax-rules' clause into a 'syntax-case' clause.
2992 (syntax-case clause (syntax-error)
2993 ;; If the template is a 'syntax-error' form, use the extended
2994 ;; internal syntax, which adds the original form as the first
2995 ;; operand for improved error reporting.
2996 (((keyword . pattern) (syntax-error message arg ...))
2997 (string? (syntax->datum #'message))
2998 #'((dummy . pattern) #'(syntax-error (dummy . pattern) message arg ...)))
3000 (((keyword . pattern) template)
3001 #'((dummy . pattern) #'template))))
3002 (define (expand-syntax-rules dots keys docstrings clauses)
3005 ((docstring ...) docstrings)
3006 ((((keyword . pattern) template) ...) clauses)
3007 ((clause ...) (map expand-clause clauses)))
3009 ((form #'(lambda (x)
3010 docstring ... ; optional docstring
3011 #((macro-type . syntax-rules)
3012 (patterns pattern ...)) ; embed patterns as procedure metadata
3013 (syntax-case x (k ...)
3016 (with-syntax ((dots dots))
3017 #'(with-ellipsis dots form))
3020 ((_ (k ...) ((keyword . pattern) template) ...)
3021 (expand-syntax-rules #f #'(k ...) #'() #'(((keyword . pattern) template) ...)))
3022 ((_ (k ...) docstring ((keyword . pattern) template) ...)
3023 (string? (syntax->datum #'docstring))
3024 (expand-syntax-rules #f #'(k ...) #'(docstring) #'(((keyword . pattern) template) ...)))
3025 ((_ dots (k ...) ((keyword . pattern) template) ...)
3026 (identifier? #'dots)
3027 (expand-syntax-rules #'dots #'(k ...) #'() #'(((keyword . pattern) template) ...)))
3028 ((_ dots (k ...) docstring ((keyword . pattern) template) ...)
3029 (and (identifier? #'dots) (string? (syntax->datum #'docstring)))
3030 (expand-syntax-rules #'dots #'(k ...) #'(docstring) #'(((keyword . pattern) template) ...))))))
3032 (define-syntax define-syntax-rule
3035 ((_ (name . pattern) template)
3036 #'(define-syntax name
3038 ((_ . pattern) template))))
3039 ((_ (name . pattern) docstring template)
3040 (string? (syntax->datum #'docstring))
3041 #'(define-syntax name
3044 ((_ . pattern) template)))))))
3049 ((let* ((x v) ...) e1 e2 ...)
3050 (and-map identifier? #'(x ...))
3051 (let f ((bindings #'((x v) ...)))
3052 (if (null? bindings)
3053 #'(let () e1 e2 ...)
3054 (with-syntax ((body (f (cdr bindings)))
3055 (binding (car bindings)))
3056 #'(let (binding) body))))))))
3058 (define-syntax quasiquote
3060 (define (quasi p lev)
3061 (syntax-case p (unquote quasiquote)
3065 (quasicons #'("quote" unquote) (quasi #'(p) (- lev 1)))))
3066 ((quasiquote p) (quasicons #'("quote" quasiquote) (quasi #'(p) (+ lev 1))))
3068 (syntax-case #'p (unquote unquote-splicing)
3071 (quasilist* #'(("value" p) ...) (quasi #'q lev))
3073 (quasicons #'("quote" unquote) (quasi #'(p ...) (- lev 1)))
3075 ((unquote-splicing p ...)
3077 (quasiappend #'(("value" p) ...) (quasi #'q lev))
3079 (quasicons #'("quote" unquote-splicing) (quasi #'(p ...) (- lev 1)))
3081 (_ (quasicons (quasi #'p lev) (quasi #'q lev)))))
3082 (#(x ...) (quasivector (vquasi #'(x ...) lev)))
3084 (define (vquasi p lev)
3087 (syntax-case #'p (unquote unquote-splicing)
3090 (quasilist* #'(("value" p) ...) (vquasi #'q lev))
3092 (quasicons #'("quote" unquote) (quasi #'(p ...) (- lev 1)))
3094 ((unquote-splicing p ...)
3096 (quasiappend #'(("value" p) ...) (vquasi #'q lev))
3099 #'("quote" unquote-splicing)
3100 (quasi #'(p ...) (- lev 1)))
3102 (_ (quasicons (quasi #'p lev) (vquasi #'q lev)))))
3103 (() #'("quote" ()))))
3104 (define (quasicons x y)
3105 (with-syntax ((x x) (y y))
3109 (("quote" dx) #'("quote" (dx . dy)))
3110 (_ (if (null? #'dy) #'("list" x) #'("list*" x y)))))
3111 (("list" . stuff) #'("list" x . stuff))
3112 (("list*" . stuff) #'("list*" x . stuff))
3113 (_ #'("list*" x y)))))
3114 (define (quasiappend x y)
3118 ((null? x) #'("quote" ()))
3119 ((null? (cdr x)) (car x))
3120 (else (with-syntax (((p ...) x)) #'("append" p ...)))))
3124 (else (with-syntax (((p ...) x) (y y)) #'("append" p ... y)))))))
3125 (define (quasilist* x y)
3129 (quasicons (car x) (f (cdr x))))))
3130 (define (quasivector x)
3132 (("quote" (x ...)) #'("quote" #(x ...)))
3134 (let f ((y x) (k (lambda (ls) #`("vector" #,@ls))))
3136 (("quote" (y ...)) (k #'(("quote" y) ...)))
3137 (("list" y ...) (k #'(y ...)))
3138 (("list*" y ... z) (f #'z (lambda (ls) (k (append #'(y ...) ls)))))
3139 (else #`("list->vector" #,x)))))))
3143 (("list" x ...) #`(list #,@(map emit #'(x ...))))
3144 ;; could emit list* for 3+ arguments if implementation supports
3147 (let f ((x* #'(x ...)))
3150 #`(cons #,(emit (car x*)) #,(f (cdr x*))))))
3151 (("append" x ...) #`(append #,@(map emit #'(x ...))))
3152 (("vector" x ...) #`(vector #,@(map emit #'(x ...))))
3153 (("list->vector" x) #`(list->vector #,(emit #'x)))
3157 ;; convert to intermediate language, combining introduced (but
3158 ;; not unquoted source) quote expressions where possible and
3159 ;; choosing optimal construction code otherwise, then emit
3160 ;; Scheme code corresponding to the intermediate language forms.
3161 ((_ e) (emit (quasi #'e 0)))))))
3163 (define-syntax include
3167 (let* ((p (open-input-file
3168 (cond ((absolute-file-name? fn)
3171 (in-vicinity dir fn))
3175 "relative file name only allowed when the include form is in a file"
3177 (enc (file-encoding p)))
3179 ;; Choose the input encoding deterministically.
3180 (set-port-encoding! p (or enc "UTF-8"))
3182 (let f ((x (read p))
3186 (close-input-port p)
3189 (cons (datum->syntax k x) result)))))))
3190 (let* ((src (syntax-source x))
3191 (file (and src (assq-ref src 'filename)))
3192 (dir (and (string? file) (dirname file))))
3195 (let ((fn (syntax->datum #'filename)))
3196 (with-syntax (((exp ...) (read-file fn dir #'filename)))
3197 #'(begin exp ...))))))))
3199 (define-syntax include-from-path
3203 (let ((fn (syntax->datum #'filename)))
3204 (with-syntax ((fn (datum->syntax
3206 (or (%search-load-path fn)
3207 (syntax-violation 'include-from-path
3208 "file not found in path"
3210 #'(include fn)))))))
3212 (define-syntax unquote
3214 (syntax-violation 'unquote
3215 "expression not valid outside of quasiquote"
3218 (define-syntax unquote-splicing
3220 (syntax-violation 'unquote-splicing
3221 "expression not valid outside of quasiquote"
3224 (define (make-variable-transformer proc)
3225 (if (procedure? proc)
3226 (let ((trans (lambda (x)
3227 #((macro-type . variable-transformer))
3229 (set-procedure-property! trans 'variable-transformer #t)
3231 (error "variable transformer not a procedure" proc)))
3233 (define-syntax identifier-syntax
3235 (syntax-case xx (set!)
3238 #((macro-type . identifier-syntax))
3244 #'(e x (... ...))))))
3245 ((_ (id exp1) ((set! var val) exp2))
3246 (and (identifier? #'id) (identifier? #'var))
3247 #'(make-variable-transformer
3249 #((macro-type . variable-transformer))
3250 (syntax-case x (set!)
3251 ((set! var val) #'exp2)
3252 ((id x (... ...)) #'(exp1 x (... ...)))
3253 (id (identifier? #'id) #'exp1))))))))
3255 (define-syntax define*
3258 ((_ (id . args) b0 b1 ...)
3259 #'(define id (lambda* args b0 b1 ...)))
3260 ((_ id val) (identifier? #'id)
3261 #'(define id val)))))