1
0
forked from alex-eg/sex

expand a module's toplevel macro calls for its importers

Its interface was read as written, so pub forms a macro wrote never
reached them, which list-macros' is-public? flag exists for. The
module's macros, and those its imports give it, are lent for the
reading and taken back after. One reading a type the module keeps to
itself cannot expand there, and gives importers nothing, as before.
This commit is contained in:
2026-10-19 14:18:56 +03:00
parent 7c897b6dc6
commit 1dd1cf0ae9
6 changed files with 141 additions and 11 deletions

View File

@@ -36,6 +36,9 @@
;;; Two modules may import each other with `pub': cycle/a and cycle/b,
;;; whose interfaces each import the other back.
;;;
;;; gen/made writes its pub forms by macro calls, which are expanded when
;;; its interface is read; the macros it used for that stay its own.
;;;
;;; make check-modules
(import scheme
@@ -120,6 +123,21 @@
'("cycle 1 2")
(output-of (built "cycle-app")))
;;; ...it lists pub forms a macro wrote, and not the macros that wrote them
(test "a module's interface holds what its macros wrote"
'("(pub fn forty-two () int)" "(pub fn seven () int)"
"(pub fn one () int)" "(pub fn two () int)")
(output-of sexc "--public-interface" "gen/made.sex"))
(test-assert "and an importer links with them"
(and (output-of sexc "gen/made.sex" "-c" "-o" (built "made.o"))
(output-of sexc "made-app.sex" "-o" (built "made-app")
"--" (built "made.o"))))
(test "and calls them"
'("made 42 7 1 2")
(output-of (built "made-app")))
;;; ...while a realization of a spec importers cannot see is refused by
;;; the module that writes it, not by an importer naming it first
(test-assert "a pub realization of a spec imported without pub is refused"

View File

@@ -0,0 +1,23 @@
;;; Its pub forms are written by macro calls -- its own, one it imports
;;; without pub, and a splice of two -- and reach importers as written
;;; ones do. One reading a record kept private here cannot expand for an
;;; importer, who is given nothing of it, as before, and still builds
(import makers)
(defmacro (getter name value)
`(pub fn ,name () int (return ,value)))
(defmacro (pair-of a b)
`($ (pub fn ,a () int (return 1)) (pub fn ,b () int (return 2))))
(defmacro (field-count name)
(let ((fields (get-fields name)))
(unless fields (error "no such record" name))
`(pub fn ,(cat name "-fields") () int (return ,(length fields)))))
(record box ((w int) (h int)))
(getter forty-two 42)
(field-count box)
(constant seven 7)
(pair-of one two)

View File

@@ -0,0 +1,3 @@
;;; A macro module: what it writes is up to whoever calls it
(pub defmacro (constant name value)
`(pub fn ,name () int (return ,value)))

View File

@@ -0,0 +1,6 @@
(include stdio.h)
(import gen/made)
(pub fn main () int
(printf "made %d %d %d %d\n" (forty-two) (seven) (one) (two))
(return 0))