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