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