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

@@ -115,9 +115,9 @@
(cons sex-form acc)) (cons sex-form acc))
(('comment . _) (cons sex-form acc)) (('comment . _) (cons sex-form acc))
((or ('import . modules) ((or ('import . _)
('pub 'import . modules)) ('pub 'import . _))
(process-imports (get-modules-public-forms modules) acc)) (process-imports (get-modules-public-forms sex-form) acc))
((or ('defmacro . rest) ((or ('defmacro . rest)
('pub 'defmacro . rest)) (defmacro rest) acc) ('pub 'defmacro . rest)) (defmacro rest) acc)

View File

@@ -1,6 +1,5 @@
(import (import
scheme scheme
brev-separate
(chicken base) (chicken base)
(chicken file) (chicken file)
(chicken load) (chicken load)
@@ -8,7 +7,6 @@
(chicken pathname) (chicken pathname)
(chicken process-context) (chicken process-context)
(chicken string) (chicken string)
fmt
matchable matchable
reader reader
srfi-1 srfi-1
@@ -28,29 +26,36 @@
;;; once) ;;; once)
(define +imported-modules+ (list)) (define +imported-modules+ (list))
(define (get-modules-public-forms module-list) (define (get-modules-public-forms import-form)
;; Module list is a list of symbols
;; How Sex handles modules: ;; How Sex handles modules:
;; For each module in a list, construct path, find module by path in ;; For each module IMPORT-FORM names, construct path, find module by
;; module path directories, extract public definitions from the ;; path in module path directories, extract public definitions from
;; module, paste them in current one in emulation of C include ;; the module, paste them in current one in emulation of C include
;; directives. ;; directives.
(fold-right append (list) (append-map (lambda (name) (import-module (symbol->string name) import-form))
(map (fn (import-module (symbol->string x))) (cdr (strip-pub import-form))))
module-list)))
(define (import-module name) (define (import-module name form)
(let ((module-path (locate-module name))) (let* ((directory (import-directory form))
(assert module-path (fmt #f "Failed to find module " name " in " (module-path (locate-module name directory)))
(get-module-paths))) (unless module-path
(sex-error form "Failed to find module" name (get-module-paths directory)))
(if (member module-path +imported-modules+) (if (member module-path +imported-modules+)
(list) (list)
(begin (begin
(set! +imported-modules+ (cons module-path +imported-modules+)) (set! +imported-modules+ (cons module-path +imported-modules+))
(read-public-interface module-path))))) (read-public-interface module-path)))))
(define (get-module-paths) ;;; An import is looked for beside the file it is written in, so a
(cons (current-directory) ;;; 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)))) (append +persistent-module-paths+ (standard-library-paths))))
;;; Where the modules shipped with the compiler live. Asked of the ;;; Where the modules shipped with the compiler live. Asked of the
@@ -66,12 +71,12 @@
(make-pathname (list (pathname-directory bin) "share") "sex"))) (make-pathname (list (pathname-directory bin) "share") "sex")))
(list)))) (list))))
(define (locate-module name) (define (locate-module name directory)
;; Module locations: relative to file being compiled, or in what was ;; Module locations: relative to DIRECTORY, or in what was in
;; in SEX_MODULE_PATH env var at the start of the process (see ;; SEX_MODULE_PATH env var at the start of the process (see
;; load-persistent-module-paths function) ;; load-persistent-module-paths function)
(let ((search-paths (get-module-paths))) (let ((search-paths (get-module-paths directory)))
(let loop ((paths search-paths)) (let loop ((paths search-paths))
(if (null? paths) (if (null? paths)
#f #f
@@ -279,28 +284,31 @@
(map (lambda (entry) (append entry (list #t))) (map (lambda (entry) (append entry (list #t)))
(append-map exported-types (imported-modules forms #f))))) (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?) (define (imported-modules forms pub?)
(append-map (lambda (form) (append-map (lambda (form)
(match form (match form
(('pub 'import . modules) (if pub? modules (list))) (('pub 'import . modules) (if pub? (located form modules) (list)))
(('import . modules) (if pub? (list) modules)) (('import . modules) (if pub? (list) (located form modules)))
(_ (list)))) (_ (list))))
forms)) 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. ;;; The types a module gives its importers, its pub imports' included.
;;; Two modules may import each other, so each is read once ;;; Two modules may import each other, so each is read once
(define (exported-types module) (define (exported-types path)
(let next ((modules (list module)) (seen (list)) (acc (list))) (let next ((paths (list path)) (seen (list)) (acc (list)))
(if (null? modules) (cond ((null? paths) (concatenate (reverse acc)))
(concatenate (reverse acc)) ((member (car paths) seen) (next (cdr paths) seen acc))
(let ((path (locate-module (symbol->string (car modules))))) (else
(if (or (not path) (member path seen)) (let ((exports (module-exports (car paths))))
(next (cdr modules) seen acc) (next (append (cdr paths) (cdr exports))
(let ((exports (module-exports path))) (cons (car paths) seen)
(next (append (cdr modules) (cdr exports)) (cons (car exports) acc)))))))
(cons path seen)
(cons (car exports) acc))))))))
;;; The types the module at PATH declares to its importers, and the ;;; The types the module at PATH declares to its importers, and the
;;; modules its interface imports; worked out once, though the check of ;;; 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 ;;; and ledger, which only greet does but which decides what the app is
;;; told of it. ;;; 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 ;;; make check-modules
(import scheme (import scheme
@@ -80,6 +84,20 @@
(and (irregex-match ".*\\.sex:" line) (pathname-strip-directory line))) (and (irregex-match ".*\\.sex:" line) (pathname-strip-directory line)))
(call-with-input-file (built "greet-app.d") read-lines))) (call-with-input-file (built "greet-app.d") read-lines)))
(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" (test "the app runs through both"
'("hello, world" '("hello, world"
"hello, sex" "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)))