;;; installed-scm-file
-;;;; Copyright (C) 1995, 1996, 1997, 1998, 1999 Free Software Foundation, Inc.
-;;;;
+;;;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
+;;;;
;;;; This program is free software; you can redistribute it and/or modify
;;;; it under the terms of the GNU General Public License as published by
;;;; the Free Software Foundation; either version 2, or (at your option)
;;;; any later version.
-;;;;
+;;;;
;;;; This program is distributed in the hope that it will be useful,
;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;;;; GNU General Public License for more details.
-;;;;
+;;;;
;;;; You should have received a copy of the GNU General Public License
;;;; along with this software; see the file COPYING. If not, write to
;;;; the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
;;;; Boston, MA 02111-1307 USA
-;;;;
+;;;;
+;;;; As a special exception, the Free Software Foundation gives permission
+;;;; for additional uses of the text contained in its release of GUILE.
+;;;;
+;;;; The exception is that, if you link the GUILE library with other files
+;;;; to produce an executable, this does not by itself cause the
+;;;; resulting executable to be covered by the GNU General Public License.
+;;;; Your use of that executable is in no way restricted on account of
+;;;; linking the GUILE library code into it.
+;;;;
+;;;; This exception does not however invalidate any other reasons why
+;;;; the executable file might be covered by the GNU General Public License.
+;;;;
+;;;; This exception applies only to the code released by the
+;;;; Free Software Foundation under the name GUILE. If you copy
+;;;; code from other Free Software Foundation releases into a copy of
+;;;; GUILE, as the General Public License permits, the exception does
+;;;; not apply to the code that you add in this way. To avoid misleading
+;;;; anyone as to the status of such modified files, you must delete
+;;;; this exception notice from them.
+;;;;
+;;;; If you write modifications of your own for GUILE, it is your choice
+;;;; whether to permit this exception to apply to your modifications.
+;;;; If you do not wish that, delete this exception notice.
+;;;;
\f
+;;; Commentary:
+
;;; This file is the first thing loaded into Guile. It adds many mundane
;;; definitions and a few that are interesting.
;;;
-;;; The module system (hence the hierarchical namespace) are defined in this
+;;; The module system (hence the hierarchical namespace) are defined in this
;;; file.
;;;
+;;; Code:
+
+\f
+;;; {Deprecation}
+;;;
+
+;; We don't have macros here, but we do want to define
+;; `begin-deprecated' early.
+
+(define begin-deprecated
+ (procedure->memoizing-macro
+ (lambda (exp env)
+ (if (include-deprecated-features)
+ `(begin ,@(cdr exp))
+ `#f))))
+
\f
;;; {Features}
;;
(define (provided? feature)
(and (memq feature *features*) #t))
+(begin-deprecated
+ (define feature? provided?))
+
+;;; let format alias simple-format until the more complete version is loaded
+(define format simple-format)
+
\f
;;; {R4RS compliance}
;;; {Trivial Functions}
;;;
-(define (id x) x)
+(define (identity x) x)
(define (1+ n) (+ n 1))
-(define (-1+ n) (+ n -1))
-(define 1- -1+)
-(define return-it noop)
+(define (1- n) (+ n -1))
(define (and=> value procedure) (and value (procedure value)))
(define (make-hash-table k) (make-vector k '()))
-;;; apply-to-args is functionally redunant with apply and, worse,
+;;; apply-to-args is functionally redundant with apply and, worse,
;;; is less general than apply since it only takes two arguments.
;;;
-;;; On the other hand, apply-to-args is a syntacticly convenient way to
+;;; On the other hand, apply-to-args is a syntacticly convenient way to
;;; perform binding in many circumstances when the "let" family of
;;; of forms don't cut it. E.g.:
;;;
;;; (apply-to-args (return-3d-mouse-coords)
-;;; (lambda (x y z)
+;;; (lambda (x y z)
;;; ...))
;;;
(define (apply-to-args args fn) (apply fn args))
\f
+
;;; {Integer Math}
;;;
(if (even? k) acc (proc acc x))
proc))))
-(define string-character-length string-length)
-
-
-
-;; A convenience function for combining flag bits. Like logior, but
-;; handles the cases of 0 and 1 arguments.
-;;
-(define (flags . args)
- (cond
- ((null? args) 0)
- ((null? (cdr args)) (car args))
- (else (apply logior args))))
-
\f
;;; {Symbol Properties}
;;;
(if pair
(symbol-pset! sym (delq! pair (symbol-pref sym))))))
-\f
+;;; {General Properties}
+;;;
-;;; {Line and Delimited I/O}
-
-;;; corresponds to SCM_LINE_INCREMENTORS in libguile.
-(define scm-line-incrementors "\n")
-
-(define (read-line! string . maybe-port)
- (let* ((port (if (pair? maybe-port)
- (car maybe-port)
- (current-input-port))))
- (let* ((rv (%read-delimited! scm-line-incrementors
- string
- #t
- port))
- (terminator (car rv))
- (nchars (cdr rv)))
- (cond ((and (= nchars 0)
- (eof-object? terminator))
- terminator)
- ((not terminator) #f)
- (else nchars)))))
-
-(define (read-delimited! delims buf . args)
- (let* ((num-args (length args))
- (port (if (> num-args 0)
- (car args)
- (current-input-port)))
- (handle-delim (if (> num-args 1)
- (cadr args)
- 'trim))
- (start (if (> num-args 2)
- (caddr args)
- 0))
- (end (if (> num-args 3)
- (cadddr args)
- (string-length buf))))
- (let* ((rv (%read-delimited! delims
- buf
- (not (eq? handle-delim 'peek))
- port
- start
- end))
- (terminator (car rv))
- (nchars (cdr rv)))
- (cond ((or (not terminator) ; buffer filled
- (eof-object? terminator))
- (if (zero? nchars)
- (if (eq? handle-delim 'split)
- (cons terminator terminator)
- terminator)
- (if (eq? handle-delim 'split)
- (cons nchars terminator)
- nchars)))
- (else
- (case handle-delim
- ((trim peek) nchars)
- ((concat) (string-set! buf (+ nchars start) terminator)
- (+ nchars 1))
- ((split) (cons nchars terminator))
- (else (error "unexpected handle-delim value: "
- handle-delim))))))))
-
-(define (read-delimited delims . args)
- (let* ((port (if (pair? args)
- (let ((pt (car args)))
- (set! args (cdr args))
- pt)
- (current-input-port)))
- (handle-delim (if (pair? args)
- (car args)
- 'trim)))
- (let loop ((substrings ())
- (total-chars 0)
- (buf-size 100)) ; doubled each time through.
- (let* ((buf (make-string buf-size))
- (rv (%read-delimited! delims
- buf
- (not (eq? handle-delim 'peek))
- port))
- (terminator (car rv))
- (nchars (cdr rv))
- (join-substrings
- (lambda ()
- (apply string-append
- (reverse
- (cons (if (and (eq? handle-delim 'concat)
- (not (eof-object? terminator)))
- (string terminator)
- "")
- (cons (make-shared-substring buf 0 nchars)
- substrings))))))
- (new-total (+ total-chars nchars)))
- (cond ((not terminator)
- ;; buffer filled.
- (loop (cons (substring buf 0 nchars) substrings)
- new-total
- (* buf-size 2)))
- ((eof-object? terminator)
- (if (zero? new-total)
- (if (eq? handle-delim 'split)
- (cons terminator terminator)
- terminator)
- (if (eq? handle-delim 'split)
- (cons (join-substrings) terminator)
- (join-substrings))))
- (else
- (case handle-delim
- ((trim peek concat) (join-substrings))
- ((split) (cons (join-substrings) terminator))
-
-
- (else (error "unexpected handle-delim value: "
- handle-delim)))))))))
-
-;;; read-line [PORT [HANDLE-DELIM]] reads a newline-terminated string
-;;; from PORT. The return value depends on the value of HANDLE-DELIM,
-;;; which may be one of the symbols `trim', `concat', `peek' and
-;;; `split'. If it is `trim' (the default), the trailing newline is
-;;; removed and the string is returned. If `concat', the string is
-;;; returned with the trailing newline intact. If `peek', the newline
-;;; is left in the input port buffer and the string is returned. If
-;;; `split', the newline is split from the string and read-line
-;;; returns a pair consisting of the truncated string and the newline.
-
-(define (read-line . args)
- (let* ((port (if (null? args)
- (current-input-port)
- (car args)))
- (handle-delim (if (> (length args) 1)
- (cadr args)
- 'trim))
- (line/delim (%read-line port))
- (line (car line/delim))
- (delim (cdr line/delim)))
- (case handle-delim
- ((trim) line)
- ((split) line/delim)
- ((concat) (if (and (string? line) (char? delim))
- (string-append line (string delim))
- line))
- ((peek) (if (char? delim)
- (unread-char delim port))
- line)
- (else
- (error "unexpected handle-delim value: " handle-delim)))))
+;; This is a more modern interface to properties. It will replace all
+;; other property-like things eventually.
+
+(define (make-object-property)
+ (let ((prop (primitive-make-property #f)))
+ (make-procedure-with-setter
+ (lambda (obj) (primitive-property-ref prop obj))
+ (lambda (obj val) (primitive-property-set! prop obj val)))))
\f
+
;;; {Arrays}
;;;
-(begin
- (define uniform-vector? array?)
- (define make-uniform-vector dimensions->uniform-array)
- ; (define uniform-vector-ref array-ref)
- (define (uniform-vector-set! u i o)
- (uniform-array-set1! u o i))
- (define uniform-vector-fill! array-fill!)
- (define uniform-vector-read! uniform-array-read!)
- (define uniform-vector-write uniform-array-write)
-
- (define (make-array fill . args)
- (dimensions->uniform-array args () fill))
- (define (make-uniform-array prot . args)
- (dimensions->uniform-array args prot))
- (define (list->array ndim lst)
- (list->uniform-array ndim '() lst))
- (define (list->uniform-vector prot lst)
- (list->uniform-array 1 prot lst))
- (define (array-shape a)
- (map (lambda (ind) (if (number? ind) (list 0 (+ -1 ind)) ind))
- (array-dimensions a))))
+(if (provided? 'array)
+ (primitive-load-path "ice-9/arrays.scm"))
\f
;;; {Keywords}
(make-keyword-from-dash-symbol (symbol-append '- symbol)))
(define (keyword->symbol kw)
- (let ((sym (keyword-dash-symbol kw)))
+ (let ((sym (symbol->string (keyword-dash-symbol kw))))
(string->symbol (substring sym 1 (string-length sym)))))
(define (kw-arg-ref args kw)
(struct-ref (struct-vtable s) vtable-index-layout))
\f
+
+;;; Environments
+
+(define the-environment
+ (procedure->syntax
+ (lambda (x e)
+ e)))
+
+(define the-root-environment (the-environment))
+
+(define (environment-module env)
+ (let ((closure (and (pair? env) (car (last-pair env)))))
+ (and closure (procedure-property closure 'module))))
+
+\f
;;; {Records}
;;;
new-port))
;; 0: type-name, 1: fields
-(define record-type-vtable
+(define record-type-vtable
(make-vtable-vtable "prpr" 0
(lambda (s p)
(cond ((eq? s record-type-vtable)
(let ((printer-fn (and (pair? opt) (car opt))))
(let ((struct (make-struct record-type-vtable 0
(make-struct-layout
- (apply symbol-append
+ (apply string-append
(map (lambda (f) "pw") fields)))
(or printer-fn
(lambda (s p)
(define (record-constructor rtd . opt)
(let ((field-names (if (pair? opt) (car opt) (record-type-fields rtd))))
- (eval `(lambda ,field-names
- (make-struct ',rtd 0 ,@(map (lambda (f)
- (if (memq f field-names)
- f
- #f))
- (record-type-fields rtd)))))))
+ (local-eval `(lambda ,field-names
+ (make-struct ',rtd 0 ,@(map (lambda (f)
+ (if (memq f field-names)
+ f
+ #f))
+ (record-type-fields rtd))))
+ the-root-environment)))
(define (record-predicate rtd)
(lambda (obj) (and (struct? obj) (eq? rtd (struct-vtable obj)))))
(let* ((pos (list-index (record-type-fields rtd) field-name)))
(if (not pos)
(error 'no-such-field field-name))
- (eval `(lambda (obj)
- (and (eq? ',rtd (record-type-descriptor obj))
- (struct-ref obj ,pos))))))
+ (local-eval `(lambda (obj)
+ (and (eq? ',rtd (record-type-descriptor obj))
+ (struct-ref obj ,pos)))
+ the-root-environment)))
(define (record-modifier rtd field-name)
(let* ((pos (list-index (record-type-fields rtd) field-name)))
(if (not pos)
(error 'no-such-field field-name))
- (eval `(lambda (obj val)
- (and (eq? ',rtd (record-type-descriptor obj))
- (struct-set! obj ,pos val))))))
+ (local-eval `(lambda (obj val)
+ (and (eq? ',rtd (record-type-descriptor obj))
+ (struct-set! obj ,pos val)))
+ the-root-environment)))
(define (record? obj)
;;;
(define (symbol-append . args)
- (string->symbol (apply string-append args)))
+ (string->symbol (apply string-append (map symbol->string args))))
(define (list->symbol . args)
(string->symbol (apply list->string args)))
(define (symbol . args)
(string->symbol (apply string args)))
-(define (obarray-symbol-append ob . args)
- (string->obarray-symbol (apply string-append ob args)))
-
-(define (obarray-gensym obarray . opt)
- (if (null? opt)
- (gensym "%%gensym" obarray)
- (gensym (car opt) obarray)))
-
\f
;;; {Lists}
;;;
answer
(loop (cons init answer) (- n 1)))))
-
-\f
-;;; {Multiple return values}
-
-(define *values-rtd*
- (make-record-type "values"
- '(values)))
-
-(define values
- (let ((make-values (record-constructor *values-rtd*)))
- (lambda x
- (if (and (not (null? x))
- (null? (cdr x)))
- (car x)
- (make-values x)))))
-
-(define call-with-values
- (let ((access-values (record-accessor *values-rtd* 'values))
- (values-predicate? (record-predicate *values-rtd*)))
- (lambda (producer consumer)
- (let ((result (producer)))
- (if (values-predicate? result)
- (apply consumer (access-values result))
- (consumer result))))))
-
-(provide 'values)
-
\f
;;; {and-map and or-map}
;;;
;;; (and-map fn lst) is like (and (fn (car lst)) (fn (cadr lst)) (fn...) ...)
;;; (or-map fn lst) is like (or (fn (car lst)) (fn (cadr lst)) (fn...) ...)
-;;; (map-in-order fn lst) is like (map fn lst) but definately in order of lst.
;;;
;; and-map f l
;; Apply f to successive elements of l until exhaustion or f returns #f.
;; If returning early, return #f. Otherwise, return the last value returned
;; by f. If f has never been called because l is empty, return #t.
-;;
+;;
(define (and-map f lst)
(let loop ((result #t)
(l lst))
(loop (f (car l)) (cdr l))))))
\f
-;;; {Files}
-;;;
-;;; If no one can explain this comment to me by 31 Jan 1998, I will
-;;; assume it is meaningless and remove it. -twp
-;;; !!!! these should be implemented using Tcl commands, not fports.
-(define (feature? feature)
- (and (memq feature *features*) #t))
+(if (provided? 'posix)
+ (primitive-load-path "ice-9/posix.scm"))
-;; Using the vector returned by stat directly is probably not a good
-;; idea (it could just as well be a record). Hence some accessors.
-(define (stat:dev f) (vector-ref f 0))
-(define (stat:ino f) (vector-ref f 1))
-(define (stat:mode f) (vector-ref f 2))
-(define (stat:nlink f) (vector-ref f 3))
-(define (stat:uid f) (vector-ref f 4))
-(define (stat:gid f) (vector-ref f 5))
-(define (stat:rdev f) (vector-ref f 6))
-(define (stat:size f) (vector-ref f 7))
-(define (stat:atime f) (vector-ref f 8))
-(define (stat:mtime f) (vector-ref f 9))
-(define (stat:ctime f) (vector-ref f 10))
-(define (stat:blksize f) (vector-ref f 11))
-(define (stat:blocks f) (vector-ref f 12))
-
-;; derived from stat mode.
-(define (stat:type f) (vector-ref f 13))
-(define (stat:perms f) (vector-ref f 14))
+(if (provided? 'socket)
+ (primitive-load-path "ice-9/networking.scm"))
(define file-exists?
- (if (feature? 'posix)
+ (if (provided? 'posix)
(lambda (str)
(access? str F_OK))
(lambda (str)
#f)))))
(define file-is-directory?
- (if (feature? 'i/o-extensions)
+ (if (provided? 'posix)
(lambda (str)
(eq? (stat:type (stat str)) 'directory))
(lambda (str)
- (display str)
- (newline)
(let ((port (catch 'system-error
(lambda () (open-file (string-append str "/.")
OPEN_READ))
(and (> sl sufl)
(string=? (substring str (- sl sufl) sl) suffix))))
+(define (system-error-errno args)
+ (if (eq? (car args) 'system-error)
+ (car (list-ref args 4))
+ #f))
+
\f
;;; {Error Handling}
;;;
(save-stack)
(if (null? args)
(scm-error 'misc-error #f "?" #f #f)
- (let loop ((msg "%s")
+ (let loop ((msg "~A")
(rest (cdr args)))
(if (not (null? rest))
- (loop (string-append msg " %S")
+ (loop (string-append msg " ~S")
(cdr rest))
(scm-error 'misc-error #f msg args #f)))))
(apply error "unhandled-exception:" key args))))
\f
-;;; {Non-polymorphic versions of POSIX functions}
-
-(define (getgrnam name) (getgr name))
-(define (getgrgid id) (getgr id))
-(define (gethostbyaddr addr) (gethost addr))
-(define (gethostbyname name) (gethost name))
-(define (getnetbyaddr addr) (getnet addr))
-(define (getnetbyname name) (getnet name))
-(define (getprotobyname name) (getproto name))
-(define (getprotobynumber addr) (getproto addr))
-(define (getpwnam name) (getpw name))
-(define (getpwuid uid) (getpw uid))
-(define (getservbyname name proto) (getserv name proto))
-(define (getservbyport port proto) (getserv port proto))
-(define (endgrent) (setgr))
-(define (endhostent) (sethost))
-(define (endnetent) (setnet))
-(define (endprotoent) (setproto))
-(define (endpwent) (setpw))
-(define (endservent) (setserv))
-(define (getgrent) (getgr))
-(define (gethostent) (gethost))
-(define (getnetent) (getnet))
-(define (getprotoent) (getproto))
-(define (getpwent) (getpw))
-(define (getservent) (getserv))
-(define (setgrent) (setgr #f))
-(define (sethostent) (sethost #t))
-(define (setnetent) (setnet #t))
-(define (setprotoent) (setproto #t))
-(define (setpwent) (setpw #t))
-(define (setservent) (setserv #t))
-
-(define (passwd:name obj) (vector-ref obj 0))
-(define (passwd:passwd obj) (vector-ref obj 1))
-(define (passwd:uid obj) (vector-ref obj 2))
-(define (passwd:gid obj) (vector-ref obj 3))
-(define (passwd:gecos obj) (vector-ref obj 4))
-(define (passwd:dir obj) (vector-ref obj 5))
-(define (passwd:shell obj) (vector-ref obj 6))
-
-(define (group:name obj) (vector-ref obj 0))
-(define (group:passwd obj) (vector-ref obj 1))
-(define (group:gid obj) (vector-ref obj 2))
-(define (group:mem obj) (vector-ref obj 3))
-
-(define (hostent:name obj) (vector-ref obj 0))
-(define (hostent:aliases obj) (vector-ref obj 1))
-(define (hostent:addrtype obj) (vector-ref obj 2))
-(define (hostent:length obj) (vector-ref obj 3))
-(define (hostent:addr-list obj) (vector-ref obj 4))
-
-(define (netent:name obj) (vector-ref obj 0))
-(define (netent:aliases obj) (vector-ref obj 1))
-(define (netent:addrtype obj) (vector-ref obj 2))
-(define (netent:net obj) (vector-ref obj 3))
-
-(define (protoent:name obj) (vector-ref obj 0))
-(define (protoent:aliases obj) (vector-ref obj 1))
-(define (protoent:proto obj) (vector-ref obj 2))
-
-(define (servent:name obj) (vector-ref obj 0))
-(define (servent:aliases obj) (vector-ref obj 1))
-(define (servent:port obj) (vector-ref obj 2))
-(define (servent:proto obj) (vector-ref obj 3))
-
-(define (sockaddr:fam obj) (vector-ref obj 0))
-(define (sockaddr:path obj) (vector-ref obj 1))
-(define (sockaddr:addr obj) (vector-ref obj 1))
-(define (sockaddr:port obj) (vector-ref obj 2))
-
-(define (utsname:sysname obj) (vector-ref obj 0))
-(define (utsname:nodename obj) (vector-ref obj 1))
-(define (utsname:release obj) (vector-ref obj 2))
-(define (utsname:version obj) (vector-ref obj 3))
-(define (utsname:machine obj) (vector-ref obj 4))
(define (tm:sec obj) (vector-ref obj 0))
(define (tm:min obj) (vector-ref obj 1))
(define (tms:cutime obj) (vector-ref obj 3))
(define (tms:cstime obj) (vector-ref obj 4))
-(define (file-position . args) (apply ftell args))
-(define (file-set-position . args) (apply fseek args))
+(define file-position ftell)
+(define (file-set-position port offset . whence)
+ (let ((whence (if (eq? whence '()) SEEK_SET (car whence))))
+ (seek port offset whence)))
(define (move->fdes fd/port fd)
(cond ((integer? fd/port)
(if (pair? maybe-fd)
(set-port-revealed! port 1))
port))
-
+
(define (dup->inport port/fd . maybe-fd)
(apply dup->port port/fd "r" maybe-fd))
(putenv (string-append name "=" value))
(putenv name)))
+(define (unsetenv name)
+ "Remove the entry for NAME from the environment."
+ (putenv name))
+
\f
;;; {Load Paths}
;;;
;; This is mostly for the internal use of the code generated by
;; scm_compile_shell_switches.
+
+(define (turn-on-debugging)
+ (debug-enable 'debug)
+ (debug-enable 'backtrace)
+ (read-enable 'positions))
+
(define (load-user-init)
- (define (has-init? dir)
- (let ((path (in-vicinity dir ".guile")))
- (catch 'system-error
- (lambda ()
- (let ((stats (stat path)))
- (if (not (eq? (stat:type stats) 'directory))
- path)))
- (lambda dummy #f))))
- (let ((path (or (has-init? (or (getenv "HOME") "/"))
- (has-init? (passwd:dir (getpw (getuid)))))))
- (if path (primitive-load path))))
+ (let* ((home (or (getenv "HOME")
+ (false-if-exception (passwd:dir (getpwuid (getuid))))
+ "/")) ;; fallback for cygwin etc.
+ (init-file (in-vicinity home ".guile")))
+ (if (file-exists? init-file)
+ (primitive-load init-file))))
\f
;;; {Loading by paths}
(define expt
(let ((integer-expt integer-expt))
(lambda (z1 z2)
- (cond ((exact? z2)
- (integer-expt z1 z2))
+ (cond ((integer? z2)
+ (if (>= z2 0)
+ (integer-expt z1 z2)
+ (/ 1 (integer-expt z1 (- z2)))))
((and (real? z2) (real? z1) (>= z1 0))
($expt z1 z2))
(else
(/ (log (/ (- +i z) (+ +i z))) +2i))
($atan2 z (car y))))
-(set! abs magnitude)
-
(define (log10 arg)
(/ (log arg) (log 10)))
(read-hash-extend #\' (lambda (c port)
(read port)))
-(read-hash-extend #\. (lambda (c port)
- (eval (read port))))
-
-(if (feature? 'array)
- (begin
- (let ((make-array-proc (lambda (template)
- (lambda (c port)
- (read:uniform-vector template port)))))
- (for-each (lambda (char template)
- (read-hash-extend char
- (make-array-proc template)))
- '(#\b #\a #\u #\e #\s #\i #\c #\y #\h)
- '(#t #\a 1 -1 1.0 1/3 0+i #\nul s)))
- (let ((array-proc (lambda (c port)
- (read:array c port))))
- (for-each (lambda (char) (read-hash-extend char array-proc))
- '(#\0 #\1 #\2 #\3 #\4 #\5 #\6 #\7 #\8 #\9)))))
-
-(define (read:array digit port)
- (define chr0 (char->integer #\0))
- (let ((rank (let readnum ((val (- (char->integer digit) chr0)))
- (if (char-numeric? (peek-char port))
- (readnum (+ (* 10 val)
- (- (char->integer (read-char port)) chr0)))
- val)))
- (prot (if (eq? #\( (peek-char port))
- '()
- (let ((c (read-char port)))
- (case c ((#\b) #t)
- ((#\a) #\a)
- ((#\u) 1)
- ((#\e) -1)
- ((#\s) 1.0)
- ((#\i) 1/3)
- ((#\c) 0+i)
- (else (error "read:array unknown option " c)))))))
- (if (eq? (peek-char port) #\()
- (list->uniform-array rank prot (read port))
- (error "read:array list not found"))))
-
-(define (read:uniform-vector proto port)
- (if (eq? #\( (peek-char port))
- (list->uniform-array 1 proto (read port))
- (error "read:uniform-vector list not found")))
+
+(define read-eval? (make-fluid))
+(fluid-set! read-eval? #f)
+(read-hash-extend #\.
+ (lambda (c port)
+ (if (fluid-ref read-eval?)
+ (eval (read port) (interaction-environment))
+ (error
+ "#. read expansion found and read-eval? is #f."))))
\f
;;; {Command Line Options}
(display help)
(newline))))
kw-desc))
-
-
+
+
(define (transform-usage-lambda cases)
(let* ((raw-usage (delq! 'else (map car cases)))
(usage-sans-specials (map (lambda (x)
;; is a (CLOSURE module symbol) which, as a last resort, can provide
;; bindings that would otherwise not be found locally in the module.
;;
+;; NOTE: If you change here, you also need to change libguile/modules.h.
+;;
(define module-type
(make-record-type 'module
'(obarray uses binder eval-closure transformer name kind
;; We can't pass this as an argument to module-constructor,
;; because we need it to close over a pointer to the module
;; itself.
- (set-module-eval-closure! module
- (lambda (symbol define?)
- (if define?
- (module-make-local-var! module symbol)
- (module-variable module symbol))))
+ (set-module-eval-closure! module (standard-eval-closure module))
module))))
;; to maximally one module.
(set-procedure-property! closure 'module module))))
-(define (eval-in-module exp module)
- (eval2 exp (module-eval-closure module)))
-
\f
;;; {Observer protocol}
;;;
;;;
;; module-search fn m
-;;
+;;
;; return the first non-#f result of FN applied to M and then to
;; the modules in the uses of m, and so on recursively. If all applications
;; return #f, then so does this function.
;;; {Is a symbol interned in a module?}
;;;
-;;; Symbol S in Module M is interned if S occurs in
+;;; Symbol S in Module M is interned if S occurs in
;;; of S in M has been set to some well-defined value.
;;;
;;; It is possible to intern a symbol in a module without providing
((if (symbol? key) hashq-remove! hash-remove!) ob key))
;; module-symbol-locally-interned? module symbol
-;;
+;;
;; is a symbol interned (not neccessarily defined) locally in a given module
;; or its uses? Interned symbols shadow inherited bindings even if
;; they are not themselves bound to a defined value.
(not (not (module-obarray-get-handle (module-obarray m) v))))
;; module-symbol-interned? module symbol
-;;
+;;
;; is a symbol interned (not neccessarily defined) anywhere in a given module
;; or its uses? Interned symbols shadow inherited bindings even if
;; they are not themselves bound to a defined value.
;))
;; module-variable module symbol
-;;
-;; like module-local-variable, except search the uses in the
+;;
+;; like module-local-variable, except search the uses in the
;; case V is not found in M.
;;
+;; NOTE: This function is superseded with C code (see modules.c)
+;;; when using the standard eval closure.
+;;
(define (module-variable m v)
(module-search module-local-variable m v))
;;;
;; module-symbol-binding module symbol opt-value
-;;
+;;
;; return the binding of a variable specified by name within
;; a given module, signalling an error if the variable is unbound.
;; If the OPT-VALUE is passed, then instead of signalling an error,
(error "Locally unbound variable." v)))))
;; module-symbol-binding module symbol opt-value
-;;
+;;
;; return the binding of a variable specified by name within
;; a given module, signalling an error if the variable is unbound.
;; If the OPT-VALUE is passed, then instead of signalling an error,
;; module-make-local-var! module symbol
-;;
+;;
;; ensure a variable for V in the local namespace of M.
;; If no variable was already there, then create a new and uninitialzied
;; variable.
(and (module-binder m)
((module-binder m) m v #t))
(begin
- (let ((answer (make-undefined-variable v)))
+ (let ((answer (make-undefined-variable)))
(module-obarray-set! (module-obarray m) v answer)
(module-modified m)
answer))))
+;; module-ensure-local-variable! module symbol
+;;
+;; Ensure that there is a local variable in MODULE for SYMBOL. If
+;; there is no binding for SYMBOL, create a new uninitialized
+;; variable. Return the local variable.
+;;
+(define (module-ensure-local-variable! module symbol)
+ (or (module-local-variable module symbol)
+ (let ((var (make-undefined-variable)))
+ (module-add! module symbol var)
+ var)))
+
;; module-add! module symbol var
-;;
+;;
;; ensure a particular variable for V in the local namespace of M.
;;
(define (module-add! m v var)
(module-obarray-set! (module-obarray m) v var)
(module-modified m))
-;; module-remove!
-;;
+;; module-remove!
+;;
;; make sure that a symbol is undefined in the local namespace of M.
;;
(define (module-remove! m v)
(module-modified m))
;; MODULE-FOR-EACH -- exported
-;;
+;;
;; Call PROC on each symbol in MODULE, with arguments of (SYMBOL VARIABLE).
;;
(define (module-for-each proc module)
;;; {Low Level Bootstrapping}
;;;
-;; make-root-module
+;; make-root-module
-;; A root module uses the symhash table (the system's privileged
-;; obarray). Being inside a root module is like using SCM without
-;; any module system.
+;; A root module uses the pre-modules-obarray as its obarray. This
+;; special obarray accumulates all bindings that have been established
+;; before the module system is fully booted.
;;
-
-
-(define (root-module-closure m s define?)
- (let ((bi (and (symbol-interned? #f s)
- (builtin-variable s))))
- (and bi
- (or define? (variable-bound? bi))
- (begin
- (module-add! m s bi)
- bi))))
+;; (The obarray continues to be used by code that has been closed over
+;; before the module system has been booted.)
(define (make-root-module)
- (make-module 1019 '() root-module-closure))
+ (let ((m (make-module 0)))
+ (set-module-obarray! m (%get-pre-modules-obarray))
+ m))
+;; make-scm-module
-;; make-scm-module
-
-;; An scm module is a module into which the lazy binder copies
-;; variable bindings from the system symhash table. The mapping is
-;; one way only; newly introduced bindings in an scm module are not
-;; copied back into the system symhash table (and can be used to override
-;; bindings from the symhash table).
-;;
+;; The root interface is a module that uses the same obarray as the
+;; root module. It does not allow new definitions, tho.
(define (make-scm-module)
- (make-module 1019 '()
- (lambda (m s define?)
- (let ((bi (and (symbol-interned? #f s)
- (builtin-variable s))))
- (and bi
- (variable-bound? bi)
- (begin
- (module-add! m s bi)
- bi))))))
-
-
+ (let ((m (make-module 0)))
+ (set-module-obarray! m (%get-pre-modules-obarray))
+ (set-module-eval-closure! m (standard-interface-eval-closure m))
+ m))
-;; the-module
-;;
-;; NOTE: This binding is used in libguile/modules.c.
-;;
-(define the-module #f)
-
-;; scm:eval-transformer
-;;
-(define scm:eval-transformer #f)
-
-;; set-current-module module
-;;
-;; set the current module as viewed by the normalizer.
-;;
-;; NOTE: This binding is used in libguile/modules.c.
-;;
-(define (set-current-module m)
- (set! the-module m)
- (if m
- (begin
- (set! *top-level-lookup-closure* (module-eval-closure the-module))
- (set! scm:eval-transformer (module-transformer the-module)))
- (set! *top-level-lookup-closure* #f)))
-
-
-;; current-module
-;;
-;; return the current module as viewed by the normalizer.
-;;
-(define (current-module) the-module)
\f
;;; {Module-based Loading}
;;;
;; Returns the value of a variable called NAME in MODULE or any of its
;; used modules. If there is no such variable, then if the optional third
;; argument DEFAULT is present, it is returned; otherwise an error is signaled.
-;;
+;;
(define (module-ref module name . rest)
(let ((variable (module-variable module name)))
(if (and variable (variable-bound? variable))
;;
;; Sets the variable called NAME in MODULE (or in a module that MODULE uses)
;; to VALUE; if there is no such variable, an error is signaled.
-;;
+;;
(define (module-set! module name value)
(let ((variable (module-variable module name)))
(if variable
;;
;; Sets the variable called NAME in MODULE to VALUE; if there is no such
;; variable, it is added first.
-;;
+;;
(define (module-define! module name value)
(let ((variable (module-local-variable module name)))
(if variable
(begin
(variable-set! variable value)
(module-modified module))
- (module-add! module name (make-variable value name)))))
+ (let ((variable (make-variable value)))
+ (module-add! module name variable)))))
;; MODULE-DEFINED? -- exported
;;
;; MODULE-USE! module interface
;;
;; Add INTERFACE to the list of interfaces used by MODULE.
-;;
+;;
(define (module-use! module interface)
(set-module-uses! module
(cons interface (delq! interface (module-uses module))))
;;; modules.
;;;
;;; (nested-ref some-root-module '(foo bar baz))
-;;; => <value of a variable named baz in the module bound to bar in
+;;; => <value of a variable named baz in the module bound to bar in
;;; the module bound to foo in some-root-module>
;;;
;;;
(define the-root-module (make-root-module))
(define the-scm-module (make-scm-module))
(set-module-public-interface! the-root-module the-scm-module)
-(set-module-name! the-root-module 'the-root-module)
-(set-module-name! the-scm-module 'the-scm-module)
+(set-module-name! the-root-module '(guile))
+(set-module-name! the-scm-module '(guile))
+(set-module-kind! the-scm-module 'interface)
(for-each set-system-module! (list the-root-module the-scm-module) '(#t #t))
-(set-current-module the-root-module)
-
-(define app (make-module 31))
-(local-define '(app modules) (make-module 31))
-(local-define '(app modules guile) the-root-module)
-
-;; (define-special-value '(app modules new-ws) (lambda () (make-scm-module)))
+;; NOTE: This binding is used in libguile/modules.c.
+;;
+(define (make-modules-in module name)
+ (if (null? name)
+ module
+ (cond
+ ((module-ref module (car name) #f)
+ => (lambda (m) (make-modules-in m (cdr name))))
+ (else (let ((m (make-module 31)))
+ (set-module-kind! m 'directory)
+ (set-module-name! m (append (or (module-name module)
+ '())
+ (list (car name))))
+ (module-define! module (car name) m)
+ (make-modules-in m (cdr name)))))))
-(define (try-load-module name)
- (or (try-module-linked name)
- (try-module-autoload name)
- (try-module-dynamic-link name)))
+(define (beautify-user-module! module)
+ (let ((interface (module-public-interface module)))
+ (if (or (not interface)
+ (eq? interface module))
+ (let ((interface (make-module 31)))
+ (set-module-name! interface (module-name module))
+ (set-module-kind! interface 'interface)
+ (set-module-public-interface! module interface))))
+ (if (and (not (memq the-scm-module (module-uses module)))
+ (not (eq? module the-root-module)))
+ (set-module-uses! module (append (module-uses module) (list the-scm-module)))))
;; NOTE: This binding is used in libguile/modules.c.
;;
(if already
;; The module already exists...
(if (and (or (null? maybe-autoload) (car maybe-autoload))
- (not (module-ref already '%module-public-interface #f)))
+ (not (module-public-interface already)))
;; ...but we are told to load and it doesn't contain source, so
(begin
(try-load-module name)
(try-load-module name))
;; Get/create it.
(make-modules-in (current-module) full-name))))))
-
-(define (beautify-user-module! module)
- (let ((interface (module-public-interface module)))
- (if (or (not interface)
- (eq? interface module))
- (let ((interface (make-module 31)))
- (set-module-name! interface (module-name module))
- (set-module-kind! interface 'interface)
- (set-module-public-interface! module interface))))
- (if (and (not (memq the-scm-module (module-uses module)))
- (not (eq? module the-root-module)))
- (set-module-uses! module (append (module-uses module) (list the-scm-module)))))
-;; NOTE: This binding is used in libguile/modules.c.
+;; Cheat. These bindings are needed by modules.c, but we don't want
+;; to move their real definition here because that would be unnatural.
;;
-(define (make-modules-in module name)
- (if (null? name)
- module
- (cond
- ((module-ref module (car name) #f)
- => (lambda (m) (make-modules-in m (cdr name))))
- (else (let ((m (make-module 31)))
- (set-module-kind! m 'directory)
- (set-module-name! m (car name))
- (module-define! module (car name) m)
- (make-modules-in m (cdr name)))))))
+(define try-module-autoload #f)
+(define process-define-module #f)
+(define process-use-modules #f)
+(define module-export! #f)
-(define (resolve-interface name)
- (let ((module (resolve-module name)))
- (and module (module-public-interface module))))
+;; This boots the module system. All bindings needed by modules.c
+;; must have been defined by now.
+;;
+(set-current-module the-root-module)
+(define app (make-module 31))
+(local-define '(app modules) (make-module 31))
+(local-define '(app modules guile) the-root-module)
+
+;; (define-special-value '(app modules new-ws) (lambda () (make-scm-module)))
+
+(define (try-load-module name)
+ (try-module-autoload name))
+
+(define (purify-module! module)
+ "Removes bindings in MODULE which are inherited from the (guile) module."
+ (let ((use-list (module-uses module)))
+ (if (and (pair? use-list)
+ (eq? (car (last-pair use-list)) the-scm-module))
+ (set-module-uses! module (reverse (cdr (reverse use-list)))))))
+
+;; Return a module that is an interface to the module designated by
+;; NAME.
+;;
+;; `resolve-interface' takes two keyword arguments:
+;;
+;; #:select SELECTION
+;;
+;; SELECTION is a list of binding-specs to be imported; A binding-spec
+;; is either a symbol or a pair of symbols (ORIG . SEEN), where ORIG
+;; is the name in the used module and SEEN is the name in the using
+;; module. Note that SEEN is also passed through RENAMER, below. The
+;; default is to select all bindings. If you specify no selection but
+;; a renamer, only the bindings that already exist in the used module
+;; are made available in the interface. Bindings that are added later
+;; are not picked up.
+;;
+;; #:renamer RENAMER
+;;
+;; RENAMER is a procedure that takes a symbol and returns its new
+;; name. The default is to not perform any renaming.
+;;
+;; Signal "no code for module" error if module name is not resolvable
+;; or its public interface is not available. Signal "no binding"
+;; error if selected binding does not exist in the used module.
+;;
+(define (resolve-interface name . args)
-(define %autoloader-developer-mode #t)
+ (define (get-keyword-arg args kw def)
+ (cond ((memq kw args)
+ => (lambda (kw-arg)
+ (if (null? (cdr kw-arg))
+ (error "keyword without value: " kw))
+ (cadr kw-arg)))
+ (else
+ def)))
+
+ (let* ((select (get-keyword-arg args #:select #f))
+ (renamer (get-keyword-arg args #:renamer identity))
+ (module (resolve-module name))
+ (public-i (and module (module-public-interface module))))
+ (and (or (not module) (not public-i))
+ (error "no code for module" name))
+ (if (and (not select) (eq? renamer identity))
+ public-i
+ (let ((selection (or select (module-map (lambda (sym var) sym)
+ public-i)))
+ (custom-i (make-module 31)))
+ (set-module-kind! custom-i 'interface)
+ ;; XXX - should use a lazy binder so that changes to the
+ ;; used module are picked up automatically.
+ (for-each (lambda (bspec)
+ (let* ((direct? (symbol? bspec))
+ (orig (if direct? bspec (car bspec)))
+ (seen (if direct? bspec (cdr bspec))))
+ (module-add! custom-i (renamer seen)
+ (or (module-local-variable public-i orig)
+ (module-local-variable module orig)
+ (error
+ ;; fixme: format manually for now
+ (simple-format
+ #f "no binding `~A' in module ~A"
+ orig name))))))
+ selection)
+ custom-i))))
+
+(define (symbol-prefix-proc prefix)
+ (lambda (symbol)
+ (symbol-append prefix symbol)))
+
+;; This function is called from "modules.c". If you change it, be
+;; sure to update "modules.c" as well.
(define (process-define-module args)
- (let* ((module-id (car args))
- (module (resolve-module module-id #f))
- (kws (cdr args)))
+ (let* ((module-id (car args))
+ (module (resolve-module module-id #f))
+ (kws (cdr args))
+ (unrecognized (lambda (arg)
+ (error "unrecognized define-module argument" arg))))
(beautify-user-module! module)
(let loop ((kws kws)
- (reversed-interfaces '()))
+ (reversed-interfaces '())
+ (exports '())
+ (re-exports '()))
(if (null? kws)
- (for-each (lambda (interface)
- (module-use! module interface))
- reversed-interfaces)
- (let ((keyword (cond ((keyword? (car kws))
- (keyword->symbol (car kws)))
- ((and (symbol? (car kws))
- (eq? (string-ref (car kws) 0) #\:))
- (string->symbol (substring (car kws) 1)))
- (else #f))))
- (case keyword
- ((use-module use-syntax)
- (if (not (pair? (cdr kws)))
- (error "unrecognized defmodule argument" kws))
- (let* ((used-name (cadr kws))
- (used-module (resolve-module used-name)))
- (if (not (module-ref used-module
- '%module-public-interface
- #f))
- (begin
- ((if %autoloader-developer-mode warn error)
- "no code for module" (module-name used-module))
- (beautify-user-module! used-module)))
- (let ((interface (module-public-interface used-module)))
- (if (not interface)
- (error "missing interface for use-module"
- used-module))
- (if (eq? keyword 'use-syntax)
- (set-module-transformer!
- module
- (module-ref interface (car (last-pair used-name))
- #f)))
- (loop (cddr kws)
- (cons interface reversed-interfaces)))))
- ((autoload)
- (if (not (and (pair? (cdr kws)) (pair? (cddr kws))))
- (error "unrecognized defmodule argument" kws))
- (loop (cdddr kws)
- (cons (make-autoload-interface module
- (cadr kws)
- (caddr kws))
- reversed-interfaces)))
- ((no-backtrace)
- (set-system-module! module #t)
- (loop (cdr kws) reversed-interfaces))
- (else
- (error "unrecognized defmodule argument" kws))))))
+ (begin
+ (for-each (lambda (interface)
+ (module-use! module interface))
+ (reverse reversed-interfaces))
+ (module-export! module exports)
+ (module-re-export! module re-exports))
+ (case (car kws)
+ ((#:use-module #:use-syntax)
+ (or (pair? (cdr kws))
+ (unrecognized kws))
+ (let* ((interface-args (cadr kws))
+ (interface (apply resolve-interface interface-args)))
+ (and (eq? (car kws) #:use-syntax)
+ (or (symbol? (caar interface-args))
+ (error "invalid module name for use-syntax"
+ (car interface-args)))
+ (set-module-transformer!
+ module
+ (module-ref interface
+ (car (last-pair (car interface-args)))
+ #f)))
+ (loop (cddr kws)
+ (cons interface reversed-interfaces)
+ exports
+ re-exports)))
+ ((#:autoload)
+ (or (and (pair? (cdr kws)) (pair? (cddr kws)))
+ (unrecognized kws))
+ (loop (cdddr kws)
+ (cons (make-autoload-interface module
+ (cadr kws)
+ (caddr kws))
+ reversed-interfaces)
+ exports
+ re-exports))
+ ((#:no-backtrace)
+ (set-system-module! module #t)
+ (loop (cdr kws) reversed-interfaces exports re-exports))
+ ((#:pure)
+ (purify-module! module)
+ (loop (cdr kws) reversed-interfaces exports re-exports))
+ ((#:export #:export-syntax)
+ (or (pair? (cdr kws))
+ (unrecognized kws))
+ (loop (cddr kws)
+ reversed-interfaces
+ (append (cadr kws) exports)
+ re-exports))
+ ((#:re-export #:re-export-syntax)
+ (or (pair? (cdr kws))
+ (unrecognized kws))
+ (loop (cddr kws)
+ reversed-interfaces
+ exports
+ (append (cadr kws) re-exports)))
+ (else
+ (unrecognized kws)))))
module))
;;; {Autoload}
;; Replace autoload-interface with interface
(set-car! (memq a (module-uses module)) i)
(module-local-variable i sym))))))
- (module-constructor #() #f b #f #f name 'autoload)))
+ (module-constructor #() '() b #f #f name 'autoload
+ '() (make-weak-value-hash-table 31) 0)))
+
+;;; {Compiled module}
+
+(define load-compiled #f)
\f
;;; {Autoloading modules}
(define autoloads-in-progress '())
-(define (try-module-autoload module-name)
+;; This function is called from "modules.c". If you change it, be
+;; sure to update "modules.c" as well.
- (define (sfx name) (string-append name (scheme-file-suffix)))
+(define (try-module-autoload module-name)
(let* ((reverse-name (reverse module-name))
- (name (car reverse-name))
+ (name (symbol->string (car reverse-name)))
(dir-hint-module-name (reverse (cdr reverse-name)))
- (dir-hint (apply symbol-append (map (lambda (elt) (symbol-append elt "/")) dir-hint-module-name))))
+ (dir-hint (apply string-append
+ (map (lambda (elt)
+ (string-append (symbol->string elt) "/"))
+ dir-hint-module-name))))
(resolve-module dir-hint-module-name #f)
(and (not (autoload-done-or-in-progress? dir-hint name))
(let ((didit #f))
+ (define (load-file proc file)
+ (save-module-excursion (lambda () (proc file)))
+ (set! didit #t))
(dynamic-wind
(lambda () (autoload-in-progress! dir-hint name))
(lambda ()
- (let ((full (%search-load-path (in-vicinity dir-hint name))))
- (if full
- (begin
- (save-module-excursion (lambda () (primitive-load full)))
- (set! didit #t)))))
+ (let ((file (in-vicinity dir-hint name)))
+ (cond ((and load-compiled
+ (%search-load-path (string-append file ".go")))
+ => (lambda (full)
+ (load-file load-compiled full)))
+ ((%search-load-path file)
+ => (lambda (full)
+ (load-file primitive-load full))))))
(lambda () (set-autoloaded! dir-hint name didit)))
didit))))
\f
;;; Dynamic linking of modules
-;; Initializing a module that is written in C is a two step process.
-;; First the module's `module init' function is called. This function
-;; is expected to call `scm_register_module_xxx' to register the `real
-;; init' function. Later, when the module is referenced for the first
-;; time, this real init function is called in the right context. See
-;; gtcltk-lib/gtcltk-module.c for an example.
-;;
-;; The code for the module can be in a regular shared library (so that
-;; the `module init' function will be called when libguile is
-;; initialized). Or it can be dynamically linked.
-;;
-;; You can safely call `scm_register_module_xxx' before libguile
-;; itself is initialized. You could call it from an C++ constructor
-;; of a static object, for example.
-;;
-;; To make your Guile extension into a dynamic linkable module, follow
-;; these easy steps:
-;;
-;; - Find a name for your module, like (ice-9 gtcltk)
-;; - Write a function with a name like
-;;
-;; scm_init_ice_9_gtcltk_module
-;;
-;; This is your `module init' function. It should call
-;;
-;; scm_register_module_xxx ("ice-9 gtcltk", scm_init_gtcltk);
-;;
-;; "ice-9 gtcltk" is the C version of the module name. Slashes are
-;; replaced by spaces, the rest is untouched. `scm_init_gtcltk' is
-;; the real init function that executes the usual initializations
-;; like making new smobs, etc.
-;;
-;; - Make a shared library with your code and a name like
-;;
-;; ice-9/libgtcltk.so
-;;
-;; and put it somewhere in %load-path.
-;;
-;; - Then you can simply write `:use-module (ice-9 gtcltk)' and it
-;; will be linked automatically.
-;;
-;; This is all very experimental.
-
-(define (split-c-module-name str)
- (let loop ((rev '())
- (start 0)
- (pos 0)
- (end (string-length str)))
- (cond
- ((= pos end)
- (reverse (cons (string->symbol (substring str start pos)) rev)))
- ((eq? (string-ref str pos) #\space)
- (loop (cons (string->symbol (substring str start pos)) rev)
- (+ pos 1)
- (+ pos 1)
- end))
- (else
- (loop rev start (+ pos 1) end)))))
-
-(define (convert-c-registered-modules dynobj)
- (let ((res (map (lambda (c)
- (list (split-c-module-name (car c)) (cdr c) dynobj))
- (c-registered-modules))))
- (c-clear-registered-modules)
- res))
-
-(define registered-modules '())
-
-(define (register-modules dynobj)
- (set! registered-modules
- (append! (convert-c-registered-modules dynobj)
- registered-modules)))
-
-(define (init-dynamic-module modname)
- ;; Register any linked modules which has been registered on the C level
- (register-modules #f)
- (or-map (lambda (modinfo)
- (if (equal? (car modinfo) modname)
- (begin
- (set! registered-modules (delq! modinfo registered-modules))
- (let ((mod (resolve-module modname #f)))
- (save-module-excursion
- (lambda ()
- (set-current-module mod)
- (set-module-public-interface! mod mod)
- (dynamic-call (cadr modinfo) (caddr modinfo))
- ))
- #t))
- #f))
- registered-modules))
-
-(define (dynamic-maybe-call name dynobj)
- (catch #t ; could use false-if-exception here
- (lambda ()
- (dynamic-call name dynobj))
- (lambda args
- #f)))
-
-(define (dynamic-maybe-link filename)
- (catch #t ; could use false-if-exception here
- (lambda ()
- (dynamic-link filename))
- (lambda args
- #f)))
-
-(define (find-and-link-dynamic-module module-name)
- (define (make-init-name mod-name)
- (string-append 'scm_init
- (list->string (map (lambda (c)
- (if (or (char-alphabetic? c)
- (char-numeric? c))
- c
- #\_))
- (string->list mod-name)))
- '_module))
-
- ;; Put the subdirectory for this module in the car of SUBDIR-AND-LIBNAME,
- ;; and the `libname' (the name of the module prepended by `lib') in the cdr
- ;; field. For example, if MODULE-NAME is the list (inet tcp-ip udp), then
- ;; SUBDIR-AND-LIBNAME will be the pair ("inet/tcp-ip" . "libudp").
- (let ((subdir-and-libname
- (let loop ((dirs "")
- (syms module-name))
- (if (null? (cdr syms))
- (cons dirs (string-append "lib" (car syms)))
- (loop (string-append dirs (car syms) "/") (cdr syms)))))
- (init (make-init-name (apply string-append
- (map (lambda (s)
- (string-append "_" s))
- module-name)))))
- (let ((subdir (car subdir-and-libname))
- (libname (cdr subdir-and-libname)))
-
- ;; Now look in each dir in %LOAD-PATH for `subdir/libfoo.la'. If that
- ;; file exists, fetch the dlname from that file and attempt to link
- ;; against it. If `subdir/libfoo.la' does not exist, or does not seem
- ;; to name any shared library, look for `subdir/libfoo.so' instead and
- ;; link against that.
- (let check-dirs ((dir-list %load-path))
- (if (null? dir-list)
- #f
- (let* ((dir (in-vicinity (car dir-list) subdir))
- (sharlib-full
- (or (try-using-libtool-name dir libname)
- (try-using-sharlib-name dir libname))))
- (if (and sharlib-full (file-exists? sharlib-full))
- (link-dynamic-module sharlib-full init)
- (check-dirs (cdr dir-list)))))))))
-
-(define (try-using-libtool-name libdir libname)
- (let ((libtool-filename (in-vicinity libdir
- (string-append libname ".la"))))
- (and (file-exists? libtool-filename)
- (with-input-from-file libtool-filename
- (lambda ()
- (let loop ((ln (read-line)))
- (cond ((eof-object? ln) #f)
- ((and (> (string-length ln) 9)
- (string=? "dlname='" (substring ln 0 8))
- (string-index ln #\' 8))
- =>
- (lambda (end)
- (in-vicinity libdir (substring ln 8 end))))
- (else (loop (read-line))))))))))
-
-(define (try-using-sharlib-name libdir libname)
- (in-vicinity libdir (string-append libname ".so")))
-
-(define (link-dynamic-module filename initname)
- ;; Register any linked modules which has been registered on the C level
- (register-modules #f)
- (let ((dynobj (dynamic-link filename)))
- (dynamic-call initname dynobj)
- (register-modules dynobj)))
-
-(define (try-module-linked module-name)
- (init-dynamic-module module-name))
-
-(define (try-module-dynamic-link module-name)
- (and (find-and-link-dynamic-module module-name)
- (init-dynamic-module module-name)))
-
-
-
(define autoloads-done '((guile . guile)))
(define (autoload-done-or-in-progress? p m)
+\f
+;; {EVAL-CASE}
+;;
+;; (eval-case ((situation*) forms)* (else forms)?)
+;;
+;; Evaluate certain code based on the situation that eval-case is used
+;; in. The only defined situation right now is `load-toplevel' which
+;; triggers for code evaluated at the top-level, for example from the
+;; REPL or when loading a file.
+
+(define eval-case
+ (procedure->memoizing-macro
+ (lambda (exp env)
+ (define (toplevel-env? env)
+ (or (not (pair? env)) (not (pair? (car env)))))
+ (define (syntax)
+ (error "syntax error in eval-case"))
+ (let loop ((clauses (cdr exp)))
+ (cond
+ ((null? clauses)
+ #f)
+ ((not (list? (car clauses)))
+ (syntax))
+ ((eq? 'else (caar clauses))
+ (or (null? (cdr clauses))
+ (syntax))
+ (cons 'begin (cdar clauses)))
+ ((not (list? (caar clauses)))
+ (syntax))
+ ((and (toplevel-env? env)
+ (memq 'load-toplevel (caar clauses)))
+ (cons 'begin (cdar clauses)))
+ (else
+ (loop (cdr clauses))))))))
\f
;;; {Macros}
(let ((defmacro-transformer
(lambda (name parms . body)
(let ((transformer `(lambda ,parms ,@body)))
- `(define ,name
- (,(lambda (transformer)
- (defmacro:transformer transformer))
- ,transformer))))))
+ `(eval-case
+ ((load-toplevel)
+ (define ,name (defmacro:transformer ,transformer)))
+ (else
+ (error "defmacro can only be used at the top level")))))))
(defmacro:transformer defmacro-transformer)))
(define defmacro:syntax-transformer
e)))
(#t e)))
-(define (gentemp)
- (gensym "scm:G"))
-
(provide 'defmacro)
\f
;;; {Run-time options}
-((let* ((names '((eval-options-interface
- (eval-options eval-enable eval-disable)
- (eval-set!))
-
- (debug-options-interface
- (debug-options debug-enable debug-disable)
- (debug-set!))
-
- (evaluator-traps-interface
- (traps trap-enable trap-disable)
- (trap-set!))
-
- (read-options-interface
- (read-options read-enable read-disable)
- (read-set!))
-
- (print-options-interface
- (print-options print-enable print-disable)
- (print-set!))
-
- (readline-options-interface
- (readline-options readline-enable readline-disable)
- (readline-set!))
- ))
- (option-name car)
- (option-value cadr)
- (option-documentation caddr)
-
- (print-option (lambda (option)
- (display (option-name option))
- (if (< (string-length
- (symbol->string (option-name option)))
- 8)
- (display #\tab))
- (display #\tab)
- (display (option-value option))
- (display #\tab)
- (display (option-documentation option))
- (newline)))
-
- ;; Below follows the macros defining the run-time option interfaces.
-
- (make-options (lambda (interface)
- `(lambda args
- (cond ((null? args) (,interface))
- ((list? (car args))
- (,interface (car args)) (,interface))
- (else (for-each ,print-option
- (,interface #t)))))))
-
- (make-enable (lambda (interface)
- `(lambda flags
- (,interface (append flags (,interface)))
- (,interface))))
-
- (make-disable (lambda (interface)
+(define define-option-interface
+ (let* ((option-name car)
+ (option-value cadr)
+ (option-documentation caddr)
+
+ (print-option (lambda (option)
+ (display (option-name option))
+ (if (< (string-length
+ (symbol->string (option-name option)))
+ 8)
+ (display #\tab))
+ (display #\tab)
+ (display (option-value option))
+ (display #\tab)
+ (display (option-documentation option))
+ (newline)))
+
+ ;; Below follow the macros defining the run-time option interfaces.
+
+ (make-options (lambda (interface)
+ `(lambda args
+ (cond ((null? args) (,interface))
+ ((list? (car args))
+ (,interface (car args)) (,interface))
+ (else (for-each ,print-option
+ (,interface #t)))))))
+
+ (make-enable (lambda (interface)
`(lambda flags
- (let ((options (,interface)))
- (for-each (lambda (flag)
- (set! options (delq! flag options)))
- flags)
- (,interface options)
- (,interface)))))
-
- (make-set! (lambda (interface)
- `((name exp)
- (,'quasiquote
- (begin (,interface (append (,interface)
- (list '(,'unquote name)
- (,'unquote exp))))
- (,interface))))))
- )
- (procedure->macro
+ (,interface (append flags (,interface)))
+ (,interface))))
+
+ (make-disable (lambda (interface)
+ `(lambda flags
+ (let ((options (,interface)))
+ (for-each (lambda (flag)
+ (set! options (delq! flag options)))
+ flags)
+ (,interface options)
+ (,interface)))))
+
+ (make-set! (lambda (interface)
+ `((name exp)
+ (,'quasiquote
+ (begin (,interface (append (,interface)
+ (list '(,'unquote name)
+ (,'unquote exp))))
+ (,interface)))))))
+ (procedure->macro
(lambda (exp env)
(cons 'begin
- (apply append
- (map (lambda (group)
- (let ((interface (car group)))
- (append (map (lambda (name constructor)
- `(define ,name
- ,(constructor interface)))
- (cadr group)
- (list make-options
- make-enable
- make-disable))
- (map (lambda (name constructor)
- `(defmacro ,name
- ,@(constructor interface)))
- (caddr group)
- (list make-set!)))))
- names)))))))
+ (let* ((option-group (cadr exp))
+ (interface (car option-group)))
+ (append (map (lambda (name constructor)
+ `(define ,name
+ ,(constructor interface)))
+ (cadr option-group)
+ (list make-options
+ make-enable
+ make-disable))
+ (map (lambda (name constructor)
+ `(defmacro ,name
+ ,@(constructor interface)))
+ (caddr option-group)
+ (list make-set!)))))))))
+
+(define-option-interface
+ (eval-options-interface
+ (eval-options eval-enable eval-disable)
+ (eval-set!)))
+
+(define-option-interface
+ (debug-options-interface
+ (debug-options debug-enable debug-disable)
+ (debug-set!)))
+
+(define-option-interface
+ (evaluator-traps-interface
+ (traps trap-enable trap-disable)
+ (trap-set!)))
+
+(define-option-interface
+ (read-options-interface
+ (read-options read-enable read-disable)
+ (read-set!)))
+
+(define-option-interface
+ (print-options-interface
+ (print-options print-enable print-disable)
+ (print-set!)))
\f
(save-stack lazy-handler-dispatch)
(apply throw key args))
-(define enter-frame-handler default-lazy-handler)
-(define apply-frame-handler default-lazy-handler)
-(define exit-frame-handler default-lazy-handler)
-
(define (lazy-handler-dispatch key . args)
- (case key
- ((apply-frame)
- (apply apply-frame-handler key args))
- ((exit-frame)
- (apply exit-frame-handler key args))
- ((enter-frame)
- (apply enter-frame-handler key args))
- (else
- (apply default-lazy-handler key args))))
+ (apply default-lazy-handler key args))
(define abort-hook (make-hook))
(let ((status #f)
(interactive #t))
(define (loop first)
- (let ((next
+ (let ((next
(catch #t
(lambda ()
(with-traps
(lambda ()
(first)
-
+
;; This line is needed because mark
;; doesn't do closures quite right.
;; Unreferenced locals should be
(lambda () (mask-signals))))
lazy-handler-dispatch))
-
+
(lambda (key . args)
(case key
((quit)
(begin
(newline (current-error-port))
(display
- "Type \"(backtrace)\" to get more information.\n"
+ "Type \"(backtrace)\" to get more information or \"(debug)\" to enter the debugger.\n"
(current-error-port))
(set! has-shown-debugger-hint? #t)))
(force-output (current-error-port)))
(apply bad-throw key args))))))))))
(if next (loop next) status)))
(set! set-batch-mode?! (lambda (arg)
- (cond (arg
+ (cond (arg
(set! interactive #f)
(restore-signals))
(#t
(loop (lambda () #t))))
;;(define the-last-stack (make-fluid)) Defined by scm_init_backtrace ()
+(define before-signal-stack (make-fluid))
(define stack-saved? #f)
(define (save-stack . narrowing)
the-last-stack
(case (stack-id #t)
((repl-stack)
- (apply make-stack #t save-stack eval #t 0 narrowing))
+ (apply make-stack #t save-stack primitive-eval #t 0 narrowing))
((load-stack)
(apply make-stack #t save-stack 0 #t 0 narrowing))
((tk-stack)
((memq 'backtrace (debug-options-interface))
(run-hook before-backtrace-hook)
(newline cep)
+ (display "Backtrace:\n")
(display-backtrace (fluid-ref the-last-stack) cep)
(newline cep)
(run-hook after-backtrace-hook)))
;; (display "No backtrace available.\n")))
(define (error-catching-repl r e p)
- (error-catching-loop (lambda () (p (e (r))))))
+ (error-catching-loop
+ (lambda ()
+ (call-with-values (lambda () (e (r)))
+ (lambda the-values (for-each p the-values))))))
(define (gc-run-time)
(cdr (assq 'gc-time-taken (gc-stats))))
(define before-read-hook (make-hook))
(define after-read-hook (make-hook))
+(define before-eval-hook (make-hook 1))
+(define after-eval-hook (make-hook 1))
+(define before-print-hook (make-hook 1))
+(define after-print-hook (make-hook 1))
;;; The default repl-reader function. We may override this if we've
;;; the readline library.
(read (current-input-port))))
(define (scm-style-repl)
+
(letrec (
(start-gc-rt #f)
(start-rt #f)
(-eval (lambda (sourc)
(repl-report-start-timing)
- (start-stack 'repl-stack (eval sourc))))
+ (run-hook before-eval-hook sourc)
+ (let ((val (start-stack 'repl-stack
+ ;; If you change this procedure
+ ;; (primitive-eval), please also
+ ;; modify the repl-stack case in
+ ;; save-stack so that stack cutting
+ ;; continues to work.
+ (primitive-eval sourc))))
+ (run-hook after-eval-hook sourc)
+ val)))
- (-print (lambda (result)
- (if (not scm-repl-silent)
- (begin
- (if (or scm-repl-print-unspecified
- (not (unspecified? result)))
- (begin
- (write result)
- (newline)))
- (if scm-repl-verbose
- (repl-report))
- (force-output)))))
+
+ (-print (let ((maybe-print (lambda (result)
+ (if (or scm-repl-print-unspecified
+ (not (unspecified? result)))
+ (begin
+ (write result)
+ (newline))))))
+ (lambda (result)
+ (if (not scm-repl-silent)
+ (begin
+ (run-hook before-print-hook result)
+ (maybe-print result)
+ (run-hook after-print-hook result)
+ (if scm-repl-verbose
+ (repl-report))
+ (force-output))))))
(-quit (lambda (args)
(if scm-repl-verbose
-eval
-print)))
(-quit status))))
-
+
\f
;;; {IOTA functions: generating lists of numbers}
`(with-fluids* (list ,@(map car bindings)) (list ,@(map cadr bindings))
(lambda () ,@body)))
-;;; Environments
-
-(define the-environment
- (procedure->syntax
- (lambda (x e)
- e)))
-
-(define (environment-module env)
- (let ((closure (and (pair? env) (car (last-pair env)))))
- (and closure (procedure-property closure 'module))))
-
\f
;;; {Macros}
(if (symbol? first)
(car rest)
`(lambda ,(cdr first) ,@rest))))
- `(define ,name (defmacro:transformer ,transformer))))
+ `(eval-case
+ ((load-toplevel)
+ (define ,name (defmacro:transformer ,transformer)))
+ (else
+ (error "define-macro can only be used at the top level")))))
(defmacro define-syntax-macro (first . rest)
(if (symbol? first)
(car rest)
`(lambda ,(cdr first) ,@rest))))
- `(define ,name (defmacro:syntax-transformer ,transformer))))
+ `(eval-case
+ ((load-toplevel)
+ (define ,name (defmacro:syntax-transformer ,transformer)))
+ (else
+ (error "define-syntax-macro can only be used at the top level")))))
+
\f
;;; {Module System Macros}
;;;
+;; Return a list of expressions that evaluate to the appropriate
+;; arguments for resolve-interface according to SPEC.
+
+(define (compile-interface-spec spec)
+ (define (make-keyarg sym key quote?)
+ (cond ((or (memq sym spec)
+ (memq key spec))
+ => (lambda (rest)
+ (if quote?
+ (list key (list 'quote (cadr rest)))
+ (list key (cadr rest)))))
+ (else
+ '())))
+ (define (map-apply func list)
+ (map (lambda (args) (apply func args)) list))
+ (define keys
+ ;; sym key quote?
+ '((:select #:select #t)
+ (:renamer #:renamer #f)))
+ (if (not (pair? (car spec)))
+ `(',spec)
+ `(',(car spec)
+ ,@(apply append (map-apply make-keyarg keys)))))
+
+(define (keyword-like-symbol->keyword sym)
+ (symbol->keyword (string->symbol (substring (symbol->string sym) 1))))
+
+(define (compile-define-module-args args)
+ ;; Just quote everything except #:use-module and #:use-syntax. We
+ ;; need to know about all arguments regardless since we want to turn
+ ;; symbols that look like keywords into real keywords, and the
+ ;; keyword args in a define-module form are not regular
+ ;; (i.e. no-backtrace doesn't take a value).
+ (let loop ((compiled-args `((quote ,(car args))))
+ (args (cdr args)))
+ (cond ((null? args)
+ (reverse! compiled-args))
+ ;; symbol in keyword position
+ ((symbol? (car args))
+ (loop compiled-args
+ (cons (keyword-like-symbol->keyword (car args)) (cdr args))))
+ ((memq (car args) '(#:no-backtrace #:pure))
+ (loop (cons (car args) compiled-args)
+ (cdr args)))
+ ((null? (cdr args))
+ (error "keyword without value:" (car args)))
+ ((memq (car args) '(#:use-module #:use-syntax))
+ (loop (cons* `(list ,@(compile-interface-spec (cadr args)))
+ (car args)
+ compiled-args)
+ (cddr args)))
+ ((eq? (car args) #:autoload)
+ (loop (cons* `(quote ,(caddr args))
+ `(quote ,(cadr args))
+ (car args)
+ compiled-args)
+ (cdddr args)))
+ (else
+ (loop (cons* `(quote ,(cadr args))
+ (car args)
+ compiled-args)
+ (cddr args))))))
+
(defmacro define-module args
- `(let* ((process-define-module process-define-module)
- (set-current-module set-current-module)
- (module (process-define-module ',args)))
- (set-current-module module)
- module))
-
-;; the guts of the use-modules macro. add the interfaces of the named
-;; modules to the use-list of the current module, in order
-(define (process-use-modules module-names)
- (for-each (lambda (module-name)
- (let ((mod-iface (resolve-interface module-name)))
+ `(eval-case
+ ((load-toplevel)
+ (let ((m (process-define-module
+ (list ,@(compile-define-module-args args)))))
+ (set-current-module m)
+ m))
+ (else
+ (error "define-module can only be used at the top level"))))
+
+;; The guts of the use-modules macro. Add the interfaces of the named
+;; modules to the use-list of the current module, in order.
+
+;; This function is called by "modules.c". If you change it, be sure
+;; to change scm_c_use_module as well.
+
+(define (process-use-modules module-interface-args)
+ (for-each (lambda (mif-args)
+ (let ((mod-iface (apply resolve-interface mif-args)))
(or mod-iface
- (error "no such module" module-name))
+ (error "no such module" mif-args))
(module-use! (current-module) mod-iface)))
- (reverse module-names)))
+ module-interface-args))
(defmacro use-modules modules
- `(process-use-modules ',modules))
+ `(eval-case
+ ((load-toplevel)
+ (process-use-modules
+ (list ,@(map (lambda (m)
+ `(list ,@(compile-interface-spec m)))
+ modules))))
+ (else
+ (error "use-modules can only be used at the top level"))))
(defmacro use-syntax (spec)
- `(begin
+ `(eval-case
+ ((load-toplevel)
,@(if (pair? spec)
- `((process-use-modules ',(list spec))
+ `((process-use-modules (list
+ (list ,@(compile-interface-spec spec))))
(set-module-transformer! (current-module)
,(car (last-pair spec))))
- `((set-module-transformer! (current-module) ,spec)))
- (set! scm:eval-transformer (module-transformer (current-module)))))
+ `((set-module-transformer! (current-module) ,spec))))
+ (else
+ (error "use-syntax can only be used at the top level"))))
(define define-private define)
((pair? n) (defined-name (car n)))
(else (syntax))))
(cond
- ((null? args) (syntax))
-
- (#t (let ((name (defined-name (car args))))
- `(begin
- (let ((public-i (module-public-interface (current-module))))
- ;; Make sure there is a local variable:
- ;;
- (module-define! (current-module)
- ',name
- (module-ref (current-module) ',name #f))
-
- ;; Make sure that local is exported:
- ;;
- (module-add! public-i ',name
- (module-variable (current-module) ',name)))
-
- ;; Now (re)define the var normally. Bernard URBAN
- ;; suggests we use eval here to accomodate Hobbit; it lets
- ;; the interpreter handle the define-private form, which
- ;; Hobbit can't digest.
- (eval '(define-private ,@ args)))))))
-
-
+ ((null? args)
+ (syntax))
+ (#t
+ (let ((name (defined-name (car args))))
+ `(begin
+ (define-private ,@args)
+ (eval-case ((load-toplevel) (export ,name))))))))
(defmacro defmacro-public args
(define (syntax)
(error "bad syntax" (list 'defmacro-public args)))
(define (defined-name n)
(cond
- ((symbol? n) n)
- (else (syntax))))
+ ((symbol? n) n)
+ (else (syntax))))
(cond
- ((null? args) (syntax))
-
- (#t (let ((name (defined-name (car args))))
- `(begin
- (let ((public-i (module-public-interface (current-module))))
- ;; Make sure there is a local variable:
- ;;
- (module-define! (current-module)
- ',name
- (module-ref (current-module) ',name #f))
-
- ;; Make sure that local is exported:
- ;;
- (module-add! public-i ',name (module-variable (current-module) ',name)))
-
- ;; Now (re)define the var normally.
- ;;
- (defmacro ,@ args))))))
-
+ ((null? args)
+ (syntax))
+ (#t
+ (let ((name (defined-name (car args))))
+ `(begin
+ (eval-case ((load-toplevel) (export ,name)))
+ (defmacro ,@args))))))
+
+;; Export a local variable
+
+;; This function is called from "modules.c". If you change it, be
+;; sure to update "modules.c" as well.
+
+(define (module-export! m names)
+ (let ((public-i (module-public-interface m)))
+ (for-each (lambda (name)
+ (let ((var (module-ensure-local-variable! m name)))
+ (module-add! public-i name var)))
+ names)))
+
+;; Re-export a imported variable
+;;
+(define (module-re-export! m names)
+ (let ((public-i (module-public-interface m)))
+ (for-each (lambda (name)
+ (let ((var (module-variable m name)))
+ (cond ((not var)
+ (error "Undefined variable:" name))
+ ((eq? var (module-local-variable m name))
+ (error "re-exporting local variable:" name))
+ (else
+ (module-add! public-i name var)))))
+ names)))
(defmacro export names
- `(let* ((m (current-module))
- (public-i (module-public-interface m)))
- (for-each (lambda (name)
- ;; Make sure there is a local variable:
- (module-define! m name (module-ref m name #f))
- ;; Make sure that local is exported:
- (module-add! public-i name (module-variable m name)))
- ',names)))
+ `(eval-case
+ ((load-toplevel)
+ (module-export! (current-module) ',names))
+ (else
+ (error "export can only be used at the top level"))))
+
+(defmacro re-export names
+ `(eval-case
+ ((load-toplevel)
+ (module-re-export! (current-module) ',names))
+ (else
+ (error "re-export can only be used at the top level"))))
(define export-syntax export)
+(define re-export-syntax re-export)
+(define load load-module)
+\f
-(define load load-module)
+;;; {`cond-expand' for SRFI-0 support.}
+;;;
+;;; This syntactic form expands into different commands or
+;;; definitions, depending on the features provided by the Scheme
+;;; implementation.
+;;;
+;;; Syntax:
+;;;
+;;; <cond-expand>
+;;; --> (cond-expand <cond-expand-clause>+)
+;;; | (cond-expand <cond-expand-clause>* (else <command-or-definition>))
+;;; <cond-expand-clause>
+;;; --> (<feature-requirement> <command-or-definition>*)
+;;; <feature-requirement>
+;;; --> <feature-identifier>
+;;; | (and <feature-requirement>*)
+;;; | (or <feature-requirement>*)
+;;; | (not <feature-requirement>)
+;;; <feature-identifier>
+;;; --> <a symbol which is the name or alias of a SRFI>
+;;;
+;;; Additionally, this implementation provides the
+;;; <feature-identifier>s `guile' and `r5rs', so that programs can
+;;; determine the implementation type and the supported standard.
+;;;
+;;; Currently, the following feature identifiers are supported:
+;;;
+;;; guile r5rs srfi-0
+;;;
+;;; Remember to update the features list when adding more SRFIs.
+
+(define %cond-expand-features
+ ;; Adjust the above comment when changing this.
+ '(guile r5rs srfi-0))
+
+;; This table maps module public interfaces to the list of features.
+;;
+(define %cond-expand-table (make-hash-table 31))
+
+;; Add one or more features to the `cond-expand' feature list of the
+;; module `module'.
+;;
+(define (cond-expand-provide module features)
+ (let ((mod (module-public-interface module)))
+ (and mod
+ (hashq-set! %cond-expand-table mod
+ (append (hashq-ref %cond-expand-table mod '())
+ features)))))
+
+(define cond-expand
+ (procedure->memoizing-macro
+ (lambda (exp env)
+ (let ((clauses (cdr exp))
+ (syntax-error (lambda (cl)
+ (error "invalid clause in `cond-expand'" cl))))
+ (letrec
+ ((test-clause
+ (lambda (clause)
+ (cond
+ ((symbol? clause)
+ (or (memq clause %cond-expand-features)
+ (let lp ((uses (module-uses (env-module env))))
+ (if (pair? uses)
+ (or (memq clause
+ (hashq-ref %cond-expand-table
+ (car uses) '()))
+ (lp (cdr uses)))
+ #f))))
+ ((pair? clause)
+ (cond
+ ((eq? 'and (car clause))
+ (let lp ((l (cdr clause)))
+ (cond ((null? l)
+ #t)
+ ((pair? l)
+ (and (test-clause (car l)) (lp (cdr l))))
+ (else
+ (syntax-error clause)))))
+ ((eq? 'or (car clause))
+ (let lp ((l (cdr clause)))
+ (cond ((null? l)
+ #f)
+ ((pair? l)
+ (or (test-clause (car l)) (lp (cdr l))))
+ (else
+ (syntax-error clause)))))
+ ((eq? 'not (car clause))
+ (cond ((not (pair? (cdr clause)))
+ (syntax-error clause))
+ ((pair? (cddr clause))
+ ((syntax-error clause))))
+ (not (test-clause (cadr clause))))
+ (else
+ (syntax-error clause))))
+ (else
+ (syntax-error clause))))))
+ (let lp ((c clauses))
+ (cond
+ ((null? c)
+ (error "Unfulfilled `cond-expand'"))
+ ((not (pair? c))
+ (syntax-error c))
+ ((not (pair? (car c)))
+ (syntax-error (car c)))
+ ((test-clause (caar c))
+ `(begin ,@(cdar c)))
+ ((eq? (caar c) 'else)
+ (if (pair? (cdr c))
+ (syntax-error c))
+ `(begin ,@(cdar c)))
+ (else
+ (lp (cdr c))))))))))
+;; This procedure gets called from the startup code with a list of
+;; numbers, which are the numbers of the SRFIs to be loaded on startup.
+;;
+(define (use-srfis srfis)
+ (let lp ((s srfis))
+ (if (pair? s)
+ (let* ((srfi (string->symbol
+ (string-append "srfi-" (number->string (car s)))))
+ (mod-i (resolve-interface (list 'srfi srfi))))
+ (module-use! (current-module) mod-i)
+ (lp (cdr s))))))
\f
+
;;; {Load emacs interface support if emacs option is given.}
+(define (named-module-use! user usee)
+ (module-use! (resolve-module user) (resolve-interface usee)))
+
(define (load-emacs-interface)
- (if (memq 'debug-extensions *features*)
- (debug-enable 'backtrace))
- (define-module (guile-user) :use-module (ice-9 emacs)))
+ (and (provided? 'debug-extensions)
+ (debug-enable 'backtrace))
+ (named-module-use! '(guile-user) '(ice-9 emacs)))
\f
-;;; {I/O functions for Tcl channels (disabled)}
-
-;; (define in-ch (get-standard-channel TCL_STDIN))
-;; (define out-ch (get-standard-channel TCL_STDOUT))
-;; (define err-ch (get-standard-channel TCL_STDERR))
-;;
-;; (define inp (%make-channel-port in-ch "r"))
-;; (define outp (%make-channel-port out-ch "w"))
-;; (define errp (%make-channel-port err-ch "w"))
-;;
-;; (define %system-char-ready? char-ready?)
-;;
-;; (define (char-ready? p)
-;; (if (not (channel-port? p))
-;; (%system-char-ready? p)
-;; (let* ((channel (%channel-port-channel p))
-;; (old-blocking (channel-option-ref channel :blocking)))
-;; (dynamic-wind
-;; (lambda () (set-channel-option the-root-tcl-interpreter channel :blocking "0"))
-;; (lambda () (not (eof-object? (peek-char p))))
-;; (lambda () (set-channel-option the-root-tcl-interpreter channel :blocking old-blocking))))))
-;;
-;; (define (top-repl)
-;; (with-input-from-port inp
-;; (lambda ()
-;; (with-output-to-port outp
-;; (lambda ()
-;; (with-error-to-port errp
-;; (lambda ()
-;; (scm-style-repl))))))))
-;;
-;; (set-current-input-port inp)
-;; (set-current-output-port outp)
-;; (set-current-error-port errp)
-
-;; this is just (scm-style-repl) with a wrapper to install and remove
-;; signal handlers.
-(define (top-repl)
-
- ;; Load emacs interface support if emacs option is given.
- (if (and (module-defined? the-root-module 'use-emacs-interface)
- use-emacs-interface)
- (load-emacs-interface))
-
- ;; Place the user in the guile-user module.
- (define-module (guile-user)
- :use-module (ice-9 session)
- :use-module (ice-9 debug)
- :use-module (ice-9 debugger))
- (if (memq 'threads *features*)
- (define-module (guile-user) :use-module (ice-9 threads)))
- (if (memq 'regex *features*)
- (define-module (guile-user) :use-module (ice-9 regex)))
-
- (let ((old-handlers #f)
- (signals `((,SIGINT . "User interrupt")
- (,SIGFPE . "Arithmetic error")
- (,SIGBUS . "Bad memory access (bus error)")
- (,SIGSEGV . "Bad memory access (Segmentation violation)"))))
-
- (dynamic-wind
-
- ;; call at entry
- (lambda ()
- (let ((make-handler (lambda (msg)
- (lambda (sig)
- (save-stack %deliver-signals)
- (scm-error 'signal
- #f
- msg
- #f
- (list sig))))))
- (set! old-handlers
- (map (lambda (sig-msg)
- (sigaction (car sig-msg)
- (make-handler (cdr sig-msg))))
- signals))))
-
- ;; the protected thunk.
- (lambda ()
-
- ;; If we've got readline, use it to prompt the user. This is a
- ;; kludge, but we'll fix it soon. At least we only get
- ;; readline involved when we're actually running the repl.
- (if (and (memq 'readline *features*)
- (isatty? (current-input-port))
- (not (and (module-defined? the-root-module
- 'use-emacs-interface)
- use-emacs-interface)))
- (let ((read-hook (lambda () (run-hook before-read-hook))))
- (set-current-input-port (readline-port))
- (set! repl-reader
- (lambda (prompt)
- (dynamic-wind
- (lambda ()
- (set-readline-prompt! prompt "... ")
- (set-readline-read-hook! read-hook))
- (lambda () (read))
- (lambda ()
- (set-readline-prompt! "" "")
- (set-readline-read-hook! #f)))))))
- (let ((status (scm-style-repl)))
- (run-hook exit-hook)
- status))
-
- ;; call at exit.
- (lambda ()
- (map (lambda (sig-msg old-handler)
- (if (not (car old-handler))
- ;; restore original C handler.
- (sigaction (car sig-msg) #f)
- ;; restore Scheme handler, SIG_IGN or SIG_DFL.
- (sigaction (car sig-msg)
- (car old-handler)
- (cdr old-handler))))
- signals old-handlers)))))
+
+(define using-readline?
+ (let ((using-readline? (make-fluid)))
+ (make-procedure-with-setter
+ (lambda () (fluid-ref using-readline?))
+ (lambda (v) (fluid-set! using-readline? v)))))
+
+(define (top-repl)
+ (let ((guile-user-module (resolve-module '(guile-user))))
+
+ ;; Load emacs interface support if emacs option is given.
+ (if (and (module-defined? the-root-module 'use-emacs-interface)
+ (module-ref the-root-module 'use-emacs-interface))
+ (load-emacs-interface))
+
+ ;; Use some convenient modules (in reverse order)
+
+ (if (provided? 'regex)
+ (module-use! guile-user-module (resolve-interface '(ice-9 regex))))
+ (if (provided? 'threads)
+ (module-use! guile-user-module (resolve-interface '(ice-9 threads))))
+ ;; load debugger on demand
+ (module-use! guile-user-module
+ (make-autoload-interface guile-user-module
+ '(ice-9 debugger) '(debug)))
+ (module-use! guile-user-module (resolve-interface '(ice-9 session)))
+ (module-use! guile-user-module (resolve-interface '(ice-9 debug)))
+ ;; so that builtin bindings will be checked first
+ (module-use! guile-user-module (resolve-interface '(guile)))
+
+ (set-current-module guile-user-module)
+
+ (let ((old-handlers #f)
+ (signals (if (provided? 'posix)
+ `((,SIGINT . "User interrupt")
+ (,SIGFPE . "Arithmetic error")
+ (,SIGBUS . "Bad memory access (bus error)")
+ (,SIGSEGV
+ . "Bad memory access (Segmentation violation)"))
+ '())))
+
+ (dynamic-wind
+
+ ;; call at entry
+ (lambda ()
+ (let ((make-handler (lambda (msg)
+ (lambda (sig)
+ ;; Make a backup copy of the stack
+ (fluid-set! before-signal-stack
+ (fluid-ref the-last-stack))
+ (save-stack %deliver-signals)
+ (scm-error 'signal
+ #f
+ msg
+ #f
+ (list sig))))))
+ (set! old-handlers
+ (map (lambda (sig-msg)
+ (sigaction (car sig-msg)
+ (make-handler (cdr sig-msg))))
+ signals))))
+
+ ;; the protected thunk.
+ (lambda ()
+ (let ((status (scm-style-repl)))
+ (run-hook exit-hook)
+ status))
+
+ ;; call at exit.
+ (lambda ()
+ (map (lambda (sig-msg old-handler)
+ (if (not (car old-handler))
+ ;; restore original C handler.
+ (sigaction (car sig-msg) #f)
+ ;; restore Scheme handler, SIG_IGN or SIG_DFL.
+ (sigaction (car sig-msg)
+ (car old-handler)
+ (cdr old-handler))))
+ signals old-handlers))))))
(defmacro false-if-exception (expr)
`(catch #t (lambda () ,expr)
;;;
(define exit-hook (make-hook))
-;;; Load readline code into root module if readline primitives are available.
-;;;
-;;; Ideally, we wouldn't do this until we were sure we were actually
-;;; going to enter the repl, but autoloading individual functions is
-;;; clumsy at the moment.
-(if (and (memq 'readline *features*)
- (isatty? (current-input-port)))
- (begin
- (define-module (guile) :use-module (ice-9 readline))
- (define-module (guile-user) :use-module (ice-9 readline))))
-
\f
-;;; {Load debug extension code into user module if debug extensions present.}
-;;;
-;;; *fixme* This is a temporary solution.
-;;;
-
-(if (memq 'debug-extensions *features*)
- (define-module (guile-user) :use-module (ice-9 debug)))
-
-\f
-;;; {Load session support into user module if present.}
-;;;
-;;; *fixme* This is a temporary solution.
-;;;
-
-(if (%search-load-path "ice-9/session.scm")
- (define-module (guile-user) :use-module (ice-9 session)))
-
-;;; {Load thread code into user module if threads are present.}
-;;;
-;;; *fixme* This is a temporary solution.
-;;;
-
-(if (memq 'threads *features*)
- (define-module (guile-user) :use-module (ice-9 threads)))
-
-\f
-;;; {Load regexp code if regexp primitives are available.}
-
-(if (memq 'regex *features*)
- (define-module (guile-user) :use-module (ice-9 regex)))
+(append! %load-path (list "."))
-\f
-(define-module (guile))
+;; Place the user in the guile-user module.
+;;
-(append! %load-path (cons "." ()))
+(define-module (guile-user))
+;;; boot-9.scm ends here