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