1
0
forked from alex-eg/sex

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 0cb3956e02
commit 27ef5ac805
7 changed files with 87 additions and 37 deletions

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
@@ -28,29 +26,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
@@ -66,12 +71,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
@@ -279,28 +284,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