fix bug in memoize
[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
d71cc612
AW
1244 (let ((id (car e)))
1245 (build-lexical-reference 'fun (source-annotation id)
1246 (if (syntax-object? id)
1247 (syntax->datum id)
1248 id)
1249 value))
c3ae0ed4
AW
1250 e r w s mod))
1251 ((global-call)
1252 (chi-application
1253 (build-global-reference (source-annotation (car e))
1254 (if (syntax-object? value)
1255 (syntax-object-expression value)
1256 value)
1257 (if (syntax-object? value)
1258 (syntax-object-module value)
1259 mod))
1260 e r w s mod))
1261 ((constant) (build-data s (strip (source-wrap e w s mod) empty-wrap)))
1262 ((global) (build-global-reference s value mod))
1263 ((call) (chi-application (chi (car e) r w mod) e r w s mod))
1264 ((begin-form)
1265 (syntax-case e ()
1266 ((_ e1 e2 ...) (chi-sequence #'(e1 e2 ...) r w s mod))))
1267 ((local-syntax-form)
1268 (chi-local-syntax value e r w s mod chi-sequence))
1269 ((eval-when-form)
1270 (syntax-case e ()
1271 ((_ (x ...) e1 e2 ...)
1272 (let ((when-list (chi-when-list e #'(x ...) w)))
1273 (if (memq 'eval when-list)
1274 (chi-sequence #'(e1 e2 ...) r w s mod)
1275 (chi-void))))))
1276 ((define-form define-syntax-form)
1277 (syntax-violation #f "definition in expression context"
1278 e (wrap value w mod)))
1279 ((syntax)
1280 (syntax-violation #f "reference to pattern variable outside syntax form"
1281 (source-wrap e w s mod)))
1282 ((displaced-lexical)
1283 (syntax-violation #f "reference to identifier outside its scope"
1284 (source-wrap e w s mod)))
1285 (else (syntax-violation #f "unexpected syntax"
1286 (source-wrap e w s mod))))))
1287
1288 (define chi-application
1289 (lambda (x e r w s mod)
1290 (syntax-case e ()
1291 ((e0 e1 ...)
1292 (build-application s x
1293 (map (lambda (e) (chi e r w mod)) #'(e1 ...)))))))
1294
9a749209
AW
1295 ;; (What follows is my interpretation of what's going on here -- Andy)
1296 ;;
1297 ;; A macro takes an expression, a tree, the leaves of which are identifiers
1298 ;; and datums. Identifiers are symbols along with a wrap and a module. For
1299 ;; efficiency, subtrees that share wraps and modules may be grouped as one
1300 ;; syntax object.
1301 ;;
1302 ;; Going into the expansion, the expression is given an anti-mark, which
1303 ;; logically propagates to all leaves. Then, in the new expression returned
1304 ;; from the transfomer, if we see an expression with an anti-mark, we know it
1305 ;; pertains to the original expression; conversely, expressions without the
1306 ;; anti-mark are known to be introduced by the transformer.
1307 ;;
1308 ;; OK, good until now. We know this algorithm does lexical scoping
1309 ;; appropriately because it's widely known in the literature, and psyntax is
1310 ;; widely used. But what about modules? Here we're on our own. What we do is
1311 ;; to mark the module of expressions produced by a macro as pertaining to the
1312 ;; module that was current when the macro was defined -- that is, free
1313 ;; identifiers introduced by a macro are scoped in the macro's module, not in
1314 ;; the expansion's module. Seems to work well.
1315 ;;
1316 ;; The only wrinkle is when we want a macro to expand to code in another
1317 ;; module, as is the case for the r6rs `library' form -- the body expressions
1318 ;; should be scoped relative the the new module, the one defined by the macro.
1319 ;; For that, use `(@@ mod-name body)'.
a96434cc
AW
1320 ;;
1321 ;; Part of the macro output will be from the site of the macro use and part
1322 ;; from the macro definition. We allow source information from the macro use
1323 ;; to pass through, but we annotate the parts coming from the macro with the
1324 ;; source location information corresponding to the macro use. It would be
1325 ;; really nice if we could also annotate introduced expressions with the
1326 ;; locations corresponding to the macro definition, but that is not yet
1327 ;; possible.
c3ae0ed4 1328 (define chi-macro
a96434cc 1329 (lambda (p e r w s rib mod)
5f161164 1330 ;; p := (procedure . module-name)
c3ae0ed4
AW
1331 (define rebuild-macro-output
1332 (lambda (x m)
1333 (cond ((pair? x)
a96434cc
AW
1334 (decorate-source
1335 (cons (rebuild-macro-output (car x) m)
1336 (rebuild-macro-output (cdr x) m))
1337 s))
c3ae0ed4
AW
1338 ((syntax-object? x)
1339 (let ((w (syntax-object-wrap x)))
1340 (let ((ms (wrap-marks w)) (s (wrap-subst w)))
1341 (if (and (pair? ms) (eq? (car ms) the-anti-mark))
1342 ;; output is from original text
1343 (make-syntax-object
1344 (syntax-object-expression x)
1345 (make-wrap (cdr ms) (if rib (cons rib (cdr s)) (cdr s)))
1346 (syntax-object-module x))
1347 ;; output introduced by macro
1348 (make-syntax-object
a96434cc 1349 (decorate-source (syntax-object-expression x) s)
c3ae0ed4
AW
1350 (make-wrap (cons m ms)
1351 (if rib
1352 (cons rib (cons 'shift s))
1353 (cons 'shift s)))
9846796b 1354 (syntax-object-module x))))))
5f161164 1355
c3ae0ed4 1356 ((vector? x)
a96434cc
AW
1357 (let* ((n (vector-length x))
1358 (v (decorate-source (make-vector n) x)))
c3ae0ed4
AW
1359 (do ((i 0 (fx+ i 1)))
1360 ((fx= i n) v)
a63812a2 1361 (vector-set! v i
c3ae0ed4
AW
1362 (rebuild-macro-output (vector-ref x i) m)))))
1363 ((symbol? x)
1364 (syntax-violation #f "encountered raw symbol in macro output"
1365 (source-wrap e w (wrap-subst w) mod) x))
a96434cc 1366 (else (decorate-source x s)))))
f42d8bd8 1367 (rebuild-macro-output (p (source-wrap e (anti-mark w) s mod))
a96434cc 1368 (new-mark))))
c3ae0ed4
AW
1369
1370 (define chi-body
1371 ;; In processing the forms of the body, we create a new, empty wrap.
1372 ;; This wrap is augmented (destructively) each time we discover that
1373 ;; the next form is a definition. This is done:
1374 ;;
1375 ;; (1) to allow the first nondefinition form to be a call to
1376 ;; one of the defined ids even if the id previously denoted a
1377 ;; definition keyword or keyword for a macro expanding into a
1378 ;; definition;
1379 ;; (2) to prevent subsequent definition forms (but unfortunately
1380 ;; not earlier ones) and the first nondefinition form from
1381 ;; confusing one of the bound identifiers for an auxiliary
1382 ;; keyword; and
1383 ;; (3) so that we do not need to restart the expansion of the
1384 ;; first nondefinition form, which is problematic anyway
1385 ;; since it might be the first element of a begin that we
1386 ;; have just spliced into the body (meaning if we restarted,
1387 ;; we'd really need to restart with the begin or the macro
1388 ;; call that expanded into the begin, and we'd have to give
1389 ;; up allowing (begin <defn>+ <expr>+), which is itself
1390 ;; problematic since we don't know if a begin contains only
1391 ;; definitions until we've expanded it).
1392 ;;
1393 ;; Before processing the body, we also create a new environment
1394 ;; containing a placeholder for the bindings we will add later and
1395 ;; associate this environment with each form. In processing a
1396 ;; let-syntax or letrec-syntax, the associated environment may be
1397 ;; augmented with local keyword bindings, so the environment may
1398 ;; be different for different forms in the body. Once we have
1399 ;; gathered up all of the definitions, we evaluate the transformer
1400 ;; expressions and splice into r at the placeholder the new variable
1401 ;; and keyword bindings. This allows let-syntax or letrec-syntax
1402 ;; forms local to a portion or all of the body to shadow the
1403 ;; definition bindings.
1404 ;;
1405 ;; Subforms of a begin, let-syntax, or letrec-syntax are spliced
1406 ;; into the body.
1407 ;;
1408 ;; outer-form is fully wrapped w/source
1409 (lambda (body outer-form r w mod)
1410 (let* ((r (cons '("placeholder" . (placeholder)) r))
1411 (ribcage (make-empty-ribcage))
1412 (w (make-wrap (wrap-marks w) (cons ribcage (wrap-subst w)))))
1413 (let parse ((body (map (lambda (x) (cons r (wrap x w mod))) body))
1414 (ids '()) (labels '())
1415 (var-ids '()) (vars '()) (vals '()) (bindings '()))
1416 (if (null? body)
1417 (syntax-violation #f "no expressions in body" outer-form)
1418 (let ((e (cdar body)) (er (caar body)))
1419 (call-with-values
1420 (lambda () (syntax-type e er empty-wrap (source-annotation er) ribcage mod #f))
1421 (lambda (type value e w s mod)
1422 (case type
1423 ((define-form)
1424 (let ((id (wrap value w mod)) (label (gen-label)))
1425 (let ((var (gen-var id)))
1426 (extend-ribcage! ribcage id label)
1427 (parse (cdr body)
1428 (cons id ids) (cons label labels)
1429 (cons id var-ids)
1430 (cons var vars) (cons (cons er (wrap e w mod)) vals)
1431 (cons (make-binding 'lexical var) bindings)))))
1432 ((define-syntax-form)
1433 (let ((id (wrap value w mod)) (label (gen-label)))
a63812a2
JB
1434 (extend-ribcage! ribcage id label)
1435 (parse (cdr body)
c3ae0ed4
AW
1436 (cons id ids) (cons label labels)
1437 var-ids vars vals
1438 (cons (make-binding 'macro (cons er (wrap e w mod)))
1439 bindings))))
1440 ((begin-form)
1441 (syntax-case e ()
1442 ((_ e1 ...)
1443 (parse (let f ((forms #'(e1 ...)))
1444 (if (null? forms)
1445 (cdr body)
1446 (cons (cons er (wrap (car forms) w mod))
1447 (f (cdr forms)))))
1448 ids labels var-ids vars vals bindings))))
1449 ((local-syntax-form)
1450 (chi-local-syntax value e er w s mod
1451 (lambda (forms er w s mod)
1452 (parse (let f ((forms forms))
1453 (if (null? forms)
1454 (cdr body)
1455 (cons (cons er (wrap (car forms) w mod))
1456 (f (cdr forms)))))
1457 ids labels var-ids vars vals bindings))))
1458 (else ; found a non-definition
1459 (if (null? ids)
1460 (build-sequence no-source
1461 (map (lambda (x)
1462 (chi (cdr x) (car x) empty-wrap mod))
1463 (cons (cons er (source-wrap e w s mod))
1464 (cdr body))))
1465 (begin
1466 (if (not (valid-bound-ids? ids))
1467 (syntax-violation
1468 #f "invalid or duplicate identifier in definition"
1469 outer-form))
1470 (let loop ((bs bindings) (er-cache #f) (r-cache #f))
1471 (if (not (null? bs))
1472 (let* ((b (car bs)))
1473 (if (eq? (car b) 'macro)
1474 (let* ((er (cadr b))
1475 (r-cache
1476 (if (eq? er er-cache)
1477 r-cache
1478 (macros-only-env er))))
1479 (set-cdr! b
1480 (eval-local-transformer
1481 (chi (cddr b) r-cache empty-wrap mod)
1482 mod))
1483 (loop (cdr bs) er r-cache))
1484 (loop (cdr bs) er-cache r-cache)))))
1485 (set-cdr! r (extend-env labels bindings (cdr r)))
1486 (build-letrec no-source
1487 (map syntax->datum var-ids)
1488 vars
1489 (map (lambda (x)
1490 (chi (cdr x) (car x) empty-wrap mod))
1491 vals)
1492 (build-sequence no-source
1493 (map (lambda (x)
1494 (chi (cdr x) (car x) empty-wrap mod))
1495 (cons (cons er (source-wrap e w s mod))
1496 (cdr body)))))))))))))))))
1497
1498 (define chi-local-syntax
1499 (lambda (rec? e r w s mod k)
1500 (syntax-case e ()
1501 ((_ ((id val) ...) e1 e2 ...)
1502 (let ((ids #'(id ...)))
1503 (if (not (valid-bound-ids? ids))
1504 (syntax-violation #f "duplicate bound keyword" e)
1505 (let ((labels (gen-labels ids)))
1506 (let ((new-w (make-binding-wrap ids labels w)))
1507 (k #'(e1 e2 ...)
1508 (extend-env
1509 labels
1510 (let ((w (if rec? new-w w))
1511 (trans-r (macros-only-env r)))
1512 (map (lambda (x)
1513 (make-binding 'macro
1514 (eval-local-transformer
1515 (chi x trans-r w mod)
1516 mod)))
1517 #'(val ...)))
1518 r)
1519 new-w
1520 s
1521 mod))))))
1522 (_ (syntax-violation #f "bad local syntax definition"
1523 (source-wrap e w s mod))))))
1524
1525 (define eval-local-transformer
1526 (lambda (expanded mod)
1527 (let ((p (local-eval-hook expanded mod)))
1528 (if (procedure? p)
f42d8bd8 1529 p
c3ae0ed4
AW
1530 (syntax-violation #f "nonprocedure transformer" p)))))
1531
1532 (define chi-void
1533 (lambda ()
1534 (build-void no-source)))
1535
1536 (define ellipsis?
1537 (lambda (x)
1538 (and (nonsymbol-id? x)
1539 (free-id=? x #'(... ...)))))
1540
1541 (define lambda-formals
1542 (lambda (orig-args)
1543 (define (req args rreq)
1544 (syntax-case args ()
1545 (()
1546 (check (reverse rreq) #f))
1547 ((a . b) (id? #'a)
1548 (req #'b (cons #'a rreq)))
1549 (r (id? #'r)
1550 (check (reverse rreq) #'r))
1551 (else
1552 (syntax-violation 'lambda "invalid argument list" orig-args args))))
1553 (define (check req rest)
1554 (cond
1555 ((distinct-bound-ids? (if rest (cons rest req) req))
1e2a8edb 1556 (values req #f rest #f))
c3ae0ed4
AW
1557 (else
1558 (syntax-violation 'lambda "duplicate identifier in argument list"
1559 orig-args))))
1560 (req orig-args '())))
1561
1562 (define chi-simple-lambda
3785c5b2 1563 (lambda (e r w s mod req rest meta body)
c3ae0ed4
AW
1564 (let* ((ids (if rest (append req (list rest)) req))
1565 (vars (map gen-var ids))
1566 (labels (gen-labels ids)))
1567 (build-simple-lambda
1568 s
1569 (map syntax->datum req) (and rest (syntax->datum rest)) vars
3785c5b2 1570 meta
c3ae0ed4
AW
1571 (chi-body body (source-wrap e w s mod)
1572 (extend-var-env labels vars r)
1573 (make-binding-wrap ids labels w)
1574 mod)))))
1575
1576 (define lambda*-formals
1577 (lambda (orig-args)
1578 (define (req args rreq)
1579 (syntax-case args ()
1580 (()
1e2a8edb 1581 (check (reverse rreq) '() #f '()))
c3ae0ed4
AW
1582 ((a . b) (id? #'a)
1583 (req #'b (cons #'a rreq)))
1584 ((a . b) (eq? (syntax->datum #'a) #:optional)
1585 (opt #'b (reverse rreq) '()))
1586 ((a . b) (eq? (syntax->datum #'a) #:key)
1587 (key #'b (reverse rreq) '() '()))
c3ae0ed4 1588 ((a b) (eq? (syntax->datum #'a) #:rest)
1e2a8edb 1589 (rest #'b (reverse rreq) '() '()))
c3ae0ed4 1590 (r (id? #'r)
1e2a8edb 1591 (rest #'r (reverse rreq) '() '()))
c3ae0ed4
AW
1592 (else
1593 (syntax-violation 'lambda* "invalid argument list" orig-args args))))
1594 (define (opt args req ropt)
1595 (syntax-case args ()
1596 (()
1e2a8edb 1597 (check req (reverse ropt) #f '()))
c3ae0ed4
AW
1598 ((a . b) (id? #'a)
1599 (opt #'b req (cons #'(a #f) ropt)))
1600 (((a init) . b) (id? #'a)
1601 (opt #'b req (cons #'(a init) ropt)))
1602 ((a . b) (eq? (syntax->datum #'a) #:key)
1603 (key #'b req (reverse ropt) '()))
c3ae0ed4 1604 ((a b) (eq? (syntax->datum #'a) #:rest)
1e2a8edb 1605 (rest #'b req (reverse ropt) '()))
c3ae0ed4 1606 (r (id? #'r)
1e2a8edb 1607 (rest #'r req (reverse ropt) '()))
c3ae0ed4
AW
1608 (else
1609 (syntax-violation 'lambda* "invalid optional argument list"
1610 orig-args args))))
1611 (define (key args req opt rkey)
1612 (syntax-case args ()
1613 (()
1e2a8edb 1614 (check req opt #f (cons #f (reverse rkey))))
c3ae0ed4
AW
1615 ((a . b) (id? #'a)
1616 (with-syntax ((k (symbol->keyword (syntax->datum #'a))))
1617 (key #'b req opt (cons #'(k a #f) rkey))))
1618 (((a init) . b) (id? #'a)
1619 (with-syntax ((k (symbol->keyword (syntax->datum #'a))))
1620 (key #'b req opt (cons #'(k a init) rkey))))
1621 (((a init k) . b) (and (id? #'a)
1622 (keyword? (syntax->datum #'k)))
1623 (key #'b req opt (cons #'(k a init) rkey)))
1624 ((aok) (eq? (syntax->datum #'aok) #:allow-other-keys)
1e2a8edb 1625 (check req opt #f (cons #t (reverse rkey))))
c3ae0ed4
AW
1626 ((aok a b) (and (eq? (syntax->datum #'aok) #:allow-other-keys)
1627 (eq? (syntax->datum #'a) #:rest))
1e2a8edb 1628 (rest #'b req opt (cons #t (reverse rkey))))
c3ae0ed4
AW
1629 ((aok . r) (and (eq? (syntax->datum #'aok) #:allow-other-keys)
1630 (id? #'r))
1e2a8edb 1631 (rest #'r req opt (cons #t (reverse rkey))))
c3ae0ed4 1632 ((a b) (eq? (syntax->datum #'a) #:rest)
1e2a8edb 1633 (rest #'b req opt (cons #f (reverse rkey))))
c3ae0ed4 1634 (r (id? #'r)
1e2a8edb 1635 (rest #'r req opt (cons #f (reverse rkey))))
c3ae0ed4
AW
1636 (else
1637 (syntax-violation 'lambda* "invalid keyword argument list"
1638 orig-args args))))
1e2a8edb 1639 (define (rest args req opt kw)
c3ae0ed4
AW
1640 (syntax-case args ()
1641 (r (id? #'r)
1e2a8edb 1642 (check req opt #'r kw))
c3ae0ed4
AW
1643 (else
1644 (syntax-violation 'lambda* "invalid rest argument"
1645 orig-args args))))
1e2a8edb 1646 (define (check req opt rest kw)
c3ae0ed4
AW
1647 (cond
1648 ((distinct-bound-ids?
1649 (append req (map car opt) (if rest (list rest) '())
1650 (if (pair? kw) (map cadr (cdr kw)) '())))
1e2a8edb 1651 (values req opt rest kw))
c3ae0ed4
AW
1652 (else
1653 (syntax-violation 'lambda* "duplicate identifier in argument list"
1654 orig-args))))
1655 (req orig-args '())))
1656
1657 (define chi-lambda-case
1658 (lambda (e r w s mod get-formals clauses)
1e2a8edb 1659 (define (expand-req req opt rest kw body)
c3ae0ed4
AW
1660 (let ((vars (map gen-var req))
1661 (labels (gen-labels req)))
1662 (let ((r* (extend-var-env labels vars r))
1663 (w* (make-binding-wrap req labels w)))
1664 (expand-opt (map syntax->datum req)
1e2a8edb
AW
1665 opt rest kw body (reverse vars) r* w* '() '()))))
1666 (define (expand-opt req opt rest kw body vars r* w* out inits)
c3ae0ed4
AW
1667 (cond
1668 ((pair? opt)
1669 (syntax-case (car opt) ()
1670 ((id i)
1671 (let* ((v (gen-var #'id))
1672 (l (gen-labels (list v)))
1673 (r** (extend-var-env l (list v) r*))
1674 (w** (make-binding-wrap (list #'id) l w*)))
1e2a8edb 1675 (expand-opt req (cdr opt) rest kw body (cons v vars)
c3ae0ed4
AW
1676 r** w** (cons (syntax->datum #'id) out)
1677 (cons (chi #'i r* w* mod) inits))))))
1678 (rest
1679 (let* ((v (gen-var rest))
1680 (l (gen-labels (list v)))
1681 (r* (extend-var-env l (list v) r*))
1682 (w* (make-binding-wrap (list rest) l w*)))
1683 (expand-kw req (if (pair? out) (reverse out) #f)
1684 (syntax->datum rest)
1685 (if (pair? kw) (cdr kw) kw)
1e2a8edb 1686 body (cons v vars) r* w*
c3ae0ed4
AW
1687 (if (pair? kw) (car kw) #f)
1688 '() inits)))
1689 (else
1690 (expand-kw req (if (pair? out) (reverse out) #f) #f
c89222f8 1691 (if (pair? kw) (cdr kw) kw)
1e2a8edb 1692 body vars r* w*
c89222f8 1693 (if (pair? kw) (car kw) #f)
c3ae0ed4 1694 '() inits))))
1e2a8edb 1695 (define (expand-kw req opt rest kw body vars r* w* aok out inits)
c3ae0ed4
AW
1696 (cond
1697 ((pair? kw)
1698 (syntax-case (car kw) ()
1699 ((k id i)
1700 (let* ((v (gen-var #'id))
1701 (l (gen-labels (list v)))
1702 (r** (extend-var-env l (list v) r*))
1703 (w** (make-binding-wrap (list #'id) l w*)))
1e2a8edb 1704 (expand-kw req opt rest (cdr kw) body (cons v vars)
c3ae0ed4
AW
1705 r** w** aok
1706 (cons (list (syntax->datum #'k)
1707 (syntax->datum #'id)
1708 v)
1709 out)
1710 (cons (chi #'i r* w* mod) inits))))))
1711 (else
1e2a8edb 1712 (expand-body req opt rest
c3ae0ed4 1713 (if (or aok (pair? out)) (cons aok (reverse out)) #f)
3785c5b2
AW
1714 body (reverse vars) r* w* (reverse inits) '()))))
1715 (define (expand-body req opt rest kw body vars r* w* inits meta)
c3ae0ed4
AW
1716 (syntax-case body ()
1717 ((docstring e1 e2 ...) (string? (syntax->datum #'docstring))
3785c5b2
AW
1718 (expand-body req opt rest kw #'(e1 e2 ...) vars r* w* inits
1719 (append meta
1720 `((documentation
1721 . ,(syntax->datum #'docstring))))))
1f51e275
AW
1722 ((#((k . v) ...) e1 e2 ...)
1723 (expand-body req opt rest kw #'(e1 e2 ...) vars r* w* inits
1724 (append meta (syntax->datum #'((k . v) ...)))))
c3ae0ed4 1725 ((e1 e2 ...)
3785c5b2 1726 (values meta req opt rest kw inits vars
c3ae0ed4
AW
1727 (chi-body #'(e1 e2 ...) (source-wrap e w s mod)
1728 r* w* mod)))))
1729
1730 (syntax-case clauses ()
3785c5b2 1731 (() (values '() #f))
c3ae0ed4
AW
1732 (((args e1 e2 ...) (args* e1* e2* ...) ...)
1733 (call-with-values (lambda () (get-formals #'args))
1e2a8edb 1734 (lambda (req opt rest kw)
c3ae0ed4 1735 (call-with-values (lambda ()
1e2a8edb 1736 (expand-req req opt rest kw #'(e1 e2 ...)))
3785c5b2 1737 (lambda (meta req opt rest kw inits vars body)
c3ae0ed4
AW
1738 (call-with-values
1739 (lambda ()
1740 (chi-lambda-case e r w s mod get-formals
1741 #'((args* e1* e2* ...) ...)))
3785c5b2 1742 (lambda (meta* else*)
c3ae0ed4 1743 (values
3785c5b2 1744 (append meta meta*)
c3ae0ed4 1745 (build-lambda-case s req opt rest kw inits vars
1e2a8edb 1746 body else*))))))))))))
c89222f8 1747
a63812a2
JB
1748;;; data
1749
b40d0230
AW
1750;;; strips syntax-objects down to top-wrap
1751;;;
a63812a2
JB
1752;;; since only the head of a list is annotated by the reader, not each pair
1753;;; in the spine, we also check for pairs whose cars are annotated in case
1754;;; we've been passed the cdr of an annotated list
1755
c3ae0ed4
AW
1756 (define strip
1757 (lambda (x w)
1758 (if (top-marked? w)
1759 x
1760 (let f ((x x))
1761 (cond
1762 ((syntax-object? x)
1763 (strip (syntax-object-expression x) (syntax-object-wrap x)))
1764 ((pair? x)
1765 (let ((a (f (car x))) (d (f (cdr x))))
1766 (if (and (eq? a (car x)) (eq? d (cdr x)))
1767 x
1768 (cons a d))))
1769 ((vector? x)
1770 (let ((old (vector->list x)))
1771 (let ((new (map f old)))
1772 (if (and-map* eq? old new) x (list->vector new)))))
1773 (else x))))))
a63812a2
JB
1774
1775;;; lexical variables
1776
c3ae0ed4
AW
1777 (define gen-var
1778 (lambda (id)
1779 (let ((id (if (syntax-object? id) (syntax-object-expression id) id)))
1780 (build-lexical-var no-source id))))
a63812a2 1781
c3ae0ed4
AW
1782 ;; appears to return a reversed list
1783 (define lambda-var-list
1784 (lambda (vars)
1785 (let lvl ((vars vars) (ls '()) (w empty-wrap))
1786 (cond
4e237f14
AW
1787 ((pair? vars) (lvl (cdr vars) (cons (wrap (car vars) w #f) ls) w))
1788 ((id? vars) (cons (wrap vars w #f) ls))
a63812a2
JB
1789 ((null? vars) ls)
1790 ((syntax-object? vars)
1791 (lvl (syntax-object-expression vars)
1792 ls
1793 (join-wraps w (syntax-object-wrap vars))))
c3ae0ed4
AW
1794 ; include anything else to be caught by subsequent error
1795 ; checking
a63812a2
JB
1796 (else (cons vars ls))))))
1797
1798;;; core transformers
1799
c3ae0ed4
AW
1800 (global-extend 'local-syntax 'letrec-syntax #t)
1801 (global-extend 'local-syntax 'let-syntax #f)
1802
1803 (global-extend 'core 'fluid-let-syntax
1804 (lambda (e r w s mod)
1805 (syntax-case e ()
1806 ((_ ((var val) ...) e1 e2 ...)
1807 (valid-bound-ids? #'(var ...))
1808 (let ((names (map (lambda (x) (id-var-name x w)) #'(var ...))))
1809 (for-each
1810 (lambda (id n)
1811 (case (binding-type (lookup n r mod))
1812 ((displaced-lexical)
1813 (syntax-violation 'fluid-let-syntax
1814 "identifier out of context"
1815 e
1816 (source-wrap id w s mod)))))
1817 #'(var ...)
1818 names)
1819 (chi-body
1820 #'(e1 e2 ...)
1821 (source-wrap e w s mod)
1822 (extend-env
1823 names
1824 (let ((trans-r (macros-only-env r)))
1825 (map (lambda (x)
1826 (make-binding 'macro
1827 (eval-local-transformer (chi x trans-r w mod)
1828 mod)))
1829 #'(val ...)))
1830 r)
1831 w
1832 mod)))
1833 (_ (syntax-violation 'fluid-let-syntax "bad syntax"
1834 (source-wrap e w s mod))))))
1835
1836 (global-extend 'core 'quote
1837 (lambda (e r w s mod)
1838 (syntax-case e ()
1839 ((_ e) (build-data s (strip #'e w)))
1840 (_ (syntax-violation 'quote "bad syntax"
1841 (source-wrap e w s mod))))))
1842
1843 (global-extend 'core 'syntax
1844 (let ()
1845 (define gen-syntax
1846 (lambda (src e r maps ellipsis? mod)
1847 (if (id? e)
1848 (let ((label (id-var-name e empty-wrap)))
1849 (let ((b (lookup label r mod)))
1850 (if (eq? (binding-type b) 'syntax)
1851 (call-with-values
1852 (lambda ()
1853 (let ((var.lev (binding-value b)))
1854 (gen-ref src (car var.lev) (cdr var.lev) maps)))
1855 (lambda (var maps) (values `(ref ,var) maps)))
1856 (if (ellipsis? e)
1857 (syntax-violation 'syntax "misplaced ellipsis" src)
1858 (values `(quote ,e) maps)))))
1859 (syntax-case e ()
1860 ((dots e)
1861 (ellipsis? #'dots)
1862 (gen-syntax src #'e r maps (lambda (x) #f) mod))
1863 ((x dots . y)
1864 ; this could be about a dozen lines of code, except that we
1865 ; choose to handle #'(x ... ...) forms
1866 (ellipsis? #'dots)
1867 (let f ((y #'y)
1868 (k (lambda (maps)
1869 (call-with-values
1870 (lambda ()
1871 (gen-syntax src #'x r
1872 (cons '() maps) ellipsis? mod))
1873 (lambda (x maps)
1874 (if (null? (car maps))
1875 (syntax-violation 'syntax "extra ellipsis"
1876 src)
1877 (values (gen-map x (car maps))
1878 (cdr maps))))))))
1879 (syntax-case y ()
1880 ((dots . y)
1881 (ellipsis? #'dots)
1882 (f #'y
1883 (lambda (maps)
1884 (call-with-values
1885 (lambda () (k (cons '() maps)))
1886 (lambda (x maps)
1887 (if (null? (car maps))
1888 (syntax-violation 'syntax "extra ellipsis" src)
1889 (values (gen-mappend x (car maps))
1890 (cdr maps))))))))
1891 (_ (call-with-values
1892 (lambda () (gen-syntax src y r maps ellipsis? mod))
1893 (lambda (y maps)
1894 (call-with-values
1895 (lambda () (k maps))
1896 (lambda (x maps)
1897 (values (gen-append x y) maps)))))))))
1898 ((x . y)
1899 (call-with-values
1900 (lambda () (gen-syntax src #'x r maps ellipsis? mod))
1901 (lambda (x maps)
1902 (call-with-values
1903 (lambda () (gen-syntax src #'y r maps ellipsis? mod))
1904 (lambda (y maps) (values (gen-cons x y) maps))))))
1905 (#(e1 e2 ...)
1906 (call-with-values
1907 (lambda ()
1908 (gen-syntax src #'(e1 e2 ...) r maps ellipsis? mod))
1909 (lambda (e maps) (values (gen-vector e) maps))))
1910 (_ (values `(quote ,e) maps))))))
1911
1912 (define gen-ref
1913 (lambda (src var level maps)
1914 (if (fx= level 0)
1915 (values var maps)
1916 (if (null? maps)
1917 (syntax-violation 'syntax "missing ellipsis" src)
1918 (call-with-values
1919 (lambda () (gen-ref src var (fx- level 1) (cdr maps)))
1920 (lambda (outer-var outer-maps)
1921 (let ((b (assq outer-var (car maps))))
1922 (if b
1923 (values (cdr b) maps)
1924 (let ((inner-var (gen-var 'tmp)))
1925 (values inner-var
1926 (cons (cons (cons outer-var inner-var)
1927 (car maps))
1928 outer-maps)))))))))))
1929
1930 (define gen-mappend
1931 (lambda (e map-env)
1932 `(apply (primitive append) ,(gen-map e map-env))))
1933
1934 (define gen-map
1935 (lambda (e map-env)
1936 (let ((formals (map cdr map-env))
1937 (actuals (map (lambda (x) `(ref ,(car x))) map-env)))
1938 (cond
1939 ((eq? (car e) 'ref)
1940 ; identity map equivalence:
1941 ; (map (lambda (x) x) y) == y
1942 (car actuals))
1943 ((and-map
1944 (lambda (x) (and (eq? (car x) 'ref) (memq (cadr x) formals)))
1945 (cdr e))
1946 ; eta map equivalence:
1947 ; (map (lambda (x ...) (f x ...)) y ...) == (map f y ...)
1948 `(map (primitive ,(car e))
1949 ,@(map (let ((r (map cons formals actuals)))
1950 (lambda (x) (cdr (assq (cadr x) r))))
1951 (cdr e))))
1952 (else `(map (lambda ,formals ,e) ,@actuals))))))
1953
1954 (define gen-cons
1955 (lambda (x y)
1956 (case (car y)
1957 ((quote)
1958 (if (eq? (car x) 'quote)
1959 `(quote (,(cadr x) . ,(cadr y)))
1960 (if (eq? (cadr y) '())
1961 `(list ,x)
1962 `(cons ,x ,y))))
1963 ((list) `(list ,x ,@(cdr y)))
1964 (else `(cons ,x ,y)))))
1965
1966 (define gen-append
1967 (lambda (x y)
1968 (if (equal? y '(quote ()))
1969 x
1970 `(append ,x ,y))))
1971
1972 (define gen-vector
1973 (lambda (x)
1974 (cond
1975 ((eq? (car x) 'list) `(vector ,@(cdr x)))
1976 ((eq? (car x) 'quote) `(quote #(,@(cadr x))))
1977 (else `(list->vector ,x)))))
a63812a2 1978
c3ae0ed4
AW
1979
1980 (define regen
1981 (lambda (x)
1982 (case (car x)
1983 ((ref) (build-lexical-reference 'value no-source (cadr x) (cadr x)))
1984 ((primitive) (build-primref no-source (cadr x)))
1985 ((quote) (build-data no-source (cadr x)))
1986 ((lambda)
1987 (if (list? (cadr x))
3785c5b2 1988 (build-simple-lambda no-source (cadr x) #f (cadr x) '() (regen (caddr x)))
c3ae0ed4
AW
1989 (error "how did we get here" x)))
1990 (else (build-application no-source
1991 (build-primref no-source (car x))
1992 (map regen (cdr x)))))))
1993
1994 (lambda (e r w s mod)
1995 (let ((e (source-wrap e w s mod)))
1996 (syntax-case e ()
1997 ((_ x)
a63812a2 1998 (call-with-values
c3ae0ed4
AW
1999 (lambda () (gen-syntax e #'x r '() ellipsis? mod))
2000 (lambda (e maps) (regen e))))
2001 (_ (syntax-violation 'syntax "bad `syntax' form" e)))))))
2002
2003 (global-extend 'core 'lambda
2004 (lambda (e r w s mod)
2005 (syntax-case e ()
c3ae0ed4
AW
2006 ((_ args e1 e2 ...)
2007 (call-with-values (lambda () (lambda-formals #'args))
1e2a8edb 2008 (lambda (req opt rest kw)
3785c5b2
AW
2009 (let lp ((body #'(e1 e2 ...)) (meta '()))
2010 (syntax-case body ()
2011 ((docstring e1 e2 ...) (string? (syntax->datum #'docstring))
2012 (lp #'(e1 e2 ...)
2013 (append meta
2014 `((documentation
2015 . ,(syntax->datum #'docstring))))))
1f51e275
AW
2016 ((#((k . v) ...) e1 e2 ...)
2017 (lp #'(e1 e2 ...)
2018 (append meta (syntax->datum #'((k . v) ...)))))
3785c5b2 2019 (_ (chi-simple-lambda e r w s mod req rest meta body)))))))
c3ae0ed4 2020 (_ (syntax-violation 'lambda "bad lambda" e)))))
3785c5b2 2021
c3ae0ed4
AW
2022 (global-extend 'core 'lambda*
2023 (lambda (e r w s mod)
2024 (syntax-case e ()
2025 ((_ args e1 e2 ...)
2026 (call-with-values
2027 (lambda ()
2028 (chi-lambda-case e r w s mod
2029 lambda*-formals #'((args e1 e2 ...))))
3785c5b2
AW
2030 (lambda (meta lcase)
2031 (build-case-lambda s meta lcase))))
c3ae0ed4
AW
2032 (_ (syntax-violation 'lambda "bad lambda*" e)))))
2033
2034 (global-extend 'core 'case-lambda
2035 (lambda (e r w s mod)
2036 (syntax-case e ()
2037 ((_ (args e1 e2 ...) (args* e1* e2* ...) ...)
2038 (call-with-values
2039 (lambda ()
2040 (chi-lambda-case e r w s mod
2041 lambda-formals
2042 #'((args e1 e2 ...) (args* e1* e2* ...) ...)))
3785c5b2
AW
2043 (lambda (meta lcase)
2044 (build-case-lambda s meta lcase))))
c3ae0ed4
AW
2045 (_ (syntax-violation 'case-lambda "bad case-lambda" e)))))
2046
2047 (global-extend 'core 'case-lambda*
2048 (lambda (e r w s mod)
2049 (syntax-case e ()
2050 ((_ (args e1 e2 ...) (args* e1* e2* ...) ...)
2051 (call-with-values
2052 (lambda ()
2053 (chi-lambda-case e r w s mod
2054 lambda*-formals
2055 #'((args e1 e2 ...) (args* e1* e2* ...) ...)))
3785c5b2
AW
2056 (lambda (meta lcase)
2057 (build-case-lambda s meta lcase))))
c3ae0ed4
AW
2058 (_ (syntax-violation 'case-lambda "bad case-lambda*" e)))))
2059
2060 (global-extend 'core 'let
2061 (let ()
2062 (define (chi-let e r w s mod constructor ids vals exps)
2063 (if (not (valid-bound-ids? ids))
2064 (syntax-violation 'let "duplicate bound variable" e)
2065 (let ((labels (gen-labels ids))
2066 (new-vars (map gen-var ids)))
2067 (let ((nw (make-binding-wrap ids labels w))
2068 (nr (extend-var-env labels new-vars r)))
2069 (constructor s
2070 (map syntax->datum ids)
2071 new-vars
2072 (map (lambda (x) (chi x r w mod)) vals)
2073 (chi-body exps (source-wrap e nw s mod)
2074 nr nw mod))))))
2075 (lambda (e r w s mod)
2076 (syntax-case e ()
2077 ((_ ((id val) ...) e1 e2 ...)
2078 (and-map id? #'(id ...))
2079 (chi-let e r w s mod
2080 build-let
2081 #'(id ...)
2082 #'(val ...)
2083 #'(e1 e2 ...)))
2084 ((_ f ((id val) ...) e1 e2 ...)
2085 (and (id? #'f) (and-map id? #'(id ...)))
2086 (chi-let e r w s mod
2087 build-named-let
2088 #'(f id ...)
2089 #'(val ...)
2090 #'(e1 e2 ...)))
2091 (_ (syntax-violation 'let "bad let" (source-wrap e w s mod)))))))
2092
2093
2094 (global-extend 'core 'letrec
2095 (lambda (e r w s mod)
2096 (syntax-case e ()
2097 ((_ ((id val) ...) e1 e2 ...)
2098 (and-map id? #'(id ...))
2099 (let ((ids #'(id ...)))
2100 (if (not (valid-bound-ids? ids))
2101 (syntax-violation 'letrec "duplicate bound variable" e)
2102 (let ((labels (gen-labels ids))
2103 (new-vars (map gen-var ids)))
2104 (let ((w (make-binding-wrap ids labels w))
2105 (r (extend-var-env labels new-vars r)))
2106 (build-letrec s
2107 (map syntax->datum ids)
2108 new-vars
2109 (map (lambda (x) (chi x r w mod)) #'(val ...))
2110 (chi-body #'(e1 e2 ...)
2111 (source-wrap e w s mod) r w mod)))))))
2112 (_ (syntax-violation 'letrec "bad letrec" (source-wrap e w s mod))))))
2113
2114
2115 (global-extend 'core 'set!
2116 (lambda (e r w s mod)
2117 (syntax-case e ()
2118 ((_ id val)
2119 (id? #'id)
2120 (let ((val (chi #'val r w mod))
2121 (n (id-var-name #'id w)))
2122 (let ((b (lookup n r mod)))
2123 (case (binding-type b)
2124 ((lexical)
2125 (build-lexical-assignment s
2126 (syntax->datum #'id)
2127 (binding-value b)
2128 val))
2129 ((global) (build-global-assignment s n val mod))
2130 ((displaced-lexical)
2131 (syntax-violation 'set! "identifier out of context"
2132 (wrap #'id w mod)))
2133 (else (syntax-violation 'set! "bad set!"
2134 (source-wrap e w s mod)))))))
2135 ((_ (head tail ...) val)
2136 (call-with-values
2137 (lambda () (syntax-type #'head r empty-wrap no-source #f mod #t))
2138 (lambda (type value ee ww ss modmod)
2139 (case type
2140 ((module-ref)
2141 (let ((val (chi #'val r w mod)))
9365d8ad
AW
2142 (call-with-values (lambda () (value #'(head tail ...) r w))
2143 (lambda (e r w s* mod)
2144 (syntax-case e ()
2145 (e (id? #'e)
2146 (build-global-assignment s (syntax->datum #'e)
2147 val mod)))))))
c3ae0ed4
AW
2148 (else
2149 (build-application s
2150 (chi #'(setter head) r w mod)
2151 (map (lambda (e) (chi e r w mod))
2152 #'(tail ... val))))))))
2153 (_ (syntax-violation 'set! "bad set!" (source-wrap e w s mod))))))
2154
2155 (global-extend 'module-ref '@
9365d8ad 2156 (lambda (e r w)
c3ae0ed4
AW
2157 (syntax-case e ()
2158 ((_ (mod ...) id)
2159 (and (and-map id? #'(mod ...)) (id? #'id))
9365d8ad 2160 (values (syntax->datum #'id) r w #f
c3ae0ed4
AW
2161 (syntax->datum
2162 #'(public mod ...)))))))
2163
2164 (global-extend 'module-ref '@@
9365d8ad 2165 (lambda (e r w)
27cbec84
AW
2166 (define remodulate
2167 (lambda (x mod)
2168 (cond ((pair? x)
2169 (cons (remodulate (car x) mod)
2170 (remodulate (cdr x) mod)))
2171 ((syntax-object? x)
2172 (make-syntax-object
2173 (remodulate (syntax-object-expression x) mod)
2174 (syntax-object-wrap x)
2175 ;; hither the remodulation
2176 mod))
2177 ((vector? x)
2178 (let* ((n (vector-length x)) (v (make-vector n)))
2179 (do ((i 0 (fx+ i 1)))
2180 ((fx= i n) v)
2181 (vector-set! v i (remodulate (vector-ref x i) mod)))))
2182 (else x))))
c3ae0ed4 2183 (syntax-case e ()
27cbec84
AW
2184 ((_ (mod ...) exp)
2185 (and-map id? #'(mod ...))
2186 (let ((mod (syntax->datum #'(private mod ...))))
2187 (values (remodulate #'exp mod)
2188 r w (source-annotation #'exp)
2189 mod))))))
9365d8ad 2190
c3ae0ed4
AW
2191 (global-extend 'core 'if
2192 (lambda (e r w s mod)
2193 (syntax-case e ()
2194 ((_ test then)
2195 (build-conditional
2196 s
2197 (chi #'test r w mod)
2198 (chi #'then r w mod)
2199 (build-void no-source)))
2200 ((_ test then else)
2201 (build-conditional
2202 s
2203 (chi #'test r w mod)
2204 (chi #'then r w mod)
2205 (chi #'else r w mod))))))
2206
6360c1d4
AW
2207 (global-extend 'core 'with-fluids
2208 (lambda (e r w s mod)
2209 (syntax-case e ()
2210 ((_ ((fluid val) ...) b b* ...)
2211 (build-dynlet
2212 s
2213 (map (lambda (x) (chi x r w mod)) #'(fluid ...))
2214 (map (lambda (x) (chi x r w mod)) #'(val ...))
2215 (chi-body #'(b b* ...)
2216 (source-wrap e w s mod) r w mod))))))
2217
c3ae0ed4
AW
2218 (global-extend 'begin 'begin '())
2219
2220 (global-extend 'define 'define '())
2221
2222 (global-extend 'define-syntax 'define-syntax '())
2223
2224 (global-extend 'eval-when 'eval-when '())
2225
2226 (global-extend 'core 'syntax-case
2227 (let ()
2228 (define convert-pattern
2229 ; accepts pattern & keys
2230 ; returns $sc-dispatch pattern & ids
2231 (lambda (pattern keys)
aa3819aa
AR
2232 (define cvt*
2233 (lambda (p* n ids)
2234 (if (null? p*)
2235 (values '() ids)
2236 (call-with-values
2237 (lambda () (cvt* (cdr p*) n ids))
2238 (lambda (y ids)
2239 (call-with-values
2240 (lambda () (cvt (car p*) n ids))
2241 (lambda (x ids)
2242 (values (cons x y) ids))))))))
2243 (define cvt
2244 (lambda (p n ids)
2245 (if (id? p)
2246 (if (bound-id-member? p keys)
2247 (values (vector 'free-id p) ids)
2248 (values 'any (cons (cons p n) ids)))
2249 (syntax-case p ()
2250 ((x dots)
2251 (ellipsis? (syntax dots))
2252 (call-with-values
2253 (lambda () (cvt (syntax x) (fx+ n 1) ids))
2254 (lambda (p ids)
2255 (values (if (eq? p 'any) 'each-any (vector 'each p))
2256 ids))))
2257 ((x dots ys ...)
2258 (ellipsis? (syntax dots))
2259 (call-with-values
2260 (lambda () (cvt* (syntax (ys ...)) n ids))
2261 (lambda (ys ids)
2262 (call-with-values
2263 (lambda () (cvt (syntax x) (+ n 1) ids))
2264 (lambda (x ids)
2265 (values `#(each+ ,x ,(reverse ys) ()) ids))))))
2266 ((x . y)
2267 (call-with-values
2268 (lambda () (cvt (syntax y) n ids))
2269 (lambda (y ids)
2270 (call-with-values
2271 (lambda () (cvt (syntax x) n ids))
2272 (lambda (x ids)
2273 (values (cons x y) ids))))))
2274 (() (values '() ids))
2275 (#(x ...)
2276 (call-with-values
2277 (lambda () (cvt (syntax (x ...)) n ids))
2278 (lambda (p ids) (values (vector 'vector p) ids))))
2279 (x (values (vector 'atom (strip p empty-wrap)) ids))))))
2280 (cvt pattern 0 '())))
c3ae0ed4
AW
2281
2282 (define build-dispatch-call
2283 (lambda (pvars exp y r mod)
2284 (let ((ids (map car pvars)) (levels (map cdr pvars)))
2285 (let ((labels (gen-labels ids)) (new-vars (map gen-var ids)))
2286 (build-application no-source
2287 (build-primref no-source 'apply)
3785c5b2 2288 (list (build-simple-lambda no-source (map syntax->datum ids) #f new-vars '()
c3ae0ed4
AW
2289 (chi exp
2290 (extend-env
2291 labels
2292 (map (lambda (var level)
2293 (make-binding 'syntax `(,var . ,level)))
2294 new-vars
2295 (map cdr pvars))
2296 r)
2297 (make-binding-wrap ids labels empty-wrap)
2298 mod))
2299 y))))))
2300
2301 (define gen-clause
2302 (lambda (x keys clauses r pat fender exp mod)
2303 (call-with-values
2304 (lambda () (convert-pattern pat keys))
2305 (lambda (p pvars)
2306 (cond
2307 ((not (distinct-bound-ids? (map car pvars)))
2308 (syntax-violation 'syntax-case "duplicate pattern variable" pat))
2309 ((not (and-map (lambda (x) (not (ellipsis? (car x)))) pvars))
2310 (syntax-violation 'syntax-case "misplaced ellipsis" pat))
2311 (else
2312 (let ((y (gen-var 'tmp)))
2313 ; fat finger binding and references to temp variable y
2314 (build-application no-source
3785c5b2 2315 (build-simple-lambda no-source (list 'tmp) #f (list y) '()
c3ae0ed4
AW
2316 (let ((y (build-lexical-reference 'value no-source
2317 'tmp y)))
2318 (build-conditional no-source
2319 (syntax-case fender ()
2320 (#t y)
2321 (_ (build-conditional no-source
2322 y
2323 (build-dispatch-call pvars fender y r mod)
2324 (build-data no-source #f))))
2325 (build-dispatch-call pvars exp y r mod)
2326 (gen-syntax-case x keys clauses r mod))))
2327 (list (if (eq? p 'any)
2328 (build-application no-source
2329 (build-primref no-source 'list)
2330 (list x))
2331 (build-application no-source
2332 (build-primref no-source '$sc-dispatch)
2333 (list x (build-data no-source p)))))))))))))
2334
2335 (define gen-syntax-case
2336 (lambda (x keys clauses r mod)
2337 (if (null? clauses)
2338 (build-application no-source
2339 (build-primref no-source 'syntax-violation)
2340 (list (build-data no-source #f)
2341 (build-data no-source
2342 "source expression failed to match any pattern")
2343 x))
2344 (syntax-case (car clauses) ()
2345 ((pat exp)
2346 (if (and (id? #'pat)
2347 (and-map (lambda (x) (not (free-id=? #'pat x)))
2348 (cons #'(... ...) keys)))
2349 (let ((labels (list (gen-label)))
2350 (var (gen-var #'pat)))
2351 (build-application no-source
2352 (build-simple-lambda
2353 no-source (list (syntax->datum #'pat)) #f (list var)
3785c5b2 2354 '()
c3ae0ed4
AW
2355 (chi #'exp
2356 (extend-env labels
2357 (list (make-binding 'syntax `(,var . 0)))
2358 r)
2359 (make-binding-wrap #'(pat)
2360 labels empty-wrap)
2361 mod))
2362 (list x)))
2363 (gen-clause x keys (cdr clauses) r
2364 #'pat #t #'exp mod)))
2365 ((pat fender exp)
2366 (gen-clause x keys (cdr clauses) r
2367 #'pat #'fender #'exp mod))
2368 (_ (syntax-violation 'syntax-case "invalid clause"
2369 (car clauses)))))))
2370
2371 (lambda (e r w s mod)
2372 (let ((e (source-wrap e w s mod)))
2373 (syntax-case e ()
2374 ((_ val (key ...) m ...)
2375 (if (and-map (lambda (x) (and (id? x) (not (ellipsis? x))))
2376 #'(key ...))
2377 (let ((x (gen-var 'tmp)))
2378 ; fat finger binding and references to temp variable x
2379 (build-application s
3785c5b2 2380 (build-simple-lambda no-source (list 'tmp) #f (list x) '()
c3ae0ed4
AW
2381 (gen-syntax-case (build-lexical-reference 'value no-source
2382 'tmp x)
2383 #'(key ...) #'(m ...)
2384 r
2385 mod))
2386 (list (chi #'val r empty-wrap mod))))
2387 (syntax-violation 'syntax-case "invalid literals list" e))))))))
a63812a2 2388
8a73a6d2 2389;;; The portable macroexpand seeds chi-top's mode m with 'e (for
a63812a2
JB
2390;;; evaluating) and esew (which stands for "eval syntax expanders
2391;;; when") with '(eval). In Chez Scheme, m is set to 'c instead of e
2392;;; if we are compiling a file, and esew is set to
2393;;; (eval-syntactic-expanders-when), which defaults to the list
2394;;; '(compile load eval). This means that, by default, top-level
2395;;; syntactic definitions are evaluated immediately after they are
2396;;; expanded, and the expanded definitions are also residualized into
2397;;; the object file if we are compiling a file.
8a73a6d2 2398 (set! macroexpand
82c45730
AW
2399 (lambda* (x #:optional (m 'e) (esew '(eval)))
2400 (chi-top x null-env top-wrap m esew
2401 (cons 'hygiene (module-name (current-module))))))
2402
c3ae0ed4
AW
2403 (set! identifier?
2404 (lambda (x)
2405 (nonsymbol-id? x)))
a63812a2 2406
c3ae0ed4
AW
2407 (set! datum->syntax
2408 (lambda (id datum)
9846796b
AW
2409 (make-syntax-object datum (syntax-object-wrap id)
2410 (syntax-object-module id))))
a63812a2 2411
c3ae0ed4
AW
2412 (set! syntax->datum
2413 ; accepts any object, since syntax objects may consist partially
2414 ; or entirely of unwrapped, nonsymbolic data
2415 (lambda (x)
2416 (strip x empty-wrap)))
2417
750ae8b7
AW
2418 (set! syntax-source
2419 (lambda (x) (source-annotation x)))
2420
c3ae0ed4
AW
2421 (set! generate-temporaries
2422 (lambda (ls)
2423 (arg-check list? ls 'generate-temporaries)
2424 (map (lambda (x) (wrap (gensym-hook) top-wrap #f)) ls)))
2425
2426 (set! free-identifier=?
2427 (lambda (x y)
2428 (arg-check nonsymbol-id? x 'free-identifier=?)
2429 (arg-check nonsymbol-id? y 'free-identifier=?)
2430 (free-id=? x y)))
2431
2432 (set! bound-identifier=?
2433 (lambda (x y)
2434 (arg-check nonsymbol-id? x 'bound-identifier=?)
2435 (arg-check nonsymbol-id? y 'bound-identifier=?)
2436 (bound-id=? x y)))
2437
2438 (set! syntax-violation
2439 (lambda (who message form . subform)
2440 (arg-check (lambda (x) (or (not x) (string? x) (symbol? x)))
2441 who 'syntax-violation)
2442 (arg-check string? message 'syntax-violation)
8a73a6d2 2443 (scm-error 'syntax-error 'macroexpand
c3ae0ed4
AW
2444 (string-append
2445 (if who "~a: " "")
2446 "~a "
2447 (if (null? subform) "in ~a" "in subform `~s' of `~s'"))
2448 (let ((tail (cons message
2449 (map (lambda (x) (strip x empty-wrap))
2450 (append subform (list form))))))
2451 (if who (cons who tail) tail))
2452 #f)))
a63812a2 2453
5f1a2fb1 2454;;; $sc-dispatch expects an expression and a pattern. If the expression
a63812a2
JB
2455;;; matches the pattern a list of the matching expressions for each
2456;;; "any" is returned. Otherwise, #f is returned. (This use of #f will
2457;;; not work on r4rs implementations that violate the ieee requirement
2458;;; that #f and () be distinct.)
2459
2460;;; The expression is matched with the pattern as follows:
2461
2462;;; pattern: matches:
2463;;; () empty list
2464;;; any anything
2465;;; (<pattern>1 . <pattern>2) (<pattern>1 . <pattern>2)
2466;;; each-any (any*)
2467;;; #(free-id <key>) <key> with free-identifier=?
2468;;; #(each <pattern>) (<pattern>*)
aa3819aa 2469;;; #(each+ p1 (p2_1 ... p2_n) p3) (p1* (p2_n ... p2_1) . p3)
a63812a2
JB
2470;;; #(vector <pattern>) (list->vector <pattern>)
2471;;; #(atom <object>) <object> with "equal?"
2472
2473;;; Vector cops out to pair under assumption that vectors are rare. If
2474;;; not, should convert to:
2475;;; #(vector <pattern>*) #(<pattern>*)
2476
c3ae0ed4 2477 (let ()
a63812a2 2478
c3ae0ed4
AW
2479 (define match-each
2480 (lambda (e p w mod)
2481 (cond
2482 ((pair? e)
2483 (let ((first (match (car e) p w '() mod)))
2484 (and first
2485 (let ((rest (match-each (cdr e) p w mod)))
2486 (and rest (cons first rest))))))
2487 ((null? e) '())
2488 ((syntax-object? e)
2489 (match-each (syntax-object-expression e)
2490 p
b40d0230 2491 (join-wraps w (syntax-object-wrap e))
c3ae0ed4
AW
2492 (syntax-object-module e)))
2493 (else #f))))
a63812a2 2494
aa3819aa
AR
2495 (define match-each+
2496 (lambda (e x-pat y-pat z-pat w r mod)
2497 (let f ((e e) (w w))
2498 (cond
2499 ((pair? e)
2500 (call-with-values (lambda () (f (cdr e) w))
2501 (lambda (xr* y-pat r)
2502 (if r
2503 (if (null? y-pat)
2504 (let ((xr (match (car e) x-pat w '() mod)))
2505 (if xr
2506 (values (cons xr xr*) y-pat r)
2507 (values #f #f #f)))
2508 (values
2509 '()
2510 (cdr y-pat)
2511 (match (car e) (car y-pat) w r mod)))
2512 (values #f #f #f)))))
2513 ((syntax-object? e)
2514 (f (syntax-object-expression e) (join-wraps w e)))
2515 (else
2516 (values '() y-pat (match e z-pat w r mod)))))))
2517
c3ae0ed4
AW
2518 (define match-each-any
2519 (lambda (e w mod)
2520 (cond
2521 ((pair? e)
2522 (let ((l (match-each-any (cdr e) w mod)))
2523 (and l (cons (wrap (car e) w mod) l))))
2524 ((null? e) '())
2525 ((syntax-object? e)
2526 (match-each-any (syntax-object-expression e)
2527 (join-wraps w (syntax-object-wrap e))
2528 mod))
2529 (else #f))))
2530
2531 (define match-empty
2532 (lambda (p r)
2533 (cond
2534 ((null? p) r)
2535 ((eq? p 'any) (cons '() r))
2536 ((pair? p) (match-empty (car p) (match-empty (cdr p) r)))
2537 ((eq? p 'each-any) (cons '() r))
2538 (else
2539 (case (vector-ref p 0)
2540 ((each) (match-empty (vector-ref p 1) r))
aa3819aa
AR
2541 ((each+) (match-empty (vector-ref p 1)
2542 (match-empty
2543 (reverse (vector-ref p 2))
2544 (match-empty (vector-ref p 3) r))))
c3ae0ed4
AW
2545 ((free-id atom) r)
2546 ((vector) (match-empty (vector-ref p 1) r)))))))
2547
aa3819aa
AR
2548 (define combine
2549 (lambda (r* r)
2550 (if (null? (car r*))
2551 r
2552 (cons (map car r*) (combine (map cdr r*) r)))))
2553
c3ae0ed4
AW
2554 (define match*
2555 (lambda (e p w r mod)
2556 (cond
2557 ((null? p) (and (null? e) r))
2558 ((pair? p)
2559 (and (pair? e) (match (car e) (car p) w
2560 (match (cdr e) (cdr p) w r mod)
2561 mod)))
2562 ((eq? p 'each-any)
2563 (let ((l (match-each-any e w mod))) (and l (cons l r))))
2564 (else
2565 (case (vector-ref p 0)
2566 ((each)
2567 (if (null? e)
2568 (match-empty (vector-ref p 1) r)
2569 (let ((l (match-each e (vector-ref p 1) w mod)))
2570 (and l
2571 (let collect ((l l))
2572 (if (null? (car l))
2573 r
2574 (cons (map car l) (collect (map cdr l)))))))))
aa3819aa
AR
2575 ((each+)
2576 (call-with-values
2577 (lambda ()
2578 (match-each+ e (vector-ref p 1) (vector-ref p 2) (vector-ref p 3) w r mod))
2579 (lambda (xr* y-pat r)
2580 (and r
2581 (null? y-pat)
2582 (if (null? xr*)
2583 (match-empty (vector-ref p 1) r)
2584 (combine xr* r))))))
c3ae0ed4
AW
2585 ((free-id) (and (id? e) (free-id=? (wrap e w mod) (vector-ref p 1)) r))
2586 ((atom) (and (equal? (vector-ref p 1) (strip e w)) r))
2587 ((vector)
2588 (and (vector? e)
2589 (match (vector->list e) (vector-ref p 1) w r mod))))))))
2590
2591 (define match
2592 (lambda (e p w r mod)
2593 (cond
2594 ((not r) #f)
2595 ((eq? p 'any) (cons (wrap e w mod) r))
2596 ((syntax-object? e)
2597 (match*
2598 (syntax-object-expression e)
2599 p
2600 (join-wraps w (syntax-object-wrap e))
2601 r
2602 (syntax-object-module e)))
2603 (else (match* e p w r mod)))))
2604
2605 (set! $sc-dispatch
2606 (lambda (e p)
2607 (cond
2608 ((eq? p 'any) (list e))
2609 ((syntax-object? e)
2610 (match* (syntax-object-expression e)
2611 p (syntax-object-wrap e) '() (syntax-object-module e)))
2612 (else (match* e p empty-wrap '() #f)))))
80f225df 2613
c3ae0ed4 2614 ))
a63812a2
JB
2615)
2616
2617(define-syntax with-syntax
2618 (lambda (x)
2619 (syntax-case x ()
2620 ((_ () e1 e2 ...)
c3ae0ed4 2621 #'(begin e1 e2 ...))
a63812a2 2622 ((_ ((out in)) e1 e2 ...)
c3ae0ed4 2623 #'(syntax-case in () (out (begin e1 e2 ...))))
a63812a2 2624 ((_ ((out in) ...) e1 e2 ...)
c3ae0ed4
AW
2625 #'(syntax-case (list in ...) ()
2626 ((out ...) (begin e1 e2 ...)))))))
a63812a2
JB
2627
2628(define-syntax syntax-rules
2629 (lambda (x)
2630 (syntax-case x ()
2631 ((_ (k ...) ((keyword . pattern) template) ...)
c3ae0ed4 2632 #'(lambda (x)
a5e95abe
AW
2633 ;; embed patterns as procedure metadata
2634 #((macro-type . syntax-rules)
2635 (patterns pattern ...))
c3ae0ed4
AW
2636 (syntax-case x (k ...)
2637 ((dummy . pattern) #'template)
2638 ...))))))
a63812a2
JB
2639
2640(define-syntax let*
2641 (lambda (x)
2642 (syntax-case x ()
2643 ((let* ((x v) ...) e1 e2 ...)
c3ae0ed4
AW
2644 (and-map identifier? #'(x ...))
2645 (let f ((bindings #'((x v) ...)))
a63812a2 2646 (if (null? bindings)
c3ae0ed4 2647 #'(let () e1 e2 ...)
a63812a2
JB
2648 (with-syntax ((body (f (cdr bindings)))
2649 (binding (car bindings)))
c3ae0ed4 2650 #'(let (binding) body))))))))
a63812a2
JB
2651
2652(define-syntax do
2653 (lambda (orig-x)
2654 (syntax-case orig-x ()
2655 ((_ ((var init . step) ...) (e0 e1 ...) c ...)
2656 (with-syntax (((step ...)
2657 (map (lambda (v s)
c3ae0ed4
AW
2658 (syntax-case s ()
2659 (() v)
2660 ((e) #'e)
2661 (_ (syntax-violation
2662 'do "bad step expression"
2663 orig-x s))))
2664 #'(var ...)
2665 #'(step ...))))
2666 (syntax-case #'(e1 ...) ()
2667 (() #'(let doloop ((var init) ...)
2668 (if (not e0)
2669 (begin c ... (doloop step ...)))))
2670 ((e1 e2 ...)
2671 #'(let doloop ((var init) ...)
2672 (if e0
2673 (begin e1 e2 ...)
2674 (begin c ... (doloop step ...)))))))))))
a63812a2
JB
2675
2676(define-syntax quasiquote
2677 (letrec
2678 ((quasicons
2679 (lambda (x y)
2680 (with-syntax ((x x) (y y))
c3ae0ed4 2681 (syntax-case #'y (quote list)
a63812a2 2682 ((quote dy)
c3ae0ed4
AW
2683 (syntax-case #'x (quote)
2684 ((quote dx) #'(quote (dx . dy)))
2685 (_ (if (null? #'dy)
2686 #'(list x)
2687 #'(cons x y)))))
2688 ((list . stuff) #'(list x . stuff))
2689 (else #'(cons x y))))))
a63812a2
JB
2690 (quasiappend
2691 (lambda (x y)
2692 (with-syntax ((x x) (y y))
c3ae0ed4
AW
2693 (syntax-case #'y (quote)
2694 ((quote ()) #'x)
2695 (_ #'(append x y))))))
a63812a2
JB
2696 (quasivector
2697 (lambda (x)
2698 (with-syntax ((x x))
c3ae0ed4
AW
2699 (syntax-case #'x (quote list)
2700 ((quote (x ...)) #'(quote #(x ...)))
2701 ((list x ...) #'(vector x ...))
2702 (_ #'(list->vector x))))))
a63812a2
JB
2703 (quasi
2704 (lambda (p lev)
2705 (syntax-case p (unquote unquote-splicing quasiquote)
2706 ((unquote p)
2707 (if (= lev 0)
c3ae0ed4
AW
2708 #'p
2709 (quasicons #'(quote unquote)
2710 (quasi #'(p) (- lev 1)))))
40b36cfb
AW
2711 ((unquote . args)
2712 (= lev 0)
2713 (syntax-violation 'unquote
2714 "unquote takes exactly one argument"
c3ae0ed4 2715 p #'(unquote . args)))
a63812a2
JB
2716 (((unquote-splicing p) . q)
2717 (if (= lev 0)
c3ae0ed4
AW
2718 (quasiappend #'p (quasi #'q lev))
2719 (quasicons (quasicons #'(quote unquote-splicing)
2720 (quasi #'(p) (- lev 1)))
2721 (quasi #'q lev))))
40b36cfb
AW
2722 (((unquote-splicing . args) . q)
2723 (= lev 0)
2724 (syntax-violation 'unquote-splicing
2725 "unquote-splicing takes exactly one argument"
c3ae0ed4 2726 p #'(unquote-splicing . args)))
a63812a2 2727 ((quasiquote p)
c3ae0ed4
AW
2728 (quasicons #'(quote quasiquote)
2729 (quasi #'(p) (+ lev 1))))
a63812a2 2730 ((p . q)
c3ae0ed4
AW
2731 (quasicons (quasi #'p lev) (quasi #'q lev)))
2732 (#(x ...) (quasivector (quasi #'(x ...) lev)))
2733 (p #'(quote p))))))
a63812a2
JB
2734 (lambda (x)
2735 (syntax-case x ()
c3ae0ed4 2736 ((_ e) (quasi #'e 0))))))
a63812a2
JB
2737
2738(define-syntax include
2739 (lambda (x)
2740 (define read-file
2741 (lambda (fn k)
2742 (let ((p (open-input-file fn)))
df0f5295
LC
2743 (let f ((x (read p))
2744 (result '()))
a63812a2 2745 (if (eof-object? x)
df0f5295
LC
2746 (begin
2747 (close-input-port p)
2748 (reverse result))
2749 (f (read p)
2750 (cons (datum->syntax k x) result)))))))
a63812a2
JB
2751 (syntax-case x ()
2752 ((k filename)
c3ae0ed4 2753 (let ((fn (syntax->datum #'filename)))
9846796b 2754 (with-syntax (((exp ...) (read-file fn #'filename)))
c3ae0ed4 2755 #'(begin exp ...)))))))
a63812a2 2756
d89fae24
AW
2757(define-syntax include-from-path
2758 (lambda (x)
2759 (syntax-case x ()
2760 ((k filename)
2761 (let ((fn (syntax->datum #'filename)))
9846796b
AW
2762 (with-syntax ((fn (datum->syntax
2763 #'filename
2764 (or (%search-load-path fn)
2765 (syntax-violation 'include-from-path
2766 "file not found in path"
2767 x #'filename)))))
d89fae24
AW
2768 #'(include fn)))))))
2769
a63812a2 2770(define-syntax unquote
6a952e0e
AW
2771 (lambda (x)
2772 (syntax-case x ()
2773 ((_ e)
2774 (syntax-violation 'unquote
2775 "expression not valid outside of quasiquote"
2776 x)))))
a63812a2
JB
2777
2778(define-syntax unquote-splicing
6a952e0e
AW
2779 (lambda (x)
2780 (syntax-case x ()
2781 ((_ e)
2782 (syntax-violation 'unquote-splicing
2783 "expression not valid outside of quasiquote"
2784 x)))))
a63812a2
JB
2785
2786(define-syntax case
2787 (lambda (x)
2788 (syntax-case x ()
2789 ((_ e m1 m2 ...)
2790 (with-syntax
c3ae0ed4
AW
2791 ((body (let f ((clause #'m1) (clauses #'(m2 ...)))
2792 (if (null? clauses)
a63812a2 2793 (syntax-case clause (else)
c3ae0ed4 2794 ((else e1 e2 ...) #'(begin e1 e2 ...))
a63812a2 2795 (((k ...) e1 e2 ...)
c3ae0ed4
AW
2796 #'(if (memv t '(k ...)) (begin e1 e2 ...)))
2797 (_ (syntax-violation 'case "bad clause" x clause)))
2798 (with-syntax ((rest (f (car clauses) (cdr clauses))))
2799 (syntax-case clause (else)
2800 (((k ...) e1 e2 ...)
2801 #'(if (memv t '(k ...))
2802 (begin e1 e2 ...)
2803 rest))
2804 (_ (syntax-violation 'case "bad clause" x
2805 clause))))))))
2806 #'(let ((t e)) body))))))
a63812a2
JB
2807
2808(define-syntax identifier-syntax
2809 (lambda (x)
2810 (syntax-case x ()
2811 ((_ e)
c3ae0ed4 2812 #'(lambda (x)
a5e95abe 2813 #((macro-type . identifier-syntax))
a63812a2
JB
2814 (syntax-case x ()
2815 (id
c3ae0ed4
AW
2816 (identifier? #'id)
2817 #'e)
a63812a2 2818 ((_ x (... ...))
c3ae0ed4 2819 #'(e x (... ...)))))))))
97bc28b6
AW
2820
2821(define-syntax define*
64fa96ef
AW
2822 (lambda (x)
2823 (syntax-case x ()
2824 ((_ (id . args) b0 b1 ...)
2825 #'(define id (lambda* args b0 b1 ...)))
2826 ((_ id val) (identifier? #'x)
2827 #'(define id val)))))