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