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