| 1 |
;;; Copyright (c) 2006, 2007 Abdulaziz Ghuloum and Kent Dybvig |
| 2 |
;;; |
| 3 |
;;; Permission is hereby granted, free of charge, to any person obtaining a |
| 4 |
;;; copy of this software and associated documentation files (the "Software"), |
| 5 |
;;; to deal in the Software without restriction, including without limitation |
| 6 |
;;; the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 7 |
;;; and/or sell copies of the Software, and to permit persons to whom the |
| 8 |
;;; Software is furnished to do so, subject to the following conditions: |
| 9 |
;;; |
| 10 |
;;; The above copyright notice and this permission notice shall be included in |
| 11 |
;;; all copies or substantial portions of the Software. |
| 12 |
;;; |
| 13 |
;;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 |
;;; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 |
;;; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 16 |
;;; THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 |
;;; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 18 |
;;; FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 19 |
;;; DEALINGS IN THE SOFTWARE. |
| 20 |
|
| 21 |
(library (psyntax expander) |
| 22 |
(export identifier? syntax-dispatch |
| 23 |
eval core-expand |
| 24 |
generate-temporaries free-identifier=? |
| 25 |
bound-identifier=? datum->syntax syntax-error |
| 26 |
syntax-violation |
| 27 |
syntax->datum |
| 28 |
make-variable-transformer make-compile-time-value |
| 29 |
variable-transformer? |
| 30 |
variable-transformer-procedure |
| 31 |
compile-r6rs-top-level boot-library-expand |
| 32 |
null-environment scheme-report-environment |
| 33 |
interaction-environment |
| 34 |
ellipsis-map assertion-error |
| 35 |
environment environment? environment-symbols |
| 36 |
new-interaction-environment syntax-transpose) |
| 37 |
(import |
| 38 |
(except (rnrs) |
| 39 |
environment environment? identifier? |
| 40 |
eval generate-temporaries free-identifier=? |
| 41 |
bound-identifier=? datum->syntax syntax-error |
| 42 |
syntax-violation syntax->datum make-variable-transformer |
| 43 |
null-environment scheme-report-environment) |
| 44 |
(rnrs base) |
| 45 |
(rnrs lists) |
| 46 |
(rnrs control) |
| 47 |
(rnrs io simple) |
| 48 |
(rnrs mutable-pairs) |
| 49 |
(psyntax library-manager) |
| 50 |
(psyntax builders) |
| 51 |
(psyntax compat) |
| 52 |
(psyntax config) |
| 53 |
(psyntax internal) |
| 54 |
(only (rnrs syntax-case) syntax-case syntax with-syntax) |
| 55 |
(prefix (rnrs syntax-case) sys.)) |
| 56 |
|
| 57 |
(define (set-cons x ls) |
| 58 |
(cond |
| 59 |
((memq x ls) ls) |
| 60 |
(else (cons x ls)))) |
| 61 |
|
| 62 |
(define (set-union ls1 ls2) |
| 63 |
(cond |
| 64 |
((null? ls1) ls2) |
| 65 |
((memq (car ls1) ls2) (set-union (cdr ls1) ls2)) |
| 66 |
(else (cons (car ls1) (set-union (cdr ls1) ls2))))) |
| 67 |
|
| 68 |
(define-syntax no-source |
| 69 |
(lambda (x) #f)) |
| 70 |
|
| 71 |
;;; the body of a library, when it's first processed, gets this |
| 72 |
;;; set of marks. |
| 73 |
(define top-mark* '(top)) |
| 74 |
|
| 75 |
;;; consequently, every syntax object that has a top in its marks |
| 76 |
;;; set was present in the program source. |
| 77 |
(define top-marked? |
| 78 |
(lambda (m*) (memq 'top m*))) |
| 79 |
|
| 80 |
;;; This procedure generates a fresh lexical name for renaming. |
| 81 |
;;; It's also use it to generate temporaries. |
| 82 |
(define gen-lexical |
| 83 |
(lambda (sym) |
| 84 |
(cond |
| 85 |
((symbol? sym) (gensym sym)) |
| 86 |
((stx? sym) (gen-lexical (id->sym sym))) |
| 87 |
(else (assertion-violation 'gen-lexical "BUG: invalid arg" sym))))) |
| 88 |
|
| 89 |
;;; gen-global is used to generate global names (e.g. locations |
| 90 |
;;; for library exports). We use gen-lexical since it works just |
| 91 |
;;; fine. |
| 92 |
(define (gen-global x) (gen-lexical x)) |
| 93 |
|
| 94 |
;;; every identifier in the program would have a label associated |
| 95 |
;;; with it in its substitution. gen-label generates such labels. |
| 96 |
;;; the labels have to have read/write eq? invariance to support |
| 97 |
;;; separate compilation. |
| 98 |
(define gen-label |
| 99 |
(lambda (_) (gensym))) |
| 100 |
|
| 101 |
(define (gen-top-level-label id rib) |
| 102 |
(define (find sym mark* sym* mark** label*) |
| 103 |
(and (pair? sym*) |
| 104 |
(if (and (eq? sym (car sym*)) (same-marks? mark* (car mark**))) |
| 105 |
(car label*) |
| 106 |
(find sym mark* (cdr sym*) (cdr mark**) (cdr label*))))) |
| 107 |
(let ((sym (id->sym id)) |
| 108 |
(mark* (stx-mark* id))) |
| 109 |
(let ((sym* (rib-sym* rib))) |
| 110 |
(cond |
| 111 |
((and (memq sym (rib-sym* rib)) |
| 112 |
(find sym mark* sym* (rib-mark** rib) (rib-label* rib))) |
| 113 |
=> |
| 114 |
(lambda (label) |
| 115 |
(cond |
| 116 |
((imported-label->binding label) |
| 117 |
;;; create new label to shadow imported binding |
| 118 |
(gensym)) |
| 119 |
(else |
| 120 |
;;; recycle old label |
| 121 |
label)))) |
| 122 |
(else |
| 123 |
;;; create new label for new binding |
| 124 |
(gensym)))))) |
| 125 |
|
| 126 |
(define (gen-define-label+loc id rib sd?) |
| 127 |
(if sd? |
| 128 |
(values (gensym) (gen-lexical id)) |
| 129 |
(let ([env (top-level-context)]) |
| 130 |
(let ((label (gen-top-level-label id rib)) |
| 131 |
(locs (interaction-env-locs env))) |
| 132 |
(values label |
| 133 |
(cond |
| 134 |
((assq label locs) => cdr) |
| 135 |
(else |
| 136 |
(let ((loc (gen-lexical id))) |
| 137 |
(set-interaction-env-locs! env |
| 138 |
(cons (cons label loc) locs)) |
| 139 |
loc)))))))) |
| 140 |
|
| 141 |
(define (gen-define-label id rib sd?) |
| 142 |
(if sd? |
| 143 |
(gensym) |
| 144 |
(gen-top-level-label id rib))) |
| 145 |
|
| 146 |
|
| 147 |
;;; A rib is a record constructed at every lexical contour in the |
| 148 |
;;; program to hold information about the variables introduced in that |
| 149 |
;;; contour. Adding an identifier->label mapping to an extensible rib |
| 150 |
;;; is achieved by consing the identifier's name to the list of |
| 151 |
;;; symbols, consing the identifier's list of marks to the rib's |
| 152 |
;;; mark**, and consing the label to the rib's labels. |
| 153 |
|
| 154 |
(define-record rib (sym* mark** label* sealed/freq)) |
| 155 |
|
| 156 |
(define make-empty-rib |
| 157 |
(lambda () |
| 158 |
(make-rib '() '() '() #f))) |
| 159 |
|
| 160 |
(define (top-marked-symbols rib) |
| 161 |
(let-values ([(sym* mark**) |
| 162 |
(let ([sym* (rib-sym* rib)] [mark** (rib-mark** rib)]) |
| 163 |
(if (rib-sealed/freq rib) |
| 164 |
(values (vector->list sym*) (vector->list mark**)) |
| 165 |
(values sym* mark**)))]) |
| 166 |
(let f ([sym* sym*] [mark** mark**]) |
| 167 |
(cond |
| 168 |
[(null? sym*) '()] |
| 169 |
[(equal? (car mark**) top-mark*) |
| 170 |
(cons (car sym*) (f (cdr sym*) (cdr mark**)))] |
| 171 |
[else (f (cdr sym*) (cdr mark**))])))) |
| 172 |
|
| 173 |
;;; For example, when processing a lambda's internal define, a new rib |
| 174 |
;;; is created and is added to the body of the lambda expression. |
| 175 |
;;; When an internal definition is encountered, a new entry for the |
| 176 |
;;; identifier is added (via side effect) to the rib. A rib may be |
| 177 |
;;; extensible, or sealed. An extensible rib looks like: |
| 178 |
;;; #<rib list-of-symbols list-of-list-of-marks list-of-labels #f> |
| 179 |
|
| 180 |
(define (extend-rib! rib id label sd?) |
| 181 |
(define (find sym mark* sym* mark** label*) |
| 182 |
(and (pair? sym*) |
| 183 |
(if (and (eq? sym (car sym*)) (same-marks? mark* (car mark**))) |
| 184 |
label* |
| 185 |
(find sym mark* (cdr sym*) (cdr mark**) (cdr label*))))) |
| 186 |
(when (rib-sealed/freq rib) |
| 187 |
(assertion-violation 'extend-rib! "BUG: rib is sealed" rib)) |
| 188 |
(let ((sym (id->sym id)) |
| 189 |
(mark* (stx-mark* id))) |
| 190 |
(let ((sym* (rib-sym* rib))) |
| 191 |
(cond |
| 192 |
((and (memq sym (rib-sym* rib)) |
| 193 |
(find sym mark* sym* (rib-mark** rib) (rib-label* rib))) |
| 194 |
=> |
| 195 |
(lambda (p) |
| 196 |
(unless (eq? label (car p)) |
| 197 |
(cond |
| 198 |
((not sd?) ;(top-level-context) |
| 199 |
;;; XXX override label |
| 200 |
(set-car! p label)) |
| 201 |
(else |
| 202 |
;;; signal an error if the identifier was already |
| 203 |
;;; in the rib. |
| 204 |
(stx-error id "multiple definitions of identifier")))))) |
| 205 |
(else |
| 206 |
(set-rib-sym*! rib (cons sym sym*)) |
| 207 |
(set-rib-mark**! rib (cons mark* (rib-mark** rib))) |
| 208 |
(set-rib-label*! rib (cons label (rib-label* rib)))))))) |
| 209 |
|
| 210 |
|
| 211 |
;;; A rib can be sealed once all bindings are inserted. To seal |
| 212 |
;;; a rib, we convert the lists sym*, mark**, and label* to vectors |
| 213 |
;;; and insert a frequency vector in the sealed/freq field. |
| 214 |
;;; The frequency vector is an optimization that allows the rib to |
| 215 |
;;; reorganize itself by bubbling frequently used mappings to the |
| 216 |
;;; top of the rib. The vector is maintained in non-descending |
| 217 |
;;; order and an identifier's entry in the rib is incremented at |
| 218 |
;;; every access. If an identifier's frequency exceeds the |
| 219 |
;;; preceeding one, the identifier's position is promoted to the |
| 220 |
;;; top of its class (or the bottom of the previous class). |
| 221 |
|
| 222 |
(define (seal-rib! rib) |
| 223 |
(let ((sym* (rib-sym* rib))) |
| 224 |
(unless (null? sym*) |
| 225 |
;;; only seal if rib is not empty. |
| 226 |
(let ((sym* (list->vector sym*))) |
| 227 |
(set-rib-sym*! rib sym*) |
| 228 |
(set-rib-mark**! rib |
| 229 |
(list->vector (rib-mark** rib))) |
| 230 |
(set-rib-label*! rib |
| 231 |
(list->vector (rib-label* rib))) |
| 232 |
(set-rib-sealed/freq! rib |
| 233 |
(make-vector (vector-length sym*) 0)))))) |
| 234 |
|
| 235 |
(define (unseal-rib! rib) |
| 236 |
(when (rib-sealed/freq rib) |
| 237 |
(set-rib-sealed/freq! rib #f) |
| 238 |
(set-rib-sym*! rib (vector->list (rib-sym* rib))) |
| 239 |
(set-rib-mark**! rib (vector->list (rib-mark** rib))) |
| 240 |
(set-rib-label*! rib (vector->list (rib-label* rib))))) |
| 241 |
|
| 242 |
(define (increment-rib-frequency! rib idx) |
| 243 |
(let ((freq* (rib-sealed/freq rib))) |
| 244 |
(let ((freq (vector-ref freq* idx))) |
| 245 |
(let ((i |
| 246 |
(let f ((i idx)) |
| 247 |
(cond |
| 248 |
((zero? i) 0) |
| 249 |
(else |
| 250 |
(let ((j (- i 1))) |
| 251 |
(cond |
| 252 |
((= freq (vector-ref freq* j)) (f j)) |
| 253 |
(else i)))))))) |
| 254 |
(vector-set! freq* i (+ freq 1)) |
| 255 |
(unless (= i idx) |
| 256 |
(let ((sym* (rib-sym* rib)) |
| 257 |
(mark** (rib-mark** rib)) |
| 258 |
(label* (rib-label* rib))) |
| 259 |
(let ((sym (vector-ref sym* idx))) |
| 260 |
(vector-set! sym* idx (vector-ref sym* i)) |
| 261 |
(vector-set! sym* i sym)) |
| 262 |
(let ((mark* (vector-ref mark** idx))) |
| 263 |
(vector-set! mark** idx (vector-ref mark** i)) |
| 264 |
(vector-set! mark** i mark*)) |
| 265 |
(let ((label (vector-ref label* idx))) |
| 266 |
(vector-set! label* idx (vector-ref label* i)) |
| 267 |
(vector-set! label* i label)))))))) |
| 268 |
|
| 269 |
(define make-full-rib ;;; it may be a good idea to seal this rib |
| 270 |
(lambda (id* label*) |
| 271 |
(make-rib (map id->sym id*) (map stx-mark* id*) label* #f))) |
| 272 |
|
| 273 |
;;; Now to syntax objects which are records defined like: |
| 274 |
(define-record stx (expr mark* subst* ae*) |
| 275 |
(lambda (x p wr) |
| 276 |
(display "#<syntax " p) |
| 277 |
(write (stx->datum x) p) |
| 278 |
(let ((expr (stx-expr x))) |
| 279 |
(when (annotation? expr) |
| 280 |
(let ((src (annotation-source expr))) |
| 281 |
(when (pair? src) |
| 282 |
(display " [char " p) |
| 283 |
(display (cdr src) p) |
| 284 |
(display " of " p) |
| 285 |
(display (car src) p) |
| 286 |
(display "]" p))))) |
| 287 |
(display ">" p))) |
| 288 |
|
| 289 |
;;; First, let's look at identifiers, since they're the real |
| 290 |
;;; reason why syntax objects are here to begin with. |
| 291 |
;;; An identifier is an stx whose expr is a symbol. |
| 292 |
;;; In addition to the symbol naming the identifier, the identifer |
| 293 |
;;; has a list of marks and a list of substitutions. |
| 294 |
;;; The idea is that to get the label of an identifier, we look up |
| 295 |
;;; the identifier's substitutions for a mapping with the same |
| 296 |
;;; name and same marks (see same-marks? below). |
| 297 |
|
| 298 |
;;; Since all the identifier->label bindings are encapsulated |
| 299 |
;;; within the identifier, converting a datum to a syntax object |
| 300 |
;;; (non-hygienically) is done simply by creating an stx that has |
| 301 |
;;; the same marks and substitutions as the identifier. |
| 302 |
(define datum->stx |
| 303 |
(lambda (id datum) |
| 304 |
(make-stx datum (stx-mark* id) (stx-subst* id) (stx-ae* id)))) |
| 305 |
|
| 306 |
;;; A syntax object may be wrapped or unwrapped, so what does that |
| 307 |
;;; mean exactly? |
| 308 |
;;; |
| 309 |
;;; A wrapped syntax object is just a way of saying it's an stx |
| 310 |
;;; record. All identifiers are stx records (with a symbol in |
| 311 |
;;; their expr field). Other objects such as pairs and vectors |
| 312 |
;;; may be wrapped or unwrapped. A wrapped pair is an stx whos |
| 313 |
;;; expr is a pair. An unwrapped pair is a pair whos car and cdr |
| 314 |
;;; fields are themselves syntax objects (wrapped or unwrapped). |
| 315 |
;;; |
| 316 |
;;; We always maintain the invariant that we don't double wrap |
| 317 |
;;; syntax objects. The only way to get a doubly-wrapped syntax |
| 318 |
;;; object is by doing datum->stx (above) where the datum is |
| 319 |
;;; itself a wrapped syntax object (r6rs may not even consider |
| 320 |
;;; wrapped syntax objects as datum, but let's not worry now). |
| 321 |
|
| 322 |
;;; Syntax objects have, in addition to the expr, a |
| 323 |
;;; substitution field (stx-subst*). The subst* is a list |
| 324 |
;;; where each element is either a rib or the symbol "shift". |
| 325 |
;;; Normally, a new rib is added to an stx at evert lexical |
| 326 |
;;; contour of the program in order to capture the bindings |
| 327 |
;;; inctroduced in that contour. |
| 328 |
|
| 329 |
;;; The mark* field of an stx is, well, a list of marks. |
| 330 |
;;; Each of these marks can be either a generated mark |
| 331 |
;;; or an antimark. |
| 332 |
;;; (two marks must be eq?-comparable, so we use a string |
| 333 |
;;; of one char (this assumes that strings are mutable)). |
| 334 |
|
| 335 |
;;; gen-mark generates a new unique mark |
| 336 |
(define (gen-mark) ;;; faster |
| 337 |
(string #\m)) |
| 338 |
|
| 339 |
;(define gen-mark ;;; useful for debugging |
| 340 |
; (let ((i 0)) |
| 341 |
; (lambda () |
| 342 |
; (set! i (+ i 1)) |
| 343 |
; (string-append "m." (number->string i))))) |
| 344 |
|
| 345 |
;;; We use #f as the anti-mark. |
| 346 |
(define anti-mark #f) |
| 347 |
(define anti-mark? not) |
| 348 |
|
| 349 |
;;; So, what's an anti-mark and why is it there. |
| 350 |
;;; The theory goes like this: when a macro call is encountered, |
| 351 |
;;; the input stx to the macro transformer gets an extra anti-mark, |
| 352 |
;;; and the output of the transformer gets a fresh mark. |
| 353 |
;;; When a mark collides with an anti-mark, they cancel one |
| 354 |
;;; another. Therefore, any part of the input transformer that |
| 355 |
;;; gets copied to the output would have a mark followed |
| 356 |
;;; immediately by an anti-mark, resulting in the same syntax |
| 357 |
;;; object (no extra marks). Parts of the output that were not |
| 358 |
;;; present in the input (e.g. inserted by the macro transformer) |
| 359 |
;;; would have no anti-mark and, therefore, the mark would stick |
| 360 |
;;; to them. |
| 361 |
;;; |
| 362 |
;;; Every time a mark is pushed to an stx-mark* list, a |
| 363 |
;;; corresponding 'shift is pushed to the stx-subst* list. |
| 364 |
;;; Every time a mark is cancelled by an anti-mark, the |
| 365 |
;;; corresponding shifts are also cancelled. |
| 366 |
|
| 367 |
;;; The procedure join-wraps, here, is used to compute the new |
| 368 |
;;; mark* and subst* that would result when the m1* and s1* are |
| 369 |
;;; added to an stx's mark* and subst*. |
| 370 |
;;; The only tricky part here is that e may have an anti-mark |
| 371 |
;;; that should cancel with the last mark in m1*. |
| 372 |
;;; So, if m1* is (mx* ... mx) |
| 373 |
;;; and m2* is (#f my* ...) |
| 374 |
;;; then the resulting marks should be (mx* ... my* ...) |
| 375 |
;;; since mx would cancel with the anti-mark. |
| 376 |
;;; The substs would have to also cancel since |
| 377 |
;;; s1* is (sx* ... sx) |
| 378 |
;;; and s2* is (sy sy* ...) |
| 379 |
;;; then the resulting substs should be (sx* ... sy* ...) |
| 380 |
;;; Notice that both sx and sy would be shift marks. |
| 381 |
(define join-wraps |
| 382 |
(lambda (m1* s1* ae1* e) |
| 383 |
(define merge-ae* |
| 384 |
(lambda (ls1 ls2) |
| 385 |
(if (and (pair? ls1) (pair? ls2) (not (car ls2))) |
| 386 |
(cancel ls1 ls2) |
| 387 |
(append ls1 ls2)))) |
| 388 |
(define cancel |
| 389 |
(lambda (ls1 ls2) |
| 390 |
(let f ((x (car ls1)) (ls1 (cdr ls1))) |
| 391 |
(if (null? ls1) |
| 392 |
(cdr ls2) |
| 393 |
(cons x (f (car ls1) (cdr ls1))))))) |
| 394 |
(let ((m2* (stx-mark* e)) |
| 395 |
(s2* (stx-subst* e)) |
| 396 |
(ae2* (stx-ae* e))) |
| 397 |
(if (and (not (null? m1*)) |
| 398 |
(not (null? m2*)) |
| 399 |
(anti-mark? (car m2*))) |
| 400 |
; cancel mark, anti-mark, and corresponding shifts |
| 401 |
(values (cancel m1* m2*) (cancel s1* s2*) (merge-ae* ae1* ae2*)) |
| 402 |
(values (append m1* m2*) (append s1* s2*) (merge-ae* ae1* ae2*)))))) |
| 403 |
|
| 404 |
;;; The procedure mkstx is then the proper constructor for |
| 405 |
;;; wrapped syntax objects. It takes a syntax object, a list |
| 406 |
;;; of marks, and a list of substs. It joins the two wraps |
| 407 |
;;; making sure that marks and anti-marks and corresponding |
| 408 |
;;; shifts cancel properly. |
| 409 |
(define mkstx |
| 410 |
(lambda (e m* s* ae*) |
| 411 |
(if (and (stx? e) (not (top-marked? m*))) |
| 412 |
(let-values (((m* s* ae*) (join-wraps m* s* ae* e))) |
| 413 |
(make-stx (stx-expr e) m* s* ae*)) |
| 414 |
(make-stx e m* s* ae*)))) |
| 415 |
|
| 416 |
(define add-subst |
| 417 |
(lambda (subst e) |
| 418 |
(mkstx e '() (list subst) '()))) |
| 419 |
|
| 420 |
(define add-mark |
| 421 |
(lambda (mark subst expr ae) |
| 422 |
(define merge-ae* |
| 423 |
(lambda (ls1 ls2) |
| 424 |
(if (and (pair? ls1) (pair? ls2) (not (car ls2))) |
| 425 |
(cancel ls1 ls2) |
| 426 |
(append ls1 ls2)))) |
| 427 |
(define cancel |
| 428 |
(lambda (ls1 ls2) |
| 429 |
(let f ((x (car ls1)) (ls1 (cdr ls1))) |
| 430 |
(if (null? ls1) |
| 431 |
(cdr ls2) |
| 432 |
(cons x (f (car ls1) (cdr ls1))))))) |
| 433 |
(define (f e m s1* ae*) |
| 434 |
(cond |
| 435 |
[(pair? e) |
| 436 |
(let ([a (f (car e) m s1* ae*)] |
| 437 |
[d (f (cdr e) m s1* ae*)]) |
| 438 |
(if (eq? a d) e (cons a d)))] |
| 439 |
[(vector? e) |
| 440 |
(let ([ls1 (vector->list e)]) |
| 441 |
(let ([ls2 (map (lambda (x) (f x m s1* ae*)) ls1)]) |
| 442 |
(if (for-all eq? ls1 ls2) e (list->vector ls2))))] |
| 443 |
[(stx? e) |
| 444 |
(let ([m* (stx-mark* e)] [s2* (stx-subst* e)]) |
| 445 |
(cond |
| 446 |
[(null? m*) |
| 447 |
(f (stx-expr e) m |
| 448 |
(append s1* s2*) |
| 449 |
(merge-ae* ae* (stx-ae* e)))] |
| 450 |
[(eq? (car m*) anti-mark) |
| 451 |
(make-stx (stx-expr e) (cdr m*) |
| 452 |
(cdr (append s1* s2*)) |
| 453 |
(merge-ae* ae* (stx-ae* e)))] |
| 454 |
[else |
| 455 |
(make-stx (stx-expr e) |
| 456 |
(cons m m*) |
| 457 |
(let ([s* (cons 'shift (append s1* s2*))]) |
| 458 |
(if subst (cons subst s*) s*)) |
| 459 |
(merge-ae* ae* (stx-ae* e)))]))] |
| 460 |
[(symbol? e) |
| 461 |
(syntax-violation #f |
| 462 |
"raw symbol encountered in output of macro" |
| 463 |
expr e)] |
| 464 |
[else (make-stx e (list m) s1* ae*)])) |
| 465 |
(mkstx (f expr mark '() '()) '() '() (list ae)))) |
| 466 |
|
| 467 |
;;; now are some deconstructors and predicates for syntax objects. |
| 468 |
(define syntax-kind? |
| 469 |
(lambda (x p?) |
| 470 |
(cond |
| 471 |
((stx? x) (syntax-kind? (stx-expr x) p?)) |
| 472 |
((annotation? x) |
| 473 |
(syntax-kind? (annotation-expression x) p?)) |
| 474 |
(else (p? x))))) |
| 475 |
|
| 476 |
(define syntax-vector->list |
| 477 |
(lambda (x) |
| 478 |
(cond |
| 479 |
((stx? x) |
| 480 |
(let ((ls (syntax-vector->list (stx-expr x))) |
| 481 |
(m* (stx-mark* x)) |
| 482 |
(s* (stx-subst* x)) |
| 483 |
(ae* (stx-ae* x))) |
| 484 |
(map (lambda (x) (mkstx x m* s* ae*)) ls))) |
| 485 |
((annotation? x) |
| 486 |
(syntax-vector->list (annotation-expression x))) |
| 487 |
((vector? x) (vector->list x)) |
| 488 |
(else (assertion-violation 'syntax-vector->list "BUG: not a syntax vector" x))))) |
| 489 |
(define syntax-pair? |
| 490 |
(lambda (x) (syntax-kind? x pair?))) |
| 491 |
(define syntax-vector? |
| 492 |
(lambda (x) (syntax-kind? x vector?))) |
| 493 |
(define syntax-null? |
| 494 |
(lambda (x) (syntax-kind? x null?))) |
| 495 |
(define syntax-list? ;;; FIXME: should terminate on cyclic input. |
| 496 |
(lambda (x) |
| 497 |
(or (syntax-null? x) |
| 498 |
(and (syntax-pair? x) (syntax-list? (syntax-cdr x)))))) |
| 499 |
(define syntax-car |
| 500 |
(lambda (x) |
| 501 |
(cond |
| 502 |
((stx? x) |
| 503 |
(mkstx (syntax-car (stx-expr x)) |
| 504 |
(stx-mark* x) |
| 505 |
(stx-subst* x) |
| 506 |
(stx-ae* x))) |
| 507 |
((annotation? x) |
| 508 |
(syntax-car (annotation-expression x))) |
| 509 |
((pair? x) (car x)) |
| 510 |
(else (assertion-violation 'syntax-car "BUG: not a pair" x))))) |
| 511 |
(define syntax-cdr |
| 512 |
(lambda (x) |
| 513 |
(cond |
| 514 |
((stx? x) |
| 515 |
(mkstx (syntax-cdr (stx-expr x)) |
| 516 |
(stx-mark* x) |
| 517 |
(stx-subst* x) |
| 518 |
(stx-ae* x))) |
| 519 |
((annotation? x) |
| 520 |
(syntax-cdr (annotation-expression x))) |
| 521 |
((pair? x) (cdr x)) |
| 522 |
(else (assertion-violation 'syntax-cdr "BUG: not a pair" x))))) |
| 523 |
(define syntax->list |
| 524 |
(lambda (x) |
| 525 |
(if (syntax-pair? x) |
| 526 |
(cons (syntax-car x) (syntax->list (syntax-cdr x))) |
| 527 |
(if (syntax-null? x) |
| 528 |
'() |
| 529 |
(assertion-violation 'syntax->list "BUG: invalid argument" x))))) |
| 530 |
|
| 531 |
(define id? |
| 532 |
(lambda (x) |
| 533 |
(and (stx? x) |
| 534 |
(let ((expr (stx-expr x))) |
| 535 |
(symbol? (if (annotation? expr) |
| 536 |
(annotation-stripped expr) |
| 537 |
expr)))))) |
| 538 |
|
| 539 |
(define id->sym |
| 540 |
(lambda (x) |
| 541 |
(unless (stx? x) |
| 542 |
(error 'id->sym "BUG in ikarus: not an id" x)) |
| 543 |
(let ((expr (stx-expr x))) |
| 544 |
(let ((sym (if (annotation? expr) |
| 545 |
(annotation-stripped expr) |
| 546 |
expr))) |
| 547 |
(if (symbol? sym) |
| 548 |
sym |
| 549 |
(error 'id->sym "BUG in ikarus: not an id" x)))))) |
| 550 |
|
| 551 |
;;; Two lists of marks are considered the same if they have the |
| 552 |
;;; same length and the corresponding marks on each are eq?. |
| 553 |
(define same-marks? |
| 554 |
(lambda (x y) |
| 555 |
(or (and (null? x) (null? y)) ;(eq? x y) |
| 556 |
(and (pair? x) (pair? y) |
| 557 |
(eq? (car x) (car y)) |
| 558 |
(same-marks? (cdr x) (cdr y)))))) |
| 559 |
|
| 560 |
;;; Two identifiers are bound-id=? if they have the same name and |
| 561 |
;;; the same set of marks. |
| 562 |
(define bound-id=? |
| 563 |
(lambda (x y) |
| 564 |
(and (eq? (id->sym x) (id->sym y)) |
| 565 |
(same-marks? (stx-mark* x) (stx-mark* y))))) |
| 566 |
|
| 567 |
;;; Two identifiers are free-id=? if either both are bound to the |
| 568 |
;;; same label or if both are unbound and they have the same name. |
| 569 |
(define free-id=? |
| 570 |
(lambda (i j) |
| 571 |
(let ((t0 (id->label i)) (t1 (id->label j))) |
| 572 |
(if (or t0 t1) |
| 573 |
(eq? t0 t1) |
| 574 |
(eq? (id->sym i) (id->sym j)))))) |
| 575 |
|
| 576 |
;;; valid-bound-ids? takes checks if a list is made of identifers |
| 577 |
;;; none of which is bound-id=? to another. |
| 578 |
(define valid-bound-ids? |
| 579 |
(lambda (id*) |
| 580 |
(and (for-all id? id*) |
| 581 |
(distinct-bound-ids? id*)))) |
| 582 |
|
| 583 |
(define distinct-bound-ids? |
| 584 |
(lambda (id*) |
| 585 |
(or (null? id*) |
| 586 |
(and (not (bound-id-member? (car id*) (cdr id*))) |
| 587 |
(distinct-bound-ids? (cdr id*)))))) |
| 588 |
|
| 589 |
(define bound-id-member? |
| 590 |
(lambda (id id*) |
| 591 |
(and (pair? id*) |
| 592 |
(or (bound-id=? id (car id*)) |
| 593 |
(bound-id-member? id (cdr id*)))))) |
| 594 |
|
| 595 |
(define self-evaluating? |
| 596 |
(lambda (x) ;;; am I missing something here? |
| 597 |
(or (number? x) (string? x) (char? x) (boolean? x) |
| 598 |
(bytevector? x)))) |
| 599 |
|
| 600 |
;;; strip is used to remove the wrap of a syntax object. |
| 601 |
;;; It takes an stx's expr and marks. If the marks contain |
| 602 |
;;; a top-mark, then the expr is returned. |
| 603 |
|
| 604 |
(define (strip-annotations x) |
| 605 |
(cond |
| 606 |
((pair? x) |
| 607 |
(cons (strip-annotations (car x)) |
| 608 |
(strip-annotations (cdr x)))) |
| 609 |
((vector? x) (vector-map strip-annotations x)) |
| 610 |
((annotation? x) (annotation-stripped x)) |
| 611 |
(else x))) |
| 612 |
|
| 613 |
(define strip |
| 614 |
(lambda (x m*) |
| 615 |
(if (top-marked? m*) |
| 616 |
(if (or (annotation? x) |
| 617 |
(and (pair? x) |
| 618 |
(annotation? (car x))) |
| 619 |
(and (vector? x) (> (vector-length x) 0) |
| 620 |
(annotation? (vector-ref x 0)))) |
| 621 |
;;; TODO: Ask Kent why this is a sufficient test |
| 622 |
(strip-annotations x) |
| 623 |
x) |
| 624 |
(let f ((x x)) |
| 625 |
(cond |
| 626 |
((stx? x) (strip (stx-expr x) (stx-mark* x))) |
| 627 |
((annotation? x) (annotation-stripped x)) |
| 628 |
((pair? x) |
| 629 |
(let ((a (f (car x))) (d (f (cdr x)))) |
| 630 |
(if (and (eq? a (car x)) (eq? d (cdr x))) |
| 631 |
x |
| 632 |
(cons a d)))) |
| 633 |
((vector? x) |
| 634 |
(let ((old (vector->list x))) |
| 635 |
(let ((new (map f old))) |
| 636 |
(if (for-all eq? old new) |
| 637 |
x |
| 638 |
(list->vector new))))) |
| 639 |
(else x)))))) |
| 640 |
|
| 641 |
(define stx->datum |
| 642 |
(lambda (x) |
| 643 |
(strip x '()))) |
| 644 |
|
| 645 |
;;; id->label takes an id (that's a sym x marks x substs) and |
| 646 |
;;; searches the substs for a label associated with the same sym |
| 647 |
;;; and marks. |
| 648 |
(define (id->label/intern id) |
| 649 |
(or (id->label id) |
| 650 |
(cond |
| 651 |
((top-level-context) => |
| 652 |
(lambda (env) |
| 653 |
;;; fabricate binding |
| 654 |
(let ((rib (interaction-env-rib env))) |
| 655 |
(let-values (((lab _loc) (gen-define-label+loc id rib #f))) |
| 656 |
(extend-rib! rib id lab #t) ;;; FIXME |
| 657 |
lab)))) |
| 658 |
(else #f)))) |
| 659 |
|
| 660 |
(define id->label |
| 661 |
(lambda (id) |
| 662 |
(let ((sym (id->sym id))) |
| 663 |
(let search ((subst* (stx-subst* id)) (mark* (stx-mark* id))) |
| 664 |
(cond |
| 665 |
((null? subst*) #f) |
| 666 |
((eq? (car subst*) 'shift) |
| 667 |
;;; a shift is inserted when a mark is added. |
| 668 |
;;; so, we search the rest of the substitution |
| 669 |
;;; without the mark. |
| 670 |
(search (cdr subst*) (cdr mark*))) |
| 671 |
(else |
| 672 |
(let ((rib (car subst*))) |
| 673 |
(cond |
| 674 |
((rib-sealed/freq rib) |
| 675 |
(let ((sym* (rib-sym* rib))) |
| 676 |
(let f ((i 0) (j (vector-length sym*))) |
| 677 |
(cond |
| 678 |
((= i j) (search (cdr subst*) mark*)) |
| 679 |
((and (eq? (vector-ref sym* i) sym) |
| 680 |
(same-marks? mark* |
| 681 |
(vector-ref (rib-mark** rib) i))) |
| 682 |
(let ((label (vector-ref (rib-label* rib) i))) |
| 683 |
(increment-rib-frequency! rib i) |
| 684 |
label)) |
| 685 |
(else (f (+ i 1) j)))))) |
| 686 |
(else |
| 687 |
(let f ((sym* (rib-sym* rib)) |
| 688 |
(mark** (rib-mark** rib)) |
| 689 |
(label* (rib-label* rib))) |
| 690 |
(cond |
| 691 |
((null? sym*) (search (cdr subst*) mark*)) |
| 692 |
((and (eq? (car sym*) sym) |
| 693 |
(same-marks? (car mark**) mark*)) |
| 694 |
(car label*)) |
| 695 |
(else (f (cdr sym*) (cdr mark**) (cdr label*)))))))))))))) |
| 696 |
|
| 697 |
;;; label->binding looks up the label in the environment r as |
| 698 |
;;; well as in the global environment. Since all labels are |
| 699 |
;;; unique, it doesn't matter which environment we consult first. |
| 700 |
;;; we lookup the global environment first because it's faster |
| 701 |
;;; (uses a hash table) while the lexical environment is an alist. |
| 702 |
;;; If we don't find the binding of a label, we return the binding |
| 703 |
;;; (displaced-lexical . #f) to indicate such. |
| 704 |
|
| 705 |
(define label->binding-no-fluids |
| 706 |
(lambda (x r) |
| 707 |
(cond |
| 708 |
((not x) '(displaced-lexical)) |
| 709 |
((imported-label->binding x) => |
| 710 |
(lambda (b) |
| 711 |
(cond |
| 712 |
((and (pair? b) (eq? (car b) '$core-rtd)) |
| 713 |
(cons '$rtd (map bless (cdr b)))) |
| 714 |
((and (pair? b) (eq? (car b) 'global-rtd)) |
| 715 |
(let ((lib (cadr b)) (loc (cddr b))) |
| 716 |
(cons '$rtd (symbol-value loc)))) |
| 717 |
(else b)))) |
| 718 |
((assq x r) => cdr) |
| 719 |
((top-level-context) => |
| 720 |
(lambda (env) |
| 721 |
(cond |
| 722 |
((assq x (interaction-env-locs env)) => |
| 723 |
(lambda (p) ;;; fabricate |
| 724 |
(cons* 'lexical (cdr p) #f))) |
| 725 |
(else '(displaced-lexical . #f))))) |
| 726 |
(else '(displaced-lexical . #f))))) |
| 727 |
|
| 728 |
(define label->binding |
| 729 |
(lambda (x r) |
| 730 |
(let ([b (label->binding-no-fluids x r)]) |
| 731 |
(if (and (pair? b) (eq? (car b) '$fluid)) |
| 732 |
;;; fluids require reversed logic. We have to look them |
| 733 |
;;; up in the local environment first before the global. |
| 734 |
(let ([x (cdr b)]) |
| 735 |
(cond |
| 736 |
[(assq x r) => cdr] |
| 737 |
[else (label->binding-no-fluids x '())])) |
| 738 |
b)))) |
| 739 |
|
| 740 |
(define make-binding cons) |
| 741 |
(define binding-type car) |
| 742 |
(define binding-value cdr) |
| 743 |
|
| 744 |
;;; the type of an expression is determined by two things: |
| 745 |
;;; - the shape of the expression (identifier, pair, or datum) |
| 746 |
;;; - the binding of the identifier (for id-stx) or the type of |
| 747 |
;;; car of the pair. |
| 748 |
(define (raise-unbound-error id) |
| 749 |
(syntax-violation* #f "unbound identifier" id |
| 750 |
(make-undefined-violation))) |
| 751 |
#; |
| 752 |
(define (syntax-type e r) |
| 753 |
(let-values ([(t0 t1 t2) (syntax-type^ e r)]) |
| 754 |
(printf "T ~s ~s => ~s ~s ~s\n" e r t0 t1 t2) |
| 755 |
(values t0 t1 t2))) |
| 756 |
|
| 757 |
(define syntax-type |
| 758 |
(lambda (e r) |
| 759 |
(cond |
| 760 |
((id? e) |
| 761 |
(let ((id e)) |
| 762 |
(let* ((label (id->label/intern id)) |
| 763 |
(b (label->binding label r)) |
| 764 |
(type (binding-type b))) |
| 765 |
(unless label ;;; fail early. |
| 766 |
(raise-unbound-error id)) |
| 767 |
(case type |
| 768 |
((lexical core-prim macro macro! global local-macro |
| 769 |
local-macro! global-macro global-macro! |
| 770 |
displaced-lexical syntax import export $module |
| 771 |
$core-rtd library mutable ctv local-ctv global-ctv) |
| 772 |
(values type (binding-value b) id)) |
| 773 |
(else (values 'other #f #f)))))) |
| 774 |
((syntax-pair? e) |
| 775 |
(let ((id (syntax-car e))) |
| 776 |
(if (id? id) |
| 777 |
(let* ((label (id->label/intern id)) |
| 778 |
(b (label->binding label r)) |
| 779 |
(type (binding-type b))) |
| 780 |
(unless label ;;; fail early. |
| 781 |
(raise-unbound-error id)) |
| 782 |
(case type |
| 783 |
((define define-syntax core-macro begin macro |
| 784 |
macro! local-macro local-macro! global-macro |
| 785 |
global-macro! module library set! let-syntax |
| 786 |
letrec-syntax import export $core-rtd |
| 787 |
ctv local-ctv global-ctv stale-when |
| 788 |
define-fluid-syntax) |
| 789 |
(values type (binding-value b) id)) |
| 790 |
(else |
| 791 |
(values 'call #f #f)))) |
| 792 |
(values 'call #f #f)))) |
| 793 |
(else (let ((d (stx->datum e))) |
| 794 |
(if (self-evaluating? d) |
| 795 |
(values 'constant d #f) |
| 796 |
(values 'other #f #f))))))) |
| 797 |
|
| 798 |
(define-syntax stx-error |
| 799 |
(lambda (x) |
| 800 |
(syntax-case x () |
| 801 |
((_ stx) |
| 802 |
(syntax (syntax-violation #f "invalid syntax" stx))) |
| 803 |
((_ stx msg) |
| 804 |
(syntax (syntax-violation #f msg stx)))))) |
| 805 |
|
| 806 |
;;; when the rhs of a syntax definition is evaluated, it should be |
| 807 |
;;; either a procedure, an identifier-syntax transformer or an |
| 808 |
;;; ($rtd . #<rtd>) form (ikarus/chez). sanitize-binding converts |
| 809 |
;;; the output to one of: |
| 810 |
;;; (lacal-macro . procedure) |
| 811 |
;;; (local-macro! . procedure) |
| 812 |
;;; (local-ctv . compile-time-value) |
| 813 |
;;; ($rtd . $rtd) |
| 814 |
;;; and signals an assertion-violation otherwise. |
| 815 |
(define sanitize-binding |
| 816 |
(lambda (x src) |
| 817 |
(cond |
| 818 |
((procedure? x) |
| 819 |
(cons* 'local-macro x src)) |
| 820 |
((and (pair? x) (eq? (car x) 'macro!) (procedure? (cdr x))) |
| 821 |
(cons* 'local-macro! (cdr x) src)) |
| 822 |
((and (pair? x) (eq? (car x) '$rtd)) x) |
| 823 |
((and (pair? x) (eq? (car x) 'ctv)) |
| 824 |
(cons* 'local-ctv (cdr x) src)) |
| 825 |
(else (assertion-violation 'expand "invalid transformer" x))))) |
| 826 |
|
| 827 |
;;; r6rs's make-variable-transformer: |
| 828 |
(define make-variable-transformer |
| 829 |
(lambda (x) |
| 830 |
(if (procedure? x) |
| 831 |
(cons 'macro! x) |
| 832 |
(assertion-violation 'make-variable-transformer |
| 833 |
"not a procedure" x)))) |
| 834 |
|
| 835 |
(define make-compile-time-value |
| 836 |
(lambda (x) |
| 837 |
(cons 'ctv x))) |
| 838 |
|
| 839 |
(define (variable-transformer? x) |
| 840 |
(and (pair? x) (eq? (car x) 'macro!) (procedure? (cdr x)))) |
| 841 |
|
| 842 |
(define (variable-transformer-procedure x) |
| 843 |
(if (variable-transformer? x) |
| 844 |
(cdr x) |
| 845 |
(assertion-violation |
| 846 |
'variable-transformer-procedure |
| 847 |
"not a variable transformer" |
| 848 |
x))) |
| 849 |
|
| 850 |
;;; make-eval-transformer takes an expanded expression, |
| 851 |
;;; evaluates it and returns a proper syntactic binding |
| 852 |
;;; for the resulting object. |
| 853 |
(define make-eval-transformer |
| 854 |
(lambda (x) |
| 855 |
(sanitize-binding (eval-core (expanded->core x)) x))) |
| 856 |
|
| 857 |
;;; The syntax-match macro is almost like syntax-case macro. |
| 858 |
;;; Except that: |
| 859 |
;;; The syntax objects matched are OUR stx objects, not |
| 860 |
;;; the host systems syntax objects (whatever they may be |
| 861 |
;;; we don't care). |
| 862 |
;;; The literals are matched against those in the system |
| 863 |
;;; library (psyntax system $all). -- see scheme-stx |
| 864 |
;;; The variables in the patters are bound to ordinary variables |
| 865 |
;;; not to special pattern variables. |
| 866 |
(define-syntax syntax-match |
| 867 |
(lambda (ctx) |
| 868 |
(define convert-pattern |
| 869 |
; returns syntax-dispatch pattern & ids |
| 870 |
(lambda (pattern keys) |
| 871 |
(define cvt* |
| 872 |
(lambda (p* n ids) |
| 873 |
(if (null? p*) |
| 874 |
(values '() ids) |
| 875 |
(let-values (((y ids) (cvt* (cdr p*) n ids))) |
| 876 |
(let-values (((x ids) (cvt (car p*) n ids))) |
| 877 |
(values (cons x y) ids)))))) |
| 878 |
(define free-identifier-member? |
| 879 |
(lambda (x ls) |
| 880 |
(and (exists (lambda (y) (sys.free-identifier=? x y)) ls) #t))) |
| 881 |
(define (bound-id-member? x ls) |
| 882 |
(and (pair? ls) |
| 883 |
(or (sys.bound-identifier=? x (car ls)) |
| 884 |
(bound-id-member? x (cdr ls))))) |
| 885 |
(define ellipsis? |
| 886 |
(lambda (x) |
| 887 |
(and (sys.identifier? x) |
| 888 |
(sys.free-identifier=? x (syntax (... ...)))))) |
| 889 |
(define cvt |
| 890 |
(lambda (p n ids) |
| 891 |
(syntax-case p () |
| 892 |
(id (sys.identifier? (syntax id)) |
| 893 |
(cond |
| 894 |
((bound-id-member? p keys) |
| 895 |
(values `#(scheme-id ,(sys.syntax->datum p)) ids)) |
| 896 |
((sys.free-identifier=? p (syntax _)) |
| 897 |
(values '_ ids)) |
| 898 |
(else (values 'any (cons (cons p n) ids))))) |
| 899 |
((p dots) (ellipsis? (syntax dots)) |
| 900 |
(let-values (((p ids) (cvt (syntax p) (+ n 1) ids))) |
| 901 |
(values |
| 902 |
(if (eq? p 'any) 'each-any `#(each ,p)) |
| 903 |
ids))) |
| 904 |
((x dots ys ... . z) (ellipsis? (syntax dots)) |
| 905 |
(let-values (((z ids) (cvt (syntax z) n ids))) |
| 906 |
(let-values (((ys ids) (cvt* (syntax (ys ...)) n ids))) |
| 907 |
(let-values (((x ids) (cvt (syntax x) (+ n 1) ids))) |
| 908 |
(values `#(each+ ,x ,(reverse ys) ,z) ids))))) |
| 909 |
((x . y) |
| 910 |
(let-values (((y ids) (cvt (syntax y) n ids))) |
| 911 |
(let-values (((x ids) (cvt (syntax x) n ids))) |
| 912 |
(values (cons x y) ids)))) |
| 913 |
(() (values '() ids)) |
| 914 |
(#(p ...) |
| 915 |
(let-values (((p ids) (cvt (syntax (p ...)) n ids))) |
| 916 |
(values `#(vector ,p) ids))) |
| 917 |
(datum |
| 918 |
(values `#(atom ,(sys.syntax->datum (syntax datum))) ids))))) |
| 919 |
(cvt pattern 0 '()))) |
| 920 |
(syntax-case ctx () |
| 921 |
((_ expr (lits ...)) (for-all sys.identifier? (syntax (lits ...))) |
| 922 |
(syntax (stx-error expr "invalid syntax"))) |
| 923 |
((_ expr (lits ...) (pat fender body) cls* ...) |
| 924 |
(for-all sys.identifier? (syntax (lits ...))) |
| 925 |
(let-values (((pattern ids/levels) |
| 926 |
(convert-pattern (syntax pat) (syntax (lits ...))))) |
| 927 |
(with-syntax ((pattern (sys.datum->syntax (syntax here) pattern)) |
| 928 |
(((ids . levels) ...) ids/levels)) |
| 929 |
(syntax |
| 930 |
(let ((t expr)) |
| 931 |
(let ((ls/false (syntax-dispatch t 'pattern))) |
| 932 |
(if (and ls/false (apply (lambda (ids ...) fender) ls/false)) |
| 933 |
(apply (lambda (ids ...) body) ls/false) |
| 934 |
(syntax-match t (lits ...) cls* ...)))))))) |
| 935 |
((_ expr (lits ...) (pat body) cls* ...) |
| 936 |
(for-all sys.identifier? (syntax (lits ...))) |
| 937 |
(let-values (((pattern ids/levels) |
| 938 |
(convert-pattern (syntax pat) (syntax (lits ...))))) |
| 939 |
(with-syntax ((pattern (sys.datum->syntax (syntax here) pattern)) |
| 940 |
(((ids . levels) ...) ids/levels)) |
| 941 |
(syntax |
| 942 |
(let ((t expr)) |
| 943 |
(let ((ls/false (syntax-dispatch t 'pattern))) |
| 944 |
(if ls/false |
| 945 |
(apply (lambda (ids ...) body) ls/false) |
| 946 |
(syntax-match t (lits ...) cls* ...)))))))) |
| 947 |
((_ expr (lits ...) (pat body) cls* ...) |
| 948 |
(syntax (syntax-match expr (lits ...) (pat #t body) cls* ...)))))) |
| 949 |
|
| 950 |
|
| 951 |
(define parse-define |
| 952 |
(lambda (x) |
| 953 |
(syntax-match x () |
| 954 |
((_ (id . fmls) b b* ...) (id? id) |
| 955 |
(begin |
| 956 |
(verify-formals fmls x) |
| 957 |
(values id (cons 'defun x)))) |
| 958 |
((_ id val) (id? id) |
| 959 |
(values id (cons 'expr val))) |
| 960 |
((_ id) (id? id) |
| 961 |
(values id (cons 'expr (bless '(void)))))))) |
| 962 |
|
| 963 |
(define parse-define-syntax |
| 964 |
(lambda (x) |
| 965 |
(syntax-match x () |
| 966 |
((_ id val) (id? id) (values id val))))) |
| 967 |
|
| 968 |
;;; scheme-stx takes a symbol and if it's in the |
| 969 |
;;; (psyntax system $all) library, it creates a fresh identifier |
| 970 |
;;; that maps only the symbol to its label in that library. |
| 971 |
;;; Symbols not in that library become fresh. |
| 972 |
(define scheme-stx-hashtable (make-eq-hashtable)) |
| 973 |
(define scheme-stx |
| 974 |
(lambda (sym) |
| 975 |
(or (hashtable-ref scheme-stx-hashtable sym #f) |
| 976 |
(let* ((subst |
| 977 |
(library-subst |
| 978 |
(find-library-by-name '(psyntax system $all)))) |
| 979 |
(stx (make-stx sym top-mark* '() '())) |
| 980 |
(stx |
| 981 |
(cond |
| 982 |
((assq sym subst) => |
| 983 |
(lambda (x) |
| 984 |
(let ((name (car x)) (label (cdr x))) |
| 985 |
(add-subst |
| 986 |
(make-rib (list name) |
| 987 |
(list top-mark*) (list label) #f) |
| 988 |
stx)))) |
| 989 |
(else stx)))) |
| 990 |
(hashtable-set! scheme-stx-hashtable sym stx) |
| 991 |
stx)))) |
| 992 |
|
| 993 |
;;; macros |
| 994 |
(define lexical-var car) |
| 995 |
(define lexical-mutable? cdr) |
| 996 |
(define set-lexical-mutable! set-cdr!) |
| 997 |
(define add-lexical |
| 998 |
(lambda (lab lex r) |
| 999 |
(cons (cons* lab 'lexical lex #f) r))) |
| 1000 |
;;; |
| 1001 |
(define add-lexicals |
| 1002 |
(lambda (lab* lex* r) |
| 1003 |
(cond |
| 1004 |
((null? lab*) r) |
| 1005 |
(else |
| 1006 |
(add-lexicals (cdr lab*) (cdr lex*) |
| 1007 |
(add-lexical (car lab*) (car lex*) r)))))) |
| 1008 |
;;; |
| 1009 |
(define letrec-helper |
| 1010 |
(lambda (e r mr build) |
| 1011 |
(syntax-match e () |
| 1012 |
((_ ((lhs* rhs*) ...) b b* ...) |
| 1013 |
(if (not (valid-bound-ids? lhs*)) |
| 1014 |
(invalid-fmls-error e lhs*) |
| 1015 |
(let ((lex* (map gen-lexical lhs*)) |
| 1016 |
(lab* (map gen-label lhs*))) |
| 1017 |
(let ((rib (make-full-rib lhs* lab*)) |
| 1018 |
(r (add-lexicals lab* lex* r))) |
| 1019 |
(let ((body (chi-internal |
| 1020 |
(add-subst rib (cons b b*)) r mr)) |
| 1021 |
(rhs* (chi-expr* (map (lambda (x) (add-subst rib x)) |
| 1022 |
rhs*) r mr))) |
| 1023 |
(build no-source lex* rhs* body))))))))) |
| 1024 |
|
| 1025 |
(define letrec-transformer |
| 1026 |
(lambda (e r mr) (letrec-helper e r mr build-letrec))) |
| 1027 |
|
| 1028 |
(define letrec*-transformer |
| 1029 |
(lambda (e r mr) (letrec-helper e r mr build-letrec*))) |
| 1030 |
|
| 1031 |
(define fluid-let-syntax-transformer |
| 1032 |
(lambda (e r mr) |
| 1033 |
(define (lookup x) |
| 1034 |
(let ([label |
| 1035 |
(or (id->label x) |
| 1036 |
(syntax-violation #f "unbound identifier" e x))]) |
| 1037 |
(let ([b (label->binding-no-fluids label r)]) |
| 1038 |
(cond |
| 1039 |
[(and (pair? b) (eq? (car b) '$fluid)) (cdr b)] |
| 1040 |
[else (syntax-violation #f "not a fluid identifier" e x)])))) |
| 1041 |
(syntax-match e () |
| 1042 |
((_ ((lhs* rhs*) ...) b b* ...) |
| 1043 |
(if (not (valid-bound-ids? lhs*)) |
| 1044 |
(invalid-fmls-error e lhs*) |
| 1045 |
(let ((lab* (map lookup lhs*)) |
| 1046 |
(rhs* (map (lambda (x) |
| 1047 |
(make-eval-transformer |
| 1048 |
(expand-transformer x mr))) |
| 1049 |
rhs*))) |
| 1050 |
(chi-internal (cons b b*) |
| 1051 |
(append (map cons lab* rhs*) r) |
| 1052 |
(append (map cons lab* rhs*) mr)))))))) |
| 1053 |
|
| 1054 |
(define type-descriptor-transformer |
| 1055 |
(lambda (e r mr) |
| 1056 |
(syntax-match e () |
| 1057 |
((_ id) (id? id) |
| 1058 |
(let* ((lab (id->label id)) |
| 1059 |
(b (label->binding lab r)) |
| 1060 |
(type (binding-type b))) |
| 1061 |
(unless lab (raise-unbound-error id)) |
| 1062 |
(unless (and (eq? type '$rtd) (not (list? (binding-value b)))) |
| 1063 |
(stx-error e "not a record type")) |
| 1064 |
(build-data no-source (binding-value b))))))) |
| 1065 |
|
| 1066 |
(define record-type-descriptor-transformer |
| 1067 |
(lambda (e r mr) |
| 1068 |
(syntax-match e () |
| 1069 |
((_ id) (id? id) |
| 1070 |
(let* ((lab (id->label id)) |
| 1071 |
(b (label->binding lab r)) |
| 1072 |
(type (binding-type b))) |
| 1073 |
(unless lab (raise-unbound-error id)) |
| 1074 |
(unless (and (eq? type '$rtd) (list? (binding-value b))) |
| 1075 |
(stx-error e "not a record type")) |
| 1076 |
(chi-expr (car (binding-value b)) r mr)))))) |
| 1077 |
|
| 1078 |
(define record-constructor-descriptor-transformer |
| 1079 |
(lambda (e r mr) |
| 1080 |
(syntax-match e () |
| 1081 |
((_ id) (id? id) |
| 1082 |
(let* ((lab (id->label id)) |
| 1083 |
(b (label->binding lab r)) |
| 1084 |
(type (binding-type b))) |
| 1085 |
(unless lab (raise-unbound-error id)) |
| 1086 |
(unless (and (eq? type '$rtd) (list? (binding-value b))) |
| 1087 |
(stx-error e "invalid type")) |
| 1088 |
(chi-expr (cadr (binding-value b)) r mr)))))) |
| 1089 |
|
| 1090 |
(define when-macro |
| 1091 |
(lambda (e) |
| 1092 |
(syntax-match e () |
| 1093 |
((_ test e e* ...) |
| 1094 |
(bless `(if ,test (begin ,e . ,e*))))))) |
| 1095 |
|
| 1096 |
(define unless-macro |
| 1097 |
(lambda (e) |
| 1098 |
(syntax-match e () |
| 1099 |
((_ test e e* ...) |
| 1100 |
(bless `(if (not ,test) (begin ,e . ,e*))))))) |
| 1101 |
|
| 1102 |
(define if-transformer |
| 1103 |
(lambda (e r mr) |
| 1104 |
(syntax-match e () |
| 1105 |
((_ e0 e1 e2) |
| 1106 |
(build-conditional no-source |
| 1107 |
(chi-expr e0 r mr) |
| 1108 |
(chi-expr e1 r mr) |
| 1109 |
(chi-expr e2 r mr))) |
| 1110 |
((_ e0 e1) |
| 1111 |
(build-conditional no-source |
| 1112 |
(chi-expr e0 r mr) |
| 1113 |
(chi-expr e1 r mr) |
| 1114 |
(build-void)))))) |
| 1115 |
|
| 1116 |
(define case-macro |
| 1117 |
(lambda (e) |
| 1118 |
(define (build-last cls) |
| 1119 |
(syntax-match cls (else) |
| 1120 |
((else e e* ...) `(let () #f ,e . ,e*)) |
| 1121 |
(_ (build-one cls '(if #f #f))))) |
| 1122 |
(define (build-one cls k) |
| 1123 |
(syntax-match cls () |
| 1124 |
(((d* ...) e e* ...) |
| 1125 |
`(if (memv t ',d*) (begin ,e . ,e*) ,k)))) |
| 1126 |
(syntax-match e () |
| 1127 |
((_ expr) |
| 1128 |
(bless `(let ((t ,expr)) (if #f #f)))) |
| 1129 |
((_ expr cls cls* ...) |
| 1130 |
(bless |
| 1131 |
`(let ((t ,expr)) |
| 1132 |
,(let f ((cls cls) (cls* cls*)) |
| 1133 |
(if (null? cls*) |
| 1134 |
(build-last cls) |
| 1135 |
(build-one cls (f (car cls*) (cdr cls*))))))))))) |
| 1136 |
|
| 1137 |
|
| 1138 |
(define quote-transformer |
| 1139 |
(lambda (e r mr) |
| 1140 |
(syntax-match e () |
| 1141 |
((_ datum) (build-data no-source (stx->datum datum)))))) |
| 1142 |
|
| 1143 |
(define case-lambda-transformer |
| 1144 |
(lambda (e r mr) |
| 1145 |
(syntax-match e () |
| 1146 |
((_ (fmls* b* b** ...) ...) |
| 1147 |
(let-values (((fmls* body*) |
| 1148 |
(chi-lambda-clause* e fmls* |
| 1149 |
(map cons b* b**) r mr))) |
| 1150 |
(build-case-lambda (syntax-annotation e) fmls* body*)))))) |
| 1151 |
|
| 1152 |
(define lambda-transformer |
| 1153 |
(lambda (e r mr) |
| 1154 |
(syntax-match e () |
| 1155 |
((_ fmls b b* ...) |
| 1156 |
(let-values (((fmls body) |
| 1157 |
(chi-lambda-clause e fmls |
| 1158 |
(cons b b*) r mr))) |
| 1159 |
(build-lambda (syntax-annotation e) fmls body)))))) |
| 1160 |
|
| 1161 |
(define bless |
| 1162 |
(lambda (x) |
| 1163 |
(mkstx |
| 1164 |
(let f ((x x)) |
| 1165 |
(cond |
| 1166 |
((stx? x) x) |
| 1167 |
((pair? x) (cons (f (car x)) (f (cdr x)))) |
| 1168 |
((symbol? x) (scheme-stx x)) |
| 1169 |
((vector? x) |
| 1170 |
(list->vector (map f (vector->list x)))) |
| 1171 |
(else x))) |
| 1172 |
'() '() '()))) |
| 1173 |
|
| 1174 |
(define with-syntax-macro |
| 1175 |
(lambda (e) |
| 1176 |
(syntax-match e () |
| 1177 |
((_ ((pat* expr*) ...) b b* ...) |
| 1178 |
(let ((idn* |
| 1179 |
(let f ((pat* pat*)) |
| 1180 |
(cond |
| 1181 |
((null? pat*) '()) |
| 1182 |
(else |
| 1183 |
(let-values (((pat idn*) (convert-pattern (car pat*) '()))) |
| 1184 |
(append idn* (f (cdr pat*))))))))) |
| 1185 |
(verify-formals (map car idn*) e) |
| 1186 |
(let ((t* (generate-temporaries expr*))) |
| 1187 |
(bless |
| 1188 |
`(let ,(map list t* expr*) |
| 1189 |
,(let f ((pat* pat*) (t* t*)) |
| 1190 |
(cond |
| 1191 |
((null? pat*) `(let () ,b . ,b*)) |
| 1192 |
(else |
| 1193 |
`(syntax-case ,(car t*) () |
| 1194 |
(,(car pat*) ,(f (cdr pat*) (cdr t*))) |
| 1195 |
(_ (assertion-violation 'with-syntax |
| 1196 |
"pattern does not match value" |
| 1197 |
',(car pat*) |
| 1198 |
,(car t*))))))))))))))) |
| 1199 |
|
| 1200 |
(define (invalid-fmls-error stx fmls) |
| 1201 |
(syntax-match fmls () |
| 1202 |
((id* ... . last) |
| 1203 |
(let f ((id* (cond |
| 1204 |
((id? last) (cons last id*)) |
| 1205 |
((syntax-null? last) id*) |
| 1206 |
(else |
| 1207 |
(syntax-violation #f "not an identifier" stx last))))) |
| 1208 |
(cond |
| 1209 |
((null? id*) (values)) |
| 1210 |
((not (id? (car id*))) |
| 1211 |
(syntax-violation #f "not an identifier" stx (car id*))) |
| 1212 |
(else |
| 1213 |
(f (cdr id*)) |
| 1214 |
(when (bound-id-member? (car id*) (cdr id*)) |
| 1215 |
(syntax-violation #f "duplicate binding" stx (car id*))))))) |
| 1216 |
(_ (syntax-violation #f "malformed binding form" stx fmls)))) |
| 1217 |
|
| 1218 |
(define let-macro |
| 1219 |
(lambda (stx) |
| 1220 |
(syntax-match stx () |
| 1221 |
((_ ((lhs* rhs*) ...) b b* ...) |
| 1222 |
(if (valid-bound-ids? lhs*) |
| 1223 |
(bless `((lambda ,lhs* ,b . ,b*) . ,rhs*)) |
| 1224 |
(invalid-fmls-error stx lhs*))) |
| 1225 |
((_ f ((lhs* rhs*) ...) b b* ...) (id? f) |
| 1226 |
(if (valid-bound-ids? lhs*) |
| 1227 |
(bless `((letrec ((,f (lambda ,lhs* ,b . ,b*))) ,f) . ,rhs*)) |
| 1228 |
(invalid-fmls-error stx lhs*)))))) |
| 1229 |
|
| 1230 |
(define trace-let-macro |
| 1231 |
(lambda (stx) |
| 1232 |
(syntax-match stx () |
| 1233 |
((_ f ((lhs* rhs*) ...) b b* ...) (id? f) |
| 1234 |
(if (valid-bound-ids? lhs*) |
| 1235 |
(bless |
| 1236 |
`((letrec ((,f (trace-lambda ,f ,lhs* ,b . ,b*))) ,f) . ,rhs*)) |
| 1237 |
(invalid-fmls-error stx lhs*)))))) |
| 1238 |
|
| 1239 |
(define let-values-macro |
| 1240 |
(lambda (stx) |
| 1241 |
(define (rename x old* new*) |
| 1242 |
(unless (id? x) |
| 1243 |
(syntax-violation #f "not an indentifier" stx x)) |
| 1244 |
(when (bound-id-member? x old*) |
| 1245 |
(syntax-violation #f "duplicate binding" stx x)) |
| 1246 |
(let ((y (gensym (syntax->datum x)))) |
| 1247 |
(values y (cons x old*) (cons y new*)))) |
| 1248 |
(define (rename* x* old* new*) |
| 1249 |
(cond |
| 1250 |
((null? x*) (values '() old* new*)) |
| 1251 |
(else |
| 1252 |
(let*-values (((x old* new*) (rename (car x*) old* new*)) |
| 1253 |
((x* old* new*) (rename* (cdr x*) old* new*))) |
| 1254 |
(values (cons x x*) old* new*))))) |
| 1255 |
(syntax-match stx () |
| 1256 |
((_ () b b* ...) |
| 1257 |
(cons* (bless 'let) '() b b*)) |
| 1258 |
((_ ((lhs* rhs*) ...) b b* ...) |
| 1259 |
(bless |
| 1260 |
(let f ((lhs* lhs*) (rhs* rhs*) (old* '()) (new* '())) |
| 1261 |
(cond |
| 1262 |
((null? lhs*) |
| 1263 |
`(let ,(map list old* new*) ,b . ,b*)) |
| 1264 |
(else |
| 1265 |
(syntax-match (car lhs*) () |
| 1266 |
((x* ...) |
| 1267 |
(let-values (((y* old* new*) (rename* x* old* new*))) |
| 1268 |
`(call-with-values |
| 1269 |
(lambda () ,(car rhs*)) |
| 1270 |
(lambda ,y* |
| 1271 |
,(f (cdr lhs*) (cdr rhs*) old* new*))))) |
| 1272 |
((x* ... . x) |
| 1273 |
(let*-values (((y old* new*) (rename x old* new*)) |
| 1274 |
((y* old* new*) (rename* x* old* new*))) |
| 1275 |
`(call-with-values |
| 1276 |
(lambda () ,(car rhs*)) |
| 1277 |
(lambda ,(append y* y) |
| 1278 |
,(f (cdr lhs*) (cdr rhs*) |
| 1279 |
old* new*))))) |
| 1280 |
(others |
| 1281 |
(syntax-violation #f "malformed bindings" |
| 1282 |
stx others))))))))))) |
| 1283 |
|
| 1284 |
(define let*-values-macro |
| 1285 |
(lambda (stx) |
| 1286 |
(define (check x*) |
| 1287 |
(unless (null? x*) |
| 1288 |
(let ((x (car x*))) |
| 1289 |
(unless (id? x) |
| 1290 |
(syntax-violation #f "not an identifier" stx x)) |
| 1291 |
(check (cdr x*)) |
| 1292 |
(when (bound-id-member? x (cdr x*)) |
| 1293 |
(syntax-violation #f "duplicate identifier" stx x))))) |
| 1294 |
(syntax-match stx () |
| 1295 |
((_ () b b* ...) |
| 1296 |
(cons* (bless 'let) '() b b*)) |
| 1297 |
((_ ((lhs* rhs*) ...) b b* ...) |
| 1298 |
(bless |
| 1299 |
(let f ((lhs* lhs*) (rhs* rhs*)) |
| 1300 |
(cond |
| 1301 |
((null? lhs*) |
| 1302 |
`(begin ,b . ,b*)) |
| 1303 |
(else |
| 1304 |
(syntax-match (car lhs*) () |
| 1305 |
((x* ...) |
| 1306 |
(begin |
| 1307 |
(check x*) |
| 1308 |
`(call-with-values |
| 1309 |
(lambda () ,(car rhs*)) |
| 1310 |
(lambda ,x* |
| 1311 |
,(f (cdr lhs*) (cdr rhs*)))))) |
| 1312 |
((x* ... . x) |
| 1313 |
(begin |
| 1314 |
(check (cons x x*)) |
| 1315 |
`(call-with-values |
| 1316 |
(lambda () ,(car rhs*)) |
| 1317 |
(lambda ,(append x* x) |
| 1318 |
,(f (cdr lhs*) (cdr rhs*)))))) |
| 1319 |
(others |
| 1320 |
(syntax-violation #f "malformed bindings" |
| 1321 |
stx others))))))))))) |
| 1322 |
|
| 1323 |
(define trace-lambda-macro |
| 1324 |
(lambda (stx) |
| 1325 |
(syntax-match stx () |
| 1326 |
((_ who (fmls ...) b b* ...) |
| 1327 |
(if (valid-bound-ids? fmls) |
| 1328 |
(bless `(make-traced-procedure ',who |
| 1329 |
(lambda ,fmls ,b . ,b*))) |
| 1330 |
(invalid-fmls-error stx fmls))) |
| 1331 |
((_ who (fmls ... . last) b b* ...) |
| 1332 |
(if (valid-bound-ids? (cons last fmls)) |
| 1333 |
(bless `(make-traced-procedure ',who |
| 1334 |
(lambda (,@fmls . ,last) ,b . ,b*))) |
| 1335 |
(invalid-fmls-error stx (append fmls last))))))) |
| 1336 |
|
| 1337 |
(define trace-define-macro |
| 1338 |
(lambda (stx) |
| 1339 |
(syntax-match stx () |
| 1340 |
((_ (who fmls ...) b b* ...) |
| 1341 |
(if (valid-bound-ids? fmls) |
| 1342 |
(bless `(define ,who |
| 1343 |
(make-traced-procedure ',who |
| 1344 |
(lambda ,fmls ,b . ,b*)))) |
| 1345 |
(invalid-fmls-error stx fmls))) |
| 1346 |
((_ (who fmls ... . last) b b* ...) |
| 1347 |
(if (valid-bound-ids? (cons last fmls)) |
| 1348 |
(bless `(define ,who |
| 1349 |
(make-traced-procedure ',who |
| 1350 |
(lambda (,@fmls . ,last) ,b . ,b*)))) |
| 1351 |
(invalid-fmls-error stx (append fmls last)))) |
| 1352 |
((_ who expr) |
| 1353 |
(if (id? who) |
| 1354 |
(bless `(define ,who |
| 1355 |
(let ((v ,expr)) |
| 1356 |
(if (procedure? v) |
| 1357 |
(make-traced-procedure ',who v) |
| 1358 |
v)))) |
| 1359 |
(stx-error stx "invalid name")))))) |
| 1360 |
|
| 1361 |
(define trace-define-syntax-macro |
| 1362 |
(lambda (stx) |
| 1363 |
(syntax-match stx () |
| 1364 |
((_ who expr) |
| 1365 |
(if (id? who) |
| 1366 |
(bless |
| 1367 |
`(define-syntax ,who |
| 1368 |
(make-traced-macro ',who ,expr))) |
| 1369 |
(stx-error stx "invalid name")))))) |
| 1370 |
|
| 1371 |
(define trace-let/rec-syntax |
| 1372 |
(lambda (who) |
| 1373 |
(lambda (stx) |
| 1374 |
(syntax-match stx () |
| 1375 |
((_ ((lhs* rhs*) ...) b b* ...) |
| 1376 |
(if (valid-bound-ids? lhs*) |
| 1377 |
(let ((rhs* (map (lambda (lhs rhs) |
| 1378 |
`(make-traced-macro ',lhs ,rhs)) |
| 1379 |
lhs* rhs*))) |
| 1380 |
(bless `(,who ,(map list lhs* rhs*) ,b . ,b*))) |
| 1381 |
(invalid-fmls-error stx lhs*))))))) |
| 1382 |
|
| 1383 |
(define trace-let-syntax-macro |
| 1384 |
(trace-let/rec-syntax 'let-syntax)) |
| 1385 |
|
| 1386 |
(define trace-letrec-syntax-macro |
| 1387 |
(trace-let/rec-syntax 'letrec-syntax)) |
| 1388 |
|
| 1389 |
(define guard-macro |
| 1390 |
(lambda (x) |
| 1391 |
(define (gen-clauses raised-obj con outerk clause*) |
| 1392 |
(define (f x k) |
| 1393 |
(syntax-match x (=>) |
| 1394 |
((e => p) |
| 1395 |
(let ((t (gensym))) |
| 1396 |
`(let ((,t ,e)) |
| 1397 |
(if ,t (,p ,t) ,k)))) |
| 1398 |
((e) |
| 1399 |
(let ((t (gensym))) |
| 1400 |
`(let ((,t ,e)) |
| 1401 |
(if ,t ,t ,k)))) |
| 1402 |
((e v v* ...) |
| 1403 |
`(if ,e (begin ,v ,@v*) ,k)) |
| 1404 |
(_ (stx-error x "invalid guard clause")))) |
| 1405 |
(define (f* x*) |
| 1406 |
(syntax-match x* (else) |
| 1407 |
(() |
| 1408 |
(let ((g (gensym))) |
| 1409 |
(values `(,g (lambda () (raise-continuable ,raised-obj))) g))) |
| 1410 |
(((else e e* ...)) |
| 1411 |
(values `(begin ,e ,@e*) #f)) |
| 1412 |
((cls . cls*) |
| 1413 |
(let-values (((e g) (f* cls*))) |
| 1414 |
(values (f cls e) g))) |
| 1415 |
(others (stx-error others "invalid guard clause")))) |
| 1416 |
(let-values (((code raisek) (f* clause*))) |
| 1417 |
(if raisek |
| 1418 |
`((call/cc |
| 1419 |
(lambda (,raisek) |
| 1420 |
(,outerk |
| 1421 |
(lambda () ,code))))) |
| 1422 |
`(,outerk (lambda () ,code))))) |
| 1423 |
(syntax-match x () |
| 1424 |
((_ (con clause* ...) b b* ...) |
| 1425 |
(id? con) |
| 1426 |
(let ((outerk (gensym)) |
| 1427 |
(raised-obj (gensym))) |
| 1428 |
(bless |
| 1429 |
`((call/cc |
| 1430 |
(lambda (,outerk) |
| 1431 |
(lambda () |
| 1432 |
(with-exception-handler |
| 1433 |
(lambda (,raised-obj) |
| 1434 |
(let ((,con ,raised-obj)) |
| 1435 |
,(gen-clauses raised-obj con outerk clause*))) |
| 1436 |
(lambda () ,b ,@b*)))))))))))) |
| 1437 |
|
| 1438 |
(define define-enumeration-macro |
| 1439 |
(lambda (stx) |
| 1440 |
(define (set? x) |
| 1441 |
(or (null? x) |
| 1442 |
(and (not (memq (car x) (cdr x))) |
| 1443 |
(set? (cdr x))))) |
| 1444 |
(define (remove-dups ls) |
| 1445 |
(cond |
| 1446 |
((null? ls) '()) |
| 1447 |
(else |
| 1448 |
(cons (car ls) |
| 1449 |
(remove-dups (remq (car ls) (cdr ls))))))) |
| 1450 |
(syntax-match stx () |
| 1451 |
((_ name (id* ...) maker) |
| 1452 |
(and (id? name) (id? maker) (for-all id? id*)) |
| 1453 |
(let ((name* (remove-dups (syntax->datum id*))) (mk (gensym))) |
| 1454 |
(bless |
| 1455 |
`(begin |
| 1456 |
;;; can be constructed at compile time |
| 1457 |
;;; but .... it's not worth it. |
| 1458 |
;;; also, generativity of defined enum types |
| 1459 |
;;; is completely unspecified, making them just |
| 1460 |
;;; more useless than they really are. |
| 1461 |
;;; eventually, I'll make them all compile-time |
| 1462 |
;;; generative just to piss some known people off. |
| 1463 |
(define ,mk |
| 1464 |
(enum-set-constructor |
| 1465 |
(make-enumeration ',name*))) |
| 1466 |
(define-syntax ,name |
| 1467 |
(lambda (x) |
| 1468 |
(syntax-case x () |
| 1469 |
((_ n) |
| 1470 |
(identifier? (syntax n)) |
| 1471 |
(if (memq (syntax->datum (syntax n)) ',name*) |
| 1472 |
(syntax 'n) |
| 1473 |
(syntax-violation ',name |
| 1474 |
"not a member of set" |
| 1475 |
x (syntax n))))))) |
| 1476 |
(define-syntax ,maker |
| 1477 |
(lambda (x) |
| 1478 |
(syntax-case x () |
| 1479 |
((_ n* ...) |
| 1480 |
(begin |
| 1481 |
(for-each |
| 1482 |
(lambda (n) |
| 1483 |
(unless (identifier? n) |
| 1484 |
(syntax-violation |
| 1485 |
',maker |
| 1486 |
"non-identifier argument" |
| 1487 |
x |
| 1488 |
n)) |
| 1489 |
(unless (memq (syntax->datum n) ',name*) |
| 1490 |
(syntax-violation |
| 1491 |
',maker |
| 1492 |
"not a member of set" |
| 1493 |
x |
| 1494 |
n))) |
| 1495 |
(syntax (n* ...))) |
| 1496 |
(syntax (,mk '(n* ...))))))))))))))) |
| 1497 |
|
| 1498 |
(define time-macro |
| 1499 |
(lambda (stx) |
| 1500 |
(syntax-match stx () |
| 1501 |
((_ expr) |
| 1502 |
(let ((str |
| 1503 |
(let-values (((p e) (open-string-output-port))) |
| 1504 |
(write (syntax->datum expr) p) |
| 1505 |
(e)))) |
| 1506 |
(bless `(time-it ,str (lambda () ,expr)))))))) |
| 1507 |
|
| 1508 |
(define delay-macro |
| 1509 |
(lambda (stx) |
| 1510 |
(syntax-match stx () |
| 1511 |
((_ expr) |
| 1512 |
(bless `(make-promise (lambda () ,expr))))))) |
| 1513 |
|
| 1514 |
(define assert-macro |
| 1515 |
(lambda (stx) |
| 1516 |
(syntax-match stx () |
| 1517 |
((_ expr) |
| 1518 |
(let ((pos (or (expression-position stx) |
| 1519 |
(expression-position expr)))) |
| 1520 |
(bless |
| 1521 |
`(or ,expr (assertion-error ',expr ',pos)))))))) |
| 1522 |
|
| 1523 |
(define endianness-macro |
| 1524 |
(lambda (stx) |
| 1525 |
(syntax-match stx () |
| 1526 |
((_ e) |
| 1527 |
(case (syntax->datum e) |
| 1528 |
((little) (bless `'little)) |
| 1529 |
((big) (bless `'big)) |
| 1530 |
(else (stx-error stx "endianness must be big or little"))))))) |
| 1531 |
|
| 1532 |
(define identifier-syntax-macro |
| 1533 |
(lambda (stx) |
| 1534 |
(syntax-match stx (set!) |
| 1535 |
((_ expr) |
| 1536 |
(bless `(lambda (x) |
| 1537 |
(syntax-case x () |
| 1538 |
(id (identifier? (syntax id)) (syntax ,expr)) |
| 1539 |
((id e* ...) (identifier? (syntax id)) |
| 1540 |
(cons (syntax ,expr) (syntax (e* ...)))))))) |
| 1541 |
((_ (id1 expr1) ((set! id2 expr2) expr3)) |
| 1542 |
(and (id? id1) (id? id2) (id? expr2)) |
| 1543 |
(bless `(cons 'macro! |
| 1544 |
(lambda (x) |
| 1545 |
(syntax-case x (set!) |
| 1546 |
(id (identifier? (syntax id)) (syntax ,expr1)) |
| 1547 |
((set! id ,expr2) (syntax ,expr3)) |
| 1548 |
((id e* ...) (identifier? (syntax id)) (syntax (,expr1 e* ...))))))))))) |
| 1549 |
|
| 1550 |
(define do-macro |
| 1551 |
(lambda (stx) |
| 1552 |
(define bind |
| 1553 |
(lambda (x) |
| 1554 |
(syntax-match x () |
| 1555 |
((x init) `(,x ,init ,x)) |
| 1556 |
((x init step) `(,x ,init ,step)) |
| 1557 |
(_ (stx-error stx "invalid binding"))))) |
| 1558 |
(syntax-match stx () |
| 1559 |
((_ (binding* ...) |
| 1560 |
(test expr* ...) |
| 1561 |
command* ...) |
| 1562 |
(syntax-match (map bind binding*) () |
| 1563 |
(((x* init* step*) ...) |
| 1564 |
(if (valid-bound-ids? x*) |
| 1565 |
(bless |
| 1566 |
`(letrec ((loop |
| 1567 |
(lambda ,x* |
| 1568 |
(if ,test |
| 1569 |
(begin (if #f #f) ,@expr*) |
| 1570 |
(begin |
| 1571 |
,@command* |
| 1572 |
(loop ,@step*)))))) |
| 1573 |
(loop ,@init*))) |
| 1574 |
(stx-error stx "invalid bindings")))))))) |
| 1575 |
|
| 1576 |
(define let*-macro |
| 1577 |
(lambda (stx) |
| 1578 |
(syntax-match stx () |
| 1579 |
((_ ((lhs* rhs*) ...) b b* ...) (for-all id? lhs*) |
| 1580 |
(bless |
| 1581 |
(let f ((x* (map list lhs* rhs*))) |
| 1582 |
(cond |
| 1583 |
((null? x*) `(let () ,b . ,b*)) |
| 1584 |
(else `(let (,(car x*)) ,(f (cdr x*))))))))))) |
| 1585 |
|
| 1586 |
(define or-macro |
| 1587 |
(lambda (stx) |
| 1588 |
(syntax-match stx () |
| 1589 |
((_) #f) |
| 1590 |
((_ e e* ...) |
| 1591 |
(bless |
| 1592 |
(let f ((e e) (e* e*)) |
| 1593 |
(cond |
| 1594 |
((null? e*) `(begin #f ,e)) |
| 1595 |
(else |
| 1596 |
`(let ((t ,e)) |
| 1597 |
(if t t ,(f (car e*) (cdr e*)))))))))))) |
| 1598 |
|
| 1599 |
(define and-macro |
| 1600 |
(lambda (stx) |
| 1601 |
(syntax-match stx () |
| 1602 |
((_) #t) |
| 1603 |
((_ e e* ...) |
| 1604 |
(bless |
| 1605 |
(let f ((e e) (e* e*)) |
| 1606 |
(cond |
| 1607 |
((null? e*) `(begin #f ,e)) |
| 1608 |
(else `(if ,e ,(f (car e*) (cdr e*)) #f))))))))) |
| 1609 |
|
| 1610 |
(define cond-macro |
| 1611 |
(lambda (stx) |
| 1612 |
(syntax-match stx () |
| 1613 |
((_ cls cls* ...) |
| 1614 |
(bless |
| 1615 |
(let f ((cls cls) (cls* cls*)) |
| 1616 |
(cond |
| 1617 |
((null? cls*) |
| 1618 |
(syntax-match cls (else =>) |
| 1619 |
((else e e* ...) `(let () #f ,e . ,e*)) |
| 1620 |
((e => p) `(let ((t ,e)) (if t (,p t)))) |
| 1621 |
((e) `(or ,e (if #f #f))) |
| 1622 |
((e e* ...) `(if ,e (begin . ,e*))) |
| 1623 |
(_ (stx-error stx "invalid last clause")))) |
| 1624 |
(else |
| 1625 |
(syntax-match cls (else =>) |
| 1626 |
((else e e* ...) (stx-error stx "incorrect position of keyword else")) |
| 1627 |
((e => p) `(let ((t ,e)) (if t (,p t) ,(f (car cls*) (cdr cls*))))) |
| 1628 |
((e) `(or ,e ,(f (car cls*) (cdr cls*)))) |
| 1629 |
((e e* ...) `(if ,e (begin . ,e*) ,(f (car cls*) (cdr cls*)))) |
| 1630 |
(_ (stx-error stx "invalid last clause"))))))))))) |
| 1631 |
|
| 1632 |
(define syntax-rules-macro |
| 1633 |
(lambda (e) |
| 1634 |
(syntax-match e () |
| 1635 |
((_ (lits ...) |
| 1636 |
(pat* tmp*) ...) |
| 1637 |
(begin |
| 1638 |
(verify-literals lits e) |
| 1639 |
(bless `(lambda (x) |
| 1640 |
(syntax-case x ,lits |
| 1641 |
,@(map (lambda (pat tmp) |
| 1642 |
(syntax-match pat () |
| 1643 |
((_ . rest) |
| 1644 |
`((g . ,rest) (syntax ,tmp))) |
| 1645 |
(_ |
| 1646 |
(syntax-violation #f |
| 1647 |
"invalid syntax-rules pattern" |
| 1648 |
e pat)))) |
| 1649 |
pat* tmp*))))))))) |
| 1650 |
|
| 1651 |
(define quasiquote-macro |
| 1652 |
(let () |
| 1653 |
(define (datum x) |
| 1654 |
(list (scheme-stx 'quote) (mkstx x top-mark* '() '()))) |
| 1655 |
(define-syntax app |
| 1656 |
(syntax-rules (quote) |
| 1657 |
((_ 'x arg* ...) |
| 1658 |
(list (scheme-stx 'x) arg* ...)))) |
| 1659 |
(define-syntax app* |
| 1660 |
(syntax-rules (quote) |
| 1661 |
((_ 'x arg* ... last) |
| 1662 |
(cons* (scheme-stx 'x) arg* ... last)))) |
| 1663 |
(define quasicons* |
| 1664 |
(lambda (x y) |
| 1665 |
(let f ((x x)) |
| 1666 |
(if (null? x) y (quasicons (car x) (f (cdr x))))))) |
| 1667 |
(define quasicons |
| 1668 |
(lambda (x y) |
| 1669 |
(syntax-match y (quote list) |
| 1670 |
((quote dy) |
| 1671 |
(syntax-match x (quote) |
| 1672 |
((quote dx) (app 'quote (cons dx dy))) |
| 1673 |
(_ |
| 1674 |
(syntax-match dy () |
| 1675 |
(() (app 'list x)) |
| 1676 |
(_ (app 'cons x y)))))) |
| 1677 |
((list stuff ...) |
| 1678 |
(app* 'list x stuff)) |
| 1679 |
(_ (app 'cons x y))))) |
| 1680 |
(define quasiappend |
| 1681 |
(lambda (x y) |
| 1682 |
(let ((ls (let f ((x x)) |
| 1683 |
(if (null? x) |
| 1684 |
(syntax-match y (quote) |
| 1685 |
((quote ()) '()) |
| 1686 |
(_ (list y))) |
| 1687 |
(syntax-match (car x) (quote) |
| 1688 |
((quote ()) (f (cdr x))) |
| 1689 |
(_ (cons (car x) (f (cdr x))))))))) |
| 1690 |
(cond |
| 1691 |
((null? ls) (app 'quote '())) |
| 1692 |
((null? (cdr ls)) (car ls)) |
| 1693 |
(else (app* 'append ls)))))) |
| 1694 |
(define quasivector |
| 1695 |
(lambda (x) |
| 1696 |
(let ((pat-x x)) |
| 1697 |
(syntax-match pat-x (quote) |
| 1698 |
((quote (x* ...)) (app 'quote (list->vector x*))) |
| 1699 |
(_ (let f ((x x) (k (lambda (ls) (app* 'vector ls)))) |
| 1700 |
(syntax-match x (quote list cons) |
| 1701 |
((quote (x* ...)) |
| 1702 |
(k (map (lambda (x) (app 'quote x)) x*))) |
| 1703 |
((list x* ...) |
| 1704 |
(k x*)) |
| 1705 |
((cons x y) |
| 1706 |
(f y (lambda (ls) (k (cons x ls))))) |
| 1707 |
(_ (app 'list->vector pat-x))))))))) |
| 1708 |
(define vquasi |
| 1709 |
(lambda (p lev) |
| 1710 |
(syntax-match p () |
| 1711 |
((p . q) |
| 1712 |
(syntax-match p (unquote unquote-splicing) |
| 1713 |
((unquote p ...) |
| 1714 |
(if (= lev 0) |
| 1715 |
(quasicons* p (vquasi q lev)) |
| 1716 |
(quasicons |
| 1717 |
(quasicons (datum 'unquote) |
| 1718 |
(quasi p (- lev 1))) |
| 1719 |
(vquasi q lev)))) |
| 1720 |
((unquote-splicing p ...) |
| 1721 |
(if (= lev 0) |
| 1722 |
(quasiappend p (vquasi q lev)) |
| 1723 |
(quasicons |
| 1724 |
(quasicons |
| 1725 |
(datum 'unquote-splicing) |
| 1726 |
(quasi p (- lev 1))) |
| 1727 |
(vquasi q lev)))) |
| 1728 |
(p (quasicons (quasi p lev) (vquasi q lev))))) |
| 1729 |
(() (app 'quote '()))))) |
| 1730 |
(define quasi |
| 1731 |
(lambda (p lev) |
| 1732 |
(syntax-match p (unquote unquote-splicing quasiquote) |
| 1733 |
((unquote p) |
| 1734 |
(if (= lev 0) |
| 1735 |
p |
| 1736 |
(quasicons (datum 'unquote) (quasi (list p) (- lev 1))))) |
| 1737 |
(((unquote p ...) . q) |
| 1738 |
(if (= lev 0) |
| 1739 |
(quasicons* p (quasi q lev)) |
| 1740 |
(quasicons |
| 1741 |
(quasicons (datum 'unquote) |
| 1742 |
(quasi p (- lev 1))) |
| 1743 |
(quasi q lev)))) |
| 1744 |
(((unquote-splicing p ...) . q) |
| 1745 |
(if (= lev 0) |
| 1746 |
(quasiappend p (quasi q lev)) |
| 1747 |
(quasicons |
| 1748 |
(quasicons (datum 'unquote-splicing) |
| 1749 |
(quasi p (- lev 1))) |
| 1750 |
(quasi q lev)))) |
| 1751 |
((quasiquote p) |
| 1752 |
(quasicons (datum 'quasiquote) |
| 1753 |
(quasi (list p) (+ lev 1)))) |
| 1754 |
((p . q) (quasicons (quasi p lev) (quasi q lev))) |
| 1755 |
(#(x ...) (not (stx? x)) (quasivector (vquasi x lev))) |
| 1756 |
(p (app 'quote p))))) |
| 1757 |
(lambda (x) |
| 1758 |
(syntax-match x () |
| 1759 |
((_ e) (quasi e 0)))))) |
| 1760 |
|
| 1761 |
(define quasisyntax-macro |
| 1762 |
(let () ;;; FIXME: not really correct |
| 1763 |
(define quasi |
| 1764 |
(lambda (p lev) |
| 1765 |
(syntax-match p (unsyntax unsyntax-splicing quasisyntax) |
| 1766 |
((unsyntax p) |
| 1767 |
(if (= lev 0) |
| 1768 |
(let ((g (gensym))) |
| 1769 |
(values (list g) (list p) g)) |
| 1770 |
(let-values (((lhs* rhs* p) (quasi p (- lev 1)))) |
| 1771 |
(values lhs* rhs* (list 'unsyntax p))))) |
| 1772 |
(unsyntax (= lev 0) |
| 1773 |
(stx-error p "incorrect use of unsyntax")) |
| 1774 |
(((unsyntax p* ...) . q) |
| 1775 |
(let-values (((lhs* rhs* q) (quasi q lev))) |
| 1776 |
(if (= lev 0) |
| 1777 |
(let ((g* (map (lambda (x) (gensym)) p*))) |
| 1778 |
(values |
| 1779 |
(append g* lhs*) |
| 1780 |
(append p* rhs*) |
| 1781 |
(append g* q))) |
| 1782 |
(let-values (((lhs2* rhs2* p*) (quasi p* (- lev 1)))) |
| 1783 |
(values |
| 1784 |
(append lhs2* lhs*) |
| 1785 |
(append rhs2* rhs*) |
| 1786 |
`((unsyntax . ,p*) . ,q)))))) |
| 1787 |
(((unsyntax-splicing p* ...) . q) |
| 1788 |
(let-values (((lhs* rhs* q) (quasi q lev))) |
| 1789 |
(if (= lev 0) |
| 1790 |
(let ((g* (map (lambda (x) (gensym)) p*))) |
| 1791 |
(values |
| 1792 |
(append |
| 1793 |
(map (lambda (g) `(,g ...)) g*) |
| 1794 |
lhs*) |
| 1795 |
(append p* rhs*) |
| 1796 |
(append |
| 1797 |
(apply append |
| 1798 |
(map (lambda (g) `(,g ...)) g*)) |
| 1799 |
q))) |
| 1800 |
(let-values (((lhs2* rhs2* p*) (quasi p* (- lev 1)))) |
| 1801 |
(values |
| 1802 |
(append lhs2* lhs*) |
| 1803 |
(append rhs2* rhs*) |
| 1804 |
`((unsyntax-splicing . ,p*) . ,q)))))) |
| 1805 |
(unsyntax-splicing (= lev 0) |
| 1806 |
(stx-error p "incorrect use of unsyntax-splicing")) |
| 1807 |
((quasisyntax p) |
| 1808 |
(let-values (((lhs* rhs* p) (quasi p (+ lev 1)))) |
| 1809 |
(values lhs* rhs* `(quasisyntax ,p)))) |
| 1810 |
((p . q) |
| 1811 |
(let-values (((lhs* rhs* p) (quasi p lev)) |
| 1812 |
((lhs2* rhs2* q) (quasi q lev))) |
| 1813 |
(values (append lhs2* lhs*) |
| 1814 |
(append rhs2* rhs*) |
| 1815 |
(cons p q)))) |
| 1816 |
(#(x* ...) |
| 1817 |
(let-values (((lhs* rhs* x*) (quasi x* lev))) |
| 1818 |
(values lhs* rhs* (list->vector x*)))) |
| 1819 |
(_ (values '() '() p))))) |
| 1820 |
(lambda (x) |
| 1821 |
(syntax-match x () |
| 1822 |
((_ e) |
| 1823 |
(let-values (((lhs* rhs* v) (quasi e 0))) |
| 1824 |
(bless |
| 1825 |
`(syntax-case (list ,@rhs*) () |
| 1826 |
(,lhs* (syntax ,v)))))))))) |
| 1827 |
|
| 1828 |
|
| 1829 |
(define define-struct-macro |
| 1830 |
(if-wants-define-struct |
| 1831 |
(lambda (e) |
| 1832 |
(define enumerate |
| 1833 |
(lambda (ls) |
| 1834 |
(let f ((i 0) (ls ls)) |
| 1835 |
(cond |
| 1836 |
((null? ls) '()) |
| 1837 |
(else (cons i (f (+ i 1) (cdr ls)))))))) |
| 1838 |
(define mkid |
| 1839 |
(lambda (id str) |
| 1840 |
(datum->stx id (string->symbol str)))) |
| 1841 |
(syntax-match e () |
| 1842 |
((_ name (field* ...)) |
| 1843 |
(let* ((namestr (symbol->string (id->sym name))) |
| 1844 |
(fields (map id->sym field*)) |
| 1845 |
(fieldstr* (map symbol->string fields)) |
| 1846 |
(rtd (datum->stx name (make-struct-type namestr fields))) |
| 1847 |
(constr (mkid name (string-append "make-" namestr))) |
| 1848 |
(pred (mkid name (string-append namestr "?"))) |
| 1849 |
(i* (enumerate field*)) |
| 1850 |
(getters |
| 1851 |
(map (lambda (x) |
| 1852 |
(mkid name (string-append namestr "-" x))) |
| 1853 |
fieldstr*)) |
| 1854 |
(setters |
| 1855 |
(map (lambda (x) |
| 1856 |
(mkid name (string-append "set-" namestr "-" x "!"))) |
| 1857 |
fieldstr*))) |
| 1858 |
(bless |
| 1859 |
`(begin |
| 1860 |
(define-syntax ,name (cons '$rtd ',rtd)) |
| 1861 |
(define ,constr |
| 1862 |
(lambda ,field* |
| 1863 |
($struct ',rtd ,@field*))) |
| 1864 |
(define ,pred |
| 1865 |
(lambda (x) ($struct/rtd? x ',rtd))) |
| 1866 |
,@(map (lambda (getter i) |
| 1867 |
`(define ,getter |
| 1868 |
(lambda (x) |
| 1869 |
(if ($struct/rtd? x ',rtd) |
| 1870 |
($struct-ref x ,i) |
| 1871 |
(assertion-violation ',getter |
| 1872 |
"not a struct of required type" |
| 1873 |
x ',rtd))))) |
| 1874 |
getters i*) |
| 1875 |
,@(map (lambda (setter i) |
| 1876 |
`(define ,setter |
| 1877 |
(lambda (x v) |
| 1878 |
(if ($struct/rtd? x ',rtd) |
| 1879 |
($struct-set! x ,i v) |
| 1880 |
(assertion-violation ',setter |
| 1881 |
"not a struct of required type" |
| 1882 |
x ',rtd))))) |
| 1883 |
setters i*))))))) |
| 1884 |
(lambda (stx) |
| 1885 |
(stx-error stx "define-struct not supported")))) |
| 1886 |
|
| 1887 |
|
| 1888 |
(define define-record-type-macro |
| 1889 |
(lambda (x) |
| 1890 |
(define (id ctxt . str*) |
| 1891 |
(datum->syntax ctxt |
| 1892 |
(string->symbol |
| 1893 |
(apply string-append |
| 1894 |
(map (lambda (x) |
| 1895 |
(cond |
| 1896 |
((symbol? x) (symbol->string x)) |
| 1897 |
((string? x) x) |
| 1898 |
(else (assertion-violation 'define-record-type "BUG")))) |
| 1899 |
str*))))) |
| 1900 |
(define (get-record-name spec) |
| 1901 |
(syntax-match spec () |
| 1902 |
((foo make-foo foo?) foo) |
| 1903 |
(foo foo))) |
| 1904 |
(define (get-record-constructor-name spec) |
| 1905 |
(syntax-match spec () |
| 1906 |
((foo make-foo foo?) make-foo) |
| 1907 |
(foo (id? foo) (id foo "make-" (stx->datum foo))))) |
| 1908 |
(define (get-record-predicate-name spec) |
| 1909 |
(syntax-match spec () |
| 1910 |
((foo make-foo foo?) foo?) |
| 1911 |
(foo (id? foo) (id foo (stx->datum foo) "?")))) |
| 1912 |
(define (get-clause id ls) |
| 1913 |
(syntax-match ls () |
| 1914 |
(() #f) |
| 1915 |
(((x . rest) . ls) |
| 1916 |
(if (free-id=? (bless id) x) |
| 1917 |
`(,x . ,rest) |
| 1918 |
(get-clause id ls))))) |
| 1919 |
(define (foo-rtd-code name clause* parent-rtd-code) |
| 1920 |
(define (convert-field-spec* ls) |
| 1921 |
(list->vector |
| 1922 |
(map (lambda (x) |
| 1923 |
(syntax-match x (mutable immutable) |
| 1924 |
((mutable name . rest) `(mutable ,name)) |
| 1925 |
((immutable name . rest) `(immutable ,name)) |
| 1926 |
(name `(immutable ,name)))) |
| 1927 |
ls))) |
| 1928 |
(let ((uid-code |
| 1929 |
(syntax-match (get-clause 'nongenerative clause*) () |
| 1930 |
((_) `',(gensym)) |
| 1931 |
((_ uid) `',uid) |
| 1932 |
(_ #f))) |
| 1933 |
(sealed? |
| 1934 |
(syntax-match (get-clause 'sealed clause*) () |
| 1935 |
((_ #t) #t) |
| 1936 |
(_ #f))) |
| 1937 |
(opaque? |
| 1938 |
(syntax-match (get-clause 'opaque clause*) () |
| 1939 |
((_ #t) #t) |
| 1940 |
(_ #f))) |
| 1941 |
(fields |
| 1942 |
(syntax-match (get-clause 'fields clause*) () |
| 1943 |
((_ field-spec* ...) |
| 1944 |
`(quote ,(convert-field-spec* field-spec*))) |
| 1945 |
(_ ''#())))) |
| 1946 |
(bless |
| 1947 |
`(make-record-type-descriptor ',name |
| 1948 |
,parent-rtd-code |
| 1949 |
,uid-code ,sealed? ,opaque? ,fields)))) |
| 1950 |
(define (parent-rtd-code clause*) |
| 1951 |
(syntax-match (get-clause 'parent clause*) () |
| 1952 |
((_ name) `(record-type-descriptor ,name)) |
| 1953 |
(#f (syntax-match (get-clause 'parent-rtd clause*) () |
| 1954 |
((_ rtd rcd) rtd) |
| 1955 |
(#f #f))))) |
| 1956 |
(define (parent-rcd-code clause*) |
| 1957 |
(syntax-match (get-clause 'parent clause*) () |
| 1958 |
((_ name) `(record-constructor-descriptor ,name)) |
| 1959 |
(#f (syntax-match (get-clause 'parent-rtd clause*) () |
| 1960 |
((_ rtd rcd) rcd) |
| 1961 |
(#f #f))))) |
| 1962 |
(define (foo-rcd-code clause* foo-rtd protocol parent-rcd-code) |
| 1963 |
`(make-record-constructor-descriptor ,foo-rtd |
| 1964 |
,parent-rcd-code ,protocol)) |
| 1965 |
(define (get-protocol-code clause*) |
| 1966 |
(syntax-match (get-clause 'protocol clause*) () |
| 1967 |
((_ expr) expr) |
| 1968 |
(_ #f))) |
| 1969 |
(define (get-fields clause*) |
| 1970 |
(syntax-match clause* (fields) |
| 1971 |
(() '()) |
| 1972 |
(((fields f* ...) . _) f*) |
| 1973 |
((_ . rest) (get-fields rest)))) |
| 1974 |
(define (get-mutator-indices fields) |
| 1975 |
(let f ((fields fields) (i 0)) |
| 1976 |
(syntax-match fields (mutable) |
| 1977 |
(() '()) |
| 1978 |
(((mutable . _) . rest) |
| 1979 |
(cons i (f rest (+ i 1)))) |
| 1980 |
((_ . rest) |
| 1981 |
(f rest (+ i 1)))))) |
| 1982 |
(define (get-mutators foo fields) |
| 1983 |
(define (gen-name x) |
| 1984 |
(datum->syntax foo |
| 1985 |
(string->symbol |
| 1986 |
(string-append |
| 1987 |
(symbol->string (syntax->datum foo)) |
| 1988 |
"-" |
| 1989 |
(symbol->string (syntax->datum x)) |
| 1990 |
"-set!")))) |
| 1991 |
(let f ((fields fields)) |
| 1992 |
(syntax-match fields (mutable) |
| 1993 |
(() '()) |
| 1994 |
(((mutable name accessor mutator) . rest) |
| 1995 |
(cons mutator (f rest))) |
| 1996 |
(((mutable name) . rest) |
| 1997 |
(cons (gen-name name) (f rest))) |
| 1998 |
((_ . rest) (f rest))))) |
| 1999 |
(define (get-accessors foo fields) |
| 2000 |
(define (gen-name x) |
| 2001 |
(datum->syntax foo |
| 2002 |
(string->symbol |
| 2003 |
(string-append |
| 2004 |
(symbol->string (syntax->datum foo)) |
| 2005 |
"-" |
| 2006 |
(symbol->string (syntax->datum x)))))) |
| 2007 |
(map |
| 2008 |
(lambda (field) |
| 2009 |
(syntax-match field (mutable immutable) |
| 2010 |
((mutable name accessor mutator) (id? accessor) accessor) |
| 2011 |
((immutable name accessor) (id? accessor) accessor) |
| 2012 |
((mutable name) (id? name) (gen-name name)) |
| 2013 |
((immutable name) (id? name) (gen-name name)) |
| 2014 |
(name (id? name) (gen-name name)) |
| 2015 |
(others (stx-error field "invalid field spec")))) |
| 2016 |
fields)) |
| 2017 |
(define (enumerate ls) |
| 2018 |
(let f ((ls ls) (i 0)) |
| 2019 |
(cond |
| 2020 |
((null? ls) '()) |
| 2021 |
(else (cons i (f (cdr ls) (+ i 1))))))) |
| 2022 |
(define (do-define-record namespec clause*) |
| 2023 |
(let* ((foo (get-record-name namespec)) |
| 2024 |
(foo-rtd (gensym)) |
| 2025 |
(foo-rcd (gensym)) |
| 2026 |
(protocol (gensym)) |
| 2027 |
(make-foo (get-record-constructor-name namespec)) |
| 2028 |
(fields (get-fields clause*)) |
| 2029 |
(idx* (enumerate fields)) |
| 2030 |
(foo-x* (get-accessors foo fields)) |
| 2031 |
(set-foo-x!* (get-mutators foo fields)) |
| 2032 |
(set-foo-idx* (get-mutator-indices fields)) |
| 2033 |
(foo? (get-record-predicate-name namespec)) |
| 2034 |
(foo-rtd-code (foo-rtd-code foo clause* (parent-rtd-code clause*))) |
| 2035 |
(foo-rcd-code (foo-rcd-code clause* foo-rtd protocol (parent-rcd-code clause*))) |
| 2036 |
(protocol-code (get-protocol-code clause*))) |
| 2037 |
(bless |
| 2038 |
`(begin |
| 2039 |
(define ,foo-rtd ,foo-rtd-code) |
| 2040 |
(define ,protocol ,protocol-code) |
| 2041 |
(define ,foo-rcd ,foo-rcd-code) |
| 2042 |
(define-syntax ,foo |
| 2043 |
(list '$rtd (syntax ,foo-rtd) (syntax ,foo-rcd))) |
| 2044 |
(define ,foo? (record-predicate ,foo-rtd)) |
| 2045 |
(define ,make-foo (record-constructor ,foo-rcd)) |
| 2046 |
,@(map |
| 2047 |
(lambda (foo-x idx) |
| 2048 |
`(define ,foo-x (record-accessor ,foo-rtd ,idx))) |
| 2049 |
foo-x* idx*) |
| 2050 |
,@(map |
| 2051 |
(lambda (set-foo-x! idx) |
| 2052 |
`(define ,set-foo-x! (record-mutator ,foo-rtd ,idx))) |
| 2053 |
set-foo-x!* set-foo-idx*))))) |
| 2054 |
(define (verify-clauses x cls*) |
| 2055 |
(define valid-kwds |
| 2056 |
(map bless |
| 2057 |
'(fields parent parent-rtd protocol sealed opaque nongenerative))) |
| 2058 |
(define (free-id-member? x ls) |
| 2059 |
(and (pair? ls) |
| 2060 |
(or (free-id=? x (car ls)) |
| 2061 |
(free-id-member? x (cdr ls))))) |
| 2062 |
(let f ((cls* cls*) (seen* '())) |
| 2063 |
(unless (null? cls*) |
| 2064 |
(syntax-match (car cls*) () |
| 2065 |
((kwd . rest) |
| 2066 |
(cond |
| 2067 |
((or (not (id? kwd)) |
| 2068 |
(not (free-id-member? kwd valid-kwds))) |
| 2069 |
(stx-error kwd "not a valid define-record-type keyword")) |
| 2070 |
((bound-id-member? kwd seen*) |
| 2071 |
(syntax-violation #f |
| 2072 |
"duplicate use of keyword " |
| 2073 |
x kwd)) |
| 2074 |
(else (f (cdr cls*) (cons kwd seen*))))) |
| 2075 |
(cls |
| 2076 |
(stx-error cls "malformed define-record-type clause")))))) |
| 2077 |
(syntax-match x () |
| 2078 |
((_ namespec clause* ...) |
| 2079 |
(begin |
| 2080 |
(verify-clauses x clause*) |
| 2081 |
(do-define-record namespec clause*)))))) |
| 2082 |
|
| 2083 |
(define define-condition-type-macro |
| 2084 |
(lambda (x) |
| 2085 |
(define (mkname name suffix) |
| 2086 |
(datum->syntax name |
| 2087 |
(string->symbol |
| 2088 |
(string-append |
| 2089 |
(symbol->string (syntax->datum name)) |
| 2090 |
suffix)))) |
| 2091 |
(syntax-match x () |
| 2092 |
((ctxt name super constructor predicate (field* accessor*) ...) |
| 2093 |
(and (id? name) |
| 2094 |
(id? super) |
| 2095 |
(id? constructor) |
| 2096 |
(id? predicate) |
| 2097 |
(for-all id? field*) |
| 2098 |
(for-all id? accessor*)) |
| 2099 |
(let ((aux-accessor* (map (lambda (x) (gensym)) accessor*))) |
| 2100 |
(bless |
| 2101 |
`(begin |
| 2102 |
(define-record-type (,name ,constructor ,(gensym)) |
| 2103 |
(parent ,super) |
| 2104 |
(fields ,@(map (lambda (field aux) |
| 2105 |
`(immutable ,field ,aux)) |
| 2106 |
field* aux-accessor*)) |
| 2107 |
(nongenerative) |
| 2108 |
(sealed #f) (opaque #f)) |
| 2109 |
(define ,predicate (condition-predicate |
| 2110 |
(record-type-descriptor ,name))) |
| 2111 |
,@(map |
| 2112 |
(lambda (accessor aux) |
| 2113 |
`(define ,accessor |
| 2114 |
(condition-accessor |
| 2115 |
(record-type-descriptor ,name) ,aux))) |
| 2116 |
accessor* aux-accessor*)))))))) |
| 2117 |
|
| 2118 |
(define incorrect-usage-macro |
| 2119 |
(lambda (e) (stx-error e "incorrect usage of auxiliary keyword"))) |
| 2120 |
|
| 2121 |
(define parameterize-macro |
| 2122 |
(lambda (e) |
| 2123 |
(syntax-match e () |
| 2124 |
((_ () b b* ...) |
| 2125 |
(bless `(let () ,b . ,b*))) |
| 2126 |
((_ ((olhs* orhs*) ...) b b* ...) |
| 2127 |
(let ((lhs* (generate-temporaries olhs*)) |
| 2128 |
(rhs* (generate-temporaries orhs*))) |
| 2129 |
(bless |
| 2130 |
`((lambda ,(append lhs* rhs*) |
| 2131 |
(let ((swap (lambda () |
| 2132 |
,@(map (lambda (lhs rhs) |
| 2133 |
`(let ((t (,lhs))) |
| 2134 |
(,lhs ,rhs) |
| 2135 |
(set! ,rhs t))) |
| 2136 |
lhs* rhs*)))) |
| 2137 |
(dynamic-wind |
| 2138 |
swap |
| 2139 |
(lambda () ,b . ,b*) |
| 2140 |
swap))) |
| 2141 |
,@(append olhs* orhs*)))))))) |
| 2142 |
|
| 2143 |
|
| 2144 |
(define foreign-call-transformer |
| 2145 |
(lambda (e r mr) |
| 2146 |
(syntax-match e () |
| 2147 |
((_ name arg* ...) |
| 2148 |
(build-foreign-call no-source |
| 2149 |
(chi-expr name r mr) |
| 2150 |
(chi-expr* arg* r mr)))))) |
| 2151 |
|
| 2152 |
;; p in pattern: matches: |
| 2153 |
;; () empty list |
| 2154 |
;; _ anything (no binding created) |
| 2155 |
;; any anything |
| 2156 |
;; (p1 . p2) pair |
| 2157 |
;; #(free-id <key>) <key> with free-identifier=? |
| 2158 |
;; each-any any proper list |
| 2159 |
;; #(each p) (p*) |
| 2160 |
;; #(each+ p1 (p2_1 ... p2_n) p3) (p1* (p2_n ... p2_1) . p3) |
| 2161 |
;; #(vector p) #(x ...) if p matches (x ...) |
| 2162 |
;; #(atom <object>) <object> with "equal?" |
| 2163 |
(define convert-pattern |
| 2164 |
; returns syntax-dispatch pattern & ids |
| 2165 |
(lambda (pattern keys) |
| 2166 |
(define cvt* |
| 2167 |
(lambda (p* n ids) |
| 2168 |
(if (null? p*) |
| 2169 |
(values '() ids) |
| 2170 |
(let-values (((y ids) (cvt* (cdr p*) n ids))) |
| 2171 |
(let-values (((x ids) (cvt (car p*) n ids))) |
| 2172 |
(values (cons x y) ids)))))) |
| 2173 |
(define cvt |
| 2174 |
(lambda (p n ids) |
| 2175 |
(syntax-match p () |
| 2176 |
(id (id? id) |
| 2177 |
(cond |
| 2178 |
((bound-id-member? p keys) |
| 2179 |
(values `#(free-id ,p) ids)) |
| 2180 |
((free-id=? p (scheme-stx '_)) |
| 2181 |
(values '_ ids)) |
| 2182 |
(else (values 'any (cons (cons p n) ids))))) |
| 2183 |
((p dots) (ellipsis? dots) |
| 2184 |
(let-values (((p ids) (cvt p (+ n 1) ids))) |
| 2185 |
(values |
| 2186 |
(if (eq? p 'any) 'each-any `#(each ,p)) |
| 2187 |
ids))) |
| 2188 |
((x dots ys ... . z) (ellipsis? dots) |
| 2189 |
(let-values (((z ids) (cvt z n ids))) |
| 2190 |
(let-values (((ys ids) (cvt* ys n ids))) |
| 2191 |
(let-values (((x ids) (cvt x (+ n 1) ids))) |
| 2192 |
(values `#(each+ ,x ,(reverse ys) ,z) ids))))) |
| 2193 |
((x . y) |
| 2194 |
(let-values (((y ids) (cvt y n ids))) |
| 2195 |
(let-values (((x ids) (cvt x n ids))) |
| 2196 |
(values (cons x y) ids)))) |
| 2197 |
(() (values '() ids)) |
| 2198 |
(#(p ...) (not (stx? p)) |
| 2199 |
(let-values (((p ids) (cvt p n ids))) |
| 2200 |
(values `#(vector ,p) ids))) |
| 2201 |
(datum |
| 2202 |
(values `#(atom ,(stx->datum datum)) ids))))) |
| 2203 |
(cvt pattern 0 '()))) |
| 2204 |
|
| 2205 |
(define syntax-dispatch |
| 2206 |
(lambda (e p) |
| 2207 |
(define stx^ |
| 2208 |
(lambda (e m* s* ae*) |
| 2209 |
(if (and (null? m*) (null? s*) (null? ae*)) |
| 2210 |
e |
| 2211 |
(mkstx e m* s* ae*)))) |
| 2212 |
(define match-each |
| 2213 |
(lambda (e p m* s* ae*) |
| 2214 |
(cond |
| 2215 |
((pair? e) |
| 2216 |
(let ((first (match (car e) p m* s* ae* '()))) |
| 2217 |
(and first |
| 2218 |
(let ((rest (match-each (cdr e) p m* s* ae*))) |
| 2219 |
(and rest (cons first rest)))))) |
| 2220 |
((null? e) '()) |
| 2221 |
((stx? e) |
| 2222 |
(and (not (top-marked? m*)) |
| 2223 |
(let-values (((m* s* ae*) (join-wraps m* s* ae* e))) |
| 2224 |
(match-each (stx-expr e) p m* s* ae*)))) |
| 2225 |
((annotation? e) |
| 2226 |
(match-each (annotation-expression e) p m* s* ae*)) |
| 2227 |
(else #f)))) |
| 2228 |
(define match-each+ |
| 2229 |
(lambda (e x-pat y-pat z-pat m* s* ae* r) |
| 2230 |
(let f ((e e) (m* m*) (s* s*) (ae* ae*)) |
| 2231 |
(cond |
| 2232 |
((pair? e) |
| 2233 |
(let-values (((xr* y-pat r) (f (cdr e) m* s* ae*))) |
| 2234 |
(if r |
| 2235 |
(if (null? y-pat) |
| 2236 |
(let ((xr (match (car e) x-pat m* s* ae* '()))) |
| 2237 |
(if xr |
| 2238 |
(values (cons xr xr*) y-pat r) |
| 2239 |
(values #f #f #f))) |
| 2240 |
(values |
| 2241 |
'() |
| 2242 |
(cdr y-pat) |
| 2243 |
(match (car e) (car y-pat) m* s* ae* r))) |
| 2244 |
(values #f #f #f)))) |
| 2245 |
((stx? e) |
| 2246 |
(if (top-marked? m*) |
| 2247 |
(values '() y-pat (match e z-pat m* s* ae* r)) |
| 2248 |
(let-values (((m* s* ae*) (join-wraps m* s* ae* e))) |
| 2249 |
(f (stx-expr e) m* s* ae*)))) |
| 2250 |
((annotation? e) |
| 2251 |
(f (annotation-expression e) m* s* ae*)) |
| 2252 |
(else (values '() y-pat (match e z-pat m* s* ae* r))))))) |
| 2253 |
(define match-each-any |
| 2254 |
(lambda (e m* s* ae*) |
| 2255 |
(cond |
| 2256 |
((pair? e) |
| 2257 |
(let ((l (match-each-any (cdr e) m* s* ae*))) |
| 2258 |
(and l (cons (stx^ (car e) m* s* ae*) l)))) |
| 2259 |
((null? e) '()) |
| 2260 |
((stx? e) |
| 2261 |
(and (not (top-marked? m*)) |
| 2262 |
(let-values (((m* s* ae*) (join-wraps m* s* ae* e))) |
| 2263 |
(match-each-any (stx-expr e) m* s* ae*)))) |
| 2264 |
((annotation? e) |
| 2265 |
(match-each-any (annotation-expression e) m* s* ae*)) |
| 2266 |
(else #f)))) |
| 2267 |
(define match-empty |
| 2268 |
(lambda (p r) |
| 2269 |
(cond |
| 2270 |
((null? p) r) |
| 2271 |
((eq? p '_) r) |
| 2272 |
((eq? p 'any) (cons '() r)) |
| 2273 |
((pair? p) (match-empty (car p) (match-empty (cdr p) r))) |
| 2274 |
((eq? p 'each-any) (cons '() r)) |
| 2275 |
(else |
| 2276 |
(case (vector-ref p 0) |
| 2277 |
((each) (match-empty (vector-ref p 1) r)) |
| 2278 |
((each+) |
| 2279 |
(match-empty |
| 2280 |
(vector-ref p 1) |
| 2281 |
(match-empty |
| 2282 |
(reverse (vector-ref p 2)) |
| 2283 |
(match-empty (vector-ref p 3) r)))) |
| 2284 |
((free-id atom) r) |
| 2285 |
((scheme-id atom) r) |
| 2286 |
((vector) (match-empty (vector-ref p 1) r)) |
| 2287 |
(else (assertion-violation 'syntax-dispatch "invalid pattern" p))))))) |
| 2288 |
(define combine |
| 2289 |
(lambda (r* r) |
| 2290 |
(if (null? (car r*)) |
| 2291 |
r |
| 2292 |
(cons (map car r*) (combine (map cdr r*) r))))) |
| 2293 |
(define match* |
| 2294 |
(lambda (e p m* s* ae* r) |
| 2295 |
(cond |
| 2296 |
((null? p) (and (null? e) r)) |
| 2297 |
((pair? p) |
| 2298 |
(and (pair? e) |
| 2299 |
(match (car e) (car p) m* s* ae* |
| 2300 |
(match (cdr e) (cdr p) m* s* ae* r)))) |
| 2301 |
((eq? p 'each-any) |
| 2302 |
(let ((l (match-each-any e m* s* ae*))) (and l (cons l r)))) |
| 2303 |
(else |
| 2304 |
(case (vector-ref p 0) |
| 2305 |
((each) |
| 2306 |
(if (null? e) |
| 2307 |
(match-empty (vector-ref p 1) r) |
| 2308 |
(let ((r* (match-each e (vector-ref p 1) m* s* ae*))) |
| 2309 |
(and r* (combine r* r))))) |
| 2310 |
((free-id) |
| 2311 |
(and (symbol? e) |
| 2312 |
(top-marked? m*) |
| 2313 |
(free-id=? (stx^ e m* s* ae*) (vector-ref p 1)) |
| 2314 |
r)) |
| 2315 |
((scheme-id) |
| 2316 |
(and (symbol? e) |
| 2317 |
(top-marked? m*) |
| 2318 |
(free-id=? (stx^ e m* s* ae*) |
| 2319 |
(scheme-stx (vector-ref p 1))) |
| 2320 |
r)) |
| 2321 |
((each+) |
| 2322 |
(let-values (((xr* y-pat r) |
| 2323 |
(match-each+ e (vector-ref p 1) |
| 2324 |
(vector-ref p 2) (vector-ref p 3) m* s* ae* r))) |
| 2325 |
(and r |
| 2326 |
(null? y-pat) |
| 2327 |
(if (null? xr*) |
| 2328 |
(match-empty (vector-ref p 1) r) |
| 2329 |
(combine xr* r))))) |
| 2330 |
((atom) (and (equal? (vector-ref p 1) (strip e m*)) r)) |
| 2331 |
((vector) |
| 2332 |
(and (vector? e) |
| 2333 |
(match (vector->list e) (vector-ref p 1) m* s* ae* r))) |
| 2334 |
(else (assertion-violation 'syntax-dispatch "invalid pattern" p))))))) |
| 2335 |
(define match |
| 2336 |
(lambda (e p m* s* ae* r) |
| 2337 |
(cond |
| 2338 |
((not r) #f) |
| 2339 |
((eq? p '_) r) |
| 2340 |
((eq? p 'any) (cons (stx^ e m* s* ae*) r)) |
| 2341 |
((stx? e) |
| 2342 |
(and (not (top-marked? m*)) |
| 2343 |
(let-values (((m* s* ae*) (join-wraps m* s* ae* e))) |
| 2344 |
(match (stx-expr e) p m* s* ae* r)))) |
| 2345 |
((annotation? e) |
| 2346 |
(match (annotation-expression e) p m* s* ae* r)) |
| 2347 |
(else (match* e p m* s* ae* r))))) |
| 2348 |
(match e p '() '() '() '()))) |
| 2349 |
|
| 2350 |
(define ellipsis? |
| 2351 |
(lambda (x) |
| 2352 |
(and (id? x) (free-id=? x (scheme-stx '...))))) |
| 2353 |
|
| 2354 |
(define underscore? |
| 2355 |
(lambda (x) |
| 2356 |
(and (id? x) (free-id=? x (scheme-stx '_))))) |
| 2357 |
|
| 2358 |
(define (verify-literals lits expr) |
| 2359 |
(for-each |
| 2360 |
(lambda (x) |
| 2361 |
(when (or (not (id? x)) (ellipsis? x) (underscore? x)) |
| 2362 |
(syntax-violation #f "invalid literal" expr x))) |
| 2363 |
lits)) |
| 2364 |
|
| 2365 |
(define syntax-case-transformer |
| 2366 |
(let () |
| 2367 |
(define build-dispatch-call |
| 2368 |
(lambda (pvars expr y r mr) |
| 2369 |
(let ((ids (map car pvars)) |
| 2370 |
(levels (map cdr pvars))) |
| 2371 |
(let ((labels (map gen-label ids)) |
| 2372 |
(new-vars (map gen-lexical ids))) |
| 2373 |
(let ((body |
| 2374 |
(chi-expr |
| 2375 |
(add-subst (make-full-rib ids labels) expr) |
| 2376 |
(append |
| 2377 |
(map (lambda (label var level) |
| 2378 |
(cons label (make-binding 'syntax (cons var level)))) |
| 2379 |
labels new-vars (map cdr pvars)) |
| 2380 |
r) |
| 2381 |
mr))) |
| 2382 |
(build-application no-source |
| 2383 |
(build-primref no-source 'apply) |
| 2384 |
(list (build-lambda no-source new-vars body) y))))))) |
| 2385 |
(define invalid-ids-error |
| 2386 |
(lambda (id* e class) |
| 2387 |
(let find ((id* id*) (ok* '())) |
| 2388 |
(if (null? id*) |
| 2389 |
(stx-error e) ; shouldn't happen |
| 2390 |
(if (id? (car id*)) |
| 2391 |
(if (bound-id-member? (car id*) ok*) |
| 2392 |
(syntax-error (car id*) "duplicate " class) |
| 2393 |
(find (cdr id*) (cons (car id*) ok*))) |
| 2394 |
(syntax-error (car id*) "invalid " class)))))) |
| 2395 |
(define gen-clause |
| 2396 |
(lambda (x keys clauses r mr pat fender expr) |
| 2397 |
(let-values (((p pvars) (convert-pattern pat keys))) |
| 2398 |
(cond |
| 2399 |
((not (distinct-bound-ids? (map car pvars))) |
| 2400 |
(invalid-ids-error (map car pvars) pat "pattern variable")) |
| 2401 |
((not (for-all (lambda (x) (not (ellipsis? (car x)))) pvars)) |
| 2402 |
(stx-error pat "misplaced ellipsis in syntax-case pattern")) |
| 2403 |
(else |
| 2404 |
(let ((y (gen-lexical 'tmp))) |
| 2405 |
(let ((test |
| 2406 |
(cond |
| 2407 |
((eq? fender #t) y) |
| 2408 |
(else |
| 2409 |
(let ((call |
| 2410 |
(build-dispatch-call |
| 2411 |
pvars fender y r mr))) |
| 2412 |
(build-conditional no-source |
| 2413 |
(build-lexical-reference no-source y) |
| 2414 |
call |
| 2415 |
(build-data no-source #f))))))) |
| 2416 |
(let ((conseq |
| 2417 |
(build-dispatch-call pvars expr |
| 2418 |
(build-lexical-reference no-source y) |
| 2419 |
r mr))) |
| 2420 |
(let ((altern |
| 2421 |
(gen-syntax-case x keys clauses r mr))) |
| 2422 |
(build-application no-source |
| 2423 |
(build-lambda no-source (list y) |
| 2424 |
(build-conditional no-source test conseq altern)) |
| 2425 |
(list |
| 2426 |
(build-application no-source |
| 2427 |
(build-primref no-source 'syntax-dispatch) |
| 2428 |
(list |
| 2429 |
(build-lexical-reference no-source x) |
| 2430 |
(build-data no-source p)))))))))))))) |
| 2431 |
(define gen-syntax-case |
| 2432 |
(lambda (x keys clauses r mr) |
| 2433 |
(if (null? clauses) |
| 2434 |
(build-application no-source |
| 2435 |
(build-primref no-source 'syntax-error) |
| 2436 |
(list (build-lexical-reference no-source x))) |
| 2437 |
(syntax-match (car clauses) () |
| 2438 |
((pat expr) |
| 2439 |
(if (and (id? pat) |
| 2440 |
(not (bound-id-member? pat keys)) |
| 2441 |
(not (ellipsis? pat))) |
| 2442 |
(if (free-id=? pat (scheme-stx '_)) |
| 2443 |
(chi-expr expr r mr) |
| 2444 |
(let ((lab (gen-label pat)) |
| 2445 |
(lex (gen-lexical pat))) |
| 2446 |
(let ((body |
| 2447 |
(chi-expr |
| 2448 |
(add-subst (make-full-rib (list pat) (list lab)) expr) |
| 2449 |
(cons (cons lab (make-binding 'syntax (cons lex 0))) r) |
| 2450 |
mr))) |
| 2451 |
(build-application no-source |
| 2452 |
(build-lambda no-source (list lex) body) |
| 2453 |
(list (build-lexical-reference no-source x)))))) |
| 2454 |
(gen-clause x keys (cdr clauses) r mr pat #t expr))) |
| 2455 |
((pat fender expr) |
| 2456 |
(gen-clause x keys (cdr clauses) r mr pat fender expr)))))) |
| 2457 |
(lambda (e r mr) |
| 2458 |
(syntax-match e () |
| 2459 |
((_ expr (keys ...) clauses ...) |
| 2460 |
(begin |
| 2461 |
(verify-literals keys e) |
| 2462 |
(let ((x (gen-lexical 'tmp))) |
| 2463 |
(let ((body (gen-syntax-case x keys clauses r mr))) |
| 2464 |
(build-application no-source |
| 2465 |
(build-lambda no-source (list x) body) |
| 2466 |
(list (chi-expr expr r mr))))))))))) |
| 2467 |
|
| 2468 |
(define (ellipsis-map proc ls . ls*) |
| 2469 |
(define who '...) |
| 2470 |
(unless (list? ls) |
| 2471 |
(assertion-violation who "not a list" ls)) |
| 2472 |
(unless (null? ls*) |
| 2473 |
(let ((n (length ls))) |
| 2474 |
(for-each |
| 2475 |
(lambda (x) |
| 2476 |
(unless (list? x) |
| 2477 |
(assertion-violation who "not a list" x)) |
| 2478 |
(unless (= (length x) n) |
| 2479 |
(assertion-violation who "length mismatch" ls x))) |
| 2480 |
ls*))) |
| 2481 |
(apply map proc ls ls*)) |
| 2482 |
|
| 2483 |
(define syntax-transformer |
| 2484 |
(let () |
| 2485 |
(define gen-syntax |
| 2486 |
(lambda (src e r maps ellipsis? vec?) |
| 2487 |
(syntax-match e () |
| 2488 |
(dots (ellipsis? dots) |
| 2489 |
(stx-error src "misplaced ellipsis in syntax form")) |
| 2490 |
(id (id? id) |
| 2491 |
(let* ((label (id->label e)) |
| 2492 |
(b (label->binding label r))) |
| 2493 |
(if (eq? (binding-type b) 'syntax) |
| 2494 |
(let-values (((var maps) |
| 2495 |
(let ((var.lev (binding-value b))) |
| 2496 |
(gen-ref src (car var.lev) (cdr var.lev) maps)))) |
| 2497 |
(values (list 'ref var) maps)) |
| 2498 |
(values (list 'quote e) maps)))) |
| 2499 |
((dots e) (ellipsis? dots) |
| 2500 |
(if vec? |
| 2501 |
(stx-error src "misplaced ellipsis in syntax form") |
| 2502 |
(gen-syntax src e r maps (lambda (x) #f) #f))) |
| 2503 |
((x dots . y) (ellipsis? dots) |
| 2504 |
(let f ((y y) |
| 2505 |
(k (lambda (maps) |
| 2506 |
(let-values (((x maps) |
| 2507 |
(gen-syntax src x r |
| 2508 |
(cons '() maps) ellipsis? #f))) |
| 2509 |
(if (null? (car maps)) |
| 2510 |
(stx-error src |
| 2511 |
"extra ellipsis in syntax form") |
| 2512 |
(values (gen-map x (car maps)) (cdr maps))))))) |
| 2513 |
(syntax-match y () |
| 2514 |
(() (k maps)) |
| 2515 |
((dots . y) (ellipsis? dots) |
| 2516 |
(f y |
| 2517 |
(lambda (maps) |
| 2518 |
(let-values (((x maps) (k (cons '() maps)))) |
| 2519 |
(if (null? (car maps)) |
| 2520 |
(stx-error src "extra ellipsis in syntax form") |
| 2521 |
(values (gen-mappend x (car maps)) (cdr maps))))))) |
| 2522 |
(_ |
| 2523 |
(let-values (((y maps) |
| 2524 |
(gen-syntax src y r maps ellipsis? vec?))) |
| 2525 |
(let-values (((x maps) (k maps))) |
| 2526 |
(values (gen-append x y) maps))))))) |
| 2527 |
((x . y) |
| 2528 |
(let-values (((xnew maps) |
| 2529 |
(gen-syntax src x r maps ellipsis? #f))) |
| 2530 |
(let-values (((ynew maps) |
| 2531 |
(gen-syntax src y r maps ellipsis? vec?))) |
| 2532 |
(values (gen-cons e x y xnew ynew) maps)))) |
| 2533 |
(#(ls ...) |
| 2534 |
(let-values (((lsnew maps) |
| 2535 |
(gen-syntax src ls r maps ellipsis? #t))) |
| 2536 |
(values (gen-vector e ls lsnew) maps))) |
| 2537 |
(_ (values `(quote ,e) maps))))) |
| 2538 |
(define gen-ref |
| 2539 |
(lambda (src var level maps) |
| 2540 |
(if (= level 0) |
| 2541 |
(values var maps) |
| 2542 |
(if (null? maps) |
| 2543 |
(stx-error src "missing ellipsis in syntax form") |
| 2544 |
(let-values (((outer-var outer-maps) |
| 2545 |
(gen-ref src var (- level 1) (cdr maps)))) |
| 2546 |
(cond |
| 2547 |
((assq outer-var (car maps)) => |
| 2548 |
(lambda (b) (values (cdr b) maps))) |
| 2549 |
(else |
| 2550 |
(let ((inner-var (gen-lexical 'tmp))) |
| 2551 |
(values |
| 2552 |
inner-var |
| 2553 |
(cons |
| 2554 |
(cons (cons outer-var inner-var) (car maps)) |
| 2555 |
outer-maps)))))))))) |
| 2556 |
(define gen-append |
| 2557 |
(lambda (x y) |
| 2558 |
(if (equal? y '(quote ())) x `(append ,x ,y)))) |
| 2559 |
(define gen-mappend |
| 2560 |
(lambda (e map-env) |
| 2561 |
`(apply (primitive append) ,(gen-map e map-env)))) |
| 2562 |
(define gen-map |
| 2563 |
(lambda (e map-env) |
| 2564 |
(let ((formals (map cdr map-env)) |
| 2565 |
(actuals (map (lambda (x) `(ref ,(car x))) map-env))) |
| 2566 |
(cond |
| 2567 |
; identity map equivalence: |
| 2568 |
; (map (lambda (x) x) y) == y |
| 2569 |
((eq? (car e) 'ref) |
| 2570 |
(car actuals)) |
| 2571 |
; eta map equivalence: |
| 2572 |
; (map (lambda (x ...) (f x ...)) y ...) == (map f y ...) |
| 2573 |
((for-all |
| 2574 |
(lambda (x) (and (eq? (car x) 'ref) (memq (cadr x) formals))) |
| 2575 |
(cdr e)) |
| 2576 |
(let ((args (map (let ((r (map cons formals actuals))) |
| 2577 |
(lambda (x) (cdr (assq (cadr x) r)))) |
| 2578 |
(cdr e)))) |
| 2579 |
`(map (primitive ,(car e)) . ,args))) |
| 2580 |
(else (cons* 'map (list 'lambda formals e) actuals)))))) |
| 2581 |
(define gen-cons |
| 2582 |
(lambda (e x y xnew ynew) |
| 2583 |
(case (car ynew) |
| 2584 |
((quote) |
| 2585 |
(if (eq? (car xnew) 'quote) |
| 2586 |
(let ((xnew (cadr xnew)) (ynew (cadr ynew))) |
| 2587 |
(if (and (eq? xnew x) (eq? ynew y)) |
| 2588 |
`(quote ,e) |
| 2589 |
`(quote ,(cons xnew ynew)))) |
| 2590 |
(if (null? (cadr ynew)) |
| 2591 |
`(list ,xnew) |
| 2592 |
`(cons ,xnew ,ynew)))) |
| 2593 |
((list) `(list ,xnew . ,(cdr ynew))) |
| 2594 |
(else `(cons ,xnew ,ynew))))) |
| 2595 |
(define gen-vector |
| 2596 |
(lambda (e ls lsnew) |
| 2597 |
(cond |
| 2598 |
((eq? (car lsnew) 'quote) |
| 2599 |
(if (eq? (cadr lsnew) ls) |
| 2600 |
`(quote ,e) |
| 2601 |
`(quote #(,@(cadr lsnew))))) |
| 2602 |
((eq? (car lsnew) 'list) |
| 2603 |
`(vector . ,(cdr lsnew))) |
| 2604 |
(else `(list->vector ,lsnew))))) |
| 2605 |
(define regen |
| 2606 |
(lambda (x) |
| 2607 |
(case (car x) |
| 2608 |
((ref) (build-lexical-reference no-source (cadr x))) |
| 2609 |
((primitive) (build-primref no-source (cadr x))) |
| 2610 |
((quote) (build-data no-source (cadr x))) |
| 2611 |
((lambda) (build-lambda no-source (cadr x) (regen (caddr x)))) |
| 2612 |
((map) |
| 2613 |
(let ((ls (map regen (cdr x)))) |
| 2614 |
(build-application no-source |
| 2615 |
(build-primref no-source 'ellipsis-map) |
| 2616 |
ls))) |
| 2617 |
(else |
| 2618 |
(build-application no-source |
| 2619 |
(build-primref no-source (car x)) |
| 2620 |
(map regen (cdr x))))))) |
| 2621 |
(lambda (e r mr) |
| 2622 |
(syntax-match e () |
| 2623 |
((_ x) |
| 2624 |
(let-values (((e maps) (gen-syntax e x r '() ellipsis? #f))) |
| 2625 |
(regen e))))))) |
| 2626 |
|
| 2627 |
(define core-macro-transformer |
| 2628 |
(lambda (name) |
| 2629 |
(case name |
| 2630 |
((quote) quote-transformer) |
| 2631 |
((lambda) lambda-transformer) |
| 2632 |
((case-lambda) case-lambda-transformer) |
| 2633 |
((letrec) letrec-transformer) |
| 2634 |
((letrec*) letrec*-transformer) |
| 2635 |
((if) if-transformer) |
| 2636 |
((foreign-call) foreign-call-transformer) |
| 2637 |
((syntax-case) syntax-case-transformer) |
| 2638 |
((syntax) syntax-transformer) |
| 2639 |
((type-descriptor) type-descriptor-transformer) |
| 2640 |
((record-type-descriptor) record-type-descriptor-transformer) |
| 2641 |
((record-constructor-descriptor) record-constructor-descriptor-transformer) |
| 2642 |
((fluid-let-syntax) fluid-let-syntax-transformer) |
| 2643 |
(else (assertion-violation |
| 2644 |
'macro-transformer |
| 2645 |
"BUG: cannot find transformer" |
| 2646 |
name))))) |
| 2647 |
|
| 2648 |
(define file-options-macro |
| 2649 |
(lambda (x) |
| 2650 |
(define (valid-option? x) |
| 2651 |
(and (id? x) (memq (id->sym x) '(no-fail no-create no-truncate)))) |
| 2652 |
(syntax-match x () |
| 2653 |
((_ opt* ...) |
| 2654 |
(for-all valid-option? opt*) |
| 2655 |
(bless `(make-file-options ',opt*)))))) |
| 2656 |
|
| 2657 |
(define symbol-macro |
| 2658 |
(lambda (x set) |
| 2659 |
(syntax-match x () |
| 2660 |
((_ name) |
| 2661 |
(and (id? name) (memq (id->sym name) set)) |
| 2662 |
(bless `(quote ,name)))))) |
| 2663 |
|
| 2664 |
(define macro-transformer |
| 2665 |
(lambda (x) |
| 2666 |
(cond |
| 2667 |
((procedure? x) x) |
| 2668 |
((symbol? x) |
| 2669 |
(case x |
| 2670 |
((define-record-type) define-record-type-macro) |
| 2671 |
((define-struct) define-struct-macro) |
| 2672 |
((cond) cond-macro) |
| 2673 |
((let) let-macro) |
| 2674 |
((do) do-macro) |
| 2675 |
((or) or-macro) |
| 2676 |
((and) and-macro) |
| 2677 |
((let*) let*-macro) |
| 2678 |
((let-values) let-values-macro) |
| 2679 |
((let*-values) let*-values-macro) |
| 2680 |
((syntax-rules) syntax-rules-macro) |
| 2681 |
((quasiquote) quasiquote-macro) |
| 2682 |
((quasisyntax) quasisyntax-macro) |
| 2683 |
((with-syntax) with-syntax-macro) |
| 2684 |
((when) when-macro) |
| 2685 |
((unless) unless-macro) |
| 2686 |
((case) case-macro) |
| 2687 |
((identifier-syntax) identifier-syntax-macro) |
| 2688 |
((time) time-macro) |
| 2689 |
((delay) delay-macro) |
| 2690 |
((assert) assert-macro) |
| 2691 |
((endianness) endianness-macro) |
| 2692 |
((guard) guard-macro) |
| 2693 |
((define-enumeration) define-enumeration-macro) |
| 2694 |
((trace-lambda) trace-lambda-macro) |
| 2695 |
((trace-define) trace-define-macro) |
| 2696 |
((trace-let) trace-let-macro) |
| 2697 |
((trace-define-syntax) trace-define-syntax-macro) |
| 2698 |
((trace-let-syntax) trace-let-syntax-macro) |
| 2699 |
((trace-letrec-syntax) trace-letrec-syntax-macro) |
| 2700 |
((define-condition-type) define-condition-type-macro) |
| 2701 |
((parameterize) parameterize-macro) |
| 2702 |
((eol-style) |
| 2703 |
(lambda (x) |
| 2704 |
(symbol-macro x '(none lf cr crlf nel crnel ls)))) |
| 2705 |
((error-handling-mode) |
| 2706 |
(lambda (x) |
| 2707 |
(symbol-macro x '(ignore raise replace)))) |
| 2708 |
((buffer-mode) |
| 2709 |
(lambda (x) |
| 2710 |
(symbol-macro x '(none line block)))) |
| 2711 |
((file-options) file-options-macro) |
| 2712 |
((... => _ else unquote unquote-splicing |
| 2713 |
unsyntax unsyntax-splicing |
| 2714 |
fields mutable immutable parent protocol |
| 2715 |
sealed opaque nongenerative parent-rtd) |
| 2716 |
incorrect-usage-macro) |
| 2717 |
(else |
| 2718 |
(error 'macro-transformer "BUG: invalid macro" x)))) |
| 2719 |
(else |
| 2720 |
(error 'core-macro-transformer "BUG: invalid macro" x))))) |
| 2721 |
|
| 2722 |
(define (local-macro-transformer x) |
| 2723 |
(car x)) |
| 2724 |
|
| 2725 |
(define (do-macro-call transformer expr r rib) |
| 2726 |
(define (return x) |
| 2727 |
(let f ((x x)) |
| 2728 |
;;; don't feed me cycles. |
| 2729 |
(unless (stx? x) |
| 2730 |
(cond |
| 2731 |
((pair? x) (f (car x)) (f (cdr x))) |
| 2732 |
((vector? x) (vector-for-each f x)) |
| 2733 |
((symbol? x) |
| 2734 |
(syntax-violation #f |
| 2735 |
"raw symbol encountered in output of macro" |
| 2736 |
expr x))))) |
| 2737 |
(add-mark (gen-mark) rib x expr)) |
| 2738 |
(let ((x (transformer (add-mark anti-mark #f expr #f)))) |
| 2739 |
(if (procedure? x) |
| 2740 |
(return |
| 2741 |
(x (lambda (id) |
| 2742 |
(unless (id? id) |
| 2743 |
(assertion-violation 'rho "not an identifier" id)) |
| 2744 |
(let ([label (id->label id)]) |
| 2745 |
(let ([binding (label->binding label r)]) |
| 2746 |
(case (car binding) |
| 2747 |
[(local-ctv) (cadr binding)] |
| 2748 |
[(global-ctv) |
| 2749 |
(let ([lib (cadr binding)] |
| 2750 |
[loc (cddr binding)]) |
| 2751 |
(unless (eq? lib '*interaction*) |
| 2752 |
(visit-library lib)) |
| 2753 |
(symbol-value loc))] |
| 2754 |
[else #f])))))) |
| 2755 |
(return x)))) |
| 2756 |
|
| 2757 |
;;; chi procedures |
| 2758 |
(define (chi-macro p e r rib) |
| 2759 |
(do-macro-call (macro-transformer p) e r rib)) |
| 2760 |
|
| 2761 |
(define (chi-local-macro p e r rib) |
| 2762 |
(do-macro-call (local-macro-transformer p) e r rib)) |
| 2763 |
|
| 2764 |
(define (chi-global-macro p e r rib) |
| 2765 |
(let ((lib (car p)) |
| 2766 |
(loc (cdr p))) |
| 2767 |
(unless (eq? lib '*interaction*) |
| 2768 |
(visit-library lib)) |
| 2769 |
(let ((x (symbol-value loc))) |
| 2770 |
(let ((transformer |
| 2771 |
(cond |
| 2772 |
((procedure? x) x) |
| 2773 |
((and (pair? x) |
| 2774 |
(eq? (car x) 'macro!) |
| 2775 |
(procedure? (cdr x))) |
| 2776 |
(cdr x)) |
| 2777 |
(else (assertion-violation 'chi-global-macro |
| 2778 |
"BUG: not a procedure" x))))) |
| 2779 |
(do-macro-call transformer e r rib))))) |
| 2780 |
|
| 2781 |
(define chi-expr* |
| 2782 |
(lambda (e* r mr) |
| 2783 |
;;; expand left to right |
| 2784 |
(cond |
| 2785 |
((null? e*) '()) |
| 2786 |
(else |
| 2787 |
(let ((e (chi-expr (car e*) r mr))) |
| 2788 |
(cons e (chi-expr* (cdr e*) r mr))))))) |
| 2789 |
|
| 2790 |
(define chi-application |
| 2791 |
(lambda (e r mr) |
| 2792 |
(syntax-match e () |
| 2793 |
((rator rands ...) |
| 2794 |
(let ((rator (chi-expr rator r mr))) |
| 2795 |
(build-application (syntax-annotation e) |
| 2796 |
rator |
| 2797 |
(chi-expr* rands r mr))))))) |
| 2798 |
|
| 2799 |
(define chi-expr |
| 2800 |
(lambda (e r mr) |
| 2801 |
(let-values (((type value kwd) (syntax-type e r))) |
| 2802 |
(case type |
| 2803 |
((core-macro) |
| 2804 |
(let ((transformer (core-macro-transformer value))) |
| 2805 |
(transformer e r mr))) |
| 2806 |
((global) |
| 2807 |
(let* ((lib (car value)) |
| 2808 |
(loc (cdr value))) |
| 2809 |
((inv-collector) lib) |
| 2810 |
(build-global-reference no-source loc))) |
| 2811 |
((core-prim) |
| 2812 |
(let ((name value)) |
| 2813 |
(build-primref no-source name))) |
| 2814 |
((call) (chi-application e r mr)) |
| 2815 |
((lexical) |
| 2816 |
(let ((lex (lexical-var value))) |
| 2817 |
(build-lexical-reference no-source lex))) |
| 2818 |
((global-macro global-macro!) |
| 2819 |
(chi-expr (chi-global-macro value e r #f) r mr)) |
| 2820 |
((local-macro local-macro!) |
| 2821 |
(chi-expr (chi-local-macro value e r #f) r mr)) |
| 2822 |
((macro macro!) |
| 2823 |
(chi-expr (chi-macro value e r #f) r mr)) |
| 2824 |
((constant) |
| 2825 |
(let ((datum value)) |
| 2826 |
(build-data no-source datum))) |
| 2827 |
((set!) (chi-set! e r mr)) |
| 2828 |
((begin) |
| 2829 |
(syntax-match e () |
| 2830 |
((_ x x* ...) |
| 2831 |
(build-sequence no-source |
| 2832 |
(chi-expr* (cons x x*) r mr))))) |
| 2833 |
((stale-when) |
| 2834 |
(syntax-match e () |
| 2835 |
((_ guard x x* ...) |
| 2836 |
(begin |
| 2837 |
(handle-stale-when guard mr) |
| 2838 |
(build-sequence no-source |
| 2839 |
(chi-expr* (cons x x*) r mr)))))) |
| 2840 |
((let-syntax letrec-syntax) |
| 2841 |
(syntax-match e () |
| 2842 |
((_ ((xlhs* xrhs*) ...) xbody xbody* ...) |
| 2843 |
(unless (valid-bound-ids? xlhs*) |
| 2844 |
(stx-error e "invalid identifiers")) |
| 2845 |
(let* ((xlab* (map gen-label xlhs*)) |
| 2846 |
(xrib (make-full-rib xlhs* xlab*)) |
| 2847 |
(xb* (map (lambda (x) |
| 2848 |
(make-eval-transformer |
| 2849 |
(expand-transformer |
| 2850 |
(if (eq? type 'let-syntax) |
| 2851 |
x |
| 2852 |
(add-subst xrib x)) |
| 2853 |
mr))) |
| 2854 |
xrhs*))) |
| 2855 |
(build-sequence no-source |
| 2856 |
(chi-expr* |
| 2857 |
(map (lambda (x) (add-subst xrib x)) (cons xbody xbody*)) |
| 2858 |
(append (map cons xlab* xb*) r) |
| 2859 |
(append (map cons xlab* xb*) mr))))))) |
| 2860 |
((displaced-lexical) |
| 2861 |
(stx-error e "identifier out of context")) |
| 2862 |
((syntax) (stx-error e "reference to pattern variable outside a syntax form")) |
| 2863 |
((define define-syntax define-fluid-syntax module import library) |
| 2864 |
(stx-error e |
| 2865 |
(string-append |
| 2866 |
(case type |
| 2867 |
((define) "a definition") |
| 2868 |
((define-syntax) "a define-syntax") |
| 2869 |
((define-fluid-syntax) "a define-fluid-syntax") |
| 2870 |
((module) "a module definition") |
| 2871 |
((library) "a library definition") |
| 2872 |
((import) "an import declaration") |
| 2873 |
((export) "an export declaration") |
| 2874 |
(else "a non-expression")) |
| 2875 |
" was found where an expression was expected"))) |
| 2876 |
((mutable) |
| 2877 |
(if (and (pair? value) (let ((lib (car value))) (eq? lib '*interaction*))) |
| 2878 |
(let ((loc (cdr value))) (build-global-reference no-source loc)) |
| 2879 |
(stx-error e "attempt to reference an unexportable variable"))) |
| 2880 |
(else |
| 2881 |
;(assertion-violation 'chi-expr "invalid type " type (strip e '())) |
| 2882 |
(stx-error e "invalid expression")))))) |
| 2883 |
|
| 2884 |
(define chi-set! |
| 2885 |
(lambda (e r mr) |
| 2886 |
(syntax-match e () |
| 2887 |
((_ x v) (id? x) |
| 2888 |
(let-values (((type value kwd) (syntax-type x r))) |
| 2889 |
(case type |
| 2890 |
((lexical) |
| 2891 |
(set-lexical-mutable! value #t) |
| 2892 |
(build-lexical-assignment no-source |
| 2893 |
(lexical-var value) |
| 2894 |
(chi-expr v r mr))) |
| 2895 |
((core-prim) |
| 2896 |
(stx-error e "cannot modify imported core primitive")) |
| 2897 |
((global) |
| 2898 |
(stx-error e "attempt to modify an immutable binding")) |
| 2899 |
((global-macro!) |
| 2900 |
(chi-expr (chi-global-macro value e r #f) r mr)) |
| 2901 |
((local-macro!) |
| 2902 |
(chi-expr (chi-local-macro value e r #f) r mr)) |
| 2903 |
((mutable) |
| 2904 |
(if (and (pair? value) (let ((lib (car value))) (eq? lib '*interaction*))) |
| 2905 |
(let ([loc (cdr value)]) |
| 2906 |
(build-global-assignment no-source loc |
| 2907 |
(chi-expr v r mr))) |
| 2908 |
(stx-error e "attempt to modify an unexportable variable"))) |
| 2909 |
(else (stx-error e)))))))) |
| 2910 |
|
| 2911 |
(define (verify-formals fmls stx) |
| 2912 |
(syntax-match fmls () |
| 2913 |
((x* ...) |
| 2914 |
(unless (valid-bound-ids? x*) |
| 2915 |
(invalid-fmls-error stx fmls))) |
| 2916 |
((x* ... . x) |
| 2917 |
(unless (valid-bound-ids? (cons x x*)) |
| 2918 |
(invalid-fmls-error stx fmls))) |
| 2919 |
(_ (stx-error stx "invalid syntax")))) |
| 2920 |
|
| 2921 |
(define chi-lambda-clause |
| 2922 |
(lambda (stx fmls body* r mr) |
| 2923 |
(syntax-match fmls () |
| 2924 |
((x* ...) |
| 2925 |
(begin |
| 2926 |
(verify-formals fmls stx) |
| 2927 |
(let ((lex* (map gen-lexical x*)) |
| 2928 |
(lab* (map gen-label x*))) |
| 2929 |
(values |
| 2930 |
lex* |
| 2931 |
(chi-internal |
| 2932 |
(add-subst (make-full-rib x* lab*) body*) |
| 2933 |
(add-lexicals lab* lex* r) |
| 2934 |
mr))))) |
| 2935 |
((x* ... . x) |
| 2936 |
(begin |
| 2937 |
(verify-formals fmls stx) |
| 2938 |
(let ((lex* (map gen-lexical x*)) (lab* (map gen-label x*)) |
| 2939 |
(lex (gen-lexical x)) (lab (gen-label x))) |
| 2940 |
(values |
| 2941 |
(append lex* lex) |
| 2942 |
(chi-internal |
| 2943 |
(add-subst |
| 2944 |
(make-full-rib (cons x x*) (cons lab lab*)) |
| 2945 |
body*) |
| 2946 |
(add-lexicals (cons lab lab*) (cons lex lex*) r) |
| 2947 |
mr))))) |
| 2948 |
(_ (stx-error fmls "invalid syntax"))))) |
| 2949 |
|
| 2950 |
(define chi-lambda-clause* |
| 2951 |
(lambda (stx fmls* body** r mr) |
| 2952 |
(cond |
| 2953 |
((null? fmls*) (values '() '())) |
| 2954 |
(else |
| 2955 |
(let-values (((a b) |
| 2956 |
(chi-lambda-clause stx (car fmls*) (car body**) r mr))) |
| 2957 |
(let-values (((a* b*) |
| 2958 |
(chi-lambda-clause* stx (cdr fmls*) (cdr body**) r mr))) |
| 2959 |
(values (cons a a*) (cons b b*)))))))) |
| 2960 |
|
| 2961 |
(define (chi-defun x r mr) |
| 2962 |
(syntax-match x () |
| 2963 |
[(_ (ctxt . fmls) . body*) |
| 2964 |
(let-values (((fmls body) |
| 2965 |
(chi-lambda-clause fmls fmls body* r mr))) |
| 2966 |
(build-lambda (syntax-annotation ctxt) fmls body))])) |
| 2967 |
|
| 2968 |
(define chi-rhs |
| 2969 |
(lambda (rhs r mr) |
| 2970 |
(case (car rhs) |
| 2971 |
((defun) (chi-defun (cdr rhs) r mr)) |
| 2972 |
((expr) |
| 2973 |
(let ((expr (cdr rhs))) |
| 2974 |
(chi-expr expr r mr))) |
| 2975 |
((top-expr) |
| 2976 |
(let ((expr (cdr rhs))) |
| 2977 |
(build-sequence no-source |
| 2978 |
(list (chi-expr expr r mr) |
| 2979 |
(build-void))))) |
| 2980 |
(else (assertion-violation 'chi-rhs "BUG: invalid rhs" rhs))))) |
| 2981 |
|
| 2982 |
(define (expand-interaction-rhs*/init* lhs* rhs* init* r mr) |
| 2983 |
(let f ((lhs* lhs*) (rhs* rhs*)) |
| 2984 |
(cond |
| 2985 |
((null? lhs*) |
| 2986 |
(map (lambda (x) (chi-expr x r mr)) init*)) |
| 2987 |
(else |
| 2988 |
(let ((lhs (car lhs*)) (rhs (car rhs*))) |
| 2989 |
(case (car rhs) |
| 2990 |
((defun) |
| 2991 |
(let ((rhs (chi-defun (cdr rhs) r mr))) |
| 2992 |
(cons |
| 2993 |
(build-global-assignment no-source lhs rhs) |
| 2994 |
(f (cdr lhs*) (cdr rhs*))))) |
| 2995 |
((expr) |
| 2996 |
(let ((rhs (chi-expr (cdr rhs) r mr))) |
| 2997 |
(cons |
| 2998 |
(build-global-assignment no-source lhs rhs) |
| 2999 |
(f (cdr lhs*) (cdr rhs*))))) |
| 3000 |
((top-expr) |
| 3001 |
(let ((e (chi-expr (cdr rhs) r mr))) |
| 3002 |
(cons e (f (cdr lhs*) (cdr rhs*))))) |
| 3003 |
(else (error 'expand-interaction "invallid" rhs)))))))) |
| 3004 |
|
| 3005 |
(define chi-rhs* |
| 3006 |
(lambda (rhs* r mr) |
| 3007 |
(let f ((ls rhs*)) |
| 3008 |
(cond ;;; chi-rhs in order |
| 3009 |
((null? ls) '()) |
| 3010 |
(else |
| 3011 |
(let ((a (chi-rhs (car ls) r mr))) |
| 3012 |
(cons a (f (cdr ls))))))))) |
| 3013 |
|
| 3014 |
(define find-bound=? |
| 3015 |
(lambda (x lhs* rhs*) |
| 3016 |
(cond |
| 3017 |
((null? lhs*) #f) |
| 3018 |
((bound-id=? x (car lhs*)) (car rhs*)) |
| 3019 |
(else (find-bound=? x (cdr lhs*) (cdr rhs*)))))) |
| 3020 |
|
| 3021 |
(define (find-dups ls) |
| 3022 |
(let f ((ls ls) (dups '())) |
| 3023 |
(cond |
| 3024 |
((null? ls) dups) |
| 3025 |
((find-bound=? (car ls) (cdr ls) (cdr ls)) => |
| 3026 |
(lambda (x) (f (cdr ls) (cons (list (car ls) x) dups)))) |
| 3027 |
(else (f (cdr ls) dups))))) |
| 3028 |
|
| 3029 |
(define chi-internal |
| 3030 |
(lambda (e* r mr) |
| 3031 |
(let ((rib (make-empty-rib))) |
| 3032 |
(let-values (((e* r mr lex* rhs* mod** kwd* _exp*) |
| 3033 |
(chi-body* |
| 3034 |
(map (lambda (x) (add-subst rib x)) (syntax->list e*)) |
| 3035 |
r mr '() '() '() '() '() rib #f #t))) |
| 3036 |
(when (null? e*) |
| 3037 |
(stx-error e* "no expression in body")) |
| 3038 |
(let* ((init* |
| 3039 |
(chi-expr* (append (apply append (reverse mod**)) e*) r mr)) |
| 3040 |
(rhs* (chi-rhs* rhs* r mr))) |
| 3041 |
(build-letrec* no-source |
| 3042 |
(reverse lex*) (reverse rhs*) |
| 3043 |
(build-sequence no-source init*))))))) |
| 3044 |
|
| 3045 |
(define parse-module |
| 3046 |
(lambda (e) |
| 3047 |
(syntax-match e () |
| 3048 |
((_ (export* ...) b* ...) |
| 3049 |
(begin |
| 3050 |
(unless (for-all id? export*) |
| 3051 |
(stx-error e "module exports must be identifiers")) |
| 3052 |
(values #f (list->vector export*) b*))) |
| 3053 |
((_ name (export* ...) b* ...) |
| 3054 |
(begin |
| 3055 |
(unless (id? name) |
| 3056 |
(stx-error e "module name must be an identifier")) |
| 3057 |
(unless (for-all id? export*) |
| 3058 |
(stx-error e "module exports must be identifiers")) |
| 3059 |
(values name (list->vector export*) b*)))))) |
| 3060 |
|
| 3061 |
(define-record module-interface (first-mark exp-id-vec exp-lab-vec)) |
| 3062 |
|
| 3063 |
(define (module-interface-exp-id* iface id) |
| 3064 |
(define (diff-marks ls x) |
| 3065 |
(when (null? ls) (error 'diff-marks "BUG: should not happen")) |
| 3066 |
(let ((a (car ls))) |
| 3067 |
(if (eq? a x) |
| 3068 |
'() |
| 3069 |
(cons a (diff-marks (cdr ls) x))))) |
| 3070 |
(let ((diff |
| 3071 |
(diff-marks (stx-mark* id) (module-interface-first-mark iface))) |
| 3072 |
(id-vec (module-interface-exp-id-vec iface))) |
| 3073 |
(if (null? diff) |
| 3074 |
id-vec |
| 3075 |
(vector-map |
| 3076 |
(lambda (x) |
| 3077 |
(make-stx (stx-expr x) (append diff (stx-mark* x)) '() '())) |
| 3078 |
id-vec)))) |
| 3079 |
|
| 3080 |
(define (syntax-transpose object base-id new-id) |
| 3081 |
(define who 'syntax-transpose) |
| 3082 |
(define (err msg . args) (apply assertion-violation who msg args)) |
| 3083 |
(define (split s*) |
| 3084 |
(cond |
| 3085 |
[(eq? (car s*) 'shift) |
| 3086 |
(values (list 'shift) (cdr s*))] |
| 3087 |
[else |
| 3088 |
(let-values ([(s1* s2*) (split (cdr s*))]) |
| 3089 |
(values (cons (car s*) s1*) s2*))])) |
| 3090 |
(define (final s*) |
| 3091 |
(cond |
| 3092 |
[(or (null? s*) (eq? (car s*) 'shift)) '()] |
| 3093 |
[else (cons (car s*) (final (cdr s*)))])) |
| 3094 |
(define (diff m m* s* ae*) |
| 3095 |
(if (null? m*) |
| 3096 |
(err "unmatched identifiers" base-id new-id) |
| 3097 |
(let ([m1 (car m*)]) |
| 3098 |
(if (eq? m m1) |
| 3099 |
(values '() (final s*) '()) |
| 3100 |
(let-values ([(s1* s2*) (split s*)]) |
| 3101 |
(let-values ([(nm* ns* nae*) |
| 3102 |
(diff m (cdr m*) s2* (cdr ae*))]) |
| 3103 |
(values (cons m1 nm*) |
| 3104 |
(append s1* ns*) |
| 3105 |
(cons (car ae*) nae*)))))))) |
| 3106 |
(unless (id? base-id) (err "not an identifier" base-id)) |
| 3107 |
(unless (id? new-id) (err "not an identifier" new-id)) |
| 3108 |
(unless (free-identifier=? base-id new-id) |
| 3109 |
(err "not the same identifier" base-id new-id)) |
| 3110 |
(let-values ([(m* s* ae*) |
| 3111 |
(diff (car (stx-mark* base-id)) |
| 3112 |
(stx-mark* new-id) |
| 3113 |
(stx-subst* new-id) |
| 3114 |
(stx-ae* new-id))]) |
| 3115 |
(if (and (null? m*) (null? s*)) |
| 3116 |
object |
| 3117 |
(mkstx object m* s* ae*)))) |
| 3118 |
|
| 3119 |
(define chi-internal-module |
| 3120 |
(lambda (e r mr lex* rhs* mod** kwd*) |
| 3121 |
(let-values (((name exp-id* e*) (parse-module e))) |
| 3122 |
(let* ((rib (make-empty-rib)) |
| 3123 |
(e* (map (lambda (x) (add-subst rib x)) (syntax->list e*)))) |
| 3124 |
(let-values (((e* r mr lex* rhs* mod** kwd* _exp*) |
| 3125 |
(chi-body* e* r mr lex* rhs* mod** kwd* '() rib #f #t))) |
| 3126 |
(let ((exp-lab* |
| 3127 |
(vector-map |
| 3128 |
(lambda (x) |
| 3129 |
(or (id->label |
| 3130 |
(make-stx (id->sym x) (stx-mark* x) |
| 3131 |
(list rib) |
| 3132 |
'())) |
| 3133 |
(stx-error x "cannot find module export"))) |
| 3134 |
exp-id*)) |
| 3135 |
(mod** (cons e* mod**))) |
| 3136 |
(if (not name) ;;; explicit export |
| 3137 |
(values lex* rhs* exp-id* exp-lab* r mr mod** kwd*) |
| 3138 |
(let ((lab (gen-label 'module)) |
| 3139 |
(iface |
| 3140 |
(make-module-interface |
| 3141 |
(car (stx-mark* name)) |
| 3142 |
(vector-map |
| 3143 |
(lambda (x) |
| 3144 |
(make-stx (stx-expr x) (stx-mark* x) '() '())) |
| 3145 |
exp-id*) |
| 3146 |
exp-lab*))) |
| 3147 |
(values lex* rhs* |
| 3148 |
(vector name) ;;; FIXME: module cannot |
| 3149 |
(vector lab) ;;; export itself yet |
| 3150 |
(cons (cons lab (cons '$module iface)) r) |
| 3151 |
(cons (cons lab (cons '$module iface)) mr) |
| 3152 |
mod** kwd*))))))))) |
| 3153 |
|
| 3154 |
(define chi-body* |
| 3155 |
(lambda (e* r mr lex* rhs* mod** kwd* exp* rib mix? sd?) |
| 3156 |
(cond |
| 3157 |
((null? e*) (values e* r mr lex* rhs* mod** kwd* exp*)) |
| 3158 |
(else |
| 3159 |
(let ((e (car e*))) |
| 3160 |
(let-values (((type value kwd) (syntax-type e r))) |
| 3161 |
(let ((kwd* (if (id? kwd) (cons kwd kwd*) kwd*))) |
| 3162 |
(case type |
| 3163 |
((define) |
| 3164 |
(let-values (((id rhs) (parse-define e))) |
| 3165 |
(when (bound-id-member? id kwd*) |
| 3166 |
(stx-error e "cannot redefine keyword")) |
| 3167 |
(let-values (((lab lex) (gen-define-label+loc id rib sd?))) |
| 3168 |
(extend-rib! rib id lab sd?) |
| 3169 |
(chi-body* (cdr e*) |
| 3170 |
(add-lexical lab lex r) mr |
| 3171 |
(cons lex lex*) (cons rhs rhs*) |
| 3172 |
mod** kwd* exp* rib mix? sd?)))) |
| 3173 |
((define-syntax) |
| 3174 |
(let-values (((id rhs) (parse-define-syntax e))) |
| 3175 |
(when (bound-id-member? id kwd*) |
| 3176 |
(stx-error e "cannot redefine keyword")) |
| 3177 |
(let* ((lab (gen-define-label id rib sd?)) |
| 3178 |
(expanded-rhs (expand-transformer rhs mr))) |
| 3179 |
(extend-rib! rib id lab sd?) |
| 3180 |
(let ((b (make-eval-transformer expanded-rhs))) |
| 3181 |
(chi-body* (cdr e*) |
| 3182 |
(cons (cons lab b) r) (cons (cons lab b) mr) |
| 3183 |
lex* rhs* mod** kwd* exp* rib |
| 3184 |
mix? sd?))))) |
| 3185 |
((define-fluid-syntax) |
| 3186 |
(let-values (((id rhs) (parse-define-syntax e))) |
| 3187 |
(when (bound-id-member? id kwd*) |
| 3188 |
(stx-error e "cannot redefine keyword")) |
| 3189 |
(let* ((lab (gen-define-label id rib sd?)) |
| 3190 |
(flab (gen-define-label id rib sd?)) |
| 3191 |
(expanded-rhs (expand-transformer rhs mr))) |
| 3192 |
(extend-rib! rib id lab sd?) |
| 3193 |
(let ((b (make-eval-transformer expanded-rhs))) |
| 3194 |
(let ([t1 (cons lab (cons '$fluid flab))] |
| 3195 |
[t2 (cons flab b)]) |
| 3196 |
(chi-body* (cdr e*) |
| 3197 |
(cons* t1 t2 r) (cons* t1 t2 mr) |
| 3198 |
lex* rhs* mod** kwd* exp* rib |
| 3199 |
mix? sd?)))))) |
| 3200 |
((let-syntax letrec-syntax) |
| 3201 |
(syntax-match e () |
| 3202 |
((_ ((xlhs* xrhs*) ...) xbody* ...) |
| 3203 |
(unless (valid-bound-ids? xlhs*) |
| 3204 |
(stx-error e "invalid identifiers")) |
| 3205 |
(let* ((xlab* (map gen-label xlhs*)) |
| 3206 |
(xrib (make-full-rib xlhs* xlab*)) |
| 3207 |
(xb* (map (lambda (x) |
| 3208 |
(make-eval-transformer |
| 3209 |
(expand-transformer |
| 3210 |
(if (eq? type 'let-syntax) |
| 3211 |
x |
| 3212 |
(add-subst xrib x)) |
| 3213 |
mr))) |
| 3214 |
xrhs*))) |
| 3215 |
(chi-body* |
| 3216 |
(append (map (lambda (x) (add-subst xrib x)) xbody*) (cdr e*)) |
| 3217 |
(append (map cons xlab* xb*) r) |
| 3218 |
(append (map cons xlab* xb*) mr) |
| 3219 |
lex* rhs* mod** kwd* exp* rib |
| 3220 |
mix? sd?))))) |
| 3221 |
((begin) |
| 3222 |
(syntax-match e () |
| 3223 |
((_ x* ...) |
| 3224 |
(chi-body* (append x* (cdr e*)) |
| 3225 |
r mr lex* rhs* mod** kwd* exp* rib |
| 3226 |
mix? sd?)))) |
| 3227 |
((stale-when) |
| 3228 |
(syntax-match e () |
| 3229 |
((_ guard x* ...) |
| 3230 |
(begin |
| 3231 |
(handle-stale-when guard mr) |
| 3232 |
(chi-body* (append x* (cdr e*)) |
| 3233 |
r mr lex* rhs* mod** kwd* exp* rib |
| 3234 |
mix? sd?))))) |
| 3235 |
((global-macro global-macro!) |
| 3236 |
(chi-body* |
| 3237 |
(cons (chi-global-macro value e r rib) (cdr e*)) |
| 3238 |
r mr lex* rhs* mod** kwd* exp* rib mix? sd?)) |
| 3239 |
((local-macro local-macro!) |
| 3240 |
(chi-body* |
| 3241 |
(cons (chi-local-macro value e r rib) (cdr e*)) |
| 3242 |
r mr lex* rhs* mod** kwd* exp* rib mix? sd?)) |
| 3243 |
((macro macro!) |
| 3244 |
(chi-body* |
| 3245 |
(cons (chi-macro value e r rib) (cdr e*)) |
| 3246 |
r mr lex* rhs* mod** kwd* exp* rib mix? sd?)) |
| 3247 |
((module) |
| 3248 |
(let-values (((lex* rhs* m-exp-id* m-exp-lab* r mr mod** kwd*) |
| 3249 |
(chi-internal-module e r mr lex* rhs* mod** kwd*))) |
| 3250 |
(vector-for-each |
| 3251 |
(lambda (id lab) (extend-rib! rib id lab sd?)) |
| 3252 |
m-exp-id* m-exp-lab*) |
| 3253 |
(chi-body* (cdr e*) r mr lex* rhs* mod** kwd* |
| 3254 |
exp* rib mix? sd?))) |
| 3255 |
((library) |
| 3256 |
(library-expander (stx->datum e)) |
| 3257 |
(chi-body* (cdr e*) r mr lex* rhs* mod** kwd* exp* |
| 3258 |
rib mix? sd?)) |
| 3259 |
((export) |
| 3260 |
(syntax-match e () |
| 3261 |
((_ exp-decl* ...) |
| 3262 |
(chi-body* (cdr e*) r mr lex* rhs* mod** kwd* |
| 3263 |
(append exp-decl* exp*) rib |
| 3264 |
mix? sd?)))) |
| 3265 |
((import) |
| 3266 |
(let () |
| 3267 |
(define (module-import? e) |
| 3268 |
(syntax-match e () |
| 3269 |
((_ id) (id? id) #t) |
| 3270 |
((_ imp* ...) #f) |
| 3271 |
(_ (stx-error e "malformed import form")))) |
| 3272 |
(define (module-import e r) |
| 3273 |
(syntax-match e () |
| 3274 |
((_ id) (id? id) |
| 3275 |
(let-values (((type value kwd) (syntax-type id r))) |
| 3276 |
(case type |
| 3277 |
(($module) |
| 3278 |
(let ((iface value)) |
| 3279 |
(values |
| 3280 |
(module-interface-exp-id* iface id) |
| 3281 |
(module-interface-exp-lab-vec iface)))) |
| 3282 |
(else (stx-error e "invalid import"))))))) |
| 3283 |
(define (library-import e) |
| 3284 |
(syntax-match e () |
| 3285 |
((ctxt imp* ...) |
| 3286 |
(let-values (((subst-names subst-labels) |
| 3287 |
(parse-import-spec* |
| 3288 |
(syntax->datum imp*)))) |
| 3289 |
(values |
| 3290 |
(vector-map |
| 3291 |
(lambda (name) |
| 3292 |
(datum->stx ctxt name)) |
| 3293 |
subst-names) |
| 3294 |
subst-labels))) |
| 3295 |
(_ (stx-error e "invalid import form")))) |
| 3296 |
(let-values (((id* lab*) |
| 3297 |
(if (module-import? e) |
| 3298 |
(module-import e r) |
| 3299 |
(library-import e)))) |
| 3300 |
(vector-for-each |
| 3301 |
(lambda (id lab) (extend-rib! rib id lab sd?)) |
| 3302 |
id* lab*)) |
| 3303 |
(chi-body* (cdr e*) r mr lex* rhs* mod** kwd* |
| 3304 |
exp* rib mix? sd?))) |
| 3305 |
(else |
| 3306 |
(if mix? |
| 3307 |
(chi-body* (cdr e*) r mr |
| 3308 |
(cons (gen-lexical 'dummy) lex*) |
| 3309 |
(cons (cons 'top-expr e) rhs*) |
| 3310 |
mod** kwd* exp* rib #t sd?) |
| 3311 |
(values e* r mr lex* rhs* mod** kwd* exp*))))))))))) |
| 3312 |
|
| 3313 |
(define (expand-transformer expr r) |
| 3314 |
(let ((rtc (make-collector))) |
| 3315 |
(let ((expanded-rhs |
| 3316 |
(parameterize ((inv-collector rtc) |
| 3317 |
(vis-collector (lambda (x) (values)))) |
| 3318 |
(chi-expr expr r r)))) |
| 3319 |
(for-each |
| 3320 |
(let ((mark-visit (vis-collector))) |
| 3321 |
(lambda (x) |
| 3322 |
(invoke-library x) |
| 3323 |
(mark-visit x))) |
| 3324 |
(rtc)) |
| 3325 |
expanded-rhs))) |
| 3326 |
|
| 3327 |
(define (parse-exports exp*) |
| 3328 |
(let f ((exp* exp*) (int* '()) (ext* '())) |
| 3329 |
(cond |
| 3330 |
((null? exp*) |
| 3331 |
(unless (valid-bound-ids? ext*) |
| 3332 |
(syntax-violation 'export "invalid exports" |
| 3333 |
(find-dups ext*))) |
| 3334 |
(values (map syntax->datum ext*) int*)) |
| 3335 |
(else |
| 3336 |
(syntax-match (car exp*) () |
| 3337 |
((rename (i* e*) ...) |
| 3338 |
(begin |
| 3339 |
(unless (and (eq? (syntax->datum rename) 'rename) |
| 3340 |
(for-all id? i*) |
| 3341 |
(for-all id? e*)) |
| 3342 |
(syntax-violation 'export "invalid export specifier" (car exp*))) |
| 3343 |
(f (cdr exp*) (append i* int*) (append e* ext*)))) |
| 3344 |
(ie |
| 3345 |
(begin |
| 3346 |
(unless (id? ie) |
| 3347 |
(syntax-violation 'export "invalid export" ie)) |
| 3348 |
(f (cdr exp*) (cons ie int*) (cons ie ext*))))))))) |
| 3349 |
|
| 3350 |
;;; given a library name, like (foo bar (1 2 3)), |
| 3351 |
;;; returns the identifiers and the version of the library |
| 3352 |
;;; as (foo bar) (1 2 3). |
| 3353 |
(define (parse-library-name spec) |
| 3354 |
(define (parse x) |
| 3355 |
(syntax-match x () |
| 3356 |
(((v* ...)) |
| 3357 |
(for-all |
| 3358 |
(lambda (x) |
| 3359 |
(let ((x (syntax->datum x))) |
| 3360 |
(and (integer? x) (exact? x)))) |
| 3361 |
v*) |
| 3362 |
(values '() (map syntax->datum v*))) |
| 3363 |
((x . rest) (symbol? (syntax->datum x)) |
| 3364 |
(let-values (((x* v*) (parse rest))) |
| 3365 |
(values (cons (syntax->datum x) x*) v*))) |
| 3366 |
(() (values '() '())) |
| 3367 |
(_ (stx-error spec "invalid library name")))) |
| 3368 |
(let-values (((name* ver*) (parse spec))) |
| 3369 |
(when (null? name*) (stx-error spec "empty library name")) |
| 3370 |
(values name* ver*))) |
| 3371 |
|
| 3372 |
;;; given a library form, returns the name part, the export |
| 3373 |
;;; specs, import specs and the body of the library. |
| 3374 |
(define parse-library |
| 3375 |
(lambda (e) |
| 3376 |
(syntax-match e () |
| 3377 |
((library (name* ...) |
| 3378 |
(export exp* ...) |
| 3379 |
(import imp* ...) |
| 3380 |
b* ...) |
| 3381 |
(and (eq? (syntax->datum export) 'export) |
| 3382 |
(eq? (syntax->datum import) 'import) |
| 3383 |
(eq? (syntax->datum library) 'library)) |
| 3384 |
(values name* exp* imp* b*)) |
| 3385 |
(_ (stx-error e "malformed library"))))) |
| 3386 |
|
| 3387 |
;;; given a list of import-specs, return a subst and the list of |
| 3388 |
;;; libraries that were imported. |
| 3389 |
;;; Example: given ((rename (only (foo) x z) (x y)) (only (bar) q)) |
| 3390 |
;;; returns: ((z . z$label) (y . x$label) (q . q$label)) |
| 3391 |
;;; and (#<library (foo)> #<library (bar)>) |
| 3392 |
|
| 3393 |
(define (parse-import-spec* imp*) |
| 3394 |
(define (idsyn? x) (symbol? (syntax->datum x))) |
| 3395 |
(define (dup-error name) |
| 3396 |
(syntax-violation 'import "two imports with different bindings" name)) |
| 3397 |
(define (merge-substs s subst) |
| 3398 |
(define (insert-to-subst a subst) |
| 3399 |
(let ((name (car a)) (label (cdr a))) |
| 3400 |
(cond |
| 3401 |
((assq name subst) => |
| 3402 |
(lambda (x) |
| 3403 |
(cond |
| 3404 |
((eq? (cdr x) label) subst) |
| 3405 |
(else (dup-error name))))) |
| 3406 |
(else |
| 3407 |
(cons a subst))))) |
| 3408 |
(cond |
| 3409 |
((null? s) subst) |
| 3410 |
(else |
| 3411 |
(insert-to-subst (car s) |
| 3412 |
(merge-substs (cdr s) subst))))) |
| 3413 |
(define (exclude* sym* subst) |
| 3414 |
(define (exclude sym subst) |
| 3415 |
(cond |
| 3416 |
((null? subst) |
| 3417 |
(syntax-violation 'import "cannot rename unbound identifier" sym)) |
| 3418 |
((eq? sym (caar subst)) |
| 3419 |
(values (cdar subst) (cdr subst))) |
| 3420 |
(else |
| 3421 |
(let ((a (car subst))) |
| 3422 |
(let-values (((old subst) (exclude sym (cdr subst)))) |
| 3423 |
(values old (cons a subst))))))) |
| 3424 |
(cond |
| 3425 |
((null? sym*) (values '() subst)) |
| 3426 |
(else |
| 3427 |
(let-values (((old subst) (exclude (car sym*) subst))) |
| 3428 |
(let-values (((old* subst) (exclude* (cdr sym*) subst))) |
| 3429 |
(values (cons old old*) subst)))))) |
| 3430 |
(define (find* sym* subst) |
| 3431 |
(map (lambda (x) |
| 3432 |
(cond |
| 3433 |
((assq x subst) => cdr) |
| 3434 |
(else (syntax-violation 'import "cannot find identifier" x)))) |
| 3435 |
sym*)) |
| 3436 |
(define (rem* sym* subst) |
| 3437 |
(let f ((subst subst)) |
| 3438 |
(cond |
| 3439 |
((null? subst) '()) |
| 3440 |
((memq (caar subst) sym*) (f (cdr subst))) |
| 3441 |
(else (cons (car subst) (f (cdr subst))))))) |
| 3442 |
(define (remove-dups ls) |
| 3443 |
(cond |
| 3444 |
((null? ls) '()) |
| 3445 |
((memq (car ls) (cdr ls)) (remove-dups (cdr ls))) |
| 3446 |
(else (cons (car ls) (remove-dups (cdr ls)))))) |
| 3447 |
(define (parse-library-name spec) |
| 3448 |
(define (subversion? x) |
| 3449 |
(let ((x (syntax->datum x))) |
| 3450 |
(and (integer? x) (exact? x) (>= x 0)))) |
| 3451 |
(define (subversion-pred x*) |
| 3452 |
(syntax-match x* () |
| 3453 |
(n (subversion? n) |
| 3454 |
(lambda (x) (= x (syntax->datum n)))) |
| 3455 |
((p? sub* ...) (eq? (syntax->datum p?) 'and) |
| 3456 |
(let ((p* (map subversion-pred sub*))) |
| 3457 |
(lambda (x) |
| 3458 |
(for-all (lambda (p) (p x)) p*)))) |
| 3459 |
((p? sub* ...) (eq? (syntax->datum p?) 'or) |
| 3460 |
(let ((p* (map subversion-pred sub*))) |
| 3461 |
(lambda (x) |
| 3462 |
(exists (lambda (p) (p x)) p*)))) |
| 3463 |
((p? sub) (eq? (syntax->datum p?) 'not) |
| 3464 |
(let ((p (subversion-pred sub))) |
| 3465 |
(lambda (x) |
| 3466 |
(not (p x))))) |
| 3467 |
((p? n) |
| 3468 |
(and (eq? (syntax->datum p?) '<=) (subversion? n)) |
| 3469 |
(lambda (x) (<= x (syntax->datum n)))) |
| 3470 |
((p? n) |
| 3471 |
(and (eq? (syntax->datum p?) '>=) (subversion? n)) |
| 3472 |
(lambda (x) (>= x (syntax->datum n)))) |
| 3473 |
(_ (syntax-violation 'import "invalid sub-version spec" spec x*)))) |
| 3474 |
(define (version-pred x*) |
| 3475 |
(syntax-match x* () |
| 3476 |
(() (lambda (x) #t)) |
| 3477 |
((c ver* ...) (eq? (syntax->datum c) 'and) |
| 3478 |
(let ((p* (map version-pred ver*))) |
| 3479 |
(lambda (x) |
| 3480 |
(for-all (lambda (p) (p x)) p*)))) |
| 3481 |
((c ver* ...) (eq? (syntax->datum c) 'or) |
| 3482 |
(let ((p* (map version-pred ver*))) |
| 3483 |
(lambda (x) |
| 3484 |
(exists (lambda (p) (p x)) p*)))) |
| 3485 |
((c ver) (eq? (syntax->datum c) 'not) |
| 3486 |
(let ((p (version-pred ver))) |
| 3487 |
(lambda (x) (not (p x))))) |
| 3488 |
((sub* ...) |
| 3489 |
(let ((p* (map subversion-pred sub*))) |
| 3490 |
(lambda (x) |
| 3491 |
(let f ((p* p*) (x x)) |
| 3492 |
(cond |
| 3493 |
((null? p*) #t) |
| 3494 |
((null? x) #f) |
| 3495 |
(else |
| 3496 |
(and ((car p*) (car x)) |
| 3497 |
(f (cdr p*) (cdr x))))))))) |
| 3498 |
(_ (syntax-violation 'import "invalid version spec" spec x*)))) |
| 3499 |
(let f ((x spec)) |
| 3500 |
(syntax-match x () |
| 3501 |
(((version-spec* ...)) |
| 3502 |
(values '() (version-pred version-spec*))) |
| 3503 |
((x . x*) (idsyn? x) |
| 3504 |
(let-values (((name pred) (f x*))) |
| 3505 |
(values (cons (syntax->datum x) name) pred))) |
| 3506 |
(() (values '() (lambda (x) #t))) |
| 3507 |
(_ (stx-error spec "invalid import spec"))))) |
| 3508 |
(define (import-library spec*) |
| 3509 |
(let-values (((name pred) (parse-library-name spec*))) |
| 3510 |
(when (null? name) |
| 3511 |
(syntax-violation 'import "empty library name" spec*)) |
| 3512 |
(let ((lib (find-library-by-name name))) |
| 3513 |
(unless lib |
| 3514 |
(syntax-violation 'import |
| 3515 |
"cannot find library with required name" |
| 3516 |
name)) |
| 3517 |
(unless (pred (library-version lib)) |
| 3518 |
(syntax-violation 'import |
| 3519 |
"library does not satisfy version specification" |
| 3520 |
spec* lib)) |
| 3521 |
((imp-collector) lib) |
| 3522 |
(library-subst lib)))) |
| 3523 |
(define (get-import spec) |
| 3524 |
(syntax-match spec () |
| 3525 |
((x x* ...) |
| 3526 |
(not (memq (syntax->datum x) '(for rename except only prefix library))) |
| 3527 |
(import-library (cons x x*))) |
| 3528 |
((rename isp (old* new*) ...) |
| 3529 |
(and (eq? (syntax->datum rename) 'rename) |
| 3530 |
(for-all idsyn? old*) |
| 3531 |
(for-all idsyn? new*)) |
| 3532 |
(let ((subst (get-import isp)) |
| 3533 |
(old* (map syntax->datum old*)) |
| 3534 |
(new* (map syntax->datum new*))) |
| 3535 |
;;; rewrite this to eliminate find* and rem* and merge |
| 3536 |
(let ((old-label* (find* old* subst))) |
| 3537 |
(let ((subst (rem* old* subst))) |
| 3538 |
;;; FIXME: make sure map is valid |
| 3539 |
(merge-substs (map cons new* old-label*) subst))))) |
| 3540 |
((except isp sym* ...) |
| 3541 |
(and (eq? (syntax->datum except) 'except) (for-all idsyn? sym*)) |
| 3542 |
(let ((subst (get-import isp))) |
| 3543 |
(rem* (map syntax->datum sym*) subst))) |
| 3544 |
((only isp sym* ...) |
| 3545 |
(and (eq? (syntax->datum only) 'only) (for-all idsyn? sym*)) |
| 3546 |
(let ((subst (get-import isp)) |
| 3547 |
(sym* (map syntax->datum sym*))) |
| 3548 |
(let ((sym* (remove-dups sym*))) |
| 3549 |
(let ((lab* (find* sym* subst))) |
| 3550 |
(map cons sym* lab*))))) |
| 3551 |
((prefix isp p) |
| 3552 |
(and (eq? (syntax->datum prefix) 'prefix) (idsyn? p)) |
| 3553 |
(let ((subst (get-import isp)) |
| 3554 |
(prefix (symbol->string (syntax->datum p)))) |
| 3555 |
(map |
| 3556 |
(lambda (x) |
| 3557 |
(cons |
| 3558 |
(string->symbol |
| 3559 |
(string-append prefix |
| 3560 |
(symbol->string (car x)))) |
| 3561 |
(cdr x))) |
| 3562 |
subst))) |
| 3563 |
((library (spec* ...)) (eq? (syntax->datum library) 'library) |
| 3564 |
(import-library spec*)) |
| 3565 |
((for isp . rest) |
| 3566 |
(eq? (syntax->datum for) 'for) |
| 3567 |
(get-import isp)) |
| 3568 |
(spec (syntax-violation 'import "invalid import spec" spec)))) |
| 3569 |
(define (add-imports! imp h) |
| 3570 |
(let ((subst (get-import imp))) |
| 3571 |
(for-each |
| 3572 |
(lambda (x) |
| 3573 |
(let ((name (car x)) (label (cdr x))) |
| 3574 |
(cond |
| 3575 |
((hashtable-ref h name #f) => |
| 3576 |
(lambda (l) |
| 3577 |
(unless (eq? l label) |
| 3578 |
(dup-error name)))) |
| 3579 |
(else |
| 3580 |
(hashtable-set! h name label))))) |
| 3581 |
subst))) |
| 3582 |
(let f ((imp* imp*) (h (make-eq-hashtable))) |
| 3583 |
(cond |
| 3584 |
((null? imp*) |
| 3585 |
(hashtable-entries h)) |
| 3586 |
(else |
| 3587 |
(add-imports! (car imp*) h) |
| 3588 |
(f (cdr imp*) h))))) |
| 3589 |
|
| 3590 |
;;; a top rib is constructed as follows: |
| 3591 |
;;; given a subst: name* -> label*, |
| 3592 |
;;; generate a rib containing: |
| 3593 |
;;; - name* as the rib-sym*, |
| 3594 |
;;; - a list of top-mark* as the rib-mark** |
| 3595 |
;;; - label* as the rib-label* |
| 3596 |
;;; so, a name in a top rib maps to its label if and only if |
| 3597 |
;;; its set of marks is top-mark*. |
| 3598 |
(define (make-top-rib names labels) |
| 3599 |
(let ((rib (make-empty-rib))) |
| 3600 |
(vector-for-each |
| 3601 |
(lambda (name label) |
| 3602 |
(unless (symbol? name) |
| 3603 |
(error 'make-top-rib "BUG: not a symbol" name)) |
| 3604 |
(extend-rib! rib (make-stx name top-mark* '() '()) label #t)) |
| 3605 |
names labels) |
| 3606 |
rib)) |
| 3607 |
|
| 3608 |
(define (make-collector) |
| 3609 |
(let ((ls '())) |
| 3610 |
(case-lambda |
| 3611 |
(() ls) |
| 3612 |
((x) |
| 3613 |
(unless (eq? x '*interaction*) |
| 3614 |
(set! ls (set-cons x ls))))))) |
| 3615 |
|
| 3616 |
(define inv-collector |
| 3617 |
(make-parameter |
| 3618 |
(lambda args |
| 3619 |
(assertion-violation 'inv-collector "BUG: not initialized")) |
| 3620 |
(lambda (x) |
| 3621 |
(unless (procedure? x) |
| 3622 |
(assertion-violation 'inv-collector "BUG: not a procedure" x)) |
| 3623 |
x))) |
| 3624 |
|
| 3625 |
(define vis-collector |
| 3626 |
(make-parameter |
| 3627 |
(lambda args |
| 3628 |
(assertion-violation 'vis-collector "BUG: not initialized")) |
| 3629 |
(lambda (x) |
| 3630 |
(unless (procedure? x) |
| 3631 |
(assertion-violation 'vis-collector "BUG: not a procedure" x)) |
| 3632 |
x))) |
| 3633 |
|
| 3634 |
(define imp-collector |
| 3635 |
(make-parameter |
| 3636 |
(lambda args |
| 3637 |
(assertion-violation 'imp-collector "BUG: not initialized")) |
| 3638 |
(lambda (x) |
| 3639 |
(unless (procedure? x) |
| 3640 |
(assertion-violation 'imp-collector "BUG: not a procedure" x)) |
| 3641 |
x))) |
| 3642 |
|
| 3643 |
|
| 3644 |
(define chi-library-internal |
| 3645 |
(lambda (e* rib mix?) |
| 3646 |
(let-values (((e* r mr lex* rhs* mod** _kwd* exp*) |
| 3647 |
(chi-body* e* '() '() '() '() '() '() '() rib mix? #t))) |
| 3648 |
(values (append (apply append (reverse mod**)) e*) |
| 3649 |
r mr (reverse lex*) (reverse rhs*) exp*)))) |
| 3650 |
|
| 3651 |
|
| 3652 |
(define chi-interaction-expr |
| 3653 |
(lambda (e rib r) |
| 3654 |
(let-values (((e* r mr lex* rhs* mod** _kwd* _exp*) |
| 3655 |
(chi-body* (list e) r r '() '() '() '() '() rib |
| 3656 |
#t #f))) |
| 3657 |
(let ((e* (expand-interaction-rhs*/init* |
| 3658 |
(reverse lex*) (reverse rhs*) |
| 3659 |
(append (apply append (reverse mod**)) e*) |
| 3660 |
r mr))) |
| 3661 |
(let ((e (cond |
| 3662 |
((null? e*) (build-void)) |
| 3663 |
((null? (cdr e*)) (car e*)) |
| 3664 |
(else (build-sequence no-source e*))))) |
| 3665 |
(values e r)))))) |
| 3666 |
|
| 3667 |
(define library-body-expander |
| 3668 |
(lambda (main-exp* imp* b* mix?) |
| 3669 |
(define itc (make-collector)) |
| 3670 |
(parameterize ((imp-collector itc) |
| 3671 |
(top-level-context #f)) |
| 3672 |
(let-values (((subst-names subst-labels) |
| 3673 |
(parse-import-spec* imp*))) |
| 3674 |
(let ((rib (make-top-rib subst-names subst-labels))) |
| 3675 |
(define (wrap x) (make-stx x top-mark* (list rib) '())) |
| 3676 |
(let ((b* (map wrap b*)) |
| 3677 |
(rtc (make-collector)) |
| 3678 |
(vtc (make-collector))) |
| 3679 |
(parameterize ((inv-collector rtc) |
| 3680 |
(vis-collector vtc)) |
| 3681 |
(let-values (((init* r mr lex* rhs* internal-exp*) |
| 3682 |
(chi-library-internal b* rib mix?))) |
| 3683 |
(let-values (((exp-name* exp-id*) |
| 3684 |
(parse-exports |
| 3685 |
(if (eq? main-exp* 'all) |
| 3686 |
(map wrap (top-marked-symbols rib)) |
| 3687 |
(append |
| 3688 |
(map wrap main-exp*) |
| 3689 |
internal-exp*))))) |
| 3690 |
(seal-rib! rib) |
| 3691 |
(let* ((init* (chi-expr* init* r mr)) |
| 3692 |
(rhs* (chi-rhs* rhs* r mr))) |
| 3693 |
(unseal-rib! rib) |
| 3694 |
(let ((loc* (map gen-global lex*)) |
| 3695 |
(export-subst (make-export-subst exp-name* exp-id*))) |
| 3696 |
(define errstr |
| 3697 |
"attempt to export mutated variable") |
| 3698 |
(let-values (((export-env global* macro*) |
| 3699 |
(make-export-env/macros lex* loc* r))) |
| 3700 |
(unless (eq? main-exp* 'all) |
| 3701 |
(for-each |
| 3702 |
(lambda (s) |
| 3703 |
(let ((name (car s)) (label (cdr s))) |
| 3704 |
(let ((p (assq label export-env))) |
| 3705 |
(when p |
| 3706 |
(let ((b (cdr p))) |
| 3707 |
(let ((type (car b))) |
| 3708 |
(when (eq? type 'mutable) |
| 3709 |
(syntax-violation 'export |
| 3710 |
errstr name)))))))) |
| 3711 |
export-subst)) |
| 3712 |
(let ((invoke-body |
| 3713 |
(build-library-letrec* no-source |
| 3714 |
mix? |
| 3715 |
lex* loc* rhs* |
| 3716 |
(if (null? init*) |
| 3717 |
(build-void) |
| 3718 |
(build-sequence no-source init*)))) |
| 3719 |
;(invoke-body |
| 3720 |
; (build-letrec* no-source lex* rhs* |
| 3721 |
; (build-exports global* init*))) |
| 3722 |
(invoke-definitions |
| 3723 |
(map build-global-define (map cdr global*)))) |
| 3724 |
(values |
| 3725 |
(itc) (rtc) (vtc) |
| 3726 |
(build-sequence no-source |
| 3727 |
(append invoke-definitions |
| 3728 |
(list invoke-body))) |
| 3729 |
macro* export-subst export-env)))))))))))))) |
| 3730 |
|
| 3731 |
(define stale-when-collector (make-parameter #f)) |
| 3732 |
|
| 3733 |
(define (make-stale-collector) |
| 3734 |
(let ([code (build-data no-source #f)] |
| 3735 |
[req* '()]) |
| 3736 |
(case-lambda |
| 3737 |
[() (values code req*)] |
| 3738 |
[(c r*) |
| 3739 |
(set! code |
| 3740 |
(build-conditional no-source |
| 3741 |
code |
| 3742 |
(build-data no-source #t) |
| 3743 |
c)) |
| 3744 |
(set! req* (set-union r* req*))]))) |
| 3745 |
|
| 3746 |
(define (handle-stale-when guard-expr mr) |
| 3747 |
(let ([stc (make-collector)]) |
| 3748 |
(let ([core-expr (parameterize ([inv-collector stc]) |
| 3749 |
(chi-expr guard-expr mr mr))]) |
| 3750 |
(cond |
| 3751 |
[(stale-when-collector) => |
| 3752 |
(lambda (c) (c core-expr (stc)))])))) |
| 3753 |
|
| 3754 |
(define core-library-expander |
| 3755 |
(case-lambda |
| 3756 |
((e verify-name) |
| 3757 |
(let-values (((name* exp* imp* b*) (parse-library e))) |
| 3758 |
(let-values (((name ver) (parse-library-name name*))) |
| 3759 |
(verify-name name) |
| 3760 |
(let ([c (make-stale-collector)]) |
| 3761 |
(let-values (((imp* invoke-req* visit-req* invoke-code |
| 3762 |
visit-code export-subst export-env) |
| 3763 |
(parameterize ([stale-when-collector c]) |
| 3764 |
(library-body-expander exp* imp* b* #f)))) |
| 3765 |
(let-values ([(guard-code guard-req*) (c)]) |
| 3766 |
(values name ver imp* invoke-req* visit-req* |
| 3767 |
invoke-code visit-code export-subst |
| 3768 |
export-env guard-code guard-req*))))))))) |
| 3769 |
|
| 3770 |
(define (parse-top-level-program e*) |
| 3771 |
(syntax-match e* () |
| 3772 |
(((import imp* ...) b* ...) |
| 3773 |
(eq? (syntax->datum import) 'import) |
| 3774 |
(values imp* b*)) |
| 3775 |
(((import . x) . y) |
| 3776 |
(eq? (syntax->datum import) 'import) |
| 3777 |
(syntax-violation 'expander |
| 3778 |
"invalid syntax of top-level program" (syntax-car e*))) |
| 3779 |
(_ |
| 3780 |
(assertion-violation 'expander |
| 3781 |
"top-level program is missing an (import ---) clause")))) |
| 3782 |
|
| 3783 |
|
| 3784 |
;;; An env record encapsulates a substitution and a set of |
| 3785 |
;;; libraries. |
| 3786 |
(define-record env (names labels itc) |
| 3787 |
(lambda (x p wr) |
| 3788 |
(display "#<environment>" p))) |
| 3789 |
|
| 3790 |
(define-record interaction-env (rib r locs) |
| 3791 |
(lambda (x p wr) |
| 3792 |
(display "#<environment>" p))) |
| 3793 |
|
| 3794 |
(define environment? |
| 3795 |
(lambda (x) (or (env? x) (interaction-env? x)))) |
| 3796 |
|
| 3797 |
(define (environment-symbols x) |
| 3798 |
(cond |
| 3799 |
((env? x) (vector->list (env-names x))) |
| 3800 |
((interaction-env? x) |
| 3801 |
(map values (rib-sym* (interaction-env-rib x)))) |
| 3802 |
(else |
| 3803 |
(assertion-violation 'environment-symbols "not an environment" x)))) |
| 3804 |
|
| 3805 |
;;; This is R6RS's environment. It parses the import specs |
| 3806 |
;;; and constructs an env record that can be used later by |
| 3807 |
;;; eval and/or expand. |
| 3808 |
(define environment |
| 3809 |
(lambda imp* |
| 3810 |
(let ((itc (make-collector))) |
| 3811 |
(parameterize ((imp-collector itc)) |
| 3812 |
(let-values (((subst-names subst-labels) |
| 3813 |
(parse-import-spec* imp*))) |
| 3814 |
(make-env subst-names subst-labels itc)))))) |
| 3815 |
|
| 3816 |
;;; R6RS's null-environment and scheme-report-environment are |
| 3817 |
;;; constructed simply using the corresponding libraries. |
| 3818 |
(define (null-environment n) |
| 3819 |
(unless (eqv? n 5) |
| 3820 |
(assertion-violation 'null-environment "not 5" n)) |
| 3821 |
(environment '(psyntax null-environment-5))) |
| 3822 |
(define (scheme-report-environment n) |
| 3823 |
(unless (eqv? n 5) |
| 3824 |
(assertion-violation 'scheme-report-environment "not 5" n)) |
| 3825 |
(environment '(psyntax scheme-report-environment-5))) |
| 3826 |
|
| 3827 |
;;; The expand procedure is the interface to the internal expression |
| 3828 |
;;; expander (chi-expr). It takes an expression and an environment. |
| 3829 |
;;; It returns two values: The resulting core-expression and a list of |
| 3830 |
;;; libraries that must be invoked before evaluating the core expr. |
| 3831 |
(define core-expand |
| 3832 |
(lambda (x env) |
| 3833 |
(cond |
| 3834 |
((env? env) |
| 3835 |
(let ((rib (make-top-rib (env-names env) (env-labels env)))) |
| 3836 |
(let ((x (make-stx x top-mark* (list rib) '())) |
| 3837 |
(itc (env-itc env)) |
| 3838 |
(rtc (make-collector)) |
| 3839 |
(vtc (make-collector))) |
| 3840 |
(let ((x |
| 3841 |
(parameterize ((top-level-context #f) |
| 3842 |
(inv-collector rtc) |
| 3843 |
(vis-collector vtc) |
| 3844 |
(imp-collector itc)) |
| 3845 |
(chi-expr x '() '())))) |
| 3846 |
(seal-rib! rib) |
| 3847 |
(values x (rtc)))))) |
| 3848 |
((interaction-env? env) |
| 3849 |
(let ((rib (interaction-env-rib env)) |
| 3850 |
(r (interaction-env-r env)) |
| 3851 |
(rtc (make-collector))) |
| 3852 |
(let ((x (make-stx x top-mark* (list rib) '()))) |
| 3853 |
(let-values (((e r^) |
| 3854 |
(parameterize ((top-level-context env) |
| 3855 |
(inv-collector rtc) |
| 3856 |
(vis-collector (make-collector)) |
| 3857 |
(imp-collector (make-collector))) |
| 3858 |
(chi-interaction-expr x rib r)))) |
| 3859 |
(set-interaction-env-r! env r^) |
| 3860 |
(values e (rtc)))))) |
| 3861 |
(else |
| 3862 |
(assertion-violation 'expand "not an environment" env))))) |
| 3863 |
|
| 3864 |
|
| 3865 |
;;; This is R6RS's eval. It takes an expression and an environment, |
| 3866 |
;;; expands the expression, invokes its invoke-required libraries and |
| 3867 |
;;; evaluates its expanded core form. |
| 3868 |
(define eval |
| 3869 |
(lambda (x env) |
| 3870 |
(unless (environment? env) |
| 3871 |
(error 'eval "not an environment" env)) |
| 3872 |
(let-values (((x invoke-req*) (core-expand x env))) |
| 3873 |
(for-each invoke-library invoke-req*) |
| 3874 |
(eval-core (expanded->core x))))) |
| 3875 |
|
| 3876 |
|
| 3877 |
;;; Given a (library . _) s-expression, library-expander expands |
| 3878 |
;;; it to core-form, registers it with the library manager, and |
| 3879 |
;;; returns its invoke-code, visit-code, subst and env. |
| 3880 |
|
| 3881 |
(define (initial-visit! macro*) |
| 3882 |
(for-each (lambda (x) |
| 3883 |
(let ((loc (car x)) (proc (cadr x))) |
| 3884 |
(set-symbol-value! loc proc))) |
| 3885 |
macro*)) |
| 3886 |
|
| 3887 |
(define library-expander |
| 3888 |
(case-lambda |
| 3889 |
((x filename verify-name) |
| 3890 |
(define (build-visit-code macro*) |
| 3891 |
(if (null? macro*) |
| 3892 |
(build-void) |
| 3893 |
(build-sequence no-source |
| 3894 |
(map (lambda (x) |
| 3895 |
(let ((loc (car x)) (src (cddr x))) |
| 3896 |
(build-global-assignment no-source loc src))) |
| 3897 |
macro*)))) |
| 3898 |
(let-values (((name ver imp* inv* vis* |
| 3899 |
invoke-code macro* export-subst export-env |
| 3900 |
guard-code guard-req*) |
| 3901 |
(core-library-expander x verify-name))) |
| 3902 |
(let ((id (gensym)) |
| 3903 |
(name name) |
| 3904 |
(ver ver) |
| 3905 |
(imp* (map library-spec imp*)) |
| 3906 |
(vis* (map library-spec vis*)) |
| 3907 |
(inv* (map library-spec inv*)) |
| 3908 |
(guard-req* (map library-spec guard-req*)) |
| 3909 |
(visit-proc (lambda () (initial-visit! macro*))) |
| 3910 |
(invoke-proc |
| 3911 |
(lambda () (eval-core (expanded->core invoke-code)))) |
| 3912 |
(visit-code (build-visit-code macro*)) |
| 3913 |
(invoke-code invoke-code)) |
| 3914 |
(install-library id name ver |
| 3915 |
imp* vis* inv* export-subst export-env |
| 3916 |
visit-proc invoke-proc |
| 3917 |
visit-code invoke-code |
| 3918 |
guard-code guard-req* |
| 3919 |
#t filename) |
| 3920 |
(values id name ver imp* vis* inv* |
| 3921 |
invoke-code visit-code |
| 3922 |
export-subst export-env |
| 3923 |
guard-code guard-req*)))) |
| 3924 |
((x filename) |
| 3925 |
(library-expander x filename (lambda (x) (values)))) |
| 3926 |
((x) |
| 3927 |
(library-expander x #f (lambda (x) (values)))))) |
| 3928 |
|
| 3929 |
;;; when bootstrapping the system, visit-code is not (and cannot |
| 3930 |
;;; be) be used in the "next" system. So, we drop it. |
| 3931 |
(define (boot-library-expand x) |
| 3932 |
(let-values (((id name ver imp* vis* inv* |
| 3933 |
invoke-code visit-code export-subst export-env |
| 3934 |
guard-code guard-dep*) |
| 3935 |
(library-expander x))) |
| 3936 |
(values name invoke-code export-subst export-env))) |
| 3937 |
|
| 3938 |
(define (rev-map-append f ls ac) |
| 3939 |
(cond |
| 3940 |
((null? ls) ac) |
| 3941 |
(else |
| 3942 |
(rev-map-append f (cdr ls) |
| 3943 |
(cons (f (car ls)) ac))))) |
| 3944 |
|
| 3945 |
(define build-exports |
| 3946 |
(lambda (lex*+loc* init*) |
| 3947 |
(build-sequence no-source |
| 3948 |
(cons (build-void) |
| 3949 |
(rev-map-append |
| 3950 |
(lambda (x) |
| 3951 |
(build-global-assignment no-source (cdr x) (car x))) |
| 3952 |
lex*+loc* |
| 3953 |
init*))))) |
| 3954 |
|
| 3955 |
(define (make-export-subst name* id*) |
| 3956 |
(map |
| 3957 |
(lambda (name id) |
| 3958 |
(let ((label (id->label id))) |
| 3959 |
(unless label |
| 3960 |
(stx-error id "cannot export unbound identifier")) |
| 3961 |
(cons name label))) |
| 3962 |
name* id*)) |
| 3963 |
|
| 3964 |
(define (make-export-env/macros lex* loc* r) |
| 3965 |
(define (lookup x) |
| 3966 |
(let f ((x x) (lex* lex*) (loc* loc*)) |
| 3967 |
(cond |
| 3968 |
((pair? lex*) |
| 3969 |
(if (eq? x (car lex*)) |
| 3970 |
(car loc*) |
| 3971 |
(f x (cdr lex*) (cdr loc*)))) |
| 3972 |
(else (assertion-violation 'lookup-make-export "BUG"))))) |
| 3973 |
(let f ((r r) (env '()) (global* '()) (macro* '())) |
| 3974 |
(cond |
| 3975 |
((null? r) (values env global* macro*)) |
| 3976 |
(else |
| 3977 |
(let ((x (car r))) |
| 3978 |
(let ((label (car x)) (b (cdr x))) |
| 3979 |
(case (binding-type b) |
| 3980 |
((lexical) |
| 3981 |
(let ((v (binding-value b))) |
| 3982 |
(let ((loc (lookup (lexical-var v))) |
| 3983 |
(type (if (lexical-mutable? v) |
| 3984 |
'mutable |
| 3985 |
'global))) |
| 3986 |
(f (cdr r) |
| 3987 |
(cons (cons* label type loc) env) |
| 3988 |
(cons (cons (lexical-var v) loc) global*) |
| 3989 |
macro*)))) |
| 3990 |
((local-macro) |
| 3991 |
(let ((loc (gensym))) |
| 3992 |
(f (cdr r) |
| 3993 |
(cons (cons* label 'global-macro loc) env) |
| 3994 |
global* |
| 3995 |
(cons (cons loc (binding-value b)) macro*)))) |
| 3996 |
((local-macro!) |
| 3997 |
(let ((loc (gensym))) |
| 3998 |
(f (cdr r) |
| 3999 |
(cons (cons* label 'global-macro! loc) env) |
| 4000 |
global* |
| 4001 |
(cons (cons loc (binding-value b)) macro*)))) |
| 4002 |
((local-ctv) |
| 4003 |
(let ((loc (gensym))) |
| 4004 |
(f (cdr r) |
| 4005 |
(cons (cons* label 'global-ctv loc) env) |
| 4006 |
global* |
| 4007 |
(cons (cons loc (binding-value b)) macro*)))) |
| 4008 |
(($rtd $module $fluid) |
| 4009 |
(f (cdr r) (cons x env) global* macro*)) |
| 4010 |
(else |
| 4011 |
(assertion-violation 'expander "BUG: do not know how to export" |
| 4012 |
(binding-type b) (binding-value b)))))))))) |
| 4013 |
|
| 4014 |
(define generate-temporaries |
| 4015 |
(lambda (ls) |
| 4016 |
(syntax-match ls () |
| 4017 |
((ls ...) |
| 4018 |
(map (lambda (x) |
| 4019 |
(make-stx |
| 4020 |
(let ((x (syntax->datum x))) |
| 4021 |
(cond |
| 4022 |
((or (symbol? x) (string? x)) |
| 4023 |
(gensym x)) |
| 4024 |
(else (gensym 't)))) |
| 4025 |
top-mark* '() '())) |
| 4026 |
ls)) |
| 4027 |
(_ |
| 4028 |
(assertion-violation 'generate-temporaries "not a list"))))) |
| 4029 |
|
| 4030 |
(define free-identifier=? |
| 4031 |
(lambda (x y) |
| 4032 |
(if (id? x) |
| 4033 |
(if (id? y) |
| 4034 |
(free-id=? x y) |
| 4035 |
(assertion-violation 'free-identifier=? "not an identifier" y)) |
| 4036 |
(assertion-violation 'free-identifier=? "not an identifier" x)))) |
| 4037 |
|
| 4038 |
(define bound-identifier=? |
| 4039 |
(lambda (x y) |
| 4040 |
(if (id? x) |
| 4041 |
(if (id? y) |
| 4042 |
(bound-id=? x y) |
| 4043 |
(assertion-violation 'bound-identifier=? "not an identifier" y)) |
| 4044 |
(assertion-violation 'bound-identifier=? "not an identifier" x)))) |
| 4045 |
|
| 4046 |
(define (position->condition x) |
| 4047 |
(if (pair? x) |
| 4048 |
(make-source-position-condition (car x) (cdr x)) |
| 4049 |
(condition))) |
| 4050 |
|
| 4051 |
(define (extract-position-condition x) |
| 4052 |
(position->condition (expression-position x))) |
| 4053 |
|
| 4054 |
(define (expression-position x) |
| 4055 |
(and (stx? x) |
| 4056 |
(let ((x (stx-expr x))) |
| 4057 |
(and (annotation? x) |
| 4058 |
(annotation-source x))))) |
| 4059 |
|
| 4060 |
(define (syntax-annotation x) |
| 4061 |
(if (stx? x) (stx-expr x) x)) |
| 4062 |
|
| 4063 |
; (define (syntax-annotation x) |
| 4064 |
; (if (stx? x) |
| 4065 |
; (let ([expr (stx-expr x)]) |
| 4066 |
; (if (annotation? x) |
| 4067 |
; x |
| 4068 |
; (stx->datum x))) |
| 4069 |
; (stx->datum x))) |
| 4070 |
|
| 4071 |
(define (assertion-error expr pos) |
| 4072 |
(raise |
| 4073 |
(condition |
| 4074 |
(make-assertion-violation) |
| 4075 |
(make-who-condition 'assert) |
| 4076 |
(make-message-condition "assertion failed") |
| 4077 |
(make-irritants-condition (list expr)) |
| 4078 |
(position->condition pos)))) |
| 4079 |
|
| 4080 |
(define syntax-error |
| 4081 |
(lambda (x . args) |
| 4082 |
(unless (for-all string? args) |
| 4083 |
(assertion-violation 'syntax-error "invalid argument" args)) |
| 4084 |
(raise |
| 4085 |
(condition |
| 4086 |
(make-message-condition |
| 4087 |
(if (null? args) |
| 4088 |
"invalid syntax" |
| 4089 |
(apply string-append args))) |
| 4090 |
(make-syntax-violation |
| 4091 |
(syntax->datum x) |
| 4092 |
#f) |
| 4093 |
(extract-position-condition x) |
| 4094 |
(extract-trace x))))) |
| 4095 |
|
| 4096 |
(define (extract-trace x) |
| 4097 |
(define-condition-type &trace &condition |
| 4098 |
make-trace trace? |
| 4099 |
(form trace-form)) |
| 4100 |
(let f ((x x)) |
| 4101 |
(cond |
| 4102 |
((stx? x) |
| 4103 |
(apply condition |
| 4104 |
(make-trace x) |
| 4105 |
(map f (stx-ae* x)))) |
| 4106 |
((annotation? x) |
| 4107 |
(make-trace (make-stx x '() '() '()))) |
| 4108 |
(else (condition))))) |
| 4109 |
|
| 4110 |
(define syntax-violation* |
| 4111 |
(lambda (who msg form condition-object) |
| 4112 |
(unless (string? msg) |
| 4113 |
(assertion-violation 'syntax-violation "message is not a string" msg)) |
| 4114 |
(let ((who |
| 4115 |
(cond |
| 4116 |
((or (string? who) (symbol? who)) who) |
| 4117 |
((not who) |
| 4118 |
(syntax-match form () |
| 4119 |
(id (id? id) (syntax->datum id)) |
| 4120 |
((id . rest) (id? id) (syntax->datum id)) |
| 4121 |
(_ #f))) |
| 4122 |
(else |
| 4123 |
(assertion-violation 'syntax-violation |
| 4124 |
"invalid who argument" who))))) |
| 4125 |
(raise |
| 4126 |
(condition |
| 4127 |
(if who |
| 4128 |
(make-who-condition who) |
| 4129 |
(condition)) |
| 4130 |
(make-message-condition msg) |
| 4131 |
condition-object |
| 4132 |
(extract-position-condition form) |
| 4133 |
(extract-trace form)))))) |
| 4134 |
|
| 4135 |
(define syntax-violation |
| 4136 |
(case-lambda |
| 4137 |
((who msg form) (syntax-violation who msg form #f)) |
| 4138 |
((who msg form subform) |
| 4139 |
(syntax-violation* who msg form |
| 4140 |
(make-syntax-violation |
| 4141 |
(syntax->datum form) |
| 4142 |
(syntax->datum subform)))))) |
| 4143 |
|
| 4144 |
(define identifier? (lambda (x) (id? x))) |
| 4145 |
|
| 4146 |
(define datum->syntax |
| 4147 |
(lambda (id datum) |
| 4148 |
(if (id? id) |
| 4149 |
(datum->stx id datum) |
| 4150 |
(assertion-violation 'datum->syntax "not an identifier" id)))) |
| 4151 |
|
| 4152 |
(define syntax->datum |
| 4153 |
(lambda (x) (stx->datum x))) |
| 4154 |
|
| 4155 |
(define top-level-expander |
| 4156 |
(lambda (e*) |
| 4157 |
(let-values (((imp* b*) (parse-top-level-program e*))) |
| 4158 |
(let-values (((imp* invoke-req* visit-req* invoke-code |
| 4159 |
macro* export-subst export-env) |
| 4160 |
(library-body-expander 'all imp* b* #t))) |
| 4161 |
(values invoke-req* invoke-code macro* |
| 4162 |
export-subst export-env))))) |
| 4163 |
|
| 4164 |
(define compile-r6rs-top-level |
| 4165 |
(lambda (x*) |
| 4166 |
(let-values (((lib* invoke-code macro* export-subst export-env) |
| 4167 |
(top-level-expander x*))) |
| 4168 |
(lambda () |
| 4169 |
(for-each invoke-library lib*) |
| 4170 |
(initial-visit! macro*) |
| 4171 |
(eval-core (expanded->core invoke-code)) |
| 4172 |
(make-interaction-env |
| 4173 |
(subst->rib export-subst) |
| 4174 |
(map |
| 4175 |
(lambda (x) |
| 4176 |
(let ([label (car x)] [binding (cdr x)]) |
| 4177 |
(let ([type (car binding)] [val (cdr binding)]) |
| 4178 |
(cons* label type '*interaction* val)))) |
| 4179 |
export-env) |
| 4180 |
'()))))) |
| 4181 |
|
| 4182 |
(define (subst->rib subst) |
| 4183 |
(let ([rib (make-empty-rib)]) |
| 4184 |
(set-rib-sym*! rib (map car subst)) |
| 4185 |
(set-rib-mark**! rib |
| 4186 |
(map (lambda (x) top-mark*) subst)) |
| 4187 |
(set-rib-label*! rib (map cdr subst)) |
| 4188 |
rib)) |
| 4189 |
|
| 4190 |
(define (new-interaction-environment) |
| 4191 |
(let ((lib (find-library-by-name |
| 4192 |
(base-of-interaction-library)))) |
| 4193 |
(let ((rib (subst->rib (library-subst lib)))) |
| 4194 |
(make-interaction-env rib '() '())))) |
| 4195 |
|
| 4196 |
(define interaction-environment |
| 4197 |
(let ((e #f)) |
| 4198 |
(case-lambda |
| 4199 |
[() |
| 4200 |
(or e (begin (set! e (new-interaction-environment)) e))] |
| 4201 |
[(x) |
| 4202 |
(unless (environment? x) |
| 4203 |
(assertion-violation 'interaction-environment |
| 4204 |
"not an environment" x)) |
| 4205 |
(set! e x)]))) |
| 4206 |
|
| 4207 |
(define top-level-context (make-parameter #f)) |
| 4208 |
|
| 4209 |
;;; register the expander with the library manager |
| 4210 |
(current-library-expander library-expander)) |
| 4211 |
|
| 4212 |
|
| 4213 |
|