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