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:
2026-10-16 13:55:45 +03:00
parent edfae88e4d
commit e835d39f58
7 changed files with 87 additions and 37 deletions

View File

@@ -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)

View File

@@ -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

View File

@@ -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.
;;;
;;; make check-modules
(import scheme
@@ -72,6 +76,20 @@
(irregex-match ".*/greet-app: greet-app\\.sex .*/greet\\.sex .*/ledger\\.sex"
(call-with-input-file (built "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" (built "page.o"))
(output-of sexc "shelf/book.sex" "-c" "-o" (built "book.o"))
(output-of sexc "shelf-app.sex" "-o" (built "shelf-app")
"--" (built "book.o") (built "page.o"))))
(test "and the app runs through it"
'("pages 2 3")
(output-of (built "shelf-app")))
(test "the app runs through both"
'("hello, world"
"hello, sex"

View 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))

View 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))))

View File

@@ -0,0 +1,4 @@
(pub record page ((number int)))
(pub fn page-after ((p page)) page
(return #(page : (+ (. p number) 1))))

View 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)))