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