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