add module testing

Also fix module import
This commit is contained in:
2026-09-14 23:35:48 +03:00
parent 5af2e99f5f
commit c976378363
5 changed files with 80 additions and 7 deletions

View File

@@ -60,13 +60,18 @@ sextest:
SEX_TEST_PROGRAMS = hello-world lists comments unicode
# Multi-module linking is checked end to end; see tests/modules/Makefile.
check-modules: sexc
@$(MAKE) --no-print-directory -C ./tests/modules check SEXC=../../sexc
run-tests: sexc sex-tests sextest
./sex-tests && ./sextest --sexc=./sexc $(SEX_TEST_PROGRAMS:%=./tests/sex-programs/%.sex)
./sex-tests && ./sextest --sexc=./sexc $(SEX_TEST_PROGRAMS:%=./tests/sex-programs/%.sex) && $(MAKE) check-modules
clean:
rm -f $(OBJ) main.o
rm -f *.import.scm
rm -f *.link
rm -f sexc sex-tests sextest
$(MAKE) -C ./tests/modules clean
.PHONY: clean run-tests sex-tests sextest
.PHONY: clean run-tests sex-tests sextest check-modules

View File

@@ -68,11 +68,14 @@
(case (car form)
((pub)
(case (cadr form)
((fn) ; replace with prototype
;; fn type name (arg-list) (body)
;; 1 2 3 4 - we need first 4
(cons (copy-form-source! form (take (cdr form) 4)) acc))
((define defmacro import include struct typedef union var)
;; A function is reduced to a prototype and keeps its `pub', so
;; the importing unit declares it with external linkage
((fn)
(cons (copy-form-source! form (take form 5)) acc))
;; A variable becomes an `extern' declaration
((var)
(cons (copy-form-source! form (cons 'extern (take (cdr form) 3))) acc))
((define defmacro import include struct typedef union)
(cons (copy-form-source! form (cdr form)) acc))
(else (sex-error form "pub must be followed by a definition" form))))
(else acc)))

28
tests/modules/Makefile Normal file
View File

@@ -0,0 +1,28 @@
# Multi-module linking.
#
# Modules are only testable end to end, and nothing else in the suite
# links more than one translation unit. Three things have to hold at
# once: an imported `pub fn' comes out as a prototype with external
# linkage, an imported `pub var' as an extern declaration, and the
# module's object survives being passed after `--'. Get any of them
# wrong and this fails to link -- or, in the `pub var' case, links and
# quietly counts into a private copy.
SEXC ?= ../../sexc
EXPECTED = hello, world\nhello, sex\n2 greetings
check:
@$(SEXC) greet.sex -c -o greet.o
@$(SEXC) greet-app.sex -o greet-app -- greet.o
@if [ "`./greet-app`" = "`printf '$(EXPECTED)\n'`" ]; then \
echo "modules ok"; \
else \
echo "modules FAILED, got:"; ./greet-app; $(MAKE) clean; exit 1; \
fi
@$(MAKE) --no-print-directory clean
clean:
@rm -f greet.o greet-app
.PHONY: check clean

View File

@@ -0,0 +1,19 @@
;;; Uses the greet module. Build both, then link them:
;;;
;;; ./sexc example/greet.sex -c -o greet.o
;;; ./sexc example/greet-app.sex -o greet-app -- greet.o
;;;
;;; `(import greet)' pastes greet's public declarations here: `greet'
;;; as a prototype and `greet-count' as an extern. Both keep external
;;; linkage, so they refer to the one definition in greet.o rather than
;;; to private copies.
(include stdio.h)
(import greet)
(pub fn main () int
(greet "world")
(greet "sex")
(printf "%d greetings\n" greet-count)
(return 0))

18
tests/modules/greet.sex Normal file
View File

@@ -0,0 +1,18 @@
;;; A module. Everything marked `pub' forms its public interface;
;;; everything else is private to this file.
;;;
;;; Importing a module does not link it: it pastes the declarations, so
;;; the compiled object still has to be handed to the C compiler. See
;;; greet-app.sex.
(include stdio.h)
(pub var greet-count int 0)
(pub fn greet ((name (* const char))) void
(++ greet-count)
(printf "hello, %s\n" name))
;;; Not `pub': invisible to importers, and static in the generated C.
(fn unused-helper () void
(printf "private\n"))