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