2007-05-02 04:48:41 -04:00
|
|
|
|
2007-05-04 02:23:19 -04:00
|
|
|
|
|
|
|
|
2007-05-02 04:48:41 -04:00
|
|
|
(library (ikarus library-manager)
|
2007-05-07 03:55:51 -04:00
|
|
|
(export imported-label->binding library-subst
|
2007-05-07 20:58:12 -04:00
|
|
|
installed-libraries visit-library
|
2007-05-15 10:18:58 -04:00
|
|
|
library-name
|
2007-05-06 23:42:10 -04:00
|
|
|
find-library-by-name install-library
|
2007-05-09 23:42:32 -04:00
|
|
|
library-spec invoke-library
|
2007-05-11 20:32:48 -04:00
|
|
|
extend-library-subst! extend-library-env!
|
2007-05-15 10:18:58 -04:00
|
|
|
current-library-expander current-library-collection)
|
2007-05-07 04:09:35 -04:00
|
|
|
(import (except (ikarus) installed-libraries))
|
2007-05-04 02:23:19 -04:00
|
|
|
|
|
|
|
(define (make-collection)
|
|
|
|
(let ([set '()])
|
|
|
|
(define (set-cons x ls)
|
|
|
|
(cond
|
|
|
|
[(memq x ls) ls]
|
|
|
|
[else (cons x ls)]))
|
|
|
|
(case-lambda
|
|
|
|
[() set]
|
|
|
|
[(x) (set! set (set-cons x set))])))
|
|
|
|
|
2007-05-05 22:42:26 -04:00
|
|
|
(define current-library-collection
|
2007-05-07 04:04:54 -04:00
|
|
|
;;; this works now because make-collection is a lambda
|
|
|
|
;;; binding and this turns into a complex binding as far
|
|
|
|
;;; as letrec is concerned. It will be more ok once we do
|
|
|
|
;;; letrec*.
|
|
|
|
(make-parameter (make-collection)
|
2007-05-04 02:23:19 -04:00
|
|
|
(lambda (x)
|
|
|
|
(unless (procedure? x)
|
2007-05-07 04:04:54 -04:00
|
|
|
(error 'current-library-collection "~s is not a procedure" x))
|
2007-05-04 02:23:19 -04:00
|
|
|
x)))
|
|
|
|
|
2007-05-02 04:48:41 -04:00
|
|
|
(define-record library
|
2007-05-07 04:52:22 -04:00
|
|
|
(id name ver imp* vis* inv* subst env visit-state invoke-state
|
|
|
|
visible?))
|
2007-05-02 04:48:41 -04:00
|
|
|
|
|
|
|
(define (find-dependencies ls)
|
|
|
|
(cond
|
|
|
|
[(null? ls) '()]
|
|
|
|
[else (error 'find-dependencies "cannot handle deps yet")]))
|
|
|
|
|
|
|
|
(define (find-library-by pred)
|
2007-05-05 22:42:26 -04:00
|
|
|
(let f ([ls ((current-library-collection))])
|
2007-05-02 04:48:41 -04:00
|
|
|
(cond
|
|
|
|
[(null? ls) #f]
|
|
|
|
[(pred (car ls)) (car ls)]
|
|
|
|
[else (f (cdr ls))])))
|
|
|
|
|
2007-05-11 20:32:48 -04:00
|
|
|
(define library-path
|
|
|
|
(make-parameter
|
|
|
|
'(".")
|
|
|
|
(lambda (x)
|
|
|
|
(if (and (list? x) (andmap string? x))
|
|
|
|
(map values x)
|
|
|
|
(error 'library-path "~s is not a list of strings" x)))))
|
|
|
|
|
|
|
|
(define (library-name->file-name x)
|
|
|
|
(with-output-to-string
|
|
|
|
(lambda ()
|
|
|
|
(define (display-hex n)
|
|
|
|
(cond
|
|
|
|
[(<= 0 n 9) (display n)]
|
|
|
|
[else (display
|
|
|
|
(integer->char
|
|
|
|
(+ (char->integer #\A)
|
|
|
|
(- n 10))))]))
|
|
|
|
(let f ([ls x])
|
|
|
|
(cond
|
|
|
|
[(null? ls) (display ".ss")]
|
|
|
|
[else
|
|
|
|
(display "/")
|
|
|
|
(for-each
|
|
|
|
(lambda (c)
|
|
|
|
(cond
|
|
|
|
[(or (char<=? #\a c #\z)
|
2007-05-11 21:27:00 -04:00
|
|
|
(char<=? #\A c #\Z)
|
2007-05-11 20:32:48 -04:00
|
|
|
(char<=? #\0 c #\9)
|
|
|
|
(memv c '(#\- #\. #\_ #\~)))
|
|
|
|
(display c)]
|
|
|
|
[else
|
|
|
|
(display "%")
|
|
|
|
(let ([n (char->integer c)])
|
|
|
|
(display-hex (quotient n 16))
|
|
|
|
(display-hex (remainder n 16)))]))
|
|
|
|
(string->list
|
|
|
|
(symbol->string (car ls))))
|
|
|
|
(f (cdr ls))])))))
|
|
|
|
(define file-locator
|
|
|
|
(make-parameter
|
|
|
|
(lambda (x)
|
|
|
|
(let ([str (library-name->file-name x)])
|
|
|
|
(let f ([ls (library-path)])
|
|
|
|
(and (pair? ls)
|
2007-05-11 21:27:00 -04:00
|
|
|
(let ([name (string-append (car ls) str)])
|
2007-05-11 20:32:48 -04:00
|
|
|
(if (file-exists? name)
|
|
|
|
name
|
|
|
|
(f (cdr ls))))))))
|
|
|
|
(lambda (f)
|
|
|
|
(if (procedure? f)
|
|
|
|
f
|
|
|
|
(error 'file-locator
|
|
|
|
"~s is not a procedure" f)))))
|
|
|
|
|
|
|
|
(define library-locator
|
|
|
|
(make-parameter
|
|
|
|
(lambda (x)
|
|
|
|
(let ([file-name ((file-locator) x)])
|
|
|
|
(and (string? file-name)
|
|
|
|
(with-input-from-file file-name read))))
|
|
|
|
(lambda (f)
|
|
|
|
(if (procedure? f)
|
|
|
|
f
|
|
|
|
(error 'library-locator
|
|
|
|
"~s is not a procedure" f)))))
|
|
|
|
|
|
|
|
(define current-library-expander
|
|
|
|
(make-parameter
|
|
|
|
(lambda (x)
|
|
|
|
(error 'library-expander "not initialized"))
|
|
|
|
(lambda (f)
|
|
|
|
(if (procedure? f)
|
|
|
|
f
|
|
|
|
(error 'library-expander
|
|
|
|
"~s is not a procedure" f)))))
|
|
|
|
|
|
|
|
(define external-pending-libraries
|
|
|
|
(make-parameter '()))
|
|
|
|
|
|
|
|
(define (find-external-library name)
|
|
|
|
(when (member name (external-pending-libraries))
|
|
|
|
(error #f "circular attempt to import library ~s detected"
|
|
|
|
name))
|
|
|
|
(parameterize ([external-pending-libraries
|
|
|
|
(cons name (external-pending-libraries))])
|
|
|
|
(let ([lib-expr ((library-locator) name)])
|
|
|
|
(unless lib-expr
|
|
|
|
(error #f "cannot find library ~s" name))
|
|
|
|
((current-library-expander) lib-expr)
|
|
|
|
(or (find-library-by
|
|
|
|
(lambda (x) (equal? (library-name x) name)))
|
|
|
|
(error #f "handling external library of ~s did not yield the currect library" name)))))
|
|
|
|
|
2007-05-05 22:42:26 -04:00
|
|
|
(define (find-library-by-name name)
|
2007-05-11 20:32:48 -04:00
|
|
|
(or (find-library-by
|
|
|
|
(lambda (x) (equal? (library-name x) name)))
|
|
|
|
(find-external-library name)))
|
2007-05-02 04:48:41 -04:00
|
|
|
|
2007-05-07 03:45:13 -04:00
|
|
|
(define (library-exists? name)
|
|
|
|
(and (find-library-by
|
|
|
|
(lambda (x) (equal? (library-name x) name)))
|
|
|
|
#t))
|
|
|
|
|
2007-05-02 20:05:19 -04:00
|
|
|
(define (find-library-by-spec/die spec)
|
|
|
|
(let ([id (car spec)])
|
|
|
|
(or (find-library-by
|
|
|
|
(lambda (x) (eq? id (library-id x))))
|
|
|
|
(error #f "cannot find library with spec ~s" spec))))
|
|
|
|
|
2007-05-06 23:32:51 -04:00
|
|
|
(define label->binding-table (make-hash-table))
|
|
|
|
|
2007-05-11 20:32:48 -04:00
|
|
|
(define (install-library-record lib)
|
|
|
|
(let ([exp-env (library-env lib)])
|
|
|
|
(for-each
|
|
|
|
(lambda (x)
|
|
|
|
(let ([label (car x)] [binding (cdr x)])
|
|
|
|
(let ([binding
|
|
|
|
(case (car binding)
|
|
|
|
[(global)
|
|
|
|
(cons 'global (cons lib (cdr binding)))]
|
|
|
|
[(global-macro)
|
|
|
|
(cons 'global-macro (cons lib (cdr binding)))]
|
|
|
|
[else binding])])
|
|
|
|
(put-hash-table! label->binding-table label binding))))
|
|
|
|
exp-env))
|
|
|
|
((current-library-collection) lib))
|
|
|
|
|
2007-05-07 04:54:10 -04:00
|
|
|
(define (install-library id name ver imp* vis* inv*
|
|
|
|
exp-subst exp-env visit-code invoke-code visible?)
|
|
|
|
(let ([imp-lib* (map find-library-by-spec/die imp*)]
|
|
|
|
[vis-lib* (map find-library-by-spec/die vis*)]
|
|
|
|
[inv-lib* (map find-library-by-spec/die inv*)])
|
|
|
|
(unless (and (symbol? id) (list? name) (list? ver))
|
|
|
|
(error 'install-library "invalid spec ~s ~s ~s" id name ver))
|
|
|
|
(when (library-exists? name)
|
|
|
|
(error 'install-library "~s is already installed" name))
|
|
|
|
(let ([lib (make-library id name ver imp-lib* vis-lib* inv-lib*
|
|
|
|
exp-subst exp-env visit-code invoke-code
|
|
|
|
visible?)])
|
2007-05-11 20:32:48 -04:00
|
|
|
(install-library-record lib))))
|
2007-05-02 06:24:37 -04:00
|
|
|
|
2007-05-09 23:42:32 -04:00
|
|
|
(define extend-library-subst!
|
|
|
|
(lambda (lib sym label)
|
|
|
|
(set-library-subst! lib
|
|
|
|
(cons (cons sym label) (library-subst lib)))))
|
|
|
|
|
|
|
|
(define extend-library-env!
|
|
|
|
(lambda (lib label binding)
|
|
|
|
(set-library-env! lib
|
|
|
|
(cons (cons label binding) (library-env lib)))
|
|
|
|
(put-hash-table! label->binding-table label binding)))
|
|
|
|
|
2007-05-05 22:42:26 -04:00
|
|
|
(define (imported-label->binding lab)
|
2007-05-06 23:32:51 -04:00
|
|
|
(get-hash-table label->binding-table lab #f))
|
2007-05-02 20:05:19 -04:00
|
|
|
|
2007-05-05 22:42:26 -04:00
|
|
|
(define (invoke-library lib)
|
2007-05-02 20:05:19 -04:00
|
|
|
(let ([invoke (library-invoke-state lib)])
|
|
|
|
(when (procedure? invoke)
|
|
|
|
(set-library-invoke-state! lib
|
|
|
|
(lambda () (error 'invoke "circularity detected for ~s" lib)))
|
2007-05-05 22:42:26 -04:00
|
|
|
(for-each invoke-library (library-inv* lib))
|
2007-05-02 20:05:19 -04:00
|
|
|
(set-library-invoke-state! lib
|
|
|
|
(lambda () (error 'invoke "first invoke did not return for ~s" lib)))
|
|
|
|
(invoke)
|
|
|
|
(set-library-invoke-state! lib #t))))
|
|
|
|
|
2007-05-07 20:58:12 -04:00
|
|
|
|
|
|
|
(define (visit-library lib)
|
|
|
|
(let ([visit (library-visit-state lib)])
|
|
|
|
(when (procedure? visit)
|
|
|
|
(set-library-visit-state! lib
|
|
|
|
(lambda () (error 'visit "circularity detected for ~s" lib)))
|
|
|
|
(for-each invoke-library (library-vis* lib))
|
|
|
|
(set-library-visit-state! lib
|
|
|
|
(lambda () (error 'invoke "first visit did not return for ~s" lib)))
|
|
|
|
(visit)
|
|
|
|
(set-library-visit-state! lib #t))))
|
|
|
|
|
|
|
|
|
2007-05-05 22:42:26 -04:00
|
|
|
(define (invoke-library-by-spec spec)
|
|
|
|
(invoke-library (find-library-by-spec/die spec)))
|
2007-05-02 20:05:19 -04:00
|
|
|
|
2007-05-05 22:42:26 -04:00
|
|
|
(define installed-libraries
|
2007-05-07 04:52:22 -04:00
|
|
|
(case-lambda
|
|
|
|
[(all?)
|
|
|
|
(let f ([ls ((current-library-collection))])
|
|
|
|
(cond
|
|
|
|
[(null? ls) '()]
|
|
|
|
[(or all? (library-visible? (car ls)))
|
|
|
|
(cons (car ls) (f (cdr ls)))]
|
|
|
|
[else (f (cdr ls))]))]
|
|
|
|
[() (installed-libraries #f)]))
|
|
|
|
|
2007-05-05 22:42:26 -04:00
|
|
|
(define library-spec
|
2007-05-02 19:22:54 -04:00
|
|
|
(lambda (x)
|
|
|
|
(unless (library? x)
|
|
|
|
(error 'library-spec "~s is not a library" x))
|
|
|
|
(list (library-id x) (library-name x) (library-ver x))))
|
2007-05-05 22:42:26 -04:00
|
|
|
|
|
|
|
;;; init
|
2007-05-07 04:04:54 -04:00
|
|
|
(set-rtd-printer! (type-descriptor library)
|
2007-05-04 09:22:33 -04:00
|
|
|
(lambda (x p)
|
|
|
|
(unless (library? x)
|
|
|
|
(error 'record-type-printer "not a library"))
|
|
|
|
(display
|
|
|
|
(format "#<library ~s>" (append (library-name x) (library-ver x)))
|
|
|
|
p)))
|
|
|
|
|
|
|
|
)
|
2007-05-02 19:19:37 -04:00
|
|
|
|