Commit | Line | Data |
---|---|---|
0f2d19dd JB |
1 | ;;; installed-scm-file |
2 | ||
6906bd0d | 3 | ;;;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000 Free Software Foundation, Inc. |
0f2d19dd JB |
4 | ;;;; |
5 | ;;;; This program is free software; you can redistribute it and/or modify | |
6 | ;;;; it under the terms of the GNU General Public License as published by | |
7 | ;;;; the Free Software Foundation; either version 2, or (at your option) | |
8 | ;;;; any later version. | |
9 | ;;;; | |
10 | ;;;; This program is distributed in the hope that it will be useful, | |
11 | ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | ;;;; GNU General Public License for more details. | |
14 | ;;;; | |
15 | ;;;; You should have received a copy of the GNU General Public License | |
16 | ;;;; along with this software; see the file COPYING. If not, write to | |
15328041 JB |
17 | ;;;; the Free Software Foundation, Inc., 59 Temple Place, Suite 330, |
18 | ;;;; Boston, MA 02111-1307 USA | |
0f2d19dd JB |
19 | ;;;; |
20 | \f | |
21 | ||
22 | ;;; This file is the first thing loaded into Guile. It adds many mundane | |
23 | ;;; definitions and a few that are interesting. | |
24 | ;;; | |
25 | ;;; The module system (hence the hierarchical namespace) are defined in this | |
26 | ;;; file. | |
27 | ;;; | |
28 | ||
29 | \f | |
21ed9efe MD |
30 | ;;; {Features} |
31 | ;; | |
32 | ||
33 | (define (provide sym) | |
34 | (if (not (memq sym *features*)) | |
35 | (set! *features* (cons sym *features*)))) | |
36 | ||
50706e94 JB |
37 | ;;; Return #t iff FEATURE is available to this Guile interpreter. |
38 | ;;; In SLIB, provided? also checks to see if the module is available. | |
39 | ;;; We should do that too, but don't. | |
40 | (define (provided? feature) | |
41 | (and (memq feature *features*) #t)) | |
42 | ||
52cfc69b GH |
43 | ;;; presumably deprecated. |
44 | (define feature? provided?) | |
45 | ||
8641dd9e GB |
46 | ;;; let format alias simple-format until the more complete version is loaded |
47 | (define format simple-format) | |
48 | ||
21ed9efe | 49 | \f |
79451588 JB |
50 | ;;; {R4RS compliance} |
51 | ||
52 | (primitive-load-path "ice-9/r4rs.scm") | |
53 | ||
54 | \f | |
44cf1f0f | 55 | ;;; {Simple Debugging Tools} |
0f2d19dd JB |
56 | ;; |
57 | ||
58 | ||
59 | ;; peek takes any number of arguments, writes them to the | |
60 | ;; current ouput port, and returns the last argument. | |
61 | ;; It is handy to wrap around an expression to look at | |
62 | ;; a value each time is evaluated, e.g.: | |
63 | ;; | |
64 | ;; (+ 10 (troublesome-fn)) | |
65 | ;; => (+ 10 (pk 'troublesome-fn-returned (troublesome-fn))) | |
66 | ;; | |
67 | ||
68 | (define (peek . stuff) | |
69 | (newline) | |
70 | (display ";;; ") | |
71 | (write stuff) | |
72 | (newline) | |
73 | (car (last-pair stuff))) | |
74 | ||
75 | (define pk peek) | |
76 | ||
77 | (define (warn . stuff) | |
78 | (with-output-to-port (current-error-port) | |
79 | (lambda () | |
80 | (newline) | |
81 | (display ";;; WARNING ") | |
6355358a | 82 | (display stuff) |
0f2d19dd JB |
83 | (newline) |
84 | (car (last-pair stuff))))) | |
85 | ||
86 | \f | |
79451588 | 87 | ;;; {Trivial Functions} |
0f2d19dd | 88 | ;;; |
79451588 JB |
89 | |
90 | (define (id x) x) | |
91 | (define (1+ n) (+ n 1)) | |
92 | (define (-1+ n) (+ n -1)) | |
93 | (define 1- -1+) | |
94 | (define return-it noop) | |
132e5fac | 95 | (define (and=> value procedure) (and value (procedure value))) |
79451588 JB |
96 | (define (make-hash-table k) (make-vector k '())) |
97 | ||
0f2d19dd JB |
98 | ;;; apply-to-args is functionally redunant with apply and, worse, |
99 | ;;; is less general than apply since it only takes two arguments. | |
100 | ;;; | |
101 | ;;; On the other hand, apply-to-args is a syntacticly convenient way to | |
102 | ;;; perform binding in many circumstances when the "let" family of | |
103 | ;;; of forms don't cut it. E.g.: | |
104 | ;;; | |
105 | ;;; (apply-to-args (return-3d-mouse-coords) | |
106 | ;;; (lambda (x y z) | |
107 | ;;; ...)) | |
108 | ;;; | |
109 | ||
110 | (define (apply-to-args args fn) (apply fn args)) | |
111 | ||
112 | \f | |
0f2d19dd JB |
113 | ;;; {Integer Math} |
114 | ;;; | |
115 | ||
0f2d19dd JB |
116 | (define (ipow-by-squaring x k acc proc) |
117 | (cond ((zero? k) acc) | |
118 | ((= 1 k) (proc acc x)) | |
52c5a23a JB |
119 | (else (ipow-by-squaring (proc x x) |
120 | (quotient k 2) | |
121 | (if (even? k) acc (proc acc x)) | |
122 | proc)))) | |
0f2d19dd JB |
123 | |
124 | (define string-character-length string-length) | |
125 | ||
126 | ||
127 | ||
128 | ;; A convenience function for combining flag bits. Like logior, but | |
129 | ;; handles the cases of 0 and 1 arguments. | |
130 | ;; | |
131 | (define (flags . args) | |
132 | (cond | |
133 | ((null? args) 0) | |
134 | ((null? (cdr args)) (car args)) | |
135 | (else (apply logior args)))) | |
136 | ||
137 | \f | |
0f2d19dd JB |
138 | ;;; {Symbol Properties} |
139 | ;;; | |
140 | ||
141 | (define (symbol-property sym prop) | |
142 | (let ((pair (assoc prop (symbol-pref sym)))) | |
143 | (and pair (cdr pair)))) | |
144 | ||
145 | (define (set-symbol-property! sym prop val) | |
146 | (let ((pair (assoc prop (symbol-pref sym)))) | |
147 | (if pair | |
148 | (set-cdr! pair val) | |
149 | (symbol-pset! sym (acons prop val (symbol-pref sym)))))) | |
150 | ||
151 | (define (symbol-property-remove! sym prop) | |
152 | (let ((pair (assoc prop (symbol-pref sym)))) | |
153 | (if pair | |
154 | (symbol-pset! sym (delq! pair (symbol-pref sym)))))) | |
155 | ||
156 | \f | |
1e531c3a GH |
157 | |
158 | ;;; {Line and Delimited I/O} | |
159 | ||
160 | ;;; corresponds to SCM_LINE_INCREMENTORS in libguile. | |
161 | (define scm-line-incrementors "\n") | |
162 | ||
163 | (define (read-line! string . maybe-port) | |
164 | (let* ((port (if (pair? maybe-port) | |
165 | (car maybe-port) | |
166 | (current-input-port)))) | |
167 | (let* ((rv (%read-delimited! scm-line-incrementors | |
168 | string | |
169 | #t | |
170 | port)) | |
171 | (terminator (car rv)) | |
172 | (nchars (cdr rv))) | |
173 | (cond ((and (= nchars 0) | |
174 | (eof-object? terminator)) | |
175 | terminator) | |
176 | ((not terminator) #f) | |
177 | (else nchars))))) | |
178 | ||
179 | (define (read-delimited! delims buf . args) | |
180 | (let* ((num-args (length args)) | |
181 | (port (if (> num-args 0) | |
182 | (car args) | |
183 | (current-input-port))) | |
184 | (handle-delim (if (> num-args 1) | |
185 | (cadr args) | |
186 | 'trim)) | |
187 | (start (if (> num-args 2) | |
188 | (caddr args) | |
189 | 0)) | |
190 | (end (if (> num-args 3) | |
191 | (cadddr args) | |
192 | (string-length buf)))) | |
193 | (let* ((rv (%read-delimited! delims | |
194 | buf | |
195 | (not (eq? handle-delim 'peek)) | |
196 | port | |
197 | start | |
198 | end)) | |
199 | (terminator (car rv)) | |
200 | (nchars (cdr rv))) | |
201 | (cond ((or (not terminator) ; buffer filled | |
202 | (eof-object? terminator)) | |
203 | (if (zero? nchars) | |
204 | (if (eq? handle-delim 'split) | |
205 | (cons terminator terminator) | |
206 | terminator) | |
207 | (if (eq? handle-delim 'split) | |
208 | (cons nchars terminator) | |
209 | nchars))) | |
210 | (else | |
211 | (case handle-delim | |
212 | ((trim peek) nchars) | |
e3306d80 | 213 | ((concat) (string-set! buf (+ nchars start) terminator) |
1e531c3a GH |
214 | (+ nchars 1)) |
215 | ((split) (cons nchars terminator)) | |
216 | (else (error "unexpected handle-delim value: " | |
217 | handle-delim)))))))) | |
218 | ||
219 | (define (read-delimited delims . args) | |
220 | (let* ((port (if (pair? args) | |
221 | (let ((pt (car args))) | |
222 | (set! args (cdr args)) | |
223 | pt) | |
224 | (current-input-port))) | |
225 | (handle-delim (if (pair? args) | |
226 | (car args) | |
227 | 'trim))) | |
228 | (let loop ((substrings ()) | |
229 | (total-chars 0) | |
230 | (buf-size 100)) ; doubled each time through. | |
231 | (let* ((buf (make-string buf-size)) | |
232 | (rv (%read-delimited! delims | |
233 | buf | |
234 | (not (eq? handle-delim 'peek)) | |
235 | port)) | |
236 | (terminator (car rv)) | |
237 | (nchars (cdr rv)) | |
238 | (join-substrings | |
239 | (lambda () | |
240 | (apply string-append | |
241 | (reverse | |
242 | (cons (if (and (eq? handle-delim 'concat) | |
243 | (not (eof-object? terminator))) | |
244 | (string terminator) | |
245 | "") | |
246 | (cons (make-shared-substring buf 0 nchars) | |
247 | substrings)))))) | |
248 | (new-total (+ total-chars nchars))) | |
249 | (cond ((not terminator) | |
250 | ;; buffer filled. | |
251 | (loop (cons (substring buf 0 nchars) substrings) | |
252 | new-total | |
253 | (* buf-size 2))) | |
254 | ((eof-object? terminator) | |
255 | (if (zero? new-total) | |
256 | (if (eq? handle-delim 'split) | |
257 | (cons terminator terminator) | |
258 | terminator) | |
259 | (if (eq? handle-delim 'split) | |
260 | (cons (join-substrings) terminator) | |
261 | (join-substrings)))) | |
262 | (else | |
263 | (case handle-delim | |
264 | ((trim peek concat) (join-substrings)) | |
265 | ((split) (cons (join-substrings) terminator)) | |
848f2a01 TP |
266 | |
267 | ||
1e531c3a GH |
268 | (else (error "unexpected handle-delim value: " |
269 | handle-delim))))))))) | |
848f2a01 TP |
270 | |
271 | ;;; read-line [PORT [HANDLE-DELIM]] reads a newline-terminated string | |
272 | ;;; from PORT. The return value depends on the value of HANDLE-DELIM, | |
273 | ;;; which may be one of the symbols `trim', `concat', `peek' and | |
274 | ;;; `split'. If it is `trim' (the default), the trailing newline is | |
275 | ;;; removed and the string is returned. If `concat', the string is | |
276 | ;;; returned with the trailing newline intact. If `peek', the newline | |
277 | ;;; is left in the input port buffer and the string is returned. If | |
278 | ;;; `split', the newline is split from the string and read-line | |
279 | ;;; returns a pair consisting of the truncated string and the newline. | |
280 | ||
1e531c3a | 281 | (define (read-line . args) |
848f2a01 TP |
282 | (let* ((port (if (null? args) |
283 | (current-input-port) | |
284 | (car args))) | |
285 | (handle-delim (if (> (length args) 1) | |
286 | (cadr args) | |
287 | 'trim)) | |
288 | (line/delim (%read-line port)) | |
289 | (line (car line/delim)) | |
290 | (delim (cdr line/delim))) | |
291 | (case handle-delim | |
292 | ((trim) line) | |
293 | ((split) line/delim) | |
294 | ((concat) (if (and (string? line) (char? delim)) | |
295 | (string-append line (string delim)) | |
296 | line)) | |
297 | ((peek) (if (char? delim) | |
298 | (unread-char delim port)) | |
299 | line) | |
300 | (else | |
301 | (error "unexpected handle-delim value: " handle-delim))))) | |
1e531c3a GH |
302 | |
303 | \f | |
0f2d19dd JB |
304 | ;;; {Arrays} |
305 | ;;; | |
306 | ||
afe5177e GH |
307 | (if (provided? 'array) |
308 | (primitive-load-path "ice-9/arrays.scm")) | |
0f2d19dd JB |
309 | |
310 | \f | |
311 | ;;; {Keywords} | |
312 | ;;; | |
313 | ||
314 | (define (symbol->keyword symbol) | |
315 | (make-keyword-from-dash-symbol (symbol-append '- symbol))) | |
316 | ||
317 | (define (keyword->symbol kw) | |
318 | (let ((sym (keyword-dash-symbol kw))) | |
11b05261 | 319 | (string->symbol (substring sym 1 (string-length sym))))) |
0f2d19dd JB |
320 | |
321 | (define (kw-arg-ref args kw) | |
322 | (let ((rem (member kw args))) | |
323 | (and rem (pair? (cdr rem)) (cadr rem)))) | |
324 | ||
325 | \f | |
fa7e9274 | 326 | |
9f9aa47b | 327 | ;;; {Structs} |
fa7e9274 MV |
328 | |
329 | (define (struct-layout s) | |
9f9aa47b | 330 | (struct-ref (struct-vtable s) vtable-index-layout)) |
fa7e9274 MV |
331 | |
332 | \f | |
d7faeb2e MD |
333 | |
334 | ;;; Environments | |
335 | ||
336 | (define the-environment | |
337 | (procedure->syntax | |
338 | (lambda (x e) | |
339 | e))) | |
340 | ||
341 | (define the-root-environment (the-environment)) | |
342 | ||
343 | (define (environment-module env) | |
344 | (let ((closure (and (pair? env) (car (last-pair env))))) | |
345 | (and closure (procedure-property closure 'module)))) | |
346 | ||
347 | \f | |
0f2d19dd JB |
348 | ;;; {Records} |
349 | ;;; | |
350 | ||
fa7e9274 MV |
351 | ;; Printing records: by default, records are printed as |
352 | ;; | |
353 | ;; #<type-name field1: val1 field2: val2 ...> | |
354 | ;; | |
355 | ;; You can change that by giving a custom printing function to | |
356 | ;; MAKE-RECORD-TYPE (after the list of field symbols). This function | |
357 | ;; will be called like | |
358 | ;; | |
359 | ;; (<printer> object port) | |
360 | ;; | |
361 | ;; It should print OBJECT to PORT. | |
362 | ||
cf8f1a90 | 363 | (define (inherit-print-state old-port new-port) |
8a30733e MD |
364 | (if (get-print-state old-port) |
365 | (port-with-print-state new-port (get-print-state old-port)) | |
cf8f1a90 MV |
366 | new-port)) |
367 | ||
9f9aa47b | 368 | ;; 0: type-name, 1: fields |
fa7e9274 | 369 | (define record-type-vtable |
9f9aa47b MD |
370 | (make-vtable-vtable "prpr" 0 |
371 | (lambda (s p) | |
372 | (cond ((eq? s record-type-vtable) | |
373 | (display "#<record-type-vtable>" p)) | |
374 | (else | |
375 | (display "#<record-type " p) | |
376 | (display (record-type-name s) p) | |
377 | (display ">" p)))))) | |
0f2d19dd JB |
378 | |
379 | (define (record-type? obj) | |
380 | (and (struct? obj) (eq? record-type-vtable (struct-vtable obj)))) | |
381 | ||
382 | (define (make-record-type type-name fields . opt) | |
8e693424 | 383 | (let ((printer-fn (and (pair? opt) (car opt)))) |
0f2d19dd | 384 | (let ((struct (make-struct record-type-vtable 0 |
c7c03b9f JB |
385 | (make-struct-layout |
386 | (apply symbol-append | |
387 | (map (lambda (f) "pw") fields))) | |
9f9aa47b MD |
388 | (or printer-fn |
389 | (lambda (s p) | |
390 | (display "#<" p) | |
391 | (display type-name p) | |
392 | (let loop ((fields fields) | |
393 | (off 0)) | |
394 | (cond | |
395 | ((not (null? fields)) | |
396 | (display " " p) | |
397 | (display (car fields) p) | |
398 | (display ": " p) | |
399 | (display (struct-ref s off) p) | |
400 | (loop (cdr fields) (+ 1 off))))) | |
401 | (display ">" p))) | |
0f2d19dd JB |
402 | type-name |
403 | (copy-tree fields)))) | |
c8eed875 MD |
404 | ;; Temporary solution: Associate a name to the record type descriptor |
405 | ;; so that the object system can create a wrapper class for it. | |
406 | (set-struct-vtable-name! struct (if (symbol? type-name) | |
407 | type-name | |
408 | (string->symbol type-name))) | |
0f2d19dd JB |
409 | struct))) |
410 | ||
411 | (define (record-type-name obj) | |
412 | (if (record-type? obj) | |
9f9aa47b | 413 | (struct-ref obj vtable-offset-user) |
0f2d19dd JB |
414 | (error 'not-a-record-type obj))) |
415 | ||
416 | (define (record-type-fields obj) | |
417 | (if (record-type? obj) | |
9f9aa47b | 418 | (struct-ref obj (+ 1 vtable-offset-user)) |
0f2d19dd JB |
419 | (error 'not-a-record-type obj))) |
420 | ||
421 | (define (record-constructor rtd . opt) | |
8e693424 | 422 | (let ((field-names (if (pair? opt) (car opt) (record-type-fields rtd)))) |
d7faeb2e MD |
423 | (local-eval `(lambda ,field-names |
424 | (make-struct ',rtd 0 ,@(map (lambda (f) | |
425 | (if (memq f field-names) | |
426 | f | |
427 | #f)) | |
428 | (record-type-fields rtd)))) | |
429 | the-root-environment))) | |
0f2d19dd JB |
430 | |
431 | (define (record-predicate rtd) | |
432 | (lambda (obj) (and (struct? obj) (eq? rtd (struct-vtable obj))))) | |
433 | ||
434 | (define (record-accessor rtd field-name) | |
435 | (let* ((pos (list-index (record-type-fields rtd) field-name))) | |
436 | (if (not pos) | |
437 | (error 'no-such-field field-name)) | |
d7faeb2e MD |
438 | (local-eval `(lambda (obj) |
439 | (and (eq? ',rtd (record-type-descriptor obj)) | |
440 | (struct-ref obj ,pos))) | |
441 | the-root-environment))) | |
0f2d19dd JB |
442 | |
443 | (define (record-modifier rtd field-name) | |
444 | (let* ((pos (list-index (record-type-fields rtd) field-name))) | |
445 | (if (not pos) | |
446 | (error 'no-such-field field-name)) | |
d7faeb2e MD |
447 | (local-eval `(lambda (obj val) |
448 | (and (eq? ',rtd (record-type-descriptor obj)) | |
449 | (struct-set! obj ,pos val))) | |
450 | the-root-environment))) | |
0f2d19dd JB |
451 | |
452 | ||
453 | (define (record? obj) | |
454 | (and (struct? obj) (record-type? (struct-vtable obj)))) | |
455 | ||
456 | (define (record-type-descriptor obj) | |
457 | (if (struct? obj) | |
458 | (struct-vtable obj) | |
459 | (error 'not-a-record obj))) | |
460 | ||
21ed9efe MD |
461 | (provide 'record) |
462 | ||
0f2d19dd JB |
463 | \f |
464 | ;;; {Booleans} | |
465 | ;;; | |
466 | ||
467 | (define (->bool x) (not (not x))) | |
468 | ||
469 | \f | |
470 | ;;; {Symbols} | |
471 | ;;; | |
472 | ||
473 | (define (symbol-append . args) | |
474 | (string->symbol (apply string-append args))) | |
475 | ||
476 | (define (list->symbol . args) | |
477 | (string->symbol (apply list->string args))) | |
478 | ||
479 | (define (symbol . args) | |
480 | (string->symbol (apply string args))) | |
481 | ||
482 | (define (obarray-symbol-append ob . args) | |
483 | (string->obarray-symbol (apply string-append ob args))) | |
484 | ||
e672f1b5 MD |
485 | (define (obarray-gensym obarray . opt) |
486 | (if (null? opt) | |
487 | (gensym "%%gensym" obarray) | |
488 | (gensym (car opt) obarray))) | |
0f2d19dd JB |
489 | |
490 | \f | |
491 | ;;; {Lists} | |
492 | ;;; | |
493 | ||
494 | (define (list-index l k) | |
495 | (let loop ((n 0) | |
496 | (l l)) | |
497 | (and (not (null? l)) | |
498 | (if (eq? (car l) k) | |
499 | n | |
500 | (loop (+ n 1) (cdr l)))))) | |
501 | ||
75fd4fb6 JB |
502 | (define (make-list n . init) |
503 | (if (pair? init) (set! init (car init))) | |
0f2d19dd JB |
504 | (let loop ((answer '()) |
505 | (n n)) | |
506 | (if (<= n 0) | |
507 | answer | |
508 | (loop (cons init answer) (- n 1))))) | |
509 | ||
510 | ||
511 | \f | |
1729d8ff MD |
512 | ;;; {Multiple return values} |
513 | ||
514 | (define *values-rtd* | |
515 | (make-record-type "values" | |
516 | '(values))) | |
517 | ||
44484f52 MD |
518 | ;;; These two are needed internally in boot-9.scm. |
519 | ;;; They shouldn't be visible outside this module. | |
520 | (define values? (record-predicate *values-rtd*)) | |
521 | (define get-values (record-accessor *values-rtd* 'values)) | |
522 | ||
1729d8ff MD |
523 | (define values |
524 | (let ((make-values (record-constructor *values-rtd*))) | |
525 | (lambda x | |
526 | (if (and (not (null? x)) | |
527 | (null? (cdr x))) | |
528 | (car x) | |
529 | (make-values x))))) | |
530 | ||
531 | (define call-with-values | |
44484f52 MD |
532 | (lambda (producer consumer) |
533 | (let ((result (producer))) | |
534 | (if (values? result) | |
535 | (apply consumer (get-values result)) | |
536 | (consumer result))))) | |
1729d8ff | 537 | |
87cfc5da | 538 | (provide 'values) |
1729d8ff MD |
539 | |
540 | \f | |
3e3cec45 | 541 | ;;; {and-map and or-map} |
0f2d19dd JB |
542 | ;;; |
543 | ;;; (and-map fn lst) is like (and (fn (car lst)) (fn (cadr lst)) (fn...) ...) | |
544 | ;;; (or-map fn lst) is like (or (fn (car lst)) (fn (cadr lst)) (fn...) ...) | |
545 | ;;; (map-in-order fn lst) is like (map fn lst) but definately in order of lst. | |
546 | ;;; | |
547 | ||
548 | ;; and-map f l | |
549 | ;; | |
550 | ;; Apply f to successive elements of l until exhaustion or f returns #f. | |
551 | ;; If returning early, return #f. Otherwise, return the last value returned | |
552 | ;; by f. If f has never been called because l is empty, return #t. | |
553 | ;; | |
554 | (define (and-map f lst) | |
555 | (let loop ((result #t) | |
556 | (l lst)) | |
557 | (and result | |
558 | (or (and (null? l) | |
559 | result) | |
560 | (loop (f (car l)) (cdr l)))))) | |
561 | ||
562 | ;; or-map f l | |
563 | ;; | |
564 | ;; Apply f to successive elements of l until exhaustion or while f returns #f. | |
565 | ;; If returning early, return the return value of f. | |
566 | ;; | |
567 | (define (or-map f lst) | |
568 | (let loop ((result #f) | |
569 | (l lst)) | |
570 | (or result | |
571 | (and (not (null? l)) | |
572 | (loop (f (car l)) (cdr l)))))) | |
573 | ||
59e1116d | 574 | \f |
0f2d19dd | 575 | |
52cfc69b GH |
576 | (if (provided? 'posix) |
577 | (primitive-load-path "ice-9/posix.scm")) | |
6fa8995c | 578 | |
52cfc69b GH |
579 | (if (provided? 'socket) |
580 | (primitive-load-path "ice-9/networking.scm")) | |
3afb28ce | 581 | |
6fa8995c | 582 | (define file-exists? |
52cfc69b | 583 | (if (provided? 'posix) |
6fa8995c GH |
584 | (lambda (str) |
585 | (access? str F_OK)) | |
586 | (lambda (str) | |
587 | (let ((port (catch 'system-error (lambda () (open-file str OPEN_READ)) | |
588 | (lambda args #f)))) | |
589 | (if port (begin (close-port port) #t) | |
590 | #f))))) | |
591 | ||
592 | (define file-is-directory? | |
52cfc69b | 593 | (if (provided? 'posix) |
6fa8995c | 594 | (lambda (str) |
3afb28ce | 595 | (eq? (stat:type (stat str)) 'directory)) |
6fa8995c | 596 | (lambda (str) |
6fa8995c GH |
597 | (let ((port (catch 'system-error |
598 | (lambda () (open-file (string-append str "/.") | |
599 | OPEN_READ)) | |
600 | (lambda args #f)))) | |
601 | (if port (begin (close-port port) #t) | |
602 | #f))))) | |
0f2d19dd JB |
603 | |
604 | (define (has-suffix? str suffix) | |
605 | (let ((sufl (string-length suffix)) | |
606 | (sl (string-length str))) | |
607 | (and (> sl sufl) | |
608 | (string=? (substring str (- sl sufl) sl) suffix)))) | |
609 | ||
0f2d19dd JB |
610 | \f |
611 | ;;; {Error Handling} | |
612 | ;;; | |
613 | ||
0f2d19dd | 614 | (define (error . args) |
21ed9efe | 615 | (save-stack) |
2194b6f0 | 616 | (if (null? args) |
5552355a | 617 | (scm-error 'misc-error #f "?" #f #f) |
8641dd9e | 618 | (let loop ((msg "~A") |
2194b6f0 GH |
619 | (rest (cdr args))) |
620 | (if (not (null? rest)) | |
8641dd9e | 621 | (loop (string-append msg " ~S") |
2194b6f0 | 622 | (cdr rest)) |
5552355a | 623 | (scm-error 'misc-error #f msg args #f))))) |
be2d2c70 | 624 | |
1349bd53 | 625 | ;; bad-throw is the hook that is called upon a throw to a an unhandled |
9a0d70e2 GH |
626 | ;; key (unless the throw has four arguments, in which case |
627 | ;; it's usually interpreted as an error throw.) | |
628 | ;; If the key has a default handler (a throw-handler-default property), | |
0f2d19dd JB |
629 | ;; it is applied to the throw. |
630 | ;; | |
1349bd53 | 631 | (define (bad-throw key . args) |
0f2d19dd JB |
632 | (let ((default (symbol-property key 'throw-handler-default))) |
633 | (or (and default (apply default key args)) | |
2194b6f0 | 634 | (apply error "unhandled-exception:" key args)))) |
0f2d19dd | 635 | |
0f2d19dd | 636 | \f |
bce074ee | 637 | |
708bf0f3 GH |
638 | (define (tm:sec obj) (vector-ref obj 0)) |
639 | (define (tm:min obj) (vector-ref obj 1)) | |
640 | (define (tm:hour obj) (vector-ref obj 2)) | |
641 | (define (tm:mday obj) (vector-ref obj 3)) | |
642 | (define (tm:mon obj) (vector-ref obj 4)) | |
643 | (define (tm:year obj) (vector-ref obj 5)) | |
644 | (define (tm:wday obj) (vector-ref obj 6)) | |
645 | (define (tm:yday obj) (vector-ref obj 7)) | |
646 | (define (tm:isdst obj) (vector-ref obj 8)) | |
647 | (define (tm:gmtoff obj) (vector-ref obj 9)) | |
648 | (define (tm:zone obj) (vector-ref obj 10)) | |
649 | ||
650 | (define (set-tm:sec obj val) (vector-set! obj 0 val)) | |
651 | (define (set-tm:min obj val) (vector-set! obj 1 val)) | |
652 | (define (set-tm:hour obj val) (vector-set! obj 2 val)) | |
653 | (define (set-tm:mday obj val) (vector-set! obj 3 val)) | |
654 | (define (set-tm:mon obj val) (vector-set! obj 4 val)) | |
655 | (define (set-tm:year obj val) (vector-set! obj 5 val)) | |
656 | (define (set-tm:wday obj val) (vector-set! obj 6 val)) | |
657 | (define (set-tm:yday obj val) (vector-set! obj 7 val)) | |
658 | (define (set-tm:isdst obj val) (vector-set! obj 8 val)) | |
659 | (define (set-tm:gmtoff obj val) (vector-set! obj 9 val)) | |
660 | (define (set-tm:zone obj val) (vector-set! obj 10 val)) | |
661 | ||
6afcd3b2 GH |
662 | (define (tms:clock obj) (vector-ref obj 0)) |
663 | (define (tms:utime obj) (vector-ref obj 1)) | |
664 | (define (tms:stime obj) (vector-ref obj 2)) | |
665 | (define (tms:cutime obj) (vector-ref obj 3)) | |
666 | (define (tms:cstime obj) (vector-ref obj 4)) | |
667 | ||
bce074ee GH |
668 | (define (file-position . args) (apply ftell args)) |
669 | (define (file-set-position . args) (apply fseek args)) | |
8b13c6b3 | 670 | |
e38303a2 GH |
671 | (define (move->fdes fd/port fd) |
672 | (cond ((integer? fd/port) | |
7a6f1ffa | 673 | (dup->fdes fd/port fd) |
e38303a2 GH |
674 | (close fd/port) |
675 | fd) | |
676 | (else | |
677 | (primitive-move->fdes fd/port fd) | |
678 | (set-port-revealed! fd/port 1) | |
679 | fd/port))) | |
8b13c6b3 GH |
680 | |
681 | (define (release-port-handle port) | |
682 | (let ((revealed (port-revealed port))) | |
683 | (if (> revealed 0) | |
684 | (set-port-revealed! port (- revealed 1))))) | |
0f2d19dd | 685 | |
e38303a2 | 686 | (define (dup->port port/fd mode . maybe-fd) |
7a6f1ffa | 687 | (let ((port (fdopen (apply dup->fdes port/fd maybe-fd) |
e38303a2 GH |
688 | mode))) |
689 | (if (pair? maybe-fd) | |
690 | (set-port-revealed! port 1)) | |
691 | port)) | |
692 | ||
693 | (define (dup->inport port/fd . maybe-fd) | |
694 | (apply dup->port port/fd "r" maybe-fd)) | |
695 | ||
696 | (define (dup->outport port/fd . maybe-fd) | |
697 | (apply dup->port port/fd "w" maybe-fd)) | |
698 | ||
e38303a2 GH |
699 | (define (dup port/fd . maybe-fd) |
700 | (if (integer? port/fd) | |
701 | (apply dup->fdes port/fd maybe-fd) | |
702 | (apply dup->port port/fd (port-mode port/fd) maybe-fd))) | |
703 | ||
704 | (define (duplicate-port port modes) | |
705 | (dup->port port modes)) | |
706 | ||
707 | (define (fdes->inport fdes) | |
708 | (let loop ((rest-ports (fdes->ports fdes))) | |
709 | (cond ((null? rest-ports) | |
710 | (let ((result (fdopen fdes "r"))) | |
711 | (set-port-revealed! result 1) | |
712 | result)) | |
713 | ((input-port? (car rest-ports)) | |
714 | (set-port-revealed! (car rest-ports) | |
715 | (+ (port-revealed (car rest-ports)) 1)) | |
716 | (car rest-ports)) | |
717 | (else | |
718 | (loop (cdr rest-ports)))))) | |
719 | ||
720 | (define (fdes->outport fdes) | |
721 | (let loop ((rest-ports (fdes->ports fdes))) | |
722 | (cond ((null? rest-ports) | |
723 | (let ((result (fdopen fdes "w"))) | |
724 | (set-port-revealed! result 1) | |
725 | result)) | |
726 | ((output-port? (car rest-ports)) | |
727 | (set-port-revealed! (car rest-ports) | |
728 | (+ (port-revealed (car rest-ports)) 1)) | |
729 | (car rest-ports)) | |
730 | (else | |
731 | (loop (cdr rest-ports)))))) | |
732 | ||
733 | (define (port->fdes port) | |
734 | (set-port-revealed! port (+ (port-revealed port) 1)) | |
735 | (fileno port)) | |
736 | ||
956055a9 GH |
737 | (define (setenv name value) |
738 | (if value | |
739 | (putenv (string-append name "=" value)) | |
740 | (putenv name))) | |
741 | ||
0f2d19dd JB |
742 | \f |
743 | ;;; {Load Paths} | |
744 | ;;; | |
745 | ||
0f2d19dd JB |
746 | ;;; Here for backward compatability |
747 | ;; | |
748 | (define scheme-file-suffix (lambda () ".scm")) | |
749 | ||
3cab8392 JB |
750 | (define (in-vicinity vicinity file) |
751 | (let ((tail (let ((len (string-length vicinity))) | |
534a0099 MD |
752 | (if (zero? len) |
753 | #f | |
3cab8392 JB |
754 | (string-ref vicinity (- len 1)))))) |
755 | (string-append vicinity | |
534a0099 MD |
756 | (if (or (not tail) |
757 | (eq? tail #\/)) | |
758 | "" | |
759 | "/") | |
3cab8392 | 760 | file))) |
02ceadb8 | 761 | |
0f2d19dd | 762 | \f |
ef00e7f4 JB |
763 | ;;; {Help for scm_shell} |
764 | ;;; The argument-processing code used by Guile-based shells generates | |
765 | ;;; Scheme code based on the argument list. This page contains help | |
766 | ;;; functions for the code it generates. | |
767 | ||
ef00e7f4 JB |
768 | (define (command-line) (program-arguments)) |
769 | ||
5aa7fe69 JB |
770 | ;; This is mostly for the internal use of the code generated by |
771 | ;; scm_compile_shell_switches. | |
ef00e7f4 | 772 | (define (load-user-init) |
1f08acd9 GH |
773 | (let* ((home (or (getenv "HOME") |
774 | (false-if-exception (passwd:dir (getpwuid (getuid)))) | |
775 | "/")) ;; fallback for cygwin etc. | |
776 | (init-file (in-vicinity home ".guile"))) | |
777 | (if (file-exists? init-file) | |
778 | (primitive-load init-file)))) | |
ef00e7f4 JB |
779 | |
780 | \f | |
a06181a2 JB |
781 | ;;; {Loading by paths} |
782 | ||
783 | ;;; Load a Scheme source file named NAME, searching for it in the | |
784 | ;;; directories listed in %load-path, and applying each of the file | |
785 | ;;; name extensions listed in %load-extensions. | |
786 | (define (load-from-path name) | |
787 | (start-stack 'load-stack | |
75a97b92 | 788 | (primitive-load-path name))) |
0f2d19dd | 789 | |
5552355a | 790 | |
0f2d19dd | 791 | \f |
0f2d19dd JB |
792 | ;;; {Transcendental Functions} |
793 | ;;; | |
794 | ;;; Derived from "Transcen.scm", Complex trancendental functions for SCM. | |
0543c9b7 | 795 | ;;; Written by Jerry D. Hedden, (C) FSF. |
0f2d19dd JB |
796 | ;;; See the file `COPYING' for terms applying to this program. |
797 | ;;; | |
798 | ||
799 | (define (exp z) | |
800 | (if (real? z) ($exp z) | |
801 | (make-polar ($exp (real-part z)) (imag-part z)))) | |
802 | ||
803 | (define (log z) | |
804 | (if (and (real? z) (>= z 0)) | |
805 | ($log z) | |
806 | (make-rectangular ($log (magnitude z)) (angle z)))) | |
807 | ||
808 | (define (sqrt z) | |
809 | (if (real? z) | |
810 | (if (negative? z) (make-rectangular 0 ($sqrt (- z))) | |
811 | ($sqrt z)) | |
812 | (make-polar ($sqrt (magnitude z)) (/ (angle z) 2)))) | |
813 | ||
814 | (define expt | |
815 | (let ((integer-expt integer-expt)) | |
816 | (lambda (z1 z2) | |
22381005 DH |
817 | (cond ((integer? z2) |
818 | (if (>= z2 0) | |
819 | (integer-expt z1 z2) | |
820 | (/ 1 (integer-expt z1 (- z2))))) | |
0f2d19dd JB |
821 | ((and (real? z2) (real? z1) (>= z1 0)) |
822 | ($expt z1 z2)) | |
823 | (else | |
824 | (exp (* z2 (log z1)))))))) | |
825 | ||
826 | (define (sinh z) | |
827 | (if (real? z) ($sinh z) | |
828 | (let ((x (real-part z)) (y (imag-part z))) | |
829 | (make-rectangular (* ($sinh x) ($cos y)) | |
830 | (* ($cosh x) ($sin y)))))) | |
831 | (define (cosh z) | |
832 | (if (real? z) ($cosh z) | |
833 | (let ((x (real-part z)) (y (imag-part z))) | |
834 | (make-rectangular (* ($cosh x) ($cos y)) | |
835 | (* ($sinh x) ($sin y)))))) | |
836 | (define (tanh z) | |
837 | (if (real? z) ($tanh z) | |
838 | (let* ((x (* 2 (real-part z))) | |
839 | (y (* 2 (imag-part z))) | |
840 | (w (+ ($cosh x) ($cos y)))) | |
841 | (make-rectangular (/ ($sinh x) w) (/ ($sin y) w))))) | |
842 | ||
843 | (define (asinh z) | |
844 | (if (real? z) ($asinh z) | |
845 | (log (+ z (sqrt (+ (* z z) 1)))))) | |
846 | ||
847 | (define (acosh z) | |
848 | (if (and (real? z) (>= z 1)) | |
849 | ($acosh z) | |
850 | (log (+ z (sqrt (- (* z z) 1)))))) | |
851 | ||
852 | (define (atanh z) | |
853 | (if (and (real? z) (> z -1) (< z 1)) | |
854 | ($atanh z) | |
855 | (/ (log (/ (+ 1 z) (- 1 z))) 2))) | |
856 | ||
857 | (define (sin z) | |
858 | (if (real? z) ($sin z) | |
859 | (let ((x (real-part z)) (y (imag-part z))) | |
860 | (make-rectangular (* ($sin x) ($cosh y)) | |
861 | (* ($cos x) ($sinh y)))))) | |
862 | (define (cos z) | |
863 | (if (real? z) ($cos z) | |
864 | (let ((x (real-part z)) (y (imag-part z))) | |
865 | (make-rectangular (* ($cos x) ($cosh y)) | |
866 | (- (* ($sin x) ($sinh y))))))) | |
867 | (define (tan z) | |
868 | (if (real? z) ($tan z) | |
869 | (let* ((x (* 2 (real-part z))) | |
870 | (y (* 2 (imag-part z))) | |
871 | (w (+ ($cos x) ($cosh y)))) | |
872 | (make-rectangular (/ ($sin x) w) (/ ($sinh y) w))))) | |
873 | ||
874 | (define (asin z) | |
875 | (if (and (real? z) (>= z -1) (<= z 1)) | |
876 | ($asin z) | |
877 | (* -i (asinh (* +i z))))) | |
878 | ||
879 | (define (acos z) | |
880 | (if (and (real? z) (>= z -1) (<= z 1)) | |
881 | ($acos z) | |
882 | (+ (/ (angle -1) 2) (* +i (asinh (* +i z)))))) | |
883 | ||
884 | (define (atan z . y) | |
885 | (if (null? y) | |
886 | (if (real? z) ($atan z) | |
887 | (/ (log (/ (- +i z) (+ +i z))) +2i)) | |
888 | ($atan2 z (car y)))) | |
889 | ||
65495221 GH |
890 | (define (log10 arg) |
891 | (/ (log arg) (log 10))) | |
892 | ||
0f2d19dd | 893 | \f |
0f2d19dd JB |
894 | |
895 | ;;; {Reader Extensions} | |
896 | ;;; | |
897 | ||
898 | ;;; Reader code for various "#c" forms. | |
899 | ;;; | |
900 | ||
75a97b92 GH |
901 | (read-hash-extend #\' (lambda (c port) |
902 | (read port))) | |
903 | (read-hash-extend #\. (lambda (c port) | |
d7faeb2e | 904 | (eval (read port) (interaction-environment)))) |
75a97b92 | 905 | |
0f2d19dd JB |
906 | \f |
907 | ;;; {Command Line Options} | |
908 | ;;; | |
909 | ||
910 | (define (get-option argv kw-opts kw-args return) | |
911 | (cond | |
912 | ((null? argv) | |
913 | (return #f #f argv)) | |
914 | ||
915 | ((or (not (eq? #\- (string-ref (car argv) 0))) | |
916 | (eq? (string-length (car argv)) 1)) | |
917 | (return 'normal-arg (car argv) (cdr argv))) | |
918 | ||
919 | ((eq? #\- (string-ref (car argv) 1)) | |
920 | (let* ((kw-arg-pos (or (string-index (car argv) #\=) | |
921 | (string-length (car argv)))) | |
922 | (kw (symbol->keyword (substring (car argv) 2 kw-arg-pos))) | |
923 | (kw-opt? (member kw kw-opts)) | |
924 | (kw-arg? (member kw kw-args)) | |
925 | (arg (or (and (not (eq? kw-arg-pos (string-length (car argv)))) | |
926 | (substring (car argv) | |
927 | (+ kw-arg-pos 1) | |
928 | (string-length (car argv)))) | |
929 | (and kw-arg? | |
930 | (begin (set! argv (cdr argv)) (car argv)))))) | |
931 | (if (or kw-opt? kw-arg?) | |
932 | (return kw arg (cdr argv)) | |
933 | (return 'usage-error kw (cdr argv))))) | |
934 | ||
935 | (else | |
936 | (let* ((char (substring (car argv) 1 2)) | |
937 | (kw (symbol->keyword char))) | |
938 | (cond | |
939 | ||
940 | ((member kw kw-opts) | |
941 | (let* ((rest-car (substring (car argv) 2 (string-length (car argv)))) | |
942 | (new-argv (if (= 0 (string-length rest-car)) | |
943 | (cdr argv) | |
944 | (cons (string-append "-" rest-car) (cdr argv))))) | |
945 | (return kw #f new-argv))) | |
946 | ||
947 | ((member kw kw-args) | |
948 | (let* ((rest-car (substring (car argv) 2 (string-length (car argv)))) | |
949 | (arg (if (= 0 (string-length rest-car)) | |
950 | (cadr argv) | |
951 | rest-car)) | |
952 | (new-argv (if (= 0 (string-length rest-car)) | |
953 | (cddr argv) | |
954 | (cdr argv)))) | |
955 | (return kw arg new-argv))) | |
956 | ||
957 | (else (return 'usage-error kw argv))))))) | |
958 | ||
959 | (define (for-next-option proc argv kw-opts kw-args) | |
960 | (let loop ((argv argv)) | |
961 | (get-option argv kw-opts kw-args | |
962 | (lambda (opt opt-arg argv) | |
963 | (and opt (proc opt opt-arg argv loop)))))) | |
964 | ||
965 | (define (display-usage-report kw-desc) | |
966 | (for-each | |
967 | (lambda (kw) | |
968 | (or (eq? (car kw) #t) | |
969 | (eq? (car kw) 'else) | |
970 | (let* ((opt-desc kw) | |
971 | (help (cadr opt-desc)) | |
972 | (opts (car opt-desc)) | |
973 | (opts-proper (if (string? (car opts)) (cdr opts) opts)) | |
974 | (arg-name (if (string? (car opts)) | |
975 | (string-append "<" (car opts) ">") | |
976 | "")) | |
977 | (left-part (string-append | |
978 | (with-output-to-string | |
979 | (lambda () | |
980 | (map (lambda (x) (display (keyword-symbol x)) (display " ")) | |
981 | opts-proper))) | |
982 | arg-name)) | |
11b05261 MD |
983 | (middle-part (if (and (< (string-length left-part) 30) |
984 | (< (string-length help) 40)) | |
985 | (make-string (- 30 (string-length left-part)) #\ ) | |
0f2d19dd JB |
986 | "\n\t"))) |
987 | (display left-part) | |
988 | (display middle-part) | |
989 | (display help) | |
990 | (newline)))) | |
991 | kw-desc)) | |
992 | ||
993 | ||
994 | ||
0f2d19dd JB |
995 | (define (transform-usage-lambda cases) |
996 | (let* ((raw-usage (delq! 'else (map car cases))) | |
997 | (usage-sans-specials (map (lambda (x) | |
998 | (or (and (not (list? x)) x) | |
999 | (and (symbol? (car x)) #t) | |
1000 | (and (boolean? (car x)) #t) | |
1001 | x)) | |
1002 | raw-usage)) | |
ed440df5 | 1003 | (usage-desc (delq! #t usage-sans-specials)) |
0f2d19dd JB |
1004 | (kw-desc (map car usage-desc)) |
1005 | (kw-opts (apply append (map (lambda (x) (and (not (string? (car x))) x)) kw-desc))) | |
1006 | (kw-args (apply append (map (lambda (x) (and (string? (car x)) (cdr x))) kw-desc))) | |
1007 | (transmogrified-cases (map (lambda (case) | |
1008 | (cons (let ((opts (car case))) | |
1009 | (if (or (boolean? opts) (eq? 'else opts)) | |
1010 | opts | |
1011 | (cond | |
1012 | ((symbol? (car opts)) opts) | |
1013 | ((boolean? (car opts)) opts) | |
1014 | ((string? (caar opts)) (cdar opts)) | |
1015 | (else (car opts))))) | |
1016 | (cdr case))) | |
1017 | cases))) | |
1018 | `(let ((%display-usage (lambda () (display-usage-report ',usage-desc)))) | |
1019 | (lambda (%argv) | |
1020 | (let %next-arg ((%argv %argv)) | |
1021 | (get-option %argv | |
1022 | ',kw-opts | |
1023 | ',kw-args | |
1024 | (lambda (%opt %arg %new-argv) | |
1025 | (case %opt | |
1026 | ,@ transmogrified-cases)))))))) | |
1027 | ||
1028 | ||
1029 | \f | |
1030 | ||
1031 | ;;; {Low Level Modules} | |
1032 | ;;; | |
1033 | ;;; These are the low level data structures for modules. | |
1034 | ;;; | |
1035 | ;;; !!! warning: The interface to lazy binder procedures is going | |
1036 | ;;; to be changed in an incompatible way to permit all the basic | |
1037 | ;;; module ops to be virtualized. | |
1038 | ;;; | |
1039 | ;;; (make-module size use-list lazy-binding-proc) => module | |
1040 | ;;; module-{obarray,uses,binder}[|-set!] | |
1041 | ;;; (module? obj) => [#t|#f] | |
1042 | ;;; (module-locally-bound? module symbol) => [#t|#f] | |
1043 | ;;; (module-bound? module symbol) => [#t|#f] | |
1044 | ;;; (module-symbol-locally-interned? module symbol) => [#t|#f] | |
1045 | ;;; (module-symbol-interned? module symbol) => [#t|#f] | |
1046 | ;;; (module-local-variable module symbol) => [#<variable ...> | #f] | |
1047 | ;;; (module-variable module symbol) => [#<variable ...> | #f] | |
1048 | ;;; (module-symbol-binding module symbol opt-value) | |
1049 | ;;; => [ <obj> | opt-value | an error occurs ] | |
1050 | ;;; (module-make-local-var! module symbol) => #<variable...> | |
1051 | ;;; (module-add! module symbol var) => unspecified | |
1052 | ;;; (module-remove! module symbol) => unspecified | |
1053 | ;;; (module-for-each proc module) => unspecified | |
1054 | ;;; (make-scm-module) => module ; a lazy copy of the symhash module | |
1055 | ;;; (set-current-module module) => unspecified | |
1056 | ;;; (current-module) => #<module...> | |
1057 | ;;; | |
1058 | ;;; | |
1059 | ||
1060 | \f | |
44cf1f0f JB |
1061 | ;;; {Printing Modules} |
1062 | ;; This is how modules are printed. You can re-define it. | |
fa7e9274 MV |
1063 | ;; (Redefining is actually more complicated than simply redefining |
1064 | ;; %print-module because that would only change the binding and not | |
1065 | ;; the value stored in the vtable that determines how record are | |
1066 | ;; printed. Sigh.) | |
1067 | ||
1068 | (define (%print-module mod port) ; unused args: depth length style table) | |
0f2d19dd JB |
1069 | (display "#<" port) |
1070 | (display (or (module-kind mod) "module") port) | |
1071 | (let ((name (module-name mod))) | |
1072 | (if name | |
1073 | (begin | |
1074 | (display " " port) | |
1075 | (display name port)))) | |
1076 | (display " " port) | |
1077 | (display (number->string (object-address mod) 16) port) | |
1078 | (display ">" port)) | |
1079 | ||
1080 | ;; module-type | |
1081 | ;; | |
1082 | ;; A module is characterized by an obarray in which local symbols | |
1083 | ;; are interned, a list of modules, "uses", from which non-local | |
1084 | ;; bindings can be inherited, and an optional lazy-binder which | |
31d50456 | 1085 | ;; is a (CLOSURE module symbol) which, as a last resort, can provide |
0f2d19dd JB |
1086 | ;; bindings that would otherwise not be found locally in the module. |
1087 | ;; | |
d7faeb2e MD |
1088 | ;; NOTE: If you change here, you also need to change libguile/modules.h. |
1089 | ;; | |
0f2d19dd | 1090 | (define module-type |
7a0ff2f8 | 1091 | (make-record-type 'module |
1777c18b MD |
1092 | '(obarray uses binder eval-closure transformer name kind |
1093 | observers weak-observers observer-id) | |
8b718458 | 1094 | %print-module)) |
0f2d19dd | 1095 | |
8b718458 | 1096 | ;; make-module &opt size uses binder |
0f2d19dd | 1097 | ;; |
8b718458 JB |
1098 | ;; Create a new module, perhaps with a particular size of obarray, |
1099 | ;; initial uses list, or binding procedure. | |
0f2d19dd | 1100 | ;; |
0f2d19dd JB |
1101 | (define make-module |
1102 | (lambda args | |
0f2d19dd | 1103 | |
8b718458 JB |
1104 | (define (parse-arg index default) |
1105 | (if (> (length args) index) | |
1106 | (list-ref args index) | |
1107 | default)) | |
1108 | ||
1109 | (if (> (length args) 3) | |
1110 | (error "Too many args to make-module." args)) | |
0f2d19dd | 1111 | |
8b718458 JB |
1112 | (let ((size (parse-arg 0 1021)) |
1113 | (uses (parse-arg 1 '())) | |
1114 | (binder (parse-arg 2 #f))) | |
0f2d19dd | 1115 | |
8b718458 JB |
1116 | (if (not (integer? size)) |
1117 | (error "Illegal size to make-module." size)) | |
1118 | (if (not (and (list? uses) | |
1119 | (and-map module? uses))) | |
1120 | (error "Incorrect use list." uses)) | |
0f2d19dd JB |
1121 | (if (and binder (not (procedure? binder))) |
1122 | (error | |
1123 | "Lazy-binder expected to be a procedure or #f." binder)) | |
1124 | ||
8b718458 | 1125 | (let ((module (module-constructor (make-vector size '()) |
1777c18b MD |
1126 | uses binder #f #f #f #f |
1127 | '() | |
1128 | (make-weak-value-hash-table 31) | |
1129 | 0))) | |
8b718458 JB |
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. | |
6906bd0d | 1134 | (set-module-eval-closure! module (standard-eval-closure module)) |
8b718458 JB |
1135 | |
1136 | module)))) | |
0f2d19dd | 1137 | |
8b718458 | 1138 | (define module-constructor (record-constructor module-type)) |
0f2d19dd JB |
1139 | (define module-obarray (record-accessor module-type 'obarray)) |
1140 | (define set-module-obarray! (record-modifier module-type 'obarray)) | |
1141 | (define module-uses (record-accessor module-type 'uses)) | |
1142 | (define set-module-uses! (record-modifier module-type 'uses)) | |
1143 | (define module-binder (record-accessor module-type 'binder)) | |
1144 | (define set-module-binder! (record-modifier module-type 'binder)) | |
631c1902 MD |
1145 | |
1146 | ;; NOTE: This binding is used in libguile/modules.c. | |
31d50456 | 1147 | (define module-eval-closure (record-accessor module-type 'eval-closure)) |
631c1902 | 1148 | |
7a0ff2f8 MD |
1149 | (define module-transformer (record-accessor module-type 'transformer)) |
1150 | (define set-module-transformer! (record-modifier module-type 'transformer)) | |
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)) | |
1777c18b MD |
1155 | (define module-observers (record-accessor module-type 'observers)) |
1156 | (define set-module-observers! (record-modifier module-type 'observers)) | |
1157 | (define module-weak-observers (record-accessor module-type 'weak-observers)) | |
1158 | (define module-observer-id (record-accessor module-type 'observer-id)) | |
1159 | (define set-module-observer-id! (record-modifier module-type 'observer-id)) | |
0f2d19dd JB |
1160 | (define module? (record-predicate module-type)) |
1161 | ||
edc185c7 MD |
1162 | (define set-module-eval-closure! |
1163 | (let ((setter (record-modifier module-type 'eval-closure))) | |
1164 | (lambda (module closure) | |
1165 | (setter module closure) | |
1166 | ;; Make it possible to lookup the module from the environment. | |
1167 | ;; This implementation is correct since an eval closure can belong | |
1168 | ;; to maximally one module. | |
1169 | (set-procedure-property! closure 'module module)))) | |
8b718458 | 1170 | |
d7faeb2e MD |
1171 | ;;; This procedure is depreated |
1172 | ;;; | |
1173 | (define eval-in-module eval) | |
0f2d19dd JB |
1174 | |
1175 | \f | |
1777c18b MD |
1176 | ;;; {Observer protocol} |
1177 | ;;; | |
1178 | ||
1179 | (define (module-observe module proc) | |
1180 | (set-module-observers! module (cons proc (module-observers module))) | |
1181 | (cons module proc)) | |
1182 | ||
1183 | (define (module-observe-weak module proc) | |
1184 | (let ((id (module-observer-id module))) | |
1185 | (hash-set! (module-weak-observers module) id proc) | |
1186 | (set-module-observer-id! module (+ 1 id)) | |
1187 | (cons module id))) | |
1188 | ||
1189 | (define (module-unobserve token) | |
1190 | (let ((module (car token)) | |
1191 | (id (cdr token))) | |
1192 | (if (integer? id) | |
1193 | (hash-remove! (module-weak-observers module) id) | |
1194 | (set-module-observers! module (delq1! id (module-observers module))))) | |
1195 | *unspecified*) | |
1196 | ||
1a961d7e | 1197 | (define (module-modified m) |
1777c18b MD |
1198 | (for-each (lambda (proc) (proc m)) (module-observers m)) |
1199 | (hash-fold (lambda (id proc res) (proc m)) #f (module-weak-observers m))) | |
1200 | ||
1201 | \f | |
0f2d19dd JB |
1202 | ;;; {Module Searching in General} |
1203 | ;;; | |
1204 | ;;; We sometimes want to look for properties of a symbol | |
1205 | ;;; just within the obarray of one module. If the property | |
1206 | ;;; holds, then it is said to hold ``locally'' as in, ``The symbol | |
1207 | ;;; DISPLAY is locally rebound in the module `safe-guile'.'' | |
1208 | ;;; | |
1209 | ;;; | |
1210 | ;;; Other times, we want to test for a symbol property in the obarray | |
1211 | ;;; of M and, if it is not found there, try each of the modules in the | |
1212 | ;;; uses list of M. This is the normal way of testing for some | |
1213 | ;;; property, so we state these properties without qualification as | |
1214 | ;;; in: ``The symbol 'fnord is interned in module M because it is | |
1215 | ;;; interned locally in module M2 which is a member of the uses list | |
1216 | ;;; of M.'' | |
1217 | ;;; | |
1218 | ||
1219 | ;; module-search fn m | |
1220 | ;; | |
1221 | ;; return the first non-#f result of FN applied to M and then to | |
1222 | ;; the modules in the uses of m, and so on recursively. If all applications | |
1223 | ;; return #f, then so does this function. | |
1224 | ;; | |
1225 | (define (module-search fn m v) | |
1226 | (define (loop pos) | |
1227 | (and (pair? pos) | |
1228 | (or (module-search fn (car pos) v) | |
1229 | (loop (cdr pos))))) | |
1230 | (or (fn m v) | |
1231 | (loop (module-uses m)))) | |
1232 | ||
1233 | ||
1234 | ;;; {Is a symbol bound in a module?} | |
1235 | ;;; | |
1236 | ;;; Symbol S in Module M is bound if S is interned in M and if the binding | |
1237 | ;;; of S in M has been set to some well-defined value. | |
1238 | ;;; | |
1239 | ||
1240 | ;; module-locally-bound? module symbol | |
1241 | ;; | |
1242 | ;; Is a symbol bound (interned and defined) locally in a given module? | |
1243 | ;; | |
1244 | (define (module-locally-bound? m v) | |
1245 | (let ((var (module-local-variable m v))) | |
1246 | (and var | |
1247 | (variable-bound? var)))) | |
1248 | ||
1249 | ;; module-bound? module symbol | |
1250 | ;; | |
1251 | ;; Is a symbol bound (interned and defined) anywhere in a given module | |
1252 | ;; or its uses? | |
1253 | ;; | |
1254 | (define (module-bound? m v) | |
1255 | (module-search module-locally-bound? m v)) | |
1256 | ||
1257 | ;;; {Is a symbol interned in a module?} | |
1258 | ;;; | |
1259 | ;;; Symbol S in Module M is interned if S occurs in | |
1260 | ;;; of S in M has been set to some well-defined value. | |
1261 | ;;; | |
1262 | ;;; It is possible to intern a symbol in a module without providing | |
1263 | ;;; an initial binding for the corresponding variable. This is done | |
1264 | ;;; with: | |
1265 | ;;; (module-add! module symbol (make-undefined-variable)) | |
1266 | ;;; | |
1267 | ;;; In that case, the symbol is interned in the module, but not | |
1268 | ;;; bound there. The unbound symbol shadows any binding for that | |
1269 | ;;; symbol that might otherwise be inherited from a member of the uses list. | |
1270 | ;;; | |
1271 | ||
1272 | (define (module-obarray-get-handle ob key) | |
1273 | ((if (symbol? key) hashq-get-handle hash-get-handle) ob key)) | |
1274 | ||
1275 | (define (module-obarray-ref ob key) | |
1276 | ((if (symbol? key) hashq-ref hash-ref) ob key)) | |
1277 | ||
1278 | (define (module-obarray-set! ob key val) | |
1279 | ((if (symbol? key) hashq-set! hash-set!) ob key val)) | |
1280 | ||
1281 | (define (module-obarray-remove! ob key) | |
1282 | ((if (symbol? key) hashq-remove! hash-remove!) ob key)) | |
1283 | ||
1284 | ;; module-symbol-locally-interned? module symbol | |
1285 | ;; | |
1286 | ;; is a symbol interned (not neccessarily defined) locally in a given module | |
1287 | ;; or its uses? Interned symbols shadow inherited bindings even if | |
1288 | ;; they are not themselves bound to a defined value. | |
1289 | ;; | |
1290 | (define (module-symbol-locally-interned? m v) | |
1291 | (not (not (module-obarray-get-handle (module-obarray m) v)))) | |
1292 | ||
1293 | ;; module-symbol-interned? module symbol | |
1294 | ;; | |
1295 | ;; is a symbol interned (not neccessarily defined) anywhere in a given module | |
1296 | ;; or its uses? Interned symbols shadow inherited bindings even if | |
1297 | ;; they are not themselves bound to a defined value. | |
1298 | ;; | |
1299 | (define (module-symbol-interned? m v) | |
1300 | (module-search module-symbol-locally-interned? m v)) | |
1301 | ||
1302 | ||
1303 | ;;; {Mapping modules x symbols --> variables} | |
1304 | ;;; | |
1305 | ||
1306 | ;; module-local-variable module symbol | |
1307 | ;; return the local variable associated with a MODULE and SYMBOL. | |
1308 | ;; | |
1309 | ;;; This function is very important. It is the only function that can | |
1310 | ;;; return a variable from a module other than the mutators that store | |
1311 | ;;; new variables in modules. Therefore, this function is the location | |
1312 | ;;; of the "lazy binder" hack. | |
1313 | ;;; | |
1314 | ;;; If symbol is defined in MODULE, and if the definition binds symbol | |
1315 | ;;; to a variable, return that variable object. | |
1316 | ;;; | |
1317 | ;;; If the symbols is not found at first, but the module has a lazy binder, | |
1318 | ;;; then try the binder. | |
1319 | ;;; | |
1320 | ;;; If the symbol is not found at all, return #f. | |
1321 | ;;; | |
1322 | (define (module-local-variable m v) | |
6fa8995c GH |
1323 | ; (caddr |
1324 | ; (list m v | |
0f2d19dd JB |
1325 | (let ((b (module-obarray-ref (module-obarray m) v))) |
1326 | (or (and (variable? b) b) | |
1327 | (and (module-binder m) | |
6fa8995c GH |
1328 | ((module-binder m) m v #f))))) |
1329 | ;)) | |
0f2d19dd JB |
1330 | |
1331 | ;; module-variable module symbol | |
1332 | ;; | |
1333 | ;; like module-local-variable, except search the uses in the | |
1334 | ;; case V is not found in M. | |
1335 | ;; | |
6906bd0d MD |
1336 | ;; NOTE: This function is superseded with C code (see modules.c) |
1337 | ;;; when using the standard eval closure. | |
1338 | ;; | |
0f2d19dd JB |
1339 | (define (module-variable m v) |
1340 | (module-search module-local-variable m v)) | |
1341 | ||
1342 | ||
1343 | ;;; {Mapping modules x symbols --> bindings} | |
1344 | ;;; | |
1345 | ;;; These are similar to the mapping to variables, except that the | |
1346 | ;;; variable is dereferenced. | |
1347 | ;;; | |
1348 | ||
1349 | ;; module-symbol-binding module symbol opt-value | |
1350 | ;; | |
1351 | ;; return the binding of a variable specified by name within | |
1352 | ;; a given module, signalling an error if the variable is unbound. | |
1353 | ;; If the OPT-VALUE is passed, then instead of signalling an error, | |
1354 | ;; return OPT-VALUE. | |
1355 | ;; | |
1356 | (define (module-symbol-local-binding m v . opt-val) | |
1357 | (let ((var (module-local-variable m v))) | |
1358 | (if var | |
1359 | (variable-ref var) | |
1360 | (if (not (null? opt-val)) | |
1361 | (car opt-val) | |
1362 | (error "Locally unbound variable." v))))) | |
1363 | ||
1364 | ;; module-symbol-binding module symbol opt-value | |
1365 | ;; | |
1366 | ;; return the binding of a variable specified by name within | |
1367 | ;; a given module, signalling an error if the variable is unbound. | |
1368 | ;; If the OPT-VALUE is passed, then instead of signalling an error, | |
1369 | ;; return OPT-VALUE. | |
1370 | ;; | |
1371 | (define (module-symbol-binding m v . opt-val) | |
1372 | (let ((var (module-variable m v))) | |
1373 | (if var | |
1374 | (variable-ref var) | |
1375 | (if (not (null? opt-val)) | |
1376 | (car opt-val) | |
1377 | (error "Unbound variable." v))))) | |
1378 | ||
1379 | ||
1380 | \f | |
1381 | ;;; {Adding Variables to Modules} | |
1382 | ;;; | |
1383 | ;;; | |
1384 | ||
1385 | ||
1386 | ;; module-make-local-var! module symbol | |
1387 | ;; | |
1388 | ;; ensure a variable for V in the local namespace of M. | |
1389 | ;; If no variable was already there, then create a new and uninitialzied | |
1390 | ;; variable. | |
1391 | ;; | |
1392 | (define (module-make-local-var! m v) | |
1393 | (or (let ((b (module-obarray-ref (module-obarray m) v))) | |
1777c18b MD |
1394 | (and (variable? b) |
1395 | (begin | |
1a961d7e | 1396 | (module-modified m) |
1777c18b | 1397 | b))) |
0f2d19dd JB |
1398 | (and (module-binder m) |
1399 | ((module-binder m) m v #t)) | |
1400 | (begin | |
1401 | (let ((answer (make-undefined-variable v))) | |
1402 | (module-obarray-set! (module-obarray m) v answer) | |
1a961d7e | 1403 | (module-modified m) |
0f2d19dd JB |
1404 | answer)))) |
1405 | ||
1406 | ;; module-add! module symbol var | |
1407 | ;; | |
1408 | ;; ensure a particular variable for V in the local namespace of M. | |
1409 | ;; | |
1410 | (define (module-add! m v var) | |
1411 | (if (not (variable? var)) | |
1412 | (error "Bad variable to module-add!" var)) | |
1777c18b | 1413 | (module-obarray-set! (module-obarray m) v var) |
1a961d7e | 1414 | (module-modified m)) |
0f2d19dd JB |
1415 | |
1416 | ;; module-remove! | |
1417 | ;; | |
1418 | ;; make sure that a symbol is undefined in the local namespace of M. | |
1419 | ;; | |
1420 | (define (module-remove! m v) | |
1777c18b | 1421 | (module-obarray-remove! (module-obarray m) v) |
1a961d7e | 1422 | (module-modified m)) |
0f2d19dd JB |
1423 | |
1424 | (define (module-clear! m) | |
1777c18b | 1425 | (vector-fill! (module-obarray m) '()) |
1a961d7e | 1426 | (module-modified m)) |
0f2d19dd JB |
1427 | |
1428 | ;; MODULE-FOR-EACH -- exported | |
1429 | ;; | |
1430 | ;; Call PROC on each symbol in MODULE, with arguments of (SYMBOL VARIABLE). | |
1431 | ;; | |
1432 | (define (module-for-each proc module) | |
1433 | (let ((obarray (module-obarray module))) | |
1434 | (do ((index 0 (+ index 1)) | |
1435 | (end (vector-length obarray))) | |
1436 | ((= index end)) | |
1437 | (for-each | |
1438 | (lambda (bucket) | |
1439 | (proc (car bucket) (cdr bucket))) | |
1440 | (vector-ref obarray index))))) | |
1441 | ||
1442 | ||
1443 | (define (module-map proc module) | |
1444 | (let* ((obarray (module-obarray module)) | |
1445 | (end (vector-length obarray))) | |
1446 | ||
1447 | (let loop ((i 0) | |
1448 | (answer '())) | |
1449 | (if (= i end) | |
1450 | answer | |
1451 | (loop (+ 1 i) | |
1452 | (append! | |
1453 | (map (lambda (bucket) | |
1454 | (proc (car bucket) (cdr bucket))) | |
1455 | (vector-ref obarray i)) | |
1456 | answer)))))) | |
1457 | \f | |
1458 | ||
1459 | ;;; {Low Level Bootstrapping} | |
1460 | ;;; | |
1461 | ||
1462 | ;; make-root-module | |
1463 | ||
21ed9efe | 1464 | ;; A root module uses the symhash table (the system's privileged |
0f2d19dd JB |
1465 | ;; obarray). Being inside a root module is like using SCM without |
1466 | ;; any module system. | |
1467 | ;; | |
1468 | ||
1469 | ||
31d50456 | 1470 | (define (root-module-closure m s define?) |
0f2d19dd JB |
1471 | (let ((bi (and (symbol-interned? #f s) |
1472 | (builtin-variable s)))) | |
1473 | (and bi | |
1474 | (or define? (variable-bound? bi)) | |
1475 | (begin | |
1476 | (module-add! m s bi) | |
1477 | bi)))) | |
1478 | ||
1479 | (define (make-root-module) | |
31d50456 | 1480 | (make-module 1019 '() root-module-closure)) |
0f2d19dd JB |
1481 | |
1482 | ||
1483 | ;; make-scm-module | |
1484 | ||
1485 | ;; An scm module is a module into which the lazy binder copies | |
1486 | ;; variable bindings from the system symhash table. The mapping is | |
1487 | ;; one way only; newly introduced bindings in an scm module are not | |
1488 | ;; copied back into the system symhash table (and can be used to override | |
1489 | ;; bindings from the symhash table). | |
1490 | ;; | |
1491 | ||
6906bd0d MD |
1492 | (define (scm-module-closure m s define?) |
1493 | (let ((bi (and (symbol-interned? #f s) | |
1494 | (builtin-variable s)))) | |
1495 | (and bi | |
1496 | (variable-bound? bi) | |
1497 | (begin | |
1498 | (module-add! m s bi) | |
1499 | bi)))) | |
0f2d19dd | 1500 | |
6906bd0d MD |
1501 | (define (make-scm-module) |
1502 | (make-module 1019 '() scm-module-closure)) | |
0f2d19dd JB |
1503 | |
1504 | ||
1505 | ||
1506 | ;; the-module | |
631c1902 MD |
1507 | ;; |
1508 | ;; NOTE: This binding is used in libguile/modules.c. | |
1509 | ;; | |
bd0fb3cf | 1510 | (define the-module (make-fluid)) |
0f2d19dd | 1511 | |
7a0ff2f8 | 1512 | ;; scm:eval-transformer |
d43f8c97 | 1513 | ;; |
bd0fb3cf | 1514 | ;;(define scm:eval-transformer (make-fluid)) ; initialized in eval.c. |
d43f8c97 | 1515 | |
0f2d19dd JB |
1516 | ;; set-current-module module |
1517 | ;; | |
1518 | ;; set the current module as viewed by the normalizer. | |
1519 | ;; | |
631c1902 MD |
1520 | ;; NOTE: This binding is used in libguile/modules.c. |
1521 | ;; | |
0f2d19dd | 1522 | (define (set-current-module m) |
bd0fb3cf | 1523 | (fluid-set! the-module m) |
7a0ff2f8 MD |
1524 | (if m |
1525 | (begin | |
d7faeb2e | 1526 | ;; *top-level-lookup-closure* is now deprecated |
bd0fb3cf MD |
1527 | (fluid-set! *top-level-lookup-closure* |
1528 | (module-eval-closure (fluid-ref the-module))) | |
1529 | (fluid-set! scm:eval-transformer (module-transformer (fluid-ref the-module)))) | |
1530 | (fluid-set! *top-level-lookup-closure* #f))) | |
0f2d19dd JB |
1531 | |
1532 | ||
1533 | ;; current-module | |
1534 | ;; | |
1535 | ;; return the current module as viewed by the normalizer. | |
1536 | ;; | |
bd0fb3cf | 1537 | (define (current-module) (fluid-ref the-module)) |
0f2d19dd JB |
1538 | \f |
1539 | ;;; {Module-based Loading} | |
1540 | ;;; | |
1541 | ||
1542 | (define (save-module-excursion thunk) | |
1543 | (let ((inner-module (current-module)) | |
1544 | (outer-module #f)) | |
1545 | (dynamic-wind (lambda () | |
1546 | (set! outer-module (current-module)) | |
1547 | (set-current-module inner-module) | |
1548 | (set! inner-module #f)) | |
1549 | thunk | |
1550 | (lambda () | |
1551 | (set! inner-module (current-module)) | |
1552 | (set-current-module outer-module) | |
1553 | (set! outer-module #f))))) | |
1554 | ||
0f2d19dd JB |
1555 | (define basic-load load) |
1556 | ||
c6775c40 MD |
1557 | (define (load-module filename) |
1558 | (save-module-excursion | |
1559 | (lambda () | |
1560 | (let ((oldname (and (current-load-port) | |
1561 | (port-filename (current-load-port))))) | |
1562 | (basic-load (if (and oldname | |
1563 | (> (string-length filename) 0) | |
1564 | (not (char=? (string-ref filename 0) #\/)) | |
1565 | (not (string=? (dirname oldname) "."))) | |
1566 | (string-append (dirname oldname) "/" filename) | |
1567 | filename)))))) | |
0f2d19dd JB |
1568 | |
1569 | ||
1570 | \f | |
44cf1f0f | 1571 | ;;; {MODULE-REF -- exported} |
0f2d19dd JB |
1572 | ;; |
1573 | ;; Returns the value of a variable called NAME in MODULE or any of its | |
1574 | ;; used modules. If there is no such variable, then if the optional third | |
1575 | ;; argument DEFAULT is present, it is returned; otherwise an error is signaled. | |
1576 | ;; | |
1577 | (define (module-ref module name . rest) | |
1578 | (let ((variable (module-variable module name))) | |
1579 | (if (and variable (variable-bound? variable)) | |
1580 | (variable-ref variable) | |
1581 | (if (null? rest) | |
1582 | (error "No variable named" name 'in module) | |
1583 | (car rest) ; default value | |
1584 | )))) | |
1585 | ||
1586 | ;; MODULE-SET! -- exported | |
1587 | ;; | |
1588 | ;; Sets the variable called NAME in MODULE (or in a module that MODULE uses) | |
1589 | ;; to VALUE; if there is no such variable, an error is signaled. | |
1590 | ;; | |
1591 | (define (module-set! module name value) | |
1592 | (let ((variable (module-variable module name))) | |
1593 | (if variable | |
1594 | (variable-set! variable value) | |
1595 | (error "No variable named" name 'in module)))) | |
1596 | ||
1597 | ;; MODULE-DEFINE! -- exported | |
1598 | ;; | |
1599 | ;; Sets the variable called NAME in MODULE to VALUE; if there is no such | |
1600 | ;; variable, it is added first. | |
1601 | ;; | |
1602 | (define (module-define! module name value) | |
1603 | (let ((variable (module-local-variable module name))) | |
1604 | (if variable | |
1777c18b MD |
1605 | (begin |
1606 | (variable-set! variable value) | |
1a961d7e | 1607 | (module-modified module)) |
0f2d19dd JB |
1608 | (module-add! module name (make-variable value name))))) |
1609 | ||
ed218d98 MV |
1610 | ;; MODULE-DEFINED? -- exported |
1611 | ;; | |
1612 | ;; Return #t iff NAME is defined in MODULE (or in a module that MODULE | |
1613 | ;; uses) | |
1614 | ;; | |
1615 | (define (module-defined? module name) | |
1616 | (let ((variable (module-variable module name))) | |
1617 | (and variable (variable-bound? variable)))) | |
1618 | ||
0f2d19dd JB |
1619 | ;; MODULE-USE! module interface |
1620 | ;; | |
1621 | ;; Add INTERFACE to the list of interfaces used by MODULE. | |
1622 | ;; | |
1623 | (define (module-use! module interface) | |
1624 | (set-module-uses! module | |
1777c18b | 1625 | (cons interface (delq! interface (module-uses module)))) |
1a961d7e | 1626 | (module-modified module)) |
0f2d19dd JB |
1627 | |
1628 | \f | |
0f2d19dd JB |
1629 | ;;; {Recursive Namespaces} |
1630 | ;;; | |
1631 | ;;; | |
1632 | ;;; A hierarchical namespace emerges if we consider some module to be | |
1633 | ;;; root, and variables bound to modules as nested namespaces. | |
1634 | ;;; | |
1635 | ;;; The routines in this file manage variable names in hierarchical namespace. | |
1636 | ;;; Each variable name is a list of elements, looked up in successively nested | |
1637 | ;;; modules. | |
1638 | ;;; | |
0dd5491c | 1639 | ;;; (nested-ref some-root-module '(foo bar baz)) |
0f2d19dd JB |
1640 | ;;; => <value of a variable named baz in the module bound to bar in |
1641 | ;;; the module bound to foo in some-root-module> | |
1642 | ;;; | |
1643 | ;;; | |
1644 | ;;; There are: | |
1645 | ;;; | |
1646 | ;;; ;; a-root is a module | |
1647 | ;;; ;; name is a list of symbols | |
1648 | ;;; | |
0dd5491c MD |
1649 | ;;; nested-ref a-root name |
1650 | ;;; nested-set! a-root name val | |
1651 | ;;; nested-define! a-root name val | |
1652 | ;;; nested-remove! a-root name | |
0f2d19dd JB |
1653 | ;;; |
1654 | ;;; | |
1655 | ;;; (current-module) is a natural choice for a-root so for convenience there are | |
1656 | ;;; also: | |
1657 | ;;; | |
0dd5491c MD |
1658 | ;;; local-ref name == nested-ref (current-module) name |
1659 | ;;; local-set! name val == nested-set! (current-module) name val | |
1660 | ;;; local-define! name val == nested-define! (current-module) name val | |
1661 | ;;; local-remove! name == nested-remove! (current-module) name | |
0f2d19dd JB |
1662 | ;;; |
1663 | ||
1664 | ||
0dd5491c | 1665 | (define (nested-ref root names) |
0f2d19dd JB |
1666 | (let loop ((cur root) |
1667 | (elts names)) | |
1668 | (cond | |
1669 | ((null? elts) cur) | |
1670 | ((not (module? cur)) #f) | |
1671 | (else (loop (module-ref cur (car elts) #f) (cdr elts)))))) | |
1672 | ||
0dd5491c | 1673 | (define (nested-set! root names val) |
0f2d19dd JB |
1674 | (let loop ((cur root) |
1675 | (elts names)) | |
1676 | (if (null? (cdr elts)) | |
1677 | (module-set! cur (car elts) val) | |
1678 | (loop (module-ref cur (car elts)) (cdr elts))))) | |
1679 | ||
0dd5491c | 1680 | (define (nested-define! root names val) |
0f2d19dd JB |
1681 | (let loop ((cur root) |
1682 | (elts names)) | |
1683 | (if (null? (cdr elts)) | |
1684 | (module-define! cur (car elts) val) | |
1685 | (loop (module-ref cur (car elts)) (cdr elts))))) | |
1686 | ||
0dd5491c | 1687 | (define (nested-remove! root names) |
0f2d19dd JB |
1688 | (let loop ((cur root) |
1689 | (elts names)) | |
1690 | (if (null? (cdr elts)) | |
1691 | (module-remove! cur (car elts)) | |
1692 | (loop (module-ref cur (car elts)) (cdr elts))))) | |
1693 | ||
0dd5491c MD |
1694 | (define (local-ref names) (nested-ref (current-module) names)) |
1695 | (define (local-set! names val) (nested-set! (current-module) names val)) | |
1696 | (define (local-define names val) (nested-define! (current-module) names val)) | |
1697 | (define (local-remove names) (nested-remove! (current-module) names)) | |
0f2d19dd JB |
1698 | |
1699 | ||
1700 | \f | |
8bb7330c | 1701 | ;;; {The (app) module} |
0f2d19dd JB |
1702 | ;;; |
1703 | ;;; The root of conventionally named objects not directly in the top level. | |
1704 | ;;; | |
8bb7330c JB |
1705 | ;;; (app modules) |
1706 | ;;; (app modules guile) | |
0f2d19dd JB |
1707 | ;;; |
1708 | ;;; The directory of all modules and the standard root module. | |
1709 | ;;; | |
1710 | ||
edc185c7 MD |
1711 | (define (module-public-interface m) |
1712 | (module-ref m '%module-public-interface #f)) | |
1713 | (define (set-module-public-interface! m i) | |
1714 | (module-define! m '%module-public-interface i)) | |
1715 | (define (set-system-module! m s) | |
1716 | (set-procedure-property! (module-eval-closure m) 'system-module s)) | |
0f2d19dd JB |
1717 | (define the-root-module (make-root-module)) |
1718 | (define the-scm-module (make-scm-module)) | |
1719 | (set-module-public-interface! the-root-module the-scm-module) | |
d5504515 MD |
1720 | (set-module-name! the-root-module '(guile)) |
1721 | (set-module-name! the-scm-module '(guile)) | |
1722 | (set-module-kind! the-scm-module 'interface) | |
edc185c7 | 1723 | (for-each set-system-module! (list the-root-module the-scm-module) '(#t #t)) |
0f2d19dd JB |
1724 | |
1725 | (set-current-module the-root-module) | |
1726 | ||
1727 | (define app (make-module 31)) | |
0dd5491c MD |
1728 | (local-define '(app modules) (make-module 31)) |
1729 | (local-define '(app modules guile) the-root-module) | |
0f2d19dd JB |
1730 | |
1731 | ;; (define-special-value '(app modules new-ws) (lambda () (make-scm-module))) | |
1732 | ||
432558b9 MD |
1733 | (define (try-load-module name) |
1734 | (or (try-module-linked name) | |
1735 | (try-module-autoload name) | |
1736 | (try-module-dynamic-link name))) | |
1737 | ||
1f60d9d2 MD |
1738 | ;; NOTE: This binding is used in libguile/modules.c. |
1739 | ;; | |
0209ca9a | 1740 | (define (resolve-module name . maybe-autoload) |
0f2d19dd | 1741 | (let ((full-name (append '(app modules) name))) |
0dd5491c | 1742 | (let ((already (local-ref full-name))) |
432558b9 MD |
1743 | (if already |
1744 | ;; The module already exists... | |
1745 | (if (and (or (null? maybe-autoload) (car maybe-autoload)) | |
1746 | (not (module-ref already '%module-public-interface #f))) | |
1747 | ;; ...but we are told to load and it doesn't contain source, so | |
1748 | (begin | |
1749 | (try-load-module name) | |
1750 | already) | |
1751 | ;; simply return it. | |
1752 | already) | |
3e3cec45 | 1753 | (begin |
432558b9 | 1754 | ;; Try to autoload it if we are told so |
3e3cec45 | 1755 | (if (or (null? maybe-autoload) (car maybe-autoload)) |
432558b9 MD |
1756 | (try-load-module name)) |
1757 | ;; Get/create it. | |
3e3cec45 | 1758 | (make-modules-in (current-module) full-name)))))) |
0f2d19dd JB |
1759 | |
1760 | (define (beautify-user-module! module) | |
3e3cec45 MD |
1761 | (let ((interface (module-public-interface module))) |
1762 | (if (or (not interface) | |
1763 | (eq? interface module)) | |
1764 | (let ((interface (make-module 31))) | |
1765 | (set-module-name! interface (module-name module)) | |
1766 | (set-module-kind! interface 'interface) | |
1767 | (set-module-public-interface! module interface)))) | |
cc7f066c MD |
1768 | (if (and (not (memq the-scm-module (module-uses module))) |
1769 | (not (eq? module the-root-module))) | |
0f2d19dd JB |
1770 | (set-module-uses! module (append (module-uses module) (list the-scm-module))))) |
1771 | ||
90847923 MD |
1772 | (define (purify-module! module) |
1773 | "Removes bindings in MODULE which are inherited from the (guile) module." | |
1774 | (let ((use-list (module-uses module))) | |
1775 | (if (and (pair? use-list) | |
1776 | (eq? (car (last-pair use-list)) the-scm-module)) | |
1777 | (set-module-uses! module (reverse (cdr (reverse use-list))))))) | |
1778 | ||
631c1902 MD |
1779 | ;; NOTE: This binding is used in libguile/modules.c. |
1780 | ;; | |
0f2d19dd JB |
1781 | (define (make-modules-in module name) |
1782 | (if (null? name) | |
1783 | module | |
1784 | (cond | |
3e3cec45 MD |
1785 | ((module-ref module (car name) #f) |
1786 | => (lambda (m) (make-modules-in m (cdr name)))) | |
0f2d19dd JB |
1787 | (else (let ((m (make-module 31))) |
1788 | (set-module-kind! m 'directory) | |
d5504515 MD |
1789 | (set-module-name! m (append (or (module-name module) |
1790 | '()) | |
1791 | (list (car name)))) | |
0f2d19dd JB |
1792 | (module-define! module (car name) m) |
1793 | (make-modules-in m (cdr name))))))) | |
1794 | ||
1795 | (define (resolve-interface name) | |
1796 | (let ((module (resolve-module name))) | |
1797 | (and module (module-public-interface module)))) | |
1798 | ||
1799 | ||
1800 | (define %autoloader-developer-mode #t) | |
1801 | ||
1802 | (define (process-define-module args) | |
1803 | (let* ((module-id (car args)) | |
0209ca9a | 1804 | (module (resolve-module module-id #f)) |
0f2d19dd JB |
1805 | (kws (cdr args))) |
1806 | (beautify-user-module! module) | |
0209ca9a | 1807 | (let loop ((kws kws) |
44484f52 MD |
1808 | (reversed-interfaces '()) |
1809 | (exports '())) | |
0209ca9a | 1810 | (if (null? kws) |
44484f52 MD |
1811 | (begin |
1812 | (for-each (lambda (interface) | |
1813 | (module-use! module interface)) | |
1814 | reversed-interfaces) | |
1815 | (module-export! module exports)) | |
f714ca8e MD |
1816 | (let ((keyword (cond ((keyword? (car kws)) |
1817 | (keyword->symbol (car kws))) | |
1818 | ((and (symbol? (car kws)) | |
1819 | (eq? (string-ref (car kws) 0) #\:)) | |
1820 | (string->symbol (substring (car kws) 1))) | |
1821 | (else #f)))) | |
1822 | (case keyword | |
1823 | ((use-module use-syntax) | |
1824 | (if (not (pair? (cdr kws))) | |
1825 | (error "unrecognized defmodule argument" kws)) | |
1826 | (let* ((used-name (cadr kws)) | |
1827 | (used-module (resolve-module used-name))) | |
45a02a29 MD |
1828 | (if (not (module-ref used-module |
1829 | '%module-public-interface | |
1830 | #f)) | |
3e3cec45 | 1831 | (begin |
45a02a29 MD |
1832 | ((if %autoloader-developer-mode warn error) |
1833 | "no code for module" (module-name used-module)) | |
1834 | (beautify-user-module! used-module))) | |
1835 | (let ((interface (module-public-interface used-module))) | |
1836 | (if (not interface) | |
1837 | (error "missing interface for use-module" | |
1838 | used-module)) | |
1839 | (if (eq? keyword 'use-syntax) | |
7cbaee0c MD |
1840 | (set-module-transformer! |
1841 | module | |
45a02a29 MD |
1842 | (module-ref interface (car (last-pair used-name)) |
1843 | #f))) | |
1844 | (loop (cddr kws) | |
44484f52 MD |
1845 | (cons interface reversed-interfaces) |
1846 | exports)))) | |
71225060 MD |
1847 | ((autoload) |
1848 | (if (not (and (pair? (cdr kws)) (pair? (cddr kws)))) | |
1849 | (error "unrecognized defmodule argument" kws)) | |
1850 | (loop (cdddr kws) | |
1851 | (cons (make-autoload-interface module | |
1852 | (cadr kws) | |
1853 | (caddr kws)) | |
44484f52 MD |
1854 | reversed-interfaces) |
1855 | exports)) | |
edc185c7 MD |
1856 | ((no-backtrace) |
1857 | (set-system-module! module #t) | |
44484f52 | 1858 | (loop (cdr kws) reversed-interfaces exports)) |
90847923 MD |
1859 | ((pure) |
1860 | (purify-module! module) | |
44484f52 | 1861 | (loop (cdr kws) reversed-interfaces exports)) |
90847923 | 1862 | ((export) |
a473fead | 1863 | (if (not (pair? (cdr kws))) |
90847923 | 1864 | (error "unrecognized defmodule argument" kws)) |
44484f52 MD |
1865 | (loop (cddr kws) |
1866 | reversed-interfaces | |
1867 | (append (cadr kws) exports))) | |
f714ca8e MD |
1868 | (else |
1869 | (error "unrecognized defmodule argument" kws)))))) | |
0f2d19dd | 1870 | module)) |
71225060 MD |
1871 | |
1872 | ;;; {Autoload} | |
1873 | ||
1874 | (define (make-autoload-interface module name bindings) | |
1875 | (let ((b (lambda (a sym definep) | |
1876 | (and (memq sym bindings) | |
1877 | (let ((i (module-public-interface (resolve-module name)))) | |
1878 | (if (not i) | |
1879 | (error "missing interface for module" name)) | |
1880 | ;; Replace autoload-interface with interface | |
1881 | (set-car! (memq a (module-uses module)) i) | |
1882 | (module-local-variable i sym)))))) | |
d5504515 | 1883 | (module-constructor #() '() b #f #f name 'autoload |
6b64c19b | 1884 | '() (make-weak-value-hash-table 31) 0))) |
71225060 | 1885 | |
0f2d19dd | 1886 | \f |
44cf1f0f | 1887 | ;;; {Autoloading modules} |
0f2d19dd JB |
1888 | |
1889 | (define autoloads-in-progress '()) | |
1890 | ||
1891 | (define (try-module-autoload module-name) | |
0f2d19dd JB |
1892 | (let* ((reverse-name (reverse module-name)) |
1893 | (name (car reverse-name)) | |
1894 | (dir-hint-module-name (reverse (cdr reverse-name))) | |
1895 | (dir-hint (apply symbol-append (map (lambda (elt) (symbol-append elt "/")) dir-hint-module-name)))) | |
0209ca9a | 1896 | (resolve-module dir-hint-module-name #f) |
0f2d19dd JB |
1897 | (and (not (autoload-done-or-in-progress? dir-hint name)) |
1898 | (let ((didit #f)) | |
1899 | (dynamic-wind | |
1900 | (lambda () (autoload-in-progress! dir-hint name)) | |
defed517 MD |
1901 | (lambda () |
1902 | (let ((full (%search-load-path (in-vicinity dir-hint name)))) | |
1903 | (if full | |
1904 | (begin | |
1905 | (save-module-excursion (lambda () (primitive-load full))) | |
1906 | (set! didit #t))))) | |
0f2d19dd JB |
1907 | (lambda () (set-autoloaded! dir-hint name didit))) |
1908 | didit)))) | |
1909 | ||
71225060 | 1910 | \f |
d0cbd20c MV |
1911 | ;;; Dynamic linking of modules |
1912 | ||
1913 | ;; Initializing a module that is written in C is a two step process. | |
1914 | ;; First the module's `module init' function is called. This function | |
1915 | ;; is expected to call `scm_register_module_xxx' to register the `real | |
1916 | ;; init' function. Later, when the module is referenced for the first | |
1917 | ;; time, this real init function is called in the right context. See | |
1918 | ;; gtcltk-lib/gtcltk-module.c for an example. | |
1919 | ;; | |
1920 | ;; The code for the module can be in a regular shared library (so that | |
1921 | ;; the `module init' function will be called when libguile is | |
1922 | ;; initialized). Or it can be dynamically linked. | |
1923 | ;; | |
1924 | ;; You can safely call `scm_register_module_xxx' before libguile | |
1925 | ;; itself is initialized. You could call it from an C++ constructor | |
1926 | ;; of a static object, for example. | |
1927 | ;; | |
1928 | ;; To make your Guile extension into a dynamic linkable module, follow | |
1929 | ;; these easy steps: | |
1930 | ;; | |
8bb7330c | 1931 | ;; - Find a name for your module, like (ice-9 gtcltk) |
d0cbd20c MV |
1932 | ;; - Write a function with a name like |
1933 | ;; | |
1934 | ;; scm_init_ice_9_gtcltk_module | |
1935 | ;; | |
1936 | ;; This is your `module init' function. It should call | |
1937 | ;; | |
1938 | ;; scm_register_module_xxx ("ice-9 gtcltk", scm_init_gtcltk); | |
1939 | ;; | |
1940 | ;; "ice-9 gtcltk" is the C version of the module name. Slashes are | |
1941 | ;; replaced by spaces, the rest is untouched. `scm_init_gtcltk' is | |
ed218d98 | 1942 | ;; the real init function that executes the usual initializations |
d0cbd20c MV |
1943 | ;; like making new smobs, etc. |
1944 | ;; | |
1945 | ;; - Make a shared library with your code and a name like | |
1946 | ;; | |
1947 | ;; ice-9/libgtcltk.so | |
1948 | ;; | |
1949 | ;; and put it somewhere in %load-path. | |
1950 | ;; | |
8bb7330c | 1951 | ;; - Then you can simply write `:use-module (ice-9 gtcltk)' and it |
d0cbd20c MV |
1952 | ;; will be linked automatically. |
1953 | ;; | |
1954 | ;; This is all very experimental. | |
1955 | ||
1956 | (define (split-c-module-name str) | |
1957 | (let loop ((rev '()) | |
1958 | (start 0) | |
1959 | (pos 0) | |
1960 | (end (string-length str))) | |
1961 | (cond | |
1962 | ((= pos end) | |
1963 | (reverse (cons (string->symbol (substring str start pos)) rev))) | |
1964 | ((eq? (string-ref str pos) #\space) | |
1965 | (loop (cons (string->symbol (substring str start pos)) rev) | |
1966 | (+ pos 1) | |
1967 | (+ pos 1) | |
1968 | end)) | |
1969 | (else | |
1970 | (loop rev start (+ pos 1) end))))) | |
1971 | ||
1972 | (define (convert-c-registered-modules dynobj) | |
1973 | (let ((res (map (lambda (c) | |
1974 | (list (split-c-module-name (car c)) (cdr c) dynobj)) | |
1975 | (c-registered-modules)))) | |
1976 | (c-clear-registered-modules) | |
1977 | res)) | |
1978 | ||
3e3cec45 MD |
1979 | (define registered-modules '()) |
1980 | ||
1981 | (define (register-modules dynobj) | |
1982 | (set! registered-modules | |
1983 | (append! (convert-c-registered-modules dynobj) | |
1984 | registered-modules))) | |
1985 | ||
d0cbd20c | 1986 | (define (init-dynamic-module modname) |
3e3cec45 MD |
1987 | ;; Register any linked modules which has been registered on the C level |
1988 | (register-modules #f) | |
d0cbd20c MV |
1989 | (or-map (lambda (modinfo) |
1990 | (if (equal? (car modinfo) modname) | |
c04e89c7 MD |
1991 | (begin |
1992 | (set! registered-modules (delq! modinfo registered-modules)) | |
1993 | (let ((mod (resolve-module modname #f))) | |
1994 | (save-module-excursion | |
1995 | (lambda () | |
1996 | (set-current-module mod) | |
1997 | (set-module-public-interface! mod mod) | |
1998 | (dynamic-call (cadr modinfo) (caddr modinfo)) | |
1999 | )) | |
2000 | #t)) | |
d0cbd20c MV |
2001 | #f)) |
2002 | registered-modules)) | |
2003 | ||
2004 | (define (dynamic-maybe-call name dynobj) | |
2005 | (catch #t ; could use false-if-exception here | |
2006 | (lambda () | |
2007 | (dynamic-call name dynobj)) | |
2008 | (lambda args | |
2009 | #f))) | |
2010 | ||
ed218d98 MV |
2011 | (define (dynamic-maybe-link filename) |
2012 | (catch #t ; could use false-if-exception here | |
2013 | (lambda () | |
2014 | (dynamic-link filename)) | |
2015 | (lambda args | |
2016 | #f))) | |
2017 | ||
d0cbd20c MV |
2018 | (define (find-and-link-dynamic-module module-name) |
2019 | (define (make-init-name mod-name) | |
bd9e24b3 | 2020 | (string-append "scm_init" |
d0cbd20c MV |
2021 | (list->string (map (lambda (c) |
2022 | (if (or (char-alphabetic? c) | |
2023 | (char-numeric? c)) | |
2024 | c | |
2025 | #\_)) | |
2026 | (string->list mod-name))) | |
bd9e24b3 | 2027 | "_module")) |
ebd79f62 TP |
2028 | |
2029 | ;; Put the subdirectory for this module in the car of SUBDIR-AND-LIBNAME, | |
2030 | ;; and the `libname' (the name of the module prepended by `lib') in the cdr | |
2031 | ;; field. For example, if MODULE-NAME is the list (inet tcp-ip udp), then | |
2032 | ;; SUBDIR-AND-LIBNAME will be the pair ("inet/tcp-ip" . "libudp"). | |
2033 | (let ((subdir-and-libname | |
d0cbd20c MV |
2034 | (let loop ((dirs "") |
2035 | (syms module-name)) | |
ebd79f62 TP |
2036 | (if (null? (cdr syms)) |
2037 | (cons dirs (string-append "lib" (car syms))) | |
2038 | (loop (string-append dirs (car syms) "/") (cdr syms))))) | |
d0cbd20c MV |
2039 | (init (make-init-name (apply string-append |
2040 | (map (lambda (s) | |
2041 | (string-append "_" s)) | |
2042 | module-name))))) | |
ebd79f62 TP |
2043 | (let ((subdir (car subdir-and-libname)) |
2044 | (libname (cdr subdir-and-libname))) | |
2045 | ||
2046 | ;; Now look in each dir in %LOAD-PATH for `subdir/libfoo.la'. If that | |
2047 | ;; file exists, fetch the dlname from that file and attempt to link | |
2048 | ;; against it. If `subdir/libfoo.la' does not exist, or does not seem | |
2049 | ;; to name any shared library, look for `subdir/libfoo.so' instead and | |
2050 | ;; link against that. | |
2051 | (let check-dirs ((dir-list %load-path)) | |
2052 | (if (null? dir-list) | |
2053 | #f | |
2054 | (let* ((dir (in-vicinity (car dir-list) subdir)) | |
2055 | (sharlib-full | |
2056 | (or (try-using-libtool-name dir libname) | |
2057 | (try-using-sharlib-name dir libname)))) | |
2058 | (if (and sharlib-full (file-exists? sharlib-full)) | |
2059 | (link-dynamic-module sharlib-full init) | |
2060 | (check-dirs (cdr dir-list))))))))) | |
2061 | ||
2062 | (define (try-using-libtool-name libdir libname) | |
ebd79f62 TP |
2063 | (let ((libtool-filename (in-vicinity libdir |
2064 | (string-append libname ".la")))) | |
2065 | (and (file-exists? libtool-filename) | |
352d134c | 2066 | libtool-filename))) |
ebd79f62 TP |
2067 | |
2068 | (define (try-using-sharlib-name libdir libname) | |
2069 | (in-vicinity libdir (string-append libname ".so"))) | |
d0cbd20c MV |
2070 | |
2071 | (define (link-dynamic-module filename initname) | |
3e3cec45 MD |
2072 | ;; Register any linked modules which has been registered on the C level |
2073 | (register-modules #f) | |
6b856182 MV |
2074 | (let ((dynobj (dynamic-link filename))) |
2075 | (dynamic-call initname dynobj) | |
3e3cec45 | 2076 | (register-modules dynobj))) |
a4f9b1f6 MD |
2077 | |
2078 | (define (try-module-linked module-name) | |
2079 | (init-dynamic-module module-name)) | |
2080 | ||
d0cbd20c | 2081 | (define (try-module-dynamic-link module-name) |
a4f9b1f6 MD |
2082 | (and (find-and-link-dynamic-module module-name) |
2083 | (init-dynamic-module module-name))) | |
d0cbd20c | 2084 | |
ed218d98 MV |
2085 | |
2086 | ||
0f2d19dd JB |
2087 | (define autoloads-done '((guile . guile))) |
2088 | ||
2089 | (define (autoload-done-or-in-progress? p m) | |
2090 | (let ((n (cons p m))) | |
2091 | (->bool (or (member n autoloads-done) | |
2092 | (member n autoloads-in-progress))))) | |
2093 | ||
2094 | (define (autoload-done! p m) | |
2095 | (let ((n (cons p m))) | |
2096 | (set! autoloads-in-progress | |
2097 | (delete! n autoloads-in-progress)) | |
2098 | (or (member n autoloads-done) | |
2099 | (set! autoloads-done (cons n autoloads-done))))) | |
2100 | ||
2101 | (define (autoload-in-progress! p m) | |
2102 | (let ((n (cons p m))) | |
2103 | (set! autoloads-done | |
2104 | (delete! n autoloads-done)) | |
2105 | (set! autoloads-in-progress (cons n autoloads-in-progress)))) | |
2106 | ||
2107 | (define (set-autoloaded! p m done?) | |
2108 | (if done? | |
2109 | (autoload-done! p m) | |
2110 | (let ((n (cons p m))) | |
2111 | (set! autoloads-done (delete! n autoloads-done)) | |
2112 | (set! autoloads-in-progress (delete! n autoloads-in-progress))))) | |
2113 | ||
2114 | ||
2115 | ||
2116 | ||
2117 | \f | |
2118 | ;;; {Macros} | |
2119 | ;;; | |
2120 | ||
7a0ff2f8 MD |
2121 | (define (primitive-macro? m) |
2122 | (and (macro? m) | |
2123 | (not (macro-transformer m)))) | |
2124 | ||
2125 | ;;; {Defmacros} | |
2126 | ;;; | |
9591db87 MD |
2127 | (define macro-table (make-weak-key-hash-table 523)) |
2128 | (define xformer-table (make-weak-key-hash-table 523)) | |
0f2d19dd JB |
2129 | |
2130 | (define (defmacro? m) (hashq-ref macro-table m)) | |
2131 | (define (assert-defmacro?! m) (hashq-set! macro-table m #t)) | |
2132 | (define (defmacro-transformer m) (hashq-ref xformer-table m)) | |
2133 | (define (set-defmacro-transformer! m t) (hashq-set! xformer-table m t)) | |
2134 | ||
2135 | (define defmacro:transformer | |
2136 | (lambda (f) | |
2137 | (let* ((xform (lambda (exp env) | |
2138 | (copy-tree (apply f (cdr exp))))) | |
2139 | (a (procedure->memoizing-macro xform))) | |
2140 | (assert-defmacro?! a) | |
2141 | (set-defmacro-transformer! a f) | |
2142 | a))) | |
2143 | ||
2144 | ||
2145 | (define defmacro | |
2146 | (let ((defmacro-transformer | |
2147 | (lambda (name parms . body) | |
2148 | (let ((transformer `(lambda ,parms ,@body))) | |
2149 | `(define ,name | |
2150 | (,(lambda (transformer) | |
2151 | (defmacro:transformer transformer)) | |
2152 | ,transformer)))))) | |
2153 | (defmacro:transformer defmacro-transformer))) | |
2154 | ||
2155 | (define defmacro:syntax-transformer | |
2156 | (lambda (f) | |
2157 | (procedure->syntax | |
2158 | (lambda (exp env) | |
2159 | (copy-tree (apply f (cdr exp))))))) | |
2160 | ||
ed218d98 MV |
2161 | |
2162 | ;; XXX - should the definition of the car really be looked up in the | |
2163 | ;; current module? | |
2164 | ||
0f2d19dd JB |
2165 | (define (macroexpand-1 e) |
2166 | (cond | |
2167 | ((pair? e) (let* ((a (car e)) | |
ed218d98 | 2168 | (val (and (symbol? a) (local-ref (list a))))) |
0f2d19dd JB |
2169 | (if (defmacro? val) |
2170 | (apply (defmacro-transformer val) (cdr e)) | |
2171 | e))) | |
2172 | (#t e))) | |
2173 | ||
2174 | (define (macroexpand e) | |
2175 | (cond | |
2176 | ((pair? e) (let* ((a (car e)) | |
ed218d98 | 2177 | (val (and (symbol? a) (local-ref (list a))))) |
0f2d19dd JB |
2178 | (if (defmacro? val) |
2179 | (macroexpand (apply (defmacro-transformer val) (cdr e))) | |
2180 | e))) | |
2181 | (#t e))) | |
2182 | ||
e672f1b5 MD |
2183 | (define (gentemp) |
2184 | (gensym "scm:G")) | |
0f2d19dd | 2185 | |
534a0099 | 2186 | (provide 'defmacro) |
0f2d19dd JB |
2187 | |
2188 | \f | |
2189 | ||
83b38198 MD |
2190 | ;;; {Run-time options} |
2191 | ||
16b8ebbe MD |
2192 | ((let* ((names '((eval-options-interface |
2193 | (eval-options eval-enable eval-disable) | |
2194 | (eval-set!)) | |
2195 | ||
2196 | (debug-options-interface | |
83b38198 MD |
2197 | (debug-options debug-enable debug-disable) |
2198 | (debug-set!)) | |
2199 | ||
2200 | (evaluator-traps-interface | |
2201 | (traps trap-enable trap-disable) | |
2202 | (trap-set!)) | |
2203 | ||
2204 | (read-options-interface | |
2205 | (read-options read-enable read-disable) | |
2206 | (read-set!)) | |
2207 | ||
2208 | (print-options-interface | |
2209 | (print-options print-enable print-disable) | |
2210 | (print-set!)) | |
e586be78 MD |
2211 | |
2212 | (readline-options-interface | |
2213 | (readline-options readline-enable readline-disable) | |
2214 | (readline-set!)) | |
83b38198 MD |
2215 | )) |
2216 | (option-name car) | |
2217 | (option-value cadr) | |
2218 | (option-documentation caddr) | |
2219 | ||
2220 | (print-option (lambda (option) | |
2221 | (display (option-name option)) | |
2222 | (if (< (string-length | |
2223 | (symbol->string (option-name option))) | |
2224 | 8) | |
2225 | (display #\tab)) | |
2226 | (display #\tab) | |
2227 | (display (option-value option)) | |
2228 | (display #\tab) | |
2229 | (display (option-documentation option)) | |
2230 | (newline))) | |
2231 | ||
2232 | ;; Below follows the macros defining the run-time option interfaces. | |
2233 | ||
2234 | (make-options (lambda (interface) | |
2235 | `(lambda args | |
2236 | (cond ((null? args) (,interface)) | |
45456413 | 2237 | ((list? (car args)) |
83b38198 | 2238 | (,interface (car args)) (,interface)) |
2f110c3c | 2239 | (else (for-each ,print-option |
83b38198 MD |
2240 | (,interface #t))))))) |
2241 | ||
2242 | (make-enable (lambda (interface) | |
2243 | `(lambda flags | |
2244 | (,interface (append flags (,interface))) | |
2245 | (,interface)))) | |
2246 | ||
2247 | (make-disable (lambda (interface) | |
2248 | `(lambda flags | |
2249 | (let ((options (,interface))) | |
2250 | (for-each (lambda (flag) | |
2251 | (set! options (delq! flag options))) | |
2252 | flags) | |
2253 | (,interface options) | |
2254 | (,interface))))) | |
2255 | ||
2256 | (make-set! (lambda (interface) | |
2257 | `((name exp) | |
2258 | (,'quasiquote | |
2259 | (begin (,interface (append (,interface) | |
2260 | (list '(,'unquote name) | |
2261 | (,'unquote exp)))) | |
2262 | (,interface)))))) | |
2263 | ) | |
2264 | (procedure->macro | |
2265 | (lambda (exp env) | |
2266 | (cons 'begin | |
2267 | (apply append | |
2268 | (map (lambda (group) | |
2269 | (let ((interface (car group))) | |
2270 | (append (map (lambda (name constructor) | |
2271 | `(define ,name | |
2272 | ,(constructor interface))) | |
2273 | (cadr group) | |
2274 | (list make-options | |
2275 | make-enable | |
2276 | make-disable)) | |
2277 | (map (lambda (name constructor) | |
2278 | `(defmacro ,name | |
2279 | ,@(constructor interface))) | |
2280 | (caddr group) | |
2281 | (list make-set!))))) | |
2282 | names))))))) | |
2283 | ||
2284 | \f | |
2285 | ||
0f2d19dd JB |
2286 | ;;; {Running Repls} |
2287 | ;;; | |
2288 | ||
2289 | (define (repl read evaler print) | |
75a97b92 | 2290 | (let loop ((source (read (current-input-port)))) |
0f2d19dd | 2291 | (print (evaler source)) |
75a97b92 | 2292 | (loop (read (current-input-port))))) |
0f2d19dd JB |
2293 | |
2294 | ;; A provisional repl that acts like the SCM repl: | |
2295 | ;; | |
2296 | (define scm-repl-silent #f) | |
2297 | (define (assert-repl-silence v) (set! scm-repl-silent v)) | |
2298 | ||
21ed9efe MD |
2299 | (define *unspecified* (if #f #f)) |
2300 | (define (unspecified? v) (eq? v *unspecified*)) | |
2301 | ||
2302 | (define scm-repl-print-unspecified #f) | |
2303 | (define (assert-repl-print-unspecified v) (set! scm-repl-print-unspecified v)) | |
2304 | ||
79451588 | 2305 | (define scm-repl-verbose #f) |
0f2d19dd JB |
2306 | (define (assert-repl-verbosity v) (set! scm-repl-verbose v)) |
2307 | ||
e6875011 | 2308 | (define scm-repl-prompt "guile> ") |
0f2d19dd | 2309 | |
e6875011 MD |
2310 | (define (set-repl-prompt! v) (set! scm-repl-prompt v)) |
2311 | ||
d5d34fa1 MD |
2312 | (define (default-lazy-handler key . args) |
2313 | (save-stack lazy-handler-dispatch) | |
2314 | (apply throw key args)) | |
2315 | ||
45456413 | 2316 | (define enter-frame-handler default-lazy-handler) |
d5d34fa1 MD |
2317 | (define apply-frame-handler default-lazy-handler) |
2318 | (define exit-frame-handler default-lazy-handler) | |
2319 | ||
2320 | (define (lazy-handler-dispatch key . args) | |
2321 | (case key | |
2322 | ((apply-frame) | |
2323 | (apply apply-frame-handler key args)) | |
2324 | ((exit-frame) | |
2325 | (apply exit-frame-handler key args)) | |
45456413 MD |
2326 | ((enter-frame) |
2327 | (apply enter-frame-handler key args)) | |
d5d34fa1 MD |
2328 | (else |
2329 | (apply default-lazy-handler key args)))) | |
0f2d19dd | 2330 | |
3e3cec45 | 2331 | (define abort-hook (make-hook)) |
59e1116d | 2332 | |
28d8ab3c GH |
2333 | ;; these definitions are used if running a script. |
2334 | ;; otherwise redefined in error-catching-loop. | |
2335 | (define (set-batch-mode?! arg) #t) | |
2336 | (define (batch-mode?) #t) | |
4bbbcd5c | 2337 | |
0f2d19dd | 2338 | (define (error-catching-loop thunk) |
4bbbcd5c GH |
2339 | (let ((status #f) |
2340 | (interactive #t)) | |
8e44e7a0 GH |
2341 | (define (loop first) |
2342 | (let ((next | |
2343 | (catch #t | |
9a0d70e2 | 2344 | |
8e44e7a0 GH |
2345 | (lambda () |
2346 | (lazy-catch #t | |
2347 | (lambda () | |
2348 | (dynamic-wind | |
2349 | (lambda () (unmask-signals)) | |
2350 | (lambda () | |
45456413 MD |
2351 | (with-traps |
2352 | (lambda () | |
2353 | (first) | |
8e44e7a0 | 2354 | |
45456413 MD |
2355 | ;; This line is needed because mark |
2356 | ;; doesn't do closures quite right. | |
2357 | ;; Unreferenced locals should be | |
2358 | ;; collected. | |
2359 | ;; | |
2360 | (set! first #f) | |
2361 | (let loop ((v (thunk))) | |
2362 | (loop (thunk))) | |
2363 | #f))) | |
8e44e7a0 GH |
2364 | (lambda () (mask-signals)))) |
2365 | ||
2366 | lazy-handler-dispatch)) | |
2367 | ||
2368 | (lambda (key . args) | |
2369 | (case key | |
2370 | ((quit) | |
8e44e7a0 GH |
2371 | (set! status args) |
2372 | #f) | |
2373 | ||
2374 | ((switch-repl) | |
2375 | (apply throw 'switch-repl args)) | |
2376 | ||
2377 | ((abort) | |
2378 | ;; This is one of the closures that require | |
2379 | ;; (set! first #f) above | |
2380 | ;; | |
2381 | (lambda () | |
04efd24d | 2382 | (run-hook abort-hook) |
e13c54c4 | 2383 | (force-output (current-output-port)) |
8e44e7a0 GH |
2384 | (display "ABORT: " (current-error-port)) |
2385 | (write args (current-error-port)) | |
2386 | (newline (current-error-port)) | |
4bbbcd5c | 2387 | (if interactive |
e13c54c4 JB |
2388 | (begin |
2389 | (if (and | |
2390 | (not has-shown-debugger-hint?) | |
2391 | (not (memq 'backtrace | |
2392 | (debug-options-interface))) | |
2393 | (stack? (fluid-ref the-last-stack))) | |
2394 | (begin | |
2395 | (newline (current-error-port)) | |
2396 | (display | |
cb546c61 | 2397 | "Type \"(backtrace)\" to get more information or \"(debug)\" to enter the debugger.\n" |
e13c54c4 JB |
2398 | (current-error-port)) |
2399 | (set! has-shown-debugger-hint? #t))) | |
2400 | (force-output (current-error-port))) | |
2401 | (begin | |
2402 | (primitive-exit 1))) | |
8e44e7a0 GH |
2403 | (set! stack-saved? #f))) |
2404 | ||
2405 | (else | |
2406 | ;; This is the other cons-leak closure... | |
2407 | (lambda () | |
2408 | (cond ((= (length args) 4) | |
2409 | (apply handle-system-error key args)) | |
2410 | (else | |
2411 | (apply bad-throw key args)))))))))) | |
2412 | (if next (loop next) status))) | |
5f5f2642 MD |
2413 | (set! set-batch-mode?! (lambda (arg) |
2414 | (cond (arg | |
2415 | (set! interactive #f) | |
2416 | (restore-signals)) | |
2417 | (#t | |
2418 | (error "sorry, not implemented"))))) | |
2419 | (set! batch-mode? (lambda () (not interactive))) | |
8e44e7a0 | 2420 | (loop (lambda () #t)))) |
0f2d19dd | 2421 | |
8bb7f646 | 2422 | ;;(define the-last-stack (make-fluid)) Defined by scm_init_backtrace () |
8087b6be | 2423 | (define before-signal-stack (make-fluid)) |
21ed9efe MD |
2424 | (define stack-saved? #f) |
2425 | ||
2426 | (define (save-stack . narrowing) | |
edc185c7 MD |
2427 | (or stack-saved? |
2428 | (cond ((not (memq 'debug (debug-options-interface))) | |
2429 | (fluid-set! the-last-stack #f) | |
2430 | (set! stack-saved? #t)) | |
2431 | (else | |
2432 | (fluid-set! | |
2433 | the-last-stack | |
2434 | (case (stack-id #t) | |
2435 | ((repl-stack) | |
2436 | (apply make-stack #t save-stack eval #t 0 narrowing)) | |
2437 | ((load-stack) | |
2438 | (apply make-stack #t save-stack 0 #t 0 narrowing)) | |
2439 | ((tk-stack) | |
2440 | (apply make-stack #t save-stack tk-stack-mark #t 0 narrowing)) | |
2441 | ((#t) | |
2442 | (apply make-stack #t save-stack 0 1 narrowing)) | |
2443 | (else | |
2444 | (let ((id (stack-id #t))) | |
2445 | (and (procedure? id) | |
2446 | (apply make-stack #t save-stack id #t 0 narrowing)))))) | |
2447 | (set! stack-saved? #t))))) | |
1c6cd8e8 | 2448 | |
3e3cec45 MD |
2449 | (define before-error-hook (make-hook)) |
2450 | (define after-error-hook (make-hook)) | |
2451 | (define before-backtrace-hook (make-hook)) | |
2452 | (define after-backtrace-hook (make-hook)) | |
1c6cd8e8 | 2453 | |
21ed9efe MD |
2454 | (define has-shown-debugger-hint? #f) |
2455 | ||
35c5db87 GH |
2456 | (define (handle-system-error key . args) |
2457 | (let ((cep (current-error-port))) | |
8bb7f646 | 2458 | (cond ((not (stack? (fluid-ref the-last-stack)))) |
21ed9efe | 2459 | ((memq 'backtrace (debug-options-interface)) |
04efd24d | 2460 | (run-hook before-backtrace-hook) |
21ed9efe | 2461 | (newline cep) |
755457ec | 2462 | (display "Backtrace:\n") |
8bb7f646 | 2463 | (display-backtrace (fluid-ref the-last-stack) cep) |
21ed9efe | 2464 | (newline cep) |
04efd24d MD |
2465 | (run-hook after-backtrace-hook))) |
2466 | (run-hook before-error-hook) | |
8bb7f646 | 2467 | (apply display-error (fluid-ref the-last-stack) cep args) |
04efd24d | 2468 | (run-hook after-error-hook) |
35c5db87 GH |
2469 | (force-output cep) |
2470 | (throw 'abort key))) | |
21ed9efe | 2471 | |
0f2d19dd JB |
2472 | (define (quit . args) |
2473 | (apply throw 'quit args)) | |
2474 | ||
7950df7c GH |
2475 | (define exit quit) |
2476 | ||
d590bbf6 MD |
2477 | ;;(define has-shown-backtrace-hint? #f) Defined by scm_init_backtrace () |
2478 | ||
2479 | ;; Replaced by C code: | |
2480 | ;;(define (backtrace) | |
8bb7f646 | 2481 | ;; (if (fluid-ref the-last-stack) |
d590bbf6 MD |
2482 | ;; (begin |
2483 | ;; (newline) | |
8bb7f646 | 2484 | ;; (display-backtrace (fluid-ref the-last-stack) (current-output-port)) |
d590bbf6 MD |
2485 | ;; (newline) |
2486 | ;; (if (and (not has-shown-backtrace-hint?) | |
2487 | ;; (not (memq 'backtrace (debug-options-interface)))) | |
2488 | ;; (begin | |
2489 | ;; (display | |
2490 | ;;"Type \"(debug-enable 'backtrace)\" if you would like a backtrace | |
2491 | ;;automatically if an error occurs in the future.\n") | |
2492 | ;; (set! has-shown-backtrace-hint? #t)))) | |
2493 | ;; (display "No backtrace available.\n"))) | |
21ed9efe | 2494 | |
0f2d19dd JB |
2495 | (define (error-catching-repl r e p) |
2496 | (error-catching-loop (lambda () (p (e (r)))))) | |
2497 | ||
2498 | (define (gc-run-time) | |
2499 | (cdr (assq 'gc-time-taken (gc-stats)))) | |
2500 | ||
3e3cec45 MD |
2501 | (define before-read-hook (make-hook)) |
2502 | (define after-read-hook (make-hook)) | |
1c6cd8e8 | 2503 | |
dc5c2038 MD |
2504 | ;;; The default repl-reader function. We may override this if we've |
2505 | ;;; the readline library. | |
2506 | (define repl-reader | |
2507 | (lambda (prompt) | |
2508 | (display prompt) | |
2509 | (force-output) | |
04efd24d | 2510 | (run-hook before-read-hook) |
dc5c2038 MD |
2511 | (read (current-input-port)))) |
2512 | ||
0f2d19dd JB |
2513 | (define (scm-style-repl) |
2514 | (letrec ( | |
2515 | (start-gc-rt #f) | |
2516 | (start-rt #f) | |
0f2d19dd JB |
2517 | (repl-report-start-timing (lambda () |
2518 | (set! start-gc-rt (gc-run-time)) | |
2519 | (set! start-rt (get-internal-run-time)))) | |
2520 | (repl-report (lambda () | |
2521 | (display ";;; ") | |
2522 | (display (inexact->exact | |
2523 | (* 1000 (/ (- (get-internal-run-time) start-rt) | |
2524 | internal-time-units-per-second)))) | |
2525 | (display " msec (") | |
2526 | (display (inexact->exact | |
2527 | (* 1000 (/ (- (gc-run-time) start-gc-rt) | |
2528 | internal-time-units-per-second)))) | |
2529 | (display " msec in gc)\n"))) | |
480977d0 JB |
2530 | |
2531 | (consume-trailing-whitespace | |
2532 | (lambda () | |
2533 | (let ((ch (peek-char))) | |
2534 | (cond | |
2535 | ((eof-object? ch)) | |
2536 | ((or (char=? ch #\space) (char=? ch #\tab)) | |
2537 | (read-char) | |
2538 | (consume-trailing-whitespace)) | |
2539 | ((char=? ch #\newline) | |
2540 | (read-char)))))) | |
0f2d19dd | 2541 | (-read (lambda () |
dc5c2038 MD |
2542 | (let ((val |
2543 | (let ((prompt (cond ((string? scm-repl-prompt) | |
2544 | scm-repl-prompt) | |
2545 | ((thunk? scm-repl-prompt) | |
2546 | (scm-repl-prompt)) | |
2547 | (scm-repl-prompt "> ") | |
2548 | (else "")))) | |
2549 | (repl-reader prompt)))) | |
2550 | ||
480977d0 | 2551 | ;; As described in R4RS, the READ procedure updates the |
e13c54c4 | 2552 | ;; port to point to the first character past the end of |
480977d0 JB |
2553 | ;; the external representation of the object. This |
2554 | ;; means that it doesn't consume the newline typically | |
2555 | ;; found after an expression. This means that, when | |
2556 | ;; debugging Guile with GDB, GDB gets the newline, which | |
2557 | ;; it often interprets as a "continue" command, making | |
2558 | ;; breakpoints kind of useless. So, consume any | |
2559 | ;; trailing newline here, as well as any whitespace | |
2560 | ;; before it. | |
e13c54c4 JB |
2561 | ;; But not if EOF, for control-D. |
2562 | (if (not (eof-object? val)) | |
2563 | (consume-trailing-whitespace)) | |
04efd24d | 2564 | (run-hook after-read-hook) |
0f2d19dd JB |
2565 | (if (eof-object? val) |
2566 | (begin | |
7950df7c | 2567 | (repl-report-start-timing) |
0f2d19dd JB |
2568 | (if scm-repl-verbose |
2569 | (begin | |
2570 | (newline) | |
2571 | (display ";;; EOF -- quitting") | |
2572 | (newline))) | |
2573 | (quit 0))) | |
2574 | val))) | |
2575 | ||
2576 | (-eval (lambda (sourc) | |
2577 | (repl-report-start-timing) | |
d7faeb2e MD |
2578 | (start-stack 'repl-stack |
2579 | (eval sourc (interaction-environment))))) | |
0f2d19dd | 2580 | |
44484f52 MD |
2581 | (-print (let ((maybe-print (lambda (result) |
2582 | (if (or scm-repl-print-unspecified | |
2583 | (not (unspecified? result))) | |
2584 | (begin | |
2585 | (write result) | |
2586 | (newline)))))) | |
2587 | (lambda (result) | |
2588 | (if (not scm-repl-silent) | |
2589 | (begin | |
2590 | (if (values? result) | |
2591 | (for-each maybe-print (get-values result)) | |
2592 | (maybe-print result)) | |
2593 | (if scm-repl-verbose | |
2594 | (repl-report)) | |
2595 | (force-output)))))) | |
0f2d19dd | 2596 | |
8e44e7a0 | 2597 | (-quit (lambda (args) |
0f2d19dd JB |
2598 | (if scm-repl-verbose |
2599 | (begin | |
2600 | (display ";;; QUIT executed, repl exitting") | |
2601 | (newline) | |
2602 | (repl-report))) | |
8e44e7a0 | 2603 | args)) |
0f2d19dd JB |
2604 | |
2605 | (-abort (lambda () | |
2606 | (if scm-repl-verbose | |
2607 | (begin | |
2608 | (display ";;; ABORT executed.") | |
2609 | (newline) | |
2610 | (repl-report))) | |
2611 | (repl -read -eval -print)))) | |
2612 | ||
8e44e7a0 GH |
2613 | (let ((status (error-catching-repl -read |
2614 | -eval | |
2615 | -print))) | |
2616 | (-quit status)))) | |
2617 | ||
0f2d19dd | 2618 | |
0f2d19dd | 2619 | \f |
44cf1f0f | 2620 | ;;; {IOTA functions: generating lists of numbers} |
0f2d19dd | 2621 | |
e69cd299 MD |
2622 | (define (iota n) |
2623 | (let loop ((count (1- n)) (result '())) | |
2624 | (if (< count 0) result | |
2625 | (loop (1- count) (cons count result))))) | |
0f2d19dd JB |
2626 | |
2627 | \f | |
2628 | ;;; {While} | |
2629 | ;;; | |
2630 | ;;; with `continue' and `break'. | |
2631 | ;;; | |
2632 | ||
2633 | (defmacro while (cond . body) | |
2634 | `(letrec ((continue (lambda () (or (not ,cond) (begin (begin ,@ body) (continue))))) | |
2635 | (break (lambda val (apply throw 'break val)))) | |
2636 | (catch 'break | |
2637 | (lambda () (continue)) | |
2638 | (lambda v (cadr v))))) | |
2639 | ||
7398c2c2 MD |
2640 | ;;; {collect} |
2641 | ;;; | |
2642 | ;;; Similar to `begin' but returns a list of the results of all constituent | |
2643 | ;;; forms instead of the result of the last form. | |
2644 | ;;; (The definition relies on the current left-to-right | |
2645 | ;;; order of evaluation of operands in applications.) | |
2646 | ||
2647 | (defmacro collect forms | |
2648 | (cons 'list forms)) | |
0f2d19dd | 2649 | |
8a6a8671 MV |
2650 | ;;; {with-fluids} |
2651 | ||
2652 | ;; with-fluids is a convenience wrapper for the builtin procedure | |
2653 | ;; `with-fluids*'. The syntax is just like `let': | |
2654 | ;; | |
2655 | ;; (with-fluids ((fluid val) | |
2656 | ;; ...) | |
2657 | ;; body) | |
2658 | ||
2659 | (defmacro with-fluids (bindings . body) | |
2660 | `(with-fluids* (list ,@(map car bindings)) (list ,@(map cadr bindings)) | |
2661 | (lambda () ,@body))) | |
2662 | ||
0f2d19dd JB |
2663 | \f |
2664 | ||
2665 | ;;; {Macros} | |
2666 | ;;; | |
2667 | ||
2668 | ;; actually....hobbit might be able to hack these with a little | |
2669 | ;; coaxing | |
2670 | ;; | |
2671 | ||
2672 | (defmacro define-macro (first . rest) | |
2673 | (let ((name (if (symbol? first) first (car first))) | |
2674 | (transformer | |
2675 | (if (symbol? first) | |
2676 | (car rest) | |
2677 | `(lambda ,(cdr first) ,@rest)))) | |
2678 | `(define ,name (defmacro:transformer ,transformer)))) | |
2679 | ||
2680 | ||
2681 | (defmacro define-syntax-macro (first . rest) | |
2682 | (let ((name (if (symbol? first) first (car first))) | |
2683 | (transformer | |
2684 | (if (symbol? first) | |
2685 | (car rest) | |
2686 | `(lambda ,(cdr first) ,@rest)))) | |
2687 | `(define ,name (defmacro:syntax-transformer ,transformer)))) | |
2688 | \f | |
2689 | ;;; {Module System Macros} | |
2690 | ;;; | |
2691 | ||
2692 | (defmacro define-module args | |
2693 | `(let* ((process-define-module process-define-module) | |
2694 | (set-current-module set-current-module) | |
2695 | (module (process-define-module ',args))) | |
2696 | (set-current-module module) | |
2697 | module)) | |
2698 | ||
89da9036 MV |
2699 | ;; the guts of the use-modules macro. add the interfaces of the named |
2700 | ;; modules to the use-list of the current module, in order | |
2701 | (define (process-use-modules module-names) | |
2702 | (for-each (lambda (module-name) | |
2703 | (let ((mod-iface (resolve-interface module-name))) | |
2704 | (or mod-iface | |
2705 | (error "no such module" module-name)) | |
2706 | (module-use! (current-module) mod-iface))) | |
2707 | (reverse module-names))) | |
2708 | ||
33cf699f | 2709 | (defmacro use-modules modules |
89da9036 | 2710 | `(process-use-modules ',modules)) |
33cf699f | 2711 | |
cf266109 | 2712 | (defmacro use-syntax (spec) |
7cbaee0c MD |
2713 | `(begin |
2714 | ,@(if (pair? spec) | |
2715 | `((process-use-modules ',(list spec)) | |
2716 | (set-module-transformer! (current-module) | |
2717 | ,(car (last-pair spec)))) | |
2718 | `((set-module-transformer! (current-module) ,spec))) | |
bd0fb3cf | 2719 | (fluid-set! scm:eval-transformer (module-transformer (current-module))))) |
7a0ff2f8 | 2720 | |
0f2d19dd JB |
2721 | (define define-private define) |
2722 | ||
2723 | (defmacro define-public args | |
2724 | (define (syntax) | |
2725 | (error "bad syntax" (list 'define-public args))) | |
2726 | (define (defined-name n) | |
2727 | (cond | |
3c5af9ef JB |
2728 | ((symbol? n) n) |
2729 | ((pair? n) (defined-name (car n))) | |
2730 | (else (syntax)))) | |
0f2d19dd | 2731 | (cond |
3c5af9ef JB |
2732 | ((null? args) (syntax)) |
2733 | ||
2734 | (#t (let ((name (defined-name (car args)))) | |
2735 | `(begin | |
2736 | (let ((public-i (module-public-interface (current-module)))) | |
2737 | ;; Make sure there is a local variable: | |
2738 | ;; | |
2739 | (module-define! (current-module) | |
2740 | ',name | |
2741 | (module-ref (current-module) ',name #f)) | |
0f2d19dd | 2742 | |
3c5af9ef JB |
2743 | ;; Make sure that local is exported: |
2744 | ;; | |
2745 | (module-add! public-i ',name | |
2746 | (module-variable (current-module) ',name))) | |
0f2d19dd | 2747 | |
3c5af9ef JB |
2748 | ;; Now (re)define the var normally. Bernard URBAN |
2749 | ;; suggests we use eval here to accomodate Hobbit; it lets | |
2750 | ;; the interpreter handle the define-private form, which | |
2751 | ;; Hobbit can't digest. | |
d7faeb2e | 2752 | (eval '(define-private ,@ args) (interaction-environment))))))) |
0f2d19dd JB |
2753 | |
2754 | ||
2755 | ||
2756 | (defmacro defmacro-public args | |
2757 | (define (syntax) | |
2758 | (error "bad syntax" (list 'defmacro-public args))) | |
2759 | (define (defined-name n) | |
2760 | (cond | |
2761 | ((symbol? n) n) | |
2762 | (else (syntax)))) | |
2763 | (cond | |
2764 | ((null? args) (syntax)) | |
2765 | ||
2766 | (#t (let ((name (defined-name (car args)))) | |
2767 | `(begin | |
2768 | (let ((public-i (module-public-interface (current-module)))) | |
2769 | ;; Make sure there is a local variable: | |
2770 | ;; | |
2771 | (module-define! (current-module) | |
2772 | ',name | |
2773 | (module-ref (current-module) ',name #f)) | |
2774 | ||
2775 | ;; Make sure that local is exported: | |
2776 | ;; | |
2777 | (module-add! public-i ',name (module-variable (current-module) ',name))) | |
2778 | ||
2779 | ;; Now (re)define the var normally. | |
2780 | ;; | |
2781 | (defmacro ,@ args)))))) | |
2782 | ||
2783 | ||
90847923 MD |
2784 | (define (module-export! m names) |
2785 | (let ((public-i (module-public-interface m))) | |
2786 | (for-each (lambda (name) | |
2787 | ;; Make sure there is a local variable: | |
2788 | (module-define! m name (module-ref m name #f)) | |
2789 | ;; Make sure that local is exported: | |
2790 | (module-add! public-i name (module-variable m name))) | |
2791 | names))) | |
2792 | ||
a0cc0a01 | 2793 | (defmacro export names |
90847923 | 2794 | `(module-export! (current-module) ',names)) |
a0cc0a01 MD |
2795 | |
2796 | (define export-syntax export) | |
2797 | ||
2798 | ||
0f2d19dd JB |
2799 | (define load load-module) |
2800 | ||
2801 | ||
2802 | \f | |
9aca88c3 JB |
2803 | ;;; {Load emacs interface support if emacs option is given.} |
2804 | ||
2805 | (define (load-emacs-interface) | |
2806 | (if (memq 'debug-extensions *features*) | |
2807 | (debug-enable 'backtrace)) | |
2808 | (define-module (guile-user) :use-module (ice-9 emacs))) | |
2809 | ||
2810 | \f | |
0f2d19dd | 2811 | |
755457ec MD |
2812 | (define using-readline? |
2813 | (let ((using-readline? (make-fluid))) | |
2814 | (make-procedure-with-setter | |
2815 | (lambda () (fluid-ref using-readline?)) | |
2816 | (lambda (v) (fluid-set! using-readline? v))))) | |
2817 | ||
e1a191a8 GH |
2818 | ;; this is just (scm-style-repl) with a wrapper to install and remove |
2819 | ;; signal handlers. | |
8e44e7a0 | 2820 | (define (top-repl) |
9aca88c3 JB |
2821 | |
2822 | ;; Load emacs interface support if emacs option is given. | |
2823 | (if (and (module-defined? the-root-module 'use-emacs-interface) | |
2824 | use-emacs-interface) | |
2825 | (load-emacs-interface)) | |
2826 | ||
4fdf8b2c | 2827 | ;; Place the user in the guile-user module. |
00b33968 | 2828 | (define-module (guile-user) |
6b64c19b | 2829 | :use-module (guile) ;so that bindings will be checked here first |
00b33968 MD |
2830 | :use-module (ice-9 session) |
2831 | :use-module (ice-9 debug) | |
6b64c19b | 2832 | :autoload (ice-9 debugger) (debug)) ;load debugger on demand |
00b33968 MD |
2833 | (if (memq 'threads *features*) |
2834 | (define-module (guile-user) :use-module (ice-9 threads))) | |
2835 | (if (memq 'regex *features*) | |
2836 | (define-module (guile-user) :use-module (ice-9 regex))) | |
4fdf8b2c | 2837 | |
e1a191a8 | 2838 | (let ((old-handlers #f) |
52cfc69b GH |
2839 | (signals (if (provided? 'posix) |
2840 | `((,SIGINT . "User interrupt") | |
2841 | (,SIGFPE . "Arithmetic error") | |
2842 | (,SIGBUS . "Bad memory access (bus error)") | |
2843 | (,SIGSEGV . | |
2844 | "Bad memory access (Segmentation violation)")) | |
2845 | '()))) | |
e1a191a8 GH |
2846 | |
2847 | (dynamic-wind | |
2848 | ||
2849 | ;; call at entry | |
2850 | (lambda () | |
2851 | (let ((make-handler (lambda (msg) | |
2852 | (lambda (sig) | |
8087b6be MD |
2853 | ;; Make a backup copy of the stack |
2854 | (fluid-set! before-signal-stack | |
2855 | (fluid-ref the-last-stack)) | |
096d5f90 | 2856 | (save-stack %deliver-signals) |
e1a191a8 GH |
2857 | (scm-error 'signal |
2858 | #f | |
2859 | msg | |
2860 | #f | |
2861 | (list sig)))))) | |
2862 | (set! old-handlers | |
2863 | (map (lambda (sig-msg) | |
2864 | (sigaction (car sig-msg) | |
2865 | (make-handler (cdr sig-msg)))) | |
2866 | signals)))) | |
2867 | ||
2868 | ;; the protected thunk. | |
2869 | (lambda () | |
2055a1bc | 2870 | (let ((status (scm-style-repl))) |
04efd24d | 2871 | (run-hook exit-hook) |
2055a1bc | 2872 | status)) |
e1a191a8 GH |
2873 | |
2874 | ;; call at exit. | |
2875 | (lambda () | |
2876 | (map (lambda (sig-msg old-handler) | |
2877 | (if (not (car old-handler)) | |
2878 | ;; restore original C handler. | |
2879 | (sigaction (car sig-msg) #f) | |
2880 | ;; restore Scheme handler, SIG_IGN or SIG_DFL. | |
2881 | (sigaction (car sig-msg) | |
2882 | (car old-handler) | |
2883 | (cdr old-handler)))) | |
bd9e24b3 | 2884 | signals old-handlers))))) |
0f2d19dd | 2885 | |
02b754d3 GH |
2886 | (defmacro false-if-exception (expr) |
2887 | `(catch #t (lambda () ,expr) | |
2888 | (lambda args #f))) | |
2889 | ||
2055a1bc MD |
2890 | ;;; This hook is run at the very end of an interactive session. |
2891 | ;;; | |
3e3cec45 | 2892 | (define exit-hook (make-hook)) |
2055a1bc | 2893 | |
4d31f0da JB |
2894 | \f |
2895 | (define-module (guile)) | |
9946dd45 | 2896 | |
13e341bb | 2897 | (append! %load-path (cons "." ())) |