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 ,test-exp ,then-exp ,else-exp)))))
373 (define build-lexical-reference
374 (lambda (type source name var)
375 (case (fluid-ref *mode*)
376 ((c) ((@ (language tree-il) make-lexical-ref) source name var))
379 (define build-lexical-assignment
380 (lambda (source name var exp)
381 (case (fluid-ref *mode*)
382 ((c) ((@ (language tree-il) make-lexical-set) source name var exp))
383 (else `(set! ,var ,exp)))))
385 ;; Before modules are booted, we can't expand into data structures from
386 ;; (language tree-il) -- we need to give the evaluator the
387 ;; s-expressions that it understands natively. Actually the real truth
388 ;; of the matter is that the evaluator doesn't understand tree-il
389 ;; structures at all. So until we fix the evaluator, if ever, the
390 ;; conflation that we should use tree-il iff we are compiling
393 (define (analyze-variable mod var modref-cont bare-cont)
396 (let ((kind (car mod))
399 ((public) (modref-cont mod var #t))
400 ((private) (if (not (equal? mod (module-name (current-module))))
401 (modref-cont mod var #f)
403 ((bare) (bare-cont var))
404 ((hygiene) (if (and (not (equal? mod (module-name (current-module))))
405 (module-variable (resolve-module mod) var))
406 (modref-cont mod var #f)
408 (else (syntax-violation #f "bad module kind" var mod))))))
410 (define build-global-reference
411 (lambda (source var mod)
414 (lambda (mod var public?)
415 (case (fluid-ref *mode*)
416 ((c) ((@ (language tree-il) make-module-ref) source mod var public?))
417 (else (list (if public? '@ '@@) mod var))))
419 (case (fluid-ref *mode*)
420 ((c) ((@ (language tree-il) make-toplevel-ref) source var))
423 (define build-global-assignment
424 (lambda (source var exp mod)
427 (lambda (mod var public?)
428 (case (fluid-ref *mode*)
429 ((c) ((@ (language tree-il) make-module-set) source mod var public? exp))
430 (else `(set! ,(list (if public? '@ '@@) mod var) ,exp))))
432 (case (fluid-ref *mode*)
433 ((c) ((@ (language tree-il) make-toplevel-set) source var exp))
434 (else `(set! ,var ,exp)))))))
436 (define build-global-definition
437 (lambda (source var exp)
438 (case (fluid-ref *mode*)
439 ((c) ((@ (language tree-il) make-toplevel-define) source var exp))
440 (else `(define ,var ,exp)))))
443 (lambda (src ids vars docstring exp)
444 (case (fluid-ref *mode*)
445 ((c) ((@ (language tree-il) make-lambda) src ids vars
446 (if docstring `((documentation . ,docstring)) '())
448 (else `(lambda ,vars ,@(if docstring (list docstring) '())
451 (define build-primref
453 (if (equal? (module-name (current-module)) '(guile))
454 (case (fluid-ref *mode*)
455 ((c) ((@ (language tree-il) make-toplevel-ref) src name))
457 (case (fluid-ref *mode*)
458 ((c) ((@ (language tree-il) make-module-ref) src '(guile) name #f))
459 (else `(@@ (guile) ,name))))))
461 (define (build-data src exp)
462 (case (fluid-ref *mode*)
463 ((c) ((@ (language tree-il) make-const) src exp))
464 (else (if (and (self-evaluating? exp) (not (vector? exp)))
466 (list 'quote exp)))))
468 (define build-sequence
470 (if (null? (cdr exps))
472 (case (fluid-ref *mode*)
473 ((c) ((@ (language tree-il) make-sequence) src exps))
474 (else `(begin ,@exps))))))
477 (lambda (src ids vars val-exps body-exp)
480 (case (fluid-ref *mode*)
481 ((c) ((@ (language tree-il) make-let) src ids vars val-exps body-exp))
482 (else `(let ,(map list vars val-exps) ,body-exp))))))
484 (define build-named-let
485 (lambda (src ids vars val-exps body-exp)
490 (case (fluid-ref *mode*)
491 ((c) ((@ (language tree-il) make-letrec) src
494 (list (build-lambda src ids vars #f body-exp))
495 (build-application src (build-lexical-reference 'fun src f-name f)
497 (else `(let ,f ,(map list vars val-exps) ,body-exp))))))
500 (lambda (src ids vars val-exps body-exp)
503 (case (fluid-ref *mode*)
504 ((c) ((@ (language tree-il) make-letrec) src ids vars val-exps body-exp))
505 (else `(letrec ,(map list vars val-exps) ,body-exp))))))
507 ;; FIXME: wingo: use make-lexical ?
508 (define-syntax build-lexical-var
510 ((_ src id) (gensym (symbol->string id)))))
512 (define-structure (syntax-object expression wrap module))
514 (define-syntax unannotate
519 (annotation-expression e)
522 (define-syntax no-source (identifier-syntax #f))
524 (define source-annotation
527 ((annotation? x) (annotation-source x))
528 ((syntax-object? x) (source-annotation (syntax-object-expression x)))
531 (define-syntax arg-check
535 (if (not (pred? x)) (syntax-violation who "invalid argument" x))))))
537 ;;; compile-time environments
539 ;;; wrap and environment comprise two level mapping.
540 ;;; wrap : id --> label
541 ;;; env : label --> <element>
543 ;;; environments are represented in two parts: a lexical part and a global
544 ;;; part. The lexical part is a simple list of associations from labels
545 ;;; to bindings. The global part is implemented by
546 ;;; {put,get}-global-definition-hook and associates symbols with
549 ;;; global (assumed global variable) and displaced-lexical (see below)
550 ;;; do not show up in any environment; instead, they are fabricated by
551 ;;; lookup when it finds no other bindings.
553 ;;; <environment> ::= ((<label> . <binding>)*)
555 ;;; identifier bindings include a type and a value
557 ;;; <binding> ::= (macro . <procedure>) macros
558 ;;; (core . <procedure>) core forms
559 ;;; (external-macro . <procedure>) external-macro
560 ;;; (module-ref . <procedure>) @ or @@
563 ;;; (define-syntax) define-syntax
564 ;;; (local-syntax . rec?) let-syntax/letrec-syntax
565 ;;; (eval-when) eval-when
566 ;;; (syntax . (<var> . <level>)) pattern variables
567 ;;; (global) assumed global variable
568 ;;; (lexical . <var>) lexical variables
569 ;;; (displaced-lexical) displaced lexicals
570 ;;; <level> ::= <nonnegative integer>
571 ;;; <var> ::= variable returned by build-lexical-var
573 ;;; a macro is a user-defined syntactic-form. a core is a system-defined
574 ;;; syntactic form. begin, define, define-syntax, and eval-when are
575 ;;; treated specially since they are sensitive to whether the form is
576 ;;; at top-level and (except for eval-when) can denote valid internal
579 ;;; a pattern variable is a variable introduced by syntax-case and can
580 ;;; be referenced only within a syntax form.
582 ;;; any identifier for which no top-level syntax definition or local
583 ;;; binding of any kind has been seen is assumed to be a global
586 ;;; a lexical variable is a lambda- or letrec-bound variable.
588 ;;; a displaced-lexical identifier is a lexical identifier removed from
589 ;;; it's scope by the return of a syntax object containing the identifier.
590 ;;; a displaced lexical can also appear when a letrec-syntax-bound
591 ;;; keyword is referenced on the rhs of one of the letrec-syntax clauses.
592 ;;; a displaced lexical should never occur with properly written macros.
594 (define-syntax make-binding
595 (syntax-rules (quote)
596 ((_ type value) (cons type value))
598 ((_ type) (cons type '()))))
599 (define binding-type car)
600 (define binding-value cdr)
602 (define-syntax null-env (identifier-syntax '()))
605 (lambda (labels bindings r)
608 (extend-env (cdr labels) (cdr bindings)
609 (cons (cons (car labels) (car bindings)) r)))))
611 (define extend-var-env
612 ; variant of extend-env that forms "lexical" binding
613 (lambda (labels vars r)
616 (extend-var-env (cdr labels) (cdr vars)
617 (cons (cons (car labels) (make-binding 'lexical (car vars))) r)))))
619 ;;; we use a "macros only" environment in expansion of local macro
620 ;;; definitions so that their definitions can use local macros without
621 ;;; attempting to use other lexical identifiers.
622 (define macros-only-env
627 (if (eq? (cadr a) 'macro)
628 (cons a (macros-only-env (cdr r)))
629 (macros-only-env (cdr r)))))))
632 ; x may be a label or a symbol
633 ; although symbols are usually global, we check the environment first
634 ; anyway because a temporary binding may have been established by
640 (or (get-global-definition-hook x mod) (make-binding 'global)))
641 (else (make-binding 'displaced-lexical)))))
643 (define global-extend
644 (lambda (type sym val)
645 (put-global-definition-hook sym type val)))
648 ;;; Conceptually, identifiers are always syntax objects. Internally,
649 ;;; however, the wrap is sometimes maintained separately (a source of
650 ;;; efficiency and confusion), so that symbols are also considered
651 ;;; identifiers by id?. Externally, they are always wrapped.
653 (define nonsymbol-id?
655 (and (syntax-object? x)
656 (symbol? (unannotate (syntax-object-expression x))))))
662 ((syntax-object? x) (symbol? (unannotate (syntax-object-expression x))))
663 ((annotation? x) (symbol? (annotation-expression x)))
666 (define-syntax id-sym-name
670 (unannotate (if (syntax-object? x) (syntax-object-expression x) x))))))
672 (define id-sym-name&marks
674 (if (syntax-object? x)
676 (unannotate (syntax-object-expression x))
677 (join-marks (wrap-marks w) (wrap-marks (syntax-object-wrap x))))
678 (values (unannotate x) (wrap-marks w)))))
680 ;;; syntax object wraps
682 ;;; <wrap> ::= ((<mark> ...) . (<subst> ...))
683 ;;; <subst> ::= <shift> | <subs>
684 ;;; <subs> ::= #(<old name> <label> (<mark> ...))
685 ;;; <shift> ::= positive fixnum
687 (define make-wrap cons)
688 (define wrap-marks car)
689 (define wrap-subst cdr)
691 (define-syntax subst-rename? (identifier-syntax vector?))
692 (define-syntax rename-old (syntax-rules () ((_ x) (vector-ref x 0))))
693 (define-syntax rename-new (syntax-rules () ((_ x) (vector-ref x 1))))
694 (define-syntax rename-marks (syntax-rules () ((_ x) (vector-ref x 2))))
695 (define-syntax make-rename
697 ((_ old new marks) (vector old new marks))))
699 ;;; labels must be comparable with "eq?" and distinct from symbols.
701 (lambda () (string #\i)))
707 (cons (gen-label) (gen-labels (cdr ls))))))
709 (define-structure (ribcage symnames marks labels))
711 (define-syntax empty-wrap (identifier-syntax '(())))
713 (define-syntax top-wrap (identifier-syntax '((top))))
715 (define-syntax top-marked?
717 ((_ w) (memq 'top (wrap-marks w)))))
719 ;;; Marks must be comparable with "eq?" and distinct from pairs and
720 ;;; the symbol top. We do not use integers so that marks will remain
721 ;;; unique even across file compiles.
723 (define-syntax the-anti-mark (identifier-syntax #f))
727 (make-wrap (cons the-anti-mark (wrap-marks w))
728 (cons 'shift (wrap-subst w)))))
730 (define-syntax new-mark
734 ;;; make-empty-ribcage and extend-ribcage maintain list-based ribcages for
735 ;;; internal definitions, in which the ribcages are built incrementally
736 (define-syntax make-empty-ribcage
738 ((_) (make-ribcage '() '() '()))))
740 (define extend-ribcage!
741 ; must receive ids with complete wraps
742 (lambda (ribcage id label)
743 (set-ribcage-symnames! ribcage
744 (cons (unannotate (syntax-object-expression id))
745 (ribcage-symnames ribcage)))
746 (set-ribcage-marks! ribcage
747 (cons (wrap-marks (syntax-object-wrap id))
748 (ribcage-marks ribcage)))
749 (set-ribcage-labels! ribcage
750 (cons label (ribcage-labels ribcage)))))
752 ;;; make-binding-wrap creates vector-based ribcages
753 (define make-binding-wrap
754 (lambda (ids labels w)
760 (let ((labelvec (list->vector labels)))
761 (let ((n (vector-length labelvec)))
762 (let ((symnamevec (make-vector n)) (marksvec (make-vector n)))
763 (let f ((ids ids) (i 0))
764 (if (not (null? ids))
766 (lambda () (id-sym-name&marks (car ids) w))
767 (lambda (symname marks)
768 (vector-set! symnamevec i symname)
769 (vector-set! marksvec i marks)
770 (f (cdr ids) (fx+ i 1))))))
771 (make-ribcage symnamevec marksvec labelvec))))
782 (let ((m1 (wrap-marks w1)) (s1 (wrap-subst w1)))
788 (smart-append s1 (wrap-subst w2))))
790 (smart-append m1 (wrap-marks w2))
791 (smart-append s1 (wrap-subst w2)))))))
795 (smart-append m1 m2)))
802 (eq? (car x) (car y))
803 (same-marks? (cdr x) (cdr y))))))
809 ((_ e) (call-with-values (lambda () e) (lambda (x . ignore) x)))))
811 (lambda (sym subst marks)
814 (let ((fst (car subst)))
816 (search sym (cdr subst) (cdr marks))
817 (let ((symnames (ribcage-symnames fst)))
818 (if (vector? symnames)
819 (search-vector-rib sym subst marks symnames fst)
820 (search-list-rib sym subst marks symnames fst))))))))
821 (define search-list-rib
822 (lambda (sym subst marks symnames ribcage)
823 (let f ((symnames symnames) (i 0))
825 ((null? symnames) (search sym (cdr subst) marks))
826 ((and (eq? (car symnames) sym)
827 (same-marks? marks (list-ref (ribcage-marks ribcage) i)))
828 (values (list-ref (ribcage-labels ribcage) i) marks))
829 (else (f (cdr symnames) (fx+ i 1)))))))
830 (define search-vector-rib
831 (lambda (sym subst marks symnames ribcage)
832 (let ((n (vector-length symnames)))
835 ((fx= i n) (search sym (cdr subst) marks))
836 ((and (eq? (vector-ref symnames i) sym)
837 (same-marks? marks (vector-ref (ribcage-marks ribcage) i)))
838 (values (vector-ref (ribcage-labels ribcage) i) marks))
839 (else (f (fx+ i 1))))))))
842 (or (first (search id (wrap-subst w) (wrap-marks w))) id))
844 (let ((id (unannotate (syntax-object-expression id)))
845 (w1 (syntax-object-wrap id)))
846 (let ((marks (join-marks (wrap-marks w) (wrap-marks w1))))
847 (call-with-values (lambda () (search id (wrap-subst w) marks))
848 (lambda (new-id marks)
850 (first (search id (wrap-subst w1) marks))
853 (let ((id (unannotate id)))
854 (or (first (search id (wrap-subst w) (wrap-marks w))) id)))
855 (else (syntax-violation 'id-var-name "invalid id" id)))))
857 ;;; free-id=? must be passed fully wrapped ids since (free-id=? x y)
858 ;;; may be true even if (free-id=? (wrap x w) (wrap y w)) is not.
862 (and (eq? (id-sym-name i) (id-sym-name j)) ; accelerator
863 (eq? (id-var-name i empty-wrap) (id-var-name j empty-wrap)))))
865 ;;; bound-id=? may be passed unwrapped (or partially wrapped) ids as
866 ;;; long as the missing portion of the wrap is common to both of the ids
867 ;;; since (bound-id=? x y) iff (bound-id=? (wrap x w) (wrap y w))
871 (if (and (syntax-object? i) (syntax-object? j))
872 (and (eq? (unannotate (syntax-object-expression i))
873 (unannotate (syntax-object-expression j)))
874 (same-marks? (wrap-marks (syntax-object-wrap i))
875 (wrap-marks (syntax-object-wrap j))))
876 (eq? (unannotate i) (unannotate j)))))
878 ;;; "valid-bound-ids?" returns #t if it receives a list of distinct ids.
879 ;;; valid-bound-ids? may be passed unwrapped (or partially wrapped) ids
880 ;;; as long as the missing portion of the wrap is common to all of the
883 (define valid-bound-ids?
885 (and (let all-ids? ((ids ids))
888 (all-ids? (cdr ids)))))
889 (distinct-bound-ids? ids))))
891 ;;; distinct-bound-ids? expects a list of ids and returns #t if there are
892 ;;; no duplicates. It is quadratic on the length of the id list; long
893 ;;; lists could be sorted to make it more efficient. distinct-bound-ids?
894 ;;; may be passed unwrapped (or partially wrapped) ids as long as the
895 ;;; missing portion of the wrap is common to all of the ids.
897 (define distinct-bound-ids?
899 (let distinct? ((ids ids))
901 (and (not (bound-id-member? (car ids) (cdr ids)))
902 (distinct? (cdr ids)))))))
904 (define bound-id-member?
906 (and (not (null? list))
907 (or (bound-id=? x (car list))
908 (bound-id-member? x (cdr list))))))
910 ;;; wrapping expressions and identifiers
915 ((and (null? (wrap-marks w)) (null? (wrap-subst w))) x)
918 (syntax-object-expression x)
919 (join-wraps w (syntax-object-wrap x))
920 (syntax-object-module x)))
922 (else (make-syntax-object x w defmod)))))
925 (lambda (x w s defmod)
926 (wrap (if s (make-annotation x s #f) x) w defmod)))
931 (lambda (body r w s mod)
933 (let dobody ((body body) (r r) (w w) (mod mod))
936 (let ((first (chi (car body) r w mod)))
937 (cons first (dobody (cdr body) r w mod))))))))
939 (define chi-top-sequence
940 (lambda (body r w s m esew mod)
942 (let dobody ((body body) (r r) (w w) (m m) (esew esew) (mod mod))
945 (let ((first (chi-top (car body) r w m esew mod)))
946 (cons first (dobody (cdr body) r w m esew mod))))))))
948 (define chi-install-global
950 (build-global-definition
953 ;; FIXME: seems nasty to call current-module here
954 (if (let ((v (module-variable (current-module) name)))
955 ;; FIXME use primitive-macro?
956 (and v (variable-bound? v) (macro? (variable-ref v))
957 (not (eq? (macro-type (variable-ref v)) 'syncase-macro))))
960 (build-primref no-source 'make-extended-syncase-macro)
961 (list (build-application
963 (build-primref no-source 'module-ref)
964 (list (build-application no-source 'current-module '())
965 (build-data no-source name)))
966 (build-data no-source 'macro)
970 (build-primref no-source 'make-syncase-macro)
971 (list (build-data no-source 'macro) e))))))
973 (define chi-when-list
974 (lambda (e when-list w)
975 ; when-list is syntax'd version of list of situations
976 (let f ((when-list when-list) (situations '()))
977 (if (null? when-list)
980 (cons (let ((x (car when-list)))
982 ((free-id=? x (syntax compile)) 'compile)
983 ((free-id=? x (syntax load)) 'load)
984 ((free-id=? x (syntax eval)) 'eval)
985 (else (syntax-violation 'eval-when
990 ;;; syntax-type returns six values: type, value, e, w, s, and mod. The
991 ;;; first two are described in the table below.
993 ;;; type value explanation
994 ;;; -------------------------------------------------------------------
995 ;;; core procedure core form (including singleton)
996 ;;; external-macro procedure external macro
997 ;;; module-ref procedure @ or @@ form
998 ;;; lexical name lexical variable reference
999 ;;; global name global variable reference
1000 ;;; begin none begin keyword
1001 ;;; define none define keyword
1002 ;;; define-syntax none define-syntax keyword
1003 ;;; local-syntax rec? letrec-syntax/let-syntax keyword
1004 ;;; eval-when none eval-when keyword
1005 ;;; syntax level pattern variable
1006 ;;; displaced-lexical none displaced lexical identifier
1007 ;;; lexical-call name call to lexical variable
1008 ;;; global-call name call to global variable
1009 ;;; call none any other call
1010 ;;; begin-form none begin expression
1011 ;;; define-form id variable definition
1012 ;;; define-syntax-form id syntax definition
1013 ;;; local-syntax-form rec? syntax definition
1014 ;;; eval-when-form none eval-when form
1015 ;;; constant none self-evaluating datum
1016 ;;; other none anything else
1018 ;;; For define-form and define-syntax-form, e is the rhs expression.
1019 ;;; For all others, e is the entire form. w is the wrap for e.
1020 ;;; s is the source for the entire form. mod is the module for e.
1022 ;;; syntax-type expands macros and unwraps as necessary to get to
1023 ;;; one of the forms above. It also parses define and define-syntax
1024 ;;; forms, although perhaps this should be done by the consumer.
1027 (lambda (e r w s rib mod)
1030 (let* ((n (id-var-name e w))
1031 (b (lookup n r mod))
1032 (type (binding-type b)))
1034 ((lexical) (values type (binding-value b) e w s mod))
1035 ((global) (values type n e w s mod))
1037 (syntax-type (chi-macro (binding-value b) e r w rib mod)
1038 r empty-wrap s rib mod))
1039 (else (values type (binding-value b) e w s mod)))))
1041 (let ((first (car e)))
1043 (let* ((n (id-var-name first w))
1044 (b (lookup n r (or (and (syntax-object? first)
1045 (syntax-object-module first))
1047 (type (binding-type b)))
1050 (values 'lexical-call (binding-value b) e w s mod))
1052 (values 'global-call n e w s mod))
1054 (syntax-type (chi-macro (binding-value b) e r w rib mod)
1055 r empty-wrap s rib mod))
1056 ((core external-macro module-ref)
1057 (values type (binding-value b) e w s mod))
1059 (values 'local-syntax-form (binding-value b) e w s mod))
1061 (values 'begin-form #f e w s mod))
1063 (values 'eval-when-form #f e w s mod))
1068 (values 'define-form (syntax name) (syntax val) w s mod))
1069 ((_ (name . args) e1 e2 ...)
1070 (and (id? (syntax name))
1071 (valid-bound-ids? (lambda-var-list (syntax args))))
1072 ; need lambda here...
1073 (values 'define-form (wrap (syntax name) w mod)
1074 (cons (syntax lambda) (wrap (syntax (args e1 e2 ...)) w mod))
1078 (values 'define-form (wrap (syntax name) w mod)
1080 empty-wrap s mod))))
1085 (values 'define-syntax-form (syntax name)
1086 (syntax val) w s mod))))
1088 (values 'call #f e w s mod))))
1089 (values 'call #f e w s mod))))
1091 ;; s can't be valid source if we've unwrapped
1092 (syntax-type (syntax-object-expression e)
1094 (join-wraps w (syntax-object-wrap e))
1095 no-source rib (or (syntax-object-module e) mod)))
1097 (syntax-type (annotation-expression e) r w (annotation-source e) rib mod))
1098 ((self-evaluating? e) (values 'constant #f e w s mod))
1099 (else (values 'other #f e w s mod)))))
1102 (lambda (e r w m esew mod)
1103 (define-syntax eval-if-c&e
1107 (if (eq? m 'c&e) (top-level-eval-hook x mod))
1110 (lambda () (syntax-type e r w no-source #f mod))
1111 (lambda (type value e w s mod)
1117 (chi-top-sequence (syntax (e1 e2 ...)) r w s m esew mod))))
1118 ((local-syntax-form)
1119 (chi-local-syntax value e r w s mod
1120 (lambda (body r w s mod)
1121 (chi-top-sequence body r w s m esew mod))))
1124 ((_ (x ...) e1 e2 ...)
1125 (let ((when-list (chi-when-list e (syntax (x ...)) w))
1126 (body (syntax (e1 e2 ...))))
1129 (if (memq 'eval when-list)
1130 (chi-top-sequence body r w s 'e '(eval) mod)
1132 ((memq 'load when-list)
1133 (if (or (memq 'compile when-list)
1134 (and (eq? m 'c&e) (memq 'eval when-list)))
1135 (chi-top-sequence body r w s 'c&e '(compile load) mod)
1136 (if (memq m '(c c&e))
1137 (chi-top-sequence body r w s 'c '(load) mod)
1139 ((or (memq 'compile when-list)
1140 (and (eq? m 'c&e) (memq 'eval when-list)))
1141 (top-level-eval-hook
1142 (chi-top-sequence body r w s 'e '(eval) mod)
1145 (else (chi-void)))))))
1146 ((define-syntax-form)
1147 (let ((n (id-var-name value w)) (r (macros-only-env r)))
1150 (if (memq 'compile esew)
1151 (let ((e (chi-install-global n (chi e r w mod))))
1152 (top-level-eval-hook e mod)
1153 (if (memq 'load esew) e (chi-void)))
1154 (if (memq 'load esew)
1155 (chi-install-global n (chi e r w mod))
1158 (let ((e (chi-install-global n (chi e r w mod))))
1159 (top-level-eval-hook e mod)
1162 (if (memq 'eval esew)
1163 (top-level-eval-hook
1164 (chi-install-global n (chi e r w mod))
1168 (let* ((n (id-var-name value w))
1169 (type (binding-type (lookup n r mod))))
1171 ((global core macro module-ref)
1173 (build-global-definition s n (chi e r w mod))
1175 ((displaced-lexical)
1176 (syntax-violation #f "identifier out of context"
1177 e (wrap value w mod)))
1179 (syntax-violation #f "cannot define keyword at top level"
1180 e (wrap value w mod))))))
1181 (else (eval-if-c&e m (chi-expr type value e r w s mod) mod)))))))
1186 (lambda () (syntax-type e r w no-source #f mod))
1187 (lambda (type value e w s mod)
1188 (chi-expr type value e r w s mod)))))
1191 (lambda (type value e r w s mod)
1194 (build-lexical-reference 'value s e value))
1195 ((core external-macro)
1196 ;; apply transformer
1197 (value e r w s mod))
1199 (call-with-values (lambda () (value e))
1200 ;; we could add a public? arg here
1201 (lambda (id mod) (build-global-reference s id mod))))
1204 (build-lexical-reference 'fun (source-annotation (car e))
1209 (build-global-reference (source-annotation (car e)) value
1210 (if (syntax-object? (car e))
1211 (syntax-object-module (car e))
1214 ((constant) (build-data s (strip (source-wrap e w s mod) empty-wrap)))
1215 ((global) (build-global-reference s value mod))
1216 ((call) (chi-application (chi (car e) r w mod) e r w s mod))
1219 ((_ e1 e2 ...) (chi-sequence (syntax (e1 e2 ...)) r w s mod))))
1220 ((local-syntax-form)
1221 (chi-local-syntax value e r w s mod chi-sequence))
1224 ((_ (x ...) e1 e2 ...)
1225 (let ((when-list (chi-when-list e (syntax (x ...)) w)))
1226 (if (memq 'eval when-list)
1227 (chi-sequence (syntax (e1 e2 ...)) r w s mod)
1229 ((define-form define-syntax-form)
1230 (syntax-violation #f "definition in expression context"
1231 e (wrap value w mod)))
1233 (syntax-violation #f "reference to pattern variable outside syntax form"
1234 (source-wrap e w s mod)))
1235 ((displaced-lexical)
1236 (syntax-violation #f "reference to identifier outside its scope"
1237 (source-wrap e w s mod)))
1238 (else (syntax-violation #f "unexpected syntax"
1239 (source-wrap e w s mod))))))
1241 (define chi-application
1242 (lambda (x e r w s mod)
1245 (build-application s x
1246 (map (lambda (e) (chi e r w mod)) (syntax (e1 ...))))))))
1249 (lambda (p e r w rib mod)
1250 (define rebuild-macro-output
1253 (cons (rebuild-macro-output (car x) m)
1254 (rebuild-macro-output (cdr x) m)))
1256 (let ((w (syntax-object-wrap x)))
1257 (let ((ms (wrap-marks w)) (s (wrap-subst w)))
1258 (if (and (pair? ms) (eq? (car ms) the-anti-mark))
1259 ;; output is from original text
1261 (syntax-object-expression x)
1262 (make-wrap (cdr ms) (if rib (cons rib (cdr s)) (cdr s)))
1263 (syntax-object-module x))
1264 ;; output introduced by macro
1266 (syntax-object-expression x)
1267 (make-wrap (cons m ms)
1269 (cons rib (cons 'shift s))
1271 (let ((pmod (procedure-module p)))
1273 ;; hither the hygiene
1274 (cons 'hygiene (module-name pmod))
1275 ;; but it's possible for the proc to have
1276 ;; no mod, if it was made before modules
1278 '(hygiene guile))))))))
1280 (let* ((n (vector-length x)) (v (make-vector n)))
1281 (do ((i 0 (fx+ i 1)))
1284 (rebuild-macro-output (vector-ref x i) m)))))
1286 (syntax-violation #f "encountered raw symbol in macro output"
1287 (source-wrap e w s mod) x))
1289 (rebuild-macro-output (p (wrap e (anti-mark w) mod)) (new-mark))))
1292 ;; In processing the forms of the body, we create a new, empty wrap.
1293 ;; This wrap is augmented (destructively) each time we discover that
1294 ;; the next form is a definition. This is done:
1296 ;; (1) to allow the first nondefinition form to be a call to
1297 ;; one of the defined ids even if the id previously denoted a
1298 ;; definition keyword or keyword for a macro expanding into a
1300 ;; (2) to prevent subsequent definition forms (but unfortunately
1301 ;; not earlier ones) and the first nondefinition form from
1302 ;; confusing one of the bound identifiers for an auxiliary
1304 ;; (3) so that we do not need to restart the expansion of the
1305 ;; first nondefinition form, which is problematic anyway
1306 ;; since it might be the first element of a begin that we
1307 ;; have just spliced into the body (meaning if we restarted,
1308 ;; we'd really need to restart with the begin or the macro
1309 ;; call that expanded into the begin, and we'd have to give
1310 ;; up allowing (begin <defn>+ <expr>+), which is itself
1311 ;; problematic since we don't know if a begin contains only
1312 ;; definitions until we've expanded it).
1314 ;; Before processing the body, we also create a new environment
1315 ;; containing a placeholder for the bindings we will add later and
1316 ;; associate this environment with each form. In processing a
1317 ;; let-syntax or letrec-syntax, the associated environment may be
1318 ;; augmented with local keyword bindings, so the environment may
1319 ;; be different for different forms in the body. Once we have
1320 ;; gathered up all of the definitions, we evaluate the transformer
1321 ;; expressions and splice into r at the placeholder the new variable
1322 ;; and keyword bindings. This allows let-syntax or letrec-syntax
1323 ;; forms local to a portion or all of the body to shadow the
1324 ;; definition bindings.
1326 ;; Subforms of a begin, let-syntax, or letrec-syntax are spliced
1329 ;; outer-form is fully wrapped w/source
1330 (lambda (body outer-form r w mod)
1331 (let* ((r (cons '("placeholder" . (placeholder)) r))
1332 (ribcage (make-empty-ribcage))
1333 (w (make-wrap (wrap-marks w) (cons ribcage (wrap-subst w)))))
1334 (let parse ((body (map (lambda (x) (cons r (wrap x w mod))) body))
1335 (ids '()) (labels '()) (vars '()) (vals '()) (bindings '()))
1337 (syntax-violation #f "no expressions in body" outer-form)
1338 (let ((e (cdar body)) (er (caar body)))
1340 (lambda () (syntax-type e er empty-wrap no-source ribcage mod))
1341 (lambda (type value e w s mod)
1344 (let ((id (wrap value w mod)) (label (gen-label)))
1345 (let ((var (gen-var id)))
1346 (extend-ribcage! ribcage id label)
1348 (cons id ids) (cons label labels)
1349 (cons var vars) (cons (cons er (wrap e w mod)) vals)
1350 (cons (make-binding 'lexical var) bindings)))))
1351 ((define-syntax-form)
1352 (let ((id (wrap value w mod)) (label (gen-label)))
1353 (extend-ribcage! ribcage id label)
1355 (cons id ids) (cons label labels)
1357 (cons (make-binding 'macro (cons er (wrap e w mod)))
1362 (parse (let f ((forms (syntax (e1 ...))))
1365 (cons (cons er (wrap (car forms) w mod))
1367 ids labels vars vals bindings))))
1368 ((local-syntax-form)
1369 (chi-local-syntax value e er w s mod
1370 (lambda (forms er w s mod)
1371 (parse (let f ((forms forms))
1374 (cons (cons er (wrap (car forms) w mod))
1376 ids labels vars vals bindings))))
1377 (else ; found a non-definition
1379 (build-sequence no-source
1381 (chi (cdr x) (car x) empty-wrap mod))
1382 (cons (cons er (source-wrap e w s mod))
1385 (if (not (valid-bound-ids? ids))
1387 #f "invalid or duplicate identifier in definition"
1389 (let loop ((bs bindings) (er-cache #f) (r-cache #f))
1390 (if (not (null? bs))
1391 (let* ((b (car bs)))
1392 (if (eq? (car b) 'macro)
1393 (let* ((er (cadr b))
1395 (if (eq? er er-cache)
1397 (macros-only-env er))))
1399 (eval-local-transformer
1400 (chi (cddr b) r-cache empty-wrap mod)
1402 (loop (cdr bs) er r-cache))
1403 (loop (cdr bs) er-cache r-cache)))))
1404 (set-cdr! r (extend-env labels bindings (cdr r)))
1405 (build-letrec no-source
1406 (map syntax->datum ids)
1409 (chi (cdr x) (car x) empty-wrap mod))
1411 (build-sequence no-source
1413 (chi (cdr x) (car x) empty-wrap mod))
1414 (cons (cons er (source-wrap e w s mod))
1415 (cdr body)))))))))))))))))
1417 (define chi-lambda-clause
1418 (lambda (e docstring c r w mod k)
1420 ((args doc e1 e2 ...)
1421 (and (string? (syntax->datum (syntax doc))) (not docstring))
1422 (chi-lambda-clause e (syntax doc) (syntax (args e1 e2 ...)) r w mod k))
1423 (((id ...) e1 e2 ...)
1424 (let ((ids (syntax (id ...))))
1425 (if (not (valid-bound-ids? ids))
1426 (syntax-violation 'lambda "invalid parameter list" e)
1427 (let ((labels (gen-labels ids))
1428 (new-vars (map gen-var ids)))
1429 (k (map syntax->datum ids)
1432 (chi-body (syntax (e1 e2 ...))
1434 (extend-var-env labels new-vars r)
1435 (make-binding-wrap ids labels w)
1438 (let ((old-ids (lambda-var-list (syntax ids))))
1439 (if (not (valid-bound-ids? old-ids))
1440 (syntax-violation 'lambda "invalid parameter list" e)
1441 (let ((labels (gen-labels old-ids))
1442 (new-vars (map gen-var old-ids)))
1443 (k (let f ((ls1 (cdr old-ids)) (ls2 (car old-ids)))
1446 (f (cdr ls1) (cons (syntax->datum (car ls1)) ls2))))
1447 (let f ((ls1 (cdr new-vars)) (ls2 (car new-vars)))
1450 (f (cdr ls1) (cons (car ls1) ls2))))
1452 (chi-body (syntax (e1 e2 ...))
1454 (extend-var-env labels new-vars r)
1455 (make-binding-wrap old-ids labels w)
1457 (_ (syntax-violation 'lambda "bad lambda" e)))))
1459 (define chi-local-syntax
1460 (lambda (rec? e r w s mod k)
1462 ((_ ((id val) ...) e1 e2 ...)
1463 (let ((ids (syntax (id ...))))
1464 (if (not (valid-bound-ids? ids))
1465 (syntax-violation #f "duplicate bound keyword" e)
1466 (let ((labels (gen-labels ids)))
1467 (let ((new-w (make-binding-wrap ids labels w)))
1468 (k (syntax (e1 e2 ...))
1471 (let ((w (if rec? new-w w))
1472 (trans-r (macros-only-env r)))
1474 (make-binding 'macro
1475 (eval-local-transformer
1476 (chi x trans-r w mod)
1478 (syntax (val ...))))
1483 (_ (syntax-violation #f "bad local syntax definition"
1484 (source-wrap e w s mod))))))
1486 (define eval-local-transformer
1487 (lambda (expanded mod)
1488 (let ((p (local-eval-hook expanded mod)))
1491 (syntax-violation #f "nonprocedure transformer" p)))))
1495 (build-void no-source)))
1499 (and (nonsymbol-id? x)
1500 (free-id=? x (syntax (... ...))))))
1504 ;;; strips all annotations from potentially circular reader output
1506 (define strip-annotation
1510 (let ((new (cons #f #f)))
1511 (if parent (set-annotation-stripped! parent new))
1512 (set-car! new (strip-annotation (car x) #f))
1513 (set-cdr! new (strip-annotation (cdr x) #f))
1516 (or (annotation-stripped x)
1517 (strip-annotation (annotation-expression x) x)))
1519 (let ((new (make-vector (vector-length x))))
1520 (if parent (set-annotation-stripped! parent new))
1521 (let loop ((i (- (vector-length x) 1)))
1523 (vector-set! new i (strip-annotation (vector-ref x i) #f))
1528 ;;; strips syntax-objects down to top-wrap; if top-wrap is layered directly
1529 ;;; on an annotation, strips the annotation as well.
1530 ;;; since only the head of a list is annotated by the reader, not each pair
1531 ;;; in the spine, we also check for pairs whose cars are annotated in case
1532 ;;; we've been passed the cdr of an annotated list
1537 (if (or (annotation? x) (and (pair? x) (annotation? (car x))))
1538 (strip-annotation x #f)
1543 (strip (syntax-object-expression x) (syntax-object-wrap x)))
1545 (let ((a (f (car x))) (d (f (cdr x))))
1546 (if (and (eq? a (car x)) (eq? d (cdr x)))
1550 (let ((old (vector->list x)))
1551 (let ((new (map f old)))
1552 (if (and-map* eq? old new) x (list->vector new)))))
1555 ;;; lexical variables
1559 (let ((id (if (syntax-object? id) (syntax-object-expression id) id)))
1560 (if (annotation? id)
1561 (build-lexical-var (annotation-source id) (annotation-expression id))
1562 (build-lexical-var no-source id)))))
1564 (define lambda-var-list
1566 (let lvl ((vars vars) (ls '()) (w empty-wrap))
1568 ((pair? vars) (lvl (cdr vars) (cons (wrap (car vars) w #f) ls) w))
1569 ((id? vars) (cons (wrap vars w #f) ls))
1571 ((syntax-object? vars)
1572 (lvl (syntax-object-expression vars)
1574 (join-wraps w (syntax-object-wrap vars))))
1576 (lvl (annotation-expression vars) ls w))
1577 ; include anything else to be caught by subsequent error
1579 (else (cons vars ls))))))
1581 ;;; core transformers
1583 (global-extend 'local-syntax 'letrec-syntax #t)
1584 (global-extend 'local-syntax 'let-syntax #f)
1586 (global-extend 'core 'fluid-let-syntax
1587 (lambda (e r w s mod)
1589 ((_ ((var val) ...) e1 e2 ...)
1590 (valid-bound-ids? (syntax (var ...)))
1591 (let ((names (map (lambda (x) (id-var-name x w)) (syntax (var ...)))))
1594 (case (binding-type (lookup n r mod))
1595 ((displaced-lexical)
1596 (syntax-violation 'fluid-let-syntax
1597 "identifier out of context"
1599 (source-wrap id w s mod)))))
1603 (syntax (e1 e2 ...))
1604 (source-wrap e w s mod)
1607 (let ((trans-r (macros-only-env r)))
1609 (make-binding 'macro
1610 (eval-local-transformer (chi x trans-r w mod)
1612 (syntax (val ...))))
1616 (_ (syntax-violation 'fluid-let-syntax "bad syntax"
1617 (source-wrap e w s mod))))))
1619 (global-extend 'core 'quote
1620 (lambda (e r w s mod)
1622 ((_ e) (build-data s (strip (syntax e) w)))
1623 (_ (syntax-violation 'quote "bad syntax"
1624 (source-wrap e w s mod))))))
1626 (global-extend 'core 'syntax
1629 (lambda (src e r maps ellipsis? mod)
1631 (let ((label (id-var-name e empty-wrap)))
1632 (let ((b (lookup label r mod)))
1633 (if (eq? (binding-type b) 'syntax)
1636 (let ((var.lev (binding-value b)))
1637 (gen-ref src (car var.lev) (cdr var.lev) maps)))
1638 (lambda (var maps) (values `(ref ,var) maps)))
1640 (syntax-violation 'syntax "misplaced ellipsis" src)
1641 (values `(quote ,e) maps)))))
1644 (ellipsis? (syntax dots))
1645 (gen-syntax src (syntax e) r maps (lambda (x) #f) mod))
1647 ; this could be about a dozen lines of code, except that we
1648 ; choose to handle (syntax (x ... ...)) forms
1649 (ellipsis? (syntax dots))
1650 (let f ((y (syntax y))
1654 (gen-syntax src (syntax x) r
1655 (cons '() maps) ellipsis? mod))
1657 (if (null? (car maps))
1658 (syntax-violation 'syntax "extra ellipsis"
1660 (values (gen-map x (car maps))
1664 (ellipsis? (syntax dots))
1668 (lambda () (k (cons '() maps)))
1670 (if (null? (car maps))
1671 (syntax-violation 'syntax "extra ellipsis" src)
1672 (values (gen-mappend x (car maps))
1674 (_ (call-with-values
1675 (lambda () (gen-syntax src y r maps ellipsis? mod))
1678 (lambda () (k maps))
1680 (values (gen-append x y) maps)))))))))
1683 (lambda () (gen-syntax src (syntax x) r maps ellipsis? mod))
1686 (lambda () (gen-syntax src (syntax y) r maps ellipsis? mod))
1687 (lambda (y maps) (values (gen-cons x y) maps))))))
1691 (gen-syntax src (syntax (e1 e2 ...)) r maps ellipsis? mod))
1692 (lambda (e maps) (values (gen-vector e) maps))))
1693 (_ (values `(quote ,e) maps))))))
1696 (lambda (src var level maps)
1700 (syntax-violation 'syntax "missing ellipsis" src)
1702 (lambda () (gen-ref src var (fx- level 1) (cdr maps)))
1703 (lambda (outer-var outer-maps)
1704 (let ((b (assq outer-var (car maps))))
1706 (values (cdr b) maps)
1707 (let ((inner-var (gen-var 'tmp)))
1709 (cons (cons (cons outer-var inner-var)
1711 outer-maps)))))))))))
1715 `(apply (primitive append) ,(gen-map e map-env))))
1719 (let ((formals (map cdr map-env))
1720 (actuals (map (lambda (x) `(ref ,(car x))) map-env)))
1723 ; identity map equivalence:
1724 ; (map (lambda (x) x) y) == y
1727 (lambda (x) (and (eq? (car x) 'ref) (memq (cadr x) formals)))
1729 ; eta map equivalence:
1730 ; (map (lambda (x ...) (f x ...)) y ...) == (map f y ...)
1731 `(map (primitive ,(car e))
1732 ,@(map (let ((r (map cons formals actuals)))
1733 (lambda (x) (cdr (assq (cadr x) r))))
1735 (else `(map (lambda ,formals ,e) ,@actuals))))))
1741 (if (eq? (car x) 'quote)
1742 `(quote (,(cadr x) . ,(cadr y)))
1743 (if (eq? (cadr y) '())
1746 ((list) `(list ,x ,@(cdr y)))
1747 (else `(cons ,x ,y)))))
1751 (if (equal? y '(quote ()))
1758 ((eq? (car x) 'list) `(vector ,@(cdr x)))
1759 ((eq? (car x) 'quote) `(quote #(,@(cadr x))))
1760 (else `(list->vector ,x)))))
1766 ((ref) (build-lexical-reference 'value no-source (cadr x) (cadr x)))
1767 ((primitive) (build-primref no-source (cadr x)))
1768 ((quote) (build-data no-source (cadr x)))
1769 ((lambda) (build-lambda no-source (cadr x) (cadr x) #f (regen (caddr x))))
1770 ((map) (let ((ls (map regen (cdr x))))
1771 (build-application no-source
1772 ;; this check used to be here, not sure what for:
1773 ;; (if (fx= (length ls) 2)
1774 (build-primref no-source 'map)
1776 (else (build-application no-source
1777 (build-primref no-source (car x))
1778 (map regen (cdr x)))))))
1780 (lambda (e r w s mod)
1781 (let ((e (source-wrap e w s mod)))
1785 (lambda () (gen-syntax e (syntax x) r '() ellipsis? mod))
1786 (lambda (e maps) (regen e))))
1787 (_ (syntax-violation 'syntax "bad `syntax' form" e)))))))
1790 (global-extend 'core 'lambda
1791 (lambda (e r w s mod)
1794 (chi-lambda-clause (source-wrap e w s mod) #f (syntax c) r w mod
1795 (lambda (names vars docstring body)
1796 (build-lambda s names vars docstring body)))))))
1799 (global-extend 'core 'let
1801 (define (chi-let e r w s mod constructor ids vals exps)
1802 (if (not (valid-bound-ids? ids))
1803 (syntax-violation 'let "duplicate bound variable" e)
1804 (let ((labels (gen-labels ids))
1805 (new-vars (map gen-var ids)))
1806 (let ((nw (make-binding-wrap ids labels w))
1807 (nr (extend-var-env labels new-vars r)))
1809 (map syntax->datum ids)
1811 (map (lambda (x) (chi x r w mod)) vals)
1812 (chi-body exps (source-wrap e nw s mod)
1814 (lambda (e r w s mod)
1816 ((_ ((id val) ...) e1 e2 ...)
1817 (chi-let e r w s mod
1821 (syntax (e1 e2 ...))))
1822 ((_ f ((id val) ...) e1 e2 ...)
1824 (chi-let e r w s mod
1828 (syntax (e1 e2 ...))))
1829 (_ (syntax-violation 'let "bad let" (source-wrap e w s mod)))))))
1832 (global-extend 'core 'letrec
1833 (lambda (e r w s mod)
1835 ((_ ((id val) ...) e1 e2 ...)
1836 (let ((ids (syntax (id ...))))
1837 (if (not (valid-bound-ids? ids))
1838 (syntax-violation 'letrec "duplicate bound variable" e)
1839 (let ((labels (gen-labels ids))
1840 (new-vars (map gen-var ids)))
1841 (let ((w (make-binding-wrap ids labels w))
1842 (r (extend-var-env labels new-vars r)))
1844 (map syntax->datum ids)
1846 (map (lambda (x) (chi x r w mod)) (syntax (val ...)))
1847 (chi-body (syntax (e1 e2 ...))
1848 (source-wrap e w s mod) r w mod)))))))
1849 (_ (syntax-violation 'letrec "bad letrec" (source-wrap e w s mod))))))
1852 (global-extend 'core 'set!
1853 (lambda (e r w s mod)
1857 (let ((val (chi (syntax val) r w mod))
1858 (n (id-var-name (syntax id) w)))
1859 (let ((b (lookup n r mod)))
1860 (case (binding-type b)
1862 (build-lexical-assignment s
1863 (syntax->datum (syntax id))
1866 ((global) (build-global-assignment s n val mod))
1867 ((displaced-lexical)
1868 (syntax-violation 'set! "identifier out of context"
1869 (wrap (syntax id) w mod)))
1870 (else (syntax-violation 'set! "bad set!"
1871 (source-wrap e w s mod)))))))
1872 ((_ (head tail ...) val)
1874 (lambda () (syntax-type (syntax head) r empty-wrap no-source #f mod))
1875 (lambda (type value ee ww ss modmod)
1878 (let ((val (chi (syntax val) r w mod)))
1879 (call-with-values (lambda () (value (syntax (head tail ...))))
1881 (build-global-assignment s id val mod)))))
1883 (build-application s
1884 (chi (syntax (setter head)) r w mod)
1885 (map (lambda (e) (chi e r w mod))
1886 (syntax (tail ... val)))))))))
1887 (_ (syntax-violation 'set! "bad set!" (source-wrap e w s mod))))))
1889 (global-extend 'module-ref '@
1893 (and (and-map id? (syntax (mod ...))) (id? (syntax id)))
1894 (values (syntax->datum (syntax id))
1896 (syntax (public mod ...))))))))
1898 (global-extend 'module-ref '@@
1902 (and (and-map id? (syntax (mod ...))) (id? (syntax id)))
1903 (values (syntax->datum (syntax id))
1905 (syntax (private mod ...))))))))
1907 (global-extend 'core 'if
1908 (lambda (e r w s mod)
1913 (chi (syntax test) r w mod)
1914 (chi (syntax then) r w mod)
1915 (build-void no-source)))
1919 (chi (syntax test) r w mod)
1920 (chi (syntax then) r w mod)
1921 (chi (syntax else) r w mod))))))
1923 (global-extend 'begin 'begin '())
1925 (global-extend 'define 'define '())
1927 (global-extend 'define-syntax 'define-syntax '())
1929 (global-extend 'eval-when 'eval-when '())
1931 (global-extend 'core 'syntax-case
1933 (define convert-pattern
1934 ; accepts pattern & keys
1935 ; returns $sc-dispatch pattern & ids
1936 (lambda (pattern keys)
1937 (let cvt ((p pattern) (n 0) (ids '()))
1939 (if (bound-id-member? p keys)
1940 (values (vector 'free-id p) ids)
1941 (values 'any (cons (cons p n) ids)))
1944 (ellipsis? (syntax dots))
1946 (lambda () (cvt (syntax x) (fx+ n 1) ids))
1948 (values (if (eq? p 'any) 'each-any (vector 'each p))
1952 (lambda () (cvt (syntax y) n ids))
1955 (lambda () (cvt (syntax x) n ids))
1957 (values (cons x y) ids))))))
1958 (() (values '() ids))
1961 (lambda () (cvt (syntax (x ...)) n ids))
1962 (lambda (p ids) (values (vector 'vector p) ids))))
1963 (x (values (vector 'atom (strip p empty-wrap)) ids)))))))
1965 (define build-dispatch-call
1966 (lambda (pvars exp y r mod)
1967 (let ((ids (map car pvars)) (levels (map cdr pvars)))
1968 (let ((labels (gen-labels ids)) (new-vars (map gen-var ids)))
1969 (build-application no-source
1970 (build-primref no-source 'apply)
1971 (list (build-lambda no-source (map syntax->datum ids) new-vars #f
1975 (map (lambda (var level)
1976 (make-binding 'syntax `(,var . ,level)))
1980 (make-binding-wrap ids labels empty-wrap)
1985 (lambda (x keys clauses r pat fender exp mod)
1987 (lambda () (convert-pattern pat keys))
1990 ((not (distinct-bound-ids? (map car pvars)))
1991 (syntax-violation 'syntax-case "duplicate pattern variable" pat))
1992 ((not (and-map (lambda (x) (not (ellipsis? (car x)))) pvars))
1993 (syntax-violation 'syntax-case "misplaced ellipsis" pat))
1995 (let ((y (gen-var 'tmp)))
1996 ; fat finger binding and references to temp variable y
1997 (build-application no-source
1998 (build-lambda no-source (list 'tmp) (list y) #f
1999 (let ((y (build-lexical-reference 'value no-source
2001 (build-conditional no-source
2002 (syntax-case fender ()
2004 (_ (build-conditional no-source
2006 (build-dispatch-call pvars fender y r mod)
2007 (build-data no-source #f))))
2008 (build-dispatch-call pvars exp y r mod)
2009 (gen-syntax-case x keys clauses r mod))))
2010 (list (if (eq? p 'any)
2011 (build-application no-source
2012 (build-primref no-source 'list)
2014 (build-application no-source
2015 (build-primref no-source '$sc-dispatch)
2016 (list x (build-data no-source p)))))))))))))
2018 (define gen-syntax-case
2019 (lambda (x keys clauses r mod)
2021 (build-application no-source
2022 (build-primref no-source 'syntax-violation)
2023 (list #f "source expression failed to match any pattern" x))
2024 (syntax-case (car clauses) ()
2026 (if (and (id? (syntax pat))
2027 (and-map (lambda (x) (not (free-id=? (syntax pat) x)))
2028 (cons (syntax (... ...)) keys)))
2029 (let ((labels (list (gen-label)))
2030 (var (gen-var (syntax pat))))
2031 (build-application no-source
2032 (build-lambda no-source
2033 (list (syntax->datum (syntax pat))) (list var)
2037 (list (make-binding 'syntax `(,var . 0)))
2039 (make-binding-wrap (syntax (pat))
2043 (gen-clause x keys (cdr clauses) r
2044 (syntax pat) #t (syntax exp) mod)))
2046 (gen-clause x keys (cdr clauses) r
2047 (syntax pat) (syntax fender) (syntax exp) mod))
2048 (_ (syntax-violation 'syntax-case "invalid clause"
2051 (lambda (e r w s mod)
2052 (let ((e (source-wrap e w s mod)))
2054 ((_ val (key ...) m ...)
2055 (if (and-map (lambda (x) (and (id? x) (not (ellipsis? x))))
2057 (let ((x (gen-var 'tmp)))
2058 ; fat finger binding and references to temp variable x
2059 (build-application s
2060 (build-lambda no-source (list 'tmp) (list x) #f
2061 (gen-syntax-case (build-lexical-reference 'value no-source
2063 (syntax (key ...)) (syntax (m ...))
2066 (list (chi (syntax val) r empty-wrap mod))))
2067 (syntax-violation 'syntax-case "invalid literals list" e))))))))
2069 ;;; The portable sc-expand seeds chi-top's mode m with 'e (for
2070 ;;; evaluating) and esew (which stands for "eval syntax expanders
2071 ;;; when") with '(eval). In Chez Scheme, m is set to 'c instead of e
2072 ;;; if we are compiling a file, and esew is set to
2073 ;;; (eval-syntactic-expanders-when), which defaults to the list
2074 ;;; '(compile load eval). This means that, by default, top-level
2075 ;;; syntactic definitions are evaluated immediately after they are
2076 ;;; expanded, and the expanded definitions are also residualized into
2077 ;;; the object file if we are compiling a file.
2080 (if (and (pair? x) (equal? (car x) noexpand))
2082 (let ((m (if (null? rest) 'e (car rest)))
2083 (esew (if (or (null? rest) (null? (cdr rest)))
2086 (with-fluid* *mode* m
2088 (chi-top x null-env top-wrap m esew
2089 (cons 'hygiene (module-name (current-module))))))))))
2097 (make-syntax-object datum (syntax-object-wrap id) #f)))
2100 ; accepts any object, since syntax objects may consist partially
2101 ; or entirely of unwrapped, nonsymbolic data
2103 (strip x empty-wrap)))
2105 (set! generate-temporaries
2107 (arg-check list? ls 'generate-temporaries)
2108 (map (lambda (x) (wrap (gensym-hook) top-wrap #f)) ls)))
2110 (set! free-identifier=?
2112 (arg-check nonsymbol-id? x 'free-identifier=?)
2113 (arg-check nonsymbol-id? y 'free-identifier=?)
2116 (set! bound-identifier=?
2118 (arg-check nonsymbol-id? x 'bound-identifier=?)
2119 (arg-check nonsymbol-id? y 'bound-identifier=?)
2122 (set! syntax-violation
2123 (lambda (who message form . subform)
2124 (arg-check (lambda (x) (or (not x) (string? x) (symbol? x)))
2125 who 'syntax-violation)
2126 (arg-check string? message 'syntax-violation)
2127 (scm-error 'syntax-error 'sc-expand
2131 (if (null? subform) "in ~a" "in subform `~s' of `~s'"))
2132 (let ((tail (cons message
2133 (map (lambda (x) (strip x empty-wrap))
2134 (append subform (list form))))))
2135 (if who (cons who tail) tail))
2138 ;;; $sc-dispatch expects an expression and a pattern. If the expression
2139 ;;; matches the pattern a list of the matching expressions for each
2140 ;;; "any" is returned. Otherwise, #f is returned. (This use of #f will
2141 ;;; not work on r4rs implementations that violate the ieee requirement
2142 ;;; that #f and () be distinct.)
2144 ;;; The expression is matched with the pattern as follows:
2146 ;;; pattern: matches:
2149 ;;; (<pattern>1 . <pattern>2) (<pattern>1 . <pattern>2)
2151 ;;; #(free-id <key>) <key> with free-identifier=?
2152 ;;; #(each <pattern>) (<pattern>*)
2153 ;;; #(vector <pattern>) (list->vector <pattern>)
2154 ;;; #(atom <object>) <object> with "equal?"
2156 ;;; Vector cops out to pair under assumption that vectors are rare. If
2157 ;;; not, should convert to:
2158 ;;; #(vector <pattern>*) #(<pattern>*)
2166 (match-each (annotation-expression e) p w mod))
2168 (let ((first (match (car e) p w '() mod)))
2170 (let ((rest (match-each (cdr e) p w mod)))
2171 (and rest (cons first rest))))))
2174 (match-each (syntax-object-expression e)
2176 (join-wraps w (syntax-object-wrap e))
2177 (syntax-object-module e)))
2180 (define match-each-any
2184 (match-each-any (annotation-expression e) w mod))
2186 (let ((l (match-each-any (cdr e) w mod)))
2187 (and l (cons (wrap (car e) w mod) l))))
2190 (match-each-any (syntax-object-expression e)
2191 (join-wraps w (syntax-object-wrap e))
2199 ((eq? p 'any) (cons '() r))
2200 ((pair? p) (match-empty (car p) (match-empty (cdr p) r)))
2201 ((eq? p 'each-any) (cons '() r))
2203 (case (vector-ref p 0)
2204 ((each) (match-empty (vector-ref p 1) r))
2206 ((vector) (match-empty (vector-ref p 1) r)))))))
2209 (lambda (e p w r mod)
2211 ((null? p) (and (null? e) r))
2213 (and (pair? e) (match (car e) (car p) w
2214 (match (cdr e) (cdr p) w r mod)
2217 (let ((l (match-each-any e w mod))) (and l (cons l r))))
2219 (case (vector-ref p 0)
2222 (match-empty (vector-ref p 1) r)
2223 (let ((l (match-each e (vector-ref p 1) w mod)))
2225 (let collect ((l l))
2228 (cons (map car l) (collect (map cdr l)))))))))
2229 ((free-id) (and (id? e) (free-id=? (wrap e w mod) (vector-ref p 1)) r))
2230 ((atom) (and (equal? (vector-ref p 1) (strip e w)) r))
2233 (match (vector->list e) (vector-ref p 1) w r mod))))))))
2236 (lambda (e p w r mod)
2239 ((eq? p 'any) (cons (wrap e w mod) r))
2242 (unannotate (syntax-object-expression e))
2244 (join-wraps w (syntax-object-wrap e))
2246 (syntax-object-module e)))
2247 (else (match* (unannotate e) p w r mod)))))
2252 ((eq? p 'any) (list e))
2254 (match* (unannotate (syntax-object-expression e))
2255 p (syntax-object-wrap e) '() (syntax-object-module e)))
2256 (else (match* (unannotate e) p empty-wrap '() #f)))))
2261 (define-syntax with-syntax
2265 (syntax (begin e1 e2 ...)))
2266 ((_ ((out in)) e1 e2 ...)
2267 (syntax (syntax-case in () (out (begin e1 e2 ...)))))
2268 ((_ ((out in) ...) e1 e2 ...)
2269 (syntax (syntax-case (list in ...) ()
2270 ((out ...) (begin e1 e2 ...))))))))
2272 (define-syntax syntax-rules
2275 ((_ (k ...) ((keyword . pattern) template) ...)
2277 (syntax-case x (k ...)
2278 ((dummy . pattern) (syntax template))
2284 ((let* ((x v) ...) e1 e2 ...)
2285 (and-map identifier? (syntax (x ...)))
2286 (let f ((bindings (syntax ((x v) ...))))
2287 (if (null? bindings)
2288 (syntax (let () e1 e2 ...))
2289 (with-syntax ((body (f (cdr bindings)))
2290 (binding (car bindings)))
2291 (syntax (let (binding) body)))))))))
2295 (syntax-case orig-x ()
2296 ((_ ((var init . step) ...) (e0 e1 ...) c ...)
2297 (with-syntax (((step ...)
2302 (_ (syntax-violation
2303 'do "bad step expression"
2306 (syntax (step ...)))))
2307 (syntax-case (syntax (e1 ...)) ()
2308 (() (syntax (let doloop ((var init) ...)
2310 (begin c ... (doloop step ...))))))
2312 (syntax (let doloop ((var init) ...)
2315 (begin c ... (doloop step ...))))))))))))
2317 (define-syntax quasiquote
2321 (with-syntax ((x x) (y y))
2322 (syntax-case (syntax y) (quote list)
2324 (syntax-case (syntax x) (quote)
2325 ((quote dx) (syntax (quote (dx . dy))))
2326 (_ (if (null? (syntax dy))
2328 (syntax (cons x y))))))
2329 ((list . stuff) (syntax (list x . stuff)))
2330 (else (syntax (cons x y)))))))
2333 (with-syntax ((x x) (y y))
2334 (syntax-case (syntax y) (quote)
2335 ((quote ()) (syntax x))
2336 (_ (syntax (append x y)))))))
2339 (with-syntax ((x x))
2340 (syntax-case (syntax x) (quote list)
2341 ((quote (x ...)) (syntax (quote #(x ...))))
2342 ((list x ...) (syntax (vector x ...)))
2343 (_ (syntax (list->vector x)))))))
2346 (syntax-case p (unquote unquote-splicing quasiquote)
2350 (quasicons (syntax (quote unquote))
2351 (quasi (syntax (p)) (- lev 1)))))
2352 (((unquote-splicing p) . q)
2354 (quasiappend (syntax p) (quasi (syntax q) lev))
2355 (quasicons (quasicons (syntax (quote unquote-splicing))
2356 (quasi (syntax (p)) (- lev 1)))
2357 (quasi (syntax q) lev))))
2359 (quasicons (syntax (quote quasiquote))
2360 (quasi (syntax (p)) (+ lev 1))))
2362 (quasicons (quasi (syntax p) lev) (quasi (syntax q) lev)))
2363 (#(x ...) (quasivector (quasi (syntax (x ...)) lev)))
2364 (p (syntax (quote p)))))))
2367 ((_ e) (quasi (syntax e) 0))))))
2369 (define-syntax include
2373 (let ((p (open-input-file fn)))
2374 (let f ((x (read p)))
2376 (begin (close-input-port p) '())
2377 (cons (datum->syntax k x)
2381 (let ((fn (syntax->datum (syntax filename))))
2382 (with-syntax (((exp ...) (read-file fn (syntax k))))
2383 (syntax (begin exp ...))))))))
2385 (define-syntax unquote
2389 (syntax-violation 'unquote
2390 "expression not valid outside of quasiquote"
2393 (define-syntax unquote-splicing
2397 (syntax-violation 'unquote-splicing
2398 "expression not valid outside of quasiquote"
2406 ((body (let f ((clause (syntax m1)) (clauses (syntax (m2 ...))))
2408 (syntax-case clause (else)
2409 ((else e1 e2 ...) (syntax (begin e1 e2 ...)))
2410 (((k ...) e1 e2 ...)
2411 (syntax (if (memv t '(k ...)) (begin e1 e2 ...))))
2412 (_ (syntax-violation 'case "bad clause" x clause)))
2413 (with-syntax ((rest (f (car clauses) (cdr clauses))))
2414 (syntax-case clause (else)
2415 (((k ...) e1 e2 ...)
2416 (syntax (if (memv t '(k ...))
2419 (_ (syntax-violation 'case "bad clause" x
2421 (syntax (let ((t e)) body)))))))
2423 (define-syntax identifier-syntax
2431 (identifier? (syntax id))
2434 (syntax (e x (... ...)))))))))))