look for an import beside the file it is written in
Imports were looked for from the unit's directory, so a module in a subdirectory could not import its neighbours: a pub import failed, a private one lost its handle declarations, and --public-interface changed with where sexc ran.
This commit is contained in:
@@ -119,9 +119,9 @@
|
||||
(cons sex-form acc))
|
||||
(('comment . _) (cons sex-form acc))
|
||||
|
||||
((or ('import . modules)
|
||||
('pub 'import . modules))
|
||||
(process-imports (get-modules-public-forms modules) acc))
|
||||
((or ('import . _)
|
||||
('pub 'import . _))
|
||||
(process-imports (get-modules-public-forms sex-form) acc))
|
||||
|
||||
((or ('defmacro . rest)
|
||||
('pub 'defmacro . rest)) (defmacro rest) acc)
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
(import
|
||||
scheme
|
||||
brev-separate
|
||||
(chicken base)
|
||||
(chicken file)
|
||||
(chicken load)
|
||||
@@ -8,7 +7,6 @@
|
||||
(chicken pathname)
|
||||
(chicken process-context)
|
||||
(chicken string)
|
||||
fmt
|
||||
matchable
|
||||
reader
|
||||
srfi-1
|
||||
@@ -31,29 +29,36 @@
|
||||
;;; once)
|
||||
(define +imported-modules+ (list))
|
||||
|
||||
(define (get-modules-public-forms module-list)
|
||||
;; Module list is a list of symbols
|
||||
(define (get-modules-public-forms import-form)
|
||||
;; 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
|
||||
;; For each module IMPORT-FORM names, 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)))
|
||||
(append-map (lambda (name) (import-module (symbol->string name) import-form))
|
||||
(cdr (strip-pub import-form))))
|
||||
|
||||
(define (import-module name)
|
||||
(let ((module-path (locate-module name)))
|
||||
(assert module-path (fmt #f "Failed to find module " name " in "
|
||||
(get-module-paths)))
|
||||
(define (import-module name form)
|
||||
(let* ((directory (import-directory form))
|
||||
(module-path (locate-module name directory)))
|
||||
(unless module-path
|
||||
(sex-error form "Failed to find module" name (get-module-paths directory)))
|
||||
(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)
|
||||
;;; An import is looked for beside the file it is written in, so a
|
||||
;;; module's imports are its neighbours wherever the unit importing it
|
||||
;;; is. One with no file to be beside -- stdin's, a macro's -- is looked
|
||||
;;; for from the working directory, which is the unit's
|
||||
(define (import-directory form)
|
||||
(let ((file (form-file form)))
|
||||
(or (and file (pathname-directory file)) (current-directory))))
|
||||
|
||||
(define (get-module-paths #!optional (directory (current-directory)))
|
||||
(cons directory
|
||||
(append +persistent-module-paths+ (standard-library-paths))))
|
||||
|
||||
;;; Where the modules shipped with the compiler live. Asked of the
|
||||
@@ -69,12 +74,12 @@
|
||||
(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
|
||||
(define (locate-module name directory)
|
||||
;; Module locations: relative to DIRECTORY, 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 ((search-paths (get-module-paths directory)))
|
||||
(let loop ((paths search-paths))
|
||||
(if (null? paths)
|
||||
#f
|
||||
@@ -283,28 +288,31 @@
|
||||
(map (lambda (entry) (append entry (list #t)))
|
||||
(append-map exported-types (imported-modules forms #f)))))
|
||||
|
||||
;;; The names of the modules FORMS import, with pub or without
|
||||
;;; The files of the modules FORMS import, with pub or without
|
||||
(define (imported-modules forms pub?)
|
||||
(append-map (lambda (form)
|
||||
(match form
|
||||
(('pub 'import . modules) (if pub? modules (list)))
|
||||
(('import . modules) (if pub? (list) modules))
|
||||
(('pub 'import . modules) (if pub? (located form modules) (list)))
|
||||
(('import . modules) (if pub? (list) (located form modules)))
|
||||
(_ (list))))
|
||||
forms))
|
||||
|
||||
(define (located form modules)
|
||||
(let ((directory (import-directory form)))
|
||||
(filter-map (lambda (name) (locate-module (symbol->string name) directory))
|
||||
modules)))
|
||||
|
||||
;;; The types a module gives its importers, its pub imports' included.
|
||||
;;; Two modules may import each other, so each is read once
|
||||
(define (exported-types module)
|
||||
(let next ((modules (list module)) (seen (list)) (acc (list)))
|
||||
(if (null? modules)
|
||||
(concatenate (reverse acc))
|
||||
(let ((path (locate-module (symbol->string (car modules)))))
|
||||
(if (or (not path) (member path seen))
|
||||
(next (cdr modules) seen acc)
|
||||
(let ((forms (interface-forms (read-module path))))
|
||||
(next (append (cdr modules) (imported-modules forms #f))
|
||||
(cons path seen)
|
||||
(cons (declared-types forms) acc))))))))
|
||||
(define (exported-types path)
|
||||
(let next ((paths (list path)) (seen (list)) (acc (list)))
|
||||
(cond ((null? paths) (concatenate (reverse acc)))
|
||||
((member (car paths) seen) (next (cdr paths) seen acc))
|
||||
(else
|
||||
(let ((forms (interface-forms (read-module (car paths)))))
|
||||
(next (append (cdr paths) (imported-modules forms #f))
|
||||
(cons (car paths) seen)
|
||||
(cons (declared-types forms) acc)))))))
|
||||
|
||||
(define (public-fn-interface raw-form)
|
||||
;; (pub fn name args ret . body) -> prototype, keeping a docstring so
|
||||
|
||||
@@ -28,6 +28,10 @@
|
||||
;;; and ledger, which only greet does but which decides what the app is
|
||||
;;; told of it.
|
||||
;;;
|
||||
;;; A module in a directory of its own finds its imports beside itself,
|
||||
;;; not beside the unit importing it: shelf-app imports shelf/book, and
|
||||
;;; book its siblings page, with `pub', and spine, without.
|
||||
;;;
|
||||
;;; csi -s check.scm
|
||||
|
||||
(import scheme
|
||||
@@ -65,6 +69,19 @@
|
||||
(irregex-match "greet-app: greet-app\\.sex .*/greet\\.sex .*/ledger\\.sex"
|
||||
(call-with-input-file "greet-app.d" read-line)))
|
||||
|
||||
(test "a module's interface does not depend on where sexc runs"
|
||||
'("(record spine)" "(import page)")
|
||||
(take (or (output-of sexc "--public-interface" "shelf/book.sex") '()) 2))
|
||||
|
||||
(test-assert "a module in a directory finds its imports beside itself"
|
||||
(and (output-of sexc "shelf/page.sex" "-c" "-o" "page.o")
|
||||
(output-of sexc "shelf/book.sex" "-c" "-o" "book.o")
|
||||
(output-of sexc "shelf-app.sex" "-o" "shelf-app" "--" "book.o" "page.o")))
|
||||
|
||||
(test "and the app runs through it"
|
||||
'("pages 2 3")
|
||||
(output-of "./shelf-app"))
|
||||
|
||||
(test "the app runs through both"
|
||||
'("hello, world"
|
||||
"hello, sex"
|
||||
@@ -81,6 +98,7 @@
|
||||
"colour 123")
|
||||
(output-of "./greet-app"))
|
||||
|
||||
(for-each delete-file* '("greet.o" "ledger.o" "greet-app" "greet-app.d"))
|
||||
(for-each delete-file* '("greet.o" "ledger.o" "greet-app" "greet-app.d"
|
||||
"page.o" "book.o" "shelf-app"))
|
||||
|
||||
(test-exit)
|
||||
|
||||
9
tests/modules/shelf-app.sex
Normal file
9
tests/modules/shelf-app.sex
Normal file
@@ -0,0 +1,9 @@
|
||||
(include stdio.h)
|
||||
(import shelf/book)
|
||||
|
||||
(pub fn main () int
|
||||
(var b book #(book : NULL #(page : 1)))
|
||||
(printf "pages %d %d\n"
|
||||
(. (second-page (& b)) number)
|
||||
(. (page-after (second-page (& b))) number))
|
||||
(return 0))
|
||||
8
tests/modules/shelf/book.sex
Normal file
8
tests/modules/shelf/book.sex
Normal file
@@ -0,0 +1,8 @@
|
||||
;;; Its imports are found beside it, wherever the unit importing it is
|
||||
(pub import page)
|
||||
(import spine)
|
||||
|
||||
(pub record book ((spine (* spine)) (first page)))
|
||||
|
||||
(pub fn second-page ((b (* const book))) page
|
||||
(return (page-after (. b first))))
|
||||
4
tests/modules/shelf/page.sex
Normal file
4
tests/modules/shelf/page.sex
Normal file
@@ -0,0 +1,4 @@
|
||||
(pub record page ((number int)))
|
||||
|
||||
(pub fn page-after ((p page)) page
|
||||
(return #(page : (+ (. p number) 1))))
|
||||
3
tests/modules/shelf/spine.sex
Normal file
3
tests/modules/shelf/spine.sex
Normal file
@@ -0,0 +1,3 @@
|
||||
;;; book imports this without `pub', so book's importers get the record
|
||||
;;; behind a pointer, declared and no more
|
||||
(pub record spine ((width int)))
|
||||
Reference in New Issue
Block a user