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