Commit | Line | Data |
---|---|---|
0f2d19dd JB |
1 | ;;; installed-scm-file |
2 | ||
d0cbd20c | 3 | ;;;; Copyright (C) 1995, 1996, 1997 Free Software Foundation, Inc. |
0f2d19dd JB |
4 | ;;;; |
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. | |
9 | ;;;; | |
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. | |
14 | ;;;; | |
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 | |
15328041 JB |
17 | ;;;; the Free Software Foundation, Inc., 59 Temple Place, Suite 330, |
18 | ;;;; Boston, MA 02111-1307 USA | |
0f2d19dd JB |
19 | ;;;; |
20 | \f | |
21 | ||
22 | ;;; This file is the first thing loaded into Guile. It adds many mundane | |
23 | ;;; definitions and a few that are interesting. | |
24 | ;;; | |
25 | ;;; The module system (hence the hierarchical namespace) are defined in this | |
26 | ;;; file. | |
27 | ;;; | |
28 | ||
29 | \f | |
21ed9efe MD |
30 | ;;; {Features} |
31 | ;; | |
32 | ||
33 | (define (provide sym) | |
34 | (if (not (memq sym *features*)) | |
35 | (set! *features* (cons sym *features*)))) | |
36 | ||
37 | \f | |
79451588 JB |
38 | ;;; {R4RS compliance} |
39 | ||
40 | (primitive-load-path "ice-9/r4rs.scm") | |
41 | ||
42 | \f | |
44cf1f0f | 43 | ;;; {Simple Debugging Tools} |
0f2d19dd JB |
44 | ;; |
45 | ||
46 | ||
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.: | |
51 | ;; | |
52 | ;; (+ 10 (troublesome-fn)) | |
53 | ;; => (+ 10 (pk 'troublesome-fn-returned (troublesome-fn))) | |
54 | ;; | |
55 | ||
56 | (define (peek . stuff) | |
57 | (newline) | |
58 | (display ";;; ") | |
59 | (write stuff) | |
60 | (newline) | |
61 | (car (last-pair stuff))) | |
62 | ||
63 | (define pk peek) | |
64 | ||
65 | (define (warn . stuff) | |
66 | (with-output-to-port (current-error-port) | |
67 | (lambda () | |
68 | (newline) | |
69 | (display ";;; WARNING ") | |
6355358a | 70 | (display stuff) |
0f2d19dd JB |
71 | (newline) |
72 | (car (last-pair stuff))))) | |
73 | ||
74 | \f | |
79451588 | 75 | ;;; {Trivial Functions} |
0f2d19dd | 76 | ;;; |
79451588 JB |
77 | |
78 | (define (id x) x) | |
79 | (define (1+ n) (+ n 1)) | |
80 | (define (-1+ n) (+ n -1)) | |
81 | (define 1- -1+) | |
82 | (define return-it noop) | |
132e5fac | 83 | (define (and=> value procedure) (and value (procedure value))) |
79451588 JB |
84 | (define (make-hash-table k) (make-vector k '())) |
85 | ||
0f2d19dd JB |
86 | ;;; apply-to-args is functionally redunant with apply and, worse, |
87 | ;;; is less general than apply since it only takes two arguments. | |
88 | ;;; | |
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.: | |
92 | ;;; | |
93 | ;;; (apply-to-args (return-3d-mouse-coords) | |
94 | ;;; (lambda (x y z) | |
95 | ;;; ...)) | |
96 | ;;; | |
97 | ||
98 | (define (apply-to-args args fn) (apply fn args)) | |
99 | ||
100 | \f | |
0f2d19dd JB |
101 | ;;; {Integer Math} |
102 | ;;; | |
103 | ||
0f2d19dd JB |
104 | (define (ipow-by-squaring x k acc proc) |
105 | (cond ((zero? k) acc) | |
106 | ((= 1 k) (proc acc x)) | |
52c5a23a JB |
107 | (else (ipow-by-squaring (proc x x) |
108 | (quotient k 2) | |
109 | (if (even? k) acc (proc acc x)) | |
110 | proc)))) | |
0f2d19dd JB |
111 | |
112 | (define string-character-length string-length) | |
113 | ||
114 | ||
115 | ||
116 | ;; A convenience function for combining flag bits. Like logior, but | |
117 | ;; handles the cases of 0 and 1 arguments. | |
118 | ;; | |
119 | (define (flags . args) | |
120 | (cond | |
121 | ((null? args) 0) | |
122 | ((null? (cdr args)) (car args)) | |
123 | (else (apply logior args)))) | |
124 | ||
125 | \f | |
0f2d19dd JB |
126 | ;;; {Symbol Properties} |
127 | ;;; | |
128 | ||
129 | (define (symbol-property sym prop) | |
130 | (let ((pair (assoc prop (symbol-pref sym)))) | |
131 | (and pair (cdr pair)))) | |
132 | ||
133 | (define (set-symbol-property! sym prop val) | |
134 | (let ((pair (assoc prop (symbol-pref sym)))) | |
135 | (if pair | |
136 | (set-cdr! pair val) | |
137 | (symbol-pset! sym (acons prop val (symbol-pref sym)))))) | |
138 | ||
139 | (define (symbol-property-remove! sym prop) | |
140 | (let ((pair (assoc prop (symbol-pref sym)))) | |
141 | (if pair | |
142 | (symbol-pset! sym (delq! pair (symbol-pref sym)))))) | |
143 | ||
144 | \f | |
1e531c3a GH |
145 | |
146 | ;;; {Line and Delimited I/O} | |
147 | ||
148 | ;;; corresponds to SCM_LINE_INCREMENTORS in libguile. | |
149 | (define scm-line-incrementors "\n") | |
150 | ||
151 | (define (read-line! string . maybe-port) | |
152 | (let* ((port (if (pair? maybe-port) | |
153 | (car maybe-port) | |
154 | (current-input-port)))) | |
155 | (let* ((rv (%read-delimited! scm-line-incrementors | |
156 | string | |
157 | #t | |
158 | port)) | |
159 | (terminator (car rv)) | |
160 | (nchars (cdr rv))) | |
161 | (cond ((and (= nchars 0) | |
162 | (eof-object? terminator)) | |
163 | terminator) | |
164 | ((not terminator) #f) | |
165 | (else nchars))))) | |
166 | ||
167 | (define (read-delimited! delims buf . args) | |
168 | (let* ((num-args (length args)) | |
169 | (port (if (> num-args 0) | |
170 | (car args) | |
171 | (current-input-port))) | |
172 | (handle-delim (if (> num-args 1) | |
173 | (cadr args) | |
174 | 'trim)) | |
175 | (start (if (> num-args 2) | |
176 | (caddr args) | |
177 | 0)) | |
178 | (end (if (> num-args 3) | |
179 | (cadddr args) | |
180 | (string-length buf)))) | |
181 | (let* ((rv (%read-delimited! delims | |
182 | buf | |
183 | (not (eq? handle-delim 'peek)) | |
184 | port | |
185 | start | |
186 | end)) | |
187 | (terminator (car rv)) | |
188 | (nchars (cdr rv))) | |
189 | (cond ((or (not terminator) ; buffer filled | |
190 | (eof-object? terminator)) | |
191 | (if (zero? nchars) | |
192 | (if (eq? handle-delim 'split) | |
193 | (cons terminator terminator) | |
194 | terminator) | |
195 | (if (eq? handle-delim 'split) | |
196 | (cons nchars terminator) | |
197 | nchars))) | |
198 | (else | |
199 | (case handle-delim | |
200 | ((trim peek) nchars) | |
201 | ((concat) (string-set! buf nchars terminator) | |
202 | (+ nchars 1)) | |
203 | ((split) (cons nchars terminator)) | |
204 | (else (error "unexpected handle-delim value: " | |
205 | handle-delim)))))))) | |
206 | ||
207 | (define (read-delimited delims . args) | |
208 | (let* ((port (if (pair? args) | |
209 | (let ((pt (car args))) | |
210 | (set! args (cdr args)) | |
211 | pt) | |
212 | (current-input-port))) | |
213 | (handle-delim (if (pair? args) | |
214 | (car args) | |
215 | 'trim))) | |
216 | (let loop ((substrings ()) | |
217 | (total-chars 0) | |
218 | (buf-size 100)) ; doubled each time through. | |
219 | (let* ((buf (make-string buf-size)) | |
220 | (rv (%read-delimited! delims | |
221 | buf | |
222 | (not (eq? handle-delim 'peek)) | |
223 | port)) | |
224 | (terminator (car rv)) | |
225 | (nchars (cdr rv)) | |
226 | (join-substrings | |
227 | (lambda () | |
228 | (apply string-append | |
229 | (reverse | |
230 | (cons (if (and (eq? handle-delim 'concat) | |
231 | (not (eof-object? terminator))) | |
232 | (string terminator) | |
233 | "") | |
234 | (cons (make-shared-substring buf 0 nchars) | |
235 | substrings)))))) | |
236 | (new-total (+ total-chars nchars))) | |
237 | (cond ((not terminator) | |
238 | ;; buffer filled. | |
239 | (loop (cons (substring buf 0 nchars) substrings) | |
240 | new-total | |
241 | (* buf-size 2))) | |
242 | ((eof-object? terminator) | |
243 | (if (zero? new-total) | |
244 | (if (eq? handle-delim 'split) | |
245 | (cons terminator terminator) | |
246 | terminator) | |
247 | (if (eq? handle-delim 'split) | |
248 | (cons (join-substrings) terminator) | |
249 | (join-substrings)))) | |
250 | (else | |
251 | (case handle-delim | |
252 | ((trim peek concat) (join-substrings)) | |
253 | ((split) (cons (join-substrings) terminator)) | |
254 | (else (error "unexpected handle-delim value: " | |
255 | handle-delim))))))))) | |
256 | ||
257 | (define (read-line . args) | |
258 | (apply read-delimited scm-line-incrementors args)) | |
259 | ||
260 | \f | |
0f2d19dd JB |
261 | ;;; {Arrays} |
262 | ;;; | |
263 | ||
264 | (begin | |
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) | |
c2132276 | 269 | (uniform-array-set1! u o i)) |
0f2d19dd JB |
270 | (define uniform-vector-fill! array-fill!) |
271 | (define uniform-vector-read! uniform-array-read!) | |
272 | (define uniform-vector-write uniform-array-write) | |
273 | ||
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)))) | |
285 | ||
286 | \f | |
287 | ;;; {Keywords} | |
288 | ;;; | |
289 | ||
290 | (define (symbol->keyword symbol) | |
291 | (make-keyword-from-dash-symbol (symbol-append '- symbol))) | |
292 | ||
293 | (define (keyword->symbol kw) | |
294 | (let ((sym (keyword-dash-symbol kw))) | |
295 | (string->symbol (substring sym 1 (length sym))))) | |
296 | ||
297 | (define (kw-arg-ref args kw) | |
298 | (let ((rem (member kw args))) | |
299 | (and rem (pair? (cdr rem)) (cadr rem)))) | |
300 | ||
301 | \f | |
fa7e9274 MV |
302 | |
303 | ;;; Printing structs | |
304 | ||
305 | ;; The printing of structures can be customized by setting the builtin | |
306 | ;; variable *struct-printer* to a procedure. A second dispatching | |
307 | ;; step is implemented here to allow for struct-type specific printing | |
308 | ;; procedures. | |
309 | ;; | |
310 | ;; A particular type of structures is characterized by its vtable. In | |
311 | ;; addition to some internal fields, such a vtable can contain | |
312 | ;; arbitrary user-defined fields. We use the first of these fields to | |
313 | ;; hold the specific printing procedure. To avoid breaking code that | |
314 | ;; already uses this first extra-field for some other purposes, we use | |
315 | ;; a unique tag to decide whether it really contains a structure | |
316 | ;; printer or not. | |
317 | ;; | |
318 | ;; XXX - Printing structures is probably fundamental enough that we | |
319 | ;; can simply hardcode the vtable slot convention and expect everyone | |
320 | ;; to obey it. | |
321 | ;; | |
322 | ;; A structure-type specific printer follows the same calling | |
323 | ;; convention as the builtin *struct-printer*. | |
324 | ||
325 | ;; A shorthand for one already hardcoded vtable convention | |
326 | ||
327 | (define (struct-layout s) | |
328 | (struct-ref (struct-vtable s) 0)) | |
329 | ||
330 | ;; This is our new convention for storing printing procedures | |
331 | ||
332 | (define %struct-printer-tag (cons '%struct-printer-tag #f)) | |
333 | ||
334 | (define (struct-printer s) | |
755da2fc | 335 | (let ((vtable (struct-vtable s))) |
52c5a23a JB |
336 | (and (> (string-length (struct-layout vtable)) |
337 | (* 2 struct-vtable-offset)) | |
755da2fc | 338 | (let ((p (struct-ref vtable struct-vtable-offset))) |
52c5a23a JB |
339 | (and (pair? p) |
340 | (eq? (car p) %struct-printer-tag) | |
755da2fc | 341 | (cdr p)))))) |
fa7e9274 MV |
342 | |
343 | (define (make-struct-printer printer) | |
344 | (cons %struct-printer-tag printer)) | |
345 | ||
346 | ;; Note: While the printer is extracted from a structure itself, it | |
347 | ;; has to be set in the vtable of the structure. | |
348 | ||
349 | (define (set-struct-printer-in-vtable! vtable printer) | |
350 | (struct-set! vtable struct-vtable-offset (make-struct-printer printer))) | |
351 | ||
352 | ;; The dispatcher | |
353 | ||
354 | (set! *struct-printer* (lambda (s p) | |
355 | (let ((printer (struct-printer s))) | |
356 | (and printer | |
357 | (printer s p))))) | |
358 | ||
359 | \f | |
0f2d19dd JB |
360 | ;;; {Records} |
361 | ;;; | |
362 | ||
fa7e9274 MV |
363 | ;; Printing records: by default, records are printed as |
364 | ;; | |
365 | ;; #<type-name field1: val1 field2: val2 ...> | |
366 | ;; | |
367 | ;; You can change that by giving a custom printing function to | |
368 | ;; MAKE-RECORD-TYPE (after the list of field symbols). This function | |
369 | ;; will be called like | |
370 | ;; | |
371 | ;; (<printer> object port) | |
372 | ;; | |
373 | ;; It should print OBJECT to PORT. | |
374 | ||
375 | ;; 0: printer, 1: type-name, 2: fields | |
376 | (define record-type-vtable | |
377 | (make-vtable-vtable "prprpr" 0 | |
378 | (make-struct-printer | |
379 | (lambda (s p) | |
380 | (cond ((eq? s record-type-vtable) | |
381 | (display "#<record-type-vtable>" p)) | |
382 | (else | |
383 | (display "#<record-type " p) | |
384 | (display (record-type-name s) p) | |
385 | (display ">" p))))))) | |
0f2d19dd JB |
386 | |
387 | (define (record-type? obj) | |
388 | (and (struct? obj) (eq? record-type-vtable (struct-vtable obj)))) | |
389 | ||
390 | (define (make-record-type type-name fields . opt) | |
8e693424 | 391 | (let ((printer-fn (and (pair? opt) (car opt)))) |
0f2d19dd | 392 | (let ((struct (make-struct record-type-vtable 0 |
c7c03b9f JB |
393 | (make-struct-layout |
394 | (apply symbol-append | |
395 | (map (lambda (f) "pw") fields))) | |
fa7e9274 MV |
396 | (make-struct-printer |
397 | (or printer-fn | |
398 | (lambda (s p) | |
399 | (display "#<" p) | |
400 | (display type-name p) | |
401 | (let loop ((fields fields) | |
402 | (off 0)) | |
403 | (cond | |
404 | ((not (null? fields)) | |
405 | (display " " p) | |
406 | (display (car fields) p) | |
407 | (display ": " p) | |
408 | (display (struct-ref s off) p) | |
409 | (loop (cdr fields) (+ 1 off))))) | |
410 | (display ">" p)))) | |
0f2d19dd JB |
411 | type-name |
412 | (copy-tree fields)))) | |
0f2d19dd JB |
413 | struct))) |
414 | ||
415 | (define (record-type-name obj) | |
416 | (if (record-type? obj) | |
fa7e9274 | 417 | (struct-ref obj (+ 1 struct-vtable-offset)) |
0f2d19dd JB |
418 | (error 'not-a-record-type obj))) |
419 | ||
420 | (define (record-type-fields obj) | |
421 | (if (record-type? obj) | |
fa7e9274 | 422 | (struct-ref obj (+ 2 struct-vtable-offset)) |
0f2d19dd JB |
423 | (error 'not-a-record-type obj))) |
424 | ||
425 | (define (record-constructor rtd . opt) | |
8e693424 | 426 | (let ((field-names (if (pair? opt) (car opt) (record-type-fields rtd)))) |
0f2d19dd JB |
427 | (eval `(lambda ,field-names |
428 | (make-struct ',rtd 0 ,@(map (lambda (f) | |
429 | (if (memq f field-names) | |
430 | f | |
431 | #f)) | |
432 | (record-type-fields rtd))))))) | |
433 | ||
434 | (define (record-predicate rtd) | |
435 | (lambda (obj) (and (struct? obj) (eq? rtd (struct-vtable obj))))) | |
436 | ||
437 | (define (record-accessor rtd field-name) | |
438 | (let* ((pos (list-index (record-type-fields rtd) field-name))) | |
439 | (if (not pos) | |
440 | (error 'no-such-field field-name)) | |
441 | (eval `(lambda (obj) | |
442 | (and (eq? ',rtd (record-type-descriptor obj)) | |
443 | (struct-ref obj ,pos)))))) | |
444 | ||
445 | (define (record-modifier rtd field-name) | |
446 | (let* ((pos (list-index (record-type-fields rtd) field-name))) | |
447 | (if (not pos) | |
448 | (error 'no-such-field field-name)) | |
449 | (eval `(lambda (obj val) | |
450 | (and (eq? ',rtd (record-type-descriptor obj)) | |
451 | (struct-set! obj ,pos val)))))) | |
452 | ||
453 | ||
454 | (define (record? obj) | |
455 | (and (struct? obj) (record-type? (struct-vtable obj)))) | |
456 | ||
457 | (define (record-type-descriptor obj) | |
458 | (if (struct? obj) | |
459 | (struct-vtable obj) | |
460 | (error 'not-a-record obj))) | |
461 | ||
21ed9efe MD |
462 | (provide 'record) |
463 | ||
0f2d19dd JB |
464 | \f |
465 | ;;; {Booleans} | |
466 | ;;; | |
467 | ||
468 | (define (->bool x) (not (not x))) | |
469 | ||
470 | \f | |
471 | ;;; {Symbols} | |
472 | ;;; | |
473 | ||
474 | (define (symbol-append . args) | |
475 | (string->symbol (apply string-append args))) | |
476 | ||
477 | (define (list->symbol . args) | |
478 | (string->symbol (apply list->string args))) | |
479 | ||
480 | (define (symbol . args) | |
481 | (string->symbol (apply string args))) | |
482 | ||
483 | (define (obarray-symbol-append ob . args) | |
484 | (string->obarray-symbol (apply string-append ob args))) | |
485 | ||
486 | (define obarray-gensym | |
487 | (let ((n -1)) | |
488 | (lambda (obarray . opt) | |
489 | (if (null? opt) | |
490 | (set! opt '(%%gensym))) | |
491 | (let loop ((proposed-name (apply string-append opt))) | |
492 | (if (string->obarray-symbol obarray proposed-name #t) | |
493 | (loop (apply string-append (append opt (begin (set! n (1+ n)) (list (number->string n)))))) | |
494 | (string->obarray-symbol obarray proposed-name)))))) | |
495 | ||
496 | (define (gensym . args) (apply obarray-gensym #f args)) | |
497 | ||
498 | \f | |
499 | ;;; {Lists} | |
500 | ;;; | |
501 | ||
502 | (define (list-index l k) | |
503 | (let loop ((n 0) | |
504 | (l l)) | |
505 | (and (not (null? l)) | |
506 | (if (eq? (car l) k) | |
507 | n | |
508 | (loop (+ n 1) (cdr l)))))) | |
509 | ||
75fd4fb6 JB |
510 | (define (make-list n . init) |
511 | (if (pair? init) (set! init (car init))) | |
0f2d19dd JB |
512 | (let loop ((answer '()) |
513 | (n n)) | |
514 | (if (<= n 0) | |
515 | answer | |
516 | (loop (cons init answer) (- n 1))))) | |
517 | ||
518 | ||
519 | \f | |
520 | ;;; {and-map, or-map, and map-in-order} | |
521 | ;;; | |
522 | ;;; (and-map fn lst) is like (and (fn (car lst)) (fn (cadr lst)) (fn...) ...) | |
523 | ;;; (or-map fn lst) is like (or (fn (car lst)) (fn (cadr lst)) (fn...) ...) | |
524 | ;;; (map-in-order fn lst) is like (map fn lst) but definately in order of lst. | |
525 | ;;; | |
526 | ||
527 | ;; and-map f l | |
528 | ;; | |
529 | ;; Apply f to successive elements of l until exhaustion or f returns #f. | |
530 | ;; If returning early, return #f. Otherwise, return the last value returned | |
531 | ;; by f. If f has never been called because l is empty, return #t. | |
532 | ;; | |
533 | (define (and-map f lst) | |
534 | (let loop ((result #t) | |
535 | (l lst)) | |
536 | (and result | |
537 | (or (and (null? l) | |
538 | result) | |
539 | (loop (f (car l)) (cdr l)))))) | |
540 | ||
541 | ;; or-map f l | |
542 | ;; | |
543 | ;; Apply f to successive elements of l until exhaustion or while f returns #f. | |
544 | ;; If returning early, return the return value of f. | |
545 | ;; | |
546 | (define (or-map f lst) | |
547 | (let loop ((result #f) | |
548 | (l lst)) | |
549 | (or result | |
550 | (and (not (null? l)) | |
551 | (loop (f (car l)) (cdr l)))))) | |
552 | ||
553 | ;; map-in-order | |
554 | ;; | |
555 | ;; Like map, but guaranteed to process the list in order. | |
556 | ;; | |
557 | (define (map-in-order fn l) | |
558 | (if (null? l) | |
559 | '() | |
560 | (cons (fn (car l)) | |
561 | (map-in-order fn (cdr l))))) | |
562 | ||
563 | \f | |
59e1116d MD |
564 | ;;; {Hooks} |
565 | (define (run-hooks hook) | |
566 | (for-each (lambda (thunk) (thunk)) hook)) | |
567 | ||
568 | (define add-hook! | |
569 | (procedure->macro | |
570 | (lambda (exp env) | |
571 | `(let ((thunk ,(caddr exp))) | |
572 | (if (not (memq thunk ,(cadr exp))) | |
573 | (set! ,(cadr exp) | |
574 | (cons thunk ,(cadr exp)))))))) | |
575 | ||
576 | \f | |
0f2d19dd JB |
577 | ;;; {Files} |
578 | ;;; !!!! these should be implemented using Tcl commands, not fports. | |
579 | ;;; | |
580 | ||
6fa8995c GH |
581 | (define (feature? feature) |
582 | (and (memq feature *features*) #t)) | |
583 | ||
3afb28ce GH |
584 | ;; Using the vector returned by stat directly is probably not a good |
585 | ;; idea (it could just as well be a record). Hence some accessors. | |
586 | (define (stat:dev f) (vector-ref f 0)) | |
587 | (define (stat:ino f) (vector-ref f 1)) | |
588 | (define (stat:mode f) (vector-ref f 2)) | |
589 | (define (stat:nlink f) (vector-ref f 3)) | |
590 | (define (stat:uid f) (vector-ref f 4)) | |
591 | (define (stat:gid f) (vector-ref f 5)) | |
592 | (define (stat:rdev f) (vector-ref f 6)) | |
593 | (define (stat:size f) (vector-ref f 7)) | |
594 | (define (stat:atime f) (vector-ref f 8)) | |
595 | (define (stat:mtime f) (vector-ref f 9)) | |
596 | (define (stat:ctime f) (vector-ref f 10)) | |
597 | (define (stat:blksize f) (vector-ref f 11)) | |
598 | (define (stat:blocks f) (vector-ref f 12)) | |
599 | ||
600 | ;; derived from stat mode. | |
601 | (define (stat:type f) (vector-ref f 13)) | |
602 | (define (stat:perms f) (vector-ref f 14)) | |
603 | ||
6fa8995c GH |
604 | (define file-exists? |
605 | (if (feature? 'posix) | |
606 | (lambda (str) | |
607 | (access? str F_OK)) | |
608 | (lambda (str) | |
609 | (let ((port (catch 'system-error (lambda () (open-file str OPEN_READ)) | |
610 | (lambda args #f)))) | |
611 | (if port (begin (close-port port) #t) | |
612 | #f))))) | |
613 | ||
614 | (define file-is-directory? | |
615 | (if (feature? 'i/o-extensions) | |
616 | (lambda (str) | |
3afb28ce | 617 | (eq? (stat:type (stat str)) 'directory)) |
6fa8995c GH |
618 | (lambda (str) |
619 | (display str) | |
620 | (newline) | |
621 | (let ((port (catch 'system-error | |
622 | (lambda () (open-file (string-append str "/.") | |
623 | OPEN_READ)) | |
624 | (lambda args #f)))) | |
625 | (if port (begin (close-port port) #t) | |
626 | #f))))) | |
0f2d19dd JB |
627 | |
628 | (define (has-suffix? str suffix) | |
629 | (let ((sufl (string-length suffix)) | |
630 | (sl (string-length str))) | |
631 | (and (> sl sufl) | |
632 | (string=? (substring str (- sl sufl) sl) suffix)))) | |
633 | ||
0f2d19dd JB |
634 | \f |
635 | ;;; {Error Handling} | |
636 | ;;; | |
637 | ||
0f2d19dd | 638 | (define (error . args) |
21ed9efe | 639 | (save-stack) |
2194b6f0 | 640 | (if (null? args) |
5552355a | 641 | (scm-error 'misc-error #f "?" #f #f) |
2194b6f0 GH |
642 | (let loop ((msg "%s") |
643 | (rest (cdr args))) | |
644 | (if (not (null? rest)) | |
645 | (loop (string-append msg " %S") | |
646 | (cdr rest)) | |
5552355a | 647 | (scm-error 'misc-error #f msg args #f))))) |
be2d2c70 | 648 | |
1349bd53 | 649 | ;; bad-throw is the hook that is called upon a throw to a an unhandled |
9a0d70e2 GH |
650 | ;; key (unless the throw has four arguments, in which case |
651 | ;; it's usually interpreted as an error throw.) | |
652 | ;; If the key has a default handler (a throw-handler-default property), | |
0f2d19dd JB |
653 | ;; it is applied to the throw. |
654 | ;; | |
1349bd53 | 655 | (define (bad-throw key . args) |
0f2d19dd JB |
656 | (let ((default (symbol-property key 'throw-handler-default))) |
657 | (or (and default (apply default key args)) | |
2194b6f0 | 658 | (apply error "unhandled-exception:" key args)))) |
0f2d19dd | 659 | |
0f2d19dd | 660 | \f |
44cf1f0f JB |
661 | ;;; {Non-polymorphic versions of POSIX functions} |
662 | ||
02b754d3 GH |
663 | (define (getgrnam name) (getgr name)) |
664 | (define (getgrgid id) (getgr id)) | |
665 | (define (gethostbyaddr addr) (gethost addr)) | |
666 | (define (gethostbyname name) (gethost name)) | |
667 | (define (getnetbyaddr addr) (getnet addr)) | |
668 | (define (getnetbyname name) (getnet name)) | |
669 | (define (getprotobyname name) (getproto name)) | |
670 | (define (getprotobynumber addr) (getproto addr)) | |
671 | (define (getpwnam name) (getpw name)) | |
672 | (define (getpwuid uid) (getpw uid)) | |
920235cc GH |
673 | (define (getservbyname name proto) (getserv name proto)) |
674 | (define (getservbyport port proto) (getserv port proto)) | |
0f2d19dd JB |
675 | (define (endgrent) (setgr)) |
676 | (define (endhostent) (sethost)) | |
677 | (define (endnetent) (setnet)) | |
678 | (define (endprotoent) (setproto)) | |
679 | (define (endpwent) (setpw)) | |
680 | (define (endservent) (setserv)) | |
02b754d3 GH |
681 | (define (getgrent) (getgr)) |
682 | (define (gethostent) (gethost)) | |
683 | (define (getnetent) (getnet)) | |
684 | (define (getprotoent) (getproto)) | |
685 | (define (getpwent) (getpw)) | |
686 | (define (getservent) (getserv)) | |
0f2d19dd | 687 | (define (reopen-file . args) (apply freopen args)) |
bce074ee GH |
688 | (define (setgrent) (setgr #f)) |
689 | (define (sethostent) (sethost #t)) | |
690 | (define (setnetent) (setnet #t)) | |
691 | (define (setprotoent) (setproto #t)) | |
692 | (define (setpwent) (setpw #t)) | |
693 | (define (setservent) (setserv #t)) | |
694 | ||
695 | (define (passwd:name obj) (vector-ref obj 0)) | |
696 | (define (passwd:passwd obj) (vector-ref obj 1)) | |
697 | (define (passwd:uid obj) (vector-ref obj 2)) | |
698 | (define (passwd:gid obj) (vector-ref obj 3)) | |
699 | (define (passwd:gecos obj) (vector-ref obj 4)) | |
700 | (define (passwd:dir obj) (vector-ref obj 5)) | |
701 | (define (passwd:shell obj) (vector-ref obj 6)) | |
702 | ||
703 | (define (group:name obj) (vector-ref obj 0)) | |
704 | (define (group:passwd obj) (vector-ref obj 1)) | |
705 | (define (group:gid obj) (vector-ref obj 2)) | |
706 | (define (group:mem obj) (vector-ref obj 3)) | |
707 | ||
708 | (define (hostent:name obj) (vector-ref obj 0)) | |
709 | (define (hostent:aliases obj) (vector-ref obj 1)) | |
710 | (define (hostent:addrtype obj) (vector-ref obj 2)) | |
711 | (define (hostent:length obj) (vector-ref obj 3)) | |
712 | (define (hostent:addr-list obj) (vector-ref obj 4)) | |
713 | ||
714 | (define (netent:name obj) (vector-ref obj 0)) | |
715 | (define (netent:aliases obj) (vector-ref obj 1)) | |
9337637f GH |
716 | (define (netent:addrtype obj) (vector-ref obj 2)) |
717 | (define (netent:net obj) (vector-ref obj 3)) | |
bce074ee GH |
718 | |
719 | (define (protoent:name obj) (vector-ref obj 0)) | |
720 | (define (protoent:aliases obj) (vector-ref obj 1)) | |
721 | (define (protoent:proto obj) (vector-ref obj 2)) | |
722 | ||
723 | (define (servent:name obj) (vector-ref obj 0)) | |
724 | (define (servent:aliases obj) (vector-ref obj 1)) | |
9337637f GH |
725 | (define (servent:port obj) (vector-ref obj 2)) |
726 | (define (servent:proto obj) (vector-ref obj 3)) | |
727 | ||
728 | (define (sockaddr:fam obj) (vector-ref obj 0)) | |
729 | (define (sockaddr:path obj) (vector-ref obj 1)) | |
730 | (define (sockaddr:addr obj) (vector-ref obj 1)) | |
731 | (define (sockaddr:port obj) (vector-ref obj 2)) | |
732 | ||
733 | (define (utsname:sysname obj) (vector-ref obj 0)) | |
734 | (define (utsname:nodename obj) (vector-ref obj 1)) | |
735 | (define (utsname:release obj) (vector-ref obj 2)) | |
736 | (define (utsname:version obj) (vector-ref obj 3)) | |
737 | (define (utsname:machine obj) (vector-ref obj 4)) | |
bce074ee | 738 | |
708bf0f3 GH |
739 | (define (tm:sec obj) (vector-ref obj 0)) |
740 | (define (tm:min obj) (vector-ref obj 1)) | |
741 | (define (tm:hour obj) (vector-ref obj 2)) | |
742 | (define (tm:mday obj) (vector-ref obj 3)) | |
743 | (define (tm:mon obj) (vector-ref obj 4)) | |
744 | (define (tm:year obj) (vector-ref obj 5)) | |
745 | (define (tm:wday obj) (vector-ref obj 6)) | |
746 | (define (tm:yday obj) (vector-ref obj 7)) | |
747 | (define (tm:isdst obj) (vector-ref obj 8)) | |
748 | (define (tm:gmtoff obj) (vector-ref obj 9)) | |
749 | (define (tm:zone obj) (vector-ref obj 10)) | |
750 | ||
751 | (define (set-tm:sec obj val) (vector-set! obj 0 val)) | |
752 | (define (set-tm:min obj val) (vector-set! obj 1 val)) | |
753 | (define (set-tm:hour obj val) (vector-set! obj 2 val)) | |
754 | (define (set-tm:mday obj val) (vector-set! obj 3 val)) | |
755 | (define (set-tm:mon obj val) (vector-set! obj 4 val)) | |
756 | (define (set-tm:year obj val) (vector-set! obj 5 val)) | |
757 | (define (set-tm:wday obj val) (vector-set! obj 6 val)) | |
758 | (define (set-tm:yday obj val) (vector-set! obj 7 val)) | |
759 | (define (set-tm:isdst obj val) (vector-set! obj 8 val)) | |
760 | (define (set-tm:gmtoff obj val) (vector-set! obj 9 val)) | |
761 | (define (set-tm:zone obj val) (vector-set! obj 10 val)) | |
762 | ||
6afcd3b2 GH |
763 | (define (tms:clock obj) (vector-ref obj 0)) |
764 | (define (tms:utime obj) (vector-ref obj 1)) | |
765 | (define (tms:stime obj) (vector-ref obj 2)) | |
766 | (define (tms:cutime obj) (vector-ref obj 3)) | |
767 | (define (tms:cstime obj) (vector-ref obj 4)) | |
768 | ||
bce074ee GH |
769 | (define (file-position . args) (apply ftell args)) |
770 | (define (file-set-position . args) (apply fseek args)) | |
8b13c6b3 | 771 | |
708bf0f3 GH |
772 | (define (open-input-pipe command) (open-pipe command OPEN_READ)) |
773 | (define (open-output-pipe command) (open-pipe command OPEN_WRITE)) | |
774 | ||
e38303a2 GH |
775 | (define (move->fdes fd/port fd) |
776 | (cond ((integer? fd/port) | |
7a6f1ffa | 777 | (dup->fdes fd/port fd) |
e38303a2 GH |
778 | (close fd/port) |
779 | fd) | |
780 | (else | |
781 | (primitive-move->fdes fd/port fd) | |
782 | (set-port-revealed! fd/port 1) | |
783 | fd/port))) | |
8b13c6b3 GH |
784 | |
785 | (define (release-port-handle port) | |
786 | (let ((revealed (port-revealed port))) | |
787 | (if (> revealed 0) | |
788 | (set-port-revealed! port (- revealed 1))))) | |
0f2d19dd | 789 | |
e38303a2 | 790 | (define (dup->port port/fd mode . maybe-fd) |
7a6f1ffa | 791 | (let ((port (fdopen (apply dup->fdes port/fd maybe-fd) |
e38303a2 GH |
792 | mode))) |
793 | (if (pair? maybe-fd) | |
794 | (set-port-revealed! port 1)) | |
795 | port)) | |
796 | ||
797 | (define (dup->inport port/fd . maybe-fd) | |
798 | (apply dup->port port/fd "r" maybe-fd)) | |
799 | ||
800 | (define (dup->outport port/fd . maybe-fd) | |
801 | (apply dup->port port/fd "w" maybe-fd)) | |
802 | ||
e38303a2 GH |
803 | (define (dup port/fd . maybe-fd) |
804 | (if (integer? port/fd) | |
805 | (apply dup->fdes port/fd maybe-fd) | |
806 | (apply dup->port port/fd (port-mode port/fd) maybe-fd))) | |
807 | ||
808 | (define (duplicate-port port modes) | |
809 | (dup->port port modes)) | |
810 | ||
811 | (define (fdes->inport fdes) | |
812 | (let loop ((rest-ports (fdes->ports fdes))) | |
813 | (cond ((null? rest-ports) | |
814 | (let ((result (fdopen fdes "r"))) | |
815 | (set-port-revealed! result 1) | |
816 | result)) | |
817 | ((input-port? (car rest-ports)) | |
818 | (set-port-revealed! (car rest-ports) | |
819 | (+ (port-revealed (car rest-ports)) 1)) | |
820 | (car rest-ports)) | |
821 | (else | |
822 | (loop (cdr rest-ports)))))) | |
823 | ||
824 | (define (fdes->outport fdes) | |
825 | (let loop ((rest-ports (fdes->ports fdes))) | |
826 | (cond ((null? rest-ports) | |
827 | (let ((result (fdopen fdes "w"))) | |
828 | (set-port-revealed! result 1) | |
829 | result)) | |
830 | ((output-port? (car rest-ports)) | |
831 | (set-port-revealed! (car rest-ports) | |
832 | (+ (port-revealed (car rest-ports)) 1)) | |
833 | (car rest-ports)) | |
834 | (else | |
835 | (loop (cdr rest-ports)))))) | |
836 | ||
837 | (define (port->fdes port) | |
838 | (set-port-revealed! port (+ (port-revealed port) 1)) | |
839 | (fileno port)) | |
840 | ||
956055a9 GH |
841 | (define (setenv name value) |
842 | (if value | |
843 | (putenv (string-append name "=" value)) | |
844 | (putenv name))) | |
845 | ||
0f2d19dd JB |
846 | \f |
847 | ;;; {Load Paths} | |
848 | ;;; | |
849 | ||
0f2d19dd JB |
850 | ;;; Here for backward compatability |
851 | ;; | |
852 | (define scheme-file-suffix (lambda () ".scm")) | |
853 | ||
3cab8392 JB |
854 | (define (in-vicinity vicinity file) |
855 | (let ((tail (let ((len (string-length vicinity))) | |
856 | (if (zero? len) #f | |
857 | (string-ref vicinity (- len 1)))))) | |
858 | (string-append vicinity | |
859 | (if (eq? tail #\/) "" "/") | |
860 | file))) | |
02ceadb8 | 861 | |
0f2d19dd | 862 | \f |
ef00e7f4 JB |
863 | ;;; {Help for scm_shell} |
864 | ;;; The argument-processing code used by Guile-based shells generates | |
865 | ;;; Scheme code based on the argument list. This page contains help | |
866 | ;;; functions for the code it generates. | |
867 | ||
ef00e7f4 JB |
868 | (define (command-line) (program-arguments)) |
869 | ||
5aa7fe69 JB |
870 | ;; This is mostly for the internal use of the code generated by |
871 | ;; scm_compile_shell_switches. | |
ef00e7f4 JB |
872 | (define (load-user-init) |
873 | (define (has-init? dir) | |
874 | (let ((path (in-vicinity dir ".guile"))) | |
875 | (catch 'system-error | |
876 | (lambda () | |
877 | (let ((stats (stat path))) | |
878 | (if (not (eq? (stat:type stats) 'directory)) | |
879 | path))) | |
880 | (lambda dummy #f)))) | |
4cd2a3e6 JB |
881 | (let ((path (or (has-init? (or (getenv "HOME") "/")) |
882 | (has-init? (passwd:dir (getpw (getuid))))))) | |
ef00e7f4 JB |
883 | (if path (primitive-load path)))) |
884 | ||
885 | \f | |
a06181a2 JB |
886 | ;;; {Loading by paths} |
887 | ||
888 | ;;; Load a Scheme source file named NAME, searching for it in the | |
889 | ;;; directories listed in %load-path, and applying each of the file | |
890 | ;;; name extensions listed in %load-extensions. | |
891 | (define (load-from-path name) | |
892 | (start-stack 'load-stack | |
75a97b92 | 893 | (primitive-load-path name))) |
0f2d19dd | 894 | |
5552355a | 895 | |
0f2d19dd | 896 | \f |
0f2d19dd JB |
897 | ;;; {Transcendental Functions} |
898 | ;;; | |
899 | ;;; Derived from "Transcen.scm", Complex trancendental functions for SCM. | |
900 | ;;; Copyright (C) 1992, 1993 Jerry D. Hedden. | |
901 | ;;; See the file `COPYING' for terms applying to this program. | |
902 | ;;; | |
903 | ||
904 | (define (exp z) | |
905 | (if (real? z) ($exp z) | |
906 | (make-polar ($exp (real-part z)) (imag-part z)))) | |
907 | ||
908 | (define (log z) | |
909 | (if (and (real? z) (>= z 0)) | |
910 | ($log z) | |
911 | (make-rectangular ($log (magnitude z)) (angle z)))) | |
912 | ||
913 | (define (sqrt z) | |
914 | (if (real? z) | |
915 | (if (negative? z) (make-rectangular 0 ($sqrt (- z))) | |
916 | ($sqrt z)) | |
917 | (make-polar ($sqrt (magnitude z)) (/ (angle z) 2)))) | |
918 | ||
919 | (define expt | |
920 | (let ((integer-expt integer-expt)) | |
921 | (lambda (z1 z2) | |
922 | (cond ((exact? z2) | |
923 | (integer-expt z1 z2)) | |
924 | ((and (real? z2) (real? z1) (>= z1 0)) | |
925 | ($expt z1 z2)) | |
926 | (else | |
927 | (exp (* z2 (log z1)))))))) | |
928 | ||
929 | (define (sinh z) | |
930 | (if (real? z) ($sinh z) | |
931 | (let ((x (real-part z)) (y (imag-part z))) | |
932 | (make-rectangular (* ($sinh x) ($cos y)) | |
933 | (* ($cosh x) ($sin y)))))) | |
934 | (define (cosh z) | |
935 | (if (real? z) ($cosh z) | |
936 | (let ((x (real-part z)) (y (imag-part z))) | |
937 | (make-rectangular (* ($cosh x) ($cos y)) | |
938 | (* ($sinh x) ($sin y)))))) | |
939 | (define (tanh z) | |
940 | (if (real? z) ($tanh z) | |
941 | (let* ((x (* 2 (real-part z))) | |
942 | (y (* 2 (imag-part z))) | |
943 | (w (+ ($cosh x) ($cos y)))) | |
944 | (make-rectangular (/ ($sinh x) w) (/ ($sin y) w))))) | |
945 | ||
946 | (define (asinh z) | |
947 | (if (real? z) ($asinh z) | |
948 | (log (+ z (sqrt (+ (* z z) 1)))))) | |
949 | ||
950 | (define (acosh z) | |
951 | (if (and (real? z) (>= z 1)) | |
952 | ($acosh z) | |
953 | (log (+ z (sqrt (- (* z z) 1)))))) | |
954 | ||
955 | (define (atanh z) | |
956 | (if (and (real? z) (> z -1) (< z 1)) | |
957 | ($atanh z) | |
958 | (/ (log (/ (+ 1 z) (- 1 z))) 2))) | |
959 | ||
960 | (define (sin z) | |
961 | (if (real? z) ($sin z) | |
962 | (let ((x (real-part z)) (y (imag-part z))) | |
963 | (make-rectangular (* ($sin x) ($cosh y)) | |
964 | (* ($cos x) ($sinh y)))))) | |
965 | (define (cos z) | |
966 | (if (real? z) ($cos z) | |
967 | (let ((x (real-part z)) (y (imag-part z))) | |
968 | (make-rectangular (* ($cos x) ($cosh y)) | |
969 | (- (* ($sin x) ($sinh y))))))) | |
970 | (define (tan z) | |
971 | (if (real? z) ($tan z) | |
972 | (let* ((x (* 2 (real-part z))) | |
973 | (y (* 2 (imag-part z))) | |
974 | (w (+ ($cos x) ($cosh y)))) | |
975 | (make-rectangular (/ ($sin x) w) (/ ($sinh y) w))))) | |
976 | ||
977 | (define (asin z) | |
978 | (if (and (real? z) (>= z -1) (<= z 1)) | |
979 | ($asin z) | |
980 | (* -i (asinh (* +i z))))) | |
981 | ||
982 | (define (acos z) | |
983 | (if (and (real? z) (>= z -1) (<= z 1)) | |
984 | ($acos z) | |
985 | (+ (/ (angle -1) 2) (* +i (asinh (* +i z)))))) | |
986 | ||
987 | (define (atan z . y) | |
988 | (if (null? y) | |
989 | (if (real? z) ($atan z) | |
990 | (/ (log (/ (- +i z) (+ +i z))) +2i)) | |
991 | ($atan2 z (car y)))) | |
992 | ||
993 | (set! abs magnitude) | |
994 | ||
65495221 GH |
995 | (define (log10 arg) |
996 | (/ (log arg) (log 10))) | |
997 | ||
0f2d19dd | 998 | \f |
0f2d19dd JB |
999 | |
1000 | ;;; {Reader Extensions} | |
1001 | ;;; | |
1002 | ||
1003 | ;;; Reader code for various "#c" forms. | |
1004 | ;;; | |
1005 | ||
fb6d1065 JB |
1006 | ;;; Parse the portion of a #/ list that comes after the first slash. |
1007 | (define (read-path-list-notation slash port) | |
1008 | (letrec | |
1009 | ||
1010 | ;; Is C a delimiter? | |
1011 | ((delimiter? (lambda (c) (or (eof-object? c) | |
1012 | (char-whitespace? c) | |
1013 | (string-index "()\";" c)))) | |
1014 | ||
1015 | ;; Read and return one component of a path list. | |
1016 | (read-component | |
1017 | (lambda () | |
1018 | (let loop ((reversed-chars '())) | |
1019 | (let ((c (peek-char port))) | |
1020 | (if (or (delimiter? c) | |
1021 | (char=? c #\/)) | |
1022 | (string->symbol (list->string (reverse reversed-chars))) | |
1023 | (loop (cons (read-char port) reversed-chars)))))))) | |
1024 | ||
1025 | ;; Read and return a path list. | |
1026 | (let loop ((reversed-path (list (read-component)))) | |
1027 | (let ((c (peek-char port))) | |
1028 | (if (and (char? c) (char=? c #\/)) | |
1029 | (begin | |
1030 | (read-char port) | |
1031 | (loop (cons (read-component) reversed-path))) | |
1032 | (reverse reversed-path)))))) | |
0f2d19dd | 1033 | |
75a97b92 GH |
1034 | (read-hash-extend #\' (lambda (c port) |
1035 | (read port))) | |
1036 | (read-hash-extend #\. (lambda (c port) | |
1037 | (eval (read port)))) | |
1038 | ||
1039 | (if (feature? 'array) | |
1040 | (begin | |
1041 | (let ((make-array-proc (lambda (template) | |
1042 | (lambda (c port) | |
1043 | (read:uniform-vector template port))))) | |
1044 | (for-each (lambda (char template) | |
1045 | (read-hash-extend char | |
1046 | (make-array-proc template))) | |
1047 | '(#\b #\a #\u #\e #\s #\i #\c) | |
1048 | '(#t #\a 1 -1 1.0 1/3 0+i))) | |
1049 | (let ((array-proc (lambda (c port) | |
1050 | (read:array c port)))) | |
1051 | (for-each (lambda (char) (read-hash-extend char array-proc)) | |
1052 | '(#\0 #\1 #\2 #\3 #\4 #\5 #\6 #\7 #\8 #\9))))) | |
1053 | ||
00c34e45 GH |
1054 | ;; pushed to the beginning of the alist since it's used more than the |
1055 | ;; others at present. | |
fb6d1065 | 1056 | (read-hash-extend #\/ read-path-list-notation) |
00c34e45 | 1057 | |
0f2d19dd JB |
1058 | (define (read:array digit port) |
1059 | (define chr0 (char->integer #\0)) | |
1060 | (let ((rank (let readnum ((val (- (char->integer digit) chr0))) | |
1061 | (if (char-numeric? (peek-char port)) | |
1062 | (readnum (+ (* 10 val) | |
1063 | (- (char->integer (read-char port)) chr0))) | |
1064 | val))) | |
1065 | (prot (if (eq? #\( (peek-char port)) | |
1066 | '() | |
1067 | (let ((c (read-char port))) | |
1068 | (case c ((#\b) #t) | |
1069 | ((#\a) #\a) | |
1070 | ((#\u) 1) | |
1071 | ((#\e) -1) | |
1072 | ((#\s) 1.0) | |
1073 | ((#\i) 1/3) | |
1074 | ((#\c) 0+i) | |
1075 | (else (error "read:array unknown option " c))))))) | |
1076 | (if (eq? (peek-char port) #\() | |
75a97b92 | 1077 | (list->uniform-array rank prot (read port)) |
0f2d19dd JB |
1078 | (error "read:array list not found")))) |
1079 | ||
1080 | (define (read:uniform-vector proto port) | |
1081 | (if (eq? #\( (peek-char port)) | |
75a97b92 | 1082 | (list->uniform-array 1 proto (read port)) |
0f2d19dd JB |
1083 | (error "read:uniform-vector list not found"))) |
1084 | ||
0f2d19dd JB |
1085 | \f |
1086 | ;;; {Command Line Options} | |
1087 | ;;; | |
1088 | ||
1089 | (define (get-option argv kw-opts kw-args return) | |
1090 | (cond | |
1091 | ((null? argv) | |
1092 | (return #f #f argv)) | |
1093 | ||
1094 | ((or (not (eq? #\- (string-ref (car argv) 0))) | |
1095 | (eq? (string-length (car argv)) 1)) | |
1096 | (return 'normal-arg (car argv) (cdr argv))) | |
1097 | ||
1098 | ((eq? #\- (string-ref (car argv) 1)) | |
1099 | (let* ((kw-arg-pos (or (string-index (car argv) #\=) | |
1100 | (string-length (car argv)))) | |
1101 | (kw (symbol->keyword (substring (car argv) 2 kw-arg-pos))) | |
1102 | (kw-opt? (member kw kw-opts)) | |
1103 | (kw-arg? (member kw kw-args)) | |
1104 | (arg (or (and (not (eq? kw-arg-pos (string-length (car argv)))) | |
1105 | (substring (car argv) | |
1106 | (+ kw-arg-pos 1) | |
1107 | (string-length (car argv)))) | |
1108 | (and kw-arg? | |
1109 | (begin (set! argv (cdr argv)) (car argv)))))) | |
1110 | (if (or kw-opt? kw-arg?) | |
1111 | (return kw arg (cdr argv)) | |
1112 | (return 'usage-error kw (cdr argv))))) | |
1113 | ||
1114 | (else | |
1115 | (let* ((char (substring (car argv) 1 2)) | |
1116 | (kw (symbol->keyword char))) | |
1117 | (cond | |
1118 | ||
1119 | ((member kw kw-opts) | |
1120 | (let* ((rest-car (substring (car argv) 2 (string-length (car argv)))) | |
1121 | (new-argv (if (= 0 (string-length rest-car)) | |
1122 | (cdr argv) | |
1123 | (cons (string-append "-" rest-car) (cdr argv))))) | |
1124 | (return kw #f new-argv))) | |
1125 | ||
1126 | ((member kw kw-args) | |
1127 | (let* ((rest-car (substring (car argv) 2 (string-length (car argv)))) | |
1128 | (arg (if (= 0 (string-length rest-car)) | |
1129 | (cadr argv) | |
1130 | rest-car)) | |
1131 | (new-argv (if (= 0 (string-length rest-car)) | |
1132 | (cddr argv) | |
1133 | (cdr argv)))) | |
1134 | (return kw arg new-argv))) | |
1135 | ||
1136 | (else (return 'usage-error kw argv))))))) | |
1137 | ||
1138 | (define (for-next-option proc argv kw-opts kw-args) | |
1139 | (let loop ((argv argv)) | |
1140 | (get-option argv kw-opts kw-args | |
1141 | (lambda (opt opt-arg argv) | |
1142 | (and opt (proc opt opt-arg argv loop)))))) | |
1143 | ||
1144 | (define (display-usage-report kw-desc) | |
1145 | (for-each | |
1146 | (lambda (kw) | |
1147 | (or (eq? (car kw) #t) | |
1148 | (eq? (car kw) 'else) | |
1149 | (let* ((opt-desc kw) | |
1150 | (help (cadr opt-desc)) | |
1151 | (opts (car opt-desc)) | |
1152 | (opts-proper (if (string? (car opts)) (cdr opts) opts)) | |
1153 | (arg-name (if (string? (car opts)) | |
1154 | (string-append "<" (car opts) ">") | |
1155 | "")) | |
1156 | (left-part (string-append | |
1157 | (with-output-to-string | |
1158 | (lambda () | |
1159 | (map (lambda (x) (display (keyword-symbol x)) (display " ")) | |
1160 | opts-proper))) | |
1161 | arg-name)) | |
1162 | (middle-part (if (and (< (length left-part) 30) | |
1163 | (< (length help) 40)) | |
1164 | (make-string (- 30 (length left-part)) #\ ) | |
1165 | "\n\t"))) | |
1166 | (display left-part) | |
1167 | (display middle-part) | |
1168 | (display help) | |
1169 | (newline)))) | |
1170 | kw-desc)) | |
1171 | ||
1172 | ||
1173 | ||
0f2d19dd JB |
1174 | (define (transform-usage-lambda cases) |
1175 | (let* ((raw-usage (delq! 'else (map car cases))) | |
1176 | (usage-sans-specials (map (lambda (x) | |
1177 | (or (and (not (list? x)) x) | |
1178 | (and (symbol? (car x)) #t) | |
1179 | (and (boolean? (car x)) #t) | |
1180 | x)) | |
1181 | raw-usage)) | |
ed440df5 | 1182 | (usage-desc (delq! #t usage-sans-specials)) |
0f2d19dd JB |
1183 | (kw-desc (map car usage-desc)) |
1184 | (kw-opts (apply append (map (lambda (x) (and (not (string? (car x))) x)) kw-desc))) | |
1185 | (kw-args (apply append (map (lambda (x) (and (string? (car x)) (cdr x))) kw-desc))) | |
1186 | (transmogrified-cases (map (lambda (case) | |
1187 | (cons (let ((opts (car case))) | |
1188 | (if (or (boolean? opts) (eq? 'else opts)) | |
1189 | opts | |
1190 | (cond | |
1191 | ((symbol? (car opts)) opts) | |
1192 | ((boolean? (car opts)) opts) | |
1193 | ((string? (caar opts)) (cdar opts)) | |
1194 | (else (car opts))))) | |
1195 | (cdr case))) | |
1196 | cases))) | |
1197 | `(let ((%display-usage (lambda () (display-usage-report ',usage-desc)))) | |
1198 | (lambda (%argv) | |
1199 | (let %next-arg ((%argv %argv)) | |
1200 | (get-option %argv | |
1201 | ',kw-opts | |
1202 | ',kw-args | |
1203 | (lambda (%opt %arg %new-argv) | |
1204 | (case %opt | |
1205 | ,@ transmogrified-cases)))))))) | |
1206 | ||
1207 | ||
1208 | \f | |
1209 | ||
1210 | ;;; {Low Level Modules} | |
1211 | ;;; | |
1212 | ;;; These are the low level data structures for modules. | |
1213 | ;;; | |
1214 | ;;; !!! warning: The interface to lazy binder procedures is going | |
1215 | ;;; to be changed in an incompatible way to permit all the basic | |
1216 | ;;; module ops to be virtualized. | |
1217 | ;;; | |
1218 | ;;; (make-module size use-list lazy-binding-proc) => module | |
1219 | ;;; module-{obarray,uses,binder}[|-set!] | |
1220 | ;;; (module? obj) => [#t|#f] | |
1221 | ;;; (module-locally-bound? module symbol) => [#t|#f] | |
1222 | ;;; (module-bound? module symbol) => [#t|#f] | |
1223 | ;;; (module-symbol-locally-interned? module symbol) => [#t|#f] | |
1224 | ;;; (module-symbol-interned? module symbol) => [#t|#f] | |
1225 | ;;; (module-local-variable module symbol) => [#<variable ...> | #f] | |
1226 | ;;; (module-variable module symbol) => [#<variable ...> | #f] | |
1227 | ;;; (module-symbol-binding module symbol opt-value) | |
1228 | ;;; => [ <obj> | opt-value | an error occurs ] | |
1229 | ;;; (module-make-local-var! module symbol) => #<variable...> | |
1230 | ;;; (module-add! module symbol var) => unspecified | |
1231 | ;;; (module-remove! module symbol) => unspecified | |
1232 | ;;; (module-for-each proc module) => unspecified | |
1233 | ;;; (make-scm-module) => module ; a lazy copy of the symhash module | |
1234 | ;;; (set-current-module module) => unspecified | |
1235 | ;;; (current-module) => #<module...> | |
1236 | ;;; | |
1237 | ;;; | |
1238 | ||
1239 | \f | |
44cf1f0f JB |
1240 | ;;; {Printing Modules} |
1241 | ;; This is how modules are printed. You can re-define it. | |
fa7e9274 MV |
1242 | ;; (Redefining is actually more complicated than simply redefining |
1243 | ;; %print-module because that would only change the binding and not | |
1244 | ;; the value stored in the vtable that determines how record are | |
1245 | ;; printed. Sigh.) | |
1246 | ||
1247 | (define (%print-module mod port) ; unused args: depth length style table) | |
0f2d19dd JB |
1248 | (display "#<" port) |
1249 | (display (or (module-kind mod) "module") port) | |
1250 | (let ((name (module-name mod))) | |
1251 | (if name | |
1252 | (begin | |
1253 | (display " " port) | |
1254 | (display name port)))) | |
1255 | (display " " port) | |
1256 | (display (number->string (object-address mod) 16) port) | |
1257 | (display ">" port)) | |
1258 | ||
1259 | ;; module-type | |
1260 | ;; | |
1261 | ;; A module is characterized by an obarray in which local symbols | |
1262 | ;; are interned, a list of modules, "uses", from which non-local | |
1263 | ;; bindings can be inherited, and an optional lazy-binder which | |
31d50456 | 1264 | ;; is a (CLOSURE module symbol) which, as a last resort, can provide |
0f2d19dd JB |
1265 | ;; bindings that would otherwise not be found locally in the module. |
1266 | ;; | |
1267 | (define module-type | |
31d50456 | 1268 | (make-record-type 'module '(obarray uses binder eval-closure name kind) |
8b718458 | 1269 | %print-module)) |
0f2d19dd | 1270 | |
8b718458 | 1271 | ;; make-module &opt size uses binder |
0f2d19dd | 1272 | ;; |
8b718458 JB |
1273 | ;; Create a new module, perhaps with a particular size of obarray, |
1274 | ;; initial uses list, or binding procedure. | |
0f2d19dd | 1275 | ;; |
0f2d19dd JB |
1276 | (define make-module |
1277 | (lambda args | |
0f2d19dd | 1278 | |
8b718458 JB |
1279 | (define (parse-arg index default) |
1280 | (if (> (length args) index) | |
1281 | (list-ref args index) | |
1282 | default)) | |
1283 | ||
1284 | (if (> (length args) 3) | |
1285 | (error "Too many args to make-module." args)) | |
0f2d19dd | 1286 | |
8b718458 JB |
1287 | (let ((size (parse-arg 0 1021)) |
1288 | (uses (parse-arg 1 '())) | |
1289 | (binder (parse-arg 2 #f))) | |
0f2d19dd | 1290 | |
8b718458 JB |
1291 | (if (not (integer? size)) |
1292 | (error "Illegal size to make-module." size)) | |
1293 | (if (not (and (list? uses) | |
1294 | (and-map module? uses))) | |
1295 | (error "Incorrect use list." uses)) | |
0f2d19dd JB |
1296 | (if (and binder (not (procedure? binder))) |
1297 | (error | |
1298 | "Lazy-binder expected to be a procedure or #f." binder)) | |
1299 | ||
8b718458 JB |
1300 | (let ((module (module-constructor (make-vector size '()) |
1301 | uses binder #f #f #f))) | |
1302 | ||
1303 | ;; We can't pass this as an argument to module-constructor, | |
1304 | ;; because we need it to close over a pointer to the module | |
1305 | ;; itself. | |
31d50456 | 1306 | (set-module-eval-closure! module |
8b718458 JB |
1307 | (lambda (symbol define?) |
1308 | (if define? | |
1309 | (module-make-local-var! module symbol) | |
1310 | (module-variable module symbol)))) | |
1311 | ||
1312 | module)))) | |
0f2d19dd | 1313 | |
8b718458 | 1314 | (define module-constructor (record-constructor module-type)) |
0f2d19dd JB |
1315 | (define module-obarray (record-accessor module-type 'obarray)) |
1316 | (define set-module-obarray! (record-modifier module-type 'obarray)) | |
1317 | (define module-uses (record-accessor module-type 'uses)) | |
1318 | (define set-module-uses! (record-modifier module-type 'uses)) | |
1319 | (define module-binder (record-accessor module-type 'binder)) | |
1320 | (define set-module-binder! (record-modifier module-type 'binder)) | |
31d50456 JB |
1321 | (define module-eval-closure (record-accessor module-type 'eval-closure)) |
1322 | (define set-module-eval-closure! (record-modifier module-type 'eval-closure)) | |
0f2d19dd JB |
1323 | (define module-name (record-accessor module-type 'name)) |
1324 | (define set-module-name! (record-modifier module-type 'name)) | |
1325 | (define module-kind (record-accessor module-type 'kind)) | |
1326 | (define set-module-kind! (record-modifier module-type 'kind)) | |
1327 | (define module? (record-predicate module-type)) | |
1328 | ||
8b718458 | 1329 | |
0f2d19dd | 1330 | (define (eval-in-module exp module) |
31d50456 | 1331 | (eval2 exp (module-eval-closure module))) |
0f2d19dd JB |
1332 | |
1333 | \f | |
1334 | ;;; {Module Searching in General} | |
1335 | ;;; | |
1336 | ;;; We sometimes want to look for properties of a symbol | |
1337 | ;;; just within the obarray of one module. If the property | |
1338 | ;;; holds, then it is said to hold ``locally'' as in, ``The symbol | |
1339 | ;;; DISPLAY is locally rebound in the module `safe-guile'.'' | |
1340 | ;;; | |
1341 | ;;; | |
1342 | ;;; Other times, we want to test for a symbol property in the obarray | |
1343 | ;;; of M and, if it is not found there, try each of the modules in the | |
1344 | ;;; uses list of M. This is the normal way of testing for some | |
1345 | ;;; property, so we state these properties without qualification as | |
1346 | ;;; in: ``The symbol 'fnord is interned in module M because it is | |
1347 | ;;; interned locally in module M2 which is a member of the uses list | |
1348 | ;;; of M.'' | |
1349 | ;;; | |
1350 | ||
1351 | ;; module-search fn m | |
1352 | ;; | |
1353 | ;; return the first non-#f result of FN applied to M and then to | |
1354 | ;; the modules in the uses of m, and so on recursively. If all applications | |
1355 | ;; return #f, then so does this function. | |
1356 | ;; | |
1357 | (define (module-search fn m v) | |
1358 | (define (loop pos) | |
1359 | (and (pair? pos) | |
1360 | (or (module-search fn (car pos) v) | |
1361 | (loop (cdr pos))))) | |
1362 | (or (fn m v) | |
1363 | (loop (module-uses m)))) | |
1364 | ||
1365 | ||
1366 | ;;; {Is a symbol bound in a module?} | |
1367 | ;;; | |
1368 | ;;; Symbol S in Module M is bound if S is interned in M and if the binding | |
1369 | ;;; of S in M has been set to some well-defined value. | |
1370 | ;;; | |
1371 | ||
1372 | ;; module-locally-bound? module symbol | |
1373 | ;; | |
1374 | ;; Is a symbol bound (interned and defined) locally in a given module? | |
1375 | ;; | |
1376 | (define (module-locally-bound? m v) | |
1377 | (let ((var (module-local-variable m v))) | |
1378 | (and var | |
1379 | (variable-bound? var)))) | |
1380 | ||
1381 | ;; module-bound? module symbol | |
1382 | ;; | |
1383 | ;; Is a symbol bound (interned and defined) anywhere in a given module | |
1384 | ;; or its uses? | |
1385 | ;; | |
1386 | (define (module-bound? m v) | |
1387 | (module-search module-locally-bound? m v)) | |
1388 | ||
1389 | ;;; {Is a symbol interned in a module?} | |
1390 | ;;; | |
1391 | ;;; Symbol S in Module M is interned if S occurs in | |
1392 | ;;; of S in M has been set to some well-defined value. | |
1393 | ;;; | |
1394 | ;;; It is possible to intern a symbol in a module without providing | |
1395 | ;;; an initial binding for the corresponding variable. This is done | |
1396 | ;;; with: | |
1397 | ;;; (module-add! module symbol (make-undefined-variable)) | |
1398 | ;;; | |
1399 | ;;; In that case, the symbol is interned in the module, but not | |
1400 | ;;; bound there. The unbound symbol shadows any binding for that | |
1401 | ;;; symbol that might otherwise be inherited from a member of the uses list. | |
1402 | ;;; | |
1403 | ||
1404 | (define (module-obarray-get-handle ob key) | |
1405 | ((if (symbol? key) hashq-get-handle hash-get-handle) ob key)) | |
1406 | ||
1407 | (define (module-obarray-ref ob key) | |
1408 | ((if (symbol? key) hashq-ref hash-ref) ob key)) | |
1409 | ||
1410 | (define (module-obarray-set! ob key val) | |
1411 | ((if (symbol? key) hashq-set! hash-set!) ob key val)) | |
1412 | ||
1413 | (define (module-obarray-remove! ob key) | |
1414 | ((if (symbol? key) hashq-remove! hash-remove!) ob key)) | |
1415 | ||
1416 | ;; module-symbol-locally-interned? module symbol | |
1417 | ;; | |
1418 | ;; is a symbol interned (not neccessarily defined) locally in a given module | |
1419 | ;; or its uses? Interned symbols shadow inherited bindings even if | |
1420 | ;; they are not themselves bound to a defined value. | |
1421 | ;; | |
1422 | (define (module-symbol-locally-interned? m v) | |
1423 | (not (not (module-obarray-get-handle (module-obarray m) v)))) | |
1424 | ||
1425 | ;; module-symbol-interned? module symbol | |
1426 | ;; | |
1427 | ;; is a symbol interned (not neccessarily defined) anywhere in a given module | |
1428 | ;; or its uses? Interned symbols shadow inherited bindings even if | |
1429 | ;; they are not themselves bound to a defined value. | |
1430 | ;; | |
1431 | (define (module-symbol-interned? m v) | |
1432 | (module-search module-symbol-locally-interned? m v)) | |
1433 | ||
1434 | ||
1435 | ;;; {Mapping modules x symbols --> variables} | |
1436 | ;;; | |
1437 | ||
1438 | ;; module-local-variable module symbol | |
1439 | ;; return the local variable associated with a MODULE and SYMBOL. | |
1440 | ;; | |
1441 | ;;; This function is very important. It is the only function that can | |
1442 | ;;; return a variable from a module other than the mutators that store | |
1443 | ;;; new variables in modules. Therefore, this function is the location | |
1444 | ;;; of the "lazy binder" hack. | |
1445 | ;;; | |
1446 | ;;; If symbol is defined in MODULE, and if the definition binds symbol | |
1447 | ;;; to a variable, return that variable object. | |
1448 | ;;; | |
1449 | ;;; If the symbols is not found at first, but the module has a lazy binder, | |
1450 | ;;; then try the binder. | |
1451 | ;;; | |
1452 | ;;; If the symbol is not found at all, return #f. | |
1453 | ;;; | |
1454 | (define (module-local-variable m v) | |
6fa8995c GH |
1455 | ; (caddr |
1456 | ; (list m v | |
0f2d19dd JB |
1457 | (let ((b (module-obarray-ref (module-obarray m) v))) |
1458 | (or (and (variable? b) b) | |
1459 | (and (module-binder m) | |
6fa8995c GH |
1460 | ((module-binder m) m v #f))))) |
1461 | ;)) | |
0f2d19dd JB |
1462 | |
1463 | ;; module-variable module symbol | |
1464 | ;; | |
1465 | ;; like module-local-variable, except search the uses in the | |
1466 | ;; case V is not found in M. | |
1467 | ;; | |
1468 | (define (module-variable m v) | |
1469 | (module-search module-local-variable m v)) | |
1470 | ||
1471 | ||
1472 | ;;; {Mapping modules x symbols --> bindings} | |
1473 | ;;; | |
1474 | ;;; These are similar to the mapping to variables, except that the | |
1475 | ;;; variable is dereferenced. | |
1476 | ;;; | |
1477 | ||
1478 | ;; module-symbol-binding module symbol opt-value | |
1479 | ;; | |
1480 | ;; return the binding of a variable specified by name within | |
1481 | ;; a given module, signalling an error if the variable is unbound. | |
1482 | ;; If the OPT-VALUE is passed, then instead of signalling an error, | |
1483 | ;; return OPT-VALUE. | |
1484 | ;; | |
1485 | (define (module-symbol-local-binding m v . opt-val) | |
1486 | (let ((var (module-local-variable m v))) | |
1487 | (if var | |
1488 | (variable-ref var) | |
1489 | (if (not (null? opt-val)) | |
1490 | (car opt-val) | |
1491 | (error "Locally unbound variable." v))))) | |
1492 | ||
1493 | ;; module-symbol-binding module symbol opt-value | |
1494 | ;; | |
1495 | ;; return the binding of a variable specified by name within | |
1496 | ;; a given module, signalling an error if the variable is unbound. | |
1497 | ;; If the OPT-VALUE is passed, then instead of signalling an error, | |
1498 | ;; return OPT-VALUE. | |
1499 | ;; | |
1500 | (define (module-symbol-binding m v . opt-val) | |
1501 | (let ((var (module-variable m v))) | |
1502 | (if var | |
1503 | (variable-ref var) | |
1504 | (if (not (null? opt-val)) | |
1505 | (car opt-val) | |
1506 | (error "Unbound variable." v))))) | |
1507 | ||
1508 | ||
1509 | \f | |
1510 | ;;; {Adding Variables to Modules} | |
1511 | ;;; | |
1512 | ;;; | |
1513 | ||
1514 | ||
1515 | ;; module-make-local-var! module symbol | |
1516 | ;; | |
1517 | ;; ensure a variable for V in the local namespace of M. | |
1518 | ;; If no variable was already there, then create a new and uninitialzied | |
1519 | ;; variable. | |
1520 | ;; | |
1521 | (define (module-make-local-var! m v) | |
1522 | (or (let ((b (module-obarray-ref (module-obarray m) v))) | |
1523 | (and (variable? b) b)) | |
1524 | (and (module-binder m) | |
1525 | ((module-binder m) m v #t)) | |
1526 | (begin | |
1527 | (let ((answer (make-undefined-variable v))) | |
1528 | (module-obarray-set! (module-obarray m) v answer) | |
1529 | answer)))) | |
1530 | ||
1531 | ;; module-add! module symbol var | |
1532 | ;; | |
1533 | ;; ensure a particular variable for V in the local namespace of M. | |
1534 | ;; | |
1535 | (define (module-add! m v var) | |
1536 | (if (not (variable? var)) | |
1537 | (error "Bad variable to module-add!" var)) | |
1538 | (module-obarray-set! (module-obarray m) v var)) | |
1539 | ||
1540 | ;; module-remove! | |
1541 | ;; | |
1542 | ;; make sure that a symbol is undefined in the local namespace of M. | |
1543 | ;; | |
1544 | (define (module-remove! m v) | |
1545 | (module-obarray-remove! (module-obarray m) v)) | |
1546 | ||
1547 | (define (module-clear! m) | |
1548 | (vector-fill! (module-obarray m) '())) | |
1549 | ||
1550 | ;; MODULE-FOR-EACH -- exported | |
1551 | ;; | |
1552 | ;; Call PROC on each symbol in MODULE, with arguments of (SYMBOL VARIABLE). | |
1553 | ;; | |
1554 | (define (module-for-each proc module) | |
1555 | (let ((obarray (module-obarray module))) | |
1556 | (do ((index 0 (+ index 1)) | |
1557 | (end (vector-length obarray))) | |
1558 | ((= index end)) | |
1559 | (for-each | |
1560 | (lambda (bucket) | |
1561 | (proc (car bucket) (cdr bucket))) | |
1562 | (vector-ref obarray index))))) | |
1563 | ||
1564 | ||
1565 | (define (module-map proc module) | |
1566 | (let* ((obarray (module-obarray module)) | |
1567 | (end (vector-length obarray))) | |
1568 | ||
1569 | (let loop ((i 0) | |
1570 | (answer '())) | |
1571 | (if (= i end) | |
1572 | answer | |
1573 | (loop (+ 1 i) | |
1574 | (append! | |
1575 | (map (lambda (bucket) | |
1576 | (proc (car bucket) (cdr bucket))) | |
1577 | (vector-ref obarray i)) | |
1578 | answer)))))) | |
1579 | \f | |
1580 | ||
1581 | ;;; {Low Level Bootstrapping} | |
1582 | ;;; | |
1583 | ||
1584 | ;; make-root-module | |
1585 | ||
21ed9efe | 1586 | ;; A root module uses the symhash table (the system's privileged |
0f2d19dd JB |
1587 | ;; obarray). Being inside a root module is like using SCM without |
1588 | ;; any module system. | |
1589 | ;; | |
1590 | ||
1591 | ||
31d50456 | 1592 | (define (root-module-closure m s define?) |
0f2d19dd JB |
1593 | (let ((bi (and (symbol-interned? #f s) |
1594 | (builtin-variable s)))) | |
1595 | (and bi | |
1596 | (or define? (variable-bound? bi)) | |
1597 | (begin | |
1598 | (module-add! m s bi) | |
1599 | bi)))) | |
1600 | ||
1601 | (define (make-root-module) | |
31d50456 | 1602 | (make-module 1019 '() root-module-closure)) |
0f2d19dd JB |
1603 | |
1604 | ||
1605 | ;; make-scm-module | |
1606 | ||
1607 | ;; An scm module is a module into which the lazy binder copies | |
1608 | ;; variable bindings from the system symhash table. The mapping is | |
1609 | ;; one way only; newly introduced bindings in an scm module are not | |
1610 | ;; copied back into the system symhash table (and can be used to override | |
1611 | ;; bindings from the symhash table). | |
1612 | ;; | |
1613 | ||
1614 | (define (make-scm-module) | |
8b718458 | 1615 | (make-module 1019 '() |
0f2d19dd JB |
1616 | (lambda (m s define?) |
1617 | (let ((bi (and (symbol-interned? #f s) | |
1618 | (builtin-variable s)))) | |
1619 | (and bi | |
1620 | (variable-bound? bi) | |
1621 | (begin | |
1622 | (module-add! m s bi) | |
1623 | bi)))))) | |
1624 | ||
1625 | ||
1626 | ||
1627 | ||
1628 | ;; the-module | |
1629 | ;; | |
1630 | (define the-module #f) | |
1631 | ||
1632 | ;; set-current-module module | |
1633 | ;; | |
1634 | ;; set the current module as viewed by the normalizer. | |
1635 | ;; | |
1636 | (define (set-current-module m) | |
1637 | (set! the-module m) | |
1638 | (if m | |
31d50456 JB |
1639 | (set! *top-level-lookup-closure* (module-eval-closure the-module)) |
1640 | (set! *top-level-lookup-closure* #f))) | |
0f2d19dd JB |
1641 | |
1642 | ||
1643 | ;; current-module | |
1644 | ;; | |
1645 | ;; return the current module as viewed by the normalizer. | |
1646 | ;; | |
1647 | (define (current-module) the-module) | |
1648 | \f | |
1649 | ;;; {Module-based Loading} | |
1650 | ;;; | |
1651 | ||
1652 | (define (save-module-excursion thunk) | |
1653 | (let ((inner-module (current-module)) | |
1654 | (outer-module #f)) | |
1655 | (dynamic-wind (lambda () | |
1656 | (set! outer-module (current-module)) | |
1657 | (set-current-module inner-module) | |
1658 | (set! inner-module #f)) | |
1659 | thunk | |
1660 | (lambda () | |
1661 | (set! inner-module (current-module)) | |
1662 | (set-current-module outer-module) | |
1663 | (set! outer-module #f))))) | |
1664 | ||
0f2d19dd JB |
1665 | (define basic-load load) |
1666 | ||
0f2d19dd JB |
1667 | (define (load-module . args) |
1668 | (save-module-excursion (lambda () (apply basic-load args)))) | |
1669 | ||
1670 | ||
1671 | \f | |
44cf1f0f | 1672 | ;;; {MODULE-REF -- exported} |
0f2d19dd JB |
1673 | ;; |
1674 | ;; Returns the value of a variable called NAME in MODULE or any of its | |
1675 | ;; used modules. If there is no such variable, then if the optional third | |
1676 | ;; argument DEFAULT is present, it is returned; otherwise an error is signaled. | |
1677 | ;; | |
1678 | (define (module-ref module name . rest) | |
1679 | (let ((variable (module-variable module name))) | |
1680 | (if (and variable (variable-bound? variable)) | |
1681 | (variable-ref variable) | |
1682 | (if (null? rest) | |
1683 | (error "No variable named" name 'in module) | |
1684 | (car rest) ; default value | |
1685 | )))) | |
1686 | ||
1687 | ;; MODULE-SET! -- exported | |
1688 | ;; | |
1689 | ;; Sets the variable called NAME in MODULE (or in a module that MODULE uses) | |
1690 | ;; to VALUE; if there is no such variable, an error is signaled. | |
1691 | ;; | |
1692 | (define (module-set! module name value) | |
1693 | (let ((variable (module-variable module name))) | |
1694 | (if variable | |
1695 | (variable-set! variable value) | |
1696 | (error "No variable named" name 'in module)))) | |
1697 | ||
1698 | ;; MODULE-DEFINE! -- exported | |
1699 | ;; | |
1700 | ;; Sets the variable called NAME in MODULE to VALUE; if there is no such | |
1701 | ;; variable, it is added first. | |
1702 | ;; | |
1703 | (define (module-define! module name value) | |
1704 | (let ((variable (module-local-variable module name))) | |
1705 | (if variable | |
1706 | (variable-set! variable value) | |
1707 | (module-add! module name (make-variable value name))))) | |
1708 | ||
ed218d98 MV |
1709 | ;; MODULE-DEFINED? -- exported |
1710 | ;; | |
1711 | ;; Return #t iff NAME is defined in MODULE (or in a module that MODULE | |
1712 | ;; uses) | |
1713 | ;; | |
1714 | (define (module-defined? module name) | |
1715 | (let ((variable (module-variable module name))) | |
1716 | (and variable (variable-bound? variable)))) | |
1717 | ||
0f2d19dd JB |
1718 | ;; MODULE-USE! module interface |
1719 | ;; | |
1720 | ;; Add INTERFACE to the list of interfaces used by MODULE. | |
1721 | ;; | |
1722 | (define (module-use! module interface) | |
1723 | (set-module-uses! module | |
1724 | (cons interface (delq! interface (module-uses module))))) | |
1725 | ||
1726 | \f | |
0f2d19dd JB |
1727 | ;;; {Recursive Namespaces} |
1728 | ;;; | |
1729 | ;;; | |
1730 | ;;; A hierarchical namespace emerges if we consider some module to be | |
1731 | ;;; root, and variables bound to modules as nested namespaces. | |
1732 | ;;; | |
1733 | ;;; The routines in this file manage variable names in hierarchical namespace. | |
1734 | ;;; Each variable name is a list of elements, looked up in successively nested | |
1735 | ;;; modules. | |
1736 | ;;; | |
0dd5491c | 1737 | ;;; (nested-ref some-root-module '(foo bar baz)) |
0f2d19dd JB |
1738 | ;;; => <value of a variable named baz in the module bound to bar in |
1739 | ;;; the module bound to foo in some-root-module> | |
1740 | ;;; | |
1741 | ;;; | |
1742 | ;;; There are: | |
1743 | ;;; | |
1744 | ;;; ;; a-root is a module | |
1745 | ;;; ;; name is a list of symbols | |
1746 | ;;; | |
0dd5491c MD |
1747 | ;;; nested-ref a-root name |
1748 | ;;; nested-set! a-root name val | |
1749 | ;;; nested-define! a-root name val | |
1750 | ;;; nested-remove! a-root name | |
0f2d19dd JB |
1751 | ;;; |
1752 | ;;; | |
1753 | ;;; (current-module) is a natural choice for a-root so for convenience there are | |
1754 | ;;; also: | |
1755 | ;;; | |
0dd5491c MD |
1756 | ;;; local-ref name == nested-ref (current-module) name |
1757 | ;;; local-set! name val == nested-set! (current-module) name val | |
1758 | ;;; local-define! name val == nested-define! (current-module) name val | |
1759 | ;;; local-remove! name == nested-remove! (current-module) name | |
0f2d19dd JB |
1760 | ;;; |
1761 | ||
1762 | ||
0dd5491c | 1763 | (define (nested-ref root names) |
0f2d19dd JB |
1764 | (let loop ((cur root) |
1765 | (elts names)) | |
1766 | (cond | |
1767 | ((null? elts) cur) | |
1768 | ((not (module? cur)) #f) | |
1769 | (else (loop (module-ref cur (car elts) #f) (cdr elts)))))) | |
1770 | ||
0dd5491c | 1771 | (define (nested-set! root names val) |
0f2d19dd JB |
1772 | (let loop ((cur root) |
1773 | (elts names)) | |
1774 | (if (null? (cdr elts)) | |
1775 | (module-set! cur (car elts) val) | |
1776 | (loop (module-ref cur (car elts)) (cdr elts))))) | |
1777 | ||
0dd5491c | 1778 | (define (nested-define! root names val) |
0f2d19dd JB |
1779 | (let loop ((cur root) |
1780 | (elts names)) | |
1781 | (if (null? (cdr elts)) | |
1782 | (module-define! cur (car elts) val) | |
1783 | (loop (module-ref cur (car elts)) (cdr elts))))) | |
1784 | ||
0dd5491c | 1785 | (define (nested-remove! root names) |
0f2d19dd JB |
1786 | (let loop ((cur root) |
1787 | (elts names)) | |
1788 | (if (null? (cdr elts)) | |
1789 | (module-remove! cur (car elts)) | |
1790 | (loop (module-ref cur (car elts)) (cdr elts))))) | |
1791 | ||
0dd5491c MD |
1792 | (define (local-ref names) (nested-ref (current-module) names)) |
1793 | (define (local-set! names val) (nested-set! (current-module) names val)) | |
1794 | (define (local-define names val) (nested-define! (current-module) names val)) | |
1795 | (define (local-remove names) (nested-remove! (current-module) names)) | |
0f2d19dd JB |
1796 | |
1797 | ||
1798 | \f | |
8bb7330c | 1799 | ;;; {The (app) module} |
0f2d19dd JB |
1800 | ;;; |
1801 | ;;; The root of conventionally named objects not directly in the top level. | |
1802 | ;;; | |
8bb7330c JB |
1803 | ;;; (app modules) |
1804 | ;;; (app modules guile) | |
0f2d19dd JB |
1805 | ;;; |
1806 | ;;; The directory of all modules and the standard root module. | |
1807 | ;;; | |
1808 | ||
1809 | (define (module-public-interface m) (module-ref m '%module-public-interface #f)) | |
1810 | (define (set-module-public-interface! m i) (module-define! m '%module-public-interface i)) | |
1811 | (define the-root-module (make-root-module)) | |
1812 | (define the-scm-module (make-scm-module)) | |
1813 | (set-module-public-interface! the-root-module the-scm-module) | |
1814 | (set-module-name! the-root-module 'the-root-module) | |
1815 | (set-module-name! the-scm-module 'the-scm-module) | |
1816 | ||
1817 | (set-current-module the-root-module) | |
1818 | ||
1819 | (define app (make-module 31)) | |
0dd5491c MD |
1820 | (local-define '(app modules) (make-module 31)) |
1821 | (local-define '(app modules guile) the-root-module) | |
0f2d19dd JB |
1822 | |
1823 | ;; (define-special-value '(app modules new-ws) (lambda () (make-scm-module))) | |
1824 | ||
0209ca9a | 1825 | (define (resolve-module name . maybe-autoload) |
0f2d19dd | 1826 | (let ((full-name (append '(app modules) name))) |
0dd5491c | 1827 | (let ((already (local-ref full-name))) |
0f2d19dd JB |
1828 | (or already |
1829 | (begin | |
0209ca9a | 1830 | (if (or (null? maybe-autoload) (car maybe-autoload)) |
d0cbd20c MV |
1831 | (or (try-module-autoload name) |
1832 | (try-module-dynamic-link name))) | |
0f2d19dd JB |
1833 | (make-modules-in (current-module) full-name)))))) |
1834 | ||
1835 | (define (beautify-user-module! module) | |
1836 | (if (not (module-public-interface module)) | |
1837 | (let ((interface (make-module 31))) | |
1838 | (set-module-name! interface (module-name module)) | |
1839 | (set-module-kind! interface 'interface) | |
1840 | (set-module-public-interface! module interface))) | |
cc7f066c MD |
1841 | (if (and (not (memq the-scm-module (module-uses module))) |
1842 | (not (eq? module the-root-module))) | |
0f2d19dd JB |
1843 | (set-module-uses! module (append (module-uses module) (list the-scm-module))))) |
1844 | ||
1845 | (define (make-modules-in module name) | |
1846 | (if (null? name) | |
1847 | module | |
1848 | (cond | |
1849 | ((module-ref module (car name) #f) => (lambda (m) (make-modules-in m (cdr name)))) | |
1850 | (else (let ((m (make-module 31))) | |
1851 | (set-module-kind! m 'directory) | |
1852 | (set-module-name! m (car name)) | |
1853 | (module-define! module (car name) m) | |
1854 | (make-modules-in m (cdr name))))))) | |
1855 | ||
1856 | (define (resolve-interface name) | |
1857 | (let ((module (resolve-module name))) | |
1858 | (and module (module-public-interface module)))) | |
1859 | ||
1860 | ||
1861 | (define %autoloader-developer-mode #t) | |
1862 | ||
1863 | (define (process-define-module args) | |
1864 | (let* ((module-id (car args)) | |
0209ca9a | 1865 | (module (resolve-module module-id #f)) |
0f2d19dd JB |
1866 | (kws (cdr args))) |
1867 | (beautify-user-module! module) | |
0209ca9a MV |
1868 | (let loop ((kws kws) |
1869 | (reversed-interfaces '())) | |
1870 | (if (null? kws) | |
1871 | (for-each (lambda (interface) | |
1872 | (module-use! module interface)) | |
1873 | reversed-interfaces) | |
04798288 MD |
1874 | (case (cond ((keyword? (car kws)) |
1875 | (keyword->symbol (car kws))) | |
90268b35 MD |
1876 | ((and (symbol? (car kws)) |
1877 | (eq? (string-ref (car kws) 0) #\:)) | |
04798288 | 1878 | (string->symbol (substring (car kws) 1))) |
90268b35 | 1879 | (else #f)) |
04798288 | 1880 | ((use-module) |
0209ca9a MV |
1881 | (if (not (pair? (cdr kws))) |
1882 | (error "unrecognized defmodule argument" kws)) | |
1883 | (let* ((used-name (cadr kws)) | |
1884 | (used-module (resolve-module used-name))) | |
1885 | (if (not (module-ref used-module '%module-public-interface #f)) | |
1886 | (begin | |
1887 | ((if %autoloader-developer-mode warn error) | |
1888 | "no code for module" (module-name used-module)) | |
1889 | (beautify-user-module! used-module))) | |
1890 | (let ((interface (module-public-interface used-module))) | |
1891 | (if (not interface) | |
1892 | (error "missing interface for use-module" used-module)) | |
1893 | (loop (cddr kws) (cons interface reversed-interfaces))))) | |
1894 | (else | |
1895 | (error "unrecognized defmodule argument" kws))))) | |
0f2d19dd JB |
1896 | module)) |
1897 | \f | |
44cf1f0f | 1898 | ;;; {Autoloading modules} |
0f2d19dd JB |
1899 | |
1900 | (define autoloads-in-progress '()) | |
1901 | ||
1902 | (define (try-module-autoload module-name) | |
6fa8995c | 1903 | |
0f2d19dd JB |
1904 | (define (sfx name) (string-append name (scheme-file-suffix))) |
1905 | (let* ((reverse-name (reverse module-name)) | |
1906 | (name (car reverse-name)) | |
1907 | (dir-hint-module-name (reverse (cdr reverse-name))) | |
1908 | (dir-hint (apply symbol-append (map (lambda (elt) (symbol-append elt "/")) dir-hint-module-name)))) | |
0209ca9a | 1909 | (resolve-module dir-hint-module-name #f) |
0f2d19dd JB |
1910 | (and (not (autoload-done-or-in-progress? dir-hint name)) |
1911 | (let ((didit #f)) | |
1912 | (dynamic-wind | |
1913 | (lambda () (autoload-in-progress! dir-hint name)) | |
1914 | (lambda () | |
1915 | (let loop ((dirs %load-path)) | |
1916 | (and (not (null? dirs)) | |
1917 | (or | |
1918 | (let ((d (car dirs)) | |
1919 | (trys (list | |
1920 | dir-hint | |
1921 | (sfx dir-hint) | |
1922 | (in-vicinity dir-hint name) | |
1923 | (in-vicinity dir-hint (sfx name))))) | |
1924 | (and (or-map (lambda (f) | |
1925 | (let ((full (in-vicinity d f))) | |
1926 | full | |
6fa8995c GH |
1927 | (and (file-exists? full) |
1928 | (not (file-is-directory? full)) | |
0f2d19dd JB |
1929 | (begin |
1930 | (save-module-excursion | |
1931 | (lambda () | |
5552355a GH |
1932 | (load (string-append |
1933 | d "/" f)))) | |
0f2d19dd JB |
1934 | #t)))) |
1935 | trys) | |
1936 | (begin | |
1937 | (set! didit #t) | |
1938 | #t))) | |
1939 | (loop (cdr dirs)))))) | |
1940 | (lambda () (set-autoloaded! dir-hint name didit))) | |
1941 | didit)))) | |
1942 | ||
d0cbd20c MV |
1943 | ;;; Dynamic linking of modules |
1944 | ||
1945 | ;; Initializing a module that is written in C is a two step process. | |
1946 | ;; First the module's `module init' function is called. This function | |
1947 | ;; is expected to call `scm_register_module_xxx' to register the `real | |
1948 | ;; init' function. Later, when the module is referenced for the first | |
1949 | ;; time, this real init function is called in the right context. See | |
1950 | ;; gtcltk-lib/gtcltk-module.c for an example. | |
1951 | ;; | |
1952 | ;; The code for the module can be in a regular shared library (so that | |
1953 | ;; the `module init' function will be called when libguile is | |
1954 | ;; initialized). Or it can be dynamically linked. | |
1955 | ;; | |
1956 | ;; You can safely call `scm_register_module_xxx' before libguile | |
1957 | ;; itself is initialized. You could call it from an C++ constructor | |
1958 | ;; of a static object, for example. | |
1959 | ;; | |
1960 | ;; To make your Guile extension into a dynamic linkable module, follow | |
1961 | ;; these easy steps: | |
1962 | ;; | |
8bb7330c | 1963 | ;; - Find a name for your module, like (ice-9 gtcltk) |
d0cbd20c MV |
1964 | ;; - Write a function with a name like |
1965 | ;; | |
1966 | ;; scm_init_ice_9_gtcltk_module | |
1967 | ;; | |
1968 | ;; This is your `module init' function. It should call | |
1969 | ;; | |
1970 | ;; scm_register_module_xxx ("ice-9 gtcltk", scm_init_gtcltk); | |
1971 | ;; | |
1972 | ;; "ice-9 gtcltk" is the C version of the module name. Slashes are | |
1973 | ;; replaced by spaces, the rest is untouched. `scm_init_gtcltk' is | |
ed218d98 | 1974 | ;; the real init function that executes the usual initializations |
d0cbd20c MV |
1975 | ;; like making new smobs, etc. |
1976 | ;; | |
1977 | ;; - Make a shared library with your code and a name like | |
1978 | ;; | |
1979 | ;; ice-9/libgtcltk.so | |
1980 | ;; | |
1981 | ;; and put it somewhere in %load-path. | |
1982 | ;; | |
8bb7330c | 1983 | ;; - Then you can simply write `:use-module (ice-9 gtcltk)' and it |
d0cbd20c MV |
1984 | ;; will be linked automatically. |
1985 | ;; | |
1986 | ;; This is all very experimental. | |
1987 | ||
1988 | (define (split-c-module-name str) | |
1989 | (let loop ((rev '()) | |
1990 | (start 0) | |
1991 | (pos 0) | |
1992 | (end (string-length str))) | |
1993 | (cond | |
1994 | ((= pos end) | |
1995 | (reverse (cons (string->symbol (substring str start pos)) rev))) | |
1996 | ((eq? (string-ref str pos) #\space) | |
1997 | (loop (cons (string->symbol (substring str start pos)) rev) | |
1998 | (+ pos 1) | |
1999 | (+ pos 1) | |
2000 | end)) | |
2001 | (else | |
2002 | (loop rev start (+ pos 1) end))))) | |
2003 | ||
2004 | (define (convert-c-registered-modules dynobj) | |
2005 | (let ((res (map (lambda (c) | |
2006 | (list (split-c-module-name (car c)) (cdr c) dynobj)) | |
2007 | (c-registered-modules)))) | |
2008 | (c-clear-registered-modules) | |
2009 | res)) | |
2010 | ||
2011 | (define registered-modules (convert-c-registered-modules #f)) | |
2012 | ||
2013 | (define (init-dynamic-module modname) | |
2014 | (or-map (lambda (modinfo) | |
2015 | (if (equal? (car modinfo) modname) | |
2016 | (let ((mod (resolve-module modname #f))) | |
2017 | (save-module-excursion | |
2018 | (lambda () | |
2019 | (set-current-module mod) | |
2020 | (dynamic-call (cadr modinfo) (caddr modinfo)) | |
2021 | (set-module-public-interface! mod mod))) | |
2022 | (set! registered-modules (delq! modinfo registered-modules)) | |
2023 | #t) | |
2024 | #f)) | |
2025 | registered-modules)) | |
2026 | ||
2027 | (define (dynamic-maybe-call name dynobj) | |
2028 | (catch #t ; could use false-if-exception here | |
2029 | (lambda () | |
2030 | (dynamic-call name dynobj)) | |
2031 | (lambda args | |
2032 | #f))) | |
2033 | ||
ed218d98 MV |
2034 | (define (dynamic-maybe-link filename) |
2035 | (catch #t ; could use false-if-exception here | |
2036 | (lambda () | |
2037 | (dynamic-link filename)) | |
2038 | (lambda args | |
2039 | #f))) | |
2040 | ||
d0cbd20c MV |
2041 | (define (find-and-link-dynamic-module module-name) |
2042 | (define (make-init-name mod-name) | |
2043 | (string-append 'scm_init | |
2044 | (list->string (map (lambda (c) | |
2045 | (if (or (char-alphabetic? c) | |
2046 | (char-numeric? c)) | |
2047 | c | |
2048 | #\_)) | |
2049 | (string->list mod-name))) | |
2050 | '_module)) | |
2051 | (let ((libname | |
2052 | (let loop ((dirs "") | |
2053 | (syms module-name)) | |
2054 | (cond | |
2055 | ((null? (cdr syms)) | |
2056 | (string-append dirs "lib" (car syms) ".so")) | |
2057 | (else | |
2058 | (loop (string-append dirs (car syms) "/") (cdr syms)))))) | |
2059 | (init (make-init-name (apply string-append | |
2060 | (map (lambda (s) | |
2061 | (string-append "_" s)) | |
2062 | module-name))))) | |
2063 | ;; (pk 'libname libname 'init init) | |
2064 | (or-map | |
2065 | (lambda (dir) | |
2066 | (let ((full (in-vicinity dir libname))) | |
2067 | ;; (pk 'trying full) | |
2068 | (if (file-exists? full) | |
2069 | (begin | |
2070 | (link-dynamic-module full init) | |
2071 | #t) | |
2072 | #f))) | |
2073 | %load-path))) | |
2074 | ||
2075 | (define (link-dynamic-module filename initname) | |
6b856182 MV |
2076 | (let ((dynobj (dynamic-link filename))) |
2077 | (dynamic-call initname dynobj) | |
2078 | (set! registered-modules | |
2079 | (append! (convert-c-registered-modules dynobj) | |
2080 | registered-modules)))) | |
ed218d98 | 2081 | |
d0cbd20c MV |
2082 | (define (try-module-dynamic-link module-name) |
2083 | (or (init-dynamic-module module-name) | |
2084 | (and (find-and-link-dynamic-module module-name) | |
2085 | (init-dynamic-module module-name)))) | |
2086 | ||
ed218d98 MV |
2087 | |
2088 | ||
0f2d19dd JB |
2089 | (define autoloads-done '((guile . guile))) |
2090 | ||
2091 | (define (autoload-done-or-in-progress? p m) | |
2092 | (let ((n (cons p m))) | |
2093 | (->bool (or (member n autoloads-done) | |
2094 | (member n autoloads-in-progress))))) | |
2095 | ||
2096 | (define (autoload-done! p m) | |
2097 | (let ((n (cons p m))) | |
2098 | (set! autoloads-in-progress | |
2099 | (delete! n autoloads-in-progress)) | |
2100 | (or (member n autoloads-done) | |
2101 | (set! autoloads-done (cons n autoloads-done))))) | |
2102 | ||
2103 | (define (autoload-in-progress! p m) | |
2104 | (let ((n (cons p m))) | |
2105 | (set! autoloads-done | |
2106 | (delete! n autoloads-done)) | |
2107 | (set! autoloads-in-progress (cons n autoloads-in-progress)))) | |
2108 | ||
2109 | (define (set-autoloaded! p m done?) | |
2110 | (if done? | |
2111 | (autoload-done! p m) | |
2112 | (let ((n (cons p m))) | |
2113 | (set! autoloads-done (delete! n autoloads-done)) | |
2114 | (set! autoloads-in-progress (delete! n autoloads-in-progress))))) | |
2115 | ||
2116 | ||
2117 | ||
2118 | ||
2119 | \f | |
2120 | ;;; {Macros} | |
2121 | ;;; | |
2122 | ||
9591db87 MD |
2123 | (define macro-table (make-weak-key-hash-table 523)) |
2124 | (define xformer-table (make-weak-key-hash-table 523)) | |
0f2d19dd JB |
2125 | |
2126 | (define (defmacro? m) (hashq-ref macro-table m)) | |
2127 | (define (assert-defmacro?! m) (hashq-set! macro-table m #t)) | |
2128 | (define (defmacro-transformer m) (hashq-ref xformer-table m)) | |
2129 | (define (set-defmacro-transformer! m t) (hashq-set! xformer-table m t)) | |
2130 | ||
2131 | (define defmacro:transformer | |
2132 | (lambda (f) | |
2133 | (let* ((xform (lambda (exp env) | |
2134 | (copy-tree (apply f (cdr exp))))) | |
2135 | (a (procedure->memoizing-macro xform))) | |
2136 | (assert-defmacro?! a) | |
2137 | (set-defmacro-transformer! a f) | |
2138 | a))) | |
2139 | ||
2140 | ||
2141 | (define defmacro | |
2142 | (let ((defmacro-transformer | |
2143 | (lambda (name parms . body) | |
2144 | (let ((transformer `(lambda ,parms ,@body))) | |
2145 | `(define ,name | |
2146 | (,(lambda (transformer) | |
2147 | (defmacro:transformer transformer)) | |
2148 | ,transformer)))))) | |
2149 | (defmacro:transformer defmacro-transformer))) | |
2150 | ||
2151 | (define defmacro:syntax-transformer | |
2152 | (lambda (f) | |
2153 | (procedure->syntax | |
2154 | (lambda (exp env) | |
2155 | (copy-tree (apply f (cdr exp))))))) | |
2156 | ||
ed218d98 MV |
2157 | |
2158 | ;; XXX - should the definition of the car really be looked up in the | |
2159 | ;; current module? | |
2160 | ||
0f2d19dd JB |
2161 | (define (macroexpand-1 e) |
2162 | (cond | |
2163 | ((pair? e) (let* ((a (car e)) | |
ed218d98 | 2164 | (val (and (symbol? a) (local-ref (list a))))) |
0f2d19dd JB |
2165 | (if (defmacro? val) |
2166 | (apply (defmacro-transformer val) (cdr e)) | |
2167 | e))) | |
2168 | (#t e))) | |
2169 | ||
2170 | (define (macroexpand e) | |
2171 | (cond | |
2172 | ((pair? e) (let* ((a (car e)) | |
ed218d98 | 2173 | (val (and (symbol? a) (local-ref (list a))))) |
0f2d19dd JB |
2174 | (if (defmacro? val) |
2175 | (macroexpand (apply (defmacro-transformer val) (cdr e))) | |
2176 | e))) | |
2177 | (#t e))) | |
2178 | ||
2179 | (define gentemp | |
2180 | (let ((*gensym-counter* -1)) | |
2181 | (lambda () | |
2182 | (set! *gensym-counter* (+ *gensym-counter* 1)) | |
2183 | (string->symbol | |
2184 | (string-append "scm:G" (number->string *gensym-counter*)))))) | |
2185 | ||
2186 | ||
2187 | \f | |
2188 | ||
2189 | ;;; {Running Repls} | |
2190 | ;;; | |
2191 | ||
2192 | (define (repl read evaler print) | |
75a97b92 | 2193 | (let loop ((source (read (current-input-port)))) |
0f2d19dd | 2194 | (print (evaler source)) |
75a97b92 | 2195 | (loop (read (current-input-port))))) |
0f2d19dd JB |
2196 | |
2197 | ;; A provisional repl that acts like the SCM repl: | |
2198 | ;; | |
2199 | (define scm-repl-silent #f) | |
2200 | (define (assert-repl-silence v) (set! scm-repl-silent v)) | |
2201 | ||
21ed9efe MD |
2202 | (define *unspecified* (if #f #f)) |
2203 | (define (unspecified? v) (eq? v *unspecified*)) | |
2204 | ||
2205 | (define scm-repl-print-unspecified #f) | |
2206 | (define (assert-repl-print-unspecified v) (set! scm-repl-print-unspecified v)) | |
2207 | ||
79451588 | 2208 | (define scm-repl-verbose #f) |
0f2d19dd JB |
2209 | (define (assert-repl-verbosity v) (set! scm-repl-verbose v)) |
2210 | ||
e6875011 | 2211 | (define scm-repl-prompt "guile> ") |
0f2d19dd | 2212 | |
e6875011 MD |
2213 | (define (set-repl-prompt! v) (set! scm-repl-prompt v)) |
2214 | ||
d5d34fa1 MD |
2215 | (define (default-lazy-handler key . args) |
2216 | (save-stack lazy-handler-dispatch) | |
2217 | (apply throw key args)) | |
2218 | ||
2219 | (define apply-frame-handler default-lazy-handler) | |
2220 | (define exit-frame-handler default-lazy-handler) | |
2221 | ||
2222 | (define (lazy-handler-dispatch key . args) | |
2223 | (case key | |
2224 | ((apply-frame) | |
2225 | (apply apply-frame-handler key args)) | |
2226 | ((exit-frame) | |
2227 | (apply exit-frame-handler key args)) | |
2228 | (else | |
2229 | (apply default-lazy-handler key args)))) | |
0f2d19dd | 2230 | |
59e1116d MD |
2231 | (define abort-hook '()) |
2232 | ||
0f2d19dd | 2233 | (define (error-catching-loop thunk) |
8e44e7a0 GH |
2234 | (let ((status #f)) |
2235 | (define (loop first) | |
2236 | (let ((next | |
2237 | (catch #t | |
9a0d70e2 | 2238 | |
8e44e7a0 GH |
2239 | (lambda () |
2240 | (lazy-catch #t | |
2241 | (lambda () | |
2242 | (dynamic-wind | |
2243 | (lambda () (unmask-signals)) | |
2244 | (lambda () | |
2245 | (first) | |
2246 | ||
2247 | ;; This line is needed because mark | |
2248 | ;; doesn't do closures quite right. | |
2249 | ;; Unreferenced locals should be | |
2250 | ;; collected. | |
2251 | ;; | |
2252 | (set! first #f) | |
2253 | (let loop ((v (thunk))) | |
2254 | (loop (thunk))) | |
2255 | #f) | |
2256 | (lambda () (mask-signals)))) | |
2257 | ||
2258 | lazy-handler-dispatch)) | |
2259 | ||
2260 | (lambda (key . args) | |
2261 | (case key | |
2262 | ((quit) | |
8e44e7a0 GH |
2263 | (force-output) |
2264 | (set! status args) | |
2265 | #f) | |
2266 | ||
2267 | ((switch-repl) | |
2268 | (apply throw 'switch-repl args)) | |
2269 | ||
2270 | ((abort) | |
2271 | ;; This is one of the closures that require | |
2272 | ;; (set! first #f) above | |
2273 | ;; | |
2274 | (lambda () | |
2275 | (run-hooks abort-hook) | |
2276 | (force-output) | |
2277 | (display "ABORT: " (current-error-port)) | |
2278 | (write args (current-error-port)) | |
2279 | (newline (current-error-port)) | |
2280 | (if (and (not has-shown-debugger-hint?) | |
2281 | (not (memq 'backtrace | |
2282 | (debug-options-interface))) | |
2283 | (stack? the-last-stack)) | |
2284 | (begin | |
2285 | (newline (current-error-port)) | |
2286 | (display | |
2287 | "Type \"(backtrace)\" to get more information.\n" | |
2288 | (current-error-port)) | |
2289 | (set! has-shown-debugger-hint? #t))) | |
2290 | (set! stack-saved? #f))) | |
2291 | ||
2292 | (else | |
2293 | ;; This is the other cons-leak closure... | |
2294 | (lambda () | |
2295 | (cond ((= (length args) 4) | |
2296 | (apply handle-system-error key args)) | |
2297 | (else | |
2298 | (apply bad-throw key args)))))))))) | |
2299 | (if next (loop next) status))) | |
2300 | (loop (lambda () #t)))) | |
0f2d19dd | 2301 | |
d590bbf6 | 2302 | ;;(define the-last-stack #f) Defined by scm_init_backtrace () |
21ed9efe MD |
2303 | (define stack-saved? #f) |
2304 | ||
2305 | (define (save-stack . narrowing) | |
2306 | (cond (stack-saved?) | |
2307 | ((not (memq 'debug (debug-options-interface))) | |
2308 | (set! the-last-stack #f) | |
2309 | (set! stack-saved? #t)) | |
2310 | (else | |
2311 | (set! the-last-stack | |
2312 | (case (stack-id #t) | |
2313 | ((repl-stack) | |
2314 | (apply make-stack #t save-stack eval narrowing)) | |
2315 | ((load-stack) | |
096d5f90 | 2316 | (apply make-stack #t save-stack 0 narrowing)) |
21ed9efe MD |
2317 | ((tk-stack) |
2318 | (apply make-stack #t save-stack tk-stack-mark narrowing)) | |
2319 | ((#t) | |
e6875011 | 2320 | (apply make-stack #t save-stack 0 1 narrowing)) |
21ed9efe MD |
2321 | (else (let ((id (stack-id #t))) |
2322 | (and (procedure? id) | |
2323 | (apply make-stack #t save-stack id narrowing)))))) | |
2324 | (set! stack-saved? #t)))) | |
1c6cd8e8 | 2325 | |
59e1116d MD |
2326 | (define before-error-hook '()) |
2327 | (define after-error-hook '()) | |
2328 | (define before-backtrace-hook '()) | |
2329 | (define after-backtrace-hook '()) | |
1c6cd8e8 | 2330 | |
21ed9efe MD |
2331 | (define has-shown-debugger-hint? #f) |
2332 | ||
35c5db87 GH |
2333 | (define (handle-system-error key . args) |
2334 | (let ((cep (current-error-port))) | |
21ed9efe MD |
2335 | (cond ((not (stack? the-last-stack))) |
2336 | ((memq 'backtrace (debug-options-interface)) | |
59e1116d | 2337 | (run-hooks before-backtrace-hook) |
21ed9efe MD |
2338 | (newline cep) |
2339 | (display-backtrace the-last-stack cep) | |
2340 | (newline cep) | |
59e1116d MD |
2341 | (run-hooks after-backtrace-hook))) |
2342 | (run-hooks before-error-hook) | |
c27659c8 | 2343 | (apply display-error the-last-stack cep args) |
59e1116d | 2344 | (run-hooks after-error-hook) |
35c5db87 GH |
2345 | (force-output cep) |
2346 | (throw 'abort key))) | |
21ed9efe | 2347 | |
0f2d19dd JB |
2348 | (define (quit . args) |
2349 | (apply throw 'quit args)) | |
2350 | ||
7950df7c GH |
2351 | (define exit quit) |
2352 | ||
d590bbf6 MD |
2353 | ;;(define has-shown-backtrace-hint? #f) Defined by scm_init_backtrace () |
2354 | ||
2355 | ;; Replaced by C code: | |
2356 | ;;(define (backtrace) | |
2357 | ;; (if the-last-stack | |
2358 | ;; (begin | |
2359 | ;; (newline) | |
2360 | ;; (display-backtrace the-last-stack (current-output-port)) | |
2361 | ;; (newline) | |
2362 | ;; (if (and (not has-shown-backtrace-hint?) | |
2363 | ;; (not (memq 'backtrace (debug-options-interface)))) | |
2364 | ;; (begin | |
2365 | ;; (display | |
2366 | ;;"Type \"(debug-enable 'backtrace)\" if you would like a backtrace | |
2367 | ;;automatically if an error occurs in the future.\n") | |
2368 | ;; (set! has-shown-backtrace-hint? #t)))) | |
2369 | ;; (display "No backtrace available.\n"))) | |
21ed9efe | 2370 | |
0f2d19dd JB |
2371 | (define (error-catching-repl r e p) |
2372 | (error-catching-loop (lambda () (p (e (r)))))) | |
2373 | ||
2374 | (define (gc-run-time) | |
2375 | (cdr (assq 'gc-time-taken (gc-stats)))) | |
2376 | ||
59e1116d MD |
2377 | (define before-read-hook '()) |
2378 | (define after-read-hook '()) | |
1c6cd8e8 | 2379 | |
0f2d19dd JB |
2380 | (define (scm-style-repl) |
2381 | (letrec ( | |
2382 | (start-gc-rt #f) | |
2383 | (start-rt #f) | |
2384 | (repl-report-reset (lambda () #f)) | |
2385 | (repl-report-start-timing (lambda () | |
2386 | (set! start-gc-rt (gc-run-time)) | |
2387 | (set! start-rt (get-internal-run-time)))) | |
2388 | (repl-report (lambda () | |
2389 | (display ";;; ") | |
2390 | (display (inexact->exact | |
2391 | (* 1000 (/ (- (get-internal-run-time) start-rt) | |
2392 | internal-time-units-per-second)))) | |
2393 | (display " msec (") | |
2394 | (display (inexact->exact | |
2395 | (* 1000 (/ (- (gc-run-time) start-gc-rt) | |
2396 | internal-time-units-per-second)))) | |
2397 | (display " msec in gc)\n"))) | |
480977d0 JB |
2398 | |
2399 | (consume-trailing-whitespace | |
2400 | (lambda () | |
2401 | (let ((ch (peek-char))) | |
2402 | (cond | |
2403 | ((eof-object? ch)) | |
2404 | ((or (char=? ch #\space) (char=? ch #\tab)) | |
2405 | (read-char) | |
2406 | (consume-trailing-whitespace)) | |
2407 | ((char=? ch #\newline) | |
2408 | (read-char)))))) | |
0f2d19dd JB |
2409 | (-read (lambda () |
2410 | (if scm-repl-prompt | |
2411 | (begin | |
e6875011 MD |
2412 | (display (cond ((string? scm-repl-prompt) |
2413 | scm-repl-prompt) | |
2414 | ((thunk? scm-repl-prompt) | |
2415 | (scm-repl-prompt)) | |
2416 | (else "> "))) | |
0f2d19dd JB |
2417 | (force-output) |
2418 | (repl-report-reset))) | |
59e1116d | 2419 | (run-hooks before-read-hook) |
75a97b92 | 2420 | (let ((val (read (current-input-port)))) |
480977d0 JB |
2421 | ;; As described in R4RS, the READ procedure updates the |
2422 | ;; port to point to the first characetr past the end of | |
2423 | ;; the external representation of the object. This | |
2424 | ;; means that it doesn't consume the newline typically | |
2425 | ;; found after an expression. This means that, when | |
2426 | ;; debugging Guile with GDB, GDB gets the newline, which | |
2427 | ;; it often interprets as a "continue" command, making | |
2428 | ;; breakpoints kind of useless. So, consume any | |
2429 | ;; trailing newline here, as well as any whitespace | |
2430 | ;; before it. | |
2431 | (consume-trailing-whitespace) | |
59e1116d | 2432 | (run-hooks after-read-hook) |
0f2d19dd JB |
2433 | (if (eof-object? val) |
2434 | (begin | |
7950df7c | 2435 | (repl-report-start-timing) |
0f2d19dd JB |
2436 | (if scm-repl-verbose |
2437 | (begin | |
2438 | (newline) | |
2439 | (display ";;; EOF -- quitting") | |
2440 | (newline))) | |
2441 | (quit 0))) | |
2442 | val))) | |
2443 | ||
2444 | (-eval (lambda (sourc) | |
2445 | (repl-report-start-timing) | |
4cdee789 | 2446 | (start-stack 'repl-stack (eval sourc)))) |
0f2d19dd JB |
2447 | |
2448 | (-print (lambda (result) | |
2449 | (if (not scm-repl-silent) | |
2450 | (begin | |
21ed9efe MD |
2451 | (if (or scm-repl-print-unspecified |
2452 | (not (unspecified? result))) | |
2453 | (begin | |
2454 | (write result) | |
2455 | (newline))) | |
0f2d19dd JB |
2456 | (if scm-repl-verbose |
2457 | (repl-report)) | |
2458 | (force-output))))) | |
2459 | ||
8e44e7a0 | 2460 | (-quit (lambda (args) |
0f2d19dd JB |
2461 | (if scm-repl-verbose |
2462 | (begin | |
2463 | (display ";;; QUIT executed, repl exitting") | |
2464 | (newline) | |
2465 | (repl-report))) | |
8e44e7a0 | 2466 | args)) |
0f2d19dd JB |
2467 | |
2468 | (-abort (lambda () | |
2469 | (if scm-repl-verbose | |
2470 | (begin | |
2471 | (display ";;; ABORT executed.") | |
2472 | (newline) | |
2473 | (repl-report))) | |
2474 | (repl -read -eval -print)))) | |
2475 | ||
8e44e7a0 GH |
2476 | (let ((status (error-catching-repl -read |
2477 | -eval | |
2478 | -print))) | |
2479 | (-quit status)))) | |
2480 | ||
0f2d19dd | 2481 | |
0f2d19dd | 2482 | \f |
44cf1f0f | 2483 | ;;; {IOTA functions: generating lists of numbers} |
0f2d19dd JB |
2484 | |
2485 | (define (reverse-iota n) (if (> n 0) (cons (1- n) (reverse-iota (1- n))) '())) | |
2486 | (define (iota n) (list-reverse! (reverse-iota n))) | |
2487 | ||
2488 | \f | |
2489 | ;;; {While} | |
2490 | ;;; | |
2491 | ;;; with `continue' and `break'. | |
2492 | ;;; | |
2493 | ||
2494 | (defmacro while (cond . body) | |
2495 | `(letrec ((continue (lambda () (or (not ,cond) (begin (begin ,@ body) (continue))))) | |
2496 | (break (lambda val (apply throw 'break val)))) | |
2497 | (catch 'break | |
2498 | (lambda () (continue)) | |
2499 | (lambda v (cadr v))))) | |
2500 | ||
2501 | ||
8a6a8671 MV |
2502 | ;;; {with-fluids} |
2503 | ||
2504 | ;; with-fluids is a convenience wrapper for the builtin procedure | |
2505 | ;; `with-fluids*'. The syntax is just like `let': | |
2506 | ;; | |
2507 | ;; (with-fluids ((fluid val) | |
2508 | ;; ...) | |
2509 | ;; body) | |
2510 | ||
2511 | (defmacro with-fluids (bindings . body) | |
2512 | `(with-fluids* (list ,@(map car bindings)) (list ,@(map cadr bindings)) | |
2513 | (lambda () ,@body))) | |
2514 | ||
0f2d19dd JB |
2515 | \f |
2516 | ||
2517 | ;;; {Macros} | |
2518 | ;;; | |
2519 | ||
2520 | ;; actually....hobbit might be able to hack these with a little | |
2521 | ;; coaxing | |
2522 | ;; | |
2523 | ||
2524 | (defmacro define-macro (first . rest) | |
2525 | (let ((name (if (symbol? first) first (car first))) | |
2526 | (transformer | |
2527 | (if (symbol? first) | |
2528 | (car rest) | |
2529 | `(lambda ,(cdr first) ,@rest)))) | |
2530 | `(define ,name (defmacro:transformer ,transformer)))) | |
2531 | ||
2532 | ||
2533 | (defmacro define-syntax-macro (first . rest) | |
2534 | (let ((name (if (symbol? first) first (car first))) | |
2535 | (transformer | |
2536 | (if (symbol? first) | |
2537 | (car rest) | |
2538 | `(lambda ,(cdr first) ,@rest)))) | |
2539 | `(define ,name (defmacro:syntax-transformer ,transformer)))) | |
2540 | \f | |
2541 | ;;; {Module System Macros} | |
2542 | ;;; | |
2543 | ||
2544 | (defmacro define-module args | |
2545 | `(let* ((process-define-module process-define-module) | |
2546 | (set-current-module set-current-module) | |
2547 | (module (process-define-module ',args))) | |
2548 | (set-current-module module) | |
2549 | module)) | |
2550 | ||
89da9036 MV |
2551 | ;; the guts of the use-modules macro. add the interfaces of the named |
2552 | ;; modules to the use-list of the current module, in order | |
2553 | (define (process-use-modules module-names) | |
2554 | (for-each (lambda (module-name) | |
2555 | (let ((mod-iface (resolve-interface module-name))) | |
2556 | (or mod-iface | |
2557 | (error "no such module" module-name)) | |
2558 | (module-use! (current-module) mod-iface))) | |
2559 | (reverse module-names))) | |
2560 | ||
33cf699f | 2561 | (defmacro use-modules modules |
89da9036 | 2562 | `(process-use-modules ',modules)) |
33cf699f | 2563 | |
0f2d19dd JB |
2564 | (define define-private define) |
2565 | ||
2566 | (defmacro define-public args | |
2567 | (define (syntax) | |
2568 | (error "bad syntax" (list 'define-public args))) | |
2569 | (define (defined-name n) | |
2570 | (cond | |
3c5af9ef JB |
2571 | ((symbol? n) n) |
2572 | ((pair? n) (defined-name (car n))) | |
2573 | (else (syntax)))) | |
0f2d19dd | 2574 | (cond |
3c5af9ef JB |
2575 | ((null? args) (syntax)) |
2576 | ||
2577 | (#t (let ((name (defined-name (car args)))) | |
2578 | `(begin | |
2579 | (let ((public-i (module-public-interface (current-module)))) | |
2580 | ;; Make sure there is a local variable: | |
2581 | ;; | |
2582 | (module-define! (current-module) | |
2583 | ',name | |
2584 | (module-ref (current-module) ',name #f)) | |
0f2d19dd | 2585 | |
3c5af9ef JB |
2586 | ;; Make sure that local is exported: |
2587 | ;; | |
2588 | (module-add! public-i ',name | |
2589 | (module-variable (current-module) ',name))) | |
0f2d19dd | 2590 | |
3c5af9ef JB |
2591 | ;; Now (re)define the var normally. Bernard URBAN |
2592 | ;; suggests we use eval here to accomodate Hobbit; it lets | |
2593 | ;; the interpreter handle the define-private form, which | |
2594 | ;; Hobbit can't digest. | |
2595 | (eval '(define-private ,@ args))))))) | |
0f2d19dd JB |
2596 | |
2597 | ||
2598 | ||
2599 | (defmacro defmacro-public args | |
2600 | (define (syntax) | |
2601 | (error "bad syntax" (list 'defmacro-public args))) | |
2602 | (define (defined-name n) | |
2603 | (cond | |
2604 | ((symbol? n) n) | |
2605 | (else (syntax)))) | |
2606 | (cond | |
2607 | ((null? args) (syntax)) | |
2608 | ||
2609 | (#t (let ((name (defined-name (car args)))) | |
2610 | `(begin | |
2611 | (let ((public-i (module-public-interface (current-module)))) | |
2612 | ;; Make sure there is a local variable: | |
2613 | ;; | |
2614 | (module-define! (current-module) | |
2615 | ',name | |
2616 | (module-ref (current-module) ',name #f)) | |
2617 | ||
2618 | ;; Make sure that local is exported: | |
2619 | ;; | |
2620 | (module-add! public-i ',name (module-variable (current-module) ',name))) | |
2621 | ||
2622 | ;; Now (re)define the var normally. | |
2623 | ;; | |
2624 | (defmacro ,@ args)))))) | |
2625 | ||
2626 | ||
2627 | ||
2628 | ||
0f2d19dd | 2629 | (define load load-module) |
1c6cd8e8 MD |
2630 | ;(define (load . args) |
2631 | ; (start-stack 'load-stack (apply load-module args))) | |
0f2d19dd JB |
2632 | |
2633 | ||
2634 | \f | |
44cf1f0f | 2635 | ;;; {I/O functions for Tcl channels (disabled)} |
0f2d19dd JB |
2636 | |
2637 | ;; (define in-ch (get-standard-channel TCL_STDIN)) | |
2638 | ;; (define out-ch (get-standard-channel TCL_STDOUT)) | |
2639 | ;; (define err-ch (get-standard-channel TCL_STDERR)) | |
2640 | ;; | |
2641 | ;; (define inp (%make-channel-port in-ch "r")) | |
2642 | ;; (define outp (%make-channel-port out-ch "w")) | |
2643 | ;; (define errp (%make-channel-port err-ch "w")) | |
2644 | ;; | |
2645 | ;; (define %system-char-ready? char-ready?) | |
2646 | ;; | |
2647 | ;; (define (char-ready? p) | |
2648 | ;; (if (not (channel-port? p)) | |
2649 | ;; (%system-char-ready? p) | |
2650 | ;; (let* ((channel (%channel-port-channel p)) | |
2651 | ;; (old-blocking (channel-option-ref channel :blocking))) | |
2652 | ;; (dynamic-wind | |
2653 | ;; (lambda () (set-channel-option the-root-tcl-interpreter channel :blocking "0")) | |
2654 | ;; (lambda () (not (eof-object? (peek-char p)))) | |
2655 | ;; (lambda () (set-channel-option the-root-tcl-interpreter channel :blocking old-blocking)))))) | |
2656 | ;; | |
2657 | ;; (define (top-repl) | |
2658 | ;; (with-input-from-port inp | |
2659 | ;; (lambda () | |
2660 | ;; (with-output-to-port outp | |
2661 | ;; (lambda () | |
2662 | ;; (with-error-to-port errp | |
2663 | ;; (lambda () | |
2664 | ;; (scm-style-repl)))))))) | |
2665 | ;; | |
2666 | ;; (set-current-input-port inp) | |
2667 | ;; (set-current-output-port outp) | |
2668 | ;; (set-current-error-port errp) | |
2669 | ||
e1a191a8 GH |
2670 | ;; this is just (scm-style-repl) with a wrapper to install and remove |
2671 | ;; signal handlers. | |
8e44e7a0 | 2672 | (define (top-repl) |
e1a191a8 GH |
2673 | (let ((old-handlers #f) |
2674 | (signals `((,SIGINT . "User interrupt") | |
2675 | (,SIGFPE . "Arithmetic error") | |
2676 | (,SIGBUS . "Bad memory access (bus error)") | |
2677 | (,SIGSEGV . "Bad memory access (Segmentation violation)")))) | |
2678 | ||
2679 | (dynamic-wind | |
2680 | ||
2681 | ;; call at entry | |
2682 | (lambda () | |
2683 | (let ((make-handler (lambda (msg) | |
2684 | (lambda (sig) | |
096d5f90 | 2685 | (save-stack %deliver-signals) |
e1a191a8 GH |
2686 | (scm-error 'signal |
2687 | #f | |
2688 | msg | |
2689 | #f | |
2690 | (list sig)))))) | |
2691 | (set! old-handlers | |
2692 | (map (lambda (sig-msg) | |
2693 | (sigaction (car sig-msg) | |
2694 | (make-handler (cdr sig-msg)))) | |
2695 | signals)))) | |
2696 | ||
2697 | ;; the protected thunk. | |
2698 | (lambda () | |
2699 | (scm-style-repl)) | |
2700 | ||
2701 | ;; call at exit. | |
2702 | (lambda () | |
2703 | (map (lambda (sig-msg old-handler) | |
2704 | (if (not (car old-handler)) | |
2705 | ;; restore original C handler. | |
2706 | (sigaction (car sig-msg) #f) | |
2707 | ;; restore Scheme handler, SIG_IGN or SIG_DFL. | |
2708 | (sigaction (car sig-msg) | |
2709 | (car old-handler) | |
2710 | (cdr old-handler)))) | |
2711 | signals old-handlers))))) | |
0f2d19dd | 2712 | |
02b754d3 GH |
2713 | (defmacro false-if-exception (expr) |
2714 | `(catch #t (lambda () ,expr) | |
2715 | (lambda args #f))) | |
2716 | ||
0f2d19dd | 2717 | \f |
44cf1f0f | 2718 | ;;; {Calling Conventions} |
0f2d19dd JB |
2719 | (define-module (ice-9 calling)) |
2720 | ||
2721 | ;;;; | |
0f2d19dd JB |
2722 | ;;; |
2723 | ;;; This file contains a number of macros that support | |
2724 | ;;; common calling conventions. | |
2725 | ||
2726 | ;;; | |
2727 | ;;; with-excursion-function <vars> proc | |
2728 | ;;; <vars> is an unevaluated list of names that are bound in the caller. | |
2729 | ;;; proc is a procedure, called: | |
2730 | ;;; (proc excursion) | |
2731 | ;;; | |
2732 | ;;; excursion is a procedure isolates all changes to <vars> | |
2733 | ;;; in the dynamic scope of the call to proc. In other words, | |
2734 | ;;; the values of <vars> are saved when proc is entered, and when | |
2735 | ;;; proc returns, those values are restored. Values are also restored | |
2736 | ;;; entering and leaving the call to proc non-locally, such as using | |
2737 | ;;; call-with-current-continuation, error, or throw. | |
2738 | ;;; | |
2739 | (defmacro-public with-excursion-function (vars proc) | |
2740 | `(,proc ,(excursion-function-syntax vars))) | |
2741 | ||
2742 | ||
2743 | ||
2744 | ;;; with-getter-and-setter <vars> proc | |
2745 | ;;; <vars> is an unevaluated list of names that are bound in the caller. | |
2746 | ;;; proc is a procedure, called: | |
2747 | ;;; (proc getter setter) | |
2748 | ;;; | |
2749 | ;;; getter and setter are procedures used to access | |
2750 | ;;; or modify <vars>. | |
2751 | ;;; | |
2752 | ;;; setter, called with keywords arguments, modifies the named | |
2753 | ;;; values. If "foo" and "bar" are among <vars>, then: | |
2754 | ;;; | |
2755 | ;;; (setter :foo 1 :bar 2) | |
2756 | ;;; == (set! foo 1 bar 2) | |
2757 | ;;; | |
2758 | ;;; getter, called with just keywords, returns | |
2759 | ;;; a list of the corresponding values. For example, | |
2760 | ;;; if "foo" and "bar" are among the <vars>, then | |
2761 | ;;; | |
2762 | ;;; (getter :foo :bar) | |
2763 | ;;; => (<value-of-foo> <value-of-bar>) | |
2764 | ;;; | |
2765 | ;;; getter, called with no arguments, returns a list of all accepted | |
2766 | ;;; keywords and the corresponding values. If "foo" and "bar" are | |
2767 | ;;; the *only* <vars>, then: | |
2768 | ;;; | |
2769 | ;;; (getter) | |
2770 | ;;; => (:foo <value-of-bar> :bar <value-of-foo>) | |
2771 | ;;; | |
2772 | ;;; The unusual calling sequence of a getter supports too handy | |
2773 | ;;; idioms: | |
2774 | ;;; | |
2775 | ;;; (apply setter (getter)) ;; save and restore | |
2776 | ;;; | |
2777 | ;;; (apply-to-args (getter :foo :bar) ;; fetch and bind | |
2778 | ;;; (lambda (foo bar) ....)) | |
2779 | ;;; | |
2780 | ;;; ;; [ "apply-to-args" is just like two-argument "apply" except that it | |
2781 | ;;; ;; takes its arguments in a different order. | |
2782 | ;;; | |
2783 | ;;; | |
2784 | (defmacro-public with-getter-and-setter (vars proc) | |
2785 | `(,proc ,@ (getter-and-setter-syntax vars))) | |
2786 | ||
2787 | ;;; with-getter vars proc | |
2788 | ;;; A short-hand for a call to with-getter-and-setter. | |
2789 | ;;; The procedure is called: | |
2790 | ;;; (proc getter) | |
2791 | ;;; | |
2792 | (defmacro-public with-getter (vars proc) | |
2793 | `(,proc ,(car (getter-and-setter-syntax vars)))) | |
2794 | ||
2795 | ||
2796 | ;;; with-delegating-getter-and-setter <vars> get-delegate set-delegate proc | |
2797 | ;;; Compose getters and setters. | |
2798 | ;;; | |
2799 | ;;; <vars> is an unevaluated list of names that are bound in the caller. | |
2800 | ;;; | |
2801 | ;;; get-delegate is called by the new getter to extend the set of | |
2802 | ;;; gettable variables beyond just <vars> | |
2803 | ;;; set-delegate is called by the new setter to extend the set of | |
2804 | ;;; gettable variables beyond just <vars> | |
2805 | ;;; | |
2806 | ;;; proc is a procedure that is called | |
2807 | ;;; (proc getter setter) | |
2808 | ;;; | |
2809 | (defmacro-public with-delegating-getter-and-setter (vars get-delegate set-delegate proc) | |
2810 | `(,proc ,@ (delegating-getter-and-setter-syntax vars get-delegate set-delegate))) | |
2811 | ||
2812 | ||
52c5a23a | 2813 | ;;; with-excursion-getter-and-setter <vars> proc |
0f2d19dd JB |
2814 | ;;; <vars> is an unevaluated list of names that are bound in the caller. |
2815 | ;;; proc is called: | |
2816 | ;;; | |
2817 | ;;; (proc excursion getter setter) | |
2818 | ;;; | |
2819 | ;;; See also: | |
2820 | ;;; with-getter-and-setter | |
2821 | ;;; with-excursion-function | |
2822 | ;;; | |
2823 | (defmacro-public with-excursion-getter-and-setter (vars proc) | |
2824 | `(,proc ,(excursion-function-syntax vars) | |
2825 | ,@ (getter-and-setter-syntax vars))) | |
2826 | ||
2827 | ||
2828 | (define (excursion-function-syntax vars) | |
2829 | (let ((saved-value-names (map gensym vars)) | |
2830 | (tmp-var-name (gensym 'temp)) | |
2831 | (swap-fn-name (gensym 'swap)) | |
2832 | (thunk-name (gensym 'thunk))) | |
2833 | `(lambda (,thunk-name) | |
2834 | (letrec ((,tmp-var-name #f) | |
2835 | (,swap-fn-name | |
2836 | (lambda () ,@ (map (lambda (n sn) `(set! ,tmp-var-name ,n ,n ,sn ,sn ,tmp-var-name)) | |
2837 | vars saved-value-names))) | |
2838 | ,@ (map (lambda (sn n) `(,sn ,n)) saved-value-names vars)) | |
2839 | (dynamic-wind | |
2840 | ,swap-fn-name | |
2841 | ,thunk-name | |
2842 | ,swap-fn-name))))) | |
2843 | ||
2844 | ||
2845 | (define (getter-and-setter-syntax vars) | |
2846 | (let ((args-name (gensym 'args)) | |
2847 | (an-arg-name (gensym 'an-arg)) | |
2848 | (new-val-name (gensym 'new-value)) | |
2849 | (loop-name (gensym 'loop)) | |
2850 | (kws (map symbol->keyword vars))) | |
2851 | (list `(lambda ,args-name | |
2852 | (let ,loop-name ((,args-name ,args-name)) | |
2853 | (if (null? ,args-name) | |
2854 | ,(if (null? kws) | |
2855 | ''() | |
2856 | `(let ((all-vals (,loop-name ',kws))) | |
2857 | (let ,loop-name ((vals all-vals) | |
2858 | (kws ',kws)) | |
2859 | (if (null? vals) | |
2860 | '() | |
2861 | `(,(car kws) ,(car vals) ,@(,loop-name (cdr vals) (cdr kws))))))) | |
2862 | (map (lambda (,an-arg-name) | |
2863 | (case ,an-arg-name | |
2864 | ,@ (append | |
2865 | (map (lambda (kw v) `((,kw) ,v)) kws vars) | |
2866 | `((else (throw 'bad-get-option ,an-arg-name)))))) | |
2867 | ,args-name)))) | |
2868 | ||
2869 | `(lambda ,args-name | |
2870 | (let ,loop-name ((,args-name ,args-name)) | |
2871 | (or (null? ,args-name) | |
2872 | (null? (cdr ,args-name)) | |
2873 | (let ((,an-arg-name (car ,args-name)) | |
2874 | (,new-val-name (cadr ,args-name))) | |
2875 | (case ,an-arg-name | |
2876 | ,@ (append | |
2877 | (map (lambda (kw v) `((,kw) (set! ,v ,new-val-name))) kws vars) | |
2878 | `((else (throw 'bad-set-option ,an-arg-name))))) | |
2879 | (,loop-name (cddr ,args-name))))))))) | |
2880 | ||
2881 | (define (delegating-getter-and-setter-syntax vars get-delegate set-delegate) | |
2882 | (let ((args-name (gensym 'args)) | |
2883 | (an-arg-name (gensym 'an-arg)) | |
2884 | (new-val-name (gensym 'new-value)) | |
2885 | (loop-name (gensym 'loop)) | |
2886 | (kws (map symbol->keyword vars))) | |
2887 | (list `(lambda ,args-name | |
2888 | (let ,loop-name ((,args-name ,args-name)) | |
2889 | (if (null? ,args-name) | |
2890 | (append! | |
2891 | ,(if (null? kws) | |
2892 | ''() | |
2893 | `(let ((all-vals (,loop-name ',kws))) | |
2894 | (let ,loop-name ((vals all-vals) | |
2895 | (kws ',kws)) | |
2896 | (if (null? vals) | |
2897 | '() | |
2898 | `(,(car kws) ,(car vals) ,@(,loop-name (cdr vals) (cdr kws))))))) | |
2899 | (,get-delegate)) | |
2900 | (map (lambda (,an-arg-name) | |
2901 | (case ,an-arg-name | |
2902 | ,@ (append | |
2903 | (map (lambda (kw v) `((,kw) ,v)) kws vars) | |
2904 | `((else (car (,get-delegate ,an-arg-name))))))) | |
2905 | ,args-name)))) | |
2906 | ||
2907 | `(lambda ,args-name | |
2908 | (let ,loop-name ((,args-name ,args-name)) | |
2909 | (or (null? ,args-name) | |
2910 | (null? (cdr ,args-name)) | |
2911 | (let ((,an-arg-name (car ,args-name)) | |
2912 | (,new-val-name (cadr ,args-name))) | |
2913 | (case ,an-arg-name | |
2914 | ,@ (append | |
2915 | (map (lambda (kw v) `((,kw) (set! ,v ,new-val-name))) kws vars) | |
2916 | `((else (,set-delegate ,an-arg-name ,new-val-name))))) | |
2917 | (,loop-name (cddr ,args-name))))))))) | |
2918 | ||
2919 | ||
2920 | ||
2921 | ||
2922 | ;;; with-configuration-getter-and-setter <vars-etc> proc | |
2923 | ;;; | |
2924 | ;;; Create a getter and setter that can trigger arbitrary computation. | |
2925 | ;;; | |
2926 | ;;; <vars-etc> is a list of variable specifiers, explained below. | |
2927 | ;;; proc is called: | |
2928 | ;;; | |
2929 | ;;; (proc getter setter) | |
2930 | ;;; | |
2931 | ;;; Each element of the <vars-etc> list is of the form: | |
2932 | ;;; | |
2933 | ;;; (<var> getter-hook setter-hook) | |
2934 | ;;; | |
2935 | ;;; Both hook elements are evaluated; the variable name is not. | |
2936 | ;;; Either hook may be #f or procedure. | |
2937 | ;;; | |
2938 | ;;; A getter hook is a thunk that returns a value for the corresponding | |
2939 | ;;; variable. If omitted (#f is passed), the binding of <var> is | |
2940 | ;;; returned. | |
2941 | ;;; | |
2942 | ;;; A setter hook is a procedure of one argument that accepts a new value | |
2943 | ;;; for the corresponding variable. If omitted, the binding of <var> | |
2944 | ;;; is simply set using set!. | |
2945 | ;;; | |
2946 | (defmacro-public with-configuration-getter-and-setter (vars-etc proc) | |
2947 | `((lambda (simpler-get simpler-set body-proc) | |
2948 | (with-delegating-getter-and-setter () | |
2949 | simpler-get simpler-set body-proc)) | |
2950 | ||
2951 | (lambda (kw) | |
2952 | (case kw | |
2953 | ,@(map (lambda (v) `((,(symbol->keyword (car v))) | |
2954 | ,(cond | |
2955 | ((cadr v) => list) | |
2956 | (else `(list ,(car v)))))) | |
2957 | vars-etc))) | |
2958 | ||
2959 | (lambda (kw new-val) | |
2960 | (case kw | |
2961 | ,@(map (lambda (v) `((,(symbol->keyword (car v))) | |
2962 | ,(cond | |
2963 | ((caddr v) => (lambda (proc) `(,proc new-val))) | |
2964 | (else `(set! ,(car v) new-val))))) | |
2965 | vars-etc))) | |
2966 | ||
2967 | ,proc)) | |
2968 | ||
2969 | (defmacro-public with-delegating-configuration-getter-and-setter (vars-etc delegate-get delegate-set proc) | |
2970 | `((lambda (simpler-get simpler-set body-proc) | |
2971 | (with-delegating-getter-and-setter () | |
2972 | simpler-get simpler-set body-proc)) | |
2973 | ||
2974 | (lambda (kw) | |
2975 | (case kw | |
2976 | ,@(append! (map (lambda (v) `((,(symbol->keyword (car v))) | |
2977 | ,(cond | |
2978 | ((cadr v) => list) | |
2979 | (else `(list ,(car v)))))) | |
2980 | vars-etc) | |
2981 | `((else (,delegate-get kw)))))) | |
2982 | ||
2983 | (lambda (kw new-val) | |
2984 | (case kw | |
2985 | ,@(append! (map (lambda (v) `((,(symbol->keyword (car v))) | |
2986 | ,(cond | |
2987 | ((caddr v) => (lambda (proc) `(,proc new-val))) | |
2988 | (else `(set! ,(car v) new-val))))) | |
2989 | vars-etc) | |
2990 | `((else (,delegate-set kw new-val)))))) | |
2991 | ||
2992 | ,proc)) | |
2993 | ||
2994 | ||
2995 | ;;; let-configuration-getter-and-setter <vars-etc> proc | |
2996 | ;;; | |
2997 | ;;; This procedure is like with-configuration-getter-and-setter (q.v.) | |
2998 | ;;; except that each element of <vars-etc> is: | |
2999 | ;;; | |
3000 | ;;; (<var> initial-value getter-hook setter-hook) | |
3001 | ;;; | |
3002 | ;;; Unlike with-configuration-getter-and-setter, let-configuration-getter-and-setter | |
3003 | ;;; introduces bindings for the variables named in <vars-etc>. | |
3004 | ;;; It is short-hand for: | |
3005 | ;;; | |
3006 | ;;; (let ((<var1> initial-value-1) | |
3007 | ;;; (<var2> initial-value-2) | |
3008 | ;;; ...) | |
3009 | ;;; (with-configuration-getter-and-setter ((<var1> v1-get v1-set) ...) proc)) | |
3010 | ;;; | |
3011 | (defmacro-public let-with-configuration-getter-and-setter (vars-etc proc) | |
3012 | `(let ,(map (lambda (v) `(,(car v) ,(cadr v))) vars-etc) | |
3013 | (with-configuration-getter-and-setter ,(map (lambda (v) `(,(car v) ,(caddr v) ,(cadddr v))) vars-etc) | |
3014 | ,proc))) | |
3015 | ||
3016 | ||
3017 | ||
3018 | \f | |
44cf1f0f JB |
3019 | ;;; {Implementation of COMMON LISP list functions for Scheme} |
3020 | ||
0f2d19dd JB |
3021 | (define-module (ice-9 common-list)) |
3022 | ||
3023 | ;;"comlist.scm" Implementation of COMMON LISP list functions for Scheme | |
3024 | ; Copyright (C) 1991, 1993, 1995 Aubrey Jaffer. | |
3025 | ; | |
3026 | ;Permission to copy this software, to redistribute it, and to use it | |
3027 | ;for any purpose is granted, subject to the following restrictions and | |
3028 | ;understandings. | |
3029 | ; | |
3030 | ;1. Any copy made of this software must include this copyright notice | |
3031 | ;in full. | |
3032 | ; | |
3033 | ;2. I have made no warrantee or representation that the operation of | |
3034 | ;this software will be error-free, and I am under no obligation to | |
3035 | ;provide any services, by way of maintenance, update, or otherwise. | |
3036 | ; | |
3037 | ;3. In conjunction with products arising from the use of this | |
3038 | ;material, there shall be no use of my name in any advertising, | |
3039 | ;promotional, or sales literature without prior written consent in | |
3040 | ;each case. | |
3041 | ||
0f2d19dd JB |
3042 | (define-public (adjoin e l) (if (memq e l) l (cons e l))) |
3043 | ||
3044 | (define-public (union l1 l2) | |
3045 | (cond ((null? l1) l2) | |
3046 | ((null? l2) l1) | |
3047 | (else (union (cdr l1) (adjoin (car l1) l2))))) | |
3048 | ||
3049 | (define-public (intersection l1 l2) | |
3050 | (cond ((null? l1) l1) | |
3051 | ((null? l2) l2) | |
3052 | ((memv (car l1) l2) (cons (car l1) (intersection (cdr l1) l2))) | |
3053 | (else (intersection (cdr l1) l2)))) | |
3054 | ||
3055 | (define-public (set-difference l1 l2) | |
3056 | (cond ((null? l1) l1) | |
3057 | ((memv (car l1) l2) (set-difference (cdr l1) l2)) | |
3058 | (else (cons (car l1) (set-difference (cdr l1) l2))))) | |
3059 | ||
3060 | (define-public (reduce-init p init l) | |
3061 | (if (null? l) | |
3062 | init | |
3063 | (reduce-init p (p init (car l)) (cdr l)))) | |
3064 | ||
3065 | (define-public (reduce p l) | |
3066 | (cond ((null? l) l) | |
3067 | ((null? (cdr l)) (car l)) | |
3068 | (else (reduce-init p (car l) (cdr l))))) | |
3069 | ||
3070 | (define-public (some pred l . rest) | |
3071 | (cond ((null? rest) | |
3072 | (let mapf ((l l)) | |
3073 | (and (not (null? l)) | |
3074 | (or (pred (car l)) (mapf (cdr l)))))) | |
3075 | (else (let mapf ((l l) (rest rest)) | |
3076 | (and (not (null? l)) | |
3077 | (or (apply pred (car l) (map car rest)) | |
3078 | (mapf (cdr l) (map cdr rest)))))))) | |
3079 | ||
3080 | (define-public (every pred l . rest) | |
3081 | (cond ((null? rest) | |
3082 | (let mapf ((l l)) | |
3083 | (or (null? l) | |
3084 | (and (pred (car l)) (mapf (cdr l)))))) | |
3085 | (else (let mapf ((l l) (rest rest)) | |
3086 | (or (null? l) | |
3087 | (and (apply pred (car l) (map car rest)) | |
3088 | (mapf (cdr l) (map cdr rest)))))))) | |
3089 | ||
3090 | (define-public (notany pred . ls) (not (apply some pred ls))) | |
3091 | ||
3092 | (define-public (notevery pred . ls) (not (apply every pred ls))) | |
3093 | ||
3094 | (define-public (find-if t l) | |
3095 | (cond ((null? l) #f) | |
3096 | ((t (car l)) (car l)) | |
3097 | (else (find-if t (cdr l))))) | |
3098 | ||
3099 | (define-public (member-if t l) | |
3100 | (cond ((null? l) #f) | |
3101 | ((t (car l)) l) | |
3102 | (else (member-if t (cdr l))))) | |
3103 | ||
3104 | (define-public (remove-if p l) | |
3105 | (cond ((null? l) '()) | |
3106 | ((p (car l)) (remove-if p (cdr l))) | |
3107 | (else (cons (car l) (remove-if p (cdr l)))))) | |
3108 | ||
3109 | (define-public (delete-if! pred list) | |
3110 | (let delete-if ((list list)) | |
3111 | (cond ((null? list) '()) | |
3112 | ((pred (car list)) (delete-if (cdr list))) | |
3113 | (else | |
3114 | (set-cdr! list (delete-if (cdr list))) | |
3115 | list)))) | |
3116 | ||
3117 | (define-public (delete-if-not! pred list) | |
3118 | (let delete-if ((list list)) | |
3119 | (cond ((null? list) '()) | |
3120 | ((not (pred (car list))) (delete-if (cdr list))) | |
3121 | (else | |
3122 | (set-cdr! list (delete-if (cdr list))) | |
3123 | list)))) | |
3124 | ||
3125 | (define-public (butlast lst n) | |
3126 | (letrec ((l (- (length lst) n)) | |
3127 | (bl (lambda (lst n) | |
3128 | (cond ((null? lst) lst) | |
3129 | ((positive? n) | |
3130 | (cons (car lst) (bl (cdr lst) (+ -1 n)))) | |
3131 | (else '()))))) | |
3132 | (bl lst (if (negative? n) | |
52c5a23a | 3133 | (error "negative argument to butlast" n) |
0f2d19dd JB |
3134 | l)))) |
3135 | ||
3136 | (define-public (and? . args) | |
3137 | (cond ((null? args) #t) | |
3138 | ((car args) (apply and? (cdr args))) | |
3139 | (else #f))) | |
3140 | ||
3141 | (define-public (or? . args) | |
3142 | (cond ((null? args) #f) | |
3143 | ((car args) #t) | |
3144 | (else (apply or? (cdr args))))) | |
3145 | ||
3146 | (define-public (has-duplicates? lst) | |
3147 | (cond ((null? lst) #f) | |
3148 | ((member (car lst) (cdr lst)) #t) | |
3149 | (else (has-duplicates? (cdr lst))))) | |
3150 | ||
3151 | (define-public (list* x . y) | |
3152 | (define (list*1 x) | |
3153 | (if (null? (cdr x)) | |
3154 | (car x) | |
3155 | (cons (car x) (list*1 (cdr x))))) | |
3156 | (if (null? y) | |
3157 | x | |
3158 | (cons x (list*1 y)))) | |
3159 | ||
3160 | ;; pick p l | |
3161 | ;; Apply P to each element of L, returning a list of elts | |
3162 | ;; for which P returns a non-#f value. | |
3163 | ;; | |
3164 | (define-public (pick p l) | |
3165 | (let loop ((s '()) | |
3166 | (l l)) | |
3167 | (cond | |
3168 | ((null? l) s) | |
3169 | ((p (car l)) (loop (cons (car l) s) (cdr l))) | |
3170 | (else (loop s (cdr l)))))) | |
3171 | ||
3172 | ;; pick p l | |
3173 | ;; Apply P to each element of L, returning a list of the | |
3174 | ;; non-#f return values of P. | |
3175 | ;; | |
3176 | (define-public (pick-mappings p l) | |
3177 | (let loop ((s '()) | |
3178 | (l l)) | |
3179 | (cond | |
3180 | ((null? l) s) | |
3181 | ((p (car l)) => (lambda (mapping) (loop (cons mapping s) (cdr l)))) | |
3182 | (else (loop s (cdr l)))))) | |
3183 | ||
3184 | (define-public (uniq l) | |
3185 | (if (null? l) | |
3186 | '() | |
3187 | (let ((u (uniq (cdr l)))) | |
3188 | (if (memq (car l) u) | |
3189 | u | |
3190 | (cons (car l) u))))) | |
3191 | ||
3192 | \f | |
44cf1f0f JB |
3193 | ;;; {Functions for browsing modules} |
3194 | ||
0f2d19dd JB |
3195 | (define-module (ice-9 ls) |
3196 | :use-module (ice-9 common-list)) | |
3197 | ||
0f2d19dd JB |
3198 | ;;;; |
3199 | ;;; local-definitions-in root name | |
8b718458 JB |
3200 | ;;; Returns a list of names defined locally in the named |
3201 | ;;; subdirectory of root. | |
0f2d19dd | 3202 | ;;; definitions-in root name |
8b718458 JB |
3203 | ;;; Returns a list of all names defined in the named |
3204 | ;;; subdirectory of root. The list includes alll locally | |
3205 | ;;; defined names as well as all names inherited from a | |
3206 | ;;; member of a use-list. | |
0f2d19dd JB |
3207 | ;;; |
3208 | ;;; A convenient interface for examining the nature of things: | |
3209 | ;;; | |
3210 | ;;; ls . various-names | |
3211 | ;;; | |
8b718458 JB |
3212 | ;;; With just one argument, interpret that argument as the |
3213 | ;;; name of a subdirectory of the current module and | |
3214 | ;;; return a list of names defined there. | |
0f2d19dd | 3215 | ;;; |
8b718458 JB |
3216 | ;;; With more than one argument, still compute |
3217 | ;;; subdirectory lists, but return a list: | |
0f2d19dd JB |
3218 | ;;; ((<subdir-name> . <names-defined-there>) |
3219 | ;;; (<subdir-name> . <names-defined-there>) | |
3220 | ;;; ...) | |
3221 | ;;; | |
3222 | ||
3223 | (define-public (local-definitions-in root names) | |
0dd5491c | 3224 | (let ((m (nested-ref root names)) |
0f2d19dd JB |
3225 | (answer '())) |
3226 | (if (not (module? m)) | |
3227 | (set! answer m) | |
3228 | (module-for-each (lambda (k v) (set! answer (cons k answer))) m)) | |
3229 | answer)) | |
3230 | ||
3231 | (define-public (definitions-in root names) | |
0dd5491c | 3232 | (let ((m (nested-ref root names))) |
0f2d19dd JB |
3233 | (if (not (module? m)) |
3234 | m | |
3235 | (reduce union | |
3236 | (cons (local-definitions-in m '()) | |
8b718458 JB |
3237 | (map (lambda (m2) (definitions-in m2 '())) |
3238 | (module-uses m))))))) | |
0f2d19dd JB |
3239 | |
3240 | (define-public (ls . various-refs) | |
3241 | (and various-refs | |
3242 | (if (cdr various-refs) | |
3243 | (map (lambda (ref) | |
3244 | (cons ref (definitions-in (current-module) ref))) | |
3245 | various-refs) | |
3246 | (definitions-in (current-module) (car various-refs))))) | |
3247 | ||
3248 | (define-public (lls . various-refs) | |
3249 | (and various-refs | |
3250 | (if (cdr various-refs) | |
3251 | (map (lambda (ref) | |
3252 | (cons ref (local-definitions-in (current-module) ref))) | |
3253 | various-refs) | |
3254 | (local-definitions-in (current-module) (car various-refs))))) | |
3255 | ||
0dd5491c | 3256 | (define-public (recursive-local-define name value) |
0f2d19dd JB |
3257 | (let ((parent (reverse! (cdr (reverse name))))) |
3258 | (and parent (make-modules-in (current-module) parent)) | |
0dd5491c | 3259 | (local-define name value))) |
0f2d19dd | 3260 | \f |
44cf1f0f JB |
3261 | ;;; {Queues} |
3262 | ||
0f2d19dd JB |
3263 | (define-module (ice-9 q)) |
3264 | ||
3265 | ;;;; Copyright (C) 1995 Free Software Foundation, Inc. | |
3266 | ;;;; | |
3267 | ;;;; This program is free software; you can redistribute it and/or modify | |
3268 | ;;;; it under the terms of the GNU General Public License as published by | |
3269 | ;;;; the Free Software Foundation; either version 2, or (at your option) | |
3270 | ;;;; any later version. | |
3271 | ;;;; | |
3272 | ;;;; This program is distributed in the hope that it will be useful, | |
3273 | ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
3274 | ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
3275 | ;;;; GNU General Public License for more details. | |
3276 | ;;;; | |
3277 | ;;;; You should have received a copy of the GNU General Public License | |
3278 | ;;;; along with this software; see the file COPYING. If not, write to | |
15328041 JB |
3279 | ;;;; the Free Software Foundation, Inc., 59 Temple Place, Suite 330, |
3280 | ;;;; Boston, MA 02111-1307 USA | |
0f2d19dd JB |
3281 | ;;;; |
3282 | ||
0f2d19dd JB |
3283 | ;;;; |
3284 | ;;; Q: Based on the interface to | |
3285 | ;;; | |
3286 | ;;; "queue.scm" Queues/Stacks for Scheme | |
3287 | ;;; Written by Andrew Wilcox (awilcox@astro.psu.edu) on April 1, 1992. | |
3288 | ;;; | |
3289 | ||
0f2d19dd JB |
3290 | ;;;; |
3291 | ;;; {Q} | |
3292 | ;;; | |
3293 | ;;; A list is just a bunch of cons pairs that follows some constrains, right? | |
3294 | ;;; Association lists are the same. Hash tables are just vectors and association | |
3295 | ;;; lists. You can print them, read them, write them as constants, pun them off as other data | |
3296 | ;;; structures etc. This is good. This is lisp. These structures are fast and compact | |
3297 | ;;; and easy to manipulate arbitrarily because of their simple, regular structure and | |
3298 | ;;; non-disjointedness (associations being lists and so forth). | |
3299 | ;;; | |
3300 | ;;; So I figured, queues should be the same -- just a "subtype" of cons-pair | |
3301 | ;;; structures in general. | |
3302 | ;;; | |
3303 | ;;; A queue is a cons pair: | |
3304 | ;;; ( <the-q> . <last-pair> ) | |
3305 | ;;; | |
3306 | ;;; <the-q> is a list of things in the q. New elements go at the end of that list. | |
3307 | ;;; | |
3308 | ;;; <last-pair> is #f if the q is empty, and otherwise is the last pair of <the-q>. | |
3309 | ;;; | |
3310 | ;;; q's print nicely, but alas, they do not read well because the eq?-ness of | |
3311 | ;;; <last-pair> and (last-pair <the-q>) is lost by read. The procedure | |
3312 | ;;; | |
3313 | ;;; (sync-q! q) | |
3314 | ;;; | |
3315 | ;;; recomputes and resets the <last-pair> component of a queue. | |
3316 | ;;; | |
3317 | ||
3318 | (define-public (sync-q! obj) (set-cdr! obj (and (car obj) (last-pair (car obj))))) | |
3319 | ||
3320 | ;;; make-q | |
3321 | ;;; return a new q. | |
3322 | ;;; | |
3323 | (define-public (make-q) (cons '() '())) | |
3324 | ||
3325 | ;;; q? obj | |
3326 | ;;; Return true if obj is a Q. | |
3327 | ;;; An object is a queue if it is equal? to '(#f . #f) or | |
3328 | ;;; if it is a pair P with (list? (car P)) and (eq? (cdr P) (last-pair P)). | |
3329 | ;;; | |
3330 | (define-public (q? obj) (and (pair? obj) | |
3331 | (or (and (null? (car obj)) | |
3332 | (null? (cdr obj))) | |
3333 | (and | |
3334 | (list? (car obj)) | |
3335 | (eq? (cdr obj) (last-pair (car obj))))))) | |
3336 | ||
3337 | ;;; q-empty? obj | |
3338 | ;;; | |
3339 | (define-public (q-empty? obj) (null? (car obj))) | |
3340 | ||
3341 | ;;; q-empty-check q | |
3342 | ;;; Throw a q-empty exception if Q is empty. | |
3343 | (define-public (q-empty-check q) (if (q-empty? q) (throw 'q-empty q))) | |
3344 | ||
3345 | ||
3346 | ;;; q-front q | |
3347 | ;;; Return the first element of Q. | |
3348 | (define-public (q-front q) (q-empty-check q) (caar q)) | |
3349 | ||
52c5a23a | 3350 | ;;; q-rear q |
0f2d19dd JB |
3351 | ;;; Return the last element of Q. |
3352 | (define-public (q-rear q) (q-empty-check q) (cadr q)) | |
3353 | ||
3354 | ;;; q-remove! q obj | |
3355 | ;;; Remove all occurences of obj from Q. | |
3356 | (define-public (q-remove! q obj) | |
3357 | (while (memq obj (car q)) | |
3358 | (set-car! q (delq! obj (car q)))) | |
3359 | (set-cdr! q (last-pair (car q)))) | |
3360 | ||
3361 | ;;; q-push! q obj | |
3362 | ;;; Add obj to the front of Q | |
3363 | (define-public (q-push! q d) | |
3364 | (let ((h (cons d (car q)))) | |
3365 | (set-car! q h) | |
3366 | (if (null? (cdr q)) | |
3367 | (set-cdr! q h)))) | |
3368 | ||
3369 | ;;; enq! q obj | |
3370 | ;;; Add obj to the rear of Q | |
3371 | (define-public (enq! q d) | |
3372 | (let ((h (cons d '()))) | |
3373 | (if (not (null? (cdr q))) | |
3374 | (set-cdr! (cdr q) h) | |
3375 | (set-car! q h)) | |
3376 | (set-cdr! q h))) | |
3377 | ||
3378 | ;;; q-pop! q | |
3379 | ;;; Take the front of Q and return it. | |
3380 | (define-public (q-pop! q) | |
3381 | (q-empty-check q) | |
3382 | (let ((it (caar q)) | |
3383 | (next (cdar q))) | |
3384 | (if (not next) | |
3385 | (set-cdr! q #f)) | |
3386 | (set-car! q next) | |
3387 | it)) | |
3388 | ||
3389 | ;;; deq! q | |
3390 | ;;; Take the front of Q and return it. | |
3391 | (define-public deq! q-pop!) | |
3392 | ||
3393 | ;;; q-length q | |
3394 | ;;; Return the number of enqueued elements. | |
3395 | ;;; | |
3396 | (define-public (q-length q) (length (car q))) | |
3397 | ||
3398 | ||
3399 | ||
3400 | \f | |
44cf1f0f JB |
3401 | ;;; {The runq data structure} |
3402 | ||
0f2d19dd JB |
3403 | (define-module (ice-9 runq) |
3404 | :use-module (ice-9 q)) | |
3405 | ||
0f2d19dd JB |
3406 | ;;;; Copyright (C) 1996 Free Software Foundation, Inc. |
3407 | ;;;; | |
3408 | ;;;; This program is free software; you can redistribute it and/or modify | |
3409 | ;;;; it under the terms of the GNU General Public License as published by | |
3410 | ;;;; the Free Software Foundation; either version 2, or (at your option) | |
3411 | ;;;; any later version. | |
3412 | ;;;; | |
3413 | ;;;; This program is distributed in the hope that it will be useful, | |
3414 | ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
3415 | ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
3416 | ;;;; GNU General Public License for more details. | |
3417 | ;;;; | |
3418 | ;;;; You should have received a copy of the GNU General Public License | |
3419 | ;;;; along with this software; see the file COPYING. If not, write to | |
15328041 JB |
3420 | ;;;; the Free Software Foundation, Inc., 59 Temple Place, Suite 330, |
3421 | ;;;; Boston, MA 02111-1307 USA | |
0f2d19dd JB |
3422 | ;;;; |
3423 | ||
0f2d19dd | 3424 | ;;;; |
0f2d19dd JB |
3425 | ;;; |
3426 | ;;; One way to schedule parallel computations in a serial environment is | |
3427 | ;;; to explicitly divide each task up into small, finite execution time, | |
3428 | ;;; strips. Then you interleave the execution of strips from various | |
3429 | ;;; tasks to achieve a kind of parallelism. Runqs are a handy data | |
3430 | ;;; structure for this style of programming. | |
3431 | ;;; | |
3432 | ;;; We use thunks (nullary procedures) and lists of thunks to represent | |
3433 | ;;; strips. By convention, the return value of a strip-thunk must either | |
3434 | ;;; be another strip or the value #f. | |
3435 | ;;; | |
3436 | ;;; A runq is a procedure that manages a queue of strips. Called with no | |
3437 | ;;; arguments, it processes one strip from the queue. Called with | |
3438 | ;;; arguments, the arguments form a control message for the queue. The | |
3439 | ;;; first argument is a symbol which is the message selector. | |
3440 | ;;; | |
3441 | ;;; A strip is processed this way: If the strip is a thunk, the thunk is | |
3442 | ;;; called -- if it returns a strip, that strip is added back to the | |
3443 | ;;; queue. To process a strip which is a list of thunks, the CAR of that | |
3444 | ;;; list is called. After a call to that CAR, there are 0, 1, or 2 strips | |
3445 | ;;; -- perhaps one returned by the thunk, and perhaps the CDR of the | |
3446 | ;;; original strip if that CDR is not nil. The runq puts whichever of | |
3447 | ;;; these strips exist back on the queue. (The exact order in which | |
3448 | ;;; strips are put back on the queue determines the scheduling behavior of | |
3449 | ;;; a particular queue -- it's a parameter.) | |
3450 | ;;; | |
3451 | ;;; | |
3452 | ||
3453 | ||
3454 | ||
3455 | ;;;; | |
3456 | ;;; (runq-control q msg . args) | |
3457 | ;;; | |
3458 | ;;; processes in the default way the control messages that | |
3459 | ;;; can be sent to a runq. Q should be an ordinary | |
3460 | ;;; Q (see utils/q.scm). | |
3461 | ;;; | |
3462 | ;;; The standard runq messages are: | |
3463 | ;;; | |
3464 | ;;; 'add! strip0 strip1... ;; to enqueue one or more strips | |
3465 | ;;; 'enqueue! strip0 strip1... ;; to enqueue one or more strips | |
3466 | ;;; 'push! strip0 ... ;; add strips to the front of the queue | |
3467 | ;;; 'empty? ;; true if it is | |
3468 | ;;; 'length ;; how many strips in the queue? | |
3469 | ;;; 'kill! ;; empty the queue | |
3470 | ;;; else ;; throw 'not-understood | |
3471 | ;;; | |
3472 | (define-public (runq-control q msg . args) | |
3473 | (case msg | |
3474 | ((add!) (for-each (lambda (t) (enq! q t)) args) '*unspecified*) | |
3475 | ((enque!) (for-each (lambda (t) (enq! q t)) args) '*unspecified*) | |
3476 | ((push!) (for-each (lambda (t) (q-push! q t)) args) '*unspecified*) | |
3477 | ((empty?) (q-empty? q)) | |
3478 | ((length) (q-length q)) | |
3479 | ((kill!) (set! q (make-q))) | |
3480 | (else (throw 'not-understood msg args)))) | |
3481 | ||
3482 | (define (run-strip thunk) (catch #t thunk (lambda ign (warn 'runq-strip thunk ign) #f))) | |
3483 | ||
3484 | ;;;; | |
3485 | ;;; make-void-runq | |
3486 | ;;; | |
3487 | ;;; Make a runq that discards all messages except "length", for which | |
3488 | ;;; it returns 0. | |
3489 | ;;; | |
3490 | (define-public (make-void-runq) | |
3491 | (lambda opts | |
3492 | (and opts | |
3493 | (apply-to-args opts | |
3494 | (lambda (msg . args) | |
3495 | (case msg | |
3496 | ((length) 0) | |
3497 | (else #f))))))) | |
3498 | ||
3499 | ;;;; | |
3500 | ;;; (make-fair-runq) | |
3501 | ;;; | |
3502 | ;;; Returns a runq procedure. | |
3503 | ;;; Called with no arguments, the procedure processes one strip from the queue. | |
3504 | ;;; Called with arguments, it uses runq-control. | |
3505 | ;;; | |
3506 | ;;; In a fair runq, if a strip returns a new strip X, X is added | |
3507 | ;;; to the end of the queue, meaning it will be the last to execute | |
3508 | ;;; of all the remaining procedures. | |
3509 | ;;; | |
3510 | (define-public (make-fair-runq) | |
3511 | (letrec ((q (make-q)) | |
3512 | (self | |
3513 | (lambda ctl | |
3514 | (if ctl | |
3515 | (apply runq-control q ctl) | |
3516 | (and (not (q-empty? q)) | |
3517 | (let ((next-strip (deq! q))) | |
3518 | (cond | |
3519 | ((procedure? next-strip) (let ((k (run-strip next-strip))) | |
3520 | (and k (enq! q k)))) | |
3521 | ((pair? next-strip) (let ((k (run-strip (car next-strip)))) | |
3522 | (and k (enq! q k))) | |
3523 | (if (not (null? (cdr next-strip))) | |
3524 | (enq! q (cdr next-strip))))) | |
3525 | self)))))) | |
3526 | self)) | |
3527 | ||
3528 | ||
3529 | ;;;; | |
3530 | ;;; (make-exclusive-runq) | |
3531 | ;;; | |
3532 | ;;; Returns a runq procedure. | |
3533 | ;;; Called with no arguments, the procedure processes one strip from the queue. | |
3534 | ;;; Called with arguments, it uses runq-control. | |
3535 | ;;; | |
3536 | ;;; In an exclusive runq, if a strip W returns a new strip X, X is added | |
3537 | ;;; to the front of the queue, meaning it will be the next to execute | |
3538 | ;;; of all the remaining procedures. | |
3539 | ;;; | |
3540 | ;;; An exception to this occurs if W was the CAR of a list of strips. | |
3541 | ;;; In that case, after the return value of W is pushed onto the front | |
3542 | ;;; of the queue, the CDR of the list of strips is pushed in front | |
3543 | ;;; of that (if the CDR is not nil). This way, the rest of the thunks | |
3544 | ;;; in the list that contained W have priority over the return value of W. | |
3545 | ;;; | |
3546 | (define-public (make-exclusive-runq) | |
3547 | (letrec ((q (make-q)) | |
3548 | (self | |
3549 | (lambda ctl | |
3550 | (if ctl | |
3551 | (apply runq-control q ctl) | |
3552 | (and (not (q-empty? q)) | |
3553 | (let ((next-strip (deq! q))) | |
3554 | (cond | |
3555 | ((procedure? next-strip) (let ((k (run-strip next-strip))) | |
3556 | (and k (q-push! q k)))) | |
3557 | ((pair? next-strip) (let ((k (run-strip (car next-strip)))) | |
3558 | (and k (q-push! q k))) | |
3559 | (if (not (null? (cdr next-strip))) | |
3560 | (q-push! q (cdr next-strip))))) | |
3561 | self)))))) | |
3562 | self)) | |
3563 | ||
3564 | ||
3565 | ;;;; | |
3566 | ;;; (make-subordinate-runq-to superior basic-inferior) | |
3567 | ;;; | |
3568 | ;;; Returns a runq proxy for the runq basic-inferior. | |
3569 | ;;; | |
3570 | ;;; The proxy watches for operations on the basic-inferior that cause | |
3571 | ;;; a transition from a queue length of 0 to a non-zero length and | |
3572 | ;;; vice versa. While the basic-inferior queue is not empty, | |
3573 | ;;; the proxy installs a task on the superior runq. Each strip | |
3574 | ;;; of that task processes N strips from the basic-inferior where | |
3575 | ;;; N is the length of the basic-inferior queue when the proxy | |
3576 | ;;; strip is entered. [Countless scheduling variations are possible.] | |
3577 | ;;; | |
3578 | (define-public (make-subordinate-runq-to superior-runq basic-runq) | |
3579 | (let ((runq-task (cons #f #f))) | |
3580 | (set-car! runq-task | |
3581 | (lambda () | |
3582 | (if (basic-runq 'empty?) | |
3583 | (set-cdr! runq-task #f) | |
3584 | (do ((n (basic-runq 'length) (1- n))) | |
3585 | ((<= n 0) #f) | |
3586 | (basic-runq))))) | |
3587 | (letrec ((self | |
3588 | (lambda ctl | |
3589 | (if (not ctl) | |
3590 | (let ((answer (basic-runq))) | |
3591 | (self 'empty?) | |
3592 | answer) | |
3593 | (begin | |
3594 | (case (car ctl) | |
3595 | ((suspend) (set-cdr! runq-task #f)) | |
3596 | (else (let ((answer (apply basic-runq ctl))) | |
3597 | (if (and (not (cdr runq-task)) (not (basic-runq 'empty?))) | |
3598 | (begin | |
3599 | (set-cdr! runq-task runq-task) | |
3600 | (superior-runq 'add! runq-task))) | |
3601 | answer)))))))) | |
3602 | self))) | |
3603 | ||
3604 | ;;;; | |
3605 | ;;; (define fork-strips (lambda args args)) | |
3606 | ;;; Return a strip that starts several strips in | |
3607 | ;;; parallel. If this strip is enqueued on a fair | |
3608 | ;;; runq, strips of the parallel subtasks will run | |
3609 | ;;; round-robin style. | |
3610 | ;;; | |
3611 | (define fork-strips (lambda args args)) | |
3612 | ||
3613 | ||
3614 | ;;;; | |
3615 | ;;; (strip-sequence . strips) | |
3616 | ;;; | |
3617 | ;;; Returns a new strip which is the concatenation of the argument strips. | |
3618 | ;;; | |
3619 | (define-public ((strip-sequence . strips)) | |
3620 | (let loop ((st (let ((a strips)) (set! strips #f) a))) | |
3621 | (and (not (null? st)) | |
3622 | (let ((then ((car st)))) | |
3623 | (if then | |
3624 | (lambda () (loop (cons then (cdr st)))) | |
3625 | (lambda () (loop (cdr st)))))))) | |
3626 | ||
3627 | ||
3628 | ;;;; | |
3629 | ;;; (fair-strip-subtask . initial-strips) | |
3630 | ;;; | |
3631 | ;;; Returns a new strip which is the synchronos, fair, | |
3632 | ;;; parallel execution of the argument strips. | |
3633 | ;;; | |
3634 | ;;; | |
3635 | ;;; | |
3636 | (define-public (fair-strip-subtask . initial-strips) | |
3637 | (let ((st (make-fair-runq))) | |
3638 | (apply st 'add! initial-strips) | |
3639 | st)) | |
3640 | ||
3641 | \f | |
44cf1f0f | 3642 | ;;; {String Fun} |
0f2d19dd | 3643 | |
0f2d19dd JB |
3644 | (define-module (ice-9 string-fun)) |
3645 | ||
0f2d19dd | 3646 | ;;;; |
0f2d19dd JB |
3647 | ;;; |
3648 | ;;; Various string funcitons, particularly those that take | |
3649 | ;;; advantage of the "shared substring" capability. | |
3650 | ;;; | |
3651 | \f | |
44cf1f0f | 3652 | ;;; {String Fun: Dividing Strings Into Fields} |
0f2d19dd JB |
3653 | ;;; |
3654 | ;;; The names of these functions are very regular. | |
3655 | ;;; Here is a grammar of a call to one of these: | |
3656 | ;;; | |
3657 | ;;; <string-function-invocation> | |
3658 | ;;; := (<action>-<seperator-disposition>-<seperator-determination> <seperator-param> <str> <ret>) | |
3659 | ;;; | |
3660 | ;;; <str> = the string | |
3661 | ;;; | |
3662 | ;;; <ret> = The continuation. String functions generally return | |
3663 | ;;; multiple values by passing them to this procedure. | |
3664 | ;;; | |
3665 | ;;; <action> = split | |
3666 | ;;; | separate-fields | |
3667 | ;;; | |
3668 | ;;; "split" means to divide a string into two parts. | |
3669 | ;;; <ret> will be called with two arguments. | |
3670 | ;;; | |
3671 | ;;; "separate-fields" means to divide a string into as many | |
3672 | ;;; parts as possible. <ret> will be called with | |
3673 | ;;; however many fields are found. | |
3674 | ;;; | |
3675 | ;;; <seperator-disposition> = before | |
3676 | ;;; | after | |
3677 | ;;; | discarding | |
3678 | ;;; | |
3679 | ;;; "before" means to leave the seperator attached to | |
3680 | ;;; the beginning of the field to its right. | |
3681 | ;;; "after" means to leave the seperator attached to | |
3682 | ;;; the end of the field to its left. | |
3683 | ;;; "discarding" means to discard seperators. | |
3684 | ;;; | |
3685 | ;;; Other dispositions might be handy. For example, "isolate" | |
3686 | ;;; could mean to treat the separator as a field unto itself. | |
3687 | ;;; | |
3688 | ;;; <seperator-determination> = char | |
3689 | ;;; | predicate | |
3690 | ;;; | |
3691 | ;;; "char" means to use a particular character as field seperator. | |
3692 | ;;; "predicate" means to check each character using a particular predicate. | |
3693 | ;;; | |
3694 | ;;; Other determinations might be handy. For example, "character-set-member". | |
3695 | ;;; | |
3696 | ;;; <seperator-param> = A parameter that completes the meaning of the determinations. | |
3697 | ;;; For example, if the determination is "char", then this parameter | |
3698 | ;;; says which character. If it is "predicate", the parameter is the | |
3699 | ;;; predicate. | |
3700 | ;;; | |
3701 | ;;; | |
3702 | ;;; For example: | |
3703 | ;;; | |
3704 | ;;; (separate-fields-discarding-char #\, "foo, bar, baz, , bat" list) | |
3705 | ;;; => ("foo" " bar" " baz" " " " bat") | |
3706 | ;;; | |
3707 | ;;; (split-after-char #\- 'an-example-of-split list) | |
3708 | ;;; => ("an-" "example-of-split") | |
3709 | ;;; | |
3710 | ;;; As an alternative to using a determination "predicate", or to trying to do anything | |
3711 | ;;; complicated with these functions, consider using regular expressions. | |
3712 | ;;; | |
3713 | ||
3714 | (define-public (split-after-char char str ret) | |
3715 | (let ((end (cond | |
3716 | ((string-index str char) => 1+) | |
3717 | (else (string-length str))))) | |
3718 | (ret (make-shared-substring str 0 end) | |
3719 | (make-shared-substring str end)))) | |
3720 | ||
3721 | (define-public (split-before-char char str ret) | |
3722 | (let ((end (or (string-index str char) | |
3723 | (string-length str)))) | |
3724 | (ret (make-shared-substring str 0 end) | |
3725 | (make-shared-substring str end)))) | |
3726 | ||
3727 | (define-public (split-discarding-char char str ret) | |
3728 | (let ((end (string-index str char))) | |
3729 | (if (not end) | |
3730 | (ret str "") | |
3731 | (ret (make-shared-substring str 0 end) | |
3732 | (make-shared-substring str (1+ end)))))) | |
3733 | ||
3734 | (define-public (split-after-char-last char str ret) | |
3735 | (let ((end (cond | |
3736 | ((string-rindex str char) => 1+) | |
3737 | (else 0)))) | |
3738 | (ret (make-shared-substring str 0 end) | |
3739 | (make-shared-substring str end)))) | |
3740 | ||
3741 | (define-public (split-before-char-last char str ret) | |
3742 | (let ((end (or (string-rindex str char) 0))) | |
3743 | (ret (make-shared-substring str 0 end) | |
3744 | (make-shared-substring str end)))) | |
3745 | ||
3746 | (define-public (split-discarding-char-last char str ret) | |
3747 | (let ((end (string-rindex str char))) | |
3748 | (if (not end) | |
3749 | (ret str "") | |
3750 | (ret (make-shared-substring str 0 end) | |
3751 | (make-shared-substring str (1+ end)))))) | |
3752 | ||
3753 | (define (split-before-predicate pred str ret) | |
3754 | (let loop ((n 0)) | |
3755 | (cond | |
3756 | ((= n (length str)) (ret str "")) | |
3757 | ((not (pred (string-ref str n))) (loop (1+ n))) | |
3758 | (else (ret (make-shared-substring str 0 n) | |
3759 | (make-shared-substring str n)))))) | |
3760 | (define (split-after-predicate pred str ret) | |
3761 | (let loop ((n 0)) | |
3762 | (cond | |
3763 | ((= n (length str)) (ret str "")) | |
3764 | ((not (pred (string-ref str n))) (loop (1+ n))) | |
3765 | (else (ret (make-shared-substring str 0 (1+ n)) | |
3766 | (make-shared-substring str (1+ n))))))) | |
3767 | ||
3768 | (define (split-discarding-predicate pred str ret) | |
3769 | (let loop ((n 0)) | |
3770 | (cond | |
3771 | ((= n (length str)) (ret str "")) | |
3772 | ((not (pred (string-ref str n))) (loop (1+ n))) | |
3773 | (else (ret (make-shared-substring str 0 n) | |
3774 | (make-shared-substring str (1+ n))))))) | |
3775 | ||
21ed9efe | 3776 | (define-public (separate-fields-discarding-char ch str ret) |
0f2d19dd JB |
3777 | (let loop ((fields '()) |
3778 | (str str)) | |
3779 | (cond | |
3780 | ((string-rindex str ch) | |
3781 | => (lambda (pos) (loop (cons (make-shared-substring str (+ 1 w)) fields) | |
3782 | (make-shared-substring str 0 w)))) | |
3783 | (else (ret (cons str fields)))))) | |
3784 | ||
21ed9efe | 3785 | (define-public (separate-fields-after-char ch str ret) |
0f2d19dd JB |
3786 | (let loop ((fields '()) |
3787 | (str str)) | |
3788 | (cond | |
3789 | ((string-rindex str ch) | |
3790 | => (lambda (pos) (loop (cons (make-shared-substring str (+ 1 w)) fields) | |
3791 | (make-shared-substring str 0 (+ 1 w))))) | |
3792 | (else (ret (cons str fields)))))) | |
3793 | ||
21ed9efe | 3794 | (define-public (separate-fields-before-char ch str ret) |
0f2d19dd JB |
3795 | (let loop ((fields '()) |
3796 | (str str)) | |
3797 | (cond | |
3798 | ((string-rindex str ch) | |
3799 | => (lambda (pos) (loop (cons (make-shared-substring str w) fields) | |
3800 | (make-shared-substring str 0 w)))) | |
3801 | (else (ret (cons str fields)))))) | |
3802 | ||
3803 | \f | |
44cf1f0f | 3804 | ;;; {String Fun: String Prefix Predicates} |
0f2d19dd JB |
3805 | ;;; |
3806 | ;;; Very simple: | |
3807 | ;;; | |
21ed9efe | 3808 | ;;; (define-public ((string-prefix-predicate pred?) prefix str) |
0f2d19dd JB |
3809 | ;;; (and (<= (length prefix) (length str)) |
3810 | ;;; (pred? prefix (make-shared-substring str 0 (length prefix))))) | |
3811 | ;;; | |
3812 | ;;; (define-public string-prefix=? (string-prefix-predicate string=?)) | |
3813 | ;;; | |
3814 | ||
3815 | (define-public ((string-prefix-predicate pred?) prefix str) | |
3816 | (and (<= (length prefix) (length str)) | |
3817 | (pred? prefix (make-shared-substring str 0 (length prefix))))) | |
3818 | ||
3819 | (define-public string-prefix=? (string-prefix-predicate string=?)) | |
3820 | ||
3821 | \f | |
44cf1f0f | 3822 | ;;; {String Fun: Strippers} |
0f2d19dd JB |
3823 | ;;; |
3824 | ;;; <stripper> = sans-<removable-part> | |
3825 | ;;; | |
3826 | ;;; <removable-part> = surrounding-whitespace | |
3827 | ;;; | trailing-whitespace | |
3828 | ;;; | leading-whitespace | |
3829 | ;;; | final-newline | |
3830 | ;;; | |
3831 | ||
3832 | (define-public (sans-surrounding-whitespace s) | |
3833 | (let ((st 0) | |
3834 | (end (string-length s))) | |
3835 | (while (and (< st (string-length s)) | |
3836 | (char-whitespace? (string-ref s st))) | |
3837 | (set! st (1+ st))) | |
3838 | (while (and (< 0 end) | |
3839 | (char-whitespace? (string-ref s (1- end)))) | |
3840 | (set! end (1- end))) | |
3841 | (if (< end st) | |
3842 | "" | |
3843 | (make-shared-substring s st end)))) | |
3844 | ||
3845 | (define-public (sans-trailing-whitespace s) | |
3846 | (let ((st 0) | |
3847 | (end (string-length s))) | |
3848 | (while (and (< 0 end) | |
3849 | (char-whitespace? (string-ref s (1- end)))) | |
3850 | (set! end (1- end))) | |
3851 | (if (< end st) | |
3852 | "" | |
3853 | (make-shared-substring s st end)))) | |
3854 | ||
3855 | (define-public (sans-leading-whitespace s) | |
3856 | (let ((st 0) | |
3857 | (end (string-length s))) | |
3858 | (while (and (< st (string-length s)) | |
3859 | (char-whitespace? (string-ref s st))) | |
3860 | (set! st (1+ st))) | |
3861 | (if (< end st) | |
3862 | "" | |
3863 | (make-shared-substring s st end)))) | |
3864 | ||
3865 | (define-public (sans-final-newline str) | |
3866 | (cond | |
3867 | ((= 0 (string-length str)) | |
3868 | str) | |
3869 | ||
3870 | ((char=? #\nl (string-ref str (1- (string-length str)))) | |
3871 | (make-shared-substring str 0 (1- (string-length str)))) | |
3872 | ||
3873 | (else str))) | |
3874 | \f | |
44cf1f0f | 3875 | ;;; {String Fun: has-trailing-newline?} |
0f2d19dd JB |
3876 | ;;; |
3877 | ||
3878 | (define-public (has-trailing-newline? str) | |
3879 | (and (< 0 (string-length str)) | |
3880 | (char=? #\nl (string-ref str (1- (string-length str)))))) | |
3881 | ||
3882 | ||
3883 | \f | |
44cf1f0f | 3884 | ;;; {String Fun: with-regexp-parts} |
0f2d19dd | 3885 | |
52c5a23a JB |
3886 | ;;; This relies on the older, hairier regexp interface, which we don't |
3887 | ;;; particularly want to implement, and it's not used anywhere, so | |
3888 | ;;; we're just going to drop it for now. | |
3889 | ;;; (define-public (with-regexp-parts regexp fields str return fail) | |
3890 | ;;; (let ((parts (regexec regexp str fields))) | |
3891 | ;;; (if (number? parts) | |
3892 | ;;; (fail parts) | |
3893 | ;;; (apply return parts)))) | |
0f2d19dd JB |
3894 | |
3895 | \f | |
c56634ba MD |
3896 | ;;; {Load debug extension code if debug extensions present.} |
3897 | ;;; | |
3898 | ;;; *fixme* This is a temporary solution. | |
3899 | ;;; | |
0f2d19dd | 3900 | |
c56634ba | 3901 | (if (memq 'debug-extensions *features*) |
90895e5c MD |
3902 | (define-module (guile) :use-module (ice-9 debug))) |
3903 | ||
3904 | \f | |
90d5e280 MD |
3905 | ;;; {Load session support if present.} |
3906 | ;;; | |
3907 | ;;; *fixme* This is a temporary solution. | |
3908 | ;;; | |
3909 | ||
3910 | (if (%search-load-path "ice-9/session.scm") | |
3911 | (define-module (guile) :use-module (ice-9 session))) | |
3912 | ||
3913 | \f | |
90895e5c MD |
3914 | ;;; {Load thread code if threads are present.} |
3915 | ;;; | |
3916 | ;;; *fixme* This is a temporary solution. | |
3917 | ;;; | |
3918 | ||
3919 | (if (memq 'threads *features*) | |
3920 | (define-module (guile) :use-module (ice-9 threads))) | |
3921 | ||
21ed9efe MD |
3922 | \f |
3923 | ;;; {Load emacs interface support if emacs option is given.} | |
3924 | ;;; | |
3925 | ;;; *fixme* This is a temporary solution. | |
3926 | ;;; | |
3927 | ||
2e3fbd8d MD |
3928 | (if (and (module-defined? the-root-module 'use-emacs-interface) |
3929 | use-emacs-interface) | |
8309a10d MD |
3930 | (begin |
3931 | (if (memq 'debug-extensions *features*) | |
3932 | (debug-enable 'backtrace)) | |
3933 | (define-module (guile) :use-module (ice-9 emacs)))) | |
21ed9efe MD |
3934 | |
3935 | \f | |
05817d9e JB |
3936 | ;;; {Load regexp code if regexp primitives are available.} |
3937 | ||
3938 | (if (memq 'regex *features*) | |
3939 | (define-module (guile) :use-module (ice-9 regex))) | |
3940 | ||
3941 | \f | |
9946dd45 JB |
3942 | ;;; {Check that the interpreter and scheme code match up.} |
3943 | ||
3944 | (let ((show-line | |
3945 | (lambda args | |
3946 | (with-output-to-port (current-error-port) | |
3947 | (lambda () | |
3948 | (display (car (command-line))) | |
3949 | (display ": ") | |
3950 | (for-each (lambda (string) (display string)) | |
3951 | args) | |
3952 | (newline)))))) | |
3953 | ||
3954 | (load-from-path "ice-9/version.scm") | |
3955 | ||
3956 | (if (not (string=? | |
3957 | (libguile-config-stamp) ; from the interprpreter | |
3958 | (ice-9-config-stamp))) ; from the Scheme code | |
3959 | (begin | |
3960 | (show-line "warning: different versions of libguile and ice-9:") | |
3961 | (show-line "libguile: configured on " (libguile-config-stamp)) | |
3962 | (show-line "ice-9: configured on " (ice-9-config-stamp))))) | |
3963 | ||
3964 | \f | |
21ed9efe | 3965 | |
90895e5c | 3966 | (define-module (guile)) |
6fa8995c GH |
3967 | |
3968 | (append! %load-path (cons "." ())) |