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