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 2729e41e32
commit d8c85d9d95
7 changed files with 88 additions and 38 deletions

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.
;;;
;;; 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)

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