8 Commits

Author SHA1 Message Date
6f7b28cfa5 drop the default SEXC from the check scripts' usage 2026-10-19 18:23:20 +03:00
76a521b4b9 build the programs the way the modules are built
Each program had its own recipe: a hand-written link list, a cd into
tests/ so its includes would not find the modules' namesakes, and a
renamed output so csc would not overwrite the module sexc.o. Read off
their imports like the modules', and compiled to an object named after
their source, they share one rule; include-relative ends the cd.
2026-10-19 18:23:20 +03:00
20151ece1b take every program in tests/sex-programs as a test
A program left out of the hand-kept list was never run, and nothing said
so; the one module the tests import moves to lib/ instead.
2026-10-19 18:23:20 +03:00
7547deb8dd check modules and exit codes with the test egg
Written as shell inside make, a check said only that it failed and left
the reason to rerunning it by hand; now each says what it expected.
2026-10-19 18:23:20 +03:00
4b5f3ffd6b read the module graph off the import forms
Each module's prerequisite line restated its import form, so adding a
module or an import meant editing the Makefile to match, and a missed
line only showed when an object came out of order.
2026-10-19 18:23:14 +03:00
a6e9b3c948 build the unit tests and sextest from the root
Each subdirectory's Makefile repeated the compiler's module list just to
link one program, and its result was copied back to the root anyway.
2026-10-19 18:23:14 +03:00
b49f72e387 keep the local egg repository in a file of its own
The Makefile was mostly about where the eggs live; what sexc is built
from is easier to find without it.
2026-10-19 18:23:14 +03:00
ed369a8ff7 make each compiler module one file, built once
A module was a body plus a (module ...) wrapper naming its exports, and
the wrapper came in up to three copies: the root's, a test one that
exported everything, and sextest's, each recompiling the same body.

csc -module makes a file a module by itself, with export forms in its
body, so the wrappers are gone. tests/ and tools/sextest/ link what the
root built, finding it on the repository path; the tests see what a
module exports, and the names only they need are exported as such.
2026-10-19 18:23:14 +03:00
42 changed files with 558 additions and 597 deletions

2
.gitignore vendored
View File

@@ -5,10 +5,10 @@
*.o
*.import.scm
*.link
modules.mk
sexc
sex-tests
sextest
tools/sextest/sextest
# Scrapped design docs, kept for reference
/attic

166
Makefile
View File

@@ -1,6 +1,5 @@
CHICKEN_C ?= csc
CHICKEN_INSTALL ?= chicken-install
CHICKEN_STATUS ?= chicken-status
CSI ?= csi
CSC_FLAGS += -K prefix -static
# What and why:
# -emit-all-import-libraries: Emit import-libraries for all defined modules.
@@ -33,113 +32,52 @@ SEX_LIB = $(wildcard lib/*.sex)
MODULE_FLAGS = -emit-all-import-libraries -module-registration -c
# Order matters, since module check correctness on compilation
MODULES = utils types infer sex-macros reader sex-modules semen sex-fmt-c fmt-c-writer sexc
OBJ = $(MODULES:%=%.o)
DEPSFILE = dependencies.txt
DEPSLOCK = eggs.lock
EGGS_DIR := $(abspath .eggs)
# Compiler's own repository (types.db, chicken.*), ignoring ambient venv vars.
SYSTEM_CHICKEN_REPO := $(shell env \
-u CHICKEN_INSTALL_REPOSITORY -u CHICKEN_REPOSITORY_PATH \
-u CHICKEN_EGG_CACHE -u CHICKEN_INSTALL_PREFIX \
$(CHICKEN_INSTALL) -repository 2>/dev/null)
CHICKEN_ABI := $(notdir $(SYSTEM_CHICKEN_REPO))
EGGS_STAMP := $(EGGS_DIR)/.installed-$(CHICKEN_ABI)
# Try project-local chicken repository first. In case it doesn't exist (packaging for distros),
# system-wide repository will be used.
unexport CHICKEN_INSTALL_REPOSITORY
unexport CHICKEN_EGG_CACHE
unexport CHICKEN_INSTALL_PREFIX
export CHICKEN_REPOSITORY_PATH := $(EGGS_DIR):$(SYSTEM_CHICKEN_REPO)
all: sexc
sexc: $(OBJ) main.scm
$(CHICKEN_C) $(CSC_FLAGS) main.scm -o sexc-tmp -link sexc
# otherwise csc hangs, probably because it tries to compile to sexc.o first
mv sexc-tmp sexc
include eggs.mk
# Each program, and the source it is built from
PROGRAMS = sexc=main.scm sextest=tools/sextest/sextest.scm sex-tests=tests/run.scm
# MODULES, the programs' names and objects, and a line for each naming
# what it imports, read off the import forms
include modules.mk
OBJ = $(MODULES:%=%.o)
modules.mk: $(SOURCES) tools/module-deps.scm dependencies.txt
$(CSI) -s tools/module-deps.scm $(PROGRAMS) > $@.tmp
mv $@.tmp $@
#------------------------------------------------------------------
utils.o: utils.module.scm utils.scm
$(CHICKEN_C) $(CSC_FLAGS) $(MODULE_FLAGS) utils.module.scm -o utils.o -unit utils
# Everything is linked with the modules among its prerequisites
LINKS = $(addprefix -link ,$(basename $(filter-out $<,$(filter %.o,$^))))
types.o: types.module.scm types.scm
$(CHICKEN_C) $(CSC_FLAGS) $(MODULE_FLAGS) types.module.scm -o types.o -unit types
# A module is one file that declares its own exports, made a module by
# -module, except the vendored sex-fmt-c, a (module ...) form of its own
MODULE = -module $*
sex-fmt-c.o: MODULE =
infer.o: infer.module.scm infer.scm types.o utils.o
$(CHICKEN_C) $(CSC_FLAGS) $(MODULE_FLAGS) infer.module.scm -o infer.o -unit infer -link types,utils
%.o: %.scm
$(CHICKEN_C) $(CSC_FLAGS) $(MODULE_FLAGS) $< $(MODULE) -o $@ -unit $* $(LINKS)
sex-macros.o: sex-macros.module.scm sex-macros.scm
$(CHICKEN_C) $(CSC_FLAGS) $(MODULE_FLAGS) sex-macros.module.scm -o sex-macros.o -unit sex-macros
# A program's object is named after its source: csc names its own after
# -o, and sexc.o is the module's
$(PROGRAM_OBJ): %.o: %.scm
$(CHICKEN_C) $(CSC_FLAGS) -c $< -o $@
reader.o: reader.module.scm reader.scm utils.o
$(CHICKEN_C) $(CSC_FLAGS) $(MODULE_FLAGS) reader.module.scm -o reader.o -unit reader -link utils
$(PROGRAM_NAMES):
$(CHICKEN_C) $(CSC_FLAGS) $< -o $@ $(LINKS)
sex-modules.o: sex-modules.module.scm sex-modules.scm reader.o types.o utils.o
$(CHICKEN_C) $(CSC_FLAGS) $(MODULE_FLAGS) sex-modules.module.scm -o sex-modules.o -unit sex-modules -link reader,types,utils
semen.o: semen.module.scm semen.scm infer.o sex-macros.o sex-modules.o types.o utils.o
$(CHICKEN_C) $(CSC_FLAGS) $(MODULE_FLAGS) semen.module.scm -o semen.o -unit semen -link sex-macros,sex-modules,infer,types,utils
sex-fmt-c.o: sex-fmt-c.scm
$(CHICKEN_C) $(CSC_FLAGS) $(MODULE_FLAGS) sex-fmt-c.scm -o sex-fmt-c.o -unit sex-fmt-c
fmt-c-writer.o: fmt-c-writer.module.scm fmt-c-writer.scm sex-fmt-c.o types.o utils.o
$(CHICKEN_C) $(CSC_FLAGS) $(MODULE_FLAGS) fmt-c-writer.module.scm -o fmt-c-writer.o -unit fmt-c-writer -link sex-fmt-c,types,utils
sexc.o: sexc.module.scm infer.o types.o sexc.scm fmt-c-writer.o sex-macros.o sex-modules.o reader.o semen.o utils.o
$(CHICKEN_C) $(CSC_FLAGS) $(MODULE_FLAGS) sexc.module.scm -o sexc.o -unit sexc -link fmt-c-writer,sex-macros,sex-modules,reader,semen,infer,types,utils
# Unit testing
sex-tests:
$(MAKE) -C ./tests sex-tests
cp ./tests/sex-tests ./
sextest:
$(MAKE) -C ./tools/sextest sextest
cp ./tools/sextest/sextest .
SEX_TEST_PROGRAMS = c11 \
closure-signatures \
closures \
comments \
compound-literals \
encapsulation \
feature-flags \
features \
fixpoint \
hello-world \
inference \
lambdas \
lists \
operators \
ops \
precedence \
records \
serialize \
specs \
sums \
threading \
type-shapes \
unicode \
unnamed-params \
wildcards
# 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
# The failure paths are checked end to end; see tests/exit-code/Makefile.
check-exit-code: sexc
@$(MAKE) --no-print-directory -C ./tests/exit-code check SEXC=../../sexc
# A program directly in tests/sex-programs is a test; lib/ holds what
# they import
SEX_TEST_PROGRAMS = $(wildcard tests/sex-programs/*.sex)
check run-tests: sexc sex-tests sextest
./sex-tests && ./sextest --sexc=./sexc $(SEX_TEST_PROGRAMS:%=./tests/sex-programs/%.sex) && $(MAKE) check-modules && $(MAKE) check-exit-code
./sex-tests
./sextest --sexc=./sexc $(SEX_TEST_PROGRAMS)
cd tests/modules && SEXC=$(CURDIR)/sexc $(CSI) -s check.scm
cd tests/exit-code && SEXC=$(CURDIR)/sexc $(CSI) -s check.scm
install: all installdirs
$(INSTALL_PROGRAM) sexc $(DESTDIR)$(bindir)/sexc
@@ -156,38 +94,10 @@ uninstall:
rm -f $(SEX_LIB:lib/%=$(DESTDIR)$(sexlibdir)/%)
-rmdir $(DESTDIR)$(sexlibdir)
$(EGGS_STAMP) deps-update: export CHICKEN_EGG_CACHE := $(EGGS_DIR)/cache
$(EGGS_STAMP) deps-update: export CHICKEN_INSTALL_REPOSITORY := $(EGGS_DIR)
$(EGGS_STAMP) deps-update: export CHICKEN_INSTALL_PREFIX := $(EGGS_DIR)
$(EGGS_STAMP) deps-update: export CHICKEN_REPOSITORY_PATH := $(EGGS_DIR):$(SYSTEM_CHICKEN_REPO)
$(EGGS_STAMP): $(DEPSLOCK)
mkdir -p $(CHICKEN_EGG_CACHE)
$(CHICKEN_INSTALL) -force -from-list $(DEPSLOCK)
touch $@
deps: $(EGGS_STAMP)
deps-update: $(DEPSFILE) deps-clean
mkdir -p $(CHICKEN_EGG_CACHE)
$(CHICKEN_INSTALL) -force $$(cat $(DEPSFILE))
# Local repo only: a system path here would leak distro eggs into the lock.
CHICKEN_REPOSITORY_PATH=$(EGGS_DIR) $(CHICKEN_STATUS) -list | sort > $(DEPSLOCK).tmp
mv $(DEPSLOCK).tmp $(DEPSLOCK)
touch $(EGGS_STAMP)
clean:
rm -f $(OBJ) main.o
rm -f $(OBJ) $(PROGRAM_OBJ) modules.mk
rm -f *.import.scm
rm -f *.link
rm -f *.link $(PROGRAM_OBJ:.o=.link)
rm -f sexc sex-tests sextest
$(MAKE) -C ./tests clean
$(MAKE) -C ./tests/modules clean
$(MAKE) -C ./tools/sextest clean
deps-clean:
rm -rf $(EGGS_DIR)
.PHONY: all check run-tests check-modules check-exit-code \
install install-strip installdirs uninstall \
deps deps-update deps-clean clean sex-tests sextest
.PHONY: all check run-tests install install-strip installdirs uninstall clean

52
eggs.mk Normal file
View File

@@ -0,0 +1,52 @@
# The eggs sexc needs, in a project-local repository when there is one.
#
# make deps install the versions pinned in eggs.lock into .eggs
# make deps-update install the newest of dependencies.txt, and pin them
#
# Without .eggs (packaging for distros) the system repository is used.
CHICKEN_INSTALL ?= chicken-install
CHICKEN_STATUS ?= chicken-status
DEPSFILE = dependencies.txt
DEPSLOCK = eggs.lock
EGGS_DIR := $(abspath .eggs)
# Compiler's own repository (types.db, chicken.*), ignoring ambient venv vars.
SYSTEM_CHICKEN_REPO := $(shell env \
-u CHICKEN_INSTALL_REPOSITORY -u CHICKEN_REPOSITORY_PATH \
-u CHICKEN_EGG_CACHE -u CHICKEN_INSTALL_PREFIX \
$(CHICKEN_INSTALL) -repository 2>/dev/null)
CHICKEN_ABI := $(notdir $(SYSTEM_CHICKEN_REPO))
EGGS_STAMP := $(EGGS_DIR)/.installed-$(CHICKEN_ABI)
# The path replaces CHICKEN's own rather than extending it, so the
# system repository has to be on it too
unexport CHICKEN_INSTALL_REPOSITORY
unexport CHICKEN_EGG_CACHE
unexport CHICKEN_INSTALL_PREFIX
export CHICKEN_REPOSITORY_PATH := $(EGGS_DIR):$(SYSTEM_CHICKEN_REPO)
$(EGGS_STAMP) deps-update: export CHICKEN_EGG_CACHE := $(EGGS_DIR)/cache
$(EGGS_STAMP) deps-update: export CHICKEN_INSTALL_REPOSITORY := $(EGGS_DIR)
$(EGGS_STAMP) deps-update: export CHICKEN_INSTALL_PREFIX := $(EGGS_DIR)
$(EGGS_STAMP): $(DEPSLOCK)
mkdir -p $(CHICKEN_EGG_CACHE)
$(CHICKEN_INSTALL) -force -from-list $(DEPSLOCK)
touch $@
deps: $(EGGS_STAMP)
deps-update: $(DEPSFILE) deps-clean
mkdir -p $(CHICKEN_EGG_CACHE)
$(CHICKEN_INSTALL) -force $$(cat $(DEPSFILE))
# Local repo only: a system path here would leak distro eggs into the lock.
CHICKEN_REPOSITORY_PATH=$(EGGS_DIR) $(CHICKEN_STATUS) -list | sort > $(DEPSLOCK).tmp
mv $(DEPSLOCK).tmp $(DEPSLOCK)
touch $(EGGS_STAMP)
deps-clean:
rm -rf $(EGGS_DIR)
.PHONY: deps deps-update deps-clean

View File

@@ -1,3 +0,0 @@
(module fmt-c-writer (emit-c
sex-line-directives)
"fmt-c-writer.scm")

View File

@@ -4,6 +4,7 @@
scheme
(scheme base) ; make-parameter
(chicken base)
(chicken module)
(chicken string)
(chicken syntax)
brev-separate ; fn, flatten
@@ -16,6 +17,14 @@
types ; array-bound?, named-arg?
utils)
(export
emit-c
sex-line-directives
;; for the tests
atom-to-fmt-c unkebabify type-convert-to-c
process-toplevel-form walk-expr walk-type walk-var walk-fn-def walk-struct)
;;; egg `tree' not ported to CHICKEN 6 yet
(define (tree-map f tree)
(cond ((null? tree) (list))

View File

@@ -1,45 +0,0 @@
(module infer
(;; The IR
tvar?
tvar-id
tvar-classes
tvar-rigid?
fresh-tvar
fresh-rigid-tvar
prim-type? prim-name prim-quals make-prim
ptr-type? ptr-target ptr-quals make-ptr
array-type? array-elt array-size make-array-type
fn-type? fn-ret fn-args fn-variadic? make-fn-type
agg-type? agg-kind agg-name agg-spelling agg-quals make-agg
alias-type? alias-name alias-expansion alias-quals make-alias
unknown-type? the-unknown-type
resolve
underlying
c-primitive?
type-quals
free-tvars
decay
;; The boundary
parse-type
unparse-type
;; Constraints
register-class!
add-instance!
entails?
default-tvar!
default-type-variables!
;; Unification
unify
;; Type schemes
scheme? scheme-vars scheme-constraints scheme-type
make-scheme
generalize
instantiate
substitute)
"infer.scm")

View File

@@ -25,12 +25,58 @@
scheme
(scheme base)
(chicken base)
(chicken module)
matchable
srfi-1
srfi-69
types
utils)
(export
;; The IR
tvar?
tvar-id
tvar-classes
tvar-rigid?
fresh-tvar
fresh-rigid-tvar
prim-type? prim-name prim-quals make-prim
ptr-type? ptr-target ptr-quals make-ptr
array-type? array-elt array-size make-array-type
fn-type? fn-ret fn-args fn-variadic? make-fn-type
agg-type? agg-kind agg-name agg-spelling agg-quals make-agg
alias-type? alias-name alias-expansion alias-quals make-alias
unknown-type? the-unknown-type
resolve
underlying
c-primitive?
type-quals
free-tvars
decay
;; The boundary
parse-type
unparse-type
;; Constraints
register-class!
add-instance!
entails?
default-tvar!
default-type-variables!
;; Unification
unify
;; Type schemes
scheme? scheme-vars scheme-constraints scheme-type
make-scheme
generalize
instantiate
substitute)
;;; ---------------------------------------------------------------
;;; The IR
;;; ---------------------------------------------------------------

View File

@@ -1,6 +0,0 @@
(module reader (read-from-file
read-raw-forms
current-features
platform-features)
"reader.scm")

View File

@@ -16,11 +16,22 @@
scheme
(scheme base) ; make-parameter
(chicken base)
(chicken module)
(chicken pathname)
(chicken platform) ; software-version, machine-type
(only srfi-1 every any) ; srfi-1 also has an append-reverse
utils)
(export
read-from-file
read-raw-forms
current-features
platform-features
;; for the tests
parse-all)
;;; Sentinels for structural tokens
(define close-paren (list '%close-paren))
(define close-bracket (list '%close-bracket))

View File

@@ -1,2 +0,0 @@
(module semen ()
"semen.scm")

View File

@@ -19,7 +19,11 @@
)
(export/rename (process semen-process))
(export closure-env-declaration)
(export closure-env-declaration
;; for the tests
sex-fn? sex-fn-public? sex-fn-name sex-fn-arglist
sex-fn-return-type sex-fn-prototype sex-fn-body
walk-form macro-expand)
;;; for lambda extraction, docstring processing, macro expansion,
;;; injection of module headers, i.e. all things that rearrange code

View File

@@ -1,9 +0,0 @@
(module sex-macros
(register-macro
cat
comment
get-macro
macro?
apply-macro
defmacro)
"sex-macros.scm")

View File

@@ -2,9 +2,19 @@
scheme
(only fmt fmt)
(chicken base)
(chicken module)
(chicken plist)
(chicken string))
(export
register-macro
cat
comment
get-macro
macro?
apply-macro
defmacro)
(define (cat-syms s-1 s-2)
(fmt #f s-1 s-2))

View File

@@ -1,7 +0,0 @@
(module sex-modules
(get-modules-public-forms
load-persistent-module-paths
check-public-interface
module-dependencies
read-public-interface)
"sex-modules.scm")

View File

@@ -4,6 +4,7 @@
(chicken base)
(chicken file)
(chicken load)
(chicken module)
(chicken pathname)
(chicken process-context)
(chicken string)
@@ -14,6 +15,16 @@
types
utils)
(export
get-modules-public-forms
load-persistent-module-paths
check-public-interface
module-dependencies
read-public-interface
;; for the tests
get-module-paths)
(define +persistent-module-paths+ (list))
;;; for guarding against multiple imports (sort of mandatory #pragma

View File

@@ -1 +0,0 @@
(module sexc (main) "sexc.scm")

View File

@@ -4,6 +4,7 @@
(chicken base)
(chicken condition) ; handle-exceptions
(chicken file)
(chicken module)
(chicken plist)
(chicken pretty-print)
(chicken process)
@@ -20,6 +21,13 @@
srfi-1 ; list routines
utils)
(export
main
;; for the tests
args-before-separator args-after-separator
dependency-rule spell-stdout-output)
;;; Main function facilities
(define opts-grammar

View File

@@ -1,53 +0,0 @@
CHICKEN_C = csc
CSC_FLAGS += -K prefix -static
MODULE_FLAGS = -emit-all-import-libraries -module-registration -c
MODULES = utils types infer sex-macros reader sex-modules semen sex-fmt-c fmt-c-writer sexc
SEX_OBJ = $(MODULES:%=%.o)
TESTS = basic semen reader fmt-c-writer utils line-directives codegen encapsulation args module-path types infer
TEST_SRCS = $(TESTS:%=%.scm)
sex-tests: run.scm $(TEST_SRCS) $(SEX_OBJ)
$(CHICKEN_C) $(CSC_FLAGS) run.scm -o sex-tests -link sexc
#------------------------------------------------------------------
utils.o: utils.module.scm ../utils.scm
$(CHICKEN_C) $(CSC_FLAGS) $(MODULE_FLAGS) utils.module.scm -o utils.o -unit utils
types.o: types.module.scm ../types.scm
$(CHICKEN_C) $(CSC_FLAGS) $(MODULE_FLAGS) types.module.scm -o types.o -unit types
infer.o: infer.module.scm ../infer.scm types.o utils.o
$(CHICKEN_C) $(CSC_FLAGS) $(MODULE_FLAGS) infer.module.scm -o infer.o -unit infer -link types,utils
sex-macros.o: sex-macros.module.scm ../sex-macros.scm
$(CHICKEN_C) $(CSC_FLAGS) $(MODULE_FLAGS) sex-macros.module.scm -o sex-macros.o -unit sex-macros
reader.o: reader.module.scm ../reader.scm utils.o
$(CHICKEN_C) $(CSC_FLAGS) $(MODULE_FLAGS) reader.module.scm -o reader.o -unit reader -link utils
sex-modules.o: sex-modules.module.scm ../sex-modules.scm reader.o types.o utils.o
$(CHICKEN_C) $(CSC_FLAGS) $(MODULE_FLAGS) sex-modules.module.scm -o sex-modules.o -unit sex-modules -link reader,types,utils
semen.o: semen.module.scm ../semen.scm infer.o sex-macros.o sex-modules.o types.o utils.o
$(CHICKEN_C) $(CSC_FLAGS) $(MODULE_FLAGS) semen.module.scm -o semen.o -unit semen -link sex-macros,sex-modules,infer,types,utils
sex-fmt-c.o: ../sex-fmt-c.scm
$(CHICKEN_C) $(CSC_FLAGS) $(MODULE_FLAGS) ../sex-fmt-c.scm -o sex-fmt-c.o -unit sex-fmt-c
fmt-c-writer.o: fmt-c-writer.module.scm ../fmt-c-writer.scm sex-fmt-c.o types.o utils.o
$(CHICKEN_C) $(CSC_FLAGS) $(MODULE_FLAGS) fmt-c-writer.module.scm -o fmt-c-writer.o -unit fmt-c-writer -link sex-fmt-c,types,utils
sexc.o: sexc.module.scm infer.o types.o ../sexc.scm fmt-c-writer.o sex-macros.o sex-modules.o reader.o semen.o utils.o
$(CHICKEN_C) $(CSC_FLAGS) $(MODULE_FLAGS) sexc.module.scm -o sexc.o -unit sexc -link fmt-c-writer,sex-macros,sex-modules,reader,semen,infer,types,utils
clean:
rm -f $(SEX_OBJ)
rm -f *.import.scm
rm -f *.link
rm -f sex-tests
.PHONY: clean

View File

@@ -1,32 +0,0 @@
# The failure paths.
#
# Each needs a process to show itself, so none fits the unit suite:
# sexc has to fail when cc fails -- exiting 0 after a failed compile
# makes every driver, sextest included, read it as success -- it has to
# clean up its temporary .c when its own emission throws, and it has to
# refuse `-o -' for an executable, which cc would write to a file named
# `-'.
#
# Everything is built inside a scratch directory, so nothing is left
# here to clean up.
SEXC ?= ../../sexc
check:
@d=`mktemp -d`; \
TMPDIR=$$d $(SEXC) nested-pointer.sex -o $$d/out >/dev/null 2>&1; \
if [ -n "`find $$d -name '*.c'`" ]; then \
echo "exit code FAILED: the temporary .c survived a failed emission"; \
rm -rf $$d; exit 1; \
fi; \
if $(SEXC) hello.sex -o $$d/out -- -no-such-cc-flag >/dev/null 2>&1; then \
echo "exit code FAILED: sexc reported success after cc failed"; \
rm -rf $$d; exit 1; \
fi; \
if (cd $$d && $(abspath $(SEXC)) $(CURDIR)/hello.sex -o - >/dev/null 2>&1) || [ -e $$d/- ]; then \
echo "exit code FAILED: -o - built an executable"; \
rm -rf $$d; exit 1; \
fi; \
rm -rf $$d; echo "exit code ok"
.PHONY: check

62
tests/exit-code/check.scm Normal file
View File

@@ -0,0 +1,62 @@
;;; The failure paths.
;;;
;;; Each needs a process to show itself, so none fits the unit suite:
;;; sexc has to fail when cc fails -- exiting 0 after a failed compile
;;; makes every driver, sextest included, read it as success -- it has to
;;; clean up its temporary .c when its own emission throws, and it has to
;;; refuse `-o -' for an executable, which cc would write to a file named
;;; `-'.
;;;
;;; Everything is built inside a scratch directory, so nothing is left
;;; here to clean up.
;;;
;;; csi -s check.scm
(import scheme
(chicken base)
(chicken file)
(chicken io)
(chicken pathname)
(chicken process)
(chicken process-context)
test)
(define sexc
(let ((path (or (get-environment-variable "SEXC") "../../sexc")))
(if (absolute-pathname? path)
path
(make-pathname (current-directory) path))))
(define scratch (create-temporary-directory))
(define out (make-pathname scratch "out"))
(set-environment-variable! "TMPDIR" scratch)
;;; Whether sexc exits 0 when run with ARGS, saying nothing of it
(define (sexc-succeeds? . args)
(let ((proc (process* sexc args)))
(read-string #f (process-output-port proc))
(read-string #f (process-error-port proc))
(process-wait proc)
(and (process-returned-normally? proc)
(zero? (process-exit-status proc)))))
(test "a failed emission leaves no temporary .c" '()
(begin
(sexc-succeeds? "nested-pointer.sex" "-o" out)
(glob (make-pathname scratch "*.c"))))
(test "a failed cc fails sexc" #f
(sexc-succeeds? "hello.sex" "-o" out "--" "-no-such-cc-flag"))
(define hello (make-pathname (current-directory) "hello.sex"))
(change-directory scratch)
(test "-o - is refused for an executable" #f
(sexc-succeeds? hello "-o" "-"))
(test "and leaves no file named -" #f
(file-exists? "-"))
(delete-directory scratch #t)
(test-exit)

View File

@@ -1,3 +0,0 @@
(module fmt-c-writer
*
"../fmt-c-writer.scm")

View File

@@ -1,45 +0,0 @@
(module infer
(;; The IR
tvar?
tvar-id
tvar-classes
tvar-rigid?
fresh-tvar
fresh-rigid-tvar
prim-type? prim-name prim-quals make-prim
ptr-type? ptr-target ptr-quals make-ptr
array-type? array-elt array-size make-array-type
fn-type? fn-ret fn-args fn-variadic? make-fn-type
agg-type? agg-kind agg-name agg-spelling agg-quals make-agg
alias-type? alias-name alias-expansion alias-quals make-alias
unknown-type? the-unknown-type
resolve
underlying
c-primitive?
type-quals
free-tvars
decay
;; The boundary
parse-type
unparse-type
;; Constraints
register-class!
add-instance!
entails?
default-tvar!
default-type-variables!
;; Unification
unify
;; Type schemes
scheme? scheme-vars scheme-constraints scheme-type
make-scheme
generalize
instantiate
substitute)
"../infer.scm")

View File

@@ -1,56 +0,0 @@
# 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.
#
# It also checks what only a second translation unit can check: that an
# imported type reaches the type database, by expanding a macro that
# reads the imported struct's fields; and that a closure type crossing
# the boundary works both ways -- one built in the module and called
# here, one built here and called there, through a code pointer that is
# static in the other object. And that a record can cross as a handle
# alone, its fields left behind and its methods still found.
#
# The public forms carry comments in their headers, which the reduction
# to a prototype and to an extern both have to look past. And a type the
# app cannot see -- private to greet, or from a module greet imports
# without `pub' -- crosses behind a pointer, declared and no more.
#
# The interface prints as importers get it: the declarations first, then
# the pub forms in the order they were written.
#
# The app's dependency rule names both modules: greet, which it imports,
# and ledger, which only greet does but which decides what the app is
# told of it.
SEXC ?= ../../sexc
EXPECTED = hello, world\nhello, sex\n2 greetings\ntext times \ntext times \nmood 1\nclosure 15 21 201\nvec2 7 7\nvec2(3 4)\ntally 2\nboard 42\nledger 2\ncolour 123
check:
@head="`$(SEXC) --public-interface greet.sex | head -3`"; \
if [ "$$head" != "`printf '(record cell)\n(record ledger)\n(include stdio.h)'`" ]; then \
echo "modules FAILED, the interface begins:"; echo "$$head"; exit 1; \
fi
@$(SEXC) ledger.sex -c -o ledger.o
@$(SEXC) greet.sex -c -o greet.o
@$(SEXC) greet-app.sex -o greet-app --depfile=greet-app.d -- greet.o ledger.o
@if ! head -1 greet-app.d | grep -q '^greet-app: greet-app.sex .*/greet.sex .*/ledger.sex$$'; then \
echo "modules FAILED, the dependency rule is:"; cat greet-app.d; $(MAKE) clean; exit 1; \
fi
@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 ledger.o greet-app greet-app.d
.PHONY: check clean

86
tests/modules/check.scm Normal file
View File

@@ -0,0 +1,86 @@
;;; 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.
;;;
;;; It also checks what only a second translation unit can check: that an
;;; imported type reaches the type database, by expanding a macro that
;;; reads the imported struct's fields; and that a closure type crossing
;;; the boundary works both ways -- one built in the module and called
;;; here, one built here and called there, through a code pointer that is
;;; static in the other object. And that a record can cross as a handle
;;; alone, its fields left behind and its methods still found.
;;;
;;; The public forms carry comments in their headers, which the reduction
;;; to a prototype and to an extern both have to look past. And a type the
;;; app cannot see -- private to greet, or from a module greet imports
;;; without `pub' -- crosses behind a pointer, declared and no more.
;;;
;;; The interface prints as importers get it: the declarations first, then
;;; the pub forms in the order they were written.
;;;
;;; The app's dependency rule names both modules: greet, which it imports,
;;; and ledger, which only greet does but which decides what the app is
;;; told of it.
;;;
;;; csi -s check.scm
(import scheme
(chicken base)
(chicken file)
(chicken io)
(chicken irregex)
(chicken process)
(chicken process-context)
srfi-1
test)
(define sexc (or (get-environment-variable "SEXC") "../../sexc"))
;;; The lines PROGRAM prints when run with ARGS, or #f when it fails
(define (output-of program . args)
(let* ((proc (process program args))
(lines (read-lines (process-output-port proc))))
(process-wait proc)
(and (process-returned-normally? proc)
(zero? (process-exit-status proc))
lines)))
(test "the interface begins with the declarations"
'("(record cell)" "(record ledger)" "(include stdio.h)")
(take (or (output-of sexc "--public-interface" "greet.sex") '()) 3))
(test-assert "the app links with both modules"
(and (output-of sexc "ledger.sex" "-c" "-o" "ledger.o")
(output-of sexc "greet.sex" "-c" "-o" "greet.o")
(output-of sexc "greet-app.sex" "-o" "greet-app" "--depfile=greet-app.d"
"--" "greet.o" "ledger.o")))
(test-assert "the dependency rule names both modules"
(irregex-match "greet-app: greet-app\\.sex .*/greet\\.sex .*/ledger\\.sex"
(call-with-input-file "greet-app.d" read-line)))
(test "the app runs through both"
'("hello, world"
"hello, sex"
"2 greetings"
"text times "
"text times "
"mood 1"
"closure 15 21 201"
"vec2 7 7"
"vec2(3 4)"
"tally 2"
"board 42"
"ledger 2"
"colour 123")
(output-of "./greet-app"))
(for-each delete-file* '("greet.o" "ledger.o" "greet-app" "greet-app.d"))
(test-exit)

View File

@@ -1,3 +0,0 @@
(module reader
*
"../reader.scm")

View File

@@ -1,18 +1,18 @@
(import
test)
(include "basic.scm")
(include "semen.scm")
(include "reader.scm")
(include "fmt-c-writer.scm")
(include "utils.scm")
(include "line-directives.scm")
(include "codegen.scm")
(include "encapsulation.scm")
(include "args.scm")
(include "module-path.scm")
(include "types.scm")
(include "infer.scm")
(include-relative "basic.scm")
(include-relative "semen.scm")
(include-relative "reader.scm")
(include-relative "fmt-c-writer.scm")
(include-relative "utils.scm")
(include-relative "line-directives.scm")
(include-relative "codegen.scm")
(include-relative "encapsulation.scm")
(include-relative "args.scm")
(include-relative "module-path.scm")
(include-relative "types.scm")
(include-relative "infer.scm")
;;; Should be the last in the test suite
(test-exit)

View File

@@ -1,2 +0,0 @@
(module semen *
"../semen.scm")

View File

@@ -1,3 +0,0 @@
(module sex-macros
*
"../sex-macros.scm")

View File

@@ -1,3 +0,0 @@
(module sex-modules
*
"../sex-modules.scm")

View File

@@ -9,7 +9,7 @@
(include stddef.h)
(include stdio.h)
(import list-macros)
(import lib/list-macros)
(struct foo
((a-field float)

View File

@@ -1,3 +0,0 @@
(module sexc
*
"../sexc.scm")

View File

@@ -1,50 +0,0 @@
(module types
(add-struct
add-union
add-enum
add-typedef
add-define
type-match
type-pattern-matches?
map-fields
add-sum!
get-sum
add-constructor!
get-constructor
variant-fields
reset-declarations!
add-spec!
get-spec
spec-parameter
spec-methods
add-realization!
realizes?
normalize-fields
add-method!
get-method
qualify-op
ops-key
add-name-type!
get-name-type
type-of
current-type-of
get-return-type
get-type-info
get-tag-info
get-fields
get-underlying-type
type-head?
named-arg?
named-arg-type
arglist-types
strip-pub
array-bound?
array-element-type)
"../types.scm")

View File

@@ -1,3 +0,0 @@
(module utils
*
"../utils.scm")

130
tools/module-deps.scm Normal file
View File

@@ -0,0 +1,130 @@
;;; The compiler's modules and the programs built from them, as make
;;; reads them: what each one imports, read off its import forms. A
;;; module is a NAME.scm in the current directory; a program is named
;;; with its source, whose include-relative files count as its own.
;;;
;;; csi -s tools/module-deps.scm sexc=main.scm > modules.mk
;;;
;;; prints
;;;
;;; MODULES = fmt-c-writer infer reader ...
;;; PROGRAM_NAMES = sexc
;;; PROGRAM_OBJ = main.o
;;; SOURCES = fmt-c-writer.scm infer.scm ... main.scm
;;; infer.o: types.o utils.o
;;; ...
;;; main.o: sexc.o
;;; sexc: main.o sexc.o
(import scheme
(chicken base)
(chicken file)
(chicken io)
(chicken pathname)
(chicken process-context)
(chicken sort)
(chicken string))
;;; As csc reads them, given -K prefix
(keyword-style #:prefix)
;;; (only sex-macros cat) -> sex-macros
(define (spec-name spec)
(if (and (pair? spec) (memq (car spec) '(only except prefix rename)))
(spec-name (cadr spec))
spec))
;;; dependencies.txt names the eggs, so a stray test.scm is not the
;;; module the tests import
(define eggs (call-with-input-file "dependencies.txt" read-list))
(define (module-file name)
(and (symbol? name)
(not (memq name eggs))
(let ((file (string-append (symbol->string name) ".scm")))
(and (file-exists? file) file))))
;;; The names FILE imports, and the files it includes
(define (scan file)
(let loop ((forms (call-with-input-file file read-list)) (names '()) (files '()))
(if (null? forms)
(values names files)
(let ((form (car forms)))
(cond
((not (pair? form)) (loop (cdr forms) names files))
((eq? (car form) 'import)
(loop (cdr forms) (append (map spec-name (cdr form)) names) files))
((eq? (car form) 'include-relative)
(let ((included (make-pathname (pathname-directory file) (cadr form))))
(let-values (((more-names more-files) (scan included)))
(loop (cdr forms)
(append more-names names)
(append (cons included more-files) files)))))
(else (loop (cdr forms) names files)))))))
(define (symbol<? a b)
(string<? (symbol->string a) (symbol->string b)))
;;; The modules among NAMES, once each
(define (local-modules names)
(let loop ((names names) (acc '()))
(cond ((null? names) (sort acc symbol<?))
((or (memq (car names) acc) (not (module-file (car names))))
(loop (cdr names) acc))
(else (loop (cdr names) (cons (car names) acc))))))
(define (imported-modules file)
(let-values (((names files) (scan file)))
(local-modules names)))
;;; Each module reached from ROOTS, with the modules it imports
(define (module-graph roots)
(let loop ((pending roots) (graph '()))
(cond ((null? pending) (sort graph (lambda (a b) (symbol<? (car a) (car b)))))
((assq (car pending) graph) (loop (cdr pending) graph))
(else
(let ((deps (imported-modules (module-file (car pending)))))
(loop (append (cdr pending) deps)
(cons (cons (car pending) deps) graph)))))))
(define (objects names)
(map (lambda (name) (string-append (symbol->string name) ".o")) names))
(define (rule target prerequisites)
(unless (null? prerequisites)
(print target ": " (string-intersperse prerequisites " "))))
;;; sexc=main.scm, read off main.scm and the files it includes
(define-record-type program
(make-program name object modules sources)
program?
(name program-name)
(object program-object)
(modules program-modules)
(sources program-sources))
(define (read-program spec)
(let* ((parts (string-split spec "="))
(source (cadr parts)))
(let-values (((names files) (scan source)))
(make-program (car parts) (pathname-replace-extension source "o")
(local-modules names) (cons source files)))))
;;; Only core here: modules.mk is made before `make deps' installs eggs
(let* ((programs (map read-program (command-line-arguments)))
(graph (module-graph
(local-modules (apply append (map program-modules programs))))))
(print "MODULES = " (string-intersperse (map (o symbol->string car) graph) " "))
(print "PROGRAM_NAMES = " (string-intersperse (map program-name programs) " "))
(print "PROGRAM_OBJ = " (string-intersperse (map program-object programs) " "))
(print "SOURCES = "
(string-intersperse (append (map (o module-file car) graph)
(apply append (map program-sources programs)))
" "))
(for-each (lambda (node) (rule (car (objects (list (car node)))) (objects (cdr node))))
graph)
(for-each (lambda (p)
(let ((modules (objects (program-modules p))))
(rule (program-object p) (append (cdr (program-sources p)) modules))
(rule (program-name p) (cons (program-object p) modules))))
programs))

View File

@@ -1,23 +0,0 @@
CHICKEN_C = csc
CSC_FLAGS += -K prefix -static
MODULE_FLAGS = -emit-all-import-libraries -module-registration -c
ROOT = ../..
# sextest reuses sexc's reader (and its utils dependency) instead of
# duplicating the S-expression reader. The module wrappers here include
# the shared sources from the project root.
sextest: sextest.scm reader.o utils.o
$(CHICKEN_C) $(CSC_FLAGS) sextest.scm -o sextest -link reader,utils
utils.o: utils.module.scm $(ROOT)/utils.scm
$(CHICKEN_C) $(CSC_FLAGS) $(MODULE_FLAGS) utils.module.scm -o utils.o -unit utils
reader.o: reader.module.scm $(ROOT)/reader.scm utils.o
$(CHICKEN_C) $(CSC_FLAGS) $(MODULE_FLAGS) reader.module.scm -o reader.o -unit reader -link utils
clean:
rm -f *.o *.import.scm *.link sextest
.PHONY: clean

View File

@@ -1,6 +0,0 @@
(module reader (read-from-file
read-raw-forms
current-features
platform-features)
"../../reader.scm")

View File

@@ -1,21 +0,0 @@
(module utils
(get-env-var
set-working-directory
to-absolute-pathname
comment-form?
strip-header-comments
list-split
list-join
recons
current-source-file
set-form-source!
form-source
form-file
form-line
copy-form-source!
stamp-form-source!
form-location
sex-error
with-directory
)
"../../utils.scm")

View File

@@ -1,50 +0,0 @@
(module types
(add-struct
add-union
add-enum
add-typedef
add-define
type-match
type-pattern-matches?
map-fields
add-sum!
get-sum
add-constructor!
get-constructor
variant-fields
reset-declarations!
add-spec!
get-spec
spec-parameter
spec-methods
add-realization!
realizes?
normalize-fields
add-method!
get-method
qualify-op
ops-key
add-name-type!
get-name-type
type-of
current-type-of
get-return-type
get-type-info
get-tag-info
get-fields
get-underlying-type
type-head?
named-arg?
named-arg-type
arglist-types
strip-pub
array-bound?
array-element-type)
"types.scm")

View File

@@ -12,9 +12,60 @@
scheme
(scheme base)
(chicken base)
(chicken module)
srfi-1
srfi-69)
(export
add-struct
add-union
add-enum
add-typedef
add-define
type-match
type-pattern-matches?
map-fields
add-sum!
get-sum
add-constructor!
get-constructor
variant-fields
reset-declarations!
add-spec!
get-spec
spec-parameter
spec-methods
add-realization!
realizes?
normalize-fields
add-method!
get-method
qualify-op
ops-key
add-name-type!
get-name-type
type-of
current-type-of
get-return-type
get-type-info
get-tag-info
get-fields
get-underlying-type
type-head?
named-arg?
named-arg-type
arglist-types
strip-pub
array-bound?
array-element-type)
;;; Two namespaces: `struct point' and a `point' typedef are separate
;;; declarations. Tags -- struct, union and enum alike -- share the
;;; second table between them

View File

@@ -1,25 +0,0 @@
(module utils
(get-env-var
set-working-directory
to-absolute-pathname
comment-form?
strip-header-comments
list-split
list-join
recons
current-source-file
set-form-source!
form-source
form-file
form-line
form-end-line
copy-form-source!
stamp-form-source!
form-location
set-form-type!
form-type
sex-error
sex-warning
with-directory
)
"utils.scm")

View File

@@ -2,11 +2,36 @@
scheme
(scheme base) ; make-parameter
(chicken base)
(chicken module)
(chicken pathname)
(chicken process-context)
srfi-1
srfi-69)
(export
get-env-var
set-working-directory
to-absolute-pathname
comment-form?
strip-header-comments
list-split
list-join
recons
current-source-file
set-form-source!
form-source
form-file
form-line
form-end-line
copy-form-source!
stamp-form-source!
form-location
set-form-type!
form-type
sex-error
sex-warning
with-directory)
(define-syntax prog1
(syntax-rules ()
((prog1 form . forms)