3 ;;;; Copyright (C) 2001, 2003, 2006, 2009 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 3 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).
197 ;;; When changing syntax-object representations, it is necessary to support
198 ;;; both old and new syntax-object representations in id-var-name. It
199 ;;; should be sufficient to recognize old representations and treat
200 ;;; them as not lexically bound.
205 (set-current-module (resolve-module '(guile))))
208 ;;; Private version of and-map that handles multiple lists.
210 (lambda (f first . rest)
213 (let andmap ((first first))
214 (let ((x (car first)) (first (cdr first)))
217 (and (f x) (andmap first)))))
218 (let andmap ((first first) (rest rest))
219 (let ((x (car first))
222 (rest (map cdr rest)))
224 (apply f (cons x xr))
225 (and (apply f (cons x xr)) (andmap first rest)))))))))
227 (define-syntax define-structure
229 (define construct-name
230 (lambda (template-identifier . args)
238 (symbol->string (syntax->datum x))))
242 (and-map identifier? (syntax (name id1 ...)))
244 ((constructor (construct-name (syntax name) "make-" (syntax name)))
245 (predicate (construct-name (syntax name) (syntax name) "?"))
247 (map (lambda (x) (construct-name x (syntax name) "-" x))
251 (construct-name x "set-" (syntax name) "-" x "!"))
254 (+ (length (syntax (id1 ...))) 1))
256 (let f ((i 1) (ids (syntax (id1 ...))))
259 (cons i (f (+ i 1) (cdr ids)))))))
263 (vector 'name id1 ... )))
267 (= (vector-length x) structure-length)
268 (eq? (vector-ref x 0) 'name))))
271 (vector-ref x index)))
275 (vector-set! x index update)))
279 (define noexpand "noexpand")
280 (define *mode* (make-fluid))
282 ;;; hooks to nonportable run-time helpers
289 (define top-level-eval-hook
293 ,(case (fluid-ref *mode*)
294 ((c) ((@ (language tree-il) tree-il->scheme) x))
297 (define local-eval-hook
301 ,(case (fluid-ref *mode*)
302 ((c) ((@ (language tree-il) tree-il->scheme) x))
305 (define-syntax gensym-hook
309 (define put-global-definition-hook
310 (lambda (symbol type val)
311 (let ((existing (let ((v (module-variable (current-module) symbol)))
312 (and v (variable-bound? v)
313 (let ((val (variable-ref v)))
315 (not (syncase-macro-type val))
317 (module-define! (current-module)
320 (make-extended-syncase-macro existing type val)
321 (make-syncase-macro type val))))))
323 (define get-global-definition-hook
324 (lambda (symbol module)
325 (if (and (not module) (current-module))
326 (warn "module system is booted, we should have a module" symbol))
327 (let ((v (module-variable (if module
328 (resolve-module (cdr module))
331 (and v (variable-bound? v)
332 (let ((val (variable-ref v)))
333 (and (macro? val) (syncase-macro-type val)
334 (cons (syncase-macro-type val)
335 (syncase-macro-binding val))))))))
340 (define (decorate-source e s)
341 (if (and (pair? e) s)
342 (set-source-properties! e s))
345 ;;; output constructors
348 (case (fluid-ref *mode*)
349 ((c) ((@ (language tree-il) make-void) source))
350 (else (decorate-source '(if #f #f) source)))))
352 (define build-application
353 (lambda (source fun-exp arg-exps)
354 (case (fluid-ref *mode*)
355 ((c) ((@ (language tree-il) make-application) source fun-exp arg-exps))
356 (else (decorate-source `(,fun-exp . ,arg-exps) source)))))
358 (define build-conditional
359 (lambda (source test-exp then-exp else-exp)
360 (case (fluid-ref *mode*)
361 ((c) ((@ (language tree-il) make-conditional)
362 source test-exp then-exp else-exp))
363 (else (decorate-source
364 (if (equal? else-exp '(if #f #f))
365 `(if ,test-exp ,then-exp)
366 `(if ,test-exp ,then-exp ,else-exp))
369 (define build-lexical-reference
370 (lambda (type source name var)
371 (case (fluid-ref *mode*)
372 ((c) ((@ (language tree-il) make-lexical-ref) source name var))
373 (else (decorate-source var source)))))
375 (define build-lexical-assignment
376 (lambda (source name var exp)
377 (case (fluid-ref *mode*)
378 ((c) ((@ (language tree-il) make-lexical-set) source name var exp))
379 (else (decorate-source `(set! ,var ,exp) source)))))
381 ;; Before modules are booted, we can't expand into data structures from
382 ;; (language tree-il) -- we need to give the evaluator the
383 ;; s-expressions that it understands natively. Actually the real truth
384 ;; of the matter is that the evaluator doesn't understand tree-il
385 ;; structures at all. So until we fix the evaluator, if ever, the
386 ;; conflation that we should use tree-il iff we are compiling
389 (define (analyze-variable mod var modref-cont bare-cont)
392 (let ((kind (car mod))
395 ((public) (modref-cont mod var #t))
396 ((private) (if (not (equal? mod (module-name (current-module))))
397 (modref-cont mod var #f)
399 ((bare) (bare-cont var))
400 ((hygiene) (if (and (not (equal? mod (module-name (current-module))))
401 (module-variable (resolve-module mod) var))
402 (modref-cont mod var #f)
404 (else (syntax-violation #f "bad module kind" var mod))))))
406 (define build-global-reference
407 (lambda (source var mod)
410 (lambda (mod var public?)
411 (case (fluid-ref *mode*)
412 ((c) ((@ (language tree-il) make-module-ref) source mod var public?))
413 (else (decorate-source (list (if public? '@ '@@) mod var) source))))
415 (case (fluid-ref *mode*)
416 ((c) ((@ (language tree-il) make-toplevel-ref) source var))
417 (else (decorate-source var source)))))))
419 (define build-global-assignment
420 (lambda (source var exp mod)
423 (lambda (mod var public?)
424 (case (fluid-ref *mode*)
425 ((c) ((@ (language tree-il) make-module-set) source mod var public? exp))
426 (else (decorate-source `(set! ,(list (if public? '@ '@@) mod var) ,exp) source))))
428 (case (fluid-ref *mode*)
429 ((c) ((@ (language tree-il) make-toplevel-set) source var exp))
430 (else (decorate-source `(set! ,var ,exp) source)))))))
432 ;; FIXME: there is a bug that prevents (set! ((@ (foo) bar) baz) quz)
433 ;; from working. Hack around it.
434 (define (maybe-name-value! name val)
436 (((@ (language tree-il) lambda?) val)
437 (let ((meta ((@ (language tree-il) lambda-meta) val)))
438 (if (not (assq 'name meta))
439 ((setter (@ (language tree-il) lambda-meta))
441 (acons 'name name meta)))))))
443 (define build-global-definition
444 (lambda (source var exp)
445 (case (fluid-ref *mode*)
447 (maybe-name-value! var exp)
448 ((@ (language tree-il) make-toplevel-define) source var exp))
449 (else (decorate-source `(define ,var ,exp) source)))))
452 (lambda (src ids vars docstring exp)
453 (case (fluid-ref *mode*)
454 ((c) ((@ (language tree-il) make-lambda) src ids vars
455 (if docstring `((documentation . ,docstring)) '())
457 (else (decorate-source
458 `(lambda ,vars ,@(if docstring (list docstring) '())
462 (define build-primref
464 (if (equal? (module-name (current-module)) '(guile))
465 (case (fluid-ref *mode*)
466 ((c) ((@ (language tree-il) make-toplevel-ref) src name))
467 (else (decorate-source name src)))
468 (case (fluid-ref *mode*)
469 ((c) ((@ (language tree-il) make-module-ref) src '(guile) name #f))
470 (else (decorate-source `(@@ (guile) ,name) src))))))
472 (define (build-data src exp)
473 (case (fluid-ref *mode*)
474 ((c) ((@ (language tree-il) make-const) src exp))
475 (else (decorate-source
476 (if (and (self-evaluating? exp) (not (vector? exp)))
481 (define build-sequence
483 (if (null? (cdr exps))
485 (case (fluid-ref *mode*)
486 ((c) ((@ (language tree-il) make-sequence) src exps))
487 (else (decorate-source `(begin ,@exps) src))))))
490 (lambda (src ids vars val-exps body-exp)
493 (case (fluid-ref *mode*)
495 (for-each maybe-name-value! ids val-exps)
496 ((@ (language tree-il) make-let) src ids vars val-exps body-exp))
497 (else (decorate-source
498 `(let ,(map list vars val-exps) ,body-exp)
501 (define build-named-let
502 (lambda (src ids vars val-exps body-exp)
507 (case (fluid-ref *mode*)
509 (let ((proc (build-lambda src ids vars #f body-exp)))
510 (maybe-name-value! f-name proc)
511 (for-each maybe-name-value! ids val-exps)
512 ((@ (language tree-il) make-letrec) src
513 (list f-name) (list f) (list proc)
514 (build-application src (build-lexical-reference 'fun src f-name f)
516 (else (decorate-source
517 `(let ,f ,(map list vars val-exps) ,body-exp)
521 (lambda (src ids vars val-exps body-exp)
524 (case (fluid-ref *mode*)
526 (for-each maybe-name-value! ids val-exps)
527 ((@ (language tree-il) make-letrec) src ids vars val-exps body-exp))
528 (else (decorate-source
529 `(letrec ,(map list vars val-exps) ,body-exp)
532 ;; FIXME: use a faster gensym
533 (define-syntax build-lexical-var
535 ((_ src id) (gensym (string-append (symbol->string id) " ")))))
537 (define-structure (syntax-object expression wrap module))
539 (define-syntax no-source (identifier-syntax #f))
541 (define source-annotation
545 (source-annotation (syntax-object-expression x)))
546 ((pair? x) (let ((props (source-properties x)))
552 (define-syntax arg-check
556 (if (not (pred? x)) (syntax-violation who "invalid argument" x))))))
558 ;;; compile-time environments
560 ;;; wrap and environment comprise two level mapping.
561 ;;; wrap : id --> label
562 ;;; env : label --> <element>
564 ;;; environments are represented in two parts: a lexical part and a global
565 ;;; part. The lexical part is a simple list of associations from labels
566 ;;; to bindings. The global part is implemented by
567 ;;; {put,get}-global-definition-hook and associates symbols with
570 ;;; global (assumed global variable) and displaced-lexical (see below)
571 ;;; do not show up in any environment; instead, they are fabricated by
572 ;;; lookup when it finds no other bindings.
574 ;;; <environment> ::= ((<label> . <binding>)*)
576 ;;; identifier bindings include a type and a value
578 ;;; <binding> ::= (macro . <procedure>) macros
579 ;;; (core . <procedure>) core forms
580 ;;; (module-ref . <procedure>) @ or @@
583 ;;; (define-syntax) define-syntax
584 ;;; (local-syntax . rec?) let-syntax/letrec-syntax
585 ;;; (eval-when) eval-when
586 ;;; (syntax . (<var> . <level>)) pattern variables
587 ;;; (global) assumed global variable
588 ;;; (lexical . <var>) lexical variables
589 ;;; (displaced-lexical) displaced lexicals
590 ;;; <level> ::= <nonnegative integer>
591 ;;; <var> ::= variable returned by build-lexical-var
593 ;;; a macro is a user-defined syntactic-form. a core is a system-defined
594 ;;; syntactic form. begin, define, define-syntax, and eval-when are
595 ;;; treated specially since they are sensitive to whether the form is
596 ;;; at top-level and (except for eval-when) can denote valid internal
599 ;;; a pattern variable is a variable introduced by syntax-case and can
600 ;;; be referenced only within a syntax form.
602 ;;; any identifier for which no top-level syntax definition or local
603 ;;; binding of any kind has been seen is assumed to be a global
606 ;;; a lexical variable is a lambda- or letrec-bound variable.
608 ;;; a displaced-lexical identifier is a lexical identifier removed from
609 ;;; it's scope by the return of a syntax object containing the identifier.
610 ;;; a displaced lexical can also appear when a letrec-syntax-bound
611 ;;; keyword is referenced on the rhs of one of the letrec-syntax clauses.
612 ;;; a displaced lexical should never occur with properly written macros.
614 (define-syntax make-binding
615 (syntax-rules (quote)
616 ((_ type value) (cons type value))
618 ((_ type) (cons type '()))))
619 (define binding-type car)
620 (define binding-value cdr)
622 (define-syntax null-env (identifier-syntax '()))
625 (lambda (labels bindings r)
628 (extend-env (cdr labels) (cdr bindings)
629 (cons (cons (car labels) (car bindings)) r)))))
631 (define extend-var-env
632 ; variant of extend-env that forms "lexical" binding
633 (lambda (labels vars r)
636 (extend-var-env (cdr labels) (cdr vars)
637 (cons (cons (car labels) (make-binding 'lexical (car vars))) r)))))
639 ;;; we use a "macros only" environment in expansion of local macro
640 ;;; definitions so that their definitions can use local macros without
641 ;;; attempting to use other lexical identifiers.
642 (define macros-only-env
647 (if (eq? (cadr a) 'macro)
648 (cons a (macros-only-env (cdr r)))
649 (macros-only-env (cdr r)))))))
652 ; x may be a label or a symbol
653 ; although symbols are usually global, we check the environment first
654 ; anyway because a temporary binding may have been established by
660 (or (get-global-definition-hook x mod) (make-binding 'global)))
661 (else (make-binding 'displaced-lexical)))))
663 (define global-extend
664 (lambda (type sym val)
665 (put-global-definition-hook sym type val)))
668 ;;; Conceptually, identifiers are always syntax objects. Internally,
669 ;;; however, the wrap is sometimes maintained separately (a source of
670 ;;; efficiency and confusion), so that symbols are also considered
671 ;;; identifiers by id?. Externally, they are always wrapped.
673 (define nonsymbol-id?
675 (and (syntax-object? x)
676 (symbol? (syntax-object-expression x)))))
682 ((syntax-object? x) (symbol? (syntax-object-expression x)))
685 (define-syntax id-sym-name
689 (if (syntax-object? x)
690 (syntax-object-expression x)
693 (define id-sym-name&marks
695 (if (syntax-object? x)
697 (syntax-object-expression x)
698 (join-marks (wrap-marks w) (wrap-marks (syntax-object-wrap x))))
699 (values x (wrap-marks w)))))
701 ;;; syntax object wraps
703 ;;; <wrap> ::= ((<mark> ...) . (<subst> ...))
704 ;;; <subst> ::= <shift> | <subs>
705 ;;; <subs> ::= #(<old name> <label> (<mark> ...))
706 ;;; <shift> ::= positive fixnum
708 (define make-wrap cons)
709 (define wrap-marks car)
710 (define wrap-subst cdr)
712 (define-syntax subst-rename? (identifier-syntax vector?))
713 (define-syntax rename-old (syntax-rules () ((_ x) (vector-ref x 0))))
714 (define-syntax rename-new (syntax-rules () ((_ x) (vector-ref x 1))))
715 (define-syntax rename-marks (syntax-rules () ((_ x) (vector-ref x 2))))
716 (define-syntax make-rename
718 ((_ old new marks) (vector old new marks))))
720 ;;; labels must be comparable with "eq?" and distinct from symbols.
722 (lambda () (string #\i)))
728 (cons (gen-label) (gen-labels (cdr ls))))))
730 (define-structure (ribcage symnames marks labels))
732 (define-syntax empty-wrap (identifier-syntax '(())))
734 (define-syntax top-wrap (identifier-syntax '((top))))
736 (define-syntax top-marked?
738 ((_ w) (memq 'top (wrap-marks w)))))
740 ;;; Marks must be comparable with "eq?" and distinct from pairs and
741 ;;; the symbol top. We do not use integers so that marks will remain
742 ;;; unique even across file compiles.
744 (define-syntax the-anti-mark (identifier-syntax #f))
748 (make-wrap (cons the-anti-mark (wrap-marks w))
749 (cons 'shift (wrap-subst w)))))
751 (define-syntax new-mark
755 ;;; make-empty-ribcage and extend-ribcage maintain list-based ribcages for
756 ;;; internal definitions, in which the ribcages are built incrementally
757 (define-syntax make-empty-ribcage
759 ((_) (make-ribcage '() '() '()))))
761 (define extend-ribcage!
762 ; must receive ids with complete wraps
763 (lambda (ribcage id label)
764 (set-ribcage-symnames! ribcage
765 (cons (syntax-object-expression id)
766 (ribcage-symnames ribcage)))
767 (set-ribcage-marks! ribcage
768 (cons (wrap-marks (syntax-object-wrap id))
769 (ribcage-marks ribcage)))
770 (set-ribcage-labels! ribcage
771 (cons label (ribcage-labels ribcage)))))
773 ;;; make-binding-wrap creates vector-based ribcages
774 (define make-binding-wrap
775 (lambda (ids labels w)
781 (let ((labelvec (list->vector labels)))
782 (let ((n (vector-length labelvec)))
783 (let ((symnamevec (make-vector n)) (marksvec (make-vector n)))
784 (let f ((ids ids) (i 0))
785 (if (not (null? ids))
787 (lambda () (id-sym-name&marks (car ids) w))
788 (lambda (symname marks)
789 (vector-set! symnamevec i symname)
790 (vector-set! marksvec i marks)
791 (f (cdr ids) (fx+ i 1))))))
792 (make-ribcage symnamevec marksvec labelvec))))
803 (let ((m1 (wrap-marks w1)) (s1 (wrap-subst w1)))
809 (smart-append s1 (wrap-subst w2))))
811 (smart-append m1 (wrap-marks w2))
812 (smart-append s1 (wrap-subst w2)))))))
816 (smart-append m1 m2)))
823 (eq? (car x) (car y))
824 (same-marks? (cdr x) (cdr y))))))
830 ((_ e) (call-with-values (lambda () e) (lambda (x . ignore) x)))))
832 (lambda (sym subst marks)
835 (let ((fst (car subst)))
837 (search sym (cdr subst) (cdr marks))
838 (let ((symnames (ribcage-symnames fst)))
839 (if (vector? symnames)
840 (search-vector-rib sym subst marks symnames fst)
841 (search-list-rib sym subst marks symnames fst))))))))
842 (define search-list-rib
843 (lambda (sym subst marks symnames ribcage)
844 (let f ((symnames symnames) (i 0))
846 ((null? symnames) (search sym (cdr subst) marks))
847 ((and (eq? (car symnames) sym)
848 (same-marks? marks (list-ref (ribcage-marks ribcage) i)))
849 (values (list-ref (ribcage-labels ribcage) i) marks))
850 (else (f (cdr symnames) (fx+ i 1)))))))
851 (define search-vector-rib
852 (lambda (sym subst marks symnames ribcage)
853 (let ((n (vector-length symnames)))
856 ((fx= i n) (search sym (cdr subst) marks))
857 ((and (eq? (vector-ref symnames i) sym)
858 (same-marks? marks (vector-ref (ribcage-marks ribcage) i)))
859 (values (vector-ref (ribcage-labels ribcage) i) marks))
860 (else (f (fx+ i 1))))))))
863 (or (first (search id (wrap-subst w) (wrap-marks w))) id))
865 (let ((id (syntax-object-expression id))
866 (w1 (syntax-object-wrap id)))
867 (let ((marks (join-marks (wrap-marks w) (wrap-marks w1))))
868 (call-with-values (lambda () (search id (wrap-subst w) marks))
869 (lambda (new-id marks)
871 (first (search id (wrap-subst w1) marks))
873 (else (syntax-violation 'id-var-name "invalid id" id)))))
875 ;;; free-id=? must be passed fully wrapped ids since (free-id=? x y)
876 ;;; may be true even if (free-id=? (wrap x w) (wrap y w)) is not.
880 (and (eq? (id-sym-name i) (id-sym-name j)) ; accelerator
881 (eq? (id-var-name i empty-wrap) (id-var-name j empty-wrap)))))
883 ;;; bound-id=? may be passed unwrapped (or partially wrapped) ids as
884 ;;; long as the missing portion of the wrap is common to both of the ids
885 ;;; since (bound-id=? x y) iff (bound-id=? (wrap x w) (wrap y w))
889 (if (and (syntax-object? i) (syntax-object? j))
890 (and (eq? (syntax-object-expression i)
891 (syntax-object-expression j))
892 (same-marks? (wrap-marks (syntax-object-wrap i))
893 (wrap-marks (syntax-object-wrap j))))
896 ;;; "valid-bound-ids?" returns #t if it receives a list of distinct ids.
897 ;;; valid-bound-ids? may be passed unwrapped (or partially wrapped) ids
898 ;;; as long as the missing portion of the wrap is common to all of the
901 (define valid-bound-ids?
903 (and (let all-ids? ((ids ids))
906 (all-ids? (cdr ids)))))
907 (distinct-bound-ids? ids))))
909 ;;; distinct-bound-ids? expects a list of ids and returns #t if there are
910 ;;; no duplicates. It is quadratic on the length of the id list; long
911 ;;; lists could be sorted to make it more efficient. distinct-bound-ids?
912 ;;; may be passed unwrapped (or partially wrapped) ids as long as the
913 ;;; missing portion of the wrap is common to all of the ids.
915 (define distinct-bound-ids?
917 (let distinct? ((ids ids))
919 (and (not (bound-id-member? (car ids) (cdr ids)))
920 (distinct? (cdr ids)))))))
922 (define bound-id-member?
924 (and (not (null? list))
925 (or (bound-id=? x (car list))
926 (bound-id-member? x (cdr list))))))
928 ;;; wrapping expressions and identifiers
933 ((and (null? (wrap-marks w)) (null? (wrap-subst w))) x)
936 (syntax-object-expression x)
937 (join-wraps w (syntax-object-wrap x))
938 (syntax-object-module x)))
940 (else (make-syntax-object x w defmod)))))
943 (lambda (x w s defmod)
944 (wrap (decorate-source x s) w defmod)))
949 (lambda (body r w s mod)
951 (let dobody ((body body) (r r) (w w) (mod mod))
954 (let ((first (chi (car body) r w mod)))
955 (cons first (dobody (cdr body) r w mod))))))))
957 (define chi-top-sequence
958 (lambda (body r w s m esew mod)
960 (let dobody ((body body) (r r) (w w) (m m) (esew esew) (mod mod))
963 (let ((first (chi-top (car body) r w m esew mod)))
964 (cons first (dobody (cdr body) r w m esew mod))))))))
966 (define chi-install-global
968 (build-global-definition
971 ;; FIXME: seems nasty to call current-module here
972 (if (let ((v (module-variable (current-module) name)))
973 ;; FIXME use primitive-macro?
974 (and v (variable-bound? v) (macro? (variable-ref v))
975 (not (eq? (macro-type (variable-ref v)) 'syncase-macro))))
978 (build-primref no-source 'make-extended-syncase-macro)
979 (list (build-application
981 (build-primref no-source 'module-ref)
982 (list (build-application
984 (build-primref no-source 'current-module)
986 (build-data no-source name)))
987 (build-data no-source 'macro)
991 (build-primref no-source 'make-syncase-macro)
992 (list (build-data no-source 'macro) e))))))
994 (define chi-when-list
995 (lambda (e when-list w)
996 ; when-list is syntax'd version of list of situations
997 (let f ((when-list when-list) (situations '()))
998 (if (null? when-list)
1001 (cons (let ((x (car when-list)))
1003 ((free-id=? x (syntax compile)) 'compile)
1004 ((free-id=? x (syntax load)) 'load)
1005 ((free-id=? x (syntax eval)) 'eval)
1006 (else (syntax-violation 'eval-when
1011 ;;; syntax-type returns six values: type, value, e, w, s, and mod. The
1012 ;;; first two are described in the table below.
1014 ;;; type value explanation
1015 ;;; -------------------------------------------------------------------
1016 ;;; core procedure core singleton
1017 ;;; core-form procedure core form
1018 ;;; module-ref procedure @ or @@ singleton
1019 ;;; lexical name lexical variable reference
1020 ;;; global name global variable reference
1021 ;;; begin none begin keyword
1022 ;;; define none define keyword
1023 ;;; define-syntax none define-syntax keyword
1024 ;;; local-syntax rec? letrec-syntax/let-syntax keyword
1025 ;;; eval-when none eval-when keyword
1026 ;;; syntax level pattern variable
1027 ;;; displaced-lexical none displaced lexical identifier
1028 ;;; lexical-call name call to lexical variable
1029 ;;; global-call name call to global variable
1030 ;;; call none any other call
1031 ;;; begin-form none begin expression
1032 ;;; define-form id variable definition
1033 ;;; define-syntax-form id syntax definition
1034 ;;; local-syntax-form rec? syntax definition
1035 ;;; eval-when-form none eval-when form
1036 ;;; constant none self-evaluating datum
1037 ;;; other none anything else
1039 ;;; For define-form and define-syntax-form, e is the rhs expression.
1040 ;;; For all others, e is the entire form. w is the wrap for e.
1041 ;;; s is the source for the entire form. mod is the module for e.
1043 ;;; syntax-type expands macros and unwraps as necessary to get to
1044 ;;; one of the forms above. It also parses define and define-syntax
1045 ;;; forms, although perhaps this should be done by the consumer.
1048 (lambda (e r w s rib mod for-car?)
1051 (let* ((n (id-var-name e w))
1052 (b (lookup n r mod))
1053 (type (binding-type b)))
1055 ((lexical) (values type (binding-value b) e w s mod))
1056 ((global) (values type n e w s mod))
1059 (values type (binding-value b) e w s mod)
1060 (syntax-type (chi-macro (binding-value b) e r w rib mod)
1061 r empty-wrap s rib mod #f)))
1062 (else (values type (binding-value b) e w s mod)))))
1064 (let ((first (car e)))
1066 (lambda () (syntax-type first r w s rib mod #t))
1067 (lambda (ftype fval fe fw fs fmod)
1070 (values 'lexical-call fval e w s mod))
1072 ;; If we got here via an (@@ ...) expansion, we need to
1073 ;; make sure the fmod information is propagated back
1074 ;; correctly -- hence this consing.
1075 (values 'global-call (make-syntax-object fval w fmod)
1078 (syntax-type (chi-macro fval e r w rib mod)
1079 r empty-wrap s rib mod for-car?))
1081 (call-with-values (lambda () (fval e))
1083 (syntax-type sym r w s rib mod for-car?))))
1085 (values 'core-form fval e w s mod))
1087 (values 'local-syntax-form fval e w s mod))
1089 (values 'begin-form #f e w s mod))
1091 (values 'eval-when-form #f e w s mod))
1096 (values 'define-form (syntax name) (syntax val) w s mod))
1097 ((_ (name . args) e1 e2 ...)
1098 (and (id? (syntax name))
1099 (valid-bound-ids? (lambda-var-list (syntax args))))
1100 ; need lambda here...
1101 (values 'define-form (wrap (syntax name) w mod)
1103 (cons (syntax lambda) (wrap (syntax (args e1 e2 ...)) w mod))
1108 (values 'define-form (wrap (syntax name) w mod)
1110 empty-wrap s mod))))
1115 (values 'define-syntax-form (syntax name)
1116 (syntax val) w s mod))))
1118 (values 'call #f e w s mod)))))))
1120 (syntax-type (syntax-object-expression e)
1122 (join-wraps w (syntax-object-wrap e))
1123 s rib (or (syntax-object-module e) mod) for-car?))
1124 ((self-evaluating? e) (values 'constant #f e w s mod))
1125 (else (values 'other #f e w s mod)))))
1128 (lambda (e r w m esew mod)
1129 (define-syntax eval-if-c&e
1133 (if (eq? m 'c&e) (top-level-eval-hook x mod))
1136 (lambda () (syntax-type e r w (source-annotation e) #f mod #f))
1137 (lambda (type value e w s mod)
1143 (chi-top-sequence (syntax (e1 e2 ...)) r w s m esew mod))))
1144 ((local-syntax-form)
1145 (chi-local-syntax value e r w s mod
1146 (lambda (body r w s mod)
1147 (chi-top-sequence body r w s m esew mod))))
1150 ((_ (x ...) e1 e2 ...)
1151 (let ((when-list (chi-when-list e (syntax (x ...)) w))
1152 (body (syntax (e1 e2 ...))))
1155 (if (memq 'eval when-list)
1156 (chi-top-sequence body r w s 'e '(eval) mod)
1158 ((memq 'load when-list)
1159 (if (or (memq 'compile when-list)
1160 (and (eq? m 'c&e) (memq 'eval when-list)))
1161 (chi-top-sequence body r w s 'c&e '(compile load) mod)
1162 (if (memq m '(c c&e))
1163 (chi-top-sequence body r w s 'c '(load) mod)
1165 ((or (memq 'compile when-list)
1166 (and (eq? m 'c&e) (memq 'eval when-list)))
1167 (top-level-eval-hook
1168 (chi-top-sequence body r w s 'e '(eval) mod)
1171 (else (chi-void)))))))
1172 ((define-syntax-form)
1173 (let ((n (id-var-name value w)) (r (macros-only-env r)))
1176 (if (memq 'compile esew)
1177 (let ((e (chi-install-global n (chi e r w mod))))
1178 (top-level-eval-hook e mod)
1179 (if (memq 'load esew) e (chi-void)))
1180 (if (memq 'load esew)
1181 (chi-install-global n (chi e r w mod))
1184 (let ((e (chi-install-global n (chi e r w mod))))
1185 (top-level-eval-hook e mod)
1188 (if (memq 'eval esew)
1189 (top-level-eval-hook
1190 (chi-install-global n (chi e r w mod))
1194 (let* ((n (id-var-name value w))
1195 (type (binding-type (lookup n r mod))))
1197 ((global core macro module-ref)
1198 ;; affect compile-time environment (once we have booted)
1199 (if (and (not (module-local-variable (current-module) n))
1201 (let ((old (module-variable (current-module) n)))
1202 ;; use value of the same-named imported variable, if
1204 (module-define! (current-module) n
1209 (build-global-definition s n (chi e r w mod))
1211 ((displaced-lexical)
1212 (syntax-violation #f "identifier out of context"
1213 e (wrap value w mod)))
1215 (syntax-violation #f "cannot define keyword at top level"
1216 e (wrap value w mod))))))
1217 (else (eval-if-c&e m (chi-expr type value e r w s mod) mod)))))))
1222 (lambda () (syntax-type e r w (source-annotation e) #f mod #f))
1223 (lambda (type value e w s mod)
1224 (chi-expr type value e r w s mod)))))
1227 (lambda (type value e r w s mod)
1230 (build-lexical-reference 'value s e value))
1232 ;; apply transformer
1233 (value e r w s mod))
1235 (call-with-values (lambda () (value e))
1236 ;; we could add a public? arg here
1237 (lambda (id mod) (build-global-reference s id mod))))
1240 (build-lexical-reference 'fun (source-annotation (car e))
1245 (build-global-reference (source-annotation (car e))
1246 (if (syntax-object? value)
1247 (syntax-object-expression value)
1249 (if (syntax-object? value)
1250 (syntax-object-module value)
1253 ((constant) (build-data s (strip (source-wrap e w s mod) empty-wrap)))
1254 ((global) (build-global-reference s value mod))
1255 ((call) (chi-application (chi (car e) r w mod) e r w s mod))
1258 ((_ e1 e2 ...) (chi-sequence (syntax (e1 e2 ...)) r w s mod))))
1259 ((local-syntax-form)
1260 (chi-local-syntax value e r w s mod chi-sequence))
1263 ((_ (x ...) e1 e2 ...)
1264 (let ((when-list (chi-when-list e (syntax (x ...)) w)))
1265 (if (memq 'eval when-list)
1266 (chi-sequence (syntax (e1 e2 ...)) r w s mod)
1268 ((define-form define-syntax-form)
1269 (syntax-violation #f "definition in expression context"
1270 e (wrap value w mod)))
1272 (syntax-violation #f "reference to pattern variable outside syntax form"
1273 (source-wrap e w s mod)))
1274 ((displaced-lexical)
1275 (syntax-violation #f "reference to identifier outside its scope"
1276 (source-wrap e w s mod)))
1277 (else (syntax-violation #f "unexpected syntax"
1278 (source-wrap e w s mod))))))
1280 (define chi-application
1281 (lambda (x e r w s mod)
1284 (build-application s x
1285 (map (lambda (e) (chi e r w mod)) (syntax (e1 ...))))))))
1288 (lambda (p e r w rib mod)
1289 (define rebuild-macro-output
1292 (cons (rebuild-macro-output (car x) m)
1293 (rebuild-macro-output (cdr x) m)))
1295 (let ((w (syntax-object-wrap x)))
1296 (let ((ms (wrap-marks w)) (s (wrap-subst w)))
1297 (if (and (pair? ms) (eq? (car ms) the-anti-mark))
1298 ;; output is from original text
1300 (syntax-object-expression x)
1301 (make-wrap (cdr ms) (if rib (cons rib (cdr s)) (cdr s)))
1302 (syntax-object-module x))
1303 ;; output introduced by macro
1305 (syntax-object-expression x)
1306 (make-wrap (cons m ms)
1308 (cons rib (cons 'shift s))
1310 (let ((pmod (procedure-module p)))
1312 ;; hither the hygiene
1313 (cons 'hygiene (module-name pmod))
1314 ;; but it's possible for the proc to have
1315 ;; no mod, if it was made before modules
1317 '(hygiene guile))))))))
1319 (let* ((n (vector-length x)) (v (make-vector n)))
1320 (do ((i 0 (fx+ i 1)))
1323 (rebuild-macro-output (vector-ref x i) m)))))
1325 (syntax-violation #f "encountered raw symbol in macro output"
1326 (source-wrap e w s mod) x))
1328 (rebuild-macro-output (p (wrap e (anti-mark w) mod)) (new-mark))))
1331 ;; In processing the forms of the body, we create a new, empty wrap.
1332 ;; This wrap is augmented (destructively) each time we discover that
1333 ;; the next form is a definition. This is done:
1335 ;; (1) to allow the first nondefinition form to be a call to
1336 ;; one of the defined ids even if the id previously denoted a
1337 ;; definition keyword or keyword for a macro expanding into a
1339 ;; (2) to prevent subsequent definition forms (but unfortunately
1340 ;; not earlier ones) and the first nondefinition form from
1341 ;; confusing one of the bound identifiers for an auxiliary
1343 ;; (3) so that we do not need to restart the expansion of the
1344 ;; first nondefinition form, which is problematic anyway
1345 ;; since it might be the first element of a begin that we
1346 ;; have just spliced into the body (meaning if we restarted,
1347 ;; we'd really need to restart with the begin or the macro
1348 ;; call that expanded into the begin, and we'd have to give
1349 ;; up allowing (begin <defn>+ <expr>+), which is itself
1350 ;; problematic since we don't know if a begin contains only
1351 ;; definitions until we've expanded it).
1353 ;; Before processing the body, we also create a new environment
1354 ;; containing a placeholder for the bindings we will add later and
1355 ;; associate this environment with each form. In processing a
1356 ;; let-syntax or letrec-syntax, the associated environment may be
1357 ;; augmented with local keyword bindings, so the environment may
1358 ;; be different for different forms in the body. Once we have
1359 ;; gathered up all of the definitions, we evaluate the transformer
1360 ;; expressions and splice into r at the placeholder the new variable
1361 ;; and keyword bindings. This allows let-syntax or letrec-syntax
1362 ;; forms local to a portion or all of the body to shadow the
1363 ;; definition bindings.
1365 ;; Subforms of a begin, let-syntax, or letrec-syntax are spliced
1368 ;; outer-form is fully wrapped w/source
1369 (lambda (body outer-form r w mod)
1370 (let* ((r (cons '("placeholder" . (placeholder)) r))
1371 (ribcage (make-empty-ribcage))
1372 (w (make-wrap (wrap-marks w) (cons ribcage (wrap-subst w)))))
1373 (let parse ((body (map (lambda (x) (cons r (wrap x w mod))) body))
1374 (ids '()) (labels '())
1375 (var-ids '()) (vars '()) (vals '()) (bindings '()))
1377 (syntax-violation #f "no expressions in body" outer-form)
1378 (let ((e (cdar body)) (er (caar body)))
1380 (lambda () (syntax-type e er empty-wrap (source-annotation er) ribcage mod #f))
1381 (lambda (type value e w s mod)
1384 (let ((id (wrap value w mod)) (label (gen-label)))
1385 (let ((var (gen-var id)))
1386 (extend-ribcage! ribcage id label)
1388 (cons id ids) (cons label labels)
1390 (cons var vars) (cons (cons er (wrap e w mod)) vals)
1391 (cons (make-binding 'lexical var) bindings)))))
1392 ((define-syntax-form)
1393 (let ((id (wrap value w mod)) (label (gen-label)))
1394 (extend-ribcage! ribcage id label)
1396 (cons id ids) (cons label labels)
1398 (cons (make-binding 'macro (cons er (wrap e w mod)))
1403 (parse (let f ((forms (syntax (e1 ...))))
1406 (cons (cons er (wrap (car forms) w mod))
1408 ids labels var-ids vars vals bindings))))
1409 ((local-syntax-form)
1410 (chi-local-syntax value e er w s mod
1411 (lambda (forms er w s mod)
1412 (parse (let f ((forms forms))
1415 (cons (cons er (wrap (car forms) w mod))
1417 ids labels var-ids vars vals bindings))))
1418 (else ; found a non-definition
1420 (build-sequence no-source
1422 (chi (cdr x) (car x) empty-wrap mod))
1423 (cons (cons er (source-wrap e w s mod))
1426 (if (not (valid-bound-ids? ids))
1428 #f "invalid or duplicate identifier in definition"
1430 (let loop ((bs bindings) (er-cache #f) (r-cache #f))
1431 (if (not (null? bs))
1432 (let* ((b (car bs)))
1433 (if (eq? (car b) 'macro)
1434 (let* ((er (cadr b))
1436 (if (eq? er er-cache)
1438 (macros-only-env er))))
1440 (eval-local-transformer
1441 (chi (cddr b) r-cache empty-wrap mod)
1443 (loop (cdr bs) er r-cache))
1444 (loop (cdr bs) er-cache r-cache)))))
1445 (set-cdr! r (extend-env labels bindings (cdr r)))
1446 (build-letrec no-source
1447 (map syntax->datum var-ids)
1450 (chi (cdr x) (car x) empty-wrap mod))
1452 (build-sequence no-source
1454 (chi (cdr x) (car x) empty-wrap mod))
1455 (cons (cons er (source-wrap e w s mod))
1456 (cdr body)))))))))))))))))
1458 (define chi-lambda-clause
1459 (lambda (e docstring c r w mod k)
1461 ((args doc e1 e2 ...)
1462 (and (string? (syntax->datum (syntax doc))) (not docstring))
1463 (chi-lambda-clause e (syntax doc) (syntax (args e1 e2 ...)) r w mod k))
1464 (((id ...) e1 e2 ...)
1465 (let ((ids (syntax (id ...))))
1466 (if (not (valid-bound-ids? ids))
1467 (syntax-violation 'lambda "invalid parameter list" e)
1468 (let ((labels (gen-labels ids))
1469 (new-vars (map gen-var ids)))
1470 (k (map syntax->datum ids)
1472 (and docstring (syntax->datum docstring))
1473 (chi-body (syntax (e1 e2 ...))
1475 (extend-var-env labels new-vars r)
1476 (make-binding-wrap ids labels w)
1479 (let ((old-ids (lambda-var-list (syntax ids))))
1480 (if (not (valid-bound-ids? old-ids))
1481 (syntax-violation 'lambda "invalid parameter list" e)
1482 (let ((labels (gen-labels old-ids))
1483 (new-vars (map gen-var old-ids)))
1484 (k (let f ((ls1 (cdr old-ids)) (ls2 (car old-ids)))
1487 (f (cdr ls1) (cons (syntax->datum (car ls1)) ls2))))
1488 (let f ((ls1 (cdr new-vars)) (ls2 (car new-vars)))
1491 (f (cdr ls1) (cons (car ls1) ls2))))
1492 (and docstring (syntax->datum docstring))
1493 (chi-body (syntax (e1 e2 ...))
1495 (extend-var-env labels new-vars r)
1496 (make-binding-wrap old-ids labels w)
1498 (_ (syntax-violation 'lambda "bad lambda" e)))))
1500 (define chi-local-syntax
1501 (lambda (rec? e r w s mod k)
1503 ((_ ((id val) ...) e1 e2 ...)
1504 (let ((ids (syntax (id ...))))
1505 (if (not (valid-bound-ids? ids))
1506 (syntax-violation #f "duplicate bound keyword" e)
1507 (let ((labels (gen-labels ids)))
1508 (let ((new-w (make-binding-wrap ids labels w)))
1509 (k (syntax (e1 e2 ...))
1512 (let ((w (if rec? new-w w))
1513 (trans-r (macros-only-env r)))
1515 (make-binding 'macro
1516 (eval-local-transformer
1517 (chi x trans-r w mod)
1519 (syntax (val ...))))
1524 (_ (syntax-violation #f "bad local syntax definition"
1525 (source-wrap e w s mod))))))
1527 (define eval-local-transformer
1528 (lambda (expanded mod)
1529 (let ((p (local-eval-hook expanded mod)))
1532 (syntax-violation #f "nonprocedure transformer" p)))))
1536 (build-void no-source)))
1540 (and (nonsymbol-id? x)
1541 (free-id=? x (syntax (... ...))))))
1545 ;;; strips syntax-objects down to top-wrap
1547 ;;; since only the head of a list is annotated by the reader, not each pair
1548 ;;; in the spine, we also check for pairs whose cars are annotated in case
1549 ;;; we've been passed the cdr of an annotated list
1558 (strip (syntax-object-expression x) (syntax-object-wrap x)))
1560 (let ((a (f (car x))) (d (f (cdr x))))
1561 (if (and (eq? a (car x)) (eq? d (cdr x)))
1565 (let ((old (vector->list x)))
1566 (let ((new (map f old)))
1567 (if (and-map* eq? old new) x (list->vector new)))))
1570 ;;; lexical variables
1574 (let ((id (if (syntax-object? id) (syntax-object-expression id) id)))
1575 (build-lexical-var no-source id))))
1577 (define lambda-var-list
1579 (let lvl ((vars vars) (ls '()) (w empty-wrap))
1581 ((pair? vars) (lvl (cdr vars) (cons (wrap (car vars) w #f) ls) w))
1582 ((id? vars) (cons (wrap vars w #f) ls))
1584 ((syntax-object? vars)
1585 (lvl (syntax-object-expression vars)
1587 (join-wraps w (syntax-object-wrap vars))))
1588 ; include anything else to be caught by subsequent error
1590 (else (cons vars ls))))))
1592 ;;; core transformers
1594 (global-extend 'local-syntax 'letrec-syntax #t)
1595 (global-extend 'local-syntax 'let-syntax #f)
1597 (global-extend 'core 'fluid-let-syntax
1598 (lambda (e r w s mod)
1600 ((_ ((var val) ...) e1 e2 ...)
1601 (valid-bound-ids? (syntax (var ...)))
1602 (let ((names (map (lambda (x) (id-var-name x w)) (syntax (var ...)))))
1605 (case (binding-type (lookup n r mod))
1606 ((displaced-lexical)
1607 (syntax-violation 'fluid-let-syntax
1608 "identifier out of context"
1610 (source-wrap id w s mod)))))
1614 (syntax (e1 e2 ...))
1615 (source-wrap e w s mod)
1618 (let ((trans-r (macros-only-env r)))
1620 (make-binding 'macro
1621 (eval-local-transformer (chi x trans-r w mod)
1623 (syntax (val ...))))
1627 (_ (syntax-violation 'fluid-let-syntax "bad syntax"
1628 (source-wrap e w s mod))))))
1630 (global-extend 'core 'quote
1631 (lambda (e r w s mod)
1633 ((_ e) (build-data s (strip (syntax e) w)))
1634 (_ (syntax-violation 'quote "bad syntax"
1635 (source-wrap e w s mod))))))
1637 (global-extend 'core 'syntax
1640 (lambda (src e r maps ellipsis? mod)
1642 (let ((label (id-var-name e empty-wrap)))
1643 (let ((b (lookup label r mod)))
1644 (if (eq? (binding-type b) 'syntax)
1647 (let ((var.lev (binding-value b)))
1648 (gen-ref src (car var.lev) (cdr var.lev) maps)))
1649 (lambda (var maps) (values `(ref ,var) maps)))
1651 (syntax-violation 'syntax "misplaced ellipsis" src)
1652 (values `(quote ,e) maps)))))
1655 (ellipsis? (syntax dots))
1656 (gen-syntax src (syntax e) r maps (lambda (x) #f) mod))
1658 ; this could be about a dozen lines of code, except that we
1659 ; choose to handle (syntax (x ... ...)) forms
1660 (ellipsis? (syntax dots))
1661 (let f ((y (syntax y))
1665 (gen-syntax src (syntax x) r
1666 (cons '() maps) ellipsis? mod))
1668 (if (null? (car maps))
1669 (syntax-violation 'syntax "extra ellipsis"
1671 (values (gen-map x (car maps))
1675 (ellipsis? (syntax dots))
1679 (lambda () (k (cons '() maps)))
1681 (if (null? (car maps))
1682 (syntax-violation 'syntax "extra ellipsis" src)
1683 (values (gen-mappend x (car maps))
1685 (_ (call-with-values
1686 (lambda () (gen-syntax src y r maps ellipsis? mod))
1689 (lambda () (k maps))
1691 (values (gen-append x y) maps)))))))))
1694 (lambda () (gen-syntax src (syntax x) r maps ellipsis? mod))
1697 (lambda () (gen-syntax src (syntax y) r maps ellipsis? mod))
1698 (lambda (y maps) (values (gen-cons x y) maps))))))
1702 (gen-syntax src (syntax (e1 e2 ...)) r maps ellipsis? mod))
1703 (lambda (e maps) (values (gen-vector e) maps))))
1704 (_ (values `(quote ,e) maps))))))
1707 (lambda (src var level maps)
1711 (syntax-violation 'syntax "missing ellipsis" src)
1713 (lambda () (gen-ref src var (fx- level 1) (cdr maps)))
1714 (lambda (outer-var outer-maps)
1715 (let ((b (assq outer-var (car maps))))
1717 (values (cdr b) maps)
1718 (let ((inner-var (gen-var 'tmp)))
1720 (cons (cons (cons outer-var inner-var)
1722 outer-maps)))))))))))
1726 `(apply (primitive append) ,(gen-map e map-env))))
1730 (let ((formals (map cdr map-env))
1731 (actuals (map (lambda (x) `(ref ,(car x))) map-env)))
1734 ; identity map equivalence:
1735 ; (map (lambda (x) x) y) == y
1738 (lambda (x) (and (eq? (car x) 'ref) (memq (cadr x) formals)))
1740 ; eta map equivalence:
1741 ; (map (lambda (x ...) (f x ...)) y ...) == (map f y ...)
1742 `(map (primitive ,(car e))
1743 ,@(map (let ((r (map cons formals actuals)))
1744 (lambda (x) (cdr (assq (cadr x) r))))
1746 (else `(map (lambda ,formals ,e) ,@actuals))))))
1752 (if (eq? (car x) 'quote)
1753 `(quote (,(cadr x) . ,(cadr y)))
1754 (if (eq? (cadr y) '())
1757 ((list) `(list ,x ,@(cdr y)))
1758 (else `(cons ,x ,y)))))
1762 (if (equal? y '(quote ()))
1769 ((eq? (car x) 'list) `(vector ,@(cdr x)))
1770 ((eq? (car x) 'quote) `(quote #(,@(cadr x))))
1771 (else `(list->vector ,x)))))
1777 ((ref) (build-lexical-reference 'value no-source (cadr x) (cadr x)))
1778 ((primitive) (build-primref no-source (cadr x)))
1779 ((quote) (build-data no-source (cadr x)))
1780 ((lambda) (build-lambda no-source (cadr x) (cadr x) #f (regen (caddr x))))
1781 (else (build-application no-source
1782 (build-primref no-source (car x))
1783 (map regen (cdr x)))))))
1785 (lambda (e r w s mod)
1786 (let ((e (source-wrap e w s mod)))
1790 (lambda () (gen-syntax e (syntax x) r '() ellipsis? mod))
1791 (lambda (e maps) (regen e))))
1792 (_ (syntax-violation 'syntax "bad `syntax' form" e)))))))
1795 (global-extend 'core 'lambda
1796 (lambda (e r w s mod)
1799 (chi-lambda-clause (source-wrap e w s mod) #f (syntax c) r w mod
1800 (lambda (names vars docstring body)
1801 (build-lambda s names vars docstring body)))))))
1804 (global-extend 'core 'let
1806 (define (chi-let e r w s mod constructor ids vals exps)
1807 (if (not (valid-bound-ids? ids))
1808 (syntax-violation 'let "duplicate bound variable" e)
1809 (let ((labels (gen-labels ids))
1810 (new-vars (map gen-var ids)))
1811 (let ((nw (make-binding-wrap ids labels w))
1812 (nr (extend-var-env labels new-vars r)))
1814 (map syntax->datum ids)
1816 (map (lambda (x) (chi x r w mod)) vals)
1817 (chi-body exps (source-wrap e nw s mod)
1819 (lambda (e r w s mod)
1821 ((_ ((id val) ...) e1 e2 ...)
1822 (and-map id? (syntax (id ...)))
1823 (chi-let e r w s mod
1827 (syntax (e1 e2 ...))))
1828 ((_ f ((id val) ...) e1 e2 ...)
1829 (and (id? (syntax f)) (and-map id? (syntax (id ...))))
1830 (chi-let e r w s mod
1834 (syntax (e1 e2 ...))))
1835 (_ (syntax-violation 'let "bad let" (source-wrap e w s mod)))))))
1838 (global-extend 'core 'letrec
1839 (lambda (e r w s mod)
1841 ((_ ((id val) ...) e1 e2 ...)
1842 (and-map id? (syntax (id ...)))
1843 (let ((ids (syntax (id ...))))
1844 (if (not (valid-bound-ids? ids))
1845 (syntax-violation 'letrec "duplicate bound variable" e)
1846 (let ((labels (gen-labels ids))
1847 (new-vars (map gen-var ids)))
1848 (let ((w (make-binding-wrap ids labels w))
1849 (r (extend-var-env labels new-vars r)))
1851 (map syntax->datum ids)
1853 (map (lambda (x) (chi x r w mod)) (syntax (val ...)))
1854 (chi-body (syntax (e1 e2 ...))
1855 (source-wrap e w s mod) r w mod)))))))
1856 (_ (syntax-violation 'letrec "bad letrec" (source-wrap e w s mod))))))
1859 (global-extend 'core 'set!
1860 (lambda (e r w s mod)
1864 (let ((val (chi (syntax val) r w mod))
1865 (n (id-var-name (syntax id) w)))
1866 (let ((b (lookup n r mod)))
1867 (case (binding-type b)
1869 (build-lexical-assignment s
1870 (syntax->datum (syntax id))
1873 ((global) (build-global-assignment s n val mod))
1874 ((displaced-lexical)
1875 (syntax-violation 'set! "identifier out of context"
1876 (wrap (syntax id) w mod)))
1877 (else (syntax-violation 'set! "bad set!"
1878 (source-wrap e w s mod)))))))
1879 ((_ (head tail ...) val)
1881 (lambda () (syntax-type (syntax head) r empty-wrap no-source #f mod #t))
1882 (lambda (type value ee ww ss modmod)
1885 (let ((val (chi (syntax val) r w mod)))
1886 (call-with-values (lambda () (value (syntax (head tail ...))))
1888 (build-global-assignment s id val mod)))))
1890 (build-application s
1891 (chi (syntax (setter head)) r w mod)
1892 (map (lambda (e) (chi e r w mod))
1893 (syntax (tail ... val)))))))))
1894 (_ (syntax-violation 'set! "bad set!" (source-wrap e w s mod))))))
1896 (global-extend 'module-ref '@
1900 (and (and-map id? (syntax (mod ...))) (id? (syntax id)))
1901 (values (syntax->datum (syntax id))
1903 (syntax (public mod ...))))))))
1905 (global-extend 'module-ref '@@
1909 (and (and-map id? (syntax (mod ...))) (id? (syntax id)))
1910 (values (syntax->datum (syntax id))
1912 (syntax (private mod ...))))))))
1914 (global-extend 'core 'if
1915 (lambda (e r w s mod)
1920 (chi (syntax test) r w mod)
1921 (chi (syntax then) r w mod)
1922 (build-void no-source)))
1926 (chi (syntax test) r w mod)
1927 (chi (syntax then) r w mod)
1928 (chi (syntax else) r w mod))))))
1930 (global-extend 'begin 'begin '())
1932 (global-extend 'define 'define '())
1934 (global-extend 'define-syntax 'define-syntax '())
1936 (global-extend 'eval-when 'eval-when '())
1938 (global-extend 'core 'syntax-case
1940 (define convert-pattern
1941 ; accepts pattern & keys
1942 ; returns $sc-dispatch pattern & ids
1943 (lambda (pattern keys)
1944 (let cvt ((p pattern) (n 0) (ids '()))
1946 (if (bound-id-member? p keys)
1947 (values (vector 'free-id p) ids)
1948 (values 'any (cons (cons p n) ids)))
1951 (ellipsis? (syntax dots))
1953 (lambda () (cvt (syntax x) (fx+ n 1) ids))
1955 (values (if (eq? p 'any) 'each-any (vector 'each p))
1959 (lambda () (cvt (syntax y) n ids))
1962 (lambda () (cvt (syntax x) n ids))
1964 (values (cons x y) ids))))))
1965 (() (values '() ids))
1968 (lambda () (cvt (syntax (x ...)) n ids))
1969 (lambda (p ids) (values (vector 'vector p) ids))))
1970 (x (values (vector 'atom (strip p empty-wrap)) ids)))))))
1972 (define build-dispatch-call
1973 (lambda (pvars exp y r mod)
1974 (let ((ids (map car pvars)) (levels (map cdr pvars)))
1975 (let ((labels (gen-labels ids)) (new-vars (map gen-var ids)))
1976 (build-application no-source
1977 (build-primref no-source 'apply)
1978 (list (build-lambda no-source (map syntax->datum ids) new-vars #f
1982 (map (lambda (var level)
1983 (make-binding 'syntax `(,var . ,level)))
1987 (make-binding-wrap ids labels empty-wrap)
1992 (lambda (x keys clauses r pat fender exp mod)
1994 (lambda () (convert-pattern pat keys))
1997 ((not (distinct-bound-ids? (map car pvars)))
1998 (syntax-violation 'syntax-case "duplicate pattern variable" pat))
1999 ((not (and-map (lambda (x) (not (ellipsis? (car x)))) pvars))
2000 (syntax-violation 'syntax-case "misplaced ellipsis" pat))
2002 (let ((y (gen-var 'tmp)))
2003 ; fat finger binding and references to temp variable y
2004 (build-application no-source
2005 (build-lambda no-source (list 'tmp) (list y) #f
2006 (let ((y (build-lexical-reference 'value no-source
2008 (build-conditional no-source
2009 (syntax-case fender ()
2011 (_ (build-conditional no-source
2013 (build-dispatch-call pvars fender y r mod)
2014 (build-data no-source #f))))
2015 (build-dispatch-call pvars exp y r mod)
2016 (gen-syntax-case x keys clauses r mod))))
2017 (list (if (eq? p 'any)
2018 (build-application no-source
2019 (build-primref no-source 'list)
2021 (build-application no-source
2022 (build-primref no-source '$sc-dispatch)
2023 (list x (build-data no-source p)))))))))))))
2025 (define gen-syntax-case
2026 (lambda (x keys clauses r mod)
2028 (build-application no-source
2029 (build-primref no-source 'syntax-violation)
2030 (list (build-data no-source #f)
2031 (build-data no-source
2032 "source expression failed to match any pattern")
2034 (syntax-case (car clauses) ()
2036 (if (and (id? (syntax pat))
2037 (and-map (lambda (x) (not (free-id=? (syntax pat) x)))
2038 (cons (syntax (... ...)) keys)))
2039 (let ((labels (list (gen-label)))
2040 (var (gen-var (syntax pat))))
2041 (build-application no-source
2042 (build-lambda no-source
2043 (list (syntax->datum (syntax pat))) (list var)
2047 (list (make-binding 'syntax `(,var . 0)))
2049 (make-binding-wrap (syntax (pat))
2053 (gen-clause x keys (cdr clauses) r
2054 (syntax pat) #t (syntax exp) mod)))
2056 (gen-clause x keys (cdr clauses) r
2057 (syntax pat) (syntax fender) (syntax exp) mod))
2058 (_ (syntax-violation 'syntax-case "invalid clause"
2061 (lambda (e r w s mod)
2062 (let ((e (source-wrap e w s mod)))
2064 ((_ val (key ...) m ...)
2065 (if (and-map (lambda (x) (and (id? x) (not (ellipsis? x))))
2067 (let ((x (gen-var 'tmp)))
2068 ; fat finger binding and references to temp variable x
2069 (build-application s
2070 (build-lambda no-source (list 'tmp) (list x) #f
2071 (gen-syntax-case (build-lexical-reference 'value no-source
2073 (syntax (key ...)) (syntax (m ...))
2076 (list (chi (syntax val) r empty-wrap mod))))
2077 (syntax-violation 'syntax-case "invalid literals list" e))))))))
2079 ;;; The portable sc-expand seeds chi-top's mode m with 'e (for
2080 ;;; evaluating) and esew (which stands for "eval syntax expanders
2081 ;;; when") with '(eval). In Chez Scheme, m is set to 'c instead of e
2082 ;;; if we are compiling a file, and esew is set to
2083 ;;; (eval-syntactic-expanders-when), which defaults to the list
2084 ;;; '(compile load eval). This means that, by default, top-level
2085 ;;; syntactic definitions are evaluated immediately after they are
2086 ;;; expanded, and the expanded definitions are also residualized into
2087 ;;; the object file if we are compiling a file.
2090 (if (and (pair? x) (equal? (car x) noexpand))
2092 (let ((m (if (null? rest) 'e (car rest)))
2093 (esew (if (or (null? rest) (null? (cdr rest)))
2096 (with-fluid* *mode* m
2098 (chi-top x null-env top-wrap m esew
2099 (cons 'hygiene (module-name (current-module))))))))))
2107 (make-syntax-object datum (syntax-object-wrap id) #f)))
2110 ; accepts any object, since syntax objects may consist partially
2111 ; or entirely of unwrapped, nonsymbolic data
2113 (strip x empty-wrap)))
2115 (set! generate-temporaries
2117 (arg-check list? ls 'generate-temporaries)
2118 (map (lambda (x) (wrap (gensym-hook) top-wrap #f)) ls)))
2120 (set! free-identifier=?
2122 (arg-check nonsymbol-id? x 'free-identifier=?)
2123 (arg-check nonsymbol-id? y 'free-identifier=?)
2126 (set! bound-identifier=?
2128 (arg-check nonsymbol-id? x 'bound-identifier=?)
2129 (arg-check nonsymbol-id? y 'bound-identifier=?)
2132 (set! syntax-violation
2133 (lambda (who message form . subform)
2134 (arg-check (lambda (x) (or (not x) (string? x) (symbol? x)))
2135 who 'syntax-violation)
2136 (arg-check string? message 'syntax-violation)
2137 (scm-error 'syntax-error 'sc-expand
2141 (if (null? subform) "in ~a" "in subform `~s' of `~s'"))
2142 (let ((tail (cons message
2143 (map (lambda (x) (strip x empty-wrap))
2144 (append subform (list form))))))
2145 (if who (cons who tail) tail))
2148 ;;; $sc-dispatch expects an expression and a pattern. If the expression
2149 ;;; matches the pattern a list of the matching expressions for each
2150 ;;; "any" is returned. Otherwise, #f is returned. (This use of #f will
2151 ;;; not work on r4rs implementations that violate the ieee requirement
2152 ;;; that #f and () be distinct.)
2154 ;;; The expression is matched with the pattern as follows:
2156 ;;; pattern: matches:
2159 ;;; (<pattern>1 . <pattern>2) (<pattern>1 . <pattern>2)
2161 ;;; #(free-id <key>) <key> with free-identifier=?
2162 ;;; #(each <pattern>) (<pattern>*)
2163 ;;; #(vector <pattern>) (list->vector <pattern>)
2164 ;;; #(atom <object>) <object> with "equal?"
2166 ;;; Vector cops out to pair under assumption that vectors are rare. If
2167 ;;; not, should convert to:
2168 ;;; #(vector <pattern>*) #(<pattern>*)
2176 (let ((first (match (car e) p w '() mod)))
2178 (let ((rest (match-each (cdr e) p w mod)))
2179 (and rest (cons first rest))))))
2182 (match-each (syntax-object-expression e)
2184 (join-wraps w (syntax-object-wrap e))
2185 (syntax-object-module e)))
2188 (define match-each-any
2192 (let ((l (match-each-any (cdr e) w mod)))
2193 (and l (cons (wrap (car e) w mod) l))))
2196 (match-each-any (syntax-object-expression e)
2197 (join-wraps w (syntax-object-wrap e))
2205 ((eq? p 'any) (cons '() r))
2206 ((pair? p) (match-empty (car p) (match-empty (cdr p) r)))
2207 ((eq? p 'each-any) (cons '() r))
2209 (case (vector-ref p 0)
2210 ((each) (match-empty (vector-ref p 1) r))
2212 ((vector) (match-empty (vector-ref p 1) r)))))))
2215 (lambda (e p w r mod)
2217 ((null? p) (and (null? e) r))
2219 (and (pair? e) (match (car e) (car p) w
2220 (match (cdr e) (cdr p) w r mod)
2223 (let ((l (match-each-any e w mod))) (and l (cons l r))))
2225 (case (vector-ref p 0)
2228 (match-empty (vector-ref p 1) r)
2229 (let ((l (match-each e (vector-ref p 1) w mod)))
2231 (let collect ((l l))
2234 (cons (map car l) (collect (map cdr l)))))))))
2235 ((free-id) (and (id? e) (free-id=? (wrap e w mod) (vector-ref p 1)) r))
2236 ((atom) (and (equal? (vector-ref p 1) (strip e w)) r))
2239 (match (vector->list e) (vector-ref p 1) w r mod))))))))
2242 (lambda (e p w r mod)
2245 ((eq? p 'any) (cons (wrap e w mod) r))
2248 (syntax-object-expression e)
2250 (join-wraps w (syntax-object-wrap e))
2252 (syntax-object-module e)))
2253 (else (match* e p w r mod)))))
2258 ((eq? p 'any) (list e))
2260 (match* (syntax-object-expression e)
2261 p (syntax-object-wrap e) '() (syntax-object-module e)))
2262 (else (match* e p empty-wrap '() #f)))))
2267 (define-syntax with-syntax
2271 (syntax (begin e1 e2 ...)))
2272 ((_ ((out in)) e1 e2 ...)
2273 (syntax (syntax-case in () (out (begin e1 e2 ...)))))
2274 ((_ ((out in) ...) e1 e2 ...)
2275 (syntax (syntax-case (list in ...) ()
2276 ((out ...) (begin e1 e2 ...))))))))
2278 (define-syntax syntax-rules
2281 ((_ (k ...) ((keyword . pattern) template) ...)
2283 (syntax-case x (k ...)
2284 ((dummy . pattern) (syntax template))
2290 ((let* ((x v) ...) e1 e2 ...)
2291 (and-map identifier? (syntax (x ...)))
2292 (let f ((bindings (syntax ((x v) ...))))
2293 (if (null? bindings)
2294 (syntax (let () e1 e2 ...))
2295 (with-syntax ((body (f (cdr bindings)))
2296 (binding (car bindings)))
2297 (syntax (let (binding) body)))))))))
2301 (syntax-case orig-x ()
2302 ((_ ((var init . step) ...) (e0 e1 ...) c ...)
2303 (with-syntax (((step ...)
2308 (_ (syntax-violation
2309 'do "bad step expression"
2312 (syntax (step ...)))))
2313 (syntax-case (syntax (e1 ...)) ()
2314 (() (syntax (let doloop ((var init) ...)
2316 (begin c ... (doloop step ...))))))
2318 (syntax (let doloop ((var init) ...)
2321 (begin c ... (doloop step ...))))))))))))
2323 (define-syntax quasiquote
2327 (with-syntax ((x x) (y y))
2328 (syntax-case (syntax y) (quote list)
2330 (syntax-case (syntax x) (quote)
2331 ((quote dx) (syntax (quote (dx . dy))))
2332 (_ (if (null? (syntax dy))
2334 (syntax (cons x y))))))
2335 ((list . stuff) (syntax (list x . stuff)))
2336 (else (syntax (cons x y)))))))
2339 (with-syntax ((x x) (y y))
2340 (syntax-case (syntax y) (quote)
2341 ((quote ()) (syntax x))
2342 (_ (syntax (append x y)))))))
2345 (with-syntax ((x x))
2346 (syntax-case (syntax x) (quote list)
2347 ((quote (x ...)) (syntax (quote #(x ...))))
2348 ((list x ...) (syntax (vector x ...)))
2349 (_ (syntax (list->vector x)))))))
2352 (syntax-case p (unquote unquote-splicing quasiquote)
2356 (quasicons (syntax (quote unquote))
2357 (quasi (syntax (p)) (- lev 1)))))
2360 (syntax-violation 'unquote
2361 "unquote takes exactly one argument"
2362 p (syntax (unquote . args))))
2363 (((unquote-splicing p) . q)
2365 (quasiappend (syntax p) (quasi (syntax q) lev))
2366 (quasicons (quasicons (syntax (quote unquote-splicing))
2367 (quasi (syntax (p)) (- lev 1)))
2368 (quasi (syntax q) lev))))
2369 (((unquote-splicing . args) . q)
2371 (syntax-violation 'unquote-splicing
2372 "unquote-splicing takes exactly one argument"
2373 p (syntax (unquote-splicing . args))))
2375 (quasicons (syntax (quote quasiquote))
2376 (quasi (syntax (p)) (+ lev 1))))
2378 (quasicons (quasi (syntax p) lev) (quasi (syntax q) lev)))
2379 (#(x ...) (quasivector (quasi (syntax (x ...)) lev)))
2380 (p (syntax (quote p)))))))
2383 ((_ e) (quasi (syntax e) 0))))))
2385 (define-syntax include
2389 (let ((p (open-input-file fn)))
2390 (let f ((x (read p)))
2392 (begin (close-input-port p) '())
2393 (cons (datum->syntax k x)
2397 (let ((fn (syntax->datum (syntax filename))))
2398 (with-syntax (((exp ...) (read-file fn (syntax k))))
2399 (syntax (begin exp ...))))))))
2401 (define-syntax unquote
2405 (syntax-violation 'unquote
2406 "expression not valid outside of quasiquote"
2409 (define-syntax unquote-splicing
2413 (syntax-violation 'unquote-splicing
2414 "expression not valid outside of quasiquote"
2422 ((body (let f ((clause (syntax m1)) (clauses (syntax (m2 ...))))
2424 (syntax-case clause (else)
2425 ((else e1 e2 ...) (syntax (begin e1 e2 ...)))
2426 (((k ...) e1 e2 ...)
2427 (syntax (if (memv t '(k ...)) (begin e1 e2 ...))))
2428 (_ (syntax-violation 'case "bad clause" x clause)))
2429 (with-syntax ((rest (f (car clauses) (cdr clauses))))
2430 (syntax-case clause (else)
2431 (((k ...) e1 e2 ...)
2432 (syntax (if (memv t '(k ...))
2435 (_ (syntax-violation 'case "bad clause" x
2437 (syntax (let ((t e)) body)))))))
2439 (define-syntax identifier-syntax
2447 (identifier? (syntax id))
2450 (syntax (e x (... ...)))))))))))