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