3 ;;;; Copyright (C) 2001, 2003, 2006 Free Software Foundation, Inc.
5 ;;;; This library is free software; you can redistribute it and/or
6 ;;;; modify it under the terms of the GNU Lesser General Public
7 ;;;; License as published by the Free Software Foundation; either
8 ;;;; version 2.1 of the License, or (at your option) any later version.
10 ;;;; This library is distributed in the hope that it will be useful,
11 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ;;;; Lesser General Public License for more details.
15 ;;;; You should have received a copy of the GNU Lesser General Public
16 ;;;; License along with this library; if not, write to the Free Software
17 ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 ;;; Portable implementation of syntax-case
22 ;;; Extracted from Chez Scheme Version 5.9f
23 ;;; Authors: R. Kent Dybvig, Oscar Waddell, Bob Hieb, Carl Bruggeman
25 ;;; Modified by Andy Wingo <wingo@pobox.com> according to the Git
26 ;;; revision control logs corresponding to this file: 2009.
28 ;;; Modified by Mikael Djurfeldt <djurfeldt@nada.kth.se> according
29 ;;; to the ChangeLog distributed in the same directory as this file:
30 ;;; 1997-08-19, 1997-09-03, 1997-09-10, 2000-08-13, 2000-08-24,
31 ;;; 2000-09-12, 2001-03-08
33 ;;; Copyright (c) 1992-1997 Cadence Research Systems
34 ;;; Permission to copy this software, in whole or in part, to use this
35 ;;; software for any lawful purpose, and to redistribute this software
36 ;;; is granted subject to the restriction that all copies made of this
37 ;;; software must include this copyright notice in full. This software
38 ;;; is provided AS IS, with NO WARRANTY, EITHER EXPRESS OR IMPLIED,
39 ;;; INCLUDING BUT NOT LIMITED TO IMPLIED WARRANTIES OF MERCHANTABILITY
40 ;;; OR FITNESS FOR ANY PARTICULAR PURPOSE. IN NO EVENT SHALL THE
41 ;;; AUTHORS BE LIABLE FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES OF ANY
42 ;;; NATURE WHATSOEVER.
44 ;;; Before attempting to port this code to a new implementation of
45 ;;; Scheme, please read the notes below carefully.
48 ;;; This file defines the syntax-case expander, sc-expand, and a set
49 ;;; of associated syntactic forms and procedures. Of these, the
50 ;;; following are documented in The Scheme Programming Language,
51 ;;; Second Edition (R. Kent Dybvig, Prentice Hall, 1996). Most are
52 ;;; also documented in the R4RS and draft R5RS.
54 ;;; bound-identifier=?
59 ;;; generate-temporaries
70 ;;; All standard Scheme syntactic forms are supported by the expander
71 ;;; or syntactic abstractions defined in this file. Only the R4RS
72 ;;; delay is omitted, since its expansion is implementation-dependent.
74 ;;; The remaining exports are listed below:
77 ;;; if datum represents a valid expression, sc-expand returns an
78 ;;; expanded version of datum in a core language that includes no
79 ;;; syntactic abstractions. The core language includes begin,
80 ;;; define, if, lambda, letrec, quote, and set!.
81 ;;; (eval-when situations expr ...)
82 ;;; conditionally evaluates expr ... at compile-time or run-time
83 ;;; depending upon situations (see the Chez Scheme System Manual,
84 ;;; Revision 3, for a complete description)
85 ;;; (syntax-violation who message form [subform])
86 ;;; used to report errors found during expansion
87 ;;; ($sc-dispatch e p)
88 ;;; used by expanded code to handle syntax-case matching
90 ;;; The following nonstandard procedures must be provided by the
91 ;;; implementation for this code to run using the standard portable
92 ;;; hooks and output constructors. They are not used by expanded code,
93 ;;; and so need be present only at expansion time.
96 ;;; where x is always in the form ("noexpand" expr).
97 ;;; returns the value of expr. the "noexpand" flag is used to tell the
98 ;;; evaluator/expander that no expansion is necessary, since expr has
99 ;;; already been fully expanded to core forms.
101 ;;; eval will not be invoked during the loading of psyntax.pp. After
102 ;;; psyntax.pp has been loaded, the expansion of any macro definition,
103 ;;; whether local or global, will result in a call to eval. If, however,
104 ;;; sc-expand has already been registered as the expander to be used
105 ;;; by eval, and eval accepts one argument, nothing special must be done
106 ;;; to support the "noexpand" flag, since it is handled by sc-expand.
109 ;;; returns a unique symbol each time it's called
111 ;;; When porting to a new Scheme implementation, you should define the
112 ;;; procedures listed above, load the expanded version of psyntax.ss
113 ;;; (psyntax.pp, which should be available whereever you found
114 ;;; psyntax.ss), and register sc-expand as the current expander (how
115 ;;; you do this depends upon your implementation of Scheme). You may
116 ;;; change the hooks and constructors defined toward the beginning of
117 ;;; the code below, but to avoid bootstrapping problems, do so only
118 ;;; after you have a working version of the expander.
120 ;;; Chez Scheme allows the syntactic form (syntax <template>) to be
121 ;;; abbreviated to #'<template>, just as (quote <datum>) may be
122 ;;; abbreviated to '<datum>. The #' syntax makes programs written
123 ;;; using syntax-case shorter and more readable and draws out the
124 ;;; intuitive connection between syntax and quote.
126 ;;; If you find that this code loads or runs slowly, consider
127 ;;; switching to faster hardware or a faster implementation of
128 ;;; Scheme. In Chez Scheme on a 200Mhz Pentium Pro, expanding,
129 ;;; compiling (with full optimization), and loading this file takes
130 ;;; between one and two seconds.
132 ;;; In the expander implementation, we sometimes use syntactic abstractions
133 ;;; when procedural abstractions would suffice. For example, we define
134 ;;; top-wrap and top-marked? as
135 ;;; (define-syntax top-wrap (identifier-syntax '((top))))
136 ;;; (define-syntax top-marked?
138 ;;; ((_ w) (memq 'top (wrap-marks w)))))
140 ;;; (define top-wrap '((top)))
141 ;;; (define top-marked?
142 ;;; (lambda (w) (memq 'top (wrap-marks w))))
143 ;;; On ther other hand, we don't do this consistently; we define make-wrap,
144 ;;; wrap-marks, and wrap-subst simply as
145 ;;; (define make-wrap cons)
146 ;;; (define wrap-marks car)
147 ;;; (define wrap-subst cdr)
148 ;;; In Chez Scheme, the syntactic and procedural forms of these
149 ;;; abstractions are equivalent, since the optimizer consistently
150 ;;; integrates constants and small procedures. Some Scheme
151 ;;; implementations, however, may benefit from more consistent use
152 ;;; of one form or the other.
155 ;;; implementation information:
157 ;;; "begin" is treated as a splicing construct at top level and at
158 ;;; the beginning of bodies. Any sequence of expressions that would
159 ;;; be allowed where the "begin" occurs is allowed.
161 ;;; "let-syntax" and "letrec-syntax" are also treated as splicing
162 ;;; constructs, in violation of the R4RS appendix and probably the R5RS
163 ;;; when it comes out. A consequence, let-syntax and letrec-syntax do
164 ;;; not create local contours, as do let and letrec. Although the
165 ;;; functionality is greater as it is presently implemented, we will
166 ;;; probably change it to conform to the R4RS/expected R5RS.
168 ;;; Objects with no standard print syntax, including objects containing
169 ;;; cycles and syntax object, are allowed in quoted data as long as they
170 ;;; are contained within a syntax form or produced by datum->syntax.
171 ;;; Such objects are never copied.
173 ;;; All identifiers that don't have macro definitions and are not bound
174 ;;; lexically are assumed to be global variables
176 ;;; Top-level definitions of macro-introduced identifiers are allowed.
177 ;;; This may not be appropriate for implementations in which the
178 ;;; model is that bindings are created by definitions, as opposed to
179 ;;; one in which initial values are assigned by definitions.
181 ;;; Top-level variable definitions of syntax keywords is not permitted.
182 ;;; Any solution allowing this would be kludgey and would yield
183 ;;; surprising results in some cases. We can provide an undefine-syntax
184 ;;; form. The questions is, should define be an implicit undefine-syntax?
185 ;;; We've decided no for now.
187 ;;; Identifiers and syntax objects are implemented as vectors for
188 ;;; portability. As a result, it is possible to "forge" syntax
191 ;;; The implementation of generate-temporaries assumes that it is possible
192 ;;; to generate globally unique symbols (gensyms).
194 ;;; The input to sc-expand may contain "annotations" describing, e.g., the
195 ;;; source file and character position from where each object was read if
196 ;;; it was read from a file. These annotations are handled properly by
197 ;;; sc-expand only if the annotation? hook (see hooks below) is implemented
198 ;;; properly and the operators make-annotation, annotation-expression,
199 ;;; annotation-source, annotation-stripped, and set-annotation-stripped!
200 ;;; are supplied. If annotations are supplied, the proper annotation
201 ;;; source is passed to the various output constructors, allowing
202 ;;; implementations to accurately correlate source and expanded code.
203 ;;; Contact one of the authors for details if you wish to make use of
210 ;;; When changing syntax-object representations, it is necessary to support
211 ;;; both old and new syntax-object representations in id-var-name. It
212 ;;; should be sufficient to recognize old representations and treat
213 ;;; them as not lexically bound.
218 (set-current-module (resolve-module '(guile))))
221 ;;; Private version of and-map that handles multiple lists.
223 (lambda (f first . rest)
226 (let andmap ((first first))
227 (let ((x (car first)) (first (cdr first)))
230 (and (f x) (andmap first)))))
231 (let andmap ((first first) (rest rest))
232 (let ((x (car first))
235 (rest (map cdr rest)))
237 (apply f (cons x xr))
238 (and (apply f (cons x xr)) (andmap first rest)))))))))
240 (define-syntax define-structure
242 (define construct-name
243 (lambda (template-identifier . args)
251 (symbol->string (syntax->datum x))))
255 (and-map identifier? (syntax (name id1 ...)))
257 ((constructor (construct-name (syntax name) "make-" (syntax name)))
258 (predicate (construct-name (syntax name) (syntax name) "?"))
260 (map (lambda (x) (construct-name x (syntax name) "-" x))
264 (construct-name x "set-" (syntax name) "-" x "!"))
267 (+ (length (syntax (id1 ...))) 1))
269 (let f ((i 1) (ids (syntax (id1 ...))))
272 (cons i (f (+ i 1) (cdr ids)))))))
276 (vector 'name id1 ... )))
280 (= (vector-length x) structure-length)
281 (eq? (vector-ref x 0) 'name))))
284 (vector-ref x index)))
288 (vector-set! x index update)))
292 (define noexpand "noexpand")
293 (define *mode* (make-fluid))
295 ;;; hooks to nonportable run-time helpers
302 (define top-level-eval-hook
306 ,(case (fluid-ref *mode*)
307 ((c) ((@ (language tree-il) tree-il->scheme) x))
310 (define local-eval-hook
314 ,(case (fluid-ref *mode*)
315 ((c) ((@ (language tree-il) tree-il->scheme) x))
318 (define-syntax gensym-hook
322 (define put-global-definition-hook
323 (lambda (symbol type val)
324 (let ((existing (let ((v (module-variable (current-module) symbol)))
325 (and v (variable-bound? v)
326 (let ((val (variable-ref v)))
328 (not (syncase-macro-type val))
330 (module-define! (current-module)
333 (make-extended-syncase-macro existing type val)
334 (make-syncase-macro type val))))))
336 (define get-global-definition-hook
337 (lambda (symbol module)
338 (if (and (not module) (current-module))
339 (warn "module system is booted, we should have a module" symbol))
340 (let ((v (module-variable (if module
341 (resolve-module (cdr module))
344 (and v (variable-bound? v)
345 (let ((val (variable-ref v)))
346 (and (macro? val) (syncase-macro-type val)
347 (cons (syncase-macro-type val)
348 (syncase-macro-binding val))))))))
353 ;;; output constructors
356 (case (fluid-ref *mode*)
357 ((c) ((@ (language tree-il) make-void) source))
358 (else '(if #f #f)))))
360 (define build-application
361 (lambda (source fun-exp arg-exps)
362 (case (fluid-ref *mode*)
363 ((c) ((@ (language tree-il) make-application) source fun-exp arg-exps))
364 (else `(,fun-exp . ,arg-exps)))))
366 (define build-conditional
367 (lambda (source test-exp then-exp else-exp)
368 (case (fluid-ref *mode*)
369 ((c) ((@ (language tree-il) make-conditional)
370 source test-exp then-exp else-exp))
371 (else (if (equal? else-exp '(if #f #f))
372 `(if ,test-exp ,then-exp)
373 `(if ,test-exp ,then-exp ,else-exp))))))
375 (define build-lexical-reference
376 (lambda (type source name var)
377 (case (fluid-ref *mode*)
378 ((c) ((@ (language tree-il) make-lexical-ref) source name var))
381 (define build-lexical-assignment
382 (lambda (source name var exp)
383 (case (fluid-ref *mode*)
384 ((c) ((@ (language tree-il) make-lexical-set) source name var exp))
385 (else `(set! ,var ,exp)))))
387 ;; Before modules are booted, we can't expand into data structures from
388 ;; (language tree-il) -- we need to give the evaluator the
389 ;; s-expressions that it understands natively. Actually the real truth
390 ;; of the matter is that the evaluator doesn't understand tree-il
391 ;; structures at all. So until we fix the evaluator, if ever, the
392 ;; conflation that we should use tree-il iff we are compiling
395 (define (analyze-variable mod var modref-cont bare-cont)
398 (let ((kind (car mod))
401 ((public) (modref-cont mod var #t))
402 ((private) (if (not (equal? mod (module-name (current-module))))
403 (modref-cont mod var #f)
405 ((bare) (bare-cont var))
406 ((hygiene) (if (and (not (equal? mod (module-name (current-module))))
407 (module-variable (resolve-module mod) var))
408 (modref-cont mod var #f)
410 (else (syntax-violation #f "bad module kind" var mod))))))
412 (define build-global-reference
413 (lambda (source var mod)
416 (lambda (mod var public?)
417 (case (fluid-ref *mode*)
418 ((c) ((@ (language tree-il) make-module-ref) source mod var public?))
419 (else (list (if public? '@ '@@) mod var))))
421 (case (fluid-ref *mode*)
422 ((c) ((@ (language tree-il) make-toplevel-ref) source var))
425 (define build-global-assignment
426 (lambda (source var exp mod)
429 (lambda (mod var public?)
430 (case (fluid-ref *mode*)
431 ((c) ((@ (language tree-il) make-module-set) source mod var public? exp))
432 (else `(set! ,(list (if public? '@ '@@) mod var) ,exp))))
434 (case (fluid-ref *mode*)
435 ((c) ((@ (language tree-il) make-toplevel-set) source var exp))
436 (else `(set! ,var ,exp)))))))
438 (define build-global-definition
439 (lambda (source var exp)
440 (case (fluid-ref *mode*)
441 ((c) ((@ (language tree-il) make-toplevel-define) source var exp))
442 (else `(define ,var ,exp)))))
445 (lambda (src ids vars docstring exp)
446 (case (fluid-ref *mode*)
447 ((c) ((@ (language tree-il) make-lambda) src ids vars
448 (if docstring `((documentation . ,docstring)) '())
450 (else `(lambda ,vars ,@(if docstring (list docstring) '())
453 (define build-primref
455 (if (equal? (module-name (current-module)) '(guile))
456 (case (fluid-ref *mode*)
457 ((c) ((@ (language tree-il) make-toplevel-ref) src name))
459 (case (fluid-ref *mode*)
460 ((c) ((@ (language tree-il) make-module-ref) src '(guile) name #f))
461 (else `(@@ (guile) ,name))))))
463 (define (build-data src exp)
464 (case (fluid-ref *mode*)
465 ((c) ((@ (language tree-il) make-const) src exp))
466 (else (if (and (self-evaluating? exp) (not (vector? exp)))
468 (list 'quote exp)))))
470 (define build-sequence
472 (if (null? (cdr exps))
474 (case (fluid-ref *mode*)
475 ((c) ((@ (language tree-il) make-sequence) src exps))
476 (else `(begin ,@exps))))))
479 (lambda (src ids vars val-exps body-exp)
482 (case (fluid-ref *mode*)
483 ((c) ((@ (language tree-il) make-let) src ids vars val-exps body-exp))
484 (else `(let ,(map list vars val-exps) ,body-exp))))))
486 (define build-named-let
487 (lambda (src ids vars val-exps body-exp)
492 (case (fluid-ref *mode*)
493 ((c) ((@ (language tree-il) make-letrec) src
496 (list (build-lambda src ids vars #f body-exp))
497 (build-application src (build-lexical-reference 'fun src f-name f)
499 (else `(let ,f ,(map list vars val-exps) ,body-exp))))))
502 (lambda (src ids vars val-exps body-exp)
505 (case (fluid-ref *mode*)
506 ((c) ((@ (language tree-il) make-letrec) src ids vars val-exps body-exp))
507 (else `(letrec ,(map list vars val-exps) ,body-exp))))))
509 ;; FIXME: wingo: use make-lexical ?
510 (define-syntax build-lexical-var
512 ((_ src id) (gensym (symbol->string id)))))
514 (define-structure (syntax-object expression wrap module))
516 (define-syntax unannotate
521 (annotation-expression e)
524 (define-syntax no-source (identifier-syntax #f))
526 (define source-annotation
529 ((annotation? x) (annotation-source x))
530 ((syntax-object? x) (source-annotation (syntax-object-expression x)))
533 (define-syntax arg-check
537 (if (not (pred? x)) (syntax-violation who "invalid argument" x))))))
539 ;;; compile-time environments
541 ;;; wrap and environment comprise two level mapping.
542 ;;; wrap : id --> label
543 ;;; env : label --> <element>
545 ;;; environments are represented in two parts: a lexical part and a global
546 ;;; part. The lexical part is a simple list of associations from labels
547 ;;; to bindings. The global part is implemented by
548 ;;; {put,get}-global-definition-hook and associates symbols with
551 ;;; global (assumed global variable) and displaced-lexical (see below)
552 ;;; do not show up in any environment; instead, they are fabricated by
553 ;;; lookup when it finds no other bindings.
555 ;;; <environment> ::= ((<label> . <binding>)*)
557 ;;; identifier bindings include a type and a value
559 ;;; <binding> ::= (macro . <procedure>) macros
560 ;;; (core . <procedure>) core forms
561 ;;; (external-macro . <procedure>) external-macro
562 ;;; (module-ref . <procedure>) @ or @@
565 ;;; (define-syntax) define-syntax
566 ;;; (local-syntax . rec?) let-syntax/letrec-syntax
567 ;;; (eval-when) eval-when
568 ;;; (syntax . (<var> . <level>)) pattern variables
569 ;;; (global) assumed global variable
570 ;;; (lexical . <var>) lexical variables
571 ;;; (displaced-lexical) displaced lexicals
572 ;;; <level> ::= <nonnegative integer>
573 ;;; <var> ::= variable returned by build-lexical-var
575 ;;; a macro is a user-defined syntactic-form. a core is a system-defined
576 ;;; syntactic form. begin, define, define-syntax, and eval-when are
577 ;;; treated specially since they are sensitive to whether the form is
578 ;;; at top-level and (except for eval-when) can denote valid internal
581 ;;; a pattern variable is a variable introduced by syntax-case and can
582 ;;; be referenced only within a syntax form.
584 ;;; any identifier for which no top-level syntax definition or local
585 ;;; binding of any kind has been seen is assumed to be a global
588 ;;; a lexical variable is a lambda- or letrec-bound variable.
590 ;;; a displaced-lexical identifier is a lexical identifier removed from
591 ;;; it's scope by the return of a syntax object containing the identifier.
592 ;;; a displaced lexical can also appear when a letrec-syntax-bound
593 ;;; keyword is referenced on the rhs of one of the letrec-syntax clauses.
594 ;;; a displaced lexical should never occur with properly written macros.
596 (define-syntax make-binding
597 (syntax-rules (quote)
598 ((_ type value) (cons type value))
600 ((_ type) (cons type '()))))
601 (define binding-type car)
602 (define binding-value cdr)
604 (define-syntax null-env (identifier-syntax '()))
607 (lambda (labels bindings r)
610 (extend-env (cdr labels) (cdr bindings)
611 (cons (cons (car labels) (car bindings)) r)))))
613 (define extend-var-env
614 ; variant of extend-env that forms "lexical" binding
615 (lambda (labels vars r)
618 (extend-var-env (cdr labels) (cdr vars)
619 (cons (cons (car labels) (make-binding 'lexical (car vars))) r)))))
621 ;;; we use a "macros only" environment in expansion of local macro
622 ;;; definitions so that their definitions can use local macros without
623 ;;; attempting to use other lexical identifiers.
624 (define macros-only-env
629 (if (eq? (cadr a) 'macro)
630 (cons a (macros-only-env (cdr r)))
631 (macros-only-env (cdr r)))))))
634 ; x may be a label or a symbol
635 ; although symbols are usually global, we check the environment first
636 ; anyway because a temporary binding may have been established by
642 (or (get-global-definition-hook x mod) (make-binding 'global)))
643 (else (make-binding 'displaced-lexical)))))
645 (define global-extend
646 (lambda (type sym val)
647 (put-global-definition-hook sym type val)))
650 ;;; Conceptually, identifiers are always syntax objects. Internally,
651 ;;; however, the wrap is sometimes maintained separately (a source of
652 ;;; efficiency and confusion), so that symbols are also considered
653 ;;; identifiers by id?. Externally, they are always wrapped.
655 (define nonsymbol-id?
657 (and (syntax-object? x)
658 (symbol? (unannotate (syntax-object-expression x))))))
664 ((syntax-object? x) (symbol? (unannotate (syntax-object-expression x))))
665 ((annotation? x) (symbol? (annotation-expression x)))
668 (define-syntax id-sym-name
672 (unannotate (if (syntax-object? x) (syntax-object-expression x) x))))))
674 (define id-sym-name&marks
676 (if (syntax-object? x)
678 (unannotate (syntax-object-expression x))
679 (join-marks (wrap-marks w) (wrap-marks (syntax-object-wrap x))))
680 (values (unannotate x) (wrap-marks w)))))
682 ;;; syntax object wraps
684 ;;; <wrap> ::= ((<mark> ...) . (<subst> ...))
685 ;;; <subst> ::= <shift> | <subs>
686 ;;; <subs> ::= #(<old name> <label> (<mark> ...))
687 ;;; <shift> ::= positive fixnum
689 (define make-wrap cons)
690 (define wrap-marks car)
691 (define wrap-subst cdr)
693 (define-syntax subst-rename? (identifier-syntax vector?))
694 (define-syntax rename-old (syntax-rules () ((_ x) (vector-ref x 0))))
695 (define-syntax rename-new (syntax-rules () ((_ x) (vector-ref x 1))))
696 (define-syntax rename-marks (syntax-rules () ((_ x) (vector-ref x 2))))
697 (define-syntax make-rename
699 ((_ old new marks) (vector old new marks))))
701 ;;; labels must be comparable with "eq?" and distinct from symbols.
703 (lambda () (string #\i)))
709 (cons (gen-label) (gen-labels (cdr ls))))))
711 (define-structure (ribcage symnames marks labels))
713 (define-syntax empty-wrap (identifier-syntax '(())))
715 (define-syntax top-wrap (identifier-syntax '((top))))
717 (define-syntax top-marked?
719 ((_ w) (memq 'top (wrap-marks w)))))
721 ;;; Marks must be comparable with "eq?" and distinct from pairs and
722 ;;; the symbol top. We do not use integers so that marks will remain
723 ;;; unique even across file compiles.
725 (define-syntax the-anti-mark (identifier-syntax #f))
729 (make-wrap (cons the-anti-mark (wrap-marks w))
730 (cons 'shift (wrap-subst w)))))
732 (define-syntax new-mark
736 ;;; make-empty-ribcage and extend-ribcage maintain list-based ribcages for
737 ;;; internal definitions, in which the ribcages are built incrementally
738 (define-syntax make-empty-ribcage
740 ((_) (make-ribcage '() '() '()))))
742 (define extend-ribcage!
743 ; must receive ids with complete wraps
744 (lambda (ribcage id label)
745 (set-ribcage-symnames! ribcage
746 (cons (unannotate (syntax-object-expression id))
747 (ribcage-symnames ribcage)))
748 (set-ribcage-marks! ribcage
749 (cons (wrap-marks (syntax-object-wrap id))
750 (ribcage-marks ribcage)))
751 (set-ribcage-labels! ribcage
752 (cons label (ribcage-labels ribcage)))))
754 ;;; make-binding-wrap creates vector-based ribcages
755 (define make-binding-wrap
756 (lambda (ids labels w)
762 (let ((labelvec (list->vector labels)))
763 (let ((n (vector-length labelvec)))
764 (let ((symnamevec (make-vector n)) (marksvec (make-vector n)))
765 (let f ((ids ids) (i 0))
766 (if (not (null? ids))
768 (lambda () (id-sym-name&marks (car ids) w))
769 (lambda (symname marks)
770 (vector-set! symnamevec i symname)
771 (vector-set! marksvec i marks)
772 (f (cdr ids) (fx+ i 1))))))
773 (make-ribcage symnamevec marksvec labelvec))))
784 (let ((m1 (wrap-marks w1)) (s1 (wrap-subst w1)))
790 (smart-append s1 (wrap-subst w2))))
792 (smart-append m1 (wrap-marks w2))
793 (smart-append s1 (wrap-subst w2)))))))
797 (smart-append m1 m2)))
804 (eq? (car x) (car y))
805 (same-marks? (cdr x) (cdr y))))))
811 ((_ e) (call-with-values (lambda () e) (lambda (x . ignore) x)))))
813 (lambda (sym subst marks)
816 (let ((fst (car subst)))
818 (search sym (cdr subst) (cdr marks))
819 (let ((symnames (ribcage-symnames fst)))
820 (if (vector? symnames)
821 (search-vector-rib sym subst marks symnames fst)
822 (search-list-rib sym subst marks symnames fst))))))))
823 (define search-list-rib
824 (lambda (sym subst marks symnames ribcage)
825 (let f ((symnames symnames) (i 0))
827 ((null? symnames) (search sym (cdr subst) marks))
828 ((and (eq? (car symnames) sym)
829 (same-marks? marks (list-ref (ribcage-marks ribcage) i)))
830 (values (list-ref (ribcage-labels ribcage) i) marks))
831 (else (f (cdr symnames) (fx+ i 1)))))))
832 (define search-vector-rib
833 (lambda (sym subst marks symnames ribcage)
834 (let ((n (vector-length symnames)))
837 ((fx= i n) (search sym (cdr subst) marks))
838 ((and (eq? (vector-ref symnames i) sym)
839 (same-marks? marks (vector-ref (ribcage-marks ribcage) i)))
840 (values (vector-ref (ribcage-labels ribcage) i) marks))
841 (else (f (fx+ i 1))))))))
844 (or (first (search id (wrap-subst w) (wrap-marks w))) id))
846 (let ((id (unannotate (syntax-object-expression id)))
847 (w1 (syntax-object-wrap id)))
848 (let ((marks (join-marks (wrap-marks w) (wrap-marks w1))))
849 (call-with-values (lambda () (search id (wrap-subst w) marks))
850 (lambda (new-id marks)
852 (first (search id (wrap-subst w1) marks))
855 (let ((id (unannotate id)))
856 (or (first (search id (wrap-subst w) (wrap-marks w))) id)))
857 (else (syntax-violation 'id-var-name "invalid id" id)))))
859 ;;; free-id=? must be passed fully wrapped ids since (free-id=? x y)
860 ;;; may be true even if (free-id=? (wrap x w) (wrap y w)) is not.
864 (and (eq? (id-sym-name i) (id-sym-name j)) ; accelerator
865 (eq? (id-var-name i empty-wrap) (id-var-name j empty-wrap)))))
867 ;;; bound-id=? may be passed unwrapped (or partially wrapped) ids as
868 ;;; long as the missing portion of the wrap is common to both of the ids
869 ;;; since (bound-id=? x y) iff (bound-id=? (wrap x w) (wrap y w))
873 (if (and (syntax-object? i) (syntax-object? j))
874 (and (eq? (unannotate (syntax-object-expression i))
875 (unannotate (syntax-object-expression j)))
876 (same-marks? (wrap-marks (syntax-object-wrap i))
877 (wrap-marks (syntax-object-wrap j))))
878 (eq? (unannotate i) (unannotate j)))))
880 ;;; "valid-bound-ids?" returns #t if it receives a list of distinct ids.
881 ;;; valid-bound-ids? may be passed unwrapped (or partially wrapped) ids
882 ;;; as long as the missing portion of the wrap is common to all of the
885 (define valid-bound-ids?
887 (and (let all-ids? ((ids ids))
890 (all-ids? (cdr ids)))))
891 (distinct-bound-ids? ids))))
893 ;;; distinct-bound-ids? expects a list of ids and returns #t if there are
894 ;;; no duplicates. It is quadratic on the length of the id list; long
895 ;;; lists could be sorted to make it more efficient. distinct-bound-ids?
896 ;;; may be passed unwrapped (or partially wrapped) ids as long as the
897 ;;; missing portion of the wrap is common to all of the ids.
899 (define distinct-bound-ids?
901 (let distinct? ((ids ids))
903 (and (not (bound-id-member? (car ids) (cdr ids)))
904 (distinct? (cdr ids)))))))
906 (define bound-id-member?
908 (and (not (null? list))
909 (or (bound-id=? x (car list))
910 (bound-id-member? x (cdr list))))))
912 ;;; wrapping expressions and identifiers
917 ((and (null? (wrap-marks w)) (null? (wrap-subst w))) x)
920 (syntax-object-expression x)
921 (join-wraps w (syntax-object-wrap x))
922 (syntax-object-module x)))
924 (else (make-syntax-object x w defmod)))))
927 (lambda (x w s defmod)
928 (wrap (if s (make-annotation x s #f) x) w defmod)))
933 (lambda (body r w s mod)
935 (let dobody ((body body) (r r) (w w) (mod mod))
938 (let ((first (chi (car body) r w mod)))
939 (cons first (dobody (cdr body) r w mod))))))))
941 (define chi-top-sequence
942 (lambda (body r w s m esew mod)
944 (let dobody ((body body) (r r) (w w) (m m) (esew esew) (mod mod))
947 (let ((first (chi-top (car body) r w m esew mod)))
948 (cons first (dobody (cdr body) r w m esew mod))))))))
950 (define chi-install-global
952 (build-global-definition
955 ;; FIXME: seems nasty to call current-module here
956 (if (let ((v (module-variable (current-module) name)))
957 ;; FIXME use primitive-macro?
958 (and v (variable-bound? v) (macro? (variable-ref v))
959 (not (eq? (macro-type (variable-ref v)) 'syncase-macro))))
962 (build-primref no-source 'make-extended-syncase-macro)
963 (list (build-application
965 (build-primref no-source 'module-ref)
966 (list (build-application
968 (build-primref no-source 'current-module)
970 (build-data no-source name)))
971 (build-data no-source 'macro)
975 (build-primref no-source 'make-syncase-macro)
976 (list (build-data no-source 'macro) e))))))
978 (define chi-when-list
979 (lambda (e when-list w)
980 ; when-list is syntax'd version of list of situations
981 (let f ((when-list when-list) (situations '()))
982 (if (null? when-list)
985 (cons (let ((x (car when-list)))
987 ((free-id=? x (syntax compile)) 'compile)
988 ((free-id=? x (syntax load)) 'load)
989 ((free-id=? x (syntax eval)) 'eval)
990 (else (syntax-violation 'eval-when
995 ;;; syntax-type returns six values: type, value, e, w, s, and mod. The
996 ;;; first two are described in the table below.
998 ;;; type value explanation
999 ;;; -------------------------------------------------------------------
1000 ;;; core procedure core form (including singleton)
1001 ;;; external-macro procedure external macro
1002 ;;; module-ref procedure @ or @@ form
1003 ;;; lexical name lexical variable reference
1004 ;;; global name global variable reference
1005 ;;; begin none begin keyword
1006 ;;; define none define keyword
1007 ;;; define-syntax none define-syntax keyword
1008 ;;; local-syntax rec? letrec-syntax/let-syntax keyword
1009 ;;; eval-when none eval-when keyword
1010 ;;; syntax level pattern variable
1011 ;;; displaced-lexical none displaced lexical identifier
1012 ;;; lexical-call name call to lexical variable
1013 ;;; global-call name call to global variable
1014 ;;; call none any other call
1015 ;;; begin-form none begin expression
1016 ;;; define-form id variable definition
1017 ;;; define-syntax-form id syntax definition
1018 ;;; local-syntax-form rec? syntax definition
1019 ;;; eval-when-form none eval-when form
1020 ;;; constant none self-evaluating datum
1021 ;;; other none anything else
1023 ;;; For define-form and define-syntax-form, e is the rhs expression.
1024 ;;; For all others, e is the entire form. w is the wrap for e.
1025 ;;; s is the source for the entire form. mod is the module for e.
1027 ;;; syntax-type expands macros and unwraps as necessary to get to
1028 ;;; one of the forms above. It also parses define and define-syntax
1029 ;;; forms, although perhaps this should be done by the consumer.
1032 (lambda (e r w s rib mod)
1035 (let* ((n (id-var-name e w))
1036 (b (lookup n r mod))
1037 (type (binding-type b)))
1039 ((lexical) (values type (binding-value b) e w s mod))
1040 ((global) (values type n e w s mod))
1042 (syntax-type (chi-macro (binding-value b) e r w rib mod)
1043 r empty-wrap s rib mod))
1044 (else (values type (binding-value b) e w s mod)))))
1046 (let ((first (car e)))
1048 (let* ((n (id-var-name first w))
1049 (b (lookup n r (or (and (syntax-object? first)
1050 (syntax-object-module first))
1052 (type (binding-type b)))
1055 (values 'lexical-call (binding-value b) e w s mod))
1057 (values 'global-call n e w s mod))
1059 (syntax-type (chi-macro (binding-value b) e r w rib mod)
1060 r empty-wrap s rib mod))
1061 ((core external-macro module-ref)
1062 (values type (binding-value b) e w s mod))
1064 (values 'local-syntax-form (binding-value b) e w s mod))
1066 (values 'begin-form #f e w s mod))
1068 (values 'eval-when-form #f e w s mod))
1073 (values 'define-form (syntax name) (syntax val) w s mod))
1074 ((_ (name . args) e1 e2 ...)
1075 (and (id? (syntax name))
1076 (valid-bound-ids? (lambda-var-list (syntax args))))
1077 ; need lambda here...
1078 (values 'define-form (wrap (syntax name) w mod)
1079 (cons (syntax lambda) (wrap (syntax (args e1 e2 ...)) w mod))
1083 (values 'define-form (wrap (syntax name) w mod)
1085 empty-wrap s mod))))
1090 (values 'define-syntax-form (syntax name)
1091 (syntax val) w s mod))))
1093 (values 'call #f e w s mod))))
1094 (values 'call #f e w s mod))))
1096 ;; s can't be valid source if we've unwrapped
1097 (syntax-type (syntax-object-expression e)
1099 (join-wraps w (syntax-object-wrap e))
1100 no-source rib (or (syntax-object-module e) mod)))
1102 (syntax-type (annotation-expression e) r w (annotation-source e) rib mod))
1103 ((self-evaluating? e) (values 'constant #f e w s mod))
1104 (else (values 'other #f e w s mod)))))
1107 (lambda (e r w m esew mod)
1108 (define-syntax eval-if-c&e
1112 (if (eq? m 'c&e) (top-level-eval-hook x mod))
1115 (lambda () (syntax-type e r w no-source #f mod))
1116 (lambda (type value e w s mod)
1122 (chi-top-sequence (syntax (e1 e2 ...)) r w s m esew mod))))
1123 ((local-syntax-form)
1124 (chi-local-syntax value e r w s mod
1125 (lambda (body r w s mod)
1126 (chi-top-sequence body r w s m esew mod))))
1129 ((_ (x ...) e1 e2 ...)
1130 (let ((when-list (chi-when-list e (syntax (x ...)) w))
1131 (body (syntax (e1 e2 ...))))
1134 (if (memq 'eval when-list)
1135 (chi-top-sequence body r w s 'e '(eval) mod)
1137 ((memq 'load when-list)
1138 (if (or (memq 'compile when-list)
1139 (and (eq? m 'c&e) (memq 'eval when-list)))
1140 (chi-top-sequence body r w s 'c&e '(compile load) mod)
1141 (if (memq m '(c c&e))
1142 (chi-top-sequence body r w s 'c '(load) mod)
1144 ((or (memq 'compile when-list)
1145 (and (eq? m 'c&e) (memq 'eval when-list)))
1146 (top-level-eval-hook
1147 (chi-top-sequence body r w s 'e '(eval) mod)
1150 (else (chi-void)))))))
1151 ((define-syntax-form)
1152 (let ((n (id-var-name value w)) (r (macros-only-env r)))
1155 (if (memq 'compile esew)
1156 (let ((e (chi-install-global n (chi e r w mod))))
1157 (top-level-eval-hook e mod)
1158 (if (memq 'load esew) e (chi-void)))
1159 (if (memq 'load esew)
1160 (chi-install-global n (chi e r w mod))
1163 (let ((e (chi-install-global n (chi e r w mod))))
1164 (top-level-eval-hook e mod)
1167 (if (memq 'eval esew)
1168 (top-level-eval-hook
1169 (chi-install-global n (chi e r w mod))
1173 (let* ((n (id-var-name value w))
1174 (type (binding-type (lookup n r mod))))
1176 ((global core macro module-ref)
1178 (build-global-definition s n (chi e r w mod))
1180 ((displaced-lexical)
1181 (syntax-violation #f "identifier out of context"
1182 e (wrap value w mod)))
1184 (syntax-violation #f "cannot define keyword at top level"
1185 e (wrap value w mod))))))
1186 (else (eval-if-c&e m (chi-expr type value e r w s mod) mod)))))))
1191 (lambda () (syntax-type e r w no-source #f mod))
1192 (lambda (type value e w s mod)
1193 (chi-expr type value e r w s mod)))))
1196 (lambda (type value e r w s mod)
1199 (build-lexical-reference 'value s e value))
1200 ((core external-macro)
1201 ;; apply transformer
1202 (value e r w s mod))
1204 (call-with-values (lambda () (value e))
1205 ;; we could add a public? arg here
1206 (lambda (id mod) (build-global-reference s id mod))))
1209 (build-lexical-reference 'fun (source-annotation (car e))
1214 (build-global-reference (source-annotation (car e)) value
1215 (if (syntax-object? (car e))
1216 (syntax-object-module (car e))
1219 ((constant) (build-data s (strip (source-wrap e w s mod) empty-wrap)))
1220 ((global) (build-global-reference s value mod))
1221 ((call) (chi-application (chi (car e) r w mod) e r w s mod))
1224 ((_ e1 e2 ...) (chi-sequence (syntax (e1 e2 ...)) r w s mod))))
1225 ((local-syntax-form)
1226 (chi-local-syntax value e r w s mod chi-sequence))
1229 ((_ (x ...) e1 e2 ...)
1230 (let ((when-list (chi-when-list e (syntax (x ...)) w)))
1231 (if (memq 'eval when-list)
1232 (chi-sequence (syntax (e1 e2 ...)) r w s mod)
1234 ((define-form define-syntax-form)
1235 (syntax-violation #f "definition in expression context"
1236 e (wrap value w mod)))
1238 (syntax-violation #f "reference to pattern variable outside syntax form"
1239 (source-wrap e w s mod)))
1240 ((displaced-lexical)
1241 (syntax-violation #f "reference to identifier outside its scope"
1242 (source-wrap e w s mod)))
1243 (else (syntax-violation #f "unexpected syntax"
1244 (source-wrap e w s mod))))))
1246 (define chi-application
1247 (lambda (x e r w s mod)
1250 (build-application s x
1251 (map (lambda (e) (chi e r w mod)) (syntax (e1 ...))))))))
1254 (lambda (p e r w rib mod)
1255 (define rebuild-macro-output
1258 (cons (rebuild-macro-output (car x) m)
1259 (rebuild-macro-output (cdr x) m)))
1261 (let ((w (syntax-object-wrap x)))
1262 (let ((ms (wrap-marks w)) (s (wrap-subst w)))
1263 (if (and (pair? ms) (eq? (car ms) the-anti-mark))
1264 ;; output is from original text
1266 (syntax-object-expression x)
1267 (make-wrap (cdr ms) (if rib (cons rib (cdr s)) (cdr s)))
1268 (syntax-object-module x))
1269 ;; output introduced by macro
1271 (syntax-object-expression x)
1272 (make-wrap (cons m ms)
1274 (cons rib (cons 'shift s))
1276 (let ((pmod (procedure-module p)))
1278 ;; hither the hygiene
1279 (cons 'hygiene (module-name pmod))
1280 ;; but it's possible for the proc to have
1281 ;; no mod, if it was made before modules
1283 '(hygiene guile))))))))
1285 (let* ((n (vector-length x)) (v (make-vector n)))
1286 (do ((i 0 (fx+ i 1)))
1289 (rebuild-macro-output (vector-ref x i) m)))))
1291 (syntax-violation #f "encountered raw symbol in macro output"
1292 (source-wrap e w s mod) x))
1294 (rebuild-macro-output (p (wrap e (anti-mark w) mod)) (new-mark))))
1297 ;; In processing the forms of the body, we create a new, empty wrap.
1298 ;; This wrap is augmented (destructively) each time we discover that
1299 ;; the next form is a definition. This is done:
1301 ;; (1) to allow the first nondefinition form to be a call to
1302 ;; one of the defined ids even if the id previously denoted a
1303 ;; definition keyword or keyword for a macro expanding into a
1305 ;; (2) to prevent subsequent definition forms (but unfortunately
1306 ;; not earlier ones) and the first nondefinition form from
1307 ;; confusing one of the bound identifiers for an auxiliary
1309 ;; (3) so that we do not need to restart the expansion of the
1310 ;; first nondefinition form, which is problematic anyway
1311 ;; since it might be the first element of a begin that we
1312 ;; have just spliced into the body (meaning if we restarted,
1313 ;; we'd really need to restart with the begin or the macro
1314 ;; call that expanded into the begin, and we'd have to give
1315 ;; up allowing (begin <defn>+ <expr>+), which is itself
1316 ;; problematic since we don't know if a begin contains only
1317 ;; definitions until we've expanded it).
1319 ;; Before processing the body, we also create a new environment
1320 ;; containing a placeholder for the bindings we will add later and
1321 ;; associate this environment with each form. In processing a
1322 ;; let-syntax or letrec-syntax, the associated environment may be
1323 ;; augmented with local keyword bindings, so the environment may
1324 ;; be different for different forms in the body. Once we have
1325 ;; gathered up all of the definitions, we evaluate the transformer
1326 ;; expressions and splice into r at the placeholder the new variable
1327 ;; and keyword bindings. This allows let-syntax or letrec-syntax
1328 ;; forms local to a portion or all of the body to shadow the
1329 ;; definition bindings.
1331 ;; Subforms of a begin, let-syntax, or letrec-syntax are spliced
1334 ;; outer-form is fully wrapped w/source
1335 (lambda (body outer-form r w mod)
1336 (let* ((r (cons '("placeholder" . (placeholder)) r))
1337 (ribcage (make-empty-ribcage))
1338 (w (make-wrap (wrap-marks w) (cons ribcage (wrap-subst w)))))
1339 (let parse ((body (map (lambda (x) (cons r (wrap x w mod))) body))
1340 (ids '()) (labels '()) (vars '()) (vals '()) (bindings '()))
1342 (syntax-violation #f "no expressions in body" outer-form)
1343 (let ((e (cdar body)) (er (caar body)))
1345 (lambda () (syntax-type e er empty-wrap no-source ribcage mod))
1346 (lambda (type value e w s mod)
1349 (let ((id (wrap value w mod)) (label (gen-label)))
1350 (let ((var (gen-var id)))
1351 (extend-ribcage! ribcage id label)
1353 (cons id ids) (cons label labels)
1354 (cons var vars) (cons (cons er (wrap e w mod)) vals)
1355 (cons (make-binding 'lexical var) bindings)))))
1356 ((define-syntax-form)
1357 (let ((id (wrap value w mod)) (label (gen-label)))
1358 (extend-ribcage! ribcage id label)
1360 (cons id ids) (cons label labels)
1362 (cons (make-binding 'macro (cons er (wrap e w mod)))
1367 (parse (let f ((forms (syntax (e1 ...))))
1370 (cons (cons er (wrap (car forms) w mod))
1372 ids labels vars vals bindings))))
1373 ((local-syntax-form)
1374 (chi-local-syntax value e er w s mod
1375 (lambda (forms er w s mod)
1376 (parse (let f ((forms forms))
1379 (cons (cons er (wrap (car forms) w mod))
1381 ids labels vars vals bindings))))
1382 (else ; found a non-definition
1384 (build-sequence no-source
1386 (chi (cdr x) (car x) empty-wrap mod))
1387 (cons (cons er (source-wrap e w s mod))
1390 (if (not (valid-bound-ids? ids))
1392 #f "invalid or duplicate identifier in definition"
1394 (let loop ((bs bindings) (er-cache #f) (r-cache #f))
1395 (if (not (null? bs))
1396 (let* ((b (car bs)))
1397 (if (eq? (car b) 'macro)
1398 (let* ((er (cadr b))
1400 (if (eq? er er-cache)
1402 (macros-only-env er))))
1404 (eval-local-transformer
1405 (chi (cddr b) r-cache empty-wrap mod)
1407 (loop (cdr bs) er r-cache))
1408 (loop (cdr bs) er-cache r-cache)))))
1409 (set-cdr! r (extend-env labels bindings (cdr r)))
1410 (build-letrec no-source
1411 (map syntax->datum ids)
1414 (chi (cdr x) (car x) empty-wrap mod))
1416 (build-sequence no-source
1418 (chi (cdr x) (car x) empty-wrap mod))
1419 (cons (cons er (source-wrap e w s mod))
1420 (cdr body)))))))))))))))))
1422 (define chi-lambda-clause
1423 (lambda (e docstring c r w mod k)
1425 ((args doc e1 e2 ...)
1426 (and (string? (syntax->datum (syntax doc))) (not docstring))
1427 (chi-lambda-clause e (syntax doc) (syntax (args e1 e2 ...)) r w mod k))
1428 (((id ...) e1 e2 ...)
1429 (let ((ids (syntax (id ...))))
1430 (if (not (valid-bound-ids? ids))
1431 (syntax-violation 'lambda "invalid parameter list" e)
1432 (let ((labels (gen-labels ids))
1433 (new-vars (map gen-var ids)))
1434 (k (map syntax->datum ids)
1436 (and docstring (syntax->datum docstring))
1437 (chi-body (syntax (e1 e2 ...))
1439 (extend-var-env labels new-vars r)
1440 (make-binding-wrap ids labels w)
1443 (let ((old-ids (lambda-var-list (syntax ids))))
1444 (if (not (valid-bound-ids? old-ids))
1445 (syntax-violation 'lambda "invalid parameter list" e)
1446 (let ((labels (gen-labels old-ids))
1447 (new-vars (map gen-var old-ids)))
1448 (k (let f ((ls1 (cdr old-ids)) (ls2 (car old-ids)))
1451 (f (cdr ls1) (cons (syntax->datum (car ls1)) ls2))))
1452 (let f ((ls1 (cdr new-vars)) (ls2 (car new-vars)))
1455 (f (cdr ls1) (cons (car ls1) ls2))))
1456 (and docstring (syntax->datum docstring))
1457 (chi-body (syntax (e1 e2 ...))
1459 (extend-var-env labels new-vars r)
1460 (make-binding-wrap old-ids labels w)
1462 (_ (syntax-violation 'lambda "bad lambda" e)))))
1464 (define chi-local-syntax
1465 (lambda (rec? e r w s mod k)
1467 ((_ ((id val) ...) e1 e2 ...)
1468 (let ((ids (syntax (id ...))))
1469 (if (not (valid-bound-ids? ids))
1470 (syntax-violation #f "duplicate bound keyword" e)
1471 (let ((labels (gen-labels ids)))
1472 (let ((new-w (make-binding-wrap ids labels w)))
1473 (k (syntax (e1 e2 ...))
1476 (let ((w (if rec? new-w w))
1477 (trans-r (macros-only-env r)))
1479 (make-binding 'macro
1480 (eval-local-transformer
1481 (chi x trans-r w mod)
1483 (syntax (val ...))))
1488 (_ (syntax-violation #f "bad local syntax definition"
1489 (source-wrap e w s mod))))))
1491 (define eval-local-transformer
1492 (lambda (expanded mod)
1493 (let ((p (local-eval-hook expanded mod)))
1496 (syntax-violation #f "nonprocedure transformer" p)))))
1500 (build-void no-source)))
1504 (and (nonsymbol-id? x)
1505 (free-id=? x (syntax (... ...))))))
1509 ;;; strips all annotations from potentially circular reader output
1511 (define strip-annotation
1515 (let ((new (cons #f #f)))
1516 (if parent (set-annotation-stripped! parent new))
1517 (set-car! new (strip-annotation (car x) #f))
1518 (set-cdr! new (strip-annotation (cdr x) #f))
1521 (or (annotation-stripped x)
1522 (strip-annotation (annotation-expression x) x)))
1524 (let ((new (make-vector (vector-length x))))
1525 (if parent (set-annotation-stripped! parent new))
1526 (let loop ((i (- (vector-length x) 1)))
1528 (vector-set! new i (strip-annotation (vector-ref x i) #f))
1533 ;;; strips syntax-objects down to top-wrap; if top-wrap is layered directly
1534 ;;; on an annotation, strips the annotation as well.
1535 ;;; since only the head of a list is annotated by the reader, not each pair
1536 ;;; in the spine, we also check for pairs whose cars are annotated in case
1537 ;;; we've been passed the cdr of an annotated list
1542 (if (or (annotation? x) (and (pair? x) (annotation? (car x))))
1543 (strip-annotation x #f)
1548 (strip (syntax-object-expression x) (syntax-object-wrap x)))
1550 (let ((a (f (car x))) (d (f (cdr x))))
1551 (if (and (eq? a (car x)) (eq? d (cdr x)))
1555 (let ((old (vector->list x)))
1556 (let ((new (map f old)))
1557 (if (and-map* eq? old new) x (list->vector new)))))
1560 ;;; lexical variables
1564 (let ((id (if (syntax-object? id) (syntax-object-expression id) id)))
1565 (if (annotation? id)
1566 (build-lexical-var (annotation-source id) (annotation-expression id))
1567 (build-lexical-var no-source id)))))
1569 (define lambda-var-list
1571 (let lvl ((vars vars) (ls '()) (w empty-wrap))
1573 ((pair? vars) (lvl (cdr vars) (cons (wrap (car vars) w #f) ls) w))
1574 ((id? vars) (cons (wrap vars w #f) ls))
1576 ((syntax-object? vars)
1577 (lvl (syntax-object-expression vars)
1579 (join-wraps w (syntax-object-wrap vars))))
1581 (lvl (annotation-expression vars) ls w))
1582 ; include anything else to be caught by subsequent error
1584 (else (cons vars ls))))))
1586 ;;; core transformers
1588 (global-extend 'local-syntax 'letrec-syntax #t)
1589 (global-extend 'local-syntax 'let-syntax #f)
1591 (global-extend 'core 'fluid-let-syntax
1592 (lambda (e r w s mod)
1594 ((_ ((var val) ...) e1 e2 ...)
1595 (valid-bound-ids? (syntax (var ...)))
1596 (let ((names (map (lambda (x) (id-var-name x w)) (syntax (var ...)))))
1599 (case (binding-type (lookup n r mod))
1600 ((displaced-lexical)
1601 (syntax-violation 'fluid-let-syntax
1602 "identifier out of context"
1604 (source-wrap id w s mod)))))
1608 (syntax (e1 e2 ...))
1609 (source-wrap e w s mod)
1612 (let ((trans-r (macros-only-env r)))
1614 (make-binding 'macro
1615 (eval-local-transformer (chi x trans-r w mod)
1617 (syntax (val ...))))
1621 (_ (syntax-violation 'fluid-let-syntax "bad syntax"
1622 (source-wrap e w s mod))))))
1624 (global-extend 'core 'quote
1625 (lambda (e r w s mod)
1627 ((_ e) (build-data s (strip (syntax e) w)))
1628 (_ (syntax-violation 'quote "bad syntax"
1629 (source-wrap e w s mod))))))
1631 (global-extend 'core 'syntax
1634 (lambda (src e r maps ellipsis? mod)
1636 (let ((label (id-var-name e empty-wrap)))
1637 (let ((b (lookup label r mod)))
1638 (if (eq? (binding-type b) 'syntax)
1641 (let ((var.lev (binding-value b)))
1642 (gen-ref src (car var.lev) (cdr var.lev) maps)))
1643 (lambda (var maps) (values `(ref ,var) maps)))
1645 (syntax-violation 'syntax "misplaced ellipsis" src)
1646 (values `(quote ,e) maps)))))
1649 (ellipsis? (syntax dots))
1650 (gen-syntax src (syntax e) r maps (lambda (x) #f) mod))
1652 ; this could be about a dozen lines of code, except that we
1653 ; choose to handle (syntax (x ... ...)) forms
1654 (ellipsis? (syntax dots))
1655 (let f ((y (syntax y))
1659 (gen-syntax src (syntax x) r
1660 (cons '() maps) ellipsis? mod))
1662 (if (null? (car maps))
1663 (syntax-violation 'syntax "extra ellipsis"
1665 (values (gen-map x (car maps))
1669 (ellipsis? (syntax dots))
1673 (lambda () (k (cons '() maps)))
1675 (if (null? (car maps))
1676 (syntax-violation 'syntax "extra ellipsis" src)
1677 (values (gen-mappend x (car maps))
1679 (_ (call-with-values
1680 (lambda () (gen-syntax src y r maps ellipsis? mod))
1683 (lambda () (k maps))
1685 (values (gen-append x y) maps)))))))))
1688 (lambda () (gen-syntax src (syntax x) r maps ellipsis? mod))
1691 (lambda () (gen-syntax src (syntax y) r maps ellipsis? mod))
1692 (lambda (y maps) (values (gen-cons x y) maps))))))
1696 (gen-syntax src (syntax (e1 e2 ...)) r maps ellipsis? mod))
1697 (lambda (e maps) (values (gen-vector e) maps))))
1698 (_ (values `(quote ,e) maps))))))
1701 (lambda (src var level maps)
1705 (syntax-violation 'syntax "missing ellipsis" src)
1707 (lambda () (gen-ref src var (fx- level 1) (cdr maps)))
1708 (lambda (outer-var outer-maps)
1709 (let ((b (assq outer-var (car maps))))
1711 (values (cdr b) maps)
1712 (let ((inner-var (gen-var 'tmp)))
1714 (cons (cons (cons outer-var inner-var)
1716 outer-maps)))))))))))
1720 `(apply (primitive append) ,(gen-map e map-env))))
1724 (let ((formals (map cdr map-env))
1725 (actuals (map (lambda (x) `(ref ,(car x))) map-env)))
1728 ; identity map equivalence:
1729 ; (map (lambda (x) x) y) == y
1732 (lambda (x) (and (eq? (car x) 'ref) (memq (cadr x) formals)))
1734 ; eta map equivalence:
1735 ; (map (lambda (x ...) (f x ...)) y ...) == (map f y ...)
1736 `(map (primitive ,(car e))
1737 ,@(map (let ((r (map cons formals actuals)))
1738 (lambda (x) (cdr (assq (cadr x) r))))
1740 (else `(map (lambda ,formals ,e) ,@actuals))))))
1746 (if (eq? (car x) 'quote)
1747 `(quote (,(cadr x) . ,(cadr y)))
1748 (if (eq? (cadr y) '())
1751 ((list) `(list ,x ,@(cdr y)))
1752 (else `(cons ,x ,y)))))
1756 (if (equal? y '(quote ()))
1763 ((eq? (car x) 'list) `(vector ,@(cdr x)))
1764 ((eq? (car x) 'quote) `(quote #(,@(cadr x))))
1765 (else `(list->vector ,x)))))
1771 ((ref) (build-lexical-reference 'value no-source (cadr x) (cadr x)))
1772 ((primitive) (build-primref no-source (cadr x)))
1773 ((quote) (build-data no-source (cadr x)))
1774 ((lambda) (build-lambda no-source (cadr x) (cadr x) #f (regen (caddr x))))
1775 ((map) (let ((ls (map regen (cdr x))))
1776 (build-application no-source
1777 ;; this check used to be here, not sure what for:
1778 ;; (if (fx= (length ls) 2)
1779 (build-primref no-source 'map)
1781 (else (build-application no-source
1782 (build-primref no-source (car x))
1783 (map regen (cdr x)))))))
1785 (lambda (e r w s mod)
1786 (let ((e (source-wrap e w s mod)))
1790 (lambda () (gen-syntax e (syntax x) r '() ellipsis? mod))
1791 (lambda (e maps) (regen e))))
1792 (_ (syntax-violation 'syntax "bad `syntax' form" e)))))))
1795 (global-extend 'core 'lambda
1796 (lambda (e r w s mod)
1799 (chi-lambda-clause (source-wrap e w s mod) #f (syntax c) r w mod
1800 (lambda (names vars docstring body)
1801 (build-lambda s names vars docstring body)))))))
1804 (global-extend 'core 'let
1806 (define (chi-let e r w s mod constructor ids vals exps)
1807 (if (not (valid-bound-ids? ids))
1808 (syntax-violation 'let "duplicate bound variable" e)
1809 (let ((labels (gen-labels ids))
1810 (new-vars (map gen-var ids)))
1811 (let ((nw (make-binding-wrap ids labels w))
1812 (nr (extend-var-env labels new-vars r)))
1814 (map syntax->datum ids)
1816 (map (lambda (x) (chi x r w mod)) vals)
1817 (chi-body exps (source-wrap e nw s mod)
1819 (lambda (e r w s mod)
1821 ((_ ((id val) ...) e1 e2 ...)
1822 (chi-let e r w s mod
1826 (syntax (e1 e2 ...))))
1827 ((_ f ((id val) ...) e1 e2 ...)
1829 (chi-let e r w s mod
1833 (syntax (e1 e2 ...))))
1834 (_ (syntax-violation 'let "bad let" (source-wrap e w s mod)))))))
1837 (global-extend 'core 'letrec
1838 (lambda (e r w s mod)
1840 ((_ ((id val) ...) e1 e2 ...)
1841 (let ((ids (syntax (id ...))))
1842 (if (not (valid-bound-ids? ids))
1843 (syntax-violation 'letrec "duplicate bound variable" e)
1844 (let ((labels (gen-labels ids))
1845 (new-vars (map gen-var ids)))
1846 (let ((w (make-binding-wrap ids labels w))
1847 (r (extend-var-env labels new-vars r)))
1849 (map syntax->datum ids)
1851 (map (lambda (x) (chi x r w mod)) (syntax (val ...)))
1852 (chi-body (syntax (e1 e2 ...))
1853 (source-wrap e w s mod) r w mod)))))))
1854 (_ (syntax-violation 'letrec "bad letrec" (source-wrap e w s mod))))))
1857 (global-extend 'core 'set!
1858 (lambda (e r w s mod)
1862 (let ((val (chi (syntax val) r w mod))
1863 (n (id-var-name (syntax id) w)))
1864 (let ((b (lookup n r mod)))
1865 (case (binding-type b)
1867 (build-lexical-assignment s
1868 (syntax->datum (syntax id))
1871 ((global) (build-global-assignment s n val mod))
1872 ((displaced-lexical)
1873 (syntax-violation 'set! "identifier out of context"
1874 (wrap (syntax id) w mod)))
1875 (else (syntax-violation 'set! "bad set!"
1876 (source-wrap e w s mod)))))))
1877 ((_ (head tail ...) val)
1879 (lambda () (syntax-type (syntax head) r empty-wrap no-source #f mod))
1880 (lambda (type value ee ww ss modmod)
1883 (let ((val (chi (syntax val) r w mod)))
1884 (call-with-values (lambda () (value (syntax (head tail ...))))
1886 (build-global-assignment s id val mod)))))
1888 (build-application s
1889 (chi (syntax (setter head)) r w mod)
1890 (map (lambda (e) (chi e r w mod))
1891 (syntax (tail ... val)))))))))
1892 (_ (syntax-violation 'set! "bad set!" (source-wrap e w s mod))))))
1894 (global-extend 'module-ref '@
1898 (and (and-map id? (syntax (mod ...))) (id? (syntax id)))
1899 (values (syntax->datum (syntax id))
1901 (syntax (public mod ...))))))))
1903 (global-extend 'module-ref '@@
1907 (and (and-map id? (syntax (mod ...))) (id? (syntax id)))
1908 (values (syntax->datum (syntax id))
1910 (syntax (private mod ...))))))))
1912 (global-extend 'core 'if
1913 (lambda (e r w s mod)
1918 (chi (syntax test) r w mod)
1919 (chi (syntax then) r w mod)
1920 (build-void no-source)))
1924 (chi (syntax test) r w mod)
1925 (chi (syntax then) r w mod)
1926 (chi (syntax else) r w mod))))))
1928 (global-extend 'begin 'begin '())
1930 (global-extend 'define 'define '())
1932 (global-extend 'define-syntax 'define-syntax '())
1934 (global-extend 'eval-when 'eval-when '())
1936 (global-extend 'core 'syntax-case
1938 (define convert-pattern
1939 ; accepts pattern & keys
1940 ; returns $sc-dispatch pattern & ids
1941 (lambda (pattern keys)
1942 (let cvt ((p pattern) (n 0) (ids '()))
1944 (if (bound-id-member? p keys)
1945 (values (vector 'free-id p) ids)
1946 (values 'any (cons (cons p n) ids)))
1949 (ellipsis? (syntax dots))
1951 (lambda () (cvt (syntax x) (fx+ n 1) ids))
1953 (values (if (eq? p 'any) 'each-any (vector 'each p))
1957 (lambda () (cvt (syntax y) n ids))
1960 (lambda () (cvt (syntax x) n ids))
1962 (values (cons x y) ids))))))
1963 (() (values '() ids))
1966 (lambda () (cvt (syntax (x ...)) n ids))
1967 (lambda (p ids) (values (vector 'vector p) ids))))
1968 (x (values (vector 'atom (strip p empty-wrap)) ids)))))))
1970 (define build-dispatch-call
1971 (lambda (pvars exp y r mod)
1972 (let ((ids (map car pvars)) (levels (map cdr pvars)))
1973 (let ((labels (gen-labels ids)) (new-vars (map gen-var ids)))
1974 (build-application no-source
1975 (build-primref no-source 'apply)
1976 (list (build-lambda no-source (map syntax->datum ids) new-vars #f
1980 (map (lambda (var level)
1981 (make-binding 'syntax `(,var . ,level)))
1985 (make-binding-wrap ids labels empty-wrap)
1990 (lambda (x keys clauses r pat fender exp mod)
1992 (lambda () (convert-pattern pat keys))
1995 ((not (distinct-bound-ids? (map car pvars)))
1996 (syntax-violation 'syntax-case "duplicate pattern variable" pat))
1997 ((not (and-map (lambda (x) (not (ellipsis? (car x)))) pvars))
1998 (syntax-violation 'syntax-case "misplaced ellipsis" pat))
2000 (let ((y (gen-var 'tmp)))
2001 ; fat finger binding and references to temp variable y
2002 (build-application no-source
2003 (build-lambda no-source (list 'tmp) (list y) #f
2004 (let ((y (build-lexical-reference 'value no-source
2006 (build-conditional no-source
2007 (syntax-case fender ()
2009 (_ (build-conditional no-source
2011 (build-dispatch-call pvars fender y r mod)
2012 (build-data no-source #f))))
2013 (build-dispatch-call pvars exp y r mod)
2014 (gen-syntax-case x keys clauses r mod))))
2015 (list (if (eq? p 'any)
2016 (build-application no-source
2017 (build-primref no-source 'list)
2019 (build-application no-source
2020 (build-primref no-source '$sc-dispatch)
2021 (list x (build-data no-source p)))))))))))))
2023 (define gen-syntax-case
2024 (lambda (x keys clauses r mod)
2026 (build-application no-source
2027 (build-primref no-source 'syntax-violation)
2028 (list (build-data no-source #f)
2029 (build-data no-source
2030 "source expression failed to match any pattern")
2032 (syntax-case (car clauses) ()
2034 (if (and (id? (syntax pat))
2035 (and-map (lambda (x) (not (free-id=? (syntax pat) x)))
2036 (cons (syntax (... ...)) keys)))
2037 (let ((labels (list (gen-label)))
2038 (var (gen-var (syntax pat))))
2039 (build-application no-source
2040 (build-lambda no-source
2041 (list (syntax->datum (syntax pat))) (list var)
2045 (list (make-binding 'syntax `(,var . 0)))
2047 (make-binding-wrap (syntax (pat))
2051 (gen-clause x keys (cdr clauses) r
2052 (syntax pat) #t (syntax exp) mod)))
2054 (gen-clause x keys (cdr clauses) r
2055 (syntax pat) (syntax fender) (syntax exp) mod))
2056 (_ (syntax-violation 'syntax-case "invalid clause"
2059 (lambda (e r w s mod)
2060 (let ((e (source-wrap e w s mod)))
2062 ((_ val (key ...) m ...)
2063 (if (and-map (lambda (x) (and (id? x) (not (ellipsis? x))))
2065 (let ((x (gen-var 'tmp)))
2066 ; fat finger binding and references to temp variable x
2067 (build-application s
2068 (build-lambda no-source (list 'tmp) (list x) #f
2069 (gen-syntax-case (build-lexical-reference 'value no-source
2071 (syntax (key ...)) (syntax (m ...))
2074 (list (chi (syntax val) r empty-wrap mod))))
2075 (syntax-violation 'syntax-case "invalid literals list" e))))))))
2077 ;;; The portable sc-expand seeds chi-top's mode m with 'e (for
2078 ;;; evaluating) and esew (which stands for "eval syntax expanders
2079 ;;; when") with '(eval). In Chez Scheme, m is set to 'c instead of e
2080 ;;; if we are compiling a file, and esew is set to
2081 ;;; (eval-syntactic-expanders-when), which defaults to the list
2082 ;;; '(compile load eval). This means that, by default, top-level
2083 ;;; syntactic definitions are evaluated immediately after they are
2084 ;;; expanded, and the expanded definitions are also residualized into
2085 ;;; the object file if we are compiling a file.
2088 (if (and (pair? x) (equal? (car x) noexpand))
2090 (let ((m (if (null? rest) 'e (car rest)))
2091 (esew (if (or (null? rest) (null? (cdr rest)))
2094 (with-fluid* *mode* m
2096 (chi-top x null-env top-wrap m esew
2097 (cons 'hygiene (module-name (current-module))))))))))
2105 (make-syntax-object datum (syntax-object-wrap id) #f)))
2108 ; accepts any object, since syntax objects may consist partially
2109 ; or entirely of unwrapped, nonsymbolic data
2111 (strip x empty-wrap)))
2113 (set! generate-temporaries
2115 (arg-check list? ls 'generate-temporaries)
2116 (map (lambda (x) (wrap (gensym-hook) top-wrap #f)) ls)))
2118 (set! free-identifier=?
2120 (arg-check nonsymbol-id? x 'free-identifier=?)
2121 (arg-check nonsymbol-id? y 'free-identifier=?)
2124 (set! bound-identifier=?
2126 (arg-check nonsymbol-id? x 'bound-identifier=?)
2127 (arg-check nonsymbol-id? y 'bound-identifier=?)
2130 (set! syntax-violation
2131 (lambda (who message form . subform)
2132 (arg-check (lambda (x) (or (not x) (string? x) (symbol? x)))
2133 who 'syntax-violation)
2134 (arg-check string? message 'syntax-violation)
2135 (scm-error 'syntax-error 'sc-expand
2139 (if (null? subform) "in ~a" "in subform `~s' of `~s'"))
2140 (let ((tail (cons message
2141 (map (lambda (x) (strip x empty-wrap))
2142 (append subform (list form))))))
2143 (if who (cons who tail) tail))
2146 ;;; $sc-dispatch expects an expression and a pattern. If the expression
2147 ;;; matches the pattern a list of the matching expressions for each
2148 ;;; "any" is returned. Otherwise, #f is returned. (This use of #f will
2149 ;;; not work on r4rs implementations that violate the ieee requirement
2150 ;;; that #f and () be distinct.)
2152 ;;; The expression is matched with the pattern as follows:
2154 ;;; pattern: matches:
2157 ;;; (<pattern>1 . <pattern>2) (<pattern>1 . <pattern>2)
2159 ;;; #(free-id <key>) <key> with free-identifier=?
2160 ;;; #(each <pattern>) (<pattern>*)
2161 ;;; #(vector <pattern>) (list->vector <pattern>)
2162 ;;; #(atom <object>) <object> with "equal?"
2164 ;;; Vector cops out to pair under assumption that vectors are rare. If
2165 ;;; not, should convert to:
2166 ;;; #(vector <pattern>*) #(<pattern>*)
2174 (match-each (annotation-expression e) p w mod))
2176 (let ((first (match (car e) p w '() mod)))
2178 (let ((rest (match-each (cdr e) p w mod)))
2179 (and rest (cons first rest))))))
2182 (match-each (syntax-object-expression e)
2184 (join-wraps w (syntax-object-wrap e))
2185 (syntax-object-module e)))
2188 (define match-each-any
2192 (match-each-any (annotation-expression e) w mod))
2194 (let ((l (match-each-any (cdr e) w mod)))
2195 (and l (cons (wrap (car e) w mod) l))))
2198 (match-each-any (syntax-object-expression e)
2199 (join-wraps w (syntax-object-wrap e))
2207 ((eq? p 'any) (cons '() r))
2208 ((pair? p) (match-empty (car p) (match-empty (cdr p) r)))
2209 ((eq? p 'each-any) (cons '() r))
2211 (case (vector-ref p 0)
2212 ((each) (match-empty (vector-ref p 1) r))
2214 ((vector) (match-empty (vector-ref p 1) r)))))))
2217 (lambda (e p w r mod)
2219 ((null? p) (and (null? e) r))
2221 (and (pair? e) (match (car e) (car p) w
2222 (match (cdr e) (cdr p) w r mod)
2225 (let ((l (match-each-any e w mod))) (and l (cons l r))))
2227 (case (vector-ref p 0)
2230 (match-empty (vector-ref p 1) r)
2231 (let ((l (match-each e (vector-ref p 1) w mod)))
2233 (let collect ((l l))
2236 (cons (map car l) (collect (map cdr l)))))))))
2237 ((free-id) (and (id? e) (free-id=? (wrap e w mod) (vector-ref p 1)) r))
2238 ((atom) (and (equal? (vector-ref p 1) (strip e w)) r))
2241 (match (vector->list e) (vector-ref p 1) w r mod))))))))
2244 (lambda (e p w r mod)
2247 ((eq? p 'any) (cons (wrap e w mod) r))
2250 (unannotate (syntax-object-expression e))
2252 (join-wraps w (syntax-object-wrap e))
2254 (syntax-object-module e)))
2255 (else (match* (unannotate e) p w r mod)))))
2260 ((eq? p 'any) (list e))
2262 (match* (unannotate (syntax-object-expression e))
2263 p (syntax-object-wrap e) '() (syntax-object-module e)))
2264 (else (match* (unannotate e) p empty-wrap '() #f)))))
2269 (define-syntax with-syntax
2273 (syntax (begin e1 e2 ...)))
2274 ((_ ((out in)) e1 e2 ...)
2275 (syntax (syntax-case in () (out (begin e1 e2 ...)))))
2276 ((_ ((out in) ...) e1 e2 ...)
2277 (syntax (syntax-case (list in ...) ()
2278 ((out ...) (begin e1 e2 ...))))))))
2280 (define-syntax syntax-rules
2283 ((_ (k ...) ((keyword . pattern) template) ...)
2285 (syntax-case x (k ...)
2286 ((dummy . pattern) (syntax template))
2292 ((let* ((x v) ...) e1 e2 ...)
2293 (and-map identifier? (syntax (x ...)))
2294 (let f ((bindings (syntax ((x v) ...))))
2295 (if (null? bindings)
2296 (syntax (let () e1 e2 ...))
2297 (with-syntax ((body (f (cdr bindings)))
2298 (binding (car bindings)))
2299 (syntax (let (binding) body)))))))))
2303 (syntax-case orig-x ()
2304 ((_ ((var init . step) ...) (e0 e1 ...) c ...)
2305 (with-syntax (((step ...)
2310 (_ (syntax-violation
2311 'do "bad step expression"
2314 (syntax (step ...)))))
2315 (syntax-case (syntax (e1 ...)) ()
2316 (() (syntax (let doloop ((var init) ...)
2318 (begin c ... (doloop step ...))))))
2320 (syntax (let doloop ((var init) ...)
2323 (begin c ... (doloop step ...))))))))))))
2325 (define-syntax quasiquote
2329 (with-syntax ((x x) (y y))
2330 (syntax-case (syntax y) (quote list)
2332 (syntax-case (syntax x) (quote)
2333 ((quote dx) (syntax (quote (dx . dy))))
2334 (_ (if (null? (syntax dy))
2336 (syntax (cons x y))))))
2337 ((list . stuff) (syntax (list x . stuff)))
2338 (else (syntax (cons x y)))))))
2341 (with-syntax ((x x) (y y))
2342 (syntax-case (syntax y) (quote)
2343 ((quote ()) (syntax x))
2344 (_ (syntax (append x y)))))))
2347 (with-syntax ((x x))
2348 (syntax-case (syntax x) (quote list)
2349 ((quote (x ...)) (syntax (quote #(x ...))))
2350 ((list x ...) (syntax (vector x ...)))
2351 (_ (syntax (list->vector x)))))))
2354 (syntax-case p (unquote unquote-splicing quasiquote)
2358 (quasicons (syntax (quote unquote))
2359 (quasi (syntax (p)) (- lev 1)))))
2362 (syntax-violation 'unquote
2363 "unquote takes exactly one argument"
2364 p (syntax (unquote . args))))
2365 (((unquote-splicing p) . q)
2367 (quasiappend (syntax p) (quasi (syntax q) lev))
2368 (quasicons (quasicons (syntax (quote unquote-splicing))
2369 (quasi (syntax (p)) (- lev 1)))
2370 (quasi (syntax q) lev))))
2371 (((unquote-splicing . args) . q)
2373 (syntax-violation 'unquote-splicing
2374 "unquote-splicing takes exactly one argument"
2375 p (syntax (unquote-splicing . args))))
2377 (quasicons (syntax (quote quasiquote))
2378 (quasi (syntax (p)) (+ lev 1))))
2380 (quasicons (quasi (syntax p) lev) (quasi (syntax q) lev)))
2381 (#(x ...) (quasivector (quasi (syntax (x ...)) lev)))
2382 (p (syntax (quote p)))))))
2385 ((_ e) (quasi (syntax e) 0))))))
2387 (define-syntax include
2391 (let ((p (open-input-file fn)))
2392 (let f ((x (read p)))
2394 (begin (close-input-port p) '())
2395 (cons (datum->syntax k x)
2399 (let ((fn (syntax->datum (syntax filename))))
2400 (with-syntax (((exp ...) (read-file fn (syntax k))))
2401 (syntax (begin exp ...))))))))
2403 (define-syntax unquote
2407 (syntax-violation 'unquote
2408 "expression not valid outside of quasiquote"
2411 (define-syntax unquote-splicing
2415 (syntax-violation 'unquote-splicing
2416 "expression not valid outside of quasiquote"
2424 ((body (let f ((clause (syntax m1)) (clauses (syntax (m2 ...))))
2426 (syntax-case clause (else)
2427 ((else e1 e2 ...) (syntax (begin e1 e2 ...)))
2428 (((k ...) e1 e2 ...)
2429 (syntax (if (memv t '(k ...)) (begin e1 e2 ...))))
2430 (_ (syntax-violation 'case "bad clause" x clause)))
2431 (with-syntax ((rest (f (car clauses) (cdr clauses))))
2432 (syntax-case clause (else)
2433 (((k ...) e1 e2 ...)
2434 (syntax (if (memv t '(k ...))
2437 (_ (syntax-violation 'case "bad clause" x
2439 (syntax (let ((t e)) body)))))))
2441 (define-syntax identifier-syntax
2449 (identifier? (syntax id))
2452 (syntax (e x (... ...)))))))))))