psyntax: cite the paper that psyntax is based on.
[bpt/guile.git] / module / ice-9 / psyntax.scm
CommitLineData
677cd590
RB
1;;;; -*-scheme-*-
2;;;;
f78a1cce 3;;;; Copyright (C) 2001, 2003, 2006, 2009, 2010, 2011,
84f5a825 4;;;; 2012, 2013 Free Software Foundation, Inc.
f78a1cce 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
112fc7c2
MW
46;;; This code is based on "Syntax Abstraction in Scheme"
47;;; by R. Kent Dybvig, Robert Hieb, and Carl Bruggeman.
48;;; Lisp and Symbolic Computation 5:4, 295-326, 1992.
49;;; <http://www.cs.indiana.edu/~dyb/pubs/LaSC-5-4-pp295-326.pdf>
50
51
8a73a6d2 52;;; This file defines the syntax-case expander, macroexpand, and a set
a63812a2
JB
53;;; of associated syntactic forms and procedures. Of these, the
54;;; following are documented in The Scheme Programming Language,
565c8e30
AW
55;;; Fourth Edition (R. Kent Dybvig, MIT Press, 2009), and in the
56;;; R6RS:
a63812a2
JB
57;;;
58;;; bound-identifier=?
22225fc1 59;;; datum->syntax
a63812a2 60;;; define-syntax
449bf60b 61;;; syntax-parameterize
a63812a2
JB
62;;; free-identifier=?
63;;; generate-temporaries
64;;; identifier?
65;;; identifier-syntax
66;;; let-syntax
67;;; letrec-syntax
68;;; syntax
69;;; syntax-case
22225fc1 70;;; syntax->datum
a63812a2
JB
71;;; syntax-rules
72;;; with-syntax
73;;;
565c8e30
AW
74;;; Additionally, the expander provides definitions for a number of core
75;;; Scheme syntactic bindings, such as `let', `lambda', and the like.
a63812a2
JB
76
77;;; The remaining exports are listed below:
78;;;
8a73a6d2
AW
79;;; (macroexpand datum)
80;;; if datum represents a valid expression, macroexpand returns an
a63812a2
JB
81;;; expanded version of datum in a core language that includes no
82;;; syntactic abstractions. The core language includes begin,
83;;; define, if, lambda, letrec, quote, and set!.
84;;; (eval-when situations expr ...)
85;;; conditionally evaluates expr ... at compile-time or run-time
86;;; depending upon situations (see the Chez Scheme System Manual,
87;;; Revision 3, for a complete description)
e4721dde 88;;; (syntax-violation who message form [subform])
a63812a2 89;;; used to report errors found during expansion
5f1a2fb1 90;;; ($sc-dispatch e p)
a63812a2
JB
91;;; used by expanded code to handle syntax-case matching
92
565c8e30
AW
93;;; This file is shipped along with an expanded version of itself,
94;;; psyntax-pp.scm, which is loaded when psyntax.scm has not yet been
95;;; compiled. In this way, psyntax bootstraps off of an expanded
96;;; version of itself.
97
98;;; This implementation of the expander sometimes uses syntactic
99;;; abstractions when procedural abstractions would suffice. For
100;;; example, we define top-wrap and top-marked? as
a63812a2 101;;;
a63812a2
JB
102;;; (define-syntax top-wrap (identifier-syntax '((top))))
103;;; (define-syntax top-marked?
104;;; (syntax-rules ()
105;;; ((_ w) (memq 'top (wrap-marks w)))))
565c8e30 106;;;
a63812a2 107;;; rather than
565c8e30 108;;;
a63812a2
JB
109;;; (define top-wrap '((top)))
110;;; (define top-marked?
111;;; (lambda (w) (memq 'top (wrap-marks w))))
565c8e30
AW
112;;;
113;;; On the other hand, we don't do this consistently; we define
114;;; make-wrap, wrap-marks, and wrap-subst simply as
115;;;
a63812a2
JB
116;;; (define make-wrap cons)
117;;; (define wrap-marks car)
118;;; (define wrap-subst cdr)
565c8e30 119;;;
a63812a2
JB
120;;; In Chez Scheme, the syntactic and procedural forms of these
121;;; abstractions are equivalent, since the optimizer consistently
565c8e30
AW
122;;; integrates constants and small procedures. This will be true of
123;;; Guile as well, once we implement a proper inliner.
a63812a2 124
a63812a2 125
565c8e30 126;;; Implementation notes:
a63812a2
JB
127
128;;; Objects with no standard print syntax, including objects containing
129;;; cycles and syntax object, are allowed in quoted data as long as they
22225fc1 130;;; are contained within a syntax form or produced by datum->syntax.
a63812a2
JB
131;;; Such objects are never copied.
132
133;;; All identifiers that don't have macro definitions and are not bound
565c8e30 134;;; lexically are assumed to be global variables.
a63812a2
JB
135
136;;; Top-level definitions of macro-introduced identifiers are allowed.
137;;; This may not be appropriate for implementations in which the
138;;; model is that bindings are created by definitions, as opposed to
139;;; one in which initial values are assigned by definitions.
140
a63812a2 141;;; Identifiers and syntax objects are implemented as vectors for
565c8e30 142;;; portability. As a result, it is possible to "forge" syntax objects.
a63812a2 143
565c8e30
AW
144;;; The implementation of generate-temporaries assumes that it is
145;;; possible to generate globally unique symbols (gensyms).
a63812a2 146
565c8e30
AW
147;;; The source location associated with incoming expressions is tracked
148;;; via the source-properties mechanism, a weak map from expression to
149;;; source information. At times the source is separated from the
150;;; expression; see the note below about "efficiency and confusion".
a96434cc 151
a63812a2
JB
152
153;;; Bootstrapping:
154
155;;; When changing syntax-object representations, it is necessary to support
156;;; both old and new syntax-object representations in id-var-name. It
157;;; should be sufficient to recognize old representations and treat
158;;; them as not lexically bound.
159
160
161
9c35c579
AW
162(eval-when (compile)
163 (set-current-module (resolve-module '(guile))))
164
a63812a2 165(let ()
8fad25c2
AW
166 (define-syntax define-expansion-constructors
167 (lambda (x)
168 (syntax-case x ()
169 ((_)
170 (let lp ((n 0) (out '()))
171 (if (< n (vector-length %expanded-vtables))
172 (lp (1+ n)
173 (let* ((vtable (vector-ref %expanded-vtables n))
174 (stem (struct-ref vtable (+ vtable-offset-user 0)))
175 (fields (struct-ref vtable (+ vtable-offset-user 2)))
176 (sfields (map (lambda (f) (datum->syntax x f)) fields))
177 (ctor (datum->syntax x (symbol-append 'make- stem))))
178 (cons #`(define (#,ctor #,@sfields)
179 (make-struct (vector-ref %expanded-vtables #,n) 0
180 #,@sfields))
181 out)))
182 #`(begin #,@(reverse out))))))))
183
184 (define-syntax define-expansion-accessors
185 (lambda (x)
186 (syntax-case x ()
187 ((_ stem field ...)
188 (let lp ((n 0))
189 (let ((vtable (vector-ref %expanded-vtables n))
190 (stem (syntax->datum #'stem)))
191 (if (eq? (struct-ref vtable (+ vtable-offset-user 0)) stem)
192 #`(begin
193 (define (#,(datum->syntax x (symbol-append stem '?)) x)
194 (and (struct? x)
195 (eq? (struct-vtable x)
196 (vector-ref %expanded-vtables #,n))))
197 #,@(map
198 (lambda (f)
199 (let ((get (datum->syntax x (symbol-append stem '- f)))
200 (set (datum->syntax x (symbol-append 'set- stem '- f '!)))
201 (idx (list-index (struct-ref vtable
202 (+ vtable-offset-user 2))
203 f)))
204 #`(begin
205 (define (#,get x)
206 (struct-ref x #,idx))
207 (define (#,set x v)
208 (struct-set! x #,idx v)))))
209 (syntax->datum #'(field ...))))
210 (lp (1+ n)))))))))
211
212 (define-syntax define-structure
213 (lambda (x)
214 (define construct-name
215 (lambda (template-identifier . args)
216 (datum->syntax
217 template-identifier
218 (string->symbol
a63812a2
JB
219 (apply string-append
220 (map (lambda (x)
221 (if (string? x)
222 x
22225fc1 223 (symbol->string (syntax->datum x))))
a63812a2 224 args))))))
8fad25c2
AW
225 (syntax-case x ()
226 ((_ (name id1 ...))
227 (and-map identifier? #'(name id1 ...))
228 (with-syntax
229 ((constructor (construct-name #'name "make-" #'name))
230 (predicate (construct-name #'name #'name "?"))
231 ((access ...)
232 (map (lambda (x) (construct-name x #'name "-" x))
233 #'(id1 ...)))
234 ((assign ...)
235 (map (lambda (x)
236 (construct-name x "set-" #'name "-" x "!"))
237 #'(id1 ...)))
238 (structure-length
239 (+ (length #'(id1 ...)) 1))
240 ((index ...)
241 (let f ((i 1) (ids #'(id1 ...)))
242 (if (null? ids)
243 '()
244 (cons i (f (+ i 1) (cdr ids)))))))
245 #'(begin
246 (define constructor
247 (lambda (id1 ...)
248 (vector 'name id1 ... )))
249 (define predicate
250 (lambda (x)
251 (and (vector? x)
252 (= (vector-length x) structure-length)
253 (eq? (vector-ref x 0) 'name))))
254 (define access
255 (lambda (x)
256 (vector-ref x index)))
257 ...
258 (define assign
259 (lambda (x update)
260 (vector-set! x index update)))
261 ...))))))
a63812a2 262
8fad25c2
AW
263 (let ()
264 (define-expansion-constructors)
265 (define-expansion-accessors lambda meta)
266
267 ;; hooks to nonportable run-time helpers
268 (begin
d8f699e8
AW
269 (define-syntax fx+ (identifier-syntax +))
270 (define-syntax fx- (identifier-syntax -))
271 (define-syntax fx= (identifier-syntax =))
272 (define-syntax fx< (identifier-syntax <))
8fad25c2
AW
273
274 (define top-level-eval-hook
275 (lambda (x mod)
276 (primitive-eval x)))
277
278 (define local-eval-hook
279 (lambda (x mod)
280 (primitive-eval x)))
bdf7759c 281
f9685f43
AW
282 ;; Capture syntax-session-id before we shove it off into a module.
283 (define session-id
284 (let ((v (module-variable (current-module) 'syntax-session-id)))
285 (lambda ()
286 ((variable-ref v)))))
c3ae0ed4 287
8fad25c2
AW
288 (define put-global-definition-hook
289 (lambda (symbol type val)
290 (module-define! (current-module)
291 symbol
292 (make-syntax-transformer symbol type val))))
e809758a 293
8fad25c2
AW
294 (define get-global-definition-hook
295 (lambda (symbol module)
296 (if (and (not module) (current-module))
297 (warn "module system is booted, we should have a module" symbol))
298 (let ((v (module-variable (if module
299 (resolve-module (cdr module))
300 (current-module))
301 symbol)))
302 (and v (variable-bound? v)
303 (let ((val (variable-ref v)))
304 (and (macro? val) (macro-type val)
305 (cons (macro-type val)
306 (macro-binding val)))))))))
307
308
309 (define (decorate-source e s)
32fbc38f 310 (if (and s (supports-source-properties? e))
8fad25c2
AW
311 (set-source-properties! e s))
312 e)
313
314 (define (maybe-name-value! name val)
315 (if (lambda? val)
316 (let ((meta (lambda-meta val)))
317 (if (not (assq 'name meta))
318 (set-lambda-meta! val (acons 'name name meta))))))
319
320 ;; output constructors
321 (define build-void
322 (lambda (source)
323 (make-void source)))
324
325 (define build-application
326 (lambda (source fun-exp arg-exps)
327 (make-application source fun-exp arg-exps)))
bdf7759c 328
8fad25c2
AW
329 (define build-conditional
330 (lambda (source test-exp then-exp else-exp)
331 (make-conditional source test-exp then-exp else-exp)))
bdf7759c 332
8fad25c2
AW
333 (define build-dynlet
334 (lambda (source fluids vals body)
335 (make-dynlet source fluids vals body)))
bdf7759c 336
8fad25c2
AW
337 (define build-lexical-reference
338 (lambda (type source name var)
339 (make-lexical-ref source name var)))
bdf7759c 340
8fad25c2
AW
341 (define build-lexical-assignment
342 (lambda (source name var exp)
343 (maybe-name-value! name exp)
344 (make-lexical-set source name var exp)))
bdf7759c 345
8fad25c2
AW
346 (define (analyze-variable mod var modref-cont bare-cont)
347 (if (not mod)
348 (bare-cont var)
349 (let ((kind (car mod))
350 (mod (cdr mod)))
351 (case kind
352 ((public) (modref-cont mod var #t))
353 ((private) (if (not (equal? mod (module-name (current-module))))
354 (modref-cont mod var #f)
355 (bare-cont var)))
356 ((bare) (bare-cont var))
357 ((hygiene) (if (and (not (equal? mod (module-name (current-module))))
358 (module-variable (resolve-module mod) var))
359 (modref-cont mod var #f)
360 (bare-cont var)))
361 (else (syntax-violation #f "bad module kind" var mod))))))
362
363 (define build-global-reference
364 (lambda (source var mod)
365 (analyze-variable
366 mod var
367 (lambda (mod var public?)
368 (make-module-ref source mod var public?))
369 (lambda (var)
370 (make-toplevel-ref source var)))))
371
372 (define build-global-assignment
373 (lambda (source var exp mod)
374 (maybe-name-value! var exp)
375 (analyze-variable
376 mod var
377 (lambda (mod var public?)
378 (make-module-set source mod var public? exp))
379 (lambda (var)
380 (make-toplevel-set source var exp)))))
381
382 (define build-global-definition
383 (lambda (source var exp)
384 (maybe-name-value! var exp)
385 (make-toplevel-define source var exp)))
386
387 (define build-simple-lambda
388 (lambda (src req rest vars meta exp)
389 (make-lambda src
390 meta
391 ;; hah, a case in which kwargs would be nice.
392 (make-lambda-case
393 ;; src req opt rest kw inits vars body else
394 src req #f rest #f '() vars exp #f))))
395
396 (define build-case-lambda
397 (lambda (src meta body)
398 (make-lambda src meta body)))
399
400 (define build-lambda-case
401 ;; req := (name ...)
402 ;; opt := (name ...) | #f
403 ;; rest := name | #f
404 ;; kw := (allow-other-keys? (keyword name var) ...) | #f
405 ;; inits: (init ...)
406 ;; vars: (sym ...)
407 ;; vars map to named arguments in the following order:
408 ;; required, optional (positional), rest, keyword.
409 ;; the body of a lambda: anything, already expanded
410 ;; else: lambda-case | #f
411 (lambda (src req opt rest kw inits vars body else-case)
412 (make-lambda-case src req opt rest kw inits vars body else-case)))
413
414 (define build-primref
415 (lambda (src name)
416 (if (equal? (module-name (current-module)) '(guile))
417 (make-toplevel-ref src name)
418 (make-module-ref src '(guile) name #f))))
419
420 (define (build-data src exp)
421 (make-const src exp))
422
423 (define build-sequence
424 (lambda (src exps)
425 (if (null? (cdr exps))
426 (car exps)
427 (make-sequence src exps))))
428
429 (define build-let
430 (lambda (src ids vars val-exps body-exp)
431 (for-each maybe-name-value! ids val-exps)
432 (if (null? vars)
433 body-exp
434 (make-let src ids vars val-exps body-exp))))
435
436 (define build-named-let
437 (lambda (src ids vars val-exps body-exp)
438 (let ((f (car vars))
439 (f-name (car ids))
440 (vars (cdr vars))
441 (ids (cdr ids)))
442 (let ((proc (build-simple-lambda src ids #f vars '() body-exp)))
443 (maybe-name-value! f-name proc)
22cf27c8 444 (for-each maybe-name-value! ids val-exps)
8fad25c2
AW
445 (make-letrec
446 src #f
447 (list f-name) (list f) (list proc)
448 (build-application src (build-lexical-reference 'fun src f-name f)
449 val-exps))))))
450
451 (define build-letrec
452 (lambda (src in-order? ids vars val-exps body-exp)
453 (if (null? vars)
454 body-exp
455 (begin
456 (for-each maybe-name-value! ids val-exps)
457 (make-letrec src in-order? ids vars val-exps body-exp)))))
458
459
460 ;; FIXME: use a faster gensym
1bbe0a63 461 (define-syntax-rule (build-lexical-var src id)
f9685f43 462 (gensym (string-append (symbol->string id) "-")))
bdf7759c 463
8fad25c2 464 (define-structure (syntax-object expression wrap module))
a63812a2 465
8fad25c2 466 (define-syntax no-source (identifier-syntax #f))
a63812a2 467
8fad25c2
AW
468 (define source-annotation
469 (lambda (x)
32fbc38f
MW
470 (let ((props (source-properties
471 (if (syntax-object? x)
472 (syntax-object-expression x)
473 x))))
474 (and (pair? props) props))))
a63812a2 475
1bbe0a63
AW
476 (define-syntax-rule (arg-check pred? e who)
477 (let ((x e))
478 (if (not (pred? x)) (syntax-violation who "invalid argument" x))))
8fad25c2
AW
479
480 ;; compile-time environments
481
482 ;; wrap and environment comprise two level mapping.
483 ;; wrap : id --> label
484 ;; env : label --> <element>
485
486 ;; environments are represented in two parts: a lexical part and a global
487 ;; part. The lexical part is a simple list of associations from labels
488 ;; to bindings. The global part is implemented by
489 ;; {put,get}-global-definition-hook and associates symbols with
490 ;; bindings.
491
492 ;; global (assumed global variable) and displaced-lexical (see below)
493 ;; do not show up in any environment; instead, they are fabricated by
494 ;; lookup when it finds no other bindings.
495
496 ;; <environment> ::= ((<label> . <binding>)*)
497
498 ;; identifier bindings include a type and a value
499
500 ;; <binding> ::= (macro . <procedure>) macros
501 ;; (core . <procedure>) core forms
502 ;; (module-ref . <procedure>) @ or @@
503 ;; (begin) begin
504 ;; (define) define
505 ;; (define-syntax) define-syntax
286dc5e1 506 ;; (define-syntax-parameter) define-syntax-parameter
8fad25c2
AW
507 ;; (local-syntax . rec?) let-syntax/letrec-syntax
508 ;; (eval-when) eval-when
4c2e13e5 509 ;; (syntax . (<var> . <level>)) pattern variables
8fad25c2
AW
510 ;; (global) assumed global variable
511 ;; (lexical . <var>) lexical variables
512 ;; (displaced-lexical) displaced lexicals
513 ;; <level> ::= <nonnegative integer>
514 ;; <var> ::= variable returned by build-lexical-var
515
286dc5e1
AW
516 ;; a macro is a user-defined syntactic-form. a core is a
517 ;; system-defined syntactic form. begin, define, define-syntax,
518 ;; define-syntax-parameter, and eval-when are treated specially
519 ;; since they are sensitive to whether the form is at top-level and
520 ;; (except for eval-when) can denote valid internal definitions.
8fad25c2
AW
521
522 ;; a pattern variable is a variable introduced by syntax-case and can
523 ;; be referenced only within a syntax form.
524
525 ;; any identifier for which no top-level syntax definition or local
526 ;; binding of any kind has been seen is assumed to be a global
527 ;; variable.
528
529 ;; a lexical variable is a lambda- or letrec-bound variable.
530
531 ;; a displaced-lexical identifier is a lexical identifier removed from
532 ;; it's scope by the return of a syntax object containing the identifier.
533 ;; a displaced lexical can also appear when a letrec-syntax-bound
534 ;; keyword is referenced on the rhs of one of the letrec-syntax clauses.
535 ;; a displaced lexical should never occur with properly written macros.
536
537 (define-syntax make-binding
538 (syntax-rules (quote)
539 ((_ type value) (cons type value))
540 ((_ 'type) '(type))
541 ((_ type) (cons type '()))))
1bbe0a63
AW
542 (define-syntax-rule (binding-type x)
543 (car x))
544 (define-syntax-rule (binding-value x)
545 (cdr x))
8fad25c2
AW
546
547 (define-syntax null-env (identifier-syntax '()))
548
549 (define extend-env
550 (lambda (labels bindings r)
551 (if (null? labels)
552 r
553 (extend-env (cdr labels) (cdr bindings)
554 (cons (cons (car labels) (car bindings)) r)))))
c3ae0ed4 555
8fad25c2
AW
556 (define extend-var-env
557 ;; variant of extend-env that forms "lexical" binding
558 (lambda (labels vars r)
559 (if (null? labels)
560 r
561 (extend-var-env (cdr labels) (cdr vars)
562 (cons (cons (car labels) (make-binding 'lexical (car vars))) r)))))
563
564 ;; we use a "macros only" environment in expansion of local macro
565 ;; definitions so that their definitions can use local macros without
566 ;; attempting to use other lexical identifiers.
567 (define macros-only-env
568 (lambda (r)
569 (if (null? r)
570 '()
571 (let ((a (car r)))
572 (if (eq? (cadr a) 'macro)
573 (cons a (macros-only-env (cdr r)))
574 (macros-only-env (cdr r)))))))
575
576 (define lookup
577 ;; x may be a label or a symbol
578 ;; although symbols are usually global, we check the environment first
579 ;; anyway because a temporary binding may have been established by
580 ;; fluid-let-syntax
581 (lambda (x r mod)
582 (cond
583 ((assq x r) => cdr)
584 ((symbol? x)
585 (or (get-global-definition-hook x mod) (make-binding 'global)))
586 (else (make-binding 'displaced-lexical)))))
a63812a2 587
8fad25c2
AW
588 (define global-extend
589 (lambda (type sym val)
590 (put-global-definition-hook sym type val)))
591
592
593 ;; Conceptually, identifiers are always syntax objects. Internally,
594 ;; however, the wrap is sometimes maintained separately (a source of
595 ;; efficiency and confusion), so that symbols are also considered
596 ;; identifiers by id?. Externally, they are always wrapped.
597
598 (define nonsymbol-id?
599 (lambda (x)
600 (and (syntax-object? x)
601 (symbol? (syntax-object-expression x)))))
602
603 (define id?
604 (lambda (x)
605 (cond
606 ((symbol? x) #t)
607 ((syntax-object? x) (symbol? (syntax-object-expression x)))
608 (else #f))))
609
1bbe0a63
AW
610 (define-syntax-rule (id-sym-name e)
611 (let ((x e))
612 (if (syntax-object? x)
613 (syntax-object-expression x)
614 x)))
8fad25c2
AW
615
616 (define id-sym-name&marks
617 (lambda (x w)
618 (if (syntax-object? x)
619 (values
c3ae0ed4 620 (syntax-object-expression x)
8fad25c2
AW
621 (join-marks (wrap-marks w) (wrap-marks (syntax-object-wrap x))))
622 (values x (wrap-marks w)))))
c3ae0ed4 623
8fad25c2
AW
624 ;; syntax object wraps
625
1948b38d
MW
626 ;; <wrap> ::= ((<mark> ...) . (<subst> ...))
627 ;; <subst> ::= shift | <subs>
628 ;; <subs> ::= #(ribcage #(<sym> ...) #(<mark> ...) #(<label> ...))
629 ;; | #(ribcage (<sym> ...) (<mark> ...) (<label> ...))
8fad25c2 630
381ccb0b
AW
631 (define-syntax make-wrap (identifier-syntax cons))
632 (define-syntax wrap-marks (identifier-syntax car))
633 (define-syntax wrap-subst (identifier-syntax cdr))
8fad25c2 634
8fad25c2
AW
635 ;; labels must be comparable with "eq?", have read-write invariance,
636 ;; and distinct from symbols.
f9685f43
AW
637 (define (gen-label)
638 (string-append "l-" (session-id) (symbol->string (gensym "-"))))
8fad25c2
AW
639
640 (define gen-labels
641 (lambda (ls)
642 (if (null? ls)
643 '()
644 (cons (gen-label) (gen-labels (cdr ls))))))
645
646 (define-structure (ribcage symnames marks labels))
647
648 (define-syntax empty-wrap (identifier-syntax '(())))
649
650 (define-syntax top-wrap (identifier-syntax '((top))))
651
1bbe0a63
AW
652 (define-syntax-rule (top-marked? w)
653 (memq 'top (wrap-marks w)))
8fad25c2
AW
654
655 ;; Marks must be comparable with "eq?" and distinct from pairs and
656 ;; the symbol top. We do not use integers so that marks will remain
657 ;; unique even across file compiles.
658
659 (define-syntax the-anti-mark (identifier-syntax #f))
660
661 (define anti-mark
662 (lambda (w)
663 (make-wrap (cons the-anti-mark (wrap-marks w))
664 (cons 'shift (wrap-subst w)))))
665
1bbe0a63 666 (define-syntax-rule (new-mark)
f9685f43 667 (gensym (string-append "m-" (session-id) "-")))
8fad25c2
AW
668
669 ;; make-empty-ribcage and extend-ribcage maintain list-based ribcages for
670 ;; internal definitions, in which the ribcages are built incrementally
1bbe0a63
AW
671 (define-syntax-rule (make-empty-ribcage)
672 (make-ribcage '() '() '()))
8fad25c2
AW
673
674 (define extend-ribcage!
675 ;; must receive ids with complete wraps
676 (lambda (ribcage id label)
677 (set-ribcage-symnames! ribcage
678 (cons (syntax-object-expression id)
679 (ribcage-symnames ribcage)))
680 (set-ribcage-marks! ribcage
681 (cons (wrap-marks (syntax-object-wrap id))
682 (ribcage-marks ribcage)))
683 (set-ribcage-labels! ribcage
684 (cons label (ribcage-labels ribcage)))))
685
686 ;; make-binding-wrap creates vector-based ribcages
687 (define make-binding-wrap
688 (lambda (ids labels w)
689 (if (null? ids)
690 w
c3ae0ed4 691 (make-wrap
8fad25c2
AW
692 (wrap-marks w)
693 (cons
694 (let ((labelvec (list->vector labels)))
695 (let ((n (vector-length labelvec)))
696 (let ((symnamevec (make-vector n)) (marksvec (make-vector n)))
697 (let f ((ids ids) (i 0))
698 (if (not (null? ids))
699 (call-with-values
700 (lambda () (id-sym-name&marks (car ids) w))
701 (lambda (symname marks)
702 (vector-set! symnamevec i symname)
703 (vector-set! marksvec i marks)
704 (f (cdr ids) (fx+ i 1))))))
705 (make-ribcage symnamevec marksvec labelvec))))
706 (wrap-subst w))))))
707
708 (define smart-append
709 (lambda (m1 m2)
710 (if (null? m2)
711 m1
712 (append m1 m2))))
713
714 (define join-wraps
715 (lambda (w1 w2)
716 (let ((m1 (wrap-marks w1)) (s1 (wrap-subst w1)))
717 (if (null? m1)
718 (if (null? s1)
719 w2
720 (make-wrap
721 (wrap-marks w2)
722 (smart-append s1 (wrap-subst w2))))
723 (make-wrap
724 (smart-append m1 (wrap-marks w2))
725 (smart-append s1 (wrap-subst w2)))))))
726
727 (define join-marks
728 (lambda (m1 m2)
729 (smart-append m1 m2)))
730
731 (define same-marks?
732 (lambda (x y)
733 (or (eq? x y)
734 (and (not (null? x))
735 (not (null? y))
736 (eq? (car x) (car y))
737 (same-marks? (cdr x) (cdr y))))))
738
739 (define id-var-name
740 (lambda (id w)
1bbe0a63
AW
741 (define-syntax-rule (first e)
742 ;; Rely on Guile's multiple-values truncation.
743 e)
8fad25c2
AW
744 (define search
745 (lambda (sym subst marks)
746 (if (null? subst)
747 (values #f marks)
748 (let ((fst (car subst)))
749 (if (eq? fst 'shift)
750 (search sym (cdr subst) (cdr marks))
751 (let ((symnames (ribcage-symnames fst)))
752 (if (vector? symnames)
753 (search-vector-rib sym subst marks symnames fst)
754 (search-list-rib sym subst marks symnames fst))))))))
755 (define search-list-rib
756 (lambda (sym subst marks symnames ribcage)
757 (let f ((symnames symnames) (i 0))
c3ae0ed4 758 (cond
8fad25c2
AW
759 ((null? symnames) (search sym (cdr subst) marks))
760 ((and (eq? (car symnames) sym)
761 (same-marks? marks (list-ref (ribcage-marks ribcage) i)))
762 (values (list-ref (ribcage-labels ribcage) i) marks))
763 (else (f (cdr symnames) (fx+ i 1)))))))
764 (define search-vector-rib
765 (lambda (sym subst marks symnames ribcage)
766 (let ((n (vector-length symnames)))
767 (let f ((i 0))
768 (cond
769 ((fx= i n) (search sym (cdr subst) marks))
770 ((and (eq? (vector-ref symnames i) sym)
771 (same-marks? marks (vector-ref (ribcage-marks ribcage) i)))
772 (values (vector-ref (ribcage-labels ribcage) i) marks))
773 (else (f (fx+ i 1))))))))
774 (cond
775 ((symbol? id)
776 (or (first (search id (wrap-subst w) (wrap-marks w))) id))
777 ((syntax-object? id)
778 (let ((id (syntax-object-expression id))
779 (w1 (syntax-object-wrap id)))
780 (let ((marks (join-marks (wrap-marks w) (wrap-marks w1))))
781 (call-with-values (lambda () (search id (wrap-subst w) marks))
782 (lambda (new-id marks)
783 (or new-id
784 (first (search id (wrap-subst w1) marks))
785 id))))))
786 (else (syntax-violation 'id-var-name "invalid id" id)))))
787
3d51e57c
AW
788 ;; A helper procedure for syntax-locally-bound-identifiers, which
789 ;; itself is a helper for transformer procedures.
790 ;; `locally-bound-identifiers' returns a list of all bindings
791 ;; visible to a syntax object with the given wrap. They are in
792 ;; order from outer to inner.
793 ;;
794 ;; The purpose of this procedure is to give a transformer procedure
795 ;; references on bound identifiers, that the transformer can then
796 ;; introduce some of them in its output. As such, the identifiers
797 ;; are anti-marked, so that rebuild-macro-output doesn't apply new
798 ;; marks to them.
799 ;;
800 (define locally-bound-identifiers
801 (lambda (w mod)
802 (define scan
803 (lambda (subst results)
804 (if (null? subst)
805 results
806 (let ((fst (car subst)))
807 (if (eq? fst 'shift)
808 (scan (cdr subst) results)
809 (let ((symnames (ribcage-symnames fst))
810 (marks (ribcage-marks fst)))
811 (if (vector? symnames)
812 (scan-vector-rib subst symnames marks results)
813 (scan-list-rib subst symnames marks results))))))))
814 (define scan-list-rib
815 (lambda (subst symnames marks results)
816 (let f ((symnames symnames) (marks marks) (results results))
817 (if (null? symnames)
818 (scan (cdr subst) results)
819 (f (cdr symnames) (cdr marks)
820 (cons (wrap (car symnames)
821 (anti-mark (make-wrap (car marks) subst))
822 mod)
823 results))))))
824 (define scan-vector-rib
825 (lambda (subst symnames marks results)
826 (let ((n (vector-length symnames)))
827 (let f ((i 0) (results results))
828 (if (fx= i n)
829 (scan (cdr subst) results)
830 (f (fx+ i 1)
831 (cons (wrap (vector-ref symnames i)
832 (anti-mark (make-wrap (vector-ref marks i) subst))
833 mod)
834 results)))))))
835 (scan (wrap-subst w) '())))
836
9b0975f1
AW
837 ;; Returns three values: binding type, binding value, the module (for
838 ;; resolving toplevel vars).
839 (define (resolve-identifier id w r mod)
840 (define (resolve-global var mod)
841 (let ((b (or (get-global-definition-hook var mod)
842 (make-binding 'global))))
843 (if (eq? (binding-type b) 'global)
844 (values 'global var mod)
845 (values (binding-type b) (binding-value b) mod))))
846 (define (resolve-lexical label mod)
847 (let ((b (or (assq-ref r label)
848 (make-binding 'displaced-lexical))))
849 (values (binding-type b) (binding-value b) mod)))
850 (let ((n (id-var-name id w)))
851 (cond
852 ((symbol? n)
853 (resolve-global n (if (syntax-object? id)
854 (syntax-object-module id)
855 mod)))
856 ((string? n)
857 (resolve-lexical n (if (syntax-object? id)
858 (syntax-object-module id)
859 mod)))
860 (else
861 (error "unexpected id-var-name" id w n)))))
862
863 (define transformer-environment
864 (make-fluid
865 (lambda (k)
866 (error "called outside the dynamic extent of a syntax transformer"))))
867
868 (define (with-transformer-environment k)
869 ((fluid-ref transformer-environment) k))
870
8fad25c2
AW
871 ;; free-id=? must be passed fully wrapped ids since (free-id=? x y)
872 ;; may be true even if (free-id=? (wrap x w) (wrap y w)) is not.
873
874 (define free-id=?
875 (lambda (i j)
876 (and (eq? (id-sym-name i) (id-sym-name j)) ; accelerator
877 (eq? (id-var-name i empty-wrap) (id-var-name j empty-wrap)))))
878
879 ;; bound-id=? may be passed unwrapped (or partially wrapped) ids as
880 ;; long as the missing portion of the wrap is common to both of the ids
881 ;; since (bound-id=? x y) iff (bound-id=? (wrap x w) (wrap y w))
882
883 (define bound-id=?
884 (lambda (i j)
885 (if (and (syntax-object? i) (syntax-object? j))
886 (and (eq? (syntax-object-expression i)
887 (syntax-object-expression j))
888 (same-marks? (wrap-marks (syntax-object-wrap i))
889 (wrap-marks (syntax-object-wrap j))))
890 (eq? i j))))
891
892 ;; "valid-bound-ids?" returns #t if it receives a list of distinct ids.
893 ;; valid-bound-ids? may be passed unwrapped (or partially wrapped) ids
894 ;; as long as the missing portion of the wrap is common to all of the
895 ;; ids.
896
897 (define valid-bound-ids?
898 (lambda (ids)
899 (and (let all-ids? ((ids ids))
900 (or (null? ids)
901 (and (id? (car ids))
902 (all-ids? (cdr ids)))))
903 (distinct-bound-ids? ids))))
904
905 ;; distinct-bound-ids? expects a list of ids and returns #t if there are
906 ;; no duplicates. It is quadratic on the length of the id list; long
907 ;; lists could be sorted to make it more efficient. distinct-bound-ids?
908 ;; may be passed unwrapped (or partially wrapped) ids as long as the
909 ;; missing portion of the wrap is common to all of the ids.
910
911 (define distinct-bound-ids?
912 (lambda (ids)
913 (let distinct? ((ids ids))
914 (or (null? ids)
915 (and (not (bound-id-member? (car ids) (cdr ids)))
916 (distinct? (cdr ids)))))))
917
918 (define bound-id-member?
919 (lambda (x list)
920 (and (not (null? list))
921 (or (bound-id=? x (car list))
922 (bound-id-member? x (cdr list))))))
923
924 ;; wrapping expressions and identifiers
925
926 (define wrap
927 (lambda (x w defmod)
928 (cond
929 ((and (null? (wrap-marks w)) (null? (wrap-subst w))) x)
930 ((syntax-object? x)
931 (make-syntax-object
932 (syntax-object-expression x)
933 (join-wraps w (syntax-object-wrap x))
934 (syntax-object-module x)))
935 ((null? x) x)
936 (else (make-syntax-object x w defmod)))))
937
938 (define source-wrap
939 (lambda (x w s defmod)
940 (wrap (decorate-source x s) w defmod)))
941
942 ;; expanding
943
78a47455 944 (define expand-sequence
8fad25c2
AW
945 (lambda (body r w s mod)
946 (build-sequence s
947 (let dobody ((body body) (r r) (w w) (mod mod))
948 (if (null? body)
949 '()
78a47455 950 (let ((first (expand (car body) r w mod)))
8fad25c2
AW
951 (cons first (dobody (cdr body) r w mod))))))))
952
4da326f2 953 ;; At top-level, we allow mixed definitions and expressions. Like
78a47455 954 ;; expand-body we expand in two passes.
4da326f2
AW
955 ;;
956 ;; First, from left to right, we expand just enough to know what
957 ;; expressions are definitions, syntax definitions, and splicing
958 ;; statements (`begin'). If we anything needs evaluating at
959 ;; expansion-time, it is expanded directly.
960 ;;
961 ;; Otherwise we collect expressions to expand, in thunks, and then
962 ;; expand them all at the end. This allows all syntax expanders
963 ;; visible in a toplevel sequence to be visible during the
964 ;; expansions of all normal definitions and expressions in the
965 ;; sequence.
966 ;;
78a47455 967 (define expand-top-sequence
8fad25c2 968 (lambda (body r w s m esew mod)
4c2e13e5 969 (define (scan body r w s m esew mod exps)
4c2e13e5
AW
970 (cond
971 ((null? body)
972 ;; in reversed order
973 exps)
974 (else
975 (call-with-values
976 (lambda ()
977 (call-with-values
978 (lambda ()
979 (let ((e (car body)))
980 (syntax-type e r w (or (source-annotation e) s) #f mod #f)))
40e92f09 981 (lambda (type value form e w s mod)
4c2e13e5
AW
982 (case type
983 ((begin-form)
984 (syntax-case e ()
985 ((_) exps)
986 ((_ e1 e2 ...)
987 (scan #'(e1 e2 ...) r w s m esew mod exps))))
988 ((local-syntax-form)
78a47455
AW
989 (expand-local-syntax value e r w s mod
990 (lambda (body r w s mod)
991 (scan body r w s m esew mod exps))))
4c2e13e5
AW
992 ((eval-when-form)
993 (syntax-case e ()
994 ((_ (x ...) e1 e2 ...)
440ac793 995 (let ((when-list (parse-when-list e #'(x ...)))
4c2e13e5
AW
996 (body #'(e1 e2 ...)))
997 (cond
998 ((eq? m 'e)
999 (if (memq 'eval when-list)
1000 (scan body r w s
1001 (if (memq 'expand when-list) 'c&e 'e)
1002 '(eval)
1003 mod exps)
1004 (begin
1005 (if (memq 'expand when-list)
1006 (top-level-eval-hook
78a47455 1007 (expand-top-sequence body r w s 'e '(eval) mod)
4c2e13e5
AW
1008 mod))
1009 (values exps))))
1010 ((memq 'load when-list)
1011 (if (or (memq 'compile when-list)
1012 (memq 'expand when-list)
1013 (and (eq? m 'c&e) (memq 'eval when-list)))
1014 (scan body r w s 'c&e '(compile load) mod exps)
1015 (if (memq m '(c c&e))
1016 (scan body r w s 'c '(load) mod exps)
1017 (values exps))))
1018 ((or (memq 'compile when-list)
1019 (memq 'expand when-list)
1020 (and (eq? m 'c&e) (memq 'eval when-list)))
1021 (top-level-eval-hook
78a47455 1022 (expand-top-sequence body r w s 'e '(eval) mod)
4c2e13e5
AW
1023 mod)
1024 (values exps))
1025 (else
1026 (values exps)))))))
286dc5e1 1027 ((define-syntax-form define-syntax-parameter-form)
4c2e13e5
AW
1028 (let ((n (id-var-name value w)) (r (macros-only-env r)))
1029 (case m
1030 ((c)
1031 (if (memq 'compile esew)
78a47455 1032 (let ((e (expand-install-global n (expand e r w mod))))
4c2e13e5
AW
1033 (top-level-eval-hook e mod)
1034 (if (memq 'load esew)
1035 (values (cons e exps))
1036 (values exps)))
1037 (if (memq 'load esew)
78a47455 1038 (values (cons (expand-install-global n (expand e r w mod))
4c2e13e5
AW
1039 exps))
1040 (values exps))))
1041 ((c&e)
78a47455 1042 (let ((e (expand-install-global n (expand e r w mod))))
4c2e13e5
AW
1043 (top-level-eval-hook e mod)
1044 (values (cons e exps))))
1045 (else
1046 (if (memq 'eval esew)
1047 (top-level-eval-hook
78a47455 1048 (expand-install-global n (expand e r w mod))
4c2e13e5
AW
1049 mod))
1050 (values exps)))))
1051 ((define-form)
1052 (let* ((n (id-var-name value w))
1053 ;; Lookup the name in the module of the define form.
1054 (type (binding-type (lookup n r mod))))
1055 (case type
1056 ((global core macro module-ref)
1057 ;; affect compile-time environment (once we have booted)
1058 (if (and (memq m '(c c&e))
1059 (not (module-local-variable (current-module) n))
1060 (current-module))
1061 (let ((old (module-variable (current-module) n)))
1062 ;; use value of the same-named imported variable, if
1063 ;; any
1064 (if (and (variable? old) (variable-bound? old))
1065 (module-define! (current-module) n (variable-ref old))
1066 (module-add! (current-module) n (make-undefined-variable)))))
1067 (values
1068 (cons
4da326f2 1069 (if (eq? m 'c&e)
78a47455 1070 (let ((x (build-global-definition s n (expand e r w mod))))
4da326f2
AW
1071 (top-level-eval-hook x mod)
1072 x)
1073 (lambda ()
78a47455 1074 (build-global-definition s n (expand e r w mod))))
4c2e13e5
AW
1075 exps)))
1076 ((displaced-lexical)
1077 (syntax-violation #f "identifier out of context"
40e92f09
MW
1078 (source-wrap form w s mod)
1079 (wrap value w mod)))
4c2e13e5
AW
1080 (else
1081 (syntax-violation #f "cannot define keyword at top level"
40e92f09
MW
1082 (source-wrap form w s mod)
1083 (wrap value w mod))))))
4c2e13e5
AW
1084 (else
1085 (values (cons
4da326f2 1086 (if (eq? m 'c&e)
40e92f09 1087 (let ((x (expand-expr type value form e r w s mod)))
4da326f2
AW
1088 (top-level-eval-hook x mod)
1089 x)
1090 (lambda ()
40e92f09 1091 (expand-expr type value form e r w s mod)))
4c2e13e5
AW
1092 exps)))))))
1093 (lambda (exps)
1094 (scan (cdr body) r w s m esew mod exps))))))
1095
1096 (call-with-values (lambda ()
1097 (scan body r w s m esew mod '()))
1098 (lambda (exps)
1099 (if (null? exps)
1100 (build-void s)
4da326f2
AW
1101 (build-sequence
1102 s
1103 (let lp ((in exps) (out '()))
1104 (if (null? in) out
1105 (let ((e (car in)))
1106 (lp (cdr in)
1107 (cons (if (procedure? e) (e) e) out)))))))))))
4c2e13e5 1108
78a47455 1109 (define expand-install-global
8fad25c2
AW
1110 (lambda (name e)
1111 (build-global-definition
1112 no-source
1113 name
1114 (build-application
1115 no-source
1116 (build-primref no-source 'make-syntax-transformer)
1117 (list (build-data no-source name)
1118 (build-data no-source 'macro)
1119 e)))))
5f161164 1120
440ac793
AW
1121 (define parse-when-list
1122 (lambda (e when-list)
8fad25c2 1123 ;; when-list is syntax'd version of list of situations
440ac793
AW
1124 (let ((result (strip when-list empty-wrap)))
1125 (let lp ((l result))
1126 (if (null? l)
1127 result
1128 (if (memq (car l) '(compile load eval expand))
1129 (lp (cdr l))
1130 (syntax-violation 'eval-when "invalid situation" e
1131 (car l))))))))
8fad25c2 1132
40e92f09
MW
1133 ;; syntax-type returns seven values: type, value, form, e, w, s, and
1134 ;; mod. The first two are described in the table below.
8fad25c2
AW
1135 ;;
1136 ;; type value explanation
1137 ;; -------------------------------------------------------------------
1138 ;; core procedure core singleton
1139 ;; core-form procedure core form
1140 ;; module-ref procedure @ or @@ singleton
1141 ;; lexical name lexical variable reference
1142 ;; global name global variable reference
1143 ;; begin none begin keyword
1144 ;; define none define keyword
1145 ;; define-syntax none define-syntax keyword
286dc5e1 1146 ;; define-syntax-parameter none define-syntax-parameter keyword
8fad25c2
AW
1147 ;; local-syntax rec? letrec-syntax/let-syntax keyword
1148 ;; eval-when none eval-when keyword
1149 ;; syntax level pattern variable
1150 ;; displaced-lexical none displaced lexical identifier
1151 ;; lexical-call name call to lexical variable
1152 ;; global-call name call to global variable
1153 ;; call none any other call
1154 ;; begin-form none begin expression
1155 ;; define-form id variable definition
1156 ;; define-syntax-form id syntax definition
286dc5e1 1157 ;; define-syntax-parameter-form id syntax parameter definition
8fad25c2
AW
1158 ;; local-syntax-form rec? syntax definition
1159 ;; eval-when-form none eval-when form
1160 ;; constant none self-evaluating datum
1161 ;; other none anything else
1162 ;;
40e92f09
MW
1163 ;; form is the entire form. For definition forms (define-form,
1164 ;; define-syntax-form, and define-syntax-parameter-form), e is the
1165 ;; rhs expression. For all others, e is the entire form. w is the
1166 ;; wrap for both form and e. s is the source for the entire form.
1167 ;; mod is the module for both form and e.
8fad25c2 1168 ;;
286dc5e1
AW
1169 ;; syntax-type expands macros and unwraps as necessary to get to one
1170 ;; of the forms above. It also parses definition forms, although
1171 ;; perhaps this should be done by the consumer.
8fad25c2
AW
1172
1173 (define syntax-type
1174 (lambda (e r w s rib mod for-car?)
1175 (cond
1176 ((symbol? e)
1177 (let* ((n (id-var-name e w))
1178 (b (lookup n r mod))
1179 (type (binding-type b)))
1180 (case type
40e92f09
MW
1181 ((lexical) (values type (binding-value b) e e w s mod))
1182 ((global) (values type n e e w s mod))
8fad25c2
AW
1183 ((macro)
1184 (if for-car?
40e92f09 1185 (values type (binding-value b) e e w s mod)
78a47455 1186 (syntax-type (expand-macro (binding-value b) e r w s rib mod)
8fad25c2 1187 r empty-wrap s rib mod #f)))
40e92f09 1188 (else (values type (binding-value b) e e w s mod)))))
8fad25c2
AW
1189 ((pair? e)
1190 (let ((first (car e)))
1191 (call-with-values
1192 (lambda () (syntax-type first r w s rib mod #t))
40e92f09 1193 (lambda (ftype fval fform fe fw fs fmod)
8fad25c2
AW
1194 (case ftype
1195 ((lexical)
40e92f09 1196 (values 'lexical-call fval e e w s mod))
8fad25c2
AW
1197 ((global)
1198 ;; If we got here via an (@@ ...) expansion, we need to
1199 ;; make sure the fmod information is propagated back
1200 ;; correctly -- hence this consing.
1201 (values 'global-call (make-syntax-object fval w fmod)
40e92f09 1202 e e w s mod))
8fad25c2 1203 ((macro)
78a47455 1204 (syntax-type (expand-macro fval e r w s rib mod)
8fad25c2
AW
1205 r empty-wrap s rib mod for-car?))
1206 ((module-ref)
1207 (call-with-values (lambda () (fval e r w))
1208 (lambda (e r w s mod)
1209 (syntax-type e r w s rib mod for-car?))))
1210 ((core)
40e92f09 1211 (values 'core-form fval e e w s mod))
8fad25c2 1212 ((local-syntax)
40e92f09 1213 (values 'local-syntax-form fval e e w s mod))
8fad25c2 1214 ((begin)
40e92f09 1215 (values 'begin-form #f e e w s mod))
8fad25c2 1216 ((eval-when)
40e92f09 1217 (values 'eval-when-form #f e e w s mod))
8fad25c2
AW
1218 ((define)
1219 (syntax-case e ()
1220 ((_ name val)
1221 (id? #'name)
40e92f09 1222 (values 'define-form #'name e #'val w s mod))
8fad25c2
AW
1223 ((_ (name . args) e1 e2 ...)
1224 (and (id? #'name)
1225 (valid-bound-ids? (lambda-var-list #'args)))
1226 ;; need lambda here...
1227 (values 'define-form (wrap #'name w mod)
40e92f09 1228 (wrap e w mod)
8fad25c2
AW
1229 (decorate-source
1230 (cons #'lambda (wrap #'(args e1 e2 ...) w mod))
1231 s)
1232 empty-wrap s mod))
1233 ((_ name)
1234 (id? #'name)
1235 (values 'define-form (wrap #'name w mod)
40e92f09 1236 (wrap e w mod)
8fad25c2
AW
1237 #'(if #f #f)
1238 empty-wrap s mod))))
1239 ((define-syntax)
1240 (syntax-case e ()
1241 ((_ name val)
1242 (id? #'name)
40e92f09 1243 (values 'define-syntax-form #'name e #'val w s mod))))
286dc5e1
AW
1244 ((define-syntax-parameter)
1245 (syntax-case e ()
1246 ((_ name val)
1247 (id? #'name)
40e92f09 1248 (values 'define-syntax-parameter-form #'name e #'val w s mod))))
8fad25c2 1249 (else
40e92f09 1250 (values 'call #f e e w s mod)))))))
8fad25c2
AW
1251 ((syntax-object? e)
1252 (syntax-type (syntax-object-expression e)
1253 r
1254 (join-wraps w (syntax-object-wrap e))
1255 (or (source-annotation e) s) rib
1256 (or (syntax-object-module e) mod) for-car?))
40e92f09
MW
1257 ((self-evaluating? e) (values 'constant #f e e w s mod))
1258 (else (values 'other #f e e w s mod)))))
8fad25c2 1259
78a47455 1260 (define expand
8fad25c2
AW
1261 (lambda (e r w mod)
1262 (call-with-values
1263 (lambda () (syntax-type e r w (source-annotation e) #f mod #f))
40e92f09
MW
1264 (lambda (type value form e w s mod)
1265 (expand-expr type value form e r w s mod)))))
8fad25c2 1266
78a47455 1267 (define expand-expr
40e92f09 1268 (lambda (type value form e r w s mod)
8fad25c2
AW
1269 (case type
1270 ((lexical)
1271 (build-lexical-reference 'value s e value))
1272 ((core core-form)
1273 ;; apply transformer
1274 (value e r w s mod))
1275 ((module-ref)
1276 (call-with-values (lambda () (value e r w))
1277 (lambda (e r w s mod)
78a47455 1278 (expand e r w mod))))
8fad25c2 1279 ((lexical-call)
78a47455 1280 (expand-application
8fad25c2
AW
1281 (let ((id (car e)))
1282 (build-lexical-reference 'fun (source-annotation id)
1283 (if (syntax-object? id)
1284 (syntax->datum id)
1285 id)
1286 value))
1287 e r w s mod))
1288 ((global-call)
78a47455 1289 (expand-application
8fad25c2
AW
1290 (build-global-reference (source-annotation (car e))
1291 (if (syntax-object? value)
1292 (syntax-object-expression value)
1293 value)
1294 (if (syntax-object? value)
1295 (syntax-object-module value)
1296 mod))
1297 e r w s mod))
1298 ((constant) (build-data s (strip (source-wrap e w s mod) empty-wrap)))
1299 ((global) (build-global-reference s value mod))
78a47455 1300 ((call) (expand-application (expand (car e) r w mod) e r w s mod))
8fad25c2
AW
1301 ((begin-form)
1302 (syntax-case e ()
dc65d1cf
AW
1303 ((_ e1 e2 ...) (expand-sequence #'(e1 e2 ...) r w s mod))
1304 ((_)
f78a1cce
LC
1305 (if (include-deprecated-features)
1306 (begin
1307 (issue-deprecation-warning
1308 "Sequences of zero expressions are deprecated. Use *unspecified*.")
1309 (expand-void))
1310 (syntax-violation #f "sequence of zero expressions"
1311 (source-wrap e w s mod))))))
8fad25c2 1312 ((local-syntax-form)
78a47455 1313 (expand-local-syntax value e r w s mod expand-sequence))
8fad25c2
AW
1314 ((eval-when-form)
1315 (syntax-case e ()
1316 ((_ (x ...) e1 e2 ...)
440ac793 1317 (let ((when-list (parse-when-list e #'(x ...))))
8fad25c2 1318 (if (memq 'eval when-list)
78a47455
AW
1319 (expand-sequence #'(e1 e2 ...) r w s mod)
1320 (expand-void))))))
286dc5e1 1321 ((define-form define-syntax-form define-syntax-parameter-form)
40e92f09
MW
1322 (syntax-violation #f "definition in expression context, where definitions are not allowed,"
1323 (source-wrap form w s mod)))
8fad25c2
AW
1324 ((syntax)
1325 (syntax-violation #f "reference to pattern variable outside syntax form"
1326 (source-wrap e w s mod)))
1327 ((displaced-lexical)
1328 (syntax-violation #f "reference to identifier outside its scope"
1329 (source-wrap e w s mod)))
1330 (else (syntax-violation #f "unexpected syntax"
1331 (source-wrap e w s mod))))))
1332
78a47455 1333 (define expand-application
8fad25c2
AW
1334 (lambda (x e r w s mod)
1335 (syntax-case e ()
1336 ((e0 e1 ...)
1337 (build-application s x
78a47455 1338 (map (lambda (e) (expand e r w mod)) #'(e1 ...)))))))
8fad25c2
AW
1339
1340 ;; (What follows is my interpretation of what's going on here -- Andy)
1341 ;;
1342 ;; A macro takes an expression, a tree, the leaves of which are identifiers
1343 ;; and datums. Identifiers are symbols along with a wrap and a module. For
1344 ;; efficiency, subtrees that share wraps and modules may be grouped as one
1345 ;; syntax object.
c3ae0ed4 1346 ;;
8fad25c2
AW
1347 ;; Going into the expansion, the expression is given an anti-mark, which
1348 ;; logically propagates to all leaves. Then, in the new expression returned
1349 ;; from the transfomer, if we see an expression with an anti-mark, we know it
1350 ;; pertains to the original expression; conversely, expressions without the
1351 ;; anti-mark are known to be introduced by the transformer.
c3ae0ed4 1352 ;;
8fad25c2
AW
1353 ;; OK, good until now. We know this algorithm does lexical scoping
1354 ;; appropriately because it's widely known in the literature, and psyntax is
1355 ;; widely used. But what about modules? Here we're on our own. What we do is
1356 ;; to mark the module of expressions produced by a macro as pertaining to the
1357 ;; module that was current when the macro was defined -- that is, free
1358 ;; identifiers introduced by a macro are scoped in the macro's module, not in
1359 ;; the expansion's module. Seems to work well.
c3ae0ed4 1360 ;;
8fad25c2
AW
1361 ;; The only wrinkle is when we want a macro to expand to code in another
1362 ;; module, as is the case for the r6rs `library' form -- the body expressions
b3da54d1 1363 ;; should be scoped relative the new module, the one defined by the macro.
8fad25c2 1364 ;; For that, use `(@@ mod-name body)'.
c3ae0ed4 1365 ;;
8fad25c2
AW
1366 ;; Part of the macro output will be from the site of the macro use and part
1367 ;; from the macro definition. We allow source information from the macro use
1368 ;; to pass through, but we annotate the parts coming from the macro with the
1369 ;; source location information corresponding to the macro use. It would be
1370 ;; really nice if we could also annotate introduced expressions with the
1371 ;; locations corresponding to the macro definition, but that is not yet
1372 ;; possible.
78a47455 1373 (define expand-macro
8fad25c2
AW
1374 (lambda (p e r w s rib mod)
1375 (define rebuild-macro-output
1376 (lambda (x m)
1377 (cond ((pair? x)
1378 (decorate-source
1379 (cons (rebuild-macro-output (car x) m)
1380 (rebuild-macro-output (cdr x) m))
1381 s))
1382 ((syntax-object? x)
1383 (let ((w (syntax-object-wrap x)))
30398e94 1384 (let ((ms (wrap-marks w)) (ss (wrap-subst w)))
8fad25c2
AW
1385 (if (and (pair? ms) (eq? (car ms) the-anti-mark))
1386 ;; output is from original text
1387 (make-syntax-object
1388 (syntax-object-expression x)
30398e94 1389 (make-wrap (cdr ms) (if rib (cons rib (cdr ss)) (cdr ss)))
8fad25c2
AW
1390 (syntax-object-module x))
1391 ;; output introduced by macro
1392 (make-syntax-object
1393 (decorate-source (syntax-object-expression x) s)
1394 (make-wrap (cons m ms)
1395 (if rib
30398e94
MW
1396 (cons rib (cons 'shift ss))
1397 (cons 'shift ss)))
8fad25c2
AW
1398 (syntax-object-module x))))))
1399
1400 ((vector? x)
1401 (let* ((n (vector-length x))
30398e94 1402 (v (decorate-source (make-vector n) s)))
8fad25c2
AW
1403 (do ((i 0 (fx+ i 1)))
1404 ((fx= i n) v)
1405 (vector-set! v i
1406 (rebuild-macro-output (vector-ref x i) m)))))
1407 ((symbol? x)
1408 (syntax-violation #f "encountered raw symbol in macro output"
1409 (source-wrap e w (wrap-subst w) mod) x))
1410 (else (decorate-source x s)))))
9b0975f1
AW
1411 (with-fluids ((transformer-environment
1412 (lambda (k) (k e r w s rib mod))))
1413 (rebuild-macro-output (p (source-wrap e (anti-mark w) s mod))
1414 (new-mark)))))
8fad25c2 1415
78a47455 1416 (define expand-body
8fad25c2
AW
1417 ;; In processing the forms of the body, we create a new, empty wrap.
1418 ;; This wrap is augmented (destructively) each time we discover that
1419 ;; the next form is a definition. This is done:
1420 ;;
1421 ;; (1) to allow the first nondefinition form to be a call to
1422 ;; one of the defined ids even if the id previously denoted a
1423 ;; definition keyword or keyword for a macro expanding into a
1424 ;; definition;
1425 ;; (2) to prevent subsequent definition forms (but unfortunately
1426 ;; not earlier ones) and the first nondefinition form from
1427 ;; confusing one of the bound identifiers for an auxiliary
1428 ;; keyword; and
1429 ;; (3) so that we do not need to restart the expansion of the
1430 ;; first nondefinition form, which is problematic anyway
1431 ;; since it might be the first element of a begin that we
1432 ;; have just spliced into the body (meaning if we restarted,
1433 ;; we'd really need to restart with the begin or the macro
1434 ;; call that expanded into the begin, and we'd have to give
1435 ;; up allowing (begin <defn>+ <expr>+), which is itself
1436 ;; problematic since we don't know if a begin contains only
1437 ;; definitions until we've expanded it).
1438 ;;
1439 ;; Before processing the body, we also create a new environment
1440 ;; containing a placeholder for the bindings we will add later and
1441 ;; associate this environment with each form. In processing a
1442 ;; let-syntax or letrec-syntax, the associated environment may be
1443 ;; augmented with local keyword bindings, so the environment may
1444 ;; be different for different forms in the body. Once we have
1445 ;; gathered up all of the definitions, we evaluate the transformer
1446 ;; expressions and splice into r at the placeholder the new variable
1447 ;; and keyword bindings. This allows let-syntax or letrec-syntax
1448 ;; forms local to a portion or all of the body to shadow the
1449 ;; definition bindings.
1450 ;;
1451 ;; Subforms of a begin, let-syntax, or letrec-syntax are spliced
1452 ;; into the body.
1453 ;;
1454 ;; outer-form is fully wrapped w/source
1455 (lambda (body outer-form r w mod)
1456 (let* ((r (cons '("placeholder" . (placeholder)) r))
1457 (ribcage (make-empty-ribcage))
1458 (w (make-wrap (wrap-marks w) (cons ribcage (wrap-subst w)))))
1459 (let parse ((body (map (lambda (x) (cons r (wrap x w mod))) body))
1460 (ids '()) (labels '())
1461 (var-ids '()) (vars '()) (vals '()) (bindings '()))
1462 (if (null? body)
1463 (syntax-violation #f "no expressions in body" outer-form)
1464 (let ((e (cdar body)) (er (caar body)))
1465 (call-with-values
25645a0a 1466 (lambda () (syntax-type e er empty-wrap (source-annotation e) ribcage mod #f))
40e92f09 1467 (lambda (type value form e w s mod)
8fad25c2
AW
1468 (case type
1469 ((define-form)
1470 (let ((id (wrap value w mod)) (label (gen-label)))
1471 (let ((var (gen-var id)))
1472 (extend-ribcage! ribcage id label)
1473 (parse (cdr body)
1474 (cons id ids) (cons label labels)
1475 (cons id var-ids)
1476 (cons var vars) (cons (cons er (wrap e w mod)) vals)
1477 (cons (make-binding 'lexical var) bindings)))))
286dc5e1 1478 ((define-syntax-form define-syntax-parameter-form)
ceb7f9cc
MW
1479 (let ((id (wrap value w mod))
1480 (label (gen-label))
1481 (trans-r (macros-only-env er)))
c3ae0ed4 1482 (extend-ribcage! ribcage id label)
ceb7f9cc
MW
1483 ;; As required by R6RS, evaluate the right-hand-sides of internal
1484 ;; syntax definition forms and add their transformers to the
1485 ;; compile-time environment immediately, so that the newly-defined
1486 ;; keywords may be used in definition context within the same
1487 ;; lexical contour.
1488 (set-cdr! r (extend-env (list label)
1489 (list (make-binding 'macro
1490 (eval-local-transformer
1491 (expand e trans-r w mod)
1492 mod)))
1493 (cdr r)))
1494 (parse (cdr body) (cons id ids) labels var-ids vars vals bindings)))
8fad25c2
AW
1495 ((begin-form)
1496 (syntax-case e ()
1497 ((_ e1 ...)
1498 (parse (let f ((forms #'(e1 ...)))
1499 (if (null? forms)
1500 (cdr body)
1501 (cons (cons er (wrap (car forms) w mod))
1502 (f (cdr forms)))))
1503 ids labels var-ids vars vals bindings))))
1504 ((local-syntax-form)
78a47455
AW
1505 (expand-local-syntax value e er w s mod
1506 (lambda (forms er w s mod)
1507 (parse (let f ((forms forms))
1508 (if (null? forms)
1509 (cdr body)
1510 (cons (cons er (wrap (car forms) w mod))
1511 (f (cdr forms)))))
1512 ids labels var-ids vars vals bindings))))
8fad25c2
AW
1513 (else ; found a non-definition
1514 (if (null? ids)
1515 (build-sequence no-source
1516 (map (lambda (x)
78a47455 1517 (expand (cdr x) (car x) empty-wrap mod))
8fad25c2
AW
1518 (cons (cons er (source-wrap e w s mod))
1519 (cdr body))))
1520 (begin
1521 (if (not (valid-bound-ids? ids))
1522 (syntax-violation
1523 #f "invalid or duplicate identifier in definition"
1524 outer-form))
8fad25c2
AW
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)
0426b3f8
MW
2084 (define (build-it meta clauses)
2085 (call-with-values
2086 (lambda ()
2087 (expand-lambda-case e r w s mod
2088 lambda-formals
2089 clauses))
2090 (lambda (meta* lcase)
2091 (build-case-lambda s (append meta meta*) lcase))))
8fad25c2 2092 (syntax-case e ()
19113f1c 2093 ((_ (args e1 e2 ...) ...)
0426b3f8
MW
2094 (build-it '() #'((args e1 e2 ...) ...)))
2095 ((_ docstring (args e1 e2 ...) ...)
2096 (string? (syntax->datum #'docstring))
2097 (build-it `((documentation
2098 . ,(syntax->datum #'docstring)))
2099 #'((args e1 e2 ...) ...)))
8fad25c2
AW
2100 (_ (syntax-violation 'case-lambda "bad case-lambda" e)))))
2101
2102 (global-extend 'core 'case-lambda*
2103 (lambda (e r w s mod)
0426b3f8
MW
2104 (define (build-it meta clauses)
2105 (call-with-values
2106 (lambda ()
2107 (expand-lambda-case e r w s mod
2108 lambda*-formals
2109 clauses))
2110 (lambda (meta* lcase)
2111 (build-case-lambda s (append meta meta*) lcase))))
8fad25c2 2112 (syntax-case e ()
19113f1c 2113 ((_ (args e1 e2 ...) ...)
0426b3f8
MW
2114 (build-it '() #'((args e1 e2 ...) ...)))
2115 ((_ docstring (args e1 e2 ...) ...)
2116 (string? (syntax->datum #'docstring))
2117 (build-it `((documentation
2118 . ,(syntax->datum #'docstring)))
2119 #'((args e1 e2 ...) ...)))
8fad25c2
AW
2120 (_ (syntax-violation 'case-lambda "bad case-lambda*" e)))))
2121
2122 (global-extend 'core 'let
2123 (let ()
78a47455 2124 (define (expand-let e r w s mod constructor ids vals exps)
8fad25c2
AW
2125 (if (not (valid-bound-ids? ids))
2126 (syntax-violation 'let "duplicate bound variable" e)
2127 (let ((labels (gen-labels ids))
2128 (new-vars (map gen-var ids)))
2129 (let ((nw (make-binding-wrap ids labels w))
2130 (nr (extend-var-env labels new-vars r)))
2131 (constructor s
2132 (map syntax->datum ids)
2133 new-vars
78a47455
AW
2134 (map (lambda (x) (expand x r w mod)) vals)
2135 (expand-body exps (source-wrap e nw s mod)
2136 nr nw mod))))))
8fad25c2
AW
2137 (lambda (e r w s mod)
2138 (syntax-case e ()
2139 ((_ ((id val) ...) e1 e2 ...)
2140 (and-map id? #'(id ...))
78a47455
AW
2141 (expand-let e r w s mod
2142 build-let
2143 #'(id ...)
2144 #'(val ...)
2145 #'(e1 e2 ...)))
8fad25c2
AW
2146 ((_ f ((id val) ...) e1 e2 ...)
2147 (and (id? #'f) (and-map id? #'(id ...)))
78a47455
AW
2148 (expand-let e r w s mod
2149 build-named-let
2150 #'(f id ...)
2151 #'(val ...)
2152 #'(e1 e2 ...)))
8fad25c2
AW
2153 (_ (syntax-violation 'let "bad let" (source-wrap e w s mod)))))))
2154
2155
2156 (global-extend 'core 'letrec
c3ae0ed4
AW
2157 (lambda (e r w s mod)
2158 (syntax-case e ()
2159 ((_ ((id val) ...) e1 e2 ...)
2160 (and-map id? #'(id ...))
8fad25c2
AW
2161 (let ((ids #'(id ...)))
2162 (if (not (valid-bound-ids? ids))
2163 (syntax-violation 'letrec "duplicate bound variable" e)
2164 (let ((labels (gen-labels ids))
2165 (new-vars (map gen-var ids)))
2166 (let ((w (make-binding-wrap ids labels w))
2167 (r (extend-var-env labels new-vars r)))
2168 (build-letrec s #f
2169 (map syntax->datum ids)
2170 new-vars
78a47455
AW
2171 (map (lambda (x) (expand x r w mod)) #'(val ...))
2172 (expand-body #'(e1 e2 ...)
2173 (source-wrap e w s mod) r w mod)))))))
8fad25c2
AW
2174 (_ (syntax-violation 'letrec "bad letrec" (source-wrap e w s mod))))))
2175
2176
2177 (global-extend 'core 'letrec*
2178 (lambda (e r w s mod)
2179 (syntax-case e ()
2180 ((_ ((id val) ...) e1 e2 ...)
2181 (and-map id? #'(id ...))
2182 (let ((ids #'(id ...)))
2183 (if (not (valid-bound-ids? ids))
2184 (syntax-violation 'letrec* "duplicate bound variable" e)
2185 (let ((labels (gen-labels ids))
2186 (new-vars (map gen-var ids)))
2187 (let ((w (make-binding-wrap ids labels w))
2188 (r (extend-var-env labels new-vars r)))
2189 (build-letrec s #t
2190 (map syntax->datum ids)
2191 new-vars
78a47455
AW
2192 (map (lambda (x) (expand x r w mod)) #'(val ...))
2193 (expand-body #'(e1 e2 ...)
2194 (source-wrap e w s mod) r w mod)))))))
8fad25c2
AW
2195 (_ (syntax-violation 'letrec* "bad letrec*" (source-wrap e w s mod))))))
2196
2197
2198 (global-extend 'core 'set!
2199 (lambda (e r w s mod)
2200 (syntax-case e ()
2201 ((_ id val)
2202 (id? #'id)
2203 (let ((n (id-var-name #'id w))
2204 ;; Lookup id in its module
2205 (id-mod (if (syntax-object? #'id)
2206 (syntax-object-module #'id)
2207 mod)))
2208 (let ((b (lookup n r id-mod)))
2209 (case (binding-type b)
2210 ((lexical)
2211 (build-lexical-assignment s
2212 (syntax->datum #'id)
2213 (binding-value b)
78a47455 2214 (expand #'val r w mod)))
8fad25c2 2215 ((global)
78a47455 2216 (build-global-assignment s n (expand #'val r w mod) id-mod))
8fad25c2
AW
2217 ((macro)
2218 (let ((p (binding-value b)))
2219 (if (procedure-property p 'variable-transformer)
78a47455 2220 ;; As syntax-type does, call expand-macro with
8fad25c2 2221 ;; the mod of the expression. Hmm.
78a47455 2222 (expand (expand-macro p e r w s #f mod) r empty-wrap mod)
8fad25c2
AW
2223 (syntax-violation 'set! "not a variable transformer"
2224 (wrap e w mod)
2225 (wrap #'id w id-mod)))))
2226 ((displaced-lexical)
2227 (syntax-violation 'set! "identifier out of context"
2228 (wrap #'id w mod)))
2229 (else (syntax-violation 'set! "bad set!"
2230 (source-wrap e w s mod)))))))
2231 ((_ (head tail ...) val)
2232 (call-with-values
2233 (lambda () (syntax-type #'head r empty-wrap no-source #f mod #t))
40e92f09 2234 (lambda (type value formform ee ww ss modmod)
8fad25c2
AW
2235 (case type
2236 ((module-ref)
78a47455 2237 (let ((val (expand #'val r w mod)))
8fad25c2
AW
2238 (call-with-values (lambda () (value #'(head tail ...) r w))
2239 (lambda (e r w s* mod)
2240 (syntax-case e ()
2241 (e (id? #'e)
2242 (build-global-assignment s (syntax->datum #'e)
2243 val mod)))))))
2244 (else
2245 (build-application s
78a47455
AW
2246 (expand #'(setter head) r w mod)
2247 (map (lambda (e) (expand e r w mod))
8fad25c2
AW
2248 #'(tail ... val))))))))
2249 (_ (syntax-violation 'set! "bad set!" (source-wrap e w s mod))))))
2250
2251 (global-extend 'module-ref '@
2252 (lambda (e r w)
2253 (syntax-case e ()
2254 ((_ (mod ...) id)
2255 (and (and-map id? #'(mod ...)) (id? #'id))
8210c853
MW
2256 ;; Strip the wrap from the identifier and return top-wrap
2257 ;; so that the identifier will not be captured by lexicals.
2258 (values (syntax->datum #'id) r top-wrap #f
8fad25c2
AW
2259 (syntax->datum
2260 #'(public mod ...)))))))
2261
2262 (global-extend 'module-ref '@@
2263 (lambda (e r w)
2264 (define remodulate
2265 (lambda (x mod)
2266 (cond ((pair? x)
2267 (cons (remodulate (car x) mod)
2268 (remodulate (cdr x) mod)))
2269 ((syntax-object? x)
2270 (make-syntax-object
2271 (remodulate (syntax-object-expression x) mod)
2272 (syntax-object-wrap x)
2273 ;; hither the remodulation
2274 mod))
2275 ((vector? x)
2276 (let* ((n (vector-length x)) (v (make-vector n)))
2277 (do ((i 0 (fx+ i 1)))
2278 ((fx= i n) v)
2279 (vector-set! v i (remodulate (vector-ref x i) mod)))))
2280 (else x))))
8210c853
MW
2281 (syntax-case e (@@)
2282 ((_ (mod ...) id)
2283 (and (and-map id? #'(mod ...)) (id? #'id))
2284 ;; Strip the wrap from the identifier and return top-wrap
2285 ;; so that the identifier will not be captured by lexicals.
2286 (values (syntax->datum #'id) r top-wrap #f
2287 (syntax->datum
2288 #'(private mod ...))))
2289 ((_ @@ (mod ...) exp)
8fad25c2 2290 (and-map id? #'(mod ...))
8210c853
MW
2291 ;; This is a special syntax used to support R6RS library forms.
2292 ;; Unlike the syntax above, the last item is not restricted to
2293 ;; be a single identifier, and the syntax objects are kept
2294 ;; intact, with only their module changed.
8fad25c2
AW
2295 (let ((mod (syntax->datum #'(private mod ...))))
2296 (values (remodulate #'exp mod)
2297 r w (source-annotation #'exp)
2298 mod))))))
9365d8ad 2299
8fad25c2
AW
2300 (global-extend 'core 'if
2301 (lambda (e r w s mod)
2302 (syntax-case e ()
2303 ((_ test then)
2304 (build-conditional
2305 s
78a47455
AW
2306 (expand #'test r w mod)
2307 (expand #'then r w mod)
8fad25c2
AW
2308 (build-void no-source)))
2309 ((_ test then else)
2310 (build-conditional
2311 s
78a47455
AW
2312 (expand #'test r w mod)
2313 (expand #'then r w mod)
2314 (expand #'else r w mod))))))
8fad25c2
AW
2315
2316 (global-extend 'core 'with-fluids
2317 (lambda (e r w s mod)
2318 (syntax-case e ()
2319 ((_ ((fluid val) ...) b b* ...)
2320 (build-dynlet
2321 s
78a47455
AW
2322 (map (lambda (x) (expand x r w mod)) #'(fluid ...))
2323 (map (lambda (x) (expand x r w mod)) #'(val ...))
2324 (expand-body #'(b b* ...)
2325 (source-wrap e w s mod) r w mod))))))
6360c1d4 2326
8fad25c2
AW
2327 (global-extend 'begin 'begin '())
2328
2329 (global-extend 'define 'define '())
2330
2331 (global-extend 'define-syntax 'define-syntax '())
286dc5e1 2332 (global-extend 'define-syntax-parameter 'define-syntax-parameter '())
8fad25c2
AW
2333
2334 (global-extend 'eval-when 'eval-when '())
2335
2336 (global-extend 'core 'syntax-case
2337 (let ()
2338 (define convert-pattern
2339 ;; accepts pattern & keys
2340 ;; returns $sc-dispatch pattern & ids
2341 (lambda (pattern keys)
2342 (define cvt*
2343 (lambda (p* n ids)
0ed9680f
SIT
2344 (if (not (pair? p*))
2345 (cvt p* n ids)
8fad25c2
AW
2346 (call-with-values
2347 (lambda () (cvt* (cdr p*) n ids))
2348 (lambda (y ids)
2349 (call-with-values
2350 (lambda () (cvt (car p*) n ids))
2351 (lambda (x ids)
2352 (values (cons x y) ids))))))))
0ed9680f
SIT
2353
2354 (define (v-reverse x)
2355 (let loop ((r '()) (x x))
2356 (if (not (pair? x))
2357 (values r x)
2358 (loop (cons (car x) r) (cdr x)))))
2359
8fad25c2
AW
2360 (define cvt
2361 (lambda (p n ids)
2362 (if (id? p)
2363 (cond
2364 ((bound-id-member? p keys)
2365 (values (vector 'free-id p) ids))
2366 ((free-id=? p #'_)
2367 (values '_ ids))
2368 (else
2369 (values 'any (cons (cons p n) ids))))
2370 (syntax-case p ()
2371 ((x dots)
2372 (ellipsis? (syntax dots))
2373 (call-with-values
2374 (lambda () (cvt (syntax x) (fx+ n 1) ids))
2375 (lambda (p ids)
2376 (values (if (eq? p 'any) 'each-any (vector 'each p))
2377 ids))))
0ed9680f 2378 ((x dots . ys)
8fad25c2
AW
2379 (ellipsis? (syntax dots))
2380 (call-with-values
0ed9680f 2381 (lambda () (cvt* (syntax ys) n ids))
8fad25c2
AW
2382 (lambda (ys ids)
2383 (call-with-values
2384 (lambda () (cvt (syntax x) (+ n 1) ids))
2385 (lambda (x ids)
0ed9680f
SIT
2386 (call-with-values
2387 (lambda () (v-reverse ys))
2388 (lambda (ys e)
2389 (values `#(each+ ,x ,ys ,e)
2390 ids))))))))
8fad25c2
AW
2391 ((x . y)
2392 (call-with-values
2393 (lambda () (cvt (syntax y) n ids))
2394 (lambda (y ids)
2395 (call-with-values
2396 (lambda () (cvt (syntax x) n ids))
2397 (lambda (x ids)
2398 (values (cons x y) ids))))))
2399 (() (values '() ids))
2400 (#(x ...)
2401 (call-with-values
2402 (lambda () (cvt (syntax (x ...)) n ids))
2403 (lambda (p ids) (values (vector 'vector p) ids))))
2404 (x (values (vector 'atom (strip p empty-wrap)) ids))))))
2405 (cvt pattern 0 '())))
2406
2407 (define build-dispatch-call
2408 (lambda (pvars exp y r mod)
2409 (let ((ids (map car pvars)) (levels (map cdr pvars)))
2410 (let ((labels (gen-labels ids)) (new-vars (map gen-var ids)))
2411 (build-application no-source
2412 (build-primref no-source 'apply)
2413 (list (build-simple-lambda no-source (map syntax->datum ids) #f new-vars '()
78a47455
AW
2414 (expand exp
2415 (extend-env
2416 labels
2417 (map (lambda (var level)
2418 (make-binding 'syntax `(,var . ,level)))
2419 new-vars
2420 (map cdr pvars))
2421 r)
2422 (make-binding-wrap ids labels empty-wrap)
2423 mod))
8fad25c2
AW
2424 y))))))
2425
2426 (define gen-clause
2427 (lambda (x keys clauses r pat fender exp mod)
2428 (call-with-values
2429 (lambda () (convert-pattern pat keys))
2430 (lambda (p pvars)
2431 (cond
2432 ((not (distinct-bound-ids? (map car pvars)))
2433 (syntax-violation 'syntax-case "duplicate pattern variable" pat))
2434 ((not (and-map (lambda (x) (not (ellipsis? (car x)))) pvars))
2435 (syntax-violation 'syntax-case "misplaced ellipsis" pat))
2436 (else
2437 (let ((y (gen-var 'tmp)))
2438 ;; fat finger binding and references to temp variable y
2439 (build-application no-source
2440 (build-simple-lambda no-source (list 'tmp) #f (list y) '()
2441 (let ((y (build-lexical-reference 'value no-source
2442 'tmp y)))
2443 (build-conditional no-source
2444 (syntax-case fender ()
2445 (#t y)
2446 (_ (build-conditional no-source
2447 y
2448 (build-dispatch-call pvars fender y r mod)
2449 (build-data no-source #f))))
2450 (build-dispatch-call pvars exp y r mod)
2451 (gen-syntax-case x keys clauses r mod))))
2452 (list (if (eq? p 'any)
2453 (build-application no-source
2454 (build-primref no-source 'list)
2455 (list x))
2456 (build-application no-source
2457 (build-primref no-source '$sc-dispatch)
2458 (list x (build-data no-source p)))))))))))))
2459
2460 (define gen-syntax-case
2461 (lambda (x keys clauses r mod)
2462 (if (null? clauses)
2463 (build-application no-source
2464 (build-primref no-source 'syntax-violation)
2465 (list (build-data no-source #f)
2466 (build-data no-source
2467 "source expression failed to match any pattern")
2468 x))
2469 (syntax-case (car clauses) ()
2470 ((pat exp)
2471 (if (and (id? #'pat)
2472 (and-map (lambda (x) (not (free-id=? #'pat x)))
2473 (cons #'(... ...) keys)))
9120f130 2474 (if (free-id=? #'pat #'_)
78a47455 2475 (expand #'exp r empty-wrap mod)
8fad25c2
AW
2476 (let ((labels (list (gen-label)))
2477 (var (gen-var #'pat)))
2478 (build-application no-source
2479 (build-simple-lambda
2480 no-source (list (syntax->datum #'pat)) #f (list var)
2481 '()
78a47455
AW
2482 (expand #'exp
2483 (extend-env labels
2484 (list (make-binding 'syntax `(,var . 0)))
2485 r)
2486 (make-binding-wrap #'(pat)
2487 labels empty-wrap)
2488 mod))
8fad25c2
AW
2489 (list x))))
2490 (gen-clause x keys (cdr clauses) r
2491 #'pat #t #'exp mod)))
2492 ((pat fender exp)
2493 (gen-clause x keys (cdr clauses) r
2494 #'pat #'fender #'exp mod))
2495 (_ (syntax-violation 'syntax-case "invalid clause"
2496 (car clauses)))))))
2497
2498 (lambda (e r w s mod)
2499 (let ((e (source-wrap e w s mod)))
2500 (syntax-case e ()
2501 ((_ val (key ...) m ...)
2502 (if (and-map (lambda (x) (and (id? x) (not (ellipsis? x))))
2503 #'(key ...))
2504 (let ((x (gen-var 'tmp)))
2505 ;; fat finger binding and references to temp variable x
2506 (build-application s
2507 (build-simple-lambda no-source (list 'tmp) #f (list x) '()
2508 (gen-syntax-case (build-lexical-reference 'value no-source
2509 'tmp x)
2510 #'(key ...) #'(m ...)
2511 r
2512 mod))
78a47455 2513 (list (expand #'val r empty-wrap mod))))
8fad25c2
AW
2514 (syntax-violation 'syntax-case "invalid literals list" e))))))))
2515
78a47455 2516 ;; The portable macroexpand seeds expand-top's mode m with 'e (for
8fad25c2
AW
2517 ;; evaluating) and esew (which stands for "eval syntax expanders
2518 ;; when") with '(eval). In Chez Scheme, m is set to 'c instead of e
2519 ;; if we are compiling a file, and esew is set to
2520 ;; (eval-syntactic-expanders-when), which defaults to the list
2521 ;; '(compile load eval). This means that, by default, top-level
2522 ;; syntactic definitions are evaluated immediately after they are
2523 ;; expanded, and the expanded definitions are also residualized into
2524 ;; the object file if we are compiling a file.
2525 (set! macroexpand
2526 (lambda* (x #:optional (m 'e) (esew '(eval)))
78a47455
AW
2527 (expand-top-sequence (list x) null-env top-wrap #f m esew
2528 (cons 'hygiene (module-name (current-module))))))
8fad25c2
AW
2529
2530 (set! identifier?
2531 (lambda (x)
2532 (nonsymbol-id? x)))
2533
2534 (set! datum->syntax
2535 (lambda (id datum)
2536 (make-syntax-object datum (syntax-object-wrap id)
2537 (syntax-object-module id))))
2538
2539 (set! syntax->datum
2540 ;; accepts any object, since syntax objects may consist partially
2541 ;; or entirely of unwrapped, nonsymbolic data
2542 (lambda (x)
2543 (strip x empty-wrap)))
2544
2545 (set! syntax-source
2546 (lambda (x) (source-annotation x)))
2547
2548 (set! generate-temporaries
2549 (lambda (ls)
2550 (arg-check list? ls 'generate-temporaries)
933c6eb7 2551 (let ((mod (cons 'hygiene (module-name (current-module)))))
f9685f43 2552 (map (lambda (x) (wrap (gensym "t-") top-wrap mod)) ls))))
8fad25c2
AW
2553
2554 (set! free-identifier=?
2555 (lambda (x y)
2556 (arg-check nonsymbol-id? x 'free-identifier=?)
2557 (arg-check nonsymbol-id? y 'free-identifier=?)
2558 (free-id=? x y)))
2559
2560 (set! bound-identifier=?
2561 (lambda (x y)
2562 (arg-check nonsymbol-id? x 'bound-identifier=?)
2563 (arg-check nonsymbol-id? y 'bound-identifier=?)
2564 (bound-id=? x y)))
2565
2566 (set! syntax-violation
8f1870f2 2567 (lambda* (who message form #:optional subform)
8fad25c2
AW
2568 (arg-check (lambda (x) (or (not x) (string? x) (symbol? x)))
2569 who 'syntax-violation)
2570 (arg-check string? message 'syntax-violation)
8f1870f2 2571 (throw 'syntax-error who message
bbd1281a
MW
2572 (or (source-annotation subform)
2573 (source-annotation form))
8f1870f2
AW
2574 (strip form empty-wrap)
2575 (and subform (strip subform empty-wrap)))))
8fad25c2 2576
68fcf711
AW
2577 (let ()
2578 (define (syntax-module id)
2579 (arg-check nonsymbol-id? id 'syntax-module)
2580 (cdr (syntax-object-module id)))
2581
2582 (define (syntax-local-binding id)
2583 (arg-check nonsymbol-id? id 'syntax-local-binding)
2584 (with-transformer-environment
2585 (lambda (e r w s rib mod)
2586 (define (strip-anti-mark w)
2587 (let ((ms (wrap-marks w)) (s (wrap-subst w)))
2588 (if (and (pair? ms) (eq? (car ms) the-anti-mark))
2589 ;; output is from original text
2590 (make-wrap (cdr ms) (if rib (cons rib (cdr s)) (cdr s)))
2591 ;; output introduced by macro
2592 (make-wrap ms (if rib (cons rib s) s)))))
2593 (call-with-values (lambda ()
2594 (resolve-identifier
2595 (syntax-object-expression id)
2596 (strip-anti-mark (syntax-object-wrap id))
2597 r
2598 (syntax-object-module id)))
2599 (lambda (type value mod)
2600 (case type
2601 ((lexical) (values 'lexical value))
2602 ((macro) (values 'macro value))
2603 ((syntax) (values 'pattern-variable value))
2604 ((displaced-lexical) (values 'displaced-lexical #f))
2605 ((global) (values 'global (cons value (cdr mod))))
2606 (else (values 'other #f))))))))
2607
2608 (define (syntax-locally-bound-identifiers id)
2609 (arg-check nonsymbol-id? id 'syntax-locally-bound-identifiers)
2610 (locally-bound-identifiers (syntax-object-wrap id)
2611 (syntax-object-module id)))
2612
2613 ;; Using define! instead of set! to avoid warnings at
2614 ;; compile-time, after the variables are stolen away into (system
2615 ;; syntax). See the end of boot-9.scm.
2616 ;;
2617 (define! 'syntax-module syntax-module)
2618 (define! 'syntax-local-binding syntax-local-binding)
2619 (define! 'syntax-locally-bound-identifiers syntax-locally-bound-identifiers))
2620
8fad25c2
AW
2621 ;; $sc-dispatch expects an expression and a pattern. If the expression
2622 ;; matches the pattern a list of the matching expressions for each
2623 ;; "any" is returned. Otherwise, #f is returned. (This use of #f will
2624 ;; not work on r4rs implementations that violate the ieee requirement
2625 ;; that #f and () be distinct.)
2626
2627 ;; The expression is matched with the pattern as follows:
2628
2629 ;; pattern: matches:
2630 ;; () empty list
2631 ;; any anything
2632 ;; (<pattern>1 . <pattern>2) (<pattern>1 . <pattern>2)
2633 ;; each-any (any*)
2634 ;; #(free-id <key>) <key> with free-identifier=?
2635 ;; #(each <pattern>) (<pattern>*)
2636 ;; #(each+ p1 (p2_1 ... p2_n) p3) (p1* (p2_n ... p2_1) . p3)
2637 ;; #(vector <pattern>) (list->vector <pattern>)
2638 ;; #(atom <object>) <object> with "equal?"
2639
2640 ;; Vector cops out to pair under assumption that vectors are rare. If
2641 ;; not, should convert to:
2642 ;; #(vector <pattern>*) #(<pattern>*)
2643
2644 (let ()
2645
2646 (define match-each
2647 (lambda (e p w mod)
aa3819aa
AR
2648 (cond
2649 ((pair? e)
8fad25c2
AW
2650 (let ((first (match (car e) p w '() mod)))
2651 (and first
2652 (let ((rest (match-each (cdr e) p w mod)))
2653 (and rest (cons first rest))))))
2654 ((null? e) '())
aa3819aa 2655 ((syntax-object? e)
8fad25c2
AW
2656 (match-each (syntax-object-expression e)
2657 p
2658 (join-wraps w (syntax-object-wrap e))
2659 (syntax-object-module e)))
2660 (else #f))))
2661
2662 (define match-each+
2663 (lambda (e x-pat y-pat z-pat w r mod)
2664 (let f ((e e) (w w))
c3ae0ed4 2665 (cond
8fad25c2
AW
2666 ((pair? e)
2667 (call-with-values (lambda () (f (cdr e) w))
2668 (lambda (xr* y-pat r)
2669 (if r
2670 (if (null? y-pat)
2671 (let ((xr (match (car e) x-pat w '() mod)))
2672 (if xr
2673 (values (cons xr xr*) y-pat r)
2674 (values #f #f #f)))
2675 (values
2676 '()
2677 (cdr y-pat)
2678 (match (car e) (car y-pat) w r mod)))
2679 (values #f #f #f)))))
c3ae0ed4 2680 ((syntax-object? e)
8fad25c2
AW
2681 (f (syntax-object-expression e) (join-wraps w e)))
2682 (else
2683 (values '() y-pat (match e z-pat w r mod)))))))
2684
2685 (define match-each-any
2686 (lambda (e w mod)
2687 (cond
2688 ((pair? e)
2689 (let ((l (match-each-any (cdr e) w mod)))
2690 (and l (cons (wrap (car e) w mod) l))))
2691 ((null? e) '())
2692 ((syntax-object? e)
2693 (match-each-any (syntax-object-expression e)
2694 (join-wraps w (syntax-object-wrap e))
2695 mod))
2696 (else #f))))
2697
2698 (define match-empty
2699 (lambda (p r)
2700 (cond
2701 ((null? p) r)
2702 ((eq? p '_) r)
2703 ((eq? p 'any) (cons '() r))
2704 ((pair? p) (match-empty (car p) (match-empty (cdr p) r)))
2705 ((eq? p 'each-any) (cons '() r))
2706 (else
2707 (case (vector-ref p 0)
2708 ((each) (match-empty (vector-ref p 1) r))
2709 ((each+) (match-empty (vector-ref p 1)
2710 (match-empty
2711 (reverse (vector-ref p 2))
2712 (match-empty (vector-ref p 3) r))))
2713 ((free-id atom) r)
2714 ((vector) (match-empty (vector-ref p 1) r)))))))
2715
2716 (define combine
2717 (lambda (r* r)
2718 (if (null? (car r*))
2719 r
2720 (cons (map car r*) (combine (map cdr r*) r)))))
2721
2722 (define match*
2723 (lambda (e p w r mod)
2724 (cond
2725 ((null? p) (and (null? e) r))
2726 ((pair? p)
2727 (and (pair? e) (match (car e) (car p) w
2728 (match (cdr e) (cdr p) w r mod)
2729 mod)))
2730 ((eq? p 'each-any)
2731 (let ((l (match-each-any e w mod))) (and l (cons l r))))
2732 (else
2733 (case (vector-ref p 0)
2734 ((each)
2735 (if (null? e)
2736 (match-empty (vector-ref p 1) r)
2737 (let ((l (match-each e (vector-ref p 1) w mod)))
2738 (and l
2739 (let collect ((l l))
2740 (if (null? (car l))
2741 r
2742 (cons (map car l) (collect (map cdr l)))))))))
2743 ((each+)
2744 (call-with-values
2745 (lambda ()
2746 (match-each+ e (vector-ref p 1) (vector-ref p 2) (vector-ref p 3) w r mod))
2747 (lambda (xr* y-pat r)
2748 (and r
2749 (null? y-pat)
2750 (if (null? xr*)
2751 (match-empty (vector-ref p 1) r)
2752 (combine xr* r))))))
2753 ((free-id) (and (id? e) (free-id=? (wrap e w mod) (vector-ref p 1)) r))
2754 ((atom) (and (equal? (vector-ref p 1) (strip e w)) r))
2755 ((vector)
2756 (and (vector? e)
2757 (match (vector->list e) (vector-ref p 1) w r mod))))))))
2758
2759 (define match
2760 (lambda (e p w r mod)
2761 (cond
2762 ((not r) #f)
2763 ((eq? p '_) r)
2764 ((eq? p 'any) (cons (wrap e w mod) r))
2765 ((syntax-object? e)
2766 (match*
2767 (syntax-object-expression e)
2768 p
2769 (join-wraps w (syntax-object-wrap e))
2770 r
2771 (syntax-object-module e)))
2772 (else (match* e p w r mod)))))
2773
2774 (set! $sc-dispatch
2775 (lambda (e p)
2776 (cond
2777 ((eq? p 'any) (list e))
2778 ((eq? p '_) '())
2779 ((syntax-object? e)
2780 (match* (syntax-object-expression e)
2781 p (syntax-object-wrap e) '() (syntax-object-module e)))
2782 (else (match* e p empty-wrap '() #f))))))))
80f225df 2783
a63812a2
JB
2784
2785(define-syntax with-syntax
2786 (lambda (x)
2787 (syntax-case x ()
2788 ((_ () e1 e2 ...)
f929b9e5 2789 #'(let () e1 e2 ...))
a63812a2 2790 ((_ ((out in)) e1 e2 ...)
f929b9e5
AW
2791 #'(syntax-case in ()
2792 (out (let () e1 e2 ...))))
a63812a2 2793 ((_ ((out in) ...) e1 e2 ...)
c3ae0ed4 2794 #'(syntax-case (list in ...) ()
f929b9e5 2795 ((out ...) (let () e1 e2 ...)))))))
a63812a2
JB
2796
2797(define-syntax syntax-rules
1af6d2a7
MW
2798 (lambda (xx)
2799 (syntax-case xx ()
a63812a2 2800 ((_ (k ...) ((keyword . pattern) template) ...)
c3ae0ed4 2801 #'(lambda (x)
a5e95abe 2802 ;; embed patterns as procedure metadata
44d65b23
AW
2803 #((macro-type . syntax-rules)
2804 (patterns pattern ...))
2805 (syntax-case x (k ...)
8b22ced1 2806 ((dummy . pattern) #'template)
44d65b23
AW
2807 ...)))
2808 ((_ (k ...) docstring ((keyword . pattern) template) ...)
2809 (string? (syntax->datum #'docstring))
2810 #'(lambda (x)
2811 ;; the same, but allow a docstring
2812 docstring
a5e95abe
AW
2813 #((macro-type . syntax-rules)
2814 (patterns pattern ...))
c3ae0ed4 2815 (syntax-case x (k ...)
8b22ced1 2816 ((dummy . pattern) #'template)
c3ae0ed4 2817 ...))))))
a63812a2 2818
dea14eb9
AW
2819(define-syntax define-syntax-rule
2820 (lambda (x)
2821 (syntax-case x ()
2822 ((_ (name . pattern) template)
2823 #'(define-syntax name
2824 (syntax-rules ()
8b22ced1 2825 ((_ . pattern) template))))
dea14eb9
AW
2826 ((_ (name . pattern) docstring template)
2827 (string? (syntax->datum #'docstring))
2828 #'(define-syntax name
2829 (syntax-rules ()
2830 docstring
8b22ced1 2831 ((_ . pattern) template)))))))
dea14eb9 2832
a63812a2
JB
2833(define-syntax let*
2834 (lambda (x)
2835 (syntax-case x ()
2836 ((let* ((x v) ...) e1 e2 ...)
c3ae0ed4
AW
2837 (and-map identifier? #'(x ...))
2838 (let f ((bindings #'((x v) ...)))
a63812a2 2839 (if (null? bindings)
c3ae0ed4 2840 #'(let () e1 e2 ...)
a63812a2
JB
2841 (with-syntax ((body (f (cdr bindings)))
2842 (binding (car bindings)))
c3ae0ed4 2843 #'(let (binding) body))))))))
a63812a2 2844
a63812a2 2845(define-syntax quasiquote
0f550375
AW
2846 (let ()
2847 (define (quasi p lev)
2848 (syntax-case p (unquote quasiquote)
2849 ((unquote p)
2850 (if (= lev 0)
2851 #'("value" p)
2852 (quasicons #'("quote" unquote) (quasi #'(p) (- lev 1)))))
2853 ((quasiquote p) (quasicons #'("quote" quasiquote) (quasi #'(p) (+ lev 1))))
2854 ((p . q)
2855 (syntax-case #'p (unquote unquote-splicing)
2856 ((unquote p ...)
2857 (if (= lev 0)
2858 (quasilist* #'(("value" p) ...) (quasi #'q lev))
2859 (quasicons
2860 (quasicons #'("quote" unquote) (quasi #'(p ...) (- lev 1)))
2861 (quasi #'q lev))))
2862 ((unquote-splicing p ...)
2863 (if (= lev 0)
2864 (quasiappend #'(("value" p) ...) (quasi #'q lev))
2865 (quasicons
2866 (quasicons #'("quote" unquote-splicing) (quasi #'(p ...) (- lev 1)))
2867 (quasi #'q lev))))
2868 (_ (quasicons (quasi #'p lev) (quasi #'q lev)))))
2869 (#(x ...) (quasivector (vquasi #'(x ...) lev)))
2870 (p #'("quote" p))))
2871 (define (vquasi p lev)
2872 (syntax-case p ()
2873 ((p . q)
2874 (syntax-case #'p (unquote unquote-splicing)
2875 ((unquote p ...)
2876 (if (= lev 0)
2877 (quasilist* #'(("value" p) ...) (vquasi #'q lev))
2878 (quasicons
2879 (quasicons #'("quote" unquote) (quasi #'(p ...) (- lev 1)))
2880 (vquasi #'q lev))))
2881 ((unquote-splicing p ...)
2882 (if (= lev 0)
2883 (quasiappend #'(("value" p) ...) (vquasi #'q lev))
2884 (quasicons
2885 (quasicons
2886 #'("quote" unquote-splicing)
2887 (quasi #'(p ...) (- lev 1)))
2888 (vquasi #'q lev))))
2889 (_ (quasicons (quasi #'p lev) (vquasi #'q lev)))))
2890 (() #'("quote" ()))))
2891 (define (quasicons x y)
2892 (with-syntax ((x x) (y y))
2893 (syntax-case #'y ()
2894 (("quote" dy)
2895 (syntax-case #'x ()
2896 (("quote" dx) #'("quote" (dx . dy)))
2897 (_ (if (null? #'dy) #'("list" x) #'("list*" x y)))))
2898 (("list" . stuff) #'("list" x . stuff))
2899 (("list*" . stuff) #'("list*" x . stuff))
2900 (_ #'("list*" x y)))))
2901 (define (quasiappend x y)
2902 (syntax-case y ()
2903 (("quote" ())
2904 (cond
2905 ((null? x) #'("quote" ()))
2906 ((null? (cdr x)) (car x))
2907 (else (with-syntax (((p ...) x)) #'("append" p ...)))))
2908 (_
2909 (cond
2910 ((null? x) y)
2911 (else (with-syntax (((p ...) x) (y y)) #'("append" p ... y)))))))
2912 (define (quasilist* x y)
2913 (let f ((x x))
2914 (if (null? x)
2915 y
2916 (quasicons (car x) (f (cdr x))))))
2917 (define (quasivector x)
2918 (syntax-case x ()
2919 (("quote" (x ...)) #'("quote" #(x ...)))
2920 (_
2921 (let f ((y x) (k (lambda (ls) #`("vector" #,@ls))))
2922 (syntax-case y ()
2923 (("quote" (y ...)) (k #'(("quote" y) ...)))
2924 (("list" y ...) (k #'(y ...)))
2925 (("list*" y ... z) (f #'z (lambda (ls) (k (append #'(y ...) ls)))))
2926 (else #`("list->vector" #,x)))))))
2927 (define (emit x)
2928 (syntax-case x ()
2929 (("quote" x) #''x)
2930 (("list" x ...) #`(list #,@(map emit #'(x ...))))
2931 ;; could emit list* for 3+ arguments if implementation supports
2932 ;; list*
2933 (("list*" x ... y)
2934 (let f ((x* #'(x ...)))
2935 (if (null? x*)
2936 (emit #'y)
2937 #`(cons #,(emit (car x*)) #,(f (cdr x*))))))
2938 (("append" x ...) #`(append #,@(map emit #'(x ...))))
2939 (("vector" x ...) #`(vector #,@(map emit #'(x ...))))
2940 (("list->vector" x) #`(list->vector #,(emit #'x)))
2941 (("value" x) #'x)))
a63812a2 2942 (lambda (x)
0f550375
AW
2943 (syntax-case x ()
2944 ;; convert to intermediate language, combining introduced (but
2945 ;; not unquoted source) quote expressions where possible and
2946 ;; choosing optimal construction code otherwise, then emit
2947 ;; Scheme code corresponding to the intermediate language forms.
2948 ((_ e) (emit (quasi #'e 0)))))))
a63812a2
JB
2949
2950(define-syntax include
2951 (lambda (x)
2952 (define read-file
84f5a825 2953 (lambda (fn dir k)
9a334eb3
MW
2954 (let* ((p (open-input-file
2955 (if (absolute-file-name? fn)
2956 fn
2957 (in-vicinity dir fn))))
2958 (enc (file-encoding p)))
2959
2960 ;; Choose the input encoding deterministically.
2961 (set-port-encoding! p (or enc "UTF-8"))
2962
df0f5295
LC
2963 (let f ((x (read p))
2964 (result '()))
a63812a2 2965 (if (eof-object? x)
df0f5295
LC
2966 (begin
2967 (close-input-port p)
2968 (reverse result))
2969 (f (read p)
2970 (cons (datum->syntax k x) result)))))))
84f5a825
AW
2971 (let* ((src (syntax-source x))
2972 (file (and src (assq-ref src 'filename)))
2973 (dir (and (string? file) (dirname file))))
2974 (syntax-case x ()
2975 ((k filename)
2976 (let ((fn (syntax->datum #'filename)))
2977 (with-syntax (((exp ...) (read-file fn dir #'filename)))
2978 #'(begin exp ...))))))))
a63812a2 2979
d89fae24
AW
2980(define-syntax include-from-path
2981 (lambda (x)
2982 (syntax-case x ()
2983 ((k filename)
2984 (let ((fn (syntax->datum #'filename)))
9846796b
AW
2985 (with-syntax ((fn (datum->syntax
2986 #'filename
2987 (or (%search-load-path fn)
2988 (syntax-violation 'include-from-path
2989 "file not found in path"
2990 x #'filename)))))
d89fae24
AW
2991 #'(include fn)))))))
2992
a63812a2 2993(define-syntax unquote
6a952e0e 2994 (lambda (x)
0f550375
AW
2995 (syntax-violation 'unquote
2996 "expression not valid outside of quasiquote"
2997 x)))
a63812a2
JB
2998
2999(define-syntax unquote-splicing
6a952e0e 3000 (lambda (x)
0f550375
AW
3001 (syntax-violation 'unquote-splicing
3002 "expression not valid outside of quasiquote"
3003 x)))
a63812a2 3004
bfccdcd5
AW
3005(define (make-variable-transformer proc)
3006 (if (procedure? proc)
3007 (let ((trans (lambda (x)
3008 #((macro-type . variable-transformer))
3009 (proc x))))
3010 (set-procedure-property! trans 'variable-transformer #t)
3011 trans)
3012 (error "variable transformer not a procedure" proc)))
3013
a63812a2 3014(define-syntax identifier-syntax
1af6d2a7
MW
3015 (lambda (xx)
3016 (syntax-case xx (set!)
a63812a2 3017 ((_ e)
c3ae0ed4 3018 #'(lambda (x)
a5e95abe 3019 #((macro-type . identifier-syntax))
a63812a2
JB
3020 (syntax-case x ()
3021 (id
c3ae0ed4
AW
3022 (identifier? #'id)
3023 #'e)
a63812a2 3024 ((_ x (... ...))
bfccdcd5
AW
3025 #'(e x (... ...))))))
3026 ((_ (id exp1) ((set! var val) exp2))
3027 (and (identifier? #'id) (identifier? #'var))
3028 #'(make-variable-transformer
3029 (lambda (x)
3030 #((macro-type . variable-transformer))
3031 (syntax-case x (set!)
3032 ((set! var val) #'exp2)
3033 ((id x (... ...)) #'(exp1 x (... ...)))
3034 (id (identifier? #'id) #'exp1))))))))
97bc28b6
AW
3035
3036(define-syntax define*
64fa96ef
AW
3037 (lambda (x)
3038 (syntax-case x ()
3039 ((_ (id . args) b0 b1 ...)
3040 #'(define id (lambda* args b0 b1 ...)))
9120f130 3041 ((_ id val) (identifier? #'id)
64fa96ef 3042 #'(define id val)))))