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