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
354 (define build-application
355 (lambda (source fun-exp arg-exps)
356 (case (fluid-ref *mode*)
357 ((c) ((@ (language tree-il) make-application) source fun-exp arg-exps))
358 (else `(,fun-exp . ,arg-exps)))))
360 (define build-conditional
361 (lambda (source test-exp then-exp else-exp)
362 (case (fluid-ref *mode*)
363 ((c) ((@ (language tree-il) make-conditional)
364 source test-exp then-exp else-exp))
365 (else `(if ,test-exp ,then-exp ,else-exp)))))
367 (define build-lexical-reference
368 (lambda (type source name var)
369 (case (fluid-ref *mode*)
370 ((c) ((@ (language tree-il) make-lexical-ref) source name var))
373 (define build-lexical-assignment
374 (lambda (source name var exp)
375 (case (fluid-ref *mode*)
376 ((c) ((@ (language tree-il) make-lexical-set) source name var exp))
377 (else `(set! ,var ,exp)))))
379 ;; Before modules are booted, we can't expand into data structures from
380 ;; (language tree-il) -- we need to give the evaluator the
381 ;; s-expressions that it understands natively. Actually the real truth
382 ;; of the matter is that the evaluator doesn't understand tree-il
383 ;; structures at all. So until we fix the evaluator, if ever, the
384 ;; conflation that we should use tree-il iff we are compiling
387 (define (analyze-variable mod var modref-cont bare-cont)
390 (let ((kind (car mod))
393 ((public) (modref-cont mod var #t))
394 ((private) (if (not (equal? mod (module-name (current-module))))
395 (modref-cont mod var #f)
397 ((bare) (bare-cont var))
398 ((hygiene) (if (and (not (equal? mod (module-name (current-module))))
399 (module-variable (resolve-module mod) var))
400 (modref-cont mod var #f)
402 (else (syntax-violation #f "bad module kind" var mod))))))
404 (define build-global-reference
405 (lambda (source var mod)
408 (lambda (mod var public?)
409 (case (fluid-ref *mode*)
410 ((c) ((@ (language tree-il) make-module-ref) source mod var public?))
411 (else (list (if public? '@ '@@) mod var))))
413 (case (fluid-ref *mode*)
414 ((c) ((@ (language tree-il) make-toplevel-ref) source var))
417 (define build-global-assignment
418 (lambda (source var exp mod)
421 (lambda (mod var public?)
422 (case (fluid-ref *mode*)
423 ((c) ((@ (language tree-il) make-module-set) source mod var public? exp))
424 (else `(set! ,(list (if public? '@ '@@) mod var) ,exp))))
426 (case (fluid-ref *mode*)
427 ((c) ((@ (language tree-il) make-toplevel-set) source var exp))
428 (else `(set! ,var ,exp)))))))
430 (define build-global-definition
431 (lambda (source var exp)
432 (case (fluid-ref *mode*)
433 ((c) ((@ (language tree-il) make-toplevel-define) source var exp))
434 (else `(define ,var ,exp)))))
437 (lambda (src ids vars docstring exp)
438 (case (fluid-ref *mode*)
439 ((c) ((@ (language tree-il) make-lambda) src ids vars
440 (if docstring `((documentation . ,docstring)) '())
442 (else `(lambda ,vars ,@(if docstring (list docstring) '())
445 (define build-primref
447 (case (fluid-ref *mode*)
448 ((c) ((@ (language tree-il) make-primitive-ref) src name))
449 ;; hygiene guile is a hack
450 (else (build-global-reference src name '(hygiene guile))))))
452 (define (build-data src exp)
453 (case (fluid-ref *mode*)
454 ((c) ((@ (language tree-il) make-const) src exp))
455 (else (if (and (self-evaluating? exp) (not (vector? exp)))
457 (list 'quote exp)))))
459 (define build-sequence
461 (if (null? (cdr exps))
463 (case (fluid-ref *mode*)
464 ((c) ((@ (language tree-il) make-sequence) src exps))
465 (else `(begin ,@exps))))))
468 (lambda (src ids vars val-exps body-exp)
471 (case (fluid-ref *mode*)
472 ((c) ((@ (language tree-il) make-let) src ids vars val-exps body-exp))
473 (else `(let ,(map list vars val-exps) ,body-exp))))))
475 (define build-named-let
476 (lambda (src ids vars val-exps body-exp)
481 (case (fluid-ref *mode*)
482 ((c) ((@ (language tree-il) make-letrec) src
485 (list (build-lambda src ids vars #f body-exp))
486 (build-application src (build-lexical-reference 'fun src f-name f)
488 (else `(let ,f ,(map list vars val-exps) ,body-exp))))))
491 (lambda (src ids vars val-exps body-exp)
494 (case (fluid-ref *mode*)
495 ((c) ((@ (language tree-il) make-letrec) src ids vars val-exps body-exp))
496 (else `(letrec ,(map list vars val-exps) ,body-exp))))))
498 ;; FIXME: wingo: use make-lexical ?
499 (define-syntax build-lexical-var
501 ((_ src id) (gensym (symbol->string id)))))
503 (define-structure (syntax-object expression wrap module))
505 (define-syntax unannotate
510 (annotation-expression e)
513 (define-syntax no-source (identifier-syntax #f))
515 (define source-annotation
518 ((annotation? x) (annotation-source x))
519 ((syntax-object? x) (source-annotation (syntax-object-expression x)))
522 (define-syntax arg-check
526 (if (not (pred? x)) (syntax-violation who "invalid argument" x))))))
528 ;;; compile-time environments
530 ;;; wrap and environment comprise two level mapping.
531 ;;; wrap : id --> label
532 ;;; env : label --> <element>
534 ;;; environments are represented in two parts: a lexical part and a global
535 ;;; part. The lexical part is a simple list of associations from labels
536 ;;; to bindings. The global part is implemented by
537 ;;; {put,get}-global-definition-hook and associates symbols with
540 ;;; global (assumed global variable) and displaced-lexical (see below)
541 ;;; do not show up in any environment; instead, they are fabricated by
542 ;;; lookup when it finds no other bindings.
544 ;;; <environment> ::= ((<label> . <binding>)*)
546 ;;; identifier bindings include a type and a value
548 ;;; <binding> ::= (macro . <procedure>) macros
549 ;;; (core . <procedure>) core forms
550 ;;; (external-macro . <procedure>) external-macro
551 ;;; (module-ref . <procedure>) @ or @@
554 ;;; (define-syntax) define-syntax
555 ;;; (local-syntax . rec?) let-syntax/letrec-syntax
556 ;;; (eval-when) eval-when
557 ;;; (syntax . (<var> . <level>)) pattern variables
558 ;;; (global) assumed global variable
559 ;;; (lexical . <var>) lexical variables
560 ;;; (displaced-lexical) displaced lexicals
561 ;;; <level> ::= <nonnegative integer>
562 ;;; <var> ::= variable returned by build-lexical-var
564 ;;; a macro is a user-defined syntactic-form. a core is a system-defined
565 ;;; syntactic form. begin, define, define-syntax, and eval-when are
566 ;;; treated specially since they are sensitive to whether the form is
567 ;;; at top-level and (except for eval-when) can denote valid internal
570 ;;; a pattern variable is a variable introduced by syntax-case and can
571 ;;; be referenced only within a syntax form.
573 ;;; any identifier for which no top-level syntax definition or local
574 ;;; binding of any kind has been seen is assumed to be a global
577 ;;; a lexical variable is a lambda- or letrec-bound variable.
579 ;;; a displaced-lexical identifier is a lexical identifier removed from
580 ;;; it's scope by the return of a syntax object containing the identifier.
581 ;;; a displaced lexical can also appear when a letrec-syntax-bound
582 ;;; keyword is referenced on the rhs of one of the letrec-syntax clauses.
583 ;;; a displaced lexical should never occur with properly written macros.
585 (define-syntax make-binding
586 (syntax-rules (quote)
587 ((_ type value) (cons type value))
589 ((_ type) (cons type '()))))
590 (define binding-type car)
591 (define binding-value cdr)
593 (define-syntax null-env (identifier-syntax '()))
596 (lambda (labels bindings r)
599 (extend-env (cdr labels) (cdr bindings)
600 (cons (cons (car labels) (car bindings)) r)))))
602 (define extend-var-env
603 ; variant of extend-env that forms "lexical" binding
604 (lambda (labels vars r)
607 (extend-var-env (cdr labels) (cdr vars)
608 (cons (cons (car labels) (make-binding 'lexical (car vars))) r)))))
610 ;;; we use a "macros only" environment in expansion of local macro
611 ;;; definitions so that their definitions can use local macros without
612 ;;; attempting to use other lexical identifiers.
613 (define macros-only-env
618 (if (eq? (cadr a) 'macro)
619 (cons a (macros-only-env (cdr r)))
620 (macros-only-env (cdr r)))))))
623 ; x may be a label or a symbol
624 ; although symbols are usually global, we check the environment first
625 ; anyway because a temporary binding may have been established by
631 (or (get-global-definition-hook x mod) (make-binding 'global)))
632 (else (make-binding 'displaced-lexical)))))
634 (define global-extend
635 (lambda (type sym val)
636 (put-global-definition-hook sym type val)))
639 ;;; Conceptually, identifiers are always syntax objects. Internally,
640 ;;; however, the wrap is sometimes maintained separately (a source of
641 ;;; efficiency and confusion), so that symbols are also considered
642 ;;; identifiers by id?. Externally, they are always wrapped.
644 (define nonsymbol-id?
646 (and (syntax-object? x)
647 (symbol? (unannotate (syntax-object-expression x))))))
653 ((syntax-object? x) (symbol? (unannotate (syntax-object-expression x))))
654 ((annotation? x) (symbol? (annotation-expression x)))
657 (define-syntax id-sym-name
661 (unannotate (if (syntax-object? x) (syntax-object-expression x) x))))))
663 (define id-sym-name&marks
665 (if (syntax-object? x)
667 (unannotate (syntax-object-expression x))
668 (join-marks (wrap-marks w) (wrap-marks (syntax-object-wrap x))))
669 (values (unannotate x) (wrap-marks w)))))
671 ;;; syntax object wraps
673 ;;; <wrap> ::= ((<mark> ...) . (<subst> ...))
674 ;;; <subst> ::= <shift> | <subs>
675 ;;; <subs> ::= #(<old name> <label> (<mark> ...))
676 ;;; <shift> ::= positive fixnum
678 (define make-wrap cons)
679 (define wrap-marks car)
680 (define wrap-subst cdr)
682 (define-syntax subst-rename? (identifier-syntax vector?))
683 (define-syntax rename-old (syntax-rules () ((_ x) (vector-ref x 0))))
684 (define-syntax rename-new (syntax-rules () ((_ x) (vector-ref x 1))))
685 (define-syntax rename-marks (syntax-rules () ((_ x) (vector-ref x 2))))
686 (define-syntax make-rename
688 ((_ old new marks) (vector old new marks))))
690 ;;; labels must be comparable with "eq?" and distinct from symbols.
692 (lambda () (string #\i)))
698 (cons (gen-label) (gen-labels (cdr ls))))))
700 (define-structure (ribcage symnames marks labels))
702 (define-syntax empty-wrap (identifier-syntax '(())))
704 (define-syntax top-wrap (identifier-syntax '((top))))
706 (define-syntax top-marked?
708 ((_ w) (memq 'top (wrap-marks w)))))
710 ;;; Marks must be comparable with "eq?" and distinct from pairs and
711 ;;; the symbol top. We do not use integers so that marks will remain
712 ;;; unique even across file compiles.
714 (define-syntax the-anti-mark (identifier-syntax #f))
718 (make-wrap (cons the-anti-mark (wrap-marks w))
719 (cons 'shift (wrap-subst w)))))
721 (define-syntax new-mark
725 ;;; make-empty-ribcage and extend-ribcage maintain list-based ribcages for
726 ;;; internal definitions, in which the ribcages are built incrementally
727 (define-syntax make-empty-ribcage
729 ((_) (make-ribcage '() '() '()))))
731 (define extend-ribcage!
732 ; must receive ids with complete wraps
733 (lambda (ribcage id label)
734 (set-ribcage-symnames! ribcage
735 (cons (unannotate (syntax-object-expression id))
736 (ribcage-symnames ribcage)))
737 (set-ribcage-marks! ribcage
738 (cons (wrap-marks (syntax-object-wrap id))
739 (ribcage-marks ribcage)))
740 (set-ribcage-labels! ribcage
741 (cons label (ribcage-labels ribcage)))))
743 ;;; make-binding-wrap creates vector-based ribcages
744 (define make-binding-wrap
745 (lambda (ids labels w)
751 (let ((labelvec (list->vector labels)))
752 (let ((n (vector-length labelvec)))
753 (let ((symnamevec (make-vector n)) (marksvec (make-vector n)))
754 (let f ((ids ids) (i 0))
755 (if (not (null? ids))
757 (lambda () (id-sym-name&marks (car ids) w))
758 (lambda (symname marks)
759 (vector-set! symnamevec i symname)
760 (vector-set! marksvec i marks)
761 (f (cdr ids) (fx+ i 1))))))
762 (make-ribcage symnamevec marksvec labelvec))))
773 (let ((m1 (wrap-marks w1)) (s1 (wrap-subst w1)))
779 (smart-append s1 (wrap-subst w2))))
781 (smart-append m1 (wrap-marks w2))
782 (smart-append s1 (wrap-subst w2)))))))
786 (smart-append m1 m2)))
793 (eq? (car x) (car y))
794 (same-marks? (cdr x) (cdr y))))))
800 ((_ e) (call-with-values (lambda () e) (lambda (x . ignore) x)))))
802 (lambda (sym subst marks)
805 (let ((fst (car subst)))
807 (search sym (cdr subst) (cdr marks))
808 (let ((symnames (ribcage-symnames fst)))
809 (if (vector? symnames)
810 (search-vector-rib sym subst marks symnames fst)
811 (search-list-rib sym subst marks symnames fst))))))))
812 (define search-list-rib
813 (lambda (sym subst marks symnames ribcage)
814 (let f ((symnames symnames) (i 0))
816 ((null? symnames) (search sym (cdr subst) marks))
817 ((and (eq? (car symnames) sym)
818 (same-marks? marks (list-ref (ribcage-marks ribcage) i)))
819 (values (list-ref (ribcage-labels ribcage) i) marks))
820 (else (f (cdr symnames) (fx+ i 1)))))))
821 (define search-vector-rib
822 (lambda (sym subst marks symnames ribcage)
823 (let ((n (vector-length symnames)))
826 ((fx= i n) (search sym (cdr subst) marks))
827 ((and (eq? (vector-ref symnames i) sym)
828 (same-marks? marks (vector-ref (ribcage-marks ribcage) i)))
829 (values (vector-ref (ribcage-labels ribcage) i) marks))
830 (else (f (fx+ i 1))))))))
833 (or (first (search id (wrap-subst w) (wrap-marks w))) id))
835 (let ((id (unannotate (syntax-object-expression id)))
836 (w1 (syntax-object-wrap id)))
837 (let ((marks (join-marks (wrap-marks w) (wrap-marks w1))))
838 (call-with-values (lambda () (search id (wrap-subst w) marks))
839 (lambda (new-id marks)
841 (first (search id (wrap-subst w1) marks))
844 (let ((id (unannotate id)))
845 (or (first (search id (wrap-subst w) (wrap-marks w))) id)))
846 (else (syntax-violation 'id-var-name "invalid id" id)))))
848 ;;; free-id=? must be passed fully wrapped ids since (free-id=? x y)
849 ;;; may be true even if (free-id=? (wrap x w) (wrap y w)) is not.
853 (and (eq? (id-sym-name i) (id-sym-name j)) ; accelerator
854 (eq? (id-var-name i empty-wrap) (id-var-name j empty-wrap)))))
856 ;;; bound-id=? may be passed unwrapped (or partially wrapped) ids as
857 ;;; long as the missing portion of the wrap is common to both of the ids
858 ;;; since (bound-id=? x y) iff (bound-id=? (wrap x w) (wrap y w))
862 (if (and (syntax-object? i) (syntax-object? j))
863 (and (eq? (unannotate (syntax-object-expression i))
864 (unannotate (syntax-object-expression j)))
865 (same-marks? (wrap-marks (syntax-object-wrap i))
866 (wrap-marks (syntax-object-wrap j))))
867 (eq? (unannotate i) (unannotate j)))))
869 ;;; "valid-bound-ids?" returns #t if it receives a list of distinct ids.
870 ;;; valid-bound-ids? may be passed unwrapped (or partially wrapped) ids
871 ;;; as long as the missing portion of the wrap is common to all of the
874 (define valid-bound-ids?
876 (and (let all-ids? ((ids ids))
879 (all-ids? (cdr ids)))))
880 (distinct-bound-ids? ids))))
882 ;;; distinct-bound-ids? expects a list of ids and returns #t if there are
883 ;;; no duplicates. It is quadratic on the length of the id list; long
884 ;;; lists could be sorted to make it more efficient. distinct-bound-ids?
885 ;;; may be passed unwrapped (or partially wrapped) ids as long as the
886 ;;; missing portion of the wrap is common to all of the ids.
888 (define distinct-bound-ids?
890 (let distinct? ((ids ids))
892 (and (not (bound-id-member? (car ids) (cdr ids)))
893 (distinct? (cdr ids)))))))
895 (define bound-id-member?
897 (and (not (null? list))
898 (or (bound-id=? x (car list))
899 (bound-id-member? x (cdr list))))))
901 ;;; wrapping expressions and identifiers
906 ((and (null? (wrap-marks w)) (null? (wrap-subst w))) x)
909 (syntax-object-expression x)
910 (join-wraps w (syntax-object-wrap x))
911 (syntax-object-module x)))
913 (else (make-syntax-object x w defmod)))))
916 (lambda (x w s defmod)
917 (wrap (if s (make-annotation x s #f) x) w defmod)))
922 (lambda (body r w s mod)
924 (let dobody ((body body) (r r) (w w) (mod mod))
927 (let ((first (chi (car body) r w mod)))
928 (cons first (dobody (cdr body) r w mod))))))))
930 (define chi-top-sequence
931 (lambda (body r w s m esew mod)
933 (let dobody ((body body) (r r) (w w) (m m) (esew esew) (mod mod))
936 (let ((first (chi-top (car body) r w m esew mod)))
937 (cons first (dobody (cdr body) r w m esew mod))))))))
939 (define chi-install-global
941 (build-global-definition
944 ;; FIXME: seems nasty to call current-module here
945 (if (let ((v (module-variable (current-module) name)))
946 ;; FIXME use primitive-macro?
947 (and v (variable-bound? v) (macro? (variable-ref v))
948 (not (eq? (macro-type (variable-ref v)) 'syncase-macro))))
951 (build-primref no-source 'make-extended-syncase-macro)
952 (list (build-application
954 (build-primref no-source 'module-ref)
955 (list (build-application no-source 'current-module '())
956 (build-data no-source name)))
957 (build-data no-source 'macro)
961 (build-primref no-source 'make-syncase-macro)
962 (list (build-data no-source 'macro) e))))))
964 (define chi-when-list
965 (lambda (e when-list w)
966 ; when-list is syntax'd version of list of situations
967 (let f ((when-list when-list) (situations '()))
968 (if (null? when-list)
971 (cons (let ((x (car when-list)))
973 ((free-id=? x (syntax compile)) 'compile)
974 ((free-id=? x (syntax load)) 'load)
975 ((free-id=? x (syntax eval)) 'eval)
976 (else (syntax-violation 'eval-when
981 ;;; syntax-type returns six values: type, value, e, w, s, and mod. The
982 ;;; first two are described in the table below.
984 ;;; type value explanation
985 ;;; -------------------------------------------------------------------
986 ;;; core procedure core form (including singleton)
987 ;;; external-macro procedure external macro
988 ;;; module-ref procedure @ or @@ form
989 ;;; lexical name lexical variable reference
990 ;;; global name global variable reference
991 ;;; begin none begin keyword
992 ;;; define none define keyword
993 ;;; define-syntax none define-syntax keyword
994 ;;; local-syntax rec? letrec-syntax/let-syntax keyword
995 ;;; eval-when none eval-when keyword
996 ;;; syntax level pattern variable
997 ;;; displaced-lexical none displaced lexical identifier
998 ;;; lexical-call name call to lexical variable
999 ;;; global-call name call to global variable
1000 ;;; call none any other call
1001 ;;; begin-form none begin expression
1002 ;;; define-form id variable definition
1003 ;;; define-syntax-form id syntax definition
1004 ;;; local-syntax-form rec? syntax definition
1005 ;;; eval-when-form none eval-when form
1006 ;;; constant none self-evaluating datum
1007 ;;; other none anything else
1009 ;;; For define-form and define-syntax-form, e is the rhs expression.
1010 ;;; For all others, e is the entire form. w is the wrap for e.
1011 ;;; s is the source for the entire form. mod is the module for e.
1013 ;;; syntax-type expands macros and unwraps as necessary to get to
1014 ;;; one of the forms above. It also parses define and define-syntax
1015 ;;; forms, although perhaps this should be done by the consumer.
1018 (lambda (e r w s rib mod)
1021 (let* ((n (id-var-name e w))
1022 (b (lookup n r mod))
1023 (type (binding-type b)))
1025 ((lexical) (values type (binding-value b) e w s mod))
1026 ((global) (values type n e w s mod))
1028 (syntax-type (chi-macro (binding-value b) e r w rib mod)
1029 r empty-wrap s rib mod))
1030 (else (values type (binding-value b) e w s mod)))))
1032 (let ((first (car e)))
1034 (let* ((n (id-var-name first w))
1035 (b (lookup n r (or (and (syntax-object? first)
1036 (syntax-object-module first))
1038 (type (binding-type b)))
1041 (values 'lexical-call (binding-value b) e w s mod))
1043 (values 'global-call n e w s mod))
1045 (syntax-type (chi-macro (binding-value b) e r w rib mod)
1046 r empty-wrap s rib mod))
1047 ((core external-macro module-ref)
1048 (values type (binding-value b) e w s mod))
1050 (values 'local-syntax-form (binding-value b) e w s mod))
1052 (values 'begin-form #f e w s mod))
1054 (values 'eval-when-form #f e w s mod))
1059 (values 'define-form (syntax name) (syntax val) w s mod))
1060 ((_ (name . args) e1 e2 ...)
1061 (and (id? (syntax name))
1062 (valid-bound-ids? (lambda-var-list (syntax args))))
1063 ; need lambda here...
1064 (values 'define-form (wrap (syntax name) w mod)
1065 (cons (syntax lambda) (wrap (syntax (args e1 e2 ...)) w mod))
1069 (values 'define-form (wrap (syntax name) w mod)
1071 empty-wrap s mod))))
1076 (values 'define-syntax-form (syntax name)
1077 (syntax val) w s mod))))
1079 (values 'call #f e w s mod))))
1080 (values 'call #f e w s mod))))
1082 ;; s can't be valid source if we've unwrapped
1083 (syntax-type (syntax-object-expression e)
1085 (join-wraps w (syntax-object-wrap e))
1086 no-source rib (or (syntax-object-module e) mod)))
1088 (syntax-type (annotation-expression e) r w (annotation-source e) rib mod))
1089 ((self-evaluating? e) (values 'constant #f e w s mod))
1090 (else (values 'other #f e w s mod)))))
1093 (lambda (e r w m esew mod)
1094 (define-syntax eval-if-c&e
1098 (if (eq? m 'c&e) (top-level-eval-hook x mod))
1101 (lambda () (syntax-type e r w no-source #f mod))
1102 (lambda (type value e w s mod)
1108 (chi-top-sequence (syntax (e1 e2 ...)) r w s m esew mod))))
1109 ((local-syntax-form)
1110 (chi-local-syntax value e r w s mod
1111 (lambda (body r w s mod)
1112 (chi-top-sequence body r w s m esew mod))))
1115 ((_ (x ...) e1 e2 ...)
1116 (let ((when-list (chi-when-list e (syntax (x ...)) w))
1117 (body (syntax (e1 e2 ...))))
1120 (if (memq 'eval when-list)
1121 (chi-top-sequence body r w s 'e '(eval) mod)
1123 ((memq 'load when-list)
1124 (if (or (memq 'compile when-list)
1125 (and (eq? m 'c&e) (memq 'eval when-list)))
1126 (chi-top-sequence body r w s 'c&e '(compile load) mod)
1127 (if (memq m '(c c&e))
1128 (chi-top-sequence body r w s 'c '(load) mod)
1130 ((or (memq 'compile when-list)
1131 (and (eq? m 'c&e) (memq 'eval when-list)))
1132 (top-level-eval-hook
1133 (chi-top-sequence body r w s 'e '(eval) mod)
1136 (else (chi-void)))))))
1137 ((define-syntax-form)
1138 (let ((n (id-var-name value w)) (r (macros-only-env r)))
1141 (if (memq 'compile esew)
1142 (let ((e (chi-install-global n (chi e r w mod))))
1143 (top-level-eval-hook e mod)
1144 (if (memq 'load esew) e (chi-void)))
1145 (if (memq 'load esew)
1146 (chi-install-global n (chi e r w mod))
1149 (let ((e (chi-install-global n (chi e r w mod))))
1150 (top-level-eval-hook e mod)
1153 (if (memq 'eval esew)
1154 (top-level-eval-hook
1155 (chi-install-global n (chi e r w mod))
1159 (let* ((n (id-var-name value w))
1160 (type (binding-type (lookup n r mod))))
1162 ((global core macro module-ref)
1164 (build-global-definition s n (chi e r w mod))
1166 ((displaced-lexical)
1167 (syntax-violation #f "identifier out of context"
1168 e (wrap value w mod)))
1170 (syntax-violation #f "cannot define keyword at top level"
1171 e (wrap value w mod))))))
1172 (else (eval-if-c&e m (chi-expr type value e r w s mod) mod)))))))
1177 (lambda () (syntax-type e r w no-source #f mod))
1178 (lambda (type value e w s mod)
1179 (chi-expr type value e r w s mod)))))
1182 (lambda (type value e r w s mod)
1185 (build-lexical-reference 'value s e value))
1186 ((core external-macro)
1187 ;; apply transformer
1188 (value e r w s mod))
1190 (call-with-values (lambda () (value e))
1191 ;; we could add a public? arg here
1192 (lambda (id mod) (build-global-reference s id mod))))
1195 (build-lexical-reference 'fun (source-annotation (car e))
1200 (build-global-reference (source-annotation (car e)) value
1201 (if (syntax-object? (car e))
1202 (syntax-object-module (car e))
1205 ((constant) (build-data s (strip (source-wrap e w s mod) empty-wrap)))
1206 ((global) (build-global-reference s value mod))
1207 ((call) (chi-application (chi (car e) r w mod) e r w s mod))
1210 ((_ e1 e2 ...) (chi-sequence (syntax (e1 e2 ...)) r w s mod))))
1211 ((local-syntax-form)
1212 (chi-local-syntax value e r w s mod chi-sequence))
1215 ((_ (x ...) e1 e2 ...)
1216 (let ((when-list (chi-when-list e (syntax (x ...)) w)))
1217 (if (memq 'eval when-list)
1218 (chi-sequence (syntax (e1 e2 ...)) r w s mod)
1220 ((define-form define-syntax-form)
1221 (syntax-violation #f "definition in expression context"
1222 e (wrap value w mod)))
1224 (syntax-violation #f "reference to pattern variable outside syntax form"
1225 (source-wrap e w s mod)))
1226 ((displaced-lexical)
1227 (syntax-violation #f "reference to identifier outside its scope"
1228 (source-wrap e w s mod)))
1229 (else (syntax-violation #f "unexpected syntax"
1230 (source-wrap e w s mod))))))
1232 (define chi-application
1233 (lambda (x e r w s mod)
1236 (build-application s x
1237 (map (lambda (e) (chi e r w mod)) (syntax (e1 ...))))))))
1240 (lambda (p e r w rib mod)
1241 (define rebuild-macro-output
1244 (cons (rebuild-macro-output (car x) m)
1245 (rebuild-macro-output (cdr x) m)))
1247 (let ((w (syntax-object-wrap x)))
1248 (let ((ms (wrap-marks w)) (s (wrap-subst w)))
1249 (if (and (pair? ms) (eq? (car ms) the-anti-mark))
1250 ;; output is from original text
1252 (syntax-object-expression x)
1253 (make-wrap (cdr ms) (if rib (cons rib (cdr s)) (cdr s)))
1254 (syntax-object-module x))
1255 ;; output introduced by macro
1257 (syntax-object-expression x)
1258 (make-wrap (cons m ms)
1260 (cons rib (cons 'shift s))
1262 (let ((pmod (procedure-module p)))
1264 ;; hither the hygiene
1265 (cons 'hygiene (module-name pmod))
1266 ;; but it's possible for the proc to have
1267 ;; no mod, if it was made before modules
1269 '(hygiene guile))))))))
1271 (let* ((n (vector-length x)) (v (make-vector n)))
1272 (do ((i 0 (fx+ i 1)))
1275 (rebuild-macro-output (vector-ref x i) m)))))
1277 (syntax-violation #f "encountered raw symbol in macro output"
1278 (source-wrap e w s mod) x))
1280 (rebuild-macro-output (p (wrap e (anti-mark w) mod)) (new-mark))))
1283 ;; In processing the forms of the body, we create a new, empty wrap.
1284 ;; This wrap is augmented (destructively) each time we discover that
1285 ;; the next form is a definition. This is done:
1287 ;; (1) to allow the first nondefinition form to be a call to
1288 ;; one of the defined ids even if the id previously denoted a
1289 ;; definition keyword or keyword for a macro expanding into a
1291 ;; (2) to prevent subsequent definition forms (but unfortunately
1292 ;; not earlier ones) and the first nondefinition form from
1293 ;; confusing one of the bound identifiers for an auxiliary
1295 ;; (3) so that we do not need to restart the expansion of the
1296 ;; first nondefinition form, which is problematic anyway
1297 ;; since it might be the first element of a begin that we
1298 ;; have just spliced into the body (meaning if we restarted,
1299 ;; we'd really need to restart with the begin or the macro
1300 ;; call that expanded into the begin, and we'd have to give
1301 ;; up allowing (begin <defn>+ <expr>+), which is itself
1302 ;; problematic since we don't know if a begin contains only
1303 ;; definitions until we've expanded it).
1305 ;; Before processing the body, we also create a new environment
1306 ;; containing a placeholder for the bindings we will add later and
1307 ;; associate this environment with each form. In processing a
1308 ;; let-syntax or letrec-syntax, the associated environment may be
1309 ;; augmented with local keyword bindings, so the environment may
1310 ;; be different for different forms in the body. Once we have
1311 ;; gathered up all of the definitions, we evaluate the transformer
1312 ;; expressions and splice into r at the placeholder the new variable
1313 ;; and keyword bindings. This allows let-syntax or letrec-syntax
1314 ;; forms local to a portion or all of the body to shadow the
1315 ;; definition bindings.
1317 ;; Subforms of a begin, let-syntax, or letrec-syntax are spliced
1320 ;; outer-form is fully wrapped w/source
1321 (lambda (body outer-form r w mod)
1322 (let* ((r (cons '("placeholder" . (placeholder)) r))
1323 (ribcage (make-empty-ribcage))
1324 (w (make-wrap (wrap-marks w) (cons ribcage (wrap-subst w)))))
1325 (let parse ((body (map (lambda (x) (cons r (wrap x w mod))) body))
1326 (ids '()) (labels '()) (vars '()) (vals '()) (bindings '()))
1328 (syntax-violation #f "no expressions in body" outer-form)
1329 (let ((e (cdar body)) (er (caar body)))
1331 (lambda () (syntax-type e er empty-wrap no-source ribcage mod))
1332 (lambda (type value e w s mod)
1335 (let ((id (wrap value w mod)) (label (gen-label)))
1336 (let ((var (gen-var id)))
1337 (extend-ribcage! ribcage id label)
1339 (cons id ids) (cons label labels)
1340 (cons var vars) (cons (cons er (wrap e w mod)) vals)
1341 (cons (make-binding 'lexical var) bindings)))))
1342 ((define-syntax-form)
1343 (let ((id (wrap value w mod)) (label (gen-label)))
1344 (extend-ribcage! ribcage id label)
1346 (cons id ids) (cons label labels)
1348 (cons (make-binding 'macro (cons er (wrap e w mod)))
1353 (parse (let f ((forms (syntax (e1 ...))))
1356 (cons (cons er (wrap (car forms) w mod))
1358 ids labels vars vals bindings))))
1359 ((local-syntax-form)
1360 (chi-local-syntax value e er w s mod
1361 (lambda (forms er w s mod)
1362 (parse (let f ((forms forms))
1365 (cons (cons er (wrap (car forms) w mod))
1367 ids labels vars vals bindings))))
1368 (else ; found a non-definition
1370 (build-sequence no-source
1372 (chi (cdr x) (car x) empty-wrap mod))
1373 (cons (cons er (source-wrap e w s mod))
1376 (if (not (valid-bound-ids? ids))
1378 #f "invalid or duplicate identifier in definition"
1380 (let loop ((bs bindings) (er-cache #f) (r-cache #f))
1381 (if (not (null? bs))
1382 (let* ((b (car bs)))
1383 (if (eq? (car b) 'macro)
1384 (let* ((er (cadr b))
1386 (if (eq? er er-cache)
1388 (macros-only-env er))))
1390 (eval-local-transformer
1391 (chi (cddr b) r-cache empty-wrap mod)
1393 (loop (cdr bs) er r-cache))
1394 (loop (cdr bs) er-cache r-cache)))))
1395 (set-cdr! r (extend-env labels bindings (cdr r)))
1396 (build-letrec no-source
1397 (map syntax->datum ids)
1400 (chi (cdr x) (car x) empty-wrap mod))
1402 (build-sequence no-source
1404 (chi (cdr x) (car x) empty-wrap mod))
1405 (cons (cons er (source-wrap e w s mod))
1406 (cdr body)))))))))))))))))
1408 (define chi-lambda-clause
1409 (lambda (e docstring c r w mod k)
1411 ((args doc e1 e2 ...)
1412 (and (string? (syntax->datum (syntax doc))) (not docstring))
1413 (chi-lambda-clause e (syntax doc) (syntax (args e1 e2 ...)) r w mod k))
1414 (((id ...) e1 e2 ...)
1415 (let ((ids (syntax (id ...))))
1416 (if (not (valid-bound-ids? ids))
1417 (syntax-violation 'lambda "invalid parameter list" e)
1418 (let ((labels (gen-labels ids))
1419 (new-vars (map gen-var ids)))
1420 (k (map syntax->datum ids)
1423 (chi-body (syntax (e1 e2 ...))
1425 (extend-var-env labels new-vars r)
1426 (make-binding-wrap ids labels w)
1429 (let ((old-ids (lambda-var-list (syntax ids))))
1430 (if (not (valid-bound-ids? old-ids))
1431 (syntax-violation 'lambda "invalid parameter list" e)
1432 (let ((labels (gen-labels old-ids))
1433 (new-vars (map gen-var old-ids)))
1434 (k (let f ((ls1 (cdr old-ids)) (ls2 (car old-ids)))
1437 (f (cdr ls1) (cons (syntax->datum (car ls1)) ls2))))
1438 (let f ((ls1 (cdr new-vars)) (ls2 (car new-vars)))
1441 (f (cdr ls1) (cons (car ls1) ls2))))
1443 (chi-body (syntax (e1 e2 ...))
1445 (extend-var-env labels new-vars r)
1446 (make-binding-wrap old-ids labels w)
1448 (_ (syntax-violation 'lambda "bad lambda" e)))))
1450 (define chi-local-syntax
1451 (lambda (rec? e r w s mod k)
1453 ((_ ((id val) ...) e1 e2 ...)
1454 (let ((ids (syntax (id ...))))
1455 (if (not (valid-bound-ids? ids))
1456 (syntax-violation #f "duplicate bound keyword" e)
1457 (let ((labels (gen-labels ids)))
1458 (let ((new-w (make-binding-wrap ids labels w)))
1459 (k (syntax (e1 e2 ...))
1462 (let ((w (if rec? new-w w))
1463 (trans-r (macros-only-env r)))
1465 (make-binding 'macro
1466 (eval-local-transformer
1467 (chi x trans-r w mod)
1469 (syntax (val ...))))
1474 (_ (syntax-violation #f "bad local syntax definition"
1475 (source-wrap e w s mod))))))
1477 (define eval-local-transformer
1478 (lambda (expanded mod)
1479 (let ((p (local-eval-hook expanded mod)))
1482 (syntax-violation #f "nonprocedure transformer" p)))))
1486 (build-application no-source (build-primref no-source 'if) '(#f #f))))
1490 (and (nonsymbol-id? x)
1491 (free-id=? x (syntax (... ...))))))
1495 ;;; strips all annotations from potentially circular reader output
1497 (define strip-annotation
1501 (let ((new (cons #f #f)))
1502 (if parent (set-annotation-stripped! parent new))
1503 (set-car! new (strip-annotation (car x) #f))
1504 (set-cdr! new (strip-annotation (cdr x) #f))
1507 (or (annotation-stripped x)
1508 (strip-annotation (annotation-expression x) x)))
1510 (let ((new (make-vector (vector-length x))))
1511 (if parent (set-annotation-stripped! parent new))
1512 (let loop ((i (- (vector-length x) 1)))
1514 (vector-set! new i (strip-annotation (vector-ref x i) #f))
1519 ;;; strips syntax-objects down to top-wrap; if top-wrap is layered directly
1520 ;;; on an annotation, strips the annotation as well.
1521 ;;; since only the head of a list is annotated by the reader, not each pair
1522 ;;; in the spine, we also check for pairs whose cars are annotated in case
1523 ;;; we've been passed the cdr of an annotated list
1528 (if (or (annotation? x) (and (pair? x) (annotation? (car x))))
1529 (strip-annotation x #f)
1534 (strip (syntax-object-expression x) (syntax-object-wrap x)))
1536 (let ((a (f (car x))) (d (f (cdr x))))
1537 (if (and (eq? a (car x)) (eq? d (cdr x)))
1541 (let ((old (vector->list x)))
1542 (let ((new (map f old)))
1543 (if (and-map* eq? old new) x (list->vector new)))))
1546 ;;; lexical variables
1550 (let ((id (if (syntax-object? id) (syntax-object-expression id) id)))
1551 (if (annotation? id)
1552 (build-lexical-var (annotation-source id) (annotation-expression id))
1553 (build-lexical-var no-source id)))))
1555 (define lambda-var-list
1557 (let lvl ((vars vars) (ls '()) (w empty-wrap))
1559 ((pair? vars) (lvl (cdr vars) (cons (wrap (car vars) w #f) ls) w))
1560 ((id? vars) (cons (wrap vars w #f) ls))
1562 ((syntax-object? vars)
1563 (lvl (syntax-object-expression vars)
1565 (join-wraps w (syntax-object-wrap vars))))
1567 (lvl (annotation-expression vars) ls w))
1568 ; include anything else to be caught by subsequent error
1570 (else (cons vars ls))))))
1572 ;;; core transformers
1574 (global-extend 'local-syntax 'letrec-syntax #t)
1575 (global-extend 'local-syntax 'let-syntax #f)
1577 (global-extend 'core 'fluid-let-syntax
1578 (lambda (e r w s mod)
1580 ((_ ((var val) ...) e1 e2 ...)
1581 (valid-bound-ids? (syntax (var ...)))
1582 (let ((names (map (lambda (x) (id-var-name x w)) (syntax (var ...)))))
1585 (case (binding-type (lookup n r mod))
1586 ((displaced-lexical)
1587 (syntax-violation 'fluid-let-syntax
1588 "identifier out of context"
1590 (source-wrap id w s mod)))))
1594 (syntax (e1 e2 ...))
1595 (source-wrap e w s mod)
1598 (let ((trans-r (macros-only-env r)))
1600 (make-binding 'macro
1601 (eval-local-transformer (chi x trans-r w mod)
1603 (syntax (val ...))))
1607 (_ (syntax-violation 'fluid-let-syntax "bad syntax"
1608 (source-wrap e w s mod))))))
1610 (global-extend 'core 'quote
1611 (lambda (e r w s mod)
1613 ((_ e) (build-data s (strip (syntax e) w)))
1614 (_ (syntax-violation 'quote "bad syntax"
1615 (source-wrap e w s mod))))))
1617 (global-extend 'core 'syntax
1620 (lambda (src e r maps ellipsis? mod)
1622 (let ((label (id-var-name e empty-wrap)))
1623 (let ((b (lookup label r mod)))
1624 (if (eq? (binding-type b) 'syntax)
1627 (let ((var.lev (binding-value b)))
1628 (gen-ref src (car var.lev) (cdr var.lev) maps)))
1629 (lambda (var maps) (values `(ref ,var) maps)))
1631 (syntax-violation 'syntax "misplaced ellipsis" src)
1632 (values `(quote ,e) maps)))))
1635 (ellipsis? (syntax dots))
1636 (gen-syntax src (syntax e) r maps (lambda (x) #f) mod))
1638 ; this could be about a dozen lines of code, except that we
1639 ; choose to handle (syntax (x ... ...)) forms
1640 (ellipsis? (syntax dots))
1641 (let f ((y (syntax y))
1645 (gen-syntax src (syntax x) r
1646 (cons '() maps) ellipsis? mod))
1648 (if (null? (car maps))
1649 (syntax-violation 'syntax "extra ellipsis"
1651 (values (gen-map x (car maps))
1655 (ellipsis? (syntax dots))
1659 (lambda () (k (cons '() maps)))
1661 (if (null? (car maps))
1662 (syntax-violation 'syntax "extra ellipsis" src)
1663 (values (gen-mappend x (car maps))
1665 (_ (call-with-values
1666 (lambda () (gen-syntax src y r maps ellipsis? mod))
1669 (lambda () (k maps))
1671 (values (gen-append x y) maps)))))))))
1674 (lambda () (gen-syntax src (syntax x) r maps ellipsis? mod))
1677 (lambda () (gen-syntax src (syntax y) r maps ellipsis? mod))
1678 (lambda (y maps) (values (gen-cons x y) maps))))))
1682 (gen-syntax src (syntax (e1 e2 ...)) r maps ellipsis? mod))
1683 (lambda (e maps) (values (gen-vector e) maps))))
1684 (_ (values `(quote ,e) maps))))))
1687 (lambda (src var level maps)
1691 (syntax-violation 'syntax "missing ellipsis" src)
1693 (lambda () (gen-ref src var (fx- level 1) (cdr maps)))
1694 (lambda (outer-var outer-maps)
1695 (let ((b (assq outer-var (car maps))))
1697 (values (cdr b) maps)
1698 (let ((inner-var (gen-var 'tmp)))
1700 (cons (cons (cons outer-var inner-var)
1702 outer-maps)))))))))))
1706 `(apply (primitive append) ,(gen-map e map-env))))
1710 (let ((formals (map cdr map-env))
1711 (actuals (map (lambda (x) `(ref ,(car x))) map-env)))
1714 ; identity map equivalence:
1715 ; (map (lambda (x) x) y) == y
1718 (lambda (x) (and (eq? (car x) 'ref) (memq (cadr x) formals)))
1720 ; eta map equivalence:
1721 ; (map (lambda (x ...) (f x ...)) y ...) == (map f y ...)
1722 `(map (primitive ,(car e))
1723 ,@(map (let ((r (map cons formals actuals)))
1724 (lambda (x) (cdr (assq (cadr x) r))))
1726 (else `(map (lambda ,formals ,e) ,@actuals))))))
1732 (if (eq? (car x) 'quote)
1733 `(quote (,(cadr x) . ,(cadr y)))
1734 (if (eq? (cadr y) '())
1737 ((list) `(list ,x ,@(cdr y)))
1738 (else `(cons ,x ,y)))))
1742 (if (equal? y '(quote ()))
1749 ((eq? (car x) 'list) `(vector ,@(cdr x)))
1750 ((eq? (car x) 'quote) `(quote #(,@(cadr x))))
1751 (else `(list->vector ,x)))))
1757 ((ref) (build-lexical-reference 'value no-source (cadr x) (cadr x)))
1758 ((primitive) (build-primref no-source (cadr x)))
1759 ((quote) (build-data no-source (cadr x)))
1760 ((lambda) (build-lambda no-source (cadr x) (cadr x) #f (regen (caddr x))))
1761 ((map) (let ((ls (map regen (cdr x))))
1762 (build-application no-source
1763 ;; this check used to be here, not sure what for:
1764 ;; (if (fx= (length ls) 2)
1765 (build-primref no-source 'map)
1767 (else (build-application no-source
1768 (build-primref no-source (car x))
1769 (map regen (cdr x)))))))
1771 (lambda (e r w s mod)
1772 (let ((e (source-wrap e w s mod)))
1776 (lambda () (gen-syntax e (syntax x) r '() ellipsis? mod))
1777 (lambda (e maps) (regen e))))
1778 (_ (syntax-violation 'syntax "bad `syntax' form" e)))))))
1781 (global-extend 'core 'lambda
1782 (lambda (e r w s mod)
1785 (chi-lambda-clause (source-wrap e w s mod) #f (syntax c) r w mod
1786 (lambda (names vars docstring body)
1787 (build-lambda s names vars docstring body)))))))
1790 (global-extend 'core 'let
1792 (define (chi-let e r w s mod constructor ids vals exps)
1793 (if (not (valid-bound-ids? ids))
1794 (syntax-violation 'let "duplicate bound variable" e)
1795 (let ((labels (gen-labels ids))
1796 (new-vars (map gen-var ids)))
1797 (let ((nw (make-binding-wrap ids labels w))
1798 (nr (extend-var-env labels new-vars r)))
1800 (map syntax->datum ids)
1802 (map (lambda (x) (chi x r w mod)) vals)
1803 (chi-body exps (source-wrap e nw s mod)
1805 (lambda (e r w s mod)
1807 ((_ ((id val) ...) e1 e2 ...)
1808 (chi-let e r w s mod
1812 (syntax (e1 e2 ...))))
1813 ((_ f ((id val) ...) e1 e2 ...)
1815 (chi-let e r w s mod
1819 (syntax (e1 e2 ...))))
1820 (_ (syntax-violation 'let "bad let" (source-wrap e w s mod)))))))
1823 (global-extend 'core 'letrec
1824 (lambda (e r w s mod)
1826 ((_ ((id val) ...) e1 e2 ...)
1827 (let ((ids (syntax (id ...))))
1828 (if (not (valid-bound-ids? ids))
1829 (syntax-violation 'letrec "duplicate bound variable" e)
1830 (let ((labels (gen-labels ids))
1831 (new-vars (map gen-var ids)))
1832 (let ((w (make-binding-wrap ids labels w))
1833 (r (extend-var-env labels new-vars r)))
1835 (map syntax->datum ids)
1837 (map (lambda (x) (chi x r w mod)) (syntax (val ...)))
1838 (chi-body (syntax (e1 e2 ...))
1839 (source-wrap e w s mod) r w mod)))))))
1840 (_ (syntax-violation 'letrec "bad letrec" (source-wrap e w s mod))))))
1843 (global-extend 'core 'set!
1844 (lambda (e r w s mod)
1848 (let ((val (chi (syntax val) r w mod))
1849 (n (id-var-name (syntax id) w)))
1850 (let ((b (lookup n r mod)))
1851 (case (binding-type b)
1853 (build-lexical-assignment s
1854 (syntax->datum (syntax id))
1857 ((global) (build-global-assignment s n val mod))
1858 ((displaced-lexical)
1859 (syntax-violation 'set! "identifier out of context"
1860 (wrap (syntax id) w mod)))
1861 (else (syntax-violation 'set! "bad set!"
1862 (source-wrap e w s mod)))))))
1863 ((_ (head tail ...) val)
1865 (lambda () (syntax-type (syntax head) r empty-wrap no-source #f mod))
1866 (lambda (type value ee ww ss modmod)
1869 (let ((val (chi (syntax val) r w mod)))
1870 (call-with-values (lambda () (value (syntax (head tail ...))))
1872 (build-global-assignment s id val mod)))))
1874 (build-application s
1875 (chi (syntax (setter head)) r w mod)
1876 (map (lambda (e) (chi e r w mod))
1877 (syntax (tail ... val)))))))))
1878 (_ (syntax-violation 'set! "bad set!" (source-wrap e w s mod))))))
1880 (global-extend 'module-ref '@
1884 (and (and-map id? (syntax (mod ...))) (id? (syntax id)))
1885 (values (syntax->datum (syntax id))
1887 (syntax (public mod ...))))))))
1889 (global-extend 'module-ref '@@
1893 (and (and-map id? (syntax (mod ...))) (id? (syntax id)))
1894 (values (syntax->datum (syntax id))
1896 (syntax (private mod ...))))))))
1898 (global-extend 'begin 'begin '())
1900 (global-extend 'define 'define '())
1902 (global-extend 'define-syntax 'define-syntax '())
1904 (global-extend 'eval-when 'eval-when '())
1906 (global-extend 'core 'syntax-case
1908 (define convert-pattern
1909 ; accepts pattern & keys
1910 ; returns $sc-dispatch pattern & ids
1911 (lambda (pattern keys)
1912 (let cvt ((p pattern) (n 0) (ids '()))
1914 (if (bound-id-member? p keys)
1915 (values (vector 'free-id p) ids)
1916 (values 'any (cons (cons p n) ids)))
1919 (ellipsis? (syntax dots))
1921 (lambda () (cvt (syntax x) (fx+ n 1) ids))
1923 (values (if (eq? p 'any) 'each-any (vector 'each p))
1927 (lambda () (cvt (syntax y) n ids))
1930 (lambda () (cvt (syntax x) n ids))
1932 (values (cons x y) ids))))))
1933 (() (values '() ids))
1936 (lambda () (cvt (syntax (x ...)) n ids))
1937 (lambda (p ids) (values (vector 'vector p) ids))))
1938 (x (values (vector 'atom (strip p empty-wrap)) ids)))))))
1940 (define build-dispatch-call
1941 (lambda (pvars exp y r mod)
1942 (let ((ids (map car pvars)) (levels (map cdr pvars)))
1943 (let ((labels (gen-labels ids)) (new-vars (map gen-var ids)))
1944 (build-application no-source
1945 (build-primref no-source 'apply)
1946 (list (build-lambda no-source (map syntax->datum ids) new-vars #f
1950 (map (lambda (var level)
1951 (make-binding 'syntax `(,var . ,level)))
1955 (make-binding-wrap ids labels empty-wrap)
1960 (lambda (x keys clauses r pat fender exp mod)
1962 (lambda () (convert-pattern pat keys))
1965 ((not (distinct-bound-ids? (map car pvars)))
1966 (syntax-violation 'syntax-case "duplicate pattern variable" pat))
1967 ((not (and-map (lambda (x) (not (ellipsis? (car x)))) pvars))
1968 (syntax-violation 'syntax-case "misplaced ellipsis" pat))
1970 (let ((y (gen-var 'tmp)))
1971 ; fat finger binding and references to temp variable y
1972 (build-application no-source
1973 (build-lambda no-source (list 'tmp) (list y) #f
1974 (let ((y (build-lexical-reference 'value no-source
1976 (build-conditional no-source
1977 (syntax-case fender ()
1979 (_ (build-conditional no-source
1981 (build-dispatch-call pvars fender y r mod)
1982 (build-data no-source #f))))
1983 (build-dispatch-call pvars exp y r mod)
1984 (gen-syntax-case x keys clauses r mod))))
1985 (list (if (eq? p 'any)
1986 (build-application no-source
1987 (build-primref no-source 'list)
1989 (build-application no-source
1990 (build-primref no-source '$sc-dispatch)
1991 (list x (build-data no-source p)))))))))))))
1993 (define gen-syntax-case
1994 (lambda (x keys clauses r mod)
1996 (build-application no-source
1997 (build-primref no-source 'syntax-violation)
1998 (list #f "source expression failed to match any pattern" x))
1999 (syntax-case (car clauses) ()
2001 (if (and (id? (syntax pat))
2002 (and-map (lambda (x) (not (free-id=? (syntax pat) x)))
2003 (cons (syntax (... ...)) keys)))
2004 (let ((labels (list (gen-label)))
2005 (var (gen-var (syntax pat))))
2006 (build-application no-source
2007 (build-lambda no-source
2008 (list (syntax->datum (syntax pat))) (list var)
2012 (list (make-binding 'syntax `(,var . 0)))
2014 (make-binding-wrap (syntax (pat))
2018 (gen-clause x keys (cdr clauses) r
2019 (syntax pat) #t (syntax exp) mod)))
2021 (gen-clause x keys (cdr clauses) r
2022 (syntax pat) (syntax fender) (syntax exp) mod))
2023 (_ (syntax-violation 'syntax-case "invalid clause"
2026 (lambda (e r w s mod)
2027 (let ((e (source-wrap e w s mod)))
2029 ((_ val (key ...) m ...)
2030 (if (and-map (lambda (x) (and (id? x) (not (ellipsis? x))))
2032 (let ((x (gen-var 'tmp)))
2033 ; fat finger binding and references to temp variable x
2034 (build-application s
2035 (build-lambda no-source (list 'tmp) (list x) #f
2036 (gen-syntax-case (build-lexical-reference 'value no-source
2038 (syntax (key ...)) (syntax (m ...))
2041 (list (chi (syntax val) r empty-wrap mod))))
2042 (syntax-violation 'syntax-case "invalid literals list" e))))))))
2044 ;;; The portable sc-expand seeds chi-top's mode m with 'e (for
2045 ;;; evaluating) and esew (which stands for "eval syntax expanders
2046 ;;; when") with '(eval). In Chez Scheme, m is set to 'c instead of e
2047 ;;; if we are compiling a file, and esew is set to
2048 ;;; (eval-syntactic-expanders-when), which defaults to the list
2049 ;;; '(compile load eval). This means that, by default, top-level
2050 ;;; syntactic definitions are evaluated immediately after they are
2051 ;;; expanded, and the expanded definitions are also residualized into
2052 ;;; the object file if we are compiling a file.
2055 (if (and (pair? x) (equal? (car x) noexpand))
2057 (let ((m (if (null? rest) 'e (car rest)))
2058 (esew (if (or (null? rest) (null? (cdr rest)))
2061 (with-fluid* *mode* m
2063 (chi-top x null-env top-wrap m esew
2064 (cons 'hygiene (module-name (current-module))))))))))
2072 (make-syntax-object datum (syntax-object-wrap id) #f)))
2075 ; accepts any object, since syntax objects may consist partially
2076 ; or entirely of unwrapped, nonsymbolic data
2078 (strip x empty-wrap)))
2080 (set! generate-temporaries
2082 (arg-check list? ls 'generate-temporaries)
2083 (map (lambda (x) (wrap (gensym-hook) top-wrap #f)) ls)))
2085 (set! free-identifier=?
2087 (arg-check nonsymbol-id? x 'free-identifier=?)
2088 (arg-check nonsymbol-id? y 'free-identifier=?)
2091 (set! bound-identifier=?
2093 (arg-check nonsymbol-id? x 'bound-identifier=?)
2094 (arg-check nonsymbol-id? y 'bound-identifier=?)
2097 (set! syntax-violation
2098 (lambda (who message form . subform)
2099 (arg-check (lambda (x) (or (not x) (string? x) (symbol? x)))
2100 who 'syntax-violation)
2101 (arg-check string? message 'syntax-violation)
2102 (scm-error 'syntax-error 'sc-expand
2106 (if (null? subform) "in ~a" "in subform `~s' of `~s'"))
2107 (let ((tail (cons message
2108 (map (lambda (x) (strip x empty-wrap))
2109 (append subform (list form))))))
2110 (if who (cons who tail) tail))
2113 ;;; $sc-dispatch expects an expression and a pattern. If the expression
2114 ;;; matches the pattern a list of the matching expressions for each
2115 ;;; "any" is returned. Otherwise, #f is returned. (This use of #f will
2116 ;;; not work on r4rs implementations that violate the ieee requirement
2117 ;;; that #f and () be distinct.)
2119 ;;; The expression is matched with the pattern as follows:
2121 ;;; pattern: matches:
2124 ;;; (<pattern>1 . <pattern>2) (<pattern>1 . <pattern>2)
2126 ;;; #(free-id <key>) <key> with free-identifier=?
2127 ;;; #(each <pattern>) (<pattern>*)
2128 ;;; #(vector <pattern>) (list->vector <pattern>)
2129 ;;; #(atom <object>) <object> with "equal?"
2131 ;;; Vector cops out to pair under assumption that vectors are rare. If
2132 ;;; not, should convert to:
2133 ;;; #(vector <pattern>*) #(<pattern>*)
2141 (match-each (annotation-expression e) p w mod))
2143 (let ((first (match (car e) p w '() mod)))
2145 (let ((rest (match-each (cdr e) p w mod)))
2146 (and rest (cons first rest))))))
2149 (match-each (syntax-object-expression e)
2151 (join-wraps w (syntax-object-wrap e))
2152 (syntax-object-module e)))
2155 (define match-each-any
2159 (match-each-any (annotation-expression e) w mod))
2161 (let ((l (match-each-any (cdr e) w mod)))
2162 (and l (cons (wrap (car e) w mod) l))))
2165 (match-each-any (syntax-object-expression e)
2166 (join-wraps w (syntax-object-wrap e))
2174 ((eq? p 'any) (cons '() r))
2175 ((pair? p) (match-empty (car p) (match-empty (cdr p) r)))
2176 ((eq? p 'each-any) (cons '() r))
2178 (case (vector-ref p 0)
2179 ((each) (match-empty (vector-ref p 1) r))
2181 ((vector) (match-empty (vector-ref p 1) r)))))))
2184 (lambda (e p w r mod)
2186 ((null? p) (and (null? e) r))
2188 (and (pair? e) (match (car e) (car p) w
2189 (match (cdr e) (cdr p) w r mod)
2192 (let ((l (match-each-any e w mod))) (and l (cons l r))))
2194 (case (vector-ref p 0)
2197 (match-empty (vector-ref p 1) r)
2198 (let ((l (match-each e (vector-ref p 1) w mod)))
2200 (let collect ((l l))
2203 (cons (map car l) (collect (map cdr l)))))))))
2204 ((free-id) (and (id? e) (free-id=? (wrap e w mod) (vector-ref p 1)) r))
2205 ((atom) (and (equal? (vector-ref p 1) (strip e w)) r))
2208 (match (vector->list e) (vector-ref p 1) w r mod))))))))
2211 (lambda (e p w r mod)
2214 ((eq? p 'any) (cons (wrap e w mod) r))
2217 (unannotate (syntax-object-expression e))
2219 (join-wraps w (syntax-object-wrap e))
2221 (syntax-object-module e)))
2222 (else (match* (unannotate e) p w r mod)))))
2227 ((eq? p 'any) (list e))
2229 (match* (unannotate (syntax-object-expression e))
2230 p (syntax-object-wrap e) '() (syntax-object-module e)))
2231 (else (match* (unannotate e) p empty-wrap '() #f)))))
2236 (define-syntax with-syntax
2240 (syntax (begin e1 e2 ...)))
2241 ((_ ((out in)) e1 e2 ...)
2242 (syntax (syntax-case in () (out (begin e1 e2 ...)))))
2243 ((_ ((out in) ...) e1 e2 ...)
2244 (syntax (syntax-case (list in ...) ()
2245 ((out ...) (begin e1 e2 ...))))))))
2247 (define-syntax syntax-rules
2250 ((_ (k ...) ((keyword . pattern) template) ...)
2252 (syntax-case x (k ...)
2253 ((dummy . pattern) (syntax template))
2259 ((let* ((x v) ...) e1 e2 ...)
2260 (and-map identifier? (syntax (x ...)))
2261 (let f ((bindings (syntax ((x v) ...))))
2262 (if (null? bindings)
2263 (syntax (let () e1 e2 ...))
2264 (with-syntax ((body (f (cdr bindings)))
2265 (binding (car bindings)))
2266 (syntax (let (binding) body)))))))))
2270 (syntax-case orig-x ()
2271 ((_ ((var init . step) ...) (e0 e1 ...) c ...)
2272 (with-syntax (((step ...)
2277 (_ (syntax-violation
2278 'do "bad step expression"
2281 (syntax (step ...)))))
2282 (syntax-case (syntax (e1 ...)) ()
2283 (() (syntax (let doloop ((var init) ...)
2285 (begin c ... (doloop step ...))))))
2287 (syntax (let doloop ((var init) ...)
2290 (begin c ... (doloop step ...))))))))))))
2292 (define-syntax quasiquote
2296 (with-syntax ((x x) (y y))
2297 (syntax-case (syntax y) (quote list)
2299 (syntax-case (syntax x) (quote)
2300 ((quote dx) (syntax (quote (dx . dy))))
2301 (_ (if (null? (syntax dy))
2303 (syntax (cons x y))))))
2304 ((list . stuff) (syntax (list x . stuff)))
2305 (else (syntax (cons x y)))))))
2308 (with-syntax ((x x) (y y))
2309 (syntax-case (syntax y) (quote)
2310 ((quote ()) (syntax x))
2311 (_ (syntax (append x y)))))))
2314 (with-syntax ((x x))
2315 (syntax-case (syntax x) (quote list)
2316 ((quote (x ...)) (syntax (quote #(x ...))))
2317 ((list x ...) (syntax (vector x ...)))
2318 (_ (syntax (list->vector x)))))))
2321 (syntax-case p (unquote unquote-splicing quasiquote)
2325 (quasicons (syntax (quote unquote))
2326 (quasi (syntax (p)) (- lev 1)))))
2327 (((unquote-splicing p) . q)
2329 (quasiappend (syntax p) (quasi (syntax q) lev))
2330 (quasicons (quasicons (syntax (quote unquote-splicing))
2331 (quasi (syntax (p)) (- lev 1)))
2332 (quasi (syntax q) lev))))
2334 (quasicons (syntax (quote quasiquote))
2335 (quasi (syntax (p)) (+ lev 1))))
2337 (quasicons (quasi (syntax p) lev) (quasi (syntax q) lev)))
2338 (#(x ...) (quasivector (quasi (syntax (x ...)) lev)))
2339 (p (syntax (quote p)))))))
2342 ((_ e) (quasi (syntax e) 0))))))
2344 (define-syntax include
2348 (let ((p (open-input-file fn)))
2349 (let f ((x (read p)))
2351 (begin (close-input-port p) '())
2352 (cons (datum->syntax k x)
2356 (let ((fn (syntax->datum (syntax filename))))
2357 (with-syntax (((exp ...) (read-file fn (syntax k))))
2358 (syntax (begin exp ...))))))))
2360 (define-syntax unquote
2364 (syntax-violation 'unquote
2365 "expression not valid outside of quasiquote"
2368 (define-syntax unquote-splicing
2372 (syntax-violation 'unquote-splicing
2373 "expression not valid outside of quasiquote"
2381 ((body (let f ((clause (syntax m1)) (clauses (syntax (m2 ...))))
2383 (syntax-case clause (else)
2384 ((else e1 e2 ...) (syntax (begin e1 e2 ...)))
2385 (((k ...) e1 e2 ...)
2386 (syntax (if (memv t '(k ...)) (begin e1 e2 ...))))
2387 (_ (syntax-violation 'case "bad clause" x clause)))
2388 (with-syntax ((rest (f (car clauses) (cdr clauses))))
2389 (syntax-case clause (else)
2390 (((k ...) e1 e2 ...)
2391 (syntax (if (memv t '(k ...))
2394 (_ (syntax-violation 'case "bad clause" x
2396 (syntax (let ((t e)) body)))))))
2398 (define-syntax identifier-syntax
2406 (identifier? (syntax id))
2409 (syntax (e x (... ...)))))))))))