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