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