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