1 ;;; -*- mode: scheme; coding: utf-8; -*-
3 ;;;; Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003,2004,2005,2006,2007,2008,2009
4 ;;;; Free Software Foundation, Inc.
6 ;;;; This library is free software; you can redistribute it and/or
7 ;;;; modify it under the terms of the GNU Lesser General Public
8 ;;;; License as published by the Free Software Foundation; either
9 ;;;; version 3 of the License, or (at your option) any later version.
11 ;;;; This library is distributed in the hope that it will be useful,
12 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 ;;;; Lesser General Public License for more details.
16 ;;;; You should have received a copy of the GNU Lesser General Public
17 ;;;; License along with this library; if not, write to the Free Software
18 ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 ;;; This file is the first thing loaded into Guile. It adds many mundane
26 ;;; definitions and a few that are interesting.
28 ;;; The module system (hence the hierarchical namespace) are defined in this
36 ;; Before compiling, make sure any symbols are resolved in the (guile)
37 ;; module, the primary location of those symbols, rather than in
38 ;; (guile-user), the default module that we compile in.
41 (set-current-module (resolve-module '(guile))))
46 (primitive-load-path "ice-9/r4rs")
50 ;;; {Simple Debugging Tools}
53 ;; peek takes any number of arguments, writes them to the
54 ;; current ouput port, and returns the last argument.
55 ;; It is handy to wrap around an expression to look at
56 ;; a value each time is evaluated, e.g.:
58 ;; (+ 10 (troublesome-fn))
59 ;; => (+ 10 (pk 'troublesome-fn-returned (troublesome-fn)))
62 (define (peek . stuff)
67 (car (last-pair stuff)))
71 (define (warn . stuff)
72 (with-output-to-port (current-error-port)
75 (display ";;; WARNING ")
78 (car (last-pair stuff)))))
86 (if (not (memq sym *features*))
87 (set! *features* (cons sym *features*))))
89 ;; Return #t iff FEATURE is available to this Guile interpreter. In SLIB,
90 ;; provided? also checks to see if the module is available. We should do that
93 (define (provided? feature)
94 (and (memq feature *features*) #t))
98 ;;; {and-map and or-map}
100 ;;; (and-map fn lst) is like (and (fn (car lst)) (fn (cadr lst)) (fn...) ...)
101 ;;; (or-map fn lst) is like (or (fn (car lst)) (fn (cadr lst)) (fn...) ...)
106 ;; Apply f to successive elements of l until exhaustion or f returns #f.
107 ;; If returning early, return #f. Otherwise, return the last value returned
108 ;; by f. If f has never been called because l is empty, return #t.
110 (define (and-map f lst)
111 (let loop ((result #t)
116 (loop (f (car l)) (cdr l))))))
120 ;; Apply f to successive elements of l until exhaustion or while f returns #f.
121 ;; If returning early, return the return value of f.
123 (define (or-map f lst)
124 (let loop ((result #f)
128 (loop (f (car l)) (cdr l))))))
132 ;; let format alias simple-format until the more complete version is loaded
134 (define format simple-format)
136 ;; this is scheme wrapping the C code so the final pred call is a tail call,
138 (define (string-any char_pred s . rest)
139 (let ((start (if (null? rest)
141 (end (if (or (null? rest) (null? (cdr rest)))
142 (string-length s) (cadr rest))))
143 (if (and (procedure? char_pred)
145 (<= end (string-length s))) ;; let c-code handle range error
146 (or (string-any-c-code char_pred s start (1- end))
147 (char_pred (string-ref s (1- end))))
148 (string-any-c-code char_pred s start end))))
150 ;; this is scheme wrapping the C code so the final pred call is a tail call,
152 (define (string-every char_pred s . rest)
153 (let ((start (if (null? rest)
155 (end (if (or (null? rest) (null? (cdr rest)))
156 (string-length s) (cadr rest))))
157 (if (and (procedure? char_pred)
159 (<= end (string-length s))) ;; let c-code handle range error
160 (and (string-every-c-code char_pred s start (1- end))
161 (char_pred (string-ref s (1- end))))
162 (string-every-c-code char_pred s start end))))
164 ;; A variant of string-fill! that we keep for compatability
166 (define (substring-fill! str start end fill)
167 (string-fill! str fill start end))
171 ;; Define a minimal stub of the module API for psyntax, before modules
173 (define (module-name x)
175 (define (module-define! module sym val)
176 (let ((v (hashq-ref (%get-pre-modules-obarray) sym)))
178 (variable-set! v val)
179 (hashq-set! (%get-pre-modules-obarray) sym
180 (make-variable val)))))
181 (define (module-ref module sym)
182 (let ((v (module-variable module sym)))
183 (if v (variable-ref v) (error "badness!" (pk module) (pk sym)))))
184 (define (resolve-module . args)
187 ;; Input hook to syncase -- so that we might be able to pass annotated
188 ;; expressions in. Currently disabled. Maybe we should just use
189 ;; source-properties directly.
190 (define (annotation? x) #f)
192 ;; API provided by psyntax
193 (define syntax-violation #f)
194 (define datum->syntax #f)
195 (define syntax->datum #f)
196 (define identifier? #f)
197 (define generate-temporaries #f)
198 (define bound-identifier=? #f)
199 (define free-identifier=? #f)
200 (define sc-expand #f)
202 ;; $sc-expand is an implementation detail of psyntax. It is used by
203 ;; expanded macros, to dispatch an input against a set of patterns.
204 (define $sc-dispatch #f)
207 (primitive-load-path "ice-9/psyntax-pp")
209 ;; %pre-modules-transformer is the Scheme expander from now until the
210 ;; module system has booted up.
211 (define %pre-modules-transformer sc-expand)
217 ((_ x y ...) (if x (and y ...) #f))))
223 ((_ x y ...) (let ((t x)) (if t t (or y ...))))))
225 ;; The "maybe-more" bits are something of a hack, so that we can support
226 ;; SRFI-61. Rewrites into a standalone syntax-case macro would be
229 (syntax-rules (=> else)
230 ((_ "maybe-more" test consequent)
231 (if test consequent))
233 ((_ "maybe-more" test consequent clause ...)
234 (if test consequent (cond clause ...)))
236 ((_ (else else1 else2 ...))
237 (begin else1 else2 ...))
239 ((_ (test => receiver) more-clause ...)
241 (cond "maybe-more" t (receiver t) more-clause ...)))
243 ((_ (generator guard => receiver) more-clause ...)
244 (call-with-values (lambda () generator)
247 (apply guard t) (apply receiver t) more-clause ...))))
249 ((_ (test => receiver ...) more-clause ...)
250 (syntax-violation 'cond "wrong number of receiver expressions"
251 '(test => receiver ...)))
252 ((_ (generator guard => receiver ...) more-clause ...)
253 (syntax-violation 'cond "wrong number of receiver expressions"
254 '(generator guard => receiver ...)))
256 ((_ (test) more-clause ...)
258 (cond "maybe-more" t t more-clause ...)))
260 ((_ (test body1 body2 ...) more-clause ...)
262 test (begin body1 body2 ...) more-clause ...))))
268 (let ((atom-key (key ...)))
269 (case atom-key clauses ...)))
271 (else result1 result2 ...))
272 (begin result1 result2 ...))
274 ((atoms ...) result1 result2 ...))
275 (if (memv key '(atoms ...))
276 (begin result1 result2 ...)))
278 ((atoms ...) result1 result2 ...)
280 (if (memv key '(atoms ...))
281 (begin result1 result2 ...)
282 (case key clause clauses ...)))))
286 ((do ((var init step ...) ...)
299 (loop (do "step" var step ...)
309 ((_ exp) (make-promise (lambda () exp)))))
311 ;;; @bind is used by the old elisp code as a dynamic scoping mechanism.
312 ;;; Please let the Guile developers know if you are using this macro.
316 (define (bound-member id ids)
317 (cond ((null? ids) #f)
318 ((bound-identifier=? id (car ids)) #t)
319 ((bound-member (car ids) (cdr ids)))))
323 #'(let () b0 b1 ...))
324 ((_ ((id val) ...) b0 b1 ...)
325 (and-map identifier? #'(id ...))
326 (if (let lp ((ids #'(id ...)))
327 (cond ((null? ids) #f)
328 ((bound-member (car ids) (cdr ids)) #t)
329 (else (lp (cdr ids)))))
330 (syntax-violation '@bind "duplicate bound identifier" x)
331 (with-syntax (((old-v ...) (generate-temporaries #'(id ...)))
332 ((v ...) (generate-temporaries #'(id ...))))
333 #'(let ((old-v id) ...
338 (lambda () b0 b1 ...)
340 (set! id old-v) ...)))))))))
348 (define-syntax define-macro
352 ((_ (macro . args) doc body1 body ...)
353 (string? (syntax->datum (syntax doc)))
354 (syntax (define-macro macro doc (lambda args body1 body ...))))
355 ((_ (macro . args) body ...)
356 (syntax (define-macro macro #f (lambda args body ...))))
357 ((_ macro doc transformer)
358 (or (string? (syntax->datum (syntax doc)))
359 (not (syntax->datum (syntax doc))))
366 (let ((v (syntax->datum (syntax args))))
367 (datum->syntax y (apply transformer v))))))))))))
369 (define-syntax defmacro
371 "Define a defmacro, with the old lispy defun syntax."
373 ((_ macro args doc body1 body ...)
374 (string? (syntax->datum (syntax doc)))
375 (syntax (define-macro macro doc (lambda args body1 body ...))))
376 ((_ macro args body ...)
377 (syntax (define-macro macro #f (lambda args body ...)))))))
385 ;;; Depends on: defmacro
388 (defmacro begin-deprecated forms
389 (if (include-deprecated-features)
395 ;;; {Trivial Functions}
398 (define (identity x) x)
399 (define (and=> value procedure) (and value (procedure value)))
400 (define call/cc call-with-current-continuation)
402 ;;; apply-to-args is functionally redundant with apply and, worse,
403 ;;; is less general than apply since it only takes two arguments.
405 ;;; On the other hand, apply-to-args is a syntacticly convenient way to
406 ;;; perform binding in many circumstances when the "let" family of
407 ;;; of forms don't cut it. E.g.:
409 ;;; (apply-to-args (return-3d-mouse-coords)
414 (define (apply-to-args args fn) (apply fn args))
416 (defmacro false-if-exception (expr)
419 ;; avoid saving backtraces inside false-if-exception
420 (with-fluid* the-last-stack (fluid-ref the-last-stack)
426 ;;; {General Properties}
429 ;; This is a more modern interface to properties. It will replace all
430 ;; other property-like things eventually.
432 (define (make-object-property)
433 (let ((prop (primitive-make-property #f)))
434 (make-procedure-with-setter
435 (lambda (obj) (primitive-property-ref prop obj))
436 (lambda (obj val) (primitive-property-set! prop obj val)))))
440 ;;; {Symbol Properties}
443 (define (symbol-property sym prop)
444 (let ((pair (assoc prop (symbol-pref sym))))
445 (and pair (cdr pair))))
447 (define (set-symbol-property! sym prop val)
448 (let ((pair (assoc prop (symbol-pref sym))))
451 (symbol-pset! sym (acons prop val (symbol-pref sym))))))
453 (define (symbol-property-remove! sym prop)
454 (let ((pair (assoc prop (symbol-pref sym))))
456 (symbol-pset! sym (delq! pair (symbol-pref sym))))))
463 (define (array-shape a)
464 (map (lambda (ind) (if (number? ind) (list 0 (+ -1 ind)) ind))
465 (array-dimensions a)))
472 (define (kw-arg-ref args kw)
473 (let ((rem (member kw args)))
474 (and rem (pair? (cdr rem)) (cadr rem))))
481 (define (struct-layout s)
482 (struct-ref (struct-vtable s) vtable-index-layout))
489 ;; Printing records: by default, records are printed as
491 ;; #<type-name field1: val1 field2: val2 ...>
493 ;; You can change that by giving a custom printing function to
494 ;; MAKE-RECORD-TYPE (after the list of field symbols). This function
495 ;; will be called like
497 ;; (<printer> object port)
499 ;; It should print OBJECT to PORT.
501 (define (inherit-print-state old-port new-port)
502 (if (get-print-state old-port)
503 (port-with-print-state new-port (get-print-state old-port))
506 ;; 0: type-name, 1: fields
507 (define record-type-vtable
508 (make-vtable-vtable "prpr" 0
510 (cond ((eq? s record-type-vtable)
511 (display "#<record-type-vtable>" p))
513 (display "#<record-type " p)
514 (display (record-type-name s) p)
517 (define (record-type? obj)
518 (and (struct? obj) (eq? record-type-vtable (struct-vtable obj))))
520 (define (make-record-type type-name fields . opt)
521 (let ((printer-fn (and (pair? opt) (car opt))))
522 (let ((struct (make-struct record-type-vtable 0
525 (map (lambda (f) "pw") fields)))
529 (display type-name p)
530 (let loop ((fields fields)
533 ((not (null? fields))
535 (display (car fields) p)
537 (display (struct-ref s off) p)
538 (loop (cdr fields) (+ 1 off)))))
541 (copy-tree fields))))
542 ;; Temporary solution: Associate a name to the record type descriptor
543 ;; so that the object system can create a wrapper class for it.
544 (set-struct-vtable-name! struct (if (symbol? type-name)
546 (string->symbol type-name)))
549 (define (record-type-name obj)
550 (if (record-type? obj)
551 (struct-ref obj vtable-offset-user)
552 (error 'not-a-record-type obj)))
554 (define (record-type-fields obj)
555 (if (record-type? obj)
556 (struct-ref obj (+ 1 vtable-offset-user))
557 (error 'not-a-record-type obj)))
559 (define (record-constructor rtd . opt)
560 (let ((field-names (if (pair? opt) (car opt) (record-type-fields rtd))))
562 `(lambda ,field-names
563 (make-struct ',rtd 0 ,@(map (lambda (f)
564 (if (memq f field-names)
567 (record-type-fields rtd)))))))
569 (define (record-predicate rtd)
570 (lambda (obj) (and (struct? obj) (eq? rtd (struct-vtable obj)))))
572 (define (%record-type-error rtd obj) ;; private helper
573 (or (eq? rtd (record-type-descriptor obj))
574 (scm-error 'wrong-type-arg "%record-type-check"
575 "Wrong type record (want `~S'): ~S"
576 (list (record-type-name rtd) obj)
579 (define (record-accessor rtd field-name)
580 (let ((pos (list-index (record-type-fields rtd) field-name)))
582 (error 'no-such-field field-name))
584 (if (eq? (struct-vtable obj) rtd)
586 (%record-type-error rtd obj)))))
588 (define (record-modifier rtd field-name)
589 (let ((pos (list-index (record-type-fields rtd) field-name)))
591 (error 'no-such-field field-name))
593 (if (eq? (struct-vtable obj) rtd)
594 (struct-set! obj pos val)
595 (%record-type-error rtd obj)))))
597 (define (record? obj)
598 (and (struct? obj) (record-type? (struct-vtable obj))))
600 (define (record-type-descriptor obj)
603 (error 'not-a-record obj)))
612 (define (->bool x) (not (not x)))
619 (define (symbol-append . args)
620 (string->symbol (apply string-append (map symbol->string args))))
622 (define (list->symbol . args)
623 (string->symbol (apply list->string args)))
625 (define (symbol . args)
626 (string->symbol (apply string args)))
633 (define (list-index l k)
639 (loop (+ n 1) (cdr l))))))
643 (if (provided? 'posix)
644 (primitive-load-path "ice-9/posix"))
646 (if (provided? 'socket)
647 (primitive-load-path "ice-9/networking"))
649 ;; For reference, Emacs file-exists-p uses stat in this same way.
651 (if (provided? 'posix)
653 (->bool (stat str #f)))
655 (let ((port (catch 'system-error (lambda () (open-file str OPEN_READ))
657 (if port (begin (close-port port) #t)
660 (define file-is-directory?
661 (if (provided? 'posix)
663 (eq? (stat:type (stat str)) 'directory))
665 (let ((port (catch 'system-error
666 (lambda () (open-file (string-append str "/.")
669 (if port (begin (close-port port) #t)
672 (define (has-suffix? str suffix)
673 (string-suffix? suffix str))
675 (define (system-error-errno args)
676 (if (eq? (car args) 'system-error)
677 (car (list-ref args 4))
685 (define (error . args)
688 (scm-error 'misc-error #f "?" #f #f)
689 (let loop ((msg "~A")
691 (if (not (null? rest))
692 (loop (string-append msg " ~S")
694 (scm-error 'misc-error #f msg args #f)))))
696 ;; bad-throw is the hook that is called upon a throw to a an unhandled
697 ;; key (unless the throw has four arguments, in which case
698 ;; it's usually interpreted as an error throw.)
699 ;; If the key has a default handler (a throw-handler-default property),
700 ;; it is applied to the throw.
702 (define (bad-throw key . args)
703 (let ((default (symbol-property key 'throw-handler-default)))
704 (or (and default (apply default key args))
705 (apply error "unhandled-exception:" key args))))
709 (define (tm:sec obj) (vector-ref obj 0))
710 (define (tm:min obj) (vector-ref obj 1))
711 (define (tm:hour obj) (vector-ref obj 2))
712 (define (tm:mday obj) (vector-ref obj 3))
713 (define (tm:mon obj) (vector-ref obj 4))
714 (define (tm:year obj) (vector-ref obj 5))
715 (define (tm:wday obj) (vector-ref obj 6))
716 (define (tm:yday obj) (vector-ref obj 7))
717 (define (tm:isdst obj) (vector-ref obj 8))
718 (define (tm:gmtoff obj) (vector-ref obj 9))
719 (define (tm:zone obj) (vector-ref obj 10))
721 (define (set-tm:sec obj val) (vector-set! obj 0 val))
722 (define (set-tm:min obj val) (vector-set! obj 1 val))
723 (define (set-tm:hour obj val) (vector-set! obj 2 val))
724 (define (set-tm:mday obj val) (vector-set! obj 3 val))
725 (define (set-tm:mon obj val) (vector-set! obj 4 val))
726 (define (set-tm:year obj val) (vector-set! obj 5 val))
727 (define (set-tm:wday obj val) (vector-set! obj 6 val))
728 (define (set-tm:yday obj val) (vector-set! obj 7 val))
729 (define (set-tm:isdst obj val) (vector-set! obj 8 val))
730 (define (set-tm:gmtoff obj val) (vector-set! obj 9 val))
731 (define (set-tm:zone obj val) (vector-set! obj 10 val))
733 (define (tms:clock obj) (vector-ref obj 0))
734 (define (tms:utime obj) (vector-ref obj 1))
735 (define (tms:stime obj) (vector-ref obj 2))
736 (define (tms:cutime obj) (vector-ref obj 3))
737 (define (tms:cstime obj) (vector-ref obj 4))
739 (define file-position ftell)
740 (define (file-set-position port offset . whence)
741 (let ((whence (if (eq? whence '()) SEEK_SET (car whence))))
742 (seek port offset whence)))
744 (define (move->fdes fd/port fd)
745 (cond ((integer? fd/port)
746 (dup->fdes fd/port fd)
750 (primitive-move->fdes fd/port fd)
751 (set-port-revealed! fd/port 1)
754 (define (release-port-handle port)
755 (let ((revealed (port-revealed port)))
757 (set-port-revealed! port (- revealed 1)))))
759 (define (dup->port port/fd mode . maybe-fd)
760 (let ((port (fdopen (apply dup->fdes port/fd maybe-fd)
763 (set-port-revealed! port 1))
766 (define (dup->inport port/fd . maybe-fd)
767 (apply dup->port port/fd "r" maybe-fd))
769 (define (dup->outport port/fd . maybe-fd)
770 (apply dup->port port/fd "w" maybe-fd))
772 (define (dup port/fd . maybe-fd)
773 (if (integer? port/fd)
774 (apply dup->fdes port/fd maybe-fd)
775 (apply dup->port port/fd (port-mode port/fd) maybe-fd)))
777 (define (duplicate-port port modes)
778 (dup->port port modes))
780 (define (fdes->inport fdes)
781 (let loop ((rest-ports (fdes->ports fdes)))
782 (cond ((null? rest-ports)
783 (let ((result (fdopen fdes "r")))
784 (set-port-revealed! result 1)
786 ((input-port? (car rest-ports))
787 (set-port-revealed! (car rest-ports)
788 (+ (port-revealed (car rest-ports)) 1))
791 (loop (cdr rest-ports))))))
793 (define (fdes->outport fdes)
794 (let loop ((rest-ports (fdes->ports fdes)))
795 (cond ((null? rest-ports)
796 (let ((result (fdopen fdes "w")))
797 (set-port-revealed! result 1)
799 ((output-port? (car rest-ports))
800 (set-port-revealed! (car rest-ports)
801 (+ (port-revealed (car rest-ports)) 1))
804 (loop (cdr rest-ports))))))
806 (define (port->fdes port)
807 (set-port-revealed! port (+ (port-revealed port) 1))
810 (define (setenv name value)
812 (putenv (string-append name "=" value))
815 (define (unsetenv name)
816 "Remove the entry for NAME from the environment."
824 ;;; Here for backward compatability
826 (define scheme-file-suffix (lambda () ".scm"))
828 (define (in-vicinity vicinity file)
829 (let ((tail (let ((len (string-length vicinity)))
832 (string-ref vicinity (- len 1))))))
833 (string-append vicinity
842 ;;; {Help for scm_shell}
844 ;;; The argument-processing code used by Guile-based shells generates
845 ;;; Scheme code based on the argument list. This page contains help
846 ;;; functions for the code it generates.
849 (define (command-line) (program-arguments))
851 ;; This is mostly for the internal use of the code generated by
852 ;; scm_compile_shell_switches.
854 (define (turn-on-debugging)
855 (debug-enable 'debug)
856 (debug-enable 'backtrace)
857 (read-enable 'positions))
859 (define (load-user-init)
860 (let* ((home (or (getenv "HOME")
861 (false-if-exception (passwd:dir (getpwuid (getuid))))
862 "/")) ;; fallback for cygwin etc.
863 (init-file (in-vicinity home ".guile")))
864 (if (file-exists? init-file)
865 (primitive-load init-file))))
869 ;;; {The interpreter stack}
872 (defmacro start-stack (tag exp)
873 `(%start-stack ,tag (lambda () ,exp)))
877 ;;; {Loading by paths}
880 ;;; Load a Scheme source file named NAME, searching for it in the
881 ;;; directories listed in %load-path, and applying each of the file
882 ;;; name extensions listed in %load-extensions.
883 (define (load-from-path name)
884 (start-stack 'load-stack
885 (primitive-load-path name)))
887 (define %load-verbosely #f)
888 (define (assert-load-verbosity v) (set! %load-verbosely v))
890 (define (%load-announce file)
892 (with-output-to-port (current-error-port)
900 (set! %load-hook %load-announce)
902 (define (load name . reader)
903 ;; Returns the .go file corresponding to `name'. Does not search load
904 ;; paths, only the fallback path. If the .go file is missing or out of
905 ;; date, and autocompilation is enabled, will try autocompilation, just
906 ;; as primitive-load-path does internally. primitive-load is
907 ;; unaffected. Returns #f if autocompilation failed or was disabled.
908 (define (autocompiled-file-name name)
911 (let* ((cfn ((@ (system base compile) compiled-file-name) name))
912 (scmstat (stat name))
913 (gostat (stat cfn #f)))
914 (if (and gostat (= (stat:mtime gostat) (stat:mtime scmstat)))
918 (format (current-error-port)
919 ";;; note: source file ~a\n;;; newer than compiled ~a\n"
922 (%load-should-autocompile
923 (%warn-autocompilation-enabled)
924 (format (current-error-port) ";;; compiling ~a\n" name)
925 (let ((cfn ((@ (system base compile) compile-file) name
926 #:env (current-module))))
927 (format (current-error-port) ";;; compiled ~a\n" cfn)
931 (format (current-error-port)
932 ";;; WARNING: compilation of ~a failed:\n;;; key ~a, throw_args ~s\n"
935 (with-fluid* current-reader (and (pair? reader) (car reader))
937 (let ((cfn (autocompiled-file-name name)))
940 (start-stack 'load-stack
941 (primitive-load name)))))))
945 ;;; {Transcendental Functions}
947 ;;; Derived from "Transcen.scm", Complex trancendental functions for SCM.
948 ;;; Written by Jerry D. Hedden, (C) FSF.
949 ;;; See the file `COPYING' for terms applying to this program.
953 (let ((integer-expt integer-expt))
955 (cond ((and (exact? z2) (integer? z2))
956 (integer-expt z1 z2))
957 ((and (real? z2) (real? z1) (>= z1 0))
960 (exp (* z2 (log z1))))))))
963 (if (real? z) ($sinh z)
964 (let ((x (real-part z)) (y (imag-part z)))
965 (make-rectangular (* ($sinh x) ($cos y))
966 (* ($cosh x) ($sin y))))))
968 (if (real? z) ($cosh z)
969 (let ((x (real-part z)) (y (imag-part z)))
970 (make-rectangular (* ($cosh x) ($cos y))
971 (* ($sinh x) ($sin y))))))
973 (if (real? z) ($tanh z)
974 (let* ((x (* 2 (real-part z)))
975 (y (* 2 (imag-part z)))
976 (w (+ ($cosh x) ($cos y))))
977 (make-rectangular (/ ($sinh x) w) (/ ($sin y) w)))))
980 (if (real? z) ($asinh z)
981 (log (+ z (sqrt (+ (* z z) 1))))))
984 (if (and (real? z) (>= z 1))
986 (log (+ z (sqrt (- (* z z) 1))))))
989 (if (and (real? z) (> z -1) (< z 1))
991 (/ (log (/ (+ 1 z) (- 1 z))) 2)))
994 (if (real? z) ($sin z)
995 (let ((x (real-part z)) (y (imag-part z)))
996 (make-rectangular (* ($sin x) ($cosh y))
997 (* ($cos x) ($sinh y))))))
999 (if (real? z) ($cos z)
1000 (let ((x (real-part z)) (y (imag-part z)))
1001 (make-rectangular (* ($cos x) ($cosh y))
1002 (- (* ($sin x) ($sinh y)))))))
1004 (if (real? z) ($tan z)
1005 (let* ((x (* 2 (real-part z)))
1006 (y (* 2 (imag-part z)))
1007 (w (+ ($cos x) ($cosh y))))
1008 (make-rectangular (/ ($sin x) w) (/ ($sinh y) w)))))
1011 (if (and (real? z) (>= z -1) (<= z 1))
1013 (* -i (asinh (* +i z)))))
1016 (if (and (real? z) (>= z -1) (<= z 1))
1018 (+ (/ (angle -1) 2) (* +i (asinh (* +i z))))))
1020 (define (atan z . y)
1022 (if (real? z) ($atan z)
1023 (/ (log (/ (- +i z) (+ +i z))) +2i))
1024 ($atan2 z (car y))))
1028 ;;; {Reader Extensions}
1030 ;;; Reader code for various "#c" forms.
1033 (define read-eval? (make-fluid))
1034 (fluid-set! read-eval? #f)
1035 (read-hash-extend #\.
1037 (if (fluid-ref read-eval?)
1038 (eval (read port) (interaction-environment))
1040 "#. read expansion found and read-eval? is #f."))))
1044 ;;; {Command Line Options}
1047 (define (get-option argv kw-opts kw-args return)
1050 (return #f #f argv))
1052 ((or (not (eq? #\- (string-ref (car argv) 0)))
1053 (eq? (string-length (car argv)) 1))
1054 (return 'normal-arg (car argv) (cdr argv)))
1056 ((eq? #\- (string-ref (car argv) 1))
1057 (let* ((kw-arg-pos (or (string-index (car argv) #\=)
1058 (string-length (car argv))))
1059 (kw (symbol->keyword (substring (car argv) 2 kw-arg-pos)))
1060 (kw-opt? (member kw kw-opts))
1061 (kw-arg? (member kw kw-args))
1062 (arg (or (and (not (eq? kw-arg-pos (string-length (car argv))))
1063 (substring (car argv)
1065 (string-length (car argv))))
1067 (begin (set! argv (cdr argv)) (car argv))))))
1068 (if (or kw-opt? kw-arg?)
1069 (return kw arg (cdr argv))
1070 (return 'usage-error kw (cdr argv)))))
1073 (let* ((char (substring (car argv) 1 2))
1074 (kw (symbol->keyword char)))
1077 ((member kw kw-opts)
1078 (let* ((rest-car (substring (car argv) 2 (string-length (car argv))))
1079 (new-argv (if (= 0 (string-length rest-car))
1081 (cons (string-append "-" rest-car) (cdr argv)))))
1082 (return kw #f new-argv)))
1084 ((member kw kw-args)
1085 (let* ((rest-car (substring (car argv) 2 (string-length (car argv))))
1086 (arg (if (= 0 (string-length rest-car))
1089 (new-argv (if (= 0 (string-length rest-car))
1092 (return kw arg new-argv)))
1094 (else (return 'usage-error kw argv)))))))
1096 (define (for-next-option proc argv kw-opts kw-args)
1097 (let loop ((argv argv))
1098 (get-option argv kw-opts kw-args
1099 (lambda (opt opt-arg argv)
1100 (and opt (proc opt opt-arg argv loop))))))
1102 (define (display-usage-report kw-desc)
1105 (or (eq? (car kw) #t)
1106 (eq? (car kw) 'else)
1107 (let* ((opt-desc kw)
1108 (help (cadr opt-desc))
1109 (opts (car opt-desc))
1110 (opts-proper (if (string? (car opts)) (cdr opts) opts))
1111 (arg-name (if (string? (car opts))
1112 (string-append "<" (car opts) ">")
1114 (left-part (string-append
1115 (with-output-to-string
1117 (map (lambda (x) (display (keyword->symbol x)) (display " "))
1120 (middle-part (if (and (< (string-length left-part) 30)
1121 (< (string-length help) 40))
1122 (make-string (- 30 (string-length left-part)) #\ )
1125 (display middle-part)
1132 (define (transform-usage-lambda cases)
1133 (let* ((raw-usage (delq! 'else (map car cases)))
1134 (usage-sans-specials (map (lambda (x)
1135 (or (and (not (list? x)) x)
1136 (and (symbol? (car x)) #t)
1137 (and (boolean? (car x)) #t)
1140 (usage-desc (delq! #t usage-sans-specials))
1141 (kw-desc (map car usage-desc))
1142 (kw-opts (apply append (map (lambda (x) (and (not (string? (car x))) x)) kw-desc)))
1143 (kw-args (apply append (map (lambda (x) (and (string? (car x)) (cdr x))) kw-desc)))
1144 (transmogrified-cases (map (lambda (case)
1145 (cons (let ((opts (car case)))
1146 (if (or (boolean? opts) (eq? 'else opts))
1149 ((symbol? (car opts)) opts)
1150 ((boolean? (car opts)) opts)
1151 ((string? (caar opts)) (cdar opts))
1152 (else (car opts)))))
1155 `(let ((%display-usage (lambda () (display-usage-report ',usage-desc))))
1157 (let %next-arg ((%argv %argv))
1161 (lambda (%opt %arg %new-argv)
1163 ,@ transmogrified-cases))))))))
1168 ;;; {Low Level Modules}
1170 ;;; These are the low level data structures for modules.
1172 ;;; Every module object is of the type 'module-type', which is a record
1173 ;;; consisting of the following members:
1175 ;;; - eval-closure: the function that defines for its module the strategy that
1176 ;;; shall be followed when looking up symbols in the module.
1178 ;;; An eval-closure is a function taking two arguments: the symbol to be
1179 ;;; looked up and a boolean value telling whether a binding for the symbol
1180 ;;; should be created if it does not exist yet. If the symbol lookup
1181 ;;; succeeded (either because an existing binding was found or because a new
1182 ;;; binding was created), a variable object representing the binding is
1183 ;;; returned. Otherwise, the value #f is returned. Note that the eval
1184 ;;; closure does not take the module to be searched as an argument: During
1185 ;;; construction of the eval-closure, the eval-closure has to store the
1186 ;;; module it belongs to in its environment. This means, that any
1187 ;;; eval-closure can belong to only one module.
1189 ;;; The eval-closure of a module can be defined arbitrarily. However, three
1190 ;;; special cases of eval-closures are to be distinguished: During startup
1191 ;;; the module system is not yet activated. In this phase, no modules are
1192 ;;; defined and all bindings are automatically stored by the system in the
1193 ;;; pre-modules-obarray. Since no eval-closures exist at this time, the
1194 ;;; functions which require an eval-closure as their argument need to be
1195 ;;; passed the value #f.
1197 ;;; The other two special cases of eval-closures are the
1198 ;;; standard-eval-closure and the standard-interface-eval-closure. Both
1199 ;;; behave equally for the case that no new binding is to be created. The
1200 ;;; difference between the two comes in, when the boolean argument to the
1201 ;;; eval-closure indicates that a new binding shall be created if it is not
1204 ;;; Given that no new binding shall be created, both standard eval-closures
1205 ;;; define the following standard strategy of searching bindings in the
1206 ;;; module: First, the module's obarray is searched for the symbol. Second,
1207 ;;; if no binding for the symbol was found in the module's obarray, the
1208 ;;; module's binder procedure is exececuted. If this procedure did not
1209 ;;; return a binding for the symbol, the modules referenced in the module's
1210 ;;; uses list are recursively searched for a binding of the symbol. If the
1211 ;;; binding can not be found in these modules also, the symbol lookup has
1214 ;;; If a new binding shall be created, the standard-interface-eval-closure
1215 ;;; immediately returns indicating failure. That is, it does not even try
1216 ;;; to look up the symbol. In contrast, the standard-eval-closure would
1217 ;;; first search the obarray, and if no binding was found there, would
1218 ;;; create a new binding in the obarray, therefore not calling the binder
1219 ;;; procedure or searching the modules in the uses list.
1221 ;;; The explanation of the following members obarray, binder and uses
1222 ;;; assumes that the symbol lookup follows the strategy that is defined in
1223 ;;; the standard-eval-closure and the standard-interface-eval-closure.
1225 ;;; - obarray: a hash table that maps symbols to variable objects. In this
1226 ;;; hash table, the definitions are found that are local to the module (that
1227 ;;; is, not imported from other modules). When looking up bindings in the
1228 ;;; module, this hash table is searched first.
1230 ;;; - binder: either #f or a function taking a module and a symbol argument.
1231 ;;; If it is a function it is called after the obarray has been
1232 ;;; unsuccessfully searched for a binding. It then can provide bindings
1233 ;;; that would otherwise not be found locally in the module.
1235 ;;; - uses: a list of modules from which non-local bindings can be inherited.
1236 ;;; These modules are the third place queried for bindings after the obarray
1237 ;;; has been unsuccessfully searched and the binder function did not deliver
1238 ;;; a result either.
1240 ;;; - transformer: either #f or a function taking a scheme expression as
1241 ;;; delivered by read. If it is a function, it will be called to perform
1242 ;;; syntax transformations (e. g. makro expansion) on the given scheme
1243 ;;; expression. The output of the transformer function will then be passed
1244 ;;; to Guile's internal memoizer. This means that the output must be valid
1245 ;;; scheme code. The only exception is, that the output may make use of the
1246 ;;; syntax extensions provided to identify the modules that a binding
1249 ;;; - name: the name of the module. This is used for all kinds of printing
1250 ;;; outputs. In certain places the module name also serves as a way of
1251 ;;; identification. When adding a module to the uses list of another
1252 ;;; module, it is made sure that the new uses list will not contain two
1253 ;;; modules of the same name.
1255 ;;; - kind: classification of the kind of module. The value is (currently?)
1256 ;;; only used for printing. It has no influence on how a module is treated.
1257 ;;; Currently the following values are used when setting the module kind:
1258 ;;; 'module, 'directory, 'interface, 'custom-interface. If no explicit kind
1259 ;;; is set, it defaults to 'module.
1261 ;;; - duplicates-handlers: a list of procedures that get called to make a
1262 ;;; choice between two duplicate bindings when name clashes occur. See the
1263 ;;; `duplicate-handlers' global variable below.
1265 ;;; - observers: a list of procedures that get called when the module is
1268 ;;; - weak-observers: a weak-key hash table of procedures that get called
1269 ;;; when the module is modified. See `module-observe-weak' for details.
1271 ;;; In addition, the module may (must?) contain a binding for
1272 ;;; `%module-public-interface'. This variable should be bound to a module
1273 ;;; representing the exported interface of a module. See the
1274 ;;; `module-public-interface' and `module-export!' procedures.
1276 ;;; !!! warning: The interface to lazy binder procedures is going
1277 ;;; to be changed in an incompatible way to permit all the basic
1278 ;;; module ops to be virtualized.
1280 ;;; (make-module size use-list lazy-binding-proc) => module
1281 ;;; module-{obarray,uses,binder}[|-set!]
1282 ;;; (module? obj) => [#t|#f]
1283 ;;; (module-locally-bound? module symbol) => [#t|#f]
1284 ;;; (module-bound? module symbol) => [#t|#f]
1285 ;;; (module-symbol-locally-interned? module symbol) => [#t|#f]
1286 ;;; (module-symbol-interned? module symbol) => [#t|#f]
1287 ;;; (module-local-variable module symbol) => [#<variable ...> | #f]
1288 ;;; (module-variable module symbol) => [#<variable ...> | #f]
1289 ;;; (module-symbol-binding module symbol opt-value)
1290 ;;; => [ <obj> | opt-value | an error occurs ]
1291 ;;; (module-make-local-var! module symbol) => #<variable...>
1292 ;;; (module-add! module symbol var) => unspecified
1293 ;;; (module-remove! module symbol) => unspecified
1294 ;;; (module-for-each proc module) => unspecified
1295 ;;; (make-scm-module) => module ; a lazy copy of the symhash module
1296 ;;; (set-current-module module) => unspecified
1297 ;;; (current-module) => #<module...>
1303 ;;; {Printing Modules}
1306 ;; This is how modules are printed. You can re-define it.
1307 ;; (Redefining is actually more complicated than simply redefining
1308 ;; %print-module because that would only change the binding and not
1309 ;; the value stored in the vtable that determines how record are
1312 (define (%print-module mod port) ; unused args: depth length style table)
1314 (display (or (module-kind mod) "module") port)
1316 (display (module-name mod) port)
1318 (display (number->string (object-address mod) 16) port)
1323 ;; A module is characterized by an obarray in which local symbols
1324 ;; are interned, a list of modules, "uses", from which non-local
1325 ;; bindings can be inherited, and an optional lazy-binder which
1326 ;; is a (CLOSURE module symbol) which, as a last resort, can provide
1327 ;; bindings that would otherwise not be found locally in the module.
1329 ;; NOTE: If you change anything here, you also need to change
1330 ;; libguile/modules.h.
1333 (make-record-type 'module
1334 '(obarray uses binder eval-closure transformer name kind
1335 duplicates-handlers import-obarray
1336 observers weak-observers)
1339 ;; make-module &opt size uses binder
1341 ;; Create a new module, perhaps with a particular size of obarray,
1342 ;; initial uses list, or binding procedure.
1347 (define (parse-arg index default)
1348 (if (> (length args) index)
1349 (list-ref args index)
1352 (define %default-import-size
1353 ;; Typical number of imported bindings actually used by a module.
1356 (if (> (length args) 3)
1357 (error "Too many args to make-module." args))
1359 (let ((size (parse-arg 0 31))
1360 (uses (parse-arg 1 '()))
1361 (binder (parse-arg 2 #f)))
1363 (if (not (integer? size))
1364 (error "Illegal size to make-module." size))
1365 (if (not (and (list? uses)
1366 (and-map module? uses)))
1367 (error "Incorrect use list." uses))
1368 (if (and binder (not (procedure? binder)))
1370 "Lazy-binder expected to be a procedure or #f." binder))
1372 (let ((module (module-constructor (make-hash-table size)
1373 uses binder #f %pre-modules-transformer
1375 (make-hash-table %default-import-size)
1377 (make-weak-key-hash-table 31))))
1379 ;; We can't pass this as an argument to module-constructor,
1380 ;; because we need it to close over a pointer to the module
1382 (set-module-eval-closure! module (standard-eval-closure module))
1386 (define module-constructor (record-constructor module-type))
1387 (define module-obarray (record-accessor module-type 'obarray))
1388 (define set-module-obarray! (record-modifier module-type 'obarray))
1389 (define module-uses (record-accessor module-type 'uses))
1390 (define set-module-uses! (record-modifier module-type 'uses))
1391 (define module-binder (record-accessor module-type 'binder))
1392 (define set-module-binder! (record-modifier module-type 'binder))
1394 ;; NOTE: This binding is used in libguile/modules.c.
1395 (define module-eval-closure (record-accessor module-type 'eval-closure))
1397 (define module-transformer (record-accessor module-type 'transformer))
1398 (define set-module-transformer! (record-modifier module-type 'transformer))
1399 ;; (define module-name (record-accessor module-type 'name)) wait until mods are booted
1400 (define set-module-name! (record-modifier module-type 'name))
1401 (define module-kind (record-accessor module-type 'kind))
1402 (define set-module-kind! (record-modifier module-type 'kind))
1403 (define module-duplicates-handlers
1404 (record-accessor module-type 'duplicates-handlers))
1405 (define set-module-duplicates-handlers!
1406 (record-modifier module-type 'duplicates-handlers))
1407 (define module-observers (record-accessor module-type 'observers))
1408 (define set-module-observers! (record-modifier module-type 'observers))
1409 (define module-weak-observers (record-accessor module-type 'weak-observers))
1410 (define module? (record-predicate module-type))
1412 (define module-import-obarray (record-accessor module-type 'import-obarray))
1414 (define set-module-eval-closure!
1415 (let ((setter (record-modifier module-type 'eval-closure)))
1416 (lambda (module closure)
1417 (setter module closure)
1418 ;; Make it possible to lookup the module from the environment.
1419 ;; This implementation is correct since an eval closure can belong
1420 ;; to maximally one module.
1422 ;; XXX: The following line introduces a circular reference that
1423 ;; precludes garbage collection of modules with the current weak hash
1424 ;; table semantics (see
1425 ;; http://lists.gnu.org/archive/html/guile-devel/2009-01/msg00102.html and
1426 ;; http://thread.gmane.org/gmane.comp.programming.garbage-collection.boehmgc/2465
1427 ;; for details). Since it doesn't appear to be used (only in
1428 ;; `scm_lookup_closure_module ()', which has 1 caller), we just comment
1431 ;(set-procedure-property! closure 'module module)
1436 ;;; {Observer protocol}
1439 (define (module-observe module proc)
1440 (set-module-observers! module (cons proc (module-observers module)))
1443 (define (module-observe-weak module observer-id . proc)
1444 ;; Register PROC as an observer of MODULE under name OBSERVER-ID (which can
1445 ;; be any Scheme object). PROC is invoked and passed MODULE any time
1446 ;; MODULE is modified. PROC gets unregistered when OBSERVER-ID gets GC'd
1447 ;; (thus, it is never unregistered if OBSERVER-ID is an immediate value,
1450 ;; The two-argument version is kept for backward compatibility: when called
1451 ;; with two arguments, the observer gets unregistered when closure PROC
1452 ;; gets GC'd (making it impossible to use an anonymous lambda for PROC).
1454 (let ((proc (if (null? proc) observer-id (car proc))))
1455 (hashq-set! (module-weak-observers module) observer-id proc)))
1457 (define (module-unobserve token)
1458 (let ((module (car token))
1461 (hash-remove! (module-weak-observers module) id)
1462 (set-module-observers! module (delq1! id (module-observers module)))))
1465 (define module-defer-observers #f)
1466 (define module-defer-observers-mutex (make-mutex 'recursive))
1467 (define module-defer-observers-table (make-hash-table))
1469 (define (module-modified m)
1470 (if module-defer-observers
1471 (hash-set! module-defer-observers-table m #t)
1472 (module-call-observers m)))
1474 ;;; This function can be used to delay calls to observers so that they
1475 ;;; can be called once only in the face of massive updating of modules.
1477 (define (call-with-deferred-observers thunk)
1480 (lock-mutex module-defer-observers-mutex)
1481 (set! module-defer-observers #t))
1484 (set! module-defer-observers #f)
1485 (hash-for-each (lambda (m dummy)
1486 (module-call-observers m))
1487 module-defer-observers-table)
1488 (hash-clear! module-defer-observers-table)
1489 (unlock-mutex module-defer-observers-mutex))))
1491 (define (module-call-observers m)
1492 (for-each (lambda (proc) (proc m)) (module-observers m))
1494 ;; We assume that weak observers don't (un)register themselves as they are
1495 ;; called since this would preclude proper iteration over the hash table
1497 (hash-for-each (lambda (id proc) (proc m)) (module-weak-observers m)))
1501 ;;; {Module Searching in General}
1503 ;;; We sometimes want to look for properties of a symbol
1504 ;;; just within the obarray of one module. If the property
1505 ;;; holds, then it is said to hold ``locally'' as in, ``The symbol
1506 ;;; DISPLAY is locally rebound in the module `safe-guile'.''
1509 ;;; Other times, we want to test for a symbol property in the obarray
1510 ;;; of M and, if it is not found there, try each of the modules in the
1511 ;;; uses list of M. This is the normal way of testing for some
1512 ;;; property, so we state these properties without qualification as
1513 ;;; in: ``The symbol 'fnord is interned in module M because it is
1514 ;;; interned locally in module M2 which is a member of the uses list
1518 ;; module-search fn m
1520 ;; return the first non-#f result of FN applied to M and then to
1521 ;; the modules in the uses of m, and so on recursively. If all applications
1522 ;; return #f, then so does this function.
1524 (define (module-search fn m v)
1527 (or (module-search fn (car pos) v)
1530 (loop (module-uses m))))
1533 ;;; {Is a symbol bound in a module?}
1535 ;;; Symbol S in Module M is bound if S is interned in M and if the binding
1536 ;;; of S in M has been set to some well-defined value.
1539 ;; module-locally-bound? module symbol
1541 ;; Is a symbol bound (interned and defined) locally in a given module?
1543 (define (module-locally-bound? m v)
1544 (let ((var (module-local-variable m v)))
1546 (variable-bound? var))))
1548 ;; module-bound? module symbol
1550 ;; Is a symbol bound (interned and defined) anywhere in a given module
1553 (define (module-bound? m v)
1554 (let ((var (module-variable m v)))
1556 (variable-bound? var))))
1558 ;;; {Is a symbol interned in a module?}
1560 ;;; Symbol S in Module M is interned if S occurs in
1561 ;;; of S in M has been set to some well-defined value.
1563 ;;; It is possible to intern a symbol in a module without providing
1564 ;;; an initial binding for the corresponding variable. This is done
1566 ;;; (module-add! module symbol (make-undefined-variable))
1568 ;;; In that case, the symbol is interned in the module, but not
1569 ;;; bound there. The unbound symbol shadows any binding for that
1570 ;;; symbol that might otherwise be inherited from a member of the uses list.
1573 (define (module-obarray-get-handle ob key)
1574 ((if (symbol? key) hashq-get-handle hash-get-handle) ob key))
1576 (define (module-obarray-ref ob key)
1577 ((if (symbol? key) hashq-ref hash-ref) ob key))
1579 (define (module-obarray-set! ob key val)
1580 ((if (symbol? key) hashq-set! hash-set!) ob key val))
1582 (define (module-obarray-remove! ob key)
1583 ((if (symbol? key) hashq-remove! hash-remove!) ob key))
1585 ;; module-symbol-locally-interned? module symbol
1587 ;; is a symbol interned (not neccessarily defined) locally in a given module
1588 ;; or its uses? Interned symbols shadow inherited bindings even if
1589 ;; they are not themselves bound to a defined value.
1591 (define (module-symbol-locally-interned? m v)
1592 (not (not (module-obarray-get-handle (module-obarray m) v))))
1594 ;; module-symbol-interned? module symbol
1596 ;; is a symbol interned (not neccessarily defined) anywhere in a given module
1597 ;; or its uses? Interned symbols shadow inherited bindings even if
1598 ;; they are not themselves bound to a defined value.
1600 (define (module-symbol-interned? m v)
1601 (module-search module-symbol-locally-interned? m v))
1604 ;;; {Mapping modules x symbols --> variables}
1607 ;; module-local-variable module symbol
1608 ;; return the local variable associated with a MODULE and SYMBOL.
1610 ;;; This function is very important. It is the only function that can
1611 ;;; return a variable from a module other than the mutators that store
1612 ;;; new variables in modules. Therefore, this function is the location
1613 ;;; of the "lazy binder" hack.
1615 ;;; If symbol is defined in MODULE, and if the definition binds symbol
1616 ;;; to a variable, return that variable object.
1618 ;;; If the symbols is not found at first, but the module has a lazy binder,
1619 ;;; then try the binder.
1621 ;;; If the symbol is not found at all, return #f.
1623 ;;; (This is now written in C, see `modules.c'.)
1626 ;;; {Mapping modules x symbols --> bindings}
1628 ;;; These are similar to the mapping to variables, except that the
1629 ;;; variable is dereferenced.
1632 ;; module-symbol-binding module symbol opt-value
1634 ;; return the binding of a variable specified by name within
1635 ;; a given module, signalling an error if the variable is unbound.
1636 ;; If the OPT-VALUE is passed, then instead of signalling an error,
1637 ;; return OPT-VALUE.
1639 (define (module-symbol-local-binding m v . opt-val)
1640 (let ((var (module-local-variable m v)))
1641 (if (and var (variable-bound? var))
1643 (if (not (null? opt-val))
1645 (error "Locally unbound variable." v)))))
1647 ;; module-symbol-binding module symbol opt-value
1649 ;; return the binding of a variable specified by name within
1650 ;; a given module, signalling an error if the variable is unbound.
1651 ;; If the OPT-VALUE is passed, then instead of signalling an error,
1652 ;; return OPT-VALUE.
1654 (define (module-symbol-binding m v . opt-val)
1655 (let ((var (module-variable m v)))
1656 (if (and var (variable-bound? var))
1658 (if (not (null? opt-val))
1660 (error "Unbound variable." v)))))
1665 ;;; {Adding Variables to Modules}
1668 ;; module-make-local-var! module symbol
1670 ;; ensure a variable for V in the local namespace of M.
1671 ;; If no variable was already there, then create a new and uninitialzied
1674 ;; This function is used in modules.c.
1676 (define (module-make-local-var! m v)
1677 (or (let ((b (module-obarray-ref (module-obarray m) v)))
1680 ;; Mark as modified since this function is called when
1681 ;; the standard eval closure defines a binding
1685 ;; Create a new local variable.
1686 (let ((local-var (make-undefined-variable)))
1687 (module-add! m v local-var)
1690 ;; module-ensure-local-variable! module symbol
1692 ;; Ensure that there is a local variable in MODULE for SYMBOL. If
1693 ;; there is no binding for SYMBOL, create a new uninitialized
1694 ;; variable. Return the local variable.
1696 (define (module-ensure-local-variable! module symbol)
1697 (or (module-local-variable module symbol)
1698 (let ((var (make-undefined-variable)))
1699 (module-add! module symbol var)
1702 ;; module-add! module symbol var
1704 ;; ensure a particular variable for V in the local namespace of M.
1706 (define (module-add! m v var)
1707 (if (not (variable? var))
1708 (error "Bad variable to module-add!" var))
1709 (module-obarray-set! (module-obarray m) v var)
1710 (module-modified m))
1714 ;; make sure that a symbol is undefined in the local namespace of M.
1716 (define (module-remove! m v)
1717 (module-obarray-remove! (module-obarray m) v)
1718 (module-modified m))
1720 (define (module-clear! m)
1721 (hash-clear! (module-obarray m))
1722 (module-modified m))
1724 ;; MODULE-FOR-EACH -- exported
1726 ;; Call PROC on each symbol in MODULE, with arguments of (SYMBOL VARIABLE).
1728 (define (module-for-each proc module)
1729 (hash-for-each proc (module-obarray module)))
1731 (define (module-map proc module)
1732 (hash-map->list proc (module-obarray module)))
1736 ;;; {Low Level Bootstrapping}
1741 ;; A root module uses the pre-modules-obarray as its obarray. This
1742 ;; special obarray accumulates all bindings that have been established
1743 ;; before the module system is fully booted.
1745 ;; (The obarray continues to be used by code that has been closed over
1746 ;; before the module system has been booted.)
1748 (define (make-root-module)
1749 (let ((m (make-module 0)))
1750 (set-module-obarray! m (%get-pre-modules-obarray))
1755 ;; The root interface is a module that uses the same obarray as the
1756 ;; root module. It does not allow new definitions, tho.
1758 (define (make-scm-module)
1759 (let ((m (make-module 0)))
1760 (set-module-obarray! m (%get-pre-modules-obarray))
1761 (set-module-eval-closure! m (standard-interface-eval-closure m))
1767 ;;; {Module-based Loading}
1770 (define (save-module-excursion thunk)
1771 (let ((inner-module (current-module))
1773 (dynamic-wind (lambda ()
1774 (set! outer-module (current-module))
1775 (set-current-module inner-module)
1776 (set! inner-module #f))
1779 (set! inner-module (current-module))
1780 (set-current-module outer-module)
1781 (set! outer-module #f)))))
1783 (define basic-load load)
1785 (define (load-module filename . reader)
1786 (save-module-excursion
1788 (let ((oldname (and (current-load-port)
1789 (port-filename (current-load-port)))))
1792 (> (string-length filename) 0)
1793 (not (char=? (string-ref filename 0) #\/))
1794 (not (string=? (dirname oldname) ".")))
1795 (string-append (dirname oldname) "/" filename)
1802 ;;; {MODULE-REF -- exported}
1805 ;; Returns the value of a variable called NAME in MODULE or any of its
1806 ;; used modules. If there is no such variable, then if the optional third
1807 ;; argument DEFAULT is present, it is returned; otherwise an error is signaled.
1809 (define (module-ref module name . rest)
1810 (let ((variable (module-variable module name)))
1811 (if (and variable (variable-bound? variable))
1812 (variable-ref variable)
1814 (error "No variable named" name 'in module)
1815 (car rest) ; default value
1818 ;; MODULE-SET! -- exported
1820 ;; Sets the variable called NAME in MODULE (or in a module that MODULE uses)
1821 ;; to VALUE; if there is no such variable, an error is signaled.
1823 (define (module-set! module name value)
1824 (let ((variable (module-variable module name)))
1826 (variable-set! variable value)
1827 (error "No variable named" name 'in module))))
1829 ;; MODULE-DEFINE! -- exported
1831 ;; Sets the variable called NAME in MODULE to VALUE; if there is no such
1832 ;; variable, it is added first.
1834 (define (module-define! module name value)
1835 (let ((variable (module-local-variable module name)))
1838 (variable-set! variable value)
1839 (module-modified module))
1840 (let ((variable (make-variable value)))
1841 (module-add! module name variable)))))
1843 ;; MODULE-DEFINED? -- exported
1845 ;; Return #t iff NAME is defined in MODULE (or in a module that MODULE
1848 (define (module-defined? module name)
1849 (let ((variable (module-variable module name)))
1850 (and variable (variable-bound? variable))))
1852 ;; MODULE-USE! module interface
1854 ;; Add INTERFACE to the list of interfaces used by MODULE.
1856 (define (module-use! module interface)
1857 (if (not (or (eq? module interface)
1858 (memq interface (module-uses module))))
1860 ;; Newly used modules must be appended rather than consed, so that
1861 ;; `module-variable' traverses the use list starting from the first
1863 (set-module-uses! module
1864 (append (filter (lambda (m)
1866 (equal? (module-name m)
1867 (module-name interface))))
1868 (module-uses module))
1871 (module-modified module))))
1873 ;; MODULE-USE-INTERFACES! module interfaces
1875 ;; Same as MODULE-USE! but add multiple interfaces and check for duplicates
1877 (define (module-use-interfaces! module interfaces)
1878 (set-module-uses! module
1879 (append (module-uses module) interfaces))
1880 (module-modified module))
1884 ;;; {Recursive Namespaces}
1886 ;;; A hierarchical namespace emerges if we consider some module to be
1887 ;;; root, and variables bound to modules as nested namespaces.
1889 ;;; The routines in this file manage variable names in hierarchical namespace.
1890 ;;; Each variable name is a list of elements, looked up in successively nested
1893 ;;; (nested-ref some-root-module '(foo bar baz))
1894 ;;; => <value of a variable named baz in the module bound to bar in
1895 ;;; the module bound to foo in some-root-module>
1900 ;;; ;; a-root is a module
1901 ;;; ;; name is a list of symbols
1903 ;;; nested-ref a-root name
1904 ;;; nested-set! a-root name val
1905 ;;; nested-define! a-root name val
1906 ;;; nested-remove! a-root name
1909 ;;; (current-module) is a natural choice for a-root so for convenience there are
1912 ;;; local-ref name == nested-ref (current-module) name
1913 ;;; local-set! name val == nested-set! (current-module) name val
1914 ;;; local-define! name val == nested-define! (current-module) name val
1915 ;;; local-remove! name == nested-remove! (current-module) name
1919 (define (nested-ref root names)
1920 (let loop ((cur root)
1924 ((not (module? cur)) #f)
1925 (else (loop (module-ref cur (car elts) #f) (cdr elts))))))
1927 (define (nested-set! root names val)
1928 (let loop ((cur root)
1930 (if (null? (cdr elts))
1931 (module-set! cur (car elts) val)
1932 (loop (module-ref cur (car elts)) (cdr elts)))))
1934 (define (nested-define! root names val)
1935 (let loop ((cur root)
1937 (if (null? (cdr elts))
1938 (module-define! cur (car elts) val)
1939 (loop (module-ref cur (car elts)) (cdr elts)))))
1941 (define (nested-remove! root names)
1942 (let loop ((cur root)
1944 (if (null? (cdr elts))
1945 (module-remove! cur (car elts))
1946 (loop (module-ref cur (car elts)) (cdr elts)))))
1948 (define (local-ref names) (nested-ref (current-module) names))
1949 (define (local-set! names val) (nested-set! (current-module) names val))
1950 (define (local-define names val) (nested-define! (current-module) names val))
1951 (define (local-remove names) (nested-remove! (current-module) names))
1956 ;;; {The (%app) module}
1958 ;;; The root of conventionally named objects not directly in the top level.
1961 ;;; (%app modules guile)
1963 ;;; The directory of all modules and the standard root module.
1966 ;; module-public-interface is defined in C.
1967 (define (set-module-public-interface! m i)
1968 (module-define! m '%module-public-interface i))
1969 (define (set-system-module! m s)
1970 (set-procedure-property! (module-eval-closure m) 'system-module s))
1971 (define the-root-module (make-root-module))
1972 (define the-scm-module (make-scm-module))
1973 (set-module-public-interface! the-root-module the-scm-module)
1974 (set-module-name! the-root-module '(guile))
1975 (set-module-name! the-scm-module '(guile))
1976 (set-module-kind! the-scm-module 'interface)
1977 (set-system-module! the-root-module #t)
1978 (set-system-module! the-scm-module #t)
1980 ;; NOTE: This binding is used in libguile/modules.c.
1982 (define (make-modules-in module name)
1986 (let* ((var (module-local-variable module (car name)))
1987 (val (and var (variable-bound? var) (variable-ref var))))
1990 (let ((m (make-module 31)))
1991 (set-module-kind! m 'directory)
1992 (set-module-name! m (append (module-name module)
1994 (module-define! module (car name) m)
1998 (define (beautify-user-module! module)
1999 (let ((interface (module-public-interface module)))
2000 (if (or (not interface)
2001 (eq? interface module))
2002 (let ((interface (make-module 31)))
2003 (set-module-name! interface (module-name module))
2004 (set-module-kind! interface 'interface)
2005 (set-module-public-interface! module interface))))
2006 (if (and (not (memq the-scm-module (module-uses module)))
2007 (not (eq? module the-root-module)))
2008 ;; Import the default set of bindings (from the SCM module) in MODULE.
2009 (module-use! module the-scm-module)))
2011 ;; NOTE: This binding is used in libguile/modules.c.
2013 (define resolve-module
2014 (let ((the-root-module the-root-module))
2015 (lambda (name . maybe-autoload)
2016 (if (equal? name '(guile))
2018 (let ((full-name (append '(%app modules) name)))
2019 (let ((already (nested-ref the-root-module full-name))
2020 (autoload (or (null? maybe-autoload) (car maybe-autoload))))
2022 ((and already (module? already)
2023 (or (not autoload) (module-public-interface already)))
2024 ;; A hit, a palpable hit.
2027 ;; Try to autoload the module, and recurse.
2028 (try-load-module name)
2029 (resolve-module name #f))
2031 ;; A module is not bound (but maybe something else is),
2032 ;; we're not autoloading -- here's the weird semantics,
2033 ;; we create an empty module.
2034 (make-modules-in the-root-module full-name)))))))))
2036 ;; Cheat. These bindings are needed by modules.c, but we don't want
2037 ;; to move their real definition here because that would be unnatural.
2039 (define try-module-autoload #f)
2040 (define process-define-module #f)
2041 (define process-use-modules #f)
2042 (define module-export! #f)
2043 (define default-duplicate-binding-procedures #f)
2045 (define %app (make-module 31))
2046 (set-module-name! %app '(%app))
2047 (define app %app) ;; for backwards compatability
2049 (let ((m (make-module 31)))
2050 (set-module-name! m '())
2051 (local-define '(%app modules) m))
2052 (local-define '(%app modules guile) the-root-module)
2054 ;; This boots the module system. All bindings needed by modules.c
2055 ;; must have been defined by now.
2057 (set-current-module the-root-module)
2058 ;; definition deferred for syncase's benefit.
2060 (let ((accessor (record-accessor module-type 'name)))
2063 (let ((name (list (gensym))))
2064 ;; Name MOD and bind it in THE-ROOT-MODULE so that it's visible
2065 ;; to `resolve-module'. This is important as `psyntax' stores
2066 ;; module names and relies on being able to `resolve-module'
2068 (set-module-name! mod name)
2069 (nested-define! the-root-module `(%app modules ,@name) mod)
2072 ;; (define-special-value '(%app modules new-ws) (lambda () (make-scm-module)))
2074 (define (try-load-module name)
2075 (try-module-autoload name))
2077 (define (purify-module! module)
2078 "Removes bindings in MODULE which are inherited from the (guile) module."
2079 (let ((use-list (module-uses module)))
2080 (if (and (pair? use-list)
2081 (eq? (car (last-pair use-list)) the-scm-module))
2082 (set-module-uses! module (reverse (cdr (reverse use-list)))))))
2084 ;; Return a module that is an interface to the module designated by
2087 ;; `resolve-interface' takes four keyword arguments:
2089 ;; #:select SELECTION
2091 ;; SELECTION is a list of binding-specs to be imported; A binding-spec
2092 ;; is either a symbol or a pair of symbols (ORIG . SEEN), where ORIG
2093 ;; is the name in the used module and SEEN is the name in the using
2094 ;; module. Note that SEEN is also passed through RENAMER, below. The
2095 ;; default is to select all bindings. If you specify no selection but
2096 ;; a renamer, only the bindings that already exist in the used module
2097 ;; are made available in the interface. Bindings that are added later
2098 ;; are not picked up.
2102 ;; BINDINGS is a list of bindings which should not be imported.
2106 ;; PREFIX is a symbol that will be appended to each exported name.
2107 ;; The default is to not perform any renaming.
2109 ;; #:renamer RENAMER
2111 ;; RENAMER is a procedure that takes a symbol and returns its new
2112 ;; name. The default is not perform any renaming.
2114 ;; Signal "no code for module" error if module name is not resolvable
2115 ;; or its public interface is not available. Signal "no binding"
2116 ;; error if selected binding does not exist in the used module.
2118 (define (resolve-interface name . args)
2120 (define (get-keyword-arg args kw def)
2121 (cond ((memq kw args)
2123 (if (null? (cdr kw-arg))
2124 (error "keyword without value: " kw))
2129 (let* ((select (get-keyword-arg args #:select #f))
2130 (hide (get-keyword-arg args #:hide '()))
2131 (renamer (or (get-keyword-arg args #:renamer #f)
2132 (let ((prefix (get-keyword-arg args #:prefix #f)))
2133 (and prefix (symbol-prefix-proc prefix)))
2135 (module (resolve-module name))
2136 (public-i (and module (module-public-interface module))))
2137 (and (or (not module) (not public-i))
2138 (error "no code for module" name))
2139 (if (and (not select) (null? hide) (eq? renamer identity))
2141 (let ((selection (or select (module-map (lambda (sym var) sym)
2143 (custom-i (make-module 31)))
2144 (set-module-kind! custom-i 'custom-interface)
2145 (set-module-name! custom-i name)
2146 ;; XXX - should use a lazy binder so that changes to the
2147 ;; used module are picked up automatically.
2148 (for-each (lambda (bspec)
2149 (let* ((direct? (symbol? bspec))
2150 (orig (if direct? bspec (car bspec)))
2151 (seen (if direct? bspec (cdr bspec)))
2152 (var (or (module-local-variable public-i orig)
2153 (module-local-variable module orig)
2155 ;; fixme: format manually for now
2157 #f "no binding `~A' in module ~A"
2159 (if (memq orig hide)
2160 (set! hide (delq! orig hide))
2161 (module-add! custom-i
2165 ;; Check that we are not hiding bindings which don't exist
2166 (for-each (lambda (binding)
2167 (if (not (module-local-variable public-i binding))
2170 #f "no binding `~A' to hide in module ~A"
2175 (define (symbol-prefix-proc prefix)
2177 (symbol-append prefix symbol)))
2179 ;; This function is called from "modules.c". If you change it, be
2180 ;; sure to update "modules.c" as well.
2182 (define (process-define-module args)
2183 (let* ((module-id (car args))
2184 (module (resolve-module module-id #f))
2186 (unrecognized (lambda (arg)
2187 (error "unrecognized define-module argument" arg))))
2188 (beautify-user-module! module)
2189 (let loop ((kws kws)
2190 (reversed-interfaces '())
2197 (call-with-deferred-observers
2199 (module-use-interfaces! module (reverse reversed-interfaces))
2200 (module-export! module exports)
2201 (module-replace! module replacements)
2202 (module-re-export! module re-exports)
2203 (if (not (null? autoloads))
2204 (apply module-autoload! module autoloads))))
2206 ((#:use-module #:use-syntax)
2207 (or (pair? (cdr kws))
2210 ((equal? (caadr kws) '(ice-9 syncase))
2211 (issue-deprecation-warning
2212 "(ice-9 syncase) is deprecated. Support for syntax-case is now in Guile core.")
2220 (let* ((interface-args (cadr kws))
2221 (interface (apply resolve-interface interface-args)))
2222 (and (eq? (car kws) #:use-syntax)
2223 (or (symbol? (caar interface-args))
2224 (error "invalid module name for use-syntax"
2225 (car interface-args)))
2226 (set-module-transformer!
2228 (module-ref interface
2229 (car (last-pair (car interface-args)))
2232 (cons interface reversed-interfaces)
2238 (or (and (pair? (cdr kws)) (pair? (cddr kws)))
2245 (let ((name (cadr kws))
2246 (bindings (caddr kws)))
2247 (cons* name bindings autoloads))))
2249 (set-system-module! module #t)
2250 (loop (cdr kws) reversed-interfaces exports re-exports
2251 replacements autoloads))
2253 (purify-module! module)
2254 (loop (cdr kws) reversed-interfaces exports re-exports
2255 replacements autoloads))
2257 (if (not (pair? (cdr kws)))
2259 (set-module-duplicates-handlers!
2261 (lookup-duplicates-handlers (cadr kws)))
2262 (loop (cddr kws) reversed-interfaces exports re-exports
2263 replacements autoloads))
2264 ((#:export #:export-syntax)
2265 (or (pair? (cdr kws))
2269 (append (cadr kws) exports)
2273 ((#:re-export #:re-export-syntax)
2274 (or (pair? (cdr kws))
2279 (append (cadr kws) re-exports)
2282 ((#:replace #:replace-syntax)
2283 (or (pair? (cdr kws))
2289 (append (cadr kws) replacements)
2292 (unrecognized kws)))))
2293 (run-hook module-defined-hook module)
2296 ;; `module-defined-hook' is a hook that is run whenever a new module
2297 ;; is defined. Its members are called with one argument, the new
2299 (define module-defined-hook (make-hook 1))
2306 (define (make-autoload-interface module name bindings)
2307 (let ((b (lambda (a sym definep)
2308 (and (memq sym bindings)
2309 (let ((i (module-public-interface (resolve-module name))))
2311 (error "missing interface for module" name))
2312 (let ((autoload (memq a (module-uses module))))
2313 ;; Replace autoload-interface with actual interface if
2314 ;; that has not happened yet.
2315 (if (pair? autoload)
2316 (set-car! autoload i)))
2317 (module-local-variable i sym))))))
2318 (module-constructor (make-hash-table 0) '() b #f #f name 'autoload #f
2319 (make-hash-table 0) '() (make-weak-value-hash-table 31))))
2321 (define (module-autoload! module . args)
2322 "Have @var{module} automatically load the module named @var{name} when one
2323 of the symbols listed in @var{bindings} is looked up. @var{args} should be a
2324 list of module-name/binding-list pairs, e.g., as in @code{(module-autoload!
2325 module '(ice-9 q) '(make-q q-length))}."
2326 (let loop ((args args))
2330 (error "invalid name+binding autoload list" args))
2332 (let ((name (car args))
2333 (bindings (cadr args)))
2334 (module-use! module (make-autoload-interface module
2336 (loop (cddr args)))))))
2341 ;;; {Autoloading modules}
2344 (define autoloads-in-progress '())
2346 ;; This function is called from "modules.c". If you change it, be
2347 ;; sure to update "modules.c" as well.
2349 (define (try-module-autoload module-name)
2350 (let* ((reverse-name (reverse module-name))
2351 (name (symbol->string (car reverse-name)))
2352 (dir-hint-module-name (reverse (cdr reverse-name)))
2353 (dir-hint (apply string-append
2355 (string-append (symbol->string elt) "/"))
2356 dir-hint-module-name))))
2357 (resolve-module dir-hint-module-name #f)
2358 (and (not (autoload-done-or-in-progress? dir-hint name))
2361 (lambda () (autoload-in-progress! dir-hint name))
2363 (with-fluid* current-reader #f
2365 (save-module-excursion
2367 (primitive-load-path (in-vicinity dir-hint name) #f)
2368 (set! didit #t))))))
2369 (lambda () (set-autoloaded! dir-hint name didit)))
2374 ;;; {Dynamic linking of modules}
2377 (define autoloads-done '((guile . guile)))
2379 (define (autoload-done-or-in-progress? p m)
2380 (let ((n (cons p m)))
2381 (->bool (or (member n autoloads-done)
2382 (member n autoloads-in-progress)))))
2384 (define (autoload-done! p m)
2385 (let ((n (cons p m)))
2386 (set! autoloads-in-progress
2387 (delete! n autoloads-in-progress))
2388 (or (member n autoloads-done)
2389 (set! autoloads-done (cons n autoloads-done)))))
2391 (define (autoload-in-progress! p m)
2392 (let ((n (cons p m)))
2393 (set! autoloads-done
2394 (delete! n autoloads-done))
2395 (set! autoloads-in-progress (cons n autoloads-in-progress))))
2397 (define (set-autoloaded! p m done?)
2399 (autoload-done! p m)
2400 (let ((n (cons p m)))
2401 (set! autoloads-done (delete! n autoloads-done))
2402 (set! autoloads-in-progress (delete! n autoloads-in-progress)))))
2406 ;;; {Run-time options}
2409 (defmacro define-option-interface (option-group)
2410 (let* ((option-name 'car)
2411 (option-value 'cadr)
2412 (option-documentation 'caddr)
2414 ;; Below follow the macros defining the run-time option interfaces.
2416 (make-options (lambda (interface)
2418 (cond ((null? args) (,interface))
2420 (,interface (car args)) (,interface))
2423 (display (,option-name option))
2424 (if (< (string-length
2425 (symbol->string (,option-name option)))
2429 (display (,option-value option))
2431 (display (,option-documentation option))
2433 (,interface #t)))))))
2435 (make-enable (lambda (interface)
2437 (,interface (append flags (,interface)))
2440 (make-disable (lambda (interface)
2442 (let ((options (,interface)))
2443 (for-each (lambda (flag)
2444 (set! options (delq! flag options)))
2446 (,interface options)
2448 (let* ((interface (car option-group))
2449 (options/enable/disable (cadr option-group)))
2451 (define ,(car options/enable/disable)
2452 ,(make-options interface))
2453 (define ,(cadr options/enable/disable)
2454 ,(make-enable interface))
2455 (define ,(caddr options/enable/disable)
2456 ,(make-disable interface))
2457 (defmacro ,(caaddr option-group) (opt val)
2458 `(,',(car options/enable/disable)
2459 (append (,',(car options/enable/disable))
2460 (list ',opt ,val))))))))
2462 (define-option-interface
2463 (eval-options-interface
2464 (eval-options eval-enable eval-disable)
2467 (define-option-interface
2468 (debug-options-interface
2469 (debug-options debug-enable debug-disable)
2472 (define-option-interface
2473 (evaluator-traps-interface
2474 (traps trap-enable trap-disable)
2477 (define-option-interface
2478 (read-options-interface
2479 (read-options read-enable read-disable)
2482 (define-option-interface
2483 (print-options-interface
2484 (print-options print-enable print-disable)
2492 (define (repl read evaler print)
2493 (let loop ((source (read (current-input-port))))
2494 (print (evaler source))
2495 (loop (read (current-input-port)))))
2497 ;; A provisional repl that acts like the SCM repl:
2499 (define scm-repl-silent #f)
2500 (define (assert-repl-silence v) (set! scm-repl-silent v))
2502 (define *unspecified* (if #f #f))
2503 (define (unspecified? v) (eq? v *unspecified*))
2505 (define scm-repl-print-unspecified #f)
2506 (define (assert-repl-print-unspecified v) (set! scm-repl-print-unspecified v))
2508 (define scm-repl-verbose #f)
2509 (define (assert-repl-verbosity v) (set! scm-repl-verbose v))
2511 (define scm-repl-prompt "guile> ")
2513 (define (set-repl-prompt! v) (set! scm-repl-prompt v))
2515 (define (default-pre-unwind-handler key . args)
2517 (apply throw key args))
2520 (define (pre-unwind-handler-dispatch key . args)
2521 (apply default-pre-unwind-handler key args)))
2523 (define abort-hook (make-hook))
2525 ;; these definitions are used if running a script.
2526 ;; otherwise redefined in error-catching-loop.
2527 (define (set-batch-mode?! arg) #t)
2528 (define (batch-mode?) #t)
2530 (define (error-catching-loop thunk)
2533 (define (loop first)
2538 (call-with-unblocked-asyncs
2544 ;; This line is needed because mark
2545 ;; doesn't do closures quite right.
2546 ;; Unreferenced locals should be
2549 (let loop ((v (thunk)))
2553 (lambda (key . args)
2560 (apply throw 'switch-repl args))
2563 ;; This is one of the closures that require
2564 ;; (set! first #f) above
2567 (run-hook abort-hook)
2568 (force-output (current-output-port))
2569 (display "ABORT: " (current-error-port))
2570 (write args (current-error-port))
2571 (newline (current-error-port))
2575 (not has-shown-debugger-hint?)
2576 (not (memq 'backtrace
2577 (debug-options-interface)))
2578 (stack? (fluid-ref the-last-stack)))
2580 (newline (current-error-port))
2582 "Type \"(backtrace)\" to get more information or \"(debug)\" to enter the debugger.\n"
2583 (current-error-port))
2584 (set! has-shown-debugger-hint? #t)))
2585 (force-output (current-error-port)))
2587 (primitive-exit 1)))
2588 (set! stack-saved? #f)))
2591 ;; This is the other cons-leak closure...
2593 (cond ((= (length args) 4)
2594 (apply handle-system-error key args))
2596 (apply bad-throw key args)))))))
2598 default-pre-unwind-handler)))
2600 (if next (loop next) status)))
2601 (set! set-batch-mode?! (lambda (arg)
2603 (set! interactive #f)
2606 (error "sorry, not implemented")))))
2607 (set! batch-mode? (lambda () (not interactive)))
2608 (call-with-blocked-asyncs
2609 (lambda () (loop (lambda () #t))))))
2611 ;;(define the-last-stack (make-fluid)) Defined by scm_init_backtrace ()
2612 (define before-signal-stack (make-fluid))
2613 (define stack-saved? #f)
2615 (define (save-stack . narrowing)
2617 (cond ((not (memq 'debug (debug-options-interface)))
2618 (fluid-set! the-last-stack #f)
2619 (set! stack-saved? #t))
2625 (apply make-stack #t save-stack primitive-eval #t 0 narrowing))
2627 (apply make-stack #t save-stack 0 #t 0 narrowing))
2629 (apply make-stack #t save-stack tk-stack-mark #t 0 narrowing))
2631 (apply make-stack #t save-stack 0 1 narrowing))
2633 (let ((id (stack-id #t)))
2634 (and (procedure? id)
2635 (apply make-stack #t save-stack id #t 0 narrowing))))))
2636 (set! stack-saved? #t)))))
2638 (define before-error-hook (make-hook))
2639 (define after-error-hook (make-hook))
2640 (define before-backtrace-hook (make-hook))
2641 (define after-backtrace-hook (make-hook))
2643 (define has-shown-debugger-hint? #f)
2645 (define (handle-system-error key . args)
2646 (let ((cep (current-error-port)))
2647 (cond ((not (stack? (fluid-ref the-last-stack))))
2648 ((memq 'backtrace (debug-options-interface))
2649 (let ((highlights (if (or (eq? key 'wrong-type-arg)
2650 (eq? key 'out-of-range))
2653 (run-hook before-backtrace-hook)
2655 (display "Backtrace:\n")
2656 (display-backtrace (fluid-ref the-last-stack) cep
2659 (run-hook after-backtrace-hook))))
2660 (run-hook before-error-hook)
2661 (apply display-error (fluid-ref the-last-stack) cep args)
2662 (run-hook after-error-hook)
2664 (throw 'abort key)))
2666 (define (quit . args)
2667 (apply throw 'quit args))
2671 ;;(define has-shown-backtrace-hint? #f) Defined by scm_init_backtrace ()
2673 ;; Replaced by C code:
2674 ;;(define (backtrace)
2675 ;; (if (fluid-ref the-last-stack)
2678 ;; (display-backtrace (fluid-ref the-last-stack) (current-output-port))
2680 ;; (if (and (not has-shown-backtrace-hint?)
2681 ;; (not (memq 'backtrace (debug-options-interface))))
2684 ;;"Type \"(debug-enable 'backtrace)\" if you would like a backtrace
2685 ;;automatically if an error occurs in the future.\n")
2686 ;; (set! has-shown-backtrace-hint? #t))))
2687 ;; (display "No backtrace available.\n")))
2689 (define (error-catching-repl r e p)
2690 (error-catching-loop
2692 (call-with-values (lambda () (e (r)))
2693 (lambda the-values (for-each p the-values))))))
2695 (define (gc-run-time)
2696 (cdr (assq 'gc-time-taken (gc-stats))))
2698 (define before-read-hook (make-hook))
2699 (define after-read-hook (make-hook))
2700 (define before-eval-hook (make-hook 1))
2701 (define after-eval-hook (make-hook 1))
2702 (define before-print-hook (make-hook 1))
2703 (define after-print-hook (make-hook 1))
2705 ;;; The default repl-reader function. We may override this if we've
2706 ;;; the readline library.
2708 (lambda (prompt . reader)
2709 (display (if (string? prompt) prompt (prompt)))
2711 (run-hook before-read-hook)
2712 ((or (and (pair? reader) (car reader))
2713 (fluid-ref current-reader)
2715 (current-input-port))))
2717 (define (scm-style-repl)
2722 (repl-report-start-timing (lambda ()
2723 (set! start-gc-rt (gc-run-time))
2724 (set! start-rt (get-internal-run-time))))
2725 (repl-report (lambda ()
2727 (display (inexact->exact
2728 (* 1000 (/ (- (get-internal-run-time) start-rt)
2729 internal-time-units-per-second))))
2731 (display (inexact->exact
2732 (* 1000 (/ (- (gc-run-time) start-gc-rt)
2733 internal-time-units-per-second))))
2734 (display " msec in gc)\n")))
2736 (consume-trailing-whitespace
2738 (let ((ch (peek-char)))
2741 ((or (char=? ch #\space) (char=? ch #\tab))
2743 (consume-trailing-whitespace))
2744 ((char=? ch #\newline)
2748 (let ((prompt (cond ((string? scm-repl-prompt)
2750 ((thunk? scm-repl-prompt)
2752 (scm-repl-prompt "> ")
2754 (repl-reader prompt))))
2756 ;; As described in R4RS, the READ procedure updates the
2757 ;; port to point to the first character past the end of
2758 ;; the external representation of the object. This
2759 ;; means that it doesn't consume the newline typically
2760 ;; found after an expression. This means that, when
2761 ;; debugging Guile with GDB, GDB gets the newline, which
2762 ;; it often interprets as a "continue" command, making
2763 ;; breakpoints kind of useless. So, consume any
2764 ;; trailing newline here, as well as any whitespace
2766 ;; But not if EOF, for control-D.
2767 (if (not (eof-object? val))
2768 (consume-trailing-whitespace))
2769 (run-hook after-read-hook)
2770 (if (eof-object? val)
2772 (repl-report-start-timing)
2773 (if scm-repl-verbose
2776 (display ";;; EOF -- quitting")
2781 (-eval (lambda (sourc)
2782 (repl-report-start-timing)
2783 (run-hook before-eval-hook sourc)
2784 (let ((val (start-stack 'repl-stack
2785 ;; If you change this procedure
2786 ;; (primitive-eval), please also
2787 ;; modify the repl-stack case in
2788 ;; save-stack so that stack cutting
2789 ;; continues to work.
2790 (primitive-eval sourc))))
2791 (run-hook after-eval-hook sourc)
2795 (-print (let ((maybe-print (lambda (result)
2796 (if (or scm-repl-print-unspecified
2797 (not (unspecified? result)))
2802 (if (not scm-repl-silent)
2804 (run-hook before-print-hook result)
2805 (maybe-print result)
2806 (run-hook after-print-hook result)
2807 (if scm-repl-verbose
2811 (-quit (lambda (args)
2812 (if scm-repl-verbose
2814 (display ";;; QUIT executed, repl exitting")
2819 (let ((status (error-catching-repl -read
2827 ;;; {IOTA functions: generating lists of numbers}
2831 (let loop ((count (1- n)) (result '()))
2832 (if (< count 0) result
2833 (loop (1- count) (cons count result)))))
2839 ;;; Similar to `begin' but returns a list of the results of all constituent
2840 ;;; forms instead of the result of the last form.
2841 ;;; (The definition relies on the current left-to-right
2842 ;;; order of evaluation of operands in applications.)
2845 (defmacro collect forms
2853 ;; with-fluids is a convenience wrapper for the builtin procedure
2854 ;; `with-fluids*'. The syntax is just like `let':
2856 ;; (with-fluids ((fluid val)
2860 (defmacro with-fluids (bindings . body)
2861 (let ((fluids (map car bindings))
2862 (values (map cadr bindings)))
2863 (if (and (= (length fluids) 1) (= (length values) 1))
2864 `(with-fluid* ,(car fluids) ,(car values) (lambda () ,@body))
2865 `(with-fluids* (list ,@fluids) (list ,@values)
2866 (lambda () ,@body)))))
2870 ;;; with `continue' and `break'.
2873 ;; The inner `do' loop avoids re-establishing a catch every iteration,
2874 ;; that's only necessary if continue is actually used. A new key is
2875 ;; generated every time, so break and continue apply to their originating
2876 ;; `while' even when recursing.
2878 ;; FIXME: This macro is unintentionally unhygienic with respect to let,
2879 ;; make-symbol, do, throw, catch, lambda, and not.
2881 (define-macro (while cond . body)
2882 (let ((keyvar (make-symbol "while-keyvar")))
2883 `(let ((,keyvar (make-symbol "while-key")))
2887 (let ((break (lambda () (throw ,keyvar #t)))
2888 (continue (lambda () (throw ,keyvar #f))))
2899 ;;; {Module System Macros}
2902 ;; Return a list of expressions that evaluate to the appropriate
2903 ;; arguments for resolve-interface according to SPEC.
2907 (if (memq 'prefix (read-options))
2908 (error "boot-9 must be compiled with #:kw, not :kw")))
2910 (define (compile-interface-spec spec)
2911 (define (make-keyarg sym key quote?)
2912 (cond ((or (memq sym spec)
2916 (list key (list 'quote (cadr rest)))
2917 (list key (cadr rest)))))
2920 (define (map-apply func list)
2921 (map (lambda (args) (apply func args)) list))
2924 '((:select #:select #t)
2926 (:prefix #:prefix #t)
2927 (:renamer #:renamer #f)))
2928 (if (not (pair? (car spec)))
2931 ,@(apply append (map-apply make-keyarg keys)))))
2933 (define (keyword-like-symbol->keyword sym)
2934 (symbol->keyword (string->symbol (substring (symbol->string sym) 1))))
2936 (define (compile-define-module-args args)
2937 ;; Just quote everything except #:use-module and #:use-syntax. We
2938 ;; need to know about all arguments regardless since we want to turn
2939 ;; symbols that look like keywords into real keywords, and the
2940 ;; keyword args in a define-module form are not regular
2941 ;; (i.e. no-backtrace doesn't take a value).
2942 (let loop ((compiled-args `((quote ,(car args))))
2945 (reverse! compiled-args))
2946 ;; symbol in keyword position
2947 ((symbol? (car args))
2949 (cons (keyword-like-symbol->keyword (car args)) (cdr args))))
2950 ((memq (car args) '(#:no-backtrace #:pure))
2951 (loop (cons (car args) compiled-args)
2954 (error "keyword without value:" (car args)))
2955 ((memq (car args) '(#:use-module #:use-syntax))
2956 (loop (cons* `(list ,@(compile-interface-spec (cadr args)))
2960 ((eq? (car args) #:autoload)
2961 (loop (cons* `(quote ,(caddr args))
2962 `(quote ,(cadr args))
2967 (loop (cons* `(quote ,(cadr args))
2972 (defmacro define-module args
2975 (let ((m (process-define-module
2976 (list ,@(compile-define-module-args args)))))
2977 (set-current-module m)
2980 ;; The guts of the use-modules macro. Add the interfaces of the named
2981 ;; modules to the use-list of the current module, in order.
2983 ;; This function is called by "modules.c". If you change it, be sure
2984 ;; to change scm_c_use_module as well.
2986 (define (process-use-modules module-interface-args)
2987 (let ((interfaces (map (lambda (mif-args)
2988 (or (apply resolve-interface mif-args)
2989 (error "no such module" mif-args)))
2990 module-interface-args)))
2991 (call-with-deferred-observers
2993 (module-use-interfaces! (current-module) interfaces)))))
2995 (defmacro use-modules modules
2998 (process-use-modules
2999 (list ,@(map (lambda (m)
3000 `(list ,@(compile-interface-spec m)))
3004 (defmacro use-syntax (spec)
3007 (issue-deprecation-warning
3008 "`use-syntax' is deprecated. Please contact guile-devel for more info.")
3009 (process-use-modules (list (list ,@(compile-interface-spec spec))))
3012 (define-syntax define-private
3017 (define-syntax define-public
3019 ((_ (name . args) . body)
3020 (define-public name (lambda args . body)))
3026 (define-syntax defmacro-public
3028 ((_ name args . body)
3030 (defmacro name args . body)
3031 (export-syntax name)))))
3033 ;; And now for the most important macro.
3036 ((_ formals body ...)
3037 (lambda formals body ...))))
3040 ;; Export a local variable
3042 ;; This function is called from "modules.c". If you change it, be
3043 ;; sure to update "modules.c" as well.
3045 (define (module-export! m names)
3046 (let ((public-i (module-public-interface m)))
3047 (for-each (lambda (name)
3048 (let ((var (module-ensure-local-variable! m name)))
3049 (module-add! public-i name var)))
3052 (define (module-replace! m names)
3053 (let ((public-i (module-public-interface m)))
3054 (for-each (lambda (name)
3055 (let ((var (module-ensure-local-variable! m name)))
3056 (set-object-property! var 'replace #t)
3057 (module-add! public-i name var)))
3060 ;; Re-export a imported variable
3062 (define (module-re-export! m names)
3063 (let ((public-i (module-public-interface m)))
3064 (for-each (lambda (name)
3065 (let ((var (module-variable m name)))
3067 (error "Undefined variable:" name))
3068 ((eq? var (module-local-variable m name))
3069 (error "re-exporting local variable:" name))
3071 (module-add! public-i name var)))))
3074 (defmacro export names
3075 `(call-with-deferred-observers
3077 (module-export! (current-module) ',names))))
3079 (defmacro re-export names
3080 `(call-with-deferred-observers
3082 (module-re-export! (current-module) ',names))))
3084 (defmacro export-syntax names
3087 (defmacro re-export-syntax names
3088 `(re-export ,@names))
3090 (define load load-module)
3097 (define make-mutable-parameter
3098 (let ((make (lambda (fluid converter)
3102 (fluid-set! fluid (converter (car args))))))))
3103 (lambda (init . converter)
3104 (let ((fluid (make-fluid))
3105 (converter (if (null? converter)
3108 (fluid-set! fluid (converter init))
3109 (make fluid converter)))))
3113 ;;; {Handling of duplicate imported bindings}
3116 ;; Duplicate handlers take the following arguments:
3118 ;; module importing module
3119 ;; name conflicting name
3120 ;; int1 old interface where name occurs
3121 ;; val1 value of binding in old interface
3122 ;; int2 new interface where name occurs
3123 ;; val2 value of binding in new interface
3124 ;; var previous resolution or #f
3125 ;; val value of previous resolution
3127 ;; A duplicate handler can take three alternative actions:
3129 ;; 1. return #f => leave responsibility to next handler
3130 ;; 2. exit with an error
3131 ;; 3. return a variable resolving the conflict
3134 (define duplicate-handlers
3135 (let ((m (make-module 7)))
3137 (define (check module name int1 val1 int2 val2 var val)
3138 (scm-error 'misc-error
3140 "~A: `~A' imported from both ~A and ~A"
3141 (list (module-name module)
3147 (define (warn module name int1 val1 int2 val2 var val)
3148 (format (current-error-port)
3149 "WARNING: ~A: `~A' imported from both ~A and ~A\n"
3150 (module-name module)
3156 (define (replace module name int1 val1 int2 val2 var val)
3157 (let ((old (or (and var (object-property var 'replace) var)
3158 (module-variable int1 name)))
3159 (new (module-variable int2 name)))
3160 (if (object-property old 'replace)
3161 (and (or (eq? old new)
3162 (not (object-property new 'replace)))
3164 (and (object-property new 'replace)
3167 (define (warn-override-core module name int1 val1 int2 val2 var val)
3168 (and (eq? int1 the-scm-module)
3170 (format (current-error-port)
3171 "WARNING: ~A: imported module ~A overrides core binding `~A'\n"
3172 (module-name module)
3175 (module-local-variable int2 name))))
3177 (define (first module name int1 val1 int2 val2 var val)
3178 (or var (module-local-variable int1 name)))
3180 (define (last module name int1 val1 int2 val2 var val)
3181 (module-local-variable int2 name))
3183 (define (noop module name int1 val1 int2 val2 var val)
3186 (set-module-name! m 'duplicate-handlers)
3187 (set-module-kind! m 'interface)
3188 (module-define! m 'check check)
3189 (module-define! m 'warn warn)
3190 (module-define! m 'replace replace)
3191 (module-define! m 'warn-override-core warn-override-core)
3192 (module-define! m 'first first)
3193 (module-define! m 'last last)
3194 (module-define! m 'merge-generics noop)
3195 (module-define! m 'merge-accessors noop)
3198 (define (lookup-duplicates-handlers handler-names)
3200 (map (lambda (handler-name)
3201 (or (module-symbol-local-binding
3202 duplicate-handlers handler-name #f)
3203 (error "invalid duplicate handler name:"
3205 (if (list? handler-names)
3207 (list handler-names)))))
3209 (define default-duplicate-binding-procedures
3210 (make-mutable-parameter #f))
3212 (define default-duplicate-binding-handler
3213 (make-mutable-parameter '(replace warn-override-core warn last)
3214 (lambda (handler-names)
3215 (default-duplicate-binding-procedures
3216 (lookup-duplicates-handlers handler-names))
3221 ;;; {`cond-expand' for SRFI-0 support.}
3223 ;;; This syntactic form expands into different commands or
3224 ;;; definitions, depending on the features provided by the Scheme
3230 ;;; --> (cond-expand <cond-expand-clause>+)
3231 ;;; | (cond-expand <cond-expand-clause>* (else <command-or-definition>))
3232 ;;; <cond-expand-clause>
3233 ;;; --> (<feature-requirement> <command-or-definition>*)
3234 ;;; <feature-requirement>
3235 ;;; --> <feature-identifier>
3236 ;;; | (and <feature-requirement>*)
3237 ;;; | (or <feature-requirement>*)
3238 ;;; | (not <feature-requirement>)
3239 ;;; <feature-identifier>
3240 ;;; --> <a symbol which is the name or alias of a SRFI>
3242 ;;; Additionally, this implementation provides the
3243 ;;; <feature-identifier>s `guile' and `r5rs', so that programs can
3244 ;;; determine the implementation type and the supported standard.
3246 ;;; Currently, the following feature identifiers are supported:
3248 ;;; guile r5rs srfi-0 srfi-4 srfi-6 srfi-13 srfi-14 srfi-55 srfi-61
3250 ;;; Remember to update the features list when adding more SRFIs.
3253 (define %cond-expand-features
3254 ;; Adjust the above comment when changing this.
3257 srfi-0 ;; cond-expand itself
3258 srfi-4 ;; homogenous numeric vectors
3259 srfi-6 ;; open-input-string etc, in the guile core
3260 srfi-13 ;; string library
3261 srfi-14 ;; character sets
3262 srfi-55 ;; require-extension
3263 srfi-61 ;; general cond clause
3266 ;; This table maps module public interfaces to the list of features.
3268 (define %cond-expand-table (make-hash-table 31))
3270 ;; Add one or more features to the `cond-expand' feature list of the
3273 (define (cond-expand-provide module features)
3274 (let ((mod (module-public-interface module)))
3276 (hashq-set! %cond-expand-table mod
3277 (append (hashq-ref %cond-expand-table mod '())
3280 (define-macro (cond-expand . clauses)
3281 (let ((syntax-error (lambda (cl)
3282 (error "invalid clause in `cond-expand'" cl))))
3288 (or (memq clause %cond-expand-features)
3289 (let lp ((uses (module-uses (current-module))))
3292 (hashq-ref %cond-expand-table
3298 ((eq? 'and (car clause))
3299 (let lp ((l (cdr clause)))
3303 (and (test-clause (car l)) (lp (cdr l))))
3305 (syntax-error clause)))))
3306 ((eq? 'or (car clause))
3307 (let lp ((l (cdr clause)))
3311 (or (test-clause (car l)) (lp (cdr l))))
3313 (syntax-error clause)))))
3314 ((eq? 'not (car clause))
3315 (cond ((not (pair? (cdr clause)))
3316 (syntax-error clause))
3317 ((pair? (cddr clause))
3318 ((syntax-error clause))))
3319 (not (test-clause (cadr clause))))
3321 (syntax-error clause))))
3323 (syntax-error clause))))))
3324 (let lp ((c clauses))
3327 (error "Unfulfilled `cond-expand'"))
3330 ((not (pair? (car c)))
3331 (syntax-error (car c)))
3332 ((test-clause (caar c))
3333 `(begin ,@(cdar c)))
3334 ((eq? (caar c) 'else)
3337 `(begin ,@(cdar c)))
3341 ;; This procedure gets called from the startup code with a list of
3342 ;; numbers, which are the numbers of the SRFIs to be loaded on startup.
3344 (define (use-srfis srfis)
3345 (process-use-modules
3347 (list (list 'srfi (string->symbol
3348 (string-append "srfi-" (number->string num))))))
3353 ;;; srfi-55: require-extension
3356 (define-macro (require-extension extension-spec)
3357 ;; This macro only handles the srfi extension, which, at present, is
3358 ;; the only one defined by the standard.
3359 (if (not (pair? extension-spec))
3360 (scm-error 'wrong-type-arg "require-extension"
3361 "Not an extension: ~S" (list extension-spec) #f))
3362 (let ((extension (car extension-spec))
3363 (extension-args (cdr extension-spec)))
3366 (let ((use-list '()))
3369 (if (not (integer? i))
3370 (scm-error 'wrong-type-arg "require-extension"
3371 "Invalid srfi name: ~S" (list i) #f))
3372 (let ((srfi-sym (string->symbol
3373 (string-append "srfi-" (number->string i)))))
3374 (if (not (memq srfi-sym %cond-expand-features))
3375 (set! use-list (cons `(use-modules (srfi ,srfi-sym))
3378 (if (pair? use-list)
3379 ;; i.e. (begin (use-modules x) (use-modules y) (use-modules z))
3380 `(begin ,@(reverse! use-list)))))
3383 'wrong-type-arg "require-extension"
3384 "Not a recognized extension type: ~S" (list extension) #f)))))
3388 ;;; {Load emacs interface support if emacs option is given.}
3391 (define (named-module-use! user usee)
3392 (module-use! (resolve-module user) (resolve-interface usee)))
3394 (define (load-emacs-interface)
3395 (and (provided? 'debug-extensions)
3396 (debug-enable 'backtrace))
3397 (named-module-use! '(guile-user) '(ice-9 emacs)))
3401 (define using-readline?
3402 (let ((using-readline? (make-fluid)))
3403 (make-procedure-with-setter
3404 (lambda () (fluid-ref using-readline?))
3405 (lambda (v) (fluid-set! using-readline? v)))))
3408 (let ((guile-user-module (resolve-module '(guile-user))))
3410 ;; Load emacs interface support if emacs option is given.
3411 (if (and (module-defined? guile-user-module 'use-emacs-interface)
3412 (module-ref guile-user-module 'use-emacs-interface))
3413 (load-emacs-interface))
3415 ;; Use some convenient modules (in reverse order)
3417 (set-current-module guile-user-module)
3418 (process-use-modules
3423 (if (provided? 'regex)
3426 (if (provided? 'threads)
3427 '(((ice-9 threads)))
3429 ;; load debugger on demand
3430 (module-autoload! guile-user-module '(ice-9 debugger) '(debug))
3432 ;; Note: SIGFPE, SIGSEGV and SIGBUS are actually "query-only" (see
3433 ;; scmsigs.c scm_sigaction_for_thread), so the handlers setup here have
3435 (let ((old-handlers #f)
3436 (start-repl (module-ref (resolve-interface '(system repl repl))
3438 (signals (if (provided? 'posix)
3439 `((,SIGINT . "User interrupt")
3440 (,SIGFPE . "Arithmetic error")
3442 . "Bad memory access (Segmentation violation)"))
3444 ;; no SIGBUS on mingw
3445 (if (defined? 'SIGBUS)
3446 (set! signals (acons SIGBUS "Bad memory access (bus error)"
3453 (let ((make-handler (lambda (msg)
3455 ;; Make a backup copy of the stack
3456 (fluid-set! before-signal-stack
3457 (fluid-ref the-last-stack))
3465 (map (lambda (sig-msg)
3466 (sigaction (car sig-msg)
3467 (make-handler (cdr sig-msg))))
3470 ;; the protected thunk.
3472 (let ((status (start-repl 'scheme)))
3473 (run-hook exit-hook)
3478 (map (lambda (sig-msg old-handler)
3479 (if (not (car old-handler))
3480 ;; restore original C handler.
3481 (sigaction (car sig-msg) #f)
3482 ;; restore Scheme handler, SIG_IGN or SIG_DFL.
3483 (sigaction (car sig-msg)
3485 (cdr old-handler))))
3486 signals old-handlers))))))
3488 ;;; This hook is run at the very end of an interactive session.
3490 (define exit-hook (make-hook))
3494 ;;; {Deprecated stuff}
3498 (define (feature? sym)
3499 (issue-deprecation-warning
3500 "`feature?' is deprecated. Use `provided?' instead.")
3504 (primitive-load-path "ice-9/deprecated"))
3508 ;;; Place the user in the guile-user module.
3511 ;;; FIXME: annotate ?
3512 ;; (define (syncase exp)
3513 ;; (with-fluids ((expansion-eval-closure
3514 ;; (module-eval-closure (current-module))))
3515 ;; (deannotate/source-properties (sc-expand (annotate exp)))))
3517 (define-module (guile-user)
3518 #:autoload (system base compile) (compile))
3520 ;;; boot-9.scm ends here