diff --git a/semen.scm b/semen.scm index 7b64406..c4160f1 100644 --- a/semen.scm +++ b/semen.scm @@ -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) diff --git a/sex-modules.scm b/sex-modules.scm index 8044d76..f87d0ce 100644 --- a/sex-modules.scm +++ b/sex-modules.scm @@ -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 @@ -282,28 +287,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 ((exports (module-exports path))) - (next (append (cdr modules) (cdr exports)) - (cons path seen) - (cons (car exports) 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 ((exports (module-exports (car paths)))) + (next (append (cdr paths) (cdr exports)) + (cons (car paths) seen) + (cons (car exports) acc))))))) ;;; The types the module at PATH declares to its importers, and the ;;; modules its interface imports; worked out once, though the check of diff --git a/tests/modules/check.scm b/tests/modules/check.scm index 08198f9..d4c3e7f 100644 --- a/tests/modules/check.scm +++ b/tests/modules/check.scm @@ -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) diff --git a/tests/modules/shelf-app.sex b/tests/modules/shelf-app.sex new file mode 100644 index 0000000..d97ca10 --- /dev/null +++ b/tests/modules/shelf-app.sex @@ -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)) diff --git a/tests/modules/shelf/book.sex b/tests/modules/shelf/book.sex new file mode 100644 index 0000000..9a23e74 --- /dev/null +++ b/tests/modules/shelf/book.sex @@ -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)))) diff --git a/tests/modules/shelf/page.sex b/tests/modules/shelf/page.sex new file mode 100644 index 0000000..42c9585 --- /dev/null +++ b/tests/modules/shelf/page.sex @@ -0,0 +1,4 @@ +(pub record page ((number int))) + +(pub fn page-after ((p page)) page + (return #(page : (+ (. p number) 1)))) diff --git a/tests/modules/shelf/spine.sex b/tests/modules/shelf/spine.sex new file mode 100644 index 0000000..b5e1f70 --- /dev/null +++ b/tests/modules/shelf/spine.sex @@ -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)))