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