Merge remote-tracking branch 'origin/stable-2.0'
[bpt/guile.git] / module / ice-9 / psyntax.scm
CommitLineData
677cd590
RB
1;;;; -*-scheme-*-
2;;;;
f78a1cce
LC
3;;;; Copyright (C) 2001, 2003, 2006, 2009, 2010, 2011,
4;;;; 2012 Free Software Foundation, Inc.
5;;;;
73be1d9e
MV
6;;;; This library is free software; you can redistribute it and/or
7;;;; modify it under the terms of the GNU Lesser General Public
8;;;; License as published by the Free Software Foundation; either
53befeb7 9;;;; version 3 of the License, or (at your option) any later version.
86b96c16 10;;;;
73be1d9e 11;;;; This library is distributed in the hope that it will be useful,
86b96c16 12;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
73be1d9e
MV
13;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14;;;; Lesser General Public License for more details.
86b96c16 15;;;;
73be1d9e
MV
16;;;; You should have received a copy of the GNU Lesser General Public
17;;;; License along with this library; if not, write to the Free Software
92205699 18;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
86b96c16
MD
19;;;;
20\f
21
a63812a2 22;;; Portable implementation of syntax-case
565c8e30 23;;; Originally extracted from Chez Scheme Version 5.9f
a63812a2
JB
24;;; Authors: R. Kent Dybvig, Oscar Waddell, Bob Hieb, Carl Bruggeman
25
26;;; Copyright (c) 1992-1997 Cadence Research Systems
27;;; Permission to copy this software, in whole or in part, to use this
28;;; software for any lawful purpose, and to redistribute this software
29;;; is granted subject to the restriction that all copies made of this
30;;; software must include this copyright notice in full. This software
31;;; is provided AS IS, with NO WARRANTY, EITHER EXPRESS OR IMPLIED,
32;;; INCLUDING BUT NOT LIMITED TO IMPLIED WARRANTIES OF MERCHANTABILITY
33;;; OR FITNESS FOR ANY PARTICULAR PURPOSE. IN NO EVENT SHALL THE
34;;; AUTHORS BE LIABLE FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES OF ANY
35;;; NATURE WHATSOEVER.
36
565c8e30
AW
37;;; Modified by Mikael Djurfeldt <djurfeldt@nada.kth.se> according
38;;; to the ChangeLog distributed in the same directory as this file:
39;;; 1997-08-19, 1997-09-03, 1997-09-10, 2000-08-13, 2000-08-24,
40;;; 2000-09-12, 2001-03-08
41
42;;; Modified by Andy Wingo <wingo@pobox.com> according to the Git
43;;; revision control logs corresponding to this file: 2009, 2010.
a63812a2
JB
44
45
8a73a6d2 46;;; This file defines the syntax-case expander, macroexpand, and a set
a63812a2
JB
47;;; of associated syntactic forms and procedures. Of these, the
48;;; following are documented in The Scheme Programming Language,
565c8e30
AW
49;;; Fourth Edition (R. Kent Dybvig, MIT Press, 2009), and in the
50;;; R6RS:
a63812a2
JB
51;;;
52;;; bound-identifier=?
22225fc1 53;;; datum->syntax
a63812a2 54;;; define-syntax
ea3ca4e4 55;;; syntax-parameterize
a63812a2
JB
56;;; free-identifier=?
57;;; generate-temporaries
58;;; identifier?
59;;; identifier-syntax
60;;; let-syntax
61;;; letrec-syntax
62;;; syntax
63;;; syntax-case
22225fc1 64;;; syntax->datum
a63812a2
JB
65;;; syntax-rules
66;;; with-syntax
67;;;
565c8e30
AW
68;;; Additionally, the expander provides definitions for a number of core
69;;; Scheme syntactic bindings, such as `let', `lambda', and the like.
a63812a2
JB
70
71;;; The remaining exports are listed below:
72;;;
8a73a6d2
AW
73;;; (macroexpand datum)
74;;; if datum represents a valid expression, macroexpand returns an
a63812a2
JB
75;;; expanded version of datum in a core language that includes no
76;;; syntactic abstractions. The core language includes begin,
77;;; define, if, lambda, letrec, quote, and set!.
78;;; (eval-when situations expr ...)
79;;; conditionally evaluates expr ... at compile-time or run-time
80;;; depending upon situations (see the Chez Scheme System Manual,
81;;; Revision 3, for a complete description)
e4721dde 82;;; (syntax-violation who message form [subform])
a63812a2 83;;; used to report errors found during expansion
5f1a2fb1 84;;; ($sc-dispatch e p)
a63812a2
JB
85;;; used by expanded code to handle syntax-case matching
86
565c8e30
AW
87;;; This file is shipped along with an expanded version of itself,
88;;; psyntax-pp.scm, which is loaded when psyntax.scm has not yet been
89;;; compiled. In this way, psyntax bootstraps off of an expanded
90;;; version of itself.
91
92;;; This implementation of the expander sometimes uses syntactic
93;;; abstractions when procedural abstractions would suffice. For
94;;; example, we define top-wrap and top-marked? as
a63812a2 95;;;
a63812a2
JB
96;;; (define-syntax top-wrap (identifier-syntax '((top))))
97;;; (define-syntax top-marked?
98;;; (syntax-rules ()
99;;; ((_ w) (memq 'top (wrap-marks w)))))
565c8e30 100;;;
a63812a2 101;;; rather than
565c8e30 102;;;
a63812a2
JB
103;;; (define top-wrap '((top)))
104;;; (define top-marked?
105;;; (lambda (w) (memq 'top (wrap-marks w))))
565c8e30
AW
106;;;
107;;; On the other hand, we don't do this consistently; we define
108;;; make-wrap, wrap-marks, and wrap-subst simply as
109;;;
a63812a2
JB
110;;; (define make-wrap cons)
111;;; (define wrap-marks car)
112;;; (define wrap-subst cdr)
565c8e30 113;;;
a63812a2
JB
114;;; In Chez Scheme, the syntactic and procedural forms of these
115;;; abstractions are equivalent, since the optimizer consistently
565c8e30
AW
116;;; integrates constants and small procedures. This will be true of
117;;; Guile as well, once we implement a proper inliner.
a63812a2 118
a63812a2 119
565c8e30 120;;; Implementation notes:
a63812a2
JB
121
122;;; Objects with no standard print syntax, including objects containing
123;;; cycles and syntax object, are allowed in quoted data as long as they
22225fc1 124;;; are contained within a syntax form or produced by datum->syntax.
a63812a2
JB
125;;; Such objects are never copied.
126
127;;; All identifiers that don't have macro definitions and are not bound
565c8e30 128;;; lexically are assumed to be global variables.
a63812a2
JB
129
130;;; Top-level definitions of macro-introduced identifiers are allowed.
131;;; This may not be appropriate for implementations in which the
132;;; model is that bindings are created by definitions, as opposed to
133;;; one in which initial values are assigned by definitions.
134
a63812a2 135;;; Identifiers and syntax objects are implemented as vectors for
565c8e30 136;;; portability. As a result, it is possible to "forge" syntax objects.
a63812a2 137
565c8e30
AW
138;;; The implementation of generate-temporaries assumes that it is
139;;; possible to generate globally unique symbols (gensyms).
a63812a2 140
565c8e30
AW
141;;; The source location associated with incoming expressions is tracked
142;;; via the source-properties mechanism, a weak map from expression to
143;;; source information. At times the source is separated from the
144;;; expression; see the note below about "efficiency and confusion".
a96434cc 145
a63812a2
JB
146
147;;; Bootstrapping:
148
149;;; When changing syntax-object representations, it is necessary to support
150;;; both old and new syntax-object representations in id-var-name. It
151;;; should be sufficient to recognize old representations and treat
152;;; them as not lexically bound.
153
154
155
9c35c579
AW
156(eval-when (compile)
157 (set-current-module (resolve-module '(guile))))
158
a63812a2 159(let ()
8fad25c2
AW
160 (define-syntax define-expansion-constructors
161 (lambda (x)
162 (syntax-case x ()
163 ((_)
164 (let lp ((n 0) (out '()))
165 (if (< n (vector-length %expanded-vtables))
166 (lp (1+ n)
167 (let* ((vtable (vector-ref %expanded-vtables n))
168 (stem (struct-ref vtable (+ vtable-offset-user 0)))
169 (fields (struct-ref vtable (+ vtable-offset-user 2)))
170 (sfields (map (lambda (f) (datum->syntax x f)) fields))
171 (ctor (datum->syntax x (symbol-append 'make- stem))))
172 (cons #`(define (#,ctor #,@sfields)
173 (make-struct (vector-ref %expanded-vtables #,n) 0
174 #,@sfields))
175 out)))
176 #`(begin #,@(reverse out))))))))
177
178 (define-syntax define-expansion-accessors
179 (lambda (x)
180 (syntax-case x ()
181 ((_ stem field ...)
182 (let lp ((n 0))
183 (let ((vtable (vector-ref %expanded-vtables n))
184 (stem (syntax->datum #'stem)))
185 (if (eq? (struct-ref vtable (+ vtable-offset-user 0)) stem)
186 #`(begin
187 (define (#,(datum->syntax x (symbol-append stem '?)) x)
188 (and (struct? x)
189 (eq? (struct-vtable x)
190 (vector-ref %expanded-vtables #,n))))
191 #,@(map
192 (lambda (f)
193 (let ((get (datum->syntax x (symbol-append stem '- f)))
194 (set (datum->syntax x (symbol-append 'set- stem '- f '!)))
195 (idx (list-index (struct-ref vtable
196 (+ vtable-offset-user 2))
197 f)))
198 #`(begin
199 (define (#,get x)
200 (struct-ref x #,idx))
201 (define (#,set x v)
202 (struct-set! x #,idx v)))))
203 (syntax->datum #'(field ...))))
204 (lp (1+ n)))))))))
205
206 (define-syntax define-structure
207 (lambda (x)
208 (define construct-name
209 (lambda (template-identifier . args)
210 (datum->syntax
211 template-identifier
212 (string->symbol
a63812a2
JB
213 (apply string-append
214 (map (lambda (x)
215 (if (string? x)
216 x
22225fc1 217 (symbol->string (syntax->datum x))))
a63812a2 218 args))))))
8fad25c2
AW
219 (syntax-case x ()
220 ((_ (name id1 ...))
221 (and-map identifier? #'(name id1 ...))
222 (with-syntax
223 ((constructor (construct-name #'name "make-" #'name))
224 (predicate (construct-name #'name #'name "?"))
225 ((access ...)
226 (map (lambda (x) (construct-name x #'name "-" x))
227 #'(id1 ...)))
228 ((assign ...)
229 (map (lambda (x)
230 (construct-name x "set-" #'name "-" x "!"))
231 #'(id1 ...)))
232 (structure-length
233 (+ (length #'(id1 ...)) 1))
234 ((index ...)
235 (let f ((i 1) (ids #'(id1 ...)))
236 (if (null? ids)
237 '()
238 (cons i (f (+ i 1) (cdr ids)))))))
239 #'(begin
240 (define constructor
241 (lambda (id1 ...)
242 (vector 'name id1 ... )))
243 (define predicate
244 (lambda (x)
245 (and (vector? x)
246 (= (vector-length x) structure-length)
247 (eq? (vector-ref x 0) 'name))))
248 (define access
249 (lambda (x)
250 (vector-ref x index)))
251 ...
252 (define assign
253 (lambda (x update)
254 (vector-set! x index update)))
255 ...))))))
a63812a2 256
8fad25c2
AW
257 (let ()
258 (define-expansion-constructors)
259 (define-expansion-accessors lambda meta)
260
261 ;; hooks to nonportable run-time helpers
262 (begin
d8f699e8
AW
263 (define-syntax fx+ (identifier-syntax +))
264 (define-syntax fx- (identifier-syntax -))
265 (define-syntax fx= (identifier-syntax =))
266 (define-syntax fx< (identifier-syntax <))
8fad25c2
AW
267
268 (define top-level-eval-hook
269 (lambda (x mod)
270 (primitive-eval x)))
271
272 (define local-eval-hook
273 (lambda (x mod)
274 (primitive-eval x)))
bdf7759c 275
f9685f43
AW
276 ;; Capture syntax-session-id before we shove it off into a module.
277 (define session-id
278 (let ((v (module-variable (current-module) 'syntax-session-id)))
279 (lambda ()
280 ((variable-ref v)))))
c3ae0ed4 281
8fad25c2
AW
282 (define put-global-definition-hook
283 (lambda (symbol type val)
284 (module-define! (current-module)
285 symbol
286 (make-syntax-transformer symbol type val))))
e809758a 287
8fad25c2
AW
288 (define get-global-definition-hook
289 (lambda (symbol module)
290 (if (and (not module) (current-module))
291 (warn "module system is booted, we should have a module" symbol))
292 (let ((v (module-variable (if module
293 (resolve-module (cdr module))
294 (current-module))
295 symbol)))
296 (and v (variable-bound? v)
297 (let ((val (variable-ref v)))
298 (and (macro? val) (macro-type val)
299 (cons (macro-type val)
300 (macro-binding val)))))))))
301
302
303 (define (decorate-source e s)
32fbc38f 304 (if (and s (supports-source-properties? e))
8fad25c2
AW
305 (set-source-properties! e s))
306 e)
307
308 (define (maybe-name-value! name val)
309 (if (lambda? val)
310 (let ((meta (lambda-meta val)))
311 (if (not (assq 'name meta))
312 (set-lambda-meta! val (acons 'name name meta))))))
313
314 ;; output constructors
315 (define build-void
316 (lambda (source)
317 (make-void source)))
318
7081d4f9 319 (define build-call
8fad25c2 320 (lambda (source fun-exp arg-exps)
7081d4f9 321 (make-call source fun-exp arg-exps)))
bdf7759c 322
8fad25c2
AW
323 (define build-conditional
324 (lambda (source test-exp then-exp else-exp)
325 (make-conditional source test-exp then-exp else-exp)))
bdf7759c 326
8fad25c2
AW
327 (define build-dynlet
328 (lambda (source fluids vals body)
329 (make-dynlet source fluids vals body)))
bdf7759c 330
8fad25c2
AW
331 (define build-lexical-reference
332 (lambda (type source name var)
333 (make-lexical-ref source name var)))
bdf7759c 334
8fad25c2
AW
335 (define build-lexical-assignment
336 (lambda (source name var exp)
337 (maybe-name-value! name exp)
338 (make-lexical-set source name var exp)))
bdf7759c 339
8fad25c2
AW
340 (define (analyze-variable mod var modref-cont bare-cont)
341 (if (not mod)
342 (bare-cont var)
343 (let ((kind (car mod))
344 (mod (cdr mod)))
345 (case kind
346 ((public) (modref-cont mod var #t))
347 ((private) (if (not (equal? mod (module-name (current-module))))
348 (modref-cont mod var #f)
349 (bare-cont var)))
350 ((bare) (bare-cont var))
351 ((hygiene) (if (and (not (equal? mod (module-name (current-module))))
352 (module-variable (resolve-module mod) var))
353 (modref-cont mod var #f)
354 (bare-cont var)))
355 (else (syntax-violation #f "bad module kind" var mod))))))
356
357 (define build-global-reference
358 (lambda (source var mod)
359 (analyze-variable
360 mod var
361 (lambda (mod var public?)
362 (make-module-ref source mod var public?))
363 (lambda (var)
364 (make-toplevel-ref source var)))))
365
366 (define build-global-assignment
367 (lambda (source var exp mod)
368 (maybe-name-value! var exp)
369 (analyze-variable
370 mod var
371 (lambda (mod var public?)
372 (make-module-set source mod var public? exp))
373 (lambda (var)
374 (make-toplevel-set source var exp)))))
375
376 (define build-global-definition
377 (lambda (source var exp)
378 (maybe-name-value! var exp)
379 (make-toplevel-define source var exp)))
380
381 (define build-simple-lambda
382 (lambda (src req rest vars meta exp)
383 (make-lambda src
384 meta
385 ;; hah, a case in which kwargs would be nice.
386 (make-lambda-case
387 ;; src req opt rest kw inits vars body else
388 src req #f rest #f '() vars exp #f))))
389
390 (define build-case-lambda
391 (lambda (src meta body)
392 (make-lambda src meta body)))
393
394 (define build-lambda-case
395 ;; req := (name ...)
396 ;; opt := (name ...) | #f
397 ;; rest := name | #f
398 ;; kw := (allow-other-keys? (keyword name var) ...) | #f
399 ;; inits: (init ...)
400 ;; vars: (sym ...)
401 ;; vars map to named arguments in the following order:
402 ;; required, optional (positional), rest, keyword.
403 ;; the body of a lambda: anything, already expanded
404 ;; else: lambda-case | #f
405 (lambda (src req opt rest kw inits vars body else-case)
406 (make-lambda-case src req opt rest kw inits vars body else-case)))
407
a881a4ae
AW
408 (define build-primcall
409 (lambda (src name args)
410 (make-primcall src name args)))
411
8fad25c2
AW
412 (define build-primref
413 (lambda (src name)
a881a4ae
AW
414 (make-primitive-ref src name)))
415
8fad25c2
AW
416 (define (build-data src exp)
417 (make-const src exp))
418
419 (define build-sequence
420 (lambda (src exps)
421 (if (null? (cdr exps))
422 (car exps)
6fc3eae4 423 (make-seq src (car exps) (build-sequence #f (cdr exps))))))
8fad25c2
AW
424
425 (define build-let
426 (lambda (src ids vars val-exps body-exp)
427 (for-each maybe-name-value! ids val-exps)
428 (if (null? vars)
429 body-exp
430 (make-let src ids vars val-exps body-exp))))
431
432 (define build-named-let
433 (lambda (src ids vars val-exps body-exp)
434 (let ((f (car vars))
435 (f-name (car ids))
436 (vars (cdr vars))
437 (ids (cdr ids)))
438 (let ((proc (build-simple-lambda src ids #f vars '() body-exp)))
439 (maybe-name-value! f-name proc)
22cf27c8 440 (for-each maybe-name-value! ids val-exps)
8fad25c2
AW
441 (make-letrec
442 src #f
443 (list f-name) (list f) (list proc)
7081d4f9
AW
444 (build-call src (build-lexical-reference 'fun src f-name f)
445 val-exps))))))
8fad25c2
AW
446
447 (define build-letrec
448 (lambda (src in-order? ids vars val-exps body-exp)
449 (if (null? vars)
450 body-exp
451 (begin
452 (for-each maybe-name-value! ids val-exps)
453 (make-letrec src in-order? ids vars val-exps body-exp)))))
454
455
456 ;; FIXME: use a faster gensym
1bbe0a63 457 (define-syntax-rule (build-lexical-var src id)
f9685f43 458 (gensym (string-append (symbol->string id) "-")))
bdf7759c 459
8fad25c2 460 (define-structure (syntax-object expression wrap module))
a63812a2 461
8fad25c2 462 (define-syntax no-source (identifier-syntax #f))
a63812a2 463
8fad25c2
AW
464 (define source-annotation
465 (lambda (x)
32fbc38f
MW
466 (let ((props (source-properties
467 (if (syntax-object? x)
468 (syntax-object-expression x)
469 x))))
470 (and (pair? props) props))))
a63812a2 471
1bbe0a63
AW
472 (define-syntax-rule (arg-check pred? e who)
473 (let ((x e))
474 (if (not (pred? x)) (syntax-violation who "invalid argument" x))))
8fad25c2
AW
475
476 ;; compile-time environments
477
478 ;; wrap and environment comprise two level mapping.
479 ;; wrap : id --> label
480 ;; env : label --> <element>
481
482 ;; environments are represented in two parts: a lexical part and a global
483 ;; part. The lexical part is a simple list of associations from labels
484 ;; to bindings. The global part is implemented by
485 ;; {put,get}-global-definition-hook and associates symbols with
486 ;; bindings.
487
488 ;; global (assumed global variable) and displaced-lexical (see below)
489 ;; do not show up in any environment; instead, they are fabricated by
c070de63 490 ;; resolve-identifier when it finds no other bindings.
8fad25c2
AW
491
492 ;; <environment> ::= ((<label> . <binding>)*)
493
494 ;; identifier bindings include a type and a value
495
496 ;; <binding> ::= (macro . <procedure>) macros
5b36d603 497 ;; (syntax-parameter . (<procedure>)) syntax parameters
8fad25c2
AW
498 ;; (core . <procedure>) core forms
499 ;; (module-ref . <procedure>) @ or @@
500 ;; (begin) begin
501 ;; (define) define
502 ;; (define-syntax) define-syntax
c2d822de 503 ;; (define-syntax-parameter) define-syntax-parameter
8fad25c2
AW
504 ;; (local-syntax . rec?) let-syntax/letrec-syntax
505 ;; (eval-when) eval-when
4c2e13e5 506 ;; (syntax . (<var> . <level>)) pattern variables
8fad25c2
AW
507 ;; (global) assumed global variable
508 ;; (lexical . <var>) lexical variables
509 ;; (displaced-lexical) displaced lexicals
510 ;; <level> ::= <nonnegative integer>
511 ;; <var> ::= variable returned by build-lexical-var
512
c2d822de
AW
513 ;; a macro is a user-defined syntactic-form. a core is a
514 ;; system-defined syntactic form. begin, define, define-syntax,
515 ;; define-syntax-parameter, and eval-when are treated specially
516 ;; since they are sensitive to whether the form is at top-level and
517 ;; (except for eval-when) can denote valid internal definitions.
8fad25c2
AW
518
519 ;; a pattern variable is a variable introduced by syntax-case and can
520 ;; be referenced only within a syntax form.
521
522 ;; any identifier for which no top-level syntax definition or local
523 ;; binding of any kind has been seen is assumed to be a global
524 ;; variable.
525
526 ;; a lexical variable is a lambda- or letrec-bound variable.
527
528 ;; a displaced-lexical identifier is a lexical identifier removed from
529 ;; it's scope by the return of a syntax object containing the identifier.
530 ;; a displaced lexical can also appear when a letrec-syntax-bound
531 ;; keyword is referenced on the rhs of one of the letrec-syntax clauses.
532 ;; a displaced lexical should never occur with properly written macros.
533
534 (define-syntax make-binding
535 (syntax-rules (quote)
536 ((_ type value) (cons type value))
537 ((_ 'type) '(type))
538 ((_ type) (cons type '()))))
1bbe0a63
AW
539 (define-syntax-rule (binding-type x)
540 (car x))
541 (define-syntax-rule (binding-value x)
542 (cdr x))
8fad25c2
AW
543
544 (define-syntax null-env (identifier-syntax '()))
545
546 (define extend-env
547 (lambda (labels bindings r)
548 (if (null? labels)
549 r
550 (extend-env (cdr labels) (cdr bindings)
551 (cons (cons (car labels) (car bindings)) r)))))
c3ae0ed4 552
8fad25c2
AW
553 (define extend-var-env
554 ;; variant of extend-env that forms "lexical" binding
555 (lambda (labels vars r)
556 (if (null? labels)
557 r
558 (extend-var-env (cdr labels) (cdr vars)
559 (cons (cons (car labels) (make-binding 'lexical (car vars))) r)))))
560
561 ;; we use a "macros only" environment in expansion of local macro
562 ;; definitions so that their definitions can use local macros without
563 ;; attempting to use other lexical identifiers.
564 (define macros-only-env
565 (lambda (r)
566 (if (null? r)
567 '()
568 (let ((a (car r)))
5b36d603 569 (if (memq (cadr a) '(macro syntax-parameter))
8fad25c2
AW
570 (cons a (macros-only-env (cdr r)))
571 (macros-only-env (cdr r)))))))
572
8fad25c2
AW
573 (define global-extend
574 (lambda (type sym val)
575 (put-global-definition-hook sym type val)))
576
577
578 ;; Conceptually, identifiers are always syntax objects. Internally,
579 ;; however, the wrap is sometimes maintained separately (a source of
580 ;; efficiency and confusion), so that symbols are also considered
581 ;; identifiers by id?. Externally, they are always wrapped.
582
583 (define nonsymbol-id?
584 (lambda (x)
585 (and (syntax-object? x)
586 (symbol? (syntax-object-expression x)))))
587
588 (define id?
589 (lambda (x)
590 (cond
591 ((symbol? x) #t)
592 ((syntax-object? x) (symbol? (syntax-object-expression x)))
593 (else #f))))
594
1bbe0a63
AW
595 (define-syntax-rule (id-sym-name e)
596 (let ((x e))
597 (if (syntax-object? x)
598 (syntax-object-expression x)
599 x)))
8fad25c2
AW
600
601 (define id-sym-name&marks
602 (lambda (x w)
603 (if (syntax-object? x)
604 (values
c3ae0ed4 605 (syntax-object-expression x)
8fad25c2
AW
606 (join-marks (wrap-marks w) (wrap-marks (syntax-object-wrap x))))
607 (values x (wrap-marks w)))))
c3ae0ed4 608
8fad25c2
AW
609 ;; syntax object wraps
610
1948b38d
MW
611 ;; <wrap> ::= ((<mark> ...) . (<subst> ...))
612 ;; <subst> ::= shift | <subs>
613 ;; <subs> ::= #(ribcage #(<sym> ...) #(<mark> ...) #(<label> ...))
614 ;; | #(ribcage (<sym> ...) (<mark> ...) (<label> ...))
8fad25c2 615
381ccb0b
AW
616 (define-syntax make-wrap (identifier-syntax cons))
617 (define-syntax wrap-marks (identifier-syntax car))
618 (define-syntax wrap-subst (identifier-syntax cdr))
8fad25c2 619
8fad25c2
AW
620 ;; labels must be comparable with "eq?", have read-write invariance,
621 ;; and distinct from symbols.
f9685f43
AW
622 (define (gen-label)
623 (string-append "l-" (session-id) (symbol->string (gensym "-"))))
8fad25c2
AW
624
625 (define gen-labels
626 (lambda (ls)
627 (if (null? ls)
628 '()
629 (cons (gen-label) (gen-labels (cdr ls))))))
630
631 (define-structure (ribcage symnames marks labels))
632
633 (define-syntax empty-wrap (identifier-syntax '(())))
634
635 (define-syntax top-wrap (identifier-syntax '((top))))
636
1bbe0a63
AW
637 (define-syntax-rule (top-marked? w)
638 (memq 'top (wrap-marks w)))
8fad25c2
AW
639
640 ;; Marks must be comparable with "eq?" and distinct from pairs and
641 ;; the symbol top. We do not use integers so that marks will remain
642 ;; unique even across file compiles.
643
644 (define-syntax the-anti-mark (identifier-syntax #f))
645
646 (define anti-mark
647 (lambda (w)
648 (make-wrap (cons the-anti-mark (wrap-marks w))
649 (cons 'shift (wrap-subst w)))))
650
1bbe0a63 651 (define-syntax-rule (new-mark)
f9685f43 652 (gensym (string-append "m-" (session-id) "-")))
8fad25c2
AW
653
654 ;; make-empty-ribcage and extend-ribcage maintain list-based ribcages for
655 ;; internal definitions, in which the ribcages are built incrementally
1bbe0a63
AW
656 (define-syntax-rule (make-empty-ribcage)
657 (make-ribcage '() '() '()))
8fad25c2
AW
658
659 (define extend-ribcage!
660 ;; must receive ids with complete wraps
661 (lambda (ribcage id label)
662 (set-ribcage-symnames! ribcage
663 (cons (syntax-object-expression id)
664 (ribcage-symnames ribcage)))
665 (set-ribcage-marks! ribcage
666 (cons (wrap-marks (syntax-object-wrap id))
667 (ribcage-marks ribcage)))
668 (set-ribcage-labels! ribcage
669 (cons label (ribcage-labels ribcage)))))
670
671 ;; make-binding-wrap creates vector-based ribcages
672 (define make-binding-wrap
673 (lambda (ids labels w)
674 (if (null? ids)
675 w
c3ae0ed4 676 (make-wrap
8fad25c2
AW
677 (wrap-marks w)
678 (cons
679 (let ((labelvec (list->vector labels)))
680 (let ((n (vector-length labelvec)))
681 (let ((symnamevec (make-vector n)) (marksvec (make-vector n)))
682 (let f ((ids ids) (i 0))
683 (if (not (null? ids))
684 (call-with-values
685 (lambda () (id-sym-name&marks (car ids) w))
686 (lambda (symname marks)
687 (vector-set! symnamevec i symname)
688 (vector-set! marksvec i marks)
689 (f (cdr ids) (fx+ i 1))))))
690 (make-ribcage symnamevec marksvec labelvec))))
691 (wrap-subst w))))))
692
693 (define smart-append
694 (lambda (m1 m2)
695 (if (null? m2)
696 m1
697 (append m1 m2))))
698
699 (define join-wraps
700 (lambda (w1 w2)
701 (let ((m1 (wrap-marks w1)) (s1 (wrap-subst w1)))
702 (if (null? m1)
703 (if (null? s1)
704 w2
705 (make-wrap
706 (wrap-marks w2)
707 (smart-append s1 (wrap-subst w2))))
708 (make-wrap
709 (smart-append m1 (wrap-marks w2))
710 (smart-append s1 (wrap-subst w2)))))))
711
712 (define join-marks
713 (lambda (m1 m2)
714 (smart-append m1 m2)))
715
716 (define same-marks?
717 (lambda (x y)
718 (or (eq? x y)
719 (and (not (null? x))
720 (not (null? y))
721 (eq? (car x) (car y))
722 (same-marks? (cdr x) (cdr y))))))
723
724 (define id-var-name
c070de63
AW
725 ;; Syntax objects use wraps to associate names with marked
726 ;; identifiers. This function returns the name corresponding to
727 ;; the given identifier and wrap, or the original identifier if no
728 ;; corresponding name was found.
729 ;;
730 ;; The name may be a string created by gen-label, indicating a
731 ;; lexical binding, or another syntax object, indicating a
732 ;; reference to a top-level definition created during a previous
733 ;; macroexpansion.
734 ;;
47e9919d
AW
735 ;; For lexical variables, finding a label simply amounts to
736 ;; looking for an entry with the same symbolic name and the same
737 ;; marks. Finding a toplevel definition is the same, except we
738 ;; also have to compare modules, hence the `mod' parameter.
739 ;; Instead of adding a separate entry in the ribcage for modules,
740 ;; which wouldn't be used for lexicals, we arrange for the entry
741 ;; for the name entry to be a pair with the module in its car, and
742 ;; the name itself in the cdr. So if the name that we find is a
743 ;; pair, we have to check modules.
744 ;;
c070de63
AW
745 ;; The identifer may be passed in wrapped or unwrapped. In any
746 ;; case, this routine returns either a symbol, a syntax object, or
747 ;; a string label.
748 ;;
47e9919d 749 (lambda (id w mod)
1bbe0a63
AW
750 (define-syntax-rule (first e)
751 ;; Rely on Guile's multiple-values truncation.
752 e)
8fad25c2 753 (define search
47e9919d 754 (lambda (sym subst marks mod)
8fad25c2
AW
755 (if (null? subst)
756 (values #f marks)
757 (let ((fst (car subst)))
758 (if (eq? fst 'shift)
47e9919d 759 (search sym (cdr subst) (cdr marks) mod)
8fad25c2
AW
760 (let ((symnames (ribcage-symnames fst)))
761 (if (vector? symnames)
47e9919d
AW
762 (search-vector-rib sym subst marks symnames fst mod)
763 (search-list-rib sym subst marks symnames fst mod))))))))
8fad25c2 764 (define search-list-rib
47e9919d 765 (lambda (sym subst marks symnames ribcage mod)
8fad25c2 766 (let f ((symnames symnames) (i 0))
c3ae0ed4 767 (cond
47e9919d 768 ((null? symnames) (search sym (cdr subst) marks mod))
8fad25c2
AW
769 ((and (eq? (car symnames) sym)
770 (same-marks? marks (list-ref (ribcage-marks ribcage) i)))
47e9919d
AW
771 (let ((n (list-ref (ribcage-labels ribcage) i)))
772 (if (pair? n)
773 (if (equal? mod (car n))
774 (values (cdr n) marks)
775 (f (cdr symnames) (fx+ i 1)))
776 (values n marks))))
8fad25c2
AW
777 (else (f (cdr symnames) (fx+ i 1)))))))
778 (define search-vector-rib
47e9919d 779 (lambda (sym subst marks symnames ribcage mod)
8fad25c2
AW
780 (let ((n (vector-length symnames)))
781 (let f ((i 0))
782 (cond
47e9919d 783 ((fx= i n) (search sym (cdr subst) marks mod))
8fad25c2
AW
784 ((and (eq? (vector-ref symnames i) sym)
785 (same-marks? marks (vector-ref (ribcage-marks ribcage) i)))
47e9919d
AW
786 (let ((n (vector-ref (ribcage-labels ribcage) i)))
787 (if (pair? n)
788 (if (equal? mod (car n))
789 (values (cdr n) marks)
790 (f (fx+ i 1)))
791 (values n marks))))
8fad25c2
AW
792 (else (f (fx+ i 1))))))))
793 (cond
794 ((symbol? id)
47e9919d 795 (or (first (search id (wrap-subst w) (wrap-marks w) mod)) id))
8fad25c2
AW
796 ((syntax-object? id)
797 (let ((id (syntax-object-expression id))
47e9919d
AW
798 (w1 (syntax-object-wrap id))
799 (mod (syntax-object-module id)))
8fad25c2 800 (let ((marks (join-marks (wrap-marks w) (wrap-marks w1))))
47e9919d 801 (call-with-values (lambda () (search id (wrap-subst w) marks mod))
8fad25c2
AW
802 (lambda (new-id marks)
803 (or new-id
47e9919d 804 (first (search id (wrap-subst w1) marks mod))
8fad25c2
AW
805 id))))))
806 (else (syntax-violation 'id-var-name "invalid id" id)))))
807
3d51e57c
AW
808 ;; A helper procedure for syntax-locally-bound-identifiers, which
809 ;; itself is a helper for transformer procedures.
810 ;; `locally-bound-identifiers' returns a list of all bindings
811 ;; visible to a syntax object with the given wrap. They are in
812 ;; order from outer to inner.
813 ;;
814 ;; The purpose of this procedure is to give a transformer procedure
815 ;; references on bound identifiers, that the transformer can then
816 ;; introduce some of them in its output. As such, the identifiers
817 ;; are anti-marked, so that rebuild-macro-output doesn't apply new
818 ;; marks to them.
819 ;;
820 (define locally-bound-identifiers
821 (lambda (w mod)
822 (define scan
823 (lambda (subst results)
824 (if (null? subst)
825 results
826 (let ((fst (car subst)))
827 (if (eq? fst 'shift)
828 (scan (cdr subst) results)
829 (let ((symnames (ribcage-symnames fst))
830 (marks (ribcage-marks fst)))
831 (if (vector? symnames)
832 (scan-vector-rib subst symnames marks results)
833 (scan-list-rib subst symnames marks results))))))))
834 (define scan-list-rib
835 (lambda (subst symnames marks results)
836 (let f ((symnames symnames) (marks marks) (results results))
837 (if (null? symnames)
838 (scan (cdr subst) results)
839 (f (cdr symnames) (cdr marks)
840 (cons (wrap (car symnames)
841 (anti-mark (make-wrap (car marks) subst))
842 mod)
843 results))))))
844 (define scan-vector-rib
845 (lambda (subst symnames marks results)
846 (let ((n (vector-length symnames)))
847 (let f ((i 0) (results results))
848 (if (fx= i n)
849 (scan (cdr subst) results)
850 (f (fx+ i 1)
851 (cons (wrap (vector-ref symnames i)
852 (anti-mark (make-wrap (vector-ref marks i) subst))
853 mod)
854 results)))))))
855 (scan (wrap-subst w) '())))
856
5b36d603
AW
857 ;; Returns three values: binding type, binding value, the module (for
858 ;; resolving toplevel vars).
859 (define (resolve-identifier id w r mod resolve-syntax-parameters?)
860 (define (resolve-syntax-parameters b)
861 (if (and resolve-syntax-parameters?
862 (eq? (binding-type b) 'syntax-parameter))
863 (or (assq-ref r (binding-value b))
864 (make-binding 'macro (car (binding-value b))))
865 b))
c070de63 866 (define (resolve-global var mod)
5b36d603
AW
867 (let ((b (resolve-syntax-parameters
868 (or (get-global-definition-hook var mod)
869 (make-binding 'global)))))
870 (if (eq? (binding-type b) 'global)
871 (values 'global var mod)
872 (values (binding-type b) (binding-value b) mod))))
c070de63 873 (define (resolve-lexical label mod)
5b36d603
AW
874 (let ((b (resolve-syntax-parameters
875 (or (assq-ref r label)
876 (make-binding 'displaced-lexical)))))
877 (values (binding-type b) (binding-value b) mod)))
47e9919d 878 (let ((n (id-var-name id w mod)))
c070de63
AW
879 (cond
880 ((syntax-object? n)
ea3ca4e4 881 ;; Recursing allows syntax-parameterize to override
5b36d603
AW
882 ;; macro-introduced syntax parameters.
883 (resolve-identifier n w r mod resolve-syntax-parameters?))
c070de63
AW
884 ((symbol? n)
885 (resolve-global n (if (syntax-object? id)
886 (syntax-object-module id)
887 mod)))
888 ((string? n)
889 (resolve-lexical n (if (syntax-object? id)
890 (syntax-object-module id)
891 mod)))
892 (else
893 (error "unexpected id-var-name" id w n)))))
894
9b0975f1
AW
895 (define transformer-environment
896 (make-fluid
897 (lambda (k)
898 (error "called outside the dynamic extent of a syntax transformer"))))
899
900 (define (with-transformer-environment k)
901 ((fluid-ref transformer-environment) k))
902
8fad25c2
AW
903 ;; free-id=? must be passed fully wrapped ids since (free-id=? x y)
904 ;; may be true even if (free-id=? (wrap x w) (wrap y w)) is not.
905
906 (define free-id=?
907 (lambda (i j)
47e9919d
AW
908 (let* ((mi (and (syntax-object? i) (syntax-object-module i)))
909 (mj (and (syntax-object? j) (syntax-object-module j)))
910 (ni (id-var-name i empty-wrap mi))
911 (nj (id-var-name j empty-wrap mj)))
912 (define (id-module-binding id mod)
913 (module-variable
914 (if mod
915 ;; The normal case.
916 (resolve-module (cdr mod))
917 ;; Either modules have not been booted, or we have a
918 ;; raw symbol coming in, which is possible.
919 (current-module))
920 (id-sym-name id)))
c070de63
AW
921 (cond
922 ((syntax-object? ni) (free-id=? ni j))
923 ((syntax-object? nj) (free-id=? i nj))
924 ((symbol? ni)
925 ;; `i' is not lexically bound. Assert that `j' is free,
926 ;; and if so, compare their bindings, that they are either
927 ;; bound to the same variable, or both unbound and have
928 ;; the same name.
929 (and (eq? nj (id-sym-name j))
47e9919d 930 (let ((bi (id-module-binding i mi)))
c070de63 931 (if bi
47e9919d
AW
932 (eq? bi (id-module-binding j mj))
933 (and (not (id-module-binding j mj))
c070de63 934 (eq? ni nj))))
47e9919d 935 (eq? (id-module-binding i mi) (id-module-binding j mj))))
c070de63
AW
936 (else
937 ;; Otherwise `i' is bound, so check that `j' is bound, and
938 ;; bound to the same thing.
939 (equal? ni nj))))))
04b04af8 940
8fad25c2
AW
941 ;; bound-id=? may be passed unwrapped (or partially wrapped) ids as
942 ;; long as the missing portion of the wrap is common to both of the ids
943 ;; since (bound-id=? x y) iff (bound-id=? (wrap x w) (wrap y w))
944
945 (define bound-id=?
946 (lambda (i j)
947 (if (and (syntax-object? i) (syntax-object? j))
948 (and (eq? (syntax-object-expression i)
949 (syntax-object-expression j))
950 (same-marks? (wrap-marks (syntax-object-wrap i))
951 (wrap-marks (syntax-object-wrap j))))
952 (eq? i j))))
953
954 ;; "valid-bound-ids?" returns #t if it receives a list of distinct ids.
955 ;; valid-bound-ids? may be passed unwrapped (or partially wrapped) ids
956 ;; as long as the missing portion of the wrap is common to all of the
957 ;; ids.
958
959 (define valid-bound-ids?
960 (lambda (ids)
961 (and (let all-ids? ((ids ids))
962 (or (null? ids)
963 (and (id? (car ids))
964 (all-ids? (cdr ids)))))
965 (distinct-bound-ids? ids))))
966
967 ;; distinct-bound-ids? expects a list of ids and returns #t if there are
968 ;; no duplicates. It is quadratic on the length of the id list; long
969 ;; lists could be sorted to make it more efficient. distinct-bound-ids?
970 ;; may be passed unwrapped (or partially wrapped) ids as long as the
971 ;; missing portion of the wrap is common to all of the ids.
972
973 (define distinct-bound-ids?
974 (lambda (ids)
975 (let distinct? ((ids ids))
976 (or (null? ids)
977 (and (not (bound-id-member? (car ids) (cdr ids)))
978 (distinct? (cdr ids)))))))
979
980 (define bound-id-member?
981 (lambda (x list)
982 (and (not (null? list))
983 (or (bound-id=? x (car list))
984 (bound-id-member? x (cdr list))))))
985
986 ;; wrapping expressions and identifiers
987
988 (define wrap
989 (lambda (x w defmod)
990 (cond
991 ((and (null? (wrap-marks w)) (null? (wrap-subst w))) x)
992 ((syntax-object? x)
993 (make-syntax-object
994 (syntax-object-expression x)
995 (join-wraps w (syntax-object-wrap x))
996 (syntax-object-module x)))
997 ((null? x) x)
998 (else (make-syntax-object x w defmod)))))
999
1000 (define source-wrap
1001 (lambda (x w s defmod)
1002 (wrap (decorate-source x s) w defmod)))
1003
1004 ;; expanding
1005
78a47455 1006 (define expand-sequence
8fad25c2
AW
1007 (lambda (body r w s mod)
1008 (build-sequence s
1009 (let dobody ((body body) (r r) (w w) (mod mod))
1010 (if (null? body)
1011 '()
78a47455 1012 (let ((first (expand (car body) r w mod)))
8fad25c2
AW
1013 (cons first (dobody (cdr body) r w mod))))))))
1014
4da326f2 1015 ;; At top-level, we allow mixed definitions and expressions. Like
78a47455 1016 ;; expand-body we expand in two passes.
4da326f2
AW
1017 ;;
1018 ;; First, from left to right, we expand just enough to know what
1019 ;; expressions are definitions, syntax definitions, and splicing
1020 ;; statements (`begin'). If we anything needs evaluating at
1021 ;; expansion-time, it is expanded directly.
1022 ;;
1023 ;; Otherwise we collect expressions to expand, in thunks, and then
1024 ;; expand them all at the end. This allows all syntax expanders
1025 ;; visible in a toplevel sequence to be visible during the
1026 ;; expansions of all normal definitions and expressions in the
1027 ;; sequence.
1028 ;;
78a47455 1029 (define expand-top-sequence
8fad25c2 1030 (lambda (body r w s m esew mod)
45f58467
AW
1031 (let* ((r (cons '("placeholder" . (placeholder)) r))
1032 (ribcage (make-empty-ribcage))
1033 (w (make-wrap (wrap-marks w) (cons ribcage (wrap-subst w)))))
19ef14f9
AW
1034 (define (record-definition! id var)
1035 (let ((mod (cons 'hygiene (module-name (current-module)))))
47e9919d
AW
1036 ;; Ribcages map symbol+marks to names, mostly for
1037 ;; resolving lexicals. Here to add a mapping for toplevel
1038 ;; definitions we also need to match the module. So, we
1039 ;; put it in the name instead, and make id-var-name handle
1040 ;; the special case of names that are pairs. See the
1041 ;; comments in id-var-name for more.
1042 (extend-ribcage! ribcage id
1043 (cons (syntax-object-module id)
1044 (wrap var top-wrap mod)))))
de41e564
AW
1045 (define (macro-introduced-identifier? id)
1046 (not (equal? (wrap-marks (syntax-object-wrap id)) '(top))))
1047 (define (fresh-derived-name id orig-form)
1048 (symbol-append
1049 (syntax-object-expression id)
1050 '-
1051 (string->symbol
1052 ;; FIXME: `hash' currently stops descending into nested
1053 ;; data at some point, so it's less unique than we would
1054 ;; like. Also this encodes hash values into the ABI of
1055 ;; compiled modules; a problem?
1056 (number->string
1057 (hash (syntax->datum orig-form) most-positive-fixnum)
1058 16))))
45f58467
AW
1059 (define (parse body r w s m esew mod)
1060 (let lp ((body body) (exps '()))
1061 (if (null? body)
1062 exps
1063 (lp (cdr body)
1064 (append (parse1 (car body) r w s m esew mod)
1065 exps)))))
1066 (define (parse1 x r w s m esew mod)
1067 (call-with-values
1068 (lambda ()
1069 (syntax-type x r w (source-annotation x) ribcage mod #f))
dfadcf85 1070 (lambda (type value form e w s mod)
45f58467
AW
1071 (case type
1072 ((define-form)
1073 (let* ((id (wrap value w mod))
1074 (label (gen-label))
de41e564
AW
1075 (var (if (macro-introduced-identifier? id)
1076 (fresh-derived-name id x)
1077 (syntax-object-expression id))))
45f58467
AW
1078 (record-definition! id var)
1079 (list
1080 (if (eq? m 'c&e)
b2208d2e 1081 (let ((x (build-global-definition s var (expand e r w mod))))
45f58467
AW
1082 (top-level-eval-hook x mod)
1083 (lambda () x))
1084 (lambda ()
b2208d2e 1085 (build-global-definition s var (expand e r w mod)))))))
c2d822de 1086 ((define-syntax-form define-syntax-parameter-form)
45f58467
AW
1087 (let* ((id (wrap value w mod))
1088 (label (gen-label))
de41e564
AW
1089 (var (if (macro-introduced-identifier? id)
1090 (fresh-derived-name id x)
1091 (syntax-object-expression id))))
45f58467
AW
1092 (record-definition! id var)
1093 (case m
1094 ((c)
1095 (cond
1096 ((memq 'compile esew)
b2208d2e 1097 (let ((e (expand-install-global var type (expand e r w mod))))
45f58467
AW
1098 (top-level-eval-hook e mod)
1099 (if (memq 'load esew)
1100 (list (lambda () e))
1101 '())))
1102 ((memq 'load esew)
1103 (list (lambda ()
b2208d2e 1104 (expand-install-global var type (expand e r w mod)))))
45f58467
AW
1105 (else '())))
1106 ((c&e)
b2208d2e 1107 (let ((e (expand-install-global var type (expand e r w mod))))
45f58467
AW
1108 (top-level-eval-hook e mod)
1109 (list (lambda () e))))
1110 (else
1111 (if (memq 'eval esew)
1112 (top-level-eval-hook
b2208d2e 1113 (expand-install-global var type (expand e r w mod))
45f58467
AW
1114 mod))
1115 '()))))
1116 ((begin-form)
1117 (syntax-case e ()
1118 ((_ e1 ...)
1119 (parse #'(e1 ...) r w s m esew mod))))
1120 ((local-syntax-form)
b2208d2e 1121 (expand-local-syntax value e r w s mod
45f58467
AW
1122 (lambda (forms r w s mod)
1123 (parse forms r w s m esew mod))))
1124 ((eval-when-form)
1125 (syntax-case e ()
1126 ((_ (x ...) e1 e2 ...)
b2208d2e 1127 (let ((when-list (parse-when-list e #'(x ...)))
45f58467
AW
1128 (body #'(e1 e2 ...)))
1129 (define (recurse m esew)
1130 (parse body r w s m esew mod))
1131 (cond
1132 ((eq? m 'e)
1133 (if (memq 'eval when-list)
1134 (recurse (if (memq 'expand when-list) 'c&e 'e)
1135 '(eval))
1136 (begin
1137 (if (memq 'expand when-list)
1138 (top-level-eval-hook
b2208d2e 1139 (expand-top-sequence body r w s 'e '(eval) mod)
45f58467
AW
1140 mod))
1141 '())))
1142 ((memq 'load when-list)
1143 (if (or (memq 'compile when-list)
1144 (memq 'expand when-list)
1145 (and (eq? m 'c&e) (memq 'eval when-list)))
1146 (recurse 'c&e '(compile load))
1147 (if (memq m '(c c&e))
1148 (recurse 'c '(load))
1149 '())))
1150 ((or (memq 'compile when-list)
1151 (memq 'expand when-list)
1152 (and (eq? m 'c&e) (memq 'eval when-list)))
1153 (top-level-eval-hook
b2208d2e 1154 (expand-top-sequence body r w s 'e '(eval) mod)
45f58467
AW
1155 mod)
1156 '())
1157 (else
1158 '()))))))
1159 (else
1160 (list
1161 (if (eq? m 'c&e)
dfadcf85 1162 (let ((x (expand-expr type value form e r w s mod)))
45f58467
AW
1163 (top-level-eval-hook x mod)
1164 (lambda () x))
1165 (lambda ()
dfadcf85 1166 (expand-expr type value form e r w s mod)))))))))
45f58467
AW
1167 (let ((exps (map (lambda (x) (x))
1168 (reverse (parse body r w s m esew mod)))))
1169 (if (null? exps)
1170 (build-void s)
1171 (build-sequence s exps))))))
4c2e13e5 1172
78a47455 1173 (define expand-install-global
5b36d603 1174 (lambda (name type e)
8fad25c2
AW
1175 (build-global-definition
1176 no-source
1177 name
a881a4ae 1178 (build-primcall
8fad25c2 1179 no-source
a881a4ae 1180 'make-syntax-transformer
5b36d603
AW
1181 (if (eq? type 'define-syntax-parameter-form)
1182 (list (build-data no-source name)
1183 (build-data no-source 'syntax-parameter)
1184 (build-primcall no-source 'list (list e)))
1185 (list (build-data no-source name)
1186 (build-data no-source 'macro)
1187 e))))))
1188
440ac793
AW
1189 (define parse-when-list
1190 (lambda (e when-list)
aa5698fb
AW
1191 ;; `when-list' is syntax'd version of list of situations. We
1192 ;; could match these keywords lexically, via free-id=?, but then
1193 ;; we twingle the definition of eval-when to the bindings of
1194 ;; eval, load, expand, and compile, which is totally unintended.
1195 ;; So do a symbolic match instead.
440ac793
AW
1196 (let ((result (strip when-list empty-wrap)))
1197 (let lp ((l result))
1198 (if (null? l)
1199 result
1200 (if (memq (car l) '(compile load eval expand))
1201 (lp (cdr l))
1202 (syntax-violation 'eval-when "invalid situation" e
1203 (car l))))))))
8fad25c2 1204
40e92f09
MW
1205 ;; syntax-type returns seven values: type, value, form, e, w, s, and
1206 ;; mod. The first two are described in the table below.
8fad25c2
AW
1207 ;;
1208 ;; type value explanation
1209 ;; -------------------------------------------------------------------
1210 ;; core procedure core singleton
1211 ;; core-form procedure core form
1212 ;; module-ref procedure @ or @@ singleton
1213 ;; lexical name lexical variable reference
1214 ;; global name global variable reference
1215 ;; begin none begin keyword
1216 ;; define none define keyword
1217 ;; define-syntax none define-syntax keyword
c2d822de 1218 ;; define-syntax-parameter none define-syntax-parameter keyword
8fad25c2
AW
1219 ;; local-syntax rec? letrec-syntax/let-syntax keyword
1220 ;; eval-when none eval-when keyword
1221 ;; syntax level pattern variable
1222 ;; displaced-lexical none displaced lexical identifier
1223 ;; lexical-call name call to lexical variable
1224 ;; global-call name call to global variable
1225 ;; call none any other call
1226 ;; begin-form none begin expression
1227 ;; define-form id variable definition
1228 ;; define-syntax-form id syntax definition
c2d822de 1229 ;; define-syntax-parameter-form id syntax parameter definition
8fad25c2
AW
1230 ;; local-syntax-form rec? syntax definition
1231 ;; eval-when-form none eval-when form
1232 ;; constant none self-evaluating datum
1233 ;; other none anything else
1234 ;;
40e92f09
MW
1235 ;; form is the entire form. For definition forms (define-form,
1236 ;; define-syntax-form, and define-syntax-parameter-form), e is the
1237 ;; rhs expression. For all others, e is the entire form. w is the
1238 ;; wrap for both form and e. s is the source for the entire form.
1239 ;; mod is the module for both form and e.
8fad25c2 1240 ;;
c2d822de
AW
1241 ;; syntax-type expands macros and unwraps as necessary to get to one
1242 ;; of the forms above. It also parses definition forms, although
1243 ;; perhaps this should be done by the consumer.
8fad25c2
AW
1244
1245 (define syntax-type
1246 (lambda (e r w s rib mod for-car?)
1247 (cond
1248 ((symbol? e)
5b36d603
AW
1249 (call-with-values (lambda () (resolve-identifier e w r mod #t))
1250 (lambda (type value mod*)
c070de63
AW
1251 (case type
1252 ((macro)
1253 (if for-car?
dfadcf85 1254 (values type value e e w s mod)
b2208d2e 1255 (syntax-type (expand-macro value e r w s rib mod)
c070de63
AW
1256 r empty-wrap s rib mod #f)))
1257 ((global)
1258 ;; Toplevel definitions may resolve to bindings with
1259 ;; different names or in different modules.
dfadcf85
AW
1260 (values type value e value w s mod*))
1261 (else (values type value e e w s mod))))))
8fad25c2
AW
1262 ((pair? e)
1263 (let ((first (car e)))
1264 (call-with-values
1265 (lambda () (syntax-type first r w s rib mod #t))
40e92f09 1266 (lambda (ftype fval fform fe fw fs fmod)
8fad25c2
AW
1267 (case ftype
1268 ((lexical)
40e92f09 1269 (values 'lexical-call fval e e w s mod))
8fad25c2
AW
1270 ((global)
1271 ;; If we got here via an (@@ ...) expansion, we need to
1272 ;; make sure the fmod information is propagated back
1273 ;; correctly -- hence this consing.
1274 (values 'global-call (make-syntax-object fval w fmod)
40e92f09 1275 e e w s mod))
8fad25c2 1276 ((macro)
78a47455 1277 (syntax-type (expand-macro fval e r w s rib mod)
8fad25c2
AW
1278 r empty-wrap s rib mod for-car?))
1279 ((module-ref)
1280 (call-with-values (lambda () (fval e r w))
1281 (lambda (e r w s mod)
1282 (syntax-type e r w s rib mod for-car?))))
1283 ((core)
40e92f09 1284 (values 'core-form fval e e w s mod))
8fad25c2 1285 ((local-syntax)
40e92f09 1286 (values 'local-syntax-form fval e e w s mod))
8fad25c2 1287 ((begin)
40e92f09 1288 (values 'begin-form #f e e w s mod))
8fad25c2 1289 ((eval-when)
40e92f09 1290 (values 'eval-when-form #f e e w s mod))
8fad25c2
AW
1291 ((define)
1292 (syntax-case e ()
1293 ((_ name val)
1294 (id? #'name)
40e92f09 1295 (values 'define-form #'name e #'val w s mod))
8fad25c2
AW
1296 ((_ (name . args) e1 e2 ...)
1297 (and (id? #'name)
1298 (valid-bound-ids? (lambda-var-list #'args)))
1299 ;; need lambda here...
1300 (values 'define-form (wrap #'name w mod)
40e92f09 1301 (wrap e w mod)
8fad25c2
AW
1302 (decorate-source
1303 (cons #'lambda (wrap #'(args e1 e2 ...) w mod))
1304 s)
1305 empty-wrap s mod))
1306 ((_ name)
1307 (id? #'name)
1308 (values 'define-form (wrap #'name w mod)
40e92f09 1309 (wrap e w mod)
8fad25c2
AW
1310 #'(if #f #f)
1311 empty-wrap s mod))))
1312 ((define-syntax)
1313 (syntax-case e ()
1314 ((_ name val)
1315 (id? #'name)
40e92f09 1316 (values 'define-syntax-form #'name e #'val w s mod))))
c2d822de
AW
1317 ((define-syntax-parameter)
1318 (syntax-case e ()
1319 ((_ name val)
1320 (id? #'name)
40e92f09 1321 (values 'define-syntax-parameter-form #'name e #'val w s mod))))
8fad25c2 1322 (else
40e92f09 1323 (values 'call #f e e w s mod)))))))
8fad25c2
AW
1324 ((syntax-object? e)
1325 (syntax-type (syntax-object-expression e)
1326 r
1327 (join-wraps w (syntax-object-wrap e))
1328 (or (source-annotation e) s) rib
1329 (or (syntax-object-module e) mod) for-car?))
40e92f09
MW
1330 ((self-evaluating? e) (values 'constant #f e e w s mod))
1331 (else (values 'other #f e e w s mod)))))
8fad25c2 1332
78a47455 1333 (define expand
8fad25c2
AW
1334 (lambda (e r w mod)
1335 (call-with-values
1336 (lambda () (syntax-type e r w (source-annotation e) #f mod #f))
40e92f09
MW
1337 (lambda (type value form e w s mod)
1338 (expand-expr type value form e r w s mod)))))
8fad25c2 1339
78a47455 1340 (define expand-expr
40e92f09 1341 (lambda (type value form e r w s mod)
8fad25c2
AW
1342 (case type
1343 ((lexical)
1344 (build-lexical-reference 'value s e value))
1345 ((core core-form)
1346 ;; apply transformer
1347 (value e r w s mod))
1348 ((module-ref)
1349 (call-with-values (lambda () (value e r w))
1350 (lambda (e r w s mod)
78a47455 1351 (expand e r w mod))))
8fad25c2 1352 ((lexical-call)
b2208d2e 1353 (expand-call
8fad25c2
AW
1354 (let ((id (car e)))
1355 (build-lexical-reference 'fun (source-annotation id)
1356 (if (syntax-object? id)
1357 (syntax->datum id)
1358 id)
1359 value))
1360 e r w s mod))
1361 ((global-call)
b2208d2e 1362 (expand-call
8fad25c2
AW
1363 (build-global-reference (source-annotation (car e))
1364 (if (syntax-object? value)
1365 (syntax-object-expression value)
1366 value)
1367 (if (syntax-object? value)
1368 (syntax-object-module value)
1369 mod))
1370 e r w s mod))
1371 ((constant) (build-data s (strip (source-wrap e w s mod) empty-wrap)))
1372 ((global) (build-global-reference s value mod))
b2208d2e 1373 ((call) (expand-call (expand (car e) r w mod) e r w s mod))
8fad25c2
AW
1374 ((begin-form)
1375 (syntax-case e ()
dc65d1cf
AW
1376 ((_ e1 e2 ...) (expand-sequence #'(e1 e2 ...) r w s mod))
1377 ((_)
0bdd4351
AW
1378 (syntax-violation #f "sequence of zero expressions"
1379 (source-wrap e w s mod)))))
8fad25c2 1380 ((local-syntax-form)
78a47455 1381 (expand-local-syntax value e r w s mod expand-sequence))
8fad25c2
AW
1382 ((eval-when-form)
1383 (syntax-case e ()
1384 ((_ (x ...) e1 e2 ...)
440ac793 1385 (let ((when-list (parse-when-list e #'(x ...))))
8fad25c2 1386 (if (memq 'eval when-list)
78a47455
AW
1387 (expand-sequence #'(e1 e2 ...) r w s mod)
1388 (expand-void))))))
c2d822de 1389 ((define-form define-syntax-form define-syntax-parameter-form)
40e92f09
MW
1390 (syntax-violation #f "definition in expression context, where definitions are not allowed,"
1391 (source-wrap form w s mod)))
8fad25c2
AW
1392 ((syntax)
1393 (syntax-violation #f "reference to pattern variable outside syntax form"
1394 (source-wrap e w s mod)))
1395 ((displaced-lexical)
1396 (syntax-violation #f "reference to identifier outside its scope"
1397 (source-wrap e w s mod)))
1398 (else (syntax-violation #f "unexpected syntax"
1399 (source-wrap e w s mod))))))
1400
b2208d2e 1401 (define expand-call
8fad25c2
AW
1402 (lambda (x e r w s mod)
1403 (syntax-case e ()
1404 ((e0 e1 ...)
7081d4f9 1405 (build-call s x
b2208d2e 1406 (map (lambda (e) (expand e r w mod)) #'(e1 ...)))))))
8fad25c2
AW
1407
1408 ;; (What follows is my interpretation of what's going on here -- Andy)
1409 ;;
1410 ;; A macro takes an expression, a tree, the leaves of which are identifiers
1411 ;; and datums. Identifiers are symbols along with a wrap and a module. For
1412 ;; efficiency, subtrees that share wraps and modules may be grouped as one
1413 ;; syntax object.
c3ae0ed4 1414 ;;
8fad25c2
AW
1415 ;; Going into the expansion, the expression is given an anti-mark, which
1416 ;; logically propagates to all leaves. Then, in the new expression returned
1417 ;; from the transfomer, if we see an expression with an anti-mark, we know it
1418 ;; pertains to the original expression; conversely, expressions without the
1419 ;; anti-mark are known to be introduced by the transformer.
c3ae0ed4 1420 ;;
8fad25c2
AW
1421 ;; OK, good until now. We know this algorithm does lexical scoping
1422 ;; appropriately because it's widely known in the literature, and psyntax is
1423 ;; widely used. But what about modules? Here we're on our own. What we do is
1424 ;; to mark the module of expressions produced by a macro as pertaining to the
1425 ;; module that was current when the macro was defined -- that is, free
1426 ;; identifiers introduced by a macro are scoped in the macro's module, not in
1427 ;; the expansion's module. Seems to work well.
c3ae0ed4 1428 ;;
8fad25c2
AW
1429 ;; The only wrinkle is when we want a macro to expand to code in another
1430 ;; module, as is the case for the r6rs `library' form -- the body expressions
1431 ;; should be scoped relative the the new module, the one defined by the macro.
1432 ;; For that, use `(@@ mod-name body)'.
c3ae0ed4 1433 ;;
8fad25c2
AW
1434 ;; Part of the macro output will be from the site of the macro use and part
1435 ;; from the macro definition. We allow source information from the macro use
1436 ;; to pass through, but we annotate the parts coming from the macro with the
1437 ;; source location information corresponding to the macro use. It would be
1438 ;; really nice if we could also annotate introduced expressions with the
1439 ;; locations corresponding to the macro definition, but that is not yet
1440 ;; possible.
78a47455 1441 (define expand-macro
8fad25c2
AW
1442 (lambda (p e r w s rib mod)
1443 (define rebuild-macro-output
1444 (lambda (x m)
1445 (cond ((pair? x)
1446 (decorate-source
1447 (cons (rebuild-macro-output (car x) m)
1448 (rebuild-macro-output (cdr x) m))
1449 s))
1450 ((syntax-object? x)
1451 (let ((w (syntax-object-wrap x)))
30398e94 1452 (let ((ms (wrap-marks w)) (ss (wrap-subst w)))
8fad25c2
AW
1453 (if (and (pair? ms) (eq? (car ms) the-anti-mark))
1454 ;; output is from original text
1455 (make-syntax-object
1456 (syntax-object-expression x)
30398e94 1457 (make-wrap (cdr ms) (if rib (cons rib (cdr ss)) (cdr ss)))
8fad25c2
AW
1458 (syntax-object-module x))
1459 ;; output introduced by macro
1460 (make-syntax-object
1461 (decorate-source (syntax-object-expression x) s)
1462 (make-wrap (cons m ms)
1463 (if rib
30398e94
MW
1464 (cons rib (cons 'shift ss))
1465 (cons 'shift ss)))
8fad25c2
AW
1466 (syntax-object-module x))))))
1467
1468 ((vector? x)
1469 (let* ((n (vector-length x))
30398e94 1470 (v (decorate-source (make-vector n) s)))
8fad25c2
AW
1471 (do ((i 0 (fx+ i 1)))
1472 ((fx= i n) v)
1473 (vector-set! v i
1474 (rebuild-macro-output (vector-ref x i) m)))))
1475 ((symbol? x)
1476 (syntax-violation #f "encountered raw symbol in macro output"
1477 (source-wrap e w (wrap-subst w) mod) x))
1478 (else (decorate-source x s)))))
9b0975f1
AW
1479 (with-fluids ((transformer-environment
1480 (lambda (k) (k e r w s rib mod))))
1481 (rebuild-macro-output (p (source-wrap e (anti-mark w) s mod))
1482 (new-mark)))))
8fad25c2 1483
78a47455 1484 (define expand-body
8fad25c2
AW
1485 ;; In processing the forms of the body, we create a new, empty wrap.
1486 ;; This wrap is augmented (destructively) each time we discover that
1487 ;; the next form is a definition. This is done:
1488 ;;
1489 ;; (1) to allow the first nondefinition form to be a call to
1490 ;; one of the defined ids even if the id previously denoted a
1491 ;; definition keyword or keyword for a macro expanding into a
1492 ;; definition;
1493 ;; (2) to prevent subsequent definition forms (but unfortunately
1494 ;; not earlier ones) and the first nondefinition form from
1495 ;; confusing one of the bound identifiers for an auxiliary
1496 ;; keyword; and
1497 ;; (3) so that we do not need to restart the expansion of the
1498 ;; first nondefinition form, which is problematic anyway
1499 ;; since it might be the first element of a begin that we
1500 ;; have just spliced into the body (meaning if we restarted,
1501 ;; we'd really need to restart with the begin or the macro
1502 ;; call that expanded into the begin, and we'd have to give
1503 ;; up allowing (begin <defn>+ <expr>+), which is itself
1504 ;; problematic since we don't know if a begin contains only
1505 ;; definitions until we've expanded it).
1506 ;;
1507 ;; Before processing the body, we also create a new environment
1508 ;; containing a placeholder for the bindings we will add later and
1509 ;; associate this environment with each form. In processing a
1510 ;; let-syntax or letrec-syntax, the associated environment may be
1511 ;; augmented with local keyword bindings, so the environment may
1512 ;; be different for different forms in the body. Once we have
1513 ;; gathered up all of the definitions, we evaluate the transformer
1514 ;; expressions and splice into r at the placeholder the new variable
1515 ;; and keyword bindings. This allows let-syntax or letrec-syntax
1516 ;; forms local to a portion or all of the body to shadow the
1517 ;; definition bindings.
1518 ;;
1519 ;; Subforms of a begin, let-syntax, or letrec-syntax are spliced
1520 ;; into the body.
1521 ;;
1522 ;; outer-form is fully wrapped w/source
1523 (lambda (body outer-form r w mod)
1524 (let* ((r (cons '("placeholder" . (placeholder)) r))
1525 (ribcage (make-empty-ribcage))
1526 (w (make-wrap (wrap-marks w) (cons ribcage (wrap-subst w)))))
1527 (let parse ((body (map (lambda (x) (cons r (wrap x w mod))) body))
1528 (ids '()) (labels '())
1529 (var-ids '()) (vars '()) (vals '()) (bindings '()))
1530 (if (null? body)
1531 (syntax-violation #f "no expressions in body" outer-form)
1532 (let ((e (cdar body)) (er (caar body)))
1533 (call-with-values
1534 (lambda () (syntax-type e er empty-wrap (source-annotation er) ribcage mod #f))
40e92f09 1535 (lambda (type value form e w s mod)
8fad25c2
AW
1536 (case type
1537 ((define-form)
1538 (let ((id (wrap value w mod)) (label (gen-label)))
1539 (let ((var (gen-var id)))
1540 (extend-ribcage! ribcage id label)
1541 (parse (cdr body)
1542 (cons id ids) (cons label labels)
1543 (cons id var-ids)
1544 (cons var vars) (cons (cons er (wrap e w mod)) vals)
1545 (cons (make-binding 'lexical var) bindings)))))
c2d822de 1546 ((define-syntax-form define-syntax-parameter-form)
8fad25c2 1547 (let ((id (wrap value w mod)) (label (gen-label)))
c3ae0ed4
AW
1548 (extend-ribcage! ribcage id label)
1549 (parse (cdr body)
1550 (cons id ids) (cons label labels)
8fad25c2 1551 var-ids vars vals
5b36d603
AW
1552 (cons (make-binding
1553 (if (eq? type 'define-syntax-parameter-form)
1554 'syntax-parameter
1555 'macro)
1556 (cons er (wrap e w mod)))
8fad25c2
AW
1557 bindings))))
1558 ((begin-form)
1559 (syntax-case e ()
1560 ((_ e1 ...)
1561 (parse (let f ((forms #'(e1 ...)))
1562 (if (null? forms)
1563 (cdr body)
1564 (cons (cons er (wrap (car forms) w mod))
1565 (f (cdr forms)))))
1566 ids labels var-ids vars vals bindings))))
1567 ((local-syntax-form)
78a47455
AW
1568 (expand-local-syntax value e er w s mod
1569 (lambda (forms er w s mod)
1570 (parse (let f ((forms forms))
1571 (if (null? forms)
1572 (cdr body)
1573 (cons (cons er (wrap (car forms) w mod))
1574 (f (cdr forms)))))
1575 ids labels var-ids vars vals bindings))))
8fad25c2
AW
1576 (else ; found a non-definition
1577 (if (null? ids)
1578 (build-sequence no-source
1579 (map (lambda (x)
78a47455 1580 (expand (cdr x) (car x) empty-wrap mod))
8fad25c2
AW
1581 (cons (cons er (source-wrap e w s mod))
1582 (cdr body))))
1583 (begin
1584 (if (not (valid-bound-ids? ids))
1585 (syntax-violation
1586 #f "invalid or duplicate identifier in definition"
1587 outer-form))
1588 (let loop ((bs bindings) (er-cache #f) (r-cache #f))
1589 (if (not (null? bs))
1590 (let* ((b (car bs)))
5b36d603 1591 (if (memq (car b) '(macro syntax-parameter))
8fad25c2
AW
1592 (let* ((er (cadr b))
1593 (r-cache
1594 (if (eq? er er-cache)
1595 r-cache
1596 (macros-only-env er))))
1597 (set-cdr! b
1598 (eval-local-transformer
78a47455 1599 (expand (cddr b) r-cache empty-wrap mod)
8fad25c2 1600 mod))
5b36d603
AW
1601 (if (eq? (car b) 'syntax-parameter)
1602 (set-cdr! b (list (cdr b))))
8fad25c2
AW
1603 (loop (cdr bs) er r-cache))
1604 (loop (cdr bs) er-cache r-cache)))))
1605 (set-cdr! r (extend-env labels bindings (cdr r)))
1606 (build-letrec no-source #t
1607 (reverse (map syntax->datum var-ids))
1608 (reverse vars)
1609 (map (lambda (x)
78a47455 1610 (expand (cdr x) (car x) empty-wrap mod))
8fad25c2
AW
1611 (reverse vals))
1612 (build-sequence no-source
1613 (map (lambda (x)
78a47455 1614 (expand (cdr x) (car x) empty-wrap mod))
8fad25c2
AW
1615 (cons (cons er (source-wrap e w s mod))
1616 (cdr body)))))))))))))))))
1617
78a47455 1618 (define expand-local-syntax
8fad25c2
AW
1619 (lambda (rec? e r w s mod k)
1620 (syntax-case e ()
1621 ((_ ((id val) ...) e1 e2 ...)
1622 (let ((ids #'(id ...)))
1623 (if (not (valid-bound-ids? ids))
1624 (syntax-violation #f "duplicate bound keyword" e)
1625 (let ((labels (gen-labels ids)))
1626 (let ((new-w (make-binding-wrap ids labels w)))
1627 (k #'(e1 e2 ...)
1628 (extend-env
1629 labels
1630 (let ((w (if rec? new-w w))
1631 (trans-r (macros-only-env r)))
1632 (map (lambda (x)
1633 (make-binding 'macro
1634 (eval-local-transformer
78a47455 1635 (expand x trans-r w mod)
8fad25c2
AW
1636 mod)))
1637 #'(val ...)))
1638 r)
1639 new-w
1640 s
1641 mod))))))
1642 (_ (syntax-violation #f "bad local syntax definition"
1643 (source-wrap e w s mod))))))
1644
1645 (define eval-local-transformer
1646 (lambda (expanded mod)
1647 (let ((p (local-eval-hook expanded mod)))
1648 (if (procedure? p)
1649 p
1650 (syntax-violation #f "nonprocedure transformer" p)))))
1651
78a47455 1652 (define expand-void
8fad25c2
AW
1653 (lambda ()
1654 (build-void no-source)))
1655
1656 (define ellipsis?
1657 (lambda (x)
1658 (and (nonsymbol-id? x)
1659 (free-id=? x #'(... ...)))))
1660
1661 (define lambda-formals
1662 (lambda (orig-args)
1663 (define (req args rreq)
1664 (syntax-case args ()
1665 (()
1666 (check (reverse rreq) #f))
1667 ((a . b) (id? #'a)
1668 (req #'b (cons #'a rreq)))
1669 (r (id? #'r)
1670 (check (reverse rreq) #'r))
1671 (else
1672 (syntax-violation 'lambda "invalid argument list" orig-args args))))
1673 (define (check req rest)
1674 (cond
1675 ((distinct-bound-ids? (if rest (cons rest req) req))
1676 (values req #f rest #f))
1677 (else
1678 (syntax-violation 'lambda "duplicate identifier in argument list"
1679 orig-args))))
1680 (req orig-args '())))
1681
78a47455 1682 (define expand-simple-lambda
8fad25c2
AW
1683 (lambda (e r w s mod req rest meta body)
1684 (let* ((ids (if rest (append req (list rest)) req))
1685 (vars (map gen-var ids))
1686 (labels (gen-labels ids)))
1687 (build-simple-lambda
1688 s
1689 (map syntax->datum req) (and rest (syntax->datum rest)) vars
1690 meta
78a47455
AW
1691 (expand-body body (source-wrap e w s mod)
1692 (extend-var-env labels vars r)
1693 (make-binding-wrap ids labels w)
1694 mod)))))
8fad25c2
AW
1695
1696 (define lambda*-formals
1697 (lambda (orig-args)
1698 (define (req args rreq)
1699 (syntax-case args ()
1700 (()
1701 (check (reverse rreq) '() #f '()))
1702 ((a . b) (id? #'a)
1703 (req #'b (cons #'a rreq)))
1704 ((a . b) (eq? (syntax->datum #'a) #:optional)
1705 (opt #'b (reverse rreq) '()))
1706 ((a . b) (eq? (syntax->datum #'a) #:key)
1707 (key #'b (reverse rreq) '() '()))
1708 ((a b) (eq? (syntax->datum #'a) #:rest)
1709 (rest #'b (reverse rreq) '() '()))
1710 (r (id? #'r)
1711 (rest #'r (reverse rreq) '() '()))
1712 (else
1713 (syntax-violation 'lambda* "invalid argument list" orig-args args))))
1714 (define (opt args req ropt)
1715 (syntax-case args ()
1716 (()
1717 (check req (reverse ropt) #f '()))
1718 ((a . b) (id? #'a)
1719 (opt #'b req (cons #'(a #f) ropt)))
1720 (((a init) . b) (id? #'a)
1721 (opt #'b req (cons #'(a init) ropt)))
1722 ((a . b) (eq? (syntax->datum #'a) #:key)
1723 (key #'b req (reverse ropt) '()))
1724 ((a b) (eq? (syntax->datum #'a) #:rest)
1725 (rest #'b req (reverse ropt) '()))
1726 (r (id? #'r)
1727 (rest #'r req (reverse ropt) '()))
1728 (else
1729 (syntax-violation 'lambda* "invalid optional argument list"
1730 orig-args args))))
1731 (define (key args req opt rkey)
1732 (syntax-case args ()
1733 (()
1734 (check req opt #f (cons #f (reverse rkey))))
1735 ((a . b) (id? #'a)
1736 (with-syntax ((k (symbol->keyword (syntax->datum #'a))))
1737 (key #'b req opt (cons #'(k a #f) rkey))))
1738 (((a init) . b) (id? #'a)
1739 (with-syntax ((k (symbol->keyword (syntax->datum #'a))))
1740 (key #'b req opt (cons #'(k a init) rkey))))
1741 (((a init k) . b) (and (id? #'a)
1742 (keyword? (syntax->datum #'k)))
1743 (key #'b req opt (cons #'(k a init) rkey)))
1744 ((aok) (eq? (syntax->datum #'aok) #:allow-other-keys)
1745 (check req opt #f (cons #t (reverse rkey))))
1746 ((aok a b) (and (eq? (syntax->datum #'aok) #:allow-other-keys)
1747 (eq? (syntax->datum #'a) #:rest))
1748 (rest #'b req opt (cons #t (reverse rkey))))
1749 ((aok . r) (and (eq? (syntax->datum #'aok) #:allow-other-keys)
1750 (id? #'r))
1751 (rest #'r req opt (cons #t (reverse rkey))))
1752 ((a b) (eq? (syntax->datum #'a) #:rest)
1753 (rest #'b req opt (cons #f (reverse rkey))))
1754 (r (id? #'r)
1755 (rest #'r req opt (cons #f (reverse rkey))))
1756 (else
1757 (syntax-violation 'lambda* "invalid keyword argument list"
1758 orig-args args))))
1759 (define (rest args req opt kw)
1760 (syntax-case args ()
1761 (r (id? #'r)
1762 (check req opt #'r kw))
1763 (else
1764 (syntax-violation 'lambda* "invalid rest argument"
1765 orig-args args))))
1766 (define (check req opt rest kw)
1767 (cond
1768 ((distinct-bound-ids?
1769 (append req (map car opt) (if rest (list rest) '())
1770 (if (pair? kw) (map cadr (cdr kw)) '())))
1771 (values req opt rest kw))
1772 (else
1773 (syntax-violation 'lambda* "duplicate identifier in argument list"
1774 orig-args))))
1775 (req orig-args '())))
1776
78a47455 1777 (define expand-lambda-case
8fad25c2 1778 (lambda (e r w s mod get-formals clauses)
78a47455 1779 (define (parse-req req opt rest kw body)
8fad25c2
AW
1780 (let ((vars (map gen-var req))
1781 (labels (gen-labels req)))
1782 (let ((r* (extend-var-env labels vars r))
1783 (w* (make-binding-wrap req labels w)))
78a47455
AW
1784 (parse-opt (map syntax->datum req)
1785 opt rest kw body (reverse vars) r* w* '() '()))))
1786 (define (parse-opt req opt rest kw body vars r* w* out inits)
8fad25c2
AW
1787 (cond
1788 ((pair? opt)
1789 (syntax-case (car opt) ()
1790 ((id i)
1791 (let* ((v (gen-var #'id))
1792 (l (gen-labels (list v)))
1793 (r** (extend-var-env l (list v) r*))
1794 (w** (make-binding-wrap (list #'id) l w*)))
78a47455
AW
1795 (parse-opt req (cdr opt) rest kw body (cons v vars)
1796 r** w** (cons (syntax->datum #'id) out)
1797 (cons (expand #'i r* w* mod) inits))))))
8fad25c2
AW
1798 (rest
1799 (let* ((v (gen-var rest))
1800 (l (gen-labels (list v)))
1801 (r* (extend-var-env l (list v) r*))
1802 (w* (make-binding-wrap (list rest) l w*)))
78a47455
AW
1803 (parse-kw req (if (pair? out) (reverse out) #f)
1804 (syntax->datum rest)
1805 (if (pair? kw) (cdr kw) kw)
1806 body (cons v vars) r* w*
1807 (if (pair? kw) (car kw) #f)
1808 '() inits)))
8fad25c2 1809 (else
78a47455
AW
1810 (parse-kw req (if (pair? out) (reverse out) #f) #f
1811 (if (pair? kw) (cdr kw) kw)
1812 body vars r* w*
1813 (if (pair? kw) (car kw) #f)
1814 '() inits))))
1815 (define (parse-kw req opt rest kw body vars r* w* aok out inits)
8fad25c2
AW
1816 (cond
1817 ((pair? kw)
1818 (syntax-case (car kw) ()
1819 ((k id i)
1820 (let* ((v (gen-var #'id))
1821 (l (gen-labels (list v)))
1822 (r** (extend-var-env l (list v) r*))
1823 (w** (make-binding-wrap (list #'id) l w*)))
78a47455
AW
1824 (parse-kw req opt rest (cdr kw) body (cons v vars)
1825 r** w** aok
1826 (cons (list (syntax->datum #'k)
1827 (syntax->datum #'id)
1828 v)
1829 out)
1830 (cons (expand #'i r* w* mod) inits))))))
8fad25c2 1831 (else
78a47455
AW
1832 (parse-body req opt rest
1833 (if (or aok (pair? out)) (cons aok (reverse out)) #f)
1834 body (reverse vars) r* w* (reverse inits) '()))))
1835 (define (parse-body req opt rest kw body vars r* w* inits meta)
8fad25c2
AW
1836 (syntax-case body ()
1837 ((docstring e1 e2 ...) (string? (syntax->datum #'docstring))
78a47455
AW
1838 (parse-body req opt rest kw #'(e1 e2 ...) vars r* w* inits
1839 (append meta
1840 `((documentation
1841 . ,(syntax->datum #'docstring))))))
8fad25c2 1842 ((#((k . v) ...) e1 e2 ...)
78a47455
AW
1843 (parse-body req opt rest kw #'(e1 e2 ...) vars r* w* inits
1844 (append meta (syntax->datum #'((k . v) ...)))))
8fad25c2
AW
1845 ((e1 e2 ...)
1846 (values meta req opt rest kw inits vars
78a47455
AW
1847 (expand-body #'(e1 e2 ...) (source-wrap e w s mod)
1848 r* w* mod)))))
8fad25c2
AW
1849
1850 (syntax-case clauses ()
1851 (() (values '() #f))
1852 (((args e1 e2 ...) (args* e1* e2* ...) ...)
1853 (call-with-values (lambda () (get-formals #'args))
1854 (lambda (req opt rest kw)
1855 (call-with-values (lambda ()
78a47455 1856 (parse-req req opt rest kw #'(e1 e2 ...)))
8fad25c2
AW
1857 (lambda (meta req opt rest kw inits vars body)
1858 (call-with-values
1859 (lambda ()
78a47455
AW
1860 (expand-lambda-case e r w s mod get-formals
1861 #'((args* e1* e2* ...) ...)))
8fad25c2
AW
1862 (lambda (meta* else*)
1863 (values
1864 (append meta meta*)
1865 (build-lambda-case s req opt rest kw inits vars
1866 body else*))))))))))))
1867
1868 ;; data
1869
1870 ;; strips syntax-objects down to top-wrap
1871 ;;
1872 ;; since only the head of a list is annotated by the reader, not each pair
1873 ;; in the spine, we also check for pairs whose cars are annotated in case
1874 ;; we've been passed the cdr of an annotated list
1875
1876 (define strip
1877 (lambda (x w)
1878 (if (top-marked? w)
1879 x
1880 (let f ((x x))
1881 (cond
1882 ((syntax-object? x)
1883 (strip (syntax-object-expression x) (syntax-object-wrap x)))
1884 ((pair? x)
1885 (let ((a (f (car x))) (d (f (cdr x))))
1886 (if (and (eq? a (car x)) (eq? d (cdr x)))
1887 x
1888 (cons a d))))
1889 ((vector? x)
1890 (let ((old (vector->list x)))
1891 (let ((new (map f old)))
e2ccab57
AW
1892 ;; inlined and-map with two args
1893 (let lp ((l1 old) (l2 new))
1894 (if (null? l1)
1895 x
1896 (if (eq? (car l1) (car l2))
1897 (lp (cdr l1) (cdr l2))
1898 (list->vector new)))))))
8fad25c2
AW
1899 (else x))))))
1900
1901 ;; lexical variables
1902
1903 (define gen-var
1904 (lambda (id)
1905 (let ((id (if (syntax-object? id) (syntax-object-expression id) id)))
1906 (build-lexical-var no-source id))))
1907
1908 ;; appears to return a reversed list
1909 (define lambda-var-list
1910 (lambda (vars)
1911 (let lvl ((vars vars) (ls '()) (w empty-wrap))
1912 (cond
1913 ((pair? vars) (lvl (cdr vars) (cons (wrap (car vars) w #f) ls) w))
1914 ((id? vars) (cons (wrap vars w #f) ls))
1915 ((null? vars) ls)
1916 ((syntax-object? vars)
1917 (lvl (syntax-object-expression vars)
1918 ls
1919 (join-wraps w (syntax-object-wrap vars))))
1920 ;; include anything else to be caught by subsequent error
1921 ;; checking
1922 (else (cons vars ls))))))
1923
1924 ;; core transformers
1925
1926 (global-extend 'local-syntax 'letrec-syntax #t)
1927 (global-extend 'local-syntax 'let-syntax #f)
1928
c070de63 1929 (global-extend
ea3ca4e4 1930 'core 'syntax-parameterize
c070de63
AW
1931 (lambda (e r w s mod)
1932 (syntax-case e ()
1933 ((_ ((var val) ...) e1 e2 ...)
1934 (valid-bound-ids? #'(var ...))
1935 (let ((names
1936 (map (lambda (x)
1937 (call-with-values
5b36d603
AW
1938 (lambda () (resolve-identifier x w r mod #f))
1939 (lambda (type value mod)
c070de63
AW
1940 (case type
1941 ((displaced-lexical)
ea3ca4e4 1942 (syntax-violation 'syntax-parameterize
c070de63
AW
1943 "identifier out of context"
1944 e
1945 (source-wrap x w s mod)))
5b36d603
AW
1946 ((syntax-parameter)
1947 value)
1948 (else
1949 (syntax-violation 'syntax-parameterize
1950 "invalid syntax parameter"
1951 e
1952 (source-wrap x w s mod)))))))
c070de63
AW
1953 #'(var ...)))
1954 (bindings
1955 (let ((trans-r (macros-only-env r)))
1956 (map (lambda (x)
1957 (make-binding
1958 'macro
b2208d2e 1959 (eval-local-transformer (expand x trans-r w mod) mod)))
c070de63 1960 #'(val ...)))))
b2208d2e 1961 (expand-body #'(e1 e2 ...)
c070de63
AW
1962 (source-wrap e w s mod)
1963 (extend-env names bindings r)
1964 w
1965 mod)))
ea3ca4e4 1966 (_ (syntax-violation 'syntax-parameterize "bad syntax"
c070de63 1967 (source-wrap e w s mod))))))
8fad25c2
AW
1968
1969 (global-extend 'core 'quote
1970 (lambda (e r w s mod)
1971 (syntax-case e ()
1972 ((_ e) (build-data s (strip #'e w)))
1973 (_ (syntax-violation 'quote "bad syntax"
1974 (source-wrap e w s mod))))))
1975
c070de63
AW
1976 (global-extend
1977 'core 'syntax
1978 (let ()
1979 (define gen-syntax
1980 (lambda (src e r maps ellipsis? mod)
1981 (if (id? e)
1982 (call-with-values (lambda ()
5b36d603
AW
1983 (resolve-identifier e empty-wrap r mod #f))
1984 (lambda (type value mod)
c070de63
AW
1985 (case type
1986 ((syntax)
1987 (call-with-values
1988 (lambda () (gen-ref src (car value) (cdr value) maps))
1989 (lambda (var maps)
1990 (values `(ref ,var) maps))))
1991 (else
1992 (if (ellipsis? e)
1993 (syntax-violation 'syntax "misplaced ellipsis" src)
1994 (values `(quote ,e) maps))))))
1995 (syntax-case e ()
1996 ((dots e)
1997 (ellipsis? #'dots)
1998 (gen-syntax src #'e r maps (lambda (x) #f) mod))
1999 ((x dots . y)
2000 ;; this could be about a dozen lines of code, except that we
2001 ;; choose to handle #'(x ... ...) forms
2002 (ellipsis? #'dots)
2003 (let f ((y #'y)
2004 (k (lambda (maps)
2005 (call-with-values
2006 (lambda ()
2007 (gen-syntax src #'x r
2008 (cons '() maps) ellipsis? mod))
2009 (lambda (x maps)
2010 (if (null? (car maps))
2011 (syntax-violation 'syntax "extra ellipsis"
2012 src)
2013 (values (gen-map x (car maps))
2014 (cdr maps))))))))
2015 (syntax-case y ()
2016 ((dots . y)
2017 (ellipsis? #'dots)
2018 (f #'y
2019 (lambda (maps)
8fad25c2 2020 (call-with-values
c070de63
AW
2021 (lambda () (k (cons '() maps)))
2022 (lambda (x maps)
2023 (if (null? (car maps))
2024 (syntax-violation 'syntax "extra ellipsis" src)
2025 (values (gen-mappend x (car maps))
2026 (cdr maps))))))))
2027 (_ (call-with-values
2028 (lambda () (gen-syntax src y r maps ellipsis? mod))
2029 (lambda (y maps)
2030 (call-with-values
2031 (lambda () (k maps))
2032 (lambda (x maps)
2033 (values (gen-append x y) maps)))))))))
2034 ((x . y)
2035 (call-with-values
2036 (lambda () (gen-syntax src #'x r maps ellipsis? mod))
2037 (lambda (x maps)
2038 (call-with-values
2039 (lambda () (gen-syntax src #'y r maps ellipsis? mod))
2040 (lambda (y maps) (values (gen-cons x y) maps))))))
2041 (#(e1 e2 ...)
2042 (call-with-values
2043 (lambda ()
2044 (gen-syntax src #'(e1 e2 ...) r maps ellipsis? mod))
2045 (lambda (e maps) (values (gen-vector e) maps))))
2046 (_ (values `(quote ,e) maps))))))
2047
2048 (define gen-ref
2049 (lambda (src var level maps)
2050 (if (fx= level 0)
2051 (values var maps)
2052 (if (null? maps)
2053 (syntax-violation 'syntax "missing ellipsis" src)
2054 (call-with-values
2055 (lambda () (gen-ref src var (fx- level 1) (cdr maps)))
2056 (lambda (outer-var outer-maps)
2057 (let ((b (assq outer-var (car maps))))
2058 (if b
2059 (values (cdr b) maps)
2060 (let ((inner-var (gen-var 'tmp)))
2061 (values inner-var
2062 (cons (cons (cons outer-var inner-var)
2063 (car maps))
2064 outer-maps)))))))))))
2065
2066 (define gen-mappend
2067 (lambda (e map-env)
2068 `(apply (primitive append) ,(gen-map e map-env))))
2069
2070 (define gen-map
2071 (lambda (e map-env)
2072 (let ((formals (map cdr map-env))
2073 (actuals (map (lambda (x) `(ref ,(car x))) map-env)))
2074 (cond
2075 ((eq? (car e) 'ref)
2076 ;; identity map equivalence:
2077 ;; (map (lambda (x) x) y) == y
2078 (car actuals))
2079 ((and-map
2080 (lambda (x) (and (eq? (car x) 'ref) (memq (cadr x) formals)))
2081 (cdr e))
2082 ;; eta map equivalence:
2083 ;; (map (lambda (x ...) (f x ...)) y ...) == (map f y ...)
2084 `(map (primitive ,(car e))
2085 ,@(map (let ((r (map cons formals actuals)))
2086 (lambda (x) (cdr (assq (cadr x) r))))
2087 (cdr e))))
2088 (else `(map (lambda ,formals ,e) ,@actuals))))))
2089
2090 (define gen-cons
2091 (lambda (x y)
2092 (case (car y)
2093 ((quote)
2094 (if (eq? (car x) 'quote)
2095 `(quote (,(cadr x) . ,(cadr y)))
2096 (if (eq? (cadr y) '())
2097 `(list ,x)
2098 `(cons ,x ,y))))
2099 ((list) `(list ,x ,@(cdr y)))
2100 (else `(cons ,x ,y)))))
2101
2102 (define gen-append
2103 (lambda (x y)
2104 (if (equal? y '(quote ()))
2105 x
2106 `(append ,x ,y))))
2107
2108 (define gen-vector
2109 (lambda (x)
2110 (cond
2111 ((eq? (car x) 'list) `(vector ,@(cdr x)))
2112 ((eq? (car x) 'quote) `(quote #(,@(cadr x))))
2113 (else `(list->vector ,x)))))
2114
2115
2116 (define regen
2117 (lambda (x)
2118 (case (car x)
2119 ((ref) (build-lexical-reference 'value no-source (cadr x) (cadr x)))
2120 ((primitive) (build-primref no-source (cadr x)))
2121 ((quote) (build-data no-source (cadr x)))
2122 ((lambda)
2123 (if (list? (cadr x))
2124 (build-simple-lambda no-source (cadr x) #f (cadr x) '() (regen (caddr x)))
2125 (error "how did we get here" x)))
2126 (else (build-primcall no-source (car x) (map regen (cdr x)))))))
2127
2128 (lambda (e r w s mod)
2129 (let ((e (source-wrap e w s mod)))
2130 (syntax-case e ()
2131 ((_ x)
2132 (call-with-values
2133 (lambda () (gen-syntax e #'x r '() ellipsis? mod))
2134 (lambda (e maps) (regen e))))
2135 (_ (syntax-violation 'syntax "bad `syntax' form" e)))))))
8fad25c2
AW
2136
2137 (global-extend 'core 'lambda
c3ae0ed4 2138 (lambda (e r w s mod)
8fad25c2
AW
2139 (syntax-case e ()
2140 ((_ args e1 e2 ...)
2141 (call-with-values (lambda () (lambda-formals #'args))
2142 (lambda (req opt rest kw)
2143 (let lp ((body #'(e1 e2 ...)) (meta '()))
2144 (syntax-case body ()
2145 ((docstring e1 e2 ...) (string? (syntax->datum #'docstring))
2146 (lp #'(e1 e2 ...)
2147 (append meta
2148 `((documentation
2149 . ,(syntax->datum #'docstring))))))
2150 ((#((k . v) ...) e1 e2 ...)
2151 (lp #'(e1 e2 ...)
2152 (append meta (syntax->datum #'((k . v) ...)))))
78a47455 2153 (_ (expand-simple-lambda e r w s mod req rest meta body)))))))
8fad25c2 2154 (_ (syntax-violation 'lambda "bad lambda" e)))))
3785c5b2 2155
8fad25c2
AW
2156 (global-extend 'core 'lambda*
2157 (lambda (e r w s mod)
2158 (syntax-case e ()
2159 ((_ args e1 e2 ...)
2160 (call-with-values
2161 (lambda ()
78a47455
AW
2162 (expand-lambda-case e r w s mod
2163 lambda*-formals #'((args e1 e2 ...))))
8fad25c2
AW
2164 (lambda (meta lcase)
2165 (build-case-lambda s meta lcase))))
2166 (_ (syntax-violation 'lambda "bad lambda*" e)))))
2167
2168 (global-extend 'core 'case-lambda
2169 (lambda (e r w s mod)
2170 (syntax-case e ()
2171 ((_ (args e1 e2 ...) (args* e1* e2* ...) ...)
2172 (call-with-values
2173 (lambda ()
78a47455
AW
2174 (expand-lambda-case e r w s mod
2175 lambda-formals
2176 #'((args e1 e2 ...) (args* e1* e2* ...) ...)))
8fad25c2
AW
2177 (lambda (meta lcase)
2178 (build-case-lambda s meta lcase))))
2179 (_ (syntax-violation 'case-lambda "bad case-lambda" e)))))
2180
2181 (global-extend 'core 'case-lambda*
2182 (lambda (e r w s mod)
2183 (syntax-case e ()
2184 ((_ (args e1 e2 ...) (args* e1* e2* ...) ...)
2185 (call-with-values
2186 (lambda ()
78a47455
AW
2187 (expand-lambda-case e r w s mod
2188 lambda*-formals
2189 #'((args e1 e2 ...) (args* e1* e2* ...) ...)))
8fad25c2
AW
2190 (lambda (meta lcase)
2191 (build-case-lambda s meta lcase))))
2192 (_ (syntax-violation 'case-lambda "bad case-lambda*" e)))))
2193
2194 (global-extend 'core 'let
2195 (let ()
78a47455 2196 (define (expand-let e r w s mod constructor ids vals exps)
8fad25c2
AW
2197 (if (not (valid-bound-ids? ids))
2198 (syntax-violation 'let "duplicate bound variable" e)
2199 (let ((labels (gen-labels ids))
2200 (new-vars (map gen-var ids)))
2201 (let ((nw (make-binding-wrap ids labels w))
2202 (nr (extend-var-env labels new-vars r)))
2203 (constructor s
2204 (map syntax->datum ids)
2205 new-vars
78a47455
AW
2206 (map (lambda (x) (expand x r w mod)) vals)
2207 (expand-body exps (source-wrap e nw s mod)
2208 nr nw mod))))))
8fad25c2
AW
2209 (lambda (e r w s mod)
2210 (syntax-case e ()
2211 ((_ ((id val) ...) e1 e2 ...)
2212 (and-map id? #'(id ...))
78a47455
AW
2213 (expand-let e r w s mod
2214 build-let
2215 #'(id ...)
2216 #'(val ...)
2217 #'(e1 e2 ...)))
8fad25c2
AW
2218 ((_ f ((id val) ...) e1 e2 ...)
2219 (and (id? #'f) (and-map id? #'(id ...)))
78a47455
AW
2220 (expand-let e r w s mod
2221 build-named-let
2222 #'(f id ...)
2223 #'(val ...)
2224 #'(e1 e2 ...)))
8fad25c2
AW
2225 (_ (syntax-violation 'let "bad let" (source-wrap e w s mod)))))))
2226
2227
2228 (global-extend 'core 'letrec
c3ae0ed4
AW
2229 (lambda (e r w s mod)
2230 (syntax-case e ()
2231 ((_ ((id val) ...) e1 e2 ...)
2232 (and-map id? #'(id ...))
8fad25c2
AW
2233 (let ((ids #'(id ...)))
2234 (if (not (valid-bound-ids? ids))
2235 (syntax-violation 'letrec "duplicate bound variable" e)
2236 (let ((labels (gen-labels ids))
2237 (new-vars (map gen-var ids)))
2238 (let ((w (make-binding-wrap ids labels w))
2239 (r (extend-var-env labels new-vars r)))
2240 (build-letrec s #f
2241 (map syntax->datum ids)
2242 new-vars
78a47455
AW
2243 (map (lambda (x) (expand x r w mod)) #'(val ...))
2244 (expand-body #'(e1 e2 ...)
2245 (source-wrap e w s mod) r w mod)))))))
8fad25c2
AW
2246 (_ (syntax-violation 'letrec "bad letrec" (source-wrap e w s mod))))))
2247
2248
2249 (global-extend 'core 'letrec*
2250 (lambda (e r w s mod)
2251 (syntax-case e ()
2252 ((_ ((id val) ...) e1 e2 ...)
2253 (and-map id? #'(id ...))
2254 (let ((ids #'(id ...)))
2255 (if (not (valid-bound-ids? ids))
2256 (syntax-violation 'letrec* "duplicate bound variable" e)
2257 (let ((labels (gen-labels ids))
2258 (new-vars (map gen-var ids)))
2259 (let ((w (make-binding-wrap ids labels w))
2260 (r (extend-var-env labels new-vars r)))
2261 (build-letrec s #t
2262 (map syntax->datum ids)
2263 new-vars
78a47455
AW
2264 (map (lambda (x) (expand x r w mod)) #'(val ...))
2265 (expand-body #'(e1 e2 ...)
2266 (source-wrap e w s mod) r w mod)))))))
8fad25c2
AW
2267 (_ (syntax-violation 'letrec* "bad letrec*" (source-wrap e w s mod))))))
2268
2269
c070de63
AW
2270 (global-extend
2271 'core 'set!
2272 (lambda (e r w s mod)
2273 (syntax-case e ()
2274 ((_ id val)
2275 (id? #'id)
2276 (call-with-values
5b36d603
AW
2277 (lambda () (resolve-identifier #'id w r mod #t))
2278 (lambda (type value id-mod)
c070de63
AW
2279 (case type
2280 ((lexical)
2281 (build-lexical-assignment s (syntax->datum #'id) value
b2208d2e 2282 (expand #'val r w mod)))
c070de63 2283 ((global)
b2208d2e 2284 (build-global-assignment s value (expand #'val r w mod) id-mod))
c070de63
AW
2285 ((macro)
2286 (if (procedure-property value 'variable-transformer)
b2208d2e 2287 ;; As syntax-type does, call expand-macro with
c070de63 2288 ;; the mod of the expression. Hmm.
b2208d2e 2289 (expand (expand-macro value e r w s #f mod) r empty-wrap mod)
c070de63
AW
2290 (syntax-violation 'set! "not a variable transformer"
2291 (wrap e w mod)
2292 (wrap #'id w id-mod))))
2293 ((displaced-lexical)
2294 (syntax-violation 'set! "identifier out of context"
2295 (wrap #'id w mod)))
2296 (else
2297 (syntax-violation 'set! "bad set!" (source-wrap e w s mod)))))))
2298 ((_ (head tail ...) val)
2299 (call-with-values
2300 (lambda () (syntax-type #'head r empty-wrap no-source #f mod #t))
dfadcf85 2301 (lambda (type value ee* ee ww ss modmod)
c070de63
AW
2302 (case type
2303 ((module-ref)
b2208d2e 2304 (let ((val (expand #'val r w mod)))
c070de63
AW
2305 (call-with-values (lambda () (value #'(head tail ...) r w))
2306 (lambda (e r w s* mod)
2307 (syntax-case e ()
2308 (e (id? #'e)
2309 (build-global-assignment s (syntax->datum #'e)
2310 val mod)))))))
2311 (else
2312 (build-call s
b2208d2e
AW
2313 (expand #'(setter head) r w mod)
2314 (map (lambda (e) (expand e r w mod))
c070de63
AW
2315 #'(tail ... val))))))))
2316 (_ (syntax-violation 'set! "bad set!" (source-wrap e w s mod))))))
8fad25c2
AW
2317
2318 (global-extend 'module-ref '@
2319 (lambda (e r w)
2320 (syntax-case e ()
2321 ((_ (mod ...) id)
2322 (and (and-map id? #'(mod ...)) (id? #'id))
8210c853
MW
2323 ;; Strip the wrap from the identifier and return top-wrap
2324 ;; so that the identifier will not be captured by lexicals.
2325 (values (syntax->datum #'id) r top-wrap #f
8fad25c2
AW
2326 (syntax->datum
2327 #'(public mod ...)))))))
2328
2329 (global-extend 'module-ref '@@
2330 (lambda (e r w)
2331 (define remodulate
2332 (lambda (x mod)
2333 (cond ((pair? x)
2334 (cons (remodulate (car x) mod)
2335 (remodulate (cdr x) mod)))
2336 ((syntax-object? x)
2337 (make-syntax-object
2338 (remodulate (syntax-object-expression x) mod)
2339 (syntax-object-wrap x)
2340 ;; hither the remodulation
2341 mod))
2342 ((vector? x)
2343 (let* ((n (vector-length x)) (v (make-vector n)))
2344 (do ((i 0 (fx+ i 1)))
2345 ((fx= i n) v)
2346 (vector-set! v i (remodulate (vector-ref x i) mod)))))
2347 (else x))))
8210c853
MW
2348 (syntax-case e (@@)
2349 ((_ (mod ...) id)
2350 (and (and-map id? #'(mod ...)) (id? #'id))
2351 ;; Strip the wrap from the identifier and return top-wrap
2352 ;; so that the identifier will not be captured by lexicals.
2353 (values (syntax->datum #'id) r top-wrap #f
2354 (syntax->datum
2355 #'(private mod ...))))
2356 ((_ @@ (mod ...) exp)
8fad25c2 2357 (and-map id? #'(mod ...))
8210c853
MW
2358 ;; This is a special syntax used to support R6RS library forms.
2359 ;; Unlike the syntax above, the last item is not restricted to
2360 ;; be a single identifier, and the syntax objects are kept
2361 ;; intact, with only their module changed.
8fad25c2
AW
2362 (let ((mod (syntax->datum #'(private mod ...))))
2363 (values (remodulate #'exp mod)
2364 r w (source-annotation #'exp)
2365 mod))))))
9365d8ad 2366
8fad25c2
AW
2367 (global-extend 'core 'if
2368 (lambda (e r w s mod)
2369 (syntax-case e ()
2370 ((_ test then)
2371 (build-conditional
2372 s
78a47455
AW
2373 (expand #'test r w mod)
2374 (expand #'then r w mod)
8fad25c2
AW
2375 (build-void no-source)))
2376 ((_ test then else)
2377 (build-conditional
2378 s
78a47455
AW
2379 (expand #'test r w mod)
2380 (expand #'then r w mod)
2381 (expand #'else r w mod))))))
8fad25c2
AW
2382
2383 (global-extend 'core 'with-fluids
2384 (lambda (e r w s mod)
2385 (syntax-case e ()
2386 ((_ ((fluid val) ...) b b* ...)
2387 (build-dynlet
2388 s
78a47455
AW
2389 (map (lambda (x) (expand x r w mod)) #'(fluid ...))
2390 (map (lambda (x) (expand x r w mod)) #'(val ...))
2391 (expand-body #'(b b* ...)
2392 (source-wrap e w s mod) r w mod))))))
6360c1d4 2393
8fad25c2
AW
2394 (global-extend 'begin 'begin '())
2395
2396 (global-extend 'define 'define '())
2397
2398 (global-extend 'define-syntax 'define-syntax '())
c2d822de 2399 (global-extend 'define-syntax-parameter 'define-syntax-parameter '())
8fad25c2
AW
2400
2401 (global-extend 'eval-when 'eval-when '())
2402
2403 (global-extend 'core 'syntax-case
2404 (let ()
2405 (define convert-pattern
2406 ;; accepts pattern & keys
2407 ;; returns $sc-dispatch pattern & ids
2408 (lambda (pattern keys)
2409 (define cvt*
2410 (lambda (p* n ids)
0ed9680f
SIT
2411 (if (not (pair? p*))
2412 (cvt p* n ids)
8fad25c2
AW
2413 (call-with-values
2414 (lambda () (cvt* (cdr p*) n ids))
2415 (lambda (y ids)
2416 (call-with-values
2417 (lambda () (cvt (car p*) n ids))
2418 (lambda (x ids)
2419 (values (cons x y) ids))))))))
0ed9680f
SIT
2420
2421 (define (v-reverse x)
2422 (let loop ((r '()) (x x))
2423 (if (not (pair? x))
2424 (values r x)
2425 (loop (cons (car x) r) (cdr x)))))
2426
8fad25c2
AW
2427 (define cvt
2428 (lambda (p n ids)
2429 (if (id? p)
2430 (cond
2431 ((bound-id-member? p keys)
2432 (values (vector 'free-id p) ids))
2433 ((free-id=? p #'_)
2434 (values '_ ids))
2435 (else
2436 (values 'any (cons (cons p n) ids))))
2437 (syntax-case p ()
2438 ((x dots)
2439 (ellipsis? (syntax dots))
2440 (call-with-values
2441 (lambda () (cvt (syntax x) (fx+ n 1) ids))
2442 (lambda (p ids)
2443 (values (if (eq? p 'any) 'each-any (vector 'each p))
2444 ids))))
0ed9680f 2445 ((x dots . ys)
8fad25c2
AW
2446 (ellipsis? (syntax dots))
2447 (call-with-values
0ed9680f 2448 (lambda () (cvt* (syntax ys) n ids))
8fad25c2
AW
2449 (lambda (ys ids)
2450 (call-with-values
2451 (lambda () (cvt (syntax x) (+ n 1) ids))
2452 (lambda (x ids)
0ed9680f
SIT
2453 (call-with-values
2454 (lambda () (v-reverse ys))
2455 (lambda (ys e)
2456 (values `#(each+ ,x ,ys ,e)
2457 ids))))))))
8fad25c2
AW
2458 ((x . y)
2459 (call-with-values
2460 (lambda () (cvt (syntax y) n ids))
2461 (lambda (y ids)
2462 (call-with-values
2463 (lambda () (cvt (syntax x) n ids))
2464 (lambda (x ids)
2465 (values (cons x y) ids))))))
2466 (() (values '() ids))
2467 (#(x ...)
2468 (call-with-values
2469 (lambda () (cvt (syntax (x ...)) n ids))
2470 (lambda (p ids) (values (vector 'vector p) ids))))
2471 (x (values (vector 'atom (strip p empty-wrap)) ids))))))
2472 (cvt pattern 0 '())))
2473
2474 (define build-dispatch-call
2475 (lambda (pvars exp y r mod)
2476 (let ((ids (map car pvars)) (levels (map cdr pvars)))
2477 (let ((labels (gen-labels ids)) (new-vars (map gen-var ids)))
a881a4ae
AW
2478 (build-primcall
2479 no-source
2480 'apply
2481 (list (build-simple-lambda no-source (map syntax->datum ids) #f new-vars '()
b2208d2e 2482 (expand exp
a881a4ae
AW
2483 (extend-env
2484 labels
2485 (map (lambda (var level)
2486 (make-binding 'syntax `(,var . ,level)))
2487 new-vars
2488 (map cdr pvars))
2489 r)
2490 (make-binding-wrap ids labels empty-wrap)
2491 mod))
2492 y))))))
8fad25c2
AW
2493
2494 (define gen-clause
2495 (lambda (x keys clauses r pat fender exp mod)
2496 (call-with-values
2497 (lambda () (convert-pattern pat keys))
2498 (lambda (p pvars)
2499 (cond
2500 ((not (distinct-bound-ids? (map car pvars)))
2501 (syntax-violation 'syntax-case "duplicate pattern variable" pat))
2502 ((not (and-map (lambda (x) (not (ellipsis? (car x)))) pvars))
2503 (syntax-violation 'syntax-case "misplaced ellipsis" pat))
2504 (else
2505 (let ((y (gen-var 'tmp)))
2506 ;; fat finger binding and references to temp variable y
7081d4f9
AW
2507 (build-call no-source
2508 (build-simple-lambda no-source (list 'tmp) #f (list y) '()
2509 (let ((y (build-lexical-reference 'value no-source
2510 'tmp y)))
2511 (build-conditional no-source
2512 (syntax-case fender ()
2513 (#t y)
2514 (_ (build-conditional no-source
2515 y
2516 (build-dispatch-call pvars fender y r mod)
2517 (build-data no-source #f))))
2518 (build-dispatch-call pvars exp y r mod)
2519 (gen-syntax-case x keys clauses r mod))))
2520 (list (if (eq? p 'any)
a881a4ae
AW
2521 (build-primcall no-source 'list (list x))
2522 (build-primcall no-source '$sc-dispatch
2523 (list x (build-data no-source p)))))))))))))
8fad25c2
AW
2524
2525 (define gen-syntax-case
2526 (lambda (x keys clauses r mod)
2527 (if (null? clauses)
a881a4ae
AW
2528 (build-primcall no-source 'syntax-violation
2529 (list (build-data no-source #f)
2530 (build-data no-source
2531 "source expression failed to match any pattern")
2532 x))
8fad25c2
AW
2533 (syntax-case (car clauses) ()
2534 ((pat exp)
2535 (if (and (id? #'pat)
2536 (and-map (lambda (x) (not (free-id=? #'pat x)))
2537 (cons #'(... ...) keys)))
9120f130 2538 (if (free-id=? #'pat #'_)
78a47455 2539 (expand #'exp r empty-wrap mod)
8fad25c2
AW
2540 (let ((labels (list (gen-label)))
2541 (var (gen-var #'pat)))
7081d4f9
AW
2542 (build-call no-source
2543 (build-simple-lambda
2544 no-source (list (syntax->datum #'pat)) #f (list var)
2545 '()
b2208d2e 2546 (expand #'exp
7081d4f9
AW
2547 (extend-env labels
2548 (list (make-binding 'syntax `(,var . 0)))
2549 r)
2550 (make-binding-wrap #'(pat)
2551 labels empty-wrap)
2552 mod))
2553 (list x))))
8fad25c2
AW
2554 (gen-clause x keys (cdr clauses) r
2555 #'pat #t #'exp mod)))
2556 ((pat fender exp)
2557 (gen-clause x keys (cdr clauses) r
2558 #'pat #'fender #'exp mod))
2559 (_ (syntax-violation 'syntax-case "invalid clause"
2560 (car clauses)))))))
2561
2562 (lambda (e r w s mod)
2563 (let ((e (source-wrap e w s mod)))
2564 (syntax-case e ()
2565 ((_ val (key ...) m ...)
2566 (if (and-map (lambda (x) (and (id? x) (not (ellipsis? x))))
2567 #'(key ...))
2568 (let ((x (gen-var 'tmp)))
2569 ;; fat finger binding and references to temp variable x
7081d4f9
AW
2570 (build-call s
2571 (build-simple-lambda no-source (list 'tmp) #f (list x) '()
2572 (gen-syntax-case (build-lexical-reference 'value no-source
2573 'tmp x)
2574 #'(key ...) #'(m ...)
2575 r
2576 mod))
b2208d2e 2577 (list (expand #'val r empty-wrap mod))))
8fad25c2
AW
2578 (syntax-violation 'syntax-case "invalid literals list" e))))))))
2579
78a47455 2580 ;; The portable macroexpand seeds expand-top's mode m with 'e (for
8fad25c2
AW
2581 ;; evaluating) and esew (which stands for "eval syntax expanders
2582 ;; when") with '(eval). In Chez Scheme, m is set to 'c instead of e
2583 ;; if we are compiling a file, and esew is set to
2584 ;; (eval-syntactic-expanders-when), which defaults to the list
2585 ;; '(compile load eval). This means that, by default, top-level
2586 ;; syntactic definitions are evaluated immediately after they are
2587 ;; expanded, and the expanded definitions are also residualized into
2588 ;; the object file if we are compiling a file.
2589 (set! macroexpand
2590 (lambda* (x #:optional (m 'e) (esew '(eval)))
78a47455
AW
2591 (expand-top-sequence (list x) null-env top-wrap #f m esew
2592 (cons 'hygiene (module-name (current-module))))))
8fad25c2
AW
2593
2594 (set! identifier?
2595 (lambda (x)
2596 (nonsymbol-id? x)))
2597
2598 (set! datum->syntax
2599 (lambda (id datum)
2600 (make-syntax-object datum (syntax-object-wrap id)
2601 (syntax-object-module id))))
2602
2603 (set! syntax->datum
2604 ;; accepts any object, since syntax objects may consist partially
2605 ;; or entirely of unwrapped, nonsymbolic data
2606 (lambda (x)
2607 (strip x empty-wrap)))
2608
2609 (set! syntax-source
2610 (lambda (x) (source-annotation x)))
2611
2612 (set! generate-temporaries
2613 (lambda (ls)
2614 (arg-check list? ls 'generate-temporaries)
933c6eb7 2615 (let ((mod (cons 'hygiene (module-name (current-module)))))
f9685f43 2616 (map (lambda (x) (wrap (gensym "t-") top-wrap mod)) ls))))
8fad25c2
AW
2617
2618 (set! free-identifier=?
2619 (lambda (x y)
2620 (arg-check nonsymbol-id? x 'free-identifier=?)
2621 (arg-check nonsymbol-id? y 'free-identifier=?)
2622 (free-id=? x y)))
2623
2624 (set! bound-identifier=?
2625 (lambda (x y)
2626 (arg-check nonsymbol-id? x 'bound-identifier=?)
2627 (arg-check nonsymbol-id? y 'bound-identifier=?)
2628 (bound-id=? x y)))
2629
2630 (set! syntax-violation
8f1870f2 2631 (lambda* (who message form #:optional subform)
8fad25c2
AW
2632 (arg-check (lambda (x) (or (not x) (string? x) (symbol? x)))
2633 who 'syntax-violation)
2634 (arg-check string? message 'syntax-violation)
8f1870f2 2635 (throw 'syntax-error who message
bbd1281a
MW
2636 (or (source-annotation subform)
2637 (source-annotation form))
8f1870f2
AW
2638 (strip form empty-wrap)
2639 (and subform (strip subform empty-wrap)))))
8fad25c2 2640
68fcf711
AW
2641 (let ()
2642 (define (syntax-module id)
2643 (arg-check nonsymbol-id? id 'syntax-module)
2644 (cdr (syntax-object-module id)))
2645
2646 (define (syntax-local-binding id)
2647 (arg-check nonsymbol-id? id 'syntax-local-binding)
2648 (with-transformer-environment
2649 (lambda (e r w s rib mod)
2650 (define (strip-anti-mark w)
2651 (let ((ms (wrap-marks w)) (s (wrap-subst w)))
2652 (if (and (pair? ms) (eq? (car ms) the-anti-mark))
2653 ;; output is from original text
2654 (make-wrap (cdr ms) (if rib (cons rib (cdr s)) (cdr s)))
2655 ;; output introduced by macro
2656 (make-wrap ms (if rib (cons rib s) s)))))
2657 (call-with-values (lambda ()
2658 (resolve-identifier
2659 (syntax-object-expression id)
2660 (strip-anti-mark (syntax-object-wrap id))
2661 r
dfadcf85
AW
2662 (syntax-object-module id)
2663 ;; FIXME: come up with a better policy for
2664 ;; resolve-syntax-parameters
2665 #t))
68fcf711
AW
2666 (lambda (type value mod)
2667 (case type
2668 ((lexical) (values 'lexical value))
2669 ((macro) (values 'macro value))
2670 ((syntax) (values 'pattern-variable value))
2671 ((displaced-lexical) (values 'displaced-lexical #f))
2672 ((global) (values 'global (cons value (cdr mod))))
2673 (else (values 'other #f))))))))
2674
2675 (define (syntax-locally-bound-identifiers id)
2676 (arg-check nonsymbol-id? id 'syntax-locally-bound-identifiers)
2677 (locally-bound-identifiers (syntax-object-wrap id)
2678 (syntax-object-module id)))
2679
2680 ;; Using define! instead of set! to avoid warnings at
2681 ;; compile-time, after the variables are stolen away into (system
2682 ;; syntax). See the end of boot-9.scm.
2683 ;;
2684 (define! 'syntax-module syntax-module)
2685 (define! 'syntax-local-binding syntax-local-binding)
2686 (define! 'syntax-locally-bound-identifiers syntax-locally-bound-identifiers))
2687
8fad25c2
AW
2688 ;; $sc-dispatch expects an expression and a pattern. If the expression
2689 ;; matches the pattern a list of the matching expressions for each
2690 ;; "any" is returned. Otherwise, #f is returned. (This use of #f will
2691 ;; not work on r4rs implementations that violate the ieee requirement
2692 ;; that #f and () be distinct.)
2693
2694 ;; The expression is matched with the pattern as follows:
2695
2696 ;; pattern: matches:
2697 ;; () empty list
2698 ;; any anything
2699 ;; (<pattern>1 . <pattern>2) (<pattern>1 . <pattern>2)
2700 ;; each-any (any*)
2701 ;; #(free-id <key>) <key> with free-identifier=?
2702 ;; #(each <pattern>) (<pattern>*)
2703 ;; #(each+ p1 (p2_1 ... p2_n) p3) (p1* (p2_n ... p2_1) . p3)
2704 ;; #(vector <pattern>) (list->vector <pattern>)
2705 ;; #(atom <object>) <object> with "equal?"
2706
2707 ;; Vector cops out to pair under assumption that vectors are rare. If
2708 ;; not, should convert to:
2709 ;; #(vector <pattern>*) #(<pattern>*)
2710
2711 (let ()
2712
2713 (define match-each
2714 (lambda (e p w mod)
aa3819aa
AR
2715 (cond
2716 ((pair? e)
8fad25c2
AW
2717 (let ((first (match (car e) p w '() mod)))
2718 (and first
2719 (let ((rest (match-each (cdr e) p w mod)))
2720 (and rest (cons first rest))))))
2721 ((null? e) '())
aa3819aa 2722 ((syntax-object? e)
8fad25c2
AW
2723 (match-each (syntax-object-expression e)
2724 p
2725 (join-wraps w (syntax-object-wrap e))
2726 (syntax-object-module e)))
2727 (else #f))))
2728
2729 (define match-each+
2730 (lambda (e x-pat y-pat z-pat w r mod)
2731 (let f ((e e) (w w))
c3ae0ed4 2732 (cond
8fad25c2
AW
2733 ((pair? e)
2734 (call-with-values (lambda () (f (cdr e) w))
2735 (lambda (xr* y-pat r)
2736 (if r
2737 (if (null? y-pat)
2738 (let ((xr (match (car e) x-pat w '() mod)))
2739 (if xr
2740 (values (cons xr xr*) y-pat r)
2741 (values #f #f #f)))
2742 (values
2743 '()
2744 (cdr y-pat)
2745 (match (car e) (car y-pat) w r mod)))
2746 (values #f #f #f)))))
c3ae0ed4 2747 ((syntax-object? e)
8fad25c2
AW
2748 (f (syntax-object-expression e) (join-wraps w e)))
2749 (else
2750 (values '() y-pat (match e z-pat w r mod)))))))
2751
2752 (define match-each-any
2753 (lambda (e w mod)
2754 (cond
2755 ((pair? e)
2756 (let ((l (match-each-any (cdr e) w mod)))
2757 (and l (cons (wrap (car e) w mod) l))))
2758 ((null? e) '())
2759 ((syntax-object? e)
2760 (match-each-any (syntax-object-expression e)
2761 (join-wraps w (syntax-object-wrap e))
2762 mod))
2763 (else #f))))
2764
2765 (define match-empty
2766 (lambda (p r)
2767 (cond
2768 ((null? p) r)
2769 ((eq? p '_) r)
2770 ((eq? p 'any) (cons '() r))
2771 ((pair? p) (match-empty (car p) (match-empty (cdr p) r)))
2772 ((eq? p 'each-any) (cons '() r))
2773 (else
2774 (case (vector-ref p 0)
2775 ((each) (match-empty (vector-ref p 1) r))
2776 ((each+) (match-empty (vector-ref p 1)
2777 (match-empty
2778 (reverse (vector-ref p 2))
2779 (match-empty (vector-ref p 3) r))))
2780 ((free-id atom) r)
2781 ((vector) (match-empty (vector-ref p 1) r)))))))
2782
2783 (define combine
2784 (lambda (r* r)
2785 (if (null? (car r*))
2786 r
2787 (cons (map car r*) (combine (map cdr r*) r)))))
2788
2789 (define match*
2790 (lambda (e p w r mod)
2791 (cond
2792 ((null? p) (and (null? e) r))
2793 ((pair? p)
2794 (and (pair? e) (match (car e) (car p) w
2795 (match (cdr e) (cdr p) w r mod)
2796 mod)))
2797 ((eq? p 'each-any)
2798 (let ((l (match-each-any e w mod))) (and l (cons l r))))
2799 (else
2800 (case (vector-ref p 0)
2801 ((each)
2802 (if (null? e)
2803 (match-empty (vector-ref p 1) r)
2804 (let ((l (match-each e (vector-ref p 1) w mod)))
2805 (and l
2806 (let collect ((l l))
2807 (if (null? (car l))
2808 r
2809 (cons (map car l) (collect (map cdr l)))))))))
2810 ((each+)
2811 (call-with-values
2812 (lambda ()
2813 (match-each+ e (vector-ref p 1) (vector-ref p 2) (vector-ref p 3) w r mod))
2814 (lambda (xr* y-pat r)
2815 (and r
2816 (null? y-pat)
2817 (if (null? xr*)
2818 (match-empty (vector-ref p 1) r)
2819 (combine xr* r))))))
2820 ((free-id) (and (id? e) (free-id=? (wrap e w mod) (vector-ref p 1)) r))
2821 ((atom) (and (equal? (vector-ref p 1) (strip e w)) r))
2822 ((vector)
2823 (and (vector? e)
2824 (match (vector->list e) (vector-ref p 1) w r mod))))))))
2825
2826 (define match
2827 (lambda (e p w r mod)
2828 (cond
2829 ((not r) #f)
2830 ((eq? p '_) r)
2831 ((eq? p 'any) (cons (wrap e w mod) r))
2832 ((syntax-object? e)
2833 (match*
2834 (syntax-object-expression e)
2835 p
2836 (join-wraps w (syntax-object-wrap e))
2837 r
2838 (syntax-object-module e)))
2839 (else (match* e p w r mod)))))
2840
2841 (set! $sc-dispatch
2842 (lambda (e p)
2843 (cond
2844 ((eq? p 'any) (list e))
2845 ((eq? p '_) '())
2846 ((syntax-object? e)
2847 (match* (syntax-object-expression e)
2848 p (syntax-object-wrap e) '() (syntax-object-module e)))
2849 (else (match* e p empty-wrap '() #f))))))))
80f225df 2850
a63812a2
JB
2851
2852(define-syntax with-syntax
2853 (lambda (x)
2854 (syntax-case x ()
2855 ((_ () e1 e2 ...)
f929b9e5 2856 #'(let () e1 e2 ...))
a63812a2 2857 ((_ ((out in)) e1 e2 ...)
f929b9e5
AW
2858 #'(syntax-case in ()
2859 (out (let () e1 e2 ...))))
a63812a2 2860 ((_ ((out in) ...) e1 e2 ...)
c3ae0ed4 2861 #'(syntax-case (list in ...) ()
f929b9e5 2862 ((out ...) (let () e1 e2 ...)))))))
a63812a2
JB
2863
2864(define-syntax syntax-rules
1af6d2a7
MW
2865 (lambda (xx)
2866 (syntax-case xx ()
a63812a2 2867 ((_ (k ...) ((keyword . pattern) template) ...)
c3ae0ed4 2868 #'(lambda (x)
a5e95abe 2869 ;; embed patterns as procedure metadata
44d65b23
AW
2870 #((macro-type . syntax-rules)
2871 (patterns pattern ...))
2872 (syntax-case x (k ...)
f698b728 2873 ((_ . pattern) #'template)
44d65b23
AW
2874 ...)))
2875 ((_ (k ...) docstring ((keyword . pattern) template) ...)
2876 (string? (syntax->datum #'docstring))
2877 #'(lambda (x)
2878 ;; the same, but allow a docstring
2879 docstring
a5e95abe
AW
2880 #((macro-type . syntax-rules)
2881 (patterns pattern ...))
c3ae0ed4 2882 (syntax-case x (k ...)
f698b728 2883 ((_ . pattern) #'template)
c3ae0ed4 2884 ...))))))
a63812a2 2885
dea14eb9
AW
2886(define-syntax define-syntax-rule
2887 (lambda (x)
2888 (syntax-case x ()
2889 ((_ (name . pattern) template)
2890 #'(define-syntax name
2891 (syntax-rules ()
2892 ((_ . pattern) template))))
2893 ((_ (name . pattern) docstring template)
2894 (string? (syntax->datum #'docstring))
2895 #'(define-syntax name
2896 (syntax-rules ()
2897 docstring
2898 ((_ . pattern) template)))))))
2899
a63812a2
JB
2900(define-syntax let*
2901 (lambda (x)
2902 (syntax-case x ()
2903 ((let* ((x v) ...) e1 e2 ...)
c3ae0ed4
AW
2904 (and-map identifier? #'(x ...))
2905 (let f ((bindings #'((x v) ...)))
a63812a2 2906 (if (null? bindings)
c3ae0ed4 2907 #'(let () e1 e2 ...)
a63812a2
JB
2908 (with-syntax ((body (f (cdr bindings)))
2909 (binding (car bindings)))
c3ae0ed4 2910 #'(let (binding) body))))))))
a63812a2 2911
a63812a2 2912(define-syntax quasiquote
0f550375
AW
2913 (let ()
2914 (define (quasi p lev)
2915 (syntax-case p (unquote quasiquote)
2916 ((unquote p)
2917 (if (= lev 0)
2918 #'("value" p)
2919 (quasicons #'("quote" unquote) (quasi #'(p) (- lev 1)))))
2920 ((quasiquote p) (quasicons #'("quote" quasiquote) (quasi #'(p) (+ lev 1))))
2921 ((p . q)
2922 (syntax-case #'p (unquote unquote-splicing)
2923 ((unquote p ...)
2924 (if (= lev 0)
2925 (quasilist* #'(("value" p) ...) (quasi #'q lev))
2926 (quasicons
2927 (quasicons #'("quote" unquote) (quasi #'(p ...) (- lev 1)))
2928 (quasi #'q lev))))
2929 ((unquote-splicing p ...)
2930 (if (= lev 0)
2931 (quasiappend #'(("value" p) ...) (quasi #'q lev))
2932 (quasicons
2933 (quasicons #'("quote" unquote-splicing) (quasi #'(p ...) (- lev 1)))
2934 (quasi #'q lev))))
2935 (_ (quasicons (quasi #'p lev) (quasi #'q lev)))))
2936 (#(x ...) (quasivector (vquasi #'(x ...) lev)))
2937 (p #'("quote" p))))
2938 (define (vquasi p lev)
2939 (syntax-case p ()
2940 ((p . q)
2941 (syntax-case #'p (unquote unquote-splicing)
2942 ((unquote p ...)
2943 (if (= lev 0)
2944 (quasilist* #'(("value" p) ...) (vquasi #'q lev))
2945 (quasicons
2946 (quasicons #'("quote" unquote) (quasi #'(p ...) (- lev 1)))
2947 (vquasi #'q lev))))
2948 ((unquote-splicing p ...)
2949 (if (= lev 0)
2950 (quasiappend #'(("value" p) ...) (vquasi #'q lev))
2951 (quasicons
2952 (quasicons
2953 #'("quote" unquote-splicing)
2954 (quasi #'(p ...) (- lev 1)))
2955 (vquasi #'q lev))))
2956 (_ (quasicons (quasi #'p lev) (vquasi #'q lev)))))
2957 (() #'("quote" ()))))
2958 (define (quasicons x y)
2959 (with-syntax ((x x) (y y))
2960 (syntax-case #'y ()
2961 (("quote" dy)
2962 (syntax-case #'x ()
2963 (("quote" dx) #'("quote" (dx . dy)))
2964 (_ (if (null? #'dy) #'("list" x) #'("list*" x y)))))
2965 (("list" . stuff) #'("list" x . stuff))
2966 (("list*" . stuff) #'("list*" x . stuff))
2967 (_ #'("list*" x y)))))
2968 (define (quasiappend x y)
2969 (syntax-case y ()
2970 (("quote" ())
2971 (cond
2972 ((null? x) #'("quote" ()))
2973 ((null? (cdr x)) (car x))
2974 (else (with-syntax (((p ...) x)) #'("append" p ...)))))
2975 (_
2976 (cond
2977 ((null? x) y)
2978 (else (with-syntax (((p ...) x) (y y)) #'("append" p ... y)))))))
2979 (define (quasilist* x y)
2980 (let f ((x x))
2981 (if (null? x)
2982 y
2983 (quasicons (car x) (f (cdr x))))))
2984 (define (quasivector x)
2985 (syntax-case x ()
2986 (("quote" (x ...)) #'("quote" #(x ...)))
2987 (_
2988 (let f ((y x) (k (lambda (ls) #`("vector" #,@ls))))
2989 (syntax-case y ()
2990 (("quote" (y ...)) (k #'(("quote" y) ...)))
2991 (("list" y ...) (k #'(y ...)))
2992 (("list*" y ... z) (f #'z (lambda (ls) (k (append #'(y ...) ls)))))
2993 (else #`("list->vector" #,x)))))))
2994 (define (emit x)
2995 (syntax-case x ()
2996 (("quote" x) #''x)
2997 (("list" x ...) #`(list #,@(map emit #'(x ...))))
2998 ;; could emit list* for 3+ arguments if implementation supports
2999 ;; list*
3000 (("list*" x ... y)
3001 (let f ((x* #'(x ...)))
3002 (if (null? x*)
3003 (emit #'y)
3004 #`(cons #,(emit (car x*)) #,(f (cdr x*))))))
3005 (("append" x ...) #`(append #,@(map emit #'(x ...))))
3006 (("vector" x ...) #`(vector #,@(map emit #'(x ...))))
3007 (("list->vector" x) #`(list->vector #,(emit #'x)))
3008 (("value" x) #'x)))
a63812a2 3009 (lambda (x)
0f550375
AW
3010 (syntax-case x ()
3011 ;; convert to intermediate language, combining introduced (but
3012 ;; not unquoted source) quote expressions where possible and
3013 ;; choosing optimal construction code otherwise, then emit
3014 ;; Scheme code corresponding to the intermediate language forms.
3015 ((_ e) (emit (quasi #'e 0)))))))
a63812a2
JB
3016
3017(define-syntax include
3018 (lambda (x)
3019 (define read-file
3020 (lambda (fn k)
3021 (let ((p (open-input-file fn)))
df0f5295
LC
3022 (let f ((x (read p))
3023 (result '()))
a63812a2 3024 (if (eof-object? x)
df0f5295
LC
3025 (begin
3026 (close-input-port p)
3027 (reverse result))
3028 (f (read p)
3029 (cons (datum->syntax k x) result)))))))
a63812a2
JB
3030 (syntax-case x ()
3031 ((k filename)
c3ae0ed4 3032 (let ((fn (syntax->datum #'filename)))
9846796b 3033 (with-syntax (((exp ...) (read-file fn #'filename)))
c3ae0ed4 3034 #'(begin exp ...)))))))
a63812a2 3035
d89fae24
AW
3036(define-syntax include-from-path
3037 (lambda (x)
3038 (syntax-case x ()
3039 ((k filename)
3040 (let ((fn (syntax->datum #'filename)))
9846796b
AW
3041 (with-syntax ((fn (datum->syntax
3042 #'filename
3043 (or (%search-load-path fn)
3044 (syntax-violation 'include-from-path
3045 "file not found in path"
3046 x #'filename)))))
d89fae24
AW
3047 #'(include fn)))))))
3048
a63812a2 3049(define-syntax unquote
6a952e0e 3050 (lambda (x)
0f550375
AW
3051 (syntax-violation 'unquote
3052 "expression not valid outside of quasiquote"
3053 x)))
a63812a2
JB
3054
3055(define-syntax unquote-splicing
6a952e0e 3056 (lambda (x)
0f550375
AW
3057 (syntax-violation 'unquote-splicing
3058 "expression not valid outside of quasiquote"
3059 x)))
a63812a2 3060
bfccdcd5
AW
3061(define (make-variable-transformer proc)
3062 (if (procedure? proc)
3063 (let ((trans (lambda (x)
3064 #((macro-type . variable-transformer))
3065 (proc x))))
3066 (set-procedure-property! trans 'variable-transformer #t)
3067 trans)
3068 (error "variable transformer not a procedure" proc)))
3069
a63812a2 3070(define-syntax identifier-syntax
1af6d2a7
MW
3071 (lambda (xx)
3072 (syntax-case xx (set!)
a63812a2 3073 ((_ e)
c3ae0ed4 3074 #'(lambda (x)
a5e95abe 3075 #((macro-type . identifier-syntax))
a63812a2
JB
3076 (syntax-case x ()
3077 (id
c3ae0ed4
AW
3078 (identifier? #'id)
3079 #'e)
a63812a2 3080 ((_ x (... ...))
bfccdcd5
AW
3081 #'(e x (... ...))))))
3082 ((_ (id exp1) ((set! var val) exp2))
3083 (and (identifier? #'id) (identifier? #'var))
3084 #'(make-variable-transformer
3085 (lambda (x)
3086 #((macro-type . variable-transformer))
3087 (syntax-case x (set!)
3088 ((set! var val) #'exp2)
3089 ((id x (... ...)) #'(exp1 x (... ...)))
3090 (id (identifier? #'id) #'exp1))))))))
97bc28b6
AW
3091
3092(define-syntax define*
64fa96ef
AW
3093 (lambda (x)
3094 (syntax-case x ()
3095 ((_ (id . args) b0 b1 ...)
3096 #'(define id (lambda* args b0 b1 ...)))
9120f130 3097 ((_ id val) (identifier? #'id)
64fa96ef 3098 #'(define id val)))))