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