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