and, or, cond etc use syntax-rules, compile scheme through tree-il
[bpt/guile.git] / module / ice-9 / psyntax.scm
CommitLineData
677cd590
RB
1;;;; -*-scheme-*-
2;;;;
cd5fea8d 3;;;; Copyright (C) 2001, 2003, 2006 Free Software Foundation, Inc.
86b96c16 4;;;;
73be1d9e
MV
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.
86b96c16 9;;;;
73be1d9e 10;;;; This library is distributed in the hope that it will be useful,
86b96c16 11;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
73be1d9e
MV
12;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13;;;; Lesser General Public License for more details.
86b96c16 14;;;;
73be1d9e
MV
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
92205699 17;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
86b96c16
MD
18;;;;
19\f
20
a63812a2
JB
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
41af2381
AW
25;;; Modified by Andy Wingo <wingo@pobox.com> according to the Git
26;;; revision control logs corresponding to this file: 2009.
27
86b96c16
MD
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
a63812a2
JB
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
a63812a2
JB
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=?
22225fc1 55;;; datum->syntax
a63812a2
JB
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
22225fc1 66;;; syntax->datum
a63812a2
JB
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)
e4721dde 85;;; (syntax-violation who message form [subform])
a63812a2 86;;; used to report errors found during expansion
5f1a2fb1 87;;; ($sc-dispatch e p)
a63812a2
JB
88;;; used by expanded code to handle syntax-case matching
89
90;;; The following nonstandard procedures must be provided by the
a63812a2 91;;; implementation for this code to run using the standard portable
41af2381 92;;; hooks and output constructors. They are not used by expanded code,
a63812a2
JB
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;;;
a63812a2
JB
108;;; (gensym)
109;;; returns a unique symbol each time it's called
a63812a2
JB
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
22225fc1 170;;; are contained within a syntax form or produced by datum->syntax.
a63812a2
JB
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
9c35c579
AW
217(eval-when (compile)
218 (set-current-module (resolve-module '(guile))))
219
a63812a2 220(let ()
4d248541
AW
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
a63812a2
JB
240(define-syntax define-structure
241 (lambda (x)
242 (define construct-name
243 (lambda (template-identifier . args)
22225fc1 244 (datum->syntax
a63812a2
JB
245 template-identifier
246 (string->symbol
247 (apply string-append
248 (map (lambda (x)
249 (if (string? x)
250 x
22225fc1 251 (symbol->string (syntax->datum x))))
a63812a2
JB
252 args))))))
253 (syntax-case x ()
254 ((_ (name id1 ...))
4d248541 255 (and-map identifier? (syntax (name id1 ...)))
a63812a2
JB
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")
71f46dbd 293(define *mode* (make-fluid))
a63812a2
JB
294
295;;; hooks to nonportable run-time helpers
296(begin
297(define fx+ +)
298(define fx- -)
299(define fx= =)
300(define fx< <)
301
a63812a2 302(define top-level-eval-hook
4e237f14 303 (lambda (x mod)
71f46dbd
AW
304 (primitive-eval
305 `(,noexpand
306 ,(case (fluid-ref *mode*)
811d10f5 307 ((c) ((@ (language tree-il) tree-il->scheme) x))
71f46dbd 308 (else x))))))
a63812a2
JB
309
310(define local-eval-hook
4e237f14 311 (lambda (x mod)
71f46dbd
AW
312 (primitive-eval
313 `(,noexpand
314 ,(case (fluid-ref *mode*)
811d10f5 315 ((c) ((@ (language tree-il) tree-il->scheme) x))
71f46dbd 316 (else x))))))
a63812a2 317
a63812a2
JB
318(define-syntax gensym-hook
319 (syntax-rules ()
320 ((_) (gensym))))
321
322(define put-global-definition-hook
97ce9dbf 323 (lambda (symbol type val)
3d5f3091
AW
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))))))
c5ad45c7 335
a63812a2 336(define get-global-definition-hook
8e1d0d50 337 (lambda (symbol module)
165a7596
AW
338 (if (and (not module) (current-module))
339 (warn "module system is booted, we should have a module" symbol))
3d5f3091
AW
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))))))))
d6ebfd72 349
a63812a2
JB
350)
351
352
353;;; output constructors
a1a482e0
AW
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
811d10f5
AW
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)))))
a63812a2 372
f4a644ee
AW
373(define build-lexical-reference
374 (lambda (type source name var)
811d10f5
AW
375 (case (fluid-ref *mode*)
376 ((c) ((@ (language tree-il) make-lexical-ref) source name var))
377 (else var))))
a63812a2 378
f4a644ee
AW
379(define build-lexical-assignment
380 (lambda (source name var exp)
811d10f5
AW
381 (case (fluid-ref *mode*)
382 ((c) ((@ (language tree-il) make-lexical-set) source name var exp))
383 (else `(set! ,var ,exp)))))
a63812a2 384
71f46dbd 385;; Before modules are booted, we can't expand into data structures from
811d10f5 386;; (language tree-il) -- we need to give the evaluator the
71f46dbd 387;; s-expressions that it understands natively. Actually the real truth
982a1c20 388;; of the matter is that the evaluator doesn't understand tree-il
71f46dbd 389;; structures at all. So until we fix the evaluator, if ever, the
811d10f5 390;; conflation that we should use tree-il iff we are compiling
71f46dbd
AW
391;; holds true.
392;;
811d10f5
AW
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
71f46dbd
AW
410(define build-global-reference
411 (lambda (source var mod)
811d10f5
AW
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))))))
71f46dbd
AW
422
423(define build-global-assignment
424 (lambda (source var exp mod)
811d10f5
AW
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
547a602d 443 (lambda (src ids vars docstring exp)
811d10f5 444 (case (fluid-ref *mode*)
696495f4 445 ((c) ((@ (language tree-il) make-lambda) src ids vars
811d10f5
AW
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)
a1a482e0
AW
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))))))
a63812a2 460
80f225df 461(define (build-data src exp)
811d10f5
AW
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)))))
a63812a2
JB
467
468(define build-sequence
469 (lambda (src exps)
470 (if (null? (cdr exps))
811d10f5
AW
471 (car exps)
472 (case (fluid-ref *mode*)
473 ((c) ((@ (language tree-il) make-sequence) src exps))
474 (else `(begin ,@exps))))))
a63812a2
JB
475
476(define build-let
547a602d 477 (lambda (src ids vars val-exps body-exp)
a63812a2 478 (if (null? vars)
811d10f5
AW
479 body-exp
480 (case (fluid-ref *mode*)
696495f4 481 ((c) ((@ (language tree-il) make-let) src ids vars val-exps body-exp))
811d10f5 482 (else `(let ,(map list vars val-exps) ,body-exp))))))
a63812a2
JB
483
484(define build-named-let
547a602d 485 (lambda (src ids vars val-exps body-exp)
811d10f5 486 (let ((f (car vars))
547a602d
AW
487 (f-name (car ids))
488 (vars (cdr vars))
489 (ids (cdr ids)))
811d10f5
AW
490 (case (fluid-ref *mode*)
491 ((c) ((@ (language tree-il) make-letrec) src
696495f4 492 (list f-name)
547a602d
AW
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)
811d10f5
AW
496 val-exps)))
497 (else `(let ,f ,(map list vars val-exps) ,body-exp))))))
a63812a2
JB
498
499(define build-letrec
547a602d 500 (lambda (src ids vars val-exps body-exp)
a63812a2 501 (if (null? vars)
811d10f5
AW
502 body-exp
503 (case (fluid-ref *mode*)
696495f4 504 ((c) ((@ (language tree-il) make-letrec) src ids vars val-exps body-exp))
811d10f5 505 (else `(letrec ,(map list vars val-exps) ,body-exp))))))
a63812a2 506
811d10f5 507;; FIXME: wingo: use make-lexical ?
a63812a2
JB
508(define-syntax build-lexical-var
509 (syntax-rules ()
811d10f5 510 ((_ src id) (gensym (symbol->string id)))))
a63812a2 511
e02e84de 512(define-structure (syntax-object expression wrap module))
a63812a2
JB
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))
6a952e0e 535 (if (not (pred? x)) (syntax-violation who "invalid argument" x))))))
a63812a2
JB
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
80f225df 559;;; (external-macro . <procedure>) external-macro
265e6127 560;;; (module-ref . <procedure>) @ or @@
a63812a2
JB
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
8e1d0d50 636 (lambda (x r mod)
a63812a2
JB
637 (cond
638 ((assq x r) => cdr)
639 ((symbol? x)
8e1d0d50 640 (or (get-global-definition-hook x mod) (make-binding 'global)))
a63812a2
JB
641 (else (make-binding 'displaced-lexical)))))
642
643(define global-extend
644 (lambda (type sym val)
97ce9dbf 645 (put-global-definition-hook sym type val)))
a63812a2
JB
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)))
6a952e0e 855 (else (syntax-violation 'id-var-name "invalid id" id)))))
a63812a2
JB
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
4e237f14 913 (lambda (x w defmod)
a63812a2
JB
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)
e02e84de
AW
919 (join-wraps w (syntax-object-wrap x))
920 (syntax-object-module x)))
a63812a2 921 ((null? x) x)
4e237f14 922 (else (make-syntax-object x w defmod)))))
a63812a2
JB
923
924(define source-wrap
4e237f14
AW
925 (lambda (x w s defmod)
926 (wrap (if s (make-annotation x s #f) x) w defmod)))
a63812a2
JB
927
928;;; expanding
929
930(define chi-sequence
4e237f14 931 (lambda (body r w s mod)
a63812a2 932 (build-sequence s
4e237f14 933 (let dobody ((body body) (r r) (w w) (mod mod))
a63812a2
JB
934 (if (null? body)
935 '()
4e237f14
AW
936 (let ((first (chi (car body) r w mod)))
937 (cons first (dobody (cdr body) r w mod))))))))
a63812a2
JB
938
939(define chi-top-sequence
4e237f14 940 (lambda (body r w s m esew mod)
a63812a2 941 (build-sequence s
4e237f14 942 (let dobody ((body body) (r r) (w w) (m m) (esew esew) (mod mod))
a63812a2
JB
943 (if (null? body)
944 '()
4e237f14
AW
945 (let ((first (chi-top (car body) r w m esew mod)))
946 (cons first (dobody (cdr body) r w m esew mod))))))))
a63812a2
JB
947
948(define chi-install-global
949 (lambda (name e)
f27e9e11
AW
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 no-source 'current-module '())
965 (build-data no-source name)))
966 (build-data no-source 'macro)
967 e))
968 (build-application
969 no-source
970 (build-primref no-source 'make-syncase-macro)
971 (list (build-data no-source 'macro) e))))))
a63812a2
JB
972
973(define chi-when-list
974 (lambda (e when-list w)
975 ; when-list is syntax'd version of list of situations
976 (let f ((when-list when-list) (situations '()))
977 (if (null? when-list)
978 situations
979 (f (cdr when-list)
980 (cons (let ((x (car when-list)))
981 (cond
982 ((free-id=? x (syntax compile)) 'compile)
983 ((free-id=? x (syntax load)) 'load)
984 ((free-id=? x (syntax eval)) 'eval)
e4721dde
AW
985 (else (syntax-violation 'eval-when
986 "invalid situation"
987 e (wrap x w #f)))))
a63812a2
JB
988 situations))))))
989
4e237f14
AW
990;;; syntax-type returns six values: type, value, e, w, s, and mod. The
991;;; first two are described in the table below.
a63812a2
JB
992;;;
993;;; type value explanation
994;;; -------------------------------------------------------------------
995;;; core procedure core form (including singleton)
80f225df 996;;; external-macro procedure external macro
265e6127 997;;; module-ref procedure @ or @@ form
a63812a2
JB
998;;; lexical name lexical variable reference
999;;; global name global variable reference
1000;;; begin none begin keyword
1001;;; define none define keyword
1002;;; define-syntax none define-syntax keyword
1003;;; local-syntax rec? letrec-syntax/let-syntax keyword
1004;;; eval-when none eval-when keyword
1005;;; syntax level pattern variable
1006;;; displaced-lexical none displaced lexical identifier
1007;;; lexical-call name call to lexical variable
1008;;; global-call name call to global variable
1009;;; call none any other call
1010;;; begin-form none begin expression
1011;;; define-form id variable definition
1012;;; define-syntax-form id syntax definition
1013;;; local-syntax-form rec? syntax definition
1014;;; eval-when-form none eval-when form
1015;;; constant none self-evaluating datum
1016;;; other none anything else
1017;;;
1018;;; For define-form and define-syntax-form, e is the rhs expression.
1019;;; For all others, e is the entire form. w is the wrap for e.
4e237f14 1020;;; s is the source for the entire form. mod is the module for e.
a63812a2
JB
1021;;;
1022;;; syntax-type expands macros and unwraps as necessary to get to
1023;;; one of the forms above. It also parses define and define-syntax
1024;;; forms, although perhaps this should be done by the consumer.
1025
1026(define syntax-type
4e237f14 1027 (lambda (e r w s rib mod)
a63812a2
JB
1028 (cond
1029 ((symbol? e)
1030 (let* ((n (id-var-name e w))
8e1d0d50 1031 (b (lookup n r mod))
a63812a2
JB
1032 (type (binding-type b)))
1033 (case type
8e1d0d50 1034 ((lexical) (values type (binding-value b) e w s mod))
4e237f14 1035 ((global) (values type n e w s mod))
a63812a2 1036 ((macro)
4e237f14
AW
1037 (syntax-type (chi-macro (binding-value b) e r w rib mod)
1038 r empty-wrap s rib mod))
1039 (else (values type (binding-value b) e w s mod)))))
a63812a2
JB
1040 ((pair? e)
1041 (let ((first (car e)))
1042 (if (id? first)
1043 (let* ((n (id-var-name first w))
c5cc65ac
AW
1044 (b (lookup n r (or (and (syntax-object? first)
1045 (syntax-object-module first))
1046 mod)))
a63812a2
JB
1047 (type (binding-type b)))
1048 (case type
4e237f14
AW
1049 ((lexical)
1050 (values 'lexical-call (binding-value b) e w s mod))
1051 ((global)
1052 (values 'global-call n e w s mod))
a63812a2 1053 ((macro)
4e237f14
AW
1054 (syntax-type (chi-macro (binding-value b) e r w rib mod)
1055 r empty-wrap s rib mod))
265e6127 1056 ((core external-macro module-ref)
4e237f14 1057 (values type (binding-value b) e w s mod))
a63812a2 1058 ((local-syntax)
4e237f14
AW
1059 (values 'local-syntax-form (binding-value b) e w s mod))
1060 ((begin)
1061 (values 'begin-form #f e w s mod))
1062 ((eval-when)
1063 (values 'eval-when-form #f e w s mod))
a63812a2
JB
1064 ((define)
1065 (syntax-case e ()
1066 ((_ name val)
1067 (id? (syntax name))
4e237f14 1068 (values 'define-form (syntax name) (syntax val) w s mod))
a63812a2
JB
1069 ((_ (name . args) e1 e2 ...)
1070 (and (id? (syntax name))
1071 (valid-bound-ids? (lambda-var-list (syntax args))))
1072 ; need lambda here...
8e1d0d50 1073 (values 'define-form (wrap (syntax name) w mod)
4e237f14
AW
1074 (cons (syntax lambda) (wrap (syntax (args e1 e2 ...)) w mod))
1075 empty-wrap s mod))
a63812a2
JB
1076 ((_ name)
1077 (id? (syntax name))
8e1d0d50 1078 (values 'define-form (wrap (syntax name) w mod)
41af2381 1079 (syntax (if #f #f))
4e237f14 1080 empty-wrap s mod))))
a63812a2
JB
1081 ((define-syntax)
1082 (syntax-case e ()
1083 ((_ name val)
1084 (id? (syntax name))
1085 (values 'define-syntax-form (syntax name)
4e237f14
AW
1086 (syntax val) w s mod))))
1087 (else
1088 (values 'call #f e w s mod))))
1089 (values 'call #f e w s mod))))
a63812a2
JB
1090 ((syntax-object? e)
1091 ;; s can't be valid source if we've unwrapped
1092 (syntax-type (syntax-object-expression e)
1093 r
1094 (join-wraps w (syntax-object-wrap e))
8e1d0d50 1095 no-source rib (or (syntax-object-module e) mod)))
a63812a2 1096 ((annotation? e)
4e237f14
AW
1097 (syntax-type (annotation-expression e) r w (annotation-source e) rib mod))
1098 ((self-evaluating? e) (values 'constant #f e w s mod))
1099 (else (values 'other #f e w s mod)))))
a63812a2
JB
1100
1101(define chi-top
4e237f14 1102 (lambda (e r w m esew mod)
a63812a2
JB
1103 (define-syntax eval-if-c&e
1104 (syntax-rules ()
4e237f14 1105 ((_ m e mod)
a63812a2 1106 (let ((x e))
4e237f14 1107 (if (eq? m 'c&e) (top-level-eval-hook x mod))
a63812a2
JB
1108 x))))
1109 (call-with-values
4e237f14
AW
1110 (lambda () (syntax-type e r w no-source #f mod))
1111 (lambda (type value e w s mod)
a63812a2
JB
1112 (case type
1113 ((begin-form)
1114 (syntax-case e ()
1115 ((_) (chi-void))
1116 ((_ e1 e2 ...)
4e237f14 1117 (chi-top-sequence (syntax (e1 e2 ...)) r w s m esew mod))))
a63812a2 1118 ((local-syntax-form)
4e237f14
AW
1119 (chi-local-syntax value e r w s mod
1120 (lambda (body r w s mod)
1121 (chi-top-sequence body r w s m esew mod))))
a63812a2
JB
1122 ((eval-when-form)
1123 (syntax-case e ()
1124 ((_ (x ...) e1 e2 ...)
1125 (let ((when-list (chi-when-list e (syntax (x ...)) w))
1126 (body (syntax (e1 e2 ...))))
1127 (cond
1128 ((eq? m 'e)
1129 (if (memq 'eval when-list)
4e237f14 1130 (chi-top-sequence body r w s 'e '(eval) mod)
a63812a2
JB
1131 (chi-void)))
1132 ((memq 'load when-list)
1133 (if (or (memq 'compile when-list)
1134 (and (eq? m 'c&e) (memq 'eval when-list)))
4e237f14 1135 (chi-top-sequence body r w s 'c&e '(compile load) mod)
a63812a2 1136 (if (memq m '(c c&e))
4e237f14 1137 (chi-top-sequence body r w s 'c '(load) mod)
a63812a2
JB
1138 (chi-void))))
1139 ((or (memq 'compile when-list)
1140 (and (eq? m 'c&e) (memq 'eval when-list)))
1141 (top-level-eval-hook
4e237f14
AW
1142 (chi-top-sequence body r w s 'e '(eval) mod)
1143 mod)
a63812a2
JB
1144 (chi-void))
1145 (else (chi-void)))))))
1146 ((define-syntax-form)
1147 (let ((n (id-var-name value w)) (r (macros-only-env r)))
1148 (case m
1149 ((c)
1150 (if (memq 'compile esew)
4e237f14
AW
1151 (let ((e (chi-install-global n (chi e r w mod))))
1152 (top-level-eval-hook e mod)
a63812a2
JB
1153 (if (memq 'load esew) e (chi-void)))
1154 (if (memq 'load esew)
4e237f14 1155 (chi-install-global n (chi e r w mod))
a63812a2
JB
1156 (chi-void))))
1157 ((c&e)
4e237f14
AW
1158 (let ((e (chi-install-global n (chi e r w mod))))
1159 (top-level-eval-hook e mod)
a63812a2
JB
1160 e))
1161 (else
1162 (if (memq 'eval esew)
1163 (top-level-eval-hook
4e237f14
AW
1164 (chi-install-global n (chi e r w mod))
1165 mod))
a63812a2
JB
1166 (chi-void)))))
1167 ((define-form)
80f225df 1168 (let* ((n (id-var-name value w))
8e1d0d50 1169 (type (binding-type (lookup n r mod))))
80f225df 1170 (case type
3d5f3091 1171 ((global core macro module-ref)
a63812a2 1172 (eval-if-c&e m
f27e9e11 1173 (build-global-definition s n (chi e r w mod))
4e237f14 1174 mod))
a63812a2 1175 ((displaced-lexical)
e4721dde
AW
1176 (syntax-violation #f "identifier out of context"
1177 e (wrap value w mod)))
80f225df 1178 (else
e4721dde
AW
1179 (syntax-violation #f "cannot define keyword at top level"
1180 e (wrap value w mod))))))
4e237f14 1181 (else (eval-if-c&e m (chi-expr type value e r w s mod) mod)))))))
a63812a2
JB
1182
1183(define chi
4e237f14 1184 (lambda (e r w mod)
a63812a2 1185 (call-with-values
4e237f14
AW
1186 (lambda () (syntax-type e r w no-source #f mod))
1187 (lambda (type value e w s mod)
1188 (chi-expr type value e r w s mod)))))
a63812a2
JB
1189
1190(define chi-expr
4e237f14 1191 (lambda (type value e r w s mod)
a63812a2
JB
1192 (case type
1193 ((lexical)
f4a644ee 1194 (build-lexical-reference 'value s e value))
4e237f14
AW
1195 ((core external-macro)
1196 ;; apply transformer
1197 (value e r w s mod))
265e6127 1198 ((module-ref)
dec62b5e 1199 (call-with-values (lambda () (value e))
265e6127
AW
1200 ;; we could add a public? arg here
1201 (lambda (id mod) (build-global-reference s id mod))))
a63812a2
JB
1202 ((lexical-call)
1203 (chi-application
f4a644ee
AW
1204 (build-lexical-reference 'fun (source-annotation (car e))
1205 (car e) value)
4e237f14 1206 e r w s mod))
a63812a2
JB
1207 ((global-call)
1208 (chi-application
d2b61fe0
AW
1209 (build-global-reference (source-annotation (car e)) value
1210 (if (syntax-object? (car e))
1211 (syntax-object-module (car e))
1212 mod))
4e237f14
AW
1213 e r w s mod))
1214 ((constant) (build-data s (strip (source-wrap e w s mod) empty-wrap)))
1215 ((global) (build-global-reference s value mod))
1216 ((call) (chi-application (chi (car e) r w mod) e r w s mod))
a63812a2
JB
1217 ((begin-form)
1218 (syntax-case e ()
4e237f14 1219 ((_ e1 e2 ...) (chi-sequence (syntax (e1 e2 ...)) r w s mod))))
a63812a2 1220 ((local-syntax-form)
4e237f14 1221 (chi-local-syntax value e r w s mod chi-sequence))
a63812a2
JB
1222 ((eval-when-form)
1223 (syntax-case e ()
1224 ((_ (x ...) e1 e2 ...)
1225 (let ((when-list (chi-when-list e (syntax (x ...)) w)))
1226 (if (memq 'eval when-list)
4e237f14 1227 (chi-sequence (syntax (e1 e2 ...)) r w s mod)
a63812a2
JB
1228 (chi-void))))))
1229 ((define-form define-syntax-form)
e4721dde
AW
1230 (syntax-violation #f "definition in expression context"
1231 e (wrap value w mod)))
a63812a2 1232 ((syntax)
e4721dde
AW
1233 (syntax-violation #f "reference to pattern variable outside syntax form"
1234 (source-wrap e w s mod)))
a63812a2 1235 ((displaced-lexical)
5a0132b3
AW
1236 (syntax-violation #f "reference to identifier outside its scope"
1237 (source-wrap e w s mod)))
e4721dde
AW
1238 (else (syntax-violation #f "unexpected syntax"
1239 (source-wrap e w s mod))))))
a63812a2
JB
1240
1241(define chi-application
4e237f14 1242 (lambda (x e r w s mod)
a63812a2
JB
1243 (syntax-case e ()
1244 ((e0 e1 ...)
1245 (build-application s x
4e237f14 1246 (map (lambda (e) (chi e r w mod)) (syntax (e1 ...))))))))
a63812a2
JB
1247
1248(define chi-macro
4e237f14 1249 (lambda (p e r w rib mod)
a63812a2
JB
1250 (define rebuild-macro-output
1251 (lambda (x m)
1252 (cond ((pair? x)
1253 (cons (rebuild-macro-output (car x) m)
1254 (rebuild-macro-output (cdr x) m)))
1255 ((syntax-object? x)
1256 (let ((w (syntax-object-wrap x)))
1257 (let ((ms (wrap-marks w)) (s (wrap-subst w)))
4e237f14
AW
1258 (if (and (pair? ms) (eq? (car ms) the-anti-mark))
1259 ;; output is from original text
1260 (make-syntax-object
1261 (syntax-object-expression x)
1262 (make-wrap (cdr ms) (if rib (cons rib (cdr s)) (cdr s)))
1263 (syntax-object-module x))
1264 ;; output introduced by macro
1265 (make-syntax-object
1266 (syntax-object-expression x)
1267 (make-wrap (cons m ms)
1268 (if rib
1269 (cons rib (cons 'shift s))
1270 (cons 'shift s)))
34ad4f83
AW
1271 (let ((pmod (procedure-module p)))
1272 (if pmod
1273 ;; hither the hygiene
1274 (cons 'hygiene (module-name pmod))
1275 ;; but it's possible for the proc to have
1276 ;; no mod, if it was made before modules
1277 ;; were booted
1278 '(hygiene guile))))))))
a63812a2
JB
1279 ((vector? x)
1280 (let* ((n (vector-length x)) (v (make-vector n)))
1281 (do ((i 0 (fx+ i 1)))
1282 ((fx= i n) v)
1283 (vector-set! v i
1284 (rebuild-macro-output (vector-ref x i) m)))))
1285 ((symbol? x)
e4721dde
AW
1286 (syntax-violation #f "encountered raw symbol in macro output"
1287 (source-wrap e w s mod) x))
a63812a2 1288 (else x))))
4e237f14 1289 (rebuild-macro-output (p (wrap e (anti-mark w) mod)) (new-mark))))
a63812a2
JB
1290
1291(define chi-body
1292 ;; In processing the forms of the body, we create a new, empty wrap.
1293 ;; This wrap is augmented (destructively) each time we discover that
1294 ;; the next form is a definition. This is done:
1295 ;;
1296 ;; (1) to allow the first nondefinition form to be a call to
1297 ;; one of the defined ids even if the id previously denoted a
1298 ;; definition keyword or keyword for a macro expanding into a
1299 ;; definition;
1300 ;; (2) to prevent subsequent definition forms (but unfortunately
1301 ;; not earlier ones) and the first nondefinition form from
1302 ;; confusing one of the bound identifiers for an auxiliary
1303 ;; keyword; and
1304 ;; (3) so that we do not need to restart the expansion of the
1305 ;; first nondefinition form, which is problematic anyway
1306 ;; since it might be the first element of a begin that we
1307 ;; have just spliced into the body (meaning if we restarted,
1308 ;; we'd really need to restart with the begin or the macro
1309 ;; call that expanded into the begin, and we'd have to give
1310 ;; up allowing (begin <defn>+ <expr>+), which is itself
1311 ;; problematic since we don't know if a begin contains only
1312 ;; definitions until we've expanded it).
1313 ;;
1314 ;; Before processing the body, we also create a new environment
1315 ;; containing a placeholder for the bindings we will add later and
1316 ;; associate this environment with each form. In processing a
1317 ;; let-syntax or letrec-syntax, the associated environment may be
1318 ;; augmented with local keyword bindings, so the environment may
1319 ;; be different for different forms in the body. Once we have
1320 ;; gathered up all of the definitions, we evaluate the transformer
1321 ;; expressions and splice into r at the placeholder the new variable
1322 ;; and keyword bindings. This allows let-syntax or letrec-syntax
1323 ;; forms local to a portion or all of the body to shadow the
1324 ;; definition bindings.
1325 ;;
1326 ;; Subforms of a begin, let-syntax, or letrec-syntax are spliced
1327 ;; into the body.
1328 ;;
1329 ;; outer-form is fully wrapped w/source
4e237f14 1330 (lambda (body outer-form r w mod)
a63812a2
JB
1331 (let* ((r (cons '("placeholder" . (placeholder)) r))
1332 (ribcage (make-empty-ribcage))
1333 (w (make-wrap (wrap-marks w) (cons ribcage (wrap-subst w)))))
4e237f14 1334 (let parse ((body (map (lambda (x) (cons r (wrap x w mod))) body))
a63812a2
JB
1335 (ids '()) (labels '()) (vars '()) (vals '()) (bindings '()))
1336 (if (null? body)
e4721dde 1337 (syntax-violation #f "no expressions in body" outer-form)
a63812a2
JB
1338 (let ((e (cdar body)) (er (caar body)))
1339 (call-with-values
4e237f14
AW
1340 (lambda () (syntax-type e er empty-wrap no-source ribcage mod))
1341 (lambda (type value e w s mod)
a63812a2
JB
1342 (case type
1343 ((define-form)
4e237f14 1344 (let ((id (wrap value w mod)) (label (gen-label)))
a63812a2
JB
1345 (let ((var (gen-var id)))
1346 (extend-ribcage! ribcage id label)
1347 (parse (cdr body)
1348 (cons id ids) (cons label labels)
4e237f14 1349 (cons var vars) (cons (cons er (wrap e w mod)) vals)
a63812a2
JB
1350 (cons (make-binding 'lexical var) bindings)))))
1351 ((define-syntax-form)
4e237f14 1352 (let ((id (wrap value w mod)) (label (gen-label)))
a63812a2
JB
1353 (extend-ribcage! ribcage id label)
1354 (parse (cdr body)
1355 (cons id ids) (cons label labels)
1356 vars vals
4e237f14 1357 (cons (make-binding 'macro (cons er (wrap e w mod)))
a63812a2
JB
1358 bindings))))
1359 ((begin-form)
1360 (syntax-case e ()
1361 ((_ e1 ...)
1362 (parse (let f ((forms (syntax (e1 ...))))
1363 (if (null? forms)
1364 (cdr body)
4e237f14 1365 (cons (cons er (wrap (car forms) w mod))
a63812a2
JB
1366 (f (cdr forms)))))
1367 ids labels vars vals bindings))))
1368 ((local-syntax-form)
4e237f14
AW
1369 (chi-local-syntax value e er w s mod
1370 (lambda (forms er w s mod)
a63812a2
JB
1371 (parse (let f ((forms forms))
1372 (if (null? forms)
1373 (cdr body)
4e237f14 1374 (cons (cons er (wrap (car forms) w mod))
a63812a2
JB
1375 (f (cdr forms)))))
1376 ids labels vars vals bindings))))
1377 (else ; found a non-definition
1378 (if (null? ids)
1379 (build-sequence no-source
1380 (map (lambda (x)
4e237f14
AW
1381 (chi (cdr x) (car x) empty-wrap mod))
1382 (cons (cons er (source-wrap e w s mod))
a63812a2
JB
1383 (cdr body))))
1384 (begin
1385 (if (not (valid-bound-ids? ids))
e4721dde
AW
1386 (syntax-violation
1387 #f "invalid or duplicate identifier in definition"
1388 outer-form))
a63812a2
JB
1389 (let loop ((bs bindings) (er-cache #f) (r-cache #f))
1390 (if (not (null? bs))
1391 (let* ((b (car bs)))
1392 (if (eq? (car b) 'macro)
1393 (let* ((er (cadr b))
1394 (r-cache
1395 (if (eq? er er-cache)
1396 r-cache
1397 (macros-only-env er))))
1398 (set-cdr! b
1399 (eval-local-transformer
4e237f14
AW
1400 (chi (cddr b) r-cache empty-wrap mod)
1401 mod))
a63812a2
JB
1402 (loop (cdr bs) er r-cache))
1403 (loop (cdr bs) er-cache r-cache)))))
1404 (set-cdr! r (extend-env labels bindings (cdr r)))
1405 (build-letrec no-source
547a602d 1406 (map syntax->datum ids)
a63812a2
JB
1407 vars
1408 (map (lambda (x)
4e237f14 1409 (chi (cdr x) (car x) empty-wrap mod))
a63812a2
JB
1410 vals)
1411 (build-sequence no-source
1412 (map (lambda (x)
4e237f14
AW
1413 (chi (cdr x) (car x) empty-wrap mod))
1414 (cons (cons er (source-wrap e w s mod))
a63812a2
JB
1415 (cdr body)))))))))))))))))
1416
1417(define chi-lambda-clause
0ee32d01 1418 (lambda (e docstring c r w mod k)
a63812a2 1419 (syntax-case c ()
0ee32d01 1420 ((args doc e1 e2 ...)
22225fc1 1421 (and (string? (syntax->datum (syntax doc))) (not docstring))
0ee32d01 1422 (chi-lambda-clause e (syntax doc) (syntax (args e1 e2 ...)) r w mod k))
a63812a2
JB
1423 (((id ...) e1 e2 ...)
1424 (let ((ids (syntax (id ...))))
1425 (if (not (valid-bound-ids? ids))
e4721dde 1426 (syntax-violation 'lambda "invalid parameter list" e)
a63812a2
JB
1427 (let ((labels (gen-labels ids))
1428 (new-vars (map gen-var ids)))
547a602d
AW
1429 (k (map syntax->datum ids)
1430 new-vars
0ee32d01 1431 docstring
a63812a2
JB
1432 (chi-body (syntax (e1 e2 ...))
1433 e
1434 (extend-var-env labels new-vars r)
4e237f14
AW
1435 (make-binding-wrap ids labels w)
1436 mod))))))
a63812a2
JB
1437 ((ids e1 e2 ...)
1438 (let ((old-ids (lambda-var-list (syntax ids))))
1439 (if (not (valid-bound-ids? old-ids))
e4721dde 1440 (syntax-violation 'lambda "invalid parameter list" e)
a63812a2
JB
1441 (let ((labels (gen-labels old-ids))
1442 (new-vars (map gen-var old-ids)))
547a602d
AW
1443 (k (let f ((ls1 (cdr old-ids)) (ls2 (car old-ids)))
1444 (if (null? ls1)
1445 (syntax->datum ls2)
1446 (f (cdr ls1) (cons (syntax->datum (car ls1)) ls2))))
1447 (let f ((ls1 (cdr new-vars)) (ls2 (car new-vars)))
a63812a2
JB
1448 (if (null? ls1)
1449 ls2
1450 (f (cdr ls1) (cons (car ls1) ls2))))
0ee32d01 1451 docstring
a63812a2
JB
1452 (chi-body (syntax (e1 e2 ...))
1453 e
1454 (extend-var-env labels new-vars r)
4e237f14
AW
1455 (make-binding-wrap old-ids labels w)
1456 mod))))))
e4721dde 1457 (_ (syntax-violation 'lambda "bad lambda" e)))))
a63812a2
JB
1458
1459(define chi-local-syntax
4e237f14 1460 (lambda (rec? e r w s mod k)
a63812a2
JB
1461 (syntax-case e ()
1462 ((_ ((id val) ...) e1 e2 ...)
1463 (let ((ids (syntax (id ...))))
1464 (if (not (valid-bound-ids? ids))
e4721dde 1465 (syntax-violation #f "duplicate bound keyword" e)
a63812a2
JB
1466 (let ((labels (gen-labels ids)))
1467 (let ((new-w (make-binding-wrap ids labels w)))
1468 (k (syntax (e1 e2 ...))
1469 (extend-env
1470 labels
1471 (let ((w (if rec? new-w w))
1472 (trans-r (macros-only-env r)))
1473 (map (lambda (x)
1474 (make-binding 'macro
4e237f14
AW
1475 (eval-local-transformer
1476 (chi x trans-r w mod)
1477 mod)))
a63812a2
JB
1478 (syntax (val ...))))
1479 r)
1480 new-w
4e237f14
AW
1481 s
1482 mod))))))
e4721dde
AW
1483 (_ (syntax-violation #f "bad local syntax definition"
1484 (source-wrap e w s mod))))))
a63812a2
JB
1485
1486(define eval-local-transformer
4e237f14
AW
1487 (lambda (expanded mod)
1488 (let ((p (local-eval-hook expanded mod)))
a63812a2
JB
1489 (if (procedure? p)
1490 p
e4721dde 1491 (syntax-violation #f "nonprocedure transformer" p)))))
a63812a2
JB
1492
1493(define chi-void
1494 (lambda ()
a1a482e0 1495 (build-void no-source)))
a63812a2
JB
1496
1497(define ellipsis?
1498 (lambda (x)
1499 (and (nonsymbol-id? x)
1500 (free-id=? x (syntax (... ...))))))
1501
1502;;; data
1503
1504;;; strips all annotations from potentially circular reader output
1505
1506(define strip-annotation
1507 (lambda (x parent)
1508 (cond
1509 ((pair? x)
1510 (let ((new (cons #f #f)))
979933ab 1511 (if parent (set-annotation-stripped! parent new))
a63812a2
JB
1512 (set-car! new (strip-annotation (car x) #f))
1513 (set-cdr! new (strip-annotation (cdr x) #f))
1514 new))
1515 ((annotation? x)
1516 (or (annotation-stripped x)
1517 (strip-annotation (annotation-expression x) x)))
1518 ((vector? x)
1519 (let ((new (make-vector (vector-length x))))
979933ab 1520 (if parent (set-annotation-stripped! parent new))
a63812a2
JB
1521 (let loop ((i (- (vector-length x) 1)))
1522 (unless (fx< i 0)
1523 (vector-set! new i (strip-annotation (vector-ref x i) #f))
1524 (loop (fx- i 1))))
1525 new))
1526 (else x))))
1527
1528;;; strips syntax-objects down to top-wrap; if top-wrap is layered directly
1529;;; on an annotation, strips the annotation as well.
1530;;; since only the head of a list is annotated by the reader, not each pair
1531;;; in the spine, we also check for pairs whose cars are annotated in case
1532;;; we've been passed the cdr of an annotated list
1533
1534(define strip
1535 (lambda (x w)
1536 (if (top-marked? w)
1537 (if (or (annotation? x) (and (pair? x) (annotation? (car x))))
1538 (strip-annotation x #f)
1539 x)
1540 (let f ((x x))
1541 (cond
1542 ((syntax-object? x)
1543 (strip (syntax-object-expression x) (syntax-object-wrap x)))
1544 ((pair? x)
1545 (let ((a (f (car x))) (d (f (cdr x))))
1546 (if (and (eq? a (car x)) (eq? d (cdr x)))
1547 x
1548 (cons a d))))
1549 ((vector? x)
1550 (let ((old (vector->list x)))
1551 (let ((new (map f old)))
4d248541 1552 (if (and-map* eq? old new) x (list->vector new)))))
a63812a2
JB
1553 (else x))))))
1554
1555;;; lexical variables
1556
1557(define gen-var
1558 (lambda (id)
1559 (let ((id (if (syntax-object? id) (syntax-object-expression id) id)))
1560 (if (annotation? id)
1561 (build-lexical-var (annotation-source id) (annotation-expression id))
1562 (build-lexical-var no-source id)))))
1563
1564(define lambda-var-list
1565 (lambda (vars)
1566 (let lvl ((vars vars) (ls '()) (w empty-wrap))
1567 (cond
4e237f14
AW
1568 ((pair? vars) (lvl (cdr vars) (cons (wrap (car vars) w #f) ls) w))
1569 ((id? vars) (cons (wrap vars w #f) ls))
a63812a2
JB
1570 ((null? vars) ls)
1571 ((syntax-object? vars)
1572 (lvl (syntax-object-expression vars)
1573 ls
1574 (join-wraps w (syntax-object-wrap vars))))
1575 ((annotation? vars)
1576 (lvl (annotation-expression vars) ls w))
1577 ; include anything else to be caught by subsequent error
1578 ; checking
1579 (else (cons vars ls))))))
1580
1581;;; core transformers
1582
1583(global-extend 'local-syntax 'letrec-syntax #t)
1584(global-extend 'local-syntax 'let-syntax #f)
1585
1586(global-extend 'core 'fluid-let-syntax
4e237f14 1587 (lambda (e r w s mod)
a63812a2
JB
1588 (syntax-case e ()
1589 ((_ ((var val) ...) e1 e2 ...)
1590 (valid-bound-ids? (syntax (var ...)))
1591 (let ((names (map (lambda (x) (id-var-name x w)) (syntax (var ...)))))
1592 (for-each
1593 (lambda (id n)
8e1d0d50 1594 (case (binding-type (lookup n r mod))
a63812a2 1595 ((displaced-lexical)
e4721dde
AW
1596 (syntax-violation 'fluid-let-syntax
1597 "identifier out of context"
1598 e
1599 (source-wrap id w s mod)))))
a63812a2
JB
1600 (syntax (var ...))
1601 names)
1602 (chi-body
1603 (syntax (e1 e2 ...))
4e237f14 1604 (source-wrap e w s mod)
a63812a2
JB
1605 (extend-env
1606 names
1607 (let ((trans-r (macros-only-env r)))
1608 (map (lambda (x)
1609 (make-binding 'macro
4e237f14
AW
1610 (eval-local-transformer (chi x trans-r w mod)
1611 mod)))
a63812a2
JB
1612 (syntax (val ...))))
1613 r)
4e237f14
AW
1614 w
1615 mod)))
e4721dde
AW
1616 (_ (syntax-violation 'fluid-let-syntax "bad syntax"
1617 (source-wrap e w s mod))))))
a63812a2
JB
1618
1619(global-extend 'core 'quote
4e237f14 1620 (lambda (e r w s mod)
a63812a2
JB
1621 (syntax-case e ()
1622 ((_ e) (build-data s (strip (syntax e) w)))
e4721dde
AW
1623 (_ (syntax-violation 'quote "bad syntax"
1624 (source-wrap e w s mod))))))
a63812a2
JB
1625
1626(global-extend 'core 'syntax
1627 (let ()
1628 (define gen-syntax
8e1d0d50 1629 (lambda (src e r maps ellipsis? mod)
a63812a2
JB
1630 (if (id? e)
1631 (let ((label (id-var-name e empty-wrap)))
8e1d0d50 1632 (let ((b (lookup label r mod)))
a63812a2
JB
1633 (if (eq? (binding-type b) 'syntax)
1634 (call-with-values
1635 (lambda ()
1636 (let ((var.lev (binding-value b)))
1637 (gen-ref src (car var.lev) (cdr var.lev) maps)))
1638 (lambda (var maps) (values `(ref ,var) maps)))
1639 (if (ellipsis? e)
e4721dde 1640 (syntax-violation 'syntax "misplaced ellipsis" src)
a63812a2
JB
1641 (values `(quote ,e) maps)))))
1642 (syntax-case e ()
1643 ((dots e)
1644 (ellipsis? (syntax dots))
8e1d0d50 1645 (gen-syntax src (syntax e) r maps (lambda (x) #f) mod))
a63812a2
JB
1646 ((x dots . y)
1647 ; this could be about a dozen lines of code, except that we
1648 ; choose to handle (syntax (x ... ...)) forms
1649 (ellipsis? (syntax dots))
1650 (let f ((y (syntax y))
1651 (k (lambda (maps)
1652 (call-with-values
1653 (lambda ()
1654 (gen-syntax src (syntax x) r
8e1d0d50 1655 (cons '() maps) ellipsis? mod))
a63812a2
JB
1656 (lambda (x maps)
1657 (if (null? (car maps))
e4721dde
AW
1658 (syntax-violation 'syntax "extra ellipsis"
1659 src)
a63812a2
JB
1660 (values (gen-map x (car maps))
1661 (cdr maps))))))))
1662 (syntax-case y ()
1663 ((dots . y)
1664 (ellipsis? (syntax dots))
1665 (f (syntax y)
1666 (lambda (maps)
1667 (call-with-values
1668 (lambda () (k (cons '() maps)))
1669 (lambda (x maps)
1670 (if (null? (car maps))
e4721dde 1671 (syntax-violation 'syntax "extra ellipsis" src)
a63812a2
JB
1672 (values (gen-mappend x (car maps))
1673 (cdr maps))))))))
1674 (_ (call-with-values
8e1d0d50 1675 (lambda () (gen-syntax src y r maps ellipsis? mod))
a63812a2
JB
1676 (lambda (y maps)
1677 (call-with-values
1678 (lambda () (k maps))
1679 (lambda (x maps)
1680 (values (gen-append x y) maps)))))))))
1681 ((x . y)
1682 (call-with-values
8e1d0d50 1683 (lambda () (gen-syntax src (syntax x) r maps ellipsis? mod))
a63812a2
JB
1684 (lambda (x maps)
1685 (call-with-values
8e1d0d50 1686 (lambda () (gen-syntax src (syntax y) r maps ellipsis? mod))
a63812a2
JB
1687 (lambda (y maps) (values (gen-cons x y) maps))))))
1688 (#(e1 e2 ...)
1689 (call-with-values
1690 (lambda ()
8e1d0d50 1691 (gen-syntax src (syntax (e1 e2 ...)) r maps ellipsis? mod))
a63812a2
JB
1692 (lambda (e maps) (values (gen-vector e) maps))))
1693 (_ (values `(quote ,e) maps))))))
1694
1695 (define gen-ref
1696 (lambda (src var level maps)
1697 (if (fx= level 0)
1698 (values var maps)
1699 (if (null? maps)
e4721dde 1700 (syntax-violation 'syntax "missing ellipsis" src)
a63812a2
JB
1701 (call-with-values
1702 (lambda () (gen-ref src var (fx- level 1) (cdr maps)))
1703 (lambda (outer-var outer-maps)
1704 (let ((b (assq outer-var (car maps))))
1705 (if b
1706 (values (cdr b) maps)
1707 (let ((inner-var (gen-var 'tmp)))
1708 (values inner-var
1709 (cons (cons (cons outer-var inner-var)
1710 (car maps))
1711 outer-maps)))))))))))
1712
1713 (define gen-mappend
1714 (lambda (e map-env)
1715 `(apply (primitive append) ,(gen-map e map-env))))
1716
1717 (define gen-map
1718 (lambda (e map-env)
1719 (let ((formals (map cdr map-env))
1720 (actuals (map (lambda (x) `(ref ,(car x))) map-env)))
1721 (cond
1722 ((eq? (car e) 'ref)
1723 ; identity map equivalence:
1724 ; (map (lambda (x) x) y) == y
1725 (car actuals))
4d248541 1726 ((and-map
a63812a2
JB
1727 (lambda (x) (and (eq? (car x) 'ref) (memq (cadr x) formals)))
1728 (cdr e))
1729 ; eta map equivalence:
1730 ; (map (lambda (x ...) (f x ...)) y ...) == (map f y ...)
1731 `(map (primitive ,(car e))
1732 ,@(map (let ((r (map cons formals actuals)))
1733 (lambda (x) (cdr (assq (cadr x) r))))
1734 (cdr e))))
1735 (else `(map (lambda ,formals ,e) ,@actuals))))))
1736
1737 (define gen-cons
1738 (lambda (x y)
1739 (case (car y)
1740 ((quote)
1741 (if (eq? (car x) 'quote)
1742 `(quote (,(cadr x) . ,(cadr y)))
1743 (if (eq? (cadr y) '())
1744 `(list ,x)
1745 `(cons ,x ,y))))
1746 ((list) `(list ,x ,@(cdr y)))
1747 (else `(cons ,x ,y)))))
1748
1749 (define gen-append
1750 (lambda (x y)
1751 (if (equal? y '(quote ()))
1752 x
1753 `(append ,x ,y))))
1754
1755 (define gen-vector
1756 (lambda (x)
1757 (cond
1758 ((eq? (car x) 'list) `(vector ,@(cdr x)))
1759 ((eq? (car x) 'quote) `(quote #(,@(cadr x))))
1760 (else `(list->vector ,x)))))
1761
1762
1763 (define regen
1764 (lambda (x)
1765 (case (car x)
f4a644ee 1766 ((ref) (build-lexical-reference 'value no-source (cadr x) (cadr x)))
a63812a2
JB
1767 ((primitive) (build-primref no-source (cadr x)))
1768 ((quote) (build-data no-source (cadr x)))
547a602d 1769 ((lambda) (build-lambda no-source (cadr x) (cadr x) #f (regen (caddr x))))
a63812a2
JB
1770 ((map) (let ((ls (map regen (cdr x))))
1771 (build-application no-source
811d10f5
AW
1772 ;; this check used to be here, not sure what for:
1773 ;; (if (fx= (length ls) 2)
1774 (build-primref no-source 'map)
a63812a2
JB
1775 ls)))
1776 (else (build-application no-source
1777 (build-primref no-source (car x))
1778 (map regen (cdr x)))))))
1779
4e237f14
AW
1780 (lambda (e r w s mod)
1781 (let ((e (source-wrap e w s mod)))
a63812a2
JB
1782 (syntax-case e ()
1783 ((_ x)
1784 (call-with-values
8e1d0d50 1785 (lambda () (gen-syntax e (syntax x) r '() ellipsis? mod))
a63812a2 1786 (lambda (e maps) (regen e))))
e4721dde 1787 (_ (syntax-violation 'syntax "bad `syntax' form" e)))))))
a63812a2
JB
1788
1789
1790(global-extend 'core 'lambda
4e237f14 1791 (lambda (e r w s mod)
a63812a2
JB
1792 (syntax-case e ()
1793 ((_ . c)
0ee32d01 1794 (chi-lambda-clause (source-wrap e w s mod) #f (syntax c) r w mod
547a602d
AW
1795 (lambda (names vars docstring body)
1796 (build-lambda s names vars docstring body)))))))
a63812a2
JB
1797
1798
1799(global-extend 'core 'let
1800 (let ()
4e237f14 1801 (define (chi-let e r w s mod constructor ids vals exps)
a63812a2 1802 (if (not (valid-bound-ids? ids))
e4721dde 1803 (syntax-violation 'let "duplicate bound variable" e)
a63812a2
JB
1804 (let ((labels (gen-labels ids))
1805 (new-vars (map gen-var ids)))
1806 (let ((nw (make-binding-wrap ids labels w))
1807 (nr (extend-var-env labels new-vars r)))
1808 (constructor s
547a602d 1809 (map syntax->datum ids)
a63812a2 1810 new-vars
4e237f14
AW
1811 (map (lambda (x) (chi x r w mod)) vals)
1812 (chi-body exps (source-wrap e nw s mod)
1813 nr nw mod))))))
1814 (lambda (e r w s mod)
a63812a2
JB
1815 (syntax-case e ()
1816 ((_ ((id val) ...) e1 e2 ...)
4e237f14 1817 (chi-let e r w s mod
a63812a2
JB
1818 build-let
1819 (syntax (id ...))
1820 (syntax (val ...))
1821 (syntax (e1 e2 ...))))
1822 ((_ f ((id val) ...) e1 e2 ...)
1823 (id? (syntax f))
4e237f14 1824 (chi-let e r w s mod
a63812a2
JB
1825 build-named-let
1826 (syntax (f id ...))
1827 (syntax (val ...))
1828 (syntax (e1 e2 ...))))
e4721dde 1829 (_ (syntax-violation 'let "bad let" (source-wrap e w s mod)))))))
a63812a2
JB
1830
1831
1832(global-extend 'core 'letrec
4e237f14 1833 (lambda (e r w s mod)
a63812a2
JB
1834 (syntax-case e ()
1835 ((_ ((id val) ...) e1 e2 ...)
1836 (let ((ids (syntax (id ...))))
1837 (if (not (valid-bound-ids? ids))
e4721dde 1838 (syntax-violation 'letrec "duplicate bound variable" e)
a63812a2
JB
1839 (let ((labels (gen-labels ids))
1840 (new-vars (map gen-var ids)))
1841 (let ((w (make-binding-wrap ids labels w))
1842 (r (extend-var-env labels new-vars r)))
1843 (build-letrec s
547a602d 1844 (map syntax->datum ids)
a63812a2 1845 new-vars
4e237f14
AW
1846 (map (lambda (x) (chi x r w mod)) (syntax (val ...)))
1847 (chi-body (syntax (e1 e2 ...))
1848 (source-wrap e w s mod) r w mod)))))))
e4721dde 1849 (_ (syntax-violation 'letrec "bad letrec" (source-wrap e w s mod))))))
a63812a2
JB
1850
1851
1852(global-extend 'core 'set!
4e237f14 1853 (lambda (e r w s mod)
a63812a2
JB
1854 (syntax-case e ()
1855 ((_ id val)
1856 (id? (syntax id))
4e237f14 1857 (let ((val (chi (syntax val) r w mod))
a63812a2 1858 (n (id-var-name (syntax id) w)))
8e1d0d50 1859 (let ((b (lookup n r mod)))
a63812a2
JB
1860 (case (binding-type b)
1861 ((lexical)
f4a644ee
AW
1862 (build-lexical-assignment s
1863 (syntax->datum (syntax id))
1864 (binding-value b)
1865 val))
4e237f14 1866 ((global) (build-global-assignment s n val mod))
a63812a2 1867 ((displaced-lexical)
e4721dde
AW
1868 (syntax-violation 'set! "identifier out of context"
1869 (wrap (syntax id) w mod)))
1870 (else (syntax-violation 'set! "bad set!"
1871 (source-wrap e w s mod)))))))
dec62b5e
AW
1872 ((_ (head tail ...) val)
1873 (call-with-values
1874 (lambda () (syntax-type (syntax head) r empty-wrap no-source #f mod))
1875 (lambda (type value ee ww ss modmod)
1876 (case type
1877 ((module-ref)
01c161ca
AW
1878 (let ((val (chi (syntax val) r w mod)))
1879 (call-with-values (lambda () (value (syntax (head tail ...))))
1880 (lambda (id mod)
1881 (build-global-assignment s id val mod)))))
dec62b5e
AW
1882 (else
1883 (build-application s
1884 (chi (syntax (setter head)) r w mod)
1885 (map (lambda (e) (chi e r w mod))
1886 (syntax (tail ... val)))))))))
e4721dde 1887 (_ (syntax-violation 'set! "bad set!" (source-wrap e w s mod))))))
a63812a2 1888
265e6127 1889(global-extend 'module-ref '@
dec62b5e 1890 (lambda (e)
384e92b3 1891 (syntax-case e ()
265e6127 1892 ((_ (mod ...) id)
4d248541 1893 (and (and-map id? (syntax (mod ...))) (id? (syntax id)))
22225fc1
AW
1894 (values (syntax->datum (syntax id))
1895 (syntax->datum
a2716cbe 1896 (syntax (public mod ...))))))))
265e6127
AW
1897
1898(global-extend 'module-ref '@@
dec62b5e 1899 (lambda (e)
265e6127
AW
1900 (syntax-case e ()
1901 ((_ (mod ...) id)
4d248541 1902 (and (and-map id? (syntax (mod ...))) (id? (syntax id)))
22225fc1
AW
1903 (values (syntax->datum (syntax id))
1904 (syntax->datum
a2716cbe 1905 (syntax (private mod ...))))))))
265e6127 1906
a1a482e0
AW
1907(global-extend 'core 'if
1908 (lambda (e r w s mod)
1909 (syntax-case e ()
1910 ((_ test then)
1911 (build-conditional
1912 s
1913 (chi (syntax test) r w mod)
1914 (chi (syntax then) r w mod)
1915 (build-void no-source)))
1916 ((_ test then else)
1917 (build-conditional
1918 s
1919 (chi (syntax test) r w mod)
1920 (chi (syntax then) r w mod)
1921 (chi (syntax else) r w mod))))))
1922
a63812a2
JB
1923(global-extend 'begin 'begin '())
1924
1925(global-extend 'define 'define '())
1926
1927(global-extend 'define-syntax 'define-syntax '())
1928
1929(global-extend 'eval-when 'eval-when '())
1930
1931(global-extend 'core 'syntax-case
1932 (let ()
1933 (define convert-pattern
1934 ; accepts pattern & keys
5f1a2fb1 1935 ; returns $sc-dispatch pattern & ids
a63812a2
JB
1936 (lambda (pattern keys)
1937 (let cvt ((p pattern) (n 0) (ids '()))
1938 (if (id? p)
1939 (if (bound-id-member? p keys)
1940 (values (vector 'free-id p) ids)
1941 (values 'any (cons (cons p n) ids)))
1942 (syntax-case p ()
1943 ((x dots)
1944 (ellipsis? (syntax dots))
1945 (call-with-values
1946 (lambda () (cvt (syntax x) (fx+ n 1) ids))
1947 (lambda (p ids)
1948 (values (if (eq? p 'any) 'each-any (vector 'each p))
1949 ids))))
1950 ((x . y)
1951 (call-with-values
1952 (lambda () (cvt (syntax y) n ids))
1953 (lambda (y ids)
1954 (call-with-values
1955 (lambda () (cvt (syntax x) n ids))
1956 (lambda (x ids)
1957 (values (cons x y) ids))))))
1958 (() (values '() ids))
1959 (#(x ...)
1960 (call-with-values
1961 (lambda () (cvt (syntax (x ...)) n ids))
1962 (lambda (p ids) (values (vector 'vector p) ids))))
1963 (x (values (vector 'atom (strip p empty-wrap)) ids)))))))
1964
1965 (define build-dispatch-call
4e237f14 1966 (lambda (pvars exp y r mod)
a63812a2
JB
1967 (let ((ids (map car pvars)) (levels (map cdr pvars)))
1968 (let ((labels (gen-labels ids)) (new-vars (map gen-var ids)))
1969 (build-application no-source
1970 (build-primref no-source 'apply)
547a602d 1971 (list (build-lambda no-source (map syntax->datum ids) new-vars #f
a63812a2 1972 (chi exp
4e237f14
AW
1973 (extend-env
1974 labels
1975 (map (lambda (var level)
1976 (make-binding 'syntax `(,var . ,level)))
1977 new-vars
1978 (map cdr pvars))
1979 r)
1980 (make-binding-wrap ids labels empty-wrap)
1981 mod))
a63812a2
JB
1982 y))))))
1983
1984 (define gen-clause
4e237f14 1985 (lambda (x keys clauses r pat fender exp mod)
a63812a2
JB
1986 (call-with-values
1987 (lambda () (convert-pattern pat keys))
1988 (lambda (p pvars)
1989 (cond
1990 ((not (distinct-bound-ids? (map car pvars)))
e4721dde 1991 (syntax-violation 'syntax-case "duplicate pattern variable" pat))
4d248541 1992 ((not (and-map (lambda (x) (not (ellipsis? (car x)))) pvars))
e4721dde 1993 (syntax-violation 'syntax-case "misplaced ellipsis" pat))
a63812a2
JB
1994 (else
1995 (let ((y (gen-var 'tmp)))
1996 ; fat finger binding and references to temp variable y
1997 (build-application no-source
547a602d 1998 (build-lambda no-source (list 'tmp) (list y) #f
f4a644ee
AW
1999 (let ((y (build-lexical-reference 'value no-source
2000 'tmp y)))
a63812a2
JB
2001 (build-conditional no-source
2002 (syntax-case fender ()
2003 (#t y)
2004 (_ (build-conditional no-source
2005 y
4e237f14 2006 (build-dispatch-call pvars fender y r mod)
a63812a2 2007 (build-data no-source #f))))
4e237f14
AW
2008 (build-dispatch-call pvars exp y r mod)
2009 (gen-syntax-case x keys clauses r mod))))
a63812a2
JB
2010 (list (if (eq? p 'any)
2011 (build-application no-source
2012 (build-primref no-source 'list)
2013 (list x))
2014 (build-application no-source
5f1a2fb1 2015 (build-primref no-source '$sc-dispatch)
a63812a2
JB
2016 (list x (build-data no-source p)))))))))))))
2017
2018 (define gen-syntax-case
4e237f14 2019 (lambda (x keys clauses r mod)
a63812a2
JB
2020 (if (null? clauses)
2021 (build-application no-source
e4721dde
AW
2022 (build-primref no-source 'syntax-violation)
2023 (list #f "source expression failed to match any pattern" x))
a63812a2
JB
2024 (syntax-case (car clauses) ()
2025 ((pat exp)
2026 (if (and (id? (syntax pat))
4d248541
AW
2027 (and-map (lambda (x) (not (free-id=? (syntax pat) x)))
2028 (cons (syntax (... ...)) keys)))
a63812a2
JB
2029 (let ((labels (list (gen-label)))
2030 (var (gen-var (syntax pat))))
2031 (build-application no-source
547a602d
AW
2032 (build-lambda no-source
2033 (list (syntax->datum (syntax pat))) (list var)
2034 #f
a63812a2
JB
2035 (chi (syntax exp)
2036 (extend-env labels
2037 (list (make-binding 'syntax `(,var . 0)))
2038 r)
2039 (make-binding-wrap (syntax (pat))
4e237f14
AW
2040 labels empty-wrap)
2041 mod))
a63812a2
JB
2042 (list x)))
2043 (gen-clause x keys (cdr clauses) r
4e237f14 2044 (syntax pat) #t (syntax exp) mod)))
a63812a2
JB
2045 ((pat fender exp)
2046 (gen-clause x keys (cdr clauses) r
4e237f14 2047 (syntax pat) (syntax fender) (syntax exp) mod))
e4721dde
AW
2048 (_ (syntax-violation 'syntax-case "invalid clause"
2049 (car clauses)))))))
a63812a2 2050
4e237f14
AW
2051 (lambda (e r w s mod)
2052 (let ((e (source-wrap e w s mod)))
a63812a2
JB
2053 (syntax-case e ()
2054 ((_ val (key ...) m ...)
4d248541
AW
2055 (if (and-map (lambda (x) (and (id? x) (not (ellipsis? x))))
2056 (syntax (key ...)))
a63812a2
JB
2057 (let ((x (gen-var 'tmp)))
2058 ; fat finger binding and references to temp variable x
2059 (build-application s
547a602d 2060 (build-lambda no-source (list 'tmp) (list x) #f
f4a644ee
AW
2061 (gen-syntax-case (build-lexical-reference 'value no-source
2062 'tmp x)
a63812a2 2063 (syntax (key ...)) (syntax (m ...))
4e237f14
AW
2064 r
2065 mod))
2066 (list (chi (syntax val) r empty-wrap mod))))
e4721dde 2067 (syntax-violation 'syntax-case "invalid literals list" e))))))))
a63812a2
JB
2068
2069;;; The portable sc-expand seeds chi-top's mode m with 'e (for
2070;;; evaluating) and esew (which stands for "eval syntax expanders
2071;;; when") with '(eval). In Chez Scheme, m is set to 'c instead of e
2072;;; if we are compiling a file, and esew is set to
2073;;; (eval-syntactic-expanders-when), which defaults to the list
2074;;; '(compile load eval). This means that, by default, top-level
2075;;; syntactic definitions are evaluated immediately after they are
2076;;; expanded, and the expanded definitions are also residualized into
2077;;; the object file if we are compiling a file.
2078(set! sc-expand
71f46dbd
AW
2079 (lambda (x . rest)
2080 (if (and (pair? x) (equal? (car x) noexpand))
2081 (cadr x)
2082 (let ((m (if (null? rest) 'e (car rest)))
2083 (esew (if (or (null? rest) (null? (cdr rest)))
2084 '(eval)
2085 (cadr rest))))
2086 (with-fluid* *mode* m
2087 (lambda ()
2088 (chi-top x null-env top-wrap m esew
2089 (cons 'hygiene (module-name (current-module))))))))))
a63812a2
JB
2090
2091(set! identifier?
2092 (lambda (x)
2093 (nonsymbol-id? x)))
2094
22225fc1 2095(set! datum->syntax
a63812a2 2096 (lambda (id datum)
e02e84de 2097 (make-syntax-object datum (syntax-object-wrap id) #f)))
a63812a2 2098
22225fc1 2099(set! syntax->datum
a63812a2
JB
2100 ; accepts any object, since syntax objects may consist partially
2101 ; or entirely of unwrapped, nonsymbolic data
2102 (lambda (x)
2103 (strip x empty-wrap)))
2104
2105(set! generate-temporaries
2106 (lambda (ls)
2107 (arg-check list? ls 'generate-temporaries)
4e237f14 2108 (map (lambda (x) (wrap (gensym-hook) top-wrap #f)) ls)))
a63812a2
JB
2109
2110(set! free-identifier=?
2111 (lambda (x y)
2112 (arg-check nonsymbol-id? x 'free-identifier=?)
2113 (arg-check nonsymbol-id? y 'free-identifier=?)
2114 (free-id=? x y)))
2115
2116(set! bound-identifier=?
2117 (lambda (x y)
2118 (arg-check nonsymbol-id? x 'bound-identifier=?)
2119 (arg-check nonsymbol-id? y 'bound-identifier=?)
2120 (bound-id=? x y)))
2121
e4721dde
AW
2122(set! syntax-violation
2123 (lambda (who message form . subform)
2124 (arg-check (lambda (x) (or (not x) (string? x) (symbol? x)))
2125 who 'syntax-violation)
2126 (arg-check string? message 'syntax-violation)
2127 (scm-error 'syntax-error 'sc-expand
2128 (string-append
2129 (if who "~a: " "")
2130 "~a "
2131 (if (null? subform) "in ~a" "in subform `~s' of `~s'"))
2132 (let ((tail (cons message
2133 (map (lambda (x) (strip x empty-wrap))
2134 (append subform (list form))))))
2135 (if who (cons who tail) tail))
2136 #f)))
a63812a2 2137
5f1a2fb1 2138;;; $sc-dispatch expects an expression and a pattern. If the expression
a63812a2
JB
2139;;; matches the pattern a list of the matching expressions for each
2140;;; "any" is returned. Otherwise, #f is returned. (This use of #f will
2141;;; not work on r4rs implementations that violate the ieee requirement
2142;;; that #f and () be distinct.)
2143
2144;;; The expression is matched with the pattern as follows:
2145
2146;;; pattern: matches:
2147;;; () empty list
2148;;; any anything
2149;;; (<pattern>1 . <pattern>2) (<pattern>1 . <pattern>2)
2150;;; each-any (any*)
2151;;; #(free-id <key>) <key> with free-identifier=?
2152;;; #(each <pattern>) (<pattern>*)
2153;;; #(vector <pattern>) (list->vector <pattern>)
2154;;; #(atom <object>) <object> with "equal?"
2155
2156;;; Vector cops out to pair under assumption that vectors are rare. If
2157;;; not, should convert to:
2158;;; #(vector <pattern>*) #(<pattern>*)
2159
2160(let ()
2161
2162(define match-each
d2b61fe0 2163 (lambda (e p w mod)
a63812a2
JB
2164 (cond
2165 ((annotation? e)
d2b61fe0 2166 (match-each (annotation-expression e) p w mod))
a63812a2 2167 ((pair? e)
d2b61fe0 2168 (let ((first (match (car e) p w '() mod)))
a63812a2 2169 (and first
d2b61fe0 2170 (let ((rest (match-each (cdr e) p w mod)))
a63812a2
JB
2171 (and rest (cons first rest))))))
2172 ((null? e) '())
2173 ((syntax-object? e)
2174 (match-each (syntax-object-expression e)
2175 p
d2b61fe0
AW
2176 (join-wraps w (syntax-object-wrap e))
2177 (syntax-object-module e)))
a63812a2
JB
2178 (else #f))))
2179
2180(define match-each-any
d2b61fe0 2181 (lambda (e w mod)
a63812a2
JB
2182 (cond
2183 ((annotation? e)
d2b61fe0 2184 (match-each-any (annotation-expression e) w mod))
a63812a2 2185 ((pair? e)
d2b61fe0
AW
2186 (let ((l (match-each-any (cdr e) w mod)))
2187 (and l (cons (wrap (car e) w mod) l))))
a63812a2
JB
2188 ((null? e) '())
2189 ((syntax-object? e)
2190 (match-each-any (syntax-object-expression e)
d2b61fe0
AW
2191 (join-wraps w (syntax-object-wrap e))
2192 mod))
a63812a2
JB
2193 (else #f))))
2194
2195(define match-empty
2196 (lambda (p r)
2197 (cond
2198 ((null? p) r)
2199 ((eq? p 'any) (cons '() r))
2200 ((pair? p) (match-empty (car p) (match-empty (cdr p) r)))
2201 ((eq? p 'each-any) (cons '() r))
2202 (else
2203 (case (vector-ref p 0)
2204 ((each) (match-empty (vector-ref p 1) r))
2205 ((free-id atom) r)
2206 ((vector) (match-empty (vector-ref p 1) r)))))))
2207
2208(define match*
d2b61fe0 2209 (lambda (e p w r mod)
a63812a2
JB
2210 (cond
2211 ((null? p) (and (null? e) r))
2212 ((pair? p)
2213 (and (pair? e) (match (car e) (car p) w
d2b61fe0
AW
2214 (match (cdr e) (cdr p) w r mod)
2215 mod)))
a63812a2 2216 ((eq? p 'each-any)
d2b61fe0 2217 (let ((l (match-each-any e w mod))) (and l (cons l r))))
a63812a2
JB
2218 (else
2219 (case (vector-ref p 0)
2220 ((each)
2221 (if (null? e)
2222 (match-empty (vector-ref p 1) r)
d2b61fe0 2223 (let ((l (match-each e (vector-ref p 1) w mod)))
a63812a2
JB
2224 (and l
2225 (let collect ((l l))
2226 (if (null? (car l))
2227 r
2228 (cons (map car l) (collect (map cdr l)))))))))
d2b61fe0 2229 ((free-id) (and (id? e) (free-id=? (wrap e w mod) (vector-ref p 1)) r))
a63812a2
JB
2230 ((atom) (and (equal? (vector-ref p 1) (strip e w)) r))
2231 ((vector)
2232 (and (vector? e)
d2b61fe0 2233 (match (vector->list e) (vector-ref p 1) w r mod))))))))
a63812a2
JB
2234
2235(define match
d2b61fe0 2236 (lambda (e p w r mod)
a63812a2
JB
2237 (cond
2238 ((not r) #f)
d2b61fe0 2239 ((eq? p 'any) (cons (wrap e w mod) r))
a63812a2
JB
2240 ((syntax-object? e)
2241 (match*
2242 (unannotate (syntax-object-expression e))
2243 p
2244 (join-wraps w (syntax-object-wrap e))
d2b61fe0
AW
2245 r
2246 (syntax-object-module e)))
2247 (else (match* (unannotate e) p w r mod)))))
a63812a2 2248
5f1a2fb1 2249(set! $sc-dispatch
a63812a2
JB
2250 (lambda (e p)
2251 (cond
2252 ((eq? p 'any) (list e))
2253 ((syntax-object? e)
2254 (match* (unannotate (syntax-object-expression e))
d2b61fe0
AW
2255 p (syntax-object-wrap e) '() (syntax-object-module e)))
2256 (else (match* (unannotate e) p empty-wrap '() #f)))))
80f225df 2257
a63812a2
JB
2258))
2259)
2260
2261(define-syntax with-syntax
2262 (lambda (x)
2263 (syntax-case x ()
2264 ((_ () e1 e2 ...)
2265 (syntax (begin e1 e2 ...)))
2266 ((_ ((out in)) e1 e2 ...)
2267 (syntax (syntax-case in () (out (begin e1 e2 ...)))))
2268 ((_ ((out in) ...) e1 e2 ...)
2269 (syntax (syntax-case (list in ...) ()
2270 ((out ...) (begin e1 e2 ...))))))))
2271
2272(define-syntax syntax-rules
2273 (lambda (x)
2274 (syntax-case x ()
2275 ((_ (k ...) ((keyword . pattern) template) ...)
2276 (syntax (lambda (x)
2277 (syntax-case x (k ...)
2278 ((dummy . pattern) (syntax template))
2279 ...)))))))
2280
2281(define-syntax let*
2282 (lambda (x)
2283 (syntax-case x ()
2284 ((let* ((x v) ...) e1 e2 ...)
4d248541 2285 (and-map identifier? (syntax (x ...)))
a63812a2
JB
2286 (let f ((bindings (syntax ((x v) ...))))
2287 (if (null? bindings)
2288 (syntax (let () e1 e2 ...))
2289 (with-syntax ((body (f (cdr bindings)))
2290 (binding (car bindings)))
2291 (syntax (let (binding) body)))))))))
2292
2293(define-syntax do
2294 (lambda (orig-x)
2295 (syntax-case orig-x ()
2296 ((_ ((var init . step) ...) (e0 e1 ...) c ...)
2297 (with-syntax (((step ...)
2298 (map (lambda (v s)
2299 (syntax-case s ()
2300 (() v)
2301 ((e) (syntax e))
e4721dde
AW
2302 (_ (syntax-violation
2303 'do "bad step expression"
2304 orig-x s))))
a63812a2
JB
2305 (syntax (var ...))
2306 (syntax (step ...)))))
2307 (syntax-case (syntax (e1 ...)) ()
2308 (() (syntax (let doloop ((var init) ...)
2309 (if (not e0)
2310 (begin c ... (doloop step ...))))))
2311 ((e1 e2 ...)
2312 (syntax (let doloop ((var init) ...)
2313 (if e0
2314 (begin e1 e2 ...)
2315 (begin c ... (doloop step ...))))))))))))
2316
2317(define-syntax quasiquote
2318 (letrec
2319 ((quasicons
2320 (lambda (x y)
2321 (with-syntax ((x x) (y y))
2322 (syntax-case (syntax y) (quote list)
2323 ((quote dy)
2324 (syntax-case (syntax x) (quote)
2325 ((quote dx) (syntax (quote (dx . dy))))
2326 (_ (if (null? (syntax dy))
2327 (syntax (list x))
2328 (syntax (cons x y))))))
2329 ((list . stuff) (syntax (list x . stuff)))
2330 (else (syntax (cons x y)))))))
2331 (quasiappend
2332 (lambda (x y)
2333 (with-syntax ((x x) (y y))
2334 (syntax-case (syntax y) (quote)
2335 ((quote ()) (syntax x))
2336 (_ (syntax (append x y)))))))
2337 (quasivector
2338 (lambda (x)
2339 (with-syntax ((x x))
2340 (syntax-case (syntax x) (quote list)
2341 ((quote (x ...)) (syntax (quote #(x ...))))
2342 ((list x ...) (syntax (vector x ...)))
2343 (_ (syntax (list->vector x)))))))
2344 (quasi
2345 (lambda (p lev)
2346 (syntax-case p (unquote unquote-splicing quasiquote)
2347 ((unquote p)
2348 (if (= lev 0)
2349 (syntax p)
2350 (quasicons (syntax (quote unquote))
2351 (quasi (syntax (p)) (- lev 1)))))
2352 (((unquote-splicing p) . q)
2353 (if (= lev 0)
2354 (quasiappend (syntax p) (quasi (syntax q) lev))
2355 (quasicons (quasicons (syntax (quote unquote-splicing))
2356 (quasi (syntax (p)) (- lev 1)))
2357 (quasi (syntax q) lev))))
2358 ((quasiquote p)
2359 (quasicons (syntax (quote quasiquote))
2360 (quasi (syntax (p)) (+ lev 1))))
2361 ((p . q)
2362 (quasicons (quasi (syntax p) lev) (quasi (syntax q) lev)))
2363 (#(x ...) (quasivector (quasi (syntax (x ...)) lev)))
2364 (p (syntax (quote p)))))))
2365 (lambda (x)
2366 (syntax-case x ()
2367 ((_ e) (quasi (syntax e) 0))))))
2368
2369(define-syntax include
2370 (lambda (x)
2371 (define read-file
2372 (lambda (fn k)
2373 (let ((p (open-input-file fn)))
2374 (let f ((x (read p)))
2375 (if (eof-object? x)
2376 (begin (close-input-port p) '())
22225fc1 2377 (cons (datum->syntax k x)
a63812a2
JB
2378 (f (read p))))))))
2379 (syntax-case x ()
2380 ((k filename)
22225fc1 2381 (let ((fn (syntax->datum (syntax filename))))
a63812a2
JB
2382 (with-syntax (((exp ...) (read-file fn (syntax k))))
2383 (syntax (begin exp ...))))))))
2384
2385(define-syntax unquote
6a952e0e
AW
2386 (lambda (x)
2387 (syntax-case x ()
2388 ((_ e)
2389 (syntax-violation 'unquote
2390 "expression not valid outside of quasiquote"
2391 x)))))
a63812a2
JB
2392
2393(define-syntax unquote-splicing
6a952e0e
AW
2394 (lambda (x)
2395 (syntax-case x ()
2396 ((_ e)
2397 (syntax-violation 'unquote-splicing
2398 "expression not valid outside of quasiquote"
2399 x)))))
a63812a2
JB
2400
2401(define-syntax case
2402 (lambda (x)
2403 (syntax-case x ()
2404 ((_ e m1 m2 ...)
2405 (with-syntax
2406 ((body (let f ((clause (syntax m1)) (clauses (syntax (m2 ...))))
2407 (if (null? clauses)
2408 (syntax-case clause (else)
2409 ((else e1 e2 ...) (syntax (begin e1 e2 ...)))
2410 (((k ...) e1 e2 ...)
2411 (syntax (if (memv t '(k ...)) (begin e1 e2 ...))))
e4721dde 2412 (_ (syntax-violation 'case "bad clause" x clause)))
a63812a2
JB
2413 (with-syntax ((rest (f (car clauses) (cdr clauses))))
2414 (syntax-case clause (else)
2415 (((k ...) e1 e2 ...)
2416 (syntax (if (memv t '(k ...))
2417 (begin e1 e2 ...)
2418 rest)))
e4721dde
AW
2419 (_ (syntax-violation 'case "bad clause" x
2420 clause))))))))
a63812a2
JB
2421 (syntax (let ((t e)) body)))))))
2422
2423(define-syntax identifier-syntax
2424 (lambda (x)
2425 (syntax-case x ()
2426 ((_ e)
2427 (syntax
2428 (lambda (x)
2429 (syntax-case x ()
2430 (id
2431 (identifier? (syntax id))
2432 (syntax e))
2433 ((_ x (... ...))
2434 (syntax (e x (... ...)))))))))))