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