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