1 ;;; -*- mode: scheme; coding: utf-8; -*-
3 ;;;; Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003,2004,2005,2006,2007,2008,2009,2010
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))))
48 ;; Define delimited continuation operators, and implement catch and throw in
51 (define make-prompt-tag
52 (lambda* (#:optional (stem "prompt"))
55 (define default-prompt-tag
56 ;; not sure if we should expose this to the user as a fluid
57 (let ((%default-prompt-tag (make-prompt-tag)))
59 %default-prompt-tag)))
61 (define (call-with-prompt tag thunk handler)
62 (@prompt tag (thunk) handler))
63 (define (abort-to-prompt tag . args)
67 ;; Define catch and with-throw-handler, using some common helper routines and a
68 ;; shared fluid. Hide the helpers in a lexical contour.
71 ;; Ideally we'd like to be able to give these default values for all threads,
72 ;; even threads not created by Guile; but alack, that does not currently seem
73 ;; possible. So wrap the getters in thunks.
74 (define %running-exception-handlers (make-fluid))
75 (define %exception-handler (make-fluid))
77 (define (running-exception-handlers)
78 (or (fluid-ref %running-exception-handlers)
80 (fluid-set! %running-exception-handlers '())
82 (define (exception-handler)
83 (or (fluid-ref %exception-handler)
85 (fluid-set! %exception-handler default-exception-handler)
86 default-exception-handler)))
88 (define (default-exception-handler k . args)
92 ((not (pair? args)) 0)
93 ((integer? (car args)) (car args))
97 (format (current-error-port) "guile: uncaught throw to ~a: ~a\n" k args)
100 (define (default-throw-handler prompt-tag catch-k)
101 (let ((prev (exception-handler)))
102 (lambda (thrown-k . args)
103 (if (or (eq? thrown-k catch-k) (eqv? catch-k #t))
104 (apply abort-to-prompt prompt-tag thrown-k args)
105 (apply prev thrown-k args)))))
107 (define (custom-throw-handler prompt-tag catch-k pre)
108 (let ((prev (exception-handler)))
109 (lambda (thrown-k . args)
110 (if (or (eq? thrown-k catch-k) (eqv? catch-k #t))
111 (let ((running (running-exception-handlers)))
112 (with-fluids ((%running-exception-handlers (cons pre running)))
113 (if (not (memq pre running))
114 (apply pre thrown-k args))
117 (apply abort-to-prompt prompt-tag thrown-k args)
118 (apply prev thrown-k args))))
119 (apply prev thrown-k args)))))
122 (lambda* (k thunk handler #:optional pre-unwind-handler)
123 "Invoke @var{thunk} in the dynamic context of @var{handler} for
124 exceptions matching @var{key}. If thunk throws to the symbol
125 @var{key}, then @var{handler} is invoked this way:
127 (handler key args ...)
130 @var{key} is a symbol or @code{#t}.
132 @var{thunk} takes no arguments. If @var{thunk} returns
133 normally, that is the return value of @code{catch}.
135 Handler is invoked outside the scope of its own @code{catch}.
136 If @var{handler} again throws to the same key, a new handler
137 from further up the call chain is invoked.
139 If the key is @code{#t}, then a throw to @emph{any} symbol will
140 match this call to @code{catch}.
142 If a @var{pre-unwind-handler} is given and @var{thunk} throws
143 an exception that matches @var{key}, Guile calls the
144 @var{pre-unwind-handler} before unwinding the dynamic state and
145 invoking the main @var{handler}. @var{pre-unwind-handler} should
146 be a procedure with the same signature as @var{handler}, that
147 is @code{(lambda (key . args))}. It is typically used to save
148 the stack at the point where the exception occurred, but can also
149 query other parts of the dynamic state at that point, such as
152 A @var{pre-unwind-handler} can exit either normally or non-locally.
153 If it exits normally, Guile unwinds the stack and dynamic context
154 and then calls the normal (third argument) handler. If it exits
155 non-locally, that exit determines the continuation."
156 (if (not (or (symbol? k) (eqv? k #t)))
157 (scm-error "catch" 'wrong-type-arg
158 "Wrong type argument in position ~a: ~a"
159 (list 1 k) (list k)))
160 (let ((tag (make-prompt-tag "catch")))
166 (if pre-unwind-handler
167 (custom-throw-handler tag k pre-unwind-handler)
168 (default-throw-handler tag k))))
170 (lambda (cont k . args)
171 (apply handler k args))))))
173 (define! 'with-throw-handler
174 (lambda (k thunk pre-unwind-handler)
175 "Add @var{handler} to the dynamic context as a throw handler
176 for key @var{key}, then invoke @var{thunk}."
177 (if (not (or (symbol? k) (eqv? k #t)))
178 (scm-error "with-throw-handler" 'wrong-type-arg
179 "Wrong type argument in position ~a: ~a"
180 (list 1 k) (list k)))
181 (with-fluids ((%exception-handler
182 (custom-throw-handler #f k pre-unwind-handler)))
187 "Invoke the catch form matching @var{key}, passing @var{args} to the
190 @var{key} is a symbol. It will match catches of the same symbol or of @code{#t}.
192 If there is no handler at all, Guile prints an error and then exits."
193 (if (not (symbol? key))
194 ((exception-handler) 'wrong-type-arg "throw"
195 "Wrong type argument in position ~a: ~a" (list 1 key) (list key))
196 (apply (exception-handler) key args)))))
201 ;;; {R4RS compliance}
204 (primitive-load-path "ice-9/r4rs")
208 ;;; {Simple Debugging Tools}
211 ;; peek takes any number of arguments, writes them to the
212 ;; current ouput port, and returns the last argument.
213 ;; It is handy to wrap around an expression to look at
214 ;; a value each time is evaluated, e.g.:
216 ;; (+ 10 (troublesome-fn))
217 ;; => (+ 10 (pk 'troublesome-fn-returned (troublesome-fn)))
220 (define (peek . stuff)
225 (car (last-pair stuff)))
230 (define (warn . stuff)
231 (with-output-to-port (current-error-port)
234 (display ";;; WARNING ")
237 (car (last-pair stuff)))))
244 (define (provide sym)
245 (if (not (memq sym *features*))
246 (set! *features* (cons sym *features*))))
248 ;; Return #t iff FEATURE is available to this Guile interpreter. In SLIB,
249 ;; provided? also checks to see if the module is available. We should do that
252 (define (provided? feature)
253 (and (memq feature *features*) #t))
257 ;;; {and-map and or-map}
259 ;;; (and-map fn lst) is like (and (fn (car lst)) (fn (cadr lst)) (fn...) ...)
260 ;;; (or-map fn lst) is like (or (fn (car lst)) (fn (cadr lst)) (fn...) ...)
265 ;; Apply f to successive elements of l until exhaustion or f returns #f.
266 ;; If returning early, return #f. Otherwise, return the last value returned
267 ;; by f. If f has never been called because l is empty, return #t.
269 (define (and-map f lst)
270 (let loop ((result #t)
275 (loop (f (car l)) (cdr l))))))
279 ;; Apply f to successive elements of l until exhaustion or while f returns #f.
280 ;; If returning early, return the return value of f.
282 (define (or-map f lst)
283 (let loop ((result #f)
287 (loop (f (car l)) (cdr l))))))
291 ;; let format alias simple-format until the more complete version is loaded
293 (define format simple-format)
295 ;; this is scheme wrapping the C code so the final pred call is a tail call,
297 (define (string-any char_pred s . rest)
298 (let ((start (if (null? rest)
300 (end (if (or (null? rest) (null? (cdr rest)))
301 (string-length s) (cadr rest))))
302 (if (and (procedure? char_pred)
304 (<= end (string-length s))) ;; let c-code handle range error
305 (or (string-any-c-code char_pred s start (1- end))
306 (char_pred (string-ref s (1- end))))
307 (string-any-c-code char_pred s start end))))
309 ;; this is scheme wrapping the C code so the final pred call is a tail call,
311 (define (string-every char_pred s . rest)
312 (let ((start (if (null? rest)
314 (end (if (or (null? rest) (null? (cdr rest)))
315 (string-length s) (cadr rest))))
316 (if (and (procedure? char_pred)
318 (<= end (string-length s))) ;; let c-code handle range error
319 (and (string-every-c-code char_pred s start (1- end))
320 (char_pred (string-ref s (1- end))))
321 (string-every-c-code char_pred s start end))))
323 ;; A variant of string-fill! that we keep for compatability
325 (define (substring-fill! str start end fill)
326 (string-fill! str fill start end))
330 ;; Define a minimal stub of the module API for psyntax, before modules
332 (define (module-name x)
334 (define (module-define! module sym val)
335 (let ((v (hashq-ref (%get-pre-modules-obarray) sym)))
337 (variable-set! v val)
338 (hashq-set! (%get-pre-modules-obarray) sym
339 (make-variable val)))))
340 (define (module-ref module sym)
341 (let ((v (module-variable module sym)))
342 (if v (variable-ref v) (error "badness!" (pk module) (pk sym)))))
343 (define (resolve-module . args)
346 ;; Input hook to syncase -- so that we might be able to pass annotated
347 ;; expressions in. Currently disabled. Maybe we should just use
348 ;; source-properties directly.
349 (define (annotation? x) #f)
351 ;; API provided by psyntax
352 (define syntax-violation #f)
353 (define datum->syntax #f)
354 (define syntax->datum #f)
355 (define identifier? #f)
356 (define generate-temporaries #f)
357 (define bound-identifier=? #f)
358 (define free-identifier=? #f)
360 ;; $sc-dispatch is an implementation detail of psyntax. It is used by
361 ;; expanded macros, to dispatch an input against a set of patterns.
362 (define $sc-dispatch #f)
365 (primitive-load-path "ice-9/psyntax-pp")
366 ;; The binding for `macroexpand' has now been overridden, making psyntax the
373 ((_ x y ...) (if x (and y ...) #f))))
379 ((_ x y ...) (let ((t x)) (if t t (or y ...))))))
381 ;; The "maybe-more" bits are something of a hack, so that we can support
382 ;; SRFI-61. Rewrites into a standalone syntax-case macro would be
385 (syntax-rules (=> else)
386 ((_ "maybe-more" test consequent)
387 (if test consequent))
389 ((_ "maybe-more" test consequent clause ...)
390 (if test consequent (cond clause ...)))
392 ((_ (else else1 else2 ...))
393 (begin else1 else2 ...))
395 ((_ (test => receiver) more-clause ...)
397 (cond "maybe-more" t (receiver t) more-clause ...)))
399 ((_ (generator guard => receiver) more-clause ...)
400 (call-with-values (lambda () generator)
403 (apply guard t) (apply receiver t) more-clause ...))))
405 ((_ (test => receiver ...) more-clause ...)
406 (syntax-violation 'cond "wrong number of receiver expressions"
407 '(test => receiver ...)))
408 ((_ (generator guard => receiver ...) more-clause ...)
409 (syntax-violation 'cond "wrong number of receiver expressions"
410 '(generator guard => receiver ...)))
412 ((_ (test) more-clause ...)
414 (cond "maybe-more" t t more-clause ...)))
416 ((_ (test body1 body2 ...) more-clause ...)
418 test (begin body1 body2 ...) more-clause ...))))
424 (let ((atom-key (key ...)))
425 (case atom-key clauses ...)))
427 (else result1 result2 ...))
428 (begin result1 result2 ...))
430 ((atoms ...) result1 result2 ...))
431 (if (memv key '(atoms ...))
432 (begin result1 result2 ...)))
434 ((atoms ...) result1 result2 ...)
436 (if (memv key '(atoms ...))
437 (begin result1 result2 ...)
438 (case key clause clauses ...)))))
442 ((do ((var init step ...) ...)
455 (loop (do "step" var step ...)
465 ((_ exp) (make-promise (lambda () exp)))))
467 (include-from-path "ice-9/quasisyntax")
474 (define-syntax define-macro
478 ((_ (macro . args) doc body1 body ...)
479 (string? (syntax->datum #'doc))
480 #'(define-macro macro doc (lambda args body1 body ...)))
481 ((_ (macro . args) body ...)
482 #'(define-macro macro #f (lambda args body ...)))
483 ((_ macro doc transformer)
484 (or (string? (syntax->datum #'doc))
485 (not (syntax->datum #'doc)))
486 #'(define-syntax macro
489 #((macro-type . defmacro)
490 (defmacro-args args))
493 (let ((v (syntax->datum #'args)))
494 (datum->syntax y (apply transformer v)))))))))))
496 (define-syntax defmacro
498 "Define a defmacro, with the old lispy defun syntax."
500 ((_ macro args doc body1 body ...)
501 (string? (syntax->datum #'doc))
502 #'(define-macro macro doc (lambda args body1 body ...)))
503 ((_ macro args body ...)
504 #'(define-macro macro #f (lambda args body ...))))))
512 ;;; Depends on: defmacro
515 (defmacro begin-deprecated forms
516 (if (include-deprecated-features)
522 ;;; {Trivial Functions}
525 (define (identity x) x)
526 (define (and=> value procedure) (and value (procedure value)))
527 (define call/cc call-with-current-continuation)
529 ;;; apply-to-args is functionally redundant with apply and, worse,
530 ;;; is less general than apply since it only takes two arguments.
532 ;;; On the other hand, apply-to-args is a syntacticly convenient way to
533 ;;; perform binding in many circumstances when the "let" family of
534 ;;; of forms don't cut it. E.g.:
536 ;;; (apply-to-args (return-3d-mouse-coords)
541 (define (apply-to-args args fn) (apply fn args))
543 (defmacro false-if-exception (expr)
546 ;; avoid saving backtraces inside false-if-exception
547 (with-fluids ((the-last-stack (fluid-ref the-last-stack)))
553 ;;; {General Properties}
556 ;; This is a more modern interface to properties. It will replace all
557 ;; other property-like things eventually.
559 (define (make-object-property)
560 (let ((prop (primitive-make-property #f)))
561 (make-procedure-with-setter
562 (lambda (obj) (primitive-property-ref prop obj))
563 (lambda (obj val) (primitive-property-set! prop obj val)))))
567 ;;; {Symbol Properties}
570 (define (symbol-property sym prop)
571 (let ((pair (assoc prop (symbol-pref sym))))
572 (and pair (cdr pair))))
574 (define (set-symbol-property! sym prop val)
575 (let ((pair (assoc prop (symbol-pref sym))))
578 (symbol-pset! sym (acons prop val (symbol-pref sym))))))
580 (define (symbol-property-remove! sym prop)
581 (let ((pair (assoc prop (symbol-pref sym))))
583 (symbol-pset! sym (delq! pair (symbol-pref sym))))))
590 (define (array-shape a)
591 (map (lambda (ind) (if (number? ind) (list 0 (+ -1 ind)) ind))
592 (array-dimensions a)))
599 (define (kw-arg-ref args kw)
600 (let ((rem (member kw args)))
601 (and rem (pair? (cdr rem)) (cadr rem))))
608 (define (struct-layout s)
609 (struct-ref (struct-vtable s) vtable-index-layout))
616 ;; Printing records: by default, records are printed as
618 ;; #<type-name field1: val1 field2: val2 ...>
620 ;; You can change that by giving a custom printing function to
621 ;; MAKE-RECORD-TYPE (after the list of field symbols). This function
622 ;; will be called like
624 ;; (<printer> object port)
626 ;; It should print OBJECT to PORT.
628 (define (inherit-print-state old-port new-port)
629 (if (get-print-state old-port)
630 (port-with-print-state new-port (get-print-state old-port))
633 ;; 0: type-name, 1: fields, 2: constructor
634 (define record-type-vtable
635 ;; FIXME: This should just call make-vtable, not make-vtable-vtable; but for
636 ;; that we need to expose the bare vtable-vtable to Scheme.
637 (make-vtable-vtable "prprpw" 0
639 (cond ((eq? s record-type-vtable)
640 (display "#<record-type-vtable>" p))
642 (display "#<record-type " p)
643 (display (record-type-name s) p)
646 (define (record-type? obj)
647 (and (struct? obj) (eq? record-type-vtable (struct-vtable obj))))
649 (define (make-record-type type-name fields . opt)
650 ;; Pre-generate constructors for nfields < 20.
651 (define-syntax make-constructor
653 (define *max-static-argument-count* 20)
654 (define (make-formals n)
660 (string (integer->char (+ (char->integer #\a) i)))))
664 ((_ rtd exp) (not (identifier? #'exp))
666 (make-constructor rtd n)))
670 (if (< n *max-static-argument-count*)
671 (cons (with-syntax (((formal ...) (make-formals n))
675 (make-struct rtd 0 formal ...))))
680 (if (= (length args) nfields)
681 (apply make-struct rtd 0 args)
682 (scm-error 'wrong-number-of-args
683 (format #f "make-~a" type-name)
684 "Wrong number of arguments" '() #f)))))))))
686 (define (default-record-printer s p)
688 (display (record-type-name (record-type-descriptor s)) p)
689 (let loop ((fields (record-type-fields (record-type-descriptor s)))
692 ((not (null? fields))
694 (display (car fields) p)
696 (display (struct-ref s off) p)
697 (loop (cdr fields) (+ 1 off)))))
700 (let ((rtd (make-struct record-type-vtable 0
703 (map (lambda (f) "pw") fields)))
704 (or (and (pair? opt) (car opt))
705 default-record-printer)
707 (copy-tree fields))))
708 (struct-set! rtd (+ vtable-offset-user 2)
709 (make-constructor rtd (length fields)))
710 ;; Temporary solution: Associate a name to the record type descriptor
711 ;; so that the object system can create a wrapper class for it.
712 (set-struct-vtable-name! rtd (if (symbol? type-name)
714 (string->symbol type-name)))
717 (define (record-type-name obj)
718 (if (record-type? obj)
719 (struct-ref obj vtable-offset-user)
720 (error 'not-a-record-type obj)))
722 (define (record-type-fields obj)
723 (if (record-type? obj)
724 (struct-ref obj (+ 1 vtable-offset-user))
725 (error 'not-a-record-type obj)))
727 (define (record-constructor rtd . opt)
729 (struct-ref rtd (+ 2 vtable-offset-user))
730 (let ((field-names (car opt)))
732 `(lambda ,field-names
733 (make-struct ',rtd 0 ,@(map (lambda (f)
734 (if (memq f field-names)
737 (record-type-fields rtd))))))))
739 (define (record-predicate rtd)
740 (lambda (obj) (and (struct? obj) (eq? rtd (struct-vtable obj)))))
742 (define (%record-type-error rtd obj) ;; private helper
743 (or (eq? rtd (record-type-descriptor obj))
744 (scm-error 'wrong-type-arg "%record-type-check"
745 "Wrong type record (want `~S'): ~S"
746 (list (record-type-name rtd) obj)
749 (define (record-accessor rtd field-name)
750 (let ((pos (list-index (record-type-fields rtd) field-name)))
752 (error 'no-such-field field-name))
754 (if (eq? (struct-vtable obj) rtd)
756 (%record-type-error rtd obj)))))
758 (define (record-modifier rtd field-name)
759 (let ((pos (list-index (record-type-fields rtd) field-name)))
761 (error 'no-such-field field-name))
763 (if (eq? (struct-vtable obj) rtd)
764 (struct-set! obj pos val)
765 (%record-type-error rtd obj)))))
767 (define (record? obj)
768 (and (struct? obj) (record-type? (struct-vtable obj))))
770 (define (record-type-descriptor obj)
773 (error 'not-a-record obj)))
782 (define (->bool x) (not (not x)))
789 (define (symbol-append . args)
790 (string->symbol (apply string-append (map symbol->string args))))
792 (define (list->symbol . args)
793 (string->symbol (apply list->string args)))
795 (define (symbol . args)
796 (string->symbol (apply string args)))
803 (define (list-index l k)
809 (loop (+ n 1) (cdr l))))))
813 (if (provided? 'posix)
814 (primitive-load-path "ice-9/posix"))
816 (if (provided? 'socket)
817 (primitive-load-path "ice-9/networking"))
819 ;; For reference, Emacs file-exists-p uses stat in this same way.
821 (if (provided? 'posix)
823 (->bool (stat str #f)))
825 (let ((port (catch 'system-error (lambda () (open-file str OPEN_READ))
827 (if port (begin (close-port port) #t)
830 (define file-is-directory?
831 (if (provided? 'posix)
833 (eq? (stat:type (stat str)) 'directory))
835 (let ((port (catch 'system-error
836 (lambda () (open-file (string-append str "/.")
839 (if port (begin (close-port port) #t)
842 (define (has-suffix? str suffix)
843 (string-suffix? suffix str))
845 (define (system-error-errno args)
846 (if (eq? (car args) 'system-error)
847 (car (list-ref args 4))
855 (define (error . args)
858 (scm-error 'misc-error #f "?" #f #f)
859 (let loop ((msg "~A")
861 (if (not (null? rest))
862 (loop (string-append msg " ~S")
864 (scm-error 'misc-error #f msg args #f)))))
866 ;; bad-throw is the hook that is called upon a throw to a an unhandled
867 ;; key (unless the throw has four arguments, in which case
868 ;; it's usually interpreted as an error throw.)
869 ;; If the key has a default handler (a throw-handler-default property),
870 ;; it is applied to the throw.
872 (define (bad-throw key . args)
873 (let ((default (symbol-property key 'throw-handler-default)))
874 (or (and default (apply default key args))
875 (apply error "unhandled-exception:" key args))))
879 (define (tm:sec obj) (vector-ref obj 0))
880 (define (tm:min obj) (vector-ref obj 1))
881 (define (tm:hour obj) (vector-ref obj 2))
882 (define (tm:mday obj) (vector-ref obj 3))
883 (define (tm:mon obj) (vector-ref obj 4))
884 (define (tm:year obj) (vector-ref obj 5))
885 (define (tm:wday obj) (vector-ref obj 6))
886 (define (tm:yday obj) (vector-ref obj 7))
887 (define (tm:isdst obj) (vector-ref obj 8))
888 (define (tm:gmtoff obj) (vector-ref obj 9))
889 (define (tm:zone obj) (vector-ref obj 10))
891 (define (set-tm:sec obj val) (vector-set! obj 0 val))
892 (define (set-tm:min obj val) (vector-set! obj 1 val))
893 (define (set-tm:hour obj val) (vector-set! obj 2 val))
894 (define (set-tm:mday obj val) (vector-set! obj 3 val))
895 (define (set-tm:mon obj val) (vector-set! obj 4 val))
896 (define (set-tm:year obj val) (vector-set! obj 5 val))
897 (define (set-tm:wday obj val) (vector-set! obj 6 val))
898 (define (set-tm:yday obj val) (vector-set! obj 7 val))
899 (define (set-tm:isdst obj val) (vector-set! obj 8 val))
900 (define (set-tm:gmtoff obj val) (vector-set! obj 9 val))
901 (define (set-tm:zone obj val) (vector-set! obj 10 val))
903 (define (tms:clock obj) (vector-ref obj 0))
904 (define (tms:utime obj) (vector-ref obj 1))
905 (define (tms:stime obj) (vector-ref obj 2))
906 (define (tms:cutime obj) (vector-ref obj 3))
907 (define (tms:cstime obj) (vector-ref obj 4))
909 (define file-position ftell)
910 (define (file-set-position port offset . whence)
911 (let ((whence (if (eq? whence '()) SEEK_SET (car whence))))
912 (seek port offset whence)))
914 (define (move->fdes fd/port fd)
915 (cond ((integer? fd/port)
916 (dup->fdes fd/port fd)
920 (primitive-move->fdes fd/port fd)
921 (set-port-revealed! fd/port 1)
924 (define (release-port-handle port)
925 (let ((revealed (port-revealed port)))
927 (set-port-revealed! port (- revealed 1)))))
929 (define (dup->port port/fd mode . maybe-fd)
930 (let ((port (fdopen (apply dup->fdes port/fd maybe-fd)
933 (set-port-revealed! port 1))
936 (define (dup->inport port/fd . maybe-fd)
937 (apply dup->port port/fd "r" maybe-fd))
939 (define (dup->outport port/fd . maybe-fd)
940 (apply dup->port port/fd "w" maybe-fd))
942 (define (dup port/fd . maybe-fd)
943 (if (integer? port/fd)
944 (apply dup->fdes port/fd maybe-fd)
945 (apply dup->port port/fd (port-mode port/fd) maybe-fd)))
947 (define (duplicate-port port modes)
948 (dup->port port modes))
950 (define (fdes->inport fdes)
951 (let loop ((rest-ports (fdes->ports fdes)))
952 (cond ((null? rest-ports)
953 (let ((result (fdopen fdes "r")))
954 (set-port-revealed! result 1)
956 ((input-port? (car rest-ports))
957 (set-port-revealed! (car rest-ports)
958 (+ (port-revealed (car rest-ports)) 1))
961 (loop (cdr rest-ports))))))
963 (define (fdes->outport fdes)
964 (let loop ((rest-ports (fdes->ports fdes)))
965 (cond ((null? rest-ports)
966 (let ((result (fdopen fdes "w")))
967 (set-port-revealed! result 1)
969 ((output-port? (car rest-ports))
970 (set-port-revealed! (car rest-ports)
971 (+ (port-revealed (car rest-ports)) 1))
974 (loop (cdr rest-ports))))))
976 (define (port->fdes port)
977 (set-port-revealed! port (+ (port-revealed port) 1))
980 (define (setenv name value)
982 (putenv (string-append name "=" value))
985 (define (unsetenv name)
986 "Remove the entry for NAME from the environment."
994 ;;; Here for backward compatability
996 (define scheme-file-suffix (lambda () ".scm"))
998 (define (in-vicinity vicinity file)
999 (let ((tail (let ((len (string-length vicinity)))
1002 (string-ref vicinity (- len 1))))))
1003 (string-append vicinity
1012 ;;; {Help for scm_shell}
1014 ;;; The argument-processing code used by Guile-based shells generates
1015 ;;; Scheme code based on the argument list. This page contains help
1016 ;;; functions for the code it generates.
1019 (define (command-line) (program-arguments))
1021 ;; This is mostly for the internal use of the code generated by
1022 ;; scm_compile_shell_switches.
1024 (define (turn-on-debugging)
1025 (debug-enable 'debug)
1026 (debug-enable 'backtrace)
1027 (read-enable 'positions))
1029 (define (load-user-init)
1030 (let* ((home (or (getenv "HOME")
1031 (false-if-exception (passwd:dir (getpwuid (getuid))))
1032 "/")) ;; fallback for cygwin etc.
1033 (init-file (in-vicinity home ".guile")))
1034 (if (file-exists? init-file)
1035 (primitive-load init-file))))
1039 ;;; {The interpreter stack}
1042 ;; %stacks defined in stacks.c
1043 (define (%start-stack tag thunk)
1044 (let ((prompt-tag (make-prompt-tag "start-stack")))
1048 (with-fluids ((%stacks (acons tag prompt-tag
1049 (or (fluid-ref %stacks) '()))))
1052 (%start-stack tag (lambda () (apply k args)))))))
1053 (define-syntax start-stack
1056 (%start-stack tag (lambda () exp)))))
1060 ;;; {Loading by paths}
1063 ;;; Load a Scheme source file named NAME, searching for it in the
1064 ;;; directories listed in %load-path, and applying each of the file
1065 ;;; name extensions listed in %load-extensions.
1066 (define (load-from-path name)
1067 (start-stack 'load-stack
1068 (primitive-load-path name)))
1070 (define %load-verbosely #f)
1071 (define (assert-load-verbosity v) (set! %load-verbosely v))
1073 (define (%load-announce file)
1075 (with-output-to-port (current-error-port)
1078 (display "loading ")
1083 (set! %load-hook %load-announce)
1085 (define (load name . reader)
1086 ;; Returns the .go file corresponding to `name'. Does not search load
1087 ;; paths, only the fallback path. If the .go file is missing or out of
1088 ;; date, and autocompilation is enabled, will try autocompilation, just
1089 ;; as primitive-load-path does internally. primitive-load is
1090 ;; unaffected. Returns #f if autocompilation failed or was disabled.
1092 ;; NB: Unless we need to compile the file, this function should not cause
1093 ;; (system base compile) to be loaded up. For that reason compiled-file-name
1094 ;; partially duplicates functionality from (system base compile).
1095 (define (compiled-file-name canon-path)
1096 (and %compile-fallback-path
1098 %compile-fallback-path
1099 ;; no need for '/' separator here, canon-path is absolute
1101 (cond ((or (null? %load-compiled-extensions)
1102 (string-null? (car %load-compiled-extensions)))
1103 (warn "invalid %load-compiled-extensions"
1104 %load-compiled-extensions)
1106 (else (car %load-compiled-extensions))))))
1107 (define (fresh-compiled-file-name go-path)
1110 (let* ((scmstat (stat name))
1111 (gostat (stat go-path #f)))
1112 (if (and gostat (= (stat:mtime gostat) (stat:mtime scmstat)))
1116 (format (current-error-port)
1117 ";;; note: source file ~a\n;;; newer than compiled ~a\n"
1120 (%load-should-autocompile
1121 (%warn-autocompilation-enabled)
1122 (format (current-error-port) ";;; compiling ~a\n" name)
1123 (let ((cfn ((@ (system base compile) compile-file) name
1124 #:env (current-module))))
1125 (format (current-error-port) ";;; compiled ~a\n" cfn)
1129 (format (current-error-port)
1130 ";;; WARNING: compilation of ~a failed:\n;;; key ~a, throw_args ~s\n"
1133 (with-fluids ((current-reader (and (pair? reader) (car reader))))
1134 (let ((cfn (and=> (and=> (false-if-exception (canonicalize-path name))
1136 fresh-compiled-file-name)))
1139 (start-stack 'load-stack
1140 (primitive-load name))))))
1144 ;;; {Reader Extensions}
1146 ;;; Reader code for various "#c" forms.
1149 (define read-eval? (make-fluid))
1150 (fluid-set! read-eval? #f)
1151 (read-hash-extend #\.
1153 (if (fluid-ref read-eval?)
1154 (eval (read port) (interaction-environment))
1156 "#. read expansion found and read-eval? is #f."))))
1160 ;;; {Command Line Options}
1163 (define (get-option argv kw-opts kw-args return)
1166 (return #f #f argv))
1168 ((or (not (eq? #\- (string-ref (car argv) 0)))
1169 (eq? (string-length (car argv)) 1))
1170 (return 'normal-arg (car argv) (cdr argv)))
1172 ((eq? #\- (string-ref (car argv) 1))
1173 (let* ((kw-arg-pos (or (string-index (car argv) #\=)
1174 (string-length (car argv))))
1175 (kw (symbol->keyword (substring (car argv) 2 kw-arg-pos)))
1176 (kw-opt? (member kw kw-opts))
1177 (kw-arg? (member kw kw-args))
1178 (arg (or (and (not (eq? kw-arg-pos (string-length (car argv))))
1179 (substring (car argv)
1181 (string-length (car argv))))
1183 (begin (set! argv (cdr argv)) (car argv))))))
1184 (if (or kw-opt? kw-arg?)
1185 (return kw arg (cdr argv))
1186 (return 'usage-error kw (cdr argv)))))
1189 (let* ((char (substring (car argv) 1 2))
1190 (kw (symbol->keyword char)))
1193 ((member kw kw-opts)
1194 (let* ((rest-car (substring (car argv) 2 (string-length (car argv))))
1195 (new-argv (if (= 0 (string-length rest-car))
1197 (cons (string-append "-" rest-car) (cdr argv)))))
1198 (return kw #f new-argv)))
1200 ((member kw kw-args)
1201 (let* ((rest-car (substring (car argv) 2 (string-length (car argv))))
1202 (arg (if (= 0 (string-length rest-car))
1205 (new-argv (if (= 0 (string-length rest-car))
1208 (return kw arg new-argv)))
1210 (else (return 'usage-error kw argv)))))))
1212 (define (for-next-option proc argv kw-opts kw-args)
1213 (let loop ((argv argv))
1214 (get-option argv kw-opts kw-args
1215 (lambda (opt opt-arg argv)
1216 (and opt (proc opt opt-arg argv loop))))))
1218 (define (display-usage-report kw-desc)
1221 (or (eq? (car kw) #t)
1222 (eq? (car kw) 'else)
1223 (let* ((opt-desc kw)
1224 (help (cadr opt-desc))
1225 (opts (car opt-desc))
1226 (opts-proper (if (string? (car opts)) (cdr opts) opts))
1227 (arg-name (if (string? (car opts))
1228 (string-append "<" (car opts) ">")
1230 (left-part (string-append
1231 (with-output-to-string
1233 (map (lambda (x) (display (keyword->symbol x)) (display " "))
1236 (middle-part (if (and (< (string-length left-part) 30)
1237 (< (string-length help) 40))
1238 (make-string (- 30 (string-length left-part)) #\ )
1241 (display middle-part)
1248 (define (transform-usage-lambda cases)
1249 (let* ((raw-usage (delq! 'else (map car cases)))
1250 (usage-sans-specials (map (lambda (x)
1251 (or (and (not (list? x)) x)
1252 (and (symbol? (car x)) #t)
1253 (and (boolean? (car x)) #t)
1256 (usage-desc (delq! #t usage-sans-specials))
1257 (kw-desc (map car usage-desc))
1258 (kw-opts (apply append (map (lambda (x) (and (not (string? (car x))) x)) kw-desc)))
1259 (kw-args (apply append (map (lambda (x) (and (string? (car x)) (cdr x))) kw-desc)))
1260 (transmogrified-cases (map (lambda (case)
1261 (cons (let ((opts (car case)))
1262 (if (or (boolean? opts) (eq? 'else opts))
1265 ((symbol? (car opts)) opts)
1266 ((boolean? (car opts)) opts)
1267 ((string? (caar opts)) (cdar opts))
1268 (else (car opts)))))
1271 `(let ((%display-usage (lambda () (display-usage-report ',usage-desc))))
1273 (let %next-arg ((%argv %argv))
1277 (lambda (%opt %arg %new-argv)
1279 ,@ transmogrified-cases))))))))
1284 ;;; {Low Level Modules}
1286 ;;; These are the low level data structures for modules.
1288 ;;; Every module object is of the type 'module-type', which is a record
1289 ;;; consisting of the following members:
1291 ;;; - eval-closure: the function that defines for its module the strategy that
1292 ;;; shall be followed when looking up symbols in the module.
1294 ;;; An eval-closure is a function taking two arguments: the symbol to be
1295 ;;; looked up and a boolean value telling whether a binding for the symbol
1296 ;;; should be created if it does not exist yet. If the symbol lookup
1297 ;;; succeeded (either because an existing binding was found or because a new
1298 ;;; binding was created), a variable object representing the binding is
1299 ;;; returned. Otherwise, the value #f is returned. Note that the eval
1300 ;;; closure does not take the module to be searched as an argument: During
1301 ;;; construction of the eval-closure, the eval-closure has to store the
1302 ;;; module it belongs to in its environment. This means, that any
1303 ;;; eval-closure can belong to only one module.
1305 ;;; The eval-closure of a module can be defined arbitrarily. However, three
1306 ;;; special cases of eval-closures are to be distinguished: During startup
1307 ;;; the module system is not yet activated. In this phase, no modules are
1308 ;;; defined and all bindings are automatically stored by the system in the
1309 ;;; pre-modules-obarray. Since no eval-closures exist at this time, the
1310 ;;; functions which require an eval-closure as their argument need to be
1311 ;;; passed the value #f.
1313 ;;; The other two special cases of eval-closures are the
1314 ;;; standard-eval-closure and the standard-interface-eval-closure. Both
1315 ;;; behave equally for the case that no new binding is to be created. The
1316 ;;; difference between the two comes in, when the boolean argument to the
1317 ;;; eval-closure indicates that a new binding shall be created if it is not
1320 ;;; Given that no new binding shall be created, both standard eval-closures
1321 ;;; define the following standard strategy of searching bindings in the
1322 ;;; module: First, the module's obarray is searched for the symbol. Second,
1323 ;;; if no binding for the symbol was found in the module's obarray, the
1324 ;;; module's binder procedure is exececuted. If this procedure did not
1325 ;;; return a binding for the symbol, the modules referenced in the module's
1326 ;;; uses list are recursively searched for a binding of the symbol. If the
1327 ;;; binding can not be found in these modules also, the symbol lookup has
1330 ;;; If a new binding shall be created, the standard-interface-eval-closure
1331 ;;; immediately returns indicating failure. That is, it does not even try
1332 ;;; to look up the symbol. In contrast, the standard-eval-closure would
1333 ;;; first search the obarray, and if no binding was found there, would
1334 ;;; create a new binding in the obarray, therefore not calling the binder
1335 ;;; procedure or searching the modules in the uses list.
1337 ;;; The explanation of the following members obarray, binder and uses
1338 ;;; assumes that the symbol lookup follows the strategy that is defined in
1339 ;;; the standard-eval-closure and the standard-interface-eval-closure.
1341 ;;; - obarray: a hash table that maps symbols to variable objects. In this
1342 ;;; hash table, the definitions are found that are local to the module (that
1343 ;;; is, not imported from other modules). When looking up bindings in the
1344 ;;; module, this hash table is searched first.
1346 ;;; - binder: either #f or a function taking a module and a symbol argument.
1347 ;;; If it is a function it is called after the obarray has been
1348 ;;; unsuccessfully searched for a binding. It then can provide bindings
1349 ;;; that would otherwise not be found locally in the module.
1351 ;;; - uses: a list of modules from which non-local bindings can be inherited.
1352 ;;; These modules are the third place queried for bindings after the obarray
1353 ;;; has been unsuccessfully searched and the binder function did not deliver
1354 ;;; a result either.
1356 ;;; - transformer: either #f or a function taking a scheme expression as
1357 ;;; delivered by read. If it is a function, it will be called to perform
1358 ;;; syntax transformations (e. g. makro expansion) on the given scheme
1359 ;;; expression. The output of the transformer function will then be passed
1360 ;;; to Guile's internal memoizer. This means that the output must be valid
1361 ;;; scheme code. The only exception is, that the output may make use of the
1362 ;;; syntax extensions provided to identify the modules that a binding
1365 ;;; - name: the name of the module. This is used for all kinds of printing
1366 ;;; outputs. In certain places the module name also serves as a way of
1367 ;;; identification. When adding a module to the uses list of another
1368 ;;; module, it is made sure that the new uses list will not contain two
1369 ;;; modules of the same name.
1371 ;;; - kind: classification of the kind of module. The value is (currently?)
1372 ;;; only used for printing. It has no influence on how a module is treated.
1373 ;;; Currently the following values are used when setting the module kind:
1374 ;;; 'module, 'directory, 'interface, 'custom-interface. If no explicit kind
1375 ;;; is set, it defaults to 'module.
1377 ;;; - duplicates-handlers: a list of procedures that get called to make a
1378 ;;; choice between two duplicate bindings when name clashes occur. See the
1379 ;;; `duplicate-handlers' global variable below.
1381 ;;; - observers: a list of procedures that get called when the module is
1384 ;;; - weak-observers: a weak-key hash table of procedures that get called
1385 ;;; when the module is modified. See `module-observe-weak' for details.
1387 ;;; In addition, the module may (must?) contain a binding for
1388 ;;; `%module-public-interface'. This variable should be bound to a module
1389 ;;; representing the exported interface of a module. See the
1390 ;;; `module-public-interface' and `module-export!' procedures.
1392 ;;; !!! warning: The interface to lazy binder procedures is going
1393 ;;; to be changed in an incompatible way to permit all the basic
1394 ;;; module ops to be virtualized.
1396 ;;; (make-module size use-list lazy-binding-proc) => module
1397 ;;; module-{obarray,uses,binder}[|-set!]
1398 ;;; (module? obj) => [#t|#f]
1399 ;;; (module-locally-bound? module symbol) => [#t|#f]
1400 ;;; (module-bound? module symbol) => [#t|#f]
1401 ;;; (module-symbol-locally-interned? module symbol) => [#t|#f]
1402 ;;; (module-symbol-interned? module symbol) => [#t|#f]
1403 ;;; (module-local-variable module symbol) => [#<variable ...> | #f]
1404 ;;; (module-variable module symbol) => [#<variable ...> | #f]
1405 ;;; (module-symbol-binding module symbol opt-value)
1406 ;;; => [ <obj> | opt-value | an error occurs ]
1407 ;;; (module-make-local-var! module symbol) => #<variable...>
1408 ;;; (module-add! module symbol var) => unspecified
1409 ;;; (module-remove! module symbol) => unspecified
1410 ;;; (module-for-each proc module) => unspecified
1411 ;;; (make-scm-module) => module ; a lazy copy of the symhash module
1412 ;;; (set-current-module module) => unspecified
1413 ;;; (current-module) => #<module...>
1419 ;;; {Printing Modules}
1422 ;; This is how modules are printed. You can re-define it.
1423 (define (%print-module mod port)
1425 (display (or (module-kind mod) "module") port)
1427 (display (module-name mod) port)
1429 (display (number->string (object-address mod) 16) port)
1433 ;; Locally extend the syntax to allow record accessors to be defined at
1434 ;; compile-time. Cache the rtd locally to the constructor, the getters and
1435 ;; the setters, in order to allow for redefinition of the record type; not
1436 ;; relevant in the case of modules, but perhaps if we make this public, it
1439 ((define-record-type
1441 (define (make-id scope . fragments)
1442 (datum->syntax #'scope
1443 (apply symbol-append
1445 (if (symbol? x) x (syntax->datum x)))
1448 (define (getter rtd type-name field slot)
1449 #`(define #,(make-id rtd type-name '- field)
1451 (lambda (#,type-name)
1452 (if (eq? (struct-vtable #,type-name) rtd)
1453 (struct-ref #,type-name #,slot)
1454 (%record-type-error rtd #,type-name))))))
1456 (define (setter rtd type-name field slot)
1457 #`(define #,(make-id rtd 'set- type-name '- field '!)
1459 (lambda (#,type-name val)
1460 (if (eq? (struct-vtable #,type-name) rtd)
1461 (struct-set! #,type-name #,slot val)
1462 (%record-type-error rtd #,type-name))))))
1464 (define (accessors rtd type-name fields n exp)
1465 (syntax-case fields ()
1467 (((field #:no-accessors) field* ...) (identifier? #'field)
1468 (accessors rtd type-name #'(field* ...) (1+ n)
1470 (((field #:no-setter) field* ...) (identifier? #'field)
1471 (accessors rtd type-name #'(field* ...) (1+ n)
1473 #,(getter rtd type-name #'field n))))
1474 (((field #:no-getter) field* ...) (identifier? #'field)
1475 (accessors rtd type-name #'(field* ...) (1+ n)
1477 #,(setter rtd type-name #'field n))))
1478 ((field field* ...) (identifier? #'field)
1479 (accessors rtd type-name #'(field* ...) (1+ n)
1481 #,(getter rtd type-name #'field n)
1482 #,(setter rtd type-name #'field n))))))
1484 (define (predicate rtd type-name fields exp)
1486 rtd type-name fields 0
1489 (define (#,(make-id rtd type-name '?) obj)
1490 (and (struct? obj) (eq? (struct-vtable obj) #,rtd))))))
1492 (define (field-list fields)
1493 (syntax-case fields ()
1495 (((f . opts) . rest) (identifier? #'f)
1496 (cons #'f (field-list #'rest)))
1497 ((f . rest) (identifier? #'f)
1498 (cons #'f (field-list #'rest)))))
1500 (define (constructor rtd type-name fields exp)
1501 (let ((ctor (make-id rtd type-name '-constructor))
1502 (args (field-list fields)))
1503 (predicate rtd type-name fields
1508 (make-struct rtd 0 #,@args))))
1509 (struct-set! #,rtd (+ vtable-offset-user 2)
1512 (define (type type-name printer fields)
1513 (define (make-layout)
1514 (let lp ((fields fields) (slots '()))
1515 (syntax-case fields ()
1516 (() (datum->syntax #'here
1518 (apply string-append slots))))
1519 ((_ . rest) (lp #'rest (cons "pw" slots))))))
1521 (let ((rtd (make-id type-name type-name '-type)))
1522 (constructor rtd type-name fields
1525 (make-struct record-type-vtable 0
1529 '#,(field-list fields)))
1530 (set-struct-vtable-name! #,rtd '#,type-name)))))
1533 ((_ type-name printer (field ...))
1534 (type #'type-name #'printer #'(field ...)))))))
1538 ;; A module is characterized by an obarray in which local symbols
1539 ;; are interned, a list of modules, "uses", from which non-local
1540 ;; bindings can be inherited, and an optional lazy-binder which
1541 ;; is a (CLOSURE module symbol) which, as a last resort, can provide
1542 ;; bindings that would otherwise not be found locally in the module.
1544 ;; NOTE: If you change the set of fields or their order, you also need to
1545 ;; change the constants in libguile/modules.h.
1547 ;; NOTE: The getter `module-eval-closure' is used in libguile/modules.c.
1548 ;; NOTE: The getter `module-transfomer' is defined libguile/modules.c.
1549 ;; NOTE: The getter `module-name' is defined later, due to boot reasons.
1550 ;; NOTE: The getter `module-public-interface' is used in libguile/modules.c.
1552 (define-record-type module
1553 (lambda (obj port) (%print-module obj port))
1558 (transformer #:no-getter)
1562 (import-obarray #:no-setter)
1564 (weak-observers #:no-setter)
1571 ;; make-module &opt size uses binder
1573 ;; Create a new module, perhaps with a particular size of obarray,
1574 ;; initial uses list, or binding procedure.
1579 (define (parse-arg index default)
1580 (if (> (length args) index)
1581 (list-ref args index)
1584 (define %default-import-size
1585 ;; Typical number of imported bindings actually used by a module.
1588 (if (> (length args) 3)
1589 (error "Too many args to make-module." args))
1591 (let ((size (parse-arg 0 31))
1592 (uses (parse-arg 1 '()))
1593 (binder (parse-arg 2 #f)))
1595 (if (not (integer? size))
1596 (error "Illegal size to make-module." size))
1597 (if (not (and (list? uses)
1598 (and-map module? uses)))
1599 (error "Incorrect use list." uses))
1600 (if (and binder (not (procedure? binder)))
1602 "Lazy-binder expected to be a procedure or #f." binder))
1604 (let ((module (module-constructor (make-hash-table size)
1605 uses binder #f macroexpand
1607 (make-hash-table %default-import-size)
1609 (make-weak-key-hash-table 31) #f
1610 (make-hash-table 7) #f #f)))
1612 ;; We can't pass this as an argument to module-constructor,
1613 ;; because we need it to close over a pointer to the module
1615 (set-module-eval-closure! module (standard-eval-closure module))
1622 ;;; {Observer protocol}
1625 (define (module-observe module proc)
1626 (set-module-observers! module (cons proc (module-observers module)))
1629 (define (module-observe-weak module observer-id . proc)
1630 ;; Register PROC as an observer of MODULE under name OBSERVER-ID (which can
1631 ;; be any Scheme object). PROC is invoked and passed MODULE any time
1632 ;; MODULE is modified. PROC gets unregistered when OBSERVER-ID gets GC'd
1633 ;; (thus, it is never unregistered if OBSERVER-ID is an immediate value,
1636 ;; The two-argument version is kept for backward compatibility: when called
1637 ;; with two arguments, the observer gets unregistered when closure PROC
1638 ;; gets GC'd (making it impossible to use an anonymous lambda for PROC).
1640 (let ((proc (if (null? proc) observer-id (car proc))))
1641 (hashq-set! (module-weak-observers module) observer-id proc)))
1643 (define (module-unobserve token)
1644 (let ((module (car token))
1647 (hash-remove! (module-weak-observers module) id)
1648 (set-module-observers! module (delq1! id (module-observers module)))))
1651 (define module-defer-observers #f)
1652 (define module-defer-observers-mutex (make-mutex 'recursive))
1653 (define module-defer-observers-table (make-hash-table))
1655 (define (module-modified m)
1656 (if module-defer-observers
1657 (hash-set! module-defer-observers-table m #t)
1658 (module-call-observers m)))
1660 ;;; This function can be used to delay calls to observers so that they
1661 ;;; can be called once only in the face of massive updating of modules.
1663 (define (call-with-deferred-observers thunk)
1666 (lock-mutex module-defer-observers-mutex)
1667 (set! module-defer-observers #t))
1670 (set! module-defer-observers #f)
1671 (hash-for-each (lambda (m dummy)
1672 (module-call-observers m))
1673 module-defer-observers-table)
1674 (hash-clear! module-defer-observers-table)
1675 (unlock-mutex module-defer-observers-mutex))))
1677 (define (module-call-observers m)
1678 (for-each (lambda (proc) (proc m)) (module-observers m))
1680 ;; We assume that weak observers don't (un)register themselves as they are
1681 ;; called since this would preclude proper iteration over the hash table
1683 (hash-for-each (lambda (id proc) (proc m)) (module-weak-observers m)))
1687 ;;; {Module Searching in General}
1689 ;;; We sometimes want to look for properties of a symbol
1690 ;;; just within the obarray of one module. If the property
1691 ;;; holds, then it is said to hold ``locally'' as in, ``The symbol
1692 ;;; DISPLAY is locally rebound in the module `safe-guile'.''
1695 ;;; Other times, we want to test for a symbol property in the obarray
1696 ;;; of M and, if it is not found there, try each of the modules in the
1697 ;;; uses list of M. This is the normal way of testing for some
1698 ;;; property, so we state these properties without qualification as
1699 ;;; in: ``The symbol 'fnord is interned in module M because it is
1700 ;;; interned locally in module M2 which is a member of the uses list
1704 ;; module-search fn m
1706 ;; return the first non-#f result of FN applied to M and then to
1707 ;; the modules in the uses of m, and so on recursively. If all applications
1708 ;; return #f, then so does this function.
1710 (define (module-search fn m v)
1713 (or (module-search fn (car pos) v)
1716 (loop (module-uses m))))
1719 ;;; {Is a symbol bound in a module?}
1721 ;;; Symbol S in Module M is bound if S is interned in M and if the binding
1722 ;;; of S in M has been set to some well-defined value.
1725 ;; module-locally-bound? module symbol
1727 ;; Is a symbol bound (interned and defined) locally in a given module?
1729 (define (module-locally-bound? m v)
1730 (let ((var (module-local-variable m v)))
1732 (variable-bound? var))))
1734 ;; module-bound? module symbol
1736 ;; Is a symbol bound (interned and defined) anywhere in a given module
1739 (define (module-bound? m v)
1740 (let ((var (module-variable m v)))
1742 (variable-bound? var))))
1744 ;;; {Is a symbol interned in a module?}
1746 ;;; Symbol S in Module M is interned if S occurs in
1747 ;;; of S in M has been set to some well-defined value.
1749 ;;; It is possible to intern a symbol in a module without providing
1750 ;;; an initial binding for the corresponding variable. This is done
1752 ;;; (module-add! module symbol (make-undefined-variable))
1754 ;;; In that case, the symbol is interned in the module, but not
1755 ;;; bound there. The unbound symbol shadows any binding for that
1756 ;;; symbol that might otherwise be inherited from a member of the uses list.
1759 (define (module-obarray-get-handle ob key)
1760 ((if (symbol? key) hashq-get-handle hash-get-handle) ob key))
1762 (define (module-obarray-ref ob key)
1763 ((if (symbol? key) hashq-ref hash-ref) ob key))
1765 (define (module-obarray-set! ob key val)
1766 ((if (symbol? key) hashq-set! hash-set!) ob key val))
1768 (define (module-obarray-remove! ob key)
1769 ((if (symbol? key) hashq-remove! hash-remove!) ob key))
1771 ;; module-symbol-locally-interned? module symbol
1773 ;; is a symbol interned (not neccessarily defined) locally in a given module
1774 ;; or its uses? Interned symbols shadow inherited bindings even if
1775 ;; they are not themselves bound to a defined value.
1777 (define (module-symbol-locally-interned? m v)
1778 (not (not (module-obarray-get-handle (module-obarray m) v))))
1780 ;; module-symbol-interned? module symbol
1782 ;; is a symbol interned (not neccessarily defined) anywhere in a given module
1783 ;; or its uses? Interned symbols shadow inherited bindings even if
1784 ;; they are not themselves bound to a defined value.
1786 (define (module-symbol-interned? m v)
1787 (module-search module-symbol-locally-interned? m v))
1790 ;;; {Mapping modules x symbols --> variables}
1793 ;; module-local-variable module symbol
1794 ;; return the local variable associated with a MODULE and SYMBOL.
1796 ;;; This function is very important. It is the only function that can
1797 ;;; return a variable from a module other than the mutators that store
1798 ;;; new variables in modules. Therefore, this function is the location
1799 ;;; of the "lazy binder" hack.
1801 ;;; If symbol is defined in MODULE, and if the definition binds symbol
1802 ;;; to a variable, return that variable object.
1804 ;;; If the symbols is not found at first, but the module has a lazy binder,
1805 ;;; then try the binder.
1807 ;;; If the symbol is not found at all, return #f.
1809 ;;; (This is now written in C, see `modules.c'.)
1812 ;;; {Mapping modules x symbols --> bindings}
1814 ;;; These are similar to the mapping to variables, except that the
1815 ;;; variable is dereferenced.
1818 ;; module-symbol-binding module symbol opt-value
1820 ;; return the binding of a variable specified by name within
1821 ;; a given module, signalling an error if the variable is unbound.
1822 ;; If the OPT-VALUE is passed, then instead of signalling an error,
1823 ;; return OPT-VALUE.
1825 (define (module-symbol-local-binding m v . opt-val)
1826 (let ((var (module-local-variable m v)))
1827 (if (and var (variable-bound? var))
1829 (if (not (null? opt-val))
1831 (error "Locally unbound variable." v)))))
1833 ;; module-symbol-binding module symbol opt-value
1835 ;; return the binding of a variable specified by name within
1836 ;; a given module, signalling an error if the variable is unbound.
1837 ;; If the OPT-VALUE is passed, then instead of signalling an error,
1838 ;; return OPT-VALUE.
1840 (define (module-symbol-binding m v . opt-val)
1841 (let ((var (module-variable m v)))
1842 (if (and var (variable-bound? var))
1844 (if (not (null? opt-val))
1846 (error "Unbound variable." v)))))
1851 ;;; {Adding Variables to Modules}
1854 ;; module-make-local-var! module symbol
1856 ;; ensure a variable for V in the local namespace of M.
1857 ;; If no variable was already there, then create a new and uninitialzied
1860 ;; This function is used in modules.c.
1862 (define (module-make-local-var! m v)
1863 (or (let ((b (module-obarray-ref (module-obarray m) v)))
1866 ;; Mark as modified since this function is called when
1867 ;; the standard eval closure defines a binding
1871 ;; Create a new local variable.
1872 (let ((local-var (make-undefined-variable)))
1873 (module-add! m v local-var)
1876 ;; module-ensure-local-variable! module symbol
1878 ;; Ensure that there is a local variable in MODULE for SYMBOL. If
1879 ;; there is no binding for SYMBOL, create a new uninitialized
1880 ;; variable. Return the local variable.
1882 (define (module-ensure-local-variable! module symbol)
1883 (or (module-local-variable module symbol)
1884 (let ((var (make-undefined-variable)))
1885 (module-add! module symbol var)
1888 ;; module-add! module symbol var
1890 ;; ensure a particular variable for V in the local namespace of M.
1892 (define (module-add! m v var)
1893 (if (not (variable? var))
1894 (error "Bad variable to module-add!" var))
1895 (module-obarray-set! (module-obarray m) v var)
1896 (module-modified m))
1900 ;; make sure that a symbol is undefined in the local namespace of M.
1902 (define (module-remove! m v)
1903 (module-obarray-remove! (module-obarray m) v)
1904 (module-modified m))
1906 (define (module-clear! m)
1907 (hash-clear! (module-obarray m))
1908 (module-modified m))
1910 ;; MODULE-FOR-EACH -- exported
1912 ;; Call PROC on each symbol in MODULE, with arguments of (SYMBOL VARIABLE).
1914 (define (module-for-each proc module)
1915 (hash-for-each proc (module-obarray module)))
1917 (define (module-map proc module)
1918 (hash-map->list proc (module-obarray module)))
1922 ;; Modules exist in a separate namespace from values, because you generally do
1923 ;; not want the name of a submodule, which you might not even use, to collide
1924 ;; with local variables that happen to be named the same as the submodule.
1926 (define (module-ref-submodule module name)
1927 (or (hashq-ref (module-submodules module) name)
1928 (and (module-submodule-binder module)
1929 ((module-submodule-binder module) module name))))
1931 (define (module-define-submodule! module name submodule)
1932 (hashq-set! (module-submodules module) name submodule))
1936 ;;; {Low Level Bootstrapping}
1941 ;; A root module uses the pre-modules-obarray as its obarray. This
1942 ;; special obarray accumulates all bindings that have been established
1943 ;; before the module system is fully booted.
1945 ;; (The obarray continues to be used by code that has been closed over
1946 ;; before the module system has been booted.)
1948 (define (make-root-module)
1949 (let ((m (make-module 0)))
1950 (set-module-obarray! m (%get-pre-modules-obarray))
1955 ;; The root interface is a module that uses the same obarray as the
1956 ;; root module. It does not allow new definitions, tho.
1958 (define (make-scm-module)
1959 (let ((m (make-module 0)))
1960 (set-module-obarray! m (%get-pre-modules-obarray))
1961 (set-module-eval-closure! m (standard-interface-eval-closure m))
1967 ;;; {Module-based Loading}
1970 (define (save-module-excursion thunk)
1971 (let ((inner-module (current-module))
1973 (dynamic-wind (lambda ()
1974 (set! outer-module (current-module))
1975 (set-current-module inner-module)
1976 (set! inner-module #f))
1979 (set! inner-module (current-module))
1980 (set-current-module outer-module)
1981 (set! outer-module #f)))))
1983 (define basic-load load)
1985 (define (load-module filename . reader)
1986 (save-module-excursion
1988 (let ((oldname (and (current-load-port)
1989 (port-filename (current-load-port)))))
1992 (> (string-length filename) 0)
1993 (not (char=? (string-ref filename 0) #\/))
1994 (not (string=? (dirname oldname) ".")))
1995 (string-append (dirname oldname) "/" filename)
2002 ;;; {MODULE-REF -- exported}
2005 ;; Returns the value of a variable called NAME in MODULE or any of its
2006 ;; used modules. If there is no such variable, then if the optional third
2007 ;; argument DEFAULT is present, it is returned; otherwise an error is signaled.
2009 (define (module-ref module name . rest)
2010 (let ((variable (module-variable module name)))
2011 (if (and variable (variable-bound? variable))
2012 (variable-ref variable)
2014 (error "No variable named" name 'in module)
2015 (car rest) ; default value
2018 ;; MODULE-SET! -- exported
2020 ;; Sets the variable called NAME in MODULE (or in a module that MODULE uses)
2021 ;; to VALUE; if there is no such variable, an error is signaled.
2023 (define (module-set! module name value)
2024 (let ((variable (module-variable module name)))
2026 (variable-set! variable value)
2027 (error "No variable named" name 'in module))))
2029 ;; MODULE-DEFINE! -- exported
2031 ;; Sets the variable called NAME in MODULE to VALUE; if there is no such
2032 ;; variable, it is added first.
2034 (define (module-define! module name value)
2035 (let ((variable (module-local-variable module name)))
2038 (variable-set! variable value)
2039 (module-modified module))
2040 (let ((variable (make-variable value)))
2041 (module-add! module name variable)))))
2043 ;; MODULE-DEFINED? -- exported
2045 ;; Return #t iff NAME is defined in MODULE (or in a module that MODULE
2048 (define (module-defined? module name)
2049 (let ((variable (module-variable module name)))
2050 (and variable (variable-bound? variable))))
2052 ;; MODULE-USE! module interface
2054 ;; Add INTERFACE to the list of interfaces used by MODULE.
2056 (define (module-use! module interface)
2057 (if (not (or (eq? module interface)
2058 (memq interface (module-uses module))))
2060 ;; Newly used modules must be appended rather than consed, so that
2061 ;; `module-variable' traverses the use list starting from the first
2063 (set-module-uses! module
2064 (append (filter (lambda (m)
2066 (equal? (module-name m)
2067 (module-name interface))))
2068 (module-uses module))
2070 (hash-clear! (module-import-obarray module))
2071 (module-modified module))))
2073 ;; MODULE-USE-INTERFACES! module interfaces
2075 ;; Same as MODULE-USE! but add multiple interfaces and check for duplicates
2077 (define (module-use-interfaces! module interfaces)
2078 (set-module-uses! module
2079 (append (module-uses module) interfaces))
2080 (hash-clear! (module-import-obarray module))
2081 (module-modified module))
2085 ;;; {Recursive Namespaces}
2087 ;;; A hierarchical namespace emerges if we consider some module to be
2088 ;;; root, and submodules of that module to be nested namespaces.
2090 ;;; The routines here manage variable names in hierarchical namespace.
2091 ;;; Each variable name is a list of elements, looked up in successively nested
2094 ;;; (nested-ref some-root-module '(foo bar baz))
2095 ;;; => <value of a variable named baz in the submodule bar of
2096 ;;; the submodule foo of some-root-module>
2101 ;;; ;; a-root is a module
2102 ;;; ;; name is a list of symbols
2104 ;;; nested-ref a-root name
2105 ;;; nested-set! a-root name val
2106 ;;; nested-define! a-root name val
2107 ;;; nested-remove! a-root name
2109 ;;; These functions manipulate values in namespaces. For referencing the
2110 ;;; namespaces themselves, use the following:
2112 ;;; nested-ref-module a-root name
2113 ;;; nested-define-module! a-root name mod
2115 ;;; (current-module) is a natural choice for a root so for convenience there are
2118 ;;; local-ref name == nested-ref (current-module) name
2119 ;;; local-set! name val == nested-set! (current-module) name val
2120 ;;; local-define name val == nested-define! (current-module) name val
2121 ;;; local-remove name == nested-remove! (current-module) name
2122 ;;; local-ref-module name == nested-ref-module (current-module) name
2123 ;;; local-define-module! name m == nested-define-module! (current-module) name m
2127 (define (nested-ref root names)
2130 (let loop ((cur root)
2134 (module-ref cur head #f)
2135 (let ((cur (module-ref-submodule cur head)))
2137 (loop cur (car tail) (cdr tail))))))))
2139 (define (nested-set! root names val)
2140 (let loop ((cur root)
2144 (module-set! cur head val)
2145 (let ((cur (module-ref-submodule cur head)))
2147 (error "failed to resolve module" names)
2148 (loop cur (car tail) (cdr tail)))))))
2150 (define (nested-define! root names val)
2151 (let loop ((cur root)
2155 (module-define! cur head val)
2156 (let ((cur (module-ref-submodule cur head)))
2158 (error "failed to resolve module" names)
2159 (loop cur (car tail) (cdr tail)))))))
2161 (define (nested-remove! root names)
2162 (let loop ((cur root)
2166 (module-remove! cur head)
2167 (let ((cur (module-ref-submodule cur head)))
2169 (error "failed to resolve module" names)
2170 (loop cur (car tail) (cdr tail)))))))
2173 (define (nested-ref-module root names)
2174 (let loop ((cur root)
2178 (let ((cur (module-ref-submodule cur (car names))))
2180 (loop cur (cdr names)))))))
2182 (define (nested-define-module! root names module)
2184 (error "can't redefine root module" root module)
2185 (let loop ((cur root)
2189 (module-define-submodule! cur head module)
2190 (let ((cur (or (module-ref-submodule cur head)
2191 (let ((m (make-module 31)))
2192 (set-module-kind! m 'directory)
2193 (set-module-name! m (append (module-name cur)
2195 (module-define-submodule! cur head m)
2197 (loop cur (car tail) (cdr tail)))))))
2200 (define (local-ref names) (nested-ref (current-module) names))
2201 (define (local-set! names val) (nested-set! (current-module) names val))
2202 (define (local-define names val) (nested-define! (current-module) names val))
2203 (define (local-remove names) (nested-remove! (current-module) names))
2204 (define (local-ref-module names) (nested-ref-module (current-module) names))
2205 (define (local-define-module names mod) (nested-define-module! (current-module) names mod))
2211 ;;; {The (guile) module}
2213 ;;; The standard module, which has the core Guile bindings. Also called the
2214 ;;; "root module", as it is imported by many other modules, but it is not
2215 ;;; necessarily the root of anything; and indeed, the module named '() might be
2216 ;;; better thought of as a root.
2219 (define (set-system-module! m s)
2220 (set-procedure-property! (module-eval-closure m) 'system-module s))
2221 (define the-root-module (make-root-module))
2222 (define the-scm-module (make-scm-module))
2223 (set-module-public-interface! the-root-module the-scm-module)
2224 (set-module-name! the-root-module '(guile))
2225 (set-module-name! the-scm-module '(guile))
2226 (set-module-kind! the-scm-module 'interface)
2227 (set-system-module! the-root-module #t)
2228 (set-system-module! the-scm-module #t)
2233 ;; Now that we have a root module, even though modules aren't fully booted,
2234 ;; expand the definition of resolve-module.
2236 (define (resolve-module name . args)
2237 (if (equal? name '(guile))
2239 (error "unexpected module to resolve during module boot" name)))
2241 ;; Cheat. These bindings are needed by modules.c, but we don't want
2242 ;; to move their real definition here because that would be unnatural.
2244 (define process-define-module #f)
2245 (define process-use-modules #f)
2246 (define module-export! #f)
2247 (define default-duplicate-binding-procedures #f)
2249 ;; This boots the module system. All bindings needed by modules.c
2250 ;; must have been defined by now.
2252 (set-current-module the-root-module)
2257 ;; Now that modules are booted, give module-name its final definition.
2260 (let ((accessor (record-accessor module-type 'name)))
2263 (let ((name (list (gensym))))
2264 ;; Name MOD and bind it in the module root so that it's visible to
2265 ;; `resolve-module'. This is important as `psyntax' stores module
2266 ;; names and relies on being able to `resolve-module' them.
2267 (set-module-name! mod name)
2268 (nested-define-module! (resolve-module '() #f) name mod)
2271 (define (make-modules-in module name)
2272 (or (nested-ref-module module name)
2273 (let ((m (make-module 31)))
2274 (set-module-kind! m 'directory)
2275 (set-module-name! m (append (module-name module) name))
2276 (nested-define-module! module name m)
2279 (define (beautify-user-module! module)
2280 (let ((interface (module-public-interface module)))
2281 (if (or (not interface)
2282 (eq? interface module))
2283 (let ((interface (make-module 31)))
2284 (set-module-name! interface (module-name module))
2285 (set-module-version! interface (module-version module))
2286 (set-module-kind! interface 'interface)
2287 (set-module-public-interface! module interface))))
2288 (if (and (not (memq the-scm-module (module-uses module)))
2289 (not (eq? module the-root-module)))
2290 ;; Import the default set of bindings (from the SCM module) in MODULE.
2291 (module-use! module the-scm-module)))
2293 (define (version-matches? version-ref target)
2294 (define (any pred lst)
2295 (and (not (null? lst)) (or (pred (car lst)) (any pred (cdr lst)))))
2296 (define (every pred lst)
2297 (or (null? lst) (and (pred (car lst)) (every pred (cdr lst)))))
2298 (define (sub-versions-match? v-refs t)
2299 (define (sub-version-matches? v-ref t)
2300 (define (curried-sub-version-matches? v)
2301 (sub-version-matches? v t))
2302 (cond ((number? v-ref) (eqv? v-ref t))
2304 (let ((cv (car v-ref)))
2305 (cond ((eq? cv '>=) (>= t (cadr v-ref)))
2306 ((eq? cv '<=) (<= t (cadr v-ref)))
2308 (every curried-sub-version-matches? (cdr v-ref)))
2310 (any curried-sub-version-matches? (cdr v-ref)))
2311 ((eq? cv 'not) (not (sub-version-matches? (cadr v-ref) t)))
2312 (else (error "Incompatible sub-version reference" cv)))))
2313 (else (error "Incompatible sub-version reference" v-ref))))
2315 (and (not (null? t))
2316 (sub-version-matches? (car v-refs) (car t))
2317 (sub-versions-match? (cdr v-refs) (cdr t)))))
2318 (define (curried-version-matches? v)
2319 (version-matches? v target))
2320 (or (null? version-ref)
2321 (let ((cv (car version-ref)))
2322 (cond ((eq? cv 'and) (every curried-version-matches? (cdr version-ref)))
2323 ((eq? cv 'or) (any curried-version-matches? (cdr version-ref)))
2324 ((eq? cv 'not) (not (version-matches? (cadr version-ref) target)))
2325 (else (sub-versions-match? version-ref target))))))
2327 (define (find-versioned-module dir-hint name version-ref roots)
2328 (define (subdir-pair-less pair1 pair2)
2329 (define (numlist-less lst1 lst2)
2331 (and (not (null? lst1))
2332 (cond ((> (car lst1) (car lst2)) #t)
2333 ((< (car lst1) (car lst2)) #f)
2334 (else (numlist-less (cdr lst1) (cdr lst2)))))))
2335 (numlist-less (car pair1) (car pair2)))
2336 (define (match-version-and-file pair)
2337 (and (version-matches? version-ref (car pair))
2339 (filter (lambda (file)
2340 (let ((s (false-if-exception (stat file))))
2341 (and s (eq? (stat:type s) 'regular))))
2343 (string-append (cdr pair) "/" name ext))
2344 %load-extensions))))
2345 (and (not (null? filenames))
2346 (cons (car pair) (car filenames))))))
2348 (define (match-version-recursive root-pairs leaf-pairs)
2349 (define (filter-subdirs root-pairs ret)
2350 (define (filter-subdir root-pair dstrm subdir-pairs)
2351 (let ((entry (readdir dstrm)))
2352 (if (eof-object? entry)
2354 (let* ((subdir (string-append (cdr root-pair) "/" entry))
2355 (num (string->number entry))
2356 (num (and num (append (car root-pair) (list num)))))
2357 (if (and num (eq? (stat:type (stat subdir)) 'directory))
2359 root-pair dstrm (cons (cons num subdir) subdir-pairs))
2360 (filter-subdir root-pair dstrm subdir-pairs))))))
2362 (or (and (null? root-pairs) ret)
2363 (let* ((rp (car root-pairs))
2364 (dstrm (false-if-exception (opendir (cdr rp)))))
2366 (let ((subdir-pairs (filter-subdir rp dstrm '())))
2368 (filter-subdirs (cdr root-pairs)
2369 (or (and (null? subdir-pairs) ret)
2370 (append ret subdir-pairs))))
2371 (filter-subdirs (cdr root-pairs) ret)))))
2373 (or (and (null? root-pairs) leaf-pairs)
2374 (let ((matching-subdir-pairs (filter-subdirs root-pairs '())))
2375 (match-version-recursive
2376 matching-subdir-pairs
2377 (append leaf-pairs (filter pair? (map match-version-and-file
2378 matching-subdir-pairs)))))))
2379 (define (make-root-pair root)
2380 (cons '() (string-append root "/" dir-hint)))
2382 (let* ((root-pairs (map make-root-pair roots))
2383 (matches (if (null? version-ref)
2384 (filter pair? (map match-version-and-file root-pairs))
2386 (matches (append matches (match-version-recursive root-pairs '()))))
2387 (and (null? matches) (error "No matching modules found."))
2388 (cdar (sort matches subdir-pair-less))))
2390 (define (make-fresh-user-module)
2391 (let ((m (make-module)))
2392 (beautify-user-module! m)
2395 ;; NOTE: This binding is used in libguile/modules.c.
2397 (define resolve-module
2398 (let ((root (make-module)))
2399 (set-module-name! root '())
2400 ;; Define the-root-module as '(guile).
2401 (module-define-submodule! root 'guile the-root-module)
2403 (lambda (name . args) ;; #:optional (autoload #t) (version #f)
2404 (let* ((already (nested-ref-module root name))
2405 (numargs (length args))
2406 (autoload (or (= numargs 0) (car args)))
2407 (version (and (> numargs 1) (cadr args))))
2410 (or (not autoload) (module-public-interface already)))
2411 ;; A hit, a palpable hit.
2413 (not (version-matches? version (module-version already))))
2414 (error "incompatible module version already loaded" name))
2417 ;; Try to autoload the module, and recurse.
2418 (try-load-module name version)
2419 (resolve-module name #f))
2421 ;; No module found (or if one was, it had no public interface), and
2422 ;; we're not autoloading. Here's the weird semantics: we ensure
2423 ;; there's an empty module.
2424 (or already (make-modules-in root name))))))))
2427 (define (try-load-module name version)
2428 (try-module-autoload name version))
2430 (define (purify-module! module)
2431 "Removes bindings in MODULE which are inherited from the (guile) module."
2432 (let ((use-list (module-uses module)))
2433 (if (and (pair? use-list)
2434 (eq? (car (last-pair use-list)) the-scm-module))
2435 (set-module-uses! module (reverse (cdr (reverse use-list)))))))
2437 ;; Return a module that is an interface to the module designated by
2440 ;; `resolve-interface' takes four keyword arguments:
2442 ;; #:select SELECTION
2444 ;; SELECTION is a list of binding-specs to be imported; A binding-spec
2445 ;; is either a symbol or a pair of symbols (ORIG . SEEN), where ORIG
2446 ;; is the name in the used module and SEEN is the name in the using
2447 ;; module. Note that SEEN is also passed through RENAMER, below. The
2448 ;; default is to select all bindings. If you specify no selection but
2449 ;; a renamer, only the bindings that already exist in the used module
2450 ;; are made available in the interface. Bindings that are added later
2451 ;; are not picked up.
2455 ;; BINDINGS is a list of bindings which should not be imported.
2459 ;; PREFIX is a symbol that will be appended to each exported name.
2460 ;; The default is to not perform any renaming.
2462 ;; #:renamer RENAMER
2464 ;; RENAMER is a procedure that takes a symbol and returns its new
2465 ;; name. The default is not perform any renaming.
2467 ;; Signal "no code for module" error if module name is not resolvable
2468 ;; or its public interface is not available. Signal "no binding"
2469 ;; error if selected binding does not exist in the used module.
2471 (define (resolve-interface name . args)
2473 (define (get-keyword-arg args kw def)
2474 (cond ((memq kw args)
2476 (if (null? (cdr kw-arg))
2477 (error "keyword without value: " kw))
2482 (let* ((select (get-keyword-arg args #:select #f))
2483 (hide (get-keyword-arg args #:hide '()))
2484 (renamer (or (get-keyword-arg args #:renamer #f)
2485 (let ((prefix (get-keyword-arg args #:prefix #f)))
2486 (and prefix (symbol-prefix-proc prefix)))
2488 (version (get-keyword-arg args #:version #f))
2489 (module (resolve-module name #t version))
2490 (public-i (and module (module-public-interface module))))
2491 (and (or (not module) (not public-i))
2492 (error "no code for module" name))
2493 (if (and (not select) (null? hide) (eq? renamer identity))
2495 (let ((selection (or select (module-map (lambda (sym var) sym)
2497 (custom-i (make-module 31)))
2498 (set-module-kind! custom-i 'custom-interface)
2499 (set-module-name! custom-i name)
2500 ;; XXX - should use a lazy binder so that changes to the
2501 ;; used module are picked up automatically.
2502 (for-each (lambda (bspec)
2503 (let* ((direct? (symbol? bspec))
2504 (orig (if direct? bspec (car bspec)))
2505 (seen (if direct? bspec (cdr bspec)))
2506 (var (or (module-local-variable public-i orig)
2507 (module-local-variable module orig)
2509 ;; fixme: format manually for now
2511 #f "no binding `~A' in module ~A"
2513 (if (memq orig hide)
2514 (set! hide (delq! orig hide))
2515 (module-add! custom-i
2519 ;; Check that we are not hiding bindings which don't exist
2520 (for-each (lambda (binding)
2521 (if (not (module-local-variable public-i binding))
2524 #f "no binding `~A' to hide in module ~A"
2529 (define (symbol-prefix-proc prefix)
2531 (symbol-append prefix symbol)))
2533 ;; This function is called from "modules.c". If you change it, be
2534 ;; sure to update "modules.c" as well.
2536 (define (process-define-module args)
2537 (let* ((module-id (car args))
2538 (module (resolve-module module-id #f))
2540 (unrecognized (lambda (arg)
2541 (error "unrecognized define-module argument" arg))))
2542 (beautify-user-module! module)
2543 (let loop ((kws kws)
2544 (reversed-interfaces '())
2551 (call-with-deferred-observers
2553 (module-use-interfaces! module (reverse reversed-interfaces))
2554 (module-export! module exports)
2555 (module-replace! module replacements)
2556 (module-re-export! module re-exports)
2557 (if (not (null? autoloads))
2558 (apply module-autoload! module autoloads))))
2560 ((#:use-module #:use-syntax)
2561 (or (pair? (cdr kws))
2564 ((equal? (caadr kws) '(ice-9 syncase))
2565 (issue-deprecation-warning
2566 "(ice-9 syncase) is deprecated. Support for syntax-case is now in Guile core.")
2574 (let* ((interface-args (cadr kws))
2575 (interface (apply resolve-interface interface-args)))
2576 (and (eq? (car kws) #:use-syntax)
2577 (or (symbol? (caar interface-args))
2578 (error "invalid module name for use-syntax"
2579 (car interface-args)))
2580 (set-module-transformer!
2582 (module-ref interface
2583 (car (last-pair (car interface-args)))
2586 (cons interface reversed-interfaces)
2592 (or (and (pair? (cdr kws)) (pair? (cddr kws)))
2599 (let ((name (cadr kws))
2600 (bindings (caddr kws)))
2601 (cons* name bindings autoloads))))
2603 (set-system-module! module #t)
2604 (loop (cdr kws) reversed-interfaces exports re-exports
2605 replacements autoloads))
2607 (purify-module! module)
2608 (loop (cdr kws) reversed-interfaces exports re-exports
2609 replacements autoloads))
2611 (or (pair? (cdr kws))
2613 (let ((version (cadr kws)))
2614 (set-module-version! module version)
2615 (set-module-version! (module-public-interface module) version))
2616 (loop (cddr kws) reversed-interfaces exports re-exports
2617 replacements autoloads))
2619 (if (not (pair? (cdr kws)))
2621 (set-module-duplicates-handlers!
2623 (lookup-duplicates-handlers (cadr kws)))
2624 (loop (cddr kws) reversed-interfaces exports re-exports
2625 replacements autoloads))
2626 ((#:export #:export-syntax)
2627 (or (pair? (cdr kws))
2631 (append (cadr kws) exports)
2635 ((#:re-export #:re-export-syntax)
2636 (or (pair? (cdr kws))
2641 (append (cadr kws) re-exports)
2644 ((#:replace #:replace-syntax)
2645 (or (pair? (cdr kws))
2651 (append (cadr kws) replacements)
2654 (unrecognized kws)))))
2655 (run-hook module-defined-hook module)
2658 ;; `module-defined-hook' is a hook that is run whenever a new module
2659 ;; is defined. Its members are called with one argument, the new
2661 (define module-defined-hook (make-hook 1))
2668 (define (make-autoload-interface module name bindings)
2669 (let ((b (lambda (a sym definep)
2670 (and (memq sym bindings)
2671 (let ((i (module-public-interface (resolve-module name))))
2673 (error "missing interface for module" name))
2674 (let ((autoload (memq a (module-uses module))))
2675 ;; Replace autoload-interface with actual interface if
2676 ;; that has not happened yet.
2677 (if (pair? autoload)
2678 (set-car! autoload i)))
2679 (module-local-variable i sym))))))
2680 (module-constructor (make-hash-table 0) '() b #f #f name 'autoload #f
2681 (make-hash-table 0) '() (make-weak-value-hash-table 31) #f
2682 (make-hash-table 0) #f #f)))
2684 (define (module-autoload! module . args)
2685 "Have @var{module} automatically load the module named @var{name} when one
2686 of the symbols listed in @var{bindings} is looked up. @var{args} should be a
2687 list of module-name/binding-list pairs, e.g., as in @code{(module-autoload!
2688 module '(ice-9 q) '(make-q q-length))}."
2689 (let loop ((args args))
2693 (error "invalid name+binding autoload list" args))
2695 (let ((name (car args))
2696 (bindings (cadr args)))
2697 (module-use! module (make-autoload-interface module
2699 (loop (cddr args)))))))
2704 ;;; {Autoloading modules}
2707 (define autoloads-in-progress '())
2709 ;; This function is called from "modules.c". If you change it, be
2710 ;; sure to update "modules.c" as well.
2712 (define (try-module-autoload module-name . args)
2713 (let* ((reverse-name (reverse module-name))
2714 (name (symbol->string (car reverse-name)))
2715 (version (and (not (null? args)) (car args)))
2716 (dir-hint-module-name (reverse (cdr reverse-name)))
2717 (dir-hint (apply string-append
2719 (string-append (symbol->string elt) "/"))
2720 dir-hint-module-name))))
2721 (resolve-module dir-hint-module-name #f)
2722 (and (not (autoload-done-or-in-progress? dir-hint name))
2725 (lambda () (autoload-in-progress! dir-hint name))
2727 (with-fluids ((current-reader #f))
2728 (save-module-excursion
2731 (load (find-versioned-module
2732 dir-hint name version %load-path))
2733 (primitive-load-path (in-vicinity dir-hint name) #f))
2735 (lambda () (set-autoloaded! dir-hint name didit)))
2740 ;;; {Dynamic linking of modules}
2743 (define autoloads-done '((guile . guile)))
2745 (define (autoload-done-or-in-progress? p m)
2746 (let ((n (cons p m)))
2747 (->bool (or (member n autoloads-done)
2748 (member n autoloads-in-progress)))))
2750 (define (autoload-done! p m)
2751 (let ((n (cons p m)))
2752 (set! autoloads-in-progress
2753 (delete! n autoloads-in-progress))
2754 (or (member n autoloads-done)
2755 (set! autoloads-done (cons n autoloads-done)))))
2757 (define (autoload-in-progress! p m)
2758 (let ((n (cons p m)))
2759 (set! autoloads-done
2760 (delete! n autoloads-done))
2761 (set! autoloads-in-progress (cons n autoloads-in-progress))))
2763 (define (set-autoloaded! p m done?)
2765 (autoload-done! p m)
2766 (let ((n (cons p m)))
2767 (set! autoloads-done (delete! n autoloads-done))
2768 (set! autoloads-in-progress (delete! n autoloads-in-progress)))))
2772 ;;; {Run-time options}
2775 (defmacro define-option-interface (option-group)
2776 (let* ((option-name 'car)
2777 (option-value 'cadr)
2778 (option-documentation 'caddr)
2780 ;; Below follow the macros defining the run-time option interfaces.
2782 (make-options (lambda (interface)
2784 (cond ((null? args) (,interface))
2786 (,interface (car args)) (,interface))
2789 (display (,option-name option))
2790 (if (< (string-length
2791 (symbol->string (,option-name option)))
2795 (display (,option-value option))
2797 (display (,option-documentation option))
2799 (,interface #t)))))))
2801 (make-enable (lambda (interface)
2803 (,interface (append flags (,interface)))
2806 (make-disable (lambda (interface)
2808 (let ((options (,interface)))
2809 (for-each (lambda (flag)
2810 (set! options (delq! flag options)))
2812 (,interface options)
2814 (let* ((interface (car option-group))
2815 (options/enable/disable (cadr option-group)))
2817 (define ,(car options/enable/disable)
2818 ,(make-options interface))
2819 (define ,(cadr options/enable/disable)
2820 ,(make-enable interface))
2821 (define ,(caddr options/enable/disable)
2822 ,(make-disable interface))
2823 (defmacro ,(caaddr option-group) (opt val)
2824 `(,',(car options/enable/disable)
2825 (append (,',(car options/enable/disable))
2826 (list ',opt ,val))))))))
2828 (define-option-interface
2829 (eval-options-interface
2830 (eval-options eval-enable eval-disable)
2833 (define-option-interface
2834 (debug-options-interface
2835 (debug-options debug-enable debug-disable)
2838 (define-option-interface
2839 (evaluator-traps-interface
2840 (traps trap-enable trap-disable)
2843 (define-option-interface
2844 (read-options-interface
2845 (read-options read-enable read-disable)
2848 (define-option-interface
2849 (print-options-interface
2850 (print-options print-enable print-disable)
2858 (define (repl read evaler print)
2859 (let loop ((source (read (current-input-port))))
2860 (print (evaler source))
2861 (loop (read (current-input-port)))))
2863 ;; A provisional repl that acts like the SCM repl:
2865 (define scm-repl-silent #f)
2866 (define (assert-repl-silence v) (set! scm-repl-silent v))
2868 (define *unspecified* (if #f #f))
2869 (define (unspecified? v) (eq? v *unspecified*))
2871 (define scm-repl-print-unspecified #f)
2872 (define (assert-repl-print-unspecified v) (set! scm-repl-print-unspecified v))
2874 (define scm-repl-verbose #f)
2875 (define (assert-repl-verbosity v) (set! scm-repl-verbose v))
2877 (define scm-repl-prompt "guile> ")
2879 (define (set-repl-prompt! v) (set! scm-repl-prompt v))
2881 (define (default-pre-unwind-handler key . args)
2882 ;; Narrow by two more frames: this one, and the throw handler.
2884 (apply throw key args))
2887 (define (pre-unwind-handler-dispatch key . args)
2888 (apply default-pre-unwind-handler key args)))
2890 (define abort-hook (make-hook))
2892 ;; these definitions are used if running a script.
2893 ;; otherwise redefined in error-catching-loop.
2894 (define (set-batch-mode?! arg) #t)
2895 (define (batch-mode?) #t)
2897 (define (error-catching-loop thunk)
2900 (define (loop first)
2905 (call-with-unblocked-asyncs
2911 ;; This line is needed because mark
2912 ;; doesn't do closures quite right.
2913 ;; Unreferenced locals should be
2916 (let loop ((v (thunk)))
2920 (lambda (key . args)
2927 (apply throw 'switch-repl args))
2930 ;; This is one of the closures that require
2931 ;; (set! first #f) above
2934 (run-hook abort-hook)
2935 (force-output (current-output-port))
2936 (display "ABORT: " (current-error-port))
2937 (write args (current-error-port))
2938 (newline (current-error-port))
2942 (not has-shown-debugger-hint?)
2943 (not (memq 'backtrace
2944 (debug-options-interface)))
2945 (stack? (fluid-ref the-last-stack)))
2947 (newline (current-error-port))
2949 "Type \"(backtrace)\" to get more information or \"(debug)\" to enter the debugger.\n"
2950 (current-error-port))
2951 (set! has-shown-debugger-hint? #t)))
2952 (force-output (current-error-port)))
2954 (primitive-exit 1)))
2955 (set! stack-saved? #f)))
2958 ;; This is the other cons-leak closure...
2960 (cond ((= (length args) 4)
2961 (apply handle-system-error key args))
2963 (apply bad-throw key args)))))))
2965 default-pre-unwind-handler)))
2967 (if next (loop next) status)))
2968 (set! set-batch-mode?! (lambda (arg)
2970 (set! interactive #f)
2973 (error "sorry, not implemented")))))
2974 (set! batch-mode? (lambda () (not interactive)))
2975 (call-with-blocked-asyncs
2976 (lambda () (loop (lambda () #t))))))
2978 ;;(define the-last-stack (make-fluid)) Defined by scm_init_backtrace ()
2979 (define before-signal-stack (make-fluid))
2980 ;; FIXME: stack-saved? is broken in the presence of threads.
2981 (define stack-saved? #f)
2983 (define (save-stack . narrowing)
2984 (if (not stack-saved?)
2986 (let ((stacks (fluid-ref %stacks)))
2987 (fluid-set! the-last-stack
2988 ;; (make-stack obj inner outer inner outer ...)
2990 ;; In this case, cut away the make-stack frame, the
2991 ;; save-stack frame, and then narrow as specified by the
2992 ;; user, delimited by the nearest start-stack invocation,
2994 (apply make-stack #t
2996 (if (pair? stacks) (cdar stacks) 0)
2998 (set! stack-saved? #t))))
3000 (define before-error-hook (make-hook))
3001 (define after-error-hook (make-hook))
3002 (define before-backtrace-hook (make-hook))
3003 (define after-backtrace-hook (make-hook))
3005 (define has-shown-debugger-hint? #f)
3007 (define (handle-system-error key . args)
3008 (let ((cep (current-error-port)))
3009 (cond ((not (stack? (fluid-ref the-last-stack))))
3010 ((memq 'backtrace (debug-options-interface))
3011 (let ((highlights (if (or (eq? key 'wrong-type-arg)
3012 (eq? key 'out-of-range))
3015 (run-hook before-backtrace-hook)
3017 (display "Backtrace:\n")
3018 (display-backtrace (fluid-ref the-last-stack) cep
3021 (run-hook after-backtrace-hook))))
3022 (run-hook before-error-hook)
3023 (apply display-error (fluid-ref the-last-stack) cep args)
3024 (run-hook after-error-hook)
3026 (throw 'abort key)))
3028 (define (quit . args)
3029 (apply throw 'quit args))
3033 ;;(define has-shown-backtrace-hint? #f) Defined by scm_init_backtrace ()
3035 ;; Replaced by C code:
3036 ;;(define (backtrace)
3037 ;; (if (fluid-ref the-last-stack)
3040 ;; (display-backtrace (fluid-ref the-last-stack) (current-output-port))
3042 ;; (if (and (not has-shown-backtrace-hint?)
3043 ;; (not (memq 'backtrace (debug-options-interface))))
3046 ;;"Type \"(debug-enable 'backtrace)\" if you would like a backtrace
3047 ;;automatically if an error occurs in the future.\n")
3048 ;; (set! has-shown-backtrace-hint? #t))))
3049 ;; (display "No backtrace available.\n")))
3051 (define (error-catching-repl r e p)
3052 (error-catching-loop
3054 (call-with-values (lambda () (e (r)))
3055 (lambda the-values (for-each p the-values))))))
3057 (define (gc-run-time)
3058 (cdr (assq 'gc-time-taken (gc-stats))))
3060 (define before-read-hook (make-hook))
3061 (define after-read-hook (make-hook))
3062 (define before-eval-hook (make-hook 1))
3063 (define after-eval-hook (make-hook 1))
3064 (define before-print-hook (make-hook 1))
3065 (define after-print-hook (make-hook 1))
3067 ;;; The default repl-reader function. We may override this if we've
3068 ;;; the readline library.
3070 (lambda (prompt . reader)
3071 (if (not (char-ready?))
3072 (display (if (string? prompt) prompt (prompt))))
3074 (run-hook before-read-hook)
3075 ((or (and (pair? reader) (car reader))
3076 (fluid-ref current-reader)
3078 (current-input-port))))
3080 (define (scm-style-repl)
3085 (repl-report-start-timing (lambda ()
3086 (set! start-gc-rt (gc-run-time))
3087 (set! start-rt (get-internal-run-time))))
3088 (repl-report (lambda ()
3090 (display (inexact->exact
3091 (* 1000 (/ (- (get-internal-run-time) start-rt)
3092 internal-time-units-per-second))))
3094 (display (inexact->exact
3095 (* 1000 (/ (- (gc-run-time) start-gc-rt)
3096 internal-time-units-per-second))))
3097 (display " msec in gc)\n")))
3099 (consume-trailing-whitespace
3101 (let ((ch (peek-char)))
3104 ((or (char=? ch #\space) (char=? ch #\tab))
3106 (consume-trailing-whitespace))
3107 ((char=? ch #\newline)
3111 (let ((prompt (cond ((string? scm-repl-prompt)
3113 ((thunk? scm-repl-prompt)
3115 (scm-repl-prompt "> ")
3117 (repl-reader prompt))))
3119 ;; As described in R4RS, the READ procedure updates the
3120 ;; port to point to the first character past the end of
3121 ;; the external representation of the object. This
3122 ;; means that it doesn't consume the newline typically
3123 ;; found after an expression. This means that, when
3124 ;; debugging Guile with GDB, GDB gets the newline, which
3125 ;; it often interprets as a "continue" command, making
3126 ;; breakpoints kind of useless. So, consume any
3127 ;; trailing newline here, as well as any whitespace
3129 ;; But not if EOF, for control-D.
3130 (if (not (eof-object? val))
3131 (consume-trailing-whitespace))
3132 (run-hook after-read-hook)
3133 (if (eof-object? val)
3135 (repl-report-start-timing)
3136 (if scm-repl-verbose
3139 (display ";;; EOF -- quitting")
3144 (-eval (lambda (sourc)
3145 (repl-report-start-timing)
3146 (run-hook before-eval-hook sourc)
3147 (let ((val (start-stack 'repl-stack
3148 ;; If you change this procedure
3149 ;; (primitive-eval), please also
3150 ;; modify the repl-stack case in
3151 ;; save-stack so that stack cutting
3152 ;; continues to work.
3153 (primitive-eval sourc))))
3154 (run-hook after-eval-hook sourc)
3158 (-print (let ((maybe-print (lambda (result)
3159 (if (or scm-repl-print-unspecified
3160 (not (unspecified? result)))
3165 (if (not scm-repl-silent)
3167 (run-hook before-print-hook result)
3168 (maybe-print result)
3169 (run-hook after-print-hook result)
3170 (if scm-repl-verbose
3174 (-quit (lambda (args)
3175 (if scm-repl-verbose
3177 (display ";;; QUIT executed, repl exitting")
3182 (let ((status (error-catching-repl -read
3190 ;;; {IOTA functions: generating lists of numbers}
3194 (let loop ((count (1- n)) (result '()))
3195 (if (< count 0) result
3196 (loop (1- count) (cons count result)))))
3202 ;;; Similar to `begin' but returns a list of the results of all constituent
3203 ;;; forms instead of the result of the last form.
3204 ;;; (The definition relies on the current left-to-right
3205 ;;; order of evaluation of operands in applications.)
3208 (defmacro collect forms
3215 ;;; with `continue' and `break'.
3218 ;; The inner `do' loop avoids re-establishing a catch every iteration,
3219 ;; that's only necessary if continue is actually used. A new key is
3220 ;; generated every time, so break and continue apply to their originating
3221 ;; `while' even when recursing.
3223 ;; FIXME: This macro is unintentionally unhygienic with respect to let,
3224 ;; make-symbol, do, throw, catch, lambda, and not.
3226 (define-macro (while cond . body)
3227 (let ((keyvar (make-symbol "while-keyvar")))
3228 `(let ((,keyvar (make-symbol "while-key")))
3232 (let ((break (lambda () (throw ,keyvar #t)))
3233 (continue (lambda () (throw ,keyvar #f))))
3244 ;;; {Module System Macros}
3247 ;; Return a list of expressions that evaluate to the appropriate
3248 ;; arguments for resolve-interface according to SPEC.
3252 (if (memq 'prefix (read-options))
3253 (error "boot-9 must be compiled with #:kw, not :kw")))
3255 (define (keyword-like-symbol->keyword sym)
3256 (symbol->keyword (string->symbol (substring (symbol->string sym) 1))))
3258 ;; FIXME: we really need to clean up the guts of the module system.
3259 ;; We can compile to something better than process-define-module.
3260 (define-syntax define-module
3262 (define (keyword-like? stx)
3263 (let ((dat (syntax->datum stx)))
3265 (eqv? (string-ref (symbol->string dat) 0) #\:))))
3266 (define (->keyword sym)
3267 (symbol->keyword (string->symbol (substring (symbol->string sym) 1))))
3269 (define (quotify-iface args)
3270 (let loop ((in args) (out '()))
3273 ;; The user wanted #:foo, but wrote :foo. Fix it.
3274 ((sym . in) (keyword-like? #'sym)
3275 (loop #`(#,(->keyword (syntax->datum #'sym)) . in) out))
3276 ((kw . in) (not (keyword? (syntax->datum #'kw)))
3277 (syntax-violation 'define-module "expected keyword arg" x #'kw))
3278 ((#:renamer renamer . in)
3279 (loop #'in (cons* #'renamer #:renamer out)))
3281 (loop #'in (cons* #''val #'kw out))))))
3283 (define (quotify args)
3284 ;; Just quote everything except #:use-module and #:use-syntax. We
3285 ;; need to know about all arguments regardless since we want to turn
3286 ;; symbols that look like keywords into real keywords, and the
3287 ;; keyword args in a define-module form are not regular
3288 ;; (i.e. no-backtrace doesn't take a value).
3289 (let loop ((in args) (out '()))
3292 ;; The user wanted #:foo, but wrote :foo. Fix it.
3293 ((sym . in) (keyword-like? #'sym)
3294 (loop #`(#,(->keyword (syntax->datum #'sym)) . in) out))
3295 ((kw . in) (not (keyword? (syntax->datum #'kw)))
3296 (syntax-violation 'define-module "expected keyword arg" x #'kw))
3297 ((#:no-backtrace . in)
3298 (loop #'in (cons #:no-backtrace out)))
3300 (loop #'in (cons #:pure out)))
3302 (syntax-violation 'define-module "keyword arg without value" x #'kw))
3303 ((use-module (name name* ...) . in)
3304 (and (memq (syntax->datum #'use-module) '(#:use-module #:use-syntax))
3305 (and-map symbol? (syntax->datum #'(name name* ...))))
3307 (cons* #''((name name* ...))
3310 ((use-module ((name name* ...) arg ...) . in)
3311 (and (memq (syntax->datum #'use-module) '(#:use-module #:use-syntax))
3312 (and-map symbol? (syntax->datum #'(name name* ...))))
3314 (cons* #`(list '(name name* ...) #,@(quotify-iface #'(arg ...)))
3317 ((#:autoload name bindings . in)
3318 (loop #'in (cons* #''bindings #''name #:autoload out)))
3320 (loop #'in (cons* #''val #'kw out))))))
3323 ((_ (name name* ...) arg ...)
3324 (with-syntax (((quoted-arg ...) (quotify #'(arg ...))))
3325 #'(eval-when (eval load compile expand)
3326 (let ((m (process-define-module
3327 (list '(name name* ...) quoted-arg ...))))
3328 (set-current-module m)
3331 ;; The guts of the use-modules macro. Add the interfaces of the named
3332 ;; modules to the use-list of the current module, in order.
3334 ;; This function is called by "modules.c". If you change it, be sure
3335 ;; to change scm_c_use_module as well.
3337 (define (process-use-modules module-interface-args)
3338 (let ((interfaces (map (lambda (mif-args)
3339 (or (apply resolve-interface mif-args)
3340 (error "no such module" mif-args)))
3341 module-interface-args)))
3342 (call-with-deferred-observers
3344 (module-use-interfaces! (current-module) interfaces)))))
3346 (define-syntax use-modules
3348 (define (keyword-like? stx)
3349 (let ((dat (syntax->datum stx)))
3351 (eqv? (string-ref (symbol->string dat) 0) #\:))))
3352 (define (->keyword sym)
3353 (symbol->keyword (string->symbol (substring (symbol->string sym) 1))))
3355 (define (quotify-iface args)
3356 (let loop ((in args) (out '()))
3359 ;; The user wanted #:foo, but wrote :foo. Fix it.
3360 ((sym . in) (keyword-like? #'sym)
3361 (loop #`(#,(->keyword (syntax->datum #'sym)) . in) out))
3362 ((kw . in) (not (keyword? (syntax->datum #'kw)))
3363 (syntax-violation 'define-module "expected keyword arg" x #'kw))
3364 ((#:renamer renamer . in)
3365 (loop #'in (cons* #'renamer #:renamer out)))
3367 (loop #'in (cons* #''val #'kw out))))))
3369 (define (quotify specs)
3370 (let lp ((in specs) (out '()))
3373 (((name name* ...) . in)
3374 (and-map symbol? (syntax->datum #'(name name* ...)))
3375 (lp #'in (cons #''((name name* ...)) out)))
3376 ((((name name* ...) arg ...) . in)
3377 (and-map symbol? (syntax->datum #'(name name* ...)))
3378 (with-syntax (((quoted-arg ...) (quotify-iface #'(arg ...))))
3379 (lp #'in (cons #`(list '(name name* ...) quoted-arg ...)
3384 (with-syntax (((quoted-args ...) (quotify #'(spec ...))))
3385 #'(eval-when (eval load compile expand)
3386 (process-use-modules (list quoted-args ...))
3389 (define-syntax use-syntax
3393 (eval-when (eval load compile expand)
3394 (issue-deprecation-warning
3395 "`use-syntax' is deprecated. Please contact guile-devel for more info."))
3396 (use-modules spec ...)))))
3398 (include-from-path "ice-9/r6rs-libraries")
3400 (define-syntax define-private
3405 (define-syntax define-public
3407 ((_ (name . args) . body)
3408 (define-public name (lambda args . body)))
3414 (define-syntax defmacro-public
3416 ((_ name args . body)
3418 (defmacro name args . body)
3419 (export-syntax name)))))
3421 ;; And now for the most important macro.
3424 ((_ formals body ...)
3425 (lambda formals body ...))))
3428 ;; Export a local variable
3430 ;; This function is called from "modules.c". If you change it, be
3431 ;; sure to update "modules.c" as well.
3433 (define (module-export! m names)
3434 (let ((public-i (module-public-interface m)))
3435 (for-each (lambda (name)
3436 (let* ((internal-name (if (pair? name) (car name) name))
3437 (external-name (if (pair? name) (cdr name) name))
3438 (var (module-ensure-local-variable! m internal-name)))
3439 (module-add! public-i external-name var)))
3442 (define (module-replace! m names)
3443 (let ((public-i (module-public-interface m)))
3444 (for-each (lambda (name)
3445 (let* ((internal-name (if (pair? name) (car name) name))
3446 (external-name (if (pair? name) (cdr name) name))
3447 (var (module-ensure-local-variable! m internal-name)))
3448 (set-object-property! var 'replace #t)
3449 (module-add! public-i external-name var)))
3452 ;; Export all local variables from a module
3454 (define (module-export-all! mod)
3455 (define (fresh-interface!)
3456 (let ((iface (make-module)))
3457 (set-module-name! iface (module-name mod))
3458 ;; for guile 2: (set-module-version! iface (module-version mod))
3459 (set-module-kind! iface 'interface)
3460 (set-module-public-interface! mod iface)
3462 (let ((iface (or (module-public-interface mod)
3463 (fresh-interface!))))
3464 (set-module-obarray! iface (module-obarray mod))))
3466 ;; Re-export a imported variable
3468 (define (module-re-export! m names)
3469 (let ((public-i (module-public-interface m)))
3470 (for-each (lambda (name)
3471 (let* ((internal-name (if (pair? name) (car name) name))
3472 (external-name (if (pair? name) (cdr name) name))
3473 (var (module-variable m internal-name)))
3475 (error "Undefined variable:" internal-name))
3476 ((eq? var (module-local-variable m internal-name))
3477 (error "re-exporting local variable:" internal-name))
3479 (module-add! public-i external-name var)))))
3482 (define-syntax export
3485 (eval-when (eval load compile expand)
3486 (call-with-deferred-observers
3488 (module-export! (current-module) '(name ...))))))))
3490 (define-syntax re-export
3493 (eval-when (eval load compile expand)
3494 (call-with-deferred-observers
3496 (module-re-export! (current-module) '(name ...))))))))
3498 (define-syntax export-syntax
3501 (export name ...))))
3503 (define-syntax re-export-syntax
3506 (re-export name ...))))
3508 (define load load-module)
3515 (define make-mutable-parameter
3516 (let ((make (lambda (fluid converter)
3520 (fluid-set! fluid (converter (car args))))))))
3521 (lambda (init . converter)
3522 (let ((fluid (make-fluid))
3523 (converter (if (null? converter)
3526 (fluid-set! fluid (converter init))
3527 (make fluid converter)))))
3531 ;;; {Handling of duplicate imported bindings}
3534 ;; Duplicate handlers take the following arguments:
3536 ;; module importing module
3537 ;; name conflicting name
3538 ;; int1 old interface where name occurs
3539 ;; val1 value of binding in old interface
3540 ;; int2 new interface where name occurs
3541 ;; val2 value of binding in new interface
3542 ;; var previous resolution or #f
3543 ;; val value of previous resolution
3545 ;; A duplicate handler can take three alternative actions:
3547 ;; 1. return #f => leave responsibility to next handler
3548 ;; 2. exit with an error
3549 ;; 3. return a variable resolving the conflict
3552 (define duplicate-handlers
3553 (let ((m (make-module 7)))
3555 (define (check module name int1 val1 int2 val2 var val)
3556 (scm-error 'misc-error
3558 "~A: `~A' imported from both ~A and ~A"
3559 (list (module-name module)
3565 (define (warn module name int1 val1 int2 val2 var val)
3566 (format (current-error-port)
3567 "WARNING: ~A: `~A' imported from both ~A and ~A\n"
3568 (module-name module)
3574 (define (replace module name int1 val1 int2 val2 var val)
3575 (let ((old (or (and var (object-property var 'replace) var)
3576 (module-variable int1 name)))
3577 (new (module-variable int2 name)))
3578 (if (object-property old 'replace)
3579 (and (or (eq? old new)
3580 (not (object-property new 'replace)))
3582 (and (object-property new 'replace)
3585 (define (warn-override-core module name int1 val1 int2 val2 var val)
3586 (and (eq? int1 the-scm-module)
3588 (format (current-error-port)
3589 "WARNING: ~A: imported module ~A overrides core binding `~A'\n"
3590 (module-name module)
3593 (module-local-variable int2 name))))
3595 (define (first module name int1 val1 int2 val2 var val)
3596 (or var (module-local-variable int1 name)))
3598 (define (last module name int1 val1 int2 val2 var val)
3599 (module-local-variable int2 name))
3601 (define (noop module name int1 val1 int2 val2 var val)
3604 (set-module-name! m 'duplicate-handlers)
3605 (set-module-kind! m 'interface)
3606 (module-define! m 'check check)
3607 (module-define! m 'warn warn)
3608 (module-define! m 'replace replace)
3609 (module-define! m 'warn-override-core warn-override-core)
3610 (module-define! m 'first first)
3611 (module-define! m 'last last)
3612 (module-define! m 'merge-generics noop)
3613 (module-define! m 'merge-accessors noop)
3616 (define (lookup-duplicates-handlers handler-names)
3618 (map (lambda (handler-name)
3619 (or (module-symbol-local-binding
3620 duplicate-handlers handler-name #f)
3621 (error "invalid duplicate handler name:"
3623 (if (list? handler-names)
3625 (list handler-names)))))
3627 (define default-duplicate-binding-procedures
3628 (make-mutable-parameter #f))
3630 (define default-duplicate-binding-handler
3631 (make-mutable-parameter '(replace warn-override-core warn last)
3632 (lambda (handler-names)
3633 (default-duplicate-binding-procedures
3634 (lookup-duplicates-handlers handler-names))
3639 ;;; {`cond-expand' for SRFI-0 support.}
3641 ;;; This syntactic form expands into different commands or
3642 ;;; definitions, depending on the features provided by the Scheme
3648 ;;; --> (cond-expand <cond-expand-clause>+)
3649 ;;; | (cond-expand <cond-expand-clause>* (else <command-or-definition>))
3650 ;;; <cond-expand-clause>
3651 ;;; --> (<feature-requirement> <command-or-definition>*)
3652 ;;; <feature-requirement>
3653 ;;; --> <feature-identifier>
3654 ;;; | (and <feature-requirement>*)
3655 ;;; | (or <feature-requirement>*)
3656 ;;; | (not <feature-requirement>)
3657 ;;; <feature-identifier>
3658 ;;; --> <a symbol which is the name or alias of a SRFI>
3660 ;;; Additionally, this implementation provides the
3661 ;;; <feature-identifier>s `guile' and `r5rs', so that programs can
3662 ;;; determine the implementation type and the supported standard.
3664 ;;; Currently, the following feature identifiers are supported:
3666 ;;; guile r5rs srfi-0 srfi-4 srfi-6 srfi-13 srfi-14 srfi-55 srfi-61
3668 ;;; Remember to update the features list when adding more SRFIs.
3671 (define %cond-expand-features
3672 ;; Adjust the above comment when changing this.
3676 srfi-0 ;; cond-expand itself
3677 srfi-4 ;; homogenous numeric vectors
3678 srfi-6 ;; open-input-string etc, in the guile core
3679 srfi-13 ;; string library
3680 srfi-14 ;; character sets
3681 srfi-55 ;; require-extension
3682 srfi-61 ;; general cond clause
3685 ;; This table maps module public interfaces to the list of features.
3687 (define %cond-expand-table (make-hash-table 31))
3689 ;; Add one or more features to the `cond-expand' feature list of the
3692 (define (cond-expand-provide module features)
3693 (let ((mod (module-public-interface module)))
3695 (hashq-set! %cond-expand-table mod
3696 (append (hashq-ref %cond-expand-table mod '())
3699 (define-macro (cond-expand . clauses)
3700 (let ((syntax-error (lambda (cl)
3701 (error "invalid clause in `cond-expand'" cl))))
3707 (or (memq clause %cond-expand-features)
3708 (let lp ((uses (module-uses (current-module))))
3711 (hashq-ref %cond-expand-table
3717 ((eq? 'and (car clause))
3718 (let lp ((l (cdr clause)))
3722 (and (test-clause (car l)) (lp (cdr l))))
3724 (syntax-error clause)))))
3725 ((eq? 'or (car clause))
3726 (let lp ((l (cdr clause)))
3730 (or (test-clause (car l)) (lp (cdr l))))
3732 (syntax-error clause)))))
3733 ((eq? 'not (car clause))
3734 (cond ((not (pair? (cdr clause)))
3735 (syntax-error clause))
3736 ((pair? (cddr clause))
3737 ((syntax-error clause))))
3738 (not (test-clause (cadr clause))))
3740 (syntax-error clause))))
3742 (syntax-error clause))))))
3743 (let lp ((c clauses))
3746 (error "Unfulfilled `cond-expand'"))
3749 ((not (pair? (car c)))
3750 (syntax-error (car c)))
3751 ((test-clause (caar c))
3752 `(begin ,@(cdar c)))
3753 ((eq? (caar c) 'else)
3756 `(begin ,@(cdar c)))
3760 ;; This procedure gets called from the startup code with a list of
3761 ;; numbers, which are the numbers of the SRFIs to be loaded on startup.
3763 (define (use-srfis srfis)
3764 (process-use-modules
3766 (list (list 'srfi (string->symbol
3767 (string-append "srfi-" (number->string num))))))
3772 ;;; srfi-55: require-extension
3775 (define-macro (require-extension extension-spec)
3776 ;; This macro only handles the srfi extension, which, at present, is
3777 ;; the only one defined by the standard.
3778 (if (not (pair? extension-spec))
3779 (scm-error 'wrong-type-arg "require-extension"
3780 "Not an extension: ~S" (list extension-spec) #f))
3781 (let ((extension (car extension-spec))
3782 (extension-args (cdr extension-spec)))
3785 (let ((use-list '()))
3788 (if (not (integer? i))
3789 (scm-error 'wrong-type-arg "require-extension"
3790 "Invalid srfi name: ~S" (list i) #f))
3791 (let ((srfi-sym (string->symbol
3792 (string-append "srfi-" (number->string i)))))
3793 (if (not (memq srfi-sym %cond-expand-features))
3794 (set! use-list (cons `(use-modules (srfi ,srfi-sym))
3797 (if (pair? use-list)
3798 ;; i.e. (begin (use-modules x) (use-modules y) (use-modules z))
3799 `(begin ,@(reverse! use-list)))))
3802 'wrong-type-arg "require-extension"
3803 "Not a recognized extension type: ~S" (list extension) #f)))))
3807 ;;; {Load emacs interface support if emacs option is given.}
3810 (define (named-module-use! user usee)
3811 (module-use! (resolve-module user) (resolve-interface usee)))
3813 (define (load-emacs-interface)
3814 (and (provided? 'debug-extensions)
3815 (debug-enable 'backtrace))
3816 (named-module-use! '(guile-user) '(ice-9 emacs)))
3820 (define using-readline?
3821 (let ((using-readline? (make-fluid)))
3822 (make-procedure-with-setter
3823 (lambda () (fluid-ref using-readline?))
3824 (lambda (v) (fluid-set! using-readline? v)))))
3827 (let ((guile-user-module (resolve-module '(guile-user))))
3829 ;; Load emacs interface support if emacs option is given.
3830 (if (and (module-defined? guile-user-module 'use-emacs-interface)
3831 (module-ref guile-user-module 'use-emacs-interface))
3832 (load-emacs-interface))
3834 ;; Use some convenient modules (in reverse order)
3836 (set-current-module guile-user-module)
3837 (process-use-modules
3842 (if (provided? 'regex)
3845 (if (provided? 'threads)
3846 '(((ice-9 threads)))
3848 ;; load debugger on demand
3849 (module-autoload! guile-user-module '(system vm debug) '(debug))
3851 ;; Note: SIGFPE, SIGSEGV and SIGBUS are actually "query-only" (see
3852 ;; scmsigs.c scm_sigaction_for_thread), so the handlers setup here have
3854 (let ((old-handlers #f)
3855 (start-repl (module-ref (resolve-interface '(system repl repl))
3857 (signals (if (provided? 'posix)
3858 `((,SIGINT . "User interrupt")
3859 (,SIGFPE . "Arithmetic error")
3861 . "Bad memory access (Segmentation violation)"))
3863 ;; no SIGBUS on mingw
3864 (if (defined? 'SIGBUS)
3865 (set! signals (acons SIGBUS "Bad memory access (bus error)"
3872 (let ((make-handler (lambda (msg)
3874 ;; Make a backup copy of the stack
3875 (fluid-set! before-signal-stack
3876 (fluid-ref the-last-stack))
3884 (map (lambda (sig-msg)
3885 (sigaction (car sig-msg)
3886 (make-handler (cdr sig-msg))))
3889 ;; the protected thunk.
3891 (let ((status (start-repl 'scheme)))
3892 (run-hook exit-hook)
3897 (map (lambda (sig-msg old-handler)
3898 (if (not (car old-handler))
3899 ;; restore original C handler.
3900 (sigaction (car sig-msg) #f)
3901 ;; restore Scheme handler, SIG_IGN or SIG_DFL.
3902 (sigaction (car sig-msg)
3904 (cdr old-handler))))
3905 signals old-handlers))))))
3907 ;;; This hook is run at the very end of an interactive session.
3909 (define exit-hook (make-hook))
3913 ;;; {Deprecated stuff}
3917 (module-use! the-scm-module (resolve-interface '(ice-9 deprecated))))
3921 ;;; Place the user in the guile-user module.
3924 ;;; FIXME: annotate ?
3925 ;; (define (syncase exp)
3926 ;; (with-fluids ((expansion-eval-closure
3927 ;; (module-eval-closure (current-module))))
3928 ;; (deannotate/source-properties (macroexpand (annotate exp)))))
3931 (module-use! the-scm-module (resolve-interface '(srfi srfi-4)))
3933 (define-module (guile-user)
3934 #:autoload (system base compile) (compile))
3936 ;; Remain in the `(guile)' module at compilation-time so that the
3937 ;; `-Wunused-toplevel' warning works as expected.
3938 (eval-when (compile) (set-current-module the-root-module))
3940 ;;; boot-9.scm ends here