3 ;;;; Copyright (C) 1995, 1996, 1997 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*))))
40 (primitive-load-path "ice-9/r4rs.scm")
43 ;;; {Simple Debugging Tools}
47 ;; peek takes any number of arguments, writes them to the
48 ;; current ouput port, and returns the last argument.
49 ;; It is handy to wrap around an expression to look at
50 ;; a value each time is evaluated, e.g.:
52 ;; (+ 10 (troublesome-fn))
53 ;; => (+ 10 (pk 'troublesome-fn-returned (troublesome-fn)))
56 (define (peek . stuff)
61 (car (last-pair stuff)))
65 (define (warn . stuff)
66 (with-output-to-port (current-error-port)
69 (display ";;; WARNING ")
72 (car (last-pair stuff)))))
75 ;;; {Trivial Functions}
79 (define (1+ n) (+ n 1))
80 (define (-1+ n) (+ n -1))
82 (define return-it noop)
83 (define (and=> value procedure) (and value (procedure value)))
84 (define (make-hash-table k) (make-vector k '()))
86 ;;; apply-to-args is functionally redunant with apply and, worse,
87 ;;; is less general than apply since it only takes two arguments.
89 ;;; On the other hand, apply-to-args is a syntacticly convenient way to
90 ;;; perform binding in many circumstances when the "let" family of
91 ;;; of forms don't cut it. E.g.:
93 ;;; (apply-to-args (return-3d-mouse-coords)
98 (define (apply-to-args args fn) (apply fn args))
104 (define (ipow-by-squaring x k acc proc)
105 (cond ((zero? k) acc)
106 ((= 1 k) (proc acc x))
107 (else (ipow-by-squaring (proc x x)
109 (if (even? k) acc (proc acc x))
112 (define string-character-length string-length)
116 ;; A convenience function for combining flag bits. Like logior, but
117 ;; handles the cases of 0 and 1 arguments.
119 (define (flags . args)
122 ((null? (cdr args)) (car args))
123 (else (apply logior args))))
126 ;;; {Symbol Properties}
129 (define (symbol-property sym prop)
130 (let ((pair (assoc prop (symbol-pref sym))))
131 (and pair (cdr pair))))
133 (define (set-symbol-property! sym prop val)
134 (let ((pair (assoc prop (symbol-pref sym))))
137 (symbol-pset! sym (acons prop val (symbol-pref sym))))))
139 (define (symbol-property-remove! sym prop)
140 (let ((pair (assoc prop (symbol-pref sym))))
142 (symbol-pset! sym (delq! pair (symbol-pref sym))))))
146 ;;; {Line and Delimited I/O}
148 ;;; corresponds to SCM_LINE_INCREMENTORS in libguile.
149 (define scm-line-incrementors "\n")
151 (define (read-line! string . maybe-port)
152 (let* ((port (if (pair? maybe-port)
154 (current-input-port))))
155 (let* ((rv (%read-delimited! scm-line-incrementors
159 (terminator (car rv))
161 (cond ((and (= nchars 0)
162 (eof-object? terminator))
164 ((not terminator) #f)
167 (define (read-delimited! delims buf . args)
168 (let* ((num-args (length args))
169 (port (if (> num-args 0)
171 (current-input-port)))
172 (handle-delim (if (> num-args 1)
175 (start (if (> num-args 2)
178 (end (if (> num-args 3)
180 (string-length buf))))
181 (let* ((rv (%read-delimited! delims
183 (not (eq? handle-delim 'peek))
187 (terminator (car rv))
189 (cond ((or (not terminator) ; buffer filled
190 (eof-object? terminator))
192 (if (eq? handle-delim 'split)
193 (cons terminator terminator)
195 (if (eq? handle-delim 'split)
196 (cons nchars terminator)
201 ((concat) (string-set! buf nchars terminator)
203 ((split) (cons nchars terminator))
204 (else (error "unexpected handle-delim value: "
207 (define (read-delimited delims . args)
208 (let* ((port (if (pair? args)
209 (let ((pt (car args)))
210 (set! args (cdr args))
212 (current-input-port)))
213 (handle-delim (if (pair? args)
216 (let loop ((substrings ())
218 (buf-size 100)) ; doubled each time through.
219 (let* ((buf (make-string buf-size))
220 (rv (%read-delimited! delims
222 (not (eq? handle-delim 'peek))
224 (terminator (car rv))
230 (cons (if (and (eq? handle-delim 'concat)
231 (not (eof-object? terminator)))
234 (cons (make-shared-substring buf 0 nchars)
236 (new-total (+ total-chars nchars)))
237 (cond ((not terminator)
239 (loop (cons (substring buf 0 nchars) substrings)
242 ((eof-object? terminator)
243 (if (zero? new-total)
244 (if (eq? handle-delim 'split)
245 (cons terminator terminator)
247 (if (eq? handle-delim 'split)
248 (cons (join-substrings) terminator)
252 ((trim peek concat) (join-substrings))
253 ((split) (cons (join-substrings) terminator))
254 (else (error "unexpected handle-delim value: "
255 handle-delim)))))))))
257 (define (read-line . args)
258 (apply read-delimited scm-line-incrementors args))
265 (define uniform-vector? array?)
266 (define make-uniform-vector dimensions->uniform-array)
267 ; (define uniform-vector-ref array-ref)
268 (define (uniform-vector-set! u i o)
269 (uniform-array-set1! u o i))
270 (define uniform-vector-fill! array-fill!)
271 (define uniform-vector-read! uniform-array-read!)
272 (define uniform-vector-write uniform-array-write)
274 (define (make-array fill . args)
275 (dimensions->uniform-array args () fill))
276 (define (make-uniform-array prot . args)
277 (dimensions->uniform-array args prot))
278 (define (list->array ndim lst)
279 (list->uniform-array ndim '() lst))
280 (define (list->uniform-vector prot lst)
281 (list->uniform-array 1 prot lst))
282 (define (array-shape a)
283 (map (lambda (ind) (if (number? ind) (list 0 (+ -1 ind)) ind))
284 (array-dimensions a))))
290 (define (symbol->keyword symbol)
291 (make-keyword-from-dash-symbol (symbol-append '- symbol)))
293 (define (keyword->symbol kw)
294 (let ((sym (keyword-dash-symbol kw)))
295 (string->symbol (substring sym 1 (string-length sym)))))
297 (define (kw-arg-ref args kw)
298 (let ((rem (member kw args)))
299 (and rem (pair? (cdr rem)) (cadr rem))))
305 (define (struct-layout s)
306 (struct-ref (struct-vtable s) vtable-index-layout))
312 ;; Printing records: by default, records are printed as
314 ;; #<type-name field1: val1 field2: val2 ...>
316 ;; You can change that by giving a custom printing function to
317 ;; MAKE-RECORD-TYPE (after the list of field symbols). This function
318 ;; will be called like
320 ;; (<printer> object port)
322 ;; It should print OBJECT to PORT.
324 ;; 0: type-name, 1: fields
325 (define record-type-vtable
326 (make-vtable-vtable "prpr" 0
328 (cond ((eq? s record-type-vtable)
329 (display "#<record-type-vtable>" p))
331 (display "#<record-type " p)
332 (display (record-type-name s) p)
335 (define (record-type? obj)
336 (and (struct? obj) (eq? record-type-vtable (struct-vtable obj))))
338 (define (make-record-type type-name fields . opt)
339 (let ((printer-fn (and (pair? opt) (car opt))))
340 (let ((struct (make-struct record-type-vtable 0
343 (map (lambda (f) "pw") fields)))
347 (display type-name p)
348 (let loop ((fields fields)
351 ((not (null? fields))
353 (display (car fields) p)
355 (display (struct-ref s off) p)
356 (loop (cdr fields) (+ 1 off)))))
359 (copy-tree fields))))
362 (define (record-type-name obj)
363 (if (record-type? obj)
364 (struct-ref obj vtable-offset-user)
365 (error 'not-a-record-type obj)))
367 (define (record-type-fields obj)
368 (if (record-type? obj)
369 (struct-ref obj (+ 1 vtable-offset-user))
370 (error 'not-a-record-type obj)))
372 (define (record-constructor rtd . opt)
373 (let ((field-names (if (pair? opt) (car opt) (record-type-fields rtd))))
374 (eval `(lambda ,field-names
375 (make-struct ',rtd 0 ,@(map (lambda (f)
376 (if (memq f field-names)
379 (record-type-fields rtd)))))))
381 (define (record-predicate rtd)
382 (lambda (obj) (and (struct? obj) (eq? rtd (struct-vtable obj)))))
384 (define (record-accessor rtd field-name)
385 (let* ((pos (list-index (record-type-fields rtd) field-name)))
387 (error 'no-such-field field-name))
389 (and (eq? ',rtd (record-type-descriptor obj))
390 (struct-ref obj ,pos))))))
392 (define (record-modifier rtd field-name)
393 (let* ((pos (list-index (record-type-fields rtd) field-name)))
395 (error 'no-such-field field-name))
396 (eval `(lambda (obj val)
397 (and (eq? ',rtd (record-type-descriptor obj))
398 (struct-set! obj ,pos val))))))
401 (define (record? obj)
402 (and (struct? obj) (record-type? (struct-vtable obj))))
404 (define (record-type-descriptor obj)
407 (error 'not-a-record obj)))
415 (define (->bool x) (not (not x)))
421 (define (symbol-append . args)
422 (string->symbol (apply string-append args)))
424 (define (list->symbol . args)
425 (string->symbol (apply list->string args)))
427 (define (symbol . args)
428 (string->symbol (apply string args)))
430 (define (obarray-symbol-append ob . args)
431 (string->obarray-symbol (apply string-append ob args)))
433 (define (obarray-gensym obarray . opt)
435 (gensym "%%gensym" obarray)
436 (gensym (car opt) obarray)))
442 (define (list-index l k)
448 (loop (+ n 1) (cdr l))))))
450 (define (make-list n . init)
451 (if (pair? init) (set! init (car init)))
452 (let loop ((answer '())
456 (loop (cons init answer) (- n 1)))))
460 ;;; {and-map, or-map, and map-in-order}
462 ;;; (and-map fn lst) is like (and (fn (car lst)) (fn (cadr lst)) (fn...) ...)
463 ;;; (or-map fn lst) is like (or (fn (car lst)) (fn (cadr lst)) (fn...) ...)
464 ;;; (map-in-order fn lst) is like (map fn lst) but definately in order of lst.
469 ;; Apply f to successive elements of l until exhaustion or f returns #f.
470 ;; If returning early, return #f. Otherwise, return the last value returned
471 ;; by f. If f has never been called because l is empty, return #t.
473 (define (and-map f lst)
474 (let loop ((result #t)
479 (loop (f (car l)) (cdr l))))))
483 ;; Apply f to successive elements of l until exhaustion or while f returns #f.
484 ;; If returning early, return the return value of f.
486 (define (or-map f lst)
487 (let loop ((result #f)
491 (loop (f (car l)) (cdr l))))))
495 ;; Like map, but guaranteed to process the list in order.
497 (define (map-in-order fn l)
501 (map-in-order fn (cdr l)))))
505 (define (run-hooks hook)
506 (for-each (lambda (thunk) (thunk)) hook))
511 `(let ((thunk ,(caddr exp)))
512 (if (not (memq thunk ,(cadr exp)))
514 (cons thunk ,(cadr exp))))))))
518 ;;; !!!! these should be implemented using Tcl commands, not fports.
521 (define (feature? feature)
522 (and (memq feature *features*) #t))
524 ;; Using the vector returned by stat directly is probably not a good
525 ;; idea (it could just as well be a record). Hence some accessors.
526 (define (stat:dev f) (vector-ref f 0))
527 (define (stat:ino f) (vector-ref f 1))
528 (define (stat:mode f) (vector-ref f 2))
529 (define (stat:nlink f) (vector-ref f 3))
530 (define (stat:uid f) (vector-ref f 4))
531 (define (stat:gid f) (vector-ref f 5))
532 (define (stat:rdev f) (vector-ref f 6))
533 (define (stat:size f) (vector-ref f 7))
534 (define (stat:atime f) (vector-ref f 8))
535 (define (stat:mtime f) (vector-ref f 9))
536 (define (stat:ctime f) (vector-ref f 10))
537 (define (stat:blksize f) (vector-ref f 11))
538 (define (stat:blocks f) (vector-ref f 12))
540 ;; derived from stat mode.
541 (define (stat:type f) (vector-ref f 13))
542 (define (stat:perms f) (vector-ref f 14))
545 (if (feature? 'posix)
549 (let ((port (catch 'system-error (lambda () (open-file str OPEN_READ))
551 (if port (begin (close-port port) #t)
554 (define file-is-directory?
555 (if (feature? 'i/o-extensions)
557 (eq? (stat:type (stat str)) 'directory))
561 (let ((port (catch 'system-error
562 (lambda () (open-file (string-append str "/.")
565 (if port (begin (close-port port) #t)
568 (define (has-suffix? str suffix)
569 (let ((sufl (string-length suffix))
570 (sl (string-length str)))
572 (string=? (substring str (- sl sufl) sl) suffix))))
578 (define (error . args)
581 (scm-error 'misc-error #f "?" #f #f)
582 (let loop ((msg "%s")
584 (if (not (null? rest))
585 (loop (string-append msg " %S")
587 (scm-error 'misc-error #f msg args #f)))))
589 ;; bad-throw is the hook that is called upon a throw to a an unhandled
590 ;; key (unless the throw has four arguments, in which case
591 ;; it's usually interpreted as an error throw.)
592 ;; If the key has a default handler (a throw-handler-default property),
593 ;; it is applied to the throw.
595 (define (bad-throw key . args)
596 (let ((default (symbol-property key 'throw-handler-default)))
597 (or (and default (apply default key args))
598 (apply error "unhandled-exception:" key args))))
601 ;;; {Non-polymorphic versions of POSIX functions}
603 (define (getgrnam name) (getgr name))
604 (define (getgrgid id) (getgr id))
605 (define (gethostbyaddr addr) (gethost addr))
606 (define (gethostbyname name) (gethost name))
607 (define (getnetbyaddr addr) (getnet addr))
608 (define (getnetbyname name) (getnet name))
609 (define (getprotobyname name) (getproto name))
610 (define (getprotobynumber addr) (getproto addr))
611 (define (getpwnam name) (getpw name))
612 (define (getpwuid uid) (getpw uid))
613 (define (getservbyname name proto) (getserv name proto))
614 (define (getservbyport port proto) (getserv port proto))
615 (define (endgrent) (setgr))
616 (define (endhostent) (sethost))
617 (define (endnetent) (setnet))
618 (define (endprotoent) (setproto))
619 (define (endpwent) (setpw))
620 (define (endservent) (setserv))
621 (define (getgrent) (getgr))
622 (define (gethostent) (gethost))
623 (define (getnetent) (getnet))
624 (define (getprotoent) (getproto))
625 (define (getpwent) (getpw))
626 (define (getservent) (getserv))
627 (define (reopen-file . args) (apply freopen args))
628 (define (setgrent) (setgr #f))
629 (define (sethostent) (sethost #t))
630 (define (setnetent) (setnet #t))
631 (define (setprotoent) (setproto #t))
632 (define (setpwent) (setpw #t))
633 (define (setservent) (setserv #t))
635 (define (passwd:name obj) (vector-ref obj 0))
636 (define (passwd:passwd obj) (vector-ref obj 1))
637 (define (passwd:uid obj) (vector-ref obj 2))
638 (define (passwd:gid obj) (vector-ref obj 3))
639 (define (passwd:gecos obj) (vector-ref obj 4))
640 (define (passwd:dir obj) (vector-ref obj 5))
641 (define (passwd:shell obj) (vector-ref obj 6))
643 (define (group:name obj) (vector-ref obj 0))
644 (define (group:passwd obj) (vector-ref obj 1))
645 (define (group:gid obj) (vector-ref obj 2))
646 (define (group:mem obj) (vector-ref obj 3))
648 (define (hostent:name obj) (vector-ref obj 0))
649 (define (hostent:aliases obj) (vector-ref obj 1))
650 (define (hostent:addrtype obj) (vector-ref obj 2))
651 (define (hostent:length obj) (vector-ref obj 3))
652 (define (hostent:addr-list obj) (vector-ref obj 4))
654 (define (netent:name obj) (vector-ref obj 0))
655 (define (netent:aliases obj) (vector-ref obj 1))
656 (define (netent:addrtype obj) (vector-ref obj 2))
657 (define (netent:net obj) (vector-ref obj 3))
659 (define (protoent:name obj) (vector-ref obj 0))
660 (define (protoent:aliases obj) (vector-ref obj 1))
661 (define (protoent:proto obj) (vector-ref obj 2))
663 (define (servent:name obj) (vector-ref obj 0))
664 (define (servent:aliases obj) (vector-ref obj 1))
665 (define (servent:port obj) (vector-ref obj 2))
666 (define (servent:proto obj) (vector-ref obj 3))
668 (define (sockaddr:fam obj) (vector-ref obj 0))
669 (define (sockaddr:path obj) (vector-ref obj 1))
670 (define (sockaddr:addr obj) (vector-ref obj 1))
671 (define (sockaddr:port obj) (vector-ref obj 2))
673 (define (utsname:sysname obj) (vector-ref obj 0))
674 (define (utsname:nodename obj) (vector-ref obj 1))
675 (define (utsname:release obj) (vector-ref obj 2))
676 (define (utsname:version obj) (vector-ref obj 3))
677 (define (utsname:machine obj) (vector-ref obj 4))
679 (define (tm:sec obj) (vector-ref obj 0))
680 (define (tm:min obj) (vector-ref obj 1))
681 (define (tm:hour obj) (vector-ref obj 2))
682 (define (tm:mday obj) (vector-ref obj 3))
683 (define (tm:mon obj) (vector-ref obj 4))
684 (define (tm:year obj) (vector-ref obj 5))
685 (define (tm:wday obj) (vector-ref obj 6))
686 (define (tm:yday obj) (vector-ref obj 7))
687 (define (tm:isdst obj) (vector-ref obj 8))
688 (define (tm:gmtoff obj) (vector-ref obj 9))
689 (define (tm:zone obj) (vector-ref obj 10))
691 (define (set-tm:sec obj val) (vector-set! obj 0 val))
692 (define (set-tm:min obj val) (vector-set! obj 1 val))
693 (define (set-tm:hour obj val) (vector-set! obj 2 val))
694 (define (set-tm:mday obj val) (vector-set! obj 3 val))
695 (define (set-tm:mon obj val) (vector-set! obj 4 val))
696 (define (set-tm:year obj val) (vector-set! obj 5 val))
697 (define (set-tm:wday obj val) (vector-set! obj 6 val))
698 (define (set-tm:yday obj val) (vector-set! obj 7 val))
699 (define (set-tm:isdst obj val) (vector-set! obj 8 val))
700 (define (set-tm:gmtoff obj val) (vector-set! obj 9 val))
701 (define (set-tm:zone obj val) (vector-set! obj 10 val))
703 (define (tms:clock obj) (vector-ref obj 0))
704 (define (tms:utime obj) (vector-ref obj 1))
705 (define (tms:stime obj) (vector-ref obj 2))
706 (define (tms:cutime obj) (vector-ref obj 3))
707 (define (tms:cstime obj) (vector-ref obj 4))
709 (define (file-position . args) (apply ftell args))
710 (define (file-set-position . args) (apply fseek args))
712 (define (open-input-pipe command) (open-pipe command OPEN_READ))
713 (define (open-output-pipe command) (open-pipe command OPEN_WRITE))
715 (define (move->fdes fd/port fd)
716 (cond ((integer? fd/port)
717 (dup->fdes fd/port fd)
721 (primitive-move->fdes fd/port fd)
722 (set-port-revealed! fd/port 1)
725 (define (release-port-handle port)
726 (let ((revealed (port-revealed port)))
728 (set-port-revealed! port (- revealed 1)))))
730 (define (dup->port port/fd mode . maybe-fd)
731 (let ((port (fdopen (apply dup->fdes port/fd maybe-fd)
734 (set-port-revealed! port 1))
737 (define (dup->inport port/fd . maybe-fd)
738 (apply dup->port port/fd "r" maybe-fd))
740 (define (dup->outport port/fd . maybe-fd)
741 (apply dup->port port/fd "w" maybe-fd))
743 (define (dup port/fd . maybe-fd)
744 (if (integer? port/fd)
745 (apply dup->fdes port/fd maybe-fd)
746 (apply dup->port port/fd (port-mode port/fd) maybe-fd)))
748 (define (duplicate-port port modes)
749 (dup->port port modes))
751 (define (fdes->inport fdes)
752 (let loop ((rest-ports (fdes->ports fdes)))
753 (cond ((null? rest-ports)
754 (let ((result (fdopen fdes "r")))
755 (set-port-revealed! result 1)
757 ((input-port? (car rest-ports))
758 (set-port-revealed! (car rest-ports)
759 (+ (port-revealed (car rest-ports)) 1))
762 (loop (cdr rest-ports))))))
764 (define (fdes->outport fdes)
765 (let loop ((rest-ports (fdes->ports fdes)))
766 (cond ((null? rest-ports)
767 (let ((result (fdopen fdes "w")))
768 (set-port-revealed! result 1)
770 ((output-port? (car rest-ports))
771 (set-port-revealed! (car rest-ports)
772 (+ (port-revealed (car rest-ports)) 1))
775 (loop (cdr rest-ports))))))
777 (define (port->fdes port)
778 (set-port-revealed! port (+ (port-revealed port) 1))
781 (define (setenv name value)
783 (putenv (string-append name "=" value))
790 ;;; Here for backward compatability
792 (define scheme-file-suffix (lambda () ".scm"))
794 (define (in-vicinity vicinity file)
795 (let ((tail (let ((len (string-length vicinity)))
798 (string-ref vicinity (- len 1))))))
799 (string-append vicinity
807 ;;; {Help for scm_shell}
808 ;;; The argument-processing code used by Guile-based shells generates
809 ;;; Scheme code based on the argument list. This page contains help
810 ;;; functions for the code it generates.
812 (define (command-line) (program-arguments))
814 ;; This is mostly for the internal use of the code generated by
815 ;; scm_compile_shell_switches.
816 (define (load-user-init)
817 (define (has-init? dir)
818 (let ((path (in-vicinity dir ".guile")))
821 (let ((stats (stat path)))
822 (if (not (eq? (stat:type stats) 'directory))
825 (let ((path (or (has-init? (or (getenv "HOME") "/"))
826 (has-init? (passwd:dir (getpw (getuid)))))))
827 (if path (primitive-load path))))
830 ;;; {Loading by paths}
832 ;;; Load a Scheme source file named NAME, searching for it in the
833 ;;; directories listed in %load-path, and applying each of the file
834 ;;; name extensions listed in %load-extensions.
835 (define (load-from-path name)
836 (start-stack 'load-stack
837 (primitive-load-path name)))
841 ;;; {Transcendental Functions}
843 ;;; Derived from "Transcen.scm", Complex trancendental functions for SCM.
844 ;;; Copyright (C) 1992, 1993 Jerry D. Hedden.
845 ;;; See the file `COPYING' for terms applying to this program.
849 (if (real? z) ($exp z)
850 (make-polar ($exp (real-part z)) (imag-part z))))
853 (if (and (real? z) (>= z 0))
855 (make-rectangular ($log (magnitude z)) (angle z))))
859 (if (negative? z) (make-rectangular 0 ($sqrt (- z)))
861 (make-polar ($sqrt (magnitude z)) (/ (angle z) 2))))
864 (let ((integer-expt integer-expt))
867 (integer-expt z1 z2))
868 ((and (real? z2) (real? z1) (>= z1 0))
871 (exp (* z2 (log z1))))))))
874 (if (real? z) ($sinh z)
875 (let ((x (real-part z)) (y (imag-part z)))
876 (make-rectangular (* ($sinh x) ($cos y))
877 (* ($cosh x) ($sin y))))))
879 (if (real? z) ($cosh z)
880 (let ((x (real-part z)) (y (imag-part z)))
881 (make-rectangular (* ($cosh x) ($cos y))
882 (* ($sinh x) ($sin y))))))
884 (if (real? z) ($tanh z)
885 (let* ((x (* 2 (real-part z)))
886 (y (* 2 (imag-part z)))
887 (w (+ ($cosh x) ($cos y))))
888 (make-rectangular (/ ($sinh x) w) (/ ($sin y) w)))))
891 (if (real? z) ($asinh z)
892 (log (+ z (sqrt (+ (* z z) 1))))))
895 (if (and (real? z) (>= z 1))
897 (log (+ z (sqrt (- (* z z) 1))))))
900 (if (and (real? z) (> z -1) (< z 1))
902 (/ (log (/ (+ 1 z) (- 1 z))) 2)))
905 (if (real? z) ($sin z)
906 (let ((x (real-part z)) (y (imag-part z)))
907 (make-rectangular (* ($sin x) ($cosh y))
908 (* ($cos x) ($sinh y))))))
910 (if (real? z) ($cos z)
911 (let ((x (real-part z)) (y (imag-part z)))
912 (make-rectangular (* ($cos x) ($cosh y))
913 (- (* ($sin x) ($sinh y)))))))
915 (if (real? z) ($tan z)
916 (let* ((x (* 2 (real-part z)))
917 (y (* 2 (imag-part z)))
918 (w (+ ($cos x) ($cosh y))))
919 (make-rectangular (/ ($sin x) w) (/ ($sinh y) w)))))
922 (if (and (real? z) (>= z -1) (<= z 1))
924 (* -i (asinh (* +i z)))))
927 (if (and (real? z) (>= z -1) (<= z 1))
929 (+ (/ (angle -1) 2) (* +i (asinh (* +i z))))))
933 (if (real? z) ($atan z)
934 (/ (log (/ (- +i z) (+ +i z))) +2i))
940 (/ (log arg) (log 10)))
944 ;;; {Reader Extensions}
947 ;;; Reader code for various "#c" forms.
950 ;;; Parse the portion of a #/ list that comes after the first slash.
951 (define (read-path-list-notation slash port)
955 ((delimiter? (lambda (c) (or (eof-object? c)
957 (string-index "()\";" c))))
959 ;; Read and return one component of a path list.
962 (let loop ((reversed-chars '()))
963 (let ((c (peek-char port)))
964 (if (or (delimiter? c)
966 (string->symbol (list->string (reverse reversed-chars)))
967 (loop (cons (read-char port) reversed-chars))))))))
969 ;; Read and return a path list.
970 (let loop ((reversed-path (list (read-component))))
971 (let ((c (peek-char port)))
972 (if (and (char? c) (char=? c #\/))
975 (loop (cons (read-component) reversed-path)))
976 (reverse reversed-path))))))
978 (read-hash-extend #\' (lambda (c port)
980 (read-hash-extend #\. (lambda (c port)
983 (if (feature? 'array)
985 (let ((make-array-proc (lambda (template)
987 (read:uniform-vector template port)))))
988 (for-each (lambda (char template)
989 (read-hash-extend char
990 (make-array-proc template)))
991 '(#\b #\a #\u #\e #\s #\i #\c)
992 '(#t #\a 1 -1 1.0 1/3 0+i)))
993 (let ((array-proc (lambda (c port)
994 (read:array c port))))
995 (for-each (lambda (char) (read-hash-extend char array-proc))
996 '(#\0 #\1 #\2 #\3 #\4 #\5 #\6 #\7 #\8 #\9)))))
998 ;; pushed to the beginning of the alist since it's used more than the
999 ;; others at present.
1000 (read-hash-extend #\/ read-path-list-notation)
1002 (define (read:array digit port)
1003 (define chr0 (char->integer #\0))
1004 (let ((rank (let readnum ((val (- (char->integer digit) chr0)))
1005 (if (char-numeric? (peek-char port))
1006 (readnum (+ (* 10 val)
1007 (- (char->integer (read-char port)) chr0)))
1009 (prot (if (eq? #\( (peek-char port))
1011 (let ((c (read-char port)))
1019 (else (error "read:array unknown option " c)))))))
1020 (if (eq? (peek-char port) #\()
1021 (list->uniform-array rank prot (read port))
1022 (error "read:array list not found"))))
1024 (define (read:uniform-vector proto port)
1025 (if (eq? #\( (peek-char port))
1026 (list->uniform-array 1 proto (read port))
1027 (error "read:uniform-vector list not found")))
1030 ;;; {Command Line Options}
1033 (define (get-option argv kw-opts kw-args return)
1036 (return #f #f argv))
1038 ((or (not (eq? #\- (string-ref (car argv) 0)))
1039 (eq? (string-length (car argv)) 1))
1040 (return 'normal-arg (car argv) (cdr argv)))
1042 ((eq? #\- (string-ref (car argv) 1))
1043 (let* ((kw-arg-pos (or (string-index (car argv) #\=)
1044 (string-length (car argv))))
1045 (kw (symbol->keyword (substring (car argv) 2 kw-arg-pos)))
1046 (kw-opt? (member kw kw-opts))
1047 (kw-arg? (member kw kw-args))
1048 (arg (or (and (not (eq? kw-arg-pos (string-length (car argv))))
1049 (substring (car argv)
1051 (string-length (car argv))))
1053 (begin (set! argv (cdr argv)) (car argv))))))
1054 (if (or kw-opt? kw-arg?)
1055 (return kw arg (cdr argv))
1056 (return 'usage-error kw (cdr argv)))))
1059 (let* ((char (substring (car argv) 1 2))
1060 (kw (symbol->keyword char)))
1063 ((member kw kw-opts)
1064 (let* ((rest-car (substring (car argv) 2 (string-length (car argv))))
1065 (new-argv (if (= 0 (string-length rest-car))
1067 (cons (string-append "-" rest-car) (cdr argv)))))
1068 (return kw #f new-argv)))
1070 ((member kw kw-args)
1071 (let* ((rest-car (substring (car argv) 2 (string-length (car argv))))
1072 (arg (if (= 0 (string-length rest-car))
1075 (new-argv (if (= 0 (string-length rest-car))
1078 (return kw arg new-argv)))
1080 (else (return 'usage-error kw argv)))))))
1082 (define (for-next-option proc argv kw-opts kw-args)
1083 (let loop ((argv argv))
1084 (get-option argv kw-opts kw-args
1085 (lambda (opt opt-arg argv)
1086 (and opt (proc opt opt-arg argv loop))))))
1088 (define (display-usage-report kw-desc)
1091 (or (eq? (car kw) #t)
1092 (eq? (car kw) 'else)
1093 (let* ((opt-desc kw)
1094 (help (cadr opt-desc))
1095 (opts (car opt-desc))
1096 (opts-proper (if (string? (car opts)) (cdr opts) opts))
1097 (arg-name (if (string? (car opts))
1098 (string-append "<" (car opts) ">")
1100 (left-part (string-append
1101 (with-output-to-string
1103 (map (lambda (x) (display (keyword-symbol x)) (display " "))
1106 (middle-part (if (and (< (string-length left-part) 30)
1107 (< (string-length help) 40))
1108 (make-string (- 30 (string-length left-part)) #\ )
1111 (display middle-part)
1118 (define (transform-usage-lambda cases)
1119 (let* ((raw-usage (delq! 'else (map car cases)))
1120 (usage-sans-specials (map (lambda (x)
1121 (or (and (not (list? x)) x)
1122 (and (symbol? (car x)) #t)
1123 (and (boolean? (car x)) #t)
1126 (usage-desc (delq! #t usage-sans-specials))
1127 (kw-desc (map car usage-desc))
1128 (kw-opts (apply append (map (lambda (x) (and (not (string? (car x))) x)) kw-desc)))
1129 (kw-args (apply append (map (lambda (x) (and (string? (car x)) (cdr x))) kw-desc)))
1130 (transmogrified-cases (map (lambda (case)
1131 (cons (let ((opts (car case)))
1132 (if (or (boolean? opts) (eq? 'else opts))
1135 ((symbol? (car opts)) opts)
1136 ((boolean? (car opts)) opts)
1137 ((string? (caar opts)) (cdar opts))
1138 (else (car opts)))))
1141 `(let ((%display-usage (lambda () (display-usage-report ',usage-desc))))
1143 (let %next-arg ((%argv %argv))
1147 (lambda (%opt %arg %new-argv)
1149 ,@ transmogrified-cases))))))))
1154 ;;; {Low Level Modules}
1156 ;;; These are the low level data structures for modules.
1158 ;;; !!! warning: The interface to lazy binder procedures is going
1159 ;;; to be changed in an incompatible way to permit all the basic
1160 ;;; module ops to be virtualized.
1162 ;;; (make-module size use-list lazy-binding-proc) => module
1163 ;;; module-{obarray,uses,binder}[|-set!]
1164 ;;; (module? obj) => [#t|#f]
1165 ;;; (module-locally-bound? module symbol) => [#t|#f]
1166 ;;; (module-bound? module symbol) => [#t|#f]
1167 ;;; (module-symbol-locally-interned? module symbol) => [#t|#f]
1168 ;;; (module-symbol-interned? module symbol) => [#t|#f]
1169 ;;; (module-local-variable module symbol) => [#<variable ...> | #f]
1170 ;;; (module-variable module symbol) => [#<variable ...> | #f]
1171 ;;; (module-symbol-binding module symbol opt-value)
1172 ;;; => [ <obj> | opt-value | an error occurs ]
1173 ;;; (module-make-local-var! module symbol) => #<variable...>
1174 ;;; (module-add! module symbol var) => unspecified
1175 ;;; (module-remove! module symbol) => unspecified
1176 ;;; (module-for-each proc module) => unspecified
1177 ;;; (make-scm-module) => module ; a lazy copy of the symhash module
1178 ;;; (set-current-module module) => unspecified
1179 ;;; (current-module) => #<module...>
1184 ;;; {Printing Modules}
1185 ;; This is how modules are printed. You can re-define it.
1186 ;; (Redefining is actually more complicated than simply redefining
1187 ;; %print-module because that would only change the binding and not
1188 ;; the value stored in the vtable that determines how record are
1191 (define (%print-module mod port) ; unused args: depth length style table)
1193 (display (or (module-kind mod) "module") port)
1194 (let ((name (module-name mod)))
1198 (display name port))))
1200 (display (number->string (object-address mod) 16) port)
1205 ;; A module is characterized by an obarray in which local symbols
1206 ;; are interned, a list of modules, "uses", from which non-local
1207 ;; bindings can be inherited, and an optional lazy-binder which
1208 ;; is a (CLOSURE module symbol) which, as a last resort, can provide
1209 ;; bindings that would otherwise not be found locally in the module.
1212 (make-record-type 'module
1213 '(obarray uses binder eval-closure transformer name kind)
1216 ;; make-module &opt size uses binder
1218 ;; Create a new module, perhaps with a particular size of obarray,
1219 ;; initial uses list, or binding procedure.
1224 (define (parse-arg index default)
1225 (if (> (length args) index)
1226 (list-ref args index)
1229 (if (> (length args) 3)
1230 (error "Too many args to make-module." args))
1232 (let ((size (parse-arg 0 1021))
1233 (uses (parse-arg 1 '()))
1234 (binder (parse-arg 2 #f)))
1236 (if (not (integer? size))
1237 (error "Illegal size to make-module." size))
1238 (if (not (and (list? uses)
1239 (and-map module? uses)))
1240 (error "Incorrect use list." uses))
1241 (if (and binder (not (procedure? binder)))
1243 "Lazy-binder expected to be a procedure or #f." binder))
1245 (let ((module (module-constructor (make-vector size '())
1246 uses binder #f #f #f #f)))
1248 ;; We can't pass this as an argument to module-constructor,
1249 ;; because we need it to close over a pointer to the module
1251 (set-module-eval-closure! module
1252 (lambda (symbol define?)
1254 (module-make-local-var! module symbol)
1255 (module-variable module symbol))))
1259 (define module-constructor (record-constructor module-type))
1260 (define module-obarray (record-accessor module-type 'obarray))
1261 (define set-module-obarray! (record-modifier module-type 'obarray))
1262 (define module-uses (record-accessor module-type 'uses))
1263 (define set-module-uses! (record-modifier module-type 'uses))
1264 (define module-binder (record-accessor module-type 'binder))
1265 (define set-module-binder! (record-modifier module-type 'binder))
1266 (define module-eval-closure (record-accessor module-type 'eval-closure))
1267 (define set-module-eval-closure! (record-modifier module-type 'eval-closure))
1268 (define module-transformer (record-accessor module-type 'transformer))
1269 (define set-module-transformer! (record-modifier module-type 'transformer))
1270 (define module-name (record-accessor module-type 'name))
1271 (define set-module-name! (record-modifier module-type 'name))
1272 (define module-kind (record-accessor module-type 'kind))
1273 (define set-module-kind! (record-modifier module-type 'kind))
1274 (define module? (record-predicate module-type))
1277 (define (eval-in-module exp module)
1278 (eval2 exp (module-eval-closure module)))
1281 ;;; {Module Searching in General}
1283 ;;; We sometimes want to look for properties of a symbol
1284 ;;; just within the obarray of one module. If the property
1285 ;;; holds, then it is said to hold ``locally'' as in, ``The symbol
1286 ;;; DISPLAY is locally rebound in the module `safe-guile'.''
1289 ;;; Other times, we want to test for a symbol property in the obarray
1290 ;;; of M and, if it is not found there, try each of the modules in the
1291 ;;; uses list of M. This is the normal way of testing for some
1292 ;;; property, so we state these properties without qualification as
1293 ;;; in: ``The symbol 'fnord is interned in module M because it is
1294 ;;; interned locally in module M2 which is a member of the uses list
1298 ;; module-search fn m
1300 ;; return the first non-#f result of FN applied to M and then to
1301 ;; the modules in the uses of m, and so on recursively. If all applications
1302 ;; return #f, then so does this function.
1304 (define (module-search fn m v)
1307 (or (module-search fn (car pos) v)
1310 (loop (module-uses m))))
1313 ;;; {Is a symbol bound in a module?}
1315 ;;; Symbol S in Module M is bound if S is interned in M and if the binding
1316 ;;; of S in M has been set to some well-defined value.
1319 ;; module-locally-bound? module symbol
1321 ;; Is a symbol bound (interned and defined) locally in a given module?
1323 (define (module-locally-bound? m v)
1324 (let ((var (module-local-variable m v)))
1326 (variable-bound? var))))
1328 ;; module-bound? module symbol
1330 ;; Is a symbol bound (interned and defined) anywhere in a given module
1333 (define (module-bound? m v)
1334 (module-search module-locally-bound? m v))
1336 ;;; {Is a symbol interned in a module?}
1338 ;;; Symbol S in Module M is interned if S occurs in
1339 ;;; of S in M has been set to some well-defined value.
1341 ;;; It is possible to intern a symbol in a module without providing
1342 ;;; an initial binding for the corresponding variable. This is done
1344 ;;; (module-add! module symbol (make-undefined-variable))
1346 ;;; In that case, the symbol is interned in the module, but not
1347 ;;; bound there. The unbound symbol shadows any binding for that
1348 ;;; symbol that might otherwise be inherited from a member of the uses list.
1351 (define (module-obarray-get-handle ob key)
1352 ((if (symbol? key) hashq-get-handle hash-get-handle) ob key))
1354 (define (module-obarray-ref ob key)
1355 ((if (symbol? key) hashq-ref hash-ref) ob key))
1357 (define (module-obarray-set! ob key val)
1358 ((if (symbol? key) hashq-set! hash-set!) ob key val))
1360 (define (module-obarray-remove! ob key)
1361 ((if (symbol? key) hashq-remove! hash-remove!) ob key))
1363 ;; module-symbol-locally-interned? module symbol
1365 ;; is a symbol interned (not neccessarily defined) locally in a given module
1366 ;; or its uses? Interned symbols shadow inherited bindings even if
1367 ;; they are not themselves bound to a defined value.
1369 (define (module-symbol-locally-interned? m v)
1370 (not (not (module-obarray-get-handle (module-obarray m) v))))
1372 ;; module-symbol-interned? module symbol
1374 ;; is a symbol interned (not neccessarily defined) anywhere in a given module
1375 ;; or its uses? Interned symbols shadow inherited bindings even if
1376 ;; they are not themselves bound to a defined value.
1378 (define (module-symbol-interned? m v)
1379 (module-search module-symbol-locally-interned? m v))
1382 ;;; {Mapping modules x symbols --> variables}
1385 ;; module-local-variable module symbol
1386 ;; return the local variable associated with a MODULE and SYMBOL.
1388 ;;; This function is very important. It is the only function that can
1389 ;;; return a variable from a module other than the mutators that store
1390 ;;; new variables in modules. Therefore, this function is the location
1391 ;;; of the "lazy binder" hack.
1393 ;;; If symbol is defined in MODULE, and if the definition binds symbol
1394 ;;; to a variable, return that variable object.
1396 ;;; If the symbols is not found at first, but the module has a lazy binder,
1397 ;;; then try the binder.
1399 ;;; If the symbol is not found at all, return #f.
1401 (define (module-local-variable m v)
1404 (let ((b (module-obarray-ref (module-obarray m) v)))
1405 (or (and (variable? b) b)
1406 (and (module-binder m)
1407 ((module-binder m) m v #f)))))
1410 ;; module-variable module symbol
1412 ;; like module-local-variable, except search the uses in the
1413 ;; case V is not found in M.
1415 (define (module-variable m v)
1416 (module-search module-local-variable m v))
1419 ;;; {Mapping modules x symbols --> bindings}
1421 ;;; These are similar to the mapping to variables, except that the
1422 ;;; variable is dereferenced.
1425 ;; module-symbol-binding module symbol opt-value
1427 ;; return the binding of a variable specified by name within
1428 ;; a given module, signalling an error if the variable is unbound.
1429 ;; If the OPT-VALUE is passed, then instead of signalling an error,
1430 ;; return OPT-VALUE.
1432 (define (module-symbol-local-binding m v . opt-val)
1433 (let ((var (module-local-variable m v)))
1436 (if (not (null? opt-val))
1438 (error "Locally unbound variable." v)))))
1440 ;; module-symbol-binding module symbol opt-value
1442 ;; return the binding of a variable specified by name within
1443 ;; a given module, signalling an error if the variable is unbound.
1444 ;; If the OPT-VALUE is passed, then instead of signalling an error,
1445 ;; return OPT-VALUE.
1447 (define (module-symbol-binding m v . opt-val)
1448 (let ((var (module-variable m v)))
1451 (if (not (null? opt-val))
1453 (error "Unbound variable." v)))))
1457 ;;; {Adding Variables to Modules}
1462 ;; module-make-local-var! module symbol
1464 ;; ensure a variable for V in the local namespace of M.
1465 ;; If no variable was already there, then create a new and uninitialzied
1468 (define (module-make-local-var! m v)
1469 (or (let ((b (module-obarray-ref (module-obarray m) v)))
1470 (and (variable? b) b))
1471 (and (module-binder m)
1472 ((module-binder m) m v #t))
1474 (let ((answer (make-undefined-variable v)))
1475 (module-obarray-set! (module-obarray m) v answer)
1478 ;; module-add! module symbol var
1480 ;; ensure a particular variable for V in the local namespace of M.
1482 (define (module-add! m v var)
1483 (if (not (variable? var))
1484 (error "Bad variable to module-add!" var))
1485 (module-obarray-set! (module-obarray m) v var))
1489 ;; make sure that a symbol is undefined in the local namespace of M.
1491 (define (module-remove! m v)
1492 (module-obarray-remove! (module-obarray m) v))
1494 (define (module-clear! m)
1495 (vector-fill! (module-obarray m) '()))
1497 ;; MODULE-FOR-EACH -- exported
1499 ;; Call PROC on each symbol in MODULE, with arguments of (SYMBOL VARIABLE).
1501 (define (module-for-each proc module)
1502 (let ((obarray (module-obarray module)))
1503 (do ((index 0 (+ index 1))
1504 (end (vector-length obarray)))
1508 (proc (car bucket) (cdr bucket)))
1509 (vector-ref obarray index)))))
1512 (define (module-map proc module)
1513 (let* ((obarray (module-obarray module))
1514 (end (vector-length obarray)))
1522 (map (lambda (bucket)
1523 (proc (car bucket) (cdr bucket)))
1524 (vector-ref obarray i))
1528 ;;; {Low Level Bootstrapping}
1533 ;; A root module uses the symhash table (the system's privileged
1534 ;; obarray). Being inside a root module is like using SCM without
1535 ;; any module system.
1539 (define (root-module-closure m s define?)
1540 (let ((bi (and (symbol-interned? #f s)
1541 (builtin-variable s))))
1543 (or define? (variable-bound? bi))
1545 (module-add! m s bi)
1548 (define (make-root-module)
1549 (make-module 1019 '() root-module-closure))
1554 ;; An scm module is a module into which the lazy binder copies
1555 ;; variable bindings from the system symhash table. The mapping is
1556 ;; one way only; newly introduced bindings in an scm module are not
1557 ;; copied back into the system symhash table (and can be used to override
1558 ;; bindings from the symhash table).
1561 (define (make-scm-module)
1562 (make-module 1019 '()
1563 (lambda (m s define?)
1564 (let ((bi (and (symbol-interned? #f s)
1565 (builtin-variable s))))
1567 (variable-bound? bi)
1569 (module-add! m s bi)
1577 (define the-module #f)
1579 ;; scm:eval-transformer
1581 (define scm:eval-transformer #f)
1583 ;; set-current-module module
1585 ;; set the current module as viewed by the normalizer.
1587 (define (set-current-module m)
1591 (set! *top-level-lookup-closure* (module-eval-closure the-module))
1592 (set! scm:eval-transformer (module-transformer the-module)))
1593 (set! *top-level-lookup-closure* #f)))
1598 ;; return the current module as viewed by the normalizer.
1600 (define (current-module) the-module)
1602 ;;; {Module-based Loading}
1605 (define (save-module-excursion thunk)
1606 (let ((inner-module (current-module))
1608 (dynamic-wind (lambda ()
1609 (set! outer-module (current-module))
1610 (set-current-module inner-module)
1611 (set! inner-module #f))
1614 (set! inner-module (current-module))
1615 (set-current-module outer-module)
1616 (set! outer-module #f)))))
1618 (define basic-load load)
1620 (define (load-module . args)
1621 (save-module-excursion (lambda () (apply basic-load args))))
1625 ;;; {MODULE-REF -- exported}
1627 ;; Returns the value of a variable called NAME in MODULE or any of its
1628 ;; used modules. If there is no such variable, then if the optional third
1629 ;; argument DEFAULT is present, it is returned; otherwise an error is signaled.
1631 (define (module-ref module name . rest)
1632 (let ((variable (module-variable module name)))
1633 (if (and variable (variable-bound? variable))
1634 (variable-ref variable)
1636 (error "No variable named" name 'in module)
1637 (car rest) ; default value
1640 ;; MODULE-SET! -- exported
1642 ;; Sets the variable called NAME in MODULE (or in a module that MODULE uses)
1643 ;; to VALUE; if there is no such variable, an error is signaled.
1645 (define (module-set! module name value)
1646 (let ((variable (module-variable module name)))
1648 (variable-set! variable value)
1649 (error "No variable named" name 'in module))))
1651 ;; MODULE-DEFINE! -- exported
1653 ;; Sets the variable called NAME in MODULE to VALUE; if there is no such
1654 ;; variable, it is added first.
1656 (define (module-define! module name value)
1657 (let ((variable (module-local-variable module name)))
1659 (variable-set! variable value)
1660 (module-add! module name (make-variable value name)))))
1662 ;; MODULE-DEFINED? -- exported
1664 ;; Return #t iff NAME is defined in MODULE (or in a module that MODULE
1667 (define (module-defined? module name)
1668 (let ((variable (module-variable module name)))
1669 (and variable (variable-bound? variable))))
1671 ;; MODULE-USE! module interface
1673 ;; Add INTERFACE to the list of interfaces used by MODULE.
1675 (define (module-use! module interface)
1676 (set-module-uses! module
1677 (cons interface (delq! interface (module-uses module)))))
1680 ;;; {Recursive Namespaces}
1683 ;;; A hierarchical namespace emerges if we consider some module to be
1684 ;;; root, and variables bound to modules as nested namespaces.
1686 ;;; The routines in this file manage variable names in hierarchical namespace.
1687 ;;; Each variable name is a list of elements, looked up in successively nested
1690 ;;; (nested-ref some-root-module '(foo bar baz))
1691 ;;; => <value of a variable named baz in the module bound to bar in
1692 ;;; the module bound to foo in some-root-module>
1697 ;;; ;; a-root is a module
1698 ;;; ;; name is a list of symbols
1700 ;;; nested-ref a-root name
1701 ;;; nested-set! a-root name val
1702 ;;; nested-define! a-root name val
1703 ;;; nested-remove! a-root name
1706 ;;; (current-module) is a natural choice for a-root so for convenience there are
1709 ;;; local-ref name == nested-ref (current-module) name
1710 ;;; local-set! name val == nested-set! (current-module) name val
1711 ;;; local-define! name val == nested-define! (current-module) name val
1712 ;;; local-remove! name == nested-remove! (current-module) name
1716 (define (nested-ref root names)
1717 (let loop ((cur root)
1721 ((not (module? cur)) #f)
1722 (else (loop (module-ref cur (car elts) #f) (cdr elts))))))
1724 (define (nested-set! root names val)
1725 (let loop ((cur root)
1727 (if (null? (cdr elts))
1728 (module-set! cur (car elts) val)
1729 (loop (module-ref cur (car elts)) (cdr elts)))))
1731 (define (nested-define! root names val)
1732 (let loop ((cur root)
1734 (if (null? (cdr elts))
1735 (module-define! cur (car elts) val)
1736 (loop (module-ref cur (car elts)) (cdr elts)))))
1738 (define (nested-remove! root names)
1739 (let loop ((cur root)
1741 (if (null? (cdr elts))
1742 (module-remove! cur (car elts))
1743 (loop (module-ref cur (car elts)) (cdr elts)))))
1745 (define (local-ref names) (nested-ref (current-module) names))
1746 (define (local-set! names val) (nested-set! (current-module) names val))
1747 (define (local-define names val) (nested-define! (current-module) names val))
1748 (define (local-remove names) (nested-remove! (current-module) names))
1752 ;;; {The (app) module}
1754 ;;; The root of conventionally named objects not directly in the top level.
1757 ;;; (app modules guile)
1759 ;;; The directory of all modules and the standard root module.
1762 (define (module-public-interface m) (module-ref m '%module-public-interface #f))
1763 (define (set-module-public-interface! m i) (module-define! m '%module-public-interface i))
1764 (define the-root-module (make-root-module))
1765 (define the-scm-module (make-scm-module))
1766 (set-module-public-interface! the-root-module the-scm-module)
1767 (set-module-name! the-root-module 'the-root-module)
1768 (set-module-name! the-scm-module 'the-scm-module)
1770 (set-current-module the-root-module)
1772 (define app (make-module 31))
1773 (local-define '(app modules) (make-module 31))
1774 (local-define '(app modules guile) the-root-module)
1776 ;; (define-special-value '(app modules new-ws) (lambda () (make-scm-module)))
1778 (define (resolve-module name . maybe-autoload)
1779 (let ((full-name (append '(app modules) name)))
1780 (let ((already (local-ref full-name)))
1783 (if (or (null? maybe-autoload) (car maybe-autoload))
1784 (or (try-module-linked name)
1785 (try-module-autoload name)
1786 (try-module-dynamic-link name)))
1787 (make-modules-in (current-module) full-name))))))
1789 (define (beautify-user-module! module)
1790 (if (not (module-public-interface module))
1791 (let ((interface (make-module 31)))
1792 (set-module-name! interface (module-name module))
1793 (set-module-kind! interface 'interface)
1794 (set-module-public-interface! module interface)))
1795 (if (and (not (memq the-scm-module (module-uses module)))
1796 (not (eq? module the-root-module)))
1797 (set-module-uses! module (append (module-uses module) (list the-scm-module)))))
1799 (define (make-modules-in module name)
1803 ((module-ref module (car name) #f) => (lambda (m) (make-modules-in m (cdr name))))
1804 (else (let ((m (make-module 31)))
1805 (set-module-kind! m 'directory)
1806 (set-module-name! m (car name))
1807 (module-define! module (car name) m)
1808 (make-modules-in m (cdr name)))))))
1810 (define (resolve-interface name)
1811 (let ((module (resolve-module name)))
1812 (and module (module-public-interface module))))
1815 (define %autoloader-developer-mode #t)
1817 (define (process-define-module args)
1818 (let* ((module-id (car args))
1819 (module (resolve-module module-id #f))
1821 (beautify-user-module! module)
1822 (let loop ((kws kws)
1823 (reversed-interfaces '()))
1825 (for-each (lambda (interface)
1826 (module-use! module interface))
1827 reversed-interfaces)
1828 (case (cond ((keyword? (car kws))
1829 (keyword->symbol (car kws)))
1830 ((and (symbol? (car kws))
1831 (eq? (string-ref (car kws) 0) #\:))
1832 (string->symbol (substring (car kws) 1)))
1835 (if (not (pair? (cdr kws)))
1836 (error "unrecognized defmodule argument" kws))
1837 (let* ((used-name (cadr kws))
1838 (used-module (resolve-module used-name)))
1839 (if (not (module-ref used-module '%module-public-interface #f))
1841 ((if %autoloader-developer-mode warn error)
1842 "no code for module" (module-name used-module))
1843 (beautify-user-module! used-module)))
1844 (let ((interface (module-public-interface used-module)))
1846 (error "missing interface for use-module" used-module))
1847 (loop (cddr kws) (cons interface reversed-interfaces)))))
1849 (error "unrecognized defmodule argument" kws)))))
1852 ;;; {Autoloading modules}
1854 (define autoloads-in-progress '())
1856 (define (try-module-autoload module-name)
1858 (define (sfx name) (string-append name (scheme-file-suffix)))
1859 (let* ((reverse-name (reverse module-name))
1860 (name (car reverse-name))
1861 (dir-hint-module-name (reverse (cdr reverse-name)))
1862 (dir-hint (apply symbol-append (map (lambda (elt) (symbol-append elt "/")) dir-hint-module-name))))
1863 (resolve-module dir-hint-module-name #f)
1864 (and (not (autoload-done-or-in-progress? dir-hint name))
1867 (lambda () (autoload-in-progress! dir-hint name))
1869 (let loop ((dirs %load-path))
1870 (and (not (null? dirs))
1872 (let ((d (car dirs))
1876 (in-vicinity dir-hint name)
1877 (in-vicinity dir-hint (sfx name)))))
1878 (and (or-map (lambda (f)
1879 (let ((full (in-vicinity d f)))
1881 (and (file-exists? full)
1882 (not (file-is-directory? full))
1884 (save-module-excursion
1886 (load (string-append
1893 (loop (cdr dirs))))))
1894 (lambda () (set-autoloaded! dir-hint name didit)))
1897 ;;; Dynamic linking of modules
1899 ;; Initializing a module that is written in C is a two step process.
1900 ;; First the module's `module init' function is called. This function
1901 ;; is expected to call `scm_register_module_xxx' to register the `real
1902 ;; init' function. Later, when the module is referenced for the first
1903 ;; time, this real init function is called in the right context. See
1904 ;; gtcltk-lib/gtcltk-module.c for an example.
1906 ;; The code for the module can be in a regular shared library (so that
1907 ;; the `module init' function will be called when libguile is
1908 ;; initialized). Or it can be dynamically linked.
1910 ;; You can safely call `scm_register_module_xxx' before libguile
1911 ;; itself is initialized. You could call it from an C++ constructor
1912 ;; of a static object, for example.
1914 ;; To make your Guile extension into a dynamic linkable module, follow
1915 ;; these easy steps:
1917 ;; - Find a name for your module, like (ice-9 gtcltk)
1918 ;; - Write a function with a name like
1920 ;; scm_init_ice_9_gtcltk_module
1922 ;; This is your `module init' function. It should call
1924 ;; scm_register_module_xxx ("ice-9 gtcltk", scm_init_gtcltk);
1926 ;; "ice-9 gtcltk" is the C version of the module name. Slashes are
1927 ;; replaced by spaces, the rest is untouched. `scm_init_gtcltk' is
1928 ;; the real init function that executes the usual initializations
1929 ;; like making new smobs, etc.
1931 ;; - Make a shared library with your code and a name like
1933 ;; ice-9/libgtcltk.so
1935 ;; and put it somewhere in %load-path.
1937 ;; - Then you can simply write `:use-module (ice-9 gtcltk)' and it
1938 ;; will be linked automatically.
1940 ;; This is all very experimental.
1942 (define (split-c-module-name str)
1943 (let loop ((rev '())
1946 (end (string-length str)))
1949 (reverse (cons (string->symbol (substring str start pos)) rev)))
1950 ((eq? (string-ref str pos) #\space)
1951 (loop (cons (string->symbol (substring str start pos)) rev)
1956 (loop rev start (+ pos 1) end)))))
1958 (define (convert-c-registered-modules dynobj)
1959 (let ((res (map (lambda (c)
1960 (list (split-c-module-name (car c)) (cdr c) dynobj))
1961 (c-registered-modules))))
1962 (c-clear-registered-modules)
1965 (define registered-modules (convert-c-registered-modules #f))
1967 (define (init-dynamic-module modname)
1968 (or-map (lambda (modinfo)
1969 (if (equal? (car modinfo) modname)
1970 (let ((mod (resolve-module modname #f)))
1971 (save-module-excursion
1973 (set-current-module mod)
1974 (dynamic-call (cadr modinfo) (caddr modinfo))
1975 (set-module-public-interface! mod mod)))
1976 (set! registered-modules (delq! modinfo registered-modules))
1979 registered-modules))
1981 (define (dynamic-maybe-call name dynobj)
1982 (catch #t ; could use false-if-exception here
1984 (dynamic-call name dynobj))
1988 (define (dynamic-maybe-link filename)
1989 (catch #t ; could use false-if-exception here
1991 (dynamic-link filename))
1995 (define (find-and-link-dynamic-module module-name)
1996 (define (make-init-name mod-name)
1997 (string-append 'scm_init
1998 (list->string (map (lambda (c)
1999 (if (or (char-alphabetic? c)
2003 (string->list mod-name)))
2006 (let loop ((dirs "")
2010 (string-append dirs "lib" (car syms) ".so"))
2012 (loop (string-append dirs (car syms) "/") (cdr syms))))))
2013 (init (make-init-name (apply string-append
2015 (string-append "_" s))
2017 ;; (pk 'libname libname 'init init)
2020 (let ((full (in-vicinity dir libname)))
2021 ;; (pk 'trying full)
2022 (if (file-exists? full)
2024 (link-dynamic-module full init)
2029 (define (link-dynamic-module filename initname)
2030 (let ((dynobj (dynamic-link filename)))
2031 (dynamic-call initname dynobj)
2032 (set! registered-modules
2033 (append! (convert-c-registered-modules dynobj)
2034 registered-modules))))
2036 (define (try-module-linked module-name)
2037 (init-dynamic-module module-name))
2039 (define (try-module-dynamic-link module-name)
2040 (and (find-and-link-dynamic-module module-name)
2041 (init-dynamic-module module-name)))
2045 (define autoloads-done '((guile . guile)))
2047 (define (autoload-done-or-in-progress? p m)
2048 (let ((n (cons p m)))
2049 (->bool (or (member n autoloads-done)
2050 (member n autoloads-in-progress)))))
2052 (define (autoload-done! p m)
2053 (let ((n (cons p m)))
2054 (set! autoloads-in-progress
2055 (delete! n autoloads-in-progress))
2056 (or (member n autoloads-done)
2057 (set! autoloads-done (cons n autoloads-done)))))
2059 (define (autoload-in-progress! p m)
2060 (let ((n (cons p m)))
2061 (set! autoloads-done
2062 (delete! n autoloads-done))
2063 (set! autoloads-in-progress (cons n autoloads-in-progress))))
2065 (define (set-autoloaded! p m done?)
2067 (autoload-done! p m)
2068 (let ((n (cons p m)))
2069 (set! autoloads-done (delete! n autoloads-done))
2070 (set! autoloads-in-progress (delete! n autoloads-in-progress)))))
2079 (define (primitive-macro? m)
2081 (not (macro-transformer m))))
2085 (define macro-table (make-weak-key-hash-table 523))
2086 (define xformer-table (make-weak-key-hash-table 523))
2088 (define (defmacro? m) (hashq-ref macro-table m))
2089 (define (assert-defmacro?! m) (hashq-set! macro-table m #t))
2090 (define (defmacro-transformer m) (hashq-ref xformer-table m))
2091 (define (set-defmacro-transformer! m t) (hashq-set! xformer-table m t))
2093 (define defmacro:transformer
2095 (let* ((xform (lambda (exp env)
2096 (copy-tree (apply f (cdr exp)))))
2097 (a (procedure->memoizing-macro xform)))
2098 (assert-defmacro?! a)
2099 (set-defmacro-transformer! a f)
2104 (let ((defmacro-transformer
2105 (lambda (name parms . body)
2106 (let ((transformer `(lambda ,parms ,@body)))
2108 (,(lambda (transformer)
2109 (defmacro:transformer transformer))
2111 (defmacro:transformer defmacro-transformer)))
2113 (define defmacro:syntax-transformer
2117 (copy-tree (apply f (cdr exp)))))))
2120 ;; XXX - should the definition of the car really be looked up in the
2123 (define (macroexpand-1 e)
2125 ((pair? e) (let* ((a (car e))
2126 (val (and (symbol? a) (local-ref (list a)))))
2128 (apply (defmacro-transformer val) (cdr e))
2132 (define (macroexpand e)
2134 ((pair? e) (let* ((a (car e))
2135 (val (and (symbol? a) (local-ref (list a)))))
2137 (macroexpand (apply (defmacro-transformer val) (cdr e)))
2148 ;;; {Run-time options}
2150 ((let* ((names '((debug-options-interface
2151 (debug-options debug-enable debug-disable)
2154 (evaluator-traps-interface
2155 (traps trap-enable trap-disable)
2158 (read-options-interface
2159 (read-options read-enable read-disable)
2162 (print-options-interface
2163 (print-options print-enable print-disable)
2168 (option-documentation caddr)
2170 (print-option (lambda (option)
2171 (display (option-name option))
2172 (if (< (string-length
2173 (symbol->string (option-name option)))
2177 (display (option-value option))
2179 (display (option-documentation option))
2182 ;; Below follows the macros defining the run-time option interfaces.
2184 (make-options (lambda (interface)
2186 (cond ((null? args) (,interface))
2188 (,interface (car args)) (,interface))
2189 (else (for-each ,print-option
2190 (,interface #t)))))))
2192 (make-enable (lambda (interface)
2194 (,interface (append flags (,interface)))
2197 (make-disable (lambda (interface)
2199 (let ((options (,interface)))
2200 (for-each (lambda (flag)
2201 (set! options (delq! flag options)))
2203 (,interface options)
2206 (make-set! (lambda (interface)
2209 (begin (,interface (append (,interface)
2210 (list '(,'unquote name)
2218 (map (lambda (group)
2219 (let ((interface (car group)))
2220 (append (map (lambda (name constructor)
2222 ,(constructor interface)))
2227 (map (lambda (name constructor)
2229 ,@(constructor interface)))
2231 (list make-set!)))))
2239 (define (repl read evaler print)
2240 (let loop ((source (read (current-input-port))))
2241 (print (evaler source))
2242 (loop (read (current-input-port)))))
2244 ;; A provisional repl that acts like the SCM repl:
2246 (define scm-repl-silent #f)
2247 (define (assert-repl-silence v) (set! scm-repl-silent v))
2249 (define *unspecified* (if #f #f))
2250 (define (unspecified? v) (eq? v *unspecified*))
2252 (define scm-repl-print-unspecified #f)
2253 (define (assert-repl-print-unspecified v) (set! scm-repl-print-unspecified v))
2255 (define scm-repl-verbose #f)
2256 (define (assert-repl-verbosity v) (set! scm-repl-verbose v))
2258 (define scm-repl-prompt "guile> ")
2260 (define (set-repl-prompt! v) (set! scm-repl-prompt v))
2262 (define (default-lazy-handler key . args)
2263 (save-stack lazy-handler-dispatch)
2264 (apply throw key args))
2266 (define apply-frame-handler default-lazy-handler)
2267 (define exit-frame-handler default-lazy-handler)
2269 (define (lazy-handler-dispatch key . args)
2272 (apply apply-frame-handler key args))
2274 (apply exit-frame-handler key args))
2276 (apply default-lazy-handler key args))))
2278 (define abort-hook '())
2280 ;; defined in error-catching-loop as a closures.
2281 (define set-batch-mode?! #f)
2282 (define batch-mode? #f)
2284 (define (error-catching-loop thunk)
2287 (set! set-batch-mode?! (lambda (arg)
2289 (set! interactive #f)
2292 (error "sorry, not implemented")))))
2293 (set! batch-mode? (lambda () (not interactive)))
2294 (define (loop first)
2302 (lambda () (unmask-signals))
2306 ;; This line is needed because mark
2307 ;; doesn't do closures quite right.
2308 ;; Unreferenced locals should be
2312 (let loop ((v (thunk)))
2315 (lambda () (mask-signals))))
2317 lazy-handler-dispatch))
2319 (lambda (key . args)
2327 (apply throw 'switch-repl args))
2330 ;; This is one of the closures that require
2331 ;; (set! first #f) above
2334 (run-hooks abort-hook)
2336 (display "ABORT: " (current-error-port))
2337 (write args (current-error-port))
2338 (newline (current-error-port))
2340 (if (and (not has-shown-debugger-hint?)
2341 (not (memq 'backtrace
2342 (debug-options-interface)))
2343 (stack? the-last-stack))
2345 (newline (current-error-port))
2347 "Type \"(backtrace)\" to get more information.\n"
2348 (current-error-port))
2349 (set! has-shown-debugger-hint? #t)))
2351 (set! stack-saved? #f)))
2354 ;; This is the other cons-leak closure...
2356 (cond ((= (length args) 4)
2357 (apply handle-system-error key args))
2359 (apply bad-throw key args))))))))))
2360 (if next (loop next) status)))
2361 (loop (lambda () #t))))
2363 ;;(define the-last-stack #f) Defined by scm_init_backtrace ()
2364 (define stack-saved? #f)
2366 (define (save-stack . narrowing)
2367 (cond (stack-saved?)
2368 ((not (memq 'debug (debug-options-interface)))
2369 (set! the-last-stack #f)
2370 (set! stack-saved? #t))
2372 (set! the-last-stack
2375 (apply make-stack #t save-stack eval narrowing))
2377 (apply make-stack #t save-stack 0 narrowing))
2379 (apply make-stack #t save-stack tk-stack-mark narrowing))
2381 (apply make-stack #t save-stack 0 1 narrowing))
2382 (else (let ((id (stack-id #t)))
2383 (and (procedure? id)
2384 (apply make-stack #t save-stack id narrowing))))))
2385 (set! stack-saved? #t))))
2387 (define before-error-hook '())
2388 (define after-error-hook '())
2389 (define before-backtrace-hook '())
2390 (define after-backtrace-hook '())
2392 (define has-shown-debugger-hint? #f)
2394 (define (handle-system-error key . args)
2395 (let ((cep (current-error-port)))
2396 (cond ((not (stack? the-last-stack)))
2397 ((memq 'backtrace (debug-options-interface))
2398 (run-hooks before-backtrace-hook)
2400 (display-backtrace the-last-stack cep)
2402 (run-hooks after-backtrace-hook)))
2403 (run-hooks before-error-hook)
2404 (apply display-error the-last-stack cep args)
2405 (run-hooks after-error-hook)
2407 (throw 'abort key)))
2409 (define (quit . args)
2410 (apply throw 'quit args))
2414 ;;(define has-shown-backtrace-hint? #f) Defined by scm_init_backtrace ()
2416 ;; Replaced by C code:
2417 ;;(define (backtrace)
2418 ;; (if the-last-stack
2421 ;; (display-backtrace the-last-stack (current-output-port))
2423 ;; (if (and (not has-shown-backtrace-hint?)
2424 ;; (not (memq 'backtrace (debug-options-interface))))
2427 ;;"Type \"(debug-enable 'backtrace)\" if you would like a backtrace
2428 ;;automatically if an error occurs in the future.\n")
2429 ;; (set! has-shown-backtrace-hint? #t))))
2430 ;; (display "No backtrace available.\n")))
2432 (define (error-catching-repl r e p)
2433 (error-catching-loop (lambda () (p (e (r))))))
2435 (define (gc-run-time)
2436 (cdr (assq 'gc-time-taken (gc-stats))))
2438 (define before-read-hook '())
2439 (define after-read-hook '())
2441 ;;; The default repl-reader function. We may override this if we've
2442 ;;; the readline library.
2447 (read (current-input-port))))
2449 (define (scm-style-repl)
2453 (repl-report-start-timing (lambda ()
2454 (set! start-gc-rt (gc-run-time))
2455 (set! start-rt (get-internal-run-time))))
2456 (repl-report (lambda ()
2458 (display (inexact->exact
2459 (* 1000 (/ (- (get-internal-run-time) start-rt)
2460 internal-time-units-per-second))))
2462 (display (inexact->exact
2463 (* 1000 (/ (- (gc-run-time) start-gc-rt)
2464 internal-time-units-per-second))))
2465 (display " msec in gc)\n")))
2467 (consume-trailing-whitespace
2469 (let ((ch (peek-char)))
2472 ((or (char=? ch #\space) (char=? ch #\tab))
2474 (consume-trailing-whitespace))
2475 ((char=? ch #\newline)
2478 ;; It would be nice if we could run this after the
2479 ;; first prompt was printed, but with readline
2480 ;; that's not possible, so we punt.
2481 (run-hooks before-read-hook)
2483 (let ((prompt (cond ((string? scm-repl-prompt)
2485 ((thunk? scm-repl-prompt)
2487 (scm-repl-prompt "> ")
2489 (repl-reader prompt))))
2491 ;; As described in R4RS, the READ procedure updates the
2492 ;; port to point to the first characetr past the end of
2493 ;; the external representation of the object. This
2494 ;; means that it doesn't consume the newline typically
2495 ;; found after an expression. This means that, when
2496 ;; debugging Guile with GDB, GDB gets the newline, which
2497 ;; it often interprets as a "continue" command, making
2498 ;; breakpoints kind of useless. So, consume any
2499 ;; trailing newline here, as well as any whitespace
2501 (consume-trailing-whitespace)
2502 (run-hooks after-read-hook)
2503 (if (eof-object? val)
2505 (repl-report-start-timing)
2506 (if scm-repl-verbose
2509 (display ";;; EOF -- quitting")
2514 (-eval (lambda (sourc)
2515 (repl-report-start-timing)
2516 (start-stack 'repl-stack (eval sourc))))
2518 (-print (lambda (result)
2519 (if (not scm-repl-silent)
2521 (if (or scm-repl-print-unspecified
2522 (not (unspecified? result)))
2526 (if scm-repl-verbose
2530 (-quit (lambda (args)
2531 (if scm-repl-verbose
2533 (display ";;; QUIT executed, repl exitting")
2539 (if scm-repl-verbose
2541 (display ";;; ABORT executed.")
2544 (repl -read -eval -print))))
2546 (let ((status (error-catching-repl -read
2553 ;;; {IOTA functions: generating lists of numbers}
2555 (define (reverse-iota n) (if (> n 0) (cons (1- n) (reverse-iota (1- n))) '()))
2556 (define (iota n) (reverse! (reverse-iota n)))
2561 ;;; with `continue' and `break'.
2564 (defmacro while (cond . body)
2565 `(letrec ((continue (lambda () (or (not ,cond) (begin (begin ,@ body) (continue)))))
2566 (break (lambda val (apply throw 'break val))))
2568 (lambda () (continue))
2569 (lambda v (cadr v)))))
2574 ;; with-fluids is a convenience wrapper for the builtin procedure
2575 ;; `with-fluids*'. The syntax is just like `let':
2577 ;; (with-fluids ((fluid val)
2581 (defmacro with-fluids (bindings . body)
2582 `(with-fluids* (list ,@(map car bindings)) (list ,@(map cadr bindings))
2583 (lambda () ,@body)))
2590 ;; actually....hobbit might be able to hack these with a little
2594 (defmacro define-macro (first . rest)
2595 (let ((name (if (symbol? first) first (car first)))
2599 `(lambda ,(cdr first) ,@rest))))
2600 `(define ,name (defmacro:transformer ,transformer))))
2603 (defmacro define-syntax-macro (first . rest)
2604 (let ((name (if (symbol? first) first (car first)))
2608 `(lambda ,(cdr first) ,@rest))))
2609 `(define ,name (defmacro:syntax-transformer ,transformer))))
2611 ;;; {Module System Macros}
2614 (defmacro define-module args
2615 `(let* ((process-define-module process-define-module)
2616 (set-current-module set-current-module)
2617 (module (process-define-module ',args)))
2618 (set-current-module module)
2621 ;; the guts of the use-modules macro. add the interfaces of the named
2622 ;; modules to the use-list of the current module, in order
2623 (define (process-use-modules module-names)
2624 (for-each (lambda (module-name)
2625 (let ((mod-iface (resolve-interface module-name)))
2627 (error "no such module" module-name))
2628 (module-use! (current-module) mod-iface)))
2629 (reverse module-names)))
2631 (defmacro use-modules modules
2632 `(process-use-modules ',modules))
2634 (define (use-syntax transformer)
2635 (set-module-transformer! (current-module) transformer)
2636 (set! scm:eval-transformer transformer))
2638 (define define-private define)
2640 (defmacro define-public args
2642 (error "bad syntax" (list 'define-public args)))
2643 (define (defined-name n)
2646 ((pair? n) (defined-name (car n)))
2649 ((null? args) (syntax))
2651 (#t (let ((name (defined-name (car args))))
2653 (let ((public-i (module-public-interface (current-module))))
2654 ;; Make sure there is a local variable:
2656 (module-define! (current-module)
2658 (module-ref (current-module) ',name #f))
2660 ;; Make sure that local is exported:
2662 (module-add! public-i ',name
2663 (module-variable (current-module) ',name)))
2665 ;; Now (re)define the var normally. Bernard URBAN
2666 ;; suggests we use eval here to accomodate Hobbit; it lets
2667 ;; the interpreter handle the define-private form, which
2668 ;; Hobbit can't digest.
2669 (eval '(define-private ,@ args)))))))
2673 (defmacro defmacro-public args
2675 (error "bad syntax" (list 'defmacro-public args)))
2676 (define (defined-name n)
2681 ((null? args) (syntax))
2683 (#t (let ((name (defined-name (car args))))
2685 (let ((public-i (module-public-interface (current-module))))
2686 ;; Make sure there is a local variable:
2688 (module-define! (current-module)
2690 (module-ref (current-module) ',name #f))
2692 ;; Make sure that local is exported:
2694 (module-add! public-i ',name (module-variable (current-module) ',name)))
2696 ;; Now (re)define the var normally.
2698 (defmacro ,@ args))))))
2703 (define load load-module)
2704 ;(define (load . args)
2705 ; (start-stack 'load-stack (apply load-module args)))
2709 ;;; {I/O functions for Tcl channels (disabled)}
2711 ;; (define in-ch (get-standard-channel TCL_STDIN))
2712 ;; (define out-ch (get-standard-channel TCL_STDOUT))
2713 ;; (define err-ch (get-standard-channel TCL_STDERR))
2715 ;; (define inp (%make-channel-port in-ch "r"))
2716 ;; (define outp (%make-channel-port out-ch "w"))
2717 ;; (define errp (%make-channel-port err-ch "w"))
2719 ;; (define %system-char-ready? char-ready?)
2721 ;; (define (char-ready? p)
2722 ;; (if (not (channel-port? p))
2723 ;; (%system-char-ready? p)
2724 ;; (let* ((channel (%channel-port-channel p))
2725 ;; (old-blocking (channel-option-ref channel :blocking)))
2727 ;; (lambda () (set-channel-option the-root-tcl-interpreter channel :blocking "0"))
2728 ;; (lambda () (not (eof-object? (peek-char p))))
2729 ;; (lambda () (set-channel-option the-root-tcl-interpreter channel :blocking old-blocking))))))
2731 ;; (define (top-repl)
2732 ;; (with-input-from-port inp
2734 ;; (with-output-to-port outp
2736 ;; (with-error-to-port errp
2738 ;; (scm-style-repl))))))))
2740 ;; (set-current-input-port inp)
2741 ;; (set-current-output-port outp)
2742 ;; (set-current-error-port errp)
2744 ;; this is just (scm-style-repl) with a wrapper to install and remove
2747 (let ((old-handlers #f)
2748 (signals `((,SIGINT . "User interrupt")
2749 (,SIGFPE . "Arithmetic error")
2750 (,SIGBUS . "Bad memory access (bus error)")
2751 (,SIGSEGV . "Bad memory access (Segmentation violation)"))))
2757 (let ((make-handler (lambda (msg)
2759 (save-stack %deliver-signals)
2766 (map (lambda (sig-msg)
2767 (sigaction (car sig-msg)
2768 (make-handler (cdr sig-msg))))
2771 ;; the protected thunk.
2774 ;; If we've got readline, use it to prompt the user. This is a
2775 ;; kludge, but we'll fix it soon. At least we only get
2776 ;; readline involved when we're actually running the repl.
2777 (if (memq 'readline *features*)
2779 (set-current-input-port (readline-port))
2782 (set-readline-prompt! prompt)
2788 (map (lambda (sig-msg old-handler)
2789 (if (not (car old-handler))
2790 ;; restore original C handler.
2791 (sigaction (car sig-msg) #f)
2792 ;; restore Scheme handler, SIG_IGN or SIG_DFL.
2793 (sigaction (car sig-msg)
2795 (cdr old-handler))))
2796 signals old-handlers)))))
2798 (defmacro false-if-exception (expr)
2799 `(catch #t (lambda () ,expr)
2802 ;;; {Load debug extension code if debug extensions present.}
2804 ;;; *fixme* This is a temporary solution.
2807 (if (memq 'debug-extensions *features*)
2808 (define-module (guile) :use-module (ice-9 debug)))
2811 ;;; {Load session support if present.}
2813 ;;; *fixme* This is a temporary solution.
2816 (if (%search-load-path "ice-9/session.scm")
2817 (define-module (guile) :use-module (ice-9 session)))
2820 ;;; {Load thread code if threads are present.}
2822 ;;; *fixme* This is a temporary solution.
2825 (if (memq 'threads *features*)
2826 (define-module (guile) :use-module (ice-9 threads)))
2829 ;;; {Load emacs interface support if emacs option is given.}
2831 ;;; *fixme* This is a temporary solution.
2834 (if (and (module-defined? the-root-module 'use-emacs-interface)
2835 use-emacs-interface)
2837 (if (memq 'debug-extensions *features*)
2838 (debug-enable 'backtrace))
2839 (define-module (guile) :use-module (ice-9 emacs))))
2842 ;;; {Load regexp code if regexp primitives are available.}
2844 (if (memq 'regex *features*)
2845 (define-module (guile) :use-module (ice-9 regex)))
2848 ;;; Load readline code if rreadline primitives are available.
2850 ;;; Ideally, we wouldn't do this until we were sure we were actually
2851 ;;; going to enter the repl, but autoloading individual functions is
2852 ;;; clumsy at the moment.
2853 (if (memq 'readline *features*)
2854 (define-module (guile) :use-module (ice-9 readline)))
2857 ;;; {Check that the interpreter and scheme code match up.}
2861 (with-output-to-port (current-error-port)
2863 (display (car (command-line)))
2865 (for-each (lambda (string) (display string))
2869 (load-from-path "ice-9/version.scm")
2872 (libguile-config-stamp) ; from the interprpreter
2873 (ice-9-config-stamp))) ; from the Scheme code
2875 (show-line "warning: different versions of libguile and ice-9:")
2876 (show-line "libguile: configured on " (libguile-config-stamp))
2877 (show-line "ice-9: configured on " (ice-9-config-stamp)))))
2881 (define-module (guile))
2883 (append! %load-path (cons "." ()))
2885 (define (inherit-print-state old-port new-port)
2886 (if (pair? old-port)
2887 (cons (if (pair? new-port) (car new-port) new-port)