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