|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(eval-when (compile) |
|
(set-current-module (resolve-module '(guile)))) |
|
|
|
|
|
|
|
(if (current-module) |
|
(error "re-loading ice-9/boot-9.scm not allowed")) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define apply |
|
(case-lambda |
|
((fun args) |
|
((@@ primitive apply) fun args)) |
|
((fun arg1 . args) |
|
(letrec ((append* (lambda (tail) |
|
(let ((tail (car tail)) |
|
(tail* (cdr tail))) |
|
(if (null? tail*) |
|
tail |
|
(cons tail (append* tail*))))))) |
|
(apply fun (cons arg1 (append* args))))))) |
|
(define (call-with-current-continuation proc) |
|
((@@ primitive call-with-current-continuation) proc)) |
|
(define (call-with-values producer consumer) |
|
((@@ primitive call-with-values) producer consumer)) |
|
(define (dynamic-wind in thunk out) |
|
"All three arguments must be 0-argument procedures. |
|
Guard @var{in} is called, then @var{thunk}, then |
|
guard @var{out}. |
|
|
|
If, any time during the execution of @var{thunk}, the |
|
continuation of the @code{dynamic_wind} expression is escaped |
|
non-locally, @var{out} is called. If the continuation of |
|
the dynamic-wind is re-entered, @var{in} is called. Thus |
|
@var{in} and @var{out} may be called any number of |
|
times. |
|
@lisp |
|
(define x 'normal-binding) |
|
@result{} x |
|
(define a-cont |
|
(call-with-current-continuation |
|
(lambda (escape) |
|
(let ((old-x x)) |
|
(dynamic-wind |
|
;; in-guard: |
|
;; |
|
(lambda () (set! x 'special-binding)) |
|
|
|
;; thunk |
|
;; |
|
(lambda () (display x) (newline) |
|
(call-with-current-continuation escape) |
|
(display x) (newline) |
|
x) |
|
|
|
;; out-guard: |
|
;; |
|
(lambda () (set! x old-x))))))) |
|
|
|
;; Prints: |
|
special-binding |
|
;; Evaluates to: |
|
@result{} a-cont |
|
x |
|
@result{} normal-binding |
|
(a-cont #f) |
|
;; Prints: |
|
special-binding |
|
;; Evaluates to: |
|
@result{} a-cont ;; the value of the (define a-cont...) |
|
x |
|
@result{} normal-binding |
|
a-cont |
|
@result{} special-binding |
|
@end lisp" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# |
|
(unless (thunk? out) |
|
(scm-error 'wrong-type-arg "dynamic-wind" "Not a thunk: ~S" |
|
(list out) #f)) |
|
(in) |
|
((@@ primitive wind) in out) |
|
(call-with-values thunk |
|
(lambda vals |
|
((@@ primitive unwind)) |
|
(out) |
|
(apply values vals)))) |
|
|
|
(define (with-fluid* fluid val thunk) |
|
"Set @var{fluid} to @var{value} temporarily, and call @var{thunk}. |
|
@var{thunk} must be a procedure of no arguments." |
|
((@@ primitive push-fluid) fluid val) |
|
(call-with-values thunk |
|
(lambda vals |
|
((@@ primitive pop-fluid)) |
|
(apply values vals)))) |
|
|
|
(define (with-dynamic-state state thunk) |
|
"Call @var{proc} while @var{state} is the current dynamic state object. |
|
@var{thunk} must be a procedure of no arguments." |
|
((@@ primitive push-dynamic-state) state) |
|
(call-with-values thunk |
|
(lambda vals |
|
((@@ primitive pop-dynamic-state)) |
|
(apply values vals)))) |
|
|
|
|
|
|
|
|
|
|
|
|
|
(define (peek . stuff) |
|
"Write arguments to the current output port, and return the last argument. |
|
|
|
This is handy for tracing function calls, e.g.: |
|
|
|
(+ 10 (troublesome-fn)) |
|
=> (+ 10 (pk 'troublesome-fn-returned (troublesome-fn)))" |
|
(newline) |
|
(display ";;; ") |
|
(write stuff) |
|
(newline) |
|
(car (last-pair stuff))) |
|
|
|
(define pk peek) |
|
|
|
(define (warn . stuff) |
|
(newline (current-warning-port)) |
|
(display ";;; WARNING " (current-warning-port)) |
|
(display stuff (current-warning-port)) |
|
(newline (current-warning-port)) |
|
(car (last-pair stuff))) |
|
|
|
|
|
|
|
|
|
|
|
|
|
(define (provide sym) |
|
(if (not (memq sym *features*)) |
|
(set! *features* (cons sym *features*)))) |
|
|
|
|
|
|
|
|
|
(define (provided? feature) |
|
"Return #t iff FEATURE is available to this Guile interpreter." |
|
(and (memq feature *features*) #t)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
(define map |
|
(case-lambda |
|
((f l) |
|
(if (not (list? l)) |
|
(scm-error 'wrong-type-arg "map" "Not a list: ~S" |
|
(list l) #f)) |
|
(let map1 ((l l)) |
|
(if (pair? l) |
|
(cons (f (car l)) (map1 (cdr l))) |
|
'()))) |
|
|
|
((f l1 l2) |
|
(if (not (= (length l1) (length l2))) |
|
(scm-error 'wrong-type-arg "map" "List of wrong length: ~S" |
|
(list l2) #f)) |
|
|
|
(let map2 ((l1 l1) (l2 l2)) |
|
(if (pair? l1) |
|
(cons (f (car l1) (car l2)) |
|
(map2 (cdr l1) (cdr l2))) |
|
'()))) |
|
|
|
((f l1 . rest) |
|
(let ((len (length l1))) |
|
(let mapn ((rest rest)) |
|
(or (null? rest) |
|
(if (= (length (car rest)) len) |
|
(mapn (cdr rest)) |
|
(scm-error 'wrong-type-arg "map" "List of wrong length: ~S" |
|
(list (car rest)) #f))))) |
|
(let mapn ((l1 l1) (rest rest)) |
|
(if (pair? l1) |
|
(cons (apply f (car l1) (map car rest)) |
|
(mapn (cdr l1) (map cdr rest))) |
|
'()))))) |
|
|
|
(define map-in-order map) |
|
|
|
(define for-each |
|
(case-lambda |
|
((f l) |
|
(if (not (list? l)) |
|
(scm-error 'wrong-type-arg "for-each" "Not a list: ~S" (list l) #f)) |
|
(let for-each1 ((l l)) |
|
(if (not (null? l)) |
|
(begin |
|
(f (car l)) |
|
(for-each1 (cdr l)))))) |
|
|
|
((f l1 l2) |
|
(if (not (= (length l1) (length l2))) |
|
(scm-error 'wrong-type-arg "for-each" "List of wrong length: ~S" |
|
(list l2) #f)) |
|
(let for-each2 ((l1 l1) (l2 l2)) |
|
(if (not (null? l1)) |
|
(begin |
|
(f (car l1) (car l2)) |
|
(for-each2 (cdr l1) (cdr l2)))))) |
|
|
|
((f l1 . rest) |
|
(let ((len (length l1))) |
|
(let for-eachn ((rest rest)) |
|
(or (null? rest) |
|
(if (= (length (car rest)) len) |
|
(for-eachn (cdr rest)) |
|
(scm-error 'wrong-type-arg "for-each" "List of wrong length: ~S" |
|
(list (car rest)) #f))))) |
|
|
|
(let for-eachn ((l1 l1) (rest rest)) |
|
(if (pair? l1) |
|
(begin |
|
(apply f (car l1) (map car rest)) |
|
(for-eachn (cdr l1) (map cdr rest)))))))) |
|
|
|
|
|
|
|
|
|
(define (absolute-file-name? file-name) #t) |
|
(define (open-input-file str) (open-file str "r")) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define (and-map f lst) |
|
"Apply F to successive elements of LST until exhaustion or F returns #f. |
|
If returning early, return #f. Otherwise, return the last value returned |
|
by F. If F has never been called because LST is empty, return #t." |
|
(let loop ((result #t) |
|
(l lst)) |
|
(and result |
|
(or (and (null? l) |
|
result) |
|
(loop (f (car l)) (cdr l)))))) |
|
|
|
(define (or-map f lst) |
|
"Apply F to successive elements of LST until exhaustion or while F returns #f. |
|
If returning early, return the return value of F." |
|
(let loop ((result #f) |
|
(l lst)) |
|
(or result |
|
(and (not (null? l)) |
|
(loop (f (car l)) (cdr l)))))) |
|
|
|
|
|
|
|
|
|
|
|
(define format simple-format) |
|
|
|
|
|
|
|
(define string-any |
|
(lambda* (char_pred s #:optional (start 0) (end (string-length s))) |
|
(if (and (procedure? char_pred) |
|
(> end start) |
|
(<= end (string-length s))) |
|
(or (string-any-c-code char_pred s start (1- end)) |
|
(char_pred (string-ref s (1- end)))) |
|
(string-any-c-code char_pred s start end)))) |
|
|
|
|
|
|
|
(define string-every |
|
(lambda* (char_pred s #:optional (start 0) (end (string-length s))) |
|
(if (and (procedure? char_pred) |
|
(> end start) |
|
(<= end (string-length s))) |
|
(and (string-every-c-code char_pred s start (1- end)) |
|
(char_pred (string-ref s (1- end)))) |
|
(string-every-c-code char_pred s start end)))) |
|
|
|
(define (substring-fill! str start end fill) |
|
"A variant of string-fill! that we keep for compatibility." |
|
(string-fill! str fill start end)) |
|
|
|
|
|
|
|
|
|
|
|
(define (module-name x) |
|
'(guile)) |
|
(define (module-add! module sym var) |
|
(hashq-set! (%get-pre-modules-obarray) sym var)) |
|
(define (module-define! module sym val) |
|
(let ((v (hashq-ref (%get-pre-modules-obarray) sym))) |
|
(if v |
|
(variable-set! v val) |
|
(module-add! module sym (make-variable val))))) |
|
(define (module-ref module sym) |
|
(let ((v (module-variable module sym))) |
|
(if v (variable-ref v) (error "badness!" (pk module) (pk sym))))) |
|
(define module-generate-unique-id! |
|
(let ((next-id 0)) |
|
(lambda (m) |
|
(let ((i next-id)) |
|
(set! next-id (+ i 1)) |
|
i)))) |
|
(define module-gensym gensym) |
|
(define (resolve-module . args) |
|
#f) |
|
|
|
|
|
(define (read-syntax port) |
|
(let ((datum (read port))) |
|
(if (eof-object? datum) |
|
datum |
|
(datum->syntax #f datum)))) |
|
|
|
|
|
(define syntax-violation #f) |
|
(define datum->syntax #f) |
|
(define syntax->datum #f) |
|
(define identifier? #f) |
|
(define generate-temporaries #f) |
|
(define bound-identifier=? #f) |
|
(define free-identifier=? #f) |
|
|
|
|
|
|
|
(define $sc-dispatch #f) |
|
|
|
|
|
(primitive-load-path "ice-9/psyntax-pp") |
|
|
|
|
|
|
|
(define-syntax and |
|
(syntax-rules () |
|
((_) #t) |
|
((_ x) x) |
|
|
|
((_ x . y) (if x (and . y) #f)))) |
|
|
|
(define-syntax or |
|
(syntax-rules () |
|
((_) #f) |
|
((_ x) x) |
|
|
|
((_ x . y) (let ((t x)) (if t t (or . y)))))) |
|
|
|
(include-from-path "ice-9/quasisyntax") |
|
|
|
(define-syntax-rule (when test stmt stmt* ...) |
|
(if test (let () stmt stmt* ...))) |
|
|
|
(define-syntax-rule (unless test stmt stmt* ...) |
|
(if (not test) (let () stmt stmt* ...))) |
|
|
|
(define-syntax else |
|
(lambda (x) |
|
(syntax-violation 'else "bad use of 'else' syntactic keyword" x x))) |
|
|
|
(define-syntax => |
|
(lambda (x) |
|
(syntax-violation '=> "bad use of '=>' syntactic keyword" x x))) |
|
|
|
(define-syntax ... |
|
(lambda (x) |
|
(syntax-violation '... "bad use of '...' syntactic keyword" x x))) |
|
|
|
(define-syntax _ |
|
(lambda (x) |
|
(syntax-violation '_ "bad use of '_' syntactic keyword" x x))) |
|
|
|
(define-syntax cond |
|
(lambda (whole-expr) |
|
(define (fold f seed xs) |
|
(let loop ((xs xs) (seed seed)) |
|
(if (null? xs) seed |
|
(loop (cdr xs) (f (car xs) seed))))) |
|
(define (reverse-map f xs) |
|
(fold (lambda (x seed) (cons (f x) seed)) |
|
'() xs)) |
|
(syntax-case whole-expr () |
|
((_ clause clauses ...) |
|
#`(begin |
|
#,@(fold (lambda (clause-builder tail) |
|
(clause-builder tail)) |
|
#'() |
|
(reverse-map |
|
(lambda (clause) |
|
(define* (bad-clause #:optional (msg "invalid clause")) |
|
(syntax-violation 'cond msg whole-expr clause)) |
|
(syntax-case clause (=> else) |
|
((else e e* ...) |
|
(lambda (tail) |
|
(if (null? tail) |
|
#'((let () e e* ...)) |
|
(bad-clause "else must be the last clause")))) |
|
((else . _) (bad-clause)) |
|
((test => receiver) |
|
(lambda (tail) |
|
#`((let ((t test)) |
|
(if t |
|
(receiver t) |
|
#,@tail))))) |
|
((test => receiver ...) |
|
(bad-clause "wrong number of receiver expressions")) |
|
((generator guard => receiver) |
|
(lambda (tail) |
|
#`((call-with-values (lambda () generator) |
|
(lambda vals |
|
(if (apply guard vals) |
|
(apply receiver vals) |
|
#,@tail)))))) |
|
((generator guard => receiver ...) |
|
(bad-clause "wrong number of receiver expressions")) |
|
((test) |
|
(lambda (tail) |
|
#`((let ((t test)) |
|
(if t t #,@tail))))) |
|
((test e e* ...) |
|
(lambda (tail) |
|
#`((if test |
|
(let () e e* ...) |
|
#,@tail)))) |
|
(_ (bad-clause)))) |
|
#'(clause clauses ...)))))))) |
|
|
|
(define-syntax case |
|
(lambda (whole-expr) |
|
(define (fold f seed xs) |
|
(let loop ((xs xs) (seed seed)) |
|
(if (null? xs) seed |
|
(loop (cdr xs) (f (car xs) seed))))) |
|
(define (fold2 f a b xs) |
|
(let loop ((xs xs) (a a) (b b)) |
|
(if (null? xs) (values a b) |
|
(call-with-values |
|
(lambda () (f (car xs) a b)) |
|
(lambda (a b) |
|
(loop (cdr xs) a b)))))) |
|
(define (reverse-map-with-seed f seed xs) |
|
(fold2 (lambda (x ys seed) |
|
(call-with-values |
|
(lambda () (f x seed)) |
|
(lambda (y seed) |
|
(values (cons y ys) seed)))) |
|
'() seed xs)) |
|
(syntax-case whole-expr () |
|
((_ expr clause clauses ...) |
|
(with-syntax ((key #'key)) |
|
#`(let ((key expr)) |
|
#,@(fold |
|
(lambda (clause-builder tail) |
|
(clause-builder tail)) |
|
#'() |
|
(reverse-map-with-seed |
|
(lambda (clause seen) |
|
(define* (bad-clause #:optional (msg "invalid clause")) |
|
(syntax-violation 'case msg whole-expr clause)) |
|
(syntax-case clause () |
|
((test . rest) |
|
(with-syntax |
|
((clause-expr |
|
(syntax-case #'rest (=>) |
|
((=> receiver) #'(receiver key)) |
|
((=> receiver ...) |
|
(bad-clause |
|
"wrong number of receiver expressions")) |
|
((e e* ...) #'(let () e e* ...)) |
|
(_ (bad-clause))))) |
|
(syntax-case #'test (else) |
|
((datums ...) |
|
(let ((seen |
|
(fold |
|
(lambda (datum seen) |
|
(define (warn-datum type) |
|
((@ (system base message) |
|
warning) |
|
type |
|
(append (source-properties datum) |
|
(source-properties |
|
(syntax->datum #'test))) |
|
datum |
|
(syntax->datum clause) |
|
(syntax->datum whole-expr))) |
|
(when (memv datum seen) |
|
(warn-datum 'duplicate-case-datum)) |
|
(when (or (pair? datum) (array? datum)) |
|
(warn-datum 'bad-case-datum)) |
|
(cons datum seen)) |
|
seen |
|
(map syntax->datum #'(datums ...))))) |
|
(values (lambda (tail) |
|
#`((if (memv key '(datums ...)) |
|
clause-expr |
|
#,@tail))) |
|
seen))) |
|
(else (values (lambda (tail) |
|
(if (null? tail) |
|
#'(clause-expr) |
|
(bad-clause |
|
"else must be the last clause"))) |
|
seen)) |
|
(_ (bad-clause))))) |
|
(_ (bad-clause)))) |
|
'() #'(clause clauses ...))))))))) |
|
|
|
(define-syntax do |
|
(syntax-rules () |
|
((do ((var init step ...) ...) |
|
(test expr ...) |
|
command ...) |
|
(letrec |
|
((loop |
|
(lambda (var ...) |
|
(if test |
|
(begin |
|
(if #f #f) |
|
expr ...) |
|
(begin |
|
command |
|
... |
|
(loop (do "step" var step ...) |
|
...)))))) |
|
(loop init ...))) |
|
((do "step" x) |
|
x) |
|
((do "step" x y) |
|
y))) |
|
|
|
(define-syntax define-values |
|
(lambda (orig-form) |
|
(syntax-case orig-form () |
|
((_ () expr) |
|
|
|
(with-syntax (((dummy) (generate-temporaries '(dummy)))) |
|
#`(define dummy |
|
(call-with-values (lambda () expr) |
|
(lambda () #f))))) |
|
((_ (var) expr) |
|
(identifier? #'var) |
|
#`(define var |
|
(call-with-values (lambda () expr) |
|
(lambda (v) v)))) |
|
((_ (var0 ... varn) expr) |
|
(and-map identifier? #'(var0 ... varn)) |
|
|
|
(with-syntax (((dummy) (generate-temporaries '(dummy)))) |
|
#`(begin |
|
;; Avoid mutating the user-visible variables |
|
(define dummy |
|
(call-with-values (lambda () expr) |
|
(lambda (var0 ... varn) |
|
(list var0 ... varn)))) |
|
(define var0 |
|
(let ((v (car dummy))) |
|
(set! dummy (cdr dummy)) |
|
v)) |
|
... |
|
(define varn |
|
(let ((v (car dummy))) |
|
(set! dummy #f) ; blackhole dummy |
|
v))))) |
|
((_ var expr) |
|
(identifier? #'var) |
|
#'(define var |
|
(call-with-values (lambda () expr) |
|
list))) |
|
((_ (var0 ... . varn) expr) |
|
(and-map identifier? #'(var0 ... varn)) |
|
|
|
(with-syntax (((dummy) (generate-temporaries '(dummy)))) |
|
#`(begin |
|
;; Avoid mutating the user-visible variables |
|
(define dummy |
|
(call-with-values (lambda () expr) |
|
(lambda (var0 ... . varn) |
|
(list var0 ... varn)))) |
|
(define var0 |
|
(let ((v (car dummy))) |
|
(set! dummy (cdr dummy)) |
|
v)) |
|
... |
|
(define varn |
|
(let ((v (car dummy))) |
|
(set! dummy #f) ; blackhole dummy |
|
v)))))))) |
|
|
|
(define-syntax-rule (delay exp) |
|
(make-promise (lambda () exp))) |
|
|
|
(define-syntax with-fluids |
|
(lambda (stx) |
|
(define (emit-with-fluids bindings body) |
|
(syntax-case bindings () |
|
(() |
|
body) |
|
(((f v) . bindings) |
|
#`(with-fluid* f v |
|
(lambda () |
|
#,(emit-with-fluids #'bindings body)))))) |
|
(syntax-case stx () |
|
((_ ((fluid val) ...) exp exp* ...) |
|
(with-syntax (((fluid-tmp ...) (generate-temporaries #'(fluid ...))) |
|
((val-tmp ...) (generate-temporaries #'(val ...)))) |
|
#`(let ((fluid-tmp fluid) ...) |
|
(let ((val-tmp val) ...) |
|
#,(emit-with-fluids #'((fluid-tmp val-tmp) ...) |
|
#'(let () exp exp* ...))))))))) |
|
|
|
(define-syntax current-source-location |
|
(lambda (x) |
|
(syntax-case x () |
|
((_) |
|
(with-syntax ((s (datum->syntax x (syntax-source x)))) |
|
#''s))))) |
|
|
|
|
|
|
|
|
|
|
|
(define-syntax current-filename |
|
(lambda (x) |
|
"A macro that expands to the current filename: the filename that |
|
the (current-filename) form appears in. Expands to #f if this |
|
information is unavailable." |
|
(false-if-exception |
|
(canonicalize-path (assq-ref (syntax-source x) 'filename))))) |
|
|
|
(define-syntax-rule (define-once sym val) |
|
(define sym |
|
(if (module-locally-bound? (current-module) 'sym) sym val))) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define make-prompt-tag |
|
(lambda* (#:optional (stem "prompt")) |
|
|
|
|
|
(list stem))) |
|
|
|
(define default-prompt-tag |
|
|
|
(let ((%default-prompt-tag (make-prompt-tag))) |
|
(lambda () |
|
%default-prompt-tag))) |
|
|
|
(define (call-with-prompt tag thunk handler) |
|
((@@ primitive call-with-prompt) tag thunk handler)) |
|
(define (abort-to-prompt tag . args) |
|
(abort-to-prompt* tag args)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define-syntax define-macro |
|
(lambda (x) |
|
"Define a defmacro." |
|
(syntax-case x () |
|
((_ (macro . args) doc body1 body ...) |
|
(string? (syntax->datum #'doc)) |
|
#'(define-macro macro doc (lambda args body1 body ...))) |
|
((_ (macro . args) body ...) |
|
#'(define-macro macro #f (lambda args body ...))) |
|
((_ macro transformer) |
|
#'(define-macro macro #f transformer)) |
|
((_ macro doc transformer) |
|
(or (string? (syntax->datum #'doc)) |
|
(not (syntax->datum #'doc))) |
|
#'(define-syntax macro |
|
(lambda (y) |
|
doc |
|
#((macro-type . defmacro) |
|
(defmacro-args args)) |
|
(syntax-case y () |
|
((_ . args) |
|
(let ((v (syntax->datum #'args))) |
|
(datum->syntax y (apply transformer v))))))))))) |
|
|
|
(define-syntax defmacro |
|
(lambda (x) |
|
"Define a defmacro, with the old lispy defun syntax." |
|
(syntax-case x () |
|
((_ macro args doc body1 body ...) |
|
(string? (syntax->datum #'doc)) |
|
#'(define-macro macro doc (lambda args body1 body ...))) |
|
((_ macro args body ...) |
|
#'(define-macro macro #f (lambda args body ...)))))) |
|
|
|
(provide 'defmacro) |
|
|
|
|
|
|
|
|
|
|
|
|
|
(define-syntax begin-deprecated |
|
(lambda (x) |
|
(syntax-case x () |
|
((_ form form* ...) |
|
(if (include-deprecated-features) |
|
#'(begin form form* ...) |
|
#'(begin)))))) |
|
|
|
|
|
|
|
|
|
|
|
|
|
(define (identity x) x) |
|
|
|
(define (compose proc . rest) |
|
"Compose PROC with the procedures in REST, such that the last one in |
|
REST is applied first and PROC last, and return the resulting procedure. |
|
The given procedures must have compatible arity." |
|
(if (null? rest) |
|
proc |
|
(let ((g (apply compose rest))) |
|
(lambda args |
|
(call-with-values (lambda () (apply g args)) proc))))) |
|
|
|
(define (negate proc) |
|
"Return a procedure with the same arity as PROC that returns the `not' |
|
of PROC's result." |
|
(lambda args |
|
(not (apply proc args)))) |
|
|
|
(define (const value) |
|
"Return a procedure that accepts any number of arguments and returns |
|
VALUE." |
|
(lambda _ |
|
value)) |
|
|
|
(define (and=> value procedure) |
|
"When VALUE is #f, return #f. Otherwise, return (PROC VALUE)." |
|
(and value (procedure value))) |
|
|
|
(define call/cc call-with-current-continuation) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define (make-object-property) |
|
|
|
(let ((prop (make-weak-key-hash-table))) |
|
(make-procedure-with-setter |
|
(lambda (obj) (hashq-ref prop obj)) |
|
(lambda (obj val) (hashq-set! prop obj val))))) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define (symbol-property sym prop) |
|
(let ((pair (assoc prop (symbol-pref sym)))) |
|
(and pair (cdr pair)))) |
|
|
|
(define (set-symbol-property! sym prop val) |
|
(let ((pair (assoc prop (symbol-pref sym)))) |
|
(if pair |
|
(set-cdr! pair val) |
|
(symbol-pset! sym (acons prop val (symbol-pref sym)))))) |
|
|
|
(define (symbol-property-remove! sym prop) |
|
(let ((pair (assoc prop (symbol-pref sym)))) |
|
(if pair |
|
(symbol-pset! sym (delq! pair (symbol-pref sym)))))) |
|
|
|
|
|
|
|
|
|
|
|
|
|
(define (array-shape array) |
|
"Return a list as long as the rank of @var{array}, where each element |
|
is a two-element list containing the lower and upper bounds of the |
|
corresponding dimension. |
|
|
|
@lisp |
|
(array-dimensions (make-array 'foo '(-1 3) 5)) @result{} ((-1 3) (0 5)) |
|
@end lisp |
|
|
|
See also: @code{array-dimensions}, @code{array-rank}." |
|
|
|
(map (lambda (ind) (if (number? ind) (list 0 (+ -1 ind)) ind)) |
|
(array-dimensions array))) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define (kw-arg-ref args kw) |
|
(let ((rem (member kw args))) |
|
(and rem (pair? (cdr rem)) (cadr rem)))) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define* (iota count #:optional (start 0) (step 1)) |
|
(unless (and (integer? count) (>= count 0)) |
|
(throw 'wrong-type-arg count)) |
|
(let loop ((n (- count 1)) (result '())) |
|
(if (negative? n) |
|
result |
|
(loop (- n 1) (cons (+ start (* n step)) result))))) |
|
|
|
|
|
|
|
|
|
|
|
|
|
(define (struct-layout s) |
|
(struct-ref (struct-vtable s) vtable-index-layout)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define record-type-vtable |
|
(let ((s (make-vtable (string-append standard-vtable-fields |
|
"pwpwpwpwpwpw") |
|
(lambda (s p) |
|
(display "#<record-type " p) |
|
(display (record-type-name s) p) |
|
(display ">" p))))) |
|
(set-struct-vtable-name! s 'record-type) |
|
s)) |
|
|
|
(define (record-type? obj) |
|
(and (struct? obj) (eq? record-type-vtable (struct-vtable obj)))) |
|
|
|
(define (record-type-name rtd) |
|
(unless (record-type? rtd) |
|
(error 'not-a-record-type rtd)) |
|
(struct-ref rtd vtable-offset-user)) |
|
|
|
(define (record-type-fields rtd) |
|
(unless (record-type? rtd) |
|
(error 'not-a-record-type rtd)) |
|
(struct-ref rtd (+ 1 vtable-offset-user))) |
|
|
|
(define (record-type-constructor rtd) |
|
(unless (record-type? rtd) |
|
(error 'not-a-record-type rtd)) |
|
(struct-ref rtd (+ 2 vtable-offset-user))) |
|
|
|
(define (record-type-properties rtd) |
|
(unless (record-type? rtd) |
|
(error 'not-a-record-type rtd)) |
|
(struct-ref rtd (+ 3 vtable-offset-user))) |
|
|
|
(define (record-type-extensible? rtd) |
|
(assq-ref (record-type-properties rtd) 'extensible?)) |
|
(define (record-type-opaque? rtd) |
|
(assq-ref (record-type-properties rtd) 'opaque?)) |
|
(define (record-type-uid rtd) |
|
(assq-ref (record-type-properties rtd) 'uid)) |
|
|
|
(define (record-type-parents rtd) |
|
(unless (record-type? rtd) |
|
(error 'not-a-record-type rtd)) |
|
(struct-ref rtd (+ 4 vtable-offset-user))) |
|
(define (record-type-parent rtd) |
|
(let* ((parents (record-type-parents rtd)) |
|
(nparents (vector-length parents))) |
|
(and (not (zero? nparents)) |
|
(vector-ref parents (1- nparents))))) |
|
(define (record-type-has-parent? rtd parent) |
|
(or (eq? rtd parent) |
|
(let ((parents (record-type-parents rtd)) |
|
(nparents (vector-length (record-type-parents parent)))) |
|
(and (< nparents (vector-length parents)) |
|
(eq? (vector-ref parents nparents) parent))))) |
|
|
|
(define (record-type-mutable-fields rtd) |
|
(unless (record-type? rtd) |
|
(error 'not-a-record-type rtd)) |
|
(struct-ref rtd (+ 5 vtable-offset-user))) |
|
|
|
(define prefab-record-types |
|
(make-hash-table)) |
|
|
|
(define* (make-record-type type-name fields #:optional printer #:key |
|
parent uid extensible? allow-duplicate-field-names? |
|
(opaque? (and=> parent record-type-opaque?))) |
|
|
|
(define-syntax make-constructor |
|
(lambda (x) |
|
(define *max-static-argument-count* 20) |
|
(define (make-formals n) |
|
(let lp ((i 0)) |
|
(if (< i n) |
|
(cons (datum->syntax |
|
x |
|
(string->symbol |
|
(string (integer->char (+ (char->integer #\a) i))))) |
|
(lp (1+ i))) |
|
'()))) |
|
(syntax-case x () |
|
((_ rtd exp) (not (identifier? #'exp)) |
|
#'(let ((n exp)) |
|
(make-constructor rtd n))) |
|
((_ rtd nfields) |
|
#`(case nfields |
|
#,@(let lp ((n 0)) |
|
(if (< n *max-static-argument-count*) |
|
(cons (with-syntax (((formal ...) (make-formals n)) |
|
(n n)) |
|
#'((n) |
|
(lambda (formal ...) |
|
(make-struct/simple rtd formal ...)))) |
|
(lp (1+ n))) |
|
'())) |
|
(else |
|
(lambda args |
|
(if (= (length args) nfields) |
|
(apply make-struct/no-tail rtd args) |
|
(scm-error 'wrong-number-of-args |
|
(format #f "make-~a" type-name) |
|
"Wrong number of arguments" '() #f))))))))) |
|
|
|
(define (default-record-printer s p) |
|
(display "#<" p) |
|
(display (record-type-name (record-type-descriptor s)) p) |
|
(let loop ((fields (record-type-fields (record-type-descriptor s))) |
|
(off 0)) |
|
(cond |
|
((not (null? fields)) |
|
(display " " p) |
|
(display (car fields) p) |
|
(display ": " p) |
|
(write (struct-ref s off) p) |
|
(loop (cdr fields) (+ 1 off))))) |
|
(display ">" p)) |
|
|
|
(define parents |
|
(cond |
|
((record-type? parent) |
|
(unless (record-type-extensible? parent) |
|
(error "parent type is final")) |
|
(when (and (record-type-opaque? parent) (not opaque?)) |
|
(error "can't make non-opaque subtype of opaque type")) |
|
(let* ((parent-parents (record-type-parents parent)) |
|
(parent-nparents (vector-length parent-parents)) |
|
(parents (make-vector (1+ parent-nparents)))) |
|
(vector-move-left! parent-parents 0 parent-nparents parents 0) |
|
(vector-set! parents parent-nparents parent) |
|
parents)) |
|
(parent |
|
(error "expected parent to be a record type" parent)) |
|
(else |
|
#()))) |
|
|
|
(define (check-fields fields) |
|
(unless (null? fields) |
|
(let ((field (car fields)) |
|
(fields (cdr fields))) |
|
(unless (symbol? field) |
|
(error "expected field to be a symbol" field)) |
|
(when (and (not allow-duplicate-field-names?) (memq field fields)) |
|
(error "duplicate field" field)) |
|
(check-fields fields)))) |
|
|
|
(define (append-fields head tail) |
|
(if (null? head) |
|
tail |
|
(let ((field (car head)) |
|
(tail (append-fields (cdr head) tail))) |
|
(when (and (not allow-duplicate-field-names?) (memq field tail)) |
|
(error "duplicate field" field)) |
|
(cons field tail)))) |
|
|
|
(define computed-fields |
|
(let ((fields (map (lambda (field) |
|
(cond |
|
((symbol? field) field) |
|
(else |
|
(unless (and (pair? field) |
|
(memq (car field) '(mutable immutable)) |
|
(pair? (cdr field)) |
|
(null? (cddr field))) |
|
(error "bad field declaration" field)) |
|
(cadr field)))) |
|
fields))) |
|
(check-fields fields) |
|
(if parent |
|
(append-fields (record-type-fields parent) fields) |
|
fields))) |
|
|
|
(define mutable-fields |
|
(let lp ((fields fields) |
|
(i (if parent (length (record-type-fields parent)) 0)) |
|
(mutable (if parent (record-type-mutable-fields parent) 0))) |
|
(if (null? fields) |
|
mutable |
|
(let ((field (car fields))) |
|
(lp (cdr fields) |
|
(1+ i) |
|
(if (or (not (pair? field)) |
|
(eq? (car field) 'mutable)) |
|
(logior mutable (ash 1 i)) |
|
mutable)))))) |
|
|
|
(define name-sym |
|
(cond |
|
((symbol? type-name) type-name) |
|
((string? type-name) |
|
(issue-deprecation-warning |
|
"Passing a string as a type-name to make-record-type is deprecated." |
|
" Pass a symbol instead.") |
|
(string->symbol type-name)) |
|
(else |
|
(error "expected a symbol for record type name" type-name)))) |
|
|
|
(define properties |
|
(let ((maybe-acons (lambda (k v tail) |
|
(if v (acons k v tail) tail)))) |
|
(maybe-acons 'extensible? extensible? |
|
(maybe-acons 'opaque? opaque? |
|
(maybe-acons 'uid uid |
|
'()))))) |
|
|
|
(cond |
|
((and uid (hashq-ref prefab-record-types uid)) |
|
=> (lambda (rtd) |
|
(unless (and (equal? (record-type-name rtd) name-sym) |
|
(equal? (record-type-fields rtd) computed-fields) |
|
(not printer) |
|
(equal? (record-type-properties rtd) properties) |
|
(equal? (record-type-parents rtd) parents) |
|
(equal? (record-type-mutable-fields rtd) mutable-fields)) |
|
(error "prefab record type declaration incompatible with previous" |
|
rtd)) |
|
rtd)) |
|
(else |
|
(let ((rtd (make-struct/no-tail |
|
record-type-vtable |
|
(make-struct-layout |
|
(apply string-append |
|
(map (lambda (f) "pw") computed-fields))) |
|
(or printer default-record-printer) |
|
name-sym |
|
computed-fields |
|
#f |
|
properties |
|
parents |
|
mutable-fields))) |
|
|
|
(struct-set! rtd (+ vtable-offset-user 2) |
|
(make-constructor rtd (length computed-fields))) |
|
|
|
|
|
|
|
|
|
(set-struct-vtable-name! rtd name-sym) |
|
|
|
(when uid |
|
(unless (symbol? uid) |
|
(error "UID for prefab record type should be a symbol" uid)) |
|
(hashq-set! prefab-record-types uid rtd)) |
|
|
|
rtd)))) |
|
|
|
(define record-constructor |
|
(case-lambda |
|
((rtd) |
|
(record-type-constructor rtd)) |
|
((rtd field-names) |
|
(issue-deprecation-warning |
|
"Calling `record-constructor' with two arguments (the record type" |
|
" and a list of field names) is deprecated. Instead, call with just" |
|
" one argument, and provide a wrapper around that constructor if" |
|
" needed.") |
|
(primitive-eval |
|
`(lambda ,field-names |
|
(make-struct/no-tail ',rtd |
|
,@(map (lambda (f) |
|
(if (memq f field-names) |
|
f |
|
#f)) |
|
(record-type-fields rtd)))))))) |
|
|
|
(define (record-predicate rtd) |
|
(unless (record-type? rtd) |
|
(error 'not-a-record-type rtd)) |
|
(if (record-type-extensible? rtd) |
|
(let ((pos (vector-length (record-type-parents rtd)))) |
|
|
|
|
|
|
|
|
|
(lambda (obj) |
|
(and (struct? obj) |
|
(let* ((v (struct-vtable obj))) |
|
(or (eq? v rtd) |
|
(let ((parents (record-type-parents v))) |
|
(and (< pos (vector-length parents)) |
|
(eq? (vector-ref parents pos) rtd)))))))) |
|
(lambda (obj) (and (struct? obj) (eq? rtd (struct-vtable obj)))))) |
|
|
|
(define (record-accessor rtd field-name-or-idx) |
|
(define vtable-index-size 5) |
|
(define (record-nfields rtd) |
|
(struct-ref/unboxed rtd vtable-index-size)) |
|
(let ((type-name (record-type-name rtd)) |
|
(pos (cond |
|
((and (exact-integer? field-name-or-idx) |
|
(<= 0 field-name-or-idx (record-nfields rtd))) |
|
field-name-or-idx) |
|
((list-index (record-type-fields rtd) field-name-or-idx)) |
|
(else (error 'no-such-field field-name-or-idx)))) |
|
(pred (record-predicate rtd))) |
|
(lambda (obj) |
|
(unless (pred obj) |
|
(scm-error 'wrong-type-arg "record-accessor" |
|
"Wrong type argument (want `~S'): ~S" |
|
(list type-name obj) |
|
#f)) |
|
(struct-ref obj pos)))) |
|
|
|
(define (record-modifier rtd field-name-or-idx) |
|
(define vtable-index-size 5) |
|
(define (record-nfields rtd) |
|
(struct-ref/unboxed rtd vtable-index-size)) |
|
(let ((type-name (record-type-name rtd)) |
|
(pos (cond |
|
((and (exact-integer? field-name-or-idx) |
|
(<= 0 field-name-or-idx (record-nfields rtd))) |
|
field-name-or-idx) |
|
((list-index (record-type-fields rtd) field-name-or-idx)) |
|
(else (error 'no-such-field field-name-or-idx)))) |
|
(pred (record-predicate rtd))) |
|
(unless (logbit? pos (record-type-mutable-fields rtd)) |
|
(error "field is immutable" rtd field-name-or-idx)) |
|
(lambda (obj val) |
|
(unless (pred obj) |
|
(scm-error 'wrong-type-arg "record-modifier" |
|
"Wrong type argument (want `~S'): ~S" |
|
(list type-name obj) |
|
#f)) |
|
(struct-set! obj pos val)))) |
|
|
|
(define (record? obj) |
|
(and (struct? obj) (record-type? (struct-vtable obj)))) |
|
|
|
(define (record-type-descriptor obj) |
|
(if (record? obj) |
|
(struct-vtable obj) |
|
(error 'not-a-record obj))) |
|
|
|
(provide 'record) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define <parameter> |
|
|
|
(make-struct/no-tail <applicable-struct-vtable> 'pwpwpw)) |
|
(set-struct-vtable-name! <parameter> '<parameter>) |
|
|
|
(define* (make-parameter init #:optional (conv (lambda (x) x))) |
|
"Make a new parameter. |
|
|
|
A parameter is a dynamically bound value, accessed through a procedure. |
|
To access the current value, apply the procedure with no arguments: |
|
|
|
(define p (make-parameter 10)) |
|
(p) => 10 |
|
|
|
To provide a new value for the parameter in a dynamic extent, use |
|
`parameterize': |
|
|
|
(parameterize ((p 20)) |
|
(p)) => 20 |
|
(p) => 10 |
|
|
|
The value outside of the dynamic extent of the body is unaffected. To |
|
update the current value, apply it to one argument: |
|
|
|
(p 20) => 10 |
|
(p) => 20 |
|
|
|
As you can see, the call that updates a parameter returns its previous |
|
value. |
|
|
|
All values for the parameter are first run through the CONV procedure, |
|
including INIT, the initial value. The default CONV procedure is the |
|
identity procedure. CONV is commonly used to ensure some set of |
|
invariants on the values that a parameter may have." |
|
(let ((fluid (make-fluid (conv init)))) |
|
(make-struct/no-tail |
|
<parameter> |
|
(case-lambda |
|
(() (fluid-ref fluid)) |
|
((x) (let ((prev (fluid-ref fluid))) |
|
(fluid-set! fluid (conv x)) |
|
prev))) |
|
fluid conv))) |
|
|
|
(define (parameter? x) |
|
(and (struct? x) (eq? (struct-vtable x) <parameter>))) |
|
|
|
(define (parameter-fluid p) |
|
(if (parameter? p) |
|
(struct-ref p 1) |
|
(scm-error 'wrong-type-arg "parameter-fluid" |
|
"Not a parameter: ~S" (list p) #f))) |
|
|
|
(define (parameter-converter p) |
|
(if (parameter? p) |
|
(struct-ref p 2) |
|
(scm-error 'wrong-type-arg "parameter-fluid" |
|
"Not a parameter: ~S" (list p) #f))) |
|
|
|
(define-syntax parameterize |
|
(lambda (x) |
|
(syntax-case x () |
|
((_ ((param value) ...) body body* ...) |
|
(with-syntax (((p ...) (generate-temporaries #'(param ...)))) |
|
#'(let ((p param) ...) |
|
(if (not (parameter? p)) |
|
(scm-error 'wrong-type-arg "parameterize" |
|
"Not a parameter: ~S" (list p) #f)) |
|
... |
|
(with-fluids (((struct-ref p 1) ((struct-ref p 2) value)) |
|
...) |
|
body body* ...))))))) |
|
|
|
(define* (fluid->parameter fluid #:optional (conv (lambda (x) x))) |
|
"Make a parameter that wraps a fluid. |
|
|
|
The value of the parameter will be the same as the value of the fluid. |
|
If the parameter is rebound in some dynamic extent, perhaps via |
|
`parameterize', the new value will be run through the optional CONV |
|
procedure, as with any parameter. Note that unlike `make-parameter', |
|
CONV is not applied to the initial value." |
|
(make-struct/no-tail |
|
<parameter> |
|
(case-lambda |
|
(() (fluid-ref fluid)) |
|
((x) (let ((prev (fluid-ref fluid))) |
|
(fluid-set! fluid (conv x)) |
|
prev))) |
|
fluid conv)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(set! default-prompt-tag (make-parameter (default-prompt-tag))) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define current-language (make-parameter 'scheme)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define (call-with-output-string proc) |
|
"Calls the one-argument procedure @var{proc} with a newly created output |
|
port. When the function returns, the string composed of the characters |
|
written into the port is returned." |
|
(let ((port (open-output-string))) |
|
(proc port) |
|
(get-output-string port))) |
|
|
|
|
|
|
|
|
|
|
|
|
|
(define (->bool x) (not (not x))) |
|
|
|
|
|
|
|
|
|
|
|
|
|
(define (symbol-append . args) |
|
(string->symbol (apply string-append (map symbol->string args)))) |
|
|
|
(define (list->symbol . args) |
|
(string->symbol (apply list->string args))) |
|
|
|
(define (symbol . args) |
|
(string->symbol (apply string args))) |
|
|
|
|
|
|
|
|
|
|
|
|
|
(define (list-index l k) |
|
(let loop ((n 0) |
|
(l l)) |
|
(and (not (null? l)) |
|
(if (eq? (car l) k) |
|
n |
|
(loop (+ n 1) (cdr l)))))) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(let-syntax ((define-values* (syntax-rules () |
|
((_ (id ...) body ...) |
|
(define-values (id ...) |
|
(let () |
|
body ... |
|
(values id ...))))))) |
|
|
|
(define-values* (&exception |
|
&compound-exception |
|
simple-exceptions |
|
make-exception |
|
exception? |
|
exception-type? |
|
make-exception-type |
|
exception-predicate |
|
exception-accessor) |
|
(define &exception (make-record-type '&exception '() #:extensible? #t)) |
|
(define simple-exception? (record-predicate &exception)) |
|
|
|
(define &compound-exception (make-record-type '&compound-exception |
|
'((immutable components)))) |
|
(define compound-exception? (record-predicate &compound-exception)) |
|
(define make-compound-exception (record-constructor &compound-exception)) |
|
(define compound-exception-components |
|
(record-accessor &compound-exception 'components)) |
|
|
|
(define (simple-exceptions exception) |
|
"Return a list of the simple exceptions that compose the exception |
|
object @var{exception}." |
|
(cond ((compound-exception? exception) |
|
(compound-exception-components exception)) |
|
((simple-exception? exception) |
|
(list exception)) |
|
(else |
|
(error "not a exception" exception)))) |
|
|
|
(define (make-exception . exceptions) |
|
"Return an exception object composed of @var{exceptions}." |
|
(define (flatten exceptions) |
|
(if (null? exceptions) |
|
'() |
|
(append (simple-exceptions (car exceptions)) |
|
(flatten (cdr exceptions))))) |
|
(let ((simple (flatten exceptions))) |
|
(if (and (pair? simple) (null? (cdr simple))) |
|
(car simple) |
|
(make-compound-exception simple)))) |
|
|
|
(define (exception? obj) |
|
"Return true if @var{obj} is an exception object." |
|
(or (compound-exception? obj) (simple-exception? obj))) |
|
|
|
(define (exception-type? obj) |
|
"Return true if OBJ is an exception type." |
|
(and (record-type? obj) |
|
(record-type-has-parent? obj &exception))) |
|
|
|
(define (make-exception-type id parent field-names) |
|
"Return a new exception type named @var{id}, inheriting from |
|
@var{parent}, and with the fields whose names are listed in |
|
@var{field-names}. @var{field-names} must be a list of symbols and must |
|
not contain names already used by @var{parent} or one of its |
|
supertypes." |
|
(unless (exception-type? parent) |
|
(error "parent is not a exception type" parent)) |
|
(unless (and-map symbol? field-names) |
|
(error "field names should be a list of symbols" field-names)) |
|
(make-record-type id field-names #:parent parent #:extensible? #t)) |
|
|
|
(define (exception-predicate rtd) |
|
"Return a procedure that will return true if its argument is a |
|
simple exception that is an instance of @var{rtd}, or a compound |
|
exception composed of such an instance." |
|
(let ((rtd-predicate (record-predicate rtd))) |
|
(lambda (obj) |
|
(cond ((compound-exception? obj) |
|
(or-map rtd-predicate (simple-exceptions obj))) |
|
(else (rtd-predicate obj)))))) |
|
|
|
(define (exception-accessor rtd proc) |
|
"Return a procedure that will call @var{proc} on an instance of |
|
the exception type @var{rtd}, or on the component of a compound |
|
exception that is an instance of @var{rtd}." |
|
(let ((rtd-predicate (record-predicate rtd))) |
|
(lambda (obj) |
|
(if (rtd-predicate obj) |
|
(proc obj) |
|
(let lp ((exceptions (if (compound-exception? obj) |
|
(simple-exceptions obj) |
|
'()))) |
|
(when (null? exceptions) |
|
(error "object is not an exception of the right type" |
|
obj rtd)) (if (rtd-predicate (car exceptions)) |
|
(proc (car exceptions)) |
|
(lp (cdr exceptions))))))))) |
|
|
|
|
|
|
|
|
|
(define &exception-with-kind-and-args |
|
(make-record-type '&exception-with-kind-and-args |
|
'((immutable kind) (immutable args)) |
|
#:parent &exception #:extensible? #f)) |
|
(define &quit-exception |
|
(make-record-type '&quit-exception |
|
'((immutable code)) |
|
#:parent &exception #:extensible? #f)) |
|
|
|
(define &error |
|
(make-exception-type '&error &exception '())) |
|
(define &programming-error |
|
(make-exception-type '&programming-error &error '())) |
|
(define &non-continuable |
|
(make-exception-type '&non-continuable &programming-error '())) |
|
|
|
|
|
(define-values* (make-exception-from-throw) |
|
(define make-exception-with-kind-and-args |
|
(record-constructor &exception-with-kind-and-args)) |
|
(define make-quit-exception |
|
(record-constructor &quit-exception)) |
|
|
|
(define (make-exception-from-throw key args) |
|
(let ((exn (make-exception-with-kind-and-args key args))) |
|
(case key |
|
((quit) |
|
(let ((code (cond |
|
((not (pair? args)) 0) |
|
((integer? (car args)) (car args)) |
|
((not (car args)) 1) |
|
(else 0)))) |
|
(make-exception (make-quit-exception code) |
|
exn))) |
|
(else |
|
exn))))) |
|
|
|
(define-values* (exception-kind |
|
exception-args |
|
raise-exception |
|
with-exception-handler |
|
catch |
|
with-throw-handler |
|
throw) |
|
(define (steal-binding! sym) |
|
(let ((val (module-ref (current-module) sym))) |
|
(hashq-remove! (%get-pre-modules-obarray) sym) |
|
val)) |
|
|
|
(define %exception-handler (steal-binding! '%exception-handler)) |
|
(define %exception-epoch (steal-binding! '%exception-epoch)) |
|
(define %init-exceptions! (steal-binding! '%init-exceptions!)) |
|
|
|
(%init-exceptions! &compound-exception |
|
&exception-with-kind-and-args |
|
&quit-exception) |
|
|
|
(define exception-with-kind-and-args? |
|
(exception-predicate &exception-with-kind-and-args)) |
|
(define %exception-kind |
|
(exception-accessor &exception-with-kind-and-args |
|
(record-accessor &exception-with-kind-and-args 'kind))) |
|
(define %exception-args |
|
(exception-accessor &exception-with-kind-and-args |
|
(record-accessor &exception-with-kind-and-args 'args))) |
|
|
|
(define (exception-kind obj) |
|
(if (exception-with-kind-and-args? obj) |
|
(%exception-kind obj) |
|
'%exception)) |
|
(define (exception-args obj) |
|
(if (exception-with-kind-and-args? obj) |
|
(%exception-args obj) |
|
(list obj))) |
|
|
|
(define quit-exception? |
|
(exception-predicate &quit-exception)) |
|
(define quit-exception-code |
|
(exception-accessor &quit-exception |
|
(record-accessor &quit-exception 'code))) |
|
|
|
(define (fallback-exception-handler exn) |
|
(cond |
|
((quit-exception? exn) |
|
(primitive-exit (quit-exception-code exn))) |
|
(else |
|
(display "guile: uncaught exception:\n" (current-error-port)) |
|
(print-exception (current-error-port) #f |
|
(exception-kind exn) (exception-args exn)) |
|
(primitive-exit 1)))) |
|
|
|
(define* (raise-exception exn #:key (continuable? #f)) |
|
"Raise an exception by invoking the current exception handler on |
|
@var{exn}. The handler is called with a continuation whose dynamic |
|
environment is that of the call to @code{raise}, except that the current |
|
exception handler is the one that was in place when the handler being |
|
called was installed. |
|
|
|
If @var{continuable?} is true, the handler is invoked in tail position |
|
relative to the @code{raise-exception} call. Otherwise if the handler |
|
returns, a non-continuable exception of type @code{&non-continuable} is |
|
raised in the same dynamic environment as the handler." |
|
(define (exception-has-type? exn type) |
|
(cond |
|
((eq? type #t) |
|
#t) |
|
((symbol? type) |
|
(eq? (exception-kind exn) type)) |
|
((exception-type? type) |
|
(and (exception? exn) |
|
((exception-predicate type) exn))) |
|
(else #f))) |
|
(let ((current-epoch (fluid-ref %exception-epoch))) |
|
(let lp ((depth 0)) |
|
|
|
|
|
(let ((val (fluid-ref* %exception-handler depth))) |
|
|
|
|
|
|
|
|
|
|
|
|
|
(cond |
|
((not val) |
|
|
|
(fallback-exception-handler exn)) |
|
((fluid? (car val)) |
|
(let ((epoch (car val)) |
|
(handler (cdr val))) |
|
(cond |
|
((< (fluid-ref epoch) current-epoch) |
|
(with-fluids ((epoch current-epoch)) |
|
(cond |
|
(continuable? |
|
(handler exn)) |
|
(else |
|
(handler exn) |
|
(raise-exception |
|
((record-constructor &non-continuable))))))) |
|
(else |
|
(lp (1+ depth)))))) |
|
(else |
|
(let ((prompt-tag (car val)) |
|
(type (cdr val))) |
|
(cond |
|
((exception-has-type? exn type) |
|
(abort-to-prompt prompt-tag exn) |
|
(error "unreachable")) |
|
(else |
|
(lp (1+ depth))))))))))) |
|
|
|
(define* (with-exception-handler handler thunk #:key (unwind? #f) |
|
(unwind-for-type #t)) |
|
"Establish @var{handler}, a procedure of one argument, as the |
|
current exception handler during the dynamic extent of invoking |
|
@var{thunk}. |
|
|
|
If @code{raise-exception} is called during the dynamic extent of |
|
invoking @var{thunk}, @var{handler} will be invoked on the argument of |
|
@code{raise-exception}. |
|
|
|
There are two kinds of exception handlers: unwinding and non-unwinding. |
|
|
|
By default, exception handlers are non-unwinding. If @var{unwind?} is |
|
false, @var{handler} will be invoked within the continuation of the |
|
error, without unwinding the stack. Its dynamic environment will be |
|
that of the @code{raise-exception} call, with the exception that the |
|
current exception handler won't be @var{handler}, but rather the |
|
\"outer\" handler (the one that was in place when |
|
@code{with-exception-handler} was called). |
|
|
|
However, it's often the case that one would like to handle an exception |
|
by unwinding the computation to an earlier state and running the error |
|
handler there. After all, unless the @code{raise-exception} call is |
|
continuable, the exception handler needs to abort the continuation. To |
|
support this use case, if @var{unwind?} is true, @code{raise-exception} |
|
will first unwind the stack by invoking an @dfn{escape |
|
continuation} (@pxref{Prompt Primitives, @code{call/ec}}), and then |
|
invoke the handler with the continuation of the |
|
@code{with-exception-handler} call. |
|
|
|
Finally, one more wrinkle: for unwinding exception handlers, it can be |
|
useful to determine whether an exception handler would indeed handle a |
|
particular exception or not. This is especially the case for exceptions |
|
raised in resource-exhaustion scenarios like @code{stack-overflow} or |
|
@code{out-of-memory}, where you want to immediately shrink the |
|
continuation before recovering. @xref{Stack Overflow}. For this |
|
purpose, the @var{unwind-for-type} parameter allows users to specify the |
|
kind of exception handled by an exception handler; if @code{#t}, all |
|
exceptions will be handled; if an exception type object, only exceptions |
|
of that type will be handled; otherwise if a symbol, only that |
|
exceptions with the given @code{exception-kind} will be handled." |
|
(unless (procedure? handler) |
|
(scm-error 'wrong-type-arg "with-exception-handler" |
|
"Wrong type argument in position ~a: ~a" |
|
(list 1 handler) (list handler))) |
|
(cond |
|
(unwind? |
|
(unless (or (eq? unwind-for-type #t) |
|
(symbol? unwind-for-type) |
|
(exception-type? unwind-for-type)) |
|
(scm-error 'wrong-type-arg "with-exception-handler" |
|
"Wrong type argument for #:unwind-for-type: ~a" |
|
(list unwind-for-type) (list unwind-for-type))) |
|
(let ((tag (make-prompt-tag "exception handler"))) |
|
(call-with-prompt |
|
tag |
|
(lambda () |
|
(with-fluids ((%exception-handler (cons tag unwind-for-type))) |
|
(thunk))) |
|
(lambda (k exn) |
|
(handler exn))))) |
|
(else |
|
(let ((epoch (make-fluid 0))) |
|
(with-fluids ((%exception-handler (cons epoch handler))) |
|
(thunk)))))) |
|
|
|
(define (throw key . args) |
|
"Invoke the catch form matching @var{key}, passing @var{args} to the |
|
@var{handler}. |
|
|
|
@var{key} is a symbol. It will match catches of the same symbol or of @code{#t}. |
|
|
|
If there is no handler at all, Guile prints an error and then exits." |
|
(unless (symbol? key) |
|
(throw 'wrong-type-arg "throw" "Wrong type argument in position ~a: ~a" |
|
(list 1 key) (list key))) |
|
(raise-exception (make-exception-from-throw key args))) |
|
|
|
(define (with-throw-handler k thunk pre-unwind-handler) |
|
"Add @var{handler} to the dynamic context as a throw handler |
|
for key @var{k}, then invoke @var{thunk}." |
|
(unless (or (symbol? k) (eq? k #t)) |
|
(scm-error 'wrong-type-arg "with-throw-handler" |
|
"Wrong type argument in position ~a: ~a" |
|
(list 1 k) (list k))) |
|
(define running? (make-fluid)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(with-exception-handler |
|
(lambda (exn) |
|
(when (and (or (eq? k #t) (eq? k (exception-kind exn))) |
|
(not (fluid-ref running?))) |
|
(with-fluids ((%exception-epoch (1+ (fluid-ref %exception-epoch))) |
|
(running? #t)) |
|
(apply pre-unwind-handler (exception-kind exn) |
|
(exception-args exn)))) |
|
(raise-exception exn)) |
|
thunk)) |
|
|
|
(define* (catch k thunk handler #:optional pre-unwind-handler) |
|
"Invoke @var{thunk} in the dynamic context of @var{handler} for |
|
exceptions matching @var{key}. If thunk throws to the symbol |
|
@var{key}, then @var{handler} is invoked this way: |
|
@lisp |
|
(handler key args ...) |
|
@end lisp |
|
|
|
@var{key} is a symbol or @code{#t}. |
|
|
|
@var{thunk} takes no arguments. If @var{thunk} returns |
|
normally, that is the return value of @code{catch}. |
|
|
|
Handler is invoked outside the scope of its own @code{catch}. |
|
If @var{handler} again throws to the same key, a new handler |
|
from further up the call chain is invoked. |
|
|
|
If the key is @code{#t}, then a throw to @emph{any} symbol will |
|
match this call to @code{catch}. |
|
|
|
If a @var{pre-unwind-handler} is given and @var{thunk} throws |
|
an exception that matches @var{key}, Guile calls the |
|
@var{pre-unwind-handler} before unwinding the dynamic state and |
|
invoking the main @var{handler}. @var{pre-unwind-handler} should |
|
be a procedure with the same signature as @var{handler}, that |
|
is @code{(lambda (key . args))}. It is typically used to save |
|
the stack at the point where the exception occurred, but can also |
|
query other parts of the dynamic state at that point, such as |
|
fluid values. |
|
|
|
A @var{pre-unwind-handler} can exit either normally or non-locally. |
|
If it exits normally, Guile unwinds the stack and dynamic context |
|
and then calls the normal (third argument) handler. If it exits |
|
non-locally, that exit determines the continuation." |
|
(define (wrong-type-arg n val) |
|
(scm-error 'wrong-type-arg "catch" |
|
"Wrong type argument in position ~a: ~a" |
|
(list n val) (list val))) |
|
(unless (or (symbol? k) (eq? k #t)) |
|
(wrong-type-arg 2 k)) |
|
(unless (procedure? handler) |
|
(wrong-type-arg 3 handler)) |
|
(unless (or (not pre-unwind-handler) (procedure? pre-unwind-handler)) |
|
(wrong-type-arg 4 pre-unwind-handler)) |
|
|
|
(with-exception-handler |
|
(lambda (exn) |
|
(apply handler (exception-kind exn) (exception-args exn))) |
|
(if pre-unwind-handler |
|
(lambda () |
|
(with-throw-handler k thunk pre-unwind-handler)) |
|
thunk) |
|
#:unwind? #t |
|
#:unwind-for-type k)))) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define set-exception-printer! #f) |
|
|
|
|
|
|
|
(let ((exception-printers '())) |
|
(define (print-location frame port) |
|
(let ((source (and=> frame frame-source))) |
|
|
|
(if source |
|
(let ((filename (or (cadr source) "<unnamed port>")) |
|
(line (caddr source)) |
|
(col (cdddr source))) |
|
(format port "~a:~a:~a: " filename (1+ line) col)) |
|
(format port "ERROR: ")))) |
|
|
|
(set! set-exception-printer! |
|
(lambda (key proc) |
|
(set! exception-printers (acons key proc exception-printers)))) |
|
|
|
(set! print-exception |
|
(lambda (port frame key args) |
|
(define (default-printer) |
|
(format port "Throw to key `~a' with args `~s'." key args)) |
|
|
|
(when frame |
|
(print-location frame port) |
|
|
|
(let ((name (catch #t |
|
(lambda () (frame-procedure-name frame)) |
|
(lambda _ #f)))) |
|
(when name |
|
(format port "In procedure ~a:\n" name)))) |
|
|
|
(catch #t |
|
(lambda () |
|
(let ((printer (assq-ref exception-printers key))) |
|
(if printer |
|
(printer port key args default-printer) |
|
(default-printer)))) |
|
(lambda (k . args) |
|
(format port "Error while printing exception."))) |
|
(newline port) |
|
(force-output port)))) |
|
|
|
|
|
|
|
|
|
(let () |
|
(define (scm-error-printer port key args default-printer) |
|
|
|
|
|
(apply (case-lambda |
|
((subr msg args . rest) |
|
(if subr |
|
(format port "In procedure ~a: " subr)) |
|
(apply format port msg (or args '()))) |
|
(_ (default-printer))) |
|
args)) |
|
|
|
(define (syntax-error-printer port key args default-printer) |
|
(apply (case-lambda |
|
((who what where form subform . extra) |
|
(format port "Syntax error:\n") |
|
(if where |
|
(let ((file (or (assq-ref where 'filename) "unknown file")) |
|
(line (and=> (assq-ref where 'line) 1+)) |
|
(col (assq-ref where 'column))) |
|
(format port "~a:~a:~a: " file line col)) |
|
(format port "unknown location: ")) |
|
(if who |
|
(format port "~a: " who)) |
|
(format port "~a" what) |
|
(if subform |
|
(format port " in subform ~s of ~s" subform form) |
|
(if form |
|
(format port " in form ~s" form)))) |
|
(_ (default-printer))) |
|
args)) |
|
|
|
(define (keyword-error-printer port key args default-printer) |
|
(let ((message (cadr args)) |
|
(faulty (car (cadddr args)))) |
|
(format port "~a: ~s" message faulty))) |
|
|
|
(define (getaddrinfo-error-printer port key args default-printer) |
|
(format port "In procedure getaddrinfo: ~a" (gai-strerror (car args)))) |
|
|
|
(set-exception-printer! 'goops-error scm-error-printer) |
|
(set-exception-printer! 'host-not-found scm-error-printer) |
|
(set-exception-printer! 'keyword-argument-error keyword-error-printer) |
|
(set-exception-printer! 'misc-error scm-error-printer) |
|
(set-exception-printer! 'no-data scm-error-printer) |
|
(set-exception-printer! 'no-recovery scm-error-printer) |
|
(set-exception-printer! 'null-pointer-error scm-error-printer) |
|
(set-exception-printer! 'out-of-memory scm-error-printer) |
|
(set-exception-printer! 'out-of-range scm-error-printer) |
|
(set-exception-printer! 'program-error scm-error-printer) |
|
(set-exception-printer! 'read-error scm-error-printer) |
|
(set-exception-printer! 'regular-expression-syntax scm-error-printer) |
|
(set-exception-printer! 'signal scm-error-printer) |
|
(set-exception-printer! 'stack-overflow scm-error-printer) |
|
(set-exception-printer! 'system-error scm-error-printer) |
|
(set-exception-printer! 'try-again scm-error-printer) |
|
(set-exception-printer! 'unbound-variable scm-error-printer) |
|
(set-exception-printer! 'wrong-number-of-args scm-error-printer) |
|
(set-exception-printer! 'wrong-type-arg scm-error-printer) |
|
|
|
(set-exception-printer! 'syntax-error syntax-error-printer) |
|
|
|
(set-exception-printer! 'getaddrinfo-error getaddrinfo-error-printer)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
(primitive-load-path "ice-9/posix") |
|
|
|
(if (provided? 'socket) |
|
(primitive-load-path "ice-9/networking")) |
|
|
|
|
|
(define file-exists? |
|
(if (provided? 'posix) |
|
(lambda (str) |
|
(->bool (stat str #f))) |
|
(lambda (str) |
|
(let ((port (catch 'system-error (lambda () (open-input-file str)) |
|
(lambda args #f)))) |
|
(if port (begin (close-port port) #t) |
|
#f))))) |
|
|
|
(define file-is-directory? |
|
(if (provided? 'posix) |
|
(lambda (str) |
|
(eq? (stat:type (stat str)) 'directory)) |
|
(lambda (str) |
|
(let ((port (catch 'system-error |
|
(lambda () |
|
(open-input-file (string-append str "/."))) |
|
(lambda args #f)))) |
|
(if port (begin (close-port port) #t) |
|
#f))))) |
|
|
|
(define (system-error-errno args) |
|
(if (eq? (car args) 'system-error) |
|
(car (list-ref args 4)) |
|
#f)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
(define error |
|
(case-lambda |
|
(() |
|
(scm-error 'misc-error #f "?" #f #f)) |
|
((message . args) |
|
(let ((msg (string-join (cons "~A" (make-list (length args) "~S"))))) |
|
(scm-error 'misc-error #f msg (cons message args) #f))))) |
|
|
|
|
|
|
|
|
|
|
|
|
|
(define (tm:sec obj) (vector-ref obj 0)) |
|
(define (tm:min obj) (vector-ref obj 1)) |
|
(define (tm:hour obj) (vector-ref obj 2)) |
|
(define (tm:mday obj) (vector-ref obj 3)) |
|
(define (tm:mon obj) (vector-ref obj 4)) |
|
(define (tm:year obj) (vector-ref obj 5)) |
|
(define (tm:wday obj) (vector-ref obj 6)) |
|
(define (tm:yday obj) (vector-ref obj 7)) |
|
(define (tm:isdst obj) (vector-ref obj 8)) |
|
(define (tm:gmtoff obj) (vector-ref obj 9)) |
|
(define (tm:zone obj) (vector-ref obj 10)) |
|
|
|
(define (set-tm:sec obj val) (vector-set! obj 0 val)) |
|
(define (set-tm:min obj val) (vector-set! obj 1 val)) |
|
(define (set-tm:hour obj val) (vector-set! obj 2 val)) |
|
(define (set-tm:mday obj val) (vector-set! obj 3 val)) |
|
(define (set-tm:mon obj val) (vector-set! obj 4 val)) |
|
(define (set-tm:year obj val) (vector-set! obj 5 val)) |
|
(define (set-tm:wday obj val) (vector-set! obj 6 val)) |
|
(define (set-tm:yday obj val) (vector-set! obj 7 val)) |
|
(define (set-tm:isdst obj val) (vector-set! obj 8 val)) |
|
(define (set-tm:gmtoff obj val) (vector-set! obj 9 val)) |
|
(define (set-tm:zone obj val) (vector-set! obj 10 val)) |
|
|
|
(define (tms:clock obj) (vector-ref obj 0)) |
|
(define (tms:utime obj) (vector-ref obj 1)) |
|
(define (tms:stime obj) (vector-ref obj 2)) |
|
(define (tms:cutime obj) (vector-ref obj 3)) |
|
(define (tms:cstime obj) (vector-ref obj 4)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
(define (setenv name value) |
|
(if value |
|
(putenv (string-append name "=" value)) |
|
(putenv name))) |
|
|
|
(define (unsetenv name) |
|
"Remove the entry for NAME from the environment." |
|
(putenv name)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
(let-syntax ((compile-time-case |
|
(lambda (stx) |
|
(syntax-case stx () |
|
((_ exp clauses ...) |
|
(let ((val (primitive-eval (syntax->datum #'exp)))) |
|
(let next-clause ((clauses #'(clauses ...))) |
|
(syntax-case clauses (else) |
|
(() |
|
(syntax-violation 'compile-time-case |
|
"all clauses failed to match" stx)) |
|
(((else form ...)) |
|
#'(begin form ...)) |
|
((((k ...) form ...) clauses ...) |
|
(if (memv val (syntax->datum #'(k ...))) |
|
#'(begin form ...) |
|
(next-clause #'(clauses ...)))))))))))) |
|
|
|
(compile-time-case (system-file-name-convention) |
|
((posix) |
|
(define (file-name-separator? c) |
|
(char=? c #\/)) |
|
|
|
(define file-name-separator-string "/") |
|
|
|
(define (absolute-file-name? file-name) |
|
(string-prefix? "/" file-name))) |
|
|
|
((windows) |
|
(define (file-name-separator? c) |
|
(or (char=? c #\/) |
|
(char=? c #\\))) |
|
|
|
(define file-name-separator-string "/") |
|
|
|
(define (absolute-file-name? file-name) |
|
(define (file-name-separator-at-index? idx) |
|
(and (> (string-length file-name) idx) |
|
(file-name-separator? (string-ref file-name idx)))) |
|
(define (unc-file-name?) |
|
|
|
|
|
|
|
(and (file-name-separator-at-index? 0) |
|
(file-name-separator-at-index? 1))) |
|
(define (has-drive-specifier?) |
|
(and (>= (string-length file-name) 2) |
|
(let ((drive (string-ref file-name 0))) |
|
(or (char<=? #\a drive #\z) |
|
(char<=? #\A drive #\Z))) |
|
(eqv? (string-ref file-name 1) #\:))) |
|
(or (unc-file-name?) |
|
(if (has-drive-specifier?) |
|
(file-name-separator-at-index? 2) |
|
(file-name-separator-at-index? 0))))))) |
|
|
|
(define (in-vicinity vicinity file) |
|
(let ((tail (let ((len (string-length vicinity))) |
|
(if (zero? len) |
|
#f |
|
(string-ref vicinity (- len 1)))))) |
|
(string-append vicinity |
|
(if (or (not tail) (file-name-separator? tail)) |
|
"" |
|
file-name-separator-string) |
|
file))) |
|
|
|
|
|
|
|
|
|
|
|
|
|
(define-syntax false-if-exception |
|
(syntax-rules () |
|
((false-if-exception expr) |
|
(catch #t |
|
(lambda () expr) |
|
(lambda args #f))) |
|
((false-if-exception expr #:warning template arg ...) |
|
(catch #t |
|
(lambda () expr) |
|
(lambda (key . args) |
|
(for-each (lambda (s) |
|
(if (not (string-null? s)) |
|
(format (current-warning-port) ";;; ~a\n" s))) |
|
(string-split |
|
(call-with-output-string |
|
(lambda (port) |
|
(format port template arg ...) |
|
(print-exception port #f key args))) |
|
#\newline)) |
|
#f))))) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define (command-line) (program-arguments)) |
|
|
|
|
|
|
|
|
|
(define (load-user-init) |
|
(let* ((home (or (getenv "HOME") |
|
(false-if-exception (passwd:dir (getpwuid (getuid)))) |
|
file-name-separator-string)) |
|
(init-file (in-vicinity home ".guile"))) |
|
(if (file-exists? init-file) |
|
(primitive-load init-file)))) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define (%start-stack tag thunk) |
|
(let ((prompt-tag (make-prompt-tag "start-stack"))) |
|
(call-with-prompt |
|
prompt-tag |
|
(lambda () |
|
(with-fluids ((%stacks (cons tag prompt-tag))) |
|
(thunk))) |
|
(lambda (k . args) |
|
(%start-stack tag (lambda () (apply k args))))))) |
|
|
|
(define-syntax-rule (start-stack tag exp) |
|
(%start-stack tag (lambda () exp))) |
|
|
|
|
|
|
|
|
|
|
|
|
|
(define (load-from-path name) |
|
"Load a Scheme source file named NAME, searching for it in the |
|
directories listed in %load-path, and applying each of the file |
|
name extensions listed in %load-extensions." |
|
(start-stack 'load-stack |
|
(primitive-load-path name))) |
|
|
|
(define-syntax-rule (add-to-load-path elt) |
|
"Add ELT to Guile's load path, at compile-time and at run-time." |
|
(eval-when (expand load eval) |
|
(set! %load-path (cons elt (delete elt %load-path))))) |
|
|
|
(define %load-verbosely #f) |
|
(define (assert-load-verbosity v) (set! %load-verbosely v)) |
|
|
|
(define (%load-announce file) |
|
(if %load-verbosely |
|
(with-output-to-port (current-warning-port) |
|
(lambda () |
|
(display ";;; ") |
|
(display "loading ") |
|
(display file) |
|
(newline) |
|
(force-output))))) |
|
|
|
(set! %load-hook %load-announce) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define read-hash-procedures |
|
(fluid->parameter %read-hash-procedures)) |
|
|
|
(define (read-hash-procedure ch) |
|
(assq-ref (read-hash-procedures) ch)) |
|
|
|
(define (read-hash-extend ch proc) |
|
(let ((alist (read-hash-procedures))) |
|
(read-hash-procedures |
|
(if proc |
|
(assq-set! alist ch proc) |
|
(assq-remove! alist ch))))) |
|
|
|
(define read-eval? (make-fluid #f)) |
|
(read-hash-extend #\. |
|
(lambda (c port) |
|
(if (fluid-ref read-eval?) |
|
(eval (read port) (interaction-environment)) |
|
(error |
|
"#. read expansion found and read-eval? is #f.")))) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define (%print-module mod port) |
|
(display "#<" port) |
|
(display (or (module-kind mod) "module") port) |
|
(display " " port) |
|
(display (module-name mod) port) |
|
(display " " port) |
|
(display (number->string (object-address mod) 16) port) |
|
(display ">" port)) |
|
|
|
(letrec-syntax |
|
|
|
|
|
|
|
|
|
|
|
|
|
((define-record-type |
|
(lambda (x) |
|
(define (make-id scope . fragments) |
|
(datum->syntax scope |
|
(apply symbol-append |
|
(map (lambda (x) |
|
(if (symbol? x) x (syntax->datum x))) |
|
fragments)))) |
|
|
|
(define (getter rtd type-name field slot) |
|
(define id (make-id rtd type-name '- field)) |
|
#`(define #,id |
|
(let ((rtd #,rtd)) |
|
(lambda (#,type-name) |
|
(unless (eq? (struct-vtable #,type-name) rtd) |
|
(scm-error 'wrong-type-arg |
|
#,(symbol->string (syntax->datum id)) |
|
"Wrong type argument (want `~S'): ~S" |
|
(list '#,type-name #,type-name) |
|
#f)) |
|
(struct-ref #,type-name #,slot))))) |
|
|
|
(define (setter rtd type-name field slot) |
|
(define id (make-id rtd 'set- type-name '- field '!)) |
|
#`(define #,id |
|
(let ((rtd #,rtd)) |
|
(lambda (#,type-name val) |
|
(unless (eq? (struct-vtable #,type-name) rtd) |
|
(scm-error 'wrong-type-arg |
|
#,(symbol->string (syntax->datum id)) |
|
"Wrong type argument (want `~S'): ~S" |
|
(list '#,type-name #,type-name) |
|
#f)) |
|
(struct-set! #,type-name #,slot val))))) |
|
|
|
(define (accessors rtd type-name fields n exp) |
|
(syntax-case fields () |
|
(() exp) |
|
(((field #:no-accessors) field* ...) (identifier? #'field) |
|
(accessors rtd type-name #'(field* ...) (1+ n) |
|
exp)) |
|
(((field #:no-setter) field* ...) (identifier? #'field) |
|
(accessors rtd type-name #'(field* ...) (1+ n) |
|
#`(begin #,exp |
|
#,(getter rtd type-name #'field n)))) |
|
(((field #:no-getter) field* ...) (identifier? #'field) |
|
(accessors rtd type-name #'(field* ...) (1+ n) |
|
#`(begin #,exp |
|
#,(setter rtd type-name #'field n)))) |
|
((field field* ...) (identifier? #'field) |
|
(accessors rtd type-name #'(field* ...) (1+ n) |
|
#`(begin #,exp |
|
#,(getter rtd type-name #'field n) |
|
#,(setter rtd type-name #'field n)))))) |
|
|
|
(define (predicate rtd type-name fields exp) |
|
(accessors |
|
rtd type-name fields 0 |
|
#`(begin |
|
#,exp |
|
(define (#,(make-id rtd type-name '?) obj) |
|
(and (struct? obj) (eq? (struct-vtable obj) #,rtd)))))) |
|
|
|
(define (field-list fields) |
|
(syntax-case fields () |
|
(() '()) |
|
(((f . opts) . rest) (identifier? #'f) |
|
(cons #'f (field-list #'rest))) |
|
((f . rest) (identifier? #'f) |
|
(cons #'f (field-list #'rest))))) |
|
|
|
(define (constructor rtd type-name fields exp) |
|
(let* ((ctor (make-id rtd type-name '-constructor)) |
|
(args (field-list fields)) |
|
(n (length fields)) |
|
(slots (iota n))) |
|
(predicate rtd type-name fields |
|
#`(begin #,exp |
|
(define #,ctor |
|
(let ((rtd #,rtd)) |
|
(lambda #,args |
|
(make-struct/simple rtd #,@args)))) |
|
(struct-set! #,rtd (+ vtable-offset-user 2) |
|
#,ctor))))) |
|
|
|
(define (type type-name printer fields) |
|
(define (make-layout) |
|
(let lp ((fields fields) (slots '())) |
|
(syntax-case fields () |
|
(() (datum->syntax #'here |
|
(make-struct-layout |
|
(apply string-append slots)))) |
|
((_ . rest) (lp #'rest (cons "pw" slots)))))) |
|
|
|
(let ((rtd (make-id type-name type-name '-type))) |
|
(constructor rtd type-name fields |
|
#`(begin |
|
(define #,rtd |
|
(make-struct/no-tail |
|
record-type-vtable |
|
'#,(make-layout) |
|
#,printer |
|
'#,type-name |
|
'#,(field-list fields) |
|
#f ; constructor; set later |
|
'() ; properties |
|
#())) ; parents |
|
(set-struct-vtable-name! #,rtd '#,type-name))))) |
|
|
|
(syntax-case x () |
|
((_ type-name printer (field ...)) |
|
(type #'type-name #'printer #'(field ...))))))) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define-record-type module |
|
(lambda (obj port) (%print-module obj port)) |
|
(obarray |
|
uses |
|
binder |
|
declarative? |
|
(transformer #:no-getter) |
|
(name #:no-getter) |
|
kind |
|
duplicates-handlers |
|
(import-obarray #:no-setter) |
|
observers |
|
(weak-observers #:no-setter) |
|
version |
|
submodules |
|
submodule-binder |
|
public-interface |
|
filename |
|
next-unique-id |
|
(replacements #:no-setter) |
|
inlinable-exports))) |
|
|
|
|
|
|
|
|
|
(define* (make-module #:optional (size 0) (uses '()) (binder #f)) |
|
"Create a new module, perhaps with a particular size of obarray, |
|
initial uses list, or binding procedure." |
|
(unless (integer? size) |
|
(error "Illegal size to make-module." size)) |
|
(unless (zero? size) |
|
(issue-deprecation-warning |
|
"Passing a non-zero size argument to `make-module' is deprecated. " |
|
"Omit the argument or pass zero instead.")) |
|
(unless (and (list? uses) (and-map module? uses)) |
|
(error "Incorrect use list." uses)) |
|
(when (and binder (not (procedure? binder))) |
|
(error "Lazy-binder expected to be a procedure or #f." binder)) |
|
|
|
(module-constructor (make-hash-table size) |
|
uses binder #f macroexpand |
|
#f #f #f |
|
(make-hash-table) |
|
'() |
|
(make-weak-key-hash-table) #f |
|
(make-hash-table) #f #f #f 0 |
|
(make-hash-table) #f)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define (module-observe module proc) |
|
(set-module-observers! module (cons proc (module-observers module))) |
|
(cons module proc)) |
|
|
|
(define* (module-observe-weak module observer-id #:optional (proc observer-id)) |
|
"Register PROC as an observer of MODULE under name OBSERVER-ID (which can |
|
be any Scheme object). PROC is invoked and passed MODULE any time |
|
MODULE is modified. PROC gets unregistered when OBSERVER-ID gets GC'd |
|
(thus, it is never unregistered if OBSERVER-ID is an immediate value, |
|
for instance). |
|
|
|
The two-argument version is kept for backward compatibility: when called |
|
with two arguments, the observer gets unregistered when closure PROC |
|
gets GC'd (making it impossible to use an anonymous lambda for PROC)." |
|
(hashq-set! (module-weak-observers module) observer-id proc)) |
|
|
|
(define (module-unobserve token) |
|
(let ((module (car token)) |
|
(id (cdr token))) |
|
(if (integer? id) |
|
(hash-remove! (module-weak-observers module) id) |
|
(set-module-observers! module (delq1! id (module-observers module))))) |
|
*unspecified*) |
|
|
|
|
|
|
|
(define module-defer-observers (make-parameter #f)) |
|
|
|
(define (module-modified m) |
|
(cond |
|
((module-defer-observers) => (lambda (tab) (hashq-set! tab m #t))) |
|
(else (module-call-observers m)))) |
|
|
|
|
|
|
|
|
|
(define (call-with-deferred-observers thunk) |
|
(cond |
|
((module-defer-observers) (thunk)) |
|
(else |
|
(let ((modules (make-hash-table))) |
|
(dynamic-wind (lambda () #t) |
|
(lambda () |
|
(parameterize ((module-defer-observers modules)) |
|
(thunk))) |
|
(lambda () |
|
(let ((changed (hash-map->list cons modules))) |
|
(hash-clear! modules) |
|
(for-each (lambda (pair) |
|
(module-call-observers (car pair))) |
|
changed)))))))) |
|
|
|
(define (module-call-observers m) |
|
(for-each (lambda (proc) (proc m)) (module-observers m)) |
|
|
|
|
|
|
|
|
|
(hash-for-each (lambda (id proc) (proc m)) (module-weak-observers m))) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define (module-search fn m v) |
|
"Return the first non-#f result of FN applied to M and then to |
|
the modules in the uses of M, and so on recursively. If all applications |
|
return #f, then so does this function." |
|
(define (loop pos) |
|
(and (pair? pos) |
|
(or (module-search fn (car pos) v) |
|
(loop (cdr pos))))) |
|
(or (fn m v) |
|
(loop (module-uses m)))) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define (module-locally-bound? m v) |
|
"Is symbol V bound (interned and defined) locally in module M?" |
|
(let ((var (module-local-variable m v))) |
|
(and var |
|
(variable-bound? var)))) |
|
|
|
(define (module-bound? m v) |
|
"Is symbol V bound (interned and defined) anywhere in module M or its |
|
uses?" |
|
(let ((var (module-variable m v))) |
|
(and var |
|
(variable-bound? var)))) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define (module-obarray-get-handle ob key) |
|
((if (symbol? key) hashq-get-handle hash-get-handle) ob key)) |
|
|
|
(define (module-obarray-ref ob key) |
|
((if (symbol? key) hashq-ref hash-ref) ob key)) |
|
|
|
(define (module-obarray-set! ob key val) |
|
((if (symbol? key) hashq-set! hash-set!) ob key val)) |
|
|
|
(define (module-obarray-remove! ob key) |
|
((if (symbol? key) hashq-remove! hash-remove!) ob key)) |
|
|
|
(define (module-symbol-locally-interned? m v) |
|
"Is symbol V interned (not neccessarily defined) locally in module M |
|
or its uses? Interned symbols shadow inherited bindings even if they |
|
are not themselves bound to a defined value." |
|
(not (not (module-obarray-get-handle (module-obarray m) v)))) |
|
|
|
(define (module-symbol-interned? m v) |
|
"Is symbol V interned (not neccessarily defined) anywhere in module M |
|
or its uses? Interned symbols shadow inherited bindings even if they |
|
are not themselves bound to a defined value." |
|
(module-search module-symbol-locally-interned? m v)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define (module-symbol-local-binding m v . opt-val) |
|
"Return the binding of variable V specified by name within module M, |
|
signaling an error if the variable is unbound. If the OPT-VALUE is |
|
passed, then instead of signaling an error, return OPT-VALUE." |
|
(let ((var (module-local-variable m v))) |
|
(if (and var (variable-bound? var)) |
|
(variable-ref var) |
|
(if (not (null? opt-val)) |
|
(car opt-val) |
|
(error "Locally unbound variable." v))))) |
|
|
|
(define (module-symbol-binding m v . opt-val) |
|
"Return the binding of variable V specified by name within module M, |
|
signaling an error if the variable is unbound. If the OPT-VALUE is |
|
passed, then instead of signaling an error, return OPT-VALUE." |
|
(let ((var (module-variable m v))) |
|
(if (and var (variable-bound? var)) |
|
(variable-ref var) |
|
(if (not (null? opt-val)) |
|
(car opt-val) |
|
(error "Unbound variable." v))))) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define (module-make-local-var! m v) |
|
"Ensure a variable for V in the local namespace of M. |
|
If no variable was already there, then create a new and uninitialized |
|
variable." |
|
(or (let ((b (module-obarray-ref (module-obarray m) v))) |
|
(and (variable? b) |
|
(begin |
|
|
|
|
|
(module-modified m) |
|
b))) |
|
|
|
|
|
(let ((local-var (make-undefined-variable))) |
|
(module-add! m v local-var) |
|
local-var))) |
|
|
|
(define (module-ensure-local-variable! module symbol) |
|
"Ensure that there is a local variable in MODULE for SYMBOL. If |
|
there is no binding for SYMBOL, create a new uninitialized |
|
variable. Return the local variable." |
|
(or (module-local-variable module symbol) |
|
(let ((var (make-undefined-variable))) |
|
(module-add! module symbol var) |
|
var))) |
|
|
|
|
|
|
|
(define (module-add! m v var) |
|
"Ensure a particular variable for V in the local namespace of M." |
|
(if (not (variable? var)) |
|
(error "Bad variable to module-add!" var)) |
|
(if (not (symbol? v)) |
|
(error "Bad symbol to module-add!" v)) |
|
(module-obarray-set! (module-obarray m) v var) |
|
(module-modified m)) |
|
|
|
(define (module-remove! m v) |
|
"Make sure that symbol V is undefined in the local namespace of M." |
|
(module-obarray-remove! (module-obarray m) v) |
|
(module-modified m)) |
|
|
|
(define (module-clear! m) |
|
(hash-clear! (module-obarray m)) |
|
(module-modified m)) |
|
|
|
|
|
|
|
(define (module-for-each proc module) |
|
"Call PROC on each symbol in MODULE, with arguments of (SYMBOL VARIABLE)." |
|
(hash-for-each proc (module-obarray module))) |
|
|
|
(define (module-map proc module) |
|
(hash-map->list proc (module-obarray module))) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define (module-ref-submodule module name) |
|
(or (hashq-ref (module-submodules module) name) |
|
(and (module-submodule-binder module) |
|
((module-submodule-binder module) module name)))) |
|
|
|
(define (module-define-submodule! module name submodule) |
|
(hashq-set! (module-submodules module) name submodule)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
(define (save-module-excursion thunk) |
|
(let ((inner-module (current-module)) |
|
(outer-module #f)) |
|
(dynamic-wind (lambda () |
|
(set! outer-module (current-module)) |
|
(set-current-module inner-module) |
|
(set! inner-module #f)) |
|
thunk |
|
(lambda () |
|
(set! inner-module (current-module)) |
|
(set-current-module outer-module) |
|
(set! outer-module #f))))) |
|
|
|
|
|
|
|
|
|
|
|
(define (module-ref module name . rest) |
|
"Returns the value of a variable called NAME in MODULE or any of its |
|
used modules. If there is no such variable, then if the optional third |
|
argument DEFAULT is present, it is returned; otherwise an error is signaled." |
|
(let ((variable (module-variable module name))) |
|
(if (and variable (variable-bound? variable)) |
|
(variable-ref variable) |
|
(if (null? rest) |
|
(error "No variable named" name 'in module) |
|
(car rest) |
|
)))) |
|
|
|
|
|
|
|
(define (module-set! module name value) |
|
"Sets the variable called NAME in MODULE (or in a module that MODULE uses) |
|
to VALUE; if there is no such variable, an error is signaled." |
|
(let ((variable (module-variable module name))) |
|
(if variable |
|
(variable-set! variable value) |
|
(error "No variable named" name 'in module)))) |
|
|
|
|
|
|
|
(define (module-define! module name value) |
|
"Sets the variable called NAME in MODULE to VALUE; if there is no such |
|
variable, it is added first." |
|
(let ((variable (module-local-variable module name))) |
|
(if variable |
|
(begin |
|
(variable-set! variable value) |
|
(module-modified module)) |
|
(let ((variable (make-variable value))) |
|
(module-add! module name variable))))) |
|
|
|
|
|
|
|
(define (module-defined? module name) |
|
"Return #t iff NAME is defined in MODULE (or in a module that MODULE |
|
uses)." |
|
(let ((variable (module-variable module name))) |
|
(and variable (variable-bound? variable)))) |
|
|
|
(define (module-use! module interface) |
|
"Add INTERFACE to the list of interfaces used by MODULE." |
|
(if (not (or (eq? module interface) |
|
(memq interface (module-uses module)))) |
|
(begin |
|
|
|
|
|
|
|
(set-module-uses! module (append (module-uses module) |
|
(list interface))) |
|
(hash-clear! (module-import-obarray module)) |
|
(module-modified module)))) |
|
|
|
(define (module-use-interfaces! module interfaces) |
|
"Same as MODULE-USE!, but only notifies module observers after all |
|
interfaces are added to the inports list." |
|
(let* ((cur (module-uses module)) |
|
(new (let lp ((in interfaces) (out '())) |
|
(if (null? in) |
|
(reverse out) |
|
(lp (cdr in) |
|
(let ((iface (car in))) |
|
(if (or (memq iface cur) (memq iface out)) |
|
out |
|
(cons iface out)))))))) |
|
(set-module-uses! module (append cur new)) |
|
(hash-clear! (module-import-obarray module)) |
|
(module-modified module))) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define (nested-ref root names) |
|
(if (null? names) |
|
root |
|
(let loop ((cur root) |
|
(head (car names)) |
|
(tail (cdr names))) |
|
(if (null? tail) |
|
(module-ref cur head #f) |
|
(let ((cur (module-ref-submodule cur head))) |
|
(and cur |
|
(loop cur (car tail) (cdr tail)))))))) |
|
|
|
(define (nested-set! root names val) |
|
(let loop ((cur root) |
|
(head (car names)) |
|
(tail (cdr names))) |
|
(if (null? tail) |
|
(module-set! cur head val) |
|
(let ((cur (module-ref-submodule cur head))) |
|
(if (not cur) |
|
(error "failed to resolve module" names) |
|
(loop cur (car tail) (cdr tail))))))) |
|
|
|
(define (nested-define! root names val) |
|
(let loop ((cur root) |
|
(head (car names)) |
|
(tail (cdr names))) |
|
(if (null? tail) |
|
(module-define! cur head val) |
|
(let ((cur (module-ref-submodule cur head))) |
|
(if (not cur) |
|
(error "failed to resolve module" names) |
|
(loop cur (car tail) (cdr tail))))))) |
|
|
|
(define (nested-remove! root names) |
|
(let loop ((cur root) |
|
(head (car names)) |
|
(tail (cdr names))) |
|
(if (null? tail) |
|
(module-remove! cur head) |
|
(let ((cur (module-ref-submodule cur head))) |
|
(if (not cur) |
|
(error "failed to resolve module" names) |
|
(loop cur (car tail) (cdr tail))))))) |
|
|
|
|
|
(define (nested-ref-module root names) |
|
(let loop ((cur root) |
|
(names names)) |
|
(if (null? names) |
|
cur |
|
(let ((cur (module-ref-submodule cur (car names)))) |
|
(and cur |
|
(loop cur (cdr names))))))) |
|
|
|
(define (nested-define-module! root names module) |
|
(if (null? names) |
|
(error "can't redefine root module" root module) |
|
(let loop ((cur root) |
|
(head (car names)) |
|
(tail (cdr names))) |
|
(if (null? tail) |
|
(module-define-submodule! cur head module) |
|
(let ((cur (or (module-ref-submodule cur head) |
|
(let ((m (make-module))) |
|
(set-module-kind! m 'directory) |
|
(set-module-name! m (append (module-name cur) |
|
(list head))) |
|
(module-define-submodule! cur head m) |
|
m)))) |
|
(loop cur (car tail) (cdr tail))))))) |
|
|
|
|
|
(define (local-ref names) |
|
(nested-ref (current-module) names)) |
|
|
|
(define (local-set! names val) |
|
(nested-set! (current-module) names val)) |
|
|
|
(define (local-define names val) |
|
(nested-define! (current-module) names val)) |
|
|
|
(define (local-remove names) |
|
(nested-remove! (current-module) names)) |
|
|
|
(define (local-ref-module names) |
|
(nested-ref-module (current-module) names)) |
|
|
|
(define (local-define-module names mod) |
|
(nested-define-module! (current-module) names mod)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define the-root-module |
|
(let ((m (make-module 0))) |
|
(set-module-obarray! m (%get-pre-modules-obarray)) |
|
(set-module-name! m '(guile)) |
|
|
|
|
|
|
|
(set-module-next-unique-id! m (module-generate-unique-id! #f)) |
|
|
|
m)) |
|
|
|
|
|
|
|
|
|
(define the-scm-module |
|
(let ((m (make-module 0))) |
|
(set-module-obarray! m (%get-pre-modules-obarray)) |
|
(set-module-name! m '(guile)) |
|
(set-module-kind! m 'interface) |
|
|
|
|
|
(set-module-public-interface! m m) |
|
|
|
m)) |
|
|
|
(set-module-public-interface! the-root-module the-scm-module) |
|
|
|
|
|
|
|
|
|
|
|
|
|
(define (resolve-module name . args) |
|
(if (equal? name '(guile)) |
|
the-root-module |
|
(error "unexpected module to resolve during module boot" name))) |
|
|
|
(define (module-generate-unique-id! m) |
|
(let ((i (module-next-unique-id m))) |
|
(set-module-next-unique-id! m (+ i 1)) |
|
i)) |
|
|
|
|
|
|
|
|
|
(define define-module* #f) |
|
(define process-use-modules #f) |
|
(define module-export! #f) |
|
(define default-duplicate-binding-procedures #f) |
|
|
|
|
|
|
|
|
|
(set-current-module the-root-module) |
|
|
|
|
|
|
|
|
|
(define (call-with-module-autoload-lock thunk) |
|
|
|
|
|
|
|
|
|
|
|
(thunk)) |
|
|
|
|
|
|
|
(define module-name |
|
(let ((accessor (record-accessor module-type 'name))) |
|
(lambda (mod) |
|
(or (accessor mod) |
|
(let ((name (list (gensym)))) |
|
|
|
|
|
|
|
(set-module-name! mod name) |
|
(call-with-module-autoload-lock |
|
(lambda () |
|
(nested-define-module! (resolve-module '() #f) name mod))) |
|
(accessor mod)))))) |
|
|
|
(define* (module-gensym #:optional (id " mg") (m (current-module))) |
|
"Return a fresh symbol in the context of module M, based on ID (a |
|
string or symbol). As long as M is a valid module, this procedure is |
|
deterministic." |
|
(define (->string number) |
|
(number->string number 16)) |
|
|
|
(if m |
|
(string->symbol |
|
(string-append id "-" |
|
(->string (hash (module-name m) most-positive-fixnum)) |
|
"-" |
|
(->string (module-generate-unique-id! m)))) |
|
(gensym id))) |
|
|
|
(define (make-modules-in module name) |
|
(or (nested-ref-module module name) |
|
(let ((m (make-module))) |
|
(set-module-kind! m 'directory) |
|
(set-module-name! m (append (module-name module) name)) |
|
(nested-define-module! module name m) |
|
m))) |
|
|
|
(define user-modules-declarative? (make-parameter #t)) |
|
|
|
(define (beautify-user-module! module) |
|
(let ((interface (module-public-interface module))) |
|
(if (or (not interface) |
|
(eq? interface module)) |
|
(let ((interface (make-module))) |
|
(set-module-name! interface (module-name module)) |
|
(set-module-version! interface (module-version module)) |
|
(set-module-kind! interface 'interface) |
|
(set-module-public-interface! module interface)))) |
|
(if (and (not (memq the-scm-module (module-uses module))) |
|
(not (eq? module the-root-module))) |
|
|
|
(module-use! module the-scm-module))) |
|
|
|
(define (version-matches? version-ref target) |
|
(define (sub-versions-match? v-refs t) |
|
(define (sub-version-matches? v-ref t) |
|
(let ((matches? (lambda (v) (sub-version-matches? v t)))) |
|
(cond |
|
((number? v-ref) (eqv? v-ref t)) |
|
((list? v-ref) |
|
(case (car v-ref) |
|
((>=) (>= t (cadr v-ref))) |
|
((<=) (<= t (cadr v-ref))) |
|
((and) (and-map matches? (cdr v-ref))) |
|
((or) (or-map matches? (cdr v-ref))) |
|
((not) (not (matches? (cadr v-ref)))) |
|
(else (error "Invalid sub-version reference" v-ref)))) |
|
(else (error "Invalid sub-version reference" v-ref))))) |
|
(or (null? v-refs) |
|
(and (not (null? t)) |
|
(sub-version-matches? (car v-refs) (car t)) |
|
(sub-versions-match? (cdr v-refs) (cdr t))))) |
|
|
|
(let ((matches? (lambda (v) (version-matches? v target)))) |
|
(or (null? version-ref) |
|
(case (car version-ref) |
|
((and) (and-map matches? (cdr version-ref))) |
|
((or) (or-map matches? (cdr version-ref))) |
|
((not) (not (matches? (cadr version-ref)))) |
|
(else (sub-versions-match? version-ref target)))))) |
|
|
|
(define (make-fresh-user-module) |
|
(let ((m (make-module))) |
|
(beautify-user-module! m) |
|
(set-module-declarative?! m (user-modules-declarative?)) |
|
m)) |
|
|
|
|
|
|
|
(define resolve-module |
|
(let ((root (make-module))) |
|
(set-module-name! root '()) |
|
|
|
(module-define-submodule! root 'guile the-root-module) |
|
|
|
(lambda* (name #:optional (autoload #t) (version #f) #:key (ensure #t)) |
|
(call-with-module-autoload-lock |
|
(lambda () |
|
(let ((already (nested-ref-module root name))) |
|
(cond |
|
((and already |
|
(or (not autoload) (module-public-interface already))) |
|
|
|
(if (and version |
|
(not (version-matches? version (module-version already)))) |
|
(error "incompatible module version already loaded" name)) |
|
already) |
|
(autoload |
|
|
|
(try-load-module name version) |
|
(resolve-module name #f #:ensure ensure)) |
|
(else |
|
|
|
|
|
(or already |
|
(and ensure |
|
(make-modules-in root name))))))))))) |
|
|
|
|
|
(define (try-load-module name version) |
|
(try-module-autoload name version)) |
|
|
|
(define (reload-module m) |
|
"Revisit the source file corresponding to the module @var{m}." |
|
(let ((f (module-filename m))) |
|
(if f |
|
(save-module-excursion |
|
(lambda () |
|
|
|
(set-current-module (make-fresh-user-module)) |
|
(primitive-load-path f) |
|
m)) |
|
|
|
(error "unknown file name for module" m)))) |
|
|
|
(define (purify-module! module) |
|
"Removes bindings in MODULE which are inherited from the (guile) module." |
|
(let ((use-list (module-uses module))) |
|
(if (and (pair? use-list) |
|
(eq? (car (last-pair use-list)) the-scm-module)) |
|
(set-module-uses! module (reverse (cdr (reverse use-list))))))) |
|
|
|
(define* (resolve-interface name #:key |
|
(select #f) |
|
(hide '()) |
|
(prefix #f) |
|
(renamer (if prefix |
|
(symbol-prefix-proc prefix) |
|
identity)) |
|
version) |
|
"Return a module that is an interface to the module designated by |
|
NAME. |
|
|
|
`resolve-interface' takes four keyword arguments: |
|
|
|
#:select SELECTION |
|
|
|
SELECTION is a list of binding-specs to be imported; A binding-spec |
|
is either a symbol or a pair of symbols (ORIG . SEEN), where ORIG |
|
is the name in the used module and SEEN is the name in the using |
|
module. Note that SEEN is also passed through RENAMER, below. The |
|
default is to select all bindings. If you specify no selection but |
|
a renamer, only the bindings that already exist in the used module |
|
are made available in the interface. Bindings that are added later |
|
are not picked up. |
|
|
|
#:hide BINDINGS |
|
|
|
BINDINGS is a list of bindings which should not be imported. |
|
|
|
#:prefix PREFIX |
|
|
|
PREFIX is a symbol that will be appended to each exported name. |
|
The default is to not perform any renaming. |
|
|
|
#:renamer RENAMER |
|
|
|
RENAMER is a procedure that takes a symbol and returns its new |
|
name. The default is not perform any renaming. |
|
|
|
Signal \"no code for module\" error if module name is not resolvable |
|
or its public interface is not available. Signal \"no binding\" |
|
error if selected binding does not exist in the used module." |
|
(let* ((module (resolve-module name #t version #:ensure #f)) |
|
(public-i (and module (module-public-interface module)))) |
|
(unless public-i |
|
(error "no code for module" name)) |
|
(if (and (not select) (null? hide) (eq? renamer identity)) |
|
public-i |
|
(let ((custom-i (make-module))) |
|
(set-module-kind! custom-i 'custom-interface) |
|
(set-module-name! custom-i name) |
|
|
|
(for-each (lambda (binding) |
|
(unless (module-local-variable public-i binding) |
|
(error |
|
(simple-format |
|
#f "no binding `~A' to hide in module ~A" |
|
binding name)))) |
|
hide) |
|
(define (maybe-export! src dst var) |
|
(unless (memq src hide) |
|
(let ((name (renamer dst))) |
|
(when (hashq-ref (module-replacements public-i) src) |
|
(hashq-set! (module-replacements custom-i) name #t)) |
|
(module-add! custom-i name var)))) |
|
(cond |
|
(select |
|
(for-each |
|
(lambda (bspec) |
|
(let* ((direct? (symbol? bspec)) |
|
(orig (if direct? bspec (car bspec))) |
|
(seen (if direct? bspec (cdr bspec))) |
|
(var (module-local-variable public-i orig))) |
|
(unless var |
|
(scm-error 'unbound-variable "resolve-interface" |
|
"no binding `~A' in module ~A" (list orig name) |
|
#f)) |
|
(maybe-export! orig seen var))) |
|
select)) |
|
(else |
|
|
|
|
|
(module-for-each (lambda (sym var) |
|
(maybe-export! sym sym var)) |
|
public-i))) |
|
custom-i)))) |
|
|
|
(define (symbol-prefix-proc prefix) |
|
(lambda (symbol) |
|
(symbol-append prefix symbol))) |
|
|
|
|
|
|
|
|
|
(define* (define-module* name |
|
#:key filename pure version (imports '()) (exports '()) |
|
(replacements '()) (re-exports '()) (re-export-replacements '()) |
|
(autoloads '()) (duplicates #f) transformer declarative? |
|
inlinable-exports) |
|
(define (list-of pred l) |
|
(or (null? l) |
|
(and (pair? l) (pred (car l)) (list-of pred (cdr l))))) |
|
(define (valid-import? x) |
|
(list? x)) |
|
(define (valid-export? x) |
|
(or (symbol? x) (and (pair? x) (symbol? (car x)) (symbol? (cdr x))))) |
|
(define (valid-autoload? x) |
|
(and (pair? x) (list-of symbol? (car x)) (list-of symbol? (cdr x)))) |
|
|
|
|
|
|
|
|
|
(let ((module (resolve-module name #f))) |
|
(beautify-user-module! module) |
|
(set-module-declarative?! module declarative?) |
|
(when filename |
|
(set-module-filename! module filename)) |
|
(when pure |
|
(purify-module! module)) |
|
(when version |
|
(unless (list-of integer? version) |
|
(error "expected list of integers for version")) |
|
(set-module-version! module version) |
|
(set-module-version! (module-public-interface module) version)) |
|
(call-with-deferred-observers |
|
(lambda () |
|
(unless (list-of valid-import? imports) |
|
(error "expected imports to be a list of import specifications")) |
|
(unless (list-of valid-export? exports) |
|
(error "expected exports to be a list of symbols or symbol pairs")) |
|
(unless (list-of valid-export? replacements) |
|
(error "expected replacements to be a list of symbols or symbol pairs")) |
|
(unless (list-of valid-export? re-exports) |
|
(error "expected re-exports to be a list of symbols or symbol pairs")) |
|
(module-export! module exports) |
|
(module-replace! module replacements) |
|
(unless (null? imports) |
|
(let ((imports (map (lambda (import-spec) |
|
(apply resolve-interface import-spec)) |
|
imports))) |
|
(module-use-interfaces! module imports))) |
|
(module-re-export! module re-exports) |
|
(module-re-export! module re-export-replacements #:replace? #t) |
|
|
|
(apply module-autoload! module autoloads) |
|
(let ((duplicates (or duplicates |
|
|
|
|
|
(and (not (module-duplicates-handlers module)) |
|
|
|
|
|
|
|
'(replace warn-override-core warn last))))) |
|
(when duplicates |
|
(let ((handlers (lookup-duplicates-handlers duplicates))) |
|
(set-module-duplicates-handlers! module handlers)))))) |
|
|
|
(when transformer |
|
(unless (and (pair? transformer) (list-of symbol? transformer)) |
|
(error "expected transformer to be a module name" transformer)) |
|
(let ((iface (resolve-interface transformer)) |
|
(sym (car (last-pair transformer)))) |
|
(set-module-transformer! module (module-ref iface sym)))) |
|
|
|
(when inlinable-exports |
|
(unless (procedure? inlinable-exports) |
|
(error "expected inlinable-exports to be a procedure" inlinable-exports)) |
|
(set-module-inlinable-exports! (module-public-interface module) |
|
inlinable-exports)) |
|
|
|
(run-hook module-defined-hook module) |
|
module)) |
|
|
|
|
|
|
|
|
|
(define module-defined-hook (make-hook 1)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
(define (make-autoload-interface module name bindings) |
|
(let ((b (lambda (a sym definep) |
|
(false-if-exception |
|
(and (memq sym bindings) |
|
(let ((i (resolve-interface name #:select bindings))) |
|
(unless i |
|
(error "missing interface for module" name)) |
|
(let ((uses (memq a (module-uses module)))) |
|
(when uses |
|
|
|
|
|
(set-car! uses i))) |
|
(for-each |
|
(lambda (name) |
|
(when (hashq-ref (module-replacements i) name) |
|
(hashq-set! (module-replacements a) name #t))) |
|
bindings) |
|
(or (module-local-variable i sym) |
|
(error "binding not presentin module" name sym)))) |
|
#:warning "Failed to autoload ~a in ~a:\n" sym name)))) |
|
(module-constructor (make-hash-table 0) '() b #f #f name 'autoload #f |
|
(make-hash-table 0) '() (make-weak-value-hash-table) #f |
|
(make-hash-table 0) #f #f #f 0 (make-hash-table 0) #f))) |
|
|
|
(define (module-autoload! module . args) |
|
"Have @var{module} automatically load the module named @var{name} when one |
|
of the symbols listed in @var{bindings} is looked up. @var{args} should be a |
|
list of module-name/binding-list pairs, e.g., as in @code{(module-autoload! |
|
module '(ice-9 q) '(make-q q-length))}." |
|
(let loop ((args args)) |
|
(cond ((null? args) |
|
#t) |
|
((null? (cdr args)) |
|
(error "invalid name+binding autoload list" args)) |
|
(else |
|
(let ((name (car args)) |
|
(bindings (cadr args))) |
|
(module-use! module (make-autoload-interface module |
|
name bindings)) |
|
(loop (cddr args))))))) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define autoloads-in-progress '()) |
|
|
|
|
|
|
|
|
|
(define* (try-module-autoload module-name #:optional version) |
|
"Try to load a module of the given name. If it is not found, return |
|
#f. Otherwise return #t. May raise an exception if a file is found, |
|
but it fails to load." |
|
(let* ((reverse-name (reverse module-name)) |
|
(name (symbol->string (car reverse-name))) |
|
(dir-hint-module-name (reverse (cdr reverse-name))) |
|
(dir-hint (apply string-append |
|
(map (lambda (elt) |
|
(string-append (symbol->string elt) |
|
file-name-separator-string)) |
|
dir-hint-module-name)))) |
|
(resolve-module dir-hint-module-name #f) |
|
|
|
(call-with-module-autoload-lock |
|
(lambda () |
|
(and (not (autoload-done-or-in-progress? dir-hint name)) |
|
(let ((didit #f)) |
|
(dynamic-wind |
|
(lambda () (autoload-in-progress! dir-hint name)) |
|
(lambda () |
|
(with-fluids ((current-reader #f)) |
|
(save-module-excursion |
|
(lambda () |
|
(define (call/ec proc) |
|
(let ((tag (make-prompt-tag))) |
|
(call-with-prompt |
|
tag |
|
(lambda () |
|
(proc (lambda () (abort-to-prompt tag)))) |
|
(lambda (k) (values))))) |
|
|
|
|
|
(set-current-module (make-fresh-user-module)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
(call/ec |
|
(lambda (abort) |
|
(primitive-load-path (in-vicinity dir-hint name) |
|
abort) |
|
(set! didit #t))))))) |
|
(lambda () (set-autoloaded! dir-hint name didit))) |
|
didit)))))) |
|
|
|
|
|
|
|
|
|
|
|
|
|
(define autoloads-done '((guile . guile))) |
|
|
|
(define (autoload-done-or-in-progress? p m) |
|
(let ((n (cons p m))) |
|
(->bool (or (member n autoloads-done) |
|
(member n autoloads-in-progress))))) |
|
|
|
(define (autoload-done! p m) |
|
(let ((n (cons p m))) |
|
(set! autoloads-in-progress |
|
(delete! n autoloads-in-progress)) |
|
(or (member n autoloads-done) |
|
(set! autoloads-done (cons n autoloads-done))))) |
|
|
|
(define (autoload-in-progress! p m) |
|
(let ((n (cons p m))) |
|
(set! autoloads-done |
|
(delete! n autoloads-done)) |
|
(set! autoloads-in-progress (cons n autoloads-in-progress)))) |
|
|
|
(define (set-autoloaded! p m done?) |
|
(if done? |
|
(autoload-done! p m) |
|
(let ((n (cons p m))) |
|
(set! autoloads-done (delete! n autoloads-done)) |
|
(set! autoloads-in-progress (delete! n autoloads-in-progress))))) |
|
|
|
|
|
|
|
|
|
|
|
|
|
(define-syntax define-option-interface |
|
(syntax-rules () |
|
((_ (interface (options enable disable) (option-set!))) |
|
(begin |
|
(define options |
|
(case-lambda |
|
(() (interface)) |
|
((arg) |
|
(if (list? arg) |
|
(begin (interface arg) (interface)) |
|
(for-each |
|
(lambda (option) |
|
(apply (lambda (name value documentation) |
|
(display name) |
|
(let ((len (string-length (symbol->string name)))) |
|
(when (< len 16) |
|
(display #\tab) |
|
(when (< len 8) |
|
(display #\tab)))) |
|
(display #\tab) |
|
(display value) |
|
(display #\tab) |
|
(display documentation) |
|
(newline)) |
|
option)) |
|
(interface #t)))))) |
|
(define (enable . flags) |
|
(interface (append flags (interface))) |
|
(interface)) |
|
(define (disable . flags) |
|
(let ((options (interface))) |
|
(for-each (lambda (flag) (set! options (delq! flag options))) |
|
flags) |
|
(interface options) |
|
(interface))) |
|
(define-syntax-rule (option-set! opt val) |
|
(eval-when (expand load eval) |
|
(options (append (options) (list 'opt val))))))))) |
|
|
|
(define-option-interface |
|
(debug-options-interface |
|
(debug-options debug-enable debug-disable) |
|
(debug-set!))) |
|
|
|
(define-option-interface |
|
(read-options-interface |
|
(read-options read-enable read-disable) |
|
(read-set!))) |
|
|
|
(define-option-interface |
|
(print-options-interface |
|
(print-options print-enable print-disable) |
|
(print-set!))) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define-syntax *unspecified* |
|
(identifier-syntax (if #f #f))) |
|
|
|
(define (unspecified? v) (eq? v *unspecified*)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define *repl-stack* (make-fluid '())) |
|
|
|
|
|
|
|
|
|
|
|
(define (batch-mode?) |
|
(null? (fluid-ref *repl-stack*))) |
|
|
|
|
|
|
|
|
|
|
|
(define (ensure-batch-mode!) |
|
(set! batch-mode? (lambda () #t))) |
|
|
|
(define (quit . args) |
|
(apply throw 'quit args)) |
|
|
|
(define exit quit) |
|
|
|
(define (gc-run-time) |
|
(cdr (assq 'gc-time-taken (gc-stats)))) |
|
|
|
(define abort-hook (make-hook)) |
|
(define before-error-hook (make-hook)) |
|
(define after-error-hook (make-hook)) |
|
(define before-backtrace-hook (make-hook)) |
|
(define after-backtrace-hook (make-hook)) |
|
|
|
(define before-read-hook (make-hook)) |
|
(define after-read-hook (make-hook)) |
|
(define before-eval-hook (make-hook 1)) |
|
(define after-eval-hook (make-hook 1)) |
|
(define before-print-hook (make-hook 1)) |
|
(define after-print-hook (make-hook 1)) |
|
|
|
|
|
|
|
(define exit-hook (make-hook)) |
|
|
|
|
|
|
|
(define repl-reader |
|
(lambda* (prompt #:optional (reader (fluid-ref current-reader))) |
|
(if (not (char-ready?)) |
|
(begin |
|
(display (if (string? prompt) prompt (prompt))) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(set-port-column! (current-output-port) 0))) |
|
(force-output) |
|
(run-hook before-read-hook) |
|
((or reader read) (current-input-port)))) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define-syntax while |
|
(lambda (x) |
|
(syntax-case x () |
|
((while cond body ...) |
|
#`(let ((break-tag (make-prompt-tag "break")) |
|
(continue-tag (make-prompt-tag "continue"))) |
|
(call-with-prompt |
|
break-tag |
|
(lambda () |
|
(define-syntax #,(datum->syntax #'while 'break) |
|
(lambda (x) |
|
(syntax-case x () |
|
((_ arg (... ...)) |
|
#'(abort-to-prompt break-tag arg (... ...))) |
|
(_ |
|
#'(lambda args |
|
(apply abort-to-prompt break-tag args)))))) |
|
(let lp () |
|
(call-with-prompt |
|
continue-tag |
|
(lambda () |
|
(define-syntax #,(datum->syntax #'while 'continue) |
|
(lambda (x) |
|
(syntax-case x () |
|
((_) |
|
#'(abort-to-prompt continue-tag)) |
|
((_ . args) |
|
(syntax-violation 'continue "too many arguments" x)) |
|
(_ |
|
#'(lambda () |
|
(abort-to-prompt continue-tag)))))) |
|
(do () ((not cond) #f) body ...)) |
|
(lambda (k) (lp))))) |
|
(lambda (k . args) |
|
(if (null? args) |
|
#t |
|
(apply values args))))))))) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(eval-when (expand) |
|
(if (memq 'prefix (read-options)) |
|
(error "boot-9 must be compiled with #:kw, not :kw"))) |
|
|
|
(define (keyword-like-symbol->keyword sym) |
|
(symbol->keyword (string->symbol (substring (symbol->string sym) 1)))) |
|
|
|
(define-syntax define-module |
|
(lambda (x) |
|
(define (keyword-like? stx) |
|
(let ((dat (syntax->datum stx))) |
|
(and (symbol? dat) |
|
(eqv? (string-ref (symbol->string dat) 0) #\:)))) |
|
(define (->keyword sym) |
|
(symbol->keyword (string->symbol (substring (symbol->string sym) 1)))) |
|
|
|
(define (parse-iface args) |
|
(let loop ((in args) (out '())) |
|
(syntax-case in () |
|
(() (reverse! out)) |
|
|
|
((sym . in) (keyword-like? #'sym) |
|
(loop #`(#,(->keyword (syntax->datum #'sym)) . in) out)) |
|
((kw . in) (not (keyword? (syntax->datum #'kw))) |
|
(syntax-violation 'define-module "expected keyword arg" x #'kw)) |
|
((#:renamer renamer . in) |
|
(loop #'in (cons* #',renamer #:renamer out))) |
|
((kw val . in) |
|
(loop #'in (cons* #'val #'kw out)))))) |
|
|
|
(define (parse args imp exp rex rep rxp aut dec) |
|
|
|
|
|
|
|
|
|
|
|
(syntax-case args () |
|
(() |
|
(let ((imp (if (null? imp) '() #`(#:imports `#,imp))) |
|
(exp (if (null? exp) '() #`(#:exports '#,exp))) |
|
(rex (if (null? rex) '() #`(#:re-exports '#,rex))) |
|
(rep (if (null? rep) '() #`(#:replacements '#,rep))) |
|
(rxp (if (null? rxp) '() #`(#:re-export-replacements '#,rxp))) |
|
(aut (if (null? aut) '() #`(#:autoloads '#,aut))) |
|
(dec (if dec '() #`(#:declarative? |
|
#,(user-modules-declarative?))))) |
|
#`(#,@imp #,@exp #,@rex #,@rep #,@rxp #,@aut #,@dec))) |
|
|
|
((sym . args) (keyword-like? #'sym) |
|
(parse #`(#,(->keyword (syntax->datum #'sym)) . args) |
|
imp exp rex rep rxp aut dec)) |
|
((kw . args) (not (keyword? (syntax->datum #'kw))) |
|
(syntax-violation 'define-module "expected keyword arg" x #'kw)) |
|
((#:no-backtrace . args) |
|
|
|
(parse #'args imp exp rex rep rxp aut dec)) |
|
((#:pure . args) |
|
#`(#:pure #t . #,(parse #'args imp exp rex rep rxp aut dec))) |
|
((kw) |
|
(syntax-violation 'define-module "keyword arg without value" x #'kw)) |
|
((#:version (v ...) . args) |
|
#`(#:version '(v ...) . #,(parse #'args imp exp rex rep rxp aut dec))) |
|
((#:duplicates (d ...) . args) |
|
#`(#:duplicates '(d ...) . #,(parse #'args imp exp rex rep rxp aut dec))) |
|
((#:filename f . args) |
|
#`(#:filename 'f . #,(parse #'args imp exp rex rep rxp aut dec))) |
|
((#:declarative? d . args) |
|
#`(#:declarative? 'd . #,(parse #'args imp exp rex rep rxp aut #t))) |
|
((#:use-module (name name* ...) . args) |
|
(and (and-map symbol? (syntax->datum #'(name name* ...)))) |
|
(parse #'args #`(#,@imp ((name name* ...))) exp rex rep rxp aut dec)) |
|
((#:use-syntax (name name* ...) . args) |
|
(and (and-map symbol? (syntax->datum #'(name name* ...)))) |
|
#`(#:transformer '(name name* ...) |
|
. #,(parse #'args #`(#,@imp ((name name* ...))) exp rex |
|
rep rxp aut dec))) |
|
((#:use-module ((name name* ...) arg ...) . args) |
|
(and (and-map symbol? (syntax->datum #'(name name* ...)))) |
|
(parse #'args |
|
#`(#,@imp ((name name* ...) #,@(parse-iface #'(arg ...)))) |
|
exp rex rep rxp aut dec)) |
|
((#:export (ex ...) . args) |
|
(parse #'args imp #`(#,@exp ex ...) rex rep rxp aut dec)) |
|
((#:export-syntax (ex ...) . args) |
|
(parse #'args imp #`(#,@exp ex ...) rex rep rxp aut dec)) |
|
((#:re-export (re ...) . args) |
|
(parse #'args imp exp #`(#,@rex re ...) rep rxp aut dec)) |
|
((#:re-export-syntax (re ...) . args) |
|
(parse #'args imp exp #`(#,@rex re ...) rep rxp aut dec)) |
|
((#:replace (r ...) . args) |
|
(parse #'args imp exp rex #`(#,@rep r ...) rxp aut dec)) |
|
((#:replace-syntax (r ...) . args) |
|
(parse #'args imp exp rex #`(#,@rep r ...) rxp aut dec)) |
|
((#:re-export-and-replace (r ...) . args) |
|
(parse #'args imp exp rex rep #`(#,@rxp r ...) aut dec)) |
|
((#:autoload name bindings . args) |
|
(parse #'args imp exp rex rep rxp #`(#,@aut name bindings) dec)) |
|
((kw val . args) |
|
(syntax-violation 'define-module "unknown keyword or bad argument" |
|
#'kw #'val)))) |
|
|
|
(syntax-case x () |
|
((_ (name name* ...) arg ...) |
|
(and-map symbol? (syntax->datum #'(name name* ...))) |
|
(with-syntax (((quoted-arg ...) |
|
(parse #'(arg ...) '() '() '() '() '() '() #f)) |
|
|
|
|
|
|
|
|
|
(filename (let ((f (assq-ref (or (syntax-source x) '()) |
|
'filename))) |
|
(and (string? f) f)))) |
|
#'(eval-when (expand load eval) |
|
(let ((m (define-module* '(name name* ...) |
|
#:filename filename quoted-arg ...))) |
|
(set-current-module m) |
|
m))))))) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define (process-use-modules module-interface-args) |
|
(let ((interfaces (map (lambda (mif-args) |
|
(or (apply resolve-interface mif-args) |
|
(error "no such module" mif-args))) |
|
module-interface-args))) |
|
(call-with-deferred-observers |
|
(lambda () |
|
(module-use-interfaces! (current-module) interfaces))))) |
|
|
|
(define-syntax use-modules |
|
(lambda (x) |
|
(define (keyword-like? stx) |
|
(let ((dat (syntax->datum stx))) |
|
(and (symbol? dat) |
|
(eqv? (string-ref (symbol->string dat) 0) #\:)))) |
|
(define (->keyword sym) |
|
(symbol->keyword (string->symbol (substring (symbol->string sym) 1)))) |
|
|
|
(define (quotify-iface args) |
|
(let loop ((in args) (out '())) |
|
(syntax-case in () |
|
(() (reverse! out)) |
|
|
|
((sym . in) (keyword-like? #'sym) |
|
(loop #`(#,(->keyword (syntax->datum #'sym)) . in) out)) |
|
((kw . in) (not (keyword? (syntax->datum #'kw))) |
|
(syntax-violation 'define-module "expected keyword arg" x #'kw)) |
|
((#:renamer renamer . in) |
|
(loop #'in (cons* #'renamer #:renamer out))) |
|
((kw val . in) |
|
(loop #'in (cons* #''val #'kw out)))))) |
|
|
|
(define (quotify specs) |
|
(let lp ((in specs) (out '())) |
|
(syntax-case in () |
|
(() (reverse out)) |
|
(((name name* ...) . in) |
|
(and-map symbol? (syntax->datum #'(name name* ...))) |
|
(lp #'in (cons #''((name name* ...)) out))) |
|
((((name name* ...) arg ...) . in) |
|
(and-map symbol? (syntax->datum #'(name name* ...))) |
|
(with-syntax (((quoted-arg ...) (quotify-iface #'(arg ...)))) |
|
(lp #'in (cons #`(list '(name name* ...) quoted-arg ...) |
|
out))))))) |
|
|
|
(syntax-case x () |
|
((_ spec ...) |
|
(with-syntax (((quoted-args ...) (quotify #'(spec ...)))) |
|
#'(eval-when (expand load eval) |
|
(process-use-modules (list quoted-args ...)) |
|
*unspecified*)))))) |
|
|
|
(include-from-path "ice-9/r6rs-libraries") |
|
(include-from-path "ice-9/r7rs-libraries") |
|
|
|
(define-syntax-rule (define-private foo bar) |
|
(define foo bar)) |
|
|
|
(define-syntax define-public |
|
(syntax-rules () |
|
((_ (name . args) . body) |
|
(begin |
|
(define (name . args) . body) |
|
(export name))) |
|
((_ name val) |
|
(begin |
|
(define name val) |
|
(export name))))) |
|
|
|
(define-syntax-rule (defmacro-public name args body ...) |
|
(begin |
|
(defmacro name args body ...) |
|
(export-syntax name))) |
|
|
|
|
|
(define-syntax-rule (λ formals body ...) |
|
(lambda formals body ...)) |
|
|
|
|
|
|
|
|
|
|
|
(define* (module-export! m names #:key replace?) |
|
"Export a local variable." |
|
(let ((public-i (module-public-interface m))) |
|
(for-each (lambda (name) |
|
(let* ((internal-name (if (pair? name) (car name) name)) |
|
(external-name (if (pair? name) (cdr name) name)) |
|
(var (module-ensure-local-variable! m internal-name))) |
|
(when replace? |
|
(hashq-set! (module-replacements public-i) external-name #t)) |
|
(module-add! public-i external-name var))) |
|
names))) |
|
|
|
(define (module-replace! m names) |
|
(module-export! m names #:replace? #t)) |
|
|
|
(define (module-export-all! mod) |
|
"Export all local variables from a module." |
|
(define (fresh-interface!) |
|
(let ((iface (make-module))) |
|
(set-module-name! iface (module-name mod)) |
|
(set-module-version! iface (module-version mod)) |
|
(set-module-kind! iface 'interface) |
|
(set-module-public-interface! mod iface) |
|
iface)) |
|
(let ((iface (or (module-public-interface mod) |
|
(fresh-interface!)))) |
|
(set-module-obarray! iface (module-obarray mod)))) |
|
|
|
(define* (module-re-export! m names #:key replace?) |
|
"Re-export an imported variable." |
|
(let ((public-i (module-public-interface m))) |
|
(for-each |
|
(lambda (name) |
|
(let* ((internal-name (if (pair? name) (car name) name)) |
|
(external-name (if (pair? name) (cdr name) name)) |
|
(var (module-variable m internal-name))) |
|
(cond |
|
((not var) |
|
(error "Undefined variable:" internal-name)) |
|
((eq? var (module-local-variable m internal-name)) |
|
(error "re-exporting local variable:" internal-name)) |
|
(else |
|
(when replace? |
|
(hashq-set! (module-replacements public-i) external-name #t)) |
|
(module-add! public-i external-name var))))) |
|
names))) |
|
|
|
(define-syntax-rule (export name ...) |
|
(eval-when (expand load eval) |
|
(call-with-deferred-observers |
|
(lambda () |
|
(module-export! (current-module) '(name ...)))))) |
|
|
|
(define-syntax-rule (re-export name ...) |
|
(eval-when (expand load eval) |
|
(call-with-deferred-observers |
|
(lambda () |
|
(module-re-export! (current-module) '(name ...)))))) |
|
|
|
(define-syntax-rule (export! name ...) |
|
(eval-when (expand load eval) |
|
(call-with-deferred-observers |
|
(lambda () |
|
(module-replace! (current-module) '(name ...)))))) |
|
|
|
(define-syntax-rule (export-syntax name ...) |
|
(export name ...)) |
|
|
|
(define-syntax-rule (re-export-syntax name ...) |
|
(re-export name ...)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
(define* (make-mutable-parameter init #:optional (converter identity)) |
|
(let ((fluid (make-fluid (converter init)))) |
|
(case-lambda |
|
(() (fluid-ref fluid)) |
|
((val) (fluid-set! fluid (converter val)))))) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define duplicate-handlers |
|
(let ((m (make-module))) |
|
|
|
(define (check module name int1 val1 int2 val2 var val) |
|
(scm-error 'misc-error |
|
#f |
|
"~A: `~A' imported from both ~A and ~A" |
|
(list (module-name module) |
|
name |
|
(module-name int1) |
|
(module-name int2)) |
|
#f)) |
|
|
|
(define (warn module name int1 val1 int2 val2 var val) |
|
(format (current-warning-port) |
|
"WARNING: ~A: `~A' imported from both ~A and ~A\n" |
|
(module-name module) |
|
name |
|
(module-name int1) |
|
(module-name int2)) |
|
#f) |
|
|
|
(define (replace module name int1 val1 int2 val2 var val) |
|
(let* ((replace1 (hashq-ref (module-replacements int1) name)) |
|
(replace2 (hashq-ref (module-replacements int2) name)) |
|
(old (or (and replace1 var) |
|
(module-variable int1 name))) |
|
(new (module-variable int2 name))) |
|
(if replace1 |
|
(and (or (eq? old new) (not replace2)) |
|
old) |
|
(and replace2 new)))) |
|
|
|
(define (warn-override-core module name int1 val1 int2 val2 var val) |
|
(and (eq? int1 the-scm-module) |
|
(begin |
|
(format (current-warning-port) |
|
"WARNING: ~A: imported module ~A overrides core binding `~A'\n" |
|
(module-name module) |
|
(module-name int2) |
|
name) |
|
(module-variable int2 name)))) |
|
|
|
(define (first module name int1 val1 int2 val2 var val) |
|
(or var (module-variable int1 name))) |
|
|
|
(define (last module name int1 val1 int2 val2 var val) |
|
(module-variable int2 name)) |
|
|
|
(define (noop module name int1 val1 int2 val2 var val) |
|
#f) |
|
|
|
(set-module-name! m 'duplicate-handlers) |
|
(set-module-kind! m 'interface) |
|
(module-define! m 'check check) |
|
(module-define! m 'warn warn) |
|
(module-define! m 'replace replace) |
|
(module-define! m 'warn-override-core warn-override-core) |
|
(module-define! m 'first first) |
|
(module-define! m 'last last) |
|
(module-define! m 'merge-generics noop) |
|
(module-define! m 'merge-accessors noop) |
|
m)) |
|
|
|
(define (lookup-duplicates-handlers handler-names) |
|
(and handler-names |
|
(map (lambda (handler-name) |
|
(or (module-symbol-local-binding |
|
duplicate-handlers handler-name #f) |
|
(error "invalid duplicate handler name:" |
|
handler-name))) |
|
(if (list? handler-names) |
|
handler-names |
|
(list handler-names))))) |
|
|
|
(define default-duplicate-binding-procedures |
|
(case-lambda |
|
(() |
|
(or (module-duplicates-handlers (current-module)) |
|
|
|
|
|
(lookup-duplicates-handlers |
|
'(replace warn-override-core warn last)))) |
|
((procs) |
|
(set-module-duplicates-handlers! (current-module) procs)))) |
|
|
|
(define default-duplicate-binding-handler |
|
(case-lambda |
|
(() |
|
(map procedure-name (default-duplicate-binding-procedures))) |
|
((handlers) |
|
(default-duplicate-binding-procedures |
|
(lookup-duplicates-handlers handlers))))) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define %auto-compilation-options |
|
|
|
'(#:warnings (shadowed-toplevel use-before-definition arity-mismatch |
|
format duplicate-case-datum bad-case-datum |
|
non-idempotent-definition))) |
|
|
|
(define* (load-in-vicinity dir file-name #:optional reader) |
|
"Load source file FILE-NAME in vicinity of directory DIR. Use a |
|
pre-compiled version of FILE-NAME when available, and auto-compile one |
|
when none is available, reading FILE-NAME with READER." |
|
|
|
|
|
|
|
|
|
|
|
(define (canonical->suffix canon) |
|
(cond |
|
((and (not (string-null? canon)) |
|
(file-name-separator? (string-ref canon 0))) |
|
canon) |
|
((and (eq? (system-file-name-convention) 'windows) |
|
(absolute-file-name? canon)) |
|
|
|
|
|
|
|
(string-append file-name-separator-string |
|
(substring canon 0 1) |
|
(substring canon 2))) |
|
(else canon))) |
|
|
|
(define compiled-extension |
|
|
|
(cond ((or (null? %load-compiled-extensions) |
|
(string-null? (car %load-compiled-extensions))) |
|
(warn "invalid %load-compiled-extensions" |
|
%load-compiled-extensions) |
|
".go") |
|
(else (car %load-compiled-extensions)))) |
|
|
|
(define (more-recent? stat1 stat2) |
|
|
|
(or (> (stat:mtime stat1) (stat:mtime stat2)) |
|
(and (= (stat:mtime stat1) (stat:mtime stat2)) |
|
(>= (stat:mtimensec stat1) |
|
(stat:mtimensec stat2))))) |
|
|
|
(define (fallback-file-name canon-file-name) |
|
|
|
|
|
|
|
|
|
|
|
|
|
(and %compile-fallback-path |
|
(string-append %compile-fallback-path |
|
(canonical->suffix canon-file-name) |
|
compiled-extension))) |
|
|
|
(define (compile file) |
|
|
|
((module-ref (resolve-interface '(system base compile)) |
|
'compile-file) |
|
file |
|
#:opts %auto-compilation-options |
|
#:env (current-module))) |
|
|
|
(define (load-thunk-from-file file) |
|
(let ((loader (resolve-interface '(system vm loader)))) |
|
((module-ref loader 'load-thunk-from-file) file))) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define (fresh-compiled-thunk name scmstat go-file-name) |
|
|
|
|
|
|
|
(false-if-exception |
|
(let ((gostat (and (not %fresh-auto-compile) |
|
(stat go-file-name #f)))) |
|
(if (and gostat (more-recent? gostat scmstat)) |
|
(load-thunk-from-file go-file-name) |
|
(begin |
|
(when gostat |
|
(format (current-warning-port) |
|
";;; note: source file ~a\n;;; newer than compiled ~a\n" |
|
name go-file-name)) |
|
(cond |
|
(%load-should-auto-compile |
|
(%warn-auto-compilation-enabled) |
|
(format (current-warning-port) ";;; compiling ~a\n" name) |
|
(let ((cfn (compile name))) |
|
(format (current-warning-port) ";;; compiled ~a\n" cfn) |
|
(load-thunk-from-file cfn))) |
|
(else #f))))) |
|
#:warning "WARNING: compilation of ~a failed:\n" name)) |
|
|
|
(define (sans-extension file) |
|
(let ((dot (string-rindex file #\.))) |
|
(if dot |
|
(substring file 0 dot) |
|
file))) |
|
|
|
(define (load-absolute abs-file-name) |
|
|
|
|
|
(define scmstat |
|
(false-if-exception |
|
(stat abs-file-name) |
|
#:warning "Stat of ~a failed:\n" abs-file-name)) |
|
|
|
(define (pre-compiled) |
|
(or-map |
|
(lambda (dir) |
|
(or-map |
|
(lambda (ext) |
|
(let ((candidate (string-append (in-vicinity dir file-name) ext))) |
|
(let ((gostat (stat candidate #f))) |
|
(and gostat |
|
(more-recent? gostat scmstat) |
|
(false-if-exception |
|
(load-thunk-from-file candidate) |
|
#:warning "WARNING: failed to load compiled file ~a:\n" |
|
candidate))))) |
|
%load-compiled-extensions)) |
|
%load-compiled-path)) |
|
|
|
(define (fallback) |
|
(and=> (false-if-exception (canonicalize-path abs-file-name)) |
|
(lambda (canon) |
|
(and=> (fallback-file-name canon) |
|
(lambda (go-file-name) |
|
(fresh-compiled-thunk abs-file-name |
|
scmstat |
|
go-file-name)))))) |
|
|
|
(let ((compiled (and scmstat (or (pre-compiled) (fallback))))) |
|
(if compiled |
|
(begin |
|
(if %load-hook |
|
(%load-hook abs-file-name)) |
|
(compiled)) |
|
(start-stack 'load-stack |
|
(primitive-load abs-file-name))))) |
|
|
|
(save-module-excursion |
|
(lambda () |
|
(with-fluids ((current-reader reader) |
|
(%file-port-name-canonicalization 'relative)) |
|
(cond |
|
((absolute-file-name? file-name) |
|
(load-absolute file-name)) |
|
((absolute-file-name? dir) |
|
(load-absolute (in-vicinity dir file-name))) |
|
(else |
|
(load-from-path (in-vicinity dir file-name)))))))) |
|
|
|
(define-syntax load |
|
(make-variable-transformer |
|
(lambda (x) |
|
(let* ((src (syntax-source x)) |
|
(file (and src (assq-ref src 'filename))) |
|
(dir (and (string? file) (dirname file)))) |
|
|
|
(when (module-declarative? (current-module)) |
|
(format (current-warning-port) |
|
"WARNING: Use of `load' in declarative module ~A. ~A\n" |
|
(module-name (current-module)) |
|
"Add #:declarative? #f to your define-module invocation.") |
|
(set-module-declarative?! (current-module) #f)) |
|
(syntax-case x () |
|
((_ arg ...) |
|
#`(load-in-vicinity #,(or dir #'(getcwd)) arg ...)) |
|
(id |
|
(identifier? #'id) |
|
#`(lambda args |
|
(apply load-in-vicinity #,(or dir #'(getcwd)) args)))))))) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define %cond-expand-features |
|
|
|
|
|
|
|
'(guile |
|
guile-2 |
|
guile-2.2 |
|
guile-3 |
|
guile-3.0 |
|
r5rs |
|
r6rs |
|
r7rs |
|
exact-closed ieee-float full-unicode ratios ;; R7RS features. |
|
srfi-0 ;; cond-expand itself |
|
srfi-4 ;; homogeneous numeric vectors |
|
srfi-6 ;; string ports |
|
srfi-13 ;; string library |
|
srfi-14 ;; character sets |
|
srfi-16 ;; case-lambda |
|
srfi-23 ;; `error` procedure |
|
srfi-30 ;; nested multi-line comments |
|
srfi-39 ;; parameterize |
|
srfi-46 ;; basic syntax-rules extensions |
|
srfi-55 ;; require-extension |
|
srfi-61 ;; general cond clause |
|
srfi-62 ;; s-expression comments |
|
srfi-87 ;; => in case clauses |
|
srfi-105 ;; curly infix expressions |
|
)) |
|
|
|
|
|
|
|
(define %cond-expand-table (make-hash-table)) |
|
|
|
|
|
|
|
|
|
(define (cond-expand-provide module features) |
|
(let ((mod (module-public-interface module))) |
|
(and mod |
|
(hashq-set! %cond-expand-table mod |
|
(append (hashq-ref %cond-expand-table mod '()) |
|
features))))) |
|
|
|
(define-syntax cond-expand |
|
(lambda (x) |
|
(define (module-has-feature? mod sym) |
|
(or-map (lambda (mod) |
|
(memq sym (hashq-ref %cond-expand-table mod '()))) |
|
(module-uses mod))) |
|
|
|
(define (condition-matches? condition) |
|
(syntax-case condition (and or not) |
|
((and c ...) |
|
(and-map condition-matches? #'(c ...))) |
|
((or c ...) |
|
(or-map condition-matches? #'(c ...))) |
|
((not c) |
|
(if (condition-matches? #'c) #f #t)) |
|
(c |
|
(identifier? #'c) |
|
(let ((sym (syntax->datum #'c))) |
|
(if (memq sym %cond-expand-features) |
|
#t |
|
(module-has-feature? (current-module) sym)))))) |
|
|
|
(define (match clauses alternate) |
|
(syntax-case clauses () |
|
(((condition form ...) . rest) |
|
(if (condition-matches? #'condition) |
|
#'(begin form ...) |
|
(match #'rest alternate))) |
|
(() (alternate)))) |
|
|
|
(syntax-case x (else) |
|
((_ clause ... (else form ...)) |
|
(match #'(clause ...) |
|
(lambda () |
|
#'(begin form ...)))) |
|
((_ clause ...) |
|
(match #'(clause ...) |
|
(lambda () |
|
(syntax-violation 'cond-expand "unfulfilled cond-expand" x))))))) |
|
|
|
|
|
|
|
|
|
(define (use-srfis srfis) |
|
(process-use-modules |
|
(map (lambda (num) |
|
(list (list 'srfi (string->symbol |
|
(string-append "srfi-" (number->string num)))))) |
|
srfis))) |
|
|
|
|
|
|
|
|
|
|
|
|
|
(define-syntax require-extension |
|
(lambda (x) |
|
(syntax-case x (srfi) |
|
((_ (srfi n ...)) |
|
(and-map integer? (syntax->datum #'(n ...))) |
|
(with-syntax |
|
(((srfi-n ...) |
|
(map (lambda (n) |
|
(datum->syntax x (symbol-append 'srfi- n))) |
|
(map string->symbol |
|
(map number->string (syntax->datum #'(n ...))))))) |
|
#'(use-modules (srfi srfi-n) ...))) |
|
((_ (type arg ...)) |
|
(identifier? #'type) |
|
(syntax-violation 'require-extension "Not a recognized extension type" |
|
x))))) |
|
|
|
|
|
|
|
|
|
|
|
(define-syntax define-inlinable |
|
|
|
|
|
|
|
(lambda (x) |
|
|
|
|
|
(define prefix (string->symbol "% ")) |
|
(define (make-procedure-name name) |
|
(datum->syntax name |
|
(symbol-append prefix (syntax->datum name) |
|
'-procedure))) |
|
|
|
(syntax-case x () |
|
((_ (name formals ...) body0 body ...) |
|
(identifier? #'name) |
|
(with-syntax ((proc-name (make-procedure-name #'name)) |
|
((args ...) (generate-temporaries #'(formals ...)))) |
|
#`(begin |
|
(define (proc-name formals ...) |
|
#((maybe-unused)) |
|
(syntax-parameterize ((name (identifier-syntax proc-name))) |
|
body0 body ...)) |
|
(define-syntax-parameter name |
|
(lambda (x) |
|
(syntax-case x () |
|
((_ args ...) |
|
#'((syntax-parameterize ((name (identifier-syntax proc-name))) |
|
(lambda (formals ...) |
|
body0 body ...)) |
|
args ...)) |
|
((_ a (... ...)) |
|
(syntax-violation 'name "Wrong number of arguments" x)) |
|
(_ |
|
(identifier? x) |
|
#'proc-name)))))))))) |
|
|
|
|
|
|
|
(define using-readline? |
|
(let ((using-readline? (make-fluid))) |
|
(make-procedure-with-setter |
|
(lambda () (fluid-ref using-readline?)) |
|
(lambda (v) (fluid-set! using-readline? v))))) |
|
|
|
|
|
|
|
|
|
|
|
|
|
(define (install-r6rs!) |
|
"Make changes to the default environment to better conform to the |
|
R6RS. @xref{R6RS Incompatibilities} in the manual." |
|
(set! %load-extensions |
|
(cons* ".guile.sls" ".sls" |
|
(delete ".guile.sls" (delete ".sls" %load-extensions)))) |
|
(read-enable 'r6rs-hex-escapes) |
|
(read-enable 'hungry-eol-escapes)) |
|
|
|
(define (install-r7rs!) |
|
"Make changes to the default environment to better conform to the |
|
R7RS." |
|
(install-r6rs!) |
|
(set! %load-extensions |
|
(cons* ".guile.sld" ".sld" |
|
(delete ".guile.sld" (delete ".sld" (delete ".guile.sls" (delete ".sls" %load-extensions)))))) |
|
(read-enable 'r7rs-symbols)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
(begin-deprecated |
|
(module-use! the-scm-module (resolve-interface '(ice-9 deprecated)))) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(module-use! the-root-module (resolve-interface '(ice-9 ports))) |
|
|
|
(module-use! the-scm-module (resolve-interface '(ice-9 ports))) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(call-with-values (lambda () |
|
|
|
|
|
(let ((syntax? syntax?)) |
|
(include-from-path "ice-9/read.scm") |
|
(values read read-syntax))) |
|
(lambda (read* read-syntax*) |
|
(set! read read*) |
|
(set! read-syntax read-syntax*))) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(resolve-interface '(ice-9 threads)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(resolve-interface '(ice-9 exceptions)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(module-use! the-scm-module (resolve-interface '(srfi srfi-4))) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define (make-soft-port pv modes) |
|
((module-ref (resolve-interface '(ice-9 soft-ports)) |
|
'deprecated-make-soft-port) |
|
pv modes)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define-module (system syntax internal)) |
|
|
|
(let () |
|
(define (steal-bindings! from to ids) |
|
(for-each |
|
(lambda (sym) |
|
(let ((v (module-local-variable from sym))) |
|
(module-remove! from sym) |
|
(module-add! to sym v))) |
|
ids) |
|
(module-export! to ids)) |
|
|
|
(steal-bindings! the-root-module (resolve-module '(system syntax internal)) |
|
'(syntax? |
|
syntax-local-binding |
|
%syntax-module |
|
syntax-locally-bound-identifiers |
|
syntax-session-id |
|
make-syntax |
|
syntax-expression |
|
syntax-wrap |
|
syntax-module |
|
syntax-sourcev))) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define-module (guile-user) |
|
#:autoload (system base compile) (compile compile-file) |
|
#:filename #f |
|
#:declarative? #f) |
|
|
|
|
|
|
|
(eval-when (compile) (set-current-module the-root-module)) |
|
|
|
|
|
|