home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / share / guile / 1.6 / ice-9 / psyntax.ss < prev    next >
Encoding:
Text File  |  2006-06-19  |  84.1 KB  |  2,238 lines

  1. ;;;; -*-scheme-*-
  2. ;;;;
  3. ;;;;     Copyright (C) 2001, 2003 Free Software Foundation, Inc.
  4. ;;;; 
  5. ;;;; This program is free software; you can redistribute it and/or modify
  6. ;;;; it under the terms of the GNU General Public License as published by
  7. ;;;; the Free Software Foundation; either version 2, or (at your option)
  8. ;;;; any later version.
  9. ;;;; 
  10. ;;;; This program 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
  13. ;;;; GNU General Public License for more details.
  14. ;;;; 
  15. ;;;; You should have received a copy of the GNU General Public License
  16. ;;;; along with this software; see the file COPYING.  If not, write to
  17. ;;;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  18. ;;;; Boston, MA 02110-1301 USA
  19. ;;;;
  20. ;;;; As a special exception, the Free Software Foundation gives permission
  21. ;;;; for additional uses of the text contained in its release of GUILE.
  22. ;;;;
  23. ;;;; The exception is that, if you link the GUILE library with other files
  24. ;;;; to produce an executable, this does not by itself cause the
  25. ;;;; resulting executable to be covered by the GNU General Public License.
  26. ;;;; Your use of that executable is in no way restricted on account of
  27. ;;;; linking the GUILE library code into it.
  28. ;;;;
  29. ;;;; This exception does not however invalidate any other reasons why
  30. ;;;; the executable file might be covered by the GNU General Public License.
  31. ;;;;
  32. ;;;; This exception applies only to the code released by the
  33. ;;;; Free Software Foundation under the name GUILE.  If you copy
  34. ;;;; code from other Free Software Foundation releases into a copy of
  35. ;;;; GUILE, as the General Public License permits, the exception does
  36. ;;;; not apply to the code that you add in this way.  To avoid misleading
  37. ;;;; anyone as to the status of such modified files, you must delete
  38. ;;;; this exception notice from them.
  39. ;;;;
  40. ;;;; If you write modifications of your own for GUILE, it is your choice
  41. ;;;; whether to permit this exception to apply to your modifications.
  42. ;;;; If you do not wish that, delete this exception notice.
  43. ;;;; 
  44.  
  45.  
  46. ;;; Portable implementation of syntax-case
  47. ;;; Extracted from Chez Scheme Version 5.9f
  48. ;;; Authors: R. Kent Dybvig, Oscar Waddell, Bob Hieb, Carl Bruggeman
  49.  
  50. ;;; Modified by Mikael Djurfeldt <djurfeldt@nada.kth.se> according
  51. ;;; to the ChangeLog distributed in the same directory as this file:
  52. ;;; 1997-08-19, 1997-09-03, 1997-09-10, 2000-08-13, 2000-08-24,
  53. ;;; 2000-09-12, 2001-03-08
  54.  
  55. ;;; Copyright (c) 1992-1997 Cadence Research Systems
  56. ;;; Permission to copy this software, in whole or in part, to use this
  57. ;;; software for any lawful purpose, and to redistribute this software
  58. ;;; is granted subject to the restriction that all copies made of this
  59. ;;; software must include this copyright notice in full.  This software
  60. ;;; is provided AS IS, with NO WARRANTY, EITHER EXPRESS OR IMPLIED,
  61. ;;; INCLUDING BUT NOT LIMITED TO IMPLIED WARRANTIES OF MERCHANTABILITY
  62. ;;; OR FITNESS FOR ANY PARTICULAR PURPOSE.  IN NO EVENT SHALL THE
  63. ;;; AUTHORS BE LIABLE FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES OF ANY
  64. ;;; NATURE WHATSOEVER.
  65.  
  66. ;;; Before attempting to port this code to a new implementation of
  67. ;;; Scheme, please read the notes below carefully.
  68.  
  69.  
  70. ;;; This file defines the syntax-case expander, sc-expand, and a set
  71. ;;; of associated syntactic forms and procedures.  Of these, the
  72. ;;; following are documented in The Scheme Programming Language,
  73. ;;; Second Edition (R. Kent Dybvig, Prentice Hall, 1996).  Most are
  74. ;;; also documented in the R4RS and draft R5RS.
  75. ;;;
  76. ;;;   bound-identifier=?
  77. ;;;   datum->syntax-object
  78. ;;;   define-syntax
  79. ;;;   fluid-let-syntax
  80. ;;;   free-identifier=?
  81. ;;;   generate-temporaries
  82. ;;;   identifier?
  83. ;;;   identifier-syntax
  84. ;;;   let-syntax
  85. ;;;   letrec-syntax
  86. ;;;   syntax
  87. ;;;   syntax-case
  88. ;;;   syntax-object->datum
  89. ;;;   syntax-rules
  90. ;;;   with-syntax
  91. ;;;
  92. ;;; All standard Scheme syntactic forms are supported by the expander
  93. ;;; or syntactic abstractions defined in this file.  Only the R4RS
  94. ;;; delay is omitted, since its expansion is implementation-dependent.
  95.  
  96. ;;; The remaining exports are listed below:
  97. ;;;
  98. ;;;   (sc-expand datum)
  99. ;;;      if datum represents a valid expression, sc-expand returns an
  100. ;;;      expanded version of datum in a core language that includes no
  101. ;;;      syntactic abstractions.  The core language includes begin,
  102. ;;;      define, if, lambda, letrec, quote, and set!.
  103. ;;;   (eval-when situations expr ...)
  104. ;;;      conditionally evaluates expr ... at compile-time or run-time
  105. ;;;      depending upon situations (see the Chez Scheme System Manual,
  106. ;;;      Revision 3, for a complete description)
  107. ;;;   (syntax-error object message)
  108. ;;;      used to report errors found during expansion
  109. ;;;   (install-global-transformer symbol value)
  110. ;;;      used by expanded code to install top-level syntactic abstractions
  111. ;;;   (syntax-dispatch e p)
  112. ;;;      used by expanded code to handle syntax-case matching
  113.  
  114. ;;; The following nonstandard procedures must be provided by the
  115. ;;; implementation for this code to run.
  116. ;;;
  117. ;;; (void)
  118. ;;; returns the implementation's cannonical "unspecified value".  This
  119. ;;; usually works: (define void (lambda () (if #f #f))).
  120. ;;;
  121. ;;; (andmap proc list1 list2 ...)
  122. ;;; returns true if proc returns true when applied to each element of list1
  123. ;;; along with the corresponding elements of list2 ....
  124. ;;; The following definition works but does no error checking:
  125. ;;;
  126. ;;; (define andmap
  127. ;;;   (lambda (f first . rest)
  128. ;;;     (or (null? first)
  129. ;;;         (if (null? rest)
  130. ;;;             (let andmap ((first first))
  131. ;;;               (let ((x (car first)) (first (cdr first)))
  132. ;;;                 (if (null? first)
  133. ;;;                     (f x)
  134. ;;;                     (and (f x) (andmap first)))))
  135. ;;;             (let andmap ((first first) (rest rest))
  136. ;;;               (let ((x (car first))
  137. ;;;                     (xr (map car rest))
  138. ;;;                     (first (cdr first))
  139. ;;;                     (rest (map cdr rest)))
  140. ;;;                 (if (null? first)
  141. ;;;                     (apply f (cons x xr))
  142. ;;;                     (and (apply f (cons x xr)) (andmap first rest)))))))))
  143. ;;;
  144. ;;; The following nonstandard procedures must also be provided by the
  145. ;;; implementation for this code to run using the standard portable
  146. ;;; hooks and output constructors.  They are not used by expanded code,
  147. ;;; and so need be present only at expansion time.
  148. ;;;
  149. ;;; (eval x)
  150. ;;; where x is always in the form ("noexpand" expr).
  151. ;;; returns the value of expr.  the "noexpand" flag is used to tell the
  152. ;;; evaluator/expander that no expansion is necessary, since expr has
  153. ;;; already been fully expanded to core forms.
  154. ;;;
  155. ;;; eval will not be invoked during the loading of psyntax.pp.  After
  156. ;;; psyntax.pp has been loaded, the expansion of any macro definition,
  157. ;;; whether local or global, will result in a call to eval.  If, however,
  158. ;;; sc-expand has already been registered as the expander to be used
  159. ;;; by eval, and eval accepts one argument, nothing special must be done
  160. ;;; to support the "noexpand" flag, since it is handled by sc-expand.
  161. ;;;
  162. ;;; (error who format-string why what)
  163. ;;; where who is either a symbol or #f, format-string is always "~a ~s",
  164. ;;; why is always a string, and what may be any object.  error should
  165. ;;; signal an error with a message something like
  166. ;;;
  167. ;;;    "error in <who>: <why> <what>"
  168. ;;;
  169. ;;; (gensym)
  170. ;;; returns a unique symbol each time it's called
  171. ;;;
  172. ;;; (putprop symbol key value)
  173. ;;; (getprop symbol key)
  174. ;;; key is always the symbol *sc-expander*; value may be any object.
  175. ;;; putprop should associate the given value with the given symbol in
  176. ;;; some way that it can be retrieved later with getprop.
  177.  
  178. ;;; When porting to a new Scheme implementation, you should define the
  179. ;;; procedures listed above, load the expanded version of psyntax.ss
  180. ;;; (psyntax.pp, which should be available whereever you found
  181. ;;; psyntax.ss), and register sc-expand as the current expander (how
  182. ;;; you do this depends upon your implementation of Scheme).  You may
  183. ;;; change the hooks and constructors defined toward the beginning of
  184. ;;; the code below, but to avoid bootstrapping problems, do so only
  185. ;;; after you have a working version of the expander.
  186.  
  187. ;;; Chez Scheme allows the syntactic form (syntax <template>) to be
  188. ;;; abbreviated to #'<template>, just as (quote <datum>) may be
  189. ;;; abbreviated to '<datum>.  The #' syntax makes programs written
  190. ;;; using syntax-case shorter and more readable and draws out the
  191. ;;; intuitive connection between syntax and quote.
  192.  
  193. ;;; If you find that this code loads or runs slowly, consider
  194. ;;; switching to faster hardware or a faster implementation of
  195. ;;; Scheme.  In Chez Scheme on a 200Mhz Pentium Pro, expanding,
  196. ;;; compiling (with full optimization), and loading this file takes
  197. ;;; between one and two seconds.
  198.  
  199. ;;; In the expander implementation, we sometimes use syntactic abstractions
  200. ;;; when procedural abstractions would suffice.  For example, we define
  201. ;;; top-wrap and top-marked? as
  202. ;;;   (define-syntax top-wrap (identifier-syntax '((top))))
  203. ;;;   (define-syntax top-marked?
  204. ;;;     (syntax-rules ()
  205. ;;;       ((_ w) (memq 'top (wrap-marks w)))))
  206. ;;; rather than
  207. ;;;   (define top-wrap '((top)))
  208. ;;;   (define top-marked?
  209. ;;;     (lambda (w) (memq 'top (wrap-marks w))))
  210. ;;; On ther other hand, we don't do this consistently; we define make-wrap,
  211. ;;; wrap-marks, and wrap-subst simply as
  212. ;;;   (define make-wrap cons)
  213. ;;;   (define wrap-marks car)
  214. ;;;   (define wrap-subst cdr)
  215. ;;; In Chez Scheme, the syntactic and procedural forms of these
  216. ;;; abstractions are equivalent, since the optimizer consistently
  217. ;;; integrates constants and small procedures.  Some Scheme
  218. ;;; implementations, however, may benefit from more consistent use 
  219. ;;; of one form or the other.
  220.  
  221.  
  222. ;;; implementation information:
  223.  
  224. ;;; "begin" is treated as a splicing construct at top level and at
  225. ;;; the beginning of bodies.  Any sequence of expressions that would
  226. ;;; be allowed where the "begin" occurs is allowed.
  227.  
  228. ;;; "let-syntax" and "letrec-syntax" are also treated as splicing
  229. ;;; constructs, in violation of the R4RS appendix and probably the R5RS
  230. ;;; when it comes out.  A consequence, let-syntax and letrec-syntax do
  231. ;;; not create local contours, as do let and letrec.  Although the
  232. ;;; functionality is greater as it is presently implemented, we will
  233. ;;; probably change it to conform to the R4RS/expected R5RS.
  234.  
  235. ;;; Objects with no standard print syntax, including objects containing
  236. ;;; cycles and syntax object, are allowed in quoted data as long as they
  237. ;;; are contained within a syntax form or produced by datum->syntax-object.
  238. ;;; Such objects are never copied.
  239.  
  240. ;;; All identifiers that don't have macro definitions and are not bound
  241. ;;; lexically are assumed to be global variables
  242.  
  243. ;;; Top-level definitions of macro-introduced identifiers are allowed.
  244. ;;; This may not be appropriate for implementations in which the
  245. ;;; model is that bindings are created by definitions, as opposed to
  246. ;;; one in which initial values are assigned by definitions.
  247.  
  248. ;;; Top-level variable definitions of syntax keywords is not permitted.
  249. ;;; Any solution allowing this would be kludgey and would yield
  250. ;;; surprising results in some cases.  We can provide an undefine-syntax
  251. ;;; form.  The questions is, should define be an implicit undefine-syntax?
  252. ;;; We've decided no for now.
  253.  
  254. ;;; Identifiers and syntax objects are implemented as vectors for
  255. ;;; portability.  As a result, it is possible to "forge" syntax
  256. ;;; objects.
  257.  
  258. ;;; The implementation of generate-temporaries assumes that it is possible
  259. ;;; to generate globally unique symbols (gensyms).
  260.  
  261. ;;; The input to sc-expand may contain "annotations" describing, e.g., the
  262. ;;; source file and character position from where each object was read if
  263. ;;; it was read from a file.  These annotations are handled properly by
  264. ;;; sc-expand only if the annotation? hook (see hooks below) is implemented
  265. ;;; properly and the operators make-annotation, annotation-expression,
  266. ;;; annotation-source, annotation-stripped, and set-annotation-stripped!
  267. ;;; are supplied.  If annotations are supplied, the proper annotation
  268. ;;; source is passed to the various output constructors, allowing
  269. ;;; implementations to accurately correlate source and expanded code.
  270. ;;; Contact one of the authors for details if you wish to make use of
  271. ;;; this feature.
  272.  
  273.  
  274.  
  275. ;;; Bootstrapping:
  276.  
  277. ;;; When changing syntax-object representations, it is necessary to support
  278. ;;; both old and new syntax-object representations in id-var-name.  It
  279. ;;; should be sufficient to recognize old representations and treat
  280. ;;; them as not lexically bound.
  281.  
  282.  
  283.  
  284. (let ()
  285. (define-syntax define-structure
  286.   (lambda (x)
  287.     (define construct-name
  288.       (lambda (template-identifier . args)
  289.         (datum->syntax-object
  290.           template-identifier
  291.           (string->symbol
  292.             (apply string-append
  293.                    (map (lambda (x)
  294.                           (if (string? x)
  295.                               x
  296.                               (symbol->string (syntax-object->datum x))))
  297.                         args))))))
  298.     (syntax-case x ()
  299.       ((_ (name id1 ...))
  300.        (andmap identifier? (syntax (name id1 ...)))
  301.        (with-syntax
  302.          ((constructor (construct-name (syntax name) "make-" (syntax name)))
  303.           (predicate (construct-name (syntax name) (syntax name) "?"))
  304.           ((access ...)
  305.            (map (lambda (x) (construct-name x (syntax name) "-" x))
  306.                 (syntax (id1 ...))))
  307.           ((assign ...)
  308.            (map (lambda (x)
  309.                   (construct-name x "set-" (syntax name) "-" x "!"))
  310.                 (syntax (id1 ...))))
  311.           (structure-length
  312.            (+ (length (syntax (id1 ...))) 1))
  313.           ((index ...)
  314.            (let f ((i 1) (ids (syntax (id1 ...))))
  315.               (if (null? ids)
  316.                   '()
  317.                   (cons i (f (+ i 1) (cdr ids)))))))
  318.          (syntax (begin
  319.                    (define constructor
  320.                      (lambda (id1 ...)
  321.                        (vector 'name id1 ... )))
  322.                    (define predicate
  323.                      (lambda (x)
  324.                        (and (vector? x)
  325.                             (= (vector-length x) structure-length)
  326.                             (eq? (vector-ref x 0) 'name))))
  327.                    (define access
  328.                      (lambda (x)
  329.                        (vector-ref x index)))
  330.                    ...
  331.                    (define assign
  332.                      (lambda (x update)
  333.                        (vector-set! x index update)))
  334.                    ...)))))))
  335.  
  336. (let ()
  337. (define noexpand "noexpand")
  338.  
  339. ;;; hooks to nonportable run-time helpers
  340. (begin
  341. (define fx+ +)
  342. (define fx- -)
  343. (define fx= =)
  344. (define fx< <)
  345.  
  346. (define annotation? (lambda (x) #f))
  347.  
  348. (define top-level-eval-hook
  349.   (lambda (x)
  350.     (eval `(,noexpand ,x) (interaction-environment))))
  351.  
  352. (define local-eval-hook
  353.   (lambda (x)
  354.     (eval `(,noexpand ,x) (interaction-environment))))
  355.  
  356. (define error-hook
  357.   (lambda (who why what)
  358.     (error who "~a ~s" why what)))
  359.  
  360. (define-syntax gensym-hook
  361.   (syntax-rules ()
  362.     ((_) (gensym))))
  363.  
  364. (define put-global-definition-hook
  365.   (lambda (symbol binding)
  366.      (putprop symbol '*sc-expander* binding)))
  367.  
  368. (define get-global-definition-hook
  369.   (lambda (symbol)
  370.      (getprop symbol '*sc-expander*)))
  371. )
  372.  
  373.  
  374. ;;; output constructors
  375. (begin
  376. (define-syntax build-application
  377.   (syntax-rules ()
  378.     ((_ source fun-exp arg-exps)
  379.      `(,fun-exp . ,arg-exps))))
  380.  
  381. (define-syntax build-conditional
  382.   (syntax-rules ()
  383.     ((_ source test-exp then-exp else-exp)
  384.      `(if ,test-exp ,then-exp ,else-exp))))
  385.  
  386. (define-syntax build-lexical-reference
  387.   (syntax-rules ()
  388.     ((_ type source var)
  389.      var)))
  390.  
  391. (define-syntax build-lexical-assignment
  392.   (syntax-rules ()
  393.     ((_ source var exp)
  394.      `(set! ,var ,exp))))
  395.  
  396. (define-syntax build-global-reference
  397.   (syntax-rules ()
  398.     ((_ source var)
  399.      var)))
  400.  
  401. (define-syntax build-global-assignment
  402.   (syntax-rules ()
  403.     ((_ source var exp)
  404.      `(set! ,var ,exp))))
  405.  
  406. (define-syntax build-global-definition
  407.   (syntax-rules ()
  408.     ((_ source var exp)
  409.      `(define ,var ,exp))))
  410.  
  411. (define-syntax build-lambda
  412.   (syntax-rules ()
  413.     ((_ src vars exp)
  414.      `(lambda ,vars ,exp))))
  415.  
  416. (define-syntax build-primref
  417.   (syntax-rules ()
  418.     ((_ src name) name)
  419.     ((_ src level name) name)))
  420.  
  421. (define (build-data src exp)
  422.   (if (and (self-evaluating? exp)
  423.        (not (vector? exp)))
  424.       exp
  425.       (list 'quote exp)))
  426.  
  427. (define build-sequence
  428.   (lambda (src exps)
  429.     (if (null? (cdr exps))
  430.         (car exps)
  431.         `(begin ,@exps))))
  432.  
  433. (define build-let
  434.   (lambda (src vars val-exps body-exp)
  435.     (if (null? vars)
  436.     body-exp
  437.     `(let ,(map list vars val-exps) ,body-exp))))
  438.  
  439. (define build-named-let
  440.   (lambda (src vars val-exps body-exp)
  441.     (if (null? vars)
  442.     body-exp
  443.     `(let ,(car vars) ,(map list (cdr vars) val-exps) ,body-exp))))
  444.  
  445. (define build-letrec
  446.   (lambda (src vars val-exps body-exp)
  447.     (if (null? vars)
  448.         body-exp
  449.         `(letrec ,(map list vars val-exps) ,body-exp))))
  450.  
  451. (define-syntax build-lexical-var
  452.   (syntax-rules ()
  453.     ((_ src id) (gensym (symbol->string id)))))
  454. )
  455.  
  456. (define-structure (syntax-object expression wrap))
  457.  
  458. (define-syntax unannotate
  459.   (syntax-rules ()
  460.     ((_ x)
  461.      (let ((e x))
  462.        (if (annotation? e)
  463.            (annotation-expression e)
  464.            e)))))
  465.  
  466. (define-syntax no-source (identifier-syntax #f))
  467.  
  468. (define source-annotation
  469.   (lambda (x)
  470.      (cond
  471.        ((annotation? x) (annotation-source x))
  472.        ((syntax-object? x) (source-annotation (syntax-object-expression x)))
  473.        (else no-source))))
  474.  
  475. (define-syntax arg-check
  476.   (syntax-rules ()
  477.     ((_ pred? e who)
  478.      (let ((x e))
  479.        (if (not (pred? x)) (error-hook who "invalid argument" x))))))
  480.  
  481. ;;; compile-time environments
  482.  
  483. ;;; wrap and environment comprise two level mapping.
  484. ;;;   wrap : id --> label
  485. ;;;   env : label --> <element>
  486.  
  487. ;;; environments are represented in two parts: a lexical part and a global
  488. ;;; part.  The lexical part is a simple list of associations from labels
  489. ;;; to bindings.  The global part is implemented by
  490. ;;; {put,get}-global-definition-hook and associates symbols with
  491. ;;; bindings.
  492.  
  493. ;;; global (assumed global variable) and displaced-lexical (see below)
  494. ;;; do not show up in any environment; instead, they are fabricated by
  495. ;;; lookup when it finds no other bindings.
  496.  
  497. ;;; <environment>              ::= ((<label> . <binding>)*)
  498.  
  499. ;;; identifier bindings include a type and a value
  500.  
  501. ;;; <binding> ::= (macro . <procedure>)           macros
  502. ;;;               (core . <procedure>)            core forms
  503. ;;;               (external-macro . <procedure>)  external-macro
  504. ;;;               (begin)                         begin
  505. ;;;               (define)                        define
  506. ;;;               (define-syntax)                 define-syntax
  507. ;;;               (local-syntax . rec?)           let-syntax/letrec-syntax
  508. ;;;               (eval-when)                     eval-when
  509. ;;;               (syntax . (<var> . <level>))    pattern variables
  510. ;;;               (global)                        assumed global variable
  511. ;;;               (lexical . <var>)               lexical variables
  512. ;;;               (displaced-lexical)             displaced lexicals
  513. ;;; <level>   ::= <nonnegative integer>
  514. ;;; <var>     ::= variable returned by build-lexical-var
  515.  
  516. ;;; a macro is a user-defined syntactic-form.  a core is a system-defined
  517. ;;; syntactic form.  begin, define, define-syntax, and eval-when are
  518. ;;; treated specially since they are sensitive to whether the form is
  519. ;;; at top-level and (except for eval-when) can denote valid internal
  520. ;;; definitions.
  521.  
  522. ;;; a pattern variable is a variable introduced by syntax-case and can
  523. ;;; be referenced only within a syntax form.
  524.  
  525. ;;; any identifier for which no top-level syntax definition or local
  526. ;;; binding of any kind has been seen is assumed to be a global
  527. ;;; variable.
  528.  
  529. ;;; a lexical variable is a lambda- or letrec-bound variable.
  530.  
  531. ;;; a displaced-lexical identifier is a lexical identifier removed from
  532. ;;; it's scope by the return of a syntax object containing the identifier.
  533. ;;; a displaced lexical can also appear when a letrec-syntax-bound
  534. ;;; keyword is referenced on the rhs of one of the letrec-syntax clauses.
  535. ;;; a displaced lexical should never occur with properly written macros.
  536.  
  537. (define-syntax make-binding
  538.   (syntax-rules (quote)
  539.     ((_ type value) (cons type value))
  540.     ((_ 'type) '(type))
  541.     ((_ type) (cons type '()))))
  542. (define binding-type car)
  543. (define binding-value cdr)
  544.  
  545. (define-syntax null-env (identifier-syntax '()))
  546.  
  547. (define extend-env
  548.   (lambda (labels bindings r) 
  549.     (if (null? labels)
  550.         r
  551.         (extend-env (cdr labels) (cdr bindings)
  552.           (cons (cons (car labels) (car bindings)) r)))))
  553.  
  554. (define extend-var-env
  555.   ; variant of extend-env that forms "lexical" binding
  556.   (lambda (labels vars r)
  557.     (if (null? labels)
  558.         r
  559.         (extend-var-env (cdr labels) (cdr vars)
  560.           (cons (cons (car labels) (make-binding 'lexical (car vars))) r)))))
  561.  
  562. ;;; we use a "macros only" environment in expansion of local macro
  563. ;;; definitions so that their definitions can use local macros without
  564. ;;; attempting to use other lexical identifiers.
  565. (define macros-only-env
  566.   (lambda (r)
  567.     (if (null? r)
  568.         '()
  569.         (let ((a (car r)))
  570.           (if (eq? (cadr a) 'macro)
  571.               (cons a (macros-only-env (cdr r)))
  572.               (macros-only-env (cdr r)))))))
  573.  
  574. (define lookup
  575.   ; x may be a label or a symbol
  576.   ; although symbols are usually global, we check the environment first
  577.   ; anyway because a temporary binding may have been established by
  578.   ; fluid-let-syntax
  579.   (lambda (x r)
  580.     (cond
  581.       ((assq x r) => cdr)
  582.       ((symbol? x)
  583.        (or (get-global-definition-hook x) (make-binding 'global)))
  584.       (else (make-binding 'displaced-lexical)))))
  585.  
  586. (define global-extend
  587.   (lambda (type sym val)
  588.     (put-global-definition-hook sym (make-binding type val))))
  589.  
  590.  
  591. ;;; Conceptually, identifiers are always syntax objects.  Internally,
  592. ;;; however, the wrap is sometimes maintained separately (a source of
  593. ;;; efficiency and confusion), so that symbols are also considered
  594. ;;; identifiers by id?.  Externally, they are always wrapped.
  595.  
  596. (define nonsymbol-id?
  597.   (lambda (x)
  598.     (and (syntax-object? x)
  599.          (symbol? (unannotate (syntax-object-expression x))))))
  600.  
  601. (define id?
  602.   (lambda (x)
  603.     (cond
  604.       ((symbol? x) #t)
  605.       ((syntax-object? x) (symbol? (unannotate (syntax-object-expression x))))
  606.       ((annotation? x) (symbol? (annotation-expression x)))
  607.       (else #f))))
  608.  
  609. (define-syntax id-sym-name
  610.   (syntax-rules ()
  611.     ((_ e)
  612.      (let ((x e))
  613.        (unannotate (if (syntax-object? x) (syntax-object-expression x) x))))))
  614.  
  615. (define id-sym-name&marks
  616.   (lambda (x w)
  617.     (if (syntax-object? x)
  618.         (values
  619.           (unannotate (syntax-object-expression x))
  620.           (join-marks (wrap-marks w) (wrap-marks (syntax-object-wrap x))))
  621.         (values (unannotate x) (wrap-marks w)))))
  622.  
  623. ;;; syntax object wraps
  624.  
  625. ;;;         <wrap> ::= ((<mark> ...) . (<subst> ...))
  626. ;;;        <subst> ::= <shift> | <subs>
  627. ;;;         <subs> ::= #(<old name> <label> (<mark> ...))
  628. ;;;        <shift> ::= positive fixnum
  629.  
  630. (define make-wrap cons)
  631. (define wrap-marks car)
  632. (define wrap-subst cdr)
  633.  
  634. (define-syntax subst-rename? (identifier-syntax vector?))
  635. (define-syntax rename-old (syntax-rules () ((_ x) (vector-ref x 0))))
  636. (define-syntax rename-new (syntax-rules () ((_ x) (vector-ref x 1))))
  637. (define-syntax rename-marks (syntax-rules () ((_ x) (vector-ref x 2))))
  638. (define-syntax make-rename
  639.   (syntax-rules ()
  640.     ((_ old new marks) (vector old new marks))))
  641.  
  642. ;;; labels must be comparable with "eq?" and distinct from symbols.
  643. (define gen-label
  644.   (lambda () (string #\i)))
  645.  
  646. (define gen-labels
  647.   (lambda (ls)
  648.     (if (null? ls)
  649.         '()
  650.         (cons (gen-label) (gen-labels (cdr ls))))))
  651.  
  652. (define-structure (ribcage symnames marks labels))
  653.  
  654. (define-syntax empty-wrap (identifier-syntax '(())))
  655.  
  656. (define-syntax top-wrap (identifier-syntax '((top))))
  657.  
  658. (define-syntax top-marked?
  659.   (syntax-rules ()
  660.     ((_ w) (memq 'top (wrap-marks w)))))
  661.  
  662. ;;; Marks must be comparable with "eq?" and distinct from pairs and
  663. ;;; the symbol top.  We do not use integers so that marks will remain
  664. ;;; unique even across file compiles.
  665.  
  666. (define-syntax the-anti-mark (identifier-syntax #f))
  667.  
  668. (define anti-mark
  669.   (lambda (w)
  670.     (make-wrap (cons the-anti-mark (wrap-marks w))
  671.                (cons 'shift (wrap-subst w)))))
  672.  
  673. (define-syntax new-mark
  674.   (syntax-rules ()
  675.     ((_) (string #\m))))
  676.  
  677. ;;; make-empty-ribcage and extend-ribcage maintain list-based ribcages for
  678. ;;; internal definitions, in which the ribcages are built incrementally
  679. (define-syntax make-empty-ribcage
  680.   (syntax-rules ()
  681.     ((_) (make-ribcage '() '() '()))))
  682.  
  683. (define extend-ribcage!
  684.   ; must receive ids with complete wraps
  685.   (lambda (ribcage id label)
  686.     (set-ribcage-symnames! ribcage
  687.       (cons (unannotate (syntax-object-expression id))
  688.             (ribcage-symnames ribcage)))
  689.     (set-ribcage-marks! ribcage
  690.       (cons (wrap-marks (syntax-object-wrap id))
  691.             (ribcage-marks ribcage)))
  692.     (set-ribcage-labels! ribcage
  693.       (cons label (ribcage-labels ribcage)))))
  694.  
  695. ;;; make-binding-wrap creates vector-based ribcages
  696. (define make-binding-wrap
  697.   (lambda (ids labels w)
  698.     (if (null? ids)
  699.         w
  700.         (make-wrap
  701.           (wrap-marks w)
  702.           (cons
  703.             (let ((labelvec (list->vector labels)))
  704.               (let ((n (vector-length labelvec)))
  705.                 (let ((symnamevec (make-vector n)) (marksvec (make-vector n)))
  706.                   (let f ((ids ids) (i 0))
  707.                     (if (not (null? ids))
  708.                         (call-with-values
  709.                           (lambda () (id-sym-name&marks (car ids) w))
  710.                           (lambda (symname marks)
  711.                             (vector-set! symnamevec i symname)
  712.                             (vector-set! marksvec i marks)
  713.                             (f (cdr ids) (fx+ i 1))))))
  714.                   (make-ribcage symnamevec marksvec labelvec))))
  715.             (wrap-subst w))))))
  716.  
  717. (define smart-append
  718.   (lambda (m1 m2)
  719.     (if (null? m2)
  720.         m1
  721.         (append m1 m2))))
  722.  
  723. (define join-wraps
  724.   (lambda (w1 w2)
  725.     (let ((m1 (wrap-marks w1)) (s1 (wrap-subst w1)))
  726.       (if (null? m1)
  727.           (if (null? s1)
  728.               w2
  729.               (make-wrap
  730.                 (wrap-marks w2)
  731.                 (smart-append s1 (wrap-subst w2))))
  732.           (make-wrap
  733.             (smart-append m1 (wrap-marks w2))
  734.             (smart-append s1 (wrap-subst w2)))))))
  735.  
  736. (define join-marks
  737.   (lambda (m1 m2)
  738.     (smart-append m1 m2)))
  739.  
  740. (define same-marks?
  741.   (lambda (x y)
  742.     (or (eq? x y)
  743.         (and (not (null? x))
  744.              (not (null? y))
  745.              (eq? (car x) (car y))
  746.              (same-marks? (cdr x) (cdr y))))))
  747.  
  748. (define id-var-name
  749.   (lambda (id w)
  750.     (define-syntax first
  751.       (syntax-rules ()
  752.         ((_ e) (call-with-values (lambda () e) (lambda (x . ignore) x)))))
  753.     (define search
  754.       (lambda (sym subst marks)
  755.         (if (null? subst)
  756.             (values #f marks)
  757.             (let ((fst (car subst)))
  758.               (if (eq? fst 'shift)
  759.                   (search sym (cdr subst) (cdr marks))
  760.                   (let ((symnames (ribcage-symnames fst)))
  761.                     (if (vector? symnames)
  762.                         (search-vector-rib sym subst marks symnames fst)
  763.                         (search-list-rib sym subst marks symnames fst))))))))
  764.     (define search-list-rib
  765.       (lambda (sym subst marks symnames ribcage)
  766.         (let f ((symnames symnames) (i 0))
  767.           (cond
  768.             ((null? symnames) (search sym (cdr subst) marks))
  769.             ((and (eq? (car symnames) sym)
  770.                   (same-marks? marks (list-ref (ribcage-marks ribcage) i)))
  771.              (values (list-ref (ribcage-labels ribcage) i) marks))
  772.             (else (f (cdr symnames) (fx+ i 1)))))))
  773.     (define search-vector-rib
  774.       (lambda (sym subst marks symnames ribcage)
  775.         (let ((n (vector-length symnames)))
  776.           (let f ((i 0))
  777.             (cond
  778.               ((fx= i n) (search sym (cdr subst) marks))
  779.               ((and (eq? (vector-ref symnames i) sym)
  780.                     (same-marks? marks (vector-ref (ribcage-marks ribcage) i)))
  781.                (values (vector-ref (ribcage-labels ribcage) i) marks))
  782.               (else (f (fx+ i 1))))))))
  783.     (cond
  784.       ((symbol? id)
  785.        (or (first (search id (wrap-subst w) (wrap-marks w))) id))
  786.       ((syntax-object? id)
  787.         (let ((id (unannotate (syntax-object-expression id)))
  788.               (w1 (syntax-object-wrap id)))
  789.           (let ((marks (join-marks (wrap-marks w) (wrap-marks w1))))
  790.             (call-with-values (lambda () (search id (wrap-subst w) marks))
  791.               (lambda (new-id marks)
  792.                 (or new-id
  793.                     (first (search id (wrap-subst w1) marks))
  794.                     id))))))
  795.       ((annotation? id)
  796.        (let ((id (unannotate id)))
  797.          (or (first (search id (wrap-subst w) (wrap-marks w))) id)))
  798.       (else (error-hook 'id-var-name "invalid id" id)))))
  799.  
  800. ;;; free-id=? must be passed fully wrapped ids since (free-id=? x y)
  801. ;;; may be true even if (free-id=? (wrap x w) (wrap y w)) is not.
  802.  
  803. (define free-id=?
  804.   (lambda (i j)
  805.     (and (eq? (id-sym-name i) (id-sym-name j)) ; accelerator
  806.          (eq? (id-var-name i empty-wrap) (id-var-name j empty-wrap)))))
  807.  
  808. ;;; bound-id=? may be passed unwrapped (or partially wrapped) ids as
  809. ;;; long as the missing portion of the wrap is common to both of the ids
  810. ;;; since (bound-id=? x y) iff (bound-id=? (wrap x w) (wrap y w))
  811.  
  812. (define bound-id=?
  813.   (lambda (i j)
  814.     (if (and (syntax-object? i) (syntax-object? j))
  815.         (and (eq? (unannotate (syntax-object-expression i))
  816.                   (unannotate (syntax-object-expression j)))
  817.              (same-marks? (wrap-marks (syntax-object-wrap i))
  818.                   (wrap-marks (syntax-object-wrap j))))
  819.         (eq? (unannotate i) (unannotate j)))))
  820.  
  821. ;;; "valid-bound-ids?" returns #t if it receives a list of distinct ids.
  822. ;;; valid-bound-ids? may be passed unwrapped (or partially wrapped) ids
  823. ;;; as long as the missing portion of the wrap is common to all of the
  824. ;;; ids.
  825.  
  826. (define valid-bound-ids?
  827.   (lambda (ids)
  828.      (and (let all-ids? ((ids ids))
  829.             (or (null? ids)
  830.                 (and (id? (car ids))
  831.                      (all-ids? (cdr ids)))))
  832.           (distinct-bound-ids? ids))))
  833.  
  834. ;;; distinct-bound-ids? expects a list of ids and returns #t if there are
  835. ;;; no duplicates.  It is quadratic on the length of the id list; long
  836. ;;; lists could be sorted to make it more efficient.  distinct-bound-ids?
  837. ;;; may be passed unwrapped (or partially wrapped) ids as long as the
  838. ;;; missing portion of the wrap is common to all of the ids.
  839.  
  840. (define distinct-bound-ids?
  841.   (lambda (ids)
  842.     (let distinct? ((ids ids))
  843.       (or (null? ids)
  844.           (and (not (bound-id-member? (car ids) (cdr ids)))
  845.                (distinct? (cdr ids)))))))
  846.  
  847. (define bound-id-member?
  848.    (lambda (x list)
  849.       (and (not (null? list))
  850.            (or (bound-id=? x (car list))
  851.                (bound-id-member? x (cdr list))))))
  852.  
  853. ;;; wrapping expressions and identifiers
  854.  
  855. (define wrap
  856.   (lambda (x w)
  857.     (cond
  858.       ((and (null? (wrap-marks w)) (null? (wrap-subst w))) x)
  859.       ((syntax-object? x)
  860.        (make-syntax-object
  861.          (syntax-object-expression x)
  862.          (join-wraps w (syntax-object-wrap x))))
  863.       ((null? x) x)
  864.       (else (make-syntax-object x w)))))
  865.  
  866. (define source-wrap
  867.   (lambda (x w s)
  868.     (wrap (if s (make-annotation x s #f) x) w)))
  869.  
  870. ;;; expanding
  871.  
  872. (define chi-sequence
  873.   (lambda (body r w s)
  874.     (build-sequence s
  875.       (let dobody ((body body) (r r) (w w))
  876.         (if (null? body)
  877.             '()
  878.             (let ((first (chi (car body) r w)))
  879.               (cons first (dobody (cdr body) r w))))))))
  880.  
  881. (define chi-top-sequence
  882.   (lambda (body r w s m esew)
  883.     (build-sequence s
  884.       (let dobody ((body body) (r r) (w w) (m m) (esew esew))
  885.         (if (null? body)
  886.             '()
  887.             (let ((first (chi-top (car body) r w m esew)))
  888.               (cons first (dobody (cdr body) r w m esew))))))))
  889.  
  890. (define chi-install-global
  891.   (lambda (name e)
  892.     (build-application no-source
  893.       (build-primref no-source 'install-global-transformer)
  894.       (list (build-data no-source name) e))))
  895.  
  896. (define chi-when-list
  897.   (lambda (e when-list w)
  898.     ; when-list is syntax'd version of list of situations
  899.     (let f ((when-list when-list) (situations '()))
  900.       (if (null? when-list)
  901.           situations
  902.           (f (cdr when-list)
  903.              (cons (let ((x (car when-list)))
  904.                      (cond
  905.                        ((free-id=? x (syntax compile)) 'compile)
  906.                        ((free-id=? x (syntax load)) 'load)
  907.                        ((free-id=? x (syntax eval)) 'eval)
  908.                        (else (syntax-error (wrap x w)
  909.                                "invalid eval-when situation"))))
  910.                    situations))))))
  911.  
  912. ;;; syntax-type returns five values: type, value, e, w, and s.  The first
  913. ;;; two are described in the table below.
  914. ;;;
  915. ;;;    type                   value         explanation
  916. ;;;    -------------------------------------------------------------------
  917. ;;;    core                   procedure     core form (including singleton)
  918. ;;;    external-macro         procedure     external macro
  919. ;;;    lexical                name          lexical variable reference
  920. ;;;    global                 name          global variable reference
  921. ;;;    begin                  none          begin keyword
  922. ;;;    define                 none          define keyword
  923. ;;;    define-syntax          none          define-syntax keyword
  924. ;;;    local-syntax           rec?          letrec-syntax/let-syntax keyword
  925. ;;;    eval-when              none          eval-when keyword
  926. ;;;    syntax                 level         pattern variable
  927. ;;;    displaced-lexical      none          displaced lexical identifier
  928. ;;;    lexical-call           name          call to lexical variable
  929. ;;;    global-call            name          call to global variable
  930. ;;;    call                   none          any other call
  931. ;;;    begin-form             none          begin expression
  932. ;;;    define-form            id            variable definition
  933. ;;;    define-syntax-form     id            syntax definition
  934. ;;;    local-syntax-form      rec?          syntax definition
  935. ;;;    eval-when-form         none          eval-when form
  936. ;;;    constant               none          self-evaluating datum
  937. ;;;    other                  none          anything else
  938. ;;;
  939. ;;; For define-form and define-syntax-form, e is the rhs expression.
  940. ;;; For all others, e is the entire form.  w is the wrap for e.
  941. ;;; s is the source for the entire form.
  942. ;;;
  943. ;;; syntax-type expands macros and unwraps as necessary to get to
  944. ;;; one of the forms above.  It also parses define and define-syntax
  945. ;;; forms, although perhaps this should be done by the consumer.
  946.  
  947. (define syntax-type
  948.   (lambda (e r w s rib)
  949.     (cond
  950.       ((symbol? e)
  951.        (let* ((n (id-var-name e w))
  952.               (b (lookup n r))
  953.               (type (binding-type b)))
  954.          (case type
  955.            ((lexical) (values type (binding-value b) e w s))
  956.            ((global) (values type n e w s))
  957.            ((macro)
  958.             (syntax-type (chi-macro (binding-value b) e r w rib) r empty-wrap s rib))
  959.            (else (values type (binding-value b) e w s)))))
  960.       ((pair? e)
  961.        (let ((first (car e)))
  962.          (if (id? first)
  963.              (let* ((n (id-var-name first w))
  964.                     (b (lookup n r))
  965.                     (type (binding-type b)))
  966.                (case type
  967.                  ((lexical) (values 'lexical-call (binding-value b) e w s))
  968.                  ((global) (values 'global-call n e w s))
  969.                  ((macro)
  970.                   (syntax-type (chi-macro (binding-value b) e r w rib)
  971.                     r empty-wrap s rib))
  972.                  ((core external-macro) (values type (binding-value b) e w s))
  973.                  ((local-syntax)
  974.                   (values 'local-syntax-form (binding-value b) e w s))
  975.                  ((begin) (values 'begin-form #f e w s))
  976.                  ((eval-when) (values 'eval-when-form #f e w s))
  977.                  ((define)
  978.                   (syntax-case e ()
  979.                     ((_ name val)
  980.                      (id? (syntax name))
  981.                      (values 'define-form (syntax name) (syntax val) w s))
  982.                     ((_ (name . args) e1 e2 ...)
  983.                      (and (id? (syntax name))
  984.                           (valid-bound-ids? (lambda-var-list (syntax args))))
  985.                      ; need lambda here...
  986.                      (values 'define-form (wrap (syntax name) w)
  987.                        (cons (syntax lambda) (wrap (syntax (args e1 e2 ...)) w))
  988.                        empty-wrap s))
  989.                     ((_ name)
  990.                      (id? (syntax name))
  991.                      (values 'define-form (wrap (syntax name) w)
  992.                        (syntax (void))
  993.                        empty-wrap s))))
  994.                  ((define-syntax)
  995.                   (syntax-case e ()
  996.                     ((_ name val)
  997.                      (id? (syntax name))
  998.                      (values 'define-syntax-form (syntax name)
  999.                        (syntax val) w s))))
  1000.                  (else (values 'call #f e w s))))
  1001.              (values 'call #f e w s))))
  1002.       ((syntax-object? e)
  1003.        ;; s can't be valid source if we've unwrapped
  1004.        (syntax-type (syntax-object-expression e)
  1005.                     r
  1006.                     (join-wraps w (syntax-object-wrap e))
  1007.                     no-source rib))
  1008.       ((annotation? e)
  1009.        (syntax-type (annotation-expression e) r w (annotation-source e) rib))
  1010.       ((self-evaluating? e) (values 'constant #f e w s))
  1011.       (else (values 'other #f e w s)))))
  1012.  
  1013. (define chi-top
  1014.   (lambda (e r w m esew)
  1015.     (define-syntax eval-if-c&e
  1016.       (syntax-rules ()
  1017.         ((_ m e)
  1018.          (let ((x e))
  1019.            (if (eq? m 'c&e) (top-level-eval-hook x))
  1020.            x))))
  1021.     (call-with-values
  1022.       (lambda () (syntax-type e r w no-source #f))
  1023.       (lambda (type value e w s)
  1024.         (case type
  1025.           ((begin-form)
  1026.            (syntax-case e ()
  1027.              ((_) (chi-void))
  1028.              ((_ e1 e2 ...)
  1029.               (chi-top-sequence (syntax (e1 e2 ...)) r w s m esew))))
  1030.           ((local-syntax-form)
  1031.            (chi-local-syntax value e r w s
  1032.              (lambda (body r w s)
  1033.                (chi-top-sequence body r w s m esew))))
  1034.           ((eval-when-form)
  1035.            (syntax-case e ()
  1036.              ((_ (x ...) e1 e2 ...)
  1037.               (let ((when-list (chi-when-list e (syntax (x ...)) w))
  1038.                     (body (syntax (e1 e2 ...))))
  1039.                 (cond
  1040.                   ((eq? m 'e)
  1041.                    (if (memq 'eval when-list)
  1042.                        (chi-top-sequence body r w s 'e '(eval))
  1043.                        (chi-void)))
  1044.                   ((memq 'load when-list)
  1045.                    (if (or (memq 'compile when-list)
  1046.                            (and (eq? m 'c&e) (memq 'eval when-list)))
  1047.                        (chi-top-sequence body r w s 'c&e '(compile load))
  1048.                        (if (memq m '(c c&e))
  1049.                            (chi-top-sequence body r w s 'c '(load))
  1050.                            (chi-void))))
  1051.                   ((or (memq 'compile when-list)
  1052.                        (and (eq? m 'c&e) (memq 'eval when-list)))
  1053.                    (top-level-eval-hook
  1054.                      (chi-top-sequence body r w s 'e '(eval)))
  1055.                    (chi-void))
  1056.                   (else (chi-void)))))))
  1057.           ((define-syntax-form)
  1058.            (let ((n (id-var-name value w)) (r (macros-only-env r)))
  1059.              (case m
  1060.                ((c)
  1061.                 (if (memq 'compile esew)
  1062.                     (let ((e (chi-install-global n (chi e r w))))
  1063.                       (top-level-eval-hook e)
  1064.                       (if (memq 'load esew) e (chi-void)))
  1065.                     (if (memq 'load esew)
  1066.                         (chi-install-global n (chi e r w))
  1067.                         (chi-void))))
  1068.                ((c&e)
  1069.                 (let ((e (chi-install-global n (chi e r w))))
  1070.                   (top-level-eval-hook e)
  1071.                   e))
  1072.                (else
  1073.                 (if (memq 'eval esew)
  1074.                     (top-level-eval-hook
  1075.                       (chi-install-global n (chi e r w))))
  1076.                 (chi-void)))))
  1077.           ((define-form)
  1078.            (let* ((n (id-var-name value w))
  1079.           (type (binding-type (lookup n r))))
  1080.              (case type
  1081.                ((global)
  1082.                 (eval-if-c&e m
  1083.                   (build-global-definition s n (chi e r w))))
  1084.                ((displaced-lexical)
  1085.                 (syntax-error (wrap value w) "identifier out of context"))
  1086.                (else
  1087.         (if (eq? type 'external-macro)
  1088.             (eval-if-c&e m
  1089.                  (build-global-definition s n (chi e r w)))
  1090.             (syntax-error (wrap value w)
  1091.                   "cannot define keyword at top level"))))))
  1092.           (else (eval-if-c&e m (chi-expr type value e r w s))))))))
  1093.  
  1094. (define chi
  1095.   (lambda (e r w)
  1096.     (call-with-values
  1097.       (lambda () (syntax-type e r w no-source #f))
  1098.       (lambda (type value e w s)
  1099.         (chi-expr type value e r w s)))))
  1100.  
  1101. (define chi-expr
  1102.   (lambda (type value e r w s)
  1103.     (case type
  1104.       ((lexical)
  1105.        (build-lexical-reference 'value s value))
  1106.       ((core external-macro) (value e r w s))
  1107.       ((lexical-call)
  1108.        (chi-application
  1109.          (build-lexical-reference 'fun (source-annotation (car e)) value)
  1110.          e r w s))
  1111.       ((global-call)
  1112.        (chi-application
  1113.          (build-global-reference (source-annotation (car e)) value)
  1114.          e r w s))
  1115.       ((constant) (build-data s (strip (source-wrap e w s) empty-wrap)))
  1116.       ((global) (build-global-reference s value))
  1117.       ((call) (chi-application (chi (car e) r w) e r w s))
  1118.       ((begin-form)
  1119.        (syntax-case e ()
  1120.          ((_ e1 e2 ...) (chi-sequence (syntax (e1 e2 ...)) r w s))))
  1121.       ((local-syntax-form)
  1122.        (chi-local-syntax value e r w s chi-sequence))
  1123.       ((eval-when-form)
  1124.        (syntax-case e ()
  1125.          ((_ (x ...) e1 e2 ...)
  1126.           (let ((when-list (chi-when-list e (syntax (x ...)) w)))
  1127.             (if (memq 'eval when-list)
  1128.                 (chi-sequence (syntax (e1 e2 ...)) r w s)
  1129.                 (chi-void))))))
  1130.       ((define-form define-syntax-form)
  1131.        (syntax-error (wrap value w) "invalid context for definition of"))
  1132.       ((syntax)
  1133.        (syntax-error (source-wrap e w s)
  1134.          "reference to pattern variable outside syntax form"))
  1135.       ((displaced-lexical)
  1136.        (syntax-error (source-wrap e w s)
  1137.          "reference to identifier outside its scope"))
  1138.       (else (syntax-error (source-wrap e w s))))))
  1139.  
  1140. (define chi-application
  1141.   (lambda (x e r w s)
  1142.     (syntax-case e ()
  1143.       ((e0 e1 ...)
  1144.        (build-application s x
  1145.          (map (lambda (e) (chi e r w)) (syntax (e1 ...))))))))
  1146.  
  1147. (define chi-macro
  1148.   (lambda (p e r w rib)
  1149.     (define rebuild-macro-output
  1150.       (lambda (x m)
  1151.         (cond ((pair? x)
  1152.                (cons (rebuild-macro-output (car x) m)
  1153.                      (rebuild-macro-output (cdr x) m)))
  1154.               ((syntax-object? x)
  1155.                (let ((w (syntax-object-wrap x)))
  1156.                  (let ((ms (wrap-marks w)) (s (wrap-subst w)))
  1157.                    (make-syntax-object (syntax-object-expression x)
  1158.                      (if (and (pair? ms) (eq? (car ms) the-anti-mark))
  1159.                          (make-wrap (cdr ms)
  1160.                            (if rib (cons rib (cdr s)) (cdr s)))
  1161.                          (make-wrap (cons m ms)
  1162.                            (if rib
  1163.                                (cons rib (cons 'shift s))
  1164.                                (cons 'shift s))))))))
  1165.               ((vector? x)
  1166.                (let* ((n (vector-length x)) (v (make-vector n)))
  1167.                  (do ((i 0 (fx+ i 1)))
  1168.                      ((fx= i n) v)
  1169.                      (vector-set! v i
  1170.                        (rebuild-macro-output (vector-ref x i) m)))))
  1171.               ((symbol? x)
  1172.                (syntax-error x "encountered raw symbol in macro output"))
  1173.               (else x))))
  1174.     (rebuild-macro-output (p (wrap e (anti-mark w))) (new-mark))))
  1175.  
  1176. (define chi-body
  1177.   ;; In processing the forms of the body, we create a new, empty wrap.
  1178.   ;; This wrap is augmented (destructively) each time we discover that
  1179.   ;; the next form is a definition.  This is done:
  1180.   ;;
  1181.   ;;   (1) to allow the first nondefinition form to be a call to
  1182.   ;;       one of the defined ids even if the id previously denoted a
  1183.   ;;       definition keyword or keyword for a macro expanding into a
  1184.   ;;       definition;
  1185.   ;;   (2) to prevent subsequent definition forms (but unfortunately
  1186.   ;;       not earlier ones) and the first nondefinition form from
  1187.   ;;       confusing one of the bound identifiers for an auxiliary
  1188.   ;;       keyword; and
  1189.   ;;   (3) so that we do not need to restart the expansion of the
  1190.   ;;       first nondefinition form, which is problematic anyway
  1191.   ;;       since it might be the first element of a begin that we
  1192.   ;;       have just spliced into the body (meaning if we restarted,
  1193.   ;;       we'd really need to restart with the begin or the macro
  1194.   ;;       call that expanded into the begin, and we'd have to give
  1195.   ;;       up allowing (begin <defn>+ <expr>+), which is itself
  1196.   ;;       problematic since we don't know if a begin contains only
  1197.   ;;       definitions until we've expanded it).
  1198.   ;;
  1199.   ;; Before processing the body, we also create a new environment
  1200.   ;; containing a placeholder for the bindings we will add later and
  1201.   ;; associate this environment with each form.  In processing a
  1202.   ;; let-syntax or letrec-syntax, the associated environment may be
  1203.   ;; augmented with local keyword bindings, so the environment may
  1204.   ;; be different for different forms in the body.  Once we have
  1205.   ;; gathered up all of the definitions, we evaluate the transformer
  1206.   ;; expressions and splice into r at the placeholder the new variable
  1207.   ;; and keyword bindings.  This allows let-syntax or letrec-syntax
  1208.   ;; forms local to a portion or all of the body to shadow the
  1209.   ;; definition bindings.
  1210.   ;;
  1211.   ;; Subforms of a begin, let-syntax, or letrec-syntax are spliced
  1212.   ;; into the body.
  1213.   ;;
  1214.   ;; outer-form is fully wrapped w/source
  1215.   (lambda (body outer-form r w)
  1216.     (let* ((r (cons '("placeholder" . (placeholder)) r))
  1217.            (ribcage (make-empty-ribcage))
  1218.            (w (make-wrap (wrap-marks w) (cons ribcage (wrap-subst w)))))
  1219.       (let parse ((body (map (lambda (x) (cons r (wrap x w))) body))
  1220.                   (ids '()) (labels '()) (vars '()) (vals '()) (bindings '()))
  1221.         (if (null? body)
  1222.             (syntax-error outer-form "no expressions in body")
  1223.             (let ((e (cdar body)) (er (caar body)))
  1224.               (call-with-values
  1225.                 (lambda () (syntax-type e er empty-wrap no-source ribcage))
  1226.                 (lambda (type value e w s)
  1227.                   (case type
  1228.                     ((define-form)
  1229.                      (let ((id (wrap value w)) (label (gen-label)))
  1230.                        (let ((var (gen-var id)))
  1231.                          (extend-ribcage! ribcage id label)
  1232.                          (parse (cdr body)
  1233.                            (cons id ids) (cons label labels)
  1234.                            (cons var vars) (cons (cons er (wrap e w)) vals)
  1235.                            (cons (make-binding 'lexical var) bindings)))))
  1236.                     ((define-syntax-form)
  1237.                      (let ((id (wrap value w)) (label (gen-label)))
  1238.                        (extend-ribcage! ribcage id label)
  1239.                        (parse (cdr body)
  1240.                          (cons id ids) (cons label labels)
  1241.                          vars vals
  1242.                          (cons (make-binding 'macro (cons er (wrap e w)))
  1243.                                bindings))))
  1244.                     ((begin-form)
  1245.                      (syntax-case e ()
  1246.                        ((_ e1 ...)
  1247.                         (parse (let f ((forms (syntax (e1 ...))))
  1248.                                  (if (null? forms)
  1249.                                      (cdr body)
  1250.                                      (cons (cons er (wrap (car forms) w))
  1251.                                            (f (cdr forms)))))
  1252.                           ids labels vars vals bindings))))
  1253.                     ((local-syntax-form)
  1254.                      (chi-local-syntax value e er w s
  1255.                        (lambda (forms er w s)
  1256.                          (parse (let f ((forms forms))
  1257.                                   (if (null? forms)
  1258.                                       (cdr body)
  1259.                                       (cons (cons er (wrap (car forms) w))
  1260.                                             (f (cdr forms)))))
  1261.                            ids labels vars vals bindings))))
  1262.                     (else ; found a non-definition
  1263.                      (if (null? ids)
  1264.                          (build-sequence no-source
  1265.                            (map (lambda (x)
  1266.                                   (chi (cdr x) (car x) empty-wrap))
  1267.                                 (cons (cons er (source-wrap e w s))
  1268.                                       (cdr body))))
  1269.                          (begin
  1270.                            (if (not (valid-bound-ids? ids))
  1271.                                (syntax-error outer-form
  1272.                                  "invalid or duplicate identifier in definition"))
  1273.                            (let loop ((bs bindings) (er-cache #f) (r-cache #f))
  1274.                              (if (not (null? bs))
  1275.                                  (let* ((b (car bs)))
  1276.                                    (if (eq? (car b) 'macro)
  1277.                                        (let* ((er (cadr b))
  1278.                                               (r-cache
  1279.                                                 (if (eq? er er-cache)
  1280.                                                     r-cache
  1281.                                                     (macros-only-env er))))
  1282.                                          (set-cdr! b
  1283.                                            (eval-local-transformer
  1284.                                              (chi (cddr b) r-cache empty-wrap)))
  1285.                                          (loop (cdr bs) er r-cache))
  1286.                                        (loop (cdr bs) er-cache r-cache)))))
  1287.                            (set-cdr! r (extend-env labels bindings (cdr r)))
  1288.                            (build-letrec no-source
  1289.                              vars
  1290.                              (map (lambda (x)
  1291.                                     (chi (cdr x) (car x) empty-wrap))
  1292.                                   vals)
  1293.                              (build-sequence no-source
  1294.                                (map (lambda (x)
  1295.                                       (chi (cdr x) (car x) empty-wrap))
  1296.                                     (cons (cons er (source-wrap e w s))
  1297.                                           (cdr body)))))))))))))))))
  1298.  
  1299. (define chi-lambda-clause
  1300.   (lambda (e c r w k)
  1301.     (syntax-case c ()
  1302.       (((id ...) e1 e2 ...)
  1303.        (let ((ids (syntax (id ...))))
  1304.          (if (not (valid-bound-ids? ids))
  1305.              (syntax-error e "invalid parameter list in")
  1306.              (let ((labels (gen-labels ids))
  1307.                    (new-vars (map gen-var ids)))
  1308.                (k new-vars
  1309.                   (chi-body (syntax (e1 e2 ...))
  1310.                             e
  1311.                             (extend-var-env labels new-vars r)
  1312.                             (make-binding-wrap ids labels w)))))))
  1313.       ((ids e1 e2 ...)
  1314.        (let ((old-ids (lambda-var-list (syntax ids))))
  1315.          (if (not (valid-bound-ids? old-ids))
  1316.              (syntax-error e "invalid parameter list in")
  1317.              (let ((labels (gen-labels old-ids))
  1318.                    (new-vars (map gen-var old-ids)))
  1319.                (k (let f ((ls1 (cdr new-vars)) (ls2 (car new-vars)))
  1320.                     (if (null? ls1)
  1321.                         ls2
  1322.                         (f (cdr ls1) (cons (car ls1) ls2))))
  1323.                   (chi-body (syntax (e1 e2 ...))
  1324.                             e
  1325.                             (extend-var-env labels new-vars r)
  1326.                             (make-binding-wrap old-ids labels w)))))))
  1327.       (_ (syntax-error e)))))
  1328.  
  1329. (define chi-local-syntax
  1330.   (lambda (rec? e r w s k)
  1331.     (syntax-case e ()
  1332.       ((_ ((id val) ...) e1 e2 ...)
  1333.        (let ((ids (syntax (id ...))))
  1334.          (if (not (valid-bound-ids? ids))
  1335.              (syntax-error e "duplicate bound keyword in")
  1336.              (let ((labels (gen-labels ids)))
  1337.                (let ((new-w (make-binding-wrap ids labels w)))
  1338.                  (k (syntax (e1 e2 ...))
  1339.                     (extend-env
  1340.                       labels
  1341.                       (let ((w (if rec? new-w w))
  1342.                             (trans-r (macros-only-env r)))
  1343.                         (map (lambda (x)
  1344.                                (make-binding 'macro
  1345.                                  (eval-local-transformer (chi x trans-r w))))
  1346.                              (syntax (val ...))))
  1347.                       r)
  1348.                     new-w
  1349.                     s))))))
  1350.       (_ (syntax-error (source-wrap e w s))))))
  1351.  
  1352. (define eval-local-transformer
  1353.   (lambda (expanded)
  1354.     (let ((p (local-eval-hook expanded)))
  1355.       (if (procedure? p)
  1356.           p
  1357.           (syntax-error p "nonprocedure transformer")))))
  1358.  
  1359. (define chi-void
  1360.   (lambda ()
  1361.     (build-application no-source (build-primref no-source 'void) '())))
  1362.  
  1363. (define ellipsis?
  1364.   (lambda (x)
  1365.     (and (nonsymbol-id? x)
  1366.          (free-id=? x (syntax (... ...))))))
  1367.  
  1368. ;;; data
  1369.  
  1370. ;;; strips all annotations from potentially circular reader output
  1371.  
  1372. (define strip-annotation
  1373.   (lambda (x parent)
  1374.     (cond
  1375.       ((pair? x)
  1376.        (let ((new (cons #f #f)))
  1377.          (when parent (set-annotation-stripped! parent new))
  1378.          (set-car! new (strip-annotation (car x) #f))
  1379.          (set-cdr! new (strip-annotation (cdr x) #f))
  1380.          new))
  1381.       ((annotation? x)
  1382.        (or (annotation-stripped x)
  1383.            (strip-annotation (annotation-expression x) x)))
  1384.       ((vector? x)
  1385.        (let ((new (make-vector (vector-length x))))
  1386.          (when parent (set-annotation-stripped! parent new))
  1387.          (let loop ((i (- (vector-length x) 1)))
  1388.            (unless (fx< i 0)
  1389.              (vector-set! new i (strip-annotation (vector-ref x i) #f))
  1390.              (loop (fx- i 1))))
  1391.          new))
  1392.       (else x))))
  1393.  
  1394. ;;; strips syntax-objects down to top-wrap; if top-wrap is layered directly
  1395. ;;; on an annotation, strips the annotation as well.
  1396. ;;; since only the head of a list is annotated by the reader, not each pair
  1397. ;;; in the spine, we also check for pairs whose cars are annotated in case
  1398. ;;; we've been passed the cdr of an annotated list
  1399.  
  1400. (define strip
  1401.   (lambda (x w)
  1402.     (if (top-marked? w)
  1403.         (if (or (annotation? x) (and (pair? x) (annotation? (car x))))
  1404.             (strip-annotation x #f)
  1405.             x)
  1406.         (let f ((x x))
  1407.           (cond
  1408.             ((syntax-object? x)
  1409.              (strip (syntax-object-expression x) (syntax-object-wrap x)))
  1410.             ((pair? x)
  1411.              (let ((a (f (car x))) (d (f (cdr x))))
  1412.                (if (and (eq? a (car x)) (eq? d (cdr x)))
  1413.                    x
  1414.                    (cons a d))))
  1415.             ((vector? x)
  1416.              (let ((old (vector->list x)))
  1417.                 (let ((new (map f old)))
  1418.                    (if (andmap eq? old new) x (list->vector new)))))
  1419.             (else x))))))
  1420.  
  1421. ;;; lexical variables
  1422.  
  1423. (define gen-var
  1424.   (lambda (id)
  1425.     (let ((id (if (syntax-object? id) (syntax-object-expression id) id)))
  1426.       (if (annotation? id)
  1427.           (build-lexical-var (annotation-source id) (annotation-expression id))
  1428.           (build-lexical-var no-source id)))))
  1429.  
  1430. (define lambda-var-list
  1431.   (lambda (vars)
  1432.     (let lvl ((vars vars) (ls '()) (w empty-wrap))
  1433.        (cond
  1434.          ((pair? vars) (lvl (cdr vars) (cons (wrap (car vars) w) ls) w))
  1435.          ((id? vars) (cons (wrap vars w) ls))
  1436.          ((null? vars) ls)
  1437.          ((syntax-object? vars)
  1438.           (lvl (syntax-object-expression vars)
  1439.                ls
  1440.                (join-wraps w (syntax-object-wrap vars))))
  1441.          ((annotation? vars)
  1442.           (lvl (annotation-expression vars) ls w))
  1443.        ; include anything else to be caught by subsequent error
  1444.        ; checking
  1445.          (else (cons vars ls))))))
  1446.  
  1447. ;;; core transformers
  1448.  
  1449. (global-extend 'local-syntax 'letrec-syntax #t)
  1450. (global-extend 'local-syntax 'let-syntax #f)
  1451.  
  1452. (global-extend 'core 'fluid-let-syntax
  1453.   (lambda (e r w s)
  1454.     (syntax-case e ()
  1455.       ((_ ((var val) ...) e1 e2 ...)
  1456.        (valid-bound-ids? (syntax (var ...)))
  1457.        (let ((names (map (lambda (x) (id-var-name x w)) (syntax (var ...)))))
  1458.          (for-each
  1459.            (lambda (id n)
  1460.              (case (binding-type (lookup n r))
  1461.                ((displaced-lexical)
  1462.                 (syntax-error (source-wrap id w s)
  1463.                   "identifier out of context"))))
  1464.            (syntax (var ...))
  1465.            names)
  1466.          (chi-body
  1467.            (syntax (e1 e2 ...))
  1468.            (source-wrap e w s)
  1469.            (extend-env
  1470.              names
  1471.              (let ((trans-r (macros-only-env r)))
  1472.                (map (lambda (x)
  1473.                       (make-binding 'macro
  1474.                         (eval-local-transformer (chi x trans-r w))))
  1475.                     (syntax (val ...))))
  1476.              r)
  1477.            w)))
  1478.       (_ (syntax-error (source-wrap e w s))))))
  1479.  
  1480. (global-extend 'core 'quote
  1481.    (lambda (e r w s)
  1482.       (syntax-case e ()
  1483.          ((_ e) (build-data s (strip (syntax e) w)))
  1484.          (_ (syntax-error (source-wrap e w s))))))
  1485.  
  1486. (global-extend 'core 'syntax
  1487.   (let ()
  1488.     (define gen-syntax
  1489.       (lambda (src e r maps ellipsis?)
  1490.         (if (id? e)
  1491.             (let ((label (id-var-name e empty-wrap)))
  1492.               (let ((b (lookup label r)))
  1493.                 (if (eq? (binding-type b) 'syntax)
  1494.                     (call-with-values
  1495.                       (lambda ()
  1496.                         (let ((var.lev (binding-value b)))
  1497.                           (gen-ref src (car var.lev) (cdr var.lev) maps)))
  1498.                       (lambda (var maps) (values `(ref ,var) maps)))
  1499.                     (if (ellipsis? e)
  1500.                         (syntax-error src "misplaced ellipsis in syntax form")
  1501.                         (values `(quote ,e) maps)))))
  1502.             (syntax-case e ()
  1503.               ((dots e)
  1504.                (ellipsis? (syntax dots))
  1505.                (gen-syntax src (syntax e) r maps (lambda (x) #f)))
  1506.               ((x dots . y)
  1507.                ; this could be about a dozen lines of code, except that we
  1508.                ; choose to handle (syntax (x ... ...)) forms
  1509.                (ellipsis? (syntax dots))
  1510.                (let f ((y (syntax y))
  1511.                        (k (lambda (maps)
  1512.                             (call-with-values
  1513.                               (lambda ()
  1514.                                 (gen-syntax src (syntax x) r
  1515.                                   (cons '() maps) ellipsis?))
  1516.                               (lambda (x maps)
  1517.                                 (if (null? (car maps))
  1518.                                     (syntax-error src
  1519.                                       "extra ellipsis in syntax form")
  1520.                                     (values (gen-map x (car maps))
  1521.                                             (cdr maps))))))))
  1522.                  (syntax-case y ()
  1523.                    ((dots . y)
  1524.                     (ellipsis? (syntax dots))
  1525.                     (f (syntax y)
  1526.                        (lambda (maps)
  1527.                          (call-with-values
  1528.                            (lambda () (k (cons '() maps)))
  1529.                            (lambda (x maps)
  1530.                              (if (null? (car maps))
  1531.                                  (syntax-error src
  1532.                                    "extra ellipsis in syntax form")
  1533.                                  (values (gen-mappend x (car maps))
  1534.                                          (cdr maps))))))))
  1535.                    (_ (call-with-values
  1536.                         (lambda () (gen-syntax src y r maps ellipsis?))
  1537.                         (lambda (y maps)
  1538.                           (call-with-values
  1539.                             (lambda () (k maps))
  1540.                             (lambda (x maps)
  1541.                               (values (gen-append x y) maps)))))))))
  1542.               ((x . y)
  1543.                (call-with-values
  1544.                  (lambda () (gen-syntax src (syntax x) r maps ellipsis?))
  1545.                  (lambda (x maps)
  1546.                    (call-with-values
  1547.                      (lambda () (gen-syntax src (syntax y) r maps ellipsis?))
  1548.                      (lambda (y maps) (values (gen-cons x y) maps))))))
  1549.               (#(e1 e2 ...)
  1550.                (call-with-values
  1551.                  (lambda ()
  1552.                    (gen-syntax src (syntax (e1 e2 ...)) r maps ellipsis?))
  1553.                  (lambda (e maps) (values (gen-vector e) maps))))
  1554.               (_ (values `(quote ,e) maps))))))
  1555.  
  1556.     (define gen-ref
  1557.       (lambda (src var level maps)
  1558.         (if (fx= level 0)
  1559.             (values var maps)
  1560.             (if (null? maps)
  1561.                 (syntax-error src "missing ellipsis in syntax form")
  1562.                 (call-with-values
  1563.                   (lambda () (gen-ref src var (fx- level 1) (cdr maps)))
  1564.                   (lambda (outer-var outer-maps)
  1565.                     (let ((b (assq outer-var (car maps))))
  1566.                       (if b
  1567.                           (values (cdr b) maps)
  1568.                           (let ((inner-var (gen-var 'tmp)))
  1569.                             (values inner-var
  1570.                                     (cons (cons (cons outer-var inner-var)
  1571.                                                 (car maps))
  1572.                                           outer-maps)))))))))))
  1573.  
  1574.     (define gen-mappend
  1575.       (lambda (e map-env)
  1576.         `(apply (primitive append) ,(gen-map e map-env))))
  1577.  
  1578.     (define gen-map
  1579.       (lambda (e map-env)
  1580.         (let ((formals (map cdr map-env))
  1581.               (actuals (map (lambda (x) `(ref ,(car x))) map-env)))
  1582.           (cond
  1583.             ((eq? (car e) 'ref)
  1584.              ; identity map equivalence:
  1585.              ; (map (lambda (x) x) y) == y
  1586.              (car actuals))
  1587.             ((andmap
  1588.                 (lambda (x) (and (eq? (car x) 'ref) (memq (cadr x) formals)))
  1589.                 (cdr e))
  1590.              ; eta map equivalence:
  1591.              ; (map (lambda (x ...) (f x ...)) y ...) == (map f y ...)
  1592.              `(map (primitive ,(car e))
  1593.                    ,@(map (let ((r (map cons formals actuals)))
  1594.                             (lambda (x) (cdr (assq (cadr x) r))))
  1595.                           (cdr e))))
  1596.             (else `(map (lambda ,formals ,e) ,@actuals))))))
  1597.  
  1598.     (define gen-cons
  1599.       (lambda (x y)
  1600.         (case (car y)
  1601.           ((quote)
  1602.            (if (eq? (car x) 'quote)
  1603.                `(quote (,(cadr x) . ,(cadr y)))
  1604.                (if (eq? (cadr y) '())
  1605.                    `(list ,x)
  1606.                    `(cons ,x ,y))))
  1607.           ((list) `(list ,x ,@(cdr y)))
  1608.           (else `(cons ,x ,y)))))
  1609.  
  1610.     (define gen-append
  1611.       (lambda (x y)
  1612.         (if (equal? y '(quote ()))
  1613.             x
  1614.             `(append ,x ,y))))
  1615.  
  1616.     (define gen-vector
  1617.       (lambda (x)
  1618.         (cond
  1619.           ((eq? (car x) 'list) `(vector ,@(cdr x)))
  1620.           ((eq? (car x) 'quote) `(quote #(,@(cadr x))))
  1621.           (else `(list->vector ,x)))))
  1622.  
  1623.  
  1624.     (define regen
  1625.       (lambda (x)
  1626.         (case (car x)
  1627.           ((ref) (build-lexical-reference 'value no-source (cadr x)))
  1628.           ((primitive) (build-primref no-source (cadr x)))
  1629.           ((quote) (build-data no-source (cadr x)))
  1630.           ((lambda) (build-lambda no-source (cadr x) (regen (caddr x))))
  1631.           ((map) (let ((ls (map regen (cdr x))))
  1632.                    (build-application no-source
  1633.                      (if (fx= (length ls) 2)
  1634.                          (build-primref no-source 'map)
  1635.                         ; really need to do our own checking here
  1636.                          (build-primref no-source 2 'map)) ; require error check
  1637.                      ls)))
  1638.           (else (build-application no-source
  1639.                   (build-primref no-source (car x))
  1640.                   (map regen (cdr x)))))))
  1641.  
  1642.     (lambda (e r w s)
  1643.       (let ((e (source-wrap e w s)))
  1644.         (syntax-case e ()
  1645.           ((_ x)
  1646.            (call-with-values
  1647.              (lambda () (gen-syntax e (syntax x) r '() ellipsis?))
  1648.              (lambda (e maps) (regen e))))
  1649.           (_ (syntax-error e)))))))
  1650.  
  1651.  
  1652. (global-extend 'core 'lambda
  1653.    (lambda (e r w s)
  1654.       (syntax-case e ()
  1655.          ((_ . c)
  1656.           (chi-lambda-clause (source-wrap e w s) (syntax c) r w
  1657.             (lambda (vars body) (build-lambda s vars body)))))))
  1658.  
  1659.  
  1660. (global-extend 'core 'let
  1661.   (let ()
  1662.     (define (chi-let e r w s constructor ids vals exps)
  1663.       (if (not (valid-bound-ids? ids))
  1664.       (syntax-error e "duplicate bound variable in")
  1665.       (let ((labels (gen-labels ids))
  1666.         (new-vars (map gen-var ids)))
  1667.         (let ((nw (make-binding-wrap ids labels w))
  1668.           (nr (extend-var-env labels new-vars r)))
  1669.           (constructor s
  1670.                new-vars
  1671.                (map (lambda (x) (chi x r w)) vals)
  1672.                (chi-body exps (source-wrap e nw s) nr nw))))))
  1673.     (lambda (e r w s)
  1674.       (syntax-case e ()
  1675.     ((_ ((id val) ...) e1 e2 ...)
  1676.      (chi-let e r w s
  1677.           build-let
  1678.           (syntax (id ...))
  1679.           (syntax (val ...))
  1680.           (syntax (e1 e2 ...))))
  1681.     ((_ f ((id val) ...) e1 e2 ...)
  1682.      (id? (syntax f))
  1683.      (chi-let e r w s
  1684.           build-named-let
  1685.           (syntax (f id ...))
  1686.           (syntax (val ...))
  1687.           (syntax (e1 e2 ...))))
  1688.     (_ (syntax-error (source-wrap e w s)))))))
  1689.  
  1690.  
  1691. (global-extend 'core 'letrec
  1692.   (lambda (e r w s)
  1693.     (syntax-case e ()
  1694.       ((_ ((id val) ...) e1 e2 ...)
  1695.        (let ((ids (syntax (id ...))))
  1696.          (if (not (valid-bound-ids? ids))
  1697.              (syntax-error e "duplicate bound variable in")
  1698.              (let ((labels (gen-labels ids))
  1699.                    (new-vars (map gen-var ids)))
  1700.                (let ((w (make-binding-wrap ids labels w))
  1701.                     (r (extend-var-env labels new-vars r)))
  1702.                  (build-letrec s
  1703.                    new-vars
  1704.                    (map (lambda (x) (chi x r w)) (syntax (val ...)))
  1705.                    (chi-body (syntax (e1 e2 ...)) (source-wrap e w s) r w)))))))
  1706.       (_ (syntax-error (source-wrap e w s))))))
  1707.  
  1708.  
  1709. (global-extend 'core 'set!
  1710.   (lambda (e r w s)
  1711.     (syntax-case e ()
  1712.       ((_ id val)
  1713.        (id? (syntax id))
  1714.        (let ((val (chi (syntax val) r w))
  1715.              (n (id-var-name (syntax id) w)))
  1716.          (let ((b (lookup n r)))
  1717.            (case (binding-type b)
  1718.              ((lexical)
  1719.               (build-lexical-assignment s (binding-value b) val))
  1720.              ((global) (build-global-assignment s n val))
  1721.              ((displaced-lexical)
  1722.               (syntax-error (wrap (syntax id) w)
  1723.                 "identifier out of context"))
  1724.              (else (syntax-error (source-wrap e w s)))))))
  1725.       ((_ (getter arg ...) val)
  1726.        (build-application s
  1727.               (chi (syntax (setter getter)) r w)
  1728.               (map (lambda (e) (chi e r w))
  1729.                    (syntax (arg ... val)))))
  1730.       (_ (syntax-error (source-wrap e w s))))))
  1731.  
  1732. (global-extend 'begin 'begin '())
  1733.  
  1734. (global-extend 'define 'define '())
  1735.  
  1736. (global-extend 'define-syntax 'define-syntax '())
  1737.  
  1738. (global-extend 'eval-when 'eval-when '())
  1739.  
  1740. (global-extend 'core 'syntax-case
  1741.   (let ()
  1742.     (define convert-pattern
  1743.       ; accepts pattern & keys
  1744.       ; returns syntax-dispatch pattern & ids
  1745.       (lambda (pattern keys)
  1746.         (let cvt ((p pattern) (n 0) (ids '()))
  1747.           (if (id? p)
  1748.               (if (bound-id-member? p keys)
  1749.                   (values (vector 'free-id p) ids)
  1750.                   (values 'any (cons (cons p n) ids)))
  1751.               (syntax-case p ()
  1752.                 ((x dots)
  1753.                  (ellipsis? (syntax dots))
  1754.                  (call-with-values
  1755.                    (lambda () (cvt (syntax x) (fx+ n 1) ids))
  1756.                    (lambda (p ids)
  1757.                      (values (if (eq? p 'any) 'each-any (vector 'each p))
  1758.                              ids))))
  1759.                 ((x . y)
  1760.                  (call-with-values
  1761.                    (lambda () (cvt (syntax y) n ids))
  1762.                    (lambda (y ids)
  1763.                      (call-with-values
  1764.                        (lambda () (cvt (syntax x) n ids))
  1765.                        (lambda (x ids)
  1766.                          (values (cons x y) ids))))))
  1767.                 (() (values '() ids))
  1768.                 (#(x ...)
  1769.                  (call-with-values
  1770.                    (lambda () (cvt (syntax (x ...)) n ids))
  1771.                    (lambda (p ids) (values (vector 'vector p) ids))))
  1772.                 (x (values (vector 'atom (strip p empty-wrap)) ids)))))))
  1773.  
  1774.     (define build-dispatch-call
  1775.       (lambda (pvars exp y r)
  1776.         (let ((ids (map car pvars)) (levels (map cdr pvars)))
  1777.           (let ((labels (gen-labels ids)) (new-vars (map gen-var ids)))
  1778.             (build-application no-source
  1779.               (build-primref no-source 'apply)
  1780.               (list (build-lambda no-source new-vars
  1781.                       (chi exp
  1782.                          (extend-env
  1783.                              labels
  1784.                              (map (lambda (var level)
  1785.                                     (make-binding 'syntax `(,var . ,level)))
  1786.                                   new-vars
  1787.                                   (map cdr pvars))
  1788.                              r)
  1789.                            (make-binding-wrap ids labels empty-wrap)))
  1790.                     y))))))
  1791.  
  1792.     (define gen-clause
  1793.       (lambda (x keys clauses r pat fender exp)
  1794.         (call-with-values
  1795.           (lambda () (convert-pattern pat keys))
  1796.           (lambda (p pvars)
  1797.             (cond
  1798.               ((not (distinct-bound-ids? (map car pvars)))
  1799.                (syntax-error pat
  1800.                  "duplicate pattern variable in syntax-case pattern"))
  1801.               ((not (andmap (lambda (x) (not (ellipsis? (car x)))) pvars))
  1802.                (syntax-error pat
  1803.                  "misplaced ellipsis in syntax-case pattern"))
  1804.               (else
  1805.                (let ((y (gen-var 'tmp)))
  1806.                  ; fat finger binding and references to temp variable y
  1807.                  (build-application no-source
  1808.                    (build-lambda no-source (list y)
  1809.                      (let ((y (build-lexical-reference 'value no-source y)))
  1810.                        (build-conditional no-source
  1811.                          (syntax-case fender ()
  1812.                            (#t y)
  1813.                            (_ (build-conditional no-source
  1814.                                 y
  1815.                                 (build-dispatch-call pvars fender y r)
  1816.                                 (build-data no-source #f))))
  1817.                          (build-dispatch-call pvars exp y r)
  1818.                          (gen-syntax-case x keys clauses r))))
  1819.                    (list (if (eq? p 'any)
  1820.                              (build-application no-source
  1821.                                (build-primref no-source 'list)
  1822.                                (list x))
  1823.                              (build-application no-source
  1824.                                (build-primref no-source 'syntax-dispatch)
  1825.                                (list x (build-data no-source p)))))))))))))
  1826.  
  1827.     (define gen-syntax-case
  1828.       (lambda (x keys clauses r)
  1829.         (if (null? clauses)
  1830.             (build-application no-source
  1831.               (build-primref no-source 'syntax-error)
  1832.               (list x))
  1833.             (syntax-case (car clauses) ()
  1834.               ((pat exp)
  1835.                (if (and (id? (syntax pat))
  1836.                         (andmap (lambda (x) (not (free-id=? (syntax pat) x)))
  1837.                           (cons (syntax (... ...)) keys)))
  1838.                    (let ((labels (list (gen-label)))
  1839.                          (var (gen-var (syntax pat))))
  1840.                      (build-application no-source
  1841.                        (build-lambda no-source (list var)
  1842.                          (chi (syntax exp)
  1843.                               (extend-env labels
  1844.                                 (list (make-binding 'syntax `(,var . 0)))
  1845.                                 r)
  1846.                               (make-binding-wrap (syntax (pat))
  1847.                                 labels empty-wrap)))
  1848.                        (list x)))
  1849.                    (gen-clause x keys (cdr clauses) r
  1850.                      (syntax pat) #t (syntax exp))))
  1851.               ((pat fender exp)
  1852.                (gen-clause x keys (cdr clauses) r
  1853.                  (syntax pat) (syntax fender) (syntax exp)))
  1854.               (_ (syntax-error (car clauses) "invalid syntax-case clause"))))))
  1855.  
  1856.     (lambda (e r w s)
  1857.       (let ((e (source-wrap e w s)))
  1858.         (syntax-case e ()
  1859.           ((_ val (key ...) m ...)
  1860.            (if (andmap (lambda (x) (and (id? x) (not (ellipsis? x))))
  1861.                        (syntax (key ...)))
  1862.                (let ((x (gen-var 'tmp)))
  1863.                  ; fat finger binding and references to temp variable x
  1864.                  (build-application s
  1865.                    (build-lambda no-source (list x)
  1866.                      (gen-syntax-case (build-lexical-reference 'value no-source x)
  1867.                        (syntax (key ...)) (syntax (m ...))
  1868.                        r))
  1869.                    (list (chi (syntax val) r empty-wrap))))
  1870.                (syntax-error e "invalid literals list in"))))))))
  1871.  
  1872. ;;; The portable sc-expand seeds chi-top's mode m with 'e (for
  1873. ;;; evaluating) and esew (which stands for "eval syntax expanders
  1874. ;;; when") with '(eval).  In Chez Scheme, m is set to 'c instead of e
  1875. ;;; if we are compiling a file, and esew is set to
  1876. ;;; (eval-syntactic-expanders-when), which defaults to the list
  1877. ;;; '(compile load eval).  This means that, by default, top-level
  1878. ;;; syntactic definitions are evaluated immediately after they are
  1879. ;;; expanded, and the expanded definitions are also residualized into
  1880. ;;; the object file if we are compiling a file.
  1881. (set! sc-expand
  1882.   (let ((m 'e) (esew '(eval)))
  1883.     (lambda (x)
  1884.       (if (and (pair? x) (equal? (car x) noexpand))
  1885.           (cadr x)
  1886.           (chi-top x null-env top-wrap m esew)))))
  1887.  
  1888. (set! sc-expand3
  1889.   (let ((m 'e) (esew '(eval)))
  1890.     (lambda (x . rest)
  1891.       (if (and (pair? x) (equal? (car x) noexpand))
  1892.           (cadr x)
  1893.           (chi-top x
  1894.            null-env
  1895.            top-wrap
  1896.            (if (null? rest) m (car rest))
  1897.            (if (or (null? rest) (null? (cdr rest)))
  1898.                esew
  1899.                (cadr rest)))))))
  1900.  
  1901. (set! identifier?
  1902.   (lambda (x)
  1903.     (nonsymbol-id? x)))
  1904.  
  1905. (set! datum->syntax-object
  1906.   (lambda (id datum)
  1907.     (make-syntax-object datum (syntax-object-wrap id))))
  1908.  
  1909. (set! syntax-object->datum
  1910.   ; accepts any object, since syntax objects may consist partially
  1911.   ; or entirely of unwrapped, nonsymbolic data
  1912.   (lambda (x)
  1913.     (strip x empty-wrap)))
  1914.  
  1915. (set! generate-temporaries
  1916.   (lambda (ls)
  1917.     (arg-check list? ls 'generate-temporaries)
  1918.     (map (lambda (x) (wrap (gensym-hook) top-wrap)) ls)))
  1919.  
  1920. (set! free-identifier=?
  1921.    (lambda (x y)
  1922.       (arg-check nonsymbol-id? x 'free-identifier=?)
  1923.       (arg-check nonsymbol-id? y 'free-identifier=?)
  1924.       (free-id=? x y)))
  1925.  
  1926. (set! bound-identifier=?
  1927.    (lambda (x y)
  1928.       (arg-check nonsymbol-id? x 'bound-identifier=?)
  1929.       (arg-check nonsymbol-id? y 'bound-identifier=?)
  1930.       (bound-id=? x y)))
  1931.  
  1932. (set! syntax-error
  1933.   (lambda (object . messages)
  1934.     (for-each (lambda (x) (arg-check string? x 'syntax-error)) messages)
  1935.     (let ((message (if (null? messages)
  1936.                        "invalid syntax"
  1937.                        (apply string-append messages))))
  1938.       (error-hook #f message (strip object empty-wrap)))))
  1939.  
  1940. (set! install-global-transformer
  1941.   (lambda (sym v)
  1942.     (arg-check symbol? sym 'define-syntax)
  1943.     (arg-check procedure? v 'define-syntax)
  1944.     (global-extend 'macro sym v)))
  1945.  
  1946. ;;; syntax-dispatch expects an expression and a pattern.  If the expression
  1947. ;;; matches the pattern a list of the matching expressions for each
  1948. ;;; "any" is returned.  Otherwise, #f is returned.  (This use of #f will
  1949. ;;; not work on r4rs implementations that violate the ieee requirement
  1950. ;;; that #f and () be distinct.)
  1951.  
  1952. ;;; The expression is matched with the pattern as follows:
  1953.  
  1954. ;;; pattern:                           matches:
  1955. ;;;   ()                                 empty list
  1956. ;;;   any                                anything
  1957. ;;;   (<pattern>1 . <pattern>2)          (<pattern>1 . <pattern>2)
  1958. ;;;   each-any                           (any*)
  1959. ;;;   #(free-id <key>)                   <key> with free-identifier=?
  1960. ;;;   #(each <pattern>)                  (<pattern>*)
  1961. ;;;   #(vector <pattern>)                (list->vector <pattern>)
  1962. ;;;   #(atom <object>)                   <object> with "equal?"
  1963.  
  1964. ;;; Vector cops out to pair under assumption that vectors are rare.  If
  1965. ;;; not, should convert to:
  1966. ;;;   #(vector <pattern>*)               #(<pattern>*)
  1967.  
  1968. (let ()
  1969.  
  1970. (define match-each
  1971.   (lambda (e p w)
  1972.     (cond
  1973.       ((annotation? e)
  1974.        (match-each (annotation-expression e) p w))
  1975.       ((pair? e)
  1976.        (let ((first (match (car e) p w '())))
  1977.          (and first
  1978.               (let ((rest (match-each (cdr e) p w)))
  1979.                  (and rest (cons first rest))))))
  1980.       ((null? e) '())
  1981.       ((syntax-object? e)
  1982.        (match-each (syntax-object-expression e)
  1983.                    p
  1984.                    (join-wraps w (syntax-object-wrap e))))
  1985.       (else #f))))
  1986.  
  1987. (define match-each-any
  1988.   (lambda (e w)
  1989.     (cond
  1990.       ((annotation? e)
  1991.        (match-each-any (annotation-expression e) w))
  1992.       ((pair? e)
  1993.        (let ((l (match-each-any (cdr e) w)))
  1994.          (and l (cons (wrap (car e) w) l))))
  1995.       ((null? e) '())
  1996.       ((syntax-object? e)
  1997.        (match-each-any (syntax-object-expression e)
  1998.                        (join-wraps w (syntax-object-wrap e))))
  1999.       (else #f))))
  2000.  
  2001. (define match-empty
  2002.   (lambda (p r)
  2003.     (cond
  2004.       ((null? p) r)
  2005.       ((eq? p 'any) (cons '() r))
  2006.       ((pair? p) (match-empty (car p) (match-empty (cdr p) r)))
  2007.       ((eq? p 'each-any) (cons '() r))
  2008.       (else
  2009.        (case (vector-ref p 0)
  2010.          ((each) (match-empty (vector-ref p 1) r))
  2011.          ((free-id atom) r)
  2012.          ((vector) (match-empty (vector-ref p 1) r)))))))
  2013.  
  2014. (define match*
  2015.   (lambda (e p w r)
  2016.     (cond
  2017.       ((null? p) (and (null? e) r))
  2018.       ((pair? p)
  2019.        (and (pair? e) (match (car e) (car p) w
  2020.                         (match (cdr e) (cdr p) w r))))
  2021.       ((eq? p 'each-any)
  2022.        (let ((l (match-each-any e w))) (and l (cons l r))))
  2023.       (else
  2024.        (case (vector-ref p 0)
  2025.          ((each)
  2026.           (if (null? e)
  2027.               (match-empty (vector-ref p 1) r)
  2028.               (let ((l (match-each e (vector-ref p 1) w)))
  2029.                 (and l
  2030.                      (let collect ((l l))
  2031.                        (if (null? (car l))
  2032.                            r
  2033.                            (cons (map car l) (collect (map cdr l)))))))))
  2034.          ((free-id) (and (id? e) (free-id=? (wrap e w) (vector-ref p 1)) r))
  2035.          ((atom) (and (equal? (vector-ref p 1) (strip e w)) r))
  2036.          ((vector)
  2037.           (and (vector? e)
  2038.                (match (vector->list e) (vector-ref p 1) w r))))))))
  2039.  
  2040. (define match
  2041.   (lambda (e p w r)
  2042.     (cond
  2043.       ((not r) #f)
  2044.       ((eq? p 'any) (cons (wrap e w) r))
  2045.       ((syntax-object? e)
  2046.        (match*
  2047.          (unannotate (syntax-object-expression e))
  2048.          p
  2049.          (join-wraps w (syntax-object-wrap e))
  2050.          r))
  2051.       (else (match* (unannotate e) p w r)))))
  2052.  
  2053. (set! syntax-dispatch
  2054.   (lambda (e p)
  2055.     (cond
  2056.       ((eq? p 'any) (list e))
  2057.       ((syntax-object? e)
  2058.        (match* (unannotate (syntax-object-expression e))
  2059.          p (syntax-object-wrap e) '()))
  2060.       (else (match* (unannotate e) p empty-wrap '())))))
  2061.  
  2062. (set! sc-chi chi)
  2063. ))
  2064. )
  2065.  
  2066. (define-syntax with-syntax
  2067.    (lambda (x)
  2068.       (syntax-case x ()
  2069.          ((_ () e1 e2 ...)
  2070.           (syntax (begin e1 e2 ...)))
  2071.          ((_ ((out in)) e1 e2 ...)
  2072.           (syntax (syntax-case in () (out (begin e1 e2 ...)))))
  2073.          ((_ ((out in) ...) e1 e2 ...)
  2074.           (syntax (syntax-case (list in ...) ()
  2075.                      ((out ...) (begin e1 e2 ...))))))))
  2076.  
  2077. (define-syntax syntax-rules
  2078.   (lambda (x)
  2079.     (syntax-case x ()
  2080.       ((_ (k ...) ((keyword . pattern) template) ...)
  2081.        (syntax (lambda (x)
  2082.                 (syntax-case x (k ...)
  2083.                   ((dummy . pattern) (syntax template))
  2084.                   ...)))))))
  2085.  
  2086. (define-syntax let*
  2087.   (lambda (x)
  2088.     (syntax-case x ()
  2089.       ((let* ((x v) ...) e1 e2 ...)
  2090.        (andmap identifier? (syntax (x ...)))
  2091.        (let f ((bindings (syntax ((x v)  ...))))
  2092.          (if (null? bindings)
  2093.              (syntax (let () e1 e2 ...))
  2094.              (with-syntax ((body (f (cdr bindings)))
  2095.                            (binding (car bindings)))
  2096.                (syntax (let (binding) body)))))))))
  2097.  
  2098. (define-syntax do
  2099.    (lambda (orig-x)
  2100.       (syntax-case orig-x ()
  2101.          ((_ ((var init . step) ...) (e0 e1 ...) c ...)
  2102.           (with-syntax (((step ...)
  2103.                          (map (lambda (v s)
  2104.                                  (syntax-case s ()
  2105.                                     (() v)
  2106.                                     ((e) (syntax e))
  2107.                                     (_ (syntax-error orig-x))))
  2108.                               (syntax (var ...))
  2109.                               (syntax (step ...)))))
  2110.              (syntax-case (syntax (e1 ...)) ()
  2111.                 (() (syntax (let doloop ((var init) ...)
  2112.                                (if (not e0)
  2113.                                    (begin c ... (doloop step ...))))))
  2114.                 ((e1 e2 ...)
  2115.                  (syntax (let doloop ((var init) ...)
  2116.                             (if e0
  2117.                                 (begin e1 e2 ...)
  2118.                                 (begin c ... (doloop step ...))))))))))))
  2119.  
  2120. (define-syntax quasiquote
  2121.    (letrec
  2122.       ((quasicons
  2123.         (lambda (x y)
  2124.           (with-syntax ((x x) (y y))
  2125.             (syntax-case (syntax y) (quote list)
  2126.               ((quote dy)
  2127.                (syntax-case (syntax x) (quote)
  2128.                  ((quote dx) (syntax (quote (dx . dy))))
  2129.                  (_ (if (null? (syntax dy))
  2130.                         (syntax (list x))
  2131.                         (syntax (cons x y))))))
  2132.               ((list . stuff) (syntax (list x . stuff)))
  2133.               (else (syntax (cons x y)))))))
  2134.        (quasiappend
  2135.         (lambda (x y)
  2136.           (with-syntax ((x x) (y y))
  2137.             (syntax-case (syntax y) (quote)
  2138.               ((quote ()) (syntax x))
  2139.               (_ (syntax (append x y)))))))
  2140.        (quasivector
  2141.         (lambda (x)
  2142.           (with-syntax ((x x))
  2143.             (syntax-case (syntax x) (quote list)
  2144.               ((quote (x ...)) (syntax (quote #(x ...))))
  2145.               ((list x ...) (syntax (vector x ...)))
  2146.               (_ (syntax (list->vector x)))))))
  2147.        (quasi
  2148.         (lambda (p lev)
  2149.            (syntax-case p (unquote unquote-splicing quasiquote)
  2150.               ((unquote p)
  2151.                (if (= lev 0)
  2152.                    (syntax p)
  2153.                    (quasicons (syntax (quote unquote))
  2154.                               (quasi (syntax (p)) (- lev 1)))))
  2155.               (((unquote-splicing p) . q)
  2156.                (if (= lev 0)
  2157.                    (quasiappend (syntax p) (quasi (syntax q) lev))
  2158.                    (quasicons (quasicons (syntax (quote unquote-splicing))
  2159.                                          (quasi (syntax (p)) (- lev 1)))
  2160.                               (quasi (syntax q) lev))))
  2161.               ((quasiquote p)
  2162.                (quasicons (syntax (quote quasiquote))
  2163.                           (quasi (syntax (p)) (+ lev 1))))
  2164.               ((p . q)
  2165.                (quasicons (quasi (syntax p) lev) (quasi (syntax q) lev)))
  2166.               (#(x ...) (quasivector (quasi (syntax (x ...)) lev)))
  2167.               (p (syntax (quote p)))))))
  2168.     (lambda (x)
  2169.        (syntax-case x ()
  2170.           ((_ e) (quasi (syntax e) 0))))))
  2171.  
  2172. (define-syntax include
  2173.   (lambda (x)
  2174.     (define read-file
  2175.       (lambda (fn k)
  2176.         (let ((p (open-input-file fn)))
  2177.           (let f ((x (read p)))
  2178.             (if (eof-object? x)
  2179.                 (begin (close-input-port p) '())
  2180.                 (cons (datum->syntax-object k x)
  2181.                       (f (read p))))))))
  2182.     (syntax-case x ()
  2183.       ((k filename)
  2184.        (let ((fn (syntax-object->datum (syntax filename))))
  2185.          (with-syntax (((exp ...) (read-file fn (syntax k))))
  2186.            (syntax (begin exp ...))))))))
  2187.  
  2188. (define-syntax unquote
  2189.    (lambda (x)
  2190.       (syntax-case x ()
  2191.          ((_ e)
  2192.           (error 'unquote
  2193.          "expression ,~s not valid outside of quasiquote"
  2194.          (syntax-object->datum (syntax e)))))))
  2195.  
  2196. (define-syntax unquote-splicing
  2197.    (lambda (x)
  2198.       (syntax-case x ()
  2199.          ((_ e)
  2200.           (error 'unquote-splicing
  2201.          "expression ,@~s not valid outside of quasiquote"
  2202.          (syntax-object->datum (syntax e)))))))
  2203.  
  2204. (define-syntax case
  2205.   (lambda (x)
  2206.     (syntax-case x ()
  2207.       ((_ e m1 m2 ...)
  2208.        (with-syntax
  2209.          ((body (let f ((clause (syntax m1)) (clauses (syntax (m2 ...))))
  2210.                   (if (null? clauses)
  2211.                       (syntax-case clause (else)
  2212.                         ((else e1 e2 ...) (syntax (begin e1 e2 ...)))
  2213.                         (((k ...) e1 e2 ...)
  2214.                          (syntax (if (memv t '(k ...)) (begin e1 e2 ...))))
  2215.                         (_ (syntax-error x)))
  2216.                       (with-syntax ((rest (f (car clauses) (cdr clauses))))
  2217.                         (syntax-case clause (else)
  2218.                           (((k ...) e1 e2 ...)
  2219.                            (syntax (if (memv t '(k ...))
  2220.                                        (begin e1 e2 ...)
  2221.                                        rest)))
  2222.                           (_ (syntax-error x))))))))
  2223.          (syntax (let ((t e)) body)))))))
  2224.  
  2225. (define-syntax identifier-syntax
  2226.   (lambda (x)
  2227.     (syntax-case x ()
  2228.       ((_ e)
  2229.        (syntax
  2230.          (lambda (x)
  2231.            (syntax-case x ()
  2232.              (id
  2233.               (identifier? (syntax id))
  2234.               (syntax e))
  2235.              ((_ x (... ...))
  2236.               (syntax (e x (... ...)))))))))))
  2237.  
  2238.