Compare commits

...

11 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
540f5c75cd tidy what the fixes wrote around existing helpers
All checks were successful
Sex CI / build-linux (pull_request) Successful in 5m55s
Sex CI / build-linux (push) Successful in 5m57s
written-type? matches the shapes instead of measuring them, array-form?
and pub-form? replace their spelled-out copies, both test files share
one fails-with?, and moved test comments are back above their groups.
2026-10-19 18:21:19 +03:00
a4880f1b96 process an ops method's prototype once
Registering a method processed its prototype only for the signature it
records, and a method called from above was processed again to be
declared ahead.
2026-10-19 18:21:10 +03:00
64db505e6b work out what a module exports once per unit
Every module's interface check walks the pub imports below it, so a
chain reduced each module's interface once per level above it.
2026-10-19 18:21:02 +03:00
44 changed files with 601 additions and 629 deletions

2
.gitignore vendored
View File

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

166
Makefile
View File

@@ -1,6 +1,5 @@
CHICKEN_C ?= csc CHICKEN_C ?= csc
CHICKEN_INSTALL ?= chicken-install CSI ?= csi
CHICKEN_STATUS ?= chicken-status
CSC_FLAGS += -K prefix -static CSC_FLAGS += -K prefix -static
# What and why: # What and why:
# -emit-all-import-libraries: Emit import-libraries for all defined modules. # -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 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 all: sexc
sexc: $(OBJ) main.scm include eggs.mk
$(CHICKEN_C) $(CSC_FLAGS) main.scm -o sexc-tmp -link sexc
# otherwise csc hangs, probably because it tries to compile to sexc.o first # Each program, and the source it is built from
mv sexc-tmp sexc 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 # Everything is linked with the modules among its prerequisites
$(CHICKEN_C) $(CSC_FLAGS) $(MODULE_FLAGS) utils.module.scm -o utils.o -unit utils LINKS = $(addprefix -link ,$(basename $(filter-out $<,$(filter %.o,$^))))
types.o: types.module.scm types.scm # A module is one file that declares its own exports, made a module by
$(CHICKEN_C) $(CSC_FLAGS) $(MODULE_FLAGS) types.module.scm -o types.o -unit types # -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 %.o: %.scm
$(CHICKEN_C) $(CSC_FLAGS) $(MODULE_FLAGS) infer.module.scm -o infer.o -unit infer -link types,utils $(CHICKEN_C) $(CSC_FLAGS) $(MODULE_FLAGS) $< $(MODULE) -o $@ -unit $* $(LINKS)
sex-macros.o: sex-macros.module.scm sex-macros.scm # A program's object is named after its source: csc names its own after
$(CHICKEN_C) $(CSC_FLAGS) $(MODULE_FLAGS) sex-macros.module.scm -o sex-macros.o -unit sex-macros # -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 $(PROGRAM_NAMES):
$(CHICKEN_C) $(CSC_FLAGS) $(MODULE_FLAGS) reader.module.scm -o reader.o -unit reader -link utils $(CHICKEN_C) $(CSC_FLAGS) $< -o $@ $(LINKS)
sex-modules.o: sex-modules.module.scm sex-modules.scm reader.o types.o utils.o # A program directly in tests/sex-programs is a test; lib/ holds what
$(CHICKEN_C) $(CSC_FLAGS) $(MODULE_FLAGS) sex-modules.module.scm -o sex-modules.o -unit sex-modules -link reader,types,utils # they import
SEX_TEST_PROGRAMS = $(wildcard tests/sex-programs/*.sex)
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
check run-tests: sexc sex-tests sextest 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: all installdirs
$(INSTALL_PROGRAM) sexc $(DESTDIR)$(bindir)/sexc $(INSTALL_PROGRAM) sexc $(DESTDIR)$(bindir)/sexc
@@ -156,38 +94,10 @@ uninstall:
rm -f $(SEX_LIB:lib/%=$(DESTDIR)$(sexlibdir)/%) rm -f $(SEX_LIB:lib/%=$(DESTDIR)$(sexlibdir)/%)
-rmdir $(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: clean:
rm -f $(OBJ) main.o rm -f $(OBJ) $(PROGRAM_OBJ) modules.mk
rm -f *.import.scm rm -f *.import.scm
rm -f *.link rm -f *.link $(PROGRAM_OBJ:.o=.link)
rm -f sexc sex-tests sextest rm -f sexc sex-tests sextest
$(MAKE) -C ./tests clean
$(MAKE) -C ./tests/modules clean
$(MAKE) -C ./tools/sextest clean
deps-clean: .PHONY: all check run-tests install install-strip installdirs uninstall 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

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
(scheme base) ; make-parameter (scheme base) ; make-parameter
(chicken base) (chicken base)
(chicken module)
(chicken string) (chicken string)
(chicken syntax) (chicken syntax)
brev-separate ; fn, flatten brev-separate ; fn, flatten
@@ -16,6 +17,14 @@
types ; array-bound?, named-arg? types ; array-bound?, named-arg?
utils) 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 ;;; egg `tree' not ported to CHICKEN 6 yet
(define (tree-map f tree) (define (tree-map f tree)
(cond ((null? tree) (list)) (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
(scheme base) (scheme base)
(chicken base) (chicken base)
(chicken module)
matchable matchable
srfi-1 srfi-1
srfi-69 srfi-69
types types
utils) 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 ;;; 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
(scheme base) ; make-parameter (scheme base) ; make-parameter
(chicken base) (chicken base)
(chicken module)
(chicken pathname) (chicken pathname)
(chicken platform) ; software-version, machine-type (chicken platform) ; software-version, machine-type
(only srfi-1 every any) ; srfi-1 also has an append-reverse (only srfi-1 every any) ; srfi-1 also has an append-reverse
utils) utils)
(export
read-from-file
read-raw-forms
current-features
platform-features
;; for the tests
parse-all)
;;; Sentinels for structural tokens ;;; Sentinels for structural tokens
(define close-paren (list '%close-paren)) (define close-paren (list '%close-paren))
(define close-bracket (list '%close-bracket)) (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/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, ;;; for lambda extraction, docstring processing, macro expansion,
;;; injection of module headers, i.e. all things that rearrange code ;;; injection of module headers, i.e. all things that rearrange code
@@ -425,12 +429,16 @@
(sex-error form "ops needs a type name" form)) (sex-error form "ops needs a type name" form))
(let* ((methods (map (lambda (m) (register-op type m)) (cddr form))) (let* ((methods (map (lambda (m) (register-op type m)) (cddr form)))
(fns (remove comment-form? methods)) (fns (remove comment-form? methods))
;; the signatures too, which a call's receiver is fitted to
(protos (map (lambda (m) (process-fn (sex-fn-prototype m) (list))) fns))
(bodies (map (lambda (m) (bodies (map (lambda (m)
(if (comment-form? m) (list m) (process-fn m (list)))) (if (comment-form? m) (list m) (process-fn m (list))))
methods)) methods))
(early (filter (lambda (m) (called-before? m methods bodies)) fns))) (early (filter-map (lambda (m proto)
(append (append-map identity (reverse bodies)) (and (called-before? m methods bodies) proto))
(fold (lambda (m acc) (process-fn (sex-fn-prototype m) acc)) acc early))))) fns protos)))
(append (concatenate (reverse bodies))
(fold append acc early)))))
;;; The method's fn form, renamed to vec2/length, once it is registered ;;; The method's fn form, renamed to vec2/length, once it is registered
;;; under `length'; a comment is passed along ;;; under `length'; a comment is passed along
@@ -447,8 +455,6 @@
(let* ((qualified (qualify-op type name)) (let* ((qualified (qualify-op type name))
(renamed (rename-fn form qualified))) (renamed (rename-fn form qualified)))
(add-method! type name qualified) (add-method! type name qualified)
;; its signature too, which a call's receiver is fitted to
(process-fn (sex-fn-prototype renamed) (list))
renamed))))) renamed)))))
;;; Does a body written above METHOD call it? ;;; Does a body written above METHOD call it?
@@ -793,14 +799,12 @@
;;; (* char) (* const char) (* size-t) (unsigned int) types ;;; (* char) (* const char) (* size-t) (unsigned int) types
;;; (* p) (* (g)) (¤ xs 0) expressions ;;; (* p) (* (g)) (¤ xs 0) expressions
(define (written-type? form env) (define (written-type? form env)
(and (pair? form) (match form
;; the length a dereference and a subscript have ((or ('* operand) ('¤ operand _))
(if (= (length form) (case (car form) ((*) 2) ((¤) 3) (else -1)))
(let ((operand (second form)))
(if (symbol? operand) (if (symbol? operand)
(not (or (lookup-name env operand) (get-name-type operand))) (not (or (lookup-name env operand) (get-name-type operand)))
(written-type? operand env))) (written-type? operand env)))
(type-head? form)))) (_ (type-head? form))))
;;; `(var n _ (strlen s))' becomes `(var n size-t (strlen s))'. ;;; `(var n _ (strlen s))' becomes `(var n size-t (strlen s))'.
(define (resolve-wildcard form env) (define (resolve-wildcard form env)
@@ -1141,7 +1145,7 @@
;;; A field of a path, indexed or not: xs, [xs 0] -> xs ;;; A field of a path, indexed or not: xs, [xs 0] -> xs
(define (field-name field) (define (field-name field)
(if (and (pair? field) (eq? '¤ (car field)) (pair? (cdr field))) (if (and (array-form? field) (pair? (cdr field)))
(second field) (second field)
field)) field))

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 scheme
(only fmt fmt) (only fmt fmt)
(chicken base) (chicken base)
(chicken module)
(chicken plist) (chicken plist)
(chicken string)) (chicken string))
(export
register-macro
cat
comment
get-macro
macro?
apply-macro
defmacro)
(define (cat-syms s-1 s-2) (define (cat-syms s-1 s-2)
(fmt #f 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 base)
(chicken file) (chicken file)
(chicken load) (chicken load)
(chicken module)
(chicken pathname) (chicken pathname)
(chicken process-context) (chicken process-context)
(chicken string) (chicken string)
@@ -14,6 +15,16 @@
types types
utils) 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)) (define +persistent-module-paths+ (list))
;;; for guarding against multiple imports (sort of mandatory #pragma ;;; for guarding against multiple imports (sort of mandatory #pragma
@@ -185,8 +196,7 @@
(if (and (pair? v) (pair? (cdr v))) (field-types (second v)) (list))) (if (and (pair? v) (pair? (cdr v))) (field-types (second v)) (list)))
(remove comment-form? variants))) (remove comment-form? variants)))
(('ops type . methods) (('ops type . methods)
(append-map interface-types (append-map interface-types (filter pub-form? methods)))
(filter (lambda (m) (and (pair? m) (eq? (car m) 'pub))) methods)))
(('pub 'realize spec . methods) (('pub 'realize spec . methods)
(append-map (lambda (m) (interface-types (cons 'pub m))) (append-map (lambda (m) (interface-types (cons 'pub m)))
(remove comment-form? methods))) (remove comment-form? methods)))
@@ -290,10 +300,23 @@
(let ((path (locate-module (symbol->string (car modules))))) (let ((path (locate-module (symbol->string (car modules)))))
(if (or (not path) (member path seen)) (if (or (not path) (member path seen))
(next (cdr modules) seen acc) (next (cdr modules) seen acc)
(let ((forms (interface-forms (read-module path)))) (let ((exports (module-exports path)))
(next (append (cdr modules) (imported-modules forms #f)) (next (append (cdr modules) (cdr exports))
(cons path seen) (cons path seen)
(cons (declared-types forms) acc)))))))) (cons (car exports) acc))))))))
;;; The types the module at PATH declares to its importers, and the
;;; modules its interface imports; worked out once, though the check of
;;; every module above it on a pub import chain walks there
(define +module-exports+ (list))
(define (module-exports path)
(cond ((assoc path +module-exports+) => cdr)
(else (let* ((forms (interface-forms (read-module path)))
(exports (cons (declared-types forms)
(imported-modules forms #f))))
(set! +module-exports+ (cons (cons path exports) +module-exports+))
exports))))
(define (public-fn-interface raw-form) (define (public-fn-interface raw-form)
;; (pub fn name args ret . body) -> prototype, keeping a docstring so ;; (pub fn name args ret . body) -> prototype, keeping a docstring so

View File

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

View File

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

@@ -51,13 +51,12 @@ the way CHICKEN prints it -- or #f if it raises none."
((condition-property-accessor 'exn 'arguments) e)))) ((condition-property-accessor 'exn 'arguments) e))))
(begin (thunk) #f))) (begin (thunk) #f)))
(define (error-message source) (define (fails-with? thunk fragment)
"Compile SOURCE and return its error text, or #f if it compiles." (let ((m (failure-message thunk)))
(failure-message (lambda () (sex->c source)))) (and m (string-contains m fragment) #t)))
(define (reports? source fragment) (define (reports? source fragment)
(let ((m (error-message source))) (fails-with? (lambda () (sex->c source)) fragment))
(and m (string-contains m fragment) #t)))
(define (followed-by? source first then) (define (followed-by? source first then)
"Is FIRST in the C compiled from SOURCE, and the next thing past it, "Is FIRST in the C compiled from SOURCE, and the next thing past it,
@@ -90,9 +89,6 @@ whitespace aside, THEN?"
(test-group "codegen" (test-group "codegen"
;; c-switch handed its scrutinee straight to `cat', which only works
;; when it is an atom. Anything else was displayed as a raw
;; s-expression: `switch ((%. e type))'.
;; A case body shared the switch's scope, so two cases could not ;; A case body shared the switch's scope, so two cases could not
;; declare one name, and a label could not be followed by a ;; declare one name, and a label could not be followed by a
;; declaration before C23. Only a body that declares needs a block ;; declaration before C23. Only a body that declares needs a block
@@ -109,6 +105,9 @@ whitespace aside, THEN?"
(not (followed-by? (in-fn "(switch a (case 1 (do (var x int 1) (g x))))") (not (followed-by? (in-fn "(switch a (case 1 (do (var x int 1) (g x))))")
"{" "{")))) "{" "{"))))
;; c-switch handed its scrutinee straight to `cat', which only works
;; when it is an atom. Anything else was displayed as a raw
;; s-expression: `switch ((%. e type))'.
(test-group "switch scrutinee" (test-group "switch scrutinee"
(test-assert "member access" (test-assert "member access"
(emits? "(struct s ((type int))) (fn f ((e (struct s))) void (switch (. e type) (case 1 (g))))" (emits? "(struct s ((type int))) (fn f ((e (struct s))) void (switch (. e type) (case 1 (g))))"
@@ -336,8 +335,6 @@ whitespace aside, THEN?"
(test-assert "pub with nothing to define" (test-assert "pub with nothing to define"
(reports? "(pub 1)" "codegen.sex:1:"))) (reports? "(pub 1)" "codegen.sex:1:")))
;; A nested pointer chain used to silently lose a level:
;; (var p (* (* char))) emitted `char *p'
;; A grouped header, C's parens carried over, died in the writer with ;; A grouped header, C's parens carried over, died in the writer with
;; `(caddr) bad argument type' and no location ;; `(caddr) bad argument type' and no location
(test-group "malformed for" (test-group "malformed for"
@@ -357,6 +354,8 @@ whitespace aside, THEN?"
(reports? "(fn f () void\n (for ((var i int 0) (< i 4) (++ i)) (g i)))" (reports? "(fn f () void\n (for ((var i int 0) (< i 4) (++ i)) (g i)))"
"codegen.sex:2:"))) "codegen.sex:2:")))
;; A nested pointer chain used to silently lose a level:
;; (var p (* (* char))) emitted `char *p'
(test-group "malformed types are rejected" (test-group "malformed types are rejected"
(test-assert "nested pointer chain" (test-assert "nested pointer chain"
(reports? (in-fn "(var p (* (* char)))") "pointer chains are written flat")) (reports? (in-fn "(var p (* (* char)))") "pointer chains are written flat"))

View File

@@ -5,7 +5,7 @@
;;; is checked here is what the compiler refuses, and that it refuses ;;; is checked here is what the compiler refuses, and that it refuses
;;; it with the name of whatever is wrong. ;;; it with the name of whatever is wrong.
;;; ;;;
;;; `sex->c', `read-source', `failure-message', `emits?', `reports?' ;;; `sex->c', `read-source', `fails-with?', `emits?', `reports?'
;;; and `in-fn' come from codegen.scm, which run.scm includes first. ;;; and `in-fn' come from codegen.scm, which run.scm includes first.
(import sex-modules) (import sex-modules)
@@ -16,8 +16,7 @@ ahead of its pub forms."
(check-public-interface (read-source source))) (check-public-interface (read-source source)))
(define (interface-reports? source fragment) (define (interface-reports? source fragment)
(let ((m (failure-message (lambda () (interface-of source))))) (fails-with? (lambda () (interface-of source)) fragment))
(and m (string-contains m fragment) #t)))
(define (ops-src . body) (define (ops-src . body)
(string-append (string-append

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,55 +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:
@if [ "`$(SEXC) --public-interface greet.sex | head -3`" != "`printf '(record cell)\n(record ledger)\n(include stdio.h)'`" ]; then \
echo "modules FAILED, the interface begins:"; $(SEXC) --public-interface greet.sex | head -3; 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 (import
test) test)
(include "basic.scm") (include-relative "basic.scm")
(include "semen.scm") (include-relative "semen.scm")
(include "reader.scm") (include-relative "reader.scm")
(include "fmt-c-writer.scm") (include-relative "fmt-c-writer.scm")
(include "utils.scm") (include-relative "utils.scm")
(include "line-directives.scm") (include-relative "line-directives.scm")
(include "codegen.scm") (include-relative "codegen.scm")
(include "encapsulation.scm") (include-relative "encapsulation.scm")
(include "args.scm") (include-relative "args.scm")
(include "module-path.scm") (include-relative "module-path.scm")
(include "types.scm") (include-relative "types.scm")
(include "infer.scm") (include-relative "infer.scm")
;;; Should be the last in the test suite ;;; Should be the last in the test suite
(test-exit) (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 stddef.h)
(include stdio.h) (include stdio.h)
(import list-macros) (import lib/list-macros)
(struct foo (struct foo
((a-field float) ((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
(scheme base) (scheme base)
(chicken base) (chicken base)
(chicken module)
srfi-1 srfi-1
srfi-69) 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 ;;; Two namespaces: `struct point' and a `point' typedef are separate
;;; declarations. Tags -- struct, union and enum alike -- share the ;;; declarations. Tags -- struct, union and enum alike -- share the
;;; second table between them ;;; 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
(scheme base) ; make-parameter (scheme base) ; make-parameter
(chicken base) (chicken base)
(chicken module)
(chicken pathname) (chicken pathname)
(chicken process-context) (chicken process-context)
srfi-1 srfi-1
srfi-69) 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 (define-syntax prog1
(syntax-rules () (syntax-rules ()
((prog1 form . forms) ((prog1 form . forms)