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
22 ;;; This file is the first thing loaded into Guile. It adds many mundane
23 ;;; definitions and a few that are interesting.
25 ;;; The module system (hence the hierarchical namespace) are defined in this
34 (if (not (memq sym *features*))
35 (set! *features* (cons sym *features*))))
37 ;;; Return #t iff FEATURE is available to this Guile interpreter.
38 ;;; In SLIB, provided? also checks to see if the module is available.
39 ;;; We should do that too, but don't.
40 (define (provided? feature)
41 (and (memq feature *features*) #t))
43 ;;; presumably deprecated.
44 (define feature? provided?)
46 ;;; let format alias simple-format until the more complete version is loaded
47 (define format simple-format)
52 (primitive-load-path "ice-9/r4rs.scm")
55 ;;; {Simple Debugging Tools}
59 ;; peek takes any number of arguments, writes them to the
60 ;; current ouput port, and returns the last argument.
61 ;; It is handy to wrap around an expression to look at
62 ;; a value each time is evaluated, e.g.:
64 ;; (+ 10 (troublesome-fn))
65 ;; => (+ 10 (pk 'troublesome-fn-returned (troublesome-fn)))
68 (define (peek . stuff)
73 (car (last-pair stuff)))
77 (define (warn . stuff)
78 (with-output-to-port (current-error-port)
81 (display ";;; WARNING ")
84 (car (last-pair stuff)))))
87 ;;; {Trivial Functions}
91 (define (1+ n) (+ n 1))
92 (define (-1+ n) (+ n -1))
94 (define return-it noop)
95 (define (and=> value procedure) (and value (procedure value)))
96 (define (make-hash-table k) (make-vector k '()))
98 ;;; apply-to-args is functionally redunant with apply and, worse,
99 ;;; is less general than apply since it only takes two arguments.
101 ;;; On the other hand, apply-to-args is a syntacticly convenient way to
102 ;;; perform binding in many circumstances when the "let" family of
103 ;;; of forms don't cut it. E.g.:
105 ;;; (apply-to-args (return-3d-mouse-coords)
110 (define (apply-to-args args fn) (apply fn args))
116 (define (ipow-by-squaring x k acc proc)
117 (cond ((zero? k) acc)
118 ((= 1 k) (proc acc x))
119 (else (ipow-by-squaring (proc x x)
121 (if (even? k) acc (proc acc x))
124 (define string-character-length string-length)
128 ;; A convenience function for combining flag bits. Like logior, but
129 ;; handles the cases of 0 and 1 arguments.
131 (define (flags . args)
134 ((null? (cdr args)) (car args))
135 (else (apply logior args))))
138 ;;; {Symbol Properties}
141 (define (symbol-property sym prop)
142 (let ((pair (assoc prop (symbol-pref sym))))
143 (and pair (cdr pair))))
145 (define (set-symbol-property! sym prop val)
146 (let ((pair (assoc prop (symbol-pref sym))))
149 (symbol-pset! sym (acons prop val (symbol-pref sym))))))
151 (define (symbol-property-remove! sym prop)
152 (let ((pair (assoc prop (symbol-pref sym))))
154 (symbol-pset! sym (delq! pair (symbol-pref sym))))))
156 ;;; {General Properties}
158 ;; This is a more modern interface to properties. It will replace all
159 ;; other property-like things eventually.
161 (define (make-object-property)
162 (let ((prop (primitive-make-property #f)))
163 (make-procedure-with-setter
164 (lambda (obj) (primitive-property-ref prop obj))
165 (lambda (obj val) (primitive-property-set! prop obj val)))))
172 (if (provided? 'array)
173 (primitive-load-path "ice-9/arrays.scm"))
179 (define (symbol->keyword symbol)
180 (make-keyword-from-dash-symbol (symbol-append '- symbol)))
182 (define (keyword->symbol kw)
183 (let ((sym (symbol->string (keyword-dash-symbol kw))))
184 (string->symbol (substring sym 1 (string-length sym)))))
186 (define (kw-arg-ref args kw)
187 (let ((rem (member kw args)))
188 (and rem (pair? (cdr rem)) (cadr rem))))
194 (define (struct-layout s)
195 (struct-ref (struct-vtable s) vtable-index-layout))
201 (define the-environment
206 (define the-root-environment (the-environment))
208 (define (environment-module env)
209 (let ((closure (and (pair? env) (car (last-pair env)))))
210 (and closure (procedure-property closure 'module))))
216 ;; Printing records: by default, records are printed as
218 ;; #<type-name field1: val1 field2: val2 ...>
220 ;; You can change that by giving a custom printing function to
221 ;; MAKE-RECORD-TYPE (after the list of field symbols). This function
222 ;; will be called like
224 ;; (<printer> object port)
226 ;; It should print OBJECT to PORT.
228 (define (inherit-print-state old-port new-port)
229 (if (get-print-state old-port)
230 (port-with-print-state new-port (get-print-state old-port))
233 ;; 0: type-name, 1: fields
234 (define record-type-vtable
235 (make-vtable-vtable "prpr" 0
237 (cond ((eq? s record-type-vtable)
238 (display "#<record-type-vtable>" p))
240 (display "#<record-type " p)
241 (display (record-type-name s) p)
244 (define (record-type? obj)
245 (and (struct? obj) (eq? record-type-vtable (struct-vtable obj))))
247 (define (make-record-type type-name fields . opt)
248 (let ((printer-fn (and (pair? opt) (car opt))))
249 (let ((struct (make-struct record-type-vtable 0
252 (map (lambda (f) "pw") fields)))
256 (display type-name p)
257 (let loop ((fields fields)
260 ((not (null? fields))
262 (display (car fields) p)
264 (display (struct-ref s off) p)
265 (loop (cdr fields) (+ 1 off)))))
268 (copy-tree fields))))
269 ;; Temporary solution: Associate a name to the record type descriptor
270 ;; so that the object system can create a wrapper class for it.
271 (set-struct-vtable-name! struct (if (symbol? type-name)
273 (string->symbol type-name)))
276 (define (record-type-name obj)
277 (if (record-type? obj)
278 (struct-ref obj vtable-offset-user)
279 (error 'not-a-record-type obj)))
281 (define (record-type-fields obj)
282 (if (record-type? obj)
283 (struct-ref obj (+ 1 vtable-offset-user))
284 (error 'not-a-record-type obj)))
286 (define (record-constructor rtd . opt)
287 (let ((field-names (if (pair? opt) (car opt) (record-type-fields rtd))))
288 (local-eval `(lambda ,field-names
289 (make-struct ',rtd 0 ,@(map (lambda (f)
290 (if (memq f field-names)
293 (record-type-fields rtd))))
294 the-root-environment)))
296 (define (record-predicate rtd)
297 (lambda (obj) (and (struct? obj) (eq? rtd (struct-vtable obj)))))
299 (define (record-accessor rtd field-name)
300 (let* ((pos (list-index (record-type-fields rtd) field-name)))
302 (error 'no-such-field field-name))
303 (local-eval `(lambda (obj)
304 (and (eq? ',rtd (record-type-descriptor obj))
305 (struct-ref obj ,pos)))
306 the-root-environment)))
308 (define (record-modifier rtd field-name)
309 (let* ((pos (list-index (record-type-fields rtd) field-name)))
311 (error 'no-such-field field-name))
312 (local-eval `(lambda (obj val)
313 (and (eq? ',rtd (record-type-descriptor obj))
314 (struct-set! obj ,pos val)))
315 the-root-environment)))
318 (define (record? obj)
319 (and (struct? obj) (record-type? (struct-vtable obj))))
321 (define (record-type-descriptor obj)
324 (error 'not-a-record obj)))
332 (define (->bool x) (not (not x)))
338 (define (symbol-append . args)
339 (string->symbol (apply string-append (map symbol->string args))))
341 (define (list->symbol . args)
342 (string->symbol (apply list->string args)))
344 (define (symbol . args)
345 (string->symbol (apply string args)))
351 (define (list-index l k)
357 (loop (+ n 1) (cdr l))))))
359 (define (make-list n . init)
360 (if (pair? init) (set! init (car init)))
361 (let loop ((answer '())
365 (loop (cons init answer) (- n 1)))))
368 ;;; {and-map and or-map}
370 ;;; (and-map fn lst) is like (and (fn (car lst)) (fn (cadr lst)) (fn...) ...)
371 ;;; (or-map fn lst) is like (or (fn (car lst)) (fn (cadr lst)) (fn...) ...)
376 ;; Apply f to successive elements of l until exhaustion or f returns #f.
377 ;; If returning early, return #f. Otherwise, return the last value returned
378 ;; by f. If f has never been called because l is empty, return #t.
380 (define (and-map f lst)
381 (let loop ((result #t)
386 (loop (f (car l)) (cdr l))))))
390 ;; Apply f to successive elements of l until exhaustion or while f returns #f.
391 ;; If returning early, return the return value of f.
393 (define (or-map f lst)
394 (let loop ((result #f)
398 (loop (f (car l)) (cdr l))))))
402 (if (provided? 'posix)
403 (primitive-load-path "ice-9/posix.scm"))
405 (if (provided? 'socket)
406 (primitive-load-path "ice-9/networking.scm"))
409 (if (provided? 'posix)
413 (let ((port (catch 'system-error (lambda () (open-file str OPEN_READ))
415 (if port (begin (close-port port) #t)
418 (define file-is-directory?
419 (if (provided? 'posix)
421 (eq? (stat:type (stat str)) 'directory))
423 (let ((port (catch 'system-error
424 (lambda () (open-file (string-append str "/.")
427 (if port (begin (close-port port) #t)
430 (define (has-suffix? str suffix)
431 (let ((sufl (string-length suffix))
432 (sl (string-length str)))
434 (string=? (substring str (- sl sufl) sl) suffix))))
440 (define (error . args)
443 (scm-error 'misc-error #f "?" #f #f)
444 (let loop ((msg "~A")
446 (if (not (null? rest))
447 (loop (string-append msg " ~S")
449 (scm-error 'misc-error #f msg args #f)))))
451 ;; bad-throw is the hook that is called upon a throw to a an unhandled
452 ;; key (unless the throw has four arguments, in which case
453 ;; it's usually interpreted as an error throw.)
454 ;; If the key has a default handler (a throw-handler-default property),
455 ;; it is applied to the throw.
457 (define (bad-throw key . args)
458 (let ((default (symbol-property key 'throw-handler-default)))
459 (or (and default (apply default key args))
460 (apply error "unhandled-exception:" key args))))
464 (define (tm:sec obj) (vector-ref obj 0))
465 (define (tm:min obj) (vector-ref obj 1))
466 (define (tm:hour obj) (vector-ref obj 2))
467 (define (tm:mday obj) (vector-ref obj 3))
468 (define (tm:mon obj) (vector-ref obj 4))
469 (define (tm:year obj) (vector-ref obj 5))
470 (define (tm:wday obj) (vector-ref obj 6))
471 (define (tm:yday obj) (vector-ref obj 7))
472 (define (tm:isdst obj) (vector-ref obj 8))
473 (define (tm:gmtoff obj) (vector-ref obj 9))
474 (define (tm:zone obj) (vector-ref obj 10))
476 (define (set-tm:sec obj val) (vector-set! obj 0 val))
477 (define (set-tm:min obj val) (vector-set! obj 1 val))
478 (define (set-tm:hour obj val) (vector-set! obj 2 val))
479 (define (set-tm:mday obj val) (vector-set! obj 3 val))
480 (define (set-tm:mon obj val) (vector-set! obj 4 val))
481 (define (set-tm:year obj val) (vector-set! obj 5 val))
482 (define (set-tm:wday obj val) (vector-set! obj 6 val))
483 (define (set-tm:yday obj val) (vector-set! obj 7 val))
484 (define (set-tm:isdst obj val) (vector-set! obj 8 val))
485 (define (set-tm:gmtoff obj val) (vector-set! obj 9 val))
486 (define (set-tm:zone obj val) (vector-set! obj 10 val))
488 (define (tms:clock obj) (vector-ref obj 0))
489 (define (tms:utime obj) (vector-ref obj 1))
490 (define (tms:stime obj) (vector-ref obj 2))
491 (define (tms:cutime obj) (vector-ref obj 3))
492 (define (tms:cstime obj) (vector-ref obj 4))
494 (define (file-position . args) (apply ftell args))
495 (define (file-set-position . args) (apply fseek args))
497 (define (move->fdes fd/port fd)
498 (cond ((integer? fd/port)
499 (dup->fdes fd/port fd)
503 (primitive-move->fdes fd/port fd)
504 (set-port-revealed! fd/port 1)
507 (define (release-port-handle port)
508 (let ((revealed (port-revealed port)))
510 (set-port-revealed! port (- revealed 1)))))
512 (define (dup->port port/fd mode . maybe-fd)
513 (let ((port (fdopen (apply dup->fdes port/fd maybe-fd)
516 (set-port-revealed! port 1))
519 (define (dup->inport port/fd . maybe-fd)
520 (apply dup->port port/fd "r" maybe-fd))
522 (define (dup->outport port/fd . maybe-fd)
523 (apply dup->port port/fd "w" maybe-fd))
525 (define (dup port/fd . maybe-fd)
526 (if (integer? port/fd)
527 (apply dup->fdes port/fd maybe-fd)
528 (apply dup->port port/fd (port-mode port/fd) maybe-fd)))
530 (define (duplicate-port port modes)
531 (dup->port port modes))
533 (define (fdes->inport fdes)
534 (let loop ((rest-ports (fdes->ports fdes)))
535 (cond ((null? rest-ports)
536 (let ((result (fdopen fdes "r")))
537 (set-port-revealed! result 1)
539 ((input-port? (car rest-ports))
540 (set-port-revealed! (car rest-ports)
541 (+ (port-revealed (car rest-ports)) 1))
544 (loop (cdr rest-ports))))))
546 (define (fdes->outport fdes)
547 (let loop ((rest-ports (fdes->ports fdes)))
548 (cond ((null? rest-ports)
549 (let ((result (fdopen fdes "w")))
550 (set-port-revealed! result 1)
552 ((output-port? (car rest-ports))
553 (set-port-revealed! (car rest-ports)
554 (+ (port-revealed (car rest-ports)) 1))
557 (loop (cdr rest-ports))))))
559 (define (port->fdes port)
560 (set-port-revealed! port (+ (port-revealed port) 1))
563 (define (setenv name value)
565 (putenv (string-append name "=" value))
572 ;;; Here for backward compatability
574 (define scheme-file-suffix (lambda () ".scm"))
576 (define (in-vicinity vicinity file)
577 (let ((tail (let ((len (string-length vicinity)))
580 (string-ref vicinity (- len 1))))))
581 (string-append vicinity
589 ;;; {Help for scm_shell}
590 ;;; The argument-processing code used by Guile-based shells generates
591 ;;; Scheme code based on the argument list. This page contains help
592 ;;; functions for the code it generates.
594 (define (command-line) (program-arguments))
596 ;; This is mostly for the internal use of the code generated by
597 ;; scm_compile_shell_switches.
598 (define (load-user-init)
599 (let* ((home (or (getenv "HOME")
600 (false-if-exception (passwd:dir (getpwuid (getuid))))
601 "/")) ;; fallback for cygwin etc.
602 (init-file (in-vicinity home ".guile")))
603 (if (file-exists? init-file)
604 (primitive-load init-file))))
607 ;;; {Loading by paths}
609 ;;; Load a Scheme source file named NAME, searching for it in the
610 ;;; directories listed in %load-path, and applying each of the file
611 ;;; name extensions listed in %load-extensions.
612 (define (load-from-path name)
613 (start-stack 'load-stack
614 (primitive-load-path name)))
618 ;;; {Transcendental Functions}
620 ;;; Derived from "Transcen.scm", Complex trancendental functions for SCM.
621 ;;; Written by Jerry D. Hedden, (C) FSF.
622 ;;; See the file `COPYING' for terms applying to this program.
626 (if (real? z) ($exp z)
627 (make-polar ($exp (real-part z)) (imag-part z))))
630 (if (and (real? z) (>= z 0))
632 (make-rectangular ($log (magnitude z)) (angle z))))
636 (if (negative? z) (make-rectangular 0 ($sqrt (- z)))
638 (make-polar ($sqrt (magnitude z)) (/ (angle z) 2))))
641 (let ((integer-expt integer-expt))
646 (/ 1 (integer-expt z1 (- z2)))))
647 ((and (real? z2) (real? z1) (>= z1 0))
650 (exp (* z2 (log z1))))))))
653 (if (real? z) ($sinh z)
654 (let ((x (real-part z)) (y (imag-part z)))
655 (make-rectangular (* ($sinh x) ($cos y))
656 (* ($cosh x) ($sin y))))))
658 (if (real? z) ($cosh z)
659 (let ((x (real-part z)) (y (imag-part z)))
660 (make-rectangular (* ($cosh x) ($cos y))
661 (* ($sinh x) ($sin y))))))
663 (if (real? z) ($tanh z)
664 (let* ((x (* 2 (real-part z)))
665 (y (* 2 (imag-part z)))
666 (w (+ ($cosh x) ($cos y))))
667 (make-rectangular (/ ($sinh x) w) (/ ($sin y) w)))))
670 (if (real? z) ($asinh z)
671 (log (+ z (sqrt (+ (* z z) 1))))))
674 (if (and (real? z) (>= z 1))
676 (log (+ z (sqrt (- (* z z) 1))))))
679 (if (and (real? z) (> z -1) (< z 1))
681 (/ (log (/ (+ 1 z) (- 1 z))) 2)))
684 (if (real? z) ($sin z)
685 (let ((x (real-part z)) (y (imag-part z)))
686 (make-rectangular (* ($sin x) ($cosh y))
687 (* ($cos x) ($sinh y))))))
689 (if (real? z) ($cos z)
690 (let ((x (real-part z)) (y (imag-part z)))
691 (make-rectangular (* ($cos x) ($cosh y))
692 (- (* ($sin x) ($sinh y)))))))
694 (if (real? z) ($tan z)
695 (let* ((x (* 2 (real-part z)))
696 (y (* 2 (imag-part z)))
697 (w (+ ($cos x) ($cosh y))))
698 (make-rectangular (/ ($sin x) w) (/ ($sinh y) w)))))
701 (if (and (real? z) (>= z -1) (<= z 1))
703 (* -i (asinh (* +i z)))))
706 (if (and (real? z) (>= z -1) (<= z 1))
708 (+ (/ (angle -1) 2) (* +i (asinh (* +i z))))))
712 (if (real? z) ($atan z)
713 (/ (log (/ (- +i z) (+ +i z))) +2i))
717 (/ (log arg) (log 10)))
721 ;;; {Reader Extensions}
724 ;;; Reader code for various "#c" forms.
727 (read-hash-extend #\' (lambda (c port)
729 (read-hash-extend #\. (lambda (c port)
730 (eval (read port) (interaction-environment))))
733 ;;; {Command Line Options}
736 (define (get-option argv kw-opts kw-args return)
741 ((or (not (eq? #\- (string-ref (car argv) 0)))
742 (eq? (string-length (car argv)) 1))
743 (return 'normal-arg (car argv) (cdr argv)))
745 ((eq? #\- (string-ref (car argv) 1))
746 (let* ((kw-arg-pos (or (string-index (car argv) #\=)
747 (string-length (car argv))))
748 (kw (symbol->keyword (substring (car argv) 2 kw-arg-pos)))
749 (kw-opt? (member kw kw-opts))
750 (kw-arg? (member kw kw-args))
751 (arg (or (and (not (eq? kw-arg-pos (string-length (car argv))))
752 (substring (car argv)
754 (string-length (car argv))))
756 (begin (set! argv (cdr argv)) (car argv))))))
757 (if (or kw-opt? kw-arg?)
758 (return kw arg (cdr argv))
759 (return 'usage-error kw (cdr argv)))))
762 (let* ((char (substring (car argv) 1 2))
763 (kw (symbol->keyword char)))
767 (let* ((rest-car (substring (car argv) 2 (string-length (car argv))))
768 (new-argv (if (= 0 (string-length rest-car))
770 (cons (string-append "-" rest-car) (cdr argv)))))
771 (return kw #f new-argv)))
774 (let* ((rest-car (substring (car argv) 2 (string-length (car argv))))
775 (arg (if (= 0 (string-length rest-car))
778 (new-argv (if (= 0 (string-length rest-car))
781 (return kw arg new-argv)))
783 (else (return 'usage-error kw argv)))))))
785 (define (for-next-option proc argv kw-opts kw-args)
786 (let loop ((argv argv))
787 (get-option argv kw-opts kw-args
788 (lambda (opt opt-arg argv)
789 (and opt (proc opt opt-arg argv loop))))))
791 (define (display-usage-report kw-desc)
794 (or (eq? (car kw) #t)
797 (help (cadr opt-desc))
798 (opts (car opt-desc))
799 (opts-proper (if (string? (car opts)) (cdr opts) opts))
800 (arg-name (if (string? (car opts))
801 (string-append "<" (car opts) ">")
803 (left-part (string-append
804 (with-output-to-string
806 (map (lambda (x) (display (keyword-symbol x)) (display " "))
809 (middle-part (if (and (< (string-length left-part) 30)
810 (< (string-length help) 40))
811 (make-string (- 30 (string-length left-part)) #\ )
814 (display middle-part)
821 (define (transform-usage-lambda cases)
822 (let* ((raw-usage (delq! 'else (map car cases)))
823 (usage-sans-specials (map (lambda (x)
824 (or (and (not (list? x)) x)
825 (and (symbol? (car x)) #t)
826 (and (boolean? (car x)) #t)
829 (usage-desc (delq! #t usage-sans-specials))
830 (kw-desc (map car usage-desc))
831 (kw-opts (apply append (map (lambda (x) (and (not (string? (car x))) x)) kw-desc)))
832 (kw-args (apply append (map (lambda (x) (and (string? (car x)) (cdr x))) kw-desc)))
833 (transmogrified-cases (map (lambda (case)
834 (cons (let ((opts (car case)))
835 (if (or (boolean? opts) (eq? 'else opts))
838 ((symbol? (car opts)) opts)
839 ((boolean? (car opts)) opts)
840 ((string? (caar opts)) (cdar opts))
844 `(let ((%display-usage (lambda () (display-usage-report ',usage-desc))))
846 (let %next-arg ((%argv %argv))
850 (lambda (%opt %arg %new-argv)
852 ,@ transmogrified-cases))))))))
857 ;;; {Low Level Modules}
859 ;;; These are the low level data structures for modules.
861 ;;; !!! warning: The interface to lazy binder procedures is going
862 ;;; to be changed in an incompatible way to permit all the basic
863 ;;; module ops to be virtualized.
865 ;;; (make-module size use-list lazy-binding-proc) => module
866 ;;; module-{obarray,uses,binder}[|-set!]
867 ;;; (module? obj) => [#t|#f]
868 ;;; (module-locally-bound? module symbol) => [#t|#f]
869 ;;; (module-bound? module symbol) => [#t|#f]
870 ;;; (module-symbol-locally-interned? module symbol) => [#t|#f]
871 ;;; (module-symbol-interned? module symbol) => [#t|#f]
872 ;;; (module-local-variable module symbol) => [#<variable ...> | #f]
873 ;;; (module-variable module symbol) => [#<variable ...> | #f]
874 ;;; (module-symbol-binding module symbol opt-value)
875 ;;; => [ <obj> | opt-value | an error occurs ]
876 ;;; (module-make-local-var! module symbol) => #<variable...>
877 ;;; (module-add! module symbol var) => unspecified
878 ;;; (module-remove! module symbol) => unspecified
879 ;;; (module-for-each proc module) => unspecified
880 ;;; (make-scm-module) => module ; a lazy copy of the symhash module
881 ;;; (set-current-module module) => unspecified
882 ;;; (current-module) => #<module...>
887 ;;; {Printing Modules}
888 ;; This is how modules are printed. You can re-define it.
889 ;; (Redefining is actually more complicated than simply redefining
890 ;; %print-module because that would only change the binding and not
891 ;; the value stored in the vtable that determines how record are
894 (define (%print-module mod port) ; unused args: depth length style table)
896 (display (or (module-kind mod) "module") port)
897 (let ((name (module-name mod)))
901 (display name port))))
903 (display (number->string (object-address mod) 16) port)
908 ;; A module is characterized by an obarray in which local symbols
909 ;; are interned, a list of modules, "uses", from which non-local
910 ;; bindings can be inherited, and an optional lazy-binder which
911 ;; is a (CLOSURE module symbol) which, as a last resort, can provide
912 ;; bindings that would otherwise not be found locally in the module.
914 ;; NOTE: If you change here, you also need to change libguile/modules.h.
917 (make-record-type 'module
918 '(obarray uses binder eval-closure transformer name kind
919 observers weak-observers observer-id)
922 ;; make-module &opt size uses binder
924 ;; Create a new module, perhaps with a particular size of obarray,
925 ;; initial uses list, or binding procedure.
930 (define (parse-arg index default)
931 (if (> (length args) index)
932 (list-ref args index)
935 (if (> (length args) 3)
936 (error "Too many args to make-module." args))
938 (let ((size (parse-arg 0 1021))
939 (uses (parse-arg 1 '()))
940 (binder (parse-arg 2 #f)))
942 (if (not (integer? size))
943 (error "Illegal size to make-module." size))
944 (if (not (and (list? uses)
945 (and-map module? uses)))
946 (error "Incorrect use list." uses))
947 (if (and binder (not (procedure? binder)))
949 "Lazy-binder expected to be a procedure or #f." binder))
951 (let ((module (module-constructor (make-vector size '())
952 uses binder #f #f #f #f
954 (make-weak-value-hash-table 31)
957 ;; We can't pass this as an argument to module-constructor,
958 ;; because we need it to close over a pointer to the module
960 (set-module-eval-closure! module (standard-eval-closure module))
964 (define module-constructor (record-constructor module-type))
965 (define module-obarray (record-accessor module-type 'obarray))
966 (define set-module-obarray! (record-modifier module-type 'obarray))
967 (define module-uses (record-accessor module-type 'uses))
968 (define set-module-uses! (record-modifier module-type 'uses))
969 (define module-binder (record-accessor module-type 'binder))
970 (define set-module-binder! (record-modifier module-type 'binder))
972 ;; NOTE: This binding is used in libguile/modules.c.
973 (define module-eval-closure (record-accessor module-type 'eval-closure))
975 (define module-transformer (record-accessor module-type 'transformer))
976 (define set-module-transformer! (record-modifier module-type 'transformer))
977 (define module-name (record-accessor module-type 'name))
978 (define set-module-name! (record-modifier module-type 'name))
979 (define module-kind (record-accessor module-type 'kind))
980 (define set-module-kind! (record-modifier module-type 'kind))
981 (define module-observers (record-accessor module-type 'observers))
982 (define set-module-observers! (record-modifier module-type 'observers))
983 (define module-weak-observers (record-accessor module-type 'weak-observers))
984 (define module-observer-id (record-accessor module-type 'observer-id))
985 (define set-module-observer-id! (record-modifier module-type 'observer-id))
986 (define module? (record-predicate module-type))
988 (define set-module-eval-closure!
989 (let ((setter (record-modifier module-type 'eval-closure)))
990 (lambda (module closure)
991 (setter module closure)
992 ;; Make it possible to lookup the module from the environment.
993 ;; This implementation is correct since an eval closure can belong
994 ;; to maximally one module.
995 (set-procedure-property! closure 'module module))))
997 ;;; This procedure is depreated
999 (define eval-in-module eval)
1002 ;;; {Observer protocol}
1005 (define (module-observe module proc)
1006 (set-module-observers! module (cons proc (module-observers module)))
1009 (define (module-observe-weak module proc)
1010 (let ((id (module-observer-id module)))
1011 (hash-set! (module-weak-observers module) id proc)
1012 (set-module-observer-id! module (+ 1 id))
1015 (define (module-unobserve token)
1016 (let ((module (car token))
1019 (hash-remove! (module-weak-observers module) id)
1020 (set-module-observers! module (delq1! id (module-observers module)))))
1023 (define (module-modified m)
1024 (for-each (lambda (proc) (proc m)) (module-observers m))
1025 (hash-fold (lambda (id proc res) (proc m)) #f (module-weak-observers m)))
1028 ;;; {Module Searching in General}
1030 ;;; We sometimes want to look for properties of a symbol
1031 ;;; just within the obarray of one module. If the property
1032 ;;; holds, then it is said to hold ``locally'' as in, ``The symbol
1033 ;;; DISPLAY is locally rebound in the module `safe-guile'.''
1036 ;;; Other times, we want to test for a symbol property in the obarray
1037 ;;; of M and, if it is not found there, try each of the modules in the
1038 ;;; uses list of M. This is the normal way of testing for some
1039 ;;; property, so we state these properties without qualification as
1040 ;;; in: ``The symbol 'fnord is interned in module M because it is
1041 ;;; interned locally in module M2 which is a member of the uses list
1045 ;; module-search fn m
1047 ;; return the first non-#f result of FN applied to M and then to
1048 ;; the modules in the uses of m, and so on recursively. If all applications
1049 ;; return #f, then so does this function.
1051 (define (module-search fn m v)
1054 (or (module-search fn (car pos) v)
1057 (loop (module-uses m))))
1060 ;;; {Is a symbol bound in a module?}
1062 ;;; Symbol S in Module M is bound if S is interned in M and if the binding
1063 ;;; of S in M has been set to some well-defined value.
1066 ;; module-locally-bound? module symbol
1068 ;; Is a symbol bound (interned and defined) locally in a given module?
1070 (define (module-locally-bound? m v)
1071 (let ((var (module-local-variable m v)))
1073 (variable-bound? var))))
1075 ;; module-bound? module symbol
1077 ;; Is a symbol bound (interned and defined) anywhere in a given module
1080 (define (module-bound? m v)
1081 (module-search module-locally-bound? m v))
1083 ;;; {Is a symbol interned in a module?}
1085 ;;; Symbol S in Module M is interned if S occurs in
1086 ;;; of S in M has been set to some well-defined value.
1088 ;;; It is possible to intern a symbol in a module without providing
1089 ;;; an initial binding for the corresponding variable. This is done
1091 ;;; (module-add! module symbol (make-undefined-variable))
1093 ;;; In that case, the symbol is interned in the module, but not
1094 ;;; bound there. The unbound symbol shadows any binding for that
1095 ;;; symbol that might otherwise be inherited from a member of the uses list.
1098 (define (module-obarray-get-handle ob key)
1099 ((if (symbol? key) hashq-get-handle hash-get-handle) ob key))
1101 (define (module-obarray-ref ob key)
1102 ((if (symbol? key) hashq-ref hash-ref) ob key))
1104 (define (module-obarray-set! ob key val)
1105 ((if (symbol? key) hashq-set! hash-set!) ob key val))
1107 (define (module-obarray-remove! ob key)
1108 ((if (symbol? key) hashq-remove! hash-remove!) ob key))
1110 ;; module-symbol-locally-interned? module symbol
1112 ;; is a symbol interned (not neccessarily defined) locally in a given module
1113 ;; or its uses? Interned symbols shadow inherited bindings even if
1114 ;; they are not themselves bound to a defined value.
1116 (define (module-symbol-locally-interned? m v)
1117 (not (not (module-obarray-get-handle (module-obarray m) v))))
1119 ;; module-symbol-interned? module symbol
1121 ;; is a symbol interned (not neccessarily defined) anywhere in a given module
1122 ;; or its uses? Interned symbols shadow inherited bindings even if
1123 ;; they are not themselves bound to a defined value.
1125 (define (module-symbol-interned? m v)
1126 (module-search module-symbol-locally-interned? m v))
1129 ;;; {Mapping modules x symbols --> variables}
1132 ;; module-local-variable module symbol
1133 ;; return the local variable associated with a MODULE and SYMBOL.
1135 ;;; This function is very important. It is the only function that can
1136 ;;; return a variable from a module other than the mutators that store
1137 ;;; new variables in modules. Therefore, this function is the location
1138 ;;; of the "lazy binder" hack.
1140 ;;; If symbol is defined in MODULE, and if the definition binds symbol
1141 ;;; to a variable, return that variable object.
1143 ;;; If the symbols is not found at first, but the module has a lazy binder,
1144 ;;; then try the binder.
1146 ;;; If the symbol is not found at all, return #f.
1148 (define (module-local-variable m v)
1151 (let ((b (module-obarray-ref (module-obarray m) v)))
1152 (or (and (variable? b) b)
1153 (and (module-binder m)
1154 ((module-binder m) m v #f)))))
1157 ;; module-variable module symbol
1159 ;; like module-local-variable, except search the uses in the
1160 ;; case V is not found in M.
1162 ;; NOTE: This function is superseded with C code (see modules.c)
1163 ;;; when using the standard eval closure.
1165 (define (module-variable m v)
1166 (module-search module-local-variable m v))
1169 ;;; {Mapping modules x symbols --> bindings}
1171 ;;; These are similar to the mapping to variables, except that the
1172 ;;; variable is dereferenced.
1175 ;; module-symbol-binding module symbol opt-value
1177 ;; return the binding of a variable specified by name within
1178 ;; a given module, signalling an error if the variable is unbound.
1179 ;; If the OPT-VALUE is passed, then instead of signalling an error,
1180 ;; return OPT-VALUE.
1182 (define (module-symbol-local-binding m v . opt-val)
1183 (let ((var (module-local-variable m v)))
1186 (if (not (null? opt-val))
1188 (error "Locally unbound variable." v)))))
1190 ;; module-symbol-binding module symbol opt-value
1192 ;; return the binding of a variable specified by name within
1193 ;; a given module, signalling an error if the variable is unbound.
1194 ;; If the OPT-VALUE is passed, then instead of signalling an error,
1195 ;; return OPT-VALUE.
1197 (define (module-symbol-binding m v . opt-val)
1198 (let ((var (module-variable m v)))
1201 (if (not (null? opt-val))
1203 (error "Unbound variable." v)))))
1207 ;;; {Adding Variables to Modules}
1212 ;; module-make-local-var! module symbol
1214 ;; ensure a variable for V in the local namespace of M.
1215 ;; If no variable was already there, then create a new and uninitialzied
1218 (define (module-make-local-var! m v)
1219 (or (let ((b (module-obarray-ref (module-obarray m) v)))
1224 (and (module-binder m)
1225 ((module-binder m) m v #t))
1227 (let ((answer (make-undefined-variable v)))
1228 (module-obarray-set! (module-obarray m) v answer)
1232 ;; module-add! module symbol var
1234 ;; ensure a particular variable for V in the local namespace of M.
1236 (define (module-add! m v var)
1237 (if (not (variable? var))
1238 (error "Bad variable to module-add!" var))
1239 (module-obarray-set! (module-obarray m) v var)
1240 (module-modified m))
1244 ;; make sure that a symbol is undefined in the local namespace of M.
1246 (define (module-remove! m v)
1247 (module-obarray-remove! (module-obarray m) v)
1248 (module-modified m))
1250 (define (module-clear! m)
1251 (vector-fill! (module-obarray m) '())
1252 (module-modified m))
1254 ;; MODULE-FOR-EACH -- exported
1256 ;; Call PROC on each symbol in MODULE, with arguments of (SYMBOL VARIABLE).
1258 (define (module-for-each proc module)
1259 (let ((obarray (module-obarray module)))
1260 (do ((index 0 (+ index 1))
1261 (end (vector-length obarray)))
1265 (proc (car bucket) (cdr bucket)))
1266 (vector-ref obarray index)))))
1269 (define (module-map proc module)
1270 (let* ((obarray (module-obarray module))
1271 (end (vector-length obarray)))
1279 (map (lambda (bucket)
1280 (proc (car bucket) (cdr bucket)))
1281 (vector-ref obarray i))
1285 ;;; {Low Level Bootstrapping}
1290 ;; A root module uses the symhash table (the system's privileged
1291 ;; obarray). Being inside a root module is like using SCM without
1292 ;; any module system.
1296 (define (root-module-closure m s define?)
1297 (let ((bi (builtin-variable s)))
1299 (or define? (variable-bound? bi))
1301 (module-add! m s bi)
1304 (define (make-root-module)
1305 (make-module 1019 '() root-module-closure))
1310 ;; An scm module is a module into which the lazy binder copies
1311 ;; variable bindings from the system symhash table. The mapping is
1312 ;; one way only; newly introduced bindings in an scm module are not
1313 ;; copied back into the system symhash table (and can be used to override
1314 ;; bindings from the symhash table).
1317 (define (scm-module-closure m s define?)
1318 (let ((bi (builtin-variable s)))
1320 (variable-bound? bi)
1322 (module-add! m s bi)
1325 (define (make-scm-module)
1326 (make-module 1019 '() scm-module-closure))
1332 ;; NOTE: This binding is used in libguile/modules.c.
1334 (define the-module (make-fluid))
1336 ;; scm:eval-transformer
1338 ;;(define scm:eval-transformer (make-fluid)) ; initialized in eval.c.
1340 ;; set-current-module module
1342 ;; set the current module as viewed by the normalizer.
1344 ;; NOTE: This binding is used in libguile/modules.c.
1346 (define (set-current-module m)
1347 (fluid-set! the-module m)
1350 ;; *top-level-lookup-closure* is now deprecated
1351 (fluid-set! *top-level-lookup-closure*
1352 (module-eval-closure (fluid-ref the-module)))
1353 (fluid-set! scm:eval-transformer (module-transformer (fluid-ref the-module))))
1354 (fluid-set! *top-level-lookup-closure* #f)))
1359 ;; return the current module as viewed by the normalizer.
1361 (define (current-module) (fluid-ref the-module))
1363 ;;; {Module-based Loading}
1366 (define (save-module-excursion thunk)
1367 (let ((inner-module (current-module))
1369 (dynamic-wind (lambda ()
1370 (set! outer-module (current-module))
1371 (set-current-module inner-module)
1372 (set! inner-module #f))
1375 (set! inner-module (current-module))
1376 (set-current-module outer-module)
1377 (set! outer-module #f)))))
1379 (define basic-load load)
1381 (define (load-module filename)
1382 (save-module-excursion
1384 (let ((oldname (and (current-load-port)
1385 (port-filename (current-load-port)))))
1386 (basic-load (if (and oldname
1387 (> (string-length filename) 0)
1388 (not (char=? (string-ref filename 0) #\/))
1389 (not (string=? (dirname oldname) ".")))
1390 (string-append (dirname oldname) "/" filename)
1395 ;;; {MODULE-REF -- exported}
1397 ;; Returns the value of a variable called NAME in MODULE or any of its
1398 ;; used modules. If there is no such variable, then if the optional third
1399 ;; argument DEFAULT is present, it is returned; otherwise an error is signaled.
1401 (define (module-ref module name . rest)
1402 (let ((variable (module-variable module name)))
1403 (if (and variable (variable-bound? variable))
1404 (variable-ref variable)
1406 (error "No variable named" name 'in module)
1407 (car rest) ; default value
1410 ;; MODULE-SET! -- exported
1412 ;; Sets the variable called NAME in MODULE (or in a module that MODULE uses)
1413 ;; to VALUE; if there is no such variable, an error is signaled.
1415 (define (module-set! module name value)
1416 (let ((variable (module-variable module name)))
1418 (variable-set! variable value)
1419 (error "No variable named" name 'in module))))
1421 ;; MODULE-DEFINE! -- exported
1423 ;; Sets the variable called NAME in MODULE to VALUE; if there is no such
1424 ;; variable, it is added first.
1426 (define (module-define! module name value)
1427 (let ((variable (module-local-variable module name)))
1430 (variable-set! variable value)
1431 (module-modified module))
1432 (module-add! module name (make-variable value name)))))
1434 ;; MODULE-DEFINED? -- exported
1436 ;; Return #t iff NAME is defined in MODULE (or in a module that MODULE
1439 (define (module-defined? module name)
1440 (let ((variable (module-variable module name)))
1441 (and variable (variable-bound? variable))))
1443 ;; MODULE-USE! module interface
1445 ;; Add INTERFACE to the list of interfaces used by MODULE.
1447 (define (module-use! module interface)
1448 (set-module-uses! module
1449 (cons interface (delq! interface (module-uses module))))
1450 (module-modified module))
1453 ;;; {Recursive Namespaces}
1456 ;;; A hierarchical namespace emerges if we consider some module to be
1457 ;;; root, and variables bound to modules as nested namespaces.
1459 ;;; The routines in this file manage variable names in hierarchical namespace.
1460 ;;; Each variable name is a list of elements, looked up in successively nested
1463 ;;; (nested-ref some-root-module '(foo bar baz))
1464 ;;; => <value of a variable named baz in the module bound to bar in
1465 ;;; the module bound to foo in some-root-module>
1470 ;;; ;; a-root is a module
1471 ;;; ;; name is a list of symbols
1473 ;;; nested-ref a-root name
1474 ;;; nested-set! a-root name val
1475 ;;; nested-define! a-root name val
1476 ;;; nested-remove! a-root name
1479 ;;; (current-module) is a natural choice for a-root so for convenience there are
1482 ;;; local-ref name == nested-ref (current-module) name
1483 ;;; local-set! name val == nested-set! (current-module) name val
1484 ;;; local-define! name val == nested-define! (current-module) name val
1485 ;;; local-remove! name == nested-remove! (current-module) name
1489 (define (nested-ref root names)
1490 (let loop ((cur root)
1494 ((not (module? cur)) #f)
1495 (else (loop (module-ref cur (car elts) #f) (cdr elts))))))
1497 (define (nested-set! root names val)
1498 (let loop ((cur root)
1500 (if (null? (cdr elts))
1501 (module-set! cur (car elts) val)
1502 (loop (module-ref cur (car elts)) (cdr elts)))))
1504 (define (nested-define! root names val)
1505 (let loop ((cur root)
1507 (if (null? (cdr elts))
1508 (module-define! cur (car elts) val)
1509 (loop (module-ref cur (car elts)) (cdr elts)))))
1511 (define (nested-remove! root names)
1512 (let loop ((cur root)
1514 (if (null? (cdr elts))
1515 (module-remove! cur (car elts))
1516 (loop (module-ref cur (car elts)) (cdr elts)))))
1518 (define (local-ref names) (nested-ref (current-module) names))
1519 (define (local-set! names val) (nested-set! (current-module) names val))
1520 (define (local-define names val) (nested-define! (current-module) names val))
1521 (define (local-remove names) (nested-remove! (current-module) names))
1525 ;;; {The (app) module}
1527 ;;; The root of conventionally named objects not directly in the top level.
1530 ;;; (app modules guile)
1532 ;;; The directory of all modules and the standard root module.
1535 (define (module-public-interface m)
1536 (module-ref m '%module-public-interface #f))
1537 (define (set-module-public-interface! m i)
1538 (module-define! m '%module-public-interface i))
1539 (define (set-system-module! m s)
1540 (set-procedure-property! (module-eval-closure m) 'system-module s))
1541 (define the-root-module (make-root-module))
1542 (define the-scm-module (make-scm-module))
1543 (set-module-public-interface! the-root-module the-scm-module)
1544 (set-module-name! the-root-module '(guile))
1545 (set-module-name! the-scm-module '(guile))
1546 (set-module-kind! the-scm-module 'interface)
1547 (for-each set-system-module! (list the-root-module the-scm-module) '(#t #t))
1549 (set-current-module the-root-module)
1551 (define app (make-module 31))
1552 (local-define '(app modules) (make-module 31))
1553 (local-define '(app modules guile) the-root-module)
1555 ;; (define-special-value '(app modules new-ws) (lambda () (make-scm-module)))
1557 (define (try-load-module name)
1558 (or (try-module-linked name)
1559 (try-module-autoload name)
1560 (try-module-dynamic-link name)))
1562 ;; NOTE: This binding is used in libguile/modules.c.
1564 (define (resolve-module name . maybe-autoload)
1565 (let ((full-name (append '(app modules) name)))
1566 (let ((already (local-ref full-name)))
1568 ;; The module already exists...
1569 (if (and (or (null? maybe-autoload) (car maybe-autoload))
1570 (not (module-ref already '%module-public-interface #f)))
1571 ;; ...but we are told to load and it doesn't contain source, so
1573 (try-load-module name)
1575 ;; simply return it.
1578 ;; Try to autoload it if we are told so
1579 (if (or (null? maybe-autoload) (car maybe-autoload))
1580 (try-load-module name))
1582 (make-modules-in (current-module) full-name))))))
1584 (define (beautify-user-module! module)
1585 (let ((interface (module-public-interface module)))
1586 (if (or (not interface)
1587 (eq? interface module))
1588 (let ((interface (make-module 31)))
1589 (set-module-name! interface (module-name module))
1590 (set-module-kind! interface 'interface)
1591 (set-module-public-interface! module interface))))
1592 (if (and (not (memq the-scm-module (module-uses module)))
1593 (not (eq? module the-root-module)))
1594 (set-module-uses! module (append (module-uses module) (list the-scm-module)))))
1596 (define (purify-module! module)
1597 "Removes bindings in MODULE which are inherited from the (guile) module."
1598 (let ((use-list (module-uses module)))
1599 (if (and (pair? use-list)
1600 (eq? (car (last-pair use-list)) the-scm-module))
1601 (set-module-uses! module (reverse (cdr (reverse use-list)))))))
1603 ;; NOTE: This binding is used in libguile/modules.c.
1605 (define (make-modules-in module name)
1609 ((module-ref module (car name) #f)
1610 => (lambda (m) (make-modules-in m (cdr name))))
1611 (else (let ((m (make-module 31)))
1612 (set-module-kind! m 'directory)
1613 (set-module-name! m (append (or (module-name module)
1616 (module-define! module (car name) m)
1617 (make-modules-in m (cdr name)))))))
1619 (define (resolve-interface name)
1620 (let ((module (resolve-module name)))
1621 (and module (module-public-interface module))))
1624 (define %autoloader-developer-mode #t)
1626 (define (process-define-module args)
1627 (let* ((module-id (car args))
1628 (module (resolve-module module-id #f))
1630 (beautify-user-module! module)
1631 (let loop ((kws kws)
1632 (reversed-interfaces '())
1636 (for-each (lambda (interface)
1637 (module-use! module interface))
1638 reversed-interfaces)
1639 (module-export! module exports))
1640 (let ((keyword (if (keyword? (car kws))
1641 (keyword->symbol (car kws))
1642 (and (symbol? (car kws))
1643 (let ((s (symbol->string (car kws))))
1644 (and (eq? (string-ref s 0) #\:)
1645 (string->symbol (substring s 1))))))))
1647 ((use-module use-syntax)
1648 (if (not (pair? (cdr kws)))
1649 (error "unrecognized defmodule argument" kws))
1650 (let* ((used-name (cadr kws))
1651 (used-module (resolve-module used-name)))
1652 (if (not (module-ref used-module
1653 '%module-public-interface
1656 ((if %autoloader-developer-mode warn error)
1657 "no code for module" (module-name used-module))
1658 (beautify-user-module! used-module)))
1659 (let ((interface (module-public-interface used-module)))
1661 (error "missing interface for use-module"
1663 (if (eq? keyword 'use-syntax)
1664 (set-module-transformer!
1666 (module-ref interface (car (last-pair used-name))
1669 (cons interface reversed-interfaces)
1672 (if (not (and (pair? (cdr kws)) (pair? (cddr kws))))
1673 (error "unrecognized defmodule argument" kws))
1675 (cons (make-autoload-interface module
1678 reversed-interfaces)
1681 (set-system-module! module #t)
1682 (loop (cdr kws) reversed-interfaces exports))
1684 (purify-module! module)
1685 (loop (cdr kws) reversed-interfaces exports))
1687 (if (not (pair? (cdr kws)))
1688 (error "unrecognized defmodule argument" kws))
1691 (append (cadr kws) exports)))
1693 (error "unrecognized defmodule argument" kws))))))
1694 (set-current-module module)
1699 (define (make-autoload-interface module name bindings)
1700 (let ((b (lambda (a sym definep)
1701 (and (memq sym bindings)
1702 (let ((i (module-public-interface (resolve-module name))))
1704 (error "missing interface for module" name))
1705 ;; Replace autoload-interface with interface
1706 (set-car! (memq a (module-uses module)) i)
1707 (module-local-variable i sym))))))
1708 (module-constructor #() '() b #f #f name 'autoload
1709 '() (make-weak-value-hash-table 31) 0)))
1712 ;;; {Autoloading modules}
1714 (define autoloads-in-progress '())
1716 (define (try-module-autoload module-name)
1717 (let* ((reverse-name (reverse module-name))
1718 (name (symbol->string (car reverse-name)))
1719 (dir-hint-module-name (reverse (cdr reverse-name)))
1720 (dir-hint (apply string-append
1722 (string-append (symbol->string elt) "/"))
1723 dir-hint-module-name))))
1724 (resolve-module dir-hint-module-name #f)
1725 (and (not (autoload-done-or-in-progress? dir-hint name))
1728 (lambda () (autoload-in-progress! dir-hint name))
1730 (let ((full (%search-load-path (in-vicinity dir-hint name))))
1733 (save-module-excursion (lambda () (primitive-load full)))
1735 (lambda () (set-autoloaded! dir-hint name didit)))
1739 ;;; Dynamic linking of modules
1741 ;; Initializing a module that is written in C is a two step process.
1742 ;; First the module's `module init' function is called. This function
1743 ;; is expected to call `scm_register_module_xxx' to register the `real
1744 ;; init' function. Later, when the module is referenced for the first
1745 ;; time, this real init function is called in the right context. See
1746 ;; gtcltk-lib/gtcltk-module.c for an example.
1748 ;; The code for the module can be in a regular shared library (so that
1749 ;; the `module init' function will be called when libguile is
1750 ;; initialized). Or it can be dynamically linked.
1752 ;; You can safely call `scm_register_module_xxx' before libguile
1753 ;; itself is initialized. You could call it from an C++ constructor
1754 ;; of a static object, for example.
1756 ;; To make your Guile extension into a dynamic linkable module, follow
1757 ;; these easy steps:
1759 ;; - Find a name for your module, like (ice-9 gtcltk)
1760 ;; - Write a function with a name like
1762 ;; scm_init_ice_9_gtcltk_module
1764 ;; This is your `module init' function. It should call
1766 ;; scm_register_module_xxx ("ice-9 gtcltk", scm_init_gtcltk);
1768 ;; "ice-9 gtcltk" is the C version of the module name. Slashes are
1769 ;; replaced by spaces, the rest is untouched. `scm_init_gtcltk' is
1770 ;; the real init function that executes the usual initializations
1771 ;; like making new smobs, etc.
1773 ;; - Make a shared library with your code and a name like
1775 ;; ice-9/libgtcltk.so
1777 ;; and put it somewhere in %load-path.
1779 ;; - Then you can simply write `:use-module (ice-9 gtcltk)' and it
1780 ;; will be linked automatically.
1782 ;; This is all very experimental.
1784 (define (split-c-module-name str)
1785 (let loop ((rev '())
1788 (end (string-length str)))
1791 (reverse (cons (string->symbol (substring str start pos)) rev)))
1792 ((eq? (string-ref str pos) #\space)
1793 (loop (cons (string->symbol (substring str start pos)) rev)
1798 (loop rev start (+ pos 1) end)))))
1800 (define (convert-c-registered-modules dynobj)
1801 (let ((res (map (lambda (c)
1802 (list (split-c-module-name (car c)) (cdr c) dynobj))
1803 (c-registered-modules))))
1804 (c-clear-registered-modules)
1807 (define registered-modules '())
1809 (define (register-modules dynobj)
1810 (set! registered-modules
1811 (append! (convert-c-registered-modules dynobj)
1812 registered-modules)))
1814 (define (warn-autoload-deprecation modname)
1816 ";;; Autoloading of compiled code modules is deprecated.\n"
1817 (current-error-port))
1819 ";;; Write a Scheme file instead that uses `dynamic-link' directly.\n"
1820 (current-error-port))
1821 (format (current-error-port)
1822 ";;; (You just tried to autoload module ~S.)\n" modname))
1824 (define (init-dynamic-module modname)
1825 ;; Register any linked modules which has been registered on the C level
1826 (register-modules #f)
1827 (or-map (lambda (modinfo)
1828 (if (equal? (car modinfo) modname)
1830 (warn-autoload-deprecation modname)
1831 (set! registered-modules (delq! modinfo registered-modules))
1832 (let ((mod (resolve-module modname #f)))
1833 (save-module-excursion
1835 (set-current-module mod)
1836 (set-module-public-interface! mod mod)
1837 (dynamic-call (cadr modinfo) (caddr modinfo))
1841 registered-modules))
1843 (define (dynamic-maybe-call name dynobj)
1844 (catch #t ; could use false-if-exception here
1846 (dynamic-call name dynobj))
1850 (define (dynamic-maybe-link filename)
1851 (catch #t ; could use false-if-exception here
1853 (dynamic-link filename))
1857 (define (find-and-link-dynamic-module module-name)
1858 (define (make-init-name mod-name)
1859 (string-append "scm_init"
1860 (list->string (map (lambda (c)
1861 (if (or (char-alphabetic? c)
1865 (string->list mod-name)))
1868 ;; Put the subdirectory for this module in the car of SUBDIR-AND-LIBNAME,
1869 ;; and the `libname' (the name of the module prepended by `lib') in the cdr
1870 ;; field. For example, if MODULE-NAME is the list (inet tcp-ip udp), then
1871 ;; SUBDIR-AND-LIBNAME will be the pair ("inet/tcp-ip" . "libudp").
1872 (let ((subdir-and-libname
1873 (let loop ((dirs "")
1875 (if (null? (cdr syms))
1876 (cons dirs (string-append "lib" (symbol->string (car syms))))
1877 (loop (string-append dirs (symbol->string (car syms)) "/")
1879 (init (make-init-name (apply string-append
1882 (symbol->string s)))
1884 (let ((subdir (car subdir-and-libname))
1885 (libname (cdr subdir-and-libname)))
1887 ;; Now look in each dir in %LOAD-PATH for `subdir/libfoo.la'. If that
1888 ;; file exists, fetch the dlname from that file and attempt to link
1889 ;; against it. If `subdir/libfoo.la' does not exist, or does not seem
1890 ;; to name any shared library, look for `subdir/libfoo.so' instead and
1891 ;; link against that.
1892 (let check-dirs ((dir-list %load-path))
1893 (if (null? dir-list)
1895 (let* ((dir (in-vicinity (car dir-list) subdir))
1897 (or (try-using-libtool-name dir libname)
1898 (try-using-sharlib-name dir libname))))
1899 (if (and sharlib-full (file-exists? sharlib-full))
1900 (link-dynamic-module sharlib-full init)
1901 (check-dirs (cdr dir-list)))))))))
1903 (define (try-using-libtool-name libdir libname)
1904 (let ((libtool-filename (in-vicinity libdir
1905 (string-append libname ".la"))))
1906 (and (file-exists? libtool-filename)
1909 (define (try-using-sharlib-name libdir libname)
1910 (in-vicinity libdir (string-append libname ".so")))
1912 (define (link-dynamic-module filename initname)
1913 ;; Register any linked modules which has been registered on the C level
1914 (register-modules #f)
1915 (let ((dynobj (dynamic-link filename)))
1916 (dynamic-call initname dynobj)
1917 (register-modules dynobj)))
1919 (define (try-module-linked module-name)
1920 (init-dynamic-module module-name))
1922 (define (try-module-dynamic-link module-name)
1923 (and (find-and-link-dynamic-module module-name)
1924 (init-dynamic-module module-name)))
1928 (define autoloads-done '((guile . guile)))
1930 (define (autoload-done-or-in-progress? p m)
1931 (let ((n (cons p m)))
1932 (->bool (or (member n autoloads-done)
1933 (member n autoloads-in-progress)))))
1935 (define (autoload-done! p m)
1936 (let ((n (cons p m)))
1937 (set! autoloads-in-progress
1938 (delete! n autoloads-in-progress))
1939 (or (member n autoloads-done)
1940 (set! autoloads-done (cons n autoloads-done)))))
1942 (define (autoload-in-progress! p m)
1943 (let ((n (cons p m)))
1944 (set! autoloads-done
1945 (delete! n autoloads-done))
1946 (set! autoloads-in-progress (cons n autoloads-in-progress))))
1948 (define (set-autoloaded! p m done?)
1950 (autoload-done! p m)
1951 (let ((n (cons p m)))
1952 (set! autoloads-done (delete! n autoloads-done))
1953 (set! autoloads-in-progress (delete! n autoloads-in-progress)))))
1960 ;; (eval-case ((situation*) forms)* (else forms)?)
1962 ;; Evaluate certain code based on the situation that eval-case is used
1963 ;; in. The only defined situation right now is `load-toplevel' which
1964 ;; triggers for code evaluated at the top-level, for example from the
1965 ;; REPL or when loading a file.
1968 (procedure->memoizing-macro
1970 (define (toplevel-env? env)
1971 (or (not (pair? env)) (not (pair? (car env)))))
1973 (error "syntax error in eval-case"))
1974 (let loop ((clauses (cdr exp)))
1978 ((not (list? (car clauses)))
1980 ((eq? 'else (caar clauses))
1981 (or (null? (cdr clauses))
1983 (cons 'begin (cdar clauses)))
1984 ((not (list? (caar clauses)))
1986 ((and (toplevel-env? env)
1987 (memq 'load-toplevel (caar clauses)))
1988 (cons 'begin (cdar clauses)))
1990 (loop (cdr clauses))))))))
1996 (define (primitive-macro? m)
1998 (not (macro-transformer m))))
2002 (define macro-table (make-weak-key-hash-table 523))
2003 (define xformer-table (make-weak-key-hash-table 523))
2005 (define (defmacro? m) (hashq-ref macro-table m))
2006 (define (assert-defmacro?! m) (hashq-set! macro-table m #t))
2007 (define (defmacro-transformer m) (hashq-ref xformer-table m))
2008 (define (set-defmacro-transformer! m t) (hashq-set! xformer-table m t))
2010 (define defmacro:transformer
2012 (let* ((xform (lambda (exp env)
2013 (copy-tree (apply f (cdr exp)))))
2014 (a (procedure->memoizing-macro xform)))
2015 (assert-defmacro?! a)
2016 (set-defmacro-transformer! a f)
2021 (let ((defmacro-transformer
2022 (lambda (name parms . body)
2023 (let ((transformer `(lambda ,parms ,@body)))
2025 (,(lambda (transformer)
2026 (defmacro:transformer transformer))
2028 (defmacro:transformer defmacro-transformer)))
2030 (define defmacro:syntax-transformer
2034 (copy-tree (apply f (cdr exp)))))))
2037 ;; XXX - should the definition of the car really be looked up in the
2040 (define (macroexpand-1 e)
2042 ((pair? e) (let* ((a (car e))
2043 (val (and (symbol? a) (local-ref (list a)))))
2045 (apply (defmacro-transformer val) (cdr e))
2049 (define (macroexpand e)
2051 ((pair? e) (let* ((a (car e))
2052 (val (and (symbol? a) (local-ref (list a)))))
2054 (macroexpand (apply (defmacro-transformer val) (cdr e)))
2062 ;;; {Run-time options}
2064 (define define-option-interface
2065 (let* ((option-name car)
2067 (option-documentation caddr)
2069 (print-option (lambda (option)
2070 (display (option-name option))
2071 (if (< (string-length
2072 (symbol->string (option-name option)))
2076 (display (option-value option))
2078 (display (option-documentation option))
2081 ;; Below follow the macros defining the run-time option interfaces.
2083 (make-options (lambda (interface)
2085 (cond ((null? args) (,interface))
2087 (,interface (car args)) (,interface))
2088 (else (for-each ,print-option
2089 (,interface #t)))))))
2091 (make-enable (lambda (interface)
2093 (,interface (append flags (,interface)))
2096 (make-disable (lambda (interface)
2098 (let ((options (,interface)))
2099 (for-each (lambda (flag)
2100 (set! options (delq! flag options)))
2102 (,interface options)
2105 (make-set! (lambda (interface)
2108 (begin (,interface (append (,interface)
2109 (list '(,'unquote name)
2115 (let* ((option-group (cadr exp))
2116 (interface (car option-group)))
2117 (append (map (lambda (name constructor)
2119 ,(constructor interface)))
2124 (map (lambda (name constructor)
2126 ,@(constructor interface)))
2127 (caddr option-group)
2128 (list make-set!)))))))))
2130 (define-option-interface
2131 (eval-options-interface
2132 (eval-options eval-enable eval-disable)
2135 (define-option-interface
2136 (debug-options-interface
2137 (debug-options debug-enable debug-disable)
2140 (define-option-interface
2141 (evaluator-traps-interface
2142 (traps trap-enable trap-disable)
2145 (define-option-interface
2146 (read-options-interface
2147 (read-options read-enable read-disable)
2150 (define-option-interface
2151 (print-options-interface
2152 (print-options print-enable print-disable)
2160 (define (repl read evaler print)
2161 (let loop ((source (read (current-input-port))))
2162 (print (evaler source))
2163 (loop (read (current-input-port)))))
2165 ;; A provisional repl that acts like the SCM repl:
2167 (define scm-repl-silent #f)
2168 (define (assert-repl-silence v) (set! scm-repl-silent v))
2170 (define *unspecified* (if #f #f))
2171 (define (unspecified? v) (eq? v *unspecified*))
2173 (define scm-repl-print-unspecified #f)
2174 (define (assert-repl-print-unspecified v) (set! scm-repl-print-unspecified v))
2176 (define scm-repl-verbose #f)
2177 (define (assert-repl-verbosity v) (set! scm-repl-verbose v))
2179 (define scm-repl-prompt "guile> ")
2181 (define (set-repl-prompt! v) (set! scm-repl-prompt v))
2183 (define (default-lazy-handler key . args)
2184 (save-stack lazy-handler-dispatch)
2185 (apply throw key args))
2187 (define enter-frame-handler default-lazy-handler)
2188 (define apply-frame-handler default-lazy-handler)
2189 (define exit-frame-handler default-lazy-handler)
2191 (define (lazy-handler-dispatch key . args)
2194 (apply apply-frame-handler key args))
2196 (apply exit-frame-handler key args))
2198 (apply enter-frame-handler key args))
2200 (apply default-lazy-handler key args))))
2202 (define abort-hook (make-hook))
2204 ;; these definitions are used if running a script.
2205 ;; otherwise redefined in error-catching-loop.
2206 (define (set-batch-mode?! arg) #t)
2207 (define (batch-mode?) #t)
2209 (define (error-catching-loop thunk)
2212 (define (loop first)
2220 (lambda () (unmask-signals))
2226 ;; This line is needed because mark
2227 ;; doesn't do closures quite right.
2228 ;; Unreferenced locals should be
2232 (let loop ((v (thunk)))
2235 (lambda () (mask-signals))))
2237 lazy-handler-dispatch))
2239 (lambda (key . args)
2246 (apply throw 'switch-repl args))
2249 ;; This is one of the closures that require
2250 ;; (set! first #f) above
2253 (run-hook abort-hook)
2254 (force-output (current-output-port))
2255 (display "ABORT: " (current-error-port))
2256 (write args (current-error-port))
2257 (newline (current-error-port))
2261 (not has-shown-debugger-hint?)
2262 (not (memq 'backtrace
2263 (debug-options-interface)))
2264 (stack? (fluid-ref the-last-stack)))
2266 (newline (current-error-port))
2268 "Type \"(backtrace)\" to get more information or \"(debug)\" to enter the debugger.\n"
2269 (current-error-port))
2270 (set! has-shown-debugger-hint? #t)))
2271 (force-output (current-error-port)))
2273 (primitive-exit 1)))
2274 (set! stack-saved? #f)))
2277 ;; This is the other cons-leak closure...
2279 (cond ((= (length args) 4)
2280 (apply handle-system-error key args))
2282 (apply bad-throw key args))))))))))
2283 (if next (loop next) status)))
2284 (set! set-batch-mode?! (lambda (arg)
2286 (set! interactive #f)
2289 (error "sorry, not implemented")))))
2290 (set! batch-mode? (lambda () (not interactive)))
2291 (loop (lambda () #t))))
2293 ;;(define the-last-stack (make-fluid)) Defined by scm_init_backtrace ()
2294 (define before-signal-stack (make-fluid))
2295 (define stack-saved? #f)
2297 (define (save-stack . narrowing)
2299 (cond ((not (memq 'debug (debug-options-interface)))
2300 (fluid-set! the-last-stack #f)
2301 (set! stack-saved? #t))
2307 (apply make-stack #t save-stack primitive-eval #t 0 narrowing))
2309 (apply make-stack #t save-stack 0 #t 0 narrowing))
2311 (apply make-stack #t save-stack tk-stack-mark #t 0 narrowing))
2313 (apply make-stack #t save-stack 0 1 narrowing))
2315 (let ((id (stack-id #t)))
2316 (and (procedure? id)
2317 (apply make-stack #t save-stack id #t 0 narrowing))))))
2318 (set! stack-saved? #t)))))
2320 (define before-error-hook (make-hook))
2321 (define after-error-hook (make-hook))
2322 (define before-backtrace-hook (make-hook))
2323 (define after-backtrace-hook (make-hook))
2325 (define has-shown-debugger-hint? #f)
2327 (define (handle-system-error key . args)
2328 (let ((cep (current-error-port)))
2329 (cond ((not (stack? (fluid-ref the-last-stack))))
2330 ((memq 'backtrace (debug-options-interface))
2331 (run-hook before-backtrace-hook)
2333 (display "Backtrace:\n")
2334 (display-backtrace (fluid-ref the-last-stack) cep)
2336 (run-hook after-backtrace-hook)))
2337 (run-hook before-error-hook)
2338 (apply display-error (fluid-ref the-last-stack) cep args)
2339 (run-hook after-error-hook)
2341 (throw 'abort key)))
2343 (define (quit . args)
2344 (apply throw 'quit args))
2348 ;;(define has-shown-backtrace-hint? #f) Defined by scm_init_backtrace ()
2350 ;; Replaced by C code:
2351 ;;(define (backtrace)
2352 ;; (if (fluid-ref the-last-stack)
2355 ;; (display-backtrace (fluid-ref the-last-stack) (current-output-port))
2357 ;; (if (and (not has-shown-backtrace-hint?)
2358 ;; (not (memq 'backtrace (debug-options-interface))))
2361 ;;"Type \"(debug-enable 'backtrace)\" if you would like a backtrace
2362 ;;automatically if an error occurs in the future.\n")
2363 ;; (set! has-shown-backtrace-hint? #t))))
2364 ;; (display "No backtrace available.\n")))
2366 (define (error-catching-repl r e p)
2367 (error-catching-loop (lambda () (p (e (r))))))
2369 (define (gc-run-time)
2370 (cdr (assq 'gc-time-taken (gc-stats))))
2372 (define before-read-hook (make-hook))
2373 (define after-read-hook (make-hook))
2374 (define before-eval-hook (make-hook 1))
2375 (define after-eval-hook (make-hook 1))
2376 (define before-print-hook (make-hook 1))
2377 (define after-print-hook (make-hook 1))
2379 ;;; The default repl-reader function. We may override this if we've
2380 ;;; the readline library.
2385 (run-hook before-read-hook)
2386 (read (current-input-port))))
2388 (define (scm-style-repl)
2393 (repl-report-start-timing (lambda ()
2394 (set! start-gc-rt (gc-run-time))
2395 (set! start-rt (get-internal-run-time))))
2396 (repl-report (lambda ()
2398 (display (inexact->exact
2399 (* 1000 (/ (- (get-internal-run-time) start-rt)
2400 internal-time-units-per-second))))
2402 (display (inexact->exact
2403 (* 1000 (/ (- (gc-run-time) start-gc-rt)
2404 internal-time-units-per-second))))
2405 (display " msec in gc)\n")))
2407 (consume-trailing-whitespace
2409 (let ((ch (peek-char)))
2412 ((or (char=? ch #\space) (char=? ch #\tab))
2414 (consume-trailing-whitespace))
2415 ((char=? ch #\newline)
2419 (let ((prompt (cond ((string? scm-repl-prompt)
2421 ((thunk? scm-repl-prompt)
2423 (scm-repl-prompt "> ")
2425 (repl-reader prompt))))
2427 ;; As described in R4RS, the READ procedure updates the
2428 ;; port to point to the first character past the end of
2429 ;; the external representation of the object. This
2430 ;; means that it doesn't consume the newline typically
2431 ;; found after an expression. This means that, when
2432 ;; debugging Guile with GDB, GDB gets the newline, which
2433 ;; it often interprets as a "continue" command, making
2434 ;; breakpoints kind of useless. So, consume any
2435 ;; trailing newline here, as well as any whitespace
2437 ;; But not if EOF, for control-D.
2438 (if (not (eof-object? val))
2439 (consume-trailing-whitespace))
2440 (run-hook after-read-hook)
2441 (if (eof-object? val)
2443 (repl-report-start-timing)
2444 (if scm-repl-verbose
2447 (display ";;; EOF -- quitting")
2452 (-eval (lambda (sourc)
2453 (repl-report-start-timing)
2454 (run-hook before-eval-hook sourc)
2455 (let ((val (start-stack 'repl-stack
2456 ;; If you change this procedure
2457 ;; (primitive-eval), please also
2458 ;; modify the repl-stack case in
2459 ;; save-stack so that stack cutting
2460 ;; continues to work.
2461 (primitive-eval sourc))))
2462 (run-hook after-eval-hook sourc)
2466 (-print (let ((maybe-print (lambda (result)
2467 (if (or scm-repl-print-unspecified
2468 (not (unspecified? result)))
2473 (if (not scm-repl-silent)
2475 (run-hook before-print-hook result)
2476 (maybe-print result)
2477 (run-hook after-print-hook result)
2478 (if scm-repl-verbose
2482 (-quit (lambda (args)
2483 (if scm-repl-verbose
2485 (display ";;; QUIT executed, repl exitting")
2491 (if scm-repl-verbose
2493 (display ";;; ABORT executed.")
2496 (repl -read -eval -print))))
2498 (let ((status (error-catching-repl -read
2505 ;;; {IOTA functions: generating lists of numbers}
2508 (let loop ((count (1- n)) (result '()))
2509 (if (< count 0) result
2510 (loop (1- count) (cons count result)))))
2515 ;;; with `continue' and `break'.
2518 (defmacro while (cond . body)
2519 `(letrec ((continue (lambda () (or (not ,cond) (begin (begin ,@ body) (continue)))))
2520 (break (lambda val (apply throw 'break val))))
2522 (lambda () (continue))
2523 (lambda v (cadr v)))))
2527 ;;; Similar to `begin' but returns a list of the results of all constituent
2528 ;;; forms instead of the result of the last form.
2529 ;;; (The definition relies on the current left-to-right
2530 ;;; order of evaluation of operands in applications.)
2532 (defmacro collect forms
2537 ;; with-fluids is a convenience wrapper for the builtin procedure
2538 ;; `with-fluids*'. The syntax is just like `let':
2540 ;; (with-fluids ((fluid val)
2544 (defmacro with-fluids (bindings . body)
2545 `(with-fluids* (list ,@(map car bindings)) (list ,@(map cadr bindings))
2546 (lambda () ,@body)))
2553 ;; actually....hobbit might be able to hack these with a little
2557 (defmacro define-macro (first . rest)
2558 (let ((name (if (symbol? first) first (car first)))
2562 `(lambda ,(cdr first) ,@rest))))
2563 `(define ,name (defmacro:transformer ,transformer))))
2566 (defmacro define-syntax-macro (first . rest)
2567 (let ((name (if (symbol? first) first (car first)))
2571 `(lambda ,(cdr first) ,@rest))))
2572 `(define ,name (defmacro:syntax-transformer ,transformer))))
2575 ;;; {Module System Macros}
2578 (defmacro define-module args
2581 (process-define-module ',args))
2583 (error "define-module can only be used at the top level"))))
2585 ;; the guts of the use-modules macro. add the interfaces of the named
2586 ;; modules to the use-list of the current module, in order
2587 (define (process-use-modules module-names)
2588 (for-each (lambda (module-name)
2589 (let ((mod-iface (resolve-interface module-name)))
2591 (error "no such module" module-name))
2592 (module-use! (current-module) mod-iface)))
2593 (reverse module-names)))
2595 (defmacro use-modules modules
2598 (process-use-modules ',modules))
2600 (error "use-modules can only be used at the top level"))))
2602 (defmacro use-syntax (spec)
2606 `((process-use-modules ',(list spec))
2607 (set-module-transformer! (current-module)
2608 ,(car (last-pair spec))))
2609 `((set-module-transformer! (current-module) ,spec)))
2610 (fluid-set! scm:eval-transformer (module-transformer (current-module))))
2612 (error "use-modules can only be used at the top level"))))
2614 (define define-private define)
2616 (defmacro define-public args
2618 (error "bad syntax" (list 'define-public args)))
2619 (define (defined-name n)
2622 ((pair? n) (defined-name (car n)))
2628 (let ((name (defined-name (car args))))
2630 (eval-case ((load-toplevel) (export ,name)))
2631 (define-private ,@args))))))
2633 (defmacro defmacro-public args
2635 (error "bad syntax" (list 'defmacro-public args)))
2636 (define (defined-name n)
2644 (let ((name (defined-name (car args))))
2646 (eval-case ((load-toplevel) (export ,name)))
2647 (defmacro ,@args))))))
2649 (define (module-export! m names)
2650 (let ((public-i (module-public-interface m)))
2651 (for-each (lambda (name)
2652 ;; Make sure there is a local variable:
2653 (module-define! m name (module-ref m name #f))
2654 ;; Make sure that local is exported:
2655 (module-add! public-i name (module-variable m name)))
2658 (defmacro export names
2661 (module-export! (current-module) ',names))
2663 (error "export can only be used at the top level"))))
2665 (define export-syntax export)
2668 (define load load-module)
2673 ;;; {Load emacs interface support if emacs option is given.}
2675 (define (named-module-use! user usee)
2676 (module-use! (resolve-module user) (resolve-module usee)))
2678 (define (load-emacs-interface)
2679 (if (memq 'debug-extensions *features*)
2680 (debug-enable 'backtrace))
2681 (named-module-use! '(guile-user) '(ice-9 emacs)))
2685 (define using-readline?
2686 (let ((using-readline? (make-fluid)))
2687 (make-procedure-with-setter
2688 (lambda () (fluid-ref using-readline?))
2689 (lambda (v) (fluid-set! using-readline? v)))))
2693 ;; Load emacs interface support if emacs option is given.
2694 (if (and (module-defined? the-root-module 'use-emacs-interface)
2695 (module-ref the-root-module 'use-emacs-interface))
2696 (load-emacs-interface))
2698 ;; Place the user in the guile-user module.
2699 (process-define-module
2701 :use-module (guile) ;so that bindings will be checked here first
2702 :use-module (ice-9 session)
2703 :use-module (ice-9 debug)
2704 :autoload (ice-9 debugger) (debug))) ;load debugger on demand
2705 (if (memq 'threads *features*)
2706 (named-module-use! '(guile-user) '(ice-9 threads)))
2707 (if (memq 'regex *features*)
2708 (named-module-use! '(guile-user) '(ice-9 regex)))
2710 (let ((old-handlers #f)
2711 (signals (if (provided? 'posix)
2712 `((,SIGINT . "User interrupt")
2713 (,SIGFPE . "Arithmetic error")
2714 (,SIGBUS . "Bad memory access (bus error)")
2716 "Bad memory access (Segmentation violation)"))
2723 (let ((make-handler (lambda (msg)
2725 ;; Make a backup copy of the stack
2726 (fluid-set! before-signal-stack
2727 (fluid-ref the-last-stack))
2728 (save-stack %deliver-signals)
2735 (map (lambda (sig-msg)
2736 (sigaction (car sig-msg)
2737 (make-handler (cdr sig-msg))))
2740 ;; the protected thunk.
2742 (let ((status (scm-style-repl)))
2743 (run-hook exit-hook)
2748 (map (lambda (sig-msg old-handler)
2749 (if (not (car old-handler))
2750 ;; restore original C handler.
2751 (sigaction (car sig-msg) #f)
2752 ;; restore Scheme handler, SIG_IGN or SIG_DFL.
2753 (sigaction (car sig-msg)
2755 (cdr old-handler))))
2756 signals old-handlers)))))
2758 (defmacro false-if-exception (expr)
2759 `(catch #t (lambda () ,expr)
2762 ;;; This hook is run at the very end of an interactive session.
2764 (define exit-hook (make-hook))
2767 (define-module (guile))
2769 (append! %load-path (cons "." '()))