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