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