| 1 | ;;; installed-scm-file |
| 2 | |
| 3 | ;;;; Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002, 2003 Free Software Foundation, Inc. |
| 4 | ;;;; |
| 5 | ;;;; This program is free software; you can redistribute it and/or modify |
| 6 | ;;;; it under the terms of the GNU General Public License as published by |
| 7 | ;;;; the Free Software Foundation; either version 2, or (at your option) |
| 8 | ;;;; any later version. |
| 9 | ;;;; |
| 10 | ;;;; This program is distributed in the hope that it will be useful, |
| 11 | ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | ;;;; GNU General Public License for more details. |
| 14 | ;;;; |
| 15 | ;;;; You should have received a copy of the GNU General Public License |
| 16 | ;;;; along with this software; see the file COPYING. If not, write to |
| 17 | ;;;; the Free Software Foundation, Inc., 59 Temple Place, Suite 330, |
| 18 | ;;;; Boston, MA 02111-1307 USA |
| 19 | ;;;; |
| 20 | ;;;; As a special exception, the Free Software Foundation gives permission |
| 21 | ;;;; for additional uses of the text contained in its release of GUILE. |
| 22 | ;;;; |
| 23 | ;;;; The exception is that, if you link the GUILE library with other files |
| 24 | ;;;; to produce an executable, this does not by itself cause the |
| 25 | ;;;; resulting executable to be covered by the GNU General Public License. |
| 26 | ;;;; Your use of that executable is in no way restricted on account of |
| 27 | ;;;; linking the GUILE library code into it. |
| 28 | ;;;; |
| 29 | ;;;; This exception does not however invalidate any other reasons why |
| 30 | ;;;; the executable file might be covered by the GNU General Public License. |
| 31 | ;;;; |
| 32 | ;;;; This exception applies only to the code released by the |
| 33 | ;;;; Free Software Foundation under the name GUILE. If you copy |
| 34 | ;;;; code from other Free Software Foundation releases into a copy of |
| 35 | ;;;; GUILE, as the General Public License permits, the exception does |
| 36 | ;;;; not apply to the code that you add in this way. To avoid misleading |
| 37 | ;;;; anyone as to the status of such modified files, you must delete |
| 38 | ;;;; this exception notice from them. |
| 39 | ;;;; |
| 40 | ;;;; If you write modifications of your own for GUILE, it is your choice |
| 41 | ;;;; whether to permit this exception to apply to your modifications. |
| 42 | ;;;; If you do not wish that, delete this exception notice. |
| 43 | ;;;; |
| 44 | \f |
| 45 | |
| 46 | ;;; Commentary: |
| 47 | |
| 48 | ;;; This file is the first thing loaded into Guile. It adds many mundane |
| 49 | ;;; definitions and a few that are interesting. |
| 50 | ;;; |
| 51 | ;;; The module system (hence the hierarchical namespace) are defined in this |
| 52 | ;;; file. |
| 53 | ;;; |
| 54 | |
| 55 | ;;; Code: |
| 56 | |
| 57 | \f |
| 58 | ;;; {Deprecation} |
| 59 | ;;; |
| 60 | |
| 61 | ;; We don't have macros here, but we do want to define |
| 62 | ;; `begin-deprecated' early. |
| 63 | |
| 64 | (define begin-deprecated |
| 65 | (procedure->memoizing-macro |
| 66 | (lambda (exp env) |
| 67 | (if (include-deprecated-features) |
| 68 | `(begin ,@(cdr exp)) |
| 69 | `#f)))) |
| 70 | |
| 71 | \f |
| 72 | ;;; {Features} |
| 73 | ;; |
| 74 | |
| 75 | (define (provide sym) |
| 76 | (if (not (memq sym *features*)) |
| 77 | (set! *features* (cons sym *features*)))) |
| 78 | |
| 79 | ;;; Return #t iff FEATURE is available to this Guile interpreter. |
| 80 | ;;; In SLIB, provided? also checks to see if the module is available. |
| 81 | ;;; We should do that too, but don't. |
| 82 | (define (provided? feature) |
| 83 | (and (memq feature *features*) #t)) |
| 84 | |
| 85 | (begin-deprecated |
| 86 | (define (feature? sym) |
| 87 | (issue-deprecation-warning |
| 88 | "`feature?' is deprecated. Use `provided?' instead.") |
| 89 | (provided? sym))) |
| 90 | |
| 91 | ;;; let format alias simple-format until the more complete version is loaded |
| 92 | (define format simple-format) |
| 93 | |
| 94 | \f |
| 95 | ;;; {R4RS compliance} |
| 96 | |
| 97 | (primitive-load-path "ice-9/r4rs.scm") |
| 98 | |
| 99 | \f |
| 100 | ;;; {Simple Debugging Tools} |
| 101 | ;; |
| 102 | |
| 103 | |
| 104 | ;; peek takes any number of arguments, writes them to the |
| 105 | ;; current ouput port, and returns the last argument. |
| 106 | ;; It is handy to wrap around an expression to look at |
| 107 | ;; a value each time is evaluated, e.g.: |
| 108 | ;; |
| 109 | ;; (+ 10 (troublesome-fn)) |
| 110 | ;; => (+ 10 (pk 'troublesome-fn-returned (troublesome-fn))) |
| 111 | ;; |
| 112 | |
| 113 | (define (peek . stuff) |
| 114 | (newline) |
| 115 | (display ";;; ") |
| 116 | (write stuff) |
| 117 | (newline) |
| 118 | (car (last-pair stuff))) |
| 119 | |
| 120 | (define pk peek) |
| 121 | |
| 122 | (define (warn . stuff) |
| 123 | (with-output-to-port (current-error-port) |
| 124 | (lambda () |
| 125 | (newline) |
| 126 | (display ";;; WARNING ") |
| 127 | (display stuff) |
| 128 | (newline) |
| 129 | (car (last-pair stuff))))) |
| 130 | |
| 131 | \f |
| 132 | ;;; {Trivial Functions} |
| 133 | ;;; |
| 134 | |
| 135 | (define (identity x) x) |
| 136 | (define (1+ n) (+ n 1)) |
| 137 | (define (1- n) (+ n -1)) |
| 138 | (define (and=> value procedure) (and value (procedure value))) |
| 139 | |
| 140 | ;;; apply-to-args is functionally redundant with apply and, worse, |
| 141 | ;;; is less general than apply since it only takes two arguments. |
| 142 | ;;; |
| 143 | ;;; On the other hand, apply-to-args is a syntacticly convenient way to |
| 144 | ;;; perform binding in many circumstances when the "let" family of |
| 145 | ;;; of forms don't cut it. E.g.: |
| 146 | ;;; |
| 147 | ;;; (apply-to-args (return-3d-mouse-coords) |
| 148 | ;;; (lambda (x y z) |
| 149 | ;;; ...)) |
| 150 | ;;; |
| 151 | |
| 152 | (define (apply-to-args args fn) (apply fn args)) |
| 153 | |
| 154 | \f |
| 155 | |
| 156 | ;;; {Integer Math} |
| 157 | ;;; |
| 158 | |
| 159 | (define (ipow-by-squaring x k acc proc) |
| 160 | (cond ((zero? k) acc) |
| 161 | ((= 1 k) (proc acc x)) |
| 162 | (else (ipow-by-squaring (proc x x) |
| 163 | (quotient k 2) |
| 164 | (if (even? k) acc (proc acc x)) |
| 165 | proc)))) |
| 166 | |
| 167 | \f |
| 168 | ;;; {Symbol Properties} |
| 169 | ;;; |
| 170 | |
| 171 | (define (symbol-property sym prop) |
| 172 | (let ((pair (assoc prop (symbol-pref sym)))) |
| 173 | (and pair (cdr pair)))) |
| 174 | |
| 175 | (define (set-symbol-property! sym prop val) |
| 176 | (let ((pair (assoc prop (symbol-pref sym)))) |
| 177 | (if pair |
| 178 | (set-cdr! pair val) |
| 179 | (symbol-pset! sym (acons prop val (symbol-pref sym)))))) |
| 180 | |
| 181 | (define (symbol-property-remove! sym prop) |
| 182 | (let ((pair (assoc prop (symbol-pref sym)))) |
| 183 | (if pair |
| 184 | (symbol-pset! sym (delq! pair (symbol-pref sym)))))) |
| 185 | |
| 186 | ;;; {General Properties} |
| 187 | ;;; |
| 188 | |
| 189 | ;; This is a more modern interface to properties. It will replace all |
| 190 | ;; other property-like things eventually. |
| 191 | |
| 192 | (define (make-object-property) |
| 193 | (let ((prop (primitive-make-property #f))) |
| 194 | (make-procedure-with-setter |
| 195 | (lambda (obj) (primitive-property-ref prop obj)) |
| 196 | (lambda (obj val) (primitive-property-set! prop obj val))))) |
| 197 | |
| 198 | \f |
| 199 | |
| 200 | ;;; {Arrays} |
| 201 | ;;; |
| 202 | |
| 203 | (if (provided? 'array) |
| 204 | (primitive-load-path "ice-9/arrays.scm")) |
| 205 | |
| 206 | \f |
| 207 | ;;; {Keywords} |
| 208 | ;;; |
| 209 | |
| 210 | (define (symbol->keyword symbol) |
| 211 | (make-keyword-from-dash-symbol (symbol-append '- symbol))) |
| 212 | |
| 213 | (define (keyword->symbol kw) |
| 214 | (let ((sym (symbol->string (keyword-dash-symbol kw)))) |
| 215 | (string->symbol (substring sym 1 (string-length sym))))) |
| 216 | |
| 217 | (define (kw-arg-ref args kw) |
| 218 | (let ((rem (member kw args))) |
| 219 | (and rem (pair? (cdr rem)) (cadr rem)))) |
| 220 | |
| 221 | \f |
| 222 | |
| 223 | ;;; {Structs} |
| 224 | |
| 225 | (define (struct-layout s) |
| 226 | (struct-ref (struct-vtable s) vtable-index-layout)) |
| 227 | |
| 228 | \f |
| 229 | |
| 230 | ;;; Environments |
| 231 | |
| 232 | (define the-environment |
| 233 | (procedure->syntax |
| 234 | (lambda (x e) |
| 235 | e))) |
| 236 | |
| 237 | (define the-root-environment (the-environment)) |
| 238 | |
| 239 | (define (environment-module env) |
| 240 | (let ((closure (and (pair? env) (car (last-pair env))))) |
| 241 | (and closure (procedure-property closure 'module)))) |
| 242 | |
| 243 | \f |
| 244 | ;;; {Records} |
| 245 | ;;; |
| 246 | |
| 247 | ;; Printing records: by default, records are printed as |
| 248 | ;; |
| 249 | ;; #<type-name field1: val1 field2: val2 ...> |
| 250 | ;; |
| 251 | ;; You can change that by giving a custom printing function to |
| 252 | ;; MAKE-RECORD-TYPE (after the list of field symbols). This function |
| 253 | ;; will be called like |
| 254 | ;; |
| 255 | ;; (<printer> object port) |
| 256 | ;; |
| 257 | ;; It should print OBJECT to PORT. |
| 258 | |
| 259 | (define (inherit-print-state old-port new-port) |
| 260 | (if (get-print-state old-port) |
| 261 | (port-with-print-state new-port (get-print-state old-port)) |
| 262 | new-port)) |
| 263 | |
| 264 | ;; 0: type-name, 1: fields |
| 265 | (define record-type-vtable |
| 266 | (make-vtable-vtable "prpr" 0 |
| 267 | (lambda (s p) |
| 268 | (cond ((eq? s record-type-vtable) |
| 269 | (display "#<record-type-vtable>" p)) |
| 270 | (else |
| 271 | (display "#<record-type " p) |
| 272 | (display (record-type-name s) p) |
| 273 | (display ">" p)))))) |
| 274 | |
| 275 | (define (record-type? obj) |
| 276 | (and (struct? obj) (eq? record-type-vtable (struct-vtable obj)))) |
| 277 | |
| 278 | (define (make-record-type type-name fields . opt) |
| 279 | (let ((printer-fn (and (pair? opt) (car opt)))) |
| 280 | (let ((struct (make-struct record-type-vtable 0 |
| 281 | (make-struct-layout |
| 282 | (apply string-append |
| 283 | (map (lambda (f) "pw") fields))) |
| 284 | (or printer-fn |
| 285 | (lambda (s p) |
| 286 | (display "#<" p) |
| 287 | (display type-name p) |
| 288 | (let loop ((fields fields) |
| 289 | (off 0)) |
| 290 | (cond |
| 291 | ((not (null? fields)) |
| 292 | (display " " p) |
| 293 | (display (car fields) p) |
| 294 | (display ": " p) |
| 295 | (display (struct-ref s off) p) |
| 296 | (loop (cdr fields) (+ 1 off))))) |
| 297 | (display ">" p))) |
| 298 | type-name |
| 299 | (copy-tree fields)))) |
| 300 | ;; Temporary solution: Associate a name to the record type descriptor |
| 301 | ;; so that the object system can create a wrapper class for it. |
| 302 | (set-struct-vtable-name! struct (if (symbol? type-name) |
| 303 | type-name |
| 304 | (string->symbol type-name))) |
| 305 | struct))) |
| 306 | |
| 307 | (define (record-type-name obj) |
| 308 | (if (record-type? obj) |
| 309 | (struct-ref obj vtable-offset-user) |
| 310 | (error 'not-a-record-type obj))) |
| 311 | |
| 312 | (define (record-type-fields obj) |
| 313 | (if (record-type? obj) |
| 314 | (struct-ref obj (+ 1 vtable-offset-user)) |
| 315 | (error 'not-a-record-type obj))) |
| 316 | |
| 317 | (define (record-constructor rtd . opt) |
| 318 | (let ((field-names (if (pair? opt) (car opt) (record-type-fields rtd)))) |
| 319 | (local-eval `(lambda ,field-names |
| 320 | (make-struct ',rtd 0 ,@(map (lambda (f) |
| 321 | (if (memq f field-names) |
| 322 | f |
| 323 | #f)) |
| 324 | (record-type-fields rtd)))) |
| 325 | the-root-environment))) |
| 326 | |
| 327 | (define (record-predicate rtd) |
| 328 | (lambda (obj) (and (struct? obj) (eq? rtd (struct-vtable obj))))) |
| 329 | |
| 330 | (define (record-accessor rtd field-name) |
| 331 | (let* ((pos (list-index (record-type-fields rtd) field-name))) |
| 332 | (if (not pos) |
| 333 | (error 'no-such-field field-name)) |
| 334 | (local-eval `(lambda (obj) |
| 335 | (and (eq? ',rtd (record-type-descriptor obj)) |
| 336 | (struct-ref obj ,pos))) |
| 337 | the-root-environment))) |
| 338 | |
| 339 | (define (record-modifier rtd field-name) |
| 340 | (let* ((pos (list-index (record-type-fields rtd) field-name))) |
| 341 | (if (not pos) |
| 342 | (error 'no-such-field field-name)) |
| 343 | (local-eval `(lambda (obj val) |
| 344 | (and (eq? ',rtd (record-type-descriptor obj)) |
| 345 | (struct-set! obj ,pos val))) |
| 346 | the-root-environment))) |
| 347 | |
| 348 | |
| 349 | (define (record? obj) |
| 350 | (and (struct? obj) (record-type? (struct-vtable obj)))) |
| 351 | |
| 352 | (define (record-type-descriptor obj) |
| 353 | (if (struct? obj) |
| 354 | (struct-vtable obj) |
| 355 | (error 'not-a-record obj))) |
| 356 | |
| 357 | (provide 'record) |
| 358 | |
| 359 | \f |
| 360 | ;;; {Booleans} |
| 361 | ;;; |
| 362 | |
| 363 | (define (->bool x) (not (not x))) |
| 364 | |
| 365 | \f |
| 366 | ;;; {Symbols} |
| 367 | ;;; |
| 368 | |
| 369 | (define (symbol-append . args) |
| 370 | (string->symbol (apply string-append (map symbol->string args)))) |
| 371 | |
| 372 | (define (list->symbol . args) |
| 373 | (string->symbol (apply list->string args))) |
| 374 | |
| 375 | (define (symbol . args) |
| 376 | (string->symbol (apply string args))) |
| 377 | |
| 378 | \f |
| 379 | ;;; {Lists} |
| 380 | ;;; |
| 381 | |
| 382 | (define (list-index l k) |
| 383 | (let loop ((n 0) |
| 384 | (l l)) |
| 385 | (and (not (null? l)) |
| 386 | (if (eq? (car l) k) |
| 387 | n |
| 388 | (loop (+ n 1) (cdr l)))))) |
| 389 | |
| 390 | (define (make-list n . init) |
| 391 | (if (pair? init) (set! init (car init))) |
| 392 | (let loop ((answer '()) |
| 393 | (n n)) |
| 394 | (if (<= n 0) |
| 395 | answer |
| 396 | (loop (cons init answer) (- n 1))))) |
| 397 | |
| 398 | \f |
| 399 | ;;; {and-map and or-map} |
| 400 | ;;; |
| 401 | ;;; (and-map fn lst) is like (and (fn (car lst)) (fn (cadr lst)) (fn...) ...) |
| 402 | ;;; (or-map fn lst) is like (or (fn (car lst)) (fn (cadr lst)) (fn...) ...) |
| 403 | ;;; |
| 404 | |
| 405 | ;; and-map f l |
| 406 | ;; |
| 407 | ;; Apply f to successive elements of l until exhaustion or f returns #f. |
| 408 | ;; If returning early, return #f. Otherwise, return the last value returned |
| 409 | ;; by f. If f has never been called because l is empty, return #t. |
| 410 | ;; |
| 411 | (define (and-map f lst) |
| 412 | (let loop ((result #t) |
| 413 | (l lst)) |
| 414 | (and result |
| 415 | (or (and (null? l) |
| 416 | result) |
| 417 | (loop (f (car l)) (cdr l)))))) |
| 418 | |
| 419 | ;; or-map f l |
| 420 | ;; |
| 421 | ;; Apply f to successive elements of l until exhaustion or while f returns #f. |
| 422 | ;; If returning early, return the return value of f. |
| 423 | ;; |
| 424 | (define (or-map f lst) |
| 425 | (let loop ((result #f) |
| 426 | (l lst)) |
| 427 | (or result |
| 428 | (and (not (null? l)) |
| 429 | (loop (f (car l)) (cdr l)))))) |
| 430 | |
| 431 | \f |
| 432 | |
| 433 | (if (provided? 'posix) |
| 434 | (primitive-load-path "ice-9/posix.scm")) |
| 435 | |
| 436 | (if (provided? 'socket) |
| 437 | (primitive-load-path "ice-9/networking.scm")) |
| 438 | |
| 439 | (define file-exists? |
| 440 | (if (provided? 'posix) |
| 441 | (lambda (str) |
| 442 | (access? str F_OK)) |
| 443 | (lambda (str) |
| 444 | (let ((port (catch 'system-error (lambda () (open-file str OPEN_READ)) |
| 445 | (lambda args #f)))) |
| 446 | (if port (begin (close-port port) #t) |
| 447 | #f))))) |
| 448 | |
| 449 | (define file-is-directory? |
| 450 | (if (provided? 'posix) |
| 451 | (lambda (str) |
| 452 | (eq? (stat:type (stat str)) 'directory)) |
| 453 | (lambda (str) |
| 454 | (let ((port (catch 'system-error |
| 455 | (lambda () (open-file (string-append str "/.") |
| 456 | OPEN_READ)) |
| 457 | (lambda args #f)))) |
| 458 | (if port (begin (close-port port) #t) |
| 459 | #f))))) |
| 460 | |
| 461 | (define (has-suffix? str suffix) |
| 462 | (let ((sufl (string-length suffix)) |
| 463 | (sl (string-length str))) |
| 464 | (and (> sl sufl) |
| 465 | (string=? (substring str (- sl sufl) sl) suffix)))) |
| 466 | |
| 467 | (define (system-error-errno args) |
| 468 | (if (eq? (car args) 'system-error) |
| 469 | (car (list-ref args 4)) |
| 470 | #f)) |
| 471 | |
| 472 | \f |
| 473 | ;;; {Error Handling} |
| 474 | ;;; |
| 475 | |
| 476 | (define (error . args) |
| 477 | (save-stack) |
| 478 | (if (null? args) |
| 479 | (scm-error 'misc-error #f "?" #f #f) |
| 480 | (let loop ((msg "~A") |
| 481 | (rest (cdr args))) |
| 482 | (if (not (null? rest)) |
| 483 | (loop (string-append msg " ~S") |
| 484 | (cdr rest)) |
| 485 | (scm-error 'misc-error #f msg args #f))))) |
| 486 | |
| 487 | ;; bad-throw is the hook that is called upon a throw to a an unhandled |
| 488 | ;; key (unless the throw has four arguments, in which case |
| 489 | ;; it's usually interpreted as an error throw.) |
| 490 | ;; If the key has a default handler (a throw-handler-default property), |
| 491 | ;; it is applied to the throw. |
| 492 | ;; |
| 493 | (define (bad-throw key . args) |
| 494 | (let ((default (symbol-property key 'throw-handler-default))) |
| 495 | (or (and default (apply default key args)) |
| 496 | (apply error "unhandled-exception:" key args)))) |
| 497 | |
| 498 | \f |
| 499 | |
| 500 | (define (tm:sec obj) (vector-ref obj 0)) |
| 501 | (define (tm:min obj) (vector-ref obj 1)) |
| 502 | (define (tm:hour obj) (vector-ref obj 2)) |
| 503 | (define (tm:mday obj) (vector-ref obj 3)) |
| 504 | (define (tm:mon obj) (vector-ref obj 4)) |
| 505 | (define (tm:year obj) (vector-ref obj 5)) |
| 506 | (define (tm:wday obj) (vector-ref obj 6)) |
| 507 | (define (tm:yday obj) (vector-ref obj 7)) |
| 508 | (define (tm:isdst obj) (vector-ref obj 8)) |
| 509 | (define (tm:gmtoff obj) (vector-ref obj 9)) |
| 510 | (define (tm:zone obj) (vector-ref obj 10)) |
| 511 | |
| 512 | (define (set-tm:sec obj val) (vector-set! obj 0 val)) |
| 513 | (define (set-tm:min obj val) (vector-set! obj 1 val)) |
| 514 | (define (set-tm:hour obj val) (vector-set! obj 2 val)) |
| 515 | (define (set-tm:mday obj val) (vector-set! obj 3 val)) |
| 516 | (define (set-tm:mon obj val) (vector-set! obj 4 val)) |
| 517 | (define (set-tm:year obj val) (vector-set! obj 5 val)) |
| 518 | (define (set-tm:wday obj val) (vector-set! obj 6 val)) |
| 519 | (define (set-tm:yday obj val) (vector-set! obj 7 val)) |
| 520 | (define (set-tm:isdst obj val) (vector-set! obj 8 val)) |
| 521 | (define (set-tm:gmtoff obj val) (vector-set! obj 9 val)) |
| 522 | (define (set-tm:zone obj val) (vector-set! obj 10 val)) |
| 523 | |
| 524 | (define (tms:clock obj) (vector-ref obj 0)) |
| 525 | (define (tms:utime obj) (vector-ref obj 1)) |
| 526 | (define (tms:stime obj) (vector-ref obj 2)) |
| 527 | (define (tms:cutime obj) (vector-ref obj 3)) |
| 528 | (define (tms:cstime obj) (vector-ref obj 4)) |
| 529 | |
| 530 | (define file-position ftell) |
| 531 | (define (file-set-position port offset . whence) |
| 532 | (let ((whence (if (eq? whence '()) SEEK_SET (car whence)))) |
| 533 | (seek port offset whence))) |
| 534 | |
| 535 | (define (move->fdes fd/port fd) |
| 536 | (cond ((integer? fd/port) |
| 537 | (dup->fdes fd/port fd) |
| 538 | (close fd/port) |
| 539 | fd) |
| 540 | (else |
| 541 | (primitive-move->fdes fd/port fd) |
| 542 | (set-port-revealed! fd/port 1) |
| 543 | fd/port))) |
| 544 | |
| 545 | (define (release-port-handle port) |
| 546 | (let ((revealed (port-revealed port))) |
| 547 | (if (> revealed 0) |
| 548 | (set-port-revealed! port (- revealed 1))))) |
| 549 | |
| 550 | (define (dup->port port/fd mode . maybe-fd) |
| 551 | (let ((port (fdopen (apply dup->fdes port/fd maybe-fd) |
| 552 | mode))) |
| 553 | (if (pair? maybe-fd) |
| 554 | (set-port-revealed! port 1)) |
| 555 | port)) |
| 556 | |
| 557 | (define (dup->inport port/fd . maybe-fd) |
| 558 | (apply dup->port port/fd "r" maybe-fd)) |
| 559 | |
| 560 | (define (dup->outport port/fd . maybe-fd) |
| 561 | (apply dup->port port/fd "w" maybe-fd)) |
| 562 | |
| 563 | (define (dup port/fd . maybe-fd) |
| 564 | (if (integer? port/fd) |
| 565 | (apply dup->fdes port/fd maybe-fd) |
| 566 | (apply dup->port port/fd (port-mode port/fd) maybe-fd))) |
| 567 | |
| 568 | (define (duplicate-port port modes) |
| 569 | (dup->port port modes)) |
| 570 | |
| 571 | (define (fdes->inport fdes) |
| 572 | (let loop ((rest-ports (fdes->ports fdes))) |
| 573 | (cond ((null? rest-ports) |
| 574 | (let ((result (fdopen fdes "r"))) |
| 575 | (set-port-revealed! result 1) |
| 576 | result)) |
| 577 | ((input-port? (car rest-ports)) |
| 578 | (set-port-revealed! (car rest-ports) |
| 579 | (+ (port-revealed (car rest-ports)) 1)) |
| 580 | (car rest-ports)) |
| 581 | (else |
| 582 | (loop (cdr rest-ports)))))) |
| 583 | |
| 584 | (define (fdes->outport fdes) |
| 585 | (let loop ((rest-ports (fdes->ports fdes))) |
| 586 | (cond ((null? rest-ports) |
| 587 | (let ((result (fdopen fdes "w"))) |
| 588 | (set-port-revealed! result 1) |
| 589 | result)) |
| 590 | ((output-port? (car rest-ports)) |
| 591 | (set-port-revealed! (car rest-ports) |
| 592 | (+ (port-revealed (car rest-ports)) 1)) |
| 593 | (car rest-ports)) |
| 594 | (else |
| 595 | (loop (cdr rest-ports)))))) |
| 596 | |
| 597 | (define (port->fdes port) |
| 598 | (set-port-revealed! port (+ (port-revealed port) 1)) |
| 599 | (fileno port)) |
| 600 | |
| 601 | (define (setenv name value) |
| 602 | (if value |
| 603 | (putenv (string-append name "=" value)) |
| 604 | (putenv name))) |
| 605 | |
| 606 | (define (unsetenv name) |
| 607 | "Remove the entry for NAME from the environment." |
| 608 | (putenv name)) |
| 609 | |
| 610 | \f |
| 611 | ;;; {Load Paths} |
| 612 | ;;; |
| 613 | |
| 614 | ;;; Here for backward compatability |
| 615 | ;; |
| 616 | (define scheme-file-suffix (lambda () ".scm")) |
| 617 | |
| 618 | (define (in-vicinity vicinity file) |
| 619 | (let ((tail (let ((len (string-length vicinity))) |
| 620 | (if (zero? len) |
| 621 | #f |
| 622 | (string-ref vicinity (- len 1)))))) |
| 623 | (string-append vicinity |
| 624 | (if (or (not tail) |
| 625 | (eq? tail #\/)) |
| 626 | "" |
| 627 | "/") |
| 628 | file))) |
| 629 | |
| 630 | \f |
| 631 | ;;; {Help for scm_shell} |
| 632 | ;;; The argument-processing code used by Guile-based shells generates |
| 633 | ;;; Scheme code based on the argument list. This page contains help |
| 634 | ;;; functions for the code it generates. |
| 635 | |
| 636 | (define (command-line) (program-arguments)) |
| 637 | |
| 638 | ;; This is mostly for the internal use of the code generated by |
| 639 | ;; scm_compile_shell_switches. |
| 640 | |
| 641 | (define (turn-on-debugging) |
| 642 | (debug-enable 'debug) |
| 643 | (debug-enable 'backtrace) |
| 644 | (read-enable 'positions)) |
| 645 | |
| 646 | (define (load-user-init) |
| 647 | (let* ((home (or (getenv "HOME") |
| 648 | (false-if-exception (passwd:dir (getpwuid (getuid)))) |
| 649 | "/")) ;; fallback for cygwin etc. |
| 650 | (init-file (in-vicinity home ".guile"))) |
| 651 | (if (file-exists? init-file) |
| 652 | (primitive-load init-file)))) |
| 653 | |
| 654 | \f |
| 655 | ;;; {Loading by paths} |
| 656 | |
| 657 | ;;; Load a Scheme source file named NAME, searching for it in the |
| 658 | ;;; directories listed in %load-path, and applying each of the file |
| 659 | ;;; name extensions listed in %load-extensions. |
| 660 | (define (load-from-path name) |
| 661 | (start-stack 'load-stack |
| 662 | (primitive-load-path name))) |
| 663 | |
| 664 | |
| 665 | \f |
| 666 | ;;; {Transcendental Functions} |
| 667 | ;;; |
| 668 | ;;; Derived from "Transcen.scm", Complex trancendental functions for SCM. |
| 669 | ;;; Written by Jerry D. Hedden, (C) FSF. |
| 670 | ;;; See the file `COPYING' for terms applying to this program. |
| 671 | ;;; |
| 672 | |
| 673 | (define (exp z) |
| 674 | (if (real? z) ($exp z) |
| 675 | (make-polar ($exp (real-part z)) (imag-part z)))) |
| 676 | |
| 677 | (define (log z) |
| 678 | (if (and (real? z) (>= z 0)) |
| 679 | ($log z) |
| 680 | (make-rectangular ($log (magnitude z)) (angle z)))) |
| 681 | |
| 682 | (define (sqrt z) |
| 683 | (if (real? z) |
| 684 | (if (negative? z) (make-rectangular 0 ($sqrt (- z))) |
| 685 | ($sqrt z)) |
| 686 | (make-polar ($sqrt (magnitude z)) (/ (angle z) 2)))) |
| 687 | |
| 688 | (define expt |
| 689 | (let ((integer-expt integer-expt)) |
| 690 | (lambda (z1 z2) |
| 691 | (cond ((integer? z2) |
| 692 | (if (negative? z2) |
| 693 | (/ 1 (integer-expt z1 (- z2))) |
| 694 | (integer-expt z1 z2))) |
| 695 | ((and (real? z2) (real? z1) (>= z1 0)) |
| 696 | ($expt z1 z2)) |
| 697 | (else |
| 698 | (exp (* z2 (log z1)))))))) |
| 699 | |
| 700 | (define (sinh z) |
| 701 | (if (real? z) ($sinh z) |
| 702 | (let ((x (real-part z)) (y (imag-part z))) |
| 703 | (make-rectangular (* ($sinh x) ($cos y)) |
| 704 | (* ($cosh x) ($sin y)))))) |
| 705 | (define (cosh z) |
| 706 | (if (real? z) ($cosh z) |
| 707 | (let ((x (real-part z)) (y (imag-part z))) |
| 708 | (make-rectangular (* ($cosh x) ($cos y)) |
| 709 | (* ($sinh x) ($sin y)))))) |
| 710 | (define (tanh z) |
| 711 | (if (real? z) ($tanh z) |
| 712 | (let* ((x (* 2 (real-part z))) |
| 713 | (y (* 2 (imag-part z))) |
| 714 | (w (+ ($cosh x) ($cos y)))) |
| 715 | (make-rectangular (/ ($sinh x) w) (/ ($sin y) w))))) |
| 716 | |
| 717 | (define (asinh z) |
| 718 | (if (real? z) ($asinh z) |
| 719 | (log (+ z (sqrt (+ (* z z) 1)))))) |
| 720 | |
| 721 | (define (acosh z) |
| 722 | (if (and (real? z) (>= z 1)) |
| 723 | ($acosh z) |
| 724 | (log (+ z (sqrt (- (* z z) 1)))))) |
| 725 | |
| 726 | (define (atanh z) |
| 727 | (if (and (real? z) (> z -1) (< z 1)) |
| 728 | ($atanh z) |
| 729 | (/ (log (/ (+ 1 z) (- 1 z))) 2))) |
| 730 | |
| 731 | (define (sin z) |
| 732 | (if (real? z) ($sin z) |
| 733 | (let ((x (real-part z)) (y (imag-part z))) |
| 734 | (make-rectangular (* ($sin x) ($cosh y)) |
| 735 | (* ($cos x) ($sinh y)))))) |
| 736 | (define (cos z) |
| 737 | (if (real? z) ($cos z) |
| 738 | (let ((x (real-part z)) (y (imag-part z))) |
| 739 | (make-rectangular (* ($cos x) ($cosh y)) |
| 740 | (- (* ($sin x) ($sinh y))))))) |
| 741 | (define (tan z) |
| 742 | (if (real? z) ($tan z) |
| 743 | (let* ((x (* 2 (real-part z))) |
| 744 | (y (* 2 (imag-part z))) |
| 745 | (w (+ ($cos x) ($cosh y)))) |
| 746 | (make-rectangular (/ ($sin x) w) (/ ($sinh y) w))))) |
| 747 | |
| 748 | (define (asin z) |
| 749 | (if (and (real? z) (>= z -1) (<= z 1)) |
| 750 | ($asin z) |
| 751 | (* -i (asinh (* +i z))))) |
| 752 | |
| 753 | (define (acos z) |
| 754 | (if (and (real? z) (>= z -1) (<= z 1)) |
| 755 | ($acos z) |
| 756 | (+ (/ (angle -1) 2) (* +i (asinh (* +i z)))))) |
| 757 | |
| 758 | (define (atan z . y) |
| 759 | (if (null? y) |
| 760 | (if (real? z) ($atan z) |
| 761 | (/ (log (/ (- +i z) (+ +i z))) +2i)) |
| 762 | ($atan2 z (car y)))) |
| 763 | |
| 764 | (define (log10 arg) |
| 765 | (/ (log arg) (log 10))) |
| 766 | |
| 767 | \f |
| 768 | |
| 769 | ;;; {Reader Extensions} |
| 770 | ;;; |
| 771 | |
| 772 | ;;; Reader code for various "#c" forms. |
| 773 | ;;; |
| 774 | |
| 775 | (read-hash-extend #\' (lambda (c port) |
| 776 | (read port))) |
| 777 | |
| 778 | (define read-eval? (make-fluid)) |
| 779 | (fluid-set! read-eval? #f) |
| 780 | (read-hash-extend #\. |
| 781 | (lambda (c port) |
| 782 | (if (fluid-ref read-eval?) |
| 783 | (eval (read port) (interaction-environment)) |
| 784 | (error |
| 785 | "#. read expansion found and read-eval? is #f.")))) |
| 786 | |
| 787 | \f |
| 788 | ;;; {Command Line Options} |
| 789 | ;;; |
| 790 | |
| 791 | (define (get-option argv kw-opts kw-args return) |
| 792 | (cond |
| 793 | ((null? argv) |
| 794 | (return #f #f argv)) |
| 795 | |
| 796 | ((or (not (eq? #\- (string-ref (car argv) 0))) |
| 797 | (eq? (string-length (car argv)) 1)) |
| 798 | (return 'normal-arg (car argv) (cdr argv))) |
| 799 | |
| 800 | ((eq? #\- (string-ref (car argv) 1)) |
| 801 | (let* ((kw-arg-pos (or (string-index (car argv) #\=) |
| 802 | (string-length (car argv)))) |
| 803 | (kw (symbol->keyword (substring (car argv) 2 kw-arg-pos))) |
| 804 | (kw-opt? (member kw kw-opts)) |
| 805 | (kw-arg? (member kw kw-args)) |
| 806 | (arg (or (and (not (eq? kw-arg-pos (string-length (car argv)))) |
| 807 | (substring (car argv) |
| 808 | (+ kw-arg-pos 1) |
| 809 | (string-length (car argv)))) |
| 810 | (and kw-arg? |
| 811 | (begin (set! argv (cdr argv)) (car argv)))))) |
| 812 | (if (or kw-opt? kw-arg?) |
| 813 | (return kw arg (cdr argv)) |
| 814 | (return 'usage-error kw (cdr argv))))) |
| 815 | |
| 816 | (else |
| 817 | (let* ((char (substring (car argv) 1 2)) |
| 818 | (kw (symbol->keyword char))) |
| 819 | (cond |
| 820 | |
| 821 | ((member kw kw-opts) |
| 822 | (let* ((rest-car (substring (car argv) 2 (string-length (car argv)))) |
| 823 | (new-argv (if (= 0 (string-length rest-car)) |
| 824 | (cdr argv) |
| 825 | (cons (string-append "-" rest-car) (cdr argv))))) |
| 826 | (return kw #f new-argv))) |
| 827 | |
| 828 | ((member kw kw-args) |
| 829 | (let* ((rest-car (substring (car argv) 2 (string-length (car argv)))) |
| 830 | (arg (if (= 0 (string-length rest-car)) |
| 831 | (cadr argv) |
| 832 | rest-car)) |
| 833 | (new-argv (if (= 0 (string-length rest-car)) |
| 834 | (cddr argv) |
| 835 | (cdr argv)))) |
| 836 | (return kw arg new-argv))) |
| 837 | |
| 838 | (else (return 'usage-error kw argv))))))) |
| 839 | |
| 840 | (define (for-next-option proc argv kw-opts kw-args) |
| 841 | (let loop ((argv argv)) |
| 842 | (get-option argv kw-opts kw-args |
| 843 | (lambda (opt opt-arg argv) |
| 844 | (and opt (proc opt opt-arg argv loop)))))) |
| 845 | |
| 846 | (define (display-usage-report kw-desc) |
| 847 | (for-each |
| 848 | (lambda (kw) |
| 849 | (or (eq? (car kw) #t) |
| 850 | (eq? (car kw) 'else) |
| 851 | (let* ((opt-desc kw) |
| 852 | (help (cadr opt-desc)) |
| 853 | (opts (car opt-desc)) |
| 854 | (opts-proper (if (string? (car opts)) (cdr opts) opts)) |
| 855 | (arg-name (if (string? (car opts)) |
| 856 | (string-append "<" (car opts) ">") |
| 857 | "")) |
| 858 | (left-part (string-append |
| 859 | (with-output-to-string |
| 860 | (lambda () |
| 861 | (map (lambda (x) (display (keyword-symbol x)) (display " ")) |
| 862 | opts-proper))) |
| 863 | arg-name)) |
| 864 | (middle-part (if (and (< (string-length left-part) 30) |
| 865 | (< (string-length help) 40)) |
| 866 | (make-string (- 30 (string-length left-part)) #\ ) |
| 867 | "\n\t"))) |
| 868 | (display left-part) |
| 869 | (display middle-part) |
| 870 | (display help) |
| 871 | (newline)))) |
| 872 | kw-desc)) |
| 873 | |
| 874 | |
| 875 | |
| 876 | (define (transform-usage-lambda cases) |
| 877 | (let* ((raw-usage (delq! 'else (map car cases))) |
| 878 | (usage-sans-specials (map (lambda (x) |
| 879 | (or (and (not (list? x)) x) |
| 880 | (and (symbol? (car x)) #t) |
| 881 | (and (boolean? (car x)) #t) |
| 882 | x)) |
| 883 | raw-usage)) |
| 884 | (usage-desc (delq! #t usage-sans-specials)) |
| 885 | (kw-desc (map car usage-desc)) |
| 886 | (kw-opts (apply append (map (lambda (x) (and (not (string? (car x))) x)) kw-desc))) |
| 887 | (kw-args (apply append (map (lambda (x) (and (string? (car x)) (cdr x))) kw-desc))) |
| 888 | (transmogrified-cases (map (lambda (case) |
| 889 | (cons (let ((opts (car case))) |
| 890 | (if (or (boolean? opts) (eq? 'else opts)) |
| 891 | opts |
| 892 | (cond |
| 893 | ((symbol? (car opts)) opts) |
| 894 | ((boolean? (car opts)) opts) |
| 895 | ((string? (caar opts)) (cdar opts)) |
| 896 | (else (car opts))))) |
| 897 | (cdr case))) |
| 898 | cases))) |
| 899 | `(let ((%display-usage (lambda () (display-usage-report ',usage-desc)))) |
| 900 | (lambda (%argv) |
| 901 | (let %next-arg ((%argv %argv)) |
| 902 | (get-option %argv |
| 903 | ',kw-opts |
| 904 | ',kw-args |
| 905 | (lambda (%opt %arg %new-argv) |
| 906 | (case %opt |
| 907 | ,@ transmogrified-cases)))))))) |
| 908 | |
| 909 | |
| 910 | \f |
| 911 | |
| 912 | ;;; {Low Level Modules} |
| 913 | ;;; |
| 914 | ;;; These are the low level data structures for modules. |
| 915 | ;;; |
| 916 | ;;; !!! warning: The interface to lazy binder procedures is going |
| 917 | ;;; to be changed in an incompatible way to permit all the basic |
| 918 | ;;; module ops to be virtualized. |
| 919 | ;;; |
| 920 | ;;; (make-module size use-list lazy-binding-proc) => module |
| 921 | ;;; module-{obarray,uses,binder}[|-set!] |
| 922 | ;;; (module? obj) => [#t|#f] |
| 923 | ;;; (module-locally-bound? module symbol) => [#t|#f] |
| 924 | ;;; (module-bound? module symbol) => [#t|#f] |
| 925 | ;;; (module-symbol-locally-interned? module symbol) => [#t|#f] |
| 926 | ;;; (module-symbol-interned? module symbol) => [#t|#f] |
| 927 | ;;; (module-local-variable module symbol) => [#<variable ...> | #f] |
| 928 | ;;; (module-variable module symbol) => [#<variable ...> | #f] |
| 929 | ;;; (module-symbol-binding module symbol opt-value) |
| 930 | ;;; => [ <obj> | opt-value | an error occurs ] |
| 931 | ;;; (module-make-local-var! module symbol) => #<variable...> |
| 932 | ;;; (module-add! module symbol var) => unspecified |
| 933 | ;;; (module-remove! module symbol) => unspecified |
| 934 | ;;; (module-for-each proc module) => unspecified |
| 935 | ;;; (make-scm-module) => module ; a lazy copy of the symhash module |
| 936 | ;;; (set-current-module module) => unspecified |
| 937 | ;;; (current-module) => #<module...> |
| 938 | ;;; |
| 939 | ;;; |
| 940 | |
| 941 | \f |
| 942 | ;;; {Printing Modules} |
| 943 | ;; This is how modules are printed. You can re-define it. |
| 944 | ;; (Redefining is actually more complicated than simply redefining |
| 945 | ;; %print-module because that would only change the binding and not |
| 946 | ;; the value stored in the vtable that determines how record are |
| 947 | ;; printed. Sigh.) |
| 948 | |
| 949 | (define (%print-module mod port) ; unused args: depth length style table) |
| 950 | (display "#<" port) |
| 951 | (display (or (module-kind mod) "module") port) |
| 952 | (let ((name (module-name mod))) |
| 953 | (if name |
| 954 | (begin |
| 955 | (display " " port) |
| 956 | (display name port)))) |
| 957 | (display " " port) |
| 958 | (display (number->string (object-address mod) 16) port) |
| 959 | (display ">" port)) |
| 960 | |
| 961 | ;; module-type |
| 962 | ;; |
| 963 | ;; A module is characterized by an obarray in which local symbols |
| 964 | ;; are interned, a list of modules, "uses", from which non-local |
| 965 | ;; bindings can be inherited, and an optional lazy-binder which |
| 966 | ;; is a (CLOSURE module symbol) which, as a last resort, can provide |
| 967 | ;; bindings that would otherwise not be found locally in the module. |
| 968 | ;; |
| 969 | ;; NOTE: If you change here, you also need to change libguile/modules.h. |
| 970 | ;; |
| 971 | (define module-type |
| 972 | (make-record-type 'module |
| 973 | '(obarray uses binder eval-closure transformer name kind |
| 974 | observers weak-observers observer-id) |
| 975 | %print-module)) |
| 976 | |
| 977 | ;; make-module &opt size uses binder |
| 978 | ;; |
| 979 | ;; Create a new module, perhaps with a particular size of obarray, |
| 980 | ;; initial uses list, or binding procedure. |
| 981 | ;; |
| 982 | (define make-module |
| 983 | (lambda args |
| 984 | |
| 985 | (define (parse-arg index default) |
| 986 | (if (> (length args) index) |
| 987 | (list-ref args index) |
| 988 | default)) |
| 989 | |
| 990 | (if (> (length args) 3) |
| 991 | (error "Too many args to make-module." args)) |
| 992 | |
| 993 | (let ((size (parse-arg 0 31)) |
| 994 | (uses (parse-arg 1 '())) |
| 995 | (binder (parse-arg 2 #f))) |
| 996 | |
| 997 | (if (not (integer? size)) |
| 998 | (error "Illegal size to make-module." size)) |
| 999 | (if (not (and (list? uses) |
| 1000 | (and-map module? uses))) |
| 1001 | (error "Incorrect use list." uses)) |
| 1002 | (if (and binder (not (procedure? binder))) |
| 1003 | (error |
| 1004 | "Lazy-binder expected to be a procedure or #f." binder)) |
| 1005 | |
| 1006 | (let ((module (module-constructor (and (not (zero? size)) |
| 1007 | (make-hash-table size)) |
| 1008 | uses binder #f #f #f #f |
| 1009 | '() |
| 1010 | (make-weak-value-hash-table 31) |
| 1011 | 0))) |
| 1012 | |
| 1013 | ;; We can't pass this as an argument to module-constructor, |
| 1014 | ;; because we need it to close over a pointer to the module |
| 1015 | ;; itself. |
| 1016 | (set-module-eval-closure! module (standard-eval-closure module)) |
| 1017 | |
| 1018 | module)))) |
| 1019 | |
| 1020 | (define module-constructor (record-constructor module-type)) |
| 1021 | (define module-obarray (record-accessor module-type 'obarray)) |
| 1022 | (define set-module-obarray! (record-modifier module-type 'obarray)) |
| 1023 | (define module-uses (record-accessor module-type 'uses)) |
| 1024 | (define set-module-uses! (record-modifier module-type 'uses)) |
| 1025 | (define module-binder (record-accessor module-type 'binder)) |
| 1026 | (define set-module-binder! (record-modifier module-type 'binder)) |
| 1027 | |
| 1028 | ;; NOTE: This binding is used in libguile/modules.c. |
| 1029 | (define module-eval-closure (record-accessor module-type 'eval-closure)) |
| 1030 | |
| 1031 | (define module-transformer (record-accessor module-type 'transformer)) |
| 1032 | (define set-module-transformer! (record-modifier module-type 'transformer)) |
| 1033 | (define module-name (record-accessor module-type 'name)) |
| 1034 | (define set-module-name! (record-modifier module-type 'name)) |
| 1035 | (define module-kind (record-accessor module-type 'kind)) |
| 1036 | (define set-module-kind! (record-modifier module-type 'kind)) |
| 1037 | (define module-observers (record-accessor module-type 'observers)) |
| 1038 | (define set-module-observers! (record-modifier module-type 'observers)) |
| 1039 | (define module-weak-observers (record-accessor module-type 'weak-observers)) |
| 1040 | (define module-observer-id (record-accessor module-type 'observer-id)) |
| 1041 | (define set-module-observer-id! (record-modifier module-type 'observer-id)) |
| 1042 | (define module? (record-predicate module-type)) |
| 1043 | |
| 1044 | (define set-module-eval-closure! |
| 1045 | (let ((setter (record-modifier module-type 'eval-closure))) |
| 1046 | (lambda (module closure) |
| 1047 | (setter module closure) |
| 1048 | ;; Make it possible to lookup the module from the environment. |
| 1049 | ;; This implementation is correct since an eval closure can belong |
| 1050 | ;; to maximally one module. |
| 1051 | (set-procedure-property! closure 'module module)))) |
| 1052 | |
| 1053 | \f |
| 1054 | ;;; {Observer protocol} |
| 1055 | ;;; |
| 1056 | |
| 1057 | (define (module-observe module proc) |
| 1058 | (set-module-observers! module (cons proc (module-observers module))) |
| 1059 | (cons module proc)) |
| 1060 | |
| 1061 | (define (module-observe-weak module proc) |
| 1062 | (let ((id (module-observer-id module))) |
| 1063 | (hash-set! (module-weak-observers module) id proc) |
| 1064 | (set-module-observer-id! module (+ 1 id)) |
| 1065 | (cons module id))) |
| 1066 | |
| 1067 | (define (module-unobserve token) |
| 1068 | (let ((module (car token)) |
| 1069 | (id (cdr token))) |
| 1070 | (if (integer? id) |
| 1071 | (hash-remove! (module-weak-observers module) id) |
| 1072 | (set-module-observers! module (delq1! id (module-observers module))))) |
| 1073 | *unspecified*) |
| 1074 | |
| 1075 | (define (module-modified m) |
| 1076 | (for-each (lambda (proc) (proc m)) (module-observers m)) |
| 1077 | (hash-fold (lambda (id proc res) (proc m)) #f (module-weak-observers m))) |
| 1078 | |
| 1079 | \f |
| 1080 | ;;; {Module Searching in General} |
| 1081 | ;;; |
| 1082 | ;;; We sometimes want to look for properties of a symbol |
| 1083 | ;;; just within the obarray of one module. If the property |
| 1084 | ;;; holds, then it is said to hold ``locally'' as in, ``The symbol |
| 1085 | ;;; DISPLAY is locally rebound in the module `safe-guile'.'' |
| 1086 | ;;; |
| 1087 | ;;; |
| 1088 | ;;; Other times, we want to test for a symbol property in the obarray |
| 1089 | ;;; of M and, if it is not found there, try each of the modules in the |
| 1090 | ;;; uses list of M. This is the normal way of testing for some |
| 1091 | ;;; property, so we state these properties without qualification as |
| 1092 | ;;; in: ``The symbol 'fnord is interned in module M because it is |
| 1093 | ;;; interned locally in module M2 which is a member of the uses list |
| 1094 | ;;; of M.'' |
| 1095 | ;;; |
| 1096 | |
| 1097 | ;; module-search fn m |
| 1098 | ;; |
| 1099 | ;; return the first non-#f result of FN applied to M and then to |
| 1100 | ;; the modules in the uses of m, and so on recursively. If all applications |
| 1101 | ;; return #f, then so does this function. |
| 1102 | ;; |
| 1103 | (define (module-search fn m v) |
| 1104 | (define (loop pos) |
| 1105 | (and (pair? pos) |
| 1106 | (or (module-search fn (car pos) v) |
| 1107 | (loop (cdr pos))))) |
| 1108 | (or (fn m v) |
| 1109 | (loop (module-uses m)))) |
| 1110 | |
| 1111 | |
| 1112 | ;;; {Is a symbol bound in a module?} |
| 1113 | ;;; |
| 1114 | ;;; Symbol S in Module M is bound if S is interned in M and if the binding |
| 1115 | ;;; of S in M has been set to some well-defined value. |
| 1116 | ;;; |
| 1117 | |
| 1118 | ;; module-locally-bound? module symbol |
| 1119 | ;; |
| 1120 | ;; Is a symbol bound (interned and defined) locally in a given module? |
| 1121 | ;; |
| 1122 | (define (module-locally-bound? m v) |
| 1123 | (let ((var (module-local-variable m v))) |
| 1124 | (and var |
| 1125 | (variable-bound? var)))) |
| 1126 | |
| 1127 | ;; module-bound? module symbol |
| 1128 | ;; |
| 1129 | ;; Is a symbol bound (interned and defined) anywhere in a given module |
| 1130 | ;; or its uses? |
| 1131 | ;; |
| 1132 | (define (module-bound? m v) |
| 1133 | (module-search module-locally-bound? m v)) |
| 1134 | |
| 1135 | ;;; {Is a symbol interned in a module?} |
| 1136 | ;;; |
| 1137 | ;;; Symbol S in Module M is interned if S occurs in |
| 1138 | ;;; of S in M has been set to some well-defined value. |
| 1139 | ;;; |
| 1140 | ;;; It is possible to intern a symbol in a module without providing |
| 1141 | ;;; an initial binding for the corresponding variable. This is done |
| 1142 | ;;; with: |
| 1143 | ;;; (module-add! module symbol (make-undefined-variable)) |
| 1144 | ;;; |
| 1145 | ;;; In that case, the symbol is interned in the module, but not |
| 1146 | ;;; bound there. The unbound symbol shadows any binding for that |
| 1147 | ;;; symbol that might otherwise be inherited from a member of the uses list. |
| 1148 | ;;; |
| 1149 | |
| 1150 | (define (module-obarray-get-handle ob key) |
| 1151 | ((if (symbol? key) hashq-get-handle hash-get-handle) ob key)) |
| 1152 | |
| 1153 | (define (module-obarray-ref ob key) |
| 1154 | ((if (symbol? key) hashq-ref hash-ref) ob key)) |
| 1155 | |
| 1156 | (define (module-obarray-set! ob key val) |
| 1157 | ((if (symbol? key) hashq-set! hash-set!) ob key val)) |
| 1158 | |
| 1159 | (define (module-obarray-remove! ob key) |
| 1160 | ((if (symbol? key) hashq-remove! hash-remove!) ob key)) |
| 1161 | |
| 1162 | ;; module-symbol-locally-interned? module symbol |
| 1163 | ;; |
| 1164 | ;; is a symbol interned (not neccessarily defined) locally in a given module |
| 1165 | ;; or its uses? Interned symbols shadow inherited bindings even if |
| 1166 | ;; they are not themselves bound to a defined value. |
| 1167 | ;; |
| 1168 | (define (module-symbol-locally-interned? m v) |
| 1169 | (not (not (module-obarray-get-handle (module-obarray m) v)))) |
| 1170 | |
| 1171 | ;; module-symbol-interned? module symbol |
| 1172 | ;; |
| 1173 | ;; is a symbol interned (not neccessarily defined) anywhere in a given module |
| 1174 | ;; or its uses? Interned symbols shadow inherited bindings even if |
| 1175 | ;; they are not themselves bound to a defined value. |
| 1176 | ;; |
| 1177 | (define (module-symbol-interned? m v) |
| 1178 | (module-search module-symbol-locally-interned? m v)) |
| 1179 | |
| 1180 | |
| 1181 | ;;; {Mapping modules x symbols --> variables} |
| 1182 | ;;; |
| 1183 | |
| 1184 | ;; module-local-variable module symbol |
| 1185 | ;; return the local variable associated with a MODULE and SYMBOL. |
| 1186 | ;; |
| 1187 | ;;; This function is very important. It is the only function that can |
| 1188 | ;;; return a variable from a module other than the mutators that store |
| 1189 | ;;; new variables in modules. Therefore, this function is the location |
| 1190 | ;;; of the "lazy binder" hack. |
| 1191 | ;;; |
| 1192 | ;;; If symbol is defined in MODULE, and if the definition binds symbol |
| 1193 | ;;; to a variable, return that variable object. |
| 1194 | ;;; |
| 1195 | ;;; If the symbols is not found at first, but the module has a lazy binder, |
| 1196 | ;;; then try the binder. |
| 1197 | ;;; |
| 1198 | ;;; If the symbol is not found at all, return #f. |
| 1199 | ;;; |
| 1200 | (define (module-local-variable m v) |
| 1201 | ; (caddr |
| 1202 | ; (list m v |
| 1203 | (let ((b (module-obarray-ref (module-obarray m) v))) |
| 1204 | (or (and (variable? b) b) |
| 1205 | (and (module-binder m) |
| 1206 | ((module-binder m) m v #f))))) |
| 1207 | ;)) |
| 1208 | |
| 1209 | ;; module-variable module symbol |
| 1210 | ;; |
| 1211 | ;; like module-local-variable, except search the uses in the |
| 1212 | ;; case V is not found in M. |
| 1213 | ;; |
| 1214 | ;; NOTE: This function is superseded with C code (see modules.c) |
| 1215 | ;;; when using the standard eval closure. |
| 1216 | ;; |
| 1217 | (define (module-variable m v) |
| 1218 | (module-search module-local-variable m v)) |
| 1219 | |
| 1220 | |
| 1221 | ;;; {Mapping modules x symbols --> bindings} |
| 1222 | ;;; |
| 1223 | ;;; These are similar to the mapping to variables, except that the |
| 1224 | ;;; variable is dereferenced. |
| 1225 | ;;; |
| 1226 | |
| 1227 | ;; module-symbol-binding module symbol opt-value |
| 1228 | ;; |
| 1229 | ;; return the binding of a variable specified by name within |
| 1230 | ;; a given module, signalling an error if the variable is unbound. |
| 1231 | ;; If the OPT-VALUE is passed, then instead of signalling an error, |
| 1232 | ;; return OPT-VALUE. |
| 1233 | ;; |
| 1234 | (define (module-symbol-local-binding m v . opt-val) |
| 1235 | (let ((var (module-local-variable m v))) |
| 1236 | (if (and var (variable-bound? var)) |
| 1237 | (variable-ref var) |
| 1238 | (if (not (null? opt-val)) |
| 1239 | (car opt-val) |
| 1240 | (error "Locally unbound variable." v))))) |
| 1241 | |
| 1242 | ;; module-symbol-binding module symbol opt-value |
| 1243 | ;; |
| 1244 | ;; return the binding of a variable specified by name within |
| 1245 | ;; a given module, signalling an error if the variable is unbound. |
| 1246 | ;; If the OPT-VALUE is passed, then instead of signalling an error, |
| 1247 | ;; return OPT-VALUE. |
| 1248 | ;; |
| 1249 | (define (module-symbol-binding m v . opt-val) |
| 1250 | (let ((var (module-variable m v))) |
| 1251 | (if (and var (variable-bound? var)) |
| 1252 | (variable-ref var) |
| 1253 | (if (not (null? opt-val)) |
| 1254 | (car opt-val) |
| 1255 | (error "Unbound variable." v))))) |
| 1256 | |
| 1257 | |
| 1258 | \f |
| 1259 | ;;; {Adding Variables to Modules} |
| 1260 | ;;; |
| 1261 | ;;; |
| 1262 | |
| 1263 | |
| 1264 | ;; module-make-local-var! module symbol |
| 1265 | ;; |
| 1266 | ;; ensure a variable for V in the local namespace of M. |
| 1267 | ;; If no variable was already there, then create a new and uninitialzied |
| 1268 | ;; variable. |
| 1269 | ;; |
| 1270 | (define (module-make-local-var! m v) |
| 1271 | (or (let ((b (module-obarray-ref (module-obarray m) v))) |
| 1272 | (and (variable? b) |
| 1273 | (begin |
| 1274 | (module-modified m) |
| 1275 | b))) |
| 1276 | (and (module-binder m) |
| 1277 | ((module-binder m) m v #t)) |
| 1278 | (begin |
| 1279 | (let ((answer (make-undefined-variable))) |
| 1280 | (module-obarray-set! (module-obarray m) v answer) |
| 1281 | (module-modified m) |
| 1282 | answer)))) |
| 1283 | |
| 1284 | ;; module-ensure-local-variable! module symbol |
| 1285 | ;; |
| 1286 | ;; Ensure that there is a local variable in MODULE for SYMBOL. If |
| 1287 | ;; there is no binding for SYMBOL, create a new uninitialized |
| 1288 | ;; variable. Return the local variable. |
| 1289 | ;; |
| 1290 | (define (module-ensure-local-variable! module symbol) |
| 1291 | (or (module-local-variable module symbol) |
| 1292 | (let ((var (make-undefined-variable))) |
| 1293 | (module-add! module symbol var) |
| 1294 | var))) |
| 1295 | |
| 1296 | ;; module-add! module symbol var |
| 1297 | ;; |
| 1298 | ;; ensure a particular variable for V in the local namespace of M. |
| 1299 | ;; |
| 1300 | (define (module-add! m v var) |
| 1301 | (if (not (variable? var)) |
| 1302 | (error "Bad variable to module-add!" var)) |
| 1303 | (module-obarray-set! (module-obarray m) v var) |
| 1304 | (module-modified m)) |
| 1305 | |
| 1306 | ;; module-remove! |
| 1307 | ;; |
| 1308 | ;; make sure that a symbol is undefined in the local namespace of M. |
| 1309 | ;; |
| 1310 | (define (module-remove! m v) |
| 1311 | (module-obarray-remove! (module-obarray m) v) |
| 1312 | (module-modified m)) |
| 1313 | |
| 1314 | (define (module-clear! m) |
| 1315 | (hash-clear! (module-obarray m)) |
| 1316 | (module-modified m)) |
| 1317 | |
| 1318 | ;; MODULE-FOR-EACH -- exported |
| 1319 | ;; |
| 1320 | ;; Call PROC on each symbol in MODULE, with arguments of (SYMBOL VARIABLE). |
| 1321 | ;; |
| 1322 | (define (module-for-each proc module) |
| 1323 | (hash-for-each proc (module-obarray module))) |
| 1324 | |
| 1325 | (define (module-map proc module) |
| 1326 | (hash-map proc (module-obarray module))) |
| 1327 | |
| 1328 | \f |
| 1329 | |
| 1330 | ;;; {Low Level Bootstrapping} |
| 1331 | ;;; |
| 1332 | |
| 1333 | ;; make-root-module |
| 1334 | |
| 1335 | ;; A root module uses the pre-modules-obarray as its obarray. This |
| 1336 | ;; special obarray accumulates all bindings that have been established |
| 1337 | ;; before the module system is fully booted. |
| 1338 | ;; |
| 1339 | ;; (The obarray continues to be used by code that has been closed over |
| 1340 | ;; before the module system has been booted.) |
| 1341 | |
| 1342 | (define (make-root-module) |
| 1343 | (let ((m (make-module 0))) |
| 1344 | (set-module-obarray! m (%get-pre-modules-obarray)) |
| 1345 | m)) |
| 1346 | |
| 1347 | ;; make-scm-module |
| 1348 | |
| 1349 | ;; The root interface is a module that uses the same obarray as the |
| 1350 | ;; root module. It does not allow new definitions, tho. |
| 1351 | |
| 1352 | (define (make-scm-module) |
| 1353 | (let ((m (make-module 0))) |
| 1354 | (set-module-obarray! m (%get-pre-modules-obarray)) |
| 1355 | (set-module-eval-closure! m (standard-interface-eval-closure m)) |
| 1356 | m)) |
| 1357 | |
| 1358 | |
| 1359 | \f |
| 1360 | ;;; {Module-based Loading} |
| 1361 | ;;; |
| 1362 | |
| 1363 | (define (save-module-excursion thunk) |
| 1364 | (let ((inner-module (current-module)) |
| 1365 | (outer-module #f)) |
| 1366 | (dynamic-wind (lambda () |
| 1367 | (set! outer-module (current-module)) |
| 1368 | (set-current-module inner-module) |
| 1369 | (set! inner-module #f)) |
| 1370 | thunk |
| 1371 | (lambda () |
| 1372 | (set! inner-module (current-module)) |
| 1373 | (set-current-module outer-module) |
| 1374 | (set! outer-module #f))))) |
| 1375 | |
| 1376 | (define basic-load load) |
| 1377 | |
| 1378 | (define (load-module filename) |
| 1379 | (save-module-excursion |
| 1380 | (lambda () |
| 1381 | (let ((oldname (and (current-load-port) |
| 1382 | (port-filename (current-load-port))))) |
| 1383 | (basic-load (if (and oldname |
| 1384 | (> (string-length filename) 0) |
| 1385 | (not (char=? (string-ref filename 0) #\/)) |
| 1386 | (not (string=? (dirname oldname) "."))) |
| 1387 | (string-append (dirname oldname) "/" filename) |
| 1388 | filename)))))) |
| 1389 | |
| 1390 | |
| 1391 | \f |
| 1392 | ;;; {MODULE-REF -- exported} |
| 1393 | ;; |
| 1394 | ;; Returns the value of a variable called NAME in MODULE or any of its |
| 1395 | ;; used modules. If there is no such variable, then if the optional third |
| 1396 | ;; argument DEFAULT is present, it is returned; otherwise an error is signaled. |
| 1397 | ;; |
| 1398 | (define (module-ref module name . rest) |
| 1399 | (let ((variable (module-variable module name))) |
| 1400 | (if (and variable (variable-bound? variable)) |
| 1401 | (variable-ref variable) |
| 1402 | (if (null? rest) |
| 1403 | (error "No variable named" name 'in module) |
| 1404 | (car rest) ; default value |
| 1405 | )))) |
| 1406 | |
| 1407 | ;; MODULE-SET! -- exported |
| 1408 | ;; |
| 1409 | ;; Sets the variable called NAME in MODULE (or in a module that MODULE uses) |
| 1410 | ;; to VALUE; if there is no such variable, an error is signaled. |
| 1411 | ;; |
| 1412 | (define (module-set! module name value) |
| 1413 | (let ((variable (module-variable module name))) |
| 1414 | (if variable |
| 1415 | (variable-set! variable value) |
| 1416 | (error "No variable named" name 'in module)))) |
| 1417 | |
| 1418 | ;; MODULE-DEFINE! -- exported |
| 1419 | ;; |
| 1420 | ;; Sets the variable called NAME in MODULE to VALUE; if there is no such |
| 1421 | ;; variable, it is added first. |
| 1422 | ;; |
| 1423 | (define (module-define! module name value) |
| 1424 | (let ((variable (module-local-variable module name))) |
| 1425 | (if variable |
| 1426 | (begin |
| 1427 | (variable-set! variable value) |
| 1428 | (module-modified module)) |
| 1429 | (let ((variable (make-variable value))) |
| 1430 | (module-add! module name variable))))) |
| 1431 | |
| 1432 | ;; MODULE-DEFINED? -- exported |
| 1433 | ;; |
| 1434 | ;; Return #t iff NAME is defined in MODULE (or in a module that MODULE |
| 1435 | ;; uses) |
| 1436 | ;; |
| 1437 | (define (module-defined? module name) |
| 1438 | (let ((variable (module-variable module name))) |
| 1439 | (and variable (variable-bound? variable)))) |
| 1440 | |
| 1441 | ;; MODULE-USE! module interface |
| 1442 | ;; |
| 1443 | ;; Add INTERFACE to the list of interfaces used by MODULE. |
| 1444 | ;; |
| 1445 | (define (module-use! module interface) |
| 1446 | (set-module-uses! module |
| 1447 | (cons interface |
| 1448 | (filter (lambda (m) |
| 1449 | (not (equal? (module-name m) |
| 1450 | (module-name interface)))) |
| 1451 | (module-uses module)))) |
| 1452 | (module-modified module)) |
| 1453 | |
| 1454 | ;; MODULE-USE-INTERFACES! module interfaces |
| 1455 | ;; |
| 1456 | ;; Same as MODULE-USE! but add multiple interfaces and check for duplicates |
| 1457 | ;; |
| 1458 | (define (module-use-interfaces! module interfaces) |
| 1459 | (let* ((duplicates-info (module-duplicates-info module)) |
| 1460 | (duplicates-handlers? (or (car duplicates-info) |
| 1461 | (default-duplicate-binding-procedures))) |
| 1462 | (uses (module-uses module))) |
| 1463 | ;; remove duplicates-interface |
| 1464 | (set! uses (delq! (cdr duplicates-info) uses)) |
| 1465 | ;; remove interfaces to be added |
| 1466 | (for-each (lambda (interface) |
| 1467 | (set! uses |
| 1468 | (filter (lambda (m) |
| 1469 | (not (equal? (module-name m) |
| 1470 | (module-name interface)))) |
| 1471 | uses))) |
| 1472 | interfaces) |
| 1473 | ;; add interfaces to use list |
| 1474 | (set-module-uses! module uses) |
| 1475 | (for-each (lambda (interface) |
| 1476 | (and duplicates-handlers? |
| 1477 | ;; perform duplicate checking |
| 1478 | (process-duplicates module interface)) |
| 1479 | (set! uses (cons interface uses)) |
| 1480 | (set-module-uses! module uses)) |
| 1481 | interfaces) |
| 1482 | ;; add duplicates interface |
| 1483 | (if (cdr duplicates-info) |
| 1484 | (set-module-uses! module (cons (cdr duplicates-info) uses))) |
| 1485 | (module-modified module))) |
| 1486 | |
| 1487 | \f |
| 1488 | ;;; {Recursive Namespaces} |
| 1489 | ;;; |
| 1490 | ;;; |
| 1491 | ;;; A hierarchical namespace emerges if we consider some module to be |
| 1492 | ;;; root, and variables bound to modules as nested namespaces. |
| 1493 | ;;; |
| 1494 | ;;; The routines in this file manage variable names in hierarchical namespace. |
| 1495 | ;;; Each variable name is a list of elements, looked up in successively nested |
| 1496 | ;;; modules. |
| 1497 | ;;; |
| 1498 | ;;; (nested-ref some-root-module '(foo bar baz)) |
| 1499 | ;;; => <value of a variable named baz in the module bound to bar in |
| 1500 | ;;; the module bound to foo in some-root-module> |
| 1501 | ;;; |
| 1502 | ;;; |
| 1503 | ;;; There are: |
| 1504 | ;;; |
| 1505 | ;;; ;; a-root is a module |
| 1506 | ;;; ;; name is a list of symbols |
| 1507 | ;;; |
| 1508 | ;;; nested-ref a-root name |
| 1509 | ;;; nested-set! a-root name val |
| 1510 | ;;; nested-define! a-root name val |
| 1511 | ;;; nested-remove! a-root name |
| 1512 | ;;; |
| 1513 | ;;; |
| 1514 | ;;; (current-module) is a natural choice for a-root so for convenience there are |
| 1515 | ;;; also: |
| 1516 | ;;; |
| 1517 | ;;; local-ref name == nested-ref (current-module) name |
| 1518 | ;;; local-set! name val == nested-set! (current-module) name val |
| 1519 | ;;; local-define! name val == nested-define! (current-module) name val |
| 1520 | ;;; local-remove! name == nested-remove! (current-module) name |
| 1521 | ;;; |
| 1522 | |
| 1523 | |
| 1524 | (define (nested-ref root names) |
| 1525 | (let loop ((cur root) |
| 1526 | (elts names)) |
| 1527 | (cond |
| 1528 | ((null? elts) cur) |
| 1529 | ((not (module? cur)) #f) |
| 1530 | (else (loop (module-ref cur (car elts) #f) (cdr elts)))))) |
| 1531 | |
| 1532 | (define (nested-set! root names val) |
| 1533 | (let loop ((cur root) |
| 1534 | (elts names)) |
| 1535 | (if (null? (cdr elts)) |
| 1536 | (module-set! cur (car elts) val) |
| 1537 | (loop (module-ref cur (car elts)) (cdr elts))))) |
| 1538 | |
| 1539 | (define (nested-define! root names val) |
| 1540 | (let loop ((cur root) |
| 1541 | (elts names)) |
| 1542 | (if (null? (cdr elts)) |
| 1543 | (module-define! cur (car elts) val) |
| 1544 | (loop (module-ref cur (car elts)) (cdr elts))))) |
| 1545 | |
| 1546 | (define (nested-remove! root names) |
| 1547 | (let loop ((cur root) |
| 1548 | (elts names)) |
| 1549 | (if (null? (cdr elts)) |
| 1550 | (module-remove! cur (car elts)) |
| 1551 | (loop (module-ref cur (car elts)) (cdr elts))))) |
| 1552 | |
| 1553 | (define (local-ref names) (nested-ref (current-module) names)) |
| 1554 | (define (local-set! names val) (nested-set! (current-module) names val)) |
| 1555 | (define (local-define names val) (nested-define! (current-module) names val)) |
| 1556 | (define (local-remove names) (nested-remove! (current-module) names)) |
| 1557 | |
| 1558 | |
| 1559 | \f |
| 1560 | ;;; {The (app) module} |
| 1561 | ;;; |
| 1562 | ;;; The root of conventionally named objects not directly in the top level. |
| 1563 | ;;; |
| 1564 | ;;; (app modules) |
| 1565 | ;;; (app modules guile) |
| 1566 | ;;; |
| 1567 | ;;; The directory of all modules and the standard root module. |
| 1568 | ;;; |
| 1569 | |
| 1570 | (define (module-public-interface m) |
| 1571 | (module-ref m '%module-public-interface #f)) |
| 1572 | (define (set-module-public-interface! m i) |
| 1573 | (module-define! m '%module-public-interface i)) |
| 1574 | (define (module-duplicates-info m) |
| 1575 | (or (module-ref m '%module-duplicates-info #f) (cons #f #f))) |
| 1576 | (define (set-module-duplicates-info! m i) |
| 1577 | (module-define! m '%module-duplicates-info i)) |
| 1578 | (define (set-system-module! m s) |
| 1579 | (set-procedure-property! (module-eval-closure m) 'system-module s)) |
| 1580 | (define the-root-module (make-root-module)) |
| 1581 | (define the-scm-module (make-scm-module)) |
| 1582 | (set-module-public-interface! the-root-module the-scm-module) |
| 1583 | (set-module-duplicates-info! the-root-module (cons #f #f)) |
| 1584 | (set-module-name! the-root-module '(guile)) |
| 1585 | (set-module-name! the-scm-module '(guile)) |
| 1586 | (set-module-kind! the-scm-module 'interface) |
| 1587 | (for-each set-system-module! (list the-root-module the-scm-module) '(#t #t)) |
| 1588 | |
| 1589 | ;; NOTE: This binding is used in libguile/modules.c. |
| 1590 | ;; |
| 1591 | (define (make-modules-in module name) |
| 1592 | (if (null? name) |
| 1593 | module |
| 1594 | (cond |
| 1595 | ((module-ref module (car name) #f) |
| 1596 | => (lambda (m) (make-modules-in m (cdr name)))) |
| 1597 | (else (let ((m (make-module 31))) |
| 1598 | (set-module-kind! m 'directory) |
| 1599 | (set-module-name! m (append (or (module-name module) |
| 1600 | '()) |
| 1601 | (list (car name)))) |
| 1602 | (module-define! module (car name) m) |
| 1603 | (make-modules-in m (cdr name))))))) |
| 1604 | |
| 1605 | (define (beautify-user-module! module) |
| 1606 | (let ((interface (module-public-interface module))) |
| 1607 | (if (or (not interface) |
| 1608 | (eq? interface module)) |
| 1609 | (let ((interface (make-module 31))) |
| 1610 | (set-module-name! interface (module-name module)) |
| 1611 | (set-module-kind! interface 'interface) |
| 1612 | (set-module-public-interface! module interface) |
| 1613 | (set-module-duplicates-info! module (cons #f #f))))) |
| 1614 | (if (and (not (memq the-scm-module (module-uses module))) |
| 1615 | (not (eq? module the-root-module))) |
| 1616 | (set-module-uses! module |
| 1617 | (append (module-uses module) (list the-scm-module))))) |
| 1618 | |
| 1619 | ;; NOTE: This binding is used in libguile/modules.c. |
| 1620 | ;; |
| 1621 | (define (resolve-module name . maybe-autoload) |
| 1622 | (let ((full-name (append '(app modules) name))) |
| 1623 | (let ((already (local-ref full-name))) |
| 1624 | (if already |
| 1625 | ;; The module already exists... |
| 1626 | (if (and (or (null? maybe-autoload) (car maybe-autoload)) |
| 1627 | (not (module-public-interface already))) |
| 1628 | ;; ...but we are told to load and it doesn't contain source, so |
| 1629 | (begin |
| 1630 | (try-load-module name) |
| 1631 | already) |
| 1632 | ;; simply return it. |
| 1633 | already) |
| 1634 | (begin |
| 1635 | ;; Try to autoload it if we are told so |
| 1636 | (if (or (null? maybe-autoload) (car maybe-autoload)) |
| 1637 | (try-load-module name)) |
| 1638 | ;; Get/create it. |
| 1639 | (make-modules-in (current-module) full-name)))))) |
| 1640 | |
| 1641 | ;; Cheat. These bindings are needed by modules.c, but we don't want |
| 1642 | ;; to move their real definition here because that would be unnatural. |
| 1643 | ;; |
| 1644 | (define try-module-autoload #f) |
| 1645 | (define process-define-module #f) |
| 1646 | (define process-use-modules #f) |
| 1647 | (define module-export! #f) |
| 1648 | |
| 1649 | ;; This boots the module system. All bindings needed by modules.c |
| 1650 | ;; must have been defined by now. |
| 1651 | ;; |
| 1652 | (set-current-module the-root-module) |
| 1653 | |
| 1654 | (define app (make-module 31)) |
| 1655 | (local-define '(app modules) (make-module 31)) |
| 1656 | (local-define '(app modules guile) the-root-module) |
| 1657 | |
| 1658 | ;; (define-special-value '(app modules new-ws) (lambda () (make-scm-module))) |
| 1659 | |
| 1660 | (define (try-load-module name) |
| 1661 | (try-module-autoload name)) |
| 1662 | |
| 1663 | (define (purify-module! module) |
| 1664 | "Removes bindings in MODULE which are inherited from the (guile) module." |
| 1665 | (let ((use-list (module-uses module))) |
| 1666 | (if (and (pair? use-list) |
| 1667 | (eq? (car (last-pair use-list)) the-scm-module)) |
| 1668 | (set-module-uses! module (reverse (cdr (reverse use-list))))))) |
| 1669 | |
| 1670 | ;; Return a module that is an interface to the module designated by |
| 1671 | ;; NAME. |
| 1672 | ;; |
| 1673 | ;; `resolve-interface' takes four keyword arguments: |
| 1674 | ;; |
| 1675 | ;; #:select SELECTION |
| 1676 | ;; |
| 1677 | ;; SELECTION is a list of binding-specs to be imported; A binding-spec |
| 1678 | ;; is either a symbol or a pair of symbols (ORIG . SEEN), where ORIG |
| 1679 | ;; is the name in the used module and SEEN is the name in the using |
| 1680 | ;; module. Note that SEEN is also passed through RENAMER, below. The |
| 1681 | ;; default is to select all bindings. If you specify no selection but |
| 1682 | ;; a renamer, only the bindings that already exist in the used module |
| 1683 | ;; are made available in the interface. Bindings that are added later |
| 1684 | ;; are not picked up. |
| 1685 | ;; |
| 1686 | ;; #:hide BINDINGS |
| 1687 | ;; |
| 1688 | ;; BINDINGS is a list of bindings which should not be imported. |
| 1689 | ;; |
| 1690 | ;; #:prefix PREFIX |
| 1691 | ;; |
| 1692 | ;; PREFIX is a symbol that will be appended to each exported name. |
| 1693 | ;; The default is to not perform any renaming. |
| 1694 | ;; |
| 1695 | ;; #:renamer RENAMER |
| 1696 | ;; |
| 1697 | ;; RENAMER is a procedure that takes a symbol and returns its new |
| 1698 | ;; name. The default is not perform any renaming. |
| 1699 | ;; |
| 1700 | ;; Signal "no code for module" error if module name is not resolvable |
| 1701 | ;; or its public interface is not available. Signal "no binding" |
| 1702 | ;; error if selected binding does not exist in the used module. |
| 1703 | ;; |
| 1704 | (define (resolve-interface name . args) |
| 1705 | |
| 1706 | (define (get-keyword-arg args kw def) |
| 1707 | (cond ((memq kw args) |
| 1708 | => (lambda (kw-arg) |
| 1709 | (if (null? (cdr kw-arg)) |
| 1710 | (error "keyword without value: " kw)) |
| 1711 | (cadr kw-arg))) |
| 1712 | (else |
| 1713 | def))) |
| 1714 | |
| 1715 | (let* ((select (get-keyword-arg args #:select #f)) |
| 1716 | (hide (get-keyword-arg args #:hide '())) |
| 1717 | (renamer (or (get-keyword-arg args #:renamer #f) |
| 1718 | (let ((prefix (get-keyword-arg args #:prefix #f))) |
| 1719 | (and prefix (symbol-prefix-proc prefix))) |
| 1720 | identity)) |
| 1721 | (module (resolve-module name)) |
| 1722 | (public-i (and module (module-public-interface module)))) |
| 1723 | (and (or (not module) (not public-i)) |
| 1724 | (error "no code for module" name)) |
| 1725 | (if (and (not select) (null? hide) (eq? renamer identity)) |
| 1726 | public-i |
| 1727 | (let ((selection (or select (module-map (lambda (sym var) sym) |
| 1728 | public-i))) |
| 1729 | (custom-i (make-module 31))) |
| 1730 | (set-module-kind! custom-i 'custom-interface) |
| 1731 | (set-module-name! custom-i name) |
| 1732 | ;; XXX - should use a lazy binder so that changes to the |
| 1733 | ;; used module are picked up automatically. |
| 1734 | (for-each (lambda (bspec) |
| 1735 | (let* ((direct? (symbol? bspec)) |
| 1736 | (orig (if direct? bspec (car bspec))) |
| 1737 | (seen (if direct? bspec (cdr bspec))) |
| 1738 | (var (or (module-local-variable public-i orig) |
| 1739 | (module-local-variable module orig) |
| 1740 | (error |
| 1741 | ;; fixme: format manually for now |
| 1742 | (simple-format |
| 1743 | #f "no binding `~A' in module ~A" |
| 1744 | orig name))))) |
| 1745 | (if (memq orig hide) |
| 1746 | (set! hide (delq! orig hide)) |
| 1747 | (module-add! custom-i |
| 1748 | (renamer seen) |
| 1749 | var)))) |
| 1750 | selection) |
| 1751 | ;; Check that we are not hiding bindings which don't exist |
| 1752 | (for-each (lambda (binding) |
| 1753 | (if (not (module-local-variable public-i binding)) |
| 1754 | (error |
| 1755 | (simple-format |
| 1756 | #f "no binding `~A' to hide in module ~A" |
| 1757 | binding name)))) |
| 1758 | hide) |
| 1759 | custom-i)))) |
| 1760 | |
| 1761 | (define (symbol-prefix-proc prefix) |
| 1762 | (lambda (symbol) |
| 1763 | (symbol-append prefix symbol))) |
| 1764 | |
| 1765 | ;; This function is called from "modules.c". If you change it, be |
| 1766 | ;; sure to update "modules.c" as well. |
| 1767 | |
| 1768 | (define (process-define-module args) |
| 1769 | (let* ((module-id (car args)) |
| 1770 | (module (resolve-module module-id #f)) |
| 1771 | (kws (cdr args)) |
| 1772 | (unrecognized (lambda (arg) |
| 1773 | (error "unrecognized define-module argument" arg)))) |
| 1774 | (beautify-user-module! module) |
| 1775 | (let loop ((kws kws) |
| 1776 | (reversed-interfaces '()) |
| 1777 | (exports '()) |
| 1778 | (re-exports '()) |
| 1779 | (replacements '())) |
| 1780 | (if (null? kws) |
| 1781 | (begin |
| 1782 | (module-use-interfaces! module (reverse reversed-interfaces)) |
| 1783 | (module-export! module exports) |
| 1784 | (module-replace! module replacements) |
| 1785 | (module-re-export! module re-exports)) |
| 1786 | (case (car kws) |
| 1787 | ((#:use-module #:use-syntax) |
| 1788 | (or (pair? (cdr kws)) |
| 1789 | (unrecognized kws)) |
| 1790 | (let* ((interface-args (cadr kws)) |
| 1791 | (interface (apply resolve-interface interface-args))) |
| 1792 | (and (eq? (car kws) #:use-syntax) |
| 1793 | (or (symbol? (caar interface-args)) |
| 1794 | (error "invalid module name for use-syntax" |
| 1795 | (car interface-args))) |
| 1796 | (set-module-transformer! |
| 1797 | module |
| 1798 | (module-ref interface |
| 1799 | (car (last-pair (car interface-args))) |
| 1800 | #f))) |
| 1801 | (loop (cddr kws) |
| 1802 | (cons interface reversed-interfaces) |
| 1803 | exports |
| 1804 | re-exports |
| 1805 | replacements))) |
| 1806 | ((#:autoload) |
| 1807 | (or (and (pair? (cdr kws)) (pair? (cddr kws))) |
| 1808 | (unrecognized kws)) |
| 1809 | (loop (cdddr kws) |
| 1810 | (cons (make-autoload-interface module |
| 1811 | (cadr kws) |
| 1812 | (caddr kws)) |
| 1813 | reversed-interfaces) |
| 1814 | exports |
| 1815 | re-exports |
| 1816 | replacements)) |
| 1817 | ((#:no-backtrace) |
| 1818 | (set-system-module! module #t) |
| 1819 | (loop (cdr kws) reversed-interfaces exports re-exports replacements)) |
| 1820 | ((#:pure) |
| 1821 | (purify-module! module) |
| 1822 | (loop (cdr kws) reversed-interfaces exports re-exports replacements)) |
| 1823 | ((#:duplicates) |
| 1824 | (if (not (pair? (cdr kws))) |
| 1825 | (unrecognized kws)) |
| 1826 | (set-car! (module-duplicates-info module) |
| 1827 | (lookup-duplicates-handlers (cadr kws))) |
| 1828 | (loop (cddr kws) reversed-interfaces exports re-exports replacements)) |
| 1829 | ((#:export #:export-syntax) |
| 1830 | (or (pair? (cdr kws)) |
| 1831 | (unrecognized kws)) |
| 1832 | (loop (cddr kws) |
| 1833 | reversed-interfaces |
| 1834 | (append (cadr kws) exports) |
| 1835 | re-exports |
| 1836 | replacements)) |
| 1837 | ((#:re-export #:re-export-syntax) |
| 1838 | (or (pair? (cdr kws)) |
| 1839 | (unrecognized kws)) |
| 1840 | (loop (cddr kws) |
| 1841 | reversed-interfaces |
| 1842 | exports |
| 1843 | (append (cadr kws) re-exports) |
| 1844 | replacements)) |
| 1845 | ((#:replace #:replace-syntax) |
| 1846 | (or (pair? (cdr kws)) |
| 1847 | (unrecognized kws)) |
| 1848 | (loop (cddr kws) |
| 1849 | reversed-interfaces |
| 1850 | exports |
| 1851 | re-exports |
| 1852 | (append (cadr kws) replacements))) |
| 1853 | (else |
| 1854 | (unrecognized kws))))) |
| 1855 | (run-hook module-defined-hook module) |
| 1856 | module)) |
| 1857 | |
| 1858 | ;; `module-defined-hook' is a hook that is run whenever a new module |
| 1859 | ;; is defined. Its members are called with one argument, the new |
| 1860 | ;; module. |
| 1861 | (define module-defined-hook (make-hook 1)) |
| 1862 | |
| 1863 | ;;; {Autoload} |
| 1864 | |
| 1865 | (define (make-autoload-interface module name bindings) |
| 1866 | (let ((b (lambda (a sym definep) |
| 1867 | (and (memq sym bindings) |
| 1868 | (let ((i (module-public-interface (resolve-module name)))) |
| 1869 | (if (not i) |
| 1870 | (error "missing interface for module" name)) |
| 1871 | ;; Replace autoload-interface with interface |
| 1872 | (set-car! (memq a (module-uses module)) i) |
| 1873 | (module-local-variable i sym)))))) |
| 1874 | (module-constructor #() '() b #f #f name 'autoload |
| 1875 | '() (make-weak-value-hash-table 31) 0))) |
| 1876 | |
| 1877 | ;;; {Compiled module} |
| 1878 | |
| 1879 | (define load-compiled #f) |
| 1880 | |
| 1881 | \f |
| 1882 | ;;; {Autoloading modules} |
| 1883 | |
| 1884 | (define autoloads-in-progress '()) |
| 1885 | |
| 1886 | ;; This function is called from "modules.c". If you change it, be |
| 1887 | ;; sure to update "modules.c" as well. |
| 1888 | |
| 1889 | (define (try-module-autoload module-name) |
| 1890 | (let* ((reverse-name (reverse module-name)) |
| 1891 | (name (symbol->string (car reverse-name))) |
| 1892 | (dir-hint-module-name (reverse (cdr reverse-name))) |
| 1893 | (dir-hint (apply string-append |
| 1894 | (map (lambda (elt) |
| 1895 | (string-append (symbol->string elt) "/")) |
| 1896 | dir-hint-module-name)))) |
| 1897 | (resolve-module dir-hint-module-name #f) |
| 1898 | (and (not (autoload-done-or-in-progress? dir-hint name)) |
| 1899 | (let ((didit #f)) |
| 1900 | (define (load-file proc file) |
| 1901 | (save-module-excursion (lambda () (proc file))) |
| 1902 | (set! didit #t)) |
| 1903 | (dynamic-wind |
| 1904 | (lambda () (autoload-in-progress! dir-hint name)) |
| 1905 | (lambda () |
| 1906 | (let ((file (in-vicinity dir-hint name))) |
| 1907 | (cond ((and load-compiled |
| 1908 | (%search-load-path (string-append file ".go"))) |
| 1909 | => (lambda (full) |
| 1910 | (load-file load-compiled full))) |
| 1911 | ((%search-load-path file) |
| 1912 | => (lambda (full) |
| 1913 | (load-file primitive-load full)))))) |
| 1914 | (lambda () (set-autoloaded! dir-hint name didit))) |
| 1915 | didit)))) |
| 1916 | |
| 1917 | \f |
| 1918 | ;;; Dynamic linking of modules |
| 1919 | |
| 1920 | (define autoloads-done '((guile . guile))) |
| 1921 | |
| 1922 | (define (autoload-done-or-in-progress? p m) |
| 1923 | (let ((n (cons p m))) |
| 1924 | (->bool (or (member n autoloads-done) |
| 1925 | (member n autoloads-in-progress))))) |
| 1926 | |
| 1927 | (define (autoload-done! p m) |
| 1928 | (let ((n (cons p m))) |
| 1929 | (set! autoloads-in-progress |
| 1930 | (delete! n autoloads-in-progress)) |
| 1931 | (or (member n autoloads-done) |
| 1932 | (set! autoloads-done (cons n autoloads-done))))) |
| 1933 | |
| 1934 | (define (autoload-in-progress! p m) |
| 1935 | (let ((n (cons p m))) |
| 1936 | (set! autoloads-done |
| 1937 | (delete! n autoloads-done)) |
| 1938 | (set! autoloads-in-progress (cons n autoloads-in-progress)))) |
| 1939 | |
| 1940 | (define (set-autoloaded! p m done?) |
| 1941 | (if done? |
| 1942 | (autoload-done! p m) |
| 1943 | (let ((n (cons p m))) |
| 1944 | (set! autoloads-done (delete! n autoloads-done)) |
| 1945 | (set! autoloads-in-progress (delete! n autoloads-in-progress))))) |
| 1946 | |
| 1947 | |
| 1948 | |
| 1949 | \f |
| 1950 | ;; {EVAL-CASE} |
| 1951 | ;; |
| 1952 | ;; (eval-case ((situation*) forms)* (else forms)?) |
| 1953 | ;; |
| 1954 | ;; Evaluate certain code based on the situation that eval-case is used |
| 1955 | ;; in. The only defined situation right now is `load-toplevel' which |
| 1956 | ;; triggers for code evaluated at the top-level, for example from the |
| 1957 | ;; REPL or when loading a file. |
| 1958 | |
| 1959 | (define eval-case |
| 1960 | (procedure->memoizing-macro |
| 1961 | (lambda (exp env) |
| 1962 | (define (toplevel-env? env) |
| 1963 | (or (not (pair? env)) (not (pair? (car env))))) |
| 1964 | (define (syntax) |
| 1965 | (error "syntax error in eval-case")) |
| 1966 | (let loop ((clauses (cdr exp))) |
| 1967 | (cond |
| 1968 | ((null? clauses) |
| 1969 | #f) |
| 1970 | ((not (list? (car clauses))) |
| 1971 | (syntax)) |
| 1972 | ((eq? 'else (caar clauses)) |
| 1973 | (or (null? (cdr clauses)) |
| 1974 | (syntax)) |
| 1975 | (cons 'begin (cdar clauses))) |
| 1976 | ((not (list? (caar clauses))) |
| 1977 | (syntax)) |
| 1978 | ((and (toplevel-env? env) |
| 1979 | (memq 'load-toplevel (caar clauses))) |
| 1980 | (cons 'begin (cdar clauses))) |
| 1981 | (else |
| 1982 | (loop (cdr clauses)))))))) |
| 1983 | |
| 1984 | \f |
| 1985 | ;;; {Macros} |
| 1986 | ;;; |
| 1987 | |
| 1988 | (define (primitive-macro? m) |
| 1989 | (and (macro? m) |
| 1990 | (not (macro-transformer m)))) |
| 1991 | |
| 1992 | ;;; {Defmacros} |
| 1993 | ;;; |
| 1994 | (define macro-table (make-weak-key-hash-table 61)) |
| 1995 | (define xformer-table (make-weak-key-hash-table 61)) |
| 1996 | |
| 1997 | (define (defmacro? m) (hashq-ref macro-table m)) |
| 1998 | (define (assert-defmacro?! m) (hashq-set! macro-table m #t)) |
| 1999 | (define (defmacro-transformer m) (hashq-ref xformer-table m)) |
| 2000 | (define (set-defmacro-transformer! m t) (hashq-set! xformer-table m t)) |
| 2001 | |
| 2002 | (define defmacro:transformer |
| 2003 | (lambda (f) |
| 2004 | (let* ((xform (lambda (exp env) |
| 2005 | (copy-tree (apply f (cdr exp))))) |
| 2006 | (a (procedure->memoizing-macro xform))) |
| 2007 | (assert-defmacro?! a) |
| 2008 | (set-defmacro-transformer! a f) |
| 2009 | a))) |
| 2010 | |
| 2011 | |
| 2012 | (define defmacro |
| 2013 | (let ((defmacro-transformer |
| 2014 | (lambda (name parms . body) |
| 2015 | (let ((transformer `(lambda ,parms ,@body))) |
| 2016 | `(eval-case |
| 2017 | ((load-toplevel) |
| 2018 | (define ,name (defmacro:transformer ,transformer))) |
| 2019 | (else |
| 2020 | (error "defmacro can only be used at the top level"))))))) |
| 2021 | (defmacro:transformer defmacro-transformer))) |
| 2022 | |
| 2023 | (define defmacro:syntax-transformer |
| 2024 | (lambda (f) |
| 2025 | (procedure->syntax |
| 2026 | (lambda (exp env) |
| 2027 | (copy-tree (apply f (cdr exp))))))) |
| 2028 | |
| 2029 | |
| 2030 | ;; XXX - should the definition of the car really be looked up in the |
| 2031 | ;; current module? |
| 2032 | |
| 2033 | (define (macroexpand-1 e) |
| 2034 | (cond |
| 2035 | ((pair? e) (let* ((a (car e)) |
| 2036 | (val (and (symbol? a) (local-ref (list a))))) |
| 2037 | (if (defmacro? val) |
| 2038 | (apply (defmacro-transformer val) (cdr e)) |
| 2039 | e))) |
| 2040 | (#t e))) |
| 2041 | |
| 2042 | (define (macroexpand e) |
| 2043 | (cond |
| 2044 | ((pair? e) (let* ((a (car e)) |
| 2045 | (val (and (symbol? a) (local-ref (list a))))) |
| 2046 | (if (defmacro? val) |
| 2047 | (macroexpand (apply (defmacro-transformer val) (cdr e))) |
| 2048 | e))) |
| 2049 | (#t e))) |
| 2050 | |
| 2051 | (provide 'defmacro) |
| 2052 | |
| 2053 | \f |
| 2054 | |
| 2055 | ;;; {Run-time options} |
| 2056 | |
| 2057 | (define define-option-interface |
| 2058 | (let* ((option-name car) |
| 2059 | (option-value cadr) |
| 2060 | (option-documentation caddr) |
| 2061 | |
| 2062 | (print-option (lambda (option) |
| 2063 | (display (option-name option)) |
| 2064 | (if (< (string-length |
| 2065 | (symbol->string (option-name option))) |
| 2066 | 8) |
| 2067 | (display #\tab)) |
| 2068 | (display #\tab) |
| 2069 | (display (option-value option)) |
| 2070 | (display #\tab) |
| 2071 | (display (option-documentation option)) |
| 2072 | (newline))) |
| 2073 | |
| 2074 | ;; Below follow the macros defining the run-time option interfaces. |
| 2075 | |
| 2076 | (make-options (lambda (interface) |
| 2077 | `(lambda args |
| 2078 | (cond ((null? args) (,interface)) |
| 2079 | ((list? (car args)) |
| 2080 | (,interface (car args)) (,interface)) |
| 2081 | (else (for-each ,print-option |
| 2082 | (,interface #t))))))) |
| 2083 | |
| 2084 | (make-enable (lambda (interface) |
| 2085 | `(lambda flags |
| 2086 | (,interface (append flags (,interface))) |
| 2087 | (,interface)))) |
| 2088 | |
| 2089 | (make-disable (lambda (interface) |
| 2090 | `(lambda flags |
| 2091 | (let ((options (,interface))) |
| 2092 | (for-each (lambda (flag) |
| 2093 | (set! options (delq! flag options))) |
| 2094 | flags) |
| 2095 | (,interface options) |
| 2096 | (,interface)))))) |
| 2097 | (procedure->memoizing-macro |
| 2098 | (lambda (exp env) |
| 2099 | (let* ((option-group (cadr exp)) |
| 2100 | (interface (car option-group)) |
| 2101 | (options/enable/disable (cadr option-group))) |
| 2102 | `(begin |
| 2103 | (define ,(car options/enable/disable) |
| 2104 | ,(make-options interface)) |
| 2105 | (define ,(cadr options/enable/disable) |
| 2106 | ,(make-enable interface)) |
| 2107 | (define ,(caddr options/enable/disable) |
| 2108 | ,(make-disable interface)) |
| 2109 | (defmacro ,(caaddr option-group) (opt val) |
| 2110 | `(,,(car options/enable/disable) |
| 2111 | (append (,,(car options/enable/disable)) |
| 2112 | (list ',opt ,val)))))))))) |
| 2113 | |
| 2114 | (define-option-interface |
| 2115 | (eval-options-interface |
| 2116 | (eval-options eval-enable eval-disable) |
| 2117 | (eval-set!))) |
| 2118 | |
| 2119 | (define-option-interface |
| 2120 | (debug-options-interface |
| 2121 | (debug-options debug-enable debug-disable) |
| 2122 | (debug-set!))) |
| 2123 | |
| 2124 | (define-option-interface |
| 2125 | (evaluator-traps-interface |
| 2126 | (traps trap-enable trap-disable) |
| 2127 | (trap-set!))) |
| 2128 | |
| 2129 | (define-option-interface |
| 2130 | (read-options-interface |
| 2131 | (read-options read-enable read-disable) |
| 2132 | (read-set!))) |
| 2133 | |
| 2134 | (define-option-interface |
| 2135 | (print-options-interface |
| 2136 | (print-options print-enable print-disable) |
| 2137 | (print-set!))) |
| 2138 | |
| 2139 | \f |
| 2140 | |
| 2141 | ;;; {Running Repls} |
| 2142 | ;;; |
| 2143 | |
| 2144 | (define (repl read evaler print) |
| 2145 | (let loop ((source (read (current-input-port)))) |
| 2146 | (print (evaler source)) |
| 2147 | (loop (read (current-input-port))))) |
| 2148 | |
| 2149 | ;; A provisional repl that acts like the SCM repl: |
| 2150 | ;; |
| 2151 | (define scm-repl-silent #f) |
| 2152 | (define (assert-repl-silence v) (set! scm-repl-silent v)) |
| 2153 | |
| 2154 | (define *unspecified* (if #f #f)) |
| 2155 | (define (unspecified? v) (eq? v *unspecified*)) |
| 2156 | |
| 2157 | (define scm-repl-print-unspecified #f) |
| 2158 | (define (assert-repl-print-unspecified v) (set! scm-repl-print-unspecified v)) |
| 2159 | |
| 2160 | (define scm-repl-verbose #f) |
| 2161 | (define (assert-repl-verbosity v) (set! scm-repl-verbose v)) |
| 2162 | |
| 2163 | (define scm-repl-prompt "guile> ") |
| 2164 | |
| 2165 | (define (set-repl-prompt! v) (set! scm-repl-prompt v)) |
| 2166 | |
| 2167 | (define (default-lazy-handler key . args) |
| 2168 | (save-stack lazy-handler-dispatch) |
| 2169 | (apply throw key args)) |
| 2170 | |
| 2171 | (define (lazy-handler-dispatch key . args) |
| 2172 | (apply default-lazy-handler key args)) |
| 2173 | |
| 2174 | (define abort-hook (make-hook)) |
| 2175 | |
| 2176 | ;; these definitions are used if running a script. |
| 2177 | ;; otherwise redefined in error-catching-loop. |
| 2178 | (define (set-batch-mode?! arg) #t) |
| 2179 | (define (batch-mode?) #t) |
| 2180 | |
| 2181 | (define (error-catching-loop thunk) |
| 2182 | (let ((status #f) |
| 2183 | (interactive #t)) |
| 2184 | (define (loop first) |
| 2185 | (let ((next |
| 2186 | (catch #t |
| 2187 | |
| 2188 | (lambda () |
| 2189 | (lazy-catch #t |
| 2190 | (lambda () |
| 2191 | (call-with-unblocked-asyncs |
| 2192 | (lambda () |
| 2193 | (with-traps |
| 2194 | (lambda () |
| 2195 | (first) |
| 2196 | |
| 2197 | ;; This line is needed because mark |
| 2198 | ;; doesn't do closures quite right. |
| 2199 | ;; Unreferenced locals should be |
| 2200 | ;; collected. |
| 2201 | ;; |
| 2202 | (set! first #f) |
| 2203 | (let loop ((v (thunk))) |
| 2204 | (loop (thunk))) |
| 2205 | #f))))) |
| 2206 | |
| 2207 | lazy-handler-dispatch)) |
| 2208 | |
| 2209 | (lambda (key . args) |
| 2210 | (case key |
| 2211 | ((quit) |
| 2212 | (set! status args) |
| 2213 | #f) |
| 2214 | |
| 2215 | ((switch-repl) |
| 2216 | (apply throw 'switch-repl args)) |
| 2217 | |
| 2218 | ((abort) |
| 2219 | ;; This is one of the closures that require |
| 2220 | ;; (set! first #f) above |
| 2221 | ;; |
| 2222 | (lambda () |
| 2223 | (run-hook abort-hook) |
| 2224 | (force-output (current-output-port)) |
| 2225 | (display "ABORT: " (current-error-port)) |
| 2226 | (write args (current-error-port)) |
| 2227 | (newline (current-error-port)) |
| 2228 | (if interactive |
| 2229 | (begin |
| 2230 | (if (and |
| 2231 | (not has-shown-debugger-hint?) |
| 2232 | (not (memq 'backtrace |
| 2233 | (debug-options-interface))) |
| 2234 | (stack? (fluid-ref the-last-stack))) |
| 2235 | (begin |
| 2236 | (newline (current-error-port)) |
| 2237 | (display |
| 2238 | "Type \"(backtrace)\" to get more information or \"(debug)\" to enter the debugger.\n" |
| 2239 | (current-error-port)) |
| 2240 | (set! has-shown-debugger-hint? #t))) |
| 2241 | (force-output (current-error-port))) |
| 2242 | (begin |
| 2243 | (primitive-exit 1))) |
| 2244 | (set! stack-saved? #f))) |
| 2245 | |
| 2246 | (else |
| 2247 | ;; This is the other cons-leak closure... |
| 2248 | (lambda () |
| 2249 | (cond ((= (length args) 4) |
| 2250 | (apply handle-system-error key args)) |
| 2251 | (else |
| 2252 | (apply bad-throw key args)))))))))) |
| 2253 | (if next (loop next) status))) |
| 2254 | (set! set-batch-mode?! (lambda (arg) |
| 2255 | (cond (arg |
| 2256 | (set! interactive #f) |
| 2257 | (restore-signals)) |
| 2258 | (#t |
| 2259 | (error "sorry, not implemented"))))) |
| 2260 | (set! batch-mode? (lambda () (not interactive))) |
| 2261 | (call-with-blocked-asyncs |
| 2262 | (lambda () (loop (lambda () #t)))))) |
| 2263 | |
| 2264 | ;;(define the-last-stack (make-fluid)) Defined by scm_init_backtrace () |
| 2265 | (define before-signal-stack (make-fluid)) |
| 2266 | (define stack-saved? #f) |
| 2267 | |
| 2268 | (define (save-stack . narrowing) |
| 2269 | (or stack-saved? |
| 2270 | (cond ((not (memq 'debug (debug-options-interface))) |
| 2271 | (fluid-set! the-last-stack #f) |
| 2272 | (set! stack-saved? #t)) |
| 2273 | (else |
| 2274 | (fluid-set! |
| 2275 | the-last-stack |
| 2276 | (case (stack-id #t) |
| 2277 | ((repl-stack) |
| 2278 | (apply make-stack #t save-stack primitive-eval #t 0 narrowing)) |
| 2279 | ((load-stack) |
| 2280 | (apply make-stack #t save-stack 0 #t 0 narrowing)) |
| 2281 | ((tk-stack) |
| 2282 | (apply make-stack #t save-stack tk-stack-mark #t 0 narrowing)) |
| 2283 | ((#t) |
| 2284 | (apply make-stack #t save-stack 0 1 narrowing)) |
| 2285 | (else |
| 2286 | (let ((id (stack-id #t))) |
| 2287 | (and (procedure? id) |
| 2288 | (apply make-stack #t save-stack id #t 0 narrowing)))))) |
| 2289 | (set! stack-saved? #t))))) |
| 2290 | |
| 2291 | (define before-error-hook (make-hook)) |
| 2292 | (define after-error-hook (make-hook)) |
| 2293 | (define before-backtrace-hook (make-hook)) |
| 2294 | (define after-backtrace-hook (make-hook)) |
| 2295 | |
| 2296 | (define has-shown-debugger-hint? #f) |
| 2297 | |
| 2298 | (define (handle-system-error key . args) |
| 2299 | (let ((cep (current-error-port))) |
| 2300 | (cond ((not (stack? (fluid-ref the-last-stack)))) |
| 2301 | ((memq 'backtrace (debug-options-interface)) |
| 2302 | (run-hook before-backtrace-hook) |
| 2303 | (newline cep) |
| 2304 | (display "Backtrace:\n") |
| 2305 | (display-backtrace (fluid-ref the-last-stack) cep) |
| 2306 | (newline cep) |
| 2307 | (run-hook after-backtrace-hook))) |
| 2308 | (run-hook before-error-hook) |
| 2309 | (apply display-error (fluid-ref the-last-stack) cep args) |
| 2310 | (run-hook after-error-hook) |
| 2311 | (force-output cep) |
| 2312 | (throw 'abort key))) |
| 2313 | |
| 2314 | (define (quit . args) |
| 2315 | (apply throw 'quit args)) |
| 2316 | |
| 2317 | (define exit quit) |
| 2318 | |
| 2319 | ;;(define has-shown-backtrace-hint? #f) Defined by scm_init_backtrace () |
| 2320 | |
| 2321 | ;; Replaced by C code: |
| 2322 | ;;(define (backtrace) |
| 2323 | ;; (if (fluid-ref the-last-stack) |
| 2324 | ;; (begin |
| 2325 | ;; (newline) |
| 2326 | ;; (display-backtrace (fluid-ref the-last-stack) (current-output-port)) |
| 2327 | ;; (newline) |
| 2328 | ;; (if (and (not has-shown-backtrace-hint?) |
| 2329 | ;; (not (memq 'backtrace (debug-options-interface)))) |
| 2330 | ;; (begin |
| 2331 | ;; (display |
| 2332 | ;;"Type \"(debug-enable 'backtrace)\" if you would like a backtrace |
| 2333 | ;;automatically if an error occurs in the future.\n") |
| 2334 | ;; (set! has-shown-backtrace-hint? #t)))) |
| 2335 | ;; (display "No backtrace available.\n"))) |
| 2336 | |
| 2337 | (define (error-catching-repl r e p) |
| 2338 | (error-catching-loop |
| 2339 | (lambda () |
| 2340 | (call-with-values (lambda () (e (r))) |
| 2341 | (lambda the-values (for-each p the-values)))))) |
| 2342 | |
| 2343 | (define (gc-run-time) |
| 2344 | (cdr (assq 'gc-time-taken (gc-stats)))) |
| 2345 | |
| 2346 | (define before-read-hook (make-hook)) |
| 2347 | (define after-read-hook (make-hook)) |
| 2348 | (define before-eval-hook (make-hook 1)) |
| 2349 | (define after-eval-hook (make-hook 1)) |
| 2350 | (define before-print-hook (make-hook 1)) |
| 2351 | (define after-print-hook (make-hook 1)) |
| 2352 | |
| 2353 | ;;; The default repl-reader function. We may override this if we've |
| 2354 | ;;; the readline library. |
| 2355 | (define repl-reader |
| 2356 | (lambda (prompt) |
| 2357 | (display prompt) |
| 2358 | (force-output) |
| 2359 | (run-hook before-read-hook) |
| 2360 | (read (current-input-port)))) |
| 2361 | |
| 2362 | (define (scm-style-repl) |
| 2363 | |
| 2364 | (letrec ( |
| 2365 | (start-gc-rt #f) |
| 2366 | (start-rt #f) |
| 2367 | (repl-report-start-timing (lambda () |
| 2368 | (set! start-gc-rt (gc-run-time)) |
| 2369 | (set! start-rt (get-internal-run-time)))) |
| 2370 | (repl-report (lambda () |
| 2371 | (display ";;; ") |
| 2372 | (display (inexact->exact |
| 2373 | (* 1000 (/ (- (get-internal-run-time) start-rt) |
| 2374 | internal-time-units-per-second)))) |
| 2375 | (display " msec (") |
| 2376 | (display (inexact->exact |
| 2377 | (* 1000 (/ (- (gc-run-time) start-gc-rt) |
| 2378 | internal-time-units-per-second)))) |
| 2379 | (display " msec in gc)\n"))) |
| 2380 | |
| 2381 | (consume-trailing-whitespace |
| 2382 | (lambda () |
| 2383 | (let ((ch (peek-char))) |
| 2384 | (cond |
| 2385 | ((eof-object? ch)) |
| 2386 | ((or (char=? ch #\space) (char=? ch #\tab)) |
| 2387 | (read-char) |
| 2388 | (consume-trailing-whitespace)) |
| 2389 | ((char=? ch #\newline) |
| 2390 | (read-char)))))) |
| 2391 | (-read (lambda () |
| 2392 | (let ((val |
| 2393 | (let ((prompt (cond ((string? scm-repl-prompt) |
| 2394 | scm-repl-prompt) |
| 2395 | ((thunk? scm-repl-prompt) |
| 2396 | (scm-repl-prompt)) |
| 2397 | (scm-repl-prompt "> ") |
| 2398 | (else "")))) |
| 2399 | (repl-reader prompt)))) |
| 2400 | |
| 2401 | ;; As described in R4RS, the READ procedure updates the |
| 2402 | ;; port to point to the first character past the end of |
| 2403 | ;; the external representation of the object. This |
| 2404 | ;; means that it doesn't consume the newline typically |
| 2405 | ;; found after an expression. This means that, when |
| 2406 | ;; debugging Guile with GDB, GDB gets the newline, which |
| 2407 | ;; it often interprets as a "continue" command, making |
| 2408 | ;; breakpoints kind of useless. So, consume any |
| 2409 | ;; trailing newline here, as well as any whitespace |
| 2410 | ;; before it. |
| 2411 | ;; But not if EOF, for control-D. |
| 2412 | (if (not (eof-object? val)) |
| 2413 | (consume-trailing-whitespace)) |
| 2414 | (run-hook after-read-hook) |
| 2415 | (if (eof-object? val) |
| 2416 | (begin |
| 2417 | (repl-report-start-timing) |
| 2418 | (if scm-repl-verbose |
| 2419 | (begin |
| 2420 | (newline) |
| 2421 | (display ";;; EOF -- quitting") |
| 2422 | (newline))) |
| 2423 | (quit 0))) |
| 2424 | val))) |
| 2425 | |
| 2426 | (-eval (lambda (sourc) |
| 2427 | (repl-report-start-timing) |
| 2428 | (run-hook before-eval-hook sourc) |
| 2429 | (let ((val (start-stack 'repl-stack |
| 2430 | ;; If you change this procedure |
| 2431 | ;; (primitive-eval), please also |
| 2432 | ;; modify the repl-stack case in |
| 2433 | ;; save-stack so that stack cutting |
| 2434 | ;; continues to work. |
| 2435 | (primitive-eval sourc)))) |
| 2436 | (run-hook after-eval-hook sourc) |
| 2437 | val))) |
| 2438 | |
| 2439 | |
| 2440 | (-print (let ((maybe-print (lambda (result) |
| 2441 | (if (or scm-repl-print-unspecified |
| 2442 | (not (unspecified? result))) |
| 2443 | (begin |
| 2444 | (write result) |
| 2445 | (newline)))))) |
| 2446 | (lambda (result) |
| 2447 | (if (not scm-repl-silent) |
| 2448 | (begin |
| 2449 | (run-hook before-print-hook result) |
| 2450 | (maybe-print result) |
| 2451 | (run-hook after-print-hook result) |
| 2452 | (if scm-repl-verbose |
| 2453 | (repl-report)) |
| 2454 | (force-output)))))) |
| 2455 | |
| 2456 | (-quit (lambda (args) |
| 2457 | (if scm-repl-verbose |
| 2458 | (begin |
| 2459 | (display ";;; QUIT executed, repl exitting") |
| 2460 | (newline) |
| 2461 | (repl-report))) |
| 2462 | args)) |
| 2463 | |
| 2464 | (-abort (lambda () |
| 2465 | (if scm-repl-verbose |
| 2466 | (begin |
| 2467 | (display ";;; ABORT executed.") |
| 2468 | (newline) |
| 2469 | (repl-report))) |
| 2470 | (repl -read -eval -print)))) |
| 2471 | |
| 2472 | (let ((status (error-catching-repl -read |
| 2473 | -eval |
| 2474 | -print))) |
| 2475 | (-quit status)))) |
| 2476 | |
| 2477 | |
| 2478 | \f |
| 2479 | ;;; {IOTA functions: generating lists of numbers} |
| 2480 | |
| 2481 | (define (iota n) |
| 2482 | (let loop ((count (1- n)) (result '())) |
| 2483 | (if (< count 0) result |
| 2484 | (loop (1- count) (cons count result))))) |
| 2485 | |
| 2486 | \f |
| 2487 | ;;; {While} |
| 2488 | ;;; |
| 2489 | ;;; with `continue' and `break'. |
| 2490 | ;;; |
| 2491 | |
| 2492 | (defmacro while (cond . body) |
| 2493 | `(letrec ((continue (lambda () (or (not ,cond) (begin (begin ,@ body) (continue))))) |
| 2494 | (break (lambda val (apply throw 'break val)))) |
| 2495 | (catch 'break |
| 2496 | (lambda () (continue)) |
| 2497 | (lambda v (cadr v))))) |
| 2498 | |
| 2499 | ;;; {collect} |
| 2500 | ;;; |
| 2501 | ;;; Similar to `begin' but returns a list of the results of all constituent |
| 2502 | ;;; forms instead of the result of the last form. |
| 2503 | ;;; (The definition relies on the current left-to-right |
| 2504 | ;;; order of evaluation of operands in applications.) |
| 2505 | |
| 2506 | (defmacro collect forms |
| 2507 | (cons 'list forms)) |
| 2508 | |
| 2509 | ;;; {with-fluids} |
| 2510 | |
| 2511 | ;; with-fluids is a convenience wrapper for the builtin procedure |
| 2512 | ;; `with-fluids*'. The syntax is just like `let': |
| 2513 | ;; |
| 2514 | ;; (with-fluids ((fluid val) |
| 2515 | ;; ...) |
| 2516 | ;; body) |
| 2517 | |
| 2518 | (defmacro with-fluids (bindings . body) |
| 2519 | `(with-fluids* (list ,@(map car bindings)) (list ,@(map cadr bindings)) |
| 2520 | (lambda () ,@body))) |
| 2521 | |
| 2522 | \f |
| 2523 | |
| 2524 | ;;; {Macros} |
| 2525 | ;;; |
| 2526 | |
| 2527 | ;; actually....hobbit might be able to hack these with a little |
| 2528 | ;; coaxing |
| 2529 | ;; |
| 2530 | |
| 2531 | (defmacro define-macro (first . rest) |
| 2532 | (let ((name (if (symbol? first) first (car first))) |
| 2533 | (transformer |
| 2534 | (if (symbol? first) |
| 2535 | (car rest) |
| 2536 | `(lambda ,(cdr first) ,@rest)))) |
| 2537 | `(eval-case |
| 2538 | ((load-toplevel) |
| 2539 | (define ,name (defmacro:transformer ,transformer))) |
| 2540 | (else |
| 2541 | (error "define-macro can only be used at the top level"))))) |
| 2542 | |
| 2543 | |
| 2544 | (defmacro define-syntax-macro (first . rest) |
| 2545 | (let ((name (if (symbol? first) first (car first))) |
| 2546 | (transformer |
| 2547 | (if (symbol? first) |
| 2548 | (car rest) |
| 2549 | `(lambda ,(cdr first) ,@rest)))) |
| 2550 | `(eval-case |
| 2551 | ((load-toplevel) |
| 2552 | (define ,name (defmacro:syntax-transformer ,transformer))) |
| 2553 | (else |
| 2554 | (error "define-syntax-macro can only be used at the top level"))))) |
| 2555 | |
| 2556 | \f |
| 2557 | ;;; {Module System Macros} |
| 2558 | ;;; |
| 2559 | |
| 2560 | ;; Return a list of expressions that evaluate to the appropriate |
| 2561 | ;; arguments for resolve-interface according to SPEC. |
| 2562 | |
| 2563 | (define (compile-interface-spec spec) |
| 2564 | (define (make-keyarg sym key quote?) |
| 2565 | (cond ((or (memq sym spec) |
| 2566 | (memq key spec)) |
| 2567 | => (lambda (rest) |
| 2568 | (if quote? |
| 2569 | (list key (list 'quote (cadr rest))) |
| 2570 | (list key (cadr rest))))) |
| 2571 | (else |
| 2572 | '()))) |
| 2573 | (define (map-apply func list) |
| 2574 | (map (lambda (args) (apply func args)) list)) |
| 2575 | (define keys |
| 2576 | ;; sym key quote? |
| 2577 | '((:select #:select #t) |
| 2578 | (:hide #:hide #t) |
| 2579 | (:prefix #:prefix #t) |
| 2580 | (:renamer #:renamer #f))) |
| 2581 | (if (not (pair? (car spec))) |
| 2582 | `(',spec) |
| 2583 | `(',(car spec) |
| 2584 | ,@(apply append (map-apply make-keyarg keys))))) |
| 2585 | |
| 2586 | (define (keyword-like-symbol->keyword sym) |
| 2587 | (symbol->keyword (string->symbol (substring (symbol->string sym) 1)))) |
| 2588 | |
| 2589 | (define (compile-define-module-args args) |
| 2590 | ;; Just quote everything except #:use-module and #:use-syntax. We |
| 2591 | ;; need to know about all arguments regardless since we want to turn |
| 2592 | ;; symbols that look like keywords into real keywords, and the |
| 2593 | ;; keyword args in a define-module form are not regular |
| 2594 | ;; (i.e. no-backtrace doesn't take a value). |
| 2595 | (let loop ((compiled-args `((quote ,(car args)))) |
| 2596 | (args (cdr args))) |
| 2597 | (cond ((null? args) |
| 2598 | (reverse! compiled-args)) |
| 2599 | ;; symbol in keyword position |
| 2600 | ((symbol? (car args)) |
| 2601 | (loop compiled-args |
| 2602 | (cons (keyword-like-symbol->keyword (car args)) (cdr args)))) |
| 2603 | ((memq (car args) '(#:no-backtrace #:pure)) |
| 2604 | (loop (cons (car args) compiled-args) |
| 2605 | (cdr args))) |
| 2606 | ((null? (cdr args)) |
| 2607 | (error "keyword without value:" (car args))) |
| 2608 | ((memq (car args) '(#:use-module #:use-syntax)) |
| 2609 | (loop (cons* `(list ,@(compile-interface-spec (cadr args))) |
| 2610 | (car args) |
| 2611 | compiled-args) |
| 2612 | (cddr args))) |
| 2613 | ((eq? (car args) #:autoload) |
| 2614 | (loop (cons* `(quote ,(caddr args)) |
| 2615 | `(quote ,(cadr args)) |
| 2616 | (car args) |
| 2617 | compiled-args) |
| 2618 | (cdddr args))) |
| 2619 | (else |
| 2620 | (loop (cons* `(quote ,(cadr args)) |
| 2621 | (car args) |
| 2622 | compiled-args) |
| 2623 | (cddr args)))))) |
| 2624 | |
| 2625 | (defmacro define-module args |
| 2626 | `(eval-case |
| 2627 | ((load-toplevel) |
| 2628 | (let ((m (process-define-module |
| 2629 | (list ,@(compile-define-module-args args))))) |
| 2630 | (set-current-module m) |
| 2631 | m)) |
| 2632 | (else |
| 2633 | (error "define-module can only be used at the top level")))) |
| 2634 | |
| 2635 | ;; The guts of the use-modules macro. Add the interfaces of the named |
| 2636 | ;; modules to the use-list of the current module, in order. |
| 2637 | |
| 2638 | ;; This function is called by "modules.c". If you change it, be sure |
| 2639 | ;; to change scm_c_use_module as well. |
| 2640 | |
| 2641 | (define (process-use-modules module-interface-args) |
| 2642 | (module-use-interfaces! (current-module) |
| 2643 | (map (lambda (mif-args) |
| 2644 | (or (apply resolve-interface mif-args) |
| 2645 | (error "no such module" mif-args))) |
| 2646 | module-interface-args))) |
| 2647 | |
| 2648 | (defmacro use-modules modules |
| 2649 | `(eval-case |
| 2650 | ((load-toplevel) |
| 2651 | (process-use-modules |
| 2652 | (list ,@(map (lambda (m) |
| 2653 | `(list ,@(compile-interface-spec m))) |
| 2654 | modules))) |
| 2655 | *unspecified*) |
| 2656 | (else |
| 2657 | (error "use-modules can only be used at the top level")))) |
| 2658 | |
| 2659 | (defmacro use-syntax (spec) |
| 2660 | `(eval-case |
| 2661 | ((load-toplevel) |
| 2662 | ,@(if (pair? spec) |
| 2663 | `((process-use-modules (list |
| 2664 | (list ,@(compile-interface-spec spec)))) |
| 2665 | (set-module-transformer! (current-module) |
| 2666 | ,(car (last-pair spec)))) |
| 2667 | `((set-module-transformer! (current-module) ,spec))) |
| 2668 | *unspecified*) |
| 2669 | (else |
| 2670 | (error "use-syntax can only be used at the top level")))) |
| 2671 | |
| 2672 | ;; Dirk:FIXME:: This incorrect (according to R5RS) syntax needs to be changed |
| 2673 | ;; as soon as guile supports hygienic macros. |
| 2674 | (define define-private define) |
| 2675 | |
| 2676 | (defmacro define-public args |
| 2677 | (define (syntax) |
| 2678 | (error "bad syntax" (list 'define-public args))) |
| 2679 | (define (defined-name n) |
| 2680 | (cond |
| 2681 | ((symbol? n) n) |
| 2682 | ((pair? n) (defined-name (car n))) |
| 2683 | (else (syntax)))) |
| 2684 | (cond |
| 2685 | ((null? args) |
| 2686 | (syntax)) |
| 2687 | (#t |
| 2688 | (let ((name (defined-name (car args)))) |
| 2689 | `(begin |
| 2690 | (define-private ,@args) |
| 2691 | (eval-case ((load-toplevel) (export ,name)))))))) |
| 2692 | |
| 2693 | (defmacro defmacro-public args |
| 2694 | (define (syntax) |
| 2695 | (error "bad syntax" (list 'defmacro-public args))) |
| 2696 | (define (defined-name n) |
| 2697 | (cond |
| 2698 | ((symbol? n) n) |
| 2699 | (else (syntax)))) |
| 2700 | (cond |
| 2701 | ((null? args) |
| 2702 | (syntax)) |
| 2703 | (#t |
| 2704 | (let ((name (defined-name (car args)))) |
| 2705 | `(begin |
| 2706 | (eval-case ((load-toplevel) (export ,name))) |
| 2707 | (defmacro ,@args)))))) |
| 2708 | |
| 2709 | ;; Export a local variable |
| 2710 | |
| 2711 | ;; This function is called from "modules.c". If you change it, be |
| 2712 | ;; sure to update "modules.c" as well. |
| 2713 | |
| 2714 | (define (module-export! m names) |
| 2715 | (let ((public-i (module-public-interface m))) |
| 2716 | (for-each (lambda (name) |
| 2717 | (let ((var (module-ensure-local-variable! m name))) |
| 2718 | (module-add! public-i name var))) |
| 2719 | names))) |
| 2720 | |
| 2721 | (define (module-replace! m names) |
| 2722 | (let ((public-i (module-public-interface m))) |
| 2723 | (for-each (lambda (name) |
| 2724 | (let ((var (module-ensure-local-variable! m name))) |
| 2725 | (set-object-property! var 'replace #t) |
| 2726 | (module-add! public-i name var))) |
| 2727 | names))) |
| 2728 | |
| 2729 | ;; Re-export a imported variable |
| 2730 | ;; |
| 2731 | (define (module-re-export! m names) |
| 2732 | (let ((public-i (module-public-interface m))) |
| 2733 | (for-each (lambda (name) |
| 2734 | (let ((var (module-variable m name))) |
| 2735 | (cond ((not var) |
| 2736 | (error "Undefined variable:" name)) |
| 2737 | ((eq? var (module-local-variable m name)) |
| 2738 | (error "re-exporting local variable:" name)) |
| 2739 | (else |
| 2740 | (module-add! public-i name var))))) |
| 2741 | names))) |
| 2742 | |
| 2743 | (defmacro export names |
| 2744 | `(eval-case |
| 2745 | ((load-toplevel) |
| 2746 | (module-export! (current-module) ',names)) |
| 2747 | (else |
| 2748 | (error "export can only be used at the top level")))) |
| 2749 | |
| 2750 | (defmacro re-export names |
| 2751 | `(eval-case |
| 2752 | ((load-toplevel) |
| 2753 | (module-re-export! (current-module) ',names)) |
| 2754 | (else |
| 2755 | (error "re-export can only be used at the top level")))) |
| 2756 | |
| 2757 | (defmacro export-syntax names |
| 2758 | `(export ,@names)) |
| 2759 | |
| 2760 | (defmacro re-export-syntax names |
| 2761 | `(re-export ,@names)) |
| 2762 | |
| 2763 | (define load load-module) |
| 2764 | |
| 2765 | \f |
| 2766 | ;;; {Parameters} |
| 2767 | ;;; |
| 2768 | |
| 2769 | (define make-mutable-parameter |
| 2770 | (let ((make (lambda (fluid converter) |
| 2771 | (lambda args |
| 2772 | (if (null? args) |
| 2773 | (fluid-ref fluid) |
| 2774 | (fluid-set! fluid (converter (car args)))))))) |
| 2775 | (lambda (init . converter) |
| 2776 | (let ((fluid (make-fluid)) |
| 2777 | (converter (if (null? converter) |
| 2778 | identity |
| 2779 | (car converter)))) |
| 2780 | (fluid-set! fluid (converter init)) |
| 2781 | (make fluid converter))))) |
| 2782 | |
| 2783 | \f |
| 2784 | ;;; {Handling of duplicate imported bindings} |
| 2785 | ;;; |
| 2786 | |
| 2787 | ;; Duplicate handlers take the following arguments: |
| 2788 | ;; |
| 2789 | ;; module importing module |
| 2790 | ;; name conflicting name |
| 2791 | ;; int1 old interface where name occurs |
| 2792 | ;; val1 value of binding in old interface |
| 2793 | ;; int2 new interface where name occurs |
| 2794 | ;; val2 value of binding in new interface |
| 2795 | ;; var previous resolution or #f |
| 2796 | ;; val value of previous resolution |
| 2797 | ;; |
| 2798 | ;; A duplicate handler can take three alternative actions: |
| 2799 | ;; |
| 2800 | ;; 1. return #f => leave responsibility to next handler |
| 2801 | ;; 2. exit with an error |
| 2802 | ;; 3. return a variable resolving the conflict |
| 2803 | ;; |
| 2804 | |
| 2805 | (define duplicate-handlers |
| 2806 | (let ((m (make-module 7))) |
| 2807 | |
| 2808 | (define (check module name int1 val1 int2 val2 var val) |
| 2809 | (scm-error 'misc-error |
| 2810 | #f |
| 2811 | "~A: `~A' imported from both ~A and ~A" |
| 2812 | (list (module-name module) |
| 2813 | name |
| 2814 | (module-name int1) |
| 2815 | (module-name int2)) |
| 2816 | #f)) |
| 2817 | |
| 2818 | (define (warn module name int1 val1 int2 val2 var val) |
| 2819 | (format #t |
| 2820 | "~A: `~A' imported from both ~A and ~A\n" |
| 2821 | (module-name module) |
| 2822 | name |
| 2823 | (module-name int1) |
| 2824 | (module-name int2)) |
| 2825 | #f) |
| 2826 | |
| 2827 | (define (replace module name int1 val1 int2 val2 var val) |
| 2828 | (let ((old (or (and var (object-property var 'replace) var) |
| 2829 | (module-variable int1 name))) |
| 2830 | (new (module-variable int2 name))) |
| 2831 | (if (object-property old 'replace) |
| 2832 | (and (or (eq? old new) |
| 2833 | (not (object-property new 'replace))) |
| 2834 | old) |
| 2835 | (and (object-property new 'replace) |
| 2836 | new)))) |
| 2837 | |
| 2838 | (define (warn-override-core module name int1 val1 int2 val2 var val) |
| 2839 | (and (eq? int1 the-scm-module) |
| 2840 | (begin |
| 2841 | (format #t |
| 2842 | "WARNING: ~A: imported module ~A overrides core binding `~A'\n" |
| 2843 | (module-name module) |
| 2844 | (module-name int2) |
| 2845 | name) |
| 2846 | (module-local-variable int2 name)))) |
| 2847 | |
| 2848 | (define (first module name int1 val1 int2 val2 var val) |
| 2849 | (or var (module-local-variable int1 name))) |
| 2850 | |
| 2851 | (define (last module name int1 val1 int2 val2 var val) |
| 2852 | (module-local-variable int2 name)) |
| 2853 | |
| 2854 | (set-module-name! m 'duplicate-handlers) |
| 2855 | (set-module-kind! m 'interface) |
| 2856 | (module-define! m 'check check) |
| 2857 | (module-define! m 'warn warn) |
| 2858 | (module-define! m 'replace replace) |
| 2859 | (module-define! m 'warn-override-core warn-override-core) |
| 2860 | (module-define! m 'first first) |
| 2861 | (module-define! m 'last last) |
| 2862 | m)) |
| 2863 | |
| 2864 | (define (lookup-duplicates-handlers handler-names) |
| 2865 | (and handler-names |
| 2866 | (map (lambda (handler-name) |
| 2867 | (or (module-symbol-local-binding |
| 2868 | duplicate-handlers handler-name #f) |
| 2869 | (error "invalid duplicate handler name:" |
| 2870 | handler-name))) |
| 2871 | (if (list? handler-names) |
| 2872 | handler-names |
| 2873 | (list handler-names))))) |
| 2874 | |
| 2875 | (define default-duplicate-binding-procedures |
| 2876 | (make-mutable-parameter #f)) |
| 2877 | |
| 2878 | (define default-duplicate-binding-handler |
| 2879 | (make-mutable-parameter '(replace warn-override-core check) |
| 2880 | (lambda (handler-names) |
| 2881 | (default-duplicate-binding-procedures |
| 2882 | (lookup-duplicates-handlers handler-names)) |
| 2883 | handler-names))) |
| 2884 | |
| 2885 | (define (make-duplicates-interface) |
| 2886 | (let ((m (make-module))) |
| 2887 | (set-module-kind! m 'custom-interface) |
| 2888 | (set-module-name! m 'duplicates) |
| 2889 | m)) |
| 2890 | |
| 2891 | (define (process-duplicates module interface) |
| 2892 | (let* ((duplicates-info (module-duplicates-info module)) |
| 2893 | (duplicates-handlers (or (car duplicates-info) |
| 2894 | (default-duplicate-binding-procedures))) |
| 2895 | (duplicates-interface (cdr duplicates-info))) |
| 2896 | (module-for-each |
| 2897 | (lambda (name var) |
| 2898 | (cond ((module-import-interface module name) |
| 2899 | => |
| 2900 | (lambda (prev-interface) |
| 2901 | (let ((var1 (module-local-variable prev-interface name)) |
| 2902 | (var2 (module-local-variable interface name))) |
| 2903 | (if (not (eq? var1 var2)) |
| 2904 | (begin |
| 2905 | (if (not duplicates-interface) |
| 2906 | (begin |
| 2907 | (set! duplicates-interface |
| 2908 | (make-duplicates-interface)) |
| 2909 | (set-cdr! duplicates-info duplicates-interface))) |
| 2910 | (let* ((var (module-local-variable duplicates-interface |
| 2911 | name)) |
| 2912 | (val (and var |
| 2913 | (variable-bound? var) |
| 2914 | (variable-ref var)))) |
| 2915 | (let loop ((duplicates-handlers duplicates-handlers)) |
| 2916 | (cond ((null? duplicates-handlers)) |
| 2917 | (((car duplicates-handlers) |
| 2918 | module |
| 2919 | name |
| 2920 | prev-interface |
| 2921 | (and (variable-bound? var1) |
| 2922 | (variable-ref var1)) |
| 2923 | interface |
| 2924 | (and (variable-bound? var2) |
| 2925 | (variable-ref var2)) |
| 2926 | var |
| 2927 | val) |
| 2928 | => |
| 2929 | (lambda (var) |
| 2930 | (module-add! duplicates-interface name var))) |
| 2931 | (else |
| 2932 | (loop (cdr duplicates-handlers))))))))))))) |
| 2933 | interface))) |
| 2934 | |
| 2935 | \f |
| 2936 | |
| 2937 | ;;; {`cond-expand' for SRFI-0 support.} |
| 2938 | ;;; |
| 2939 | ;;; This syntactic form expands into different commands or |
| 2940 | ;;; definitions, depending on the features provided by the Scheme |
| 2941 | ;;; implementation. |
| 2942 | ;;; |
| 2943 | ;;; Syntax: |
| 2944 | ;;; |
| 2945 | ;;; <cond-expand> |
| 2946 | ;;; --> (cond-expand <cond-expand-clause>+) |
| 2947 | ;;; | (cond-expand <cond-expand-clause>* (else <command-or-definition>)) |
| 2948 | ;;; <cond-expand-clause> |
| 2949 | ;;; --> (<feature-requirement> <command-or-definition>*) |
| 2950 | ;;; <feature-requirement> |
| 2951 | ;;; --> <feature-identifier> |
| 2952 | ;;; | (and <feature-requirement>*) |
| 2953 | ;;; | (or <feature-requirement>*) |
| 2954 | ;;; | (not <feature-requirement>) |
| 2955 | ;;; <feature-identifier> |
| 2956 | ;;; --> <a symbol which is the name or alias of a SRFI> |
| 2957 | ;;; |
| 2958 | ;;; Additionally, this implementation provides the |
| 2959 | ;;; <feature-identifier>s `guile' and `r5rs', so that programs can |
| 2960 | ;;; determine the implementation type and the supported standard. |
| 2961 | ;;; |
| 2962 | ;;; Currently, the following feature identifiers are supported: |
| 2963 | ;;; |
| 2964 | ;;; guile r5rs srfi-0 |
| 2965 | ;;; |
| 2966 | ;;; Remember to update the features list when adding more SRFIs. |
| 2967 | |
| 2968 | (define %cond-expand-features |
| 2969 | ;; Adjust the above comment when changing this. |
| 2970 | '(guile r5rs srfi-0)) |
| 2971 | |
| 2972 | ;; This table maps module public interfaces to the list of features. |
| 2973 | ;; |
| 2974 | (define %cond-expand-table (make-hash-table 31)) |
| 2975 | |
| 2976 | ;; Add one or more features to the `cond-expand' feature list of the |
| 2977 | ;; module `module'. |
| 2978 | ;; |
| 2979 | (define (cond-expand-provide module features) |
| 2980 | (let ((mod (module-public-interface module))) |
| 2981 | (and mod |
| 2982 | (hashq-set! %cond-expand-table mod |
| 2983 | (append (hashq-ref %cond-expand-table mod '()) |
| 2984 | features))))) |
| 2985 | |
| 2986 | (define cond-expand |
| 2987 | (procedure->memoizing-macro |
| 2988 | (lambda (exp env) |
| 2989 | (let ((clauses (cdr exp)) |
| 2990 | (syntax-error (lambda (cl) |
| 2991 | (error "invalid clause in `cond-expand'" cl)))) |
| 2992 | (letrec |
| 2993 | ((test-clause |
| 2994 | (lambda (clause) |
| 2995 | (cond |
| 2996 | ((symbol? clause) |
| 2997 | (or (memq clause %cond-expand-features) |
| 2998 | (let lp ((uses (module-uses (env-module env)))) |
| 2999 | (if (pair? uses) |
| 3000 | (or (memq clause |
| 3001 | (hashq-ref %cond-expand-table |
| 3002 | (car uses) '())) |
| 3003 | (lp (cdr uses))) |
| 3004 | #f)))) |
| 3005 | ((pair? clause) |
| 3006 | (cond |
| 3007 | ((eq? 'and (car clause)) |
| 3008 | (let lp ((l (cdr clause))) |
| 3009 | (cond ((null? l) |
| 3010 | #t) |
| 3011 | ((pair? l) |
| 3012 | (and (test-clause (car l)) (lp (cdr l)))) |
| 3013 | (else |
| 3014 | (syntax-error clause))))) |
| 3015 | ((eq? 'or (car clause)) |
| 3016 | (let lp ((l (cdr clause))) |
| 3017 | (cond ((null? l) |
| 3018 | #f) |
| 3019 | ((pair? l) |
| 3020 | (or (test-clause (car l)) (lp (cdr l)))) |
| 3021 | (else |
| 3022 | (syntax-error clause))))) |
| 3023 | ((eq? 'not (car clause)) |
| 3024 | (cond ((not (pair? (cdr clause))) |
| 3025 | (syntax-error clause)) |
| 3026 | ((pair? (cddr clause)) |
| 3027 | ((syntax-error clause)))) |
| 3028 | (not (test-clause (cadr clause)))) |
| 3029 | (else |
| 3030 | (syntax-error clause)))) |
| 3031 | (else |
| 3032 | (syntax-error clause)))))) |
| 3033 | (let lp ((c clauses)) |
| 3034 | (cond |
| 3035 | ((null? c) |
| 3036 | (error "Unfulfilled `cond-expand'")) |
| 3037 | ((not (pair? c)) |
| 3038 | (syntax-error c)) |
| 3039 | ((not (pair? (car c))) |
| 3040 | (syntax-error (car c))) |
| 3041 | ((test-clause (caar c)) |
| 3042 | `(begin ,@(cdar c))) |
| 3043 | ((eq? (caar c) 'else) |
| 3044 | (if (pair? (cdr c)) |
| 3045 | (syntax-error c)) |
| 3046 | `(begin ,@(cdar c))) |
| 3047 | (else |
| 3048 | (lp (cdr c)))))))))) |
| 3049 | |
| 3050 | ;; This procedure gets called from the startup code with a list of |
| 3051 | ;; numbers, which are the numbers of the SRFIs to be loaded on startup. |
| 3052 | ;; |
| 3053 | (define (use-srfis srfis) |
| 3054 | (let lp ((s srfis)) |
| 3055 | (if (pair? s) |
| 3056 | (let* ((srfi (string->symbol |
| 3057 | (string-append "srfi-" (number->string (car s))))) |
| 3058 | (mod-i (resolve-interface (list 'srfi srfi)))) |
| 3059 | (module-use! (current-module) mod-i) |
| 3060 | (lp (cdr s)))))) |
| 3061 | |
| 3062 | \f |
| 3063 | |
| 3064 | ;;; {Load emacs interface support if emacs option is given.} |
| 3065 | |
| 3066 | (define (named-module-use! user usee) |
| 3067 | (module-use! (resolve-module user) (resolve-interface usee))) |
| 3068 | |
| 3069 | (define (load-emacs-interface) |
| 3070 | (and (provided? 'debug-extensions) |
| 3071 | (debug-enable 'backtrace)) |
| 3072 | (named-module-use! '(guile-user) '(ice-9 emacs))) |
| 3073 | |
| 3074 | \f |
| 3075 | |
| 3076 | (define using-readline? |
| 3077 | (let ((using-readline? (make-fluid))) |
| 3078 | (make-procedure-with-setter |
| 3079 | (lambda () (fluid-ref using-readline?)) |
| 3080 | (lambda (v) (fluid-set! using-readline? v))))) |
| 3081 | |
| 3082 | (define (top-repl) |
| 3083 | (let ((guile-user-module (resolve-module '(guile-user)))) |
| 3084 | |
| 3085 | ;; Load emacs interface support if emacs option is given. |
| 3086 | (if (and (module-defined? guile-user-module 'use-emacs-interface) |
| 3087 | (module-ref guile-user-module 'use-emacs-interface)) |
| 3088 | (load-emacs-interface)) |
| 3089 | |
| 3090 | ;; Use some convenient modules (in reverse order) |
| 3091 | |
| 3092 | (if (provided? 'regex) |
| 3093 | (module-use! guile-user-module (resolve-interface '(ice-9 regex)))) |
| 3094 | (if (provided? 'threads) |
| 3095 | (module-use! guile-user-module (resolve-interface '(ice-9 threads)))) |
| 3096 | ;; load debugger on demand |
| 3097 | (module-use! guile-user-module |
| 3098 | (make-autoload-interface guile-user-module |
| 3099 | '(ice-9 debugger) '(debug))) |
| 3100 | (module-use! guile-user-module (resolve-interface '(ice-9 session))) |
| 3101 | (module-use! guile-user-module (resolve-interface '(ice-9 debug))) |
| 3102 | ;; so that builtin bindings will be checked first |
| 3103 | (module-use! guile-user-module (resolve-interface '(guile))) |
| 3104 | |
| 3105 | (set-current-module guile-user-module) |
| 3106 | |
| 3107 | (let ((old-handlers #f) |
| 3108 | (signals (if (provided? 'posix) |
| 3109 | `((,SIGINT . "User interrupt") |
| 3110 | (,SIGFPE . "Arithmetic error") |
| 3111 | (,SIGBUS . "Bad memory access (bus error)") |
| 3112 | (,SIGSEGV |
| 3113 | . "Bad memory access (Segmentation violation)")) |
| 3114 | '()))) |
| 3115 | |
| 3116 | (dynamic-wind |
| 3117 | |
| 3118 | ;; call at entry |
| 3119 | (lambda () |
| 3120 | (let ((make-handler (lambda (msg) |
| 3121 | (lambda (sig) |
| 3122 | ;; Make a backup copy of the stack |
| 3123 | (fluid-set! before-signal-stack |
| 3124 | (fluid-ref the-last-stack)) |
| 3125 | (save-stack 2) |
| 3126 | (scm-error 'signal |
| 3127 | #f |
| 3128 | msg |
| 3129 | #f |
| 3130 | (list sig)))))) |
| 3131 | (set! old-handlers |
| 3132 | (map (lambda (sig-msg) |
| 3133 | (sigaction (car sig-msg) |
| 3134 | (make-handler (cdr sig-msg)))) |
| 3135 | signals)))) |
| 3136 | |
| 3137 | ;; the protected thunk. |
| 3138 | (lambda () |
| 3139 | (let ((status (scm-style-repl))) |
| 3140 | (run-hook exit-hook) |
| 3141 | status)) |
| 3142 | |
| 3143 | ;; call at exit. |
| 3144 | (lambda () |
| 3145 | (map (lambda (sig-msg old-handler) |
| 3146 | (if (not (car old-handler)) |
| 3147 | ;; restore original C handler. |
| 3148 | (sigaction (car sig-msg) #f) |
| 3149 | ;; restore Scheme handler, SIG_IGN or SIG_DFL. |
| 3150 | (sigaction (car sig-msg) |
| 3151 | (car old-handler) |
| 3152 | (cdr old-handler)))) |
| 3153 | signals old-handlers)))))) |
| 3154 | |
| 3155 | (defmacro false-if-exception (expr) |
| 3156 | `(catch #t (lambda () ,expr) |
| 3157 | (lambda args #f))) |
| 3158 | |
| 3159 | ;;; This hook is run at the very end of an interactive session. |
| 3160 | ;;; |
| 3161 | (define exit-hook (make-hook)) |
| 3162 | |
| 3163 | \f |
| 3164 | (append! %load-path (list ".")) |
| 3165 | |
| 3166 | ;; Place the user in the guile-user module. |
| 3167 | ;; |
| 3168 | |
| 3169 | (define-module (guile-user)) |
| 3170 | |
| 3171 | ;;; boot-9.scm ends here |