3 ;;;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000 Free Software Foundation, Inc.
5 ;;;; This program is free software; you can redistribute it and/or modify
6 ;;;; it under the terms of the GNU General Public License as published by
7 ;;;; the Free Software Foundation; either version 2, or (at your option)
8 ;;;; any later version.
10 ;;;; This program is distributed in the hope that it will be useful,
11 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ;;;; GNU General Public License for more details.
15 ;;;; You should have received a copy of the GNU General Public License
16 ;;;; along with this software; see the file COPYING. If not, write to
17 ;;;; the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 ;;;; Boston, MA 02111-1307 USA
22 ;;; This file is the first thing loaded into Guile. It adds many mundane
23 ;;; definitions and a few that are interesting.
25 ;;; The module system (hence the hierarchical namespace) are defined in this
34 (if (not (memq sym *features*))
35 (set! *features* (cons sym *features*))))
37 ;;; Return #t iff FEATURE is available to this Guile interpreter.
38 ;;; In SLIB, provided? also checks to see if the module is available.
39 ;;; We should do that too, but don't.
40 (define (provided? feature)
41 (and (memq feature *features*) #t))
43 ;;; presumably deprecated.
44 (define feature? provided?)
46 ;;; let format alias simple-format until the more complete version is loaded
47 (define format simple-format)
52 (primitive-load-path "ice-9/r4rs.scm")
55 ;;; {Simple Debugging Tools}
59 ;; peek takes any number of arguments, writes them to the
60 ;; current ouput port, and returns the last argument.
61 ;; It is handy to wrap around an expression to look at
62 ;; a value each time is evaluated, e.g.:
64 ;; (+ 10 (troublesome-fn))
65 ;; => (+ 10 (pk 'troublesome-fn-returned (troublesome-fn)))
68 (define (peek . stuff)
73 (car (last-pair stuff)))
77 (define (warn . stuff)
78 (with-output-to-port (current-error-port)
81 (display ";;; WARNING ")
84 (car (last-pair stuff)))))
87 ;;; {Trivial Functions}
91 (define (1+ n) (+ n 1))
92 (define (-1+ n) (+ n -1))
94 (define return-it noop)
95 (define (and=> value procedure) (and value (procedure value)))
96 (define (make-hash-table k) (make-vector k '()))
98 ;;; apply-to-args is functionally redunant with apply and, worse,
99 ;;; is less general than apply since it only takes two arguments.
101 ;;; On the other hand, apply-to-args is a syntacticly convenient way to
102 ;;; perform binding in many circumstances when the "let" family of
103 ;;; of forms don't cut it. E.g.:
105 ;;; (apply-to-args (return-3d-mouse-coords)
110 (define (apply-to-args args fn) (apply fn args))
116 (define (ipow-by-squaring x k acc proc)
117 (cond ((zero? k) acc)
118 ((= 1 k) (proc acc x))
119 (else (ipow-by-squaring (proc x x)
121 (if (even? k) acc (proc acc x))
124 (define string-character-length string-length)
128 ;; A convenience function for combining flag bits. Like logior, but
129 ;; handles the cases of 0 and 1 arguments.
131 (define (flags . args)
134 ((null? (cdr args)) (car args))
135 (else (apply logior args))))
138 ;;; {Symbol Properties}
141 (define (symbol-property sym prop)
142 (let ((pair (assoc prop (symbol-pref sym))))
143 (and pair (cdr pair))))
145 (define (set-symbol-property! sym prop val)
146 (let ((pair (assoc prop (symbol-pref sym))))
149 (symbol-pset! sym (acons prop val (symbol-pref sym))))))
151 (define (symbol-property-remove! sym prop)
152 (let ((pair (assoc prop (symbol-pref sym))))
154 (symbol-pset! sym (delq! pair (symbol-pref sym))))))
156 ;;; {General Properties}
158 ;; This is a more modern interface to properties. It will replace all
159 ;; other property-like things eventually.
161 (define (make-object-property)
162 (let ((prop (primitive-make-property #f)))
163 (make-procedure-with-setter
164 (lambda (obj) (primitive-property-ref prop obj))
165 (lambda (obj val) (primitive-property-set! prop obj val)))))
169 ;;; {Line and Delimited I/O}
171 ;;; corresponds to SCM_LINE_INCREMENTORS in libguile.
172 (define scm-line-incrementors "\n")
174 (define (read-line! string . maybe-port)
175 (let* ((port (if (pair? maybe-port)
177 (current-input-port))))
178 (let* ((rv (%read-delimited! scm-line-incrementors
182 (terminator (car rv))
184 (cond ((and (= nchars 0)
185 (eof-object? terminator))
187 ((not terminator) #f)
190 (define (read-delimited! delims buf . args)
191 (let* ((num-args (length args))
192 (port (if (> num-args 0)
194 (current-input-port)))
195 (handle-delim (if (> num-args 1)
198 (start (if (> num-args 2)
201 (end (if (> num-args 3)
203 (string-length buf))))
204 (let* ((rv (%read-delimited! delims
206 (not (eq? handle-delim 'peek))
210 (terminator (car rv))
212 (cond ((or (not terminator) ; buffer filled
213 (eof-object? terminator))
215 (if (eq? handle-delim 'split)
216 (cons terminator terminator)
218 (if (eq? handle-delim 'split)
219 (cons nchars terminator)
224 ((concat) (string-set! buf (+ nchars start) terminator)
226 ((split) (cons nchars terminator))
227 (else (error "unexpected handle-delim value: "
230 (define (read-delimited delims . args)
231 (let* ((port (if (pair? args)
232 (let ((pt (car args)))
233 (set! args (cdr args))
235 (current-input-port)))
236 (handle-delim (if (pair? args)
239 (let loop ((substrings ())
241 (buf-size 100)) ; doubled each time through.
242 (let* ((buf (make-string buf-size))
243 (rv (%read-delimited! delims
245 (not (eq? handle-delim 'peek))
247 (terminator (car rv))
253 (cons (if (and (eq? handle-delim 'concat)
254 (not (eof-object? terminator)))
257 (cons (make-shared-substring buf 0 nchars)
259 (new-total (+ total-chars nchars)))
260 (cond ((not terminator)
262 (loop (cons (substring buf 0 nchars) substrings)
265 ((eof-object? terminator)
266 (if (zero? new-total)
267 (if (eq? handle-delim 'split)
268 (cons terminator terminator)
270 (if (eq? handle-delim 'split)
271 (cons (join-substrings) terminator)
275 ((trim peek concat) (join-substrings))
276 ((split) (cons (join-substrings) terminator))
279 (else (error "unexpected handle-delim value: "
280 handle-delim)))))))))
282 ;;; read-line [PORT [HANDLE-DELIM]] reads a newline-terminated string
283 ;;; from PORT. The return value depends on the value of HANDLE-DELIM,
284 ;;; which may be one of the symbols `trim', `concat', `peek' and
285 ;;; `split'. If it is `trim' (the default), the trailing newline is
286 ;;; removed and the string is returned. If `concat', the string is
287 ;;; returned with the trailing newline intact. If `peek', the newline
288 ;;; is left in the input port buffer and the string is returned. If
289 ;;; `split', the newline is split from the string and read-line
290 ;;; returns a pair consisting of the truncated string and the newline.
292 (define (read-line . args)
293 (let* ((port (if (null? args)
296 (handle-delim (if (> (length args) 1)
299 (line/delim (%read-line port))
300 (line (car line/delim))
301 (delim (cdr line/delim)))
305 ((concat) (if (and (string? line) (char? delim))
306 (string-append line (string delim))
308 ((peek) (if (char? delim)
309 (unread-char delim port))
312 (error "unexpected handle-delim value: " handle-delim)))))
318 (if (provided? 'array)
319 (primitive-load-path "ice-9/arrays.scm"))
325 (define (symbol->keyword symbol)
326 (make-keyword-from-dash-symbol (symbol-append '- symbol)))
328 (define (keyword->symbol kw)
329 (let ((sym (symbol->string (keyword-dash-symbol kw))))
330 (string->symbol (substring sym 1 (string-length sym)))))
332 (define (kw-arg-ref args kw)
333 (let ((rem (member kw args)))
334 (and rem (pair? (cdr rem)) (cadr rem))))
340 (define (struct-layout s)
341 (struct-ref (struct-vtable s) vtable-index-layout))
347 (define the-environment
352 (define the-root-environment (the-environment))
354 (define (environment-module env)
355 (let ((closure (and (pair? env) (car (last-pair env)))))
356 (and closure (procedure-property closure 'module))))
362 ;; Printing records: by default, records are printed as
364 ;; #<type-name field1: val1 field2: val2 ...>
366 ;; You can change that by giving a custom printing function to
367 ;; MAKE-RECORD-TYPE (after the list of field symbols). This function
368 ;; will be called like
370 ;; (<printer> object port)
372 ;; It should print OBJECT to PORT.
374 (define (inherit-print-state old-port new-port)
375 (if (get-print-state old-port)
376 (port-with-print-state new-port (get-print-state old-port))
379 ;; 0: type-name, 1: fields
380 (define record-type-vtable
381 (make-vtable-vtable "prpr" 0
383 (cond ((eq? s record-type-vtable)
384 (display "#<record-type-vtable>" p))
386 (display "#<record-type " p)
387 (display (record-type-name s) p)
390 (define (record-type? obj)
391 (and (struct? obj) (eq? record-type-vtable (struct-vtable obj))))
393 (define (make-record-type type-name fields . opt)
394 (let ((printer-fn (and (pair? opt) (car opt))))
395 (let ((struct (make-struct record-type-vtable 0
398 (map (lambda (f) "pw") fields)))
402 (display type-name p)
403 (let loop ((fields fields)
406 ((not (null? fields))
408 (display (car fields) p)
410 (display (struct-ref s off) p)
411 (loop (cdr fields) (+ 1 off)))))
414 (copy-tree fields))))
415 ;; Temporary solution: Associate a name to the record type descriptor
416 ;; so that the object system can create a wrapper class for it.
417 (set-struct-vtable-name! struct (if (symbol? type-name)
419 (string->symbol type-name)))
422 (define (record-type-name obj)
423 (if (record-type? obj)
424 (struct-ref obj vtable-offset-user)
425 (error 'not-a-record-type obj)))
427 (define (record-type-fields obj)
428 (if (record-type? obj)
429 (struct-ref obj (+ 1 vtable-offset-user))
430 (error 'not-a-record-type obj)))
432 (define (record-constructor rtd . opt)
433 (let ((field-names (if (pair? opt) (car opt) (record-type-fields rtd))))
434 (local-eval `(lambda ,field-names
435 (make-struct ',rtd 0 ,@(map (lambda (f)
436 (if (memq f field-names)
439 (record-type-fields rtd))))
440 the-root-environment)))
442 (define (record-predicate rtd)
443 (lambda (obj) (and (struct? obj) (eq? rtd (struct-vtable obj)))))
445 (define (record-accessor rtd field-name)
446 (let* ((pos (list-index (record-type-fields rtd) field-name)))
448 (error 'no-such-field field-name))
449 (local-eval `(lambda (obj)
450 (and (eq? ',rtd (record-type-descriptor obj))
451 (struct-ref obj ,pos)))
452 the-root-environment)))
454 (define (record-modifier rtd field-name)
455 (let* ((pos (list-index (record-type-fields rtd) field-name)))
457 (error 'no-such-field field-name))
458 (local-eval `(lambda (obj val)
459 (and (eq? ',rtd (record-type-descriptor obj))
460 (struct-set! obj ,pos val)))
461 the-root-environment)))
464 (define (record? obj)
465 (and (struct? obj) (record-type? (struct-vtable obj))))
467 (define (record-type-descriptor obj)
470 (error 'not-a-record obj)))
478 (define (->bool x) (not (not x)))
484 (define (symbol-append . args)
485 (string->symbol (apply string-append (map symbol->string args))))
487 (define (list->symbol . args)
488 (string->symbol (apply list->string args)))
490 (define (symbol . args)
491 (string->symbol (apply string args)))
497 (define (list-index l k)
503 (loop (+ n 1) (cdr l))))))
505 (define (make-list n . init)
506 (if (pair? init) (set! init (car init)))
507 (let loop ((answer '())
511 (loop (cons init answer) (- n 1)))))
515 ;;; {Multiple return values}
518 (make-record-type "values"
521 ;;; These two are needed internally in boot-9.scm.
522 ;;; They shouldn't be visible outside this module.
523 (define values? (record-predicate *values-rtd*))
524 (define get-values (record-accessor *values-rtd* 'values))
527 (let ((make-values (record-constructor *values-rtd*)))
529 (if (and (not (null? x))
534 (define call-with-values
535 (lambda (producer consumer)
536 (let ((result (producer)))
538 (apply consumer (get-values result))
539 (consumer result)))))
544 ;;; {and-map and or-map}
546 ;;; (and-map fn lst) is like (and (fn (car lst)) (fn (cadr lst)) (fn...) ...)
547 ;;; (or-map fn lst) is like (or (fn (car lst)) (fn (cadr lst)) (fn...) ...)
548 ;;; (map-in-order fn lst) is like (map fn lst) but definately in order of lst.
553 ;; Apply f to successive elements of l until exhaustion or f returns #f.
554 ;; If returning early, return #f. Otherwise, return the last value returned
555 ;; by f. If f has never been called because l is empty, return #t.
557 (define (and-map f lst)
558 (let loop ((result #t)
563 (loop (f (car l)) (cdr l))))))
567 ;; Apply f to successive elements of l until exhaustion or while f returns #f.
568 ;; If returning early, return the return value of f.
570 (define (or-map f lst)
571 (let loop ((result #f)
575 (loop (f (car l)) (cdr l))))))
579 (if (provided? 'posix)
580 (primitive-load-path "ice-9/posix.scm"))
582 (if (provided? 'socket)
583 (primitive-load-path "ice-9/networking.scm"))
586 (if (provided? 'posix)
590 (let ((port (catch 'system-error (lambda () (open-file str OPEN_READ))
592 (if port (begin (close-port port) #t)
595 (define file-is-directory?
596 (if (provided? 'posix)
598 (eq? (stat:type (stat str)) 'directory))
600 (let ((port (catch 'system-error
601 (lambda () (open-file (string-append str "/.")
604 (if port (begin (close-port port) #t)
607 (define (has-suffix? str suffix)
608 (let ((sufl (string-length suffix))
609 (sl (string-length str)))
611 (string=? (substring str (- sl sufl) sl) suffix))))
617 (define (error . args)
620 (scm-error 'misc-error #f "?" #f #f)
621 (let loop ((msg "~A")
623 (if (not (null? rest))
624 (loop (string-append msg " ~S")
626 (scm-error 'misc-error #f msg args #f)))))
628 ;; bad-throw is the hook that is called upon a throw to a an unhandled
629 ;; key (unless the throw has four arguments, in which case
630 ;; it's usually interpreted as an error throw.)
631 ;; If the key has a default handler (a throw-handler-default property),
632 ;; it is applied to the throw.
634 (define (bad-throw key . args)
635 (let ((default (symbol-property key 'throw-handler-default)))
636 (or (and default (apply default key args))
637 (apply error "unhandled-exception:" key args))))
641 (define (tm:sec obj) (vector-ref obj 0))
642 (define (tm:min obj) (vector-ref obj 1))
643 (define (tm:hour obj) (vector-ref obj 2))
644 (define (tm:mday obj) (vector-ref obj 3))
645 (define (tm:mon obj) (vector-ref obj 4))
646 (define (tm:year obj) (vector-ref obj 5))
647 (define (tm:wday obj) (vector-ref obj 6))
648 (define (tm:yday obj) (vector-ref obj 7))
649 (define (tm:isdst obj) (vector-ref obj 8))
650 (define (tm:gmtoff obj) (vector-ref obj 9))
651 (define (tm:zone obj) (vector-ref obj 10))
653 (define (set-tm:sec obj val) (vector-set! obj 0 val))
654 (define (set-tm:min obj val) (vector-set! obj 1 val))
655 (define (set-tm:hour obj val) (vector-set! obj 2 val))
656 (define (set-tm:mday obj val) (vector-set! obj 3 val))
657 (define (set-tm:mon obj val) (vector-set! obj 4 val))
658 (define (set-tm:year obj val) (vector-set! obj 5 val))
659 (define (set-tm:wday obj val) (vector-set! obj 6 val))
660 (define (set-tm:yday obj val) (vector-set! obj 7 val))
661 (define (set-tm:isdst obj val) (vector-set! obj 8 val))
662 (define (set-tm:gmtoff obj val) (vector-set! obj 9 val))
663 (define (set-tm:zone obj val) (vector-set! obj 10 val))
665 (define (tms:clock obj) (vector-ref obj 0))
666 (define (tms:utime obj) (vector-ref obj 1))
667 (define (tms:stime obj) (vector-ref obj 2))
668 (define (tms:cutime obj) (vector-ref obj 3))
669 (define (tms:cstime obj) (vector-ref obj 4))
671 (define (file-position . args) (apply ftell args))
672 (define (file-set-position . args) (apply fseek args))
674 (define (move->fdes fd/port fd)
675 (cond ((integer? fd/port)
676 (dup->fdes fd/port fd)
680 (primitive-move->fdes fd/port fd)
681 (set-port-revealed! fd/port 1)
684 (define (release-port-handle port)
685 (let ((revealed (port-revealed port)))
687 (set-port-revealed! port (- revealed 1)))))
689 (define (dup->port port/fd mode . maybe-fd)
690 (let ((port (fdopen (apply dup->fdes port/fd maybe-fd)
693 (set-port-revealed! port 1))
696 (define (dup->inport port/fd . maybe-fd)
697 (apply dup->port port/fd "r" maybe-fd))
699 (define (dup->outport port/fd . maybe-fd)
700 (apply dup->port port/fd "w" maybe-fd))
702 (define (dup port/fd . maybe-fd)
703 (if (integer? port/fd)
704 (apply dup->fdes port/fd maybe-fd)
705 (apply dup->port port/fd (port-mode port/fd) maybe-fd)))
707 (define (duplicate-port port modes)
708 (dup->port port modes))
710 (define (fdes->inport fdes)
711 (let loop ((rest-ports (fdes->ports fdes)))
712 (cond ((null? rest-ports)
713 (let ((result (fdopen fdes "r")))
714 (set-port-revealed! result 1)
716 ((input-port? (car rest-ports))
717 (set-port-revealed! (car rest-ports)
718 (+ (port-revealed (car rest-ports)) 1))
721 (loop (cdr rest-ports))))))
723 (define (fdes->outport fdes)
724 (let loop ((rest-ports (fdes->ports fdes)))
725 (cond ((null? rest-ports)
726 (let ((result (fdopen fdes "w")))
727 (set-port-revealed! result 1)
729 ((output-port? (car rest-ports))
730 (set-port-revealed! (car rest-ports)
731 (+ (port-revealed (car rest-ports)) 1))
734 (loop (cdr rest-ports))))))
736 (define (port->fdes port)
737 (set-port-revealed! port (+ (port-revealed port) 1))
740 (define (setenv name value)
742 (putenv (string-append name "=" value))
749 ;;; Here for backward compatability
751 (define scheme-file-suffix (lambda () ".scm"))
753 (define (in-vicinity vicinity file)
754 (let ((tail (let ((len (string-length vicinity)))
757 (string-ref vicinity (- len 1))))))
758 (string-append vicinity
766 ;;; {Help for scm_shell}
767 ;;; The argument-processing code used by Guile-based shells generates
768 ;;; Scheme code based on the argument list. This page contains help
769 ;;; functions for the code it generates.
771 (define (command-line) (program-arguments))
773 ;; This is mostly for the internal use of the code generated by
774 ;; scm_compile_shell_switches.
775 (define (load-user-init)
776 (let* ((home (or (getenv "HOME")
777 (false-if-exception (passwd:dir (getpwuid (getuid))))
778 "/")) ;; fallback for cygwin etc.
779 (init-file (in-vicinity home ".guile")))
780 (if (file-exists? init-file)
781 (primitive-load init-file))))
784 ;;; {Loading by paths}
786 ;;; Load a Scheme source file named NAME, searching for it in the
787 ;;; directories listed in %load-path, and applying each of the file
788 ;;; name extensions listed in %load-extensions.
789 (define (load-from-path name)
790 (start-stack 'load-stack
791 (primitive-load-path name)))
795 ;;; {Transcendental Functions}
797 ;;; Derived from "Transcen.scm", Complex trancendental functions for SCM.
798 ;;; Written by Jerry D. Hedden, (C) FSF.
799 ;;; See the file `COPYING' for terms applying to this program.
803 (if (real? z) ($exp z)
804 (make-polar ($exp (real-part z)) (imag-part z))))
807 (if (and (real? z) (>= z 0))
809 (make-rectangular ($log (magnitude z)) (angle z))))
813 (if (negative? z) (make-rectangular 0 ($sqrt (- z)))
815 (make-polar ($sqrt (magnitude z)) (/ (angle z) 2))))
818 (let ((integer-expt integer-expt))
823 (/ 1 (integer-expt z1 (- z2)))))
824 ((and (real? z2) (real? z1) (>= z1 0))
827 (exp (* z2 (log z1))))))))
830 (if (real? z) ($sinh z)
831 (let ((x (real-part z)) (y (imag-part z)))
832 (make-rectangular (* ($sinh x) ($cos y))
833 (* ($cosh x) ($sin y))))))
835 (if (real? z) ($cosh z)
836 (let ((x (real-part z)) (y (imag-part z)))
837 (make-rectangular (* ($cosh x) ($cos y))
838 (* ($sinh x) ($sin y))))))
840 (if (real? z) ($tanh z)
841 (let* ((x (* 2 (real-part z)))
842 (y (* 2 (imag-part z)))
843 (w (+ ($cosh x) ($cos y))))
844 (make-rectangular (/ ($sinh x) w) (/ ($sin y) w)))))
847 (if (real? z) ($asinh z)
848 (log (+ z (sqrt (+ (* z z) 1))))))
851 (if (and (real? z) (>= z 1))
853 (log (+ z (sqrt (- (* z z) 1))))))
856 (if (and (real? z) (> z -1) (< z 1))
858 (/ (log (/ (+ 1 z) (- 1 z))) 2)))
861 (if (real? z) ($sin z)
862 (let ((x (real-part z)) (y (imag-part z)))
863 (make-rectangular (* ($sin x) ($cosh y))
864 (* ($cos x) ($sinh y))))))
866 (if (real? z) ($cos z)
867 (let ((x (real-part z)) (y (imag-part z)))
868 (make-rectangular (* ($cos x) ($cosh y))
869 (- (* ($sin x) ($sinh y)))))))
871 (if (real? z) ($tan z)
872 (let* ((x (* 2 (real-part z)))
873 (y (* 2 (imag-part z)))
874 (w (+ ($cos x) ($cosh y))))
875 (make-rectangular (/ ($sin x) w) (/ ($sinh y) w)))))
878 (if (and (real? z) (>= z -1) (<= z 1))
880 (* -i (asinh (* +i z)))))
883 (if (and (real? z) (>= z -1) (<= z 1))
885 (+ (/ (angle -1) 2) (* +i (asinh (* +i z))))))
889 (if (real? z) ($atan z)
890 (/ (log (/ (- +i z) (+ +i z))) +2i))
894 (/ (log arg) (log 10)))
898 ;;; {Reader Extensions}
901 ;;; Reader code for various "#c" forms.
904 (read-hash-extend #\' (lambda (c port)
906 (read-hash-extend #\. (lambda (c port)
907 (eval (read port) (interaction-environment))))
910 ;;; {Command Line Options}
913 (define (get-option argv kw-opts kw-args return)
918 ((or (not (eq? #\- (string-ref (car argv) 0)))
919 (eq? (string-length (car argv)) 1))
920 (return 'normal-arg (car argv) (cdr argv)))
922 ((eq? #\- (string-ref (car argv) 1))
923 (let* ((kw-arg-pos (or (string-index (car argv) #\=)
924 (string-length (car argv))))
925 (kw (symbol->keyword (substring (car argv) 2 kw-arg-pos)))
926 (kw-opt? (member kw kw-opts))
927 (kw-arg? (member kw kw-args))
928 (arg (or (and (not (eq? kw-arg-pos (string-length (car argv))))
929 (substring (car argv)
931 (string-length (car argv))))
933 (begin (set! argv (cdr argv)) (car argv))))))
934 (if (or kw-opt? kw-arg?)
935 (return kw arg (cdr argv))
936 (return 'usage-error kw (cdr argv)))))
939 (let* ((char (substring (car argv) 1 2))
940 (kw (symbol->keyword char)))
944 (let* ((rest-car (substring (car argv) 2 (string-length (car argv))))
945 (new-argv (if (= 0 (string-length rest-car))
947 (cons (string-append "-" rest-car) (cdr argv)))))
948 (return kw #f new-argv)))
951 (let* ((rest-car (substring (car argv) 2 (string-length (car argv))))
952 (arg (if (= 0 (string-length rest-car))
955 (new-argv (if (= 0 (string-length rest-car))
958 (return kw arg new-argv)))
960 (else (return 'usage-error kw argv)))))))
962 (define (for-next-option proc argv kw-opts kw-args)
963 (let loop ((argv argv))
964 (get-option argv kw-opts kw-args
965 (lambda (opt opt-arg argv)
966 (and opt (proc opt opt-arg argv loop))))))
968 (define (display-usage-report kw-desc)
971 (or (eq? (car kw) #t)
974 (help (cadr opt-desc))
975 (opts (car opt-desc))
976 (opts-proper (if (string? (car opts)) (cdr opts) opts))
977 (arg-name (if (string? (car opts))
978 (string-append "<" (car opts) ">")
980 (left-part (string-append
981 (with-output-to-string
983 (map (lambda (x) (display (keyword-symbol x)) (display " "))
986 (middle-part (if (and (< (string-length left-part) 30)
987 (< (string-length help) 40))
988 (make-string (- 30 (string-length left-part)) #\ )
991 (display middle-part)
998 (define (transform-usage-lambda cases)
999 (let* ((raw-usage (delq! 'else (map car cases)))
1000 (usage-sans-specials (map (lambda (x)
1001 (or (and (not (list? x)) x)
1002 (and (symbol? (car x)) #t)
1003 (and (boolean? (car x)) #t)
1006 (usage-desc (delq! #t usage-sans-specials))
1007 (kw-desc (map car usage-desc))
1008 (kw-opts (apply append (map (lambda (x) (and (not (string? (car x))) x)) kw-desc)))
1009 (kw-args (apply append (map (lambda (x) (and (string? (car x)) (cdr x))) kw-desc)))
1010 (transmogrified-cases (map (lambda (case)
1011 (cons (let ((opts (car case)))
1012 (if (or (boolean? opts) (eq? 'else opts))
1015 ((symbol? (car opts)) opts)
1016 ((boolean? (car opts)) opts)
1017 ((string? (caar opts)) (cdar opts))
1018 (else (car opts)))))
1021 `(let ((%display-usage (lambda () (display-usage-report ',usage-desc))))
1023 (let %next-arg ((%argv %argv))
1027 (lambda (%opt %arg %new-argv)
1029 ,@ transmogrified-cases))))))))
1034 ;;; {Low Level Modules}
1036 ;;; These are the low level data structures for modules.
1038 ;;; !!! warning: The interface to lazy binder procedures is going
1039 ;;; to be changed in an incompatible way to permit all the basic
1040 ;;; module ops to be virtualized.
1042 ;;; (make-module size use-list lazy-binding-proc) => module
1043 ;;; module-{obarray,uses,binder}[|-set!]
1044 ;;; (module? obj) => [#t|#f]
1045 ;;; (module-locally-bound? module symbol) => [#t|#f]
1046 ;;; (module-bound? module symbol) => [#t|#f]
1047 ;;; (module-symbol-locally-interned? module symbol) => [#t|#f]
1048 ;;; (module-symbol-interned? module symbol) => [#t|#f]
1049 ;;; (module-local-variable module symbol) => [#<variable ...> | #f]
1050 ;;; (module-variable module symbol) => [#<variable ...> | #f]
1051 ;;; (module-symbol-binding module symbol opt-value)
1052 ;;; => [ <obj> | opt-value | an error occurs ]
1053 ;;; (module-make-local-var! module symbol) => #<variable...>
1054 ;;; (module-add! module symbol var) => unspecified
1055 ;;; (module-remove! module symbol) => unspecified
1056 ;;; (module-for-each proc module) => unspecified
1057 ;;; (make-scm-module) => module ; a lazy copy of the symhash module
1058 ;;; (set-current-module module) => unspecified
1059 ;;; (current-module) => #<module...>
1064 ;;; {Printing Modules}
1065 ;; This is how modules are printed. You can re-define it.
1066 ;; (Redefining is actually more complicated than simply redefining
1067 ;; %print-module because that would only change the binding and not
1068 ;; the value stored in the vtable that determines how record are
1071 (define (%print-module mod port) ; unused args: depth length style table)
1073 (display (or (module-kind mod) "module") port)
1074 (let ((name (module-name mod)))
1078 (display name port))))
1080 (display (number->string (object-address mod) 16) port)
1085 ;; A module is characterized by an obarray in which local symbols
1086 ;; are interned, a list of modules, "uses", from which non-local
1087 ;; bindings can be inherited, and an optional lazy-binder which
1088 ;; is a (CLOSURE module symbol) which, as a last resort, can provide
1089 ;; bindings that would otherwise not be found locally in the module.
1091 ;; NOTE: If you change here, you also need to change libguile/modules.h.
1094 (make-record-type 'module
1095 '(obarray uses binder eval-closure transformer name kind
1096 observers weak-observers observer-id)
1099 ;; make-module &opt size uses binder
1101 ;; Create a new module, perhaps with a particular size of obarray,
1102 ;; initial uses list, or binding procedure.
1107 (define (parse-arg index default)
1108 (if (> (length args) index)
1109 (list-ref args index)
1112 (if (> (length args) 3)
1113 (error "Too many args to make-module." args))
1115 (let ((size (parse-arg 0 1021))
1116 (uses (parse-arg 1 '()))
1117 (binder (parse-arg 2 #f)))
1119 (if (not (integer? size))
1120 (error "Illegal size to make-module." size))
1121 (if (not (and (list? uses)
1122 (and-map module? uses)))
1123 (error "Incorrect use list." uses))
1124 (if (and binder (not (procedure? binder)))
1126 "Lazy-binder expected to be a procedure or #f." binder))
1128 (let ((module (module-constructor (make-vector size '())
1129 uses binder #f #f #f #f
1131 (make-weak-value-hash-table 31)
1134 ;; We can't pass this as an argument to module-constructor,
1135 ;; because we need it to close over a pointer to the module
1137 (set-module-eval-closure! module (standard-eval-closure module))
1141 (define module-constructor (record-constructor module-type))
1142 (define module-obarray (record-accessor module-type 'obarray))
1143 (define set-module-obarray! (record-modifier module-type 'obarray))
1144 (define module-uses (record-accessor module-type 'uses))
1145 (define set-module-uses! (record-modifier module-type 'uses))
1146 (define module-binder (record-accessor module-type 'binder))
1147 (define set-module-binder! (record-modifier module-type 'binder))
1149 ;; NOTE: This binding is used in libguile/modules.c.
1150 (define module-eval-closure (record-accessor module-type 'eval-closure))
1152 (define module-transformer (record-accessor module-type 'transformer))
1153 (define set-module-transformer! (record-modifier module-type 'transformer))
1154 (define module-name (record-accessor module-type 'name))
1155 (define set-module-name! (record-modifier module-type 'name))
1156 (define module-kind (record-accessor module-type 'kind))
1157 (define set-module-kind! (record-modifier module-type 'kind))
1158 (define module-observers (record-accessor module-type 'observers))
1159 (define set-module-observers! (record-modifier module-type 'observers))
1160 (define module-weak-observers (record-accessor module-type 'weak-observers))
1161 (define module-observer-id (record-accessor module-type 'observer-id))
1162 (define set-module-observer-id! (record-modifier module-type 'observer-id))
1163 (define module? (record-predicate module-type))
1165 (define set-module-eval-closure!
1166 (let ((setter (record-modifier module-type 'eval-closure)))
1167 (lambda (module closure)
1168 (setter module closure)
1169 ;; Make it possible to lookup the module from the environment.
1170 ;; This implementation is correct since an eval closure can belong
1171 ;; to maximally one module.
1172 (set-procedure-property! closure 'module module))))
1174 ;;; This procedure is depreated
1176 (define eval-in-module eval)
1179 ;;; {Observer protocol}
1182 (define (module-observe module proc)
1183 (set-module-observers! module (cons proc (module-observers module)))
1186 (define (module-observe-weak module proc)
1187 (let ((id (module-observer-id module)))
1188 (hash-set! (module-weak-observers module) id proc)
1189 (set-module-observer-id! module (+ 1 id))
1192 (define (module-unobserve token)
1193 (let ((module (car token))
1196 (hash-remove! (module-weak-observers module) id)
1197 (set-module-observers! module (delq1! id (module-observers module)))))
1200 (define (module-modified m)
1201 (for-each (lambda (proc) (proc m)) (module-observers m))
1202 (hash-fold (lambda (id proc res) (proc m)) #f (module-weak-observers m)))
1205 ;;; {Module Searching in General}
1207 ;;; We sometimes want to look for properties of a symbol
1208 ;;; just within the obarray of one module. If the property
1209 ;;; holds, then it is said to hold ``locally'' as in, ``The symbol
1210 ;;; DISPLAY is locally rebound in the module `safe-guile'.''
1213 ;;; Other times, we want to test for a symbol property in the obarray
1214 ;;; of M and, if it is not found there, try each of the modules in the
1215 ;;; uses list of M. This is the normal way of testing for some
1216 ;;; property, so we state these properties without qualification as
1217 ;;; in: ``The symbol 'fnord is interned in module M because it is
1218 ;;; interned locally in module M2 which is a member of the uses list
1222 ;; module-search fn m
1224 ;; return the first non-#f result of FN applied to M and then to
1225 ;; the modules in the uses of m, and so on recursively. If all applications
1226 ;; return #f, then so does this function.
1228 (define (module-search fn m v)
1231 (or (module-search fn (car pos) v)
1234 (loop (module-uses m))))
1237 ;;; {Is a symbol bound in a module?}
1239 ;;; Symbol S in Module M is bound if S is interned in M and if the binding
1240 ;;; of S in M has been set to some well-defined value.
1243 ;; module-locally-bound? module symbol
1245 ;; Is a symbol bound (interned and defined) locally in a given module?
1247 (define (module-locally-bound? m v)
1248 (let ((var (module-local-variable m v)))
1250 (variable-bound? var))))
1252 ;; module-bound? module symbol
1254 ;; Is a symbol bound (interned and defined) anywhere in a given module
1257 (define (module-bound? m v)
1258 (module-search module-locally-bound? m v))
1260 ;;; {Is a symbol interned in a module?}
1262 ;;; Symbol S in Module M is interned if S occurs in
1263 ;;; of S in M has been set to some well-defined value.
1265 ;;; It is possible to intern a symbol in a module without providing
1266 ;;; an initial binding for the corresponding variable. This is done
1268 ;;; (module-add! module symbol (make-undefined-variable))
1270 ;;; In that case, the symbol is interned in the module, but not
1271 ;;; bound there. The unbound symbol shadows any binding for that
1272 ;;; symbol that might otherwise be inherited from a member of the uses list.
1275 (define (module-obarray-get-handle ob key)
1276 ((if (symbol? key) hashq-get-handle hash-get-handle) ob key))
1278 (define (module-obarray-ref ob key)
1279 ((if (symbol? key) hashq-ref hash-ref) ob key))
1281 (define (module-obarray-set! ob key val)
1282 ((if (symbol? key) hashq-set! hash-set!) ob key val))
1284 (define (module-obarray-remove! ob key)
1285 ((if (symbol? key) hashq-remove! hash-remove!) ob key))
1287 ;; module-symbol-locally-interned? module symbol
1289 ;; is a symbol interned (not neccessarily defined) locally in a given module
1290 ;; or its uses? Interned symbols shadow inherited bindings even if
1291 ;; they are not themselves bound to a defined value.
1293 (define (module-symbol-locally-interned? m v)
1294 (not (not (module-obarray-get-handle (module-obarray m) v))))
1296 ;; module-symbol-interned? module symbol
1298 ;; is a symbol interned (not neccessarily defined) anywhere in a given module
1299 ;; or its uses? Interned symbols shadow inherited bindings even if
1300 ;; they are not themselves bound to a defined value.
1302 (define (module-symbol-interned? m v)
1303 (module-search module-symbol-locally-interned? m v))
1306 ;;; {Mapping modules x symbols --> variables}
1309 ;; module-local-variable module symbol
1310 ;; return the local variable associated with a MODULE and SYMBOL.
1312 ;;; This function is very important. It is the only function that can
1313 ;;; return a variable from a module other than the mutators that store
1314 ;;; new variables in modules. Therefore, this function is the location
1315 ;;; of the "lazy binder" hack.
1317 ;;; If symbol is defined in MODULE, and if the definition binds symbol
1318 ;;; to a variable, return that variable object.
1320 ;;; If the symbols is not found at first, but the module has a lazy binder,
1321 ;;; then try the binder.
1323 ;;; If the symbol is not found at all, return #f.
1325 (define (module-local-variable m v)
1328 (let ((b (module-obarray-ref (module-obarray m) v)))
1329 (or (and (variable? b) b)
1330 (and (module-binder m)
1331 ((module-binder m) m v #f)))))
1334 ;; module-variable module symbol
1336 ;; like module-local-variable, except search the uses in the
1337 ;; case V is not found in M.
1339 ;; NOTE: This function is superseded with C code (see modules.c)
1340 ;;; when using the standard eval closure.
1342 (define (module-variable m v)
1343 (module-search module-local-variable m v))
1346 ;;; {Mapping modules x symbols --> bindings}
1348 ;;; These are similar to the mapping to variables, except that the
1349 ;;; variable is dereferenced.
1352 ;; module-symbol-binding module symbol opt-value
1354 ;; return the binding of a variable specified by name within
1355 ;; a given module, signalling an error if the variable is unbound.
1356 ;; If the OPT-VALUE is passed, then instead of signalling an error,
1357 ;; return OPT-VALUE.
1359 (define (module-symbol-local-binding m v . opt-val)
1360 (let ((var (module-local-variable m v)))
1363 (if (not (null? opt-val))
1365 (error "Locally unbound variable." v)))))
1367 ;; module-symbol-binding module symbol opt-value
1369 ;; return the binding of a variable specified by name within
1370 ;; a given module, signalling an error if the variable is unbound.
1371 ;; If the OPT-VALUE is passed, then instead of signalling an error,
1372 ;; return OPT-VALUE.
1374 (define (module-symbol-binding m v . opt-val)
1375 (let ((var (module-variable m v)))
1378 (if (not (null? opt-val))
1380 (error "Unbound variable." v)))))
1384 ;;; {Adding Variables to Modules}
1389 ;; module-make-local-var! module symbol
1391 ;; ensure a variable for V in the local namespace of M.
1392 ;; If no variable was already there, then create a new and uninitialzied
1395 (define (module-make-local-var! m v)
1396 (or (let ((b (module-obarray-ref (module-obarray m) v)))
1401 (and (module-binder m)
1402 ((module-binder m) m v #t))
1404 (let ((answer (make-undefined-variable v)))
1405 (module-obarray-set! (module-obarray m) v answer)
1409 ;; module-add! module symbol var
1411 ;; ensure a particular variable for V in the local namespace of M.
1413 (define (module-add! m v var)
1414 (if (not (variable? var))
1415 (error "Bad variable to module-add!" var))
1416 (module-obarray-set! (module-obarray m) v var)
1417 (module-modified m))
1421 ;; make sure that a symbol is undefined in the local namespace of M.
1423 (define (module-remove! m v)
1424 (module-obarray-remove! (module-obarray m) v)
1425 (module-modified m))
1427 (define (module-clear! m)
1428 (vector-fill! (module-obarray m) '())
1429 (module-modified m))
1431 ;; MODULE-FOR-EACH -- exported
1433 ;; Call PROC on each symbol in MODULE, with arguments of (SYMBOL VARIABLE).
1435 (define (module-for-each proc module)
1436 (let ((obarray (module-obarray module)))
1437 (do ((index 0 (+ index 1))
1438 (end (vector-length obarray)))
1442 (proc (car bucket) (cdr bucket)))
1443 (vector-ref obarray index)))))
1446 (define (module-map proc module)
1447 (let* ((obarray (module-obarray module))
1448 (end (vector-length obarray)))
1456 (map (lambda (bucket)
1457 (proc (car bucket) (cdr bucket)))
1458 (vector-ref obarray i))
1462 ;;; {Low Level Bootstrapping}
1467 ;; A root module uses the symhash table (the system's privileged
1468 ;; obarray). Being inside a root module is like using SCM without
1469 ;; any module system.
1473 (define (root-module-closure m s define?)
1474 (let ((bi (and (symbol-interned? #f s)
1475 (builtin-variable s))))
1477 (or define? (variable-bound? bi))
1479 (module-add! m s bi)
1482 (define (make-root-module)
1483 (make-module 1019 '() root-module-closure))
1488 ;; An scm module is a module into which the lazy binder copies
1489 ;; variable bindings from the system symhash table. The mapping is
1490 ;; one way only; newly introduced bindings in an scm module are not
1491 ;; copied back into the system symhash table (and can be used to override
1492 ;; bindings from the symhash table).
1495 (define (scm-module-closure m s define?)
1496 (let ((bi (and (symbol-interned? #f s)
1497 (builtin-variable s))))
1499 (variable-bound? bi)
1501 (module-add! m s bi)
1504 (define (make-scm-module)
1505 (make-module 1019 '() scm-module-closure))
1511 ;; NOTE: This binding is used in libguile/modules.c.
1513 (define the-module (make-fluid))
1515 ;; scm:eval-transformer
1517 ;;(define scm:eval-transformer (make-fluid)) ; initialized in eval.c.
1519 ;; set-current-module module
1521 ;; set the current module as viewed by the normalizer.
1523 ;; NOTE: This binding is used in libguile/modules.c.
1525 (define (set-current-module m)
1526 (fluid-set! the-module m)
1529 ;; *top-level-lookup-closure* is now deprecated
1530 (fluid-set! *top-level-lookup-closure*
1531 (module-eval-closure (fluid-ref the-module)))
1532 (fluid-set! scm:eval-transformer (module-transformer (fluid-ref the-module))))
1533 (fluid-set! *top-level-lookup-closure* #f)))
1538 ;; return the current module as viewed by the normalizer.
1540 (define (current-module) (fluid-ref the-module))
1542 ;;; {Module-based Loading}
1545 (define (save-module-excursion thunk)
1546 (let ((inner-module (current-module))
1548 (dynamic-wind (lambda ()
1549 (set! outer-module (current-module))
1550 (set-current-module inner-module)
1551 (set! inner-module #f))
1554 (set! inner-module (current-module))
1555 (set-current-module outer-module)
1556 (set! outer-module #f)))))
1558 (define basic-load load)
1560 (define (load-module filename)
1561 (save-module-excursion
1563 (let ((oldname (and (current-load-port)
1564 (port-filename (current-load-port)))))
1565 (basic-load (if (and oldname
1566 (> (string-length filename) 0)
1567 (not (char=? (string-ref filename 0) #\/))
1568 (not (string=? (dirname oldname) ".")))
1569 (string-append (dirname oldname) "/" filename)
1574 ;;; {MODULE-REF -- exported}
1576 ;; Returns the value of a variable called NAME in MODULE or any of its
1577 ;; used modules. If there is no such variable, then if the optional third
1578 ;; argument DEFAULT is present, it is returned; otherwise an error is signaled.
1580 (define (module-ref module name . rest)
1581 (let ((variable (module-variable module name)))
1582 (if (and variable (variable-bound? variable))
1583 (variable-ref variable)
1585 (error "No variable named" name 'in module)
1586 (car rest) ; default value
1589 ;; MODULE-SET! -- exported
1591 ;; Sets the variable called NAME in MODULE (or in a module that MODULE uses)
1592 ;; to VALUE; if there is no such variable, an error is signaled.
1594 (define (module-set! module name value)
1595 (let ((variable (module-variable module name)))
1597 (variable-set! variable value)
1598 (error "No variable named" name 'in module))))
1600 ;; MODULE-DEFINE! -- exported
1602 ;; Sets the variable called NAME in MODULE to VALUE; if there is no such
1603 ;; variable, it is added first.
1605 (define (module-define! module name value)
1606 (let ((variable (module-local-variable module name)))
1609 (variable-set! variable value)
1610 (module-modified module))
1611 (module-add! module name (make-variable value name)))))
1613 ;; MODULE-DEFINED? -- exported
1615 ;; Return #t iff NAME is defined in MODULE (or in a module that MODULE
1618 (define (module-defined? module name)
1619 (let ((variable (module-variable module name)))
1620 (and variable (variable-bound? variable))))
1622 ;; MODULE-USE! module interface
1624 ;; Add INTERFACE to the list of interfaces used by MODULE.
1626 (define (module-use! module interface)
1627 (set-module-uses! module
1628 (cons interface (delq! interface (module-uses module))))
1629 (module-modified module))
1632 ;;; {Recursive Namespaces}
1635 ;;; A hierarchical namespace emerges if we consider some module to be
1636 ;;; root, and variables bound to modules as nested namespaces.
1638 ;;; The routines in this file manage variable names in hierarchical namespace.
1639 ;;; Each variable name is a list of elements, looked up in successively nested
1642 ;;; (nested-ref some-root-module '(foo bar baz))
1643 ;;; => <value of a variable named baz in the module bound to bar in
1644 ;;; the module bound to foo in some-root-module>
1649 ;;; ;; a-root is a module
1650 ;;; ;; name is a list of symbols
1652 ;;; nested-ref a-root name
1653 ;;; nested-set! a-root name val
1654 ;;; nested-define! a-root name val
1655 ;;; nested-remove! a-root name
1658 ;;; (current-module) is a natural choice for a-root so for convenience there are
1661 ;;; local-ref name == nested-ref (current-module) name
1662 ;;; local-set! name val == nested-set! (current-module) name val
1663 ;;; local-define! name val == nested-define! (current-module) name val
1664 ;;; local-remove! name == nested-remove! (current-module) name
1668 (define (nested-ref root names)
1669 (let loop ((cur root)
1673 ((not (module? cur)) #f)
1674 (else (loop (module-ref cur (car elts) #f) (cdr elts))))))
1676 (define (nested-set! root names val)
1677 (let loop ((cur root)
1679 (if (null? (cdr elts))
1680 (module-set! cur (car elts) val)
1681 (loop (module-ref cur (car elts)) (cdr elts)))))
1683 (define (nested-define! root names val)
1684 (let loop ((cur root)
1686 (if (null? (cdr elts))
1687 (module-define! cur (car elts) val)
1688 (loop (module-ref cur (car elts)) (cdr elts)))))
1690 (define (nested-remove! root names)
1691 (let loop ((cur root)
1693 (if (null? (cdr elts))
1694 (module-remove! cur (car elts))
1695 (loop (module-ref cur (car elts)) (cdr elts)))))
1697 (define (local-ref names) (nested-ref (current-module) names))
1698 (define (local-set! names val) (nested-set! (current-module) names val))
1699 (define (local-define names val) (nested-define! (current-module) names val))
1700 (define (local-remove names) (nested-remove! (current-module) names))
1704 ;;; {The (app) module}
1706 ;;; The root of conventionally named objects not directly in the top level.
1709 ;;; (app modules guile)
1711 ;;; The directory of all modules and the standard root module.
1714 (define (module-public-interface m)
1715 (module-ref m '%module-public-interface #f))
1716 (define (set-module-public-interface! m i)
1717 (module-define! m '%module-public-interface i))
1718 (define (set-system-module! m s)
1719 (set-procedure-property! (module-eval-closure m) 'system-module s))
1720 (define the-root-module (make-root-module))
1721 (define the-scm-module (make-scm-module))
1722 (set-module-public-interface! the-root-module the-scm-module)
1723 (set-module-name! the-root-module '(guile))
1724 (set-module-name! the-scm-module '(guile))
1725 (set-module-kind! the-scm-module 'interface)
1726 (for-each set-system-module! (list the-root-module the-scm-module) '(#t #t))
1728 (set-current-module the-root-module)
1730 (define app (make-module 31))
1731 (local-define '(app modules) (make-module 31))
1732 (local-define '(app modules guile) the-root-module)
1734 ;; (define-special-value '(app modules new-ws) (lambda () (make-scm-module)))
1736 (define (try-load-module name)
1737 (or (try-module-linked name)
1738 (try-module-autoload name)
1739 (try-module-dynamic-link name)))
1741 ;; NOTE: This binding is used in libguile/modules.c.
1743 (define (resolve-module name . maybe-autoload)
1744 (let ((full-name (append '(app modules) name)))
1745 (let ((already (local-ref full-name)))
1747 ;; The module already exists...
1748 (if (and (or (null? maybe-autoload) (car maybe-autoload))
1749 (not (module-ref already '%module-public-interface #f)))
1750 ;; ...but we are told to load and it doesn't contain source, so
1752 (try-load-module name)
1754 ;; simply return it.
1757 ;; Try to autoload it if we are told so
1758 (if (or (null? maybe-autoload) (car maybe-autoload))
1759 (try-load-module name))
1761 (make-modules-in (current-module) full-name))))))
1763 (define (beautify-user-module! module)
1764 (let ((interface (module-public-interface module)))
1765 (if (or (not interface)
1766 (eq? interface module))
1767 (let ((interface (make-module 31)))
1768 (set-module-name! interface (module-name module))
1769 (set-module-kind! interface 'interface)
1770 (set-module-public-interface! module interface))))
1771 (if (and (not (memq the-scm-module (module-uses module)))
1772 (not (eq? module the-root-module)))
1773 (set-module-uses! module (append (module-uses module) (list the-scm-module)))))
1775 (define (purify-module! module)
1776 "Removes bindings in MODULE which are inherited from the (guile) module."
1777 (let ((use-list (module-uses module)))
1778 (if (and (pair? use-list)
1779 (eq? (car (last-pair use-list)) the-scm-module))
1780 (set-module-uses! module (reverse (cdr (reverse use-list)))))))
1782 ;; NOTE: This binding is used in libguile/modules.c.
1784 (define (make-modules-in module name)
1788 ((module-ref module (car name) #f)
1789 => (lambda (m) (make-modules-in m (cdr name))))
1790 (else (let ((m (make-module 31)))
1791 (set-module-kind! m 'directory)
1792 (set-module-name! m (append (or (module-name module)
1795 (module-define! module (car name) m)
1796 (make-modules-in m (cdr name)))))))
1798 (define (resolve-interface name)
1799 (let ((module (resolve-module name)))
1800 (and module (module-public-interface module))))
1803 (define %autoloader-developer-mode #t)
1805 (define (process-define-module args)
1806 (let* ((module-id (car args))
1807 (module (resolve-module module-id #f))
1809 (beautify-user-module! module)
1810 (let loop ((kws kws)
1811 (reversed-interfaces '())
1815 (for-each (lambda (interface)
1816 (module-use! module interface))
1817 reversed-interfaces)
1818 (module-export! module exports))
1819 (let ((keyword (if (keyword? (car kws))
1820 (keyword->symbol (car kws))
1821 (and (symbol? (car kws))
1822 (let ((s (symbol->string (car kws))))
1823 (and (eq? (string-ref s 0) #\:)
1824 (string->symbol (substring s 1))))))))
1826 ((use-module use-syntax)
1827 (if (not (pair? (cdr kws)))
1828 (error "unrecognized defmodule argument" kws))
1829 (let* ((used-name (cadr kws))
1830 (used-module (resolve-module used-name)))
1831 (if (not (module-ref used-module
1832 '%module-public-interface
1835 ((if %autoloader-developer-mode warn error)
1836 "no code for module" (module-name used-module))
1837 (beautify-user-module! used-module)))
1838 (let ((interface (module-public-interface used-module)))
1840 (error "missing interface for use-module"
1842 (if (eq? keyword 'use-syntax)
1843 (set-module-transformer!
1845 (module-ref interface (car (last-pair used-name))
1848 (cons interface reversed-interfaces)
1851 (if (not (and (pair? (cdr kws)) (pair? (cddr kws))))
1852 (error "unrecognized defmodule argument" kws))
1854 (cons (make-autoload-interface module
1857 reversed-interfaces)
1860 (set-system-module! module #t)
1861 (loop (cdr kws) reversed-interfaces exports))
1863 (purify-module! module)
1864 (loop (cdr kws) reversed-interfaces exports))
1866 (if (not (pair? (cdr kws)))
1867 (error "unrecognized defmodule argument" kws))
1870 (append (cadr kws) exports)))
1872 (error "unrecognized defmodule argument" kws))))))
1877 (define (make-autoload-interface module name bindings)
1878 (let ((b (lambda (a sym definep)
1879 (and (memq sym bindings)
1880 (let ((i (module-public-interface (resolve-module name))))
1882 (error "missing interface for module" name))
1883 ;; Replace autoload-interface with interface
1884 (set-car! (memq a (module-uses module)) i)
1885 (module-local-variable i sym))))))
1886 (module-constructor #() '() b #f #f name 'autoload
1887 '() (make-weak-value-hash-table 31) 0)))
1890 ;;; {Autoloading modules}
1892 (define autoloads-in-progress '())
1894 (define (try-module-autoload module-name)
1895 (let* ((reverse-name (reverse module-name))
1896 (name (symbol->string (car reverse-name)))
1897 (dir-hint-module-name (reverse (cdr reverse-name)))
1898 (dir-hint (apply string-append
1900 (string-append (symbol->string elt) "/"))
1901 dir-hint-module-name))))
1902 (resolve-module dir-hint-module-name #f)
1903 (and (not (autoload-done-or-in-progress? dir-hint name))
1906 (lambda () (autoload-in-progress! dir-hint name))
1908 (let ((full (%search-load-path (in-vicinity dir-hint name))))
1911 (save-module-excursion (lambda () (primitive-load full)))
1913 (lambda () (set-autoloaded! dir-hint name didit)))
1917 ;;; Dynamic linking of modules
1919 ;; Initializing a module that is written in C is a two step process.
1920 ;; First the module's `module init' function is called. This function
1921 ;; is expected to call `scm_register_module_xxx' to register the `real
1922 ;; init' function. Later, when the module is referenced for the first
1923 ;; time, this real init function is called in the right context. See
1924 ;; gtcltk-lib/gtcltk-module.c for an example.
1926 ;; The code for the module can be in a regular shared library (so that
1927 ;; the `module init' function will be called when libguile is
1928 ;; initialized). Or it can be dynamically linked.
1930 ;; You can safely call `scm_register_module_xxx' before libguile
1931 ;; itself is initialized. You could call it from an C++ constructor
1932 ;; of a static object, for example.
1934 ;; To make your Guile extension into a dynamic linkable module, follow
1935 ;; these easy steps:
1937 ;; - Find a name for your module, like (ice-9 gtcltk)
1938 ;; - Write a function with a name like
1940 ;; scm_init_ice_9_gtcltk_module
1942 ;; This is your `module init' function. It should call
1944 ;; scm_register_module_xxx ("ice-9 gtcltk", scm_init_gtcltk);
1946 ;; "ice-9 gtcltk" is the C version of the module name. Slashes are
1947 ;; replaced by spaces, the rest is untouched. `scm_init_gtcltk' is
1948 ;; the real init function that executes the usual initializations
1949 ;; like making new smobs, etc.
1951 ;; - Make a shared library with your code and a name like
1953 ;; ice-9/libgtcltk.so
1955 ;; and put it somewhere in %load-path.
1957 ;; - Then you can simply write `:use-module (ice-9 gtcltk)' and it
1958 ;; will be linked automatically.
1960 ;; This is all very experimental.
1962 (define (split-c-module-name str)
1963 (let loop ((rev '())
1966 (end (string-length str)))
1969 (reverse (cons (string->symbol (substring str start pos)) rev)))
1970 ((eq? (string-ref str pos) #\space)
1971 (loop (cons (string->symbol (substring str start pos)) rev)
1976 (loop rev start (+ pos 1) end)))))
1978 (define (convert-c-registered-modules dynobj)
1979 (let ((res (map (lambda (c)
1980 (list (split-c-module-name (car c)) (cdr c) dynobj))
1981 (c-registered-modules))))
1982 (c-clear-registered-modules)
1985 (define registered-modules '())
1987 (define (register-modules dynobj)
1988 (set! registered-modules
1989 (append! (convert-c-registered-modules dynobj)
1990 registered-modules)))
1992 (define (init-dynamic-module modname)
1993 ;; Register any linked modules which has been registered on the C level
1994 (register-modules #f)
1995 (or-map (lambda (modinfo)
1996 (if (equal? (car modinfo) modname)
1998 (set! registered-modules (delq! modinfo registered-modules))
1999 (let ((mod (resolve-module modname #f)))
2000 (save-module-excursion
2002 (set-current-module mod)
2003 (set-module-public-interface! mod mod)
2004 (dynamic-call (cadr modinfo) (caddr modinfo))
2008 registered-modules))
2010 (define (dynamic-maybe-call name dynobj)
2011 (catch #t ; could use false-if-exception here
2013 (dynamic-call name dynobj))
2017 (define (dynamic-maybe-link filename)
2018 (catch #t ; could use false-if-exception here
2020 (dynamic-link filename))
2024 (define (find-and-link-dynamic-module module-name)
2025 (define (make-init-name mod-name)
2026 (string-append "scm_init"
2027 (list->string (map (lambda (c)
2028 (if (or (char-alphabetic? c)
2032 (string->list mod-name)))
2035 ;; Put the subdirectory for this module in the car of SUBDIR-AND-LIBNAME,
2036 ;; and the `libname' (the name of the module prepended by `lib') in the cdr
2037 ;; field. For example, if MODULE-NAME is the list (inet tcp-ip udp), then
2038 ;; SUBDIR-AND-LIBNAME will be the pair ("inet/tcp-ip" . "libudp").
2039 (let ((subdir-and-libname
2040 (let loop ((dirs "")
2042 (if (null? (cdr syms))
2043 (cons dirs (string-append "lib" (symbol->string (car syms))))
2044 (loop (string-append dirs (symbol->string (car syms)) "/")
2046 (init (make-init-name (apply string-append
2049 (symbol->string s)))
2051 (let ((subdir (car subdir-and-libname))
2052 (libname (cdr subdir-and-libname)))
2054 ;; Now look in each dir in %LOAD-PATH for `subdir/libfoo.la'. If that
2055 ;; file exists, fetch the dlname from that file and attempt to link
2056 ;; against it. If `subdir/libfoo.la' does not exist, or does not seem
2057 ;; to name any shared library, look for `subdir/libfoo.so' instead and
2058 ;; link against that.
2059 (let check-dirs ((dir-list %load-path))
2060 (if (null? dir-list)
2062 (let* ((dir (in-vicinity (car dir-list) subdir))
2064 (or (try-using-libtool-name dir libname)
2065 (try-using-sharlib-name dir libname))))
2066 (if (and sharlib-full (file-exists? sharlib-full))
2067 (link-dynamic-module sharlib-full init)
2068 (check-dirs (cdr dir-list)))))))))
2070 (define (try-using-libtool-name libdir libname)
2071 (let ((libtool-filename (in-vicinity libdir
2072 (string-append libname ".la"))))
2073 (and (file-exists? libtool-filename)
2076 (define (try-using-sharlib-name libdir libname)
2077 (in-vicinity libdir (string-append libname ".so")))
2079 (define (link-dynamic-module filename initname)
2080 ;; Register any linked modules which has been registered on the C level
2081 (register-modules #f)
2082 (let ((dynobj (dynamic-link filename)))
2083 (dynamic-call initname dynobj)
2084 (register-modules dynobj)))
2086 (define (try-module-linked module-name)
2087 (init-dynamic-module module-name))
2089 (define (try-module-dynamic-link module-name)
2090 (and (find-and-link-dynamic-module module-name)
2091 (init-dynamic-module module-name)))
2095 (define autoloads-done '((guile . guile)))
2097 (define (autoload-done-or-in-progress? p m)
2098 (let ((n (cons p m)))
2099 (->bool (or (member n autoloads-done)
2100 (member n autoloads-in-progress)))))
2102 (define (autoload-done! p m)
2103 (let ((n (cons p m)))
2104 (set! autoloads-in-progress
2105 (delete! n autoloads-in-progress))
2106 (or (member n autoloads-done)
2107 (set! autoloads-done (cons n autoloads-done)))))
2109 (define (autoload-in-progress! p m)
2110 (let ((n (cons p m)))
2111 (set! autoloads-done
2112 (delete! n autoloads-done))
2113 (set! autoloads-in-progress (cons n autoloads-in-progress))))
2115 (define (set-autoloaded! p m done?)
2117 (autoload-done! p m)
2118 (let ((n (cons p m)))
2119 (set! autoloads-done (delete! n autoloads-done))
2120 (set! autoloads-in-progress (delete! n autoloads-in-progress)))))
2129 (define (primitive-macro? m)
2131 (not (macro-transformer m))))
2135 (define macro-table (make-weak-key-hash-table 523))
2136 (define xformer-table (make-weak-key-hash-table 523))
2138 (define (defmacro? m) (hashq-ref macro-table m))
2139 (define (assert-defmacro?! m) (hashq-set! macro-table m #t))
2140 (define (defmacro-transformer m) (hashq-ref xformer-table m))
2141 (define (set-defmacro-transformer! m t) (hashq-set! xformer-table m t))
2143 (define defmacro:transformer
2145 (let* ((xform (lambda (exp env)
2146 (copy-tree (apply f (cdr exp)))))
2147 (a (procedure->memoizing-macro xform)))
2148 (assert-defmacro?! a)
2149 (set-defmacro-transformer! a f)
2154 (let ((defmacro-transformer
2155 (lambda (name parms . body)
2156 (let ((transformer `(lambda ,parms ,@body)))
2158 (,(lambda (transformer)
2159 (defmacro:transformer transformer))
2161 (defmacro:transformer defmacro-transformer)))
2163 (define defmacro:syntax-transformer
2167 (copy-tree (apply f (cdr exp)))))))
2170 ;; XXX - should the definition of the car really be looked up in the
2173 (define (macroexpand-1 e)
2175 ((pair? e) (let* ((a (car e))
2176 (val (and (symbol? a) (local-ref (list a)))))
2178 (apply (defmacro-transformer val) (cdr e))
2182 (define (macroexpand e)
2184 ((pair? e) (let* ((a (car e))
2185 (val (and (symbol? a) (local-ref (list a)))))
2187 (macroexpand (apply (defmacro-transformer val) (cdr e)))
2198 ;;; {Run-time options}
2200 ((let* ((names '((eval-options-interface
2201 (eval-options eval-enable eval-disable)
2204 (debug-options-interface
2205 (debug-options debug-enable debug-disable)
2208 (evaluator-traps-interface
2209 (traps trap-enable trap-disable)
2212 (read-options-interface
2213 (read-options read-enable read-disable)
2216 (print-options-interface
2217 (print-options print-enable print-disable)
2220 (readline-options-interface
2221 (readline-options readline-enable readline-disable)
2226 (option-documentation caddr)
2228 (print-option (lambda (option)
2229 (display (option-name option))
2230 (if (< (string-length
2231 (symbol->string (option-name option)))
2235 (display (option-value option))
2237 (display (option-documentation option))
2240 ;; Below follows the macros defining the run-time option interfaces.
2242 (make-options (lambda (interface)
2244 (cond ((null? args) (,interface))
2246 (,interface (car args)) (,interface))
2247 (else (for-each ,print-option
2248 (,interface #t)))))))
2250 (make-enable (lambda (interface)
2252 (,interface (append flags (,interface)))
2255 (make-disable (lambda (interface)
2257 (let ((options (,interface)))
2258 (for-each (lambda (flag)
2259 (set! options (delq! flag options)))
2261 (,interface options)
2264 (make-set! (lambda (interface)
2267 (begin (,interface (append (,interface)
2268 (list '(,'unquote name)
2276 (map (lambda (group)
2277 (let ((interface (car group)))
2278 (append (map (lambda (name constructor)
2280 ,(constructor interface)))
2285 (map (lambda (name constructor)
2287 ,@(constructor interface)))
2289 (list make-set!)))))
2297 (define (repl read evaler print)
2298 (let loop ((source (read (current-input-port))))
2299 (print (evaler source))
2300 (loop (read (current-input-port)))))
2302 ;; A provisional repl that acts like the SCM repl:
2304 (define scm-repl-silent #f)
2305 (define (assert-repl-silence v) (set! scm-repl-silent v))
2307 (define *unspecified* (if #f #f))
2308 (define (unspecified? v) (eq? v *unspecified*))
2310 (define scm-repl-print-unspecified #f)
2311 (define (assert-repl-print-unspecified v) (set! scm-repl-print-unspecified v))
2313 (define scm-repl-verbose #f)
2314 (define (assert-repl-verbosity v) (set! scm-repl-verbose v))
2316 (define scm-repl-prompt "guile> ")
2318 (define (set-repl-prompt! v) (set! scm-repl-prompt v))
2320 (define (default-lazy-handler key . args)
2321 (save-stack lazy-handler-dispatch)
2322 (apply throw key args))
2324 (define enter-frame-handler default-lazy-handler)
2325 (define apply-frame-handler default-lazy-handler)
2326 (define exit-frame-handler default-lazy-handler)
2328 (define (lazy-handler-dispatch key . args)
2331 (apply apply-frame-handler key args))
2333 (apply exit-frame-handler key args))
2335 (apply enter-frame-handler key args))
2337 (apply default-lazy-handler key args))))
2339 (define abort-hook (make-hook))
2341 ;; these definitions are used if running a script.
2342 ;; otherwise redefined in error-catching-loop.
2343 (define (set-batch-mode?! arg) #t)
2344 (define (batch-mode?) #t)
2346 (define (error-catching-loop thunk)
2349 (define (loop first)
2357 (lambda () (unmask-signals))
2363 ;; This line is needed because mark
2364 ;; doesn't do closures quite right.
2365 ;; Unreferenced locals should be
2369 (let loop ((v (thunk)))
2372 (lambda () (mask-signals))))
2374 lazy-handler-dispatch))
2376 (lambda (key . args)
2383 (apply throw 'switch-repl args))
2386 ;; This is one of the closures that require
2387 ;; (set! first #f) above
2390 (run-hook abort-hook)
2391 (force-output (current-output-port))
2392 (display "ABORT: " (current-error-port))
2393 (write args (current-error-port))
2394 (newline (current-error-port))
2398 (not has-shown-debugger-hint?)
2399 (not (memq 'backtrace
2400 (debug-options-interface)))
2401 (stack? (fluid-ref the-last-stack)))
2403 (newline (current-error-port))
2405 "Type \"(backtrace)\" to get more information or \"(debug)\" to enter the debugger.\n"
2406 (current-error-port))
2407 (set! has-shown-debugger-hint? #t)))
2408 (force-output (current-error-port)))
2410 (primitive-exit 1)))
2411 (set! stack-saved? #f)))
2414 ;; This is the other cons-leak closure...
2416 (cond ((= (length args) 4)
2417 (apply handle-system-error key args))
2419 (apply bad-throw key args))))))))))
2420 (if next (loop next) status)))
2421 (set! set-batch-mode?! (lambda (arg)
2423 (set! interactive #f)
2426 (error "sorry, not implemented")))))
2427 (set! batch-mode? (lambda () (not interactive)))
2428 (loop (lambda () #t))))
2430 ;;(define the-last-stack (make-fluid)) Defined by scm_init_backtrace ()
2431 (define before-signal-stack (make-fluid))
2432 (define stack-saved? #f)
2434 (define (save-stack . narrowing)
2436 (cond ((not (memq 'debug (debug-options-interface)))
2437 (fluid-set! the-last-stack #f)
2438 (set! stack-saved? #t))
2444 (apply make-stack #t save-stack eval #t 0 narrowing))
2446 (apply make-stack #t save-stack 0 #t 0 narrowing))
2448 (apply make-stack #t save-stack tk-stack-mark #t 0 narrowing))
2450 (apply make-stack #t save-stack 0 1 narrowing))
2452 (let ((id (stack-id #t)))
2453 (and (procedure? id)
2454 (apply make-stack #t save-stack id #t 0 narrowing))))))
2455 (set! stack-saved? #t)))))
2457 (define before-error-hook (make-hook))
2458 (define after-error-hook (make-hook))
2459 (define before-backtrace-hook (make-hook))
2460 (define after-backtrace-hook (make-hook))
2462 (define has-shown-debugger-hint? #f)
2464 (define (handle-system-error key . args)
2465 (let ((cep (current-error-port)))
2466 (cond ((not (stack? (fluid-ref the-last-stack))))
2467 ((memq 'backtrace (debug-options-interface))
2468 (run-hook before-backtrace-hook)
2470 (display "Backtrace:\n")
2471 (display-backtrace (fluid-ref the-last-stack) cep)
2473 (run-hook after-backtrace-hook)))
2474 (run-hook before-error-hook)
2475 (apply display-error (fluid-ref the-last-stack) cep args)
2476 (run-hook after-error-hook)
2478 (throw 'abort key)))
2480 (define (quit . args)
2481 (apply throw 'quit args))
2485 ;;(define has-shown-backtrace-hint? #f) Defined by scm_init_backtrace ()
2487 ;; Replaced by C code:
2488 ;;(define (backtrace)
2489 ;; (if (fluid-ref the-last-stack)
2492 ;; (display-backtrace (fluid-ref the-last-stack) (current-output-port))
2494 ;; (if (and (not has-shown-backtrace-hint?)
2495 ;; (not (memq 'backtrace (debug-options-interface))))
2498 ;;"Type \"(debug-enable 'backtrace)\" if you would like a backtrace
2499 ;;automatically if an error occurs in the future.\n")
2500 ;; (set! has-shown-backtrace-hint? #t))))
2501 ;; (display "No backtrace available.\n")))
2503 (define (error-catching-repl r e p)
2504 (error-catching-loop (lambda () (p (e (r))))))
2506 (define (gc-run-time)
2507 (cdr (assq 'gc-time-taken (gc-stats))))
2509 (define before-read-hook (make-hook))
2510 (define after-read-hook (make-hook))
2512 ;;; The default repl-reader function. We may override this if we've
2513 ;;; the readline library.
2518 (run-hook before-read-hook)
2519 (read (current-input-port))))
2521 (define (scm-style-repl)
2525 (repl-report-start-timing (lambda ()
2526 (set! start-gc-rt (gc-run-time))
2527 (set! start-rt (get-internal-run-time))))
2528 (repl-report (lambda ()
2530 (display (inexact->exact
2531 (* 1000 (/ (- (get-internal-run-time) start-rt)
2532 internal-time-units-per-second))))
2534 (display (inexact->exact
2535 (* 1000 (/ (- (gc-run-time) start-gc-rt)
2536 internal-time-units-per-second))))
2537 (display " msec in gc)\n")))
2539 (consume-trailing-whitespace
2541 (let ((ch (peek-char)))
2544 ((or (char=? ch #\space) (char=? ch #\tab))
2546 (consume-trailing-whitespace))
2547 ((char=? ch #\newline)
2551 (let ((prompt (cond ((string? scm-repl-prompt)
2553 ((thunk? scm-repl-prompt)
2555 (scm-repl-prompt "> ")
2557 (repl-reader prompt))))
2559 ;; As described in R4RS, the READ procedure updates the
2560 ;; port to point to the first character past the end of
2561 ;; the external representation of the object. This
2562 ;; means that it doesn't consume the newline typically
2563 ;; found after an expression. This means that, when
2564 ;; debugging Guile with GDB, GDB gets the newline, which
2565 ;; it often interprets as a "continue" command, making
2566 ;; breakpoints kind of useless. So, consume any
2567 ;; trailing newline here, as well as any whitespace
2569 ;; But not if EOF, for control-D.
2570 (if (not (eof-object? val))
2571 (consume-trailing-whitespace))
2572 (run-hook after-read-hook)
2573 (if (eof-object? val)
2575 (repl-report-start-timing)
2576 (if scm-repl-verbose
2579 (display ";;; EOF -- quitting")
2584 (-eval (lambda (sourc)
2585 (repl-report-start-timing)
2586 (start-stack 'repl-stack
2587 (eval sourc (interaction-environment)))))
2589 (-print (let ((maybe-print (lambda (result)
2590 (if (or scm-repl-print-unspecified
2591 (not (unspecified? result)))
2596 (if (not scm-repl-silent)
2598 (if (values? result)
2599 (for-each maybe-print (get-values result))
2600 (maybe-print result))
2601 (if scm-repl-verbose
2605 (-quit (lambda (args)
2606 (if scm-repl-verbose
2608 (display ";;; QUIT executed, repl exitting")
2614 (if scm-repl-verbose
2616 (display ";;; ABORT executed.")
2619 (repl -read -eval -print))))
2621 (let ((status (error-catching-repl -read
2628 ;;; {IOTA functions: generating lists of numbers}
2631 (let loop ((count (1- n)) (result '()))
2632 (if (< count 0) result
2633 (loop (1- count) (cons count result)))))
2638 ;;; with `continue' and `break'.
2641 (defmacro while (cond . body)
2642 `(letrec ((continue (lambda () (or (not ,cond) (begin (begin ,@ body) (continue)))))
2643 (break (lambda val (apply throw 'break val))))
2645 (lambda () (continue))
2646 (lambda v (cadr v)))))
2650 ;;; Similar to `begin' but returns a list of the results of all constituent
2651 ;;; forms instead of the result of the last form.
2652 ;;; (The definition relies on the current left-to-right
2653 ;;; order of evaluation of operands in applications.)
2655 (defmacro collect forms
2660 ;; with-fluids is a convenience wrapper for the builtin procedure
2661 ;; `with-fluids*'. The syntax is just like `let':
2663 ;; (with-fluids ((fluid val)
2667 (defmacro with-fluids (bindings . body)
2668 `(with-fluids* (list ,@(map car bindings)) (list ,@(map cadr bindings))
2669 (lambda () ,@body)))
2676 ;; actually....hobbit might be able to hack these with a little
2680 (defmacro define-macro (first . rest)
2681 (let ((name (if (symbol? first) first (car first)))
2685 `(lambda ,(cdr first) ,@rest))))
2686 `(define ,name (defmacro:transformer ,transformer))))
2689 (defmacro define-syntax-macro (first . rest)
2690 (let ((name (if (symbol? first) first (car first)))
2694 `(lambda ,(cdr first) ,@rest))))
2695 `(define ,name (defmacro:syntax-transformer ,transformer))))
2697 ;;; {Module System Macros}
2700 (defmacro define-module args
2701 `(let* ((process-define-module process-define-module)
2702 (set-current-module set-current-module)
2703 (module (process-define-module ',args)))
2704 (set-current-module module)
2707 ;; the guts of the use-modules macro. add the interfaces of the named
2708 ;; modules to the use-list of the current module, in order
2709 (define (process-use-modules module-names)
2710 (for-each (lambda (module-name)
2711 (let ((mod-iface (resolve-interface module-name)))
2713 (error "no such module" module-name))
2714 (module-use! (current-module) mod-iface)))
2715 (reverse module-names)))
2717 (defmacro use-modules modules
2718 `(process-use-modules ',modules))
2720 (defmacro use-syntax (spec)
2723 `((process-use-modules ',(list spec))
2724 (set-module-transformer! (current-module)
2725 ,(car (last-pair spec))))
2726 `((set-module-transformer! (current-module) ,spec)))
2727 (fluid-set! scm:eval-transformer (module-transformer (current-module)))))
2729 (define define-private define)
2731 (defmacro define-public args
2733 (error "bad syntax" (list 'define-public args)))
2734 (define (defined-name n)
2737 ((pair? n) (defined-name (car n)))
2740 ((null? args) (syntax))
2742 (#t (let ((name (defined-name (car args))))
2744 (let ((public-i (module-public-interface (current-module))))
2745 ;; Make sure there is a local variable:
2747 (module-define! (current-module)
2749 (module-ref (current-module) ',name #f))
2751 ;; Make sure that local is exported:
2753 (module-add! public-i ',name
2754 (module-variable (current-module) ',name)))
2756 ;; Now (re)define the var normally. Bernard URBAN
2757 ;; suggests we use eval here to accomodate Hobbit; it lets
2758 ;; the interpreter handle the define-private form, which
2759 ;; Hobbit can't digest.
2760 (eval '(define-private ,@ args) (interaction-environment)))))))
2764 (defmacro defmacro-public args
2766 (error "bad syntax" (list 'defmacro-public args)))
2767 (define (defined-name n)
2772 ((null? args) (syntax))
2774 (#t (let ((name (defined-name (car args))))
2776 (let ((public-i (module-public-interface (current-module))))
2777 ;; Make sure there is a local variable:
2779 (module-define! (current-module)
2781 (module-ref (current-module) ',name #f))
2783 ;; Make sure that local is exported:
2785 (module-add! public-i ',name (module-variable (current-module) ',name)))
2787 ;; Now (re)define the var normally.
2789 (defmacro ,@ args))))))
2792 (define (module-export! m names)
2793 (let ((public-i (module-public-interface m)))
2794 (for-each (lambda (name)
2795 ;; Make sure there is a local variable:
2796 (module-define! m name (module-ref m name #f))
2797 ;; Make sure that local is exported:
2798 (module-add! public-i name (module-variable m name)))
2801 (defmacro export names
2802 `(module-export! (current-module) ',names))
2804 (define export-syntax export)
2807 (define load load-module)
2811 ;;; {Load emacs interface support if emacs option is given.}
2813 (define (load-emacs-interface)
2814 (if (memq 'debug-extensions *features*)
2815 (debug-enable 'backtrace))
2816 (define-module (guile-user) :use-module (ice-9 emacs)))
2820 (define using-readline?
2821 (let ((using-readline? (make-fluid)))
2822 (make-procedure-with-setter
2823 (lambda () (fluid-ref using-readline?))
2824 (lambda (v) (fluid-set! using-readline? v)))))
2826 ;; this is just (scm-style-repl) with a wrapper to install and remove
2830 ;; Load emacs interface support if emacs option is given.
2831 (if (and (module-defined? the-root-module 'use-emacs-interface)
2832 use-emacs-interface)
2833 (load-emacs-interface))
2835 ;; Place the user in the guile-user module.
2836 (define-module (guile-user)
2837 :use-module (guile) ;so that bindings will be checked here first
2838 :use-module (ice-9 session)
2839 :use-module (ice-9 debug)
2840 :autoload (ice-9 debugger) (debug)) ;load debugger on demand
2841 (if (memq 'threads *features*)
2842 (define-module (guile-user) :use-module (ice-9 threads)))
2843 (if (memq 'regex *features*)
2844 (define-module (guile-user) :use-module (ice-9 regex)))
2846 (let ((old-handlers #f)
2847 (signals (if (provided? 'posix)
2848 `((,SIGINT . "User interrupt")
2849 (,SIGFPE . "Arithmetic error")
2850 (,SIGBUS . "Bad memory access (bus error)")
2852 "Bad memory access (Segmentation violation)"))
2859 (let ((make-handler (lambda (msg)
2861 ;; Make a backup copy of the stack
2862 (fluid-set! before-signal-stack
2863 (fluid-ref the-last-stack))
2864 (save-stack %deliver-signals)
2871 (map (lambda (sig-msg)
2872 (sigaction (car sig-msg)
2873 (make-handler (cdr sig-msg))))
2876 ;; the protected thunk.
2878 (let ((status (scm-style-repl)))
2879 (run-hook exit-hook)
2884 (map (lambda (sig-msg old-handler)
2885 (if (not (car old-handler))
2886 ;; restore original C handler.
2887 (sigaction (car sig-msg) #f)
2888 ;; restore Scheme handler, SIG_IGN or SIG_DFL.
2889 (sigaction (car sig-msg)
2891 (cdr old-handler))))
2892 signals old-handlers)))))
2894 (defmacro false-if-exception (expr)
2895 `(catch #t (lambda () ,expr)
2898 ;;; This hook is run at the very end of an interactive session.
2900 (define exit-hook (make-hook))
2903 (define-module (guile))
2905 (append! %load-path (cons "." ()))