| 1 | ;;; -*- mode: scheme; coding: utf-8; -*- |
| 2 | |
| 3 | ;;;; Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003,2004,2005,2006,2007,2008,2009,2010,2011 |
| 4 | ;;;; Free Software Foundation, Inc. |
| 5 | ;;;; |
| 6 | ;;;; This library is free software; you can redistribute it and/or |
| 7 | ;;;; modify it under the terms of the GNU Lesser General Public |
| 8 | ;;;; License as published by the Free Software Foundation; either |
| 9 | ;;;; version 3 of the License, or (at your option) any later version. |
| 10 | ;;;; |
| 11 | ;;;; This library is distributed in the hope that it will be useful, |
| 12 | ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | ;;;; Lesser General Public License for more details. |
| 15 | ;;;; |
| 16 | ;;;; You should have received a copy of the GNU Lesser General Public |
| 17 | ;;;; License along with this library; if not, write to the Free Software |
| 18 | ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 | ;;;; |
| 20 | |
| 21 | \f |
| 22 | |
| 23 | ;;; Commentary: |
| 24 | |
| 25 | ;;; This file is the first thing loaded into Guile. It adds many mundane |
| 26 | ;;; definitions and a few that are interesting. |
| 27 | ;;; |
| 28 | ;;; The module system (hence the hierarchical namespace) are defined in this |
| 29 | ;;; file. |
| 30 | ;;; |
| 31 | |
| 32 | ;;; Code: |
| 33 | |
| 34 | \f |
| 35 | |
| 36 | ;; Before compiling, make sure any symbols are resolved in the (guile) |
| 37 | ;; module, the primary location of those symbols, rather than in |
| 38 | ;; (guile-user), the default module that we compile in. |
| 39 | |
| 40 | (eval-when (compile) |
| 41 | (set-current-module (resolve-module '(guile)))) |
| 42 | |
| 43 | \f |
| 44 | |
| 45 | ;;; {Error handling} |
| 46 | ;;; |
| 47 | |
| 48 | ;; Define delimited continuation operators, and implement catch and throw in |
| 49 | ;; terms of them. |
| 50 | |
| 51 | (define make-prompt-tag |
| 52 | (lambda* (#:optional (stem "prompt")) |
| 53 | (gensym stem))) |
| 54 | |
| 55 | (define default-prompt-tag |
| 56 | ;; not sure if we should expose this to the user as a fluid |
| 57 | (let ((%default-prompt-tag (make-prompt-tag))) |
| 58 | (lambda () |
| 59 | %default-prompt-tag))) |
| 60 | |
| 61 | (define (call-with-prompt tag thunk handler) |
| 62 | (@prompt tag (thunk) handler)) |
| 63 | (define (abort-to-prompt tag . args) |
| 64 | (@abort tag args)) |
| 65 | |
| 66 | |
| 67 | ;; Define catch and with-throw-handler, using some common helper routines and a |
| 68 | ;; shared fluid. Hide the helpers in a lexical contour. |
| 69 | |
| 70 | (define with-throw-handler #f) |
| 71 | (let () |
| 72 | (define (default-exception-handler k . args) |
| 73 | (cond |
| 74 | ((eq? k 'quit) |
| 75 | (primitive-exit (cond |
| 76 | ((not (pair? args)) 0) |
| 77 | ((integer? (car args)) (car args)) |
| 78 | ((not (car args)) 1) |
| 79 | (else 0)))) |
| 80 | (else |
| 81 | (format (current-error-port) "guile: uncaught throw to ~a: ~a\n" k args) |
| 82 | (primitive-exit 1)))) |
| 83 | |
| 84 | (define %running-exception-handlers (make-fluid '())) |
| 85 | (define %exception-handler (make-fluid default-exception-handler)) |
| 86 | |
| 87 | (define (default-throw-handler prompt-tag catch-k) |
| 88 | (let ((prev (fluid-ref %exception-handler))) |
| 89 | (lambda (thrown-k . args) |
| 90 | (if (or (eq? thrown-k catch-k) (eqv? catch-k #t)) |
| 91 | (apply abort-to-prompt prompt-tag thrown-k args) |
| 92 | (apply prev thrown-k args))))) |
| 93 | |
| 94 | (define (custom-throw-handler prompt-tag catch-k pre) |
| 95 | (let ((prev (fluid-ref %exception-handler))) |
| 96 | (lambda (thrown-k . args) |
| 97 | (if (or (eq? thrown-k catch-k) (eqv? catch-k #t)) |
| 98 | (let ((running (fluid-ref %running-exception-handlers))) |
| 99 | (with-fluids ((%running-exception-handlers (cons pre running))) |
| 100 | (if (not (memq pre running)) |
| 101 | (apply pre thrown-k args)) |
| 102 | ;; fall through |
| 103 | (if prompt-tag |
| 104 | (apply abort-to-prompt prompt-tag thrown-k args) |
| 105 | (apply prev thrown-k args)))) |
| 106 | (apply prev thrown-k args))))) |
| 107 | |
| 108 | (set! catch |
| 109 | (lambda* (k thunk handler #:optional pre-unwind-handler) |
| 110 | "Invoke @var{thunk} in the dynamic context of @var{handler} for |
| 111 | exceptions matching @var{key}. If thunk throws to the symbol |
| 112 | @var{key}, then @var{handler} is invoked this way: |
| 113 | @lisp |
| 114 | (handler key args ...) |
| 115 | @end lisp |
| 116 | |
| 117 | @var{key} is a symbol or @code{#t}. |
| 118 | |
| 119 | @var{thunk} takes no arguments. If @var{thunk} returns |
| 120 | normally, that is the return value of @code{catch}. |
| 121 | |
| 122 | Handler is invoked outside the scope of its own @code{catch}. |
| 123 | If @var{handler} again throws to the same key, a new handler |
| 124 | from further up the call chain is invoked. |
| 125 | |
| 126 | If the key is @code{#t}, then a throw to @emph{any} symbol will |
| 127 | match this call to @code{catch}. |
| 128 | |
| 129 | If a @var{pre-unwind-handler} is given and @var{thunk} throws |
| 130 | an exception that matches @var{key}, Guile calls the |
| 131 | @var{pre-unwind-handler} before unwinding the dynamic state and |
| 132 | invoking the main @var{handler}. @var{pre-unwind-handler} should |
| 133 | be a procedure with the same signature as @var{handler}, that |
| 134 | is @code{(lambda (key . args))}. It is typically used to save |
| 135 | the stack at the point where the exception occurred, but can also |
| 136 | query other parts of the dynamic state at that point, such as |
| 137 | fluid values. |
| 138 | |
| 139 | A @var{pre-unwind-handler} can exit either normally or non-locally. |
| 140 | If it exits normally, Guile unwinds the stack and dynamic context |
| 141 | and then calls the normal (third argument) handler. If it exits |
| 142 | non-locally, that exit determines the continuation." |
| 143 | (if (not (or (symbol? k) (eqv? k #t))) |
| 144 | (scm-error "catch" 'wrong-type-arg |
| 145 | "Wrong type argument in position ~a: ~a" |
| 146 | (list 1 k) (list k))) |
| 147 | (let ((tag (make-prompt-tag "catch"))) |
| 148 | (call-with-prompt |
| 149 | tag |
| 150 | (lambda () |
| 151 | (with-fluids |
| 152 | ((%exception-handler |
| 153 | (if pre-unwind-handler |
| 154 | (custom-throw-handler tag k pre-unwind-handler) |
| 155 | (default-throw-handler tag k)))) |
| 156 | (thunk))) |
| 157 | (lambda (cont k . args) |
| 158 | (apply handler k args)))))) |
| 159 | |
| 160 | (set! with-throw-handler |
| 161 | (lambda (k thunk pre-unwind-handler) |
| 162 | "Add @var{handler} to the dynamic context as a throw handler |
| 163 | for key @var{key}, then invoke @var{thunk}." |
| 164 | (if (not (or (symbol? k) (eqv? k #t))) |
| 165 | (scm-error "with-throw-handler" 'wrong-type-arg |
| 166 | "Wrong type argument in position ~a: ~a" |
| 167 | (list 1 k) (list k))) |
| 168 | (with-fluids ((%exception-handler |
| 169 | (custom-throw-handler #f k pre-unwind-handler))) |
| 170 | (thunk)))) |
| 171 | |
| 172 | (set! throw |
| 173 | (lambda (key . args) |
| 174 | "Invoke the catch form matching @var{key}, passing @var{args} to the |
| 175 | @var{handler}. |
| 176 | |
| 177 | @var{key} is a symbol. It will match catches of the same symbol or of @code{#t}. |
| 178 | |
| 179 | If there is no handler at all, Guile prints an error and then exits." |
| 180 | (if (not (symbol? key)) |
| 181 | ((fluid-ref %exception-handler) 'wrong-type-arg "throw" |
| 182 | "Wrong type argument in position ~a: ~a" (list 1 key) (list key)) |
| 183 | (apply (fluid-ref %exception-handler) key args))))) |
| 184 | |
| 185 | |
| 186 | \f |
| 187 | |
| 188 | ;;; {R4RS compliance} |
| 189 | ;;; |
| 190 | |
| 191 | (primitive-load-path "ice-9/r4rs") |
| 192 | |
| 193 | \f |
| 194 | |
| 195 | ;;; {Simple Debugging Tools} |
| 196 | ;;; |
| 197 | |
| 198 | ;; peek takes any number of arguments, writes them to the |
| 199 | ;; current ouput port, and returns the last argument. |
| 200 | ;; It is handy to wrap around an expression to look at |
| 201 | ;; a value each time is evaluated, e.g.: |
| 202 | ;; |
| 203 | ;; (+ 10 (troublesome-fn)) |
| 204 | ;; => (+ 10 (pk 'troublesome-fn-returned (troublesome-fn))) |
| 205 | ;; |
| 206 | |
| 207 | (define (peek . stuff) |
| 208 | (newline) |
| 209 | (display ";;; ") |
| 210 | (write stuff) |
| 211 | (newline) |
| 212 | (car (last-pair stuff))) |
| 213 | |
| 214 | (define pk peek) |
| 215 | |
| 216 | ;; Temporary definition; replaced later. |
| 217 | (define current-warning-port current-error-port) |
| 218 | |
| 219 | (define (warn . stuff) |
| 220 | (with-output-to-port (current-warning-port) |
| 221 | (lambda () |
| 222 | (newline) |
| 223 | (display ";;; WARNING ") |
| 224 | (display stuff) |
| 225 | (newline) |
| 226 | (car (last-pair stuff))))) |
| 227 | |
| 228 | \f |
| 229 | |
| 230 | ;;; {Features} |
| 231 | ;;; |
| 232 | |
| 233 | (define (provide sym) |
| 234 | (if (not (memq sym *features*)) |
| 235 | (set! *features* (cons sym *features*)))) |
| 236 | |
| 237 | ;; Return #t iff FEATURE is available to this Guile interpreter. In SLIB, |
| 238 | ;; provided? also checks to see if the module is available. We should do that |
| 239 | ;; too, but don't. |
| 240 | |
| 241 | (define (provided? feature) |
| 242 | (and (memq feature *features*) #t)) |
| 243 | |
| 244 | \f |
| 245 | |
| 246 | ;;; {Structs} |
| 247 | ;;; |
| 248 | |
| 249 | (define (make-struct/no-tail vtable . args) |
| 250 | (apply make-struct vtable 0 args)) |
| 251 | |
| 252 | \f |
| 253 | |
| 254 | ;;; Boot versions of `map' and `for-each', enough to get the expander |
| 255 | ;;; running. |
| 256 | ;;; |
| 257 | (define map |
| 258 | (case-lambda |
| 259 | ((f l) |
| 260 | (let map1 ((l l)) |
| 261 | (if (null? l) |
| 262 | '() |
| 263 | (cons (f (car l)) (map1 (cdr l)))))) |
| 264 | ((f l1 l2) |
| 265 | (let map2 ((l1 l1) (l2 l2)) |
| 266 | (if (null? l1) |
| 267 | '() |
| 268 | (cons (f (car l1) (car l2)) |
| 269 | (map2 (cdr l1) (cdr l2)))))) |
| 270 | ((f l1 . rest) |
| 271 | (let lp ((l1 l1) (rest rest)) |
| 272 | (if (null? l1) |
| 273 | '() |
| 274 | (cons (apply f (car l1) (map car rest)) |
| 275 | (lp (cdr l1) (map cdr rest)))))))) |
| 276 | |
| 277 | (define for-each |
| 278 | (case-lambda |
| 279 | ((f l) |
| 280 | (let for-each1 ((l l)) |
| 281 | (if (pair? l) |
| 282 | (begin |
| 283 | (f (car l)) |
| 284 | (for-each1 (cdr l)))))) |
| 285 | ((f l1 l2) |
| 286 | (let for-each2 ((l1 l1) (l2 l2)) |
| 287 | (if (pair? l1) |
| 288 | (begin |
| 289 | (f (car l1) (car l2)) |
| 290 | (for-each2 (cdr l1) (cdr l2)))))) |
| 291 | ((f l1 . rest) |
| 292 | (let lp ((l1 l1) (rest rest)) |
| 293 | (if (pair? l1) |
| 294 | (begin |
| 295 | (apply f (car l1) (map car rest)) |
| 296 | (lp (cdr l1) (map cdr rest)))))))) |
| 297 | |
| 298 | ;;; {and-map and or-map} |
| 299 | ;;; |
| 300 | ;;; (and-map fn lst) is like (and (fn (car lst)) (fn (cadr lst)) (fn...) ...) |
| 301 | ;;; (or-map fn lst) is like (or (fn (car lst)) (fn (cadr lst)) (fn...) ...) |
| 302 | ;;; |
| 303 | |
| 304 | ;; and-map f l |
| 305 | ;; |
| 306 | ;; Apply f to successive elements of l until exhaustion or f returns #f. |
| 307 | ;; If returning early, return #f. Otherwise, return the last value returned |
| 308 | ;; by f. If f has never been called because l is empty, return #t. |
| 309 | ;; |
| 310 | (define (and-map f lst) |
| 311 | (let loop ((result #t) |
| 312 | (l lst)) |
| 313 | (and result |
| 314 | (or (and (null? l) |
| 315 | result) |
| 316 | (loop (f (car l)) (cdr l)))))) |
| 317 | |
| 318 | ;; or-map f l |
| 319 | ;; |
| 320 | ;; Apply f to successive elements of l until exhaustion or while f returns #f. |
| 321 | ;; If returning early, return the return value of f. |
| 322 | ;; |
| 323 | (define (or-map f lst) |
| 324 | (let loop ((result #f) |
| 325 | (l lst)) |
| 326 | (or result |
| 327 | (and (not (null? l)) |
| 328 | (loop (f (car l)) (cdr l)))))) |
| 329 | |
| 330 | \f |
| 331 | |
| 332 | ;; let format alias simple-format until the more complete version is loaded |
| 333 | |
| 334 | (define format simple-format) |
| 335 | |
| 336 | ;; this is scheme wrapping the C code so the final pred call is a tail call, |
| 337 | ;; per SRFI-13 spec |
| 338 | (define string-any |
| 339 | (lambda* (char_pred s #:optional (start 0) (end (string-length s))) |
| 340 | (if (and (procedure? char_pred) |
| 341 | (> end start) |
| 342 | (<= end (string-length s))) ;; let c-code handle range error |
| 343 | (or (string-any-c-code char_pred s start (1- end)) |
| 344 | (char_pred (string-ref s (1- end)))) |
| 345 | (string-any-c-code char_pred s start end)))) |
| 346 | |
| 347 | ;; this is scheme wrapping the C code so the final pred call is a tail call, |
| 348 | ;; per SRFI-13 spec |
| 349 | (define string-every |
| 350 | (lambda* (char_pred s #:optional (start 0) (end (string-length s))) |
| 351 | (if (and (procedure? char_pred) |
| 352 | (> end start) |
| 353 | (<= end (string-length s))) ;; let c-code handle range error |
| 354 | (and (string-every-c-code char_pred s start (1- end)) |
| 355 | (char_pred (string-ref s (1- end)))) |
| 356 | (string-every-c-code char_pred s start end)))) |
| 357 | |
| 358 | ;; A variant of string-fill! that we keep for compatability |
| 359 | ;; |
| 360 | (define (substring-fill! str start end fill) |
| 361 | (string-fill! str fill start end)) |
| 362 | |
| 363 | \f |
| 364 | |
| 365 | ;; Define a minimal stub of the module API for psyntax, before modules |
| 366 | ;; have booted. |
| 367 | (define (module-name x) |
| 368 | '(guile)) |
| 369 | (define (module-add! module sym var) |
| 370 | (hashq-set! (%get-pre-modules-obarray) sym var)) |
| 371 | (define (module-define! module sym val) |
| 372 | (let ((v (hashq-ref (%get-pre-modules-obarray) sym))) |
| 373 | (if v |
| 374 | (variable-set! v val) |
| 375 | (module-add! (current-module) sym (make-variable val))))) |
| 376 | (define (module-ref module sym) |
| 377 | (let ((v (module-variable module sym))) |
| 378 | (if v (variable-ref v) (error "badness!" (pk module) (pk sym))))) |
| 379 | (define (resolve-module . args) |
| 380 | #f) |
| 381 | |
| 382 | ;; API provided by psyntax |
| 383 | (define syntax-violation #f) |
| 384 | (define datum->syntax #f) |
| 385 | (define syntax->datum #f) |
| 386 | (define syntax-source #f) |
| 387 | (define identifier? #f) |
| 388 | (define generate-temporaries #f) |
| 389 | (define bound-identifier=? #f) |
| 390 | (define free-identifier=? #f) |
| 391 | |
| 392 | ;; $sc-dispatch is an implementation detail of psyntax. It is used by |
| 393 | ;; expanded macros, to dispatch an input against a set of patterns. |
| 394 | (define $sc-dispatch #f) |
| 395 | |
| 396 | ;; Load it up! |
| 397 | (primitive-load-path "ice-9/psyntax-pp") |
| 398 | ;; The binding for `macroexpand' has now been overridden, making psyntax the |
| 399 | ;; expander now. |
| 400 | |
| 401 | (define-syntax and |
| 402 | (syntax-rules () |
| 403 | ((_) #t) |
| 404 | ((_ x) x) |
| 405 | ((_ x y ...) (if x (and y ...) #f)))) |
| 406 | |
| 407 | (define-syntax or |
| 408 | (syntax-rules () |
| 409 | ((_) #f) |
| 410 | ((_ x) x) |
| 411 | ((_ x y ...) (let ((t x)) (if t t (or y ...)))))) |
| 412 | |
| 413 | ;; The "maybe-more" bits are something of a hack, so that we can support |
| 414 | ;; SRFI-61. Rewrites into a standalone syntax-case macro would be |
| 415 | ;; appreciated. |
| 416 | (define-syntax cond |
| 417 | (syntax-rules (=> else) |
| 418 | ((_ "maybe-more" test consequent) |
| 419 | (if test consequent)) |
| 420 | |
| 421 | ((_ "maybe-more" test consequent clause ...) |
| 422 | (if test consequent (cond clause ...))) |
| 423 | |
| 424 | ((_ (else else1 else2 ...)) |
| 425 | (begin else1 else2 ...)) |
| 426 | |
| 427 | ((_ (test => receiver) more-clause ...) |
| 428 | (let ((t test)) |
| 429 | (cond "maybe-more" t (receiver t) more-clause ...))) |
| 430 | |
| 431 | ((_ (generator guard => receiver) more-clause ...) |
| 432 | (call-with-values (lambda () generator) |
| 433 | (lambda t |
| 434 | (cond "maybe-more" |
| 435 | (apply guard t) (apply receiver t) more-clause ...)))) |
| 436 | |
| 437 | ((_ (test => receiver ...) more-clause ...) |
| 438 | (syntax-violation 'cond "wrong number of receiver expressions" |
| 439 | '(test => receiver ...))) |
| 440 | ((_ (generator guard => receiver ...) more-clause ...) |
| 441 | (syntax-violation 'cond "wrong number of receiver expressions" |
| 442 | '(generator guard => receiver ...))) |
| 443 | |
| 444 | ((_ (test) more-clause ...) |
| 445 | (let ((t test)) |
| 446 | (cond "maybe-more" t t more-clause ...))) |
| 447 | |
| 448 | ((_ (test body1 body2 ...) more-clause ...) |
| 449 | (cond "maybe-more" |
| 450 | test (begin body1 body2 ...) more-clause ...)))) |
| 451 | |
| 452 | (define-syntax case |
| 453 | (syntax-rules (else) |
| 454 | ((case (key ...) |
| 455 | clauses ...) |
| 456 | (let ((atom-key (key ...))) |
| 457 | (case atom-key clauses ...))) |
| 458 | ((case key |
| 459 | (else result1 result2 ...)) |
| 460 | (begin result1 result2 ...)) |
| 461 | ((case key |
| 462 | ((atoms ...) result1 result2 ...)) |
| 463 | (if (memv key '(atoms ...)) |
| 464 | (begin result1 result2 ...))) |
| 465 | ((case key |
| 466 | ((atoms ...) result1 result2 ...) |
| 467 | clause clauses ...) |
| 468 | (if (memv key '(atoms ...)) |
| 469 | (begin result1 result2 ...) |
| 470 | (case key clause clauses ...))))) |
| 471 | |
| 472 | (define-syntax do |
| 473 | (syntax-rules () |
| 474 | ((do ((var init step ...) ...) |
| 475 | (test expr ...) |
| 476 | command ...) |
| 477 | (letrec |
| 478 | ((loop |
| 479 | (lambda (var ...) |
| 480 | (if test |
| 481 | (begin |
| 482 | (if #f #f) |
| 483 | expr ...) |
| 484 | (begin |
| 485 | command |
| 486 | ... |
| 487 | (loop (do "step" var step ...) |
| 488 | ...)))))) |
| 489 | (loop init ...))) |
| 490 | ((do "step" x) |
| 491 | x) |
| 492 | ((do "step" x y) |
| 493 | y))) |
| 494 | |
| 495 | (define-syntax-rule (delay exp) |
| 496 | (make-promise (lambda () exp))) |
| 497 | |
| 498 | (include-from-path "ice-9/quasisyntax") |
| 499 | |
| 500 | (define-syntax current-source-location |
| 501 | (lambda (x) |
| 502 | (syntax-case x () |
| 503 | ((_) |
| 504 | (with-syntax ((s (datum->syntax x (syntax-source x)))) |
| 505 | #''s))))) |
| 506 | |
| 507 | (define-syntax-rule (define-once sym val) |
| 508 | (define sym |
| 509 | (if (module-locally-bound? (current-module) 'sym) sym val))) |
| 510 | |
| 511 | ;;; The real versions of `map' and `for-each', with cycle detection, and |
| 512 | ;;; that use reverse! instead of recursion in the case of `map'. |
| 513 | ;;; |
| 514 | (define map |
| 515 | (case-lambda |
| 516 | ((f l) |
| 517 | (let map1 ((hare l) (tortoise l) (move? #f) (out '())) |
| 518 | (if (pair? hare) |
| 519 | (if move? |
| 520 | (if (eq? tortoise hare) |
| 521 | (scm-error 'wrong-type-arg "map" "Circular list: ~S" |
| 522 | (list l) #f) |
| 523 | (map1 (cdr hare) (cdr tortoise) #f |
| 524 | (cons (f (car hare)) out))) |
| 525 | (map1 (cdr hare) tortoise #t |
| 526 | (cons (f (car hare)) out))) |
| 527 | (if (null? hare) |
| 528 | (reverse! out) |
| 529 | (scm-error 'wrong-type-arg "map" "Not a list: ~S" |
| 530 | (list l) #f))))) |
| 531 | |
| 532 | ((f l1 l2) |
| 533 | (let map2 ((h1 l1) (h2 l2) (t1 l1) (t2 l2) (move? #f) (out '())) |
| 534 | (cond |
| 535 | ((pair? h1) |
| 536 | (cond |
| 537 | ((not (pair? h2)) |
| 538 | (scm-error 'wrong-type-arg "map" |
| 539 | (if (list? h2) |
| 540 | "List of wrong length: ~S" |
| 541 | "Not a list: ~S") |
| 542 | (list l2) #f)) |
| 543 | ((not move?) |
| 544 | (map2 (cdr h1) (cdr h2) t1 t2 #t |
| 545 | (cons (f (car h1) (car h2)) out))) |
| 546 | ((eq? t1 h1) |
| 547 | (scm-error 'wrong-type-arg "map" "Circular list: ~S" |
| 548 | (list l1) #f)) |
| 549 | ((eq? t2 h2) |
| 550 | (scm-error 'wrong-type-arg "map" "Circular list: ~S" |
| 551 | (list l2) #f)) |
| 552 | (else |
| 553 | (map2 (cdr h1) (cdr h2) (cdr t1) (cdr t2) #f |
| 554 | (cons (f (car h1) (car h2)) out))))) |
| 555 | |
| 556 | ((and (null? h1) (null? h2)) |
| 557 | (reverse! out)) |
| 558 | |
| 559 | ((null? h1) |
| 560 | (scm-error 'wrong-type-arg "map" |
| 561 | (if (list? h2) |
| 562 | "List of wrong length: ~S" |
| 563 | "Not a list: ~S") |
| 564 | (list l2) #f)) |
| 565 | (else |
| 566 | (scm-error 'wrong-type-arg "map" |
| 567 | "Not a list: ~S" |
| 568 | (list l1) #f))))) |
| 569 | |
| 570 | ((f l1 . rest) |
| 571 | (let ((len (length l1))) |
| 572 | (let mapn ((rest rest)) |
| 573 | (or (null? rest) |
| 574 | (if (= (length (car rest)) len) |
| 575 | (mapn (cdr rest)) |
| 576 | (scm-error 'wrong-type-arg "map" "List of wrong length: ~S" |
| 577 | (list (car rest)) #f))))) |
| 578 | (let mapn ((l1 l1) (rest rest) (out '())) |
| 579 | (if (null? l1) |
| 580 | (reverse! out) |
| 581 | (mapn (cdr l1) (map cdr rest) |
| 582 | (cons (apply f (car l1) (map car rest)) out))))))) |
| 583 | |
| 584 | (define map-in-order map) |
| 585 | |
| 586 | (define for-each |
| 587 | (case-lambda |
| 588 | ((f l) |
| 589 | (let for-each1 ((hare l) (tortoise l) (move? #f)) |
| 590 | (if (pair? hare) |
| 591 | (if move? |
| 592 | (if (eq? tortoise hare) |
| 593 | (scm-error 'wrong-type-arg "for-each" "Circular list: ~S" |
| 594 | (list l) #f) |
| 595 | (begin |
| 596 | (f (car hare)) |
| 597 | (for-each1 (cdr hare) (cdr tortoise) #f))) |
| 598 | (begin |
| 599 | (f (car hare)) |
| 600 | (for-each1 (cdr hare) tortoise #t))) |
| 601 | |
| 602 | (if (not (null? hare)) |
| 603 | (scm-error 'wrong-type-arg "for-each" "Not a list: ~S" |
| 604 | (list l) #f))))) |
| 605 | |
| 606 | ((f l1 l2) |
| 607 | (let for-each2 ((h1 l1) (h2 l2) (t1 l1) (t2 l2) (move? #f)) |
| 608 | (cond |
| 609 | ((and (pair? h1) (pair? h2)) |
| 610 | (cond |
| 611 | ((not move?) |
| 612 | (f (car h1) (car h2)) |
| 613 | (for-each2 (cdr h1) (cdr h2) t1 t2 #t)) |
| 614 | ((eq? t1 h1) |
| 615 | (scm-error 'wrong-type-arg "for-each" "Circular list: ~S" |
| 616 | (list l1) #f)) |
| 617 | ((eq? t2 h2) |
| 618 | (scm-error 'wrong-type-arg "for-each" "Circular list: ~S" |
| 619 | (list l2) #f)) |
| 620 | (else |
| 621 | (f (car h1) (car h2)) |
| 622 | (for-each2 (cdr h1) (cdr h2) (cdr t1) (cdr t2) #f)))) |
| 623 | |
| 624 | ((if (null? h1) |
| 625 | (or (null? h2) (pair? h2)) |
| 626 | (and (pair? h1) (null? h2))) |
| 627 | (if #f #f)) |
| 628 | |
| 629 | ((list? h1) |
| 630 | (scm-error 'wrong-type-arg "for-each" "Unexpected tail: ~S" |
| 631 | (list h2) #f)) |
| 632 | (else |
| 633 | (scm-error 'wrong-type-arg "for-each" "Unexpected tail: ~S" |
| 634 | (list h1) #f))))) |
| 635 | |
| 636 | ((f l1 . rest) |
| 637 | (let ((len (length l1))) |
| 638 | (let for-eachn ((rest rest)) |
| 639 | (or (null? rest) |
| 640 | (if (= (length (car rest)) len) |
| 641 | (for-eachn (cdr rest)) |
| 642 | (scm-error 'wrong-type-arg "for-each" "List of wrong length: ~S" |
| 643 | (list (car rest)) #f))))) |
| 644 | |
| 645 | (let for-eachn ((l1 l1) (rest rest)) |
| 646 | (if (pair? l1) |
| 647 | (begin |
| 648 | (apply f (car l1) (map car rest)) |
| 649 | (for-eachn (cdr l1) (map cdr rest)))))))) |
| 650 | |
| 651 | |
| 652 | \f |
| 653 | |
| 654 | ;;; |
| 655 | ;;; Extensible exception printing. |
| 656 | ;;; |
| 657 | |
| 658 | (define set-exception-printer! #f) |
| 659 | ;; There is already a definition of print-exception from backtrace.c |
| 660 | ;; that we will override. |
| 661 | |
| 662 | (let ((exception-printers '())) |
| 663 | (define (print-location frame port) |
| 664 | (let ((source (and=> frame frame-source))) |
| 665 | ;; source := (addr . (filename . (line . column))) |
| 666 | (if source |
| 667 | (let ((filename (or (cadr source) "<unnamed port>")) |
| 668 | (line (caddr source)) |
| 669 | (col (cdddr source))) |
| 670 | (format port "~a:~a:~a: " filename (1+ line) col)) |
| 671 | (format port "ERROR: ")))) |
| 672 | |
| 673 | (set! set-exception-printer! |
| 674 | (lambda (key proc) |
| 675 | (set! exception-printers (acons key proc exception-printers)))) |
| 676 | |
| 677 | (set! print-exception |
| 678 | (lambda (port frame key args) |
| 679 | (define (default-printer) |
| 680 | (format port "Throw to key `~a' with args `~s'." key args)) |
| 681 | |
| 682 | (if frame |
| 683 | (let ((proc (frame-procedure frame))) |
| 684 | (print-location frame port) |
| 685 | (format port "In procedure ~a:\n" |
| 686 | (or (procedure-name proc) proc)))) |
| 687 | |
| 688 | (print-location frame port) |
| 689 | (catch #t |
| 690 | (lambda () |
| 691 | (let ((printer (assq-ref exception-printers key))) |
| 692 | (if printer |
| 693 | (printer port key args default-printer) |
| 694 | (default-printer)))) |
| 695 | (lambda (k . args) |
| 696 | (format port "Error while printing exception."))) |
| 697 | (newline port) |
| 698 | (force-output port)))) |
| 699 | |
| 700 | ;;; |
| 701 | ;;; Printers for those keys thrown by Guile. |
| 702 | ;;; |
| 703 | (let () |
| 704 | (define (scm-error-printer port key args default-printer) |
| 705 | ;; Abuse case-lambda as a pattern matcher, given that we don't have |
| 706 | ;; ice-9 match at this point. |
| 707 | (apply (case-lambda |
| 708 | ((subr msg args . rest) |
| 709 | (if subr |
| 710 | (format port "In procedure ~a: " subr)) |
| 711 | (apply format port msg (or args '()))) |
| 712 | (_ (default-printer))) |
| 713 | args)) |
| 714 | |
| 715 | (define (syntax-error-printer port key args default-printer) |
| 716 | (apply (case-lambda |
| 717 | ((who what where form subform . extra) |
| 718 | (format port "Syntax error:\n") |
| 719 | (if where |
| 720 | (let ((file (or (assq-ref where 'filename) "unknown file")) |
| 721 | (line (and=> (assq-ref where 'line) 1+)) |
| 722 | (col (assq-ref where 'column))) |
| 723 | (format port "~a:~a:~a: " file line col)) |
| 724 | (format port "unknown location: ")) |
| 725 | (if who |
| 726 | (format port "~a: " who)) |
| 727 | (format port "~a" what) |
| 728 | (if subform |
| 729 | (format port " in subform ~s of ~s" subform form) |
| 730 | (if form |
| 731 | (format port " in form ~s" form)))) |
| 732 | (_ (default-printer))) |
| 733 | args)) |
| 734 | |
| 735 | (set-exception-printer! 'goops-error scm-error-printer) |
| 736 | (set-exception-printer! 'host-not-found scm-error-printer) |
| 737 | (set-exception-printer! 'keyword-argument-error scm-error-printer) |
| 738 | (set-exception-printer! 'misc-error scm-error-printer) |
| 739 | (set-exception-printer! 'no-data scm-error-printer) |
| 740 | (set-exception-printer! 'no-recovery scm-error-printer) |
| 741 | (set-exception-printer! 'null-pointer-error scm-error-printer) |
| 742 | (set-exception-printer! 'out-of-range scm-error-printer) |
| 743 | (set-exception-printer! 'program-error scm-error-printer) |
| 744 | (set-exception-printer! 'read-error scm-error-printer) |
| 745 | (set-exception-printer! 'regular-expression-syntax scm-error-printer) |
| 746 | (set-exception-printer! 'signal scm-error-printer) |
| 747 | (set-exception-printer! 'stack-overflow scm-error-printer) |
| 748 | (set-exception-printer! 'system-error scm-error-printer) |
| 749 | (set-exception-printer! 'try-again scm-error-printer) |
| 750 | (set-exception-printer! 'unbound-variable scm-error-printer) |
| 751 | (set-exception-printer! 'wrong-number-of-args scm-error-printer) |
| 752 | (set-exception-printer! 'wrong-type-arg scm-error-printer) |
| 753 | |
| 754 | (set-exception-printer! 'syntax-error syntax-error-printer)) |
| 755 | |
| 756 | |
| 757 | \f |
| 758 | |
| 759 | ;;; {Defmacros} |
| 760 | ;;; |
| 761 | |
| 762 | (define-syntax define-macro |
| 763 | (lambda (x) |
| 764 | "Define a defmacro." |
| 765 | (syntax-case x () |
| 766 | ((_ (macro . args) doc body1 body ...) |
| 767 | (string? (syntax->datum #'doc)) |
| 768 | #'(define-macro macro doc (lambda args body1 body ...))) |
| 769 | ((_ (macro . args) body ...) |
| 770 | #'(define-macro macro #f (lambda args body ...))) |
| 771 | ((_ macro doc transformer) |
| 772 | (or (string? (syntax->datum #'doc)) |
| 773 | (not (syntax->datum #'doc))) |
| 774 | #'(define-syntax macro |
| 775 | (lambda (y) |
| 776 | doc |
| 777 | #((macro-type . defmacro) |
| 778 | (defmacro-args args)) |
| 779 | (syntax-case y () |
| 780 | ((_ . args) |
| 781 | (let ((v (syntax->datum #'args))) |
| 782 | (datum->syntax y (apply transformer v))))))))))) |
| 783 | |
| 784 | (define-syntax defmacro |
| 785 | (lambda (x) |
| 786 | "Define a defmacro, with the old lispy defun syntax." |
| 787 | (syntax-case x () |
| 788 | ((_ macro args doc body1 body ...) |
| 789 | (string? (syntax->datum #'doc)) |
| 790 | #'(define-macro macro doc (lambda args body1 body ...))) |
| 791 | ((_ macro args body ...) |
| 792 | #'(define-macro macro #f (lambda args body ...)))))) |
| 793 | |
| 794 | (provide 'defmacro) |
| 795 | |
| 796 | \f |
| 797 | |
| 798 | ;;; {Deprecation} |
| 799 | ;;; |
| 800 | |
| 801 | (define-syntax begin-deprecated |
| 802 | (lambda (x) |
| 803 | (syntax-case x () |
| 804 | ((_ form form* ...) |
| 805 | (if (include-deprecated-features) |
| 806 | #'(begin form form* ...) |
| 807 | #'(begin)))))) |
| 808 | |
| 809 | \f |
| 810 | |
| 811 | ;;; {Trivial Functions} |
| 812 | ;;; |
| 813 | |
| 814 | (define (identity x) x) |
| 815 | |
| 816 | (define (compose proc . rest) |
| 817 | "Compose PROC with the procedures in REST, such that the last one in |
| 818 | REST is applied first and PROC last, and return the resulting procedure. |
| 819 | The given procedures must have compatible arity." |
| 820 | (if (null? rest) |
| 821 | proc |
| 822 | (let ((g (apply compose rest))) |
| 823 | (lambda args |
| 824 | (call-with-values (lambda () (apply g args)) proc))))) |
| 825 | |
| 826 | (define (negate proc) |
| 827 | "Return a procedure with the same arity as PROC that returns the `not' |
| 828 | of PROC's result." |
| 829 | (lambda args |
| 830 | (not (apply proc args)))) |
| 831 | |
| 832 | (define (const value) |
| 833 | "Return a procedure that accepts any number of arguments and returns |
| 834 | VALUE." |
| 835 | (lambda _ |
| 836 | value)) |
| 837 | |
| 838 | (define (and=> value procedure) (and value (procedure value))) |
| 839 | (define call/cc call-with-current-continuation) |
| 840 | |
| 841 | (define-syntax-rule (false-if-exception expr) |
| 842 | (catch #t |
| 843 | (lambda () expr) |
| 844 | (lambda (k . args) #f))) |
| 845 | |
| 846 | \f |
| 847 | |
| 848 | ;;; {General Properties} |
| 849 | ;;; |
| 850 | |
| 851 | ;; Properties are a lispy way to associate random info with random objects. |
| 852 | ;; Traditionally properties are implemented as an alist or a plist actually |
| 853 | ;; pertaining to the object in question. |
| 854 | ;; |
| 855 | ;; These "object properties" have the advantage that they can be associated with |
| 856 | ;; any object, even if the object has no plist. Object properties are good when |
| 857 | ;; you are extending pre-existing objects in unexpected ways. They also present |
| 858 | ;; a pleasing, uniform procedure-with-setter interface. But if you have a data |
| 859 | ;; type that always has properties, it's often still best to store those |
| 860 | ;; properties within the object itself. |
| 861 | |
| 862 | (define (make-object-property) |
| 863 | (define-syntax-rule (with-mutex lock exp) |
| 864 | (dynamic-wind (lambda () (lock-mutex lock)) |
| 865 | (lambda () exp) |
| 866 | (lambda () (unlock-mutex lock)))) |
| 867 | (let ((prop (make-weak-key-hash-table)) |
| 868 | (lock (make-mutex))) |
| 869 | (make-procedure-with-setter |
| 870 | (lambda (obj) (with-mutex lock (hashq-ref prop obj))) |
| 871 | (lambda (obj val) (with-mutex lock (hashq-set! prop obj val)))))) |
| 872 | |
| 873 | |
| 874 | \f |
| 875 | |
| 876 | ;;; {Symbol Properties} |
| 877 | ;;; |
| 878 | |
| 879 | ;;; Symbol properties are something you see in old Lisp code. In most current |
| 880 | ;;; Guile code, symbols are not used as a data structure -- they are used as |
| 881 | ;;; keys into other data structures. |
| 882 | |
| 883 | (define (symbol-property sym prop) |
| 884 | (let ((pair (assoc prop (symbol-pref sym)))) |
| 885 | (and pair (cdr pair)))) |
| 886 | |
| 887 | (define (set-symbol-property! sym prop val) |
| 888 | (let ((pair (assoc prop (symbol-pref sym)))) |
| 889 | (if pair |
| 890 | (set-cdr! pair val) |
| 891 | (symbol-pset! sym (acons prop val (symbol-pref sym)))))) |
| 892 | |
| 893 | (define (symbol-property-remove! sym prop) |
| 894 | (let ((pair (assoc prop (symbol-pref sym)))) |
| 895 | (if pair |
| 896 | (symbol-pset! sym (delq! pair (symbol-pref sym)))))) |
| 897 | |
| 898 | \f |
| 899 | |
| 900 | ;;; {Arrays} |
| 901 | ;;; |
| 902 | |
| 903 | (define (array-shape a) |
| 904 | (map (lambda (ind) (if (number? ind) (list 0 (+ -1 ind)) ind)) |
| 905 | (array-dimensions a))) |
| 906 | |
| 907 | \f |
| 908 | |
| 909 | ;;; {Keywords} |
| 910 | ;;; |
| 911 | |
| 912 | ;;; It's much better if you can use lambda* / define*, of course. |
| 913 | |
| 914 | (define (kw-arg-ref args kw) |
| 915 | (let ((rem (member kw args))) |
| 916 | (and rem (pair? (cdr rem)) (cadr rem)))) |
| 917 | |
| 918 | \f |
| 919 | |
| 920 | ;;; {Structs} |
| 921 | ;;; |
| 922 | |
| 923 | (define (struct-layout s) |
| 924 | (struct-ref (struct-vtable s) vtable-index-layout)) |
| 925 | |
| 926 | \f |
| 927 | |
| 928 | ;;; {Records} |
| 929 | ;;; |
| 930 | |
| 931 | ;; Printing records: by default, records are printed as |
| 932 | ;; |
| 933 | ;; #<type-name field1: val1 field2: val2 ...> |
| 934 | ;; |
| 935 | ;; You can change that by giving a custom printing function to |
| 936 | ;; MAKE-RECORD-TYPE (after the list of field symbols). This function |
| 937 | ;; will be called like |
| 938 | ;; |
| 939 | ;; (<printer> object port) |
| 940 | ;; |
| 941 | ;; It should print OBJECT to PORT. |
| 942 | |
| 943 | (define (inherit-print-state old-port new-port) |
| 944 | (if (get-print-state old-port) |
| 945 | (port-with-print-state new-port (get-print-state old-port)) |
| 946 | new-port)) |
| 947 | |
| 948 | ;; 0: type-name, 1: fields, 2: constructor |
| 949 | (define record-type-vtable |
| 950 | ;; FIXME: This should just call make-vtable, not make-vtable-vtable; but for |
| 951 | ;; that we need to expose the bare vtable-vtable to Scheme. |
| 952 | (make-vtable-vtable "prprpw" 0 |
| 953 | (lambda (s p) |
| 954 | (cond ((eq? s record-type-vtable) |
| 955 | (display "#<record-type-vtable>" p)) |
| 956 | (else |
| 957 | (display "#<record-type " p) |
| 958 | (display (record-type-name s) p) |
| 959 | (display ">" p)))))) |
| 960 | |
| 961 | (define (record-type? obj) |
| 962 | (and (struct? obj) (eq? record-type-vtable (struct-vtable obj)))) |
| 963 | |
| 964 | (define* (make-record-type type-name fields #:optional printer) |
| 965 | ;; Pre-generate constructors for nfields < 20. |
| 966 | (define-syntax make-constructor |
| 967 | (lambda (x) |
| 968 | (define *max-static-argument-count* 20) |
| 969 | (define (make-formals n) |
| 970 | (let lp ((i 0)) |
| 971 | (if (< i n) |
| 972 | (cons (datum->syntax |
| 973 | x |
| 974 | (string->symbol |
| 975 | (string (integer->char (+ (char->integer #\a) i))))) |
| 976 | (lp (1+ i))) |
| 977 | '()))) |
| 978 | (syntax-case x () |
| 979 | ((_ rtd exp) (not (identifier? #'exp)) |
| 980 | #'(let ((n exp)) |
| 981 | (make-constructor rtd n))) |
| 982 | ((_ rtd nfields) |
| 983 | #`(case nfields |
| 984 | #,@(let lp ((n 0)) |
| 985 | (if (< n *max-static-argument-count*) |
| 986 | (cons (with-syntax (((formal ...) (make-formals n)) |
| 987 | (n n)) |
| 988 | #'((n) |
| 989 | (lambda (formal ...) |
| 990 | (make-struct rtd 0 formal ...)))) |
| 991 | (lp (1+ n))) |
| 992 | '())) |
| 993 | (else |
| 994 | (lambda args |
| 995 | (if (= (length args) nfields) |
| 996 | (apply make-struct rtd 0 args) |
| 997 | (scm-error 'wrong-number-of-args |
| 998 | (format #f "make-~a" type-name) |
| 999 | "Wrong number of arguments" '() #f))))))))) |
| 1000 | |
| 1001 | (define (default-record-printer s p) |
| 1002 | (display "#<" p) |
| 1003 | (display (record-type-name (record-type-descriptor s)) p) |
| 1004 | (let loop ((fields (record-type-fields (record-type-descriptor s))) |
| 1005 | (off 0)) |
| 1006 | (cond |
| 1007 | ((not (null? fields)) |
| 1008 | (display " " p) |
| 1009 | (display (car fields) p) |
| 1010 | (display ": " p) |
| 1011 | (display (struct-ref s off) p) |
| 1012 | (loop (cdr fields) (+ 1 off))))) |
| 1013 | (display ">" p)) |
| 1014 | |
| 1015 | (let ((rtd (make-struct record-type-vtable 0 |
| 1016 | (make-struct-layout |
| 1017 | (apply string-append |
| 1018 | (map (lambda (f) "pw") fields))) |
| 1019 | (or printer default-record-printer) |
| 1020 | type-name |
| 1021 | (copy-tree fields)))) |
| 1022 | (struct-set! rtd (+ vtable-offset-user 2) |
| 1023 | (make-constructor rtd (length fields))) |
| 1024 | ;; Temporary solution: Associate a name to the record type descriptor |
| 1025 | ;; so that the object system can create a wrapper class for it. |
| 1026 | (set-struct-vtable-name! rtd (if (symbol? type-name) |
| 1027 | type-name |
| 1028 | (string->symbol type-name))) |
| 1029 | rtd)) |
| 1030 | |
| 1031 | (define (record-type-name obj) |
| 1032 | (if (record-type? obj) |
| 1033 | (struct-ref obj vtable-offset-user) |
| 1034 | (error 'not-a-record-type obj))) |
| 1035 | |
| 1036 | (define (record-type-fields obj) |
| 1037 | (if (record-type? obj) |
| 1038 | (struct-ref obj (+ 1 vtable-offset-user)) |
| 1039 | (error 'not-a-record-type obj))) |
| 1040 | |
| 1041 | (define* (record-constructor rtd #:optional field-names) |
| 1042 | (if (not field-names) |
| 1043 | (struct-ref rtd (+ 2 vtable-offset-user)) |
| 1044 | (primitive-eval |
| 1045 | `(lambda ,field-names |
| 1046 | (make-struct ',rtd 0 ,@(map (lambda (f) |
| 1047 | (if (memq f field-names) |
| 1048 | f |
| 1049 | #f)) |
| 1050 | (record-type-fields rtd))))))) |
| 1051 | |
| 1052 | (define (record-predicate rtd) |
| 1053 | (lambda (obj) (and (struct? obj) (eq? rtd (struct-vtable obj))))) |
| 1054 | |
| 1055 | (define (%record-type-error rtd obj) ;; private helper |
| 1056 | (or (eq? rtd (record-type-descriptor obj)) |
| 1057 | (scm-error 'wrong-type-arg "%record-type-check" |
| 1058 | "Wrong type record (want `~S'): ~S" |
| 1059 | (list (record-type-name rtd) obj) |
| 1060 | #f))) |
| 1061 | |
| 1062 | (define (record-accessor rtd field-name) |
| 1063 | (let ((pos (list-index (record-type-fields rtd) field-name))) |
| 1064 | (if (not pos) |
| 1065 | (error 'no-such-field field-name)) |
| 1066 | (lambda (obj) |
| 1067 | (if (eq? (struct-vtable obj) rtd) |
| 1068 | (struct-ref obj pos) |
| 1069 | (%record-type-error rtd obj))))) |
| 1070 | |
| 1071 | (define (record-modifier rtd field-name) |
| 1072 | (let ((pos (list-index (record-type-fields rtd) field-name))) |
| 1073 | (if (not pos) |
| 1074 | (error 'no-such-field field-name)) |
| 1075 | (lambda (obj val) |
| 1076 | (if (eq? (struct-vtable obj) rtd) |
| 1077 | (struct-set! obj pos val) |
| 1078 | (%record-type-error rtd obj))))) |
| 1079 | |
| 1080 | (define (record? obj) |
| 1081 | (and (struct? obj) (record-type? (struct-vtable obj)))) |
| 1082 | |
| 1083 | (define (record-type-descriptor obj) |
| 1084 | (if (struct? obj) |
| 1085 | (struct-vtable obj) |
| 1086 | (error 'not-a-record obj))) |
| 1087 | |
| 1088 | (provide 'record) |
| 1089 | |
| 1090 | \f |
| 1091 | |
| 1092 | ;;; {Booleans} |
| 1093 | ;;; |
| 1094 | |
| 1095 | (define (->bool x) (not (not x))) |
| 1096 | |
| 1097 | \f |
| 1098 | |
| 1099 | ;;; {Symbols} |
| 1100 | ;;; |
| 1101 | |
| 1102 | (define (symbol-append . args) |
| 1103 | (string->symbol (apply string-append (map symbol->string args)))) |
| 1104 | |
| 1105 | (define (list->symbol . args) |
| 1106 | (string->symbol (apply list->string args))) |
| 1107 | |
| 1108 | (define (symbol . args) |
| 1109 | (string->symbol (apply string args))) |
| 1110 | |
| 1111 | \f |
| 1112 | |
| 1113 | ;;; {Lists} |
| 1114 | ;;; |
| 1115 | |
| 1116 | (define (list-index l k) |
| 1117 | (let loop ((n 0) |
| 1118 | (l l)) |
| 1119 | (and (not (null? l)) |
| 1120 | (if (eq? (car l) k) |
| 1121 | n |
| 1122 | (loop (+ n 1) (cdr l)))))) |
| 1123 | |
| 1124 | \f |
| 1125 | |
| 1126 | ;; Load `posix.scm' even when not (provided? 'posix) so that we get the |
| 1127 | ;; `stat' accessors. |
| 1128 | (primitive-load-path "ice-9/posix") |
| 1129 | |
| 1130 | (if (provided? 'socket) |
| 1131 | (primitive-load-path "ice-9/networking")) |
| 1132 | |
| 1133 | ;; For reference, Emacs file-exists-p uses stat in this same way. |
| 1134 | (define file-exists? |
| 1135 | (if (provided? 'posix) |
| 1136 | (lambda (str) |
| 1137 | (->bool (stat str #f))) |
| 1138 | (lambda (str) |
| 1139 | (let ((port (catch 'system-error (lambda () (open-file str OPEN_READ)) |
| 1140 | (lambda args #f)))) |
| 1141 | (if port (begin (close-port port) #t) |
| 1142 | #f))))) |
| 1143 | |
| 1144 | (define file-is-directory? |
| 1145 | (if (provided? 'posix) |
| 1146 | (lambda (str) |
| 1147 | (eq? (stat:type (stat str)) 'directory)) |
| 1148 | (lambda (str) |
| 1149 | (let ((port (catch 'system-error |
| 1150 | (lambda () (open-file (string-append str "/.") |
| 1151 | OPEN_READ)) |
| 1152 | (lambda args #f)))) |
| 1153 | (if port (begin (close-port port) #t) |
| 1154 | #f))))) |
| 1155 | |
| 1156 | (define (system-error-errno args) |
| 1157 | (if (eq? (car args) 'system-error) |
| 1158 | (car (list-ref args 4)) |
| 1159 | #f)) |
| 1160 | |
| 1161 | \f |
| 1162 | |
| 1163 | ;;; {Error Handling} |
| 1164 | ;;; |
| 1165 | |
| 1166 | (define error |
| 1167 | (case-lambda |
| 1168 | (() |
| 1169 | (scm-error 'misc-error #f "?" #f #f)) |
| 1170 | ((message . args) |
| 1171 | (let ((msg (string-join (cons "~A" (make-list (length args) "~S"))))) |
| 1172 | (scm-error 'misc-error #f msg (cons message args) #f))))) |
| 1173 | |
| 1174 | \f |
| 1175 | |
| 1176 | ;;; {Time Structures} |
| 1177 | ;;; |
| 1178 | |
| 1179 | (define (tm:sec obj) (vector-ref obj 0)) |
| 1180 | (define (tm:min obj) (vector-ref obj 1)) |
| 1181 | (define (tm:hour obj) (vector-ref obj 2)) |
| 1182 | (define (tm:mday obj) (vector-ref obj 3)) |
| 1183 | (define (tm:mon obj) (vector-ref obj 4)) |
| 1184 | (define (tm:year obj) (vector-ref obj 5)) |
| 1185 | (define (tm:wday obj) (vector-ref obj 6)) |
| 1186 | (define (tm:yday obj) (vector-ref obj 7)) |
| 1187 | (define (tm:isdst obj) (vector-ref obj 8)) |
| 1188 | (define (tm:gmtoff obj) (vector-ref obj 9)) |
| 1189 | (define (tm:zone obj) (vector-ref obj 10)) |
| 1190 | |
| 1191 | (define (set-tm:sec obj val) (vector-set! obj 0 val)) |
| 1192 | (define (set-tm:min obj val) (vector-set! obj 1 val)) |
| 1193 | (define (set-tm:hour obj val) (vector-set! obj 2 val)) |
| 1194 | (define (set-tm:mday obj val) (vector-set! obj 3 val)) |
| 1195 | (define (set-tm:mon obj val) (vector-set! obj 4 val)) |
| 1196 | (define (set-tm:year obj val) (vector-set! obj 5 val)) |
| 1197 | (define (set-tm:wday obj val) (vector-set! obj 6 val)) |
| 1198 | (define (set-tm:yday obj val) (vector-set! obj 7 val)) |
| 1199 | (define (set-tm:isdst obj val) (vector-set! obj 8 val)) |
| 1200 | (define (set-tm:gmtoff obj val) (vector-set! obj 9 val)) |
| 1201 | (define (set-tm:zone obj val) (vector-set! obj 10 val)) |
| 1202 | |
| 1203 | (define (tms:clock obj) (vector-ref obj 0)) |
| 1204 | (define (tms:utime obj) (vector-ref obj 1)) |
| 1205 | (define (tms:stime obj) (vector-ref obj 2)) |
| 1206 | (define (tms:cutime obj) (vector-ref obj 3)) |
| 1207 | (define (tms:cstime obj) (vector-ref obj 4)) |
| 1208 | |
| 1209 | \f |
| 1210 | |
| 1211 | ;;; {File Descriptors and Ports} |
| 1212 | ;;; |
| 1213 | |
| 1214 | (define file-position ftell) |
| 1215 | (define* (file-set-position port offset #:optional (whence SEEK_SET)) |
| 1216 | (seek port offset whence)) |
| 1217 | |
| 1218 | (define (move->fdes fd/port fd) |
| 1219 | (cond ((integer? fd/port) |
| 1220 | (dup->fdes fd/port fd) |
| 1221 | (close fd/port) |
| 1222 | fd) |
| 1223 | (else |
| 1224 | (primitive-move->fdes fd/port fd) |
| 1225 | (set-port-revealed! fd/port 1) |
| 1226 | fd/port))) |
| 1227 | |
| 1228 | (define (release-port-handle port) |
| 1229 | (let ((revealed (port-revealed port))) |
| 1230 | (if (> revealed 0) |
| 1231 | (set-port-revealed! port (- revealed 1))))) |
| 1232 | |
| 1233 | (define dup->port |
| 1234 | (case-lambda |
| 1235 | ((port/fd mode) |
| 1236 | (fdopen (dup->fdes port/fd) mode)) |
| 1237 | ((port/fd mode new-fd) |
| 1238 | (let ((port (fdopen (dup->fdes port/fd new-fd) mode))) |
| 1239 | (set-port-revealed! port 1) |
| 1240 | port)))) |
| 1241 | |
| 1242 | (define dup->inport |
| 1243 | (case-lambda |
| 1244 | ((port/fd) |
| 1245 | (dup->port port/fd "r")) |
| 1246 | ((port/fd new-fd) |
| 1247 | (dup->port port/fd "r" new-fd)))) |
| 1248 | |
| 1249 | (define dup->outport |
| 1250 | (case-lambda |
| 1251 | ((port/fd) |
| 1252 | (dup->port port/fd "w")) |
| 1253 | ((port/fd new-fd) |
| 1254 | (dup->port port/fd "w" new-fd)))) |
| 1255 | |
| 1256 | (define dup |
| 1257 | (case-lambda |
| 1258 | ((port/fd) |
| 1259 | (if (integer? port/fd) |
| 1260 | (dup->fdes port/fd) |
| 1261 | (dup->port port/fd (port-mode port/fd)))) |
| 1262 | ((port/fd new-fd) |
| 1263 | (if (integer? port/fd) |
| 1264 | (dup->fdes port/fd new-fd) |
| 1265 | (dup->port port/fd (port-mode port/fd) new-fd))))) |
| 1266 | |
| 1267 | (define (duplicate-port port modes) |
| 1268 | (dup->port port modes)) |
| 1269 | |
| 1270 | (define (fdes->inport fdes) |
| 1271 | (let loop ((rest-ports (fdes->ports fdes))) |
| 1272 | (cond ((null? rest-ports) |
| 1273 | (let ((result (fdopen fdes "r"))) |
| 1274 | (set-port-revealed! result 1) |
| 1275 | result)) |
| 1276 | ((input-port? (car rest-ports)) |
| 1277 | (set-port-revealed! (car rest-ports) |
| 1278 | (+ (port-revealed (car rest-ports)) 1)) |
| 1279 | (car rest-ports)) |
| 1280 | (else |
| 1281 | (loop (cdr rest-ports)))))) |
| 1282 | |
| 1283 | (define (fdes->outport fdes) |
| 1284 | (let loop ((rest-ports (fdes->ports fdes))) |
| 1285 | (cond ((null? rest-ports) |
| 1286 | (let ((result (fdopen fdes "w"))) |
| 1287 | (set-port-revealed! result 1) |
| 1288 | result)) |
| 1289 | ((output-port? (car rest-ports)) |
| 1290 | (set-port-revealed! (car rest-ports) |
| 1291 | (+ (port-revealed (car rest-ports)) 1)) |
| 1292 | (car rest-ports)) |
| 1293 | (else |
| 1294 | (loop (cdr rest-ports)))))) |
| 1295 | |
| 1296 | (define (port->fdes port) |
| 1297 | (set-port-revealed! port (+ (port-revealed port) 1)) |
| 1298 | (fileno port)) |
| 1299 | |
| 1300 | (define (setenv name value) |
| 1301 | (if value |
| 1302 | (putenv (string-append name "=" value)) |
| 1303 | (putenv name))) |
| 1304 | |
| 1305 | (define (unsetenv name) |
| 1306 | "Remove the entry for NAME from the environment." |
| 1307 | (putenv name)) |
| 1308 | |
| 1309 | \f |
| 1310 | |
| 1311 | ;;; {Load Paths} |
| 1312 | ;;; |
| 1313 | |
| 1314 | (define (in-vicinity vicinity file) |
| 1315 | (let ((tail (let ((len (string-length vicinity))) |
| 1316 | (if (zero? len) |
| 1317 | #f |
| 1318 | (string-ref vicinity (- len 1)))))) |
| 1319 | (string-append vicinity |
| 1320 | (if (or (not tail) |
| 1321 | (eq? tail #\/)) |
| 1322 | "" |
| 1323 | "/") |
| 1324 | file))) |
| 1325 | |
| 1326 | \f |
| 1327 | |
| 1328 | ;;; {Help for scm_shell} |
| 1329 | ;;; |
| 1330 | ;;; The argument-processing code used by Guile-based shells generates |
| 1331 | ;;; Scheme code based on the argument list. This page contains help |
| 1332 | ;;; functions for the code it generates. |
| 1333 | ;;; |
| 1334 | |
| 1335 | (define (command-line) (program-arguments)) |
| 1336 | |
| 1337 | ;; This is mostly for the internal use of the code generated by |
| 1338 | ;; scm_compile_shell_switches. |
| 1339 | |
| 1340 | (define (load-user-init) |
| 1341 | (let* ((home (or (getenv "HOME") |
| 1342 | (false-if-exception (passwd:dir (getpwuid (getuid)))) |
| 1343 | "/")) ;; fallback for cygwin etc. |
| 1344 | (init-file (in-vicinity home ".guile"))) |
| 1345 | (if (file-exists? init-file) |
| 1346 | (primitive-load init-file)))) |
| 1347 | |
| 1348 | \f |
| 1349 | |
| 1350 | ;;; {The interpreter stack} |
| 1351 | ;;; |
| 1352 | |
| 1353 | ;; %stacks defined in stacks.c |
| 1354 | (define (%start-stack tag thunk) |
| 1355 | (let ((prompt-tag (make-prompt-tag "start-stack"))) |
| 1356 | (call-with-prompt |
| 1357 | prompt-tag |
| 1358 | (lambda () |
| 1359 | (with-fluids ((%stacks (acons tag prompt-tag |
| 1360 | (or (fluid-ref %stacks) '())))) |
| 1361 | (thunk))) |
| 1362 | (lambda (k . args) |
| 1363 | (%start-stack tag (lambda () (apply k args))))))) |
| 1364 | |
| 1365 | (define-syntax-rule (start-stack tag exp) |
| 1366 | (%start-stack tag (lambda () exp))) |
| 1367 | |
| 1368 | \f |
| 1369 | |
| 1370 | ;;; {Loading by paths} |
| 1371 | ;;; |
| 1372 | |
| 1373 | ;;; Load a Scheme source file named NAME, searching for it in the |
| 1374 | ;;; directories listed in %load-path, and applying each of the file |
| 1375 | ;;; name extensions listed in %load-extensions. |
| 1376 | (define (load-from-path name) |
| 1377 | (start-stack 'load-stack |
| 1378 | (primitive-load-path name))) |
| 1379 | |
| 1380 | (define %load-verbosely #f) |
| 1381 | (define (assert-load-verbosity v) (set! %load-verbosely v)) |
| 1382 | |
| 1383 | (define (%load-announce file) |
| 1384 | (if %load-verbosely |
| 1385 | (with-output-to-port (current-warning-port) |
| 1386 | (lambda () |
| 1387 | (display ";;; ") |
| 1388 | (display "loading ") |
| 1389 | (display file) |
| 1390 | (newline) |
| 1391 | (force-output))))) |
| 1392 | |
| 1393 | (set! %load-hook %load-announce) |
| 1394 | |
| 1395 | \f |
| 1396 | |
| 1397 | ;;; {Reader Extensions} |
| 1398 | ;;; |
| 1399 | ;;; Reader code for various "#c" forms. |
| 1400 | ;;; |
| 1401 | |
| 1402 | (define read-eval? (make-fluid #f)) |
| 1403 | (read-hash-extend #\. |
| 1404 | (lambda (c port) |
| 1405 | (if (fluid-ref read-eval?) |
| 1406 | (eval (read port) (interaction-environment)) |
| 1407 | (error |
| 1408 | "#. read expansion found and read-eval? is #f.")))) |
| 1409 | |
| 1410 | \f |
| 1411 | |
| 1412 | ;;; {Low Level Modules} |
| 1413 | ;;; |
| 1414 | ;;; These are the low level data structures for modules. |
| 1415 | ;;; |
| 1416 | ;;; Every module object is of the type 'module-type', which is a record |
| 1417 | ;;; consisting of the following members: |
| 1418 | ;;; |
| 1419 | ;;; - eval-closure: the function that defines for its module the strategy that |
| 1420 | ;;; shall be followed when looking up symbols in the module. |
| 1421 | ;;; |
| 1422 | ;;; An eval-closure is a function taking two arguments: the symbol to be |
| 1423 | ;;; looked up and a boolean value telling whether a binding for the symbol |
| 1424 | ;;; should be created if it does not exist yet. If the symbol lookup |
| 1425 | ;;; succeeded (either because an existing binding was found or because a new |
| 1426 | ;;; binding was created), a variable object representing the binding is |
| 1427 | ;;; returned. Otherwise, the value #f is returned. Note that the eval |
| 1428 | ;;; closure does not take the module to be searched as an argument: During |
| 1429 | ;;; construction of the eval-closure, the eval-closure has to store the |
| 1430 | ;;; module it belongs to in its environment. This means, that any |
| 1431 | ;;; eval-closure can belong to only one module. |
| 1432 | ;;; |
| 1433 | ;;; The eval-closure of a module can be defined arbitrarily. However, three |
| 1434 | ;;; special cases of eval-closures are to be distinguished: During startup |
| 1435 | ;;; the module system is not yet activated. In this phase, no modules are |
| 1436 | ;;; defined and all bindings are automatically stored by the system in the |
| 1437 | ;;; pre-modules-obarray. Since no eval-closures exist at this time, the |
| 1438 | ;;; functions which require an eval-closure as their argument need to be |
| 1439 | ;;; passed the value #f. |
| 1440 | ;;; |
| 1441 | ;;; The other two special cases of eval-closures are the |
| 1442 | ;;; standard-eval-closure and the standard-interface-eval-closure. Both |
| 1443 | ;;; behave equally for the case that no new binding is to be created. The |
| 1444 | ;;; difference between the two comes in, when the boolean argument to the |
| 1445 | ;;; eval-closure indicates that a new binding shall be created if it is not |
| 1446 | ;;; found. |
| 1447 | ;;; |
| 1448 | ;;; Given that no new binding shall be created, both standard eval-closures |
| 1449 | ;;; define the following standard strategy of searching bindings in the |
| 1450 | ;;; module: First, the module's obarray is searched for the symbol. Second, |
| 1451 | ;;; if no binding for the symbol was found in the module's obarray, the |
| 1452 | ;;; module's binder procedure is exececuted. If this procedure did not |
| 1453 | ;;; return a binding for the symbol, the modules referenced in the module's |
| 1454 | ;;; uses list are recursively searched for a binding of the symbol. If the |
| 1455 | ;;; binding can not be found in these modules also, the symbol lookup has |
| 1456 | ;;; failed. |
| 1457 | ;;; |
| 1458 | ;;; If a new binding shall be created, the standard-interface-eval-closure |
| 1459 | ;;; immediately returns indicating failure. That is, it does not even try |
| 1460 | ;;; to look up the symbol. In contrast, the standard-eval-closure would |
| 1461 | ;;; first search the obarray, and if no binding was found there, would |
| 1462 | ;;; create a new binding in the obarray, therefore not calling the binder |
| 1463 | ;;; procedure or searching the modules in the uses list. |
| 1464 | ;;; |
| 1465 | ;;; The explanation of the following members obarray, binder and uses |
| 1466 | ;;; assumes that the symbol lookup follows the strategy that is defined in |
| 1467 | ;;; the standard-eval-closure and the standard-interface-eval-closure. |
| 1468 | ;;; |
| 1469 | ;;; - obarray: a hash table that maps symbols to variable objects. In this |
| 1470 | ;;; hash table, the definitions are found that are local to the module (that |
| 1471 | ;;; is, not imported from other modules). When looking up bindings in the |
| 1472 | ;;; module, this hash table is searched first. |
| 1473 | ;;; |
| 1474 | ;;; - binder: either #f or a function taking a module and a symbol argument. |
| 1475 | ;;; If it is a function it is called after the obarray has been |
| 1476 | ;;; unsuccessfully searched for a binding. It then can provide bindings |
| 1477 | ;;; that would otherwise not be found locally in the module. |
| 1478 | ;;; |
| 1479 | ;;; - uses: a list of modules from which non-local bindings can be inherited. |
| 1480 | ;;; These modules are the third place queried for bindings after the obarray |
| 1481 | ;;; has been unsuccessfully searched and the binder function did not deliver |
| 1482 | ;;; a result either. |
| 1483 | ;;; |
| 1484 | ;;; - transformer: either #f or a function taking a scheme expression as |
| 1485 | ;;; delivered by read. If it is a function, it will be called to perform |
| 1486 | ;;; syntax transformations (e. g. makro expansion) on the given scheme |
| 1487 | ;;; expression. The output of the transformer function will then be passed |
| 1488 | ;;; to Guile's internal memoizer. This means that the output must be valid |
| 1489 | ;;; scheme code. The only exception is, that the output may make use of the |
| 1490 | ;;; syntax extensions provided to identify the modules that a binding |
| 1491 | ;;; belongs to. |
| 1492 | ;;; |
| 1493 | ;;; - name: the name of the module. This is used for all kinds of printing |
| 1494 | ;;; outputs. In certain places the module name also serves as a way of |
| 1495 | ;;; identification. When adding a module to the uses list of another |
| 1496 | ;;; module, it is made sure that the new uses list will not contain two |
| 1497 | ;;; modules of the same name. |
| 1498 | ;;; |
| 1499 | ;;; - kind: classification of the kind of module. The value is (currently?) |
| 1500 | ;;; only used for printing. It has no influence on how a module is treated. |
| 1501 | ;;; Currently the following values are used when setting the module kind: |
| 1502 | ;;; 'module, 'directory, 'interface, 'custom-interface. If no explicit kind |
| 1503 | ;;; is set, it defaults to 'module. |
| 1504 | ;;; |
| 1505 | ;;; - duplicates-handlers: a list of procedures that get called to make a |
| 1506 | ;;; choice between two duplicate bindings when name clashes occur. See the |
| 1507 | ;;; `duplicate-handlers' global variable below. |
| 1508 | ;;; |
| 1509 | ;;; - observers: a list of procedures that get called when the module is |
| 1510 | ;;; modified. |
| 1511 | ;;; |
| 1512 | ;;; - weak-observers: a weak-key hash table of procedures that get called |
| 1513 | ;;; when the module is modified. See `module-observe-weak' for details. |
| 1514 | ;;; |
| 1515 | ;;; In addition, the module may (must?) contain a binding for |
| 1516 | ;;; `%module-public-interface'. This variable should be bound to a module |
| 1517 | ;;; representing the exported interface of a module. See the |
| 1518 | ;;; `module-public-interface' and `module-export!' procedures. |
| 1519 | ;;; |
| 1520 | ;;; !!! warning: The interface to lazy binder procedures is going |
| 1521 | ;;; to be changed in an incompatible way to permit all the basic |
| 1522 | ;;; module ops to be virtualized. |
| 1523 | ;;; |
| 1524 | ;;; (make-module size use-list lazy-binding-proc) => module |
| 1525 | ;;; module-{obarray,uses,binder}[|-set!] |
| 1526 | ;;; (module? obj) => [#t|#f] |
| 1527 | ;;; (module-locally-bound? module symbol) => [#t|#f] |
| 1528 | ;;; (module-bound? module symbol) => [#t|#f] |
| 1529 | ;;; (module-symbol-locally-interned? module symbol) => [#t|#f] |
| 1530 | ;;; (module-symbol-interned? module symbol) => [#t|#f] |
| 1531 | ;;; (module-local-variable module symbol) => [#<variable ...> | #f] |
| 1532 | ;;; (module-variable module symbol) => [#<variable ...> | #f] |
| 1533 | ;;; (module-symbol-binding module symbol opt-value) |
| 1534 | ;;; => [ <obj> | opt-value | an error occurs ] |
| 1535 | ;;; (module-make-local-var! module symbol) => #<variable...> |
| 1536 | ;;; (module-add! module symbol var) => unspecified |
| 1537 | ;;; (module-remove! module symbol) => unspecified |
| 1538 | ;;; (module-for-each proc module) => unspecified |
| 1539 | ;;; (make-scm-module) => module ; a lazy copy of the symhash module |
| 1540 | ;;; (set-current-module module) => unspecified |
| 1541 | ;;; (current-module) => #<module...> |
| 1542 | ;;; |
| 1543 | ;;; |
| 1544 | |
| 1545 | \f |
| 1546 | |
| 1547 | ;;; {Printing Modules} |
| 1548 | ;;; |
| 1549 | |
| 1550 | ;; This is how modules are printed. You can re-define it. |
| 1551 | (define (%print-module mod port) |
| 1552 | (display "#<" port) |
| 1553 | (display (or (module-kind mod) "module") port) |
| 1554 | (display " " port) |
| 1555 | (display (module-name mod) port) |
| 1556 | (display " " port) |
| 1557 | (display (number->string (object-address mod) 16) port) |
| 1558 | (display ">" port)) |
| 1559 | |
| 1560 | (letrec-syntax |
| 1561 | ;; Locally extend the syntax to allow record accessors to be defined at |
| 1562 | ;; compile-time. Cache the rtd locally to the constructor, the getters and |
| 1563 | ;; the setters, in order to allow for redefinition of the record type; not |
| 1564 | ;; relevant in the case of modules, but perhaps if we make this public, it |
| 1565 | ;; could matter. |
| 1566 | |
| 1567 | ((define-record-type |
| 1568 | (lambda (x) |
| 1569 | (define (make-id scope . fragments) |
| 1570 | (datum->syntax #'scope |
| 1571 | (apply symbol-append |
| 1572 | (map (lambda (x) |
| 1573 | (if (symbol? x) x (syntax->datum x))) |
| 1574 | fragments)))) |
| 1575 | |
| 1576 | (define (getter rtd type-name field slot) |
| 1577 | #`(define #,(make-id rtd type-name '- field) |
| 1578 | (let ((rtd #,rtd)) |
| 1579 | (lambda (#,type-name) |
| 1580 | (if (eq? (struct-vtable #,type-name) rtd) |
| 1581 | (struct-ref #,type-name #,slot) |
| 1582 | (%record-type-error rtd #,type-name)))))) |
| 1583 | |
| 1584 | (define (setter rtd type-name field slot) |
| 1585 | #`(define #,(make-id rtd 'set- type-name '- field '!) |
| 1586 | (let ((rtd #,rtd)) |
| 1587 | (lambda (#,type-name val) |
| 1588 | (if (eq? (struct-vtable #,type-name) rtd) |
| 1589 | (struct-set! #,type-name #,slot val) |
| 1590 | (%record-type-error rtd #,type-name)))))) |
| 1591 | |
| 1592 | (define (accessors rtd type-name fields n exp) |
| 1593 | (syntax-case fields () |
| 1594 | (() exp) |
| 1595 | (((field #:no-accessors) field* ...) (identifier? #'field) |
| 1596 | (accessors rtd type-name #'(field* ...) (1+ n) |
| 1597 | exp)) |
| 1598 | (((field #:no-setter) field* ...) (identifier? #'field) |
| 1599 | (accessors rtd type-name #'(field* ...) (1+ n) |
| 1600 | #`(begin #,exp |
| 1601 | #,(getter rtd type-name #'field n)))) |
| 1602 | (((field #:no-getter) field* ...) (identifier? #'field) |
| 1603 | (accessors rtd type-name #'(field* ...) (1+ n) |
| 1604 | #`(begin #,exp |
| 1605 | #,(setter rtd type-name #'field n)))) |
| 1606 | ((field field* ...) (identifier? #'field) |
| 1607 | (accessors rtd type-name #'(field* ...) (1+ n) |
| 1608 | #`(begin #,exp |
| 1609 | #,(getter rtd type-name #'field n) |
| 1610 | #,(setter rtd type-name #'field n)))))) |
| 1611 | |
| 1612 | (define (predicate rtd type-name fields exp) |
| 1613 | (accessors |
| 1614 | rtd type-name fields 0 |
| 1615 | #`(begin |
| 1616 | #,exp |
| 1617 | (define (#,(make-id rtd type-name '?) obj) |
| 1618 | (and (struct? obj) (eq? (struct-vtable obj) #,rtd)))))) |
| 1619 | |
| 1620 | (define (field-list fields) |
| 1621 | (syntax-case fields () |
| 1622 | (() '()) |
| 1623 | (((f . opts) . rest) (identifier? #'f) |
| 1624 | (cons #'f (field-list #'rest))) |
| 1625 | ((f . rest) (identifier? #'f) |
| 1626 | (cons #'f (field-list #'rest))))) |
| 1627 | |
| 1628 | (define (constructor rtd type-name fields exp) |
| 1629 | (let ((ctor (make-id rtd type-name '-constructor)) |
| 1630 | (args (field-list fields))) |
| 1631 | (predicate rtd type-name fields |
| 1632 | #`(begin #,exp |
| 1633 | (define #,ctor |
| 1634 | (let ((rtd #,rtd)) |
| 1635 | (lambda #,args |
| 1636 | (make-struct rtd 0 #,@args)))) |
| 1637 | (struct-set! #,rtd (+ vtable-offset-user 2) |
| 1638 | #,ctor))))) |
| 1639 | |
| 1640 | (define (type type-name printer fields) |
| 1641 | (define (make-layout) |
| 1642 | (let lp ((fields fields) (slots '())) |
| 1643 | (syntax-case fields () |
| 1644 | (() (datum->syntax #'here |
| 1645 | (make-struct-layout |
| 1646 | (apply string-append slots)))) |
| 1647 | ((_ . rest) (lp #'rest (cons "pw" slots)))))) |
| 1648 | |
| 1649 | (let ((rtd (make-id type-name type-name '-type))) |
| 1650 | (constructor rtd type-name fields |
| 1651 | #`(begin |
| 1652 | (define #,rtd |
| 1653 | (make-struct record-type-vtable 0 |
| 1654 | '#,(make-layout) |
| 1655 | #,printer |
| 1656 | '#,type-name |
| 1657 | '#,(field-list fields))) |
| 1658 | (set-struct-vtable-name! #,rtd '#,type-name))))) |
| 1659 | |
| 1660 | (syntax-case x () |
| 1661 | ((_ type-name printer (field ...)) |
| 1662 | (type #'type-name #'printer #'(field ...))))))) |
| 1663 | |
| 1664 | ;; module-type |
| 1665 | ;; |
| 1666 | ;; A module is characterized by an obarray in which local symbols |
| 1667 | ;; are interned, a list of modules, "uses", from which non-local |
| 1668 | ;; bindings can be inherited, and an optional lazy-binder which |
| 1669 | ;; is a (CLOSURE module symbol) which, as a last resort, can provide |
| 1670 | ;; bindings that would otherwise not be found locally in the module. |
| 1671 | ;; |
| 1672 | ;; NOTE: If you change the set of fields or their order, you also need to |
| 1673 | ;; change the constants in libguile/modules.h. |
| 1674 | ;; |
| 1675 | ;; NOTE: The getter `module-eval-closure' is used in libguile/modules.c. |
| 1676 | ;; NOTE: The getter `module-transfomer' is defined libguile/modules.c. |
| 1677 | ;; NOTE: The getter `module-name' is defined later, due to boot reasons. |
| 1678 | ;; NOTE: The getter `module-public-interface' is used in libguile/modules.c. |
| 1679 | ;; |
| 1680 | (define-record-type module |
| 1681 | (lambda (obj port) (%print-module obj port)) |
| 1682 | (obarray |
| 1683 | uses |
| 1684 | binder |
| 1685 | eval-closure |
| 1686 | (transformer #:no-getter) |
| 1687 | (name #:no-getter) |
| 1688 | kind |
| 1689 | duplicates-handlers |
| 1690 | (import-obarray #:no-setter) |
| 1691 | observers |
| 1692 | (weak-observers #:no-setter) |
| 1693 | version |
| 1694 | submodules |
| 1695 | submodule-binder |
| 1696 | public-interface |
| 1697 | filename))) |
| 1698 | |
| 1699 | |
| 1700 | ;; make-module &opt size uses binder |
| 1701 | ;; |
| 1702 | ;; Create a new module, perhaps with a particular size of obarray, |
| 1703 | ;; initial uses list, or binding procedure. |
| 1704 | ;; |
| 1705 | (define* (make-module #:optional (size 31) (uses '()) (binder #f)) |
| 1706 | (define %default-import-size |
| 1707 | ;; Typical number of imported bindings actually used by a module. |
| 1708 | 600) |
| 1709 | |
| 1710 | (if (not (integer? size)) |
| 1711 | (error "Illegal size to make-module." size)) |
| 1712 | (if (not (and (list? uses) |
| 1713 | (and-map module? uses))) |
| 1714 | (error "Incorrect use list." uses)) |
| 1715 | (if (and binder (not (procedure? binder))) |
| 1716 | (error |
| 1717 | "Lazy-binder expected to be a procedure or #f." binder)) |
| 1718 | |
| 1719 | (let ((module (module-constructor (make-hash-table size) |
| 1720 | uses binder #f macroexpand |
| 1721 | #f #f #f |
| 1722 | (make-hash-table %default-import-size) |
| 1723 | '() |
| 1724 | (make-weak-key-hash-table 31) #f |
| 1725 | (make-hash-table 7) #f #f #f))) |
| 1726 | |
| 1727 | ;; We can't pass this as an argument to module-constructor, |
| 1728 | ;; because we need it to close over a pointer to the module |
| 1729 | ;; itself. |
| 1730 | (set-module-eval-closure! module (standard-eval-closure module)) |
| 1731 | |
| 1732 | module)) |
| 1733 | |
| 1734 | |
| 1735 | \f |
| 1736 | |
| 1737 | ;;; {Observer protocol} |
| 1738 | ;;; |
| 1739 | |
| 1740 | (define (module-observe module proc) |
| 1741 | (set-module-observers! module (cons proc (module-observers module))) |
| 1742 | (cons module proc)) |
| 1743 | |
| 1744 | (define* (module-observe-weak module observer-id #:optional (proc observer-id)) |
| 1745 | ;; Register PROC as an observer of MODULE under name OBSERVER-ID (which can |
| 1746 | ;; be any Scheme object). PROC is invoked and passed MODULE any time |
| 1747 | ;; MODULE is modified. PROC gets unregistered when OBSERVER-ID gets GC'd |
| 1748 | ;; (thus, it is never unregistered if OBSERVER-ID is an immediate value, |
| 1749 | ;; for instance). |
| 1750 | |
| 1751 | ;; The two-argument version is kept for backward compatibility: when called |
| 1752 | ;; with two arguments, the observer gets unregistered when closure PROC |
| 1753 | ;; gets GC'd (making it impossible to use an anonymous lambda for PROC). |
| 1754 | (hashq-set! (module-weak-observers module) observer-id proc)) |
| 1755 | |
| 1756 | (define (module-unobserve token) |
| 1757 | (let ((module (car token)) |
| 1758 | (id (cdr token))) |
| 1759 | (if (integer? id) |
| 1760 | (hash-remove! (module-weak-observers module) id) |
| 1761 | (set-module-observers! module (delq1! id (module-observers module))))) |
| 1762 | *unspecified*) |
| 1763 | |
| 1764 | (define module-defer-observers #f) |
| 1765 | (define module-defer-observers-mutex (make-mutex 'recursive)) |
| 1766 | (define module-defer-observers-table (make-hash-table)) |
| 1767 | |
| 1768 | (define (module-modified m) |
| 1769 | (if module-defer-observers |
| 1770 | (hash-set! module-defer-observers-table m #t) |
| 1771 | (module-call-observers m))) |
| 1772 | |
| 1773 | ;;; This function can be used to delay calls to observers so that they |
| 1774 | ;;; can be called once only in the face of massive updating of modules. |
| 1775 | ;;; |
| 1776 | (define (call-with-deferred-observers thunk) |
| 1777 | (dynamic-wind |
| 1778 | (lambda () |
| 1779 | (lock-mutex module-defer-observers-mutex) |
| 1780 | (set! module-defer-observers #t)) |
| 1781 | thunk |
| 1782 | (lambda () |
| 1783 | (set! module-defer-observers #f) |
| 1784 | (hash-for-each (lambda (m dummy) |
| 1785 | (module-call-observers m)) |
| 1786 | module-defer-observers-table) |
| 1787 | (hash-clear! module-defer-observers-table) |
| 1788 | (unlock-mutex module-defer-observers-mutex)))) |
| 1789 | |
| 1790 | (define (module-call-observers m) |
| 1791 | (for-each (lambda (proc) (proc m)) (module-observers m)) |
| 1792 | |
| 1793 | ;; We assume that weak observers don't (un)register themselves as they are |
| 1794 | ;; called since this would preclude proper iteration over the hash table |
| 1795 | ;; elements. |
| 1796 | (hash-for-each (lambda (id proc) (proc m)) (module-weak-observers m))) |
| 1797 | |
| 1798 | \f |
| 1799 | |
| 1800 | ;;; {Module Searching in General} |
| 1801 | ;;; |
| 1802 | ;;; We sometimes want to look for properties of a symbol |
| 1803 | ;;; just within the obarray of one module. If the property |
| 1804 | ;;; holds, then it is said to hold ``locally'' as in, ``The symbol |
| 1805 | ;;; DISPLAY is locally rebound in the module `safe-guile'.'' |
| 1806 | ;;; |
| 1807 | ;;; |
| 1808 | ;;; Other times, we want to test for a symbol property in the obarray |
| 1809 | ;;; of M and, if it is not found there, try each of the modules in the |
| 1810 | ;;; uses list of M. This is the normal way of testing for some |
| 1811 | ;;; property, so we state these properties without qualification as |
| 1812 | ;;; in: ``The symbol 'fnord is interned in module M because it is |
| 1813 | ;;; interned locally in module M2 which is a member of the uses list |
| 1814 | ;;; of M.'' |
| 1815 | ;;; |
| 1816 | |
| 1817 | ;; module-search fn m |
| 1818 | ;; |
| 1819 | ;; return the first non-#f result of FN applied to M and then to |
| 1820 | ;; the modules in the uses of m, and so on recursively. If all applications |
| 1821 | ;; return #f, then so does this function. |
| 1822 | ;; |
| 1823 | (define (module-search fn m v) |
| 1824 | (define (loop pos) |
| 1825 | (and (pair? pos) |
| 1826 | (or (module-search fn (car pos) v) |
| 1827 | (loop (cdr pos))))) |
| 1828 | (or (fn m v) |
| 1829 | (loop (module-uses m)))) |
| 1830 | |
| 1831 | |
| 1832 | ;;; {Is a symbol bound in a module?} |
| 1833 | ;;; |
| 1834 | ;;; Symbol S in Module M is bound if S is interned in M and if the binding |
| 1835 | ;;; of S in M has been set to some well-defined value. |
| 1836 | ;;; |
| 1837 | |
| 1838 | ;; module-locally-bound? module symbol |
| 1839 | ;; |
| 1840 | ;; Is a symbol bound (interned and defined) locally in a given module? |
| 1841 | ;; |
| 1842 | (define (module-locally-bound? m v) |
| 1843 | (let ((var (module-local-variable m v))) |
| 1844 | (and var |
| 1845 | (variable-bound? var)))) |
| 1846 | |
| 1847 | ;; module-bound? module symbol |
| 1848 | ;; |
| 1849 | ;; Is a symbol bound (interned and defined) anywhere in a given module |
| 1850 | ;; or its uses? |
| 1851 | ;; |
| 1852 | (define (module-bound? m v) |
| 1853 | (let ((var (module-variable m v))) |
| 1854 | (and var |
| 1855 | (variable-bound? var)))) |
| 1856 | |
| 1857 | ;;; {Is a symbol interned in a module?} |
| 1858 | ;;; |
| 1859 | ;;; Symbol S in Module M is interned if S occurs in |
| 1860 | ;;; of S in M has been set to some well-defined value. |
| 1861 | ;;; |
| 1862 | ;;; It is possible to intern a symbol in a module without providing |
| 1863 | ;;; an initial binding for the corresponding variable. This is done |
| 1864 | ;;; with: |
| 1865 | ;;; (module-add! module symbol (make-undefined-variable)) |
| 1866 | ;;; |
| 1867 | ;;; In that case, the symbol is interned in the module, but not |
| 1868 | ;;; bound there. The unbound symbol shadows any binding for that |
| 1869 | ;;; symbol that might otherwise be inherited from a member of the uses list. |
| 1870 | ;;; |
| 1871 | |
| 1872 | (define (module-obarray-get-handle ob key) |
| 1873 | ((if (symbol? key) hashq-get-handle hash-get-handle) ob key)) |
| 1874 | |
| 1875 | (define (module-obarray-ref ob key) |
| 1876 | ((if (symbol? key) hashq-ref hash-ref) ob key)) |
| 1877 | |
| 1878 | (define (module-obarray-set! ob key val) |
| 1879 | ((if (symbol? key) hashq-set! hash-set!) ob key val)) |
| 1880 | |
| 1881 | (define (module-obarray-remove! ob key) |
| 1882 | ((if (symbol? key) hashq-remove! hash-remove!) ob key)) |
| 1883 | |
| 1884 | ;; module-symbol-locally-interned? module symbol |
| 1885 | ;; |
| 1886 | ;; is a symbol interned (not neccessarily defined) locally in a given module |
| 1887 | ;; or its uses? Interned symbols shadow inherited bindings even if |
| 1888 | ;; they are not themselves bound to a defined value. |
| 1889 | ;; |
| 1890 | (define (module-symbol-locally-interned? m v) |
| 1891 | (not (not (module-obarray-get-handle (module-obarray m) v)))) |
| 1892 | |
| 1893 | ;; module-symbol-interned? module symbol |
| 1894 | ;; |
| 1895 | ;; is a symbol interned (not neccessarily defined) anywhere in a given module |
| 1896 | ;; or its uses? Interned symbols shadow inherited bindings even if |
| 1897 | ;; they are not themselves bound to a defined value. |
| 1898 | ;; |
| 1899 | (define (module-symbol-interned? m v) |
| 1900 | (module-search module-symbol-locally-interned? m v)) |
| 1901 | |
| 1902 | |
| 1903 | ;;; {Mapping modules x symbols --> variables} |
| 1904 | ;;; |
| 1905 | |
| 1906 | ;; module-local-variable module symbol |
| 1907 | ;; return the local variable associated with a MODULE and SYMBOL. |
| 1908 | ;; |
| 1909 | ;;; This function is very important. It is the only function that can |
| 1910 | ;;; return a variable from a module other than the mutators that store |
| 1911 | ;;; new variables in modules. Therefore, this function is the location |
| 1912 | ;;; of the "lazy binder" hack. |
| 1913 | ;;; |
| 1914 | ;;; If symbol is defined in MODULE, and if the definition binds symbol |
| 1915 | ;;; to a variable, return that variable object. |
| 1916 | ;;; |
| 1917 | ;;; If the symbols is not found at first, but the module has a lazy binder, |
| 1918 | ;;; then try the binder. |
| 1919 | ;;; |
| 1920 | ;;; If the symbol is not found at all, return #f. |
| 1921 | ;;; |
| 1922 | ;;; (This is now written in C, see `modules.c'.) |
| 1923 | ;;; |
| 1924 | |
| 1925 | ;;; {Mapping modules x symbols --> bindings} |
| 1926 | ;;; |
| 1927 | ;;; These are similar to the mapping to variables, except that the |
| 1928 | ;;; variable is dereferenced. |
| 1929 | ;;; |
| 1930 | |
| 1931 | ;; module-symbol-binding module symbol opt-value |
| 1932 | ;; |
| 1933 | ;; return the binding of a variable specified by name within |
| 1934 | ;; a given module, signalling an error if the variable is unbound. |
| 1935 | ;; If the OPT-VALUE is passed, then instead of signalling an error, |
| 1936 | ;; return OPT-VALUE. |
| 1937 | ;; |
| 1938 | (define (module-symbol-local-binding m v . opt-val) |
| 1939 | (let ((var (module-local-variable m v))) |
| 1940 | (if (and var (variable-bound? var)) |
| 1941 | (variable-ref var) |
| 1942 | (if (not (null? opt-val)) |
| 1943 | (car opt-val) |
| 1944 | (error "Locally unbound variable." v))))) |
| 1945 | |
| 1946 | ;; module-symbol-binding module symbol opt-value |
| 1947 | ;; |
| 1948 | ;; return the binding of a variable specified by name within |
| 1949 | ;; a given module, signalling an error if the variable is unbound. |
| 1950 | ;; If the OPT-VALUE is passed, then instead of signalling an error, |
| 1951 | ;; return OPT-VALUE. |
| 1952 | ;; |
| 1953 | (define (module-symbol-binding m v . opt-val) |
| 1954 | (let ((var (module-variable m v))) |
| 1955 | (if (and var (variable-bound? var)) |
| 1956 | (variable-ref var) |
| 1957 | (if (not (null? opt-val)) |
| 1958 | (car opt-val) |
| 1959 | (error "Unbound variable." v))))) |
| 1960 | |
| 1961 | |
| 1962 | \f |
| 1963 | |
| 1964 | ;;; {Adding Variables to Modules} |
| 1965 | ;;; |
| 1966 | |
| 1967 | ;; module-make-local-var! module symbol |
| 1968 | ;; |
| 1969 | ;; ensure a variable for V in the local namespace of M. |
| 1970 | ;; If no variable was already there, then create a new and uninitialzied |
| 1971 | ;; variable. |
| 1972 | ;; |
| 1973 | ;; This function is used in modules.c. |
| 1974 | ;; |
| 1975 | (define (module-make-local-var! m v) |
| 1976 | (or (let ((b (module-obarray-ref (module-obarray m) v))) |
| 1977 | (and (variable? b) |
| 1978 | (begin |
| 1979 | ;; Mark as modified since this function is called when |
| 1980 | ;; the standard eval closure defines a binding |
| 1981 | (module-modified m) |
| 1982 | b))) |
| 1983 | |
| 1984 | ;; Create a new local variable. |
| 1985 | (let ((local-var (make-undefined-variable))) |
| 1986 | (module-add! m v local-var) |
| 1987 | local-var))) |
| 1988 | |
| 1989 | ;; module-ensure-local-variable! module symbol |
| 1990 | ;; |
| 1991 | ;; Ensure that there is a local variable in MODULE for SYMBOL. If |
| 1992 | ;; there is no binding for SYMBOL, create a new uninitialized |
| 1993 | ;; variable. Return the local variable. |
| 1994 | ;; |
| 1995 | (define (module-ensure-local-variable! module symbol) |
| 1996 | (or (module-local-variable module symbol) |
| 1997 | (let ((var (make-undefined-variable))) |
| 1998 | (module-add! module symbol var) |
| 1999 | var))) |
| 2000 | |
| 2001 | ;; module-add! module symbol var |
| 2002 | ;; |
| 2003 | ;; ensure a particular variable for V in the local namespace of M. |
| 2004 | ;; |
| 2005 | (define (module-add! m v var) |
| 2006 | (if (not (variable? var)) |
| 2007 | (error "Bad variable to module-add!" var)) |
| 2008 | (module-obarray-set! (module-obarray m) v var) |
| 2009 | (module-modified m)) |
| 2010 | |
| 2011 | ;; module-remove! |
| 2012 | ;; |
| 2013 | ;; make sure that a symbol is undefined in the local namespace of M. |
| 2014 | ;; |
| 2015 | (define (module-remove! m v) |
| 2016 | (module-obarray-remove! (module-obarray m) v) |
| 2017 | (module-modified m)) |
| 2018 | |
| 2019 | (define (module-clear! m) |
| 2020 | (hash-clear! (module-obarray m)) |
| 2021 | (module-modified m)) |
| 2022 | |
| 2023 | ;; MODULE-FOR-EACH -- exported |
| 2024 | ;; |
| 2025 | ;; Call PROC on each symbol in MODULE, with arguments of (SYMBOL VARIABLE). |
| 2026 | ;; |
| 2027 | (define (module-for-each proc module) |
| 2028 | (hash-for-each proc (module-obarray module))) |
| 2029 | |
| 2030 | (define (module-map proc module) |
| 2031 | (hash-map->list proc (module-obarray module))) |
| 2032 | |
| 2033 | ;; Submodules |
| 2034 | ;; |
| 2035 | ;; Modules exist in a separate namespace from values, because you generally do |
| 2036 | ;; not want the name of a submodule, which you might not even use, to collide |
| 2037 | ;; with local variables that happen to be named the same as the submodule. |
| 2038 | ;; |
| 2039 | (define (module-ref-submodule module name) |
| 2040 | (or (hashq-ref (module-submodules module) name) |
| 2041 | (and (module-submodule-binder module) |
| 2042 | ((module-submodule-binder module) module name)))) |
| 2043 | |
| 2044 | (define (module-define-submodule! module name submodule) |
| 2045 | (hashq-set! (module-submodules module) name submodule)) |
| 2046 | |
| 2047 | ;; It used to be, however, that module names were also present in the |
| 2048 | ;; value namespace. When we enable deprecated code, we preserve this |
| 2049 | ;; legacy behavior. |
| 2050 | ;; |
| 2051 | ;; These shims are defined here instead of in deprecated.scm because we |
| 2052 | ;; need their definitions before loading other modules. |
| 2053 | ;; |
| 2054 | (begin-deprecated |
| 2055 | (define (module-ref-submodule module name) |
| 2056 | (or (hashq-ref (module-submodules module) name) |
| 2057 | (and (module-submodule-binder module) |
| 2058 | ((module-submodule-binder module) module name)) |
| 2059 | (let ((var (module-local-variable module name))) |
| 2060 | (and var (variable-bound? var) (module? (variable-ref var)) |
| 2061 | (begin |
| 2062 | (warn "module" module "not in submodules table") |
| 2063 | (variable-ref var)))))) |
| 2064 | |
| 2065 | (define (module-define-submodule! module name submodule) |
| 2066 | (let ((var (module-local-variable module name))) |
| 2067 | (if (and var |
| 2068 | (or (not (variable-bound? var)) |
| 2069 | (not (module? (variable-ref var))))) |
| 2070 | (warn "defining module" module ": not overriding local definition" var) |
| 2071 | (module-define! module name submodule))) |
| 2072 | (hashq-set! (module-submodules module) name submodule))) |
| 2073 | |
| 2074 | \f |
| 2075 | |
| 2076 | ;;; {Module-based Loading} |
| 2077 | ;;; |
| 2078 | |
| 2079 | (define (save-module-excursion thunk) |
| 2080 | (let ((inner-module (current-module)) |
| 2081 | (outer-module #f)) |
| 2082 | (dynamic-wind (lambda () |
| 2083 | (set! outer-module (current-module)) |
| 2084 | (set-current-module inner-module) |
| 2085 | (set! inner-module #f)) |
| 2086 | thunk |
| 2087 | (lambda () |
| 2088 | (set! inner-module (current-module)) |
| 2089 | (set-current-module outer-module) |
| 2090 | (set! outer-module #f))))) |
| 2091 | |
| 2092 | \f |
| 2093 | |
| 2094 | ;;; {MODULE-REF -- exported} |
| 2095 | ;;; |
| 2096 | |
| 2097 | ;; Returns the value of a variable called NAME in MODULE or any of its |
| 2098 | ;; used modules. If there is no such variable, then if the optional third |
| 2099 | ;; argument DEFAULT is present, it is returned; otherwise an error is signaled. |
| 2100 | ;; |
| 2101 | (define (module-ref module name . rest) |
| 2102 | (let ((variable (module-variable module name))) |
| 2103 | (if (and variable (variable-bound? variable)) |
| 2104 | (variable-ref variable) |
| 2105 | (if (null? rest) |
| 2106 | (error "No variable named" name 'in module) |
| 2107 | (car rest) ; default value |
| 2108 | )))) |
| 2109 | |
| 2110 | ;; MODULE-SET! -- exported |
| 2111 | ;; |
| 2112 | ;; Sets the variable called NAME in MODULE (or in a module that MODULE uses) |
| 2113 | ;; to VALUE; if there is no such variable, an error is signaled. |
| 2114 | ;; |
| 2115 | (define (module-set! module name value) |
| 2116 | (let ((variable (module-variable module name))) |
| 2117 | (if variable |
| 2118 | (variable-set! variable value) |
| 2119 | (error "No variable named" name 'in module)))) |
| 2120 | |
| 2121 | ;; MODULE-DEFINE! -- exported |
| 2122 | ;; |
| 2123 | ;; Sets the variable called NAME in MODULE to VALUE; if there is no such |
| 2124 | ;; variable, it is added first. |
| 2125 | ;; |
| 2126 | (define (module-define! module name value) |
| 2127 | (let ((variable (module-local-variable module name))) |
| 2128 | (if variable |
| 2129 | (begin |
| 2130 | (variable-set! variable value) |
| 2131 | (module-modified module)) |
| 2132 | (let ((variable (make-variable value))) |
| 2133 | (module-add! module name variable))))) |
| 2134 | |
| 2135 | ;; MODULE-DEFINED? -- exported |
| 2136 | ;; |
| 2137 | ;; Return #t iff NAME is defined in MODULE (or in a module that MODULE |
| 2138 | ;; uses) |
| 2139 | ;; |
| 2140 | (define (module-defined? module name) |
| 2141 | (let ((variable (module-variable module name))) |
| 2142 | (and variable (variable-bound? variable)))) |
| 2143 | |
| 2144 | ;; MODULE-USE! module interface |
| 2145 | ;; |
| 2146 | ;; Add INTERFACE to the list of interfaces used by MODULE. |
| 2147 | ;; |
| 2148 | (define (module-use! module interface) |
| 2149 | (if (not (or (eq? module interface) |
| 2150 | (memq interface (module-uses module)))) |
| 2151 | (begin |
| 2152 | ;; Newly used modules must be appended rather than consed, so that |
| 2153 | ;; `module-variable' traverses the use list starting from the first |
| 2154 | ;; used module. |
| 2155 | (set-module-uses! module (append (module-uses module) |
| 2156 | (list interface))) |
| 2157 | (hash-clear! (module-import-obarray module)) |
| 2158 | (module-modified module)))) |
| 2159 | |
| 2160 | ;; MODULE-USE-INTERFACES! module interfaces |
| 2161 | ;; |
| 2162 | ;; Same as MODULE-USE!, but only notifies module observers after all |
| 2163 | ;; interfaces are added to the inports list. |
| 2164 | ;; |
| 2165 | (define (module-use-interfaces! module interfaces) |
| 2166 | (let* ((cur (module-uses module)) |
| 2167 | (new (let lp ((in interfaces) (out '())) |
| 2168 | (if (null? in) |
| 2169 | (reverse out) |
| 2170 | (lp (cdr in) |
| 2171 | (let ((iface (car in))) |
| 2172 | (if (or (memq iface cur) (memq iface out)) |
| 2173 | out |
| 2174 | (cons iface out)))))))) |
| 2175 | (set-module-uses! module (append cur new)) |
| 2176 | (hash-clear! (module-import-obarray module)) |
| 2177 | (module-modified module))) |
| 2178 | |
| 2179 | \f |
| 2180 | |
| 2181 | ;;; {Recursive Namespaces} |
| 2182 | ;;; |
| 2183 | ;;; A hierarchical namespace emerges if we consider some module to be |
| 2184 | ;;; root, and submodules of that module to be nested namespaces. |
| 2185 | ;;; |
| 2186 | ;;; The routines here manage variable names in hierarchical namespace. |
| 2187 | ;;; Each variable name is a list of elements, looked up in successively nested |
| 2188 | ;;; modules. |
| 2189 | ;;; |
| 2190 | ;;; (nested-ref some-root-module '(foo bar baz)) |
| 2191 | ;;; => <value of a variable named baz in the submodule bar of |
| 2192 | ;;; the submodule foo of some-root-module> |
| 2193 | ;;; |
| 2194 | ;;; |
| 2195 | ;;; There are: |
| 2196 | ;;; |
| 2197 | ;;; ;; a-root is a module |
| 2198 | ;;; ;; name is a list of symbols |
| 2199 | ;;; |
| 2200 | ;;; nested-ref a-root name |
| 2201 | ;;; nested-set! a-root name val |
| 2202 | ;;; nested-define! a-root name val |
| 2203 | ;;; nested-remove! a-root name |
| 2204 | ;;; |
| 2205 | ;;; These functions manipulate values in namespaces. For referencing the |
| 2206 | ;;; namespaces themselves, use the following: |
| 2207 | ;;; |
| 2208 | ;;; nested-ref-module a-root name |
| 2209 | ;;; nested-define-module! a-root name mod |
| 2210 | ;;; |
| 2211 | ;;; (current-module) is a natural choice for a root so for convenience there are |
| 2212 | ;;; also: |
| 2213 | ;;; |
| 2214 | ;;; local-ref name == nested-ref (current-module) name |
| 2215 | ;;; local-set! name val == nested-set! (current-module) name val |
| 2216 | ;;; local-define name val == nested-define! (current-module) name val |
| 2217 | ;;; local-remove name == nested-remove! (current-module) name |
| 2218 | ;;; local-ref-module name == nested-ref-module (current-module) name |
| 2219 | ;;; local-define-module! name m == nested-define-module! (current-module) name m |
| 2220 | ;;; |
| 2221 | |
| 2222 | |
| 2223 | (define (nested-ref root names) |
| 2224 | (if (null? names) |
| 2225 | root |
| 2226 | (let loop ((cur root) |
| 2227 | (head (car names)) |
| 2228 | (tail (cdr names))) |
| 2229 | (if (null? tail) |
| 2230 | (module-ref cur head #f) |
| 2231 | (let ((cur (module-ref-submodule cur head))) |
| 2232 | (and cur |
| 2233 | (loop cur (car tail) (cdr tail)))))))) |
| 2234 | |
| 2235 | (define (nested-set! root names val) |
| 2236 | (let loop ((cur root) |
| 2237 | (head (car names)) |
| 2238 | (tail (cdr names))) |
| 2239 | (if (null? tail) |
| 2240 | (module-set! cur head val) |
| 2241 | (let ((cur (module-ref-submodule cur head))) |
| 2242 | (if (not cur) |
| 2243 | (error "failed to resolve module" names) |
| 2244 | (loop cur (car tail) (cdr tail))))))) |
| 2245 | |
| 2246 | (define (nested-define! root names val) |
| 2247 | (let loop ((cur root) |
| 2248 | (head (car names)) |
| 2249 | (tail (cdr names))) |
| 2250 | (if (null? tail) |
| 2251 | (module-define! cur head val) |
| 2252 | (let ((cur (module-ref-submodule cur head))) |
| 2253 | (if (not cur) |
| 2254 | (error "failed to resolve module" names) |
| 2255 | (loop cur (car tail) (cdr tail))))))) |
| 2256 | |
| 2257 | (define (nested-remove! root names) |
| 2258 | (let loop ((cur root) |
| 2259 | (head (car names)) |
| 2260 | (tail (cdr names))) |
| 2261 | (if (null? tail) |
| 2262 | (module-remove! cur head) |
| 2263 | (let ((cur (module-ref-submodule cur head))) |
| 2264 | (if (not cur) |
| 2265 | (error "failed to resolve module" names) |
| 2266 | (loop cur (car tail) (cdr tail))))))) |
| 2267 | |
| 2268 | |
| 2269 | (define (nested-ref-module root names) |
| 2270 | (let loop ((cur root) |
| 2271 | (names names)) |
| 2272 | (if (null? names) |
| 2273 | cur |
| 2274 | (let ((cur (module-ref-submodule cur (car names)))) |
| 2275 | (and cur |
| 2276 | (loop cur (cdr names))))))) |
| 2277 | |
| 2278 | (define (nested-define-module! root names module) |
| 2279 | (if (null? names) |
| 2280 | (error "can't redefine root module" root module) |
| 2281 | (let loop ((cur root) |
| 2282 | (head (car names)) |
| 2283 | (tail (cdr names))) |
| 2284 | (if (null? tail) |
| 2285 | (module-define-submodule! cur head module) |
| 2286 | (let ((cur (or (module-ref-submodule cur head) |
| 2287 | (let ((m (make-module 31))) |
| 2288 | (set-module-kind! m 'directory) |
| 2289 | (set-module-name! m (append (module-name cur) |
| 2290 | (list head))) |
| 2291 | (module-define-submodule! cur head m) |
| 2292 | m)))) |
| 2293 | (loop cur (car tail) (cdr tail))))))) |
| 2294 | |
| 2295 | |
| 2296 | (define (local-ref names) |
| 2297 | (nested-ref (current-module) names)) |
| 2298 | |
| 2299 | (define (local-set! names val) |
| 2300 | (nested-set! (current-module) names val)) |
| 2301 | |
| 2302 | (define (local-define names val) |
| 2303 | (nested-define! (current-module) names val)) |
| 2304 | |
| 2305 | (define (local-remove names) |
| 2306 | (nested-remove! (current-module) names)) |
| 2307 | |
| 2308 | (define (local-ref-module names) |
| 2309 | (nested-ref-module (current-module) names)) |
| 2310 | |
| 2311 | (define (local-define-module names mod) |
| 2312 | (nested-define-module! (current-module) names mod)) |
| 2313 | |
| 2314 | |
| 2315 | |
| 2316 | \f |
| 2317 | |
| 2318 | ;;; {The (guile) module} |
| 2319 | ;;; |
| 2320 | ;;; The standard module, which has the core Guile bindings. Also called the |
| 2321 | ;;; "root module", as it is imported by many other modules, but it is not |
| 2322 | ;;; necessarily the root of anything; and indeed, the module named '() might be |
| 2323 | ;;; better thought of as a root. |
| 2324 | ;;; |
| 2325 | |
| 2326 | (define (set-system-module! m s) |
| 2327 | (set-procedure-property! (module-eval-closure m) 'system-module s)) |
| 2328 | |
| 2329 | ;; The root module uses the pre-modules-obarray as its obarray. This |
| 2330 | ;; special obarray accumulates all bindings that have been established |
| 2331 | ;; before the module system is fully booted. |
| 2332 | ;; |
| 2333 | ;; (The obarray continues to be used by code that has been closed over |
| 2334 | ;; before the module system has been booted.) |
| 2335 | ;; |
| 2336 | (define the-root-module |
| 2337 | (let ((m (make-module 0))) |
| 2338 | (set-module-obarray! m (%get-pre-modules-obarray)) |
| 2339 | (set-module-name! m '(guile)) |
| 2340 | (set-system-module! m #t) |
| 2341 | m)) |
| 2342 | |
| 2343 | ;; The root interface is a module that uses the same obarray as the |
| 2344 | ;; root module. It does not allow new definitions, tho. |
| 2345 | ;; |
| 2346 | (define the-scm-module |
| 2347 | (let ((m (make-module 0))) |
| 2348 | (set-module-obarray! m (%get-pre-modules-obarray)) |
| 2349 | (set-module-eval-closure! m (standard-interface-eval-closure m)) |
| 2350 | (set-module-name! m '(guile)) |
| 2351 | (set-module-kind! m 'interface) |
| 2352 | (set-system-module! m #t) |
| 2353 | |
| 2354 | ;; In Guile 1.8 and earlier M was its own public interface. |
| 2355 | (set-module-public-interface! m m) |
| 2356 | |
| 2357 | m)) |
| 2358 | |
| 2359 | (set-module-public-interface! the-root-module the-scm-module) |
| 2360 | |
| 2361 | \f |
| 2362 | |
| 2363 | ;; Now that we have a root module, even though modules aren't fully booted, |
| 2364 | ;; expand the definition of resolve-module. |
| 2365 | ;; |
| 2366 | (define (resolve-module name . args) |
| 2367 | (if (equal? name '(guile)) |
| 2368 | the-root-module |
| 2369 | (error "unexpected module to resolve during module boot" name))) |
| 2370 | |
| 2371 | ;; Cheat. These bindings are needed by modules.c, but we don't want |
| 2372 | ;; to move their real definition here because that would be unnatural. |
| 2373 | ;; |
| 2374 | (define define-module* #f) |
| 2375 | (define process-use-modules #f) |
| 2376 | (define module-export! #f) |
| 2377 | (define default-duplicate-binding-procedures #f) |
| 2378 | |
| 2379 | ;; This boots the module system. All bindings needed by modules.c |
| 2380 | ;; must have been defined by now. |
| 2381 | ;; |
| 2382 | (set-current-module the-root-module) |
| 2383 | |
| 2384 | |
| 2385 | \f |
| 2386 | |
| 2387 | ;; Now that modules are booted, give module-name its final definition. |
| 2388 | ;; |
| 2389 | (define module-name |
| 2390 | (let ((accessor (record-accessor module-type 'name))) |
| 2391 | (lambda (mod) |
| 2392 | (or (accessor mod) |
| 2393 | (let ((name (list (gensym)))) |
| 2394 | ;; Name MOD and bind it in the module root so that it's visible to |
| 2395 | ;; `resolve-module'. This is important as `psyntax' stores module |
| 2396 | ;; names and relies on being able to `resolve-module' them. |
| 2397 | (set-module-name! mod name) |
| 2398 | (nested-define-module! (resolve-module '() #f) name mod) |
| 2399 | (accessor mod)))))) |
| 2400 | |
| 2401 | (define (make-modules-in module name) |
| 2402 | (or (nested-ref-module module name) |
| 2403 | (let ((m (make-module 31))) |
| 2404 | (set-module-kind! m 'directory) |
| 2405 | (set-module-name! m (append (module-name module) name)) |
| 2406 | (nested-define-module! module name m) |
| 2407 | m))) |
| 2408 | |
| 2409 | (define (beautify-user-module! module) |
| 2410 | (let ((interface (module-public-interface module))) |
| 2411 | (if (or (not interface) |
| 2412 | (eq? interface module)) |
| 2413 | (let ((interface (make-module 31))) |
| 2414 | (set-module-name! interface (module-name module)) |
| 2415 | (set-module-version! interface (module-version module)) |
| 2416 | (set-module-kind! interface 'interface) |
| 2417 | (set-module-public-interface! module interface)))) |
| 2418 | (if (and (not (memq the-scm-module (module-uses module))) |
| 2419 | (not (eq? module the-root-module))) |
| 2420 | ;; Import the default set of bindings (from the SCM module) in MODULE. |
| 2421 | (module-use! module the-scm-module))) |
| 2422 | |
| 2423 | (define (version-matches? version-ref target) |
| 2424 | (define (sub-versions-match? v-refs t) |
| 2425 | (define (sub-version-matches? v-ref t) |
| 2426 | (let ((matches? (lambda (v) (sub-version-matches? v t)))) |
| 2427 | (cond |
| 2428 | ((number? v-ref) (eqv? v-ref t)) |
| 2429 | ((list? v-ref) |
| 2430 | (case (car v-ref) |
| 2431 | ((>=) (>= t (cadr v-ref))) |
| 2432 | ((<=) (<= t (cadr v-ref))) |
| 2433 | ((and) (and-map matches? (cdr v-ref))) |
| 2434 | ((or) (or-map matches? (cdr v-ref))) |
| 2435 | ((not) (not (matches? (cadr v-ref)))) |
| 2436 | (else (error "Invalid sub-version reference" v-ref)))) |
| 2437 | (else (error "Invalid sub-version reference" v-ref))))) |
| 2438 | (or (null? v-refs) |
| 2439 | (and (not (null? t)) |
| 2440 | (sub-version-matches? (car v-refs) (car t)) |
| 2441 | (sub-versions-match? (cdr v-refs) (cdr t))))) |
| 2442 | |
| 2443 | (let ((matches? (lambda (v) (version-matches? v target)))) |
| 2444 | (or (null? version-ref) |
| 2445 | (case (car version-ref) |
| 2446 | ((and) (and-map matches? (cdr version-ref))) |
| 2447 | ((or) (or-map matches? (cdr version-ref))) |
| 2448 | ((not) (not (matches? (cadr version-ref)))) |
| 2449 | (else (sub-versions-match? version-ref target)))))) |
| 2450 | |
| 2451 | (define (make-fresh-user-module) |
| 2452 | (let ((m (make-module))) |
| 2453 | (beautify-user-module! m) |
| 2454 | m)) |
| 2455 | |
| 2456 | ;; NOTE: This binding is used in libguile/modules.c. |
| 2457 | ;; |
| 2458 | (define resolve-module |
| 2459 | (let ((root (make-module))) |
| 2460 | (set-module-name! root '()) |
| 2461 | ;; Define the-root-module as '(guile). |
| 2462 | (module-define-submodule! root 'guile the-root-module) |
| 2463 | |
| 2464 | (lambda* (name #:optional (autoload #t) (version #f) #:key (ensure #t)) |
| 2465 | (let ((already (nested-ref-module root name))) |
| 2466 | (cond |
| 2467 | ((and already |
| 2468 | (or (not autoload) (module-public-interface already))) |
| 2469 | ;; A hit, a palpable hit. |
| 2470 | (if (and version |
| 2471 | (not (version-matches? version (module-version already)))) |
| 2472 | (error "incompatible module version already loaded" name)) |
| 2473 | already) |
| 2474 | (autoload |
| 2475 | ;; Try to autoload the module, and recurse. |
| 2476 | (try-load-module name version) |
| 2477 | (resolve-module name #f #:ensure ensure)) |
| 2478 | (else |
| 2479 | ;; No module found (or if one was, it had no public interface), and |
| 2480 | ;; we're not autoloading. Make an empty module if #:ensure is true. |
| 2481 | (or already |
| 2482 | (and ensure |
| 2483 | (make-modules-in root name))))))))) |
| 2484 | |
| 2485 | |
| 2486 | (define (try-load-module name version) |
| 2487 | (try-module-autoload name version)) |
| 2488 | |
| 2489 | (define (reload-module m) |
| 2490 | "Revisit the source file corresponding to the module @var{m}." |
| 2491 | (let ((f (module-filename m))) |
| 2492 | (if f |
| 2493 | (save-module-excursion |
| 2494 | (lambda () |
| 2495 | ;; Re-set the initial environment, as in try-module-autoload. |
| 2496 | (set-current-module (make-fresh-user-module)) |
| 2497 | (primitive-load-path f) |
| 2498 | m)) |
| 2499 | ;; Though we could guess, we *should* know it. |
| 2500 | (error "unknown file name for module" m)))) |
| 2501 | |
| 2502 | (define (purify-module! module) |
| 2503 | "Removes bindings in MODULE which are inherited from the (guile) module." |
| 2504 | (let ((use-list (module-uses module))) |
| 2505 | (if (and (pair? use-list) |
| 2506 | (eq? (car (last-pair use-list)) the-scm-module)) |
| 2507 | (set-module-uses! module (reverse (cdr (reverse use-list))))))) |
| 2508 | |
| 2509 | ;; Return a module that is an interface to the module designated by |
| 2510 | ;; NAME. |
| 2511 | ;; |
| 2512 | ;; `resolve-interface' takes four keyword arguments: |
| 2513 | ;; |
| 2514 | ;; #:select SELECTION |
| 2515 | ;; |
| 2516 | ;; SELECTION is a list of binding-specs to be imported; A binding-spec |
| 2517 | ;; is either a symbol or a pair of symbols (ORIG . SEEN), where ORIG |
| 2518 | ;; is the name in the used module and SEEN is the name in the using |
| 2519 | ;; module. Note that SEEN is also passed through RENAMER, below. The |
| 2520 | ;; default is to select all bindings. If you specify no selection but |
| 2521 | ;; a renamer, only the bindings that already exist in the used module |
| 2522 | ;; are made available in the interface. Bindings that are added later |
| 2523 | ;; are not picked up. |
| 2524 | ;; |
| 2525 | ;; #:hide BINDINGS |
| 2526 | ;; |
| 2527 | ;; BINDINGS is a list of bindings which should not be imported. |
| 2528 | ;; |
| 2529 | ;; #:prefix PREFIX |
| 2530 | ;; |
| 2531 | ;; PREFIX is a symbol that will be appended to each exported name. |
| 2532 | ;; The default is to not perform any renaming. |
| 2533 | ;; |
| 2534 | ;; #:renamer RENAMER |
| 2535 | ;; |
| 2536 | ;; RENAMER is a procedure that takes a symbol and returns its new |
| 2537 | ;; name. The default is not perform any renaming. |
| 2538 | ;; |
| 2539 | ;; Signal "no code for module" error if module name is not resolvable |
| 2540 | ;; or its public interface is not available. Signal "no binding" |
| 2541 | ;; error if selected binding does not exist in the used module. |
| 2542 | ;; |
| 2543 | (define* (resolve-interface name #:key |
| 2544 | (select #f) |
| 2545 | (hide '()) |
| 2546 | (prefix #f) |
| 2547 | (renamer (if prefix |
| 2548 | (symbol-prefix-proc prefix) |
| 2549 | identity)) |
| 2550 | version) |
| 2551 | (let* ((module (resolve-module name #t version #:ensure #f)) |
| 2552 | (public-i (and module (module-public-interface module)))) |
| 2553 | (and (or (not module) (not public-i)) |
| 2554 | (error "no code for module" name)) |
| 2555 | (if (and (not select) (null? hide) (eq? renamer identity)) |
| 2556 | public-i |
| 2557 | (let ((selection (or select (module-map (lambda (sym var) sym) |
| 2558 | public-i))) |
| 2559 | (custom-i (make-module 31))) |
| 2560 | (set-module-kind! custom-i 'custom-interface) |
| 2561 | (set-module-name! custom-i name) |
| 2562 | ;; XXX - should use a lazy binder so that changes to the |
| 2563 | ;; used module are picked up automatically. |
| 2564 | (for-each (lambda (bspec) |
| 2565 | (let* ((direct? (symbol? bspec)) |
| 2566 | (orig (if direct? bspec (car bspec))) |
| 2567 | (seen (if direct? bspec (cdr bspec))) |
| 2568 | (var (or (module-local-variable public-i orig) |
| 2569 | (module-local-variable module orig) |
| 2570 | (error |
| 2571 | ;; fixme: format manually for now |
| 2572 | (simple-format |
| 2573 | #f "no binding `~A' in module ~A" |
| 2574 | orig name))))) |
| 2575 | (if (memq orig hide) |
| 2576 | (set! hide (delq! orig hide)) |
| 2577 | (module-add! custom-i |
| 2578 | (renamer seen) |
| 2579 | var)))) |
| 2580 | selection) |
| 2581 | ;; Check that we are not hiding bindings which don't exist |
| 2582 | (for-each (lambda (binding) |
| 2583 | (if (not (module-local-variable public-i binding)) |
| 2584 | (error |
| 2585 | (simple-format |
| 2586 | #f "no binding `~A' to hide in module ~A" |
| 2587 | binding name)))) |
| 2588 | hide) |
| 2589 | custom-i)))) |
| 2590 | |
| 2591 | (define (symbol-prefix-proc prefix) |
| 2592 | (lambda (symbol) |
| 2593 | (symbol-append prefix symbol))) |
| 2594 | |
| 2595 | ;; This function is called from "modules.c". If you change it, be |
| 2596 | ;; sure to update "modules.c" as well. |
| 2597 | |
| 2598 | (define* (define-module* name |
| 2599 | #:key filename pure version (duplicates '()) |
| 2600 | (imports '()) (exports '()) (replacements '()) |
| 2601 | (re-exports '()) (autoloads '()) transformer) |
| 2602 | (define (list-of pred l) |
| 2603 | (or (null? l) |
| 2604 | (and (pair? l) (pred (car l)) (list-of pred (cdr l))))) |
| 2605 | (define (valid-export? x) |
| 2606 | (or (symbol? x) (and (pair? x) (symbol? (car x)) (symbol? (cdr x))))) |
| 2607 | (define (valid-autoload? x) |
| 2608 | (and (pair? x) (list-of symbol? (car x)) (list-of symbol? (cdr x)))) |
| 2609 | |
| 2610 | (define (resolve-imports imports) |
| 2611 | (define (resolve-import import-spec) |
| 2612 | (if (list? import-spec) |
| 2613 | (apply resolve-interface import-spec) |
| 2614 | (error "unexpected use-module specification" import-spec))) |
| 2615 | (let lp ((imports imports) (out '())) |
| 2616 | (cond |
| 2617 | ((null? imports) (reverse! out)) |
| 2618 | ((pair? imports) |
| 2619 | (lp (cdr imports) |
| 2620 | (cons (resolve-import (car imports)) out))) |
| 2621 | (else (error "unexpected tail of imports list" imports))))) |
| 2622 | |
| 2623 | ;; We could add a #:no-check arg, set by the define-module macro, if |
| 2624 | ;; these checks are taking too much time. |
| 2625 | ;; |
| 2626 | (let ((module (resolve-module name #f))) |
| 2627 | (beautify-user-module! module) |
| 2628 | (if filename |
| 2629 | (set-module-filename! module filename)) |
| 2630 | (if pure |
| 2631 | (purify-module! module)) |
| 2632 | (if version |
| 2633 | (begin |
| 2634 | (if (not (list-of integer? version)) |
| 2635 | (error "expected list of integers for version")) |
| 2636 | (set-module-version! module version) |
| 2637 | (set-module-version! (module-public-interface module) version))) |
| 2638 | (let ((imports (resolve-imports imports))) |
| 2639 | (call-with-deferred-observers |
| 2640 | (lambda () |
| 2641 | (if (pair? imports) |
| 2642 | (module-use-interfaces! module imports)) |
| 2643 | (if (list-of valid-export? exports) |
| 2644 | (if (pair? exports) |
| 2645 | (module-export! module exports)) |
| 2646 | (error "expected exports to be a list of symbols or symbol pairs")) |
| 2647 | (if (list-of valid-export? replacements) |
| 2648 | (if (pair? replacements) |
| 2649 | (module-replace! module replacements)) |
| 2650 | (error "expected replacements to be a list of symbols or symbol pairs")) |
| 2651 | (if (list-of valid-export? re-exports) |
| 2652 | (if (pair? re-exports) |
| 2653 | (module-re-export! module re-exports)) |
| 2654 | (error "expected re-exports to be a list of symbols or symbol pairs")) |
| 2655 | ;; FIXME |
| 2656 | (if (not (null? autoloads)) |
| 2657 | (apply module-autoload! module autoloads)) |
| 2658 | ;; Wait until modules have been loaded to resolve duplicates |
| 2659 | ;; handlers. |
| 2660 | (if (pair? duplicates) |
| 2661 | (let ((handlers (lookup-duplicates-handlers duplicates))) |
| 2662 | (set-module-duplicates-handlers! module handlers)))))) |
| 2663 | |
| 2664 | (if transformer |
| 2665 | (if (and (pair? transformer) (list-of symbol? transformer)) |
| 2666 | (let ((iface (resolve-interface transformer)) |
| 2667 | (sym (car (last-pair transformer)))) |
| 2668 | (set-module-transformer! module (module-ref iface sym))) |
| 2669 | (error "expected transformer to be a module name" transformer))) |
| 2670 | |
| 2671 | (run-hook module-defined-hook module) |
| 2672 | module)) |
| 2673 | |
| 2674 | ;; `module-defined-hook' is a hook that is run whenever a new module |
| 2675 | ;; is defined. Its members are called with one argument, the new |
| 2676 | ;; module. |
| 2677 | (define module-defined-hook (make-hook 1)) |
| 2678 | |
| 2679 | \f |
| 2680 | |
| 2681 | ;;; {Autoload} |
| 2682 | ;;; |
| 2683 | |
| 2684 | (define (make-autoload-interface module name bindings) |
| 2685 | (let ((b (lambda (a sym definep) |
| 2686 | (and (memq sym bindings) |
| 2687 | (let ((i (module-public-interface (resolve-module name)))) |
| 2688 | (if (not i) |
| 2689 | (error "missing interface for module" name)) |
| 2690 | (let ((autoload (memq a (module-uses module)))) |
| 2691 | ;; Replace autoload-interface with actual interface if |
| 2692 | ;; that has not happened yet. |
| 2693 | (if (pair? autoload) |
| 2694 | (set-car! autoload i))) |
| 2695 | (module-local-variable i sym)))))) |
| 2696 | (module-constructor (make-hash-table 0) '() b #f #f name 'autoload #f |
| 2697 | (make-hash-table 0) '() (make-weak-value-hash-table 31) #f |
| 2698 | (make-hash-table 0) #f #f #f))) |
| 2699 | |
| 2700 | (define (module-autoload! module . args) |
| 2701 | "Have @var{module} automatically load the module named @var{name} when one |
| 2702 | of the symbols listed in @var{bindings} is looked up. @var{args} should be a |
| 2703 | list of module-name/binding-list pairs, e.g., as in @code{(module-autoload! |
| 2704 | module '(ice-9 q) '(make-q q-length))}." |
| 2705 | (let loop ((args args)) |
| 2706 | (cond ((null? args) |
| 2707 | #t) |
| 2708 | ((null? (cdr args)) |
| 2709 | (error "invalid name+binding autoload list" args)) |
| 2710 | (else |
| 2711 | (let ((name (car args)) |
| 2712 | (bindings (cadr args))) |
| 2713 | (module-use! module (make-autoload-interface module |
| 2714 | name bindings)) |
| 2715 | (loop (cddr args))))))) |
| 2716 | |
| 2717 | |
| 2718 | \f |
| 2719 | |
| 2720 | ;;; {Autoloading modules} |
| 2721 | ;;; |
| 2722 | |
| 2723 | (define autoloads-in-progress '()) |
| 2724 | |
| 2725 | ;; This function is called from "modules.c". If you change it, be |
| 2726 | ;; sure to update "modules.c" as well. |
| 2727 | |
| 2728 | (define* (try-module-autoload module-name #:optional version) |
| 2729 | (let* ((reverse-name (reverse module-name)) |
| 2730 | (name (symbol->string (car reverse-name))) |
| 2731 | (dir-hint-module-name (reverse (cdr reverse-name))) |
| 2732 | (dir-hint (apply string-append |
| 2733 | (map (lambda (elt) |
| 2734 | (string-append (symbol->string elt) "/")) |
| 2735 | dir-hint-module-name)))) |
| 2736 | (resolve-module dir-hint-module-name #f) |
| 2737 | (and (not (autoload-done-or-in-progress? dir-hint name)) |
| 2738 | (let ((didit #f)) |
| 2739 | (dynamic-wind |
| 2740 | (lambda () (autoload-in-progress! dir-hint name)) |
| 2741 | (lambda () |
| 2742 | (with-fluids ((current-reader #f)) |
| 2743 | (save-module-excursion |
| 2744 | (lambda () |
| 2745 | ;; The initial environment when loading a module is a fresh |
| 2746 | ;; user module. |
| 2747 | (set-current-module (make-fresh-user-module)) |
| 2748 | ;; Here we could allow some other search strategy (other than |
| 2749 | ;; primitive-load-path), for example using versions encoded |
| 2750 | ;; into the file system -- but then we would have to figure |
| 2751 | ;; out how to locate the compiled file, do auto-compilation, |
| 2752 | ;; etc. Punt for now, and don't use versions when locating |
| 2753 | ;; the file. |
| 2754 | (primitive-load-path (in-vicinity dir-hint name) #f) |
| 2755 | (set! didit #t))))) |
| 2756 | (lambda () (set-autoloaded! dir-hint name didit))) |
| 2757 | didit)))) |
| 2758 | |
| 2759 | \f |
| 2760 | |
| 2761 | ;;; {Dynamic linking of modules} |
| 2762 | ;;; |
| 2763 | |
| 2764 | (define autoloads-done '((guile . guile))) |
| 2765 | |
| 2766 | (define (autoload-done-or-in-progress? p m) |
| 2767 | (let ((n (cons p m))) |
| 2768 | (->bool (or (member n autoloads-done) |
| 2769 | (member n autoloads-in-progress))))) |
| 2770 | |
| 2771 | (define (autoload-done! p m) |
| 2772 | (let ((n (cons p m))) |
| 2773 | (set! autoloads-in-progress |
| 2774 | (delete! n autoloads-in-progress)) |
| 2775 | (or (member n autoloads-done) |
| 2776 | (set! autoloads-done (cons n autoloads-done))))) |
| 2777 | |
| 2778 | (define (autoload-in-progress! p m) |
| 2779 | (let ((n (cons p m))) |
| 2780 | (set! autoloads-done |
| 2781 | (delete! n autoloads-done)) |
| 2782 | (set! autoloads-in-progress (cons n autoloads-in-progress)))) |
| 2783 | |
| 2784 | (define (set-autoloaded! p m done?) |
| 2785 | (if done? |
| 2786 | (autoload-done! p m) |
| 2787 | (let ((n (cons p m))) |
| 2788 | (set! autoloads-done (delete! n autoloads-done)) |
| 2789 | (set! autoloads-in-progress (delete! n autoloads-in-progress))))) |
| 2790 | |
| 2791 | \f |
| 2792 | |
| 2793 | ;;; {Run-time options} |
| 2794 | ;;; |
| 2795 | |
| 2796 | (define-syntax define-option-interface |
| 2797 | (syntax-rules () |
| 2798 | ((_ (interface (options enable disable) (option-set!))) |
| 2799 | (begin |
| 2800 | (define options |
| 2801 | (case-lambda |
| 2802 | (() (interface)) |
| 2803 | ((arg) |
| 2804 | (if (list? arg) |
| 2805 | (begin (interface arg) (interface)) |
| 2806 | (for-each |
| 2807 | (lambda (option) |
| 2808 | (apply (lambda (name value documentation) |
| 2809 | (display name) |
| 2810 | (if (< (string-length (symbol->string name)) 8) |
| 2811 | (display #\tab)) |
| 2812 | (display #\tab) |
| 2813 | (display value) |
| 2814 | (display #\tab) |
| 2815 | (display documentation) |
| 2816 | (newline)) |
| 2817 | option)) |
| 2818 | (interface #t)))))) |
| 2819 | (define (enable . flags) |
| 2820 | (interface (append flags (interface))) |
| 2821 | (interface)) |
| 2822 | (define (disable . flags) |
| 2823 | (let ((options (interface))) |
| 2824 | (for-each (lambda (flag) (set! options (delq! flag options))) |
| 2825 | flags) |
| 2826 | (interface options) |
| 2827 | (interface))) |
| 2828 | (define-syntax-rule (option-set! opt val) |
| 2829 | (eval-when (eval load compile expand) |
| 2830 | (options (append (options) (list 'opt val))))))))) |
| 2831 | |
| 2832 | (define-option-interface |
| 2833 | (debug-options-interface |
| 2834 | (debug-options debug-enable debug-disable) |
| 2835 | (debug-set!))) |
| 2836 | |
| 2837 | (define-option-interface |
| 2838 | (read-options-interface |
| 2839 | (read-options read-enable read-disable) |
| 2840 | (read-set!))) |
| 2841 | |
| 2842 | (define-option-interface |
| 2843 | (print-options-interface |
| 2844 | (print-options print-enable print-disable) |
| 2845 | (print-set!))) |
| 2846 | |
| 2847 | \f |
| 2848 | |
| 2849 | ;;; {The Unspecified Value} |
| 2850 | ;;; |
| 2851 | ;;; Currently Guile represents unspecified values via one particular value, |
| 2852 | ;;; which may be obtained by evaluating (if #f #f). It would be nice in the |
| 2853 | ;;; future if we could replace this with a return of 0 values, though. |
| 2854 | ;;; |
| 2855 | |
| 2856 | (define-syntax *unspecified* |
| 2857 | (identifier-syntax (if #f #f))) |
| 2858 | |
| 2859 | (define (unspecified? v) (eq? v *unspecified*)) |
| 2860 | |
| 2861 | |
| 2862 | \f |
| 2863 | |
| 2864 | ;;; {Parameters} |
| 2865 | ;;; |
| 2866 | |
| 2867 | (define <parameter> |
| 2868 | ;; Three fields: the procedure itself, the fluid, and the converter. |
| 2869 | (make-struct <applicable-struct-vtable> 0 'pwprpr)) |
| 2870 | (set-struct-vtable-name! <parameter> '<parameter>) |
| 2871 | |
| 2872 | (define* (make-parameter init #:optional (conv (lambda (x) x))) |
| 2873 | (let ((fluid (make-fluid (conv init)))) |
| 2874 | (make-struct <parameter> 0 |
| 2875 | (case-lambda |
| 2876 | (() (fluid-ref fluid)) |
| 2877 | ((x) (let ((prev (fluid-ref fluid))) |
| 2878 | (fluid-set! fluid (conv x)) |
| 2879 | prev))) |
| 2880 | fluid conv))) |
| 2881 | |
| 2882 | (define (parameter? x) |
| 2883 | (and (struct? x) (eq? (struct-vtable x) <parameter>))) |
| 2884 | |
| 2885 | (define (parameter-fluid p) |
| 2886 | (if (parameter? p) |
| 2887 | (struct-ref p 1) |
| 2888 | (scm-error 'wrong-type-arg "parameter-fluid" |
| 2889 | "Not a parameter: ~S" (list p) #f))) |
| 2890 | |
| 2891 | (define (parameter-converter p) |
| 2892 | (if (parameter? p) |
| 2893 | (struct-ref p 2) |
| 2894 | (scm-error 'wrong-type-arg "parameter-fluid" |
| 2895 | "Not a parameter: ~S" (list p) #f))) |
| 2896 | |
| 2897 | (define-syntax parameterize |
| 2898 | (lambda (x) |
| 2899 | (syntax-case x () |
| 2900 | ((_ ((param value) ...) body body* ...) |
| 2901 | (with-syntax (((p ...) (generate-temporaries #'(param ...)))) |
| 2902 | #'(let ((p param) ...) |
| 2903 | (if (not (parameter? p)) |
| 2904 | (scm-error 'wrong-type-arg "parameterize" |
| 2905 | "Not a parameter: ~S" (list p) #f)) |
| 2906 | ... |
| 2907 | (with-fluids (((struct-ref p 1) ((struct-ref p 2) value)) |
| 2908 | ...) |
| 2909 | body body* ...))))))) |
| 2910 | |
| 2911 | \f |
| 2912 | ;;; |
| 2913 | ;;; Warnings. |
| 2914 | ;;; |
| 2915 | |
| 2916 | (define current-warning-port |
| 2917 | (make-parameter (current-error-port) |
| 2918 | (lambda (x) |
| 2919 | (if (output-port? x) |
| 2920 | x |
| 2921 | (error "expected an output port" x))))) |
| 2922 | |
| 2923 | |
| 2924 | \f |
| 2925 | |
| 2926 | ;;; {Running Repls} |
| 2927 | ;;; |
| 2928 | |
| 2929 | (define *repl-stack* (make-fluid '())) |
| 2930 | |
| 2931 | ;; Programs can call `batch-mode?' to see if they are running as part of a |
| 2932 | ;; script or if they are running interactively. REPL implementations ensure that |
| 2933 | ;; `batch-mode?' returns #f during their extent. |
| 2934 | ;; |
| 2935 | (define (batch-mode?) |
| 2936 | (null? (fluid-ref *repl-stack*))) |
| 2937 | |
| 2938 | ;; Programs can re-enter batch mode, for example after a fork, by calling |
| 2939 | ;; `ensure-batch-mode!'. It's not a great interface, though; it would be better |
| 2940 | ;; to abort to the outermost prompt, and call a thunk there. |
| 2941 | ;; |
| 2942 | (define (ensure-batch-mode!) |
| 2943 | (set! batch-mode? (lambda () #t))) |
| 2944 | |
| 2945 | (define (quit . args) |
| 2946 | (apply throw 'quit args)) |
| 2947 | |
| 2948 | (define exit quit) |
| 2949 | |
| 2950 | (define (gc-run-time) |
| 2951 | (cdr (assq 'gc-time-taken (gc-stats)))) |
| 2952 | |
| 2953 | (define abort-hook (make-hook)) |
| 2954 | (define before-error-hook (make-hook)) |
| 2955 | (define after-error-hook (make-hook)) |
| 2956 | (define before-backtrace-hook (make-hook)) |
| 2957 | (define after-backtrace-hook (make-hook)) |
| 2958 | |
| 2959 | (define before-read-hook (make-hook)) |
| 2960 | (define after-read-hook (make-hook)) |
| 2961 | (define before-eval-hook (make-hook 1)) |
| 2962 | (define after-eval-hook (make-hook 1)) |
| 2963 | (define before-print-hook (make-hook 1)) |
| 2964 | (define after-print-hook (make-hook 1)) |
| 2965 | |
| 2966 | ;;; This hook is run at the very end of an interactive session. |
| 2967 | ;;; |
| 2968 | (define exit-hook (make-hook)) |
| 2969 | |
| 2970 | ;;; The default repl-reader function. We may override this if we've |
| 2971 | ;;; the readline library. |
| 2972 | (define repl-reader |
| 2973 | (lambda* (prompt #:optional (reader (fluid-ref current-reader))) |
| 2974 | (if (not (char-ready?)) |
| 2975 | (begin |
| 2976 | (display (if (string? prompt) prompt (prompt))) |
| 2977 | ;; An interesting situation. The printer resets the column to |
| 2978 | ;; 0 by printing a newline, but we then advance it by printing |
| 2979 | ;; the prompt. However the port-column of the output port |
| 2980 | ;; does not typically correspond with the actual column on the |
| 2981 | ;; screen, because the input is is echoed back! Since the |
| 2982 | ;; input is line-buffered and thus ends with a newline, the |
| 2983 | ;; output will really start on column zero. So, here we zero |
| 2984 | ;; it out. See bug 9664. |
| 2985 | ;; |
| 2986 | ;; Note that for similar reasons, the output-line will not |
| 2987 | ;; reflect the actual line on the screen. But given the |
| 2988 | ;; possibility of multiline input, the fix is not as |
| 2989 | ;; straightforward, so we don't bother. |
| 2990 | ;; |
| 2991 | ;; Also note that the readline implementation papers over |
| 2992 | ;; these concerns, because it's readline itself printing the |
| 2993 | ;; prompt, and not Guile. |
| 2994 | (set-port-column! (current-output-port) 0))) |
| 2995 | (force-output) |
| 2996 | (run-hook before-read-hook) |
| 2997 | ((or reader read) (current-input-port)))) |
| 2998 | |
| 2999 | |
| 3000 | \f |
| 3001 | |
| 3002 | ;;; {IOTA functions: generating lists of numbers} |
| 3003 | ;;; |
| 3004 | |
| 3005 | (define (iota n) |
| 3006 | (let loop ((count (1- n)) (result '())) |
| 3007 | (if (< count 0) result |
| 3008 | (loop (1- count) (cons count result))))) |
| 3009 | |
| 3010 | \f |
| 3011 | |
| 3012 | ;;; {While} |
| 3013 | ;;; |
| 3014 | ;;; with `continue' and `break'. |
| 3015 | ;;; |
| 3016 | |
| 3017 | ;; The inliner will remove the prompts at compile-time if it finds that |
| 3018 | ;; `continue' or `break' are not used. |
| 3019 | ;; |
| 3020 | (define-syntax while |
| 3021 | (lambda (x) |
| 3022 | (syntax-case x () |
| 3023 | ((while cond body ...) |
| 3024 | #`(let ((break-tag (make-prompt-tag "break")) |
| 3025 | (continue-tag (make-prompt-tag "continue"))) |
| 3026 | (call-with-prompt |
| 3027 | break-tag |
| 3028 | (lambda () |
| 3029 | (define-syntax #,(datum->syntax #'while 'break) |
| 3030 | (lambda (x) |
| 3031 | (syntax-case x () |
| 3032 | ((_ arg (... ...)) |
| 3033 | #'(abort-to-prompt break-tag arg (... ...))) |
| 3034 | (_ |
| 3035 | #'(lambda args |
| 3036 | (apply abort-to-prompt break-tag args)))))) |
| 3037 | (let lp () |
| 3038 | (call-with-prompt |
| 3039 | continue-tag |
| 3040 | (lambda () |
| 3041 | (define-syntax #,(datum->syntax #'while 'continue) |
| 3042 | (lambda (x) |
| 3043 | (syntax-case x () |
| 3044 | ((_) |
| 3045 | #'(abort-to-prompt continue-tag)) |
| 3046 | ((_ . args) |
| 3047 | (syntax-violation 'continue "too many arguments" x)) |
| 3048 | (_ |
| 3049 | #'(lambda () |
| 3050 | (abort-to-prompt continue-tag)))))) |
| 3051 | (do () ((not cond) #f) body ...)) |
| 3052 | (lambda (k) (lp))))) |
| 3053 | (lambda (k . args) |
| 3054 | (if (null? args) |
| 3055 | #t |
| 3056 | (apply values args))))))))) |
| 3057 | |
| 3058 | |
| 3059 | \f |
| 3060 | |
| 3061 | ;;; {Module System Macros} |
| 3062 | ;;; |
| 3063 | |
| 3064 | ;; Return a list of expressions that evaluate to the appropriate |
| 3065 | ;; arguments for resolve-interface according to SPEC. |
| 3066 | |
| 3067 | (eval-when (compile) |
| 3068 | (if (memq 'prefix (read-options)) |
| 3069 | (error "boot-9 must be compiled with #:kw, not :kw"))) |
| 3070 | |
| 3071 | (define (keyword-like-symbol->keyword sym) |
| 3072 | (symbol->keyword (string->symbol (substring (symbol->string sym) 1)))) |
| 3073 | |
| 3074 | (define-syntax define-module |
| 3075 | (lambda (x) |
| 3076 | (define (keyword-like? stx) |
| 3077 | (let ((dat (syntax->datum stx))) |
| 3078 | (and (symbol? dat) |
| 3079 | (eqv? (string-ref (symbol->string dat) 0) #\:)))) |
| 3080 | (define (->keyword sym) |
| 3081 | (symbol->keyword (string->symbol (substring (symbol->string sym) 1)))) |
| 3082 | |
| 3083 | (define (parse-iface args) |
| 3084 | (let loop ((in args) (out '())) |
| 3085 | (syntax-case in () |
| 3086 | (() (reverse! out)) |
| 3087 | ;; The user wanted #:foo, but wrote :foo. Fix it. |
| 3088 | ((sym . in) (keyword-like? #'sym) |
| 3089 | (loop #`(#,(->keyword (syntax->datum #'sym)) . in) out)) |
| 3090 | ((kw . in) (not (keyword? (syntax->datum #'kw))) |
| 3091 | (syntax-violation 'define-module "expected keyword arg" x #'kw)) |
| 3092 | ((#:renamer renamer . in) |
| 3093 | (loop #'in (cons* #',renamer #:renamer out))) |
| 3094 | ((kw val . in) |
| 3095 | (loop #'in (cons* #'val #'kw out)))))) |
| 3096 | |
| 3097 | (define (parse args imp exp rex rep aut) |
| 3098 | ;; Just quote everything except #:use-module and #:use-syntax. We |
| 3099 | ;; need to know about all arguments regardless since we want to turn |
| 3100 | ;; symbols that look like keywords into real keywords, and the |
| 3101 | ;; keyword args in a define-module form are not regular |
| 3102 | ;; (i.e. no-backtrace doesn't take a value). |
| 3103 | (syntax-case args () |
| 3104 | (() |
| 3105 | (let ((imp (if (null? imp) '() #`(#:imports `#,imp))) |
| 3106 | (exp (if (null? exp) '() #`(#:exports '#,exp))) |
| 3107 | (rex (if (null? rex) '() #`(#:re-exports '#,rex))) |
| 3108 | (rep (if (null? rep) '() #`(#:replacements '#,rep))) |
| 3109 | (aut (if (null? aut) '() #`(#:autoloads '#,aut)))) |
| 3110 | #`(#,@imp #,@exp #,@rex #,@rep #,@aut))) |
| 3111 | ;; The user wanted #:foo, but wrote :foo. Fix it. |
| 3112 | ((sym . args) (keyword-like? #'sym) |
| 3113 | (parse #`(#,(->keyword (syntax->datum #'sym)) . args) |
| 3114 | imp exp rex rep aut)) |
| 3115 | ((kw . args) (not (keyword? (syntax->datum #'kw))) |
| 3116 | (syntax-violation 'define-module "expected keyword arg" x #'kw)) |
| 3117 | ((#:no-backtrace . args) |
| 3118 | ;; Ignore this one. |
| 3119 | (parse #'args imp exp rex rep aut)) |
| 3120 | ((#:pure . args) |
| 3121 | #`(#:pure #t . #,(parse #'args imp exp rex rep aut))) |
| 3122 | ((kw) |
| 3123 | (syntax-violation 'define-module "keyword arg without value" x #'kw)) |
| 3124 | ((#:version (v ...) . args) |
| 3125 | #`(#:version '(v ...) . #,(parse #'args imp exp rex rep aut))) |
| 3126 | ((#:duplicates (d ...) . args) |
| 3127 | #`(#:duplicates '(d ...) . #,(parse #'args imp exp rex rep aut))) |
| 3128 | ((#:filename f . args) |
| 3129 | #`(#:filename 'f . #,(parse #'args imp exp rex rep aut))) |
| 3130 | ((#:use-module (name name* ...) . args) |
| 3131 | (and (and-map symbol? (syntax->datum #'(name name* ...)))) |
| 3132 | (parse #'args #`(#,@imp ((name name* ...))) exp rex rep aut)) |
| 3133 | ((#:use-syntax (name name* ...) . args) |
| 3134 | (and (and-map symbol? (syntax->datum #'(name name* ...)))) |
| 3135 | #`(#:transformer '(name name* ...) |
| 3136 | . #,(parse #'args #`(#,@imp ((name name* ...))) exp rex rep aut))) |
| 3137 | ((#:use-module ((name name* ...) arg ...) . args) |
| 3138 | (and (and-map symbol? (syntax->datum #'(name name* ...)))) |
| 3139 | (parse #'args |
| 3140 | #`(#,@imp ((name name* ...) #,@(parse-iface #'(arg ...)))) |
| 3141 | exp rex rep aut)) |
| 3142 | ((#:export (ex ...) . args) |
| 3143 | (parse #'args imp #`(#,@exp ex ...) rex rep aut)) |
| 3144 | ((#:export-syntax (ex ...) . args) |
| 3145 | (parse #'args imp #`(#,@exp ex ...) rex rep aut)) |
| 3146 | ((#:re-export (re ...) . args) |
| 3147 | (parse #'args imp exp #`(#,@rex re ...) rep aut)) |
| 3148 | ((#:re-export-syntax (re ...) . args) |
| 3149 | (parse #'args imp exp #`(#,@rex re ...) rep aut)) |
| 3150 | ((#:replace (r ...) . args) |
| 3151 | (parse #'args imp exp rex #`(#,@rep r ...) aut)) |
| 3152 | ((#:replace-syntax (r ...) . args) |
| 3153 | (parse #'args imp exp rex #`(#,@rep r ...) aut)) |
| 3154 | ((#:autoload name bindings . args) |
| 3155 | (parse #'args imp exp rex rep #`(#,@aut name bindings))) |
| 3156 | ((kw val . args) |
| 3157 | (syntax-violation 'define-module "unknown keyword or bad argument" |
| 3158 | #'kw #'val)))) |
| 3159 | |
| 3160 | (syntax-case x () |
| 3161 | ((_ (name name* ...) arg ...) |
| 3162 | (and-map symbol? (syntax->datum #'(name name* ...))) |
| 3163 | (with-syntax (((quoted-arg ...) |
| 3164 | (parse #'(arg ...) '() '() '() '() '())) |
| 3165 | ;; Ideally the filename is either a string or #f; |
| 3166 | ;; this hack is to work around a case in which |
| 3167 | ;; port-filename returns a symbol (`socket') for |
| 3168 | ;; sockets. |
| 3169 | (filename (let ((f (assq-ref (or (syntax-source x) '()) |
| 3170 | 'filename))) |
| 3171 | (and (string? f) f)))) |
| 3172 | #'(eval-when (eval load compile expand) |
| 3173 | (let ((m (define-module* '(name name* ...) |
| 3174 | #:filename filename quoted-arg ...))) |
| 3175 | (set-current-module m) |
| 3176 | m))))))) |
| 3177 | |
| 3178 | ;; The guts of the use-modules macro. Add the interfaces of the named |
| 3179 | ;; modules to the use-list of the current module, in order. |
| 3180 | |
| 3181 | ;; This function is called by "modules.c". If you change it, be sure |
| 3182 | ;; to change scm_c_use_module as well. |
| 3183 | |
| 3184 | (define (process-use-modules module-interface-args) |
| 3185 | (let ((interfaces (map (lambda (mif-args) |
| 3186 | (or (apply resolve-interface mif-args) |
| 3187 | (error "no such module" mif-args))) |
| 3188 | module-interface-args))) |
| 3189 | (call-with-deferred-observers |
| 3190 | (lambda () |
| 3191 | (module-use-interfaces! (current-module) interfaces))))) |
| 3192 | |
| 3193 | (define-syntax use-modules |
| 3194 | (lambda (x) |
| 3195 | (define (keyword-like? stx) |
| 3196 | (let ((dat (syntax->datum stx))) |
| 3197 | (and (symbol? dat) |
| 3198 | (eqv? (string-ref (symbol->string dat) 0) #\:)))) |
| 3199 | (define (->keyword sym) |
| 3200 | (symbol->keyword (string->symbol (substring (symbol->string sym) 1)))) |
| 3201 | |
| 3202 | (define (quotify-iface args) |
| 3203 | (let loop ((in args) (out '())) |
| 3204 | (syntax-case in () |
| 3205 | (() (reverse! out)) |
| 3206 | ;; The user wanted #:foo, but wrote :foo. Fix it. |
| 3207 | ((sym . in) (keyword-like? #'sym) |
| 3208 | (loop #`(#,(->keyword (syntax->datum #'sym)) . in) out)) |
| 3209 | ((kw . in) (not (keyword? (syntax->datum #'kw))) |
| 3210 | (syntax-violation 'define-module "expected keyword arg" x #'kw)) |
| 3211 | ((#:renamer renamer . in) |
| 3212 | (loop #'in (cons* #'renamer #:renamer out))) |
| 3213 | ((kw val . in) |
| 3214 | (loop #'in (cons* #''val #'kw out)))))) |
| 3215 | |
| 3216 | (define (quotify specs) |
| 3217 | (let lp ((in specs) (out '())) |
| 3218 | (syntax-case in () |
| 3219 | (() (reverse out)) |
| 3220 | (((name name* ...) . in) |
| 3221 | (and-map symbol? (syntax->datum #'(name name* ...))) |
| 3222 | (lp #'in (cons #''((name name* ...)) out))) |
| 3223 | ((((name name* ...) arg ...) . in) |
| 3224 | (and-map symbol? (syntax->datum #'(name name* ...))) |
| 3225 | (with-syntax (((quoted-arg ...) (quotify-iface #'(arg ...)))) |
| 3226 | (lp #'in (cons #`(list '(name name* ...) quoted-arg ...) |
| 3227 | out))))))) |
| 3228 | |
| 3229 | (syntax-case x () |
| 3230 | ((_ spec ...) |
| 3231 | (with-syntax (((quoted-args ...) (quotify #'(spec ...)))) |
| 3232 | #'(eval-when (eval load compile expand) |
| 3233 | (process-use-modules (list quoted-args ...)) |
| 3234 | *unspecified*)))))) |
| 3235 | |
| 3236 | (define-syntax-rule (use-syntax spec ...) |
| 3237 | (begin |
| 3238 | (eval-when (eval load compile expand) |
| 3239 | (issue-deprecation-warning |
| 3240 | "`use-syntax' is deprecated. Please contact guile-devel for more info.")) |
| 3241 | (use-modules spec ...))) |
| 3242 | |
| 3243 | (include-from-path "ice-9/r6rs-libraries") |
| 3244 | |
| 3245 | (define-syntax-rule (define-private foo bar) |
| 3246 | (define foo bar)) |
| 3247 | |
| 3248 | (define-syntax define-public |
| 3249 | (syntax-rules () |
| 3250 | ((_ (name . args) . body) |
| 3251 | (define-public name (lambda args . body))) |
| 3252 | ((_ name val) |
| 3253 | (begin |
| 3254 | (define name val) |
| 3255 | (export name))))) |
| 3256 | |
| 3257 | (define-syntax-rule (defmacro-public name args body ...) |
| 3258 | (begin |
| 3259 | (defmacro name args body ...) |
| 3260 | (export-syntax name))) |
| 3261 | |
| 3262 | ;; And now for the most important macro. |
| 3263 | (define-syntax-rule (λ formals body ...) |
| 3264 | (lambda formals body ...)) |
| 3265 | |
| 3266 | \f |
| 3267 | ;; Export a local variable |
| 3268 | |
| 3269 | ;; This function is called from "modules.c". If you change it, be |
| 3270 | ;; sure to update "modules.c" as well. |
| 3271 | |
| 3272 | (define (module-export! m names) |
| 3273 | (let ((public-i (module-public-interface m))) |
| 3274 | (for-each (lambda (name) |
| 3275 | (let* ((internal-name (if (pair? name) (car name) name)) |
| 3276 | (external-name (if (pair? name) (cdr name) name)) |
| 3277 | (var (module-ensure-local-variable! m internal-name))) |
| 3278 | (module-add! public-i external-name var))) |
| 3279 | names))) |
| 3280 | |
| 3281 | (define (module-replace! m names) |
| 3282 | (let ((public-i (module-public-interface m))) |
| 3283 | (for-each (lambda (name) |
| 3284 | (let* ((internal-name (if (pair? name) (car name) name)) |
| 3285 | (external-name (if (pair? name) (cdr name) name)) |
| 3286 | (var (module-ensure-local-variable! m internal-name))) |
| 3287 | ;; FIXME: use a bit on variables instead of object |
| 3288 | ;; properties. |
| 3289 | (set-object-property! var 'replace #t) |
| 3290 | (module-add! public-i external-name var))) |
| 3291 | names))) |
| 3292 | |
| 3293 | ;; Export all local variables from a module |
| 3294 | ;; |
| 3295 | (define (module-export-all! mod) |
| 3296 | (define (fresh-interface!) |
| 3297 | (let ((iface (make-module))) |
| 3298 | (set-module-name! iface (module-name mod)) |
| 3299 | (set-module-version! iface (module-version mod)) |
| 3300 | (set-module-kind! iface 'interface) |
| 3301 | (set-module-public-interface! mod iface) |
| 3302 | iface)) |
| 3303 | (let ((iface (or (module-public-interface mod) |
| 3304 | (fresh-interface!)))) |
| 3305 | (set-module-obarray! iface (module-obarray mod)))) |
| 3306 | |
| 3307 | ;; Re-export a imported variable |
| 3308 | ;; |
| 3309 | (define (module-re-export! m names) |
| 3310 | (let ((public-i (module-public-interface m))) |
| 3311 | (for-each (lambda (name) |
| 3312 | (let* ((internal-name (if (pair? name) (car name) name)) |
| 3313 | (external-name (if (pair? name) (cdr name) name)) |
| 3314 | (var (module-variable m internal-name))) |
| 3315 | (cond ((not var) |
| 3316 | (error "Undefined variable:" internal-name)) |
| 3317 | ((eq? var (module-local-variable m internal-name)) |
| 3318 | (error "re-exporting local variable:" internal-name)) |
| 3319 | (else |
| 3320 | (module-add! public-i external-name var))))) |
| 3321 | names))) |
| 3322 | |
| 3323 | (define-syntax-rule (export name ...) |
| 3324 | (eval-when (eval load compile expand) |
| 3325 | (call-with-deferred-observers |
| 3326 | (lambda () |
| 3327 | (module-export! (current-module) '(name ...)))))) |
| 3328 | |
| 3329 | (define-syntax-rule (re-export name ...) |
| 3330 | (eval-when (eval load compile expand) |
| 3331 | (call-with-deferred-observers |
| 3332 | (lambda () |
| 3333 | (module-re-export! (current-module) '(name ...)))))) |
| 3334 | |
| 3335 | (define-syntax-rule (export! name ...) |
| 3336 | (eval-when (eval load compile expand) |
| 3337 | (call-with-deferred-observers |
| 3338 | (lambda () |
| 3339 | (module-replace! (current-module) '(name ...)))))) |
| 3340 | |
| 3341 | (define-syntax-rule (export-syntax name ...) |
| 3342 | (export name ...)) |
| 3343 | |
| 3344 | (define-syntax-rule (re-export-syntax name ...) |
| 3345 | (re-export name ...)) |
| 3346 | |
| 3347 | \f |
| 3348 | |
| 3349 | ;;; {Parameters} |
| 3350 | ;;; |
| 3351 | |
| 3352 | (define* (make-mutable-parameter init #:optional (converter identity)) |
| 3353 | (let ((fluid (make-fluid (converter init)))) |
| 3354 | (case-lambda |
| 3355 | (() (fluid-ref fluid)) |
| 3356 | ((val) (fluid-set! fluid (converter val)))))) |
| 3357 | |
| 3358 | |
| 3359 | \f |
| 3360 | |
| 3361 | ;;; {Handling of duplicate imported bindings} |
| 3362 | ;;; |
| 3363 | |
| 3364 | ;; Duplicate handlers take the following arguments: |
| 3365 | ;; |
| 3366 | ;; module importing module |
| 3367 | ;; name conflicting name |
| 3368 | ;; int1 old interface where name occurs |
| 3369 | ;; val1 value of binding in old interface |
| 3370 | ;; int2 new interface where name occurs |
| 3371 | ;; val2 value of binding in new interface |
| 3372 | ;; var previous resolution or #f |
| 3373 | ;; val value of previous resolution |
| 3374 | ;; |
| 3375 | ;; A duplicate handler can take three alternative actions: |
| 3376 | ;; |
| 3377 | ;; 1. return #f => leave responsibility to next handler |
| 3378 | ;; 2. exit with an error |
| 3379 | ;; 3. return a variable resolving the conflict |
| 3380 | ;; |
| 3381 | |
| 3382 | (define duplicate-handlers |
| 3383 | (let ((m (make-module 7))) |
| 3384 | |
| 3385 | (define (check module name int1 val1 int2 val2 var val) |
| 3386 | (scm-error 'misc-error |
| 3387 | #f |
| 3388 | "~A: `~A' imported from both ~A and ~A" |
| 3389 | (list (module-name module) |
| 3390 | name |
| 3391 | (module-name int1) |
| 3392 | (module-name int2)) |
| 3393 | #f)) |
| 3394 | |
| 3395 | (define (warn module name int1 val1 int2 val2 var val) |
| 3396 | (format (current-warning-port) |
| 3397 | "WARNING: ~A: `~A' imported from both ~A and ~A\n" |
| 3398 | (module-name module) |
| 3399 | name |
| 3400 | (module-name int1) |
| 3401 | (module-name int2)) |
| 3402 | #f) |
| 3403 | |
| 3404 | (define (replace module name int1 val1 int2 val2 var val) |
| 3405 | (let ((old (or (and var (object-property var 'replace) var) |
| 3406 | (module-variable int1 name))) |
| 3407 | (new (module-variable int2 name))) |
| 3408 | (if (object-property old 'replace) |
| 3409 | (and (or (eq? old new) |
| 3410 | (not (object-property new 'replace))) |
| 3411 | old) |
| 3412 | (and (object-property new 'replace) |
| 3413 | new)))) |
| 3414 | |
| 3415 | (define (warn-override-core module name int1 val1 int2 val2 var val) |
| 3416 | (and (eq? int1 the-scm-module) |
| 3417 | (begin |
| 3418 | (format (current-warning-port) |
| 3419 | "WARNING: ~A: imported module ~A overrides core binding `~A'\n" |
| 3420 | (module-name module) |
| 3421 | (module-name int2) |
| 3422 | name) |
| 3423 | (module-local-variable int2 name)))) |
| 3424 | |
| 3425 | (define (first module name int1 val1 int2 val2 var val) |
| 3426 | (or var (module-local-variable int1 name))) |
| 3427 | |
| 3428 | (define (last module name int1 val1 int2 val2 var val) |
| 3429 | (module-local-variable int2 name)) |
| 3430 | |
| 3431 | (define (noop module name int1 val1 int2 val2 var val) |
| 3432 | #f) |
| 3433 | |
| 3434 | (set-module-name! m 'duplicate-handlers) |
| 3435 | (set-module-kind! m 'interface) |
| 3436 | (module-define! m 'check check) |
| 3437 | (module-define! m 'warn warn) |
| 3438 | (module-define! m 'replace replace) |
| 3439 | (module-define! m 'warn-override-core warn-override-core) |
| 3440 | (module-define! m 'first first) |
| 3441 | (module-define! m 'last last) |
| 3442 | (module-define! m 'merge-generics noop) |
| 3443 | (module-define! m 'merge-accessors noop) |
| 3444 | m)) |
| 3445 | |
| 3446 | (define (lookup-duplicates-handlers handler-names) |
| 3447 | (and handler-names |
| 3448 | (map (lambda (handler-name) |
| 3449 | (or (module-symbol-local-binding |
| 3450 | duplicate-handlers handler-name #f) |
| 3451 | (error "invalid duplicate handler name:" |
| 3452 | handler-name))) |
| 3453 | (if (list? handler-names) |
| 3454 | handler-names |
| 3455 | (list handler-names))))) |
| 3456 | |
| 3457 | (define default-duplicate-binding-procedures |
| 3458 | (make-mutable-parameter #f)) |
| 3459 | |
| 3460 | (define default-duplicate-binding-handler |
| 3461 | (make-mutable-parameter '(replace warn-override-core warn last) |
| 3462 | (lambda (handler-names) |
| 3463 | (default-duplicate-binding-procedures |
| 3464 | (lookup-duplicates-handlers handler-names)) |
| 3465 | handler-names))) |
| 3466 | |
| 3467 | \f |
| 3468 | |
| 3469 | ;;; {`load'.} |
| 3470 | ;;; |
| 3471 | ;;; Load is tricky when combined with relative paths, compilation, and |
| 3472 | ;;; the filesystem. If a path is relative, what is it relative to? The |
| 3473 | ;;; path of the source file at the time it was compiled? The path of |
| 3474 | ;;; the compiled file? What if both or either were installed? And how |
| 3475 | ;;; do you get that information? Tricky, I say. |
| 3476 | ;;; |
| 3477 | ;;; To get around all of this, we're going to do something nasty, and |
| 3478 | ;;; turn `load' into a macro. That way it can know the path of the |
| 3479 | ;;; source file with respect to which it was invoked, so it can resolve |
| 3480 | ;;; relative paths with respect to the original source path. |
| 3481 | ;;; |
| 3482 | ;;; There is an exception, and that is that if the source file was in |
| 3483 | ;;; the load path when it was compiled, instead of looking up against |
| 3484 | ;;; the absolute source location, we load-from-path against the relative |
| 3485 | ;;; source location. |
| 3486 | ;;; |
| 3487 | |
| 3488 | (define %auto-compilation-options |
| 3489 | ;; Default `compile-file' option when auto-compiling. |
| 3490 | '(#:warnings (unbound-variable arity-mismatch format))) |
| 3491 | |
| 3492 | (define* (load-in-vicinity dir path #:optional reader) |
| 3493 | (define (canonical->suffix canon) |
| 3494 | (cond |
| 3495 | ((string-prefix? "/" canon) canon) |
| 3496 | ((and (> (string-length canon) 2) |
| 3497 | (eqv? (string-ref canon 1) #\:)) |
| 3498 | ;; Paths like C:... transform to /C... |
| 3499 | (string-append "/" (substring canon 0 1) (substring canon 2))) |
| 3500 | (else canon))) |
| 3501 | |
| 3502 | ;; Returns the .go file corresponding to `name'. Does not search load |
| 3503 | ;; paths, only the fallback path. If the .go file is missing or out of |
| 3504 | ;; date, and auto-compilation is enabled, will try auto-compilation, just |
| 3505 | ;; as primitive-load-path does internally. primitive-load is |
| 3506 | ;; unaffected. Returns #f if auto-compilation failed or was disabled. |
| 3507 | ;; |
| 3508 | ;; NB: Unless we need to compile the file, this function should not cause |
| 3509 | ;; (system base compile) to be loaded up. For that reason compiled-file-name |
| 3510 | ;; partially duplicates functionality from (system base compile). |
| 3511 | ;; |
| 3512 | (define (compiled-file-name canon-path) |
| 3513 | ;; FIXME: would probably be better just to append SHA1(canon-path) |
| 3514 | ;; to the %compile-fallback-path, to avoid deep directory stats. |
| 3515 | (and %compile-fallback-path |
| 3516 | (string-append |
| 3517 | %compile-fallback-path |
| 3518 | (canonical->suffix canon-path) |
| 3519 | (cond ((or (null? %load-compiled-extensions) |
| 3520 | (string-null? (car %load-compiled-extensions))) |
| 3521 | (warn "invalid %load-compiled-extensions" |
| 3522 | %load-compiled-extensions) |
| 3523 | ".go") |
| 3524 | (else (car %load-compiled-extensions)))))) |
| 3525 | |
| 3526 | (define (fresh-compiled-file-name name go-path) |
| 3527 | (catch #t |
| 3528 | (lambda () |
| 3529 | (let* ((scmstat (stat name)) |
| 3530 | (gostat (and (not %fresh-auto-compile) |
| 3531 | (stat go-path #f)))) |
| 3532 | (if (and gostat |
| 3533 | (or (> (stat:mtime gostat) (stat:mtime scmstat)) |
| 3534 | (and (= (stat:mtime gostat) (stat:mtime scmstat)) |
| 3535 | (>= (stat:mtimensec gostat) |
| 3536 | (stat:mtimensec scmstat))))) |
| 3537 | go-path |
| 3538 | (begin |
| 3539 | (if gostat |
| 3540 | (format (current-warning-port) |
| 3541 | ";;; note: source file ~a\n;;; newer than compiled ~a\n" |
| 3542 | name go-path)) |
| 3543 | (cond |
| 3544 | (%load-should-auto-compile |
| 3545 | (%warn-auto-compilation-enabled) |
| 3546 | (format (current-warning-port) ";;; compiling ~a\n" name) |
| 3547 | (let ((cfn |
| 3548 | ((module-ref |
| 3549 | (resolve-interface '(system base compile)) |
| 3550 | 'compile-file) |
| 3551 | name |
| 3552 | #:opts %auto-compilation-options |
| 3553 | #:env (current-module)))) |
| 3554 | (format (current-warning-port) ";;; compiled ~a\n" cfn) |
| 3555 | cfn)) |
| 3556 | (else #f)))))) |
| 3557 | (lambda (k . args) |
| 3558 | (format (current-warning-port) |
| 3559 | ";;; WARNING: compilation of ~a failed:\n" name) |
| 3560 | (for-each (lambda (s) |
| 3561 | (if (not (string-null? s)) |
| 3562 | (format (current-warning-port) ";;; ~a\n" s))) |
| 3563 | (string-split |
| 3564 | (call-with-output-string |
| 3565 | (lambda (port) (print-exception port #f k args))) |
| 3566 | #\newline)) |
| 3567 | #f))) |
| 3568 | |
| 3569 | (define (absolute-path? path) |
| 3570 | (string-prefix? "/" path)) |
| 3571 | |
| 3572 | (define (load-absolute abs-path) |
| 3573 | (let ((cfn (let ((canon (false-if-exception (canonicalize-path abs-path)))) |
| 3574 | (and canon |
| 3575 | (let ((go-path (compiled-file-name canon))) |
| 3576 | (and go-path |
| 3577 | (fresh-compiled-file-name abs-path go-path))))))) |
| 3578 | (if cfn |
| 3579 | (begin |
| 3580 | (if %load-hook |
| 3581 | (%load-hook abs-path)) |
| 3582 | (load-compiled cfn)) |
| 3583 | (start-stack 'load-stack |
| 3584 | (primitive-load abs-path))))) |
| 3585 | |
| 3586 | (save-module-excursion |
| 3587 | (lambda () |
| 3588 | (with-fluids ((current-reader reader) |
| 3589 | (%file-port-name-canonicalization 'relative)) |
| 3590 | (cond |
| 3591 | ((or (absolute-path? path)) |
| 3592 | (load-absolute path)) |
| 3593 | ((absolute-path? dir) |
| 3594 | (load-absolute (in-vicinity dir path))) |
| 3595 | (else |
| 3596 | (load-from-path (in-vicinity dir path)))))))) |
| 3597 | |
| 3598 | (define-syntax load |
| 3599 | (make-variable-transformer |
| 3600 | (lambda (x) |
| 3601 | (let* ((src (syntax-source x)) |
| 3602 | (file (and src (assq-ref src 'filename))) |
| 3603 | (dir (and (string? file) (dirname file)))) |
| 3604 | (syntax-case x () |
| 3605 | ((_ arg ...) |
| 3606 | #`(load-in-vicinity #,(or dir #'(getcwd)) arg ...)) |
| 3607 | (id |
| 3608 | (identifier? #'id) |
| 3609 | #`(lambda args |
| 3610 | (apply load-in-vicinity #,(or dir #'(getcwd)) args)))))))) |
| 3611 | |
| 3612 | \f |
| 3613 | |
| 3614 | ;;; {`cond-expand' for SRFI-0 support.} |
| 3615 | ;;; |
| 3616 | ;;; This syntactic form expands into different commands or |
| 3617 | ;;; definitions, depending on the features provided by the Scheme |
| 3618 | ;;; implementation. |
| 3619 | ;;; |
| 3620 | ;;; Syntax: |
| 3621 | ;;; |
| 3622 | ;;; <cond-expand> |
| 3623 | ;;; --> (cond-expand <cond-expand-clause>+) |
| 3624 | ;;; | (cond-expand <cond-expand-clause>* (else <command-or-definition>)) |
| 3625 | ;;; <cond-expand-clause> |
| 3626 | ;;; --> (<feature-requirement> <command-or-definition>*) |
| 3627 | ;;; <feature-requirement> |
| 3628 | ;;; --> <feature-identifier> |
| 3629 | ;;; | (and <feature-requirement>*) |
| 3630 | ;;; | (or <feature-requirement>*) |
| 3631 | ;;; | (not <feature-requirement>) |
| 3632 | ;;; <feature-identifier> |
| 3633 | ;;; --> <a symbol which is the name or alias of a SRFI> |
| 3634 | ;;; |
| 3635 | ;;; Additionally, this implementation provides the |
| 3636 | ;;; <feature-identifier>s `guile' and `r5rs', so that programs can |
| 3637 | ;;; determine the implementation type and the supported standard. |
| 3638 | ;;; |
| 3639 | ;;; Currently, the following feature identifiers are supported: |
| 3640 | ;;; |
| 3641 | ;;; guile r5rs srfi-0 srfi-4 srfi-6 srfi-13 srfi-14 srfi-55 srfi-61 |
| 3642 | ;;; |
| 3643 | ;;; Remember to update the features list when adding more SRFIs. |
| 3644 | ;;; |
| 3645 | |
| 3646 | (define %cond-expand-features |
| 3647 | ;; Adjust the above comment when changing this. |
| 3648 | '(guile |
| 3649 | guile-2 |
| 3650 | r5rs |
| 3651 | srfi-0 ;; cond-expand itself |
| 3652 | srfi-4 ;; homogenous numeric vectors |
| 3653 | srfi-6 ;; open-input-string etc, in the guile core |
| 3654 | srfi-13 ;; string library |
| 3655 | srfi-23 ;; `error` procedure |
| 3656 | srfi-14 ;; character sets |
| 3657 | srfi-55 ;; require-extension |
| 3658 | srfi-61 ;; general cond clause |
| 3659 | )) |
| 3660 | |
| 3661 | ;; This table maps module public interfaces to the list of features. |
| 3662 | ;; |
| 3663 | (define %cond-expand-table (make-hash-table 31)) |
| 3664 | |
| 3665 | ;; Add one or more features to the `cond-expand' feature list of the |
| 3666 | ;; module `module'. |
| 3667 | ;; |
| 3668 | (define (cond-expand-provide module features) |
| 3669 | (let ((mod (module-public-interface module))) |
| 3670 | (and mod |
| 3671 | (hashq-set! %cond-expand-table mod |
| 3672 | (append (hashq-ref %cond-expand-table mod '()) |
| 3673 | features))))) |
| 3674 | |
| 3675 | (define-syntax cond-expand |
| 3676 | (lambda (x) |
| 3677 | (define (module-has-feature? mod sym) |
| 3678 | (or-map (lambda (mod) |
| 3679 | (memq sym (hashq-ref %cond-expand-table mod '()))) |
| 3680 | (module-uses mod))) |
| 3681 | |
| 3682 | (define (condition-matches? condition) |
| 3683 | (syntax-case condition (and or not) |
| 3684 | ((and c ...) |
| 3685 | (and-map condition-matches? #'(c ...))) |
| 3686 | ((or c ...) |
| 3687 | (or-map condition-matches? #'(c ...))) |
| 3688 | ((not c) |
| 3689 | (if (condition-matches? #'c) #f #t)) |
| 3690 | (c |
| 3691 | (identifier? #'c) |
| 3692 | (let ((sym (syntax->datum #'c))) |
| 3693 | (if (memq sym %cond-expand-features) |
| 3694 | #t |
| 3695 | (module-has-feature? (current-module) sym)))))) |
| 3696 | |
| 3697 | (define (match clauses alternate) |
| 3698 | (syntax-case clauses () |
| 3699 | (((condition form ...) . rest) |
| 3700 | (if (condition-matches? #'condition) |
| 3701 | #'(begin form ...) |
| 3702 | (match #'rest alternate))) |
| 3703 | (() (alternate)))) |
| 3704 | |
| 3705 | (syntax-case x (else) |
| 3706 | ((_ clause ... (else form ...)) |
| 3707 | (match #'(clause ...) |
| 3708 | (lambda () |
| 3709 | #'(begin form ...)))) |
| 3710 | ((_ clause ...) |
| 3711 | (match #'(clause ...) |
| 3712 | (lambda () |
| 3713 | (syntax-violation 'cond-expand "unfulfilled cond-expand" x))))))) |
| 3714 | |
| 3715 | ;; This procedure gets called from the startup code with a list of |
| 3716 | ;; numbers, which are the numbers of the SRFIs to be loaded on startup. |
| 3717 | ;; |
| 3718 | (define (use-srfis srfis) |
| 3719 | (process-use-modules |
| 3720 | (map (lambda (num) |
| 3721 | (list (list 'srfi (string->symbol |
| 3722 | (string-append "srfi-" (number->string num)))))) |
| 3723 | srfis))) |
| 3724 | |
| 3725 | \f |
| 3726 | |
| 3727 | ;;; srfi-55: require-extension |
| 3728 | ;;; |
| 3729 | |
| 3730 | (define-syntax require-extension |
| 3731 | (lambda (x) |
| 3732 | (syntax-case x (srfi) |
| 3733 | ((_ (srfi n ...)) |
| 3734 | (and-map integer? (syntax->datum #'(n ...))) |
| 3735 | (with-syntax |
| 3736 | (((srfi-n ...) |
| 3737 | (map (lambda (n) |
| 3738 | (datum->syntax x (symbol-append 'srfi- n))) |
| 3739 | (map string->symbol |
| 3740 | (map number->string (syntax->datum #'(n ...))))))) |
| 3741 | #'(use-modules (srfi srfi-n) ...))) |
| 3742 | ((_ (type arg ...)) |
| 3743 | (identifier? #'type) |
| 3744 | (syntax-violation 'require-extension "Not a recognized extension type" |
| 3745 | x))))) |
| 3746 | |
| 3747 | \f |
| 3748 | ;;; Defining transparently inlinable procedures |
| 3749 | ;;; |
| 3750 | |
| 3751 | (define-syntax define-inlinable |
| 3752 | ;; Define a macro and a procedure such that direct calls are inlined, via |
| 3753 | ;; the macro expansion, whereas references in non-call contexts refer to |
| 3754 | ;; the procedure. Inspired by the `define-integrable' macro by Dybvig et al. |
| 3755 | (lambda (x) |
| 3756 | ;; Use a space in the prefix to avoid potential -Wunused-toplevel |
| 3757 | ;; warning |
| 3758 | (define prefix (string->symbol "% ")) |
| 3759 | (define (make-procedure-name name) |
| 3760 | (datum->syntax name |
| 3761 | (symbol-append prefix (syntax->datum name) |
| 3762 | '-procedure))) |
| 3763 | |
| 3764 | (syntax-case x () |
| 3765 | ((_ (name formals ...) body ...) |
| 3766 | (identifier? #'name) |
| 3767 | (with-syntax ((proc-name (make-procedure-name #'name)) |
| 3768 | ((args ...) (generate-temporaries #'(formals ...)))) |
| 3769 | #`(begin |
| 3770 | (define (proc-name formals ...) |
| 3771 | (fluid-let-syntax ((name (identifier-syntax proc-name))) |
| 3772 | body ...)) |
| 3773 | (define-syntax name |
| 3774 | (lambda (x) |
| 3775 | (syntax-case x () |
| 3776 | ((_ args ...) |
| 3777 | #'((fluid-let-syntax ((name (identifier-syntax proc-name))) |
| 3778 | (lambda (formals ...) |
| 3779 | body ...)) |
| 3780 | args ...)) |
| 3781 | (_ |
| 3782 | (identifier? x) |
| 3783 | #'proc-name)))))))))) |
| 3784 | |
| 3785 | \f |
| 3786 | |
| 3787 | (define using-readline? |
| 3788 | (let ((using-readline? (make-fluid))) |
| 3789 | (make-procedure-with-setter |
| 3790 | (lambda () (fluid-ref using-readline?)) |
| 3791 | (lambda (v) (fluid-set! using-readline? v))))) |
| 3792 | |
| 3793 | \f |
| 3794 | |
| 3795 | ;;; {Deprecated stuff} |
| 3796 | ;;; |
| 3797 | |
| 3798 | (begin-deprecated |
| 3799 | (module-use! the-scm-module (resolve-interface '(ice-9 deprecated)))) |
| 3800 | |
| 3801 | \f |
| 3802 | |
| 3803 | ;;; Place the user in the guile-user module. |
| 3804 | ;;; |
| 3805 | |
| 3806 | ;; FIXME: |
| 3807 | (module-use! the-scm-module (resolve-interface '(srfi srfi-4))) |
| 3808 | |
| 3809 | ;; Set filename to #f to prevent reload. |
| 3810 | (define-module (guile-user) |
| 3811 | #:autoload (system base compile) (compile compile-file) |
| 3812 | #:filename #f) |
| 3813 | |
| 3814 | ;; Remain in the `(guile)' module at compilation-time so that the |
| 3815 | ;; `-Wunused-toplevel' warning works as expected. |
| 3816 | (eval-when (compile) (set-current-module the-root-module)) |
| 3817 | |
| 3818 | ;;; boot-9.scm ends here |