make-absolute-pathname with no directory of its own put `src' under the root, so SEX_MODULE_PATH=src searched /src/. Fixes #48.
164 lines
6.1 KiB
Scheme
164 lines
6.1 KiB
Scheme
(import
|
||
scheme
|
||
brev-separate
|
||
(chicken base)
|
||
(chicken file)
|
||
(chicken load)
|
||
(chicken pathname)
|
||
(chicken process-context)
|
||
(chicken string)
|
||
fmt
|
||
matchable
|
||
reader
|
||
srfi-1
|
||
types
|
||
utils)
|
||
|
||
(define +persistent-module-paths+ (list))
|
||
|
||
;;; for guarding against multiple imports (sort of mandatory #pragma
|
||
;;; once)
|
||
(define +imported-modules+ (list))
|
||
|
||
(define (get-modules-public-forms module-list)
|
||
;; Module list is a list of symbols
|
||
;; How Sex handles modules:
|
||
;; For each module in a list, construct path, find module by path in
|
||
;; module path directories, extract public definitions from the
|
||
;; module, paste them in current one in emulation of C include
|
||
;; directives.
|
||
(fold-right append (list)
|
||
(map (fn (import-module (symbol->string x)))
|
||
module-list)))
|
||
|
||
(define (import-module name)
|
||
(let ((module-path (locate-module name)))
|
||
(assert module-path (fmt #f "Failed to find module " name " in "
|
||
(get-module-paths)))
|
||
(if (member module-path +imported-modules+)
|
||
(list)
|
||
(begin
|
||
(set! +imported-modules+ (cons module-path +imported-modules+))
|
||
(read-public-interface module-path)))))
|
||
|
||
(define (get-module-paths)
|
||
(cons (current-directory)
|
||
(append +persistent-module-paths+ (standard-library-paths))))
|
||
|
||
;;; Where the modules shipped with the compiler live. Asked of the
|
||
;;; binary rather than baked in when it is built: in a work tree sexc
|
||
;;; sits beside lib/, and installed it sits in bindir with the modules
|
||
;;; under ../share/sex. Last in the search order, so a module of one's
|
||
;;; own wins over one of ours by the same name
|
||
(define (standard-library-paths)
|
||
(let ((binary (executable-pathname)))
|
||
(if binary
|
||
(let ((bin (pathname-directory binary)))
|
||
(list (make-pathname bin "lib")
|
||
(make-pathname (list (pathname-directory bin) "share") "sex")))
|
||
(list))))
|
||
|
||
(define (locate-module name)
|
||
;; Module locations: relative to file being compiled, or in what was
|
||
;; in SEX_MODULE_PATH env var at the start of the process (see
|
||
;; load-persistent-module-paths function)
|
||
|
||
(let ((search-paths (get-module-paths)))
|
||
(let loop ((paths search-paths))
|
||
(if (null? paths)
|
||
#f
|
||
(or (module-exists? name (car paths))
|
||
(loop (cdr paths)))))))
|
||
|
||
(define (module-exists? name module-dir)
|
||
;; returns absolute path to module, if it exists
|
||
(and (directory-exists? module-dir)
|
||
(let ((module-path (make-absolute-pathname module-dir name "sex")))
|
||
(and (file-exists? module-path)
|
||
(file-readable? module-path)
|
||
module-path))))
|
||
|
||
(define (read-public-interface module-path)
|
||
;; pub fns are reduced to prototypes, other pub forms are just pasted.
|
||
;; Declaration order is kept: a module that declares a type and then a
|
||
;; pub fn taking it has to paste them that way round, or the prototype
|
||
;; names a type C has not seen.
|
||
(let ((raw-forms (read-from-file module-path)))
|
||
(reverse
|
||
(fold
|
||
process-public-interface-form
|
||
(list)
|
||
raw-forms))))
|
||
|
||
(define (public-fn-interface raw-form)
|
||
;; (pub fn name args ret . body) -> prototype, keeping a docstring so
|
||
;; the importer can emit it above the declaration
|
||
(let ((form (strip-header-comments raw-form 5)))
|
||
(match form
|
||
(('pub 'fn name args ret)
|
||
form)
|
||
(('pub 'fn name args ret ('comment . _) . rest)
|
||
(public-fn-interface `(pub fn ,name ,args ,ret ,@rest)))
|
||
(('pub 'fn name args ret (? string? doc) . _)
|
||
`(pub fn ,name ,args ,ret ,doc))
|
||
(('pub 'fn name args ret . _)
|
||
`(pub fn ,name ,args ,ret)))))
|
||
|
||
;;; An ops block is not pub itself -- its methods are. The block leaves
|
||
;;; as a block, its pub methods reduced to prototypes, so the importer
|
||
;;; runs its own `process-ops' over it: prototypes alone would say that
|
||
;;; vec2ːlength exists without saying that vec2 has a `length', and
|
||
;;; (. v length) needs the second thing.
|
||
(define (ops-public-interface form)
|
||
(let ((type (and (pair? (cdr form)) (cadr form)))
|
||
(methods (filter (lambda (m)
|
||
(and (pair? m) (eq? (car m) 'pub)
|
||
(pair? (cdr m)) (eq? (cadr m) 'fn)))
|
||
(cddr form))))
|
||
(and (symbol? type)
|
||
(pair? methods)
|
||
(copy-form-source! form
|
||
`(ops ,type ,@(map public-fn-interface methods))))))
|
||
|
||
;;; A realization leaves with the methods written in it, reduced to
|
||
;;; prototypes. Its defaults do not travel: the importer synthesizes
|
||
;;; its own static copies from the spec, which is why a default is
|
||
;;; static even when the realization is pub.
|
||
(define (realize-public-interface form)
|
||
(let ((core (cdr form)))
|
||
(copy-form-source!
|
||
form
|
||
`(pub realize ,(second core)
|
||
;; pub goes back on at the realize, not on its methods
|
||
,@(map (lambda (m) (cdr (public-fn-interface (cons 'pub m))))
|
||
(remove comment-form? (cddr core)))))))
|
||
|
||
;;; TODO: use semen facilities to analyze modules
|
||
(define (process-public-interface-form form acc)
|
||
(match form
|
||
;; Reduced to a prototype, still `pub', so the importer declares it
|
||
;; with external linkage
|
||
(('pub 'fn . _)
|
||
(cons (copy-form-source! form (public-fn-interface form)) acc))
|
||
(('pub 'var . _)
|
||
(match-let ((('pub 'var name type . _) (strip-header-comments form 4)))
|
||
(cons (copy-form-source! form `(extern var ,name ,type)) acc)))
|
||
(('pub (or 'define 'defmacro 'enum 'import 'include 'record 'spec 'struct 'sum 'typedef 'union) . _)
|
||
(cons (copy-form-source! form (cdr form)) acc))
|
||
(('ops . _)
|
||
(let ((exported (ops-public-interface form)))
|
||
(if exported (cons exported acc) acc)))
|
||
(('pub 'realize . _)
|
||
(cons (realize-public-interface form) acc))
|
||
(('pub . _)
|
||
(sex-error form "pub must be followed by a definition" form))
|
||
(_ acc)))
|
||
|
||
(define (load-persistent-module-paths)
|
||
(let ((sex-module-path-env-var
|
||
(get-env-var "SEX_MODULE_PATH")))
|
||
(when sex-module-path-env-var
|
||
(set! +persistent-module-paths+
|
||
(map to-absolute-pathname
|
||
(string-split sex-module-path-env-var ":"))))))
|