3 ;;;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
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.
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.
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
24 ;;; This file is the first thing loaded into Guile. It adds many mundane
25 ;;; definitions and a few that are interesting.
27 ;;; The module system (hence the hierarchical namespace) are defined in this
37 ;; We don't have macros here, but we do want to define
38 ;; `begin-deprecated' early.
40 (define begin-deprecated
41 (procedure->memoizing-macro
43 (if (include-deprecated-features)
52 (if (not (memq sym *features*))
53 (set! *features* (cons sym *features*))))
55 ;;; Return #t iff FEATURE is available to this Guile interpreter.
56 ;;; In SLIB, provided? also checks to see if the module is available.
57 ;;; We should do that too, but don't.
58 (define (provided? feature)
59 (and (memq feature *features*) #t))
61 ;;; presumably deprecated.
62 (define feature? provided?)
64 ;;; let format alias simple-format until the more complete version is loaded
65 (define format simple-format)
70 (primitive-load-path "ice-9/r4rs.scm")
73 ;;; {Simple Debugging Tools}
77 ;; peek takes any number of arguments, writes them to the
78 ;; current ouput port, and returns the last argument.
79 ;; It is handy to wrap around an expression to look at
80 ;; a value each time is evaluated, e.g.:
82 ;; (+ 10 (troublesome-fn))
83 ;; => (+ 10 (pk 'troublesome-fn-returned (troublesome-fn)))
86 (define (peek . stuff)
91 (car (last-pair stuff)))
95 (define (warn . stuff)
96 (with-output-to-port (current-error-port)
99 (display ";;; WARNING ")
102 (car (last-pair stuff)))))
105 ;;; {Trivial Functions}
108 (define (identity x) x)
109 (define (1+ n) (+ n 1))
110 (define (1- n) (+ n -1))
111 (define (and=> value procedure) (and value (procedure value)))
112 (define (make-hash-table k) (make-vector k '()))
116 (issue-deprecation-warning "`id' is deprecated. Use `identity' instead.")
119 (issue-deprecation-warning "`-1+' is deprecated. Use `1-' instead.")
121 (define (return-it . args)
122 (issue-deprecation-warning "`return-it' is deprecated. Use `noop' instead.")
125 ;;; apply-to-args is functionally redundant with apply and, worse,
126 ;;; is less general than apply since it only takes two arguments.
128 ;;; On the other hand, apply-to-args is a syntacticly convenient way to
129 ;;; perform binding in many circumstances when the "let" family of
130 ;;; of forms don't cut it. E.g.:
132 ;;; (apply-to-args (return-3d-mouse-coords)
137 (define (apply-to-args args fn) (apply fn args))
144 (define (ipow-by-squaring x k acc proc)
145 (cond ((zero? k) acc)
146 ((= 1 k) (proc acc x))
147 (else (ipow-by-squaring (proc x x)
149 (if (even? k) acc (proc acc x))
153 (define (string-character-length s)
154 (issue-deprecation-warning "`string-character-length' is deprecated. Use `string-length' instead.")
156 (define (flags . args)
157 (issue-deprecation-warning "`flags' is deprecated. Use `logior' instead.")
158 (apply logior args)))
161 ;;; {Symbol Properties}
164 (define (symbol-property sym prop)
165 (let ((pair (assoc prop (symbol-pref sym))))
166 (and pair (cdr pair))))
168 (define (set-symbol-property! sym prop val)
169 (let ((pair (assoc prop (symbol-pref sym))))
172 (symbol-pset! sym (acons prop val (symbol-pref sym))))))
174 (define (symbol-property-remove! sym prop)
175 (let ((pair (assoc prop (symbol-pref sym))))
177 (symbol-pset! sym (delq! pair (symbol-pref sym))))))
179 ;;; {General Properties}
182 ;; This is a more modern interface to properties. It will replace all
183 ;; other property-like things eventually.
185 (define (make-object-property)
186 (let ((prop (primitive-make-property #f)))
187 (make-procedure-with-setter
188 (lambda (obj) (primitive-property-ref prop obj))
189 (lambda (obj val) (primitive-property-set! prop obj val)))))
196 (if (provided? 'array)
197 (primitive-load-path "ice-9/arrays.scm"))
203 (define (symbol->keyword symbol)
204 (make-keyword-from-dash-symbol (symbol-append '- symbol)))
206 (define (keyword->symbol kw)
207 (let ((sym (symbol->string (keyword-dash-symbol kw))))
208 (string->symbol (substring sym 1 (string-length sym)))))
210 (define (kw-arg-ref args kw)
211 (let ((rem (member kw args)))
212 (and rem (pair? (cdr rem)) (cadr rem))))
218 (define (struct-layout s)
219 (struct-ref (struct-vtable s) vtable-index-layout))
225 (define the-environment
230 (define the-root-environment (the-environment))
232 (define (environment-module env)
233 (let ((closure (and (pair? env) (car (last-pair env)))))
234 (and closure (procedure-property closure 'module))))
240 ;; Printing records: by default, records are printed as
242 ;; #<type-name field1: val1 field2: val2 ...>
244 ;; You can change that by giving a custom printing function to
245 ;; MAKE-RECORD-TYPE (after the list of field symbols). This function
246 ;; will be called like
248 ;; (<printer> object port)
250 ;; It should print OBJECT to PORT.
252 (define (inherit-print-state old-port new-port)
253 (if (get-print-state old-port)
254 (port-with-print-state new-port (get-print-state old-port))
257 ;; 0: type-name, 1: fields
258 (define record-type-vtable
259 (make-vtable-vtable "prpr" 0
261 (cond ((eq? s record-type-vtable)
262 (display "#<record-type-vtable>" p))
264 (display "#<record-type " p)
265 (display (record-type-name s) p)
268 (define (record-type? obj)
269 (and (struct? obj) (eq? record-type-vtable (struct-vtable obj))))
271 (define (make-record-type type-name fields . opt)
272 (let ((printer-fn (and (pair? opt) (car opt))))
273 (let ((struct (make-struct record-type-vtable 0
276 (map (lambda (f) "pw") fields)))
280 (display type-name p)
281 (let loop ((fields fields)
284 ((not (null? fields))
286 (display (car fields) p)
288 (display (struct-ref s off) p)
289 (loop (cdr fields) (+ 1 off)))))
292 (copy-tree fields))))
293 ;; Temporary solution: Associate a name to the record type descriptor
294 ;; so that the object system can create a wrapper class for it.
295 (set-struct-vtable-name! struct (if (symbol? type-name)
297 (string->symbol type-name)))
300 (define (record-type-name obj)
301 (if (record-type? obj)
302 (struct-ref obj vtable-offset-user)
303 (error 'not-a-record-type obj)))
305 (define (record-type-fields obj)
306 (if (record-type? obj)
307 (struct-ref obj (+ 1 vtable-offset-user))
308 (error 'not-a-record-type obj)))
310 (define (record-constructor rtd . opt)
311 (let ((field-names (if (pair? opt) (car opt) (record-type-fields rtd))))
312 (local-eval `(lambda ,field-names
313 (make-struct ',rtd 0 ,@(map (lambda (f)
314 (if (memq f field-names)
317 (record-type-fields rtd))))
318 the-root-environment)))
320 (define (record-predicate rtd)
321 (lambda (obj) (and (struct? obj) (eq? rtd (struct-vtable obj)))))
323 (define (record-accessor rtd field-name)
324 (let* ((pos (list-index (record-type-fields rtd) field-name)))
326 (error 'no-such-field field-name))
327 (local-eval `(lambda (obj)
328 (and (eq? ',rtd (record-type-descriptor obj))
329 (struct-ref obj ,pos)))
330 the-root-environment)))
332 (define (record-modifier rtd field-name)
333 (let* ((pos (list-index (record-type-fields rtd) field-name)))
335 (error 'no-such-field field-name))
336 (local-eval `(lambda (obj val)
337 (and (eq? ',rtd (record-type-descriptor obj))
338 (struct-set! obj ,pos val)))
339 the-root-environment)))
342 (define (record? obj)
343 (and (struct? obj) (record-type? (struct-vtable obj))))
345 (define (record-type-descriptor obj)
348 (error 'not-a-record obj)))
356 (define (->bool x) (not (not x)))
362 (define (symbol-append . args)
363 (string->symbol (apply string-append (map symbol->string args))))
365 (define (list->symbol . args)
366 (string->symbol (apply list->string args)))
368 (define (symbol . args)
369 (string->symbol (apply string args)))
375 (define (list-index l k)
381 (loop (+ n 1) (cdr l))))))
383 (define (make-list n . init)
384 (if (pair? init) (set! init (car init)))
385 (let loop ((answer '())
389 (loop (cons init answer) (- n 1)))))
392 ;;; {and-map and or-map}
394 ;;; (and-map fn lst) is like (and (fn (car lst)) (fn (cadr lst)) (fn...) ...)
395 ;;; (or-map fn lst) is like (or (fn (car lst)) (fn (cadr lst)) (fn...) ...)
400 ;; Apply f to successive elements of l until exhaustion or f returns #f.
401 ;; If returning early, return #f. Otherwise, return the last value returned
402 ;; by f. If f has never been called because l is empty, return #t.
404 (define (and-map f lst)
405 (let loop ((result #t)
410 (loop (f (car l)) (cdr l))))))
414 ;; Apply f to successive elements of l until exhaustion or while f returns #f.
415 ;; If returning early, return the return value of f.
417 (define (or-map f lst)
418 (let loop ((result #f)
422 (loop (f (car l)) (cdr l))))))
426 (if (provided? 'posix)
427 (primitive-load-path "ice-9/posix.scm"))
429 (if (provided? 'socket)
430 (primitive-load-path "ice-9/networking.scm"))
433 (if (provided? 'posix)
437 (let ((port (catch 'system-error (lambda () (open-file str OPEN_READ))
439 (if port (begin (close-port port) #t)
442 (define file-is-directory?
443 (if (provided? 'posix)
445 (eq? (stat:type (stat str)) 'directory))
447 (let ((port (catch 'system-error
448 (lambda () (open-file (string-append str "/.")
451 (if port (begin (close-port port) #t)
454 (define (has-suffix? str suffix)
455 (let ((sufl (string-length suffix))
456 (sl (string-length str)))
458 (string=? (substring str (- sl sufl) sl) suffix))))
464 (define (error . args)
467 (scm-error 'misc-error #f "?" #f #f)
468 (let loop ((msg "~A")
470 (if (not (null? rest))
471 (loop (string-append msg " ~S")
473 (scm-error 'misc-error #f msg args #f)))))
475 ;; bad-throw is the hook that is called upon a throw to a an unhandled
476 ;; key (unless the throw has four arguments, in which case
477 ;; it's usually interpreted as an error throw.)
478 ;; If the key has a default handler (a throw-handler-default property),
479 ;; it is applied to the throw.
481 (define (bad-throw key . args)
482 (let ((default (symbol-property key 'throw-handler-default)))
483 (or (and default (apply default key args))
484 (apply error "unhandled-exception:" key args))))
488 (define (tm:sec obj) (vector-ref obj 0))
489 (define (tm:min obj) (vector-ref obj 1))
490 (define (tm:hour obj) (vector-ref obj 2))
491 (define (tm:mday obj) (vector-ref obj 3))
492 (define (tm:mon obj) (vector-ref obj 4))
493 (define (tm:year obj) (vector-ref obj 5))
494 (define (tm:wday obj) (vector-ref obj 6))
495 (define (tm:yday obj) (vector-ref obj 7))
496 (define (tm:isdst obj) (vector-ref obj 8))
497 (define (tm:gmtoff obj) (vector-ref obj 9))
498 (define (tm:zone obj) (vector-ref obj 10))
500 (define (set-tm:sec obj val) (vector-set! obj 0 val))
501 (define (set-tm:min obj val) (vector-set! obj 1 val))
502 (define (set-tm:hour obj val) (vector-set! obj 2 val))
503 (define (set-tm:mday obj val) (vector-set! obj 3 val))
504 (define (set-tm:mon obj val) (vector-set! obj 4 val))
505 (define (set-tm:year obj val) (vector-set! obj 5 val))
506 (define (set-tm:wday obj val) (vector-set! obj 6 val))
507 (define (set-tm:yday obj val) (vector-set! obj 7 val))
508 (define (set-tm:isdst obj val) (vector-set! obj 8 val))
509 (define (set-tm:gmtoff obj val) (vector-set! obj 9 val))
510 (define (set-tm:zone obj val) (vector-set! obj 10 val))
512 (define (tms:clock obj) (vector-ref obj 0))
513 (define (tms:utime obj) (vector-ref obj 1))
514 (define (tms:stime obj) (vector-ref obj 2))
515 (define (tms:cutime obj) (vector-ref obj 3))
516 (define (tms:cstime obj) (vector-ref obj 4))
518 (define (file-position . args) (apply ftell args))
519 (define (file-set-position . args) (apply fseek args))
521 (define (move->fdes fd/port fd)
522 (cond ((integer? fd/port)
523 (dup->fdes fd/port fd)
527 (primitive-move->fdes fd/port fd)
528 (set-port-revealed! fd/port 1)
531 (define (release-port-handle port)
532 (let ((revealed (port-revealed port)))
534 (set-port-revealed! port (- revealed 1)))))
536 (define (dup->port port/fd mode . maybe-fd)
537 (let ((port (fdopen (apply dup->fdes port/fd maybe-fd)
540 (set-port-revealed! port 1))
543 (define (dup->inport port/fd . maybe-fd)
544 (apply dup->port port/fd "r" maybe-fd))
546 (define (dup->outport port/fd . maybe-fd)
547 (apply dup->port port/fd "w" maybe-fd))
549 (define (dup port/fd . maybe-fd)
550 (if (integer? port/fd)
551 (apply dup->fdes port/fd maybe-fd)
552 (apply dup->port port/fd (port-mode port/fd) maybe-fd)))
554 (define (duplicate-port port modes)
555 (dup->port port modes))
557 (define (fdes->inport fdes)
558 (let loop ((rest-ports (fdes->ports fdes)))
559 (cond ((null? rest-ports)
560 (let ((result (fdopen fdes "r")))
561 (set-port-revealed! result 1)
563 ((input-port? (car rest-ports))
564 (set-port-revealed! (car rest-ports)
565 (+ (port-revealed (car rest-ports)) 1))
568 (loop (cdr rest-ports))))))
570 (define (fdes->outport fdes)
571 (let loop ((rest-ports (fdes->ports fdes)))
572 (cond ((null? rest-ports)
573 (let ((result (fdopen fdes "w")))
574 (set-port-revealed! result 1)
576 ((output-port? (car rest-ports))
577 (set-port-revealed! (car rest-ports)
578 (+ (port-revealed (car rest-ports)) 1))
581 (loop (cdr rest-ports))))))
583 (define (port->fdes port)
584 (set-port-revealed! port (+ (port-revealed port) 1))
587 (define (setenv name value)
589 (putenv (string-append name "=" value))
596 ;;; Here for backward compatability
598 (define scheme-file-suffix (lambda () ".scm"))
600 (define (in-vicinity vicinity file)
601 (let ((tail (let ((len (string-length vicinity)))
604 (string-ref vicinity (- len 1))))))
605 (string-append vicinity
613 ;;; {Help for scm_shell}
614 ;;; The argument-processing code used by Guile-based shells generates
615 ;;; Scheme code based on the argument list. This page contains help
616 ;;; functions for the code it generates.
618 (define (command-line) (program-arguments))
620 ;; This is mostly for the internal use of the code generated by
621 ;; scm_compile_shell_switches.
622 (define (load-user-init)
623 (let* ((home (or (getenv "HOME")
624 (false-if-exception (passwd:dir (getpwuid (getuid))))
625 "/")) ;; fallback for cygwin etc.
626 (init-file (in-vicinity home ".guile")))
627 (if (file-exists? init-file)
628 (primitive-load init-file))))
631 ;;; {Loading by paths}
633 ;;; Load a Scheme source file named NAME, searching for it in the
634 ;;; directories listed in %load-path, and applying each of the file
635 ;;; name extensions listed in %load-extensions.
636 (define (load-from-path name)
637 (start-stack 'load-stack
638 (primitive-load-path name)))
642 ;;; {Transcendental Functions}
644 ;;; Derived from "Transcen.scm", Complex trancendental functions for SCM.
645 ;;; Written by Jerry D. Hedden, (C) FSF.
646 ;;; See the file `COPYING' for terms applying to this program.
650 (if (real? z) ($exp z)
651 (make-polar ($exp (real-part z)) (imag-part z))))
654 (if (and (real? z) (>= z 0))
656 (make-rectangular ($log (magnitude z)) (angle z))))
660 (if (negative? z) (make-rectangular 0 ($sqrt (- z)))
662 (make-polar ($sqrt (magnitude z)) (/ (angle z) 2))))
665 (let ((integer-expt integer-expt))
670 (/ 1 (integer-expt z1 (- z2)))))
671 ((and (real? z2) (real? z1) (>= z1 0))
674 (exp (* z2 (log z1))))))))
677 (if (real? z) ($sinh z)
678 (let ((x (real-part z)) (y (imag-part z)))
679 (make-rectangular (* ($sinh x) ($cos y))
680 (* ($cosh x) ($sin y))))))
682 (if (real? z) ($cosh z)
683 (let ((x (real-part z)) (y (imag-part z)))
684 (make-rectangular (* ($cosh x) ($cos y))
685 (* ($sinh x) ($sin y))))))
687 (if (real? z) ($tanh z)
688 (let* ((x (* 2 (real-part z)))
689 (y (* 2 (imag-part z)))
690 (w (+ ($cosh x) ($cos y))))
691 (make-rectangular (/ ($sinh x) w) (/ ($sin y) w)))))
694 (if (real? z) ($asinh z)
695 (log (+ z (sqrt (+ (* z z) 1))))))
698 (if (and (real? z) (>= z 1))
700 (log (+ z (sqrt (- (* z z) 1))))))
703 (if (and (real? z) (> z -1) (< z 1))
705 (/ (log (/ (+ 1 z) (- 1 z))) 2)))
708 (if (real? z) ($sin z)
709 (let ((x (real-part z)) (y (imag-part z)))
710 (make-rectangular (* ($sin x) ($cosh y))
711 (* ($cos x) ($sinh y))))))
713 (if (real? z) ($cos z)
714 (let ((x (real-part z)) (y (imag-part z)))
715 (make-rectangular (* ($cos x) ($cosh y))
716 (- (* ($sin x) ($sinh y)))))))
718 (if (real? z) ($tan z)
719 (let* ((x (* 2 (real-part z)))
720 (y (* 2 (imag-part z)))
721 (w (+ ($cos x) ($cosh y))))
722 (make-rectangular (/ ($sin x) w) (/ ($sinh y) w)))))
725 (if (and (real? z) (>= z -1) (<= z 1))
727 (* -i (asinh (* +i z)))))
730 (if (and (real? z) (>= z -1) (<= z 1))
732 (+ (/ (angle -1) 2) (* +i (asinh (* +i z))))))
736 (if (real? z) ($atan z)
737 (/ (log (/ (- +i z) (+ +i z))) +2i))
741 (/ (log arg) (log 10)))
745 ;;; {Reader Extensions}
748 ;;; Reader code for various "#c" forms.
751 (read-hash-extend #\' (lambda (c port)
753 (read-hash-extend #\. (lambda (c port)
754 (eval (read port) (interaction-environment))))
757 ;;; {Command Line Options}
760 (define (get-option argv kw-opts kw-args return)
765 ((or (not (eq? #\- (string-ref (car argv) 0)))
766 (eq? (string-length (car argv)) 1))
767 (return 'normal-arg (car argv) (cdr argv)))
769 ((eq? #\- (string-ref (car argv) 1))
770 (let* ((kw-arg-pos (or (string-index (car argv) #\=)
771 (string-length (car argv))))
772 (kw (symbol->keyword (substring (car argv) 2 kw-arg-pos)))
773 (kw-opt? (member kw kw-opts))
774 (kw-arg? (member kw kw-args))
775 (arg (or (and (not (eq? kw-arg-pos (string-length (car argv))))
776 (substring (car argv)
778 (string-length (car argv))))
780 (begin (set! argv (cdr argv)) (car argv))))))
781 (if (or kw-opt? kw-arg?)
782 (return kw arg (cdr argv))
783 (return 'usage-error kw (cdr argv)))))
786 (let* ((char (substring (car argv) 1 2))
787 (kw (symbol->keyword char)))
791 (let* ((rest-car (substring (car argv) 2 (string-length (car argv))))
792 (new-argv (if (= 0 (string-length rest-car))
794 (cons (string-append "-" rest-car) (cdr argv)))))
795 (return kw #f new-argv)))
798 (let* ((rest-car (substring (car argv) 2 (string-length (car argv))))
799 (arg (if (= 0 (string-length rest-car))
802 (new-argv (if (= 0 (string-length rest-car))
805 (return kw arg new-argv)))
807 (else (return 'usage-error kw argv)))))))
809 (define (for-next-option proc argv kw-opts kw-args)
810 (let loop ((argv argv))
811 (get-option argv kw-opts kw-args
812 (lambda (opt opt-arg argv)
813 (and opt (proc opt opt-arg argv loop))))))
815 (define (display-usage-report kw-desc)
818 (or (eq? (car kw) #t)
821 (help (cadr opt-desc))
822 (opts (car opt-desc))
823 (opts-proper (if (string? (car opts)) (cdr opts) opts))
824 (arg-name (if (string? (car opts))
825 (string-append "<" (car opts) ">")
827 (left-part (string-append
828 (with-output-to-string
830 (map (lambda (x) (display (keyword-symbol x)) (display " "))
833 (middle-part (if (and (< (string-length left-part) 30)
834 (< (string-length help) 40))
835 (make-string (- 30 (string-length left-part)) #\ )
838 (display middle-part)
845 (define (transform-usage-lambda cases)
846 (let* ((raw-usage (delq! 'else (map car cases)))
847 (usage-sans-specials (map (lambda (x)
848 (or (and (not (list? x)) x)
849 (and (symbol? (car x)) #t)
850 (and (boolean? (car x)) #t)
853 (usage-desc (delq! #t usage-sans-specials))
854 (kw-desc (map car usage-desc))
855 (kw-opts (apply append (map (lambda (x) (and (not (string? (car x))) x)) kw-desc)))
856 (kw-args (apply append (map (lambda (x) (and (string? (car x)) (cdr x))) kw-desc)))
857 (transmogrified-cases (map (lambda (case)
858 (cons (let ((opts (car case)))
859 (if (or (boolean? opts) (eq? 'else opts))
862 ((symbol? (car opts)) opts)
863 ((boolean? (car opts)) opts)
864 ((string? (caar opts)) (cdar opts))
868 `(let ((%display-usage (lambda () (display-usage-report ',usage-desc))))
870 (let %next-arg ((%argv %argv))
874 (lambda (%opt %arg %new-argv)
876 ,@ transmogrified-cases))))))))
881 ;;; {Low Level Modules}
883 ;;; These are the low level data structures for modules.
885 ;;; !!! warning: The interface to lazy binder procedures is going
886 ;;; to be changed in an incompatible way to permit all the basic
887 ;;; module ops to be virtualized.
889 ;;; (make-module size use-list lazy-binding-proc) => module
890 ;;; module-{obarray,uses,binder}[|-set!]
891 ;;; (module? obj) => [#t|#f]
892 ;;; (module-locally-bound? module symbol) => [#t|#f]
893 ;;; (module-bound? module symbol) => [#t|#f]
894 ;;; (module-symbol-locally-interned? module symbol) => [#t|#f]
895 ;;; (module-symbol-interned? module symbol) => [#t|#f]
896 ;;; (module-local-variable module symbol) => [#<variable ...> | #f]
897 ;;; (module-variable module symbol) => [#<variable ...> | #f]
898 ;;; (module-symbol-binding module symbol opt-value)
899 ;;; => [ <obj> | opt-value | an error occurs ]
900 ;;; (module-make-local-var! module symbol) => #<variable...>
901 ;;; (module-add! module symbol var) => unspecified
902 ;;; (module-remove! module symbol) => unspecified
903 ;;; (module-for-each proc module) => unspecified
904 ;;; (make-scm-module) => module ; a lazy copy of the symhash module
905 ;;; (set-current-module module) => unspecified
906 ;;; (current-module) => #<module...>
911 ;;; {Printing Modules}
912 ;; This is how modules are printed. You can re-define it.
913 ;; (Redefining is actually more complicated than simply redefining
914 ;; %print-module because that would only change the binding and not
915 ;; the value stored in the vtable that determines how record are
918 (define (%print-module mod port) ; unused args: depth length style table)
920 (display (or (module-kind mod) "module") port)
921 (let ((name (module-name mod)))
925 (display name port))))
927 (display (number->string (object-address mod) 16) port)
932 ;; A module is characterized by an obarray in which local symbols
933 ;; are interned, a list of modules, "uses", from which non-local
934 ;; bindings can be inherited, and an optional lazy-binder which
935 ;; is a (CLOSURE module symbol) which, as a last resort, can provide
936 ;; bindings that would otherwise not be found locally in the module.
938 ;; NOTE: If you change here, you also need to change libguile/modules.h.
941 (make-record-type 'module
942 '(obarray uses binder eval-closure transformer name kind
943 observers weak-observers observer-id)
946 ;; make-module &opt size uses binder
948 ;; Create a new module, perhaps with a particular size of obarray,
949 ;; initial uses list, or binding procedure.
954 (define (parse-arg index default)
955 (if (> (length args) index)
956 (list-ref args index)
959 (if (> (length args) 3)
960 (error "Too many args to make-module." args))
962 (let ((size (parse-arg 0 1021))
963 (uses (parse-arg 1 '()))
964 (binder (parse-arg 2 #f)))
966 (if (not (integer? size))
967 (error "Illegal size to make-module." size))
968 (if (not (and (list? uses)
969 (and-map module? uses)))
970 (error "Incorrect use list." uses))
971 (if (and binder (not (procedure? binder)))
973 "Lazy-binder expected to be a procedure or #f." binder))
975 (let ((module (module-constructor (make-vector size '())
976 uses binder #f #f #f #f
978 (make-weak-value-hash-table 31)
981 ;; We can't pass this as an argument to module-constructor,
982 ;; because we need it to close over a pointer to the module
984 (set-module-eval-closure! module (standard-eval-closure module))
988 (define module-constructor (record-constructor module-type))
989 (define module-obarray (record-accessor module-type 'obarray))
990 (define set-module-obarray! (record-modifier module-type 'obarray))
991 (define module-uses (record-accessor module-type 'uses))
992 (define set-module-uses! (record-modifier module-type 'uses))
993 (define module-binder (record-accessor module-type 'binder))
994 (define set-module-binder! (record-modifier module-type 'binder))
996 ;; NOTE: This binding is used in libguile/modules.c.
997 (define module-eval-closure (record-accessor module-type 'eval-closure))
999 (define module-transformer (record-accessor module-type 'transformer))
1000 (define set-module-transformer! (record-modifier module-type 'transformer))
1001 (define module-name (record-accessor module-type 'name))
1002 (define set-module-name! (record-modifier module-type 'name))
1003 (define module-kind (record-accessor module-type 'kind))
1004 (define set-module-kind! (record-modifier module-type 'kind))
1005 (define module-observers (record-accessor module-type 'observers))
1006 (define set-module-observers! (record-modifier module-type 'observers))
1007 (define module-weak-observers (record-accessor module-type 'weak-observers))
1008 (define module-observer-id (record-accessor module-type 'observer-id))
1009 (define set-module-observer-id! (record-modifier module-type 'observer-id))
1010 (define module? (record-predicate module-type))
1012 (define set-module-eval-closure!
1013 (let ((setter (record-modifier module-type 'eval-closure)))
1014 (lambda (module closure)
1015 (setter module closure)
1016 ;; Make it possible to lookup the module from the environment.
1017 ;; This implementation is correct since an eval closure can belong
1018 ;; to maximally one module.
1019 (set-procedure-property! closure 'module module))))
1022 (define (eval-in-module exp mod)
1023 (issue-deprecation-warning
1024 "`eval-in-module' is deprecated. Use `eval' instead.")
1028 ;;; {Observer protocol}
1031 (define (module-observe module proc)
1032 (set-module-observers! module (cons proc (module-observers module)))
1035 (define (module-observe-weak module proc)
1036 (let ((id (module-observer-id module)))
1037 (hash-set! (module-weak-observers module) id proc)
1038 (set-module-observer-id! module (+ 1 id))
1041 (define (module-unobserve token)
1042 (let ((module (car token))
1045 (hash-remove! (module-weak-observers module) id)
1046 (set-module-observers! module (delq1! id (module-observers module)))))
1049 (define (module-modified m)
1050 (for-each (lambda (proc) (proc m)) (module-observers m))
1051 (hash-fold (lambda (id proc res) (proc m)) #f (module-weak-observers m)))
1054 ;;; {Module Searching in General}
1056 ;;; We sometimes want to look for properties of a symbol
1057 ;;; just within the obarray of one module. If the property
1058 ;;; holds, then it is said to hold ``locally'' as in, ``The symbol
1059 ;;; DISPLAY is locally rebound in the module `safe-guile'.''
1062 ;;; Other times, we want to test for a symbol property in the obarray
1063 ;;; of M and, if it is not found there, try each of the modules in the
1064 ;;; uses list of M. This is the normal way of testing for some
1065 ;;; property, so we state these properties without qualification as
1066 ;;; in: ``The symbol 'fnord is interned in module M because it is
1067 ;;; interned locally in module M2 which is a member of the uses list
1071 ;; module-search fn m
1073 ;; return the first non-#f result of FN applied to M and then to
1074 ;; the modules in the uses of m, and so on recursively. If all applications
1075 ;; return #f, then so does this function.
1077 (define (module-search fn m v)
1080 (or (module-search fn (car pos) v)
1083 (loop (module-uses m))))
1086 ;;; {Is a symbol bound in a module?}
1088 ;;; Symbol S in Module M is bound if S is interned in M and if the binding
1089 ;;; of S in M has been set to some well-defined value.
1092 ;; module-locally-bound? module symbol
1094 ;; Is a symbol bound (interned and defined) locally in a given module?
1096 (define (module-locally-bound? m v)
1097 (let ((var (module-local-variable m v)))
1099 (variable-bound? var))))
1101 ;; module-bound? module symbol
1103 ;; Is a symbol bound (interned and defined) anywhere in a given module
1106 (define (module-bound? m v)
1107 (module-search module-locally-bound? m v))
1109 ;;; {Is a symbol interned in a module?}
1111 ;;; Symbol S in Module M is interned if S occurs in
1112 ;;; of S in M has been set to some well-defined value.
1114 ;;; It is possible to intern a symbol in a module without providing
1115 ;;; an initial binding for the corresponding variable. This is done
1117 ;;; (module-add! module symbol (make-undefined-variable))
1119 ;;; In that case, the symbol is interned in the module, but not
1120 ;;; bound there. The unbound symbol shadows any binding for that
1121 ;;; symbol that might otherwise be inherited from a member of the uses list.
1124 (define (module-obarray-get-handle ob key)
1125 ((if (symbol? key) hashq-get-handle hash-get-handle) ob key))
1127 (define (module-obarray-ref ob key)
1128 ((if (symbol? key) hashq-ref hash-ref) ob key))
1130 (define (module-obarray-set! ob key val)
1131 ((if (symbol? key) hashq-set! hash-set!) ob key val))
1133 (define (module-obarray-remove! ob key)
1134 ((if (symbol? key) hashq-remove! hash-remove!) ob key))
1136 ;; module-symbol-locally-interned? module symbol
1138 ;; is a symbol interned (not neccessarily defined) locally in a given module
1139 ;; or its uses? Interned symbols shadow inherited bindings even if
1140 ;; they are not themselves bound to a defined value.
1142 (define (module-symbol-locally-interned? m v)
1143 (not (not (module-obarray-get-handle (module-obarray m) v))))
1145 ;; module-symbol-interned? module symbol
1147 ;; is a symbol interned (not neccessarily defined) anywhere in a given module
1148 ;; or its uses? Interned symbols shadow inherited bindings even if
1149 ;; they are not themselves bound to a defined value.
1151 (define (module-symbol-interned? m v)
1152 (module-search module-symbol-locally-interned? m v))
1155 ;;; {Mapping modules x symbols --> variables}
1158 ;; module-local-variable module symbol
1159 ;; return the local variable associated with a MODULE and SYMBOL.
1161 ;;; This function is very important. It is the only function that can
1162 ;;; return a variable from a module other than the mutators that store
1163 ;;; new variables in modules. Therefore, this function is the location
1164 ;;; of the "lazy binder" hack.
1166 ;;; If symbol is defined in MODULE, and if the definition binds symbol
1167 ;;; to a variable, return that variable object.
1169 ;;; If the symbols is not found at first, but the module has a lazy binder,
1170 ;;; then try the binder.
1172 ;;; If the symbol is not found at all, return #f.
1174 (define (module-local-variable m v)
1177 (let ((b (module-obarray-ref (module-obarray m) v)))
1178 (or (and (variable? b) b)
1179 (and (module-binder m)
1180 ((module-binder m) m v #f)))))
1183 ;; module-variable module symbol
1185 ;; like module-local-variable, except search the uses in the
1186 ;; case V is not found in M.
1188 ;; NOTE: This function is superseded with C code (see modules.c)
1189 ;;; when using the standard eval closure.
1191 (define (module-variable m v)
1192 (module-search module-local-variable m v))
1195 ;;; {Mapping modules x symbols --> bindings}
1197 ;;; These are similar to the mapping to variables, except that the
1198 ;;; variable is dereferenced.
1201 ;; module-symbol-binding module symbol opt-value
1203 ;; return the binding of a variable specified by name within
1204 ;; a given module, signalling an error if the variable is unbound.
1205 ;; If the OPT-VALUE is passed, then instead of signalling an error,
1206 ;; return OPT-VALUE.
1208 (define (module-symbol-local-binding m v . opt-val)
1209 (let ((var (module-local-variable m v)))
1212 (if (not (null? opt-val))
1214 (error "Locally unbound variable." v)))))
1216 ;; module-symbol-binding module symbol opt-value
1218 ;; return the binding of a variable specified by name within
1219 ;; a given module, signalling an error if the variable is unbound.
1220 ;; If the OPT-VALUE is passed, then instead of signalling an error,
1221 ;; return OPT-VALUE.
1223 (define (module-symbol-binding m v . opt-val)
1224 (let ((var (module-variable m v)))
1227 (if (not (null? opt-val))
1229 (error "Unbound variable." v)))))
1233 ;;; {Adding Variables to Modules}
1238 ;; module-make-local-var! module symbol
1240 ;; ensure a variable for V in the local namespace of M.
1241 ;; If no variable was already there, then create a new and uninitialzied
1244 (define (module-make-local-var! m v)
1245 (or (let ((b (module-obarray-ref (module-obarray m) v)))
1250 (and (module-binder m)
1251 ((module-binder m) m v #t))
1253 (let ((answer (make-undefined-variable)))
1254 (variable-set-name-hint! answer v)
1255 (module-obarray-set! (module-obarray m) v answer)
1259 ;; module-add! module symbol var
1261 ;; ensure a particular variable for V in the local namespace of M.
1263 (define (module-add! m v var)
1264 (if (not (variable? var))
1265 (error "Bad variable to module-add!" var))
1266 (module-obarray-set! (module-obarray m) v var)
1267 (module-modified m))
1271 ;; make sure that a symbol is undefined in the local namespace of M.
1273 (define (module-remove! m v)
1274 (module-obarray-remove! (module-obarray m) v)
1275 (module-modified m))
1277 (define (module-clear! m)
1278 (vector-fill! (module-obarray m) '())
1279 (module-modified m))
1281 ;; MODULE-FOR-EACH -- exported
1283 ;; Call PROC on each symbol in MODULE, with arguments of (SYMBOL VARIABLE).
1285 (define (module-for-each proc module)
1286 (let ((obarray (module-obarray module)))
1287 (do ((index 0 (+ index 1))
1288 (end (vector-length obarray)))
1292 (proc (car bucket) (cdr bucket)))
1293 (vector-ref obarray index)))))
1296 (define (module-map proc module)
1297 (let* ((obarray (module-obarray module))
1298 (end (vector-length obarray)))
1306 (map (lambda (bucket)
1307 (proc (car bucket) (cdr bucket)))
1308 (vector-ref obarray i))
1312 ;;; {Low Level Bootstrapping}
1317 ;; A root module uses the pre-modules-obarray as its obarray. This
1318 ;; special obarray accumulates all bindings that have been established
1319 ;; before the module system is fully booted.
1321 ;; (The obarray continues to be used by code that has been closed over
1322 ;; before the module system has been booted.)
1324 (define (make-root-module)
1325 (let ((m (make-module 0)))
1326 (set-module-obarray! m (%get-pre-modules-obarray))
1331 ;; The root interface is a module that uses the same obarray as the
1332 ;; root module. It does not allow new definitions, tho.
1334 (define (make-scm-module)
1335 (let ((m (make-module 0)))
1336 (set-module-obarray! m (%get-pre-modules-obarray))
1337 (set-module-eval-closure! m (standard-interface-eval-closure m))
1342 ;;; {Module-based Loading}
1345 (define (save-module-excursion thunk)
1346 (let ((inner-module (current-module))
1348 (dynamic-wind (lambda ()
1349 (set! outer-module (current-module))
1350 (set-current-module inner-module)
1351 (set! inner-module #f))
1354 (set! inner-module (current-module))
1355 (set-current-module outer-module)
1356 (set! outer-module #f)))))
1358 (define basic-load load)
1360 (define (load-module filename)
1361 (save-module-excursion
1363 (let ((oldname (and (current-load-port)
1364 (port-filename (current-load-port)))))
1365 (basic-load (if (and oldname
1366 (> (string-length filename) 0)
1367 (not (char=? (string-ref filename 0) #\/))
1368 (not (string=? (dirname oldname) ".")))
1369 (string-append (dirname oldname) "/" filename)
1374 ;;; {MODULE-REF -- exported}
1376 ;; Returns the value of a variable called NAME in MODULE or any of its
1377 ;; used modules. If there is no such variable, then if the optional third
1378 ;; argument DEFAULT is present, it is returned; otherwise an error is signaled.
1380 (define (module-ref module name . rest)
1381 (let ((variable (module-variable module name)))
1382 (if (and variable (variable-bound? variable))
1383 (variable-ref variable)
1385 (error "No variable named" name 'in module)
1386 (car rest) ; default value
1389 ;; MODULE-SET! -- exported
1391 ;; Sets the variable called NAME in MODULE (or in a module that MODULE uses)
1392 ;; to VALUE; if there is no such variable, an error is signaled.
1394 (define (module-set! module name value)
1395 (let ((variable (module-variable module name)))
1397 (variable-set! variable value)
1398 (error "No variable named" name 'in module))))
1400 ;; MODULE-DEFINE! -- exported
1402 ;; Sets the variable called NAME in MODULE to VALUE; if there is no such
1403 ;; variable, it is added first.
1405 (define (module-define! module name value)
1406 (let ((variable (module-local-variable module name)))
1409 (variable-set! variable value)
1410 (module-modified module))
1411 (let ((variable (make-variable value)))
1412 (variable-set-name-hint! variable name)
1413 (module-add! module name variable)))))
1415 ;; MODULE-DEFINED? -- exported
1417 ;; Return #t iff NAME is defined in MODULE (or in a module that MODULE
1420 (define (module-defined? module name)
1421 (let ((variable (module-variable module name)))
1422 (and variable (variable-bound? variable))))
1424 ;; MODULE-USE! module interface
1426 ;; Add INTERFACE to the list of interfaces used by MODULE.
1428 (define (module-use! module interface)
1429 (set-module-uses! module
1430 (cons interface (delq! interface (module-uses module))))
1431 (module-modified module))
1434 ;;; {Recursive Namespaces}
1437 ;;; A hierarchical namespace emerges if we consider some module to be
1438 ;;; root, and variables bound to modules as nested namespaces.
1440 ;;; The routines in this file manage variable names in hierarchical namespace.
1441 ;;; Each variable name is a list of elements, looked up in successively nested
1444 ;;; (nested-ref some-root-module '(foo bar baz))
1445 ;;; => <value of a variable named baz in the module bound to bar in
1446 ;;; the module bound to foo in some-root-module>
1451 ;;; ;; a-root is a module
1452 ;;; ;; name is a list of symbols
1454 ;;; nested-ref a-root name
1455 ;;; nested-set! a-root name val
1456 ;;; nested-define! a-root name val
1457 ;;; nested-remove! a-root name
1460 ;;; (current-module) is a natural choice for a-root so for convenience there are
1463 ;;; local-ref name == nested-ref (current-module) name
1464 ;;; local-set! name val == nested-set! (current-module) name val
1465 ;;; local-define! name val == nested-define! (current-module) name val
1466 ;;; local-remove! name == nested-remove! (current-module) name
1470 (define (nested-ref root names)
1471 (let loop ((cur root)
1475 ((not (module? cur)) #f)
1476 (else (loop (module-ref cur (car elts) #f) (cdr elts))))))
1478 (define (nested-set! root names val)
1479 (let loop ((cur root)
1481 (if (null? (cdr elts))
1482 (module-set! cur (car elts) val)
1483 (loop (module-ref cur (car elts)) (cdr elts)))))
1485 (define (nested-define! root names val)
1486 (let loop ((cur root)
1488 (if (null? (cdr elts))
1489 (module-define! cur (car elts) val)
1490 (loop (module-ref cur (car elts)) (cdr elts)))))
1492 (define (nested-remove! root names)
1493 (let loop ((cur root)
1495 (if (null? (cdr elts))
1496 (module-remove! cur (car elts))
1497 (loop (module-ref cur (car elts)) (cdr elts)))))
1499 (define (local-ref names) (nested-ref (current-module) names))
1500 (define (local-set! names val) (nested-set! (current-module) names val))
1501 (define (local-define names val) (nested-define! (current-module) names val))
1502 (define (local-remove names) (nested-remove! (current-module) names))
1506 ;;; {The (app) module}
1508 ;;; The root of conventionally named objects not directly in the top level.
1511 ;;; (app modules guile)
1513 ;;; The directory of all modules and the standard root module.
1516 (define (module-public-interface m)
1517 (module-ref m '%module-public-interface #f))
1518 (define (set-module-public-interface! m i)
1519 (module-define! m '%module-public-interface i))
1520 (define (set-system-module! m s)
1521 (set-procedure-property! (module-eval-closure m) 'system-module s))
1522 (define the-root-module (make-root-module))
1523 (define the-scm-module (make-scm-module))
1524 (set-module-public-interface! the-root-module the-scm-module)
1525 (set-module-name! the-root-module '(guile))
1526 (set-module-name! the-scm-module '(guile))
1527 (set-module-kind! the-scm-module 'interface)
1528 (for-each set-system-module! (list the-root-module the-scm-module) '(#t #t))
1530 ;; NOTE: This binding is used in libguile/modules.c.
1532 (define (make-modules-in module name)
1536 ((module-ref module (car name) #f)
1537 => (lambda (m) (make-modules-in m (cdr name))))
1538 (else (let ((m (make-module 31)))
1539 (set-module-kind! m 'directory)
1540 (set-module-name! m (append (or (module-name module)
1543 (module-define! module (car name) m)
1544 (make-modules-in m (cdr name)))))))
1546 (define (beautify-user-module! module)
1547 (let ((interface (module-public-interface module)))
1548 (if (or (not interface)
1549 (eq? interface module))
1550 (let ((interface (make-module 31)))
1551 (set-module-name! interface (module-name module))
1552 (set-module-kind! interface 'interface)
1553 (set-module-public-interface! module interface))))
1554 (if (and (not (memq the-scm-module (module-uses module)))
1555 (not (eq? module the-root-module)))
1556 (set-module-uses! module (append (module-uses module) (list the-scm-module)))))
1558 ;; NOTE: This binding is used in libguile/modules.c.
1560 (define (resolve-module name . maybe-autoload)
1561 (let ((full-name (append '(app modules) name)))
1562 (let ((already (local-ref full-name)))
1564 ;; The module already exists...
1565 (if (and (or (null? maybe-autoload) (car maybe-autoload))
1566 (not (module-public-interface already)))
1567 ;; ...but we are told to load and it doesn't contain source, so
1569 (try-load-module name)
1571 ;; simply return it.
1574 ;; Try to autoload it if we are told so
1575 (if (or (null? maybe-autoload) (car maybe-autoload))
1576 (try-load-module name))
1578 (make-modules-in (current-module) full-name))))))
1581 (define try-module-autoload #f)
1583 ;; This boots the module system. All bindings needed by modules.c
1584 ;; must have been defined by now.
1586 (set-current-module the-root-module)
1588 (define app (make-module 31))
1589 (local-define '(app modules) (make-module 31))
1590 (local-define '(app modules guile) the-root-module)
1592 ;; (define-special-value '(app modules new-ws) (lambda () (make-scm-module)))
1594 (define (try-load-module name)
1595 (or (try-module-linked name)
1596 (try-module-autoload name)
1597 (try-module-dynamic-link name)))
1599 (define (purify-module! module)
1600 "Removes bindings in MODULE which are inherited from the (guile) module."
1601 (let ((use-list (module-uses module)))
1602 (if (and (pair? use-list)
1603 (eq? (car (last-pair use-list)) the-scm-module))
1604 (set-module-uses! module (reverse (cdr (reverse use-list)))))))
1606 (define (resolve-interface name)
1607 (let ((module (resolve-module name)))
1608 (and module (module-public-interface module))))
1611 ;; Return a module interface made from SPEC.
1612 ;; SPEC can be a list of symbols, in which case it names a module
1613 ;; whose public interface is found and returned.
1615 ;; SPEC can also be of the form:
1616 ;; (MODULE-NAME [:select SELECTION] [:rename RENAMER])
1617 ;; in which case a partial interface is newly created and returned.
1618 ;; MODULE-NAME is a list of symbols, as above; SELECTION is a list of
1619 ;; selection-specs to be imported; and RENAMER is a procedure that takes a
1620 ;; symbol and returns its new name. A selection-spec is either a symbol or a
1621 ;; pair of symbols (ORIG . SEEN), where ORIG is the name in the used module
1622 ;; and SEEN is the name in the using module. Note that SEEN is also passed
1625 ;; The `:select' and `:rename' clauses are optional. If both are omitted, the
1626 ;; returned interface has no bindings. If the `:select' clause is omitted,
1627 ;; RENAMER operates on the used module's public interface.
1629 ;; Signal "no code for module" error if module name is not resolvable or its
1630 ;; public interface is not available. Signal "no binding" error if selected
1631 ;; binding does not exist in the used module.
1633 (define (resolve-interface spec)
1634 (let* ((simple? (not (pair? (car spec))))
1635 (name (if simple? spec (car spec)))
1636 (module (resolve-module name))
1637 (public-i (and module (module-public-interface module))))
1638 (and (or (not module) (not public-i))
1639 (error "no code for module" name))
1642 (let ((selection (cond ((memq ':select spec) => cadr)
1643 (else (module-map (lambda (sym var) sym)
1645 (rename (cond ((memq ':rename spec)
1647 ;; fixme:ttn -- move to macroexpansion time
1648 (eval (cadr x) (current-module))))
1650 (custom-i (make-module 31)))
1651 (set-module-kind! custom-i 'interface)
1652 (for-each (lambda (sel-spec)
1653 (let* ((direct? (symbol? sel-spec))
1660 (module-add! custom-i (rename seen)
1661 (or (module-local-variable module orig)
1663 ;; fixme: format manually for now
1665 #f "no binding `~A' in module ~A"
1670 (define (symbol-prefix-proc prefix)
1672 (symbol-append prefix symbol)))
1674 (define (process-define-module args)
1675 (let* ((module-id (car args))
1676 (module (resolve-module module-id #f))
1678 (unrecognized (lambda ()
1679 (error "unrecognized define-module argument" kws))))
1680 (beautify-user-module! module)
1681 (let loop ((kws kws)
1682 (reversed-interfaces '())
1686 (for-each (lambda (interface)
1687 (module-use! module interface))
1688 reversed-interfaces)
1689 (module-export! module exports))
1690 (let ((keyword (if (keyword? (car kws))
1691 (keyword->symbol (car kws))
1692 (and (symbol? (car kws))
1693 (let ((s (symbol->string (car kws))))
1694 (and (eq? (string-ref s 0) #\:)
1695 (string->symbol (substring s 1))))))))
1697 ((use-module use-syntax)
1698 (or (pair? (cdr kws))
1700 (let* ((spec (cadr kws))
1701 (interface (resolve-interface spec)))
1702 (and (eq? keyword 'use-syntax)
1703 (or (symbol? (car spec))
1704 (error "invalid module name for use-syntax"
1706 (set-module-transformer!
1708 (module-ref interface (car (last-pair module-name))
1711 (cons interface reversed-interfaces)
1714 (or (and (pair? (cdr kws)) (pair? (cddr kws)))
1717 (cons (make-autoload-interface module
1720 reversed-interfaces)
1723 (set-system-module! module #t)
1724 (loop (cdr kws) reversed-interfaces exports))
1726 (purify-module! module)
1727 (loop (cdr kws) reversed-interfaces exports))
1729 (or (pair? (cdr kws))
1733 (append (cadr kws) exports)))
1736 (set-current-module module)
1741 (define (make-autoload-interface module name bindings)
1742 (let ((b (lambda (a sym definep)
1743 (and (memq sym bindings)
1744 (let ((i (module-public-interface (resolve-module name))))
1746 (error "missing interface for module" name))
1747 ;; Replace autoload-interface with interface
1748 (set-car! (memq a (module-uses module)) i)
1749 (module-local-variable i sym))))))
1750 (module-constructor #() '() b #f #f name 'autoload
1751 '() (make-weak-value-hash-table 31) 0)))
1753 ;;; {Compiled module}
1755 (define load-compiled #f)
1758 ;;; {Autoloading modules}
1760 (define autoloads-in-progress '())
1762 (define (try-module-autoload module-name)
1763 (let* ((reverse-name (reverse module-name))
1764 (name (symbol->string (car reverse-name)))
1765 (dir-hint-module-name (reverse (cdr reverse-name)))
1766 (dir-hint (apply string-append
1768 (string-append (symbol->string elt) "/"))
1769 dir-hint-module-name))))
1770 (resolve-module dir-hint-module-name #f)
1771 (and (not (autoload-done-or-in-progress? dir-hint name))
1773 (define (load-file proc file)
1774 (save-module-excursion (lambda () (proc file)))
1777 (lambda () (autoload-in-progress! dir-hint name))
1779 (let ((file (in-vicinity dir-hint name)))
1780 (cond ((and load-compiled
1781 (%search-load-path (string-append file ".go")))
1783 (load-file load-compiled full)))
1784 ((%search-load-path file)
1786 (load-file primitive-load full))))))
1787 (lambda () (set-autoloaded! dir-hint name didit)))
1791 ;;; Dynamic linking of modules
1793 ;; This method of dynamically linking Guile Extensions is deprecated.
1794 ;; Use `dynamic-link' and `dynamic-call' explicitely from Scheme code
1797 ;; XXX - We can not offer the removal of this code thru the
1798 ;; deprecation mechanism since we have no complete replacement yet.
1800 (define (split-c-module-name str)
1801 (let loop ((rev '())
1804 (end (string-length str)))
1807 (reverse (cons (string->symbol (substring str start pos)) rev)))
1808 ((eq? (string-ref str pos) #\space)
1809 (loop (cons (string->symbol (substring str start pos)) rev)
1814 (loop rev start (+ pos 1) end)))))
1816 (define (convert-c-registered-modules dynobj)
1817 (let ((res (map (lambda (c)
1818 (list (split-c-module-name (car c)) (cdr c) dynobj))
1819 (c-registered-modules))))
1820 (c-clear-registered-modules)
1823 (define registered-modules '())
1825 (define (register-modules dynobj)
1826 (set! registered-modules
1827 (append! (convert-c-registered-modules dynobj)
1828 registered-modules)))
1830 (define (warn-autoload-deprecation modname)
1831 ;; Do nothing here until we can deprecate the code for real.
1833 (issue-deprecation-warning
1834 "Autoloading of compiled code modules is deprecated."
1835 "Write a Scheme file instead that uses `dynamic-link' directly.")))
1837 (define (init-dynamic-module modname)
1838 ;; Register any linked modules which have been registered on the C level
1839 (register-modules #f)
1840 (or-map (lambda (modinfo)
1841 (if (equal? (car modinfo) modname)
1843 (warn-autoload-deprecation modname)
1844 (set! registered-modules (delq! modinfo registered-modules))
1845 (let ((mod (resolve-module modname #f)))
1846 (save-module-excursion
1848 (set-current-module mod)
1849 (set-module-public-interface! mod mod)
1850 (dynamic-call (cadr modinfo) (caddr modinfo))
1854 registered-modules))
1856 (define (dynamic-maybe-call name dynobj)
1857 (catch #t ; could use false-if-exception here
1859 (dynamic-call name dynobj))
1863 (define (dynamic-maybe-link filename)
1864 (catch #t ; could use false-if-exception here
1866 (dynamic-link filename))
1870 (define (find-and-link-dynamic-module module-name)
1871 (define (make-init-name mod-name)
1872 (string-append "scm_init"
1873 (list->string (map (lambda (c)
1874 (if (or (char-alphabetic? c)
1878 (string->list mod-name)))
1881 ;; Put the subdirectory for this module in the car of SUBDIR-AND-LIBNAME,
1882 ;; and the `libname' (the name of the module prepended by `lib') in the cdr
1883 ;; field. For example, if MODULE-NAME is the list (inet tcp-ip udp), then
1884 ;; SUBDIR-AND-LIBNAME will be the pair ("inet/tcp-ip" . "libudp").
1885 (let ((subdir-and-libname
1886 (let loop ((dirs "")
1888 (if (null? (cdr syms))
1889 (cons dirs (string-append "lib" (symbol->string (car syms))))
1890 (loop (string-append dirs (symbol->string (car syms)) "/")
1892 (init (make-init-name (apply string-append
1895 (symbol->string s)))
1897 (let ((subdir (car subdir-and-libname))
1898 (libname (cdr subdir-and-libname)))
1900 ;; Now look in each dir in %LOAD-PATH for `subdir/libfoo.la'. If that
1901 ;; file exists, fetch the dlname from that file and attempt to link
1902 ;; against it. If `subdir/libfoo.la' does not exist, or does not seem
1903 ;; to name any shared library, look for `subdir/libfoo.so' instead and
1904 ;; link against that.
1905 (let check-dirs ((dir-list %load-path))
1906 (if (null? dir-list)
1908 (let* ((dir (in-vicinity (car dir-list) subdir))
1910 (or (try-using-libtool-name dir libname)
1911 (try-using-sharlib-name dir libname))))
1912 (if (and sharlib-full (file-exists? sharlib-full))
1913 (link-dynamic-module sharlib-full init)
1914 (check-dirs (cdr dir-list)))))))))
1916 (define (try-using-libtool-name libdir libname)
1917 (let ((libtool-filename (in-vicinity libdir
1918 (string-append libname ".la"))))
1919 (and (file-exists? libtool-filename)
1922 (define (try-using-sharlib-name libdir libname)
1923 (in-vicinity libdir (string-append libname ".so")))
1925 (define (link-dynamic-module filename initname)
1926 ;; Register any linked modules which has been registered on the C level
1927 (register-modules #f)
1928 (let ((dynobj (dynamic-link filename)))
1929 (dynamic-call initname dynobj)
1930 (register-modules dynobj)))
1932 (define (try-module-linked module-name)
1933 (init-dynamic-module module-name))
1935 (define (try-module-dynamic-link module-name)
1936 (and (find-and-link-dynamic-module module-name)
1937 (init-dynamic-module module-name)))
1941 (define autoloads-done '((guile . guile)))
1943 (define (autoload-done-or-in-progress? p m)
1944 (let ((n (cons p m)))
1945 (->bool (or (member n autoloads-done)
1946 (member n autoloads-in-progress)))))
1948 (define (autoload-done! p m)
1949 (let ((n (cons p m)))
1950 (set! autoloads-in-progress
1951 (delete! n autoloads-in-progress))
1952 (or (member n autoloads-done)
1953 (set! autoloads-done (cons n autoloads-done)))))
1955 (define (autoload-in-progress! p m)
1956 (let ((n (cons p m)))
1957 (set! autoloads-done
1958 (delete! n autoloads-done))
1959 (set! autoloads-in-progress (cons n autoloads-in-progress))))
1961 (define (set-autoloaded! p m done?)
1963 (autoload-done! p m)
1964 (let ((n (cons p m)))
1965 (set! autoloads-done (delete! n autoloads-done))
1966 (set! autoloads-in-progress (delete! n autoloads-in-progress)))))
1973 ;; (eval-case ((situation*) forms)* (else forms)?)
1975 ;; Evaluate certain code based on the situation that eval-case is used
1976 ;; in. The only defined situation right now is `load-toplevel' which
1977 ;; triggers for code evaluated at the top-level, for example from the
1978 ;; REPL or when loading a file.
1981 (procedure->memoizing-macro
1983 (define (toplevel-env? env)
1984 (or (not (pair? env)) (not (pair? (car env)))))
1986 (error "syntax error in eval-case"))
1987 (let loop ((clauses (cdr exp)))
1991 ((not (list? (car clauses)))
1993 ((eq? 'else (caar clauses))
1994 (or (null? (cdr clauses))
1996 (cons 'begin (cdar clauses)))
1997 ((not (list? (caar clauses)))
1999 ((and (toplevel-env? env)
2000 (memq 'load-toplevel (caar clauses)))
2001 (cons 'begin (cdar clauses)))
2003 (loop (cdr clauses))))))))
2009 (define (primitive-macro? m)
2011 (not (macro-transformer m))))
2015 (define macro-table (make-weak-key-hash-table 523))
2016 (define xformer-table (make-weak-key-hash-table 523))
2018 (define (defmacro? m) (hashq-ref macro-table m))
2019 (define (assert-defmacro?! m) (hashq-set! macro-table m #t))
2020 (define (defmacro-transformer m) (hashq-ref xformer-table m))
2021 (define (set-defmacro-transformer! m t) (hashq-set! xformer-table m t))
2023 (define defmacro:transformer
2025 (let* ((xform (lambda (exp env)
2026 (copy-tree (apply f (cdr exp)))))
2027 (a (procedure->memoizing-macro xform)))
2028 (assert-defmacro?! a)
2029 (set-defmacro-transformer! a f)
2034 (let ((defmacro-transformer
2035 (lambda (name parms . body)
2036 (let ((transformer `(lambda ,parms ,@body)))
2039 (define ,name (defmacro:transformer ,transformer)))
2041 (error "defmacro can only be used at the top level")))))))
2042 (defmacro:transformer defmacro-transformer)))
2044 (define defmacro:syntax-transformer
2048 (copy-tree (apply f (cdr exp)))))))
2051 ;; XXX - should the definition of the car really be looked up in the
2054 (define (macroexpand-1 e)
2056 ((pair? e) (let* ((a (car e))
2057 (val (and (symbol? a) (local-ref (list a)))))
2059 (apply (defmacro-transformer val) (cdr e))
2063 (define (macroexpand e)
2065 ((pair? e) (let* ((a (car e))
2066 (val (and (symbol? a) (local-ref (list a)))))
2068 (macroexpand (apply (defmacro-transformer val) (cdr e)))
2076 ;;; {Run-time options}
2078 (define define-option-interface
2079 (let* ((option-name car)
2081 (option-documentation caddr)
2083 (print-option (lambda (option)
2084 (display (option-name option))
2085 (if (< (string-length
2086 (symbol->string (option-name option)))
2090 (display (option-value option))
2092 (display (option-documentation option))
2095 ;; Below follow the macros defining the run-time option interfaces.
2097 (make-options (lambda (interface)
2099 (cond ((null? args) (,interface))
2101 (,interface (car args)) (,interface))
2102 (else (for-each ,print-option
2103 (,interface #t)))))))
2105 (make-enable (lambda (interface)
2107 (,interface (append flags (,interface)))
2110 (make-disable (lambda (interface)
2112 (let ((options (,interface)))
2113 (for-each (lambda (flag)
2114 (set! options (delq! flag options)))
2116 (,interface options)
2119 (make-set! (lambda (interface)
2122 (begin (,interface (append (,interface)
2123 (list '(,'unquote name)
2129 (let* ((option-group (cadr exp))
2130 (interface (car option-group)))
2131 (append (map (lambda (name constructor)
2133 ,(constructor interface)))
2138 (map (lambda (name constructor)
2140 ,@(constructor interface)))
2141 (caddr option-group)
2142 (list make-set!)))))))))
2144 (define-option-interface
2145 (eval-options-interface
2146 (eval-options eval-enable eval-disable)
2149 (define-option-interface
2150 (debug-options-interface
2151 (debug-options debug-enable debug-disable)
2154 (define-option-interface
2155 (evaluator-traps-interface
2156 (traps trap-enable trap-disable)
2159 (define-option-interface
2160 (read-options-interface
2161 (read-options read-enable read-disable)
2164 (define-option-interface
2165 (print-options-interface
2166 (print-options print-enable print-disable)
2174 (define (repl read evaler print)
2175 (let loop ((source (read (current-input-port))))
2176 (print (evaler source))
2177 (loop (read (current-input-port)))))
2179 ;; A provisional repl that acts like the SCM repl:
2181 (define scm-repl-silent #f)
2182 (define (assert-repl-silence v) (set! scm-repl-silent v))
2184 (define *unspecified* (if #f #f))
2185 (define (unspecified? v) (eq? v *unspecified*))
2187 (define scm-repl-print-unspecified #f)
2188 (define (assert-repl-print-unspecified v) (set! scm-repl-print-unspecified v))
2190 (define scm-repl-verbose #f)
2191 (define (assert-repl-verbosity v) (set! scm-repl-verbose v))
2193 (define scm-repl-prompt "guile> ")
2195 (define (set-repl-prompt! v) (set! scm-repl-prompt v))
2197 (define (default-lazy-handler key . args)
2198 (save-stack lazy-handler-dispatch)
2199 (apply throw key args))
2201 (define enter-frame-handler default-lazy-handler)
2202 (define apply-frame-handler default-lazy-handler)
2203 (define exit-frame-handler default-lazy-handler)
2205 (define (lazy-handler-dispatch key . args)
2208 (apply apply-frame-handler key args))
2210 (apply exit-frame-handler key args))
2212 (apply enter-frame-handler key args))
2214 (apply default-lazy-handler key args))))
2216 (define abort-hook (make-hook))
2218 ;; these definitions are used if running a script.
2219 ;; otherwise redefined in error-catching-loop.
2220 (define (set-batch-mode?! arg) #t)
2221 (define (batch-mode?) #t)
2223 (define (error-catching-loop thunk)
2226 (define (loop first)
2234 (lambda () (unmask-signals))
2240 ;; This line is needed because mark
2241 ;; doesn't do closures quite right.
2242 ;; Unreferenced locals should be
2246 (let loop ((v (thunk)))
2249 (lambda () (mask-signals))))
2251 lazy-handler-dispatch))
2253 (lambda (key . args)
2260 (apply throw 'switch-repl args))
2263 ;; This is one of the closures that require
2264 ;; (set! first #f) above
2267 (run-hook abort-hook)
2268 (force-output (current-output-port))
2269 (display "ABORT: " (current-error-port))
2270 (write args (current-error-port))
2271 (newline (current-error-port))
2275 (not has-shown-debugger-hint?)
2276 (not (memq 'backtrace
2277 (debug-options-interface)))
2278 (stack? (fluid-ref the-last-stack)))
2280 (newline (current-error-port))
2282 "Type \"(backtrace)\" to get more information or \"(debug)\" to enter the debugger.\n"
2283 (current-error-port))
2284 (set! has-shown-debugger-hint? #t)))
2285 (force-output (current-error-port)))
2287 (primitive-exit 1)))
2288 (set! stack-saved? #f)))
2291 ;; This is the other cons-leak closure...
2293 (cond ((= (length args) 4)
2294 (apply handle-system-error key args))
2296 (apply bad-throw key args))))))))))
2297 (if next (loop next) status)))
2298 (set! set-batch-mode?! (lambda (arg)
2300 (set! interactive #f)
2303 (error "sorry, not implemented")))))
2304 (set! batch-mode? (lambda () (not interactive)))
2305 (loop (lambda () #t))))
2307 ;;(define the-last-stack (make-fluid)) Defined by scm_init_backtrace ()
2308 (define before-signal-stack (make-fluid))
2309 (define stack-saved? #f)
2311 (define (save-stack . narrowing)
2313 (cond ((not (memq 'debug (debug-options-interface)))
2314 (fluid-set! the-last-stack #f)
2315 (set! stack-saved? #t))
2321 (apply make-stack #t save-stack primitive-eval #t 0 narrowing))
2323 (apply make-stack #t save-stack 0 #t 0 narrowing))
2325 (apply make-stack #t save-stack tk-stack-mark #t 0 narrowing))
2327 (apply make-stack #t save-stack 0 1 narrowing))
2329 (let ((id (stack-id #t)))
2330 (and (procedure? id)
2331 (apply make-stack #t save-stack id #t 0 narrowing))))))
2332 (set! stack-saved? #t)))))
2334 (define before-error-hook (make-hook))
2335 (define after-error-hook (make-hook))
2336 (define before-backtrace-hook (make-hook))
2337 (define after-backtrace-hook (make-hook))
2339 (define has-shown-debugger-hint? #f)
2341 (define (handle-system-error key . args)
2342 (let ((cep (current-error-port)))
2343 (cond ((not (stack? (fluid-ref the-last-stack))))
2344 ((memq 'backtrace (debug-options-interface))
2345 (run-hook before-backtrace-hook)
2347 (display "Backtrace:\n")
2348 (display-backtrace (fluid-ref the-last-stack) cep)
2350 (run-hook after-backtrace-hook)))
2351 (run-hook before-error-hook)
2352 (apply display-error (fluid-ref the-last-stack) cep args)
2353 (run-hook after-error-hook)
2355 (throw 'abort key)))
2357 (define (quit . args)
2358 (apply throw 'quit args))
2362 ;;(define has-shown-backtrace-hint? #f) Defined by scm_init_backtrace ()
2364 ;; Replaced by C code:
2365 ;;(define (backtrace)
2366 ;; (if (fluid-ref the-last-stack)
2369 ;; (display-backtrace (fluid-ref the-last-stack) (current-output-port))
2371 ;; (if (and (not has-shown-backtrace-hint?)
2372 ;; (not (memq 'backtrace (debug-options-interface))))
2375 ;;"Type \"(debug-enable 'backtrace)\" if you would like a backtrace
2376 ;;automatically if an error occurs in the future.\n")
2377 ;; (set! has-shown-backtrace-hint? #t))))
2378 ;; (display "No backtrace available.\n")))
2380 (define (error-catching-repl r e p)
2381 (error-catching-loop
2383 (call-with-values (lambda () (e (r)))
2384 (lambda the-values (for-each p the-values))))))
2386 (define (gc-run-time)
2387 (cdr (assq 'gc-time-taken (gc-stats))))
2389 (define before-read-hook (make-hook))
2390 (define after-read-hook (make-hook))
2391 (define before-eval-hook (make-hook 1))
2392 (define after-eval-hook (make-hook 1))
2393 (define before-print-hook (make-hook 1))
2394 (define after-print-hook (make-hook 1))
2396 ;;; The default repl-reader function. We may override this if we've
2397 ;;; the readline library.
2402 (run-hook before-read-hook)
2403 (read (current-input-port))))
2405 (define (scm-style-repl)
2410 (repl-report-start-timing (lambda ()
2411 (set! start-gc-rt (gc-run-time))
2412 (set! start-rt (get-internal-run-time))))
2413 (repl-report (lambda ()
2415 (display (inexact->exact
2416 (* 1000 (/ (- (get-internal-run-time) start-rt)
2417 internal-time-units-per-second))))
2419 (display (inexact->exact
2420 (* 1000 (/ (- (gc-run-time) start-gc-rt)
2421 internal-time-units-per-second))))
2422 (display " msec in gc)\n")))
2424 (consume-trailing-whitespace
2426 (let ((ch (peek-char)))
2429 ((or (char=? ch #\space) (char=? ch #\tab))
2431 (consume-trailing-whitespace))
2432 ((char=? ch #\newline)
2436 (let ((prompt (cond ((string? scm-repl-prompt)
2438 ((thunk? scm-repl-prompt)
2440 (scm-repl-prompt "> ")
2442 (repl-reader prompt))))
2444 ;; As described in R4RS, the READ procedure updates the
2445 ;; port to point to the first character past the end of
2446 ;; the external representation of the object. This
2447 ;; means that it doesn't consume the newline typically
2448 ;; found after an expression. This means that, when
2449 ;; debugging Guile with GDB, GDB gets the newline, which
2450 ;; it often interprets as a "continue" command, making
2451 ;; breakpoints kind of useless. So, consume any
2452 ;; trailing newline here, as well as any whitespace
2454 ;; But not if EOF, for control-D.
2455 (if (not (eof-object? val))
2456 (consume-trailing-whitespace))
2457 (run-hook after-read-hook)
2458 (if (eof-object? val)
2460 (repl-report-start-timing)
2461 (if scm-repl-verbose
2464 (display ";;; EOF -- quitting")
2469 (-eval (lambda (sourc)
2470 (repl-report-start-timing)
2471 (run-hook before-eval-hook sourc)
2472 (let ((val (start-stack 'repl-stack
2473 ;; If you change this procedure
2474 ;; (primitive-eval), please also
2475 ;; modify the repl-stack case in
2476 ;; save-stack so that stack cutting
2477 ;; continues to work.
2478 (primitive-eval sourc))))
2479 (run-hook after-eval-hook sourc)
2483 (-print (let ((maybe-print (lambda (result)
2484 (if (or scm-repl-print-unspecified
2485 (not (unspecified? result)))
2490 (if (not scm-repl-silent)
2492 (run-hook before-print-hook result)
2493 (maybe-print result)
2494 (run-hook after-print-hook result)
2495 (if scm-repl-verbose
2499 (-quit (lambda (args)
2500 (if scm-repl-verbose
2502 (display ";;; QUIT executed, repl exitting")
2508 (if scm-repl-verbose
2510 (display ";;; ABORT executed.")
2513 (repl -read -eval -print))))
2515 (let ((status (error-catching-repl -read
2522 ;;; {IOTA functions: generating lists of numbers}
2525 (let loop ((count (1- n)) (result '()))
2526 (if (< count 0) result
2527 (loop (1- count) (cons count result)))))
2532 ;;; with `continue' and `break'.
2535 (defmacro while (cond . body)
2536 `(letrec ((continue (lambda () (or (not ,cond) (begin (begin ,@ body) (continue)))))
2537 (break (lambda val (apply throw 'break val))))
2539 (lambda () (continue))
2540 (lambda v (cadr v)))))
2544 ;;; Similar to `begin' but returns a list of the results of all constituent
2545 ;;; forms instead of the result of the last form.
2546 ;;; (The definition relies on the current left-to-right
2547 ;;; order of evaluation of operands in applications.)
2549 (defmacro collect forms
2554 ;; with-fluids is a convenience wrapper for the builtin procedure
2555 ;; `with-fluids*'. The syntax is just like `let':
2557 ;; (with-fluids ((fluid val)
2561 (defmacro with-fluids (bindings . body)
2562 `(with-fluids* (list ,@(map car bindings)) (list ,@(map cadr bindings))
2563 (lambda () ,@body)))
2570 ;; actually....hobbit might be able to hack these with a little
2574 (defmacro define-macro (first . rest)
2575 (let ((name (if (symbol? first) first (car first)))
2579 `(lambda ,(cdr first) ,@rest))))
2582 (define ,name (defmacro:transformer ,transformer)))
2584 (error "define-macro can only be used at the top level")))))
2587 (defmacro define-syntax-macro (first . rest)
2588 (let ((name (if (symbol? first) first (car first)))
2592 `(lambda ,(cdr first) ,@rest))))
2595 (define ,name (defmacro:syntax-transformer ,transformer)))
2597 (error "define-syntax-macro can only be used at the top level")))))
2600 ;;; {Module System Macros}
2603 (defmacro define-module args
2606 (process-define-module ',args))
2608 (error "define-module can only be used at the top level"))))
2610 ;; the guts of the use-modules macro. add the interfaces of the named
2611 ;; modules to the use-list of the current module, in order
2612 (define (process-use-modules module-interface-specs)
2613 (for-each (lambda (mif-spec)
2614 (let ((mod-iface (resolve-interface mif-spec)))
2616 (error "no such module" mif-spec))
2617 (module-use! (current-module) mod-iface)))
2618 module-interface-specs))
2620 (defmacro use-modules modules
2623 (process-use-modules ',modules))
2625 (error "use-modules can only be used at the top level"))))
2627 (defmacro use-syntax (spec)
2631 `((process-use-modules ',(list spec))
2632 (set-module-transformer! (current-module)
2633 ,(car (last-pair spec))))
2634 `((set-module-transformer! (current-module) ,spec)))
2635 (fluid-set! scm:eval-transformer (module-transformer (current-module))))
2637 (error "use-syntax can only be used at the top level"))))
2639 (define define-private define)
2641 (defmacro define-public args
2643 (error "bad syntax" (list 'define-public args)))
2644 (define (defined-name n)
2647 ((pair? n) (defined-name (car n)))
2653 (let ((name (defined-name (car args))))
2655 (eval-case ((load-toplevel) (export ,name)))
2656 (define-private ,@args))))))
2658 (defmacro defmacro-public args
2660 (error "bad syntax" (list 'defmacro-public args)))
2661 (define (defined-name n)
2669 (let ((name (defined-name (car args))))
2671 (eval-case ((load-toplevel) (export ,name)))
2672 (defmacro ,@args))))))
2674 (define (module-export! m names)
2675 (let ((public-i (module-public-interface m)))
2676 (for-each (lambda (name)
2677 ;; Make sure there is a local variable:
2678 (module-define! m name (module-ref m name #f))
2679 ;; Make sure that local is exported:
2680 (module-add! public-i name (module-variable m name)))
2683 (defmacro export names
2686 (module-export! (current-module) ',names))
2688 (error "export can only be used at the top level"))))
2690 (define export-syntax export)
2693 (define load load-module)
2697 ;;; {`cond-expand' for SRFI-0 support.}
2699 ;;; This syntactic form expands into different commands or
2700 ;;; definitions, depending on the features provided by the Scheme
2706 ;;; --> (cond-expand <cond-expand-clause>+)
2707 ;;; | (cond-expand <cond-expand-clause>* (else <command-or-definition>))
2708 ;;; <cond-expand-clause>
2709 ;;; --> (<feature-requirement> <command-or-definition>*)
2710 ;;; <feature-requirement>
2711 ;;; --> <feature-identifier>
2712 ;;; | (and <feature-requirement>*)
2713 ;;; | (or <feature-requirement>*)
2714 ;;; | (not <feature-requirement>)
2715 ;;; <feature-identifier>
2716 ;;; --> <a symbol which is the name or alias of a SRFI>
2718 ;;; Additionally, this implementation provides the
2719 ;;; <feature-identifier>s `guile' and `r5rs', so that programs can
2720 ;;; determine the implementation type and the supported standard.
2722 ;;; Currently, the following feature identifiers are supported:
2724 ;;; guile r5rs srfi-0
2726 ;;; Remember to update the features list when adding more SRFIs.
2728 (define cond-expand-features
2729 ;; Adjust the above comment when changing this.
2730 '(guile r5rs srfi-0))
2732 (define-macro (cond-expand clause . clauses)
2734 (let ((clauses (cons clause clauses))
2735 (syntax-error (lambda (cl)
2736 (error "invalid clause in `cond-expand'" cl))))
2742 (memq clause cond-expand-features))
2745 ((eq? 'and (car clause))
2746 (let lp ((l (cdr clause)))
2750 (and (test-clause (car l)) (lp (cdr l))))
2752 (syntax-error clause)))))
2753 ((eq? 'or (car clause))
2754 (let lp ((l (cdr clause)))
2758 (or (test-clause (car l)) (lp (cdr l))))
2760 (syntax-error clause)))))
2761 ((eq? 'not (car clause))
2762 (cond ((not (pair? (cdr clause)))
2763 (syntax-error clause))
2764 ((pair? (cddr clause))
2765 ((syntax-error clause))))
2766 (not (test-clause (cadr clause))))
2768 (syntax-error clause))))
2770 (syntax-error clause))))))
2771 (let lp ((c clauses))
2774 (error "Unfulfilled `cond-expand'"))
2777 ((not (pair? (car c)))
2778 (syntax-error (car c)))
2779 ((test-clause (caar c))
2780 `(begin ,@(cdar c)))
2781 ((eq? (caar c) 'else)
2784 `(begin ,@(cdar c)))
2788 ;; This procedure gets called from the startup code with a list of
2789 ;; numbers, which are the numbers of the SRFIs to be loaded on startup.
2791 (define (use-srfis srfis)
2794 (let* ((srfi (string->symbol
2795 (string-append "srfi-" (number->string (car s)))))
2796 (mod (resolve-interface (list 'srfi srfi))))
2797 (module-use! (current-module) mod)
2798 (set! cond-expand-features
2799 (append cond-expand-features (list srfi)))
2804 ;;; {Load emacs interface support if emacs option is given.}
2806 (define (named-module-use! user usee)
2807 (module-use! (resolve-module user) (resolve-module usee)))
2809 (define (load-emacs-interface)
2810 (and (provided? 'debug-extensions)
2811 (debug-enable 'backtrace))
2812 (named-module-use! '(guile-user) '(ice-9 emacs)))
2816 (define using-readline?
2817 (let ((using-readline? (make-fluid)))
2818 (make-procedure-with-setter
2819 (lambda () (fluid-ref using-readline?))
2820 (lambda (v) (fluid-set! using-readline? v)))))
2824 ;; Load emacs interface support if emacs option is given.
2825 (if (and (module-defined? the-root-module 'use-emacs-interface)
2826 (module-ref the-root-module 'use-emacs-interface))
2827 (load-emacs-interface))
2829 ;; Place the user in the guile-user module.
2830 (process-define-module
2832 :use-module (guile) ;so that bindings will be checked here first
2833 :use-module (ice-9 session)
2834 :use-module (ice-9 debug)
2835 :autoload (ice-9 debugger) (debug))) ;load debugger on demand
2836 (and (provided? 'threads)
2837 (named-module-use! '(guile-user) '(ice-9 threads)))
2838 (and (provided? 'regex)
2839 (named-module-use! '(guile-user) '(ice-9 regex)))
2841 (let ((old-handlers #f)
2842 (signals (if (provided? 'posix)
2843 `((,SIGINT . "User interrupt")
2844 (,SIGFPE . "Arithmetic error")
2845 (,SIGBUS . "Bad memory access (bus error)")
2847 "Bad memory access (Segmentation violation)"))
2854 (let ((make-handler (lambda (msg)
2856 ;; Make a backup copy of the stack
2857 (fluid-set! before-signal-stack
2858 (fluid-ref the-last-stack))
2859 (save-stack %deliver-signals)
2866 (map (lambda (sig-msg)
2867 (sigaction (car sig-msg)
2868 (make-handler (cdr sig-msg))))
2871 ;; the protected thunk.
2873 (let ((status (scm-style-repl)))
2874 (run-hook exit-hook)
2879 (map (lambda (sig-msg old-handler)
2880 (if (not (car old-handler))
2881 ;; restore original C handler.
2882 (sigaction (car sig-msg) #f)
2883 ;; restore Scheme handler, SIG_IGN or SIG_DFL.
2884 (sigaction (car sig-msg)
2886 (cdr old-handler))))
2887 signals old-handlers)))))
2889 (defmacro false-if-exception (expr)
2890 `(catch #t (lambda () ,expr)
2893 ;;; This hook is run at the very end of an interactive session.
2895 (define exit-hook (make-hook))
2898 (define-module (guile))
2900 (append! %load-path (cons "." '()))
2902 ;;; boot-9.scm ends here