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