Commit | Line | Data |
---|---|---|
7ebe6c76 JB |
1 | ;;;; slib.scm --- definitions needed to get SLIB to work with Guile |
2 | ;;;; | |
509a787a | 3 | ;;;; Copyright (C) 1997, 1998, 2000, 2001, 2002, 2003 Free Software Foundation, Inc. |
a482f2cc | 4 | ;;;; |
73be1d9e MV |
5 | ;;;; This library is free software; you can redistribute it and/or |
6 | ;;;; modify it under the terms of the GNU Lesser General Public | |
7 | ;;;; License as published by the Free Software Foundation; either | |
8 | ;;;; version 2.1 of the License, or (at your option) any later version. | |
a482f2cc | 9 | ;;;; |
73be1d9e MV |
10 | ;;;; This library is distributed in the hope that it will be useful, |
11 | ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
a482f2cc | 12 | ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
73be1d9e | 13 | ;;;; Lesser General Public License for more details. |
a482f2cc | 14 | ;;;; |
73be1d9e MV |
15 | ;;;; You should have received a copy of the GNU Lesser General Public |
16 | ;;;; License along with this library; if not, write to the Free Software | |
17 | ;;;; Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
7ebe6c76 | 18 | ;;;; |
3267d4a1 | 19 | (define-module (ice-9 slib) |
1a179b03 MD |
20 | :export (slib:load slib:load-source defmacro:load |
21 | implementation-vicinity library-vicinity home-vicinity | |
22 | scheme-implementation-type scheme-implementation-version | |
509a787a | 23 | output-port-width output-port-height array-indexes |
1a179b03 MD |
24 | make-random-state require slib:error slib:exit slib:warn slib:eval |
25 | defmacro:eval logical:logand logical:logior logical:logxor | |
26 | logical:lognot logical:ash logical:logcount logical:integer-length | |
27 | logical:bit-extract logical:integer-expt logical:ipow-by-squaring | |
28 | slib:eval-load slib:tab slib:form-feed difftime offset-time | |
29 | software-type) | |
70a459e3 | 30 | :replace (provide provided?) |
3267d4a1 | 31 | :no-backtrace) |
1a179b03 | 32 | |
0f2d19dd JB |
33 | \f |
34 | ||
35 | (define (eval-load <filename> evl) | |
36 | (if (not (file-exists? <filename>)) | |
37 | (set! <filename> (string-append <filename> (scheme-file-suffix)))) | |
38 | (call-with-input-file <filename> | |
39 | (lambda (port) | |
40 | (let ((old-load-pathname *load-pathname*)) | |
41 | (set! *load-pathname* <filename>) | |
75a97b92 | 42 | (do ((o (read port) (read port))) |
0f2d19dd JB |
43 | ((eof-object? o)) |
44 | (evl o)) | |
45 | (set! *load-pathname* old-load-pathname))))) | |
46 | ||
47 | \f | |
48 | ||
49 | (define slib:exit quit) | |
50 | (define slib:error error) | |
7ed9feb0 | 51 | (define slib:warn warn) |
21c2a33a MD |
52 | (define slib:eval (lambda (x) (eval x slib-module))) |
53 | (define defmacro:eval (lambda (x) (eval x (interaction-environment)))) | |
0f2d19dd JB |
54 | (define logical:logand logand) |
55 | (define logical:logior logior) | |
56 | (define logical:logxor logxor) | |
57 | (define logical:lognot lognot) | |
58 | (define logical:ash ash) | |
59 | (define logical:logcount logcount) | |
60 | (define logical:integer-length integer-length) | |
61 | (define logical:bit-extract bit-extract) | |
62 | (define logical:integer-expt integer-expt) | |
0f2d19dd JB |
63 | (define slib:eval-load eval-load) |
64 | (define slib:tab #\tab) | |
65 | (define slib:form-feed #\page) | |
66 | ||
ed218d98 MV |
67 | (define slib-module (current-module)) |
68 | ||
69 | (define (defined? symbol) | |
70 | (module-defined? slib-module symbol)) | |
71 | ||
1a179b03 MD |
72 | ;;; *FEATURES* should be set to a list of symbols describing features |
73 | ;;; of this implementation. Suggestions for features are: | |
70a459e3 | 74 | (set! *features* |
1a179b03 MD |
75 | (append |
76 | '( | |
77 | source ;can load scheme source files | |
78 | ;(slib:load-source "filename") | |
79 | ; compiled ;can load compiled files | |
80 | ;(slib:load-compiled "filename") | |
81 | ||
82 | ;; Scheme report features | |
83 | ||
84 | ; rev5-report ;conforms to | |
85 | eval ;R5RS two-argument eval | |
86 | ; values ;R5RS multiple values | |
87 | dynamic-wind ;R5RS dynamic-wind | |
88 | ; macro ;R5RS high level macros | |
89 | delay ;has DELAY and FORCE | |
90 | multiarg-apply ;APPLY can take more than 2 args. | |
91 | ; rationalize | |
92 | rev4-optional-procedures ;LIST-TAIL, STRING->LIST, | |
93 | ;LIST->STRING, STRING-COPY, | |
94 | ;STRING-FILL!, LIST->VECTOR, | |
95 | ;VECTOR->LIST, and VECTOR-FILL! | |
96 | ||
97 | ; rev4-report ;conforms to | |
98 | ||
99 | ; ieee-p1178 ;conforms to | |
100 | ||
101 | ; rev3-report ;conforms to | |
102 | ||
103 | rev2-procedures ;SUBSTRING-MOVE-LEFT!, | |
104 | ;SUBSTRING-MOVE-RIGHT!, | |
105 | ;SUBSTRING-FILL!, | |
106 | ;STRING-NULL?, APPEND!, 1+, | |
107 | ;-1+, <?, <=?, =?, >?, >=? | |
108 | ; object-hash ;has OBJECT-HASH | |
109 | ||
110 | multiarg/and- ;/ and - can take more than 2 args. | |
111 | with-file ;has WITH-INPUT-FROM-FILE and | |
112 | ;WITH-OUTPUT-FROM-FILE | |
113 | ; transcript ;TRANSCRIPT-ON and TRANSCRIPT-OFF | |
114 | ; ieee-floating-point ;conforms to IEEE Standard 754-1985 | |
115 | ;IEEE Standard for Binary | |
116 | ;Floating-Point Arithmetic. | |
117 | full-continuation ;can return multiple times | |
118 | ||
119 | ;; Other common features | |
120 | ||
121 | ; srfi ;srfi-0, COND-EXPAND finds all srfi-* | |
122 | ; sicp ;runs code from Structure and | |
123 | ;Interpretation of Computer | |
124 | ;Programs by Abelson and Sussman. | |
125 | defmacro ;has Common Lisp DEFMACRO | |
126 | ; record ;has user defined data structures | |
127 | string-port ;has CALL-WITH-INPUT-STRING and | |
128 | ;CALL-WITH-OUTPUT-STRING | |
129 | ; sort | |
130 | ; pretty-print | |
131 | ; object->string | |
132 | ; format ;Common-lisp output formatting | |
133 | ; trace ;has macros: TRACE and UNTRACE | |
134 | ; compiler ;has (COMPILER) | |
135 | ; ed ;(ED) is editor | |
136 | random | |
137 | ) | |
138 | ||
139 | (if (defined? 'getenv) | |
140 | '(getenv) | |
141 | '()) | |
142 | ||
143 | (if (defined? 'current-time) | |
144 | '(current-time) | |
145 | '()) | |
146 | ||
147 | (if (defined? 'system) | |
148 | '(system) | |
149 | '()) | |
150 | ||
151 | (if (defined? 'array?) | |
152 | '(array) | |
153 | '()) | |
154 | ||
155 | (if (defined? 'char-ready?) | |
156 | '(char-ready?) | |
157 | '()) | |
158 | ||
159 | (if (defined? 'array-for-each) | |
160 | '(array-for-each) | |
161 | '()) | |
162 | ||
163 | *features*)) | |
0f2d19dd JB |
164 | |
165 | ||
9b345f6c | 166 | ;;; FIXME: Because uers want require to search the path, this uses |
096d5f90 | 167 | ;;; load-from-path, which probably isn't a hot idea. slib |
9b345f6c JB |
168 | ;;; doesn't expect this function to search a path, so I expect to get |
169 | ;;; bug reports at some point complaining that the wrong file gets | |
170 | ;;; loaded when something accidentally appears in the path before | |
171 | ;;; slib, etc. ad nauseum. However, the right fix seems to involve | |
172 | ;;; changing catalog:get in slib/require.scm, and I don't expect | |
173 | ;;; Aubrey will integrate such a change. So I'm just going to punt | |
174 | ;;; for the time being. | |
1a179b03 | 175 | (define (slib:load name) |
0f2d19dd JB |
176 | (save-module-excursion |
177 | (lambda () | |
178 | (set-current-module slib-module) | |
d1005e3c MD |
179 | (let ((errinfo (catch 'system-error |
180 | (lambda () | |
181 | (load-from-path name) | |
182 | #f) | |
183 | (lambda args args)))) | |
184 | (if (and errinfo | |
185 | (catch 'system-error | |
186 | (lambda () | |
187 | (load-from-path | |
188 | (string-append name ".scm")) | |
189 | #f) | |
190 | (lambda args args))) | |
c51bfd81 | 191 | (apply throw errinfo)))))) |
0f2d19dd JB |
192 | |
193 | (define slib:load-source slib:load) | |
194 | (define defmacro:load slib:load) | |
195 | ||
c51bfd81 MD |
196 | (define slib-parent-dir |
197 | (let* ((path (%search-load-path "slib/require.scm"))) | |
00f06035 | 198 | (if path |
4e15fee8 | 199 | (substring path 0 (- (string-length path) 17)) |
00f06035 | 200 | (error "Could not find slib/require.scm in " %load-path)))) |
c51bfd81 | 201 | |
1a179b03 | 202 | (define (implementation-vicinity) |
c51bfd81 | 203 | (string-append slib-parent-dir "/")) |
1a179b03 | 204 | (define (library-vicinity) |
c51bfd81 | 205 | (string-append (implementation-vicinity) "slib/")) |
1a179b03 | 206 | (define home-vicinity |
ad76c8d9 TP |
207 | (let ((home-path (getenv "HOME"))) |
208 | (lambda () home-path))) | |
1a179b03 MD |
209 | (define (scheme-implementation-type) 'guile) |
210 | (define scheme-implementation-version version) | |
211 | ;;; (scheme-implementation-home-page) should return a (string) URI | |
212 | ;;; (Uniform Resource Identifier) for this scheme implementation's home | |
213 | ;;; page; or false if there isn't one. | |
214 | (define (scheme-implementation-home-page) | |
215 | "http://www.gnu.org/software/guile/guile.html") | |
0f2d19dd JB |
216 | |
217 | (define (output-port-width . arg) 80) | |
218 | (define (output-port-height . arg) 24) | |
4b0d6055 | 219 | |
1a179b03 MD |
220 | ;;; {array-for-each} |
221 | (define (array-indexes ra) | |
222 | (let ((ra0 (apply make-array '() (array-shape ra)))) | |
223 | (array-index-map! ra0 list) | |
224 | ra0)) | |
225 | ||
6001fe82 MD |
226 | ;;; {Random numbers} |
227 | ;;; | |
1a179b03 | 228 | (define (make-random-state . args) |
6001fe82 MD |
229 | (let ((seed (if (null? args) *random-state* (car args)))) |
230 | (cond ((string? seed)) | |
231 | ((number? seed) (set! seed (number->string seed))) | |
232 | (else (let () | |
233 | (require 'object->string) | |
234 | (set! seed (object->limited-string seed 50))))) | |
235 | (seed->random-state seed))) | |
236 | ||
0f2d19dd JB |
237 | ;;; {Time} |
238 | ;;; | |
239 | ||
240 | (define difftime -) | |
241 | (define offset-time +) | |
242 | ||
243 | \f | |
0f2d19dd JB |
244 | (define define |
245 | (procedure->memoizing-macro | |
246 | (lambda (exp env) | |
247 | (if (= (length env) 1) | |
248 | `(define-public ,@(cdr exp)) | |
9123414e | 249 | `(define-private ,@(cdr exp)))))) |
0f2d19dd | 250 | |
7a0ff2f8 MD |
251 | ;;; Hack to make syncase macros work in the slib module |
252 | (if (nested-ref the-root-module '(app modules ice-9 syncase)) | |
253 | (set-object-property! (module-local-variable (current-module) 'define) | |
254 | '*sc-expander* | |
255 | '(define))) | |
256 | ||
26ec032d | 257 | (define (software-type) |
ea4bcd7b GB |
258 | "Return a symbol describing the current platform's operating system. |
259 | This may be one of AIX, VMS, UNIX, COHERENT, WINDOWS, MS-DOS, OS/2, | |
260 | THINKC, AMIGA, ATARIST, MACH, or ACORN. | |
261 | ||
262 | Note that most varieties of Unix are considered to be simply \"UNIX\". | |
263 | That is because when a program depends on features that are not present | |
264 | on every operating system, it is usually better to test for the presence | |
265 | or absence of that specific feature. The return value of | |
266 | @code{software-type} should only be used for this purpose when there is | |
267 | no other easy or unambiguous way of detecting such features." | |
268 | 'UNIX) | |
0f2d19dd | 269 | |
c51bfd81 | 270 | (slib:load (in-vicinity (library-vicinity) "require.scm")) |
0f2d19dd | 271 | |
1a179b03 | 272 | (define require require:require) |
c51bfd81 MD |
273 | |
274 | ;; {Extensions to the require system so that the user can add new | |
275 | ;; require modules easily.} | |
276 | ||
277 | (define *vicinity-table* | |
278 | (list | |
279 | (cons 'implementation (implementation-vicinity)) | |
280 | (cons 'library (library-vicinity)))) | |
281 | ||
282 | (define (install-require-vicinity name vicinity) | |
283 | (let ((entry (assq name *vicinity-table*))) | |
284 | (if entry | |
285 | (set-cdr! entry vicinity) | |
286 | (set! *vicinity-table* | |
287 | (acons name vicinity *vicinity-table*))))) | |
288 | ||
289 | (define (install-require-module name vicinity-name file-name) | |
12ed431d MD |
290 | (if (not *catalog*) ;Fix which loads catalog in slib |
291 | (catalog:get 'random)) ;(doesn't load the feature 'random) | |
c51bfd81 MD |
292 | (let ((entry (assq name *catalog*)) |
293 | (vicinity (cdr (assq vicinity-name *vicinity-table*)))) | |
294 | (let ((path-name (in-vicinity vicinity file-name))) | |
295 | (if entry | |
296 | (set-cdr! entry path-name) | |
297 | (set! *catalog* | |
298 | (acons name path-name *catalog*)))))) | |
6012c379 MV |
299 | |
300 | (define (make-exchanger obj) | |
301 | (lambda (rep) (let ((old obj)) (set! obj rep) old))) |