Compare commits
48 Commits
0.0.1
...
d8903c38f1
| Author | SHA1 | Date | |
|---|---|---|---|
| d8903c38f1 | |||
| ebe7969553 | |||
| 2b840eb8c0 | |||
| 3b717f9634 | |||
| 2cd980bdf1 | |||
| 434ac778bf | |||
| 1a899d9f3c | |||
| 69b38e7c9f | |||
| c317d9950c | |||
| 6ced17c20e | |||
| dd1718dffb | |||
| 4ac386f373 | |||
| 1599832ece | |||
| 99a41b9d04 | |||
| 4d31e2ed9b | |||
| d0e806bf84 | |||
| d92b5fc696 | |||
| fd6520dbe8 | |||
| 4ad4133d92 | |||
| 6a8c68fe3d | |||
| ed7bf0879a | |||
| a95e55fddc | |||
| 9b503c1017 | |||
| 8db435f994 | |||
| 286936488b | |||
| 86830ee4c9 | |||
| 17badcb966 | |||
| 31d876e30e | |||
| d94daa9066 | |||
| 24f11cdcba | |||
| acd7010e6b | |||
| f8c03d17c0 | |||
| 250713e01d | |||
| 314bdc4e2d | |||
| b9b3f9da5f | |||
| 2673b25f1d | |||
| c162005241 | |||
| 12d0d13a7c | |||
| 36ff456d24 | |||
| 566e8326a3 | |||
| d41960e407 | |||
| 85511ae037 | |||
| 1acdd0e708 | |||
| c06845a80a | |||
| 816c1db9ed | |||
| e465f6892a | |||
| 3701aeab3f | |||
| 8d1c12d03b |
2
.gitignore
vendored
2
.gitignore
vendored
@@ -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
|
||||||
|
|||||||
165
Makefile
165
Makefile
@@ -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,112 +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 \
|
|
||||||
feature-flags \
|
|
||||||
features \
|
|
||||||
fixpoint \
|
|
||||||
hello-world \
|
|
||||||
inference \
|
|
||||||
lambdas \
|
|
||||||
lists \
|
|
||||||
operators \
|
|
||||||
ops \
|
|
||||||
records \
|
|
||||||
serialize \
|
|
||||||
specs \
|
|
||||||
sums \
|
|
||||||
threading \
|
|
||||||
type-shapes \
|
|
||||||
unicode \
|
|
||||||
unnamed-params \
|
|
||||||
wildcards \
|
|
||||||
worked-example
|
|
||||||
|
|
||||||
# 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
|
||||||
@@ -155,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
52
eggs.mk
Normal 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
|
||||||
@@ -1,3 +0,0 @@
|
|||||||
(module fmt-c-writer (emit-c
|
|
||||||
sex-line-directives)
|
|
||||||
"fmt-c-writer.scm")
|
|
||||||
101
fmt-c-writer.scm
101
fmt-c-writer.scm
@@ -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))
|
||||||
@@ -46,10 +55,10 @@ source location. Only statement positions may be walked this way: a
|
|||||||
(append-map (lambda (s)
|
(append-map (lambda (s)
|
||||||
(let ((src (form-source s)))
|
(let ((src (form-source s)))
|
||||||
(if src
|
(if src
|
||||||
(list (line-directive src) (walk-expr s))
|
(list (line-directive src) (walk-statement s))
|
||||||
(list (walk-expr s)))))
|
(list (walk-statement s)))))
|
||||||
(pack-comments stmts))
|
(pack-comments stmts))
|
||||||
(map walk-expr (pack-comments stmts))))
|
(map walk-statement (pack-comments stmts))))
|
||||||
|
|
||||||
(define (walk-stmt s)
|
(define (walk-stmt s)
|
||||||
"A statement in a slot that holds exactly one form -- an `if' arm.
|
"A statement in a slot that holds exactly one form -- an `if' arm.
|
||||||
@@ -59,8 +68,18 @@ as an `else if' chain, so the anchor and the statement are wrapped in
|
|||||||
surrounding c-block supplies them)."
|
surrounding c-block supplies them)."
|
||||||
(let ((src (and (pair? s) (anchor-statements?) (form-source s))))
|
(let ((src (and (pair? s) (anchor-statements?) (form-source s))))
|
||||||
(if src
|
(if src
|
||||||
`(%begin ,(line-directive src) ,(walk-expr s))
|
`(%begin ,(line-directive src) ,(walk-statement s))
|
||||||
(walk-expr s))))
|
(walk-statement s))))
|
||||||
|
|
||||||
|
;;; Only here is an `if' a statement, with statements for arms that can
|
||||||
|
;;; each be anchored. Anywhere else it is `?:', and a #line between its
|
||||||
|
;;; parts does not compile
|
||||||
|
(define (walk-statement s)
|
||||||
|
(match s
|
||||||
|
(('if . clauses)
|
||||||
|
(with-comments (filter comment-form? clauses)
|
||||||
|
(cons 'if (walk-if-clauses (remove comment-form? clauses)))))
|
||||||
|
(_ (walk-expr s))))
|
||||||
|
|
||||||
;;; A `;' comment reads as a form, so one written inside a construct
|
;;; A `;' comment reads as a form, so one written inside a construct
|
||||||
;;; with positional slots lands in a slot and shifts everything after
|
;;; with positional slots lands in a slot and shifts everything after
|
||||||
@@ -84,6 +103,22 @@ forms, and what remains."
|
|||||||
form
|
form
|
||||||
`(%begin ,@(map walk-expr comments) ,form)))
|
`(%begin ,@(map walk-expr comments) ,form)))
|
||||||
|
|
||||||
|
;;; (for init test step body ...). C's parens around the header, carried
|
||||||
|
;;; over, leave all three parts in init's slot and the body in test's
|
||||||
|
(define (check-for-header form slots)
|
||||||
|
(cond ((and (pair? slots) (list? (car slots)) (= 3 (length (car slots)))
|
||||||
|
(every pair? (car slots)))
|
||||||
|
(sex-error form "a for header is not grouped: write (for init test step body ...)"))
|
||||||
|
((< (length slots) 3)
|
||||||
|
(sex-error form "malformed for: write (for init test step body ...)"))))
|
||||||
|
|
||||||
|
;;; A case body that declares is a block, so two cases can each declare
|
||||||
|
;;; a `bits', and a declaration can follow the label before C23
|
||||||
|
(define (walk-case-body body)
|
||||||
|
(if (any (lambda (form) (and (pair? form) (eq? 'var (car form)))) body)
|
||||||
|
(list (cons '%block-begin (walk-body body)))
|
||||||
|
(walk-body body)))
|
||||||
|
|
||||||
(define (walk-if-clauses clauses)
|
(define (walk-if-clauses clauses)
|
||||||
"(test stmt test stmt ... [else-stmt]) -- tests stay expressions."
|
"(test stmt test stmt ... [else-stmt]) -- tests stay expressions."
|
||||||
(let loop ((cs clauses) (acc (list)))
|
(let loop ((cs clauses) (acc (list)))
|
||||||
@@ -102,15 +137,24 @@ forms, and what remains."
|
|||||||
;;; hand-written vec2__length would collide, and an identifier holding
|
;;; hand-written vec2__length would collide, and an identifier holding
|
||||||
;;; `__' is reserved in C++, which a pub header may be included from.
|
;;; `__' is reserved in C++, which a pub header may be included from.
|
||||||
;;; Lifted closures carry a ƛ for the same reason.
|
;;; Lifted closures carry a ƛ for the same reason.
|
||||||
|
;;;
|
||||||
|
;;; zero? -> zeroʔ, clear! -> clearꜝ: a glottal stop and a raised
|
||||||
|
;;; exclamation mark, both letters. Not the retroflex click ǃ, which
|
||||||
|
;;; looks more like `!' -- enough that clang warns at every use.
|
||||||
|
(define +name-spellings+
|
||||||
|
(map (lambda (spelling) (cons (irregex (car spelling)) (cdr spelling)))
|
||||||
|
'(("-(?!>)" . "_") ("/" . "ː") ("\\?" . "ʔ") ("!" . "ꜝ"))))
|
||||||
|
|
||||||
(define (unkebabify sym)
|
(define (unkebabify sym)
|
||||||
(case sym
|
(let ((name (symbol->string sym)))
|
||||||
((- -- -> -= / /=) sym) ; bare operators, not names
|
(if (or (memq sym '(- -- -> -= / /= ! != ?)) ; bare operators, not names
|
||||||
(else
|
(not (string-index name (lambda (c) (memv c '(#\- #\/ #\? #\!))))))
|
||||||
(string->symbol
|
sym
|
||||||
(irregex-replace/all
|
(string->symbol
|
||||||
"/"
|
(fold (lambda (spelling name)
|
||||||
(irregex-replace/all "-(?!>)" (symbol->string sym) "_")
|
(irregex-replace/all (car spelling) name (cdr spelling)))
|
||||||
"ː")))))
|
name
|
||||||
|
+name-spellings+)))))
|
||||||
|
|
||||||
(define (atom-to-fmt-c atom)
|
(define (atom-to-fmt-c atom)
|
||||||
(case atom
|
(case atom
|
||||||
@@ -195,6 +239,12 @@ forms, and what remains."
|
|||||||
;; name `->' itself being the threading macro
|
;; name `->' itself being the threading macro
|
||||||
(('%-> . rest) (cons '%-> (map walk-expr rest)))
|
(('%-> . rest) (cons '%-> (map walk-expr rest)))
|
||||||
(('var . _) (walk-var form))
|
(('var . _) (walk-var form))
|
||||||
|
;; semen has told a type from an expression; only it knows the names
|
||||||
|
;; in scope that make `(* p)' one or the other
|
||||||
|
(('sizeof ('%type type)) `(%sizeof-type ,(walk-type type)))
|
||||||
|
;; semen has already put the target first, the way C writes it
|
||||||
|
(('typedef type name)
|
||||||
|
`(typedef ,(walk-type type) ,(atom-to-fmt-c name)))
|
||||||
;; An expression has no room for a statement, so a comment in a
|
;; An expression has no room for a statement, so a comment in a
|
||||||
;; cast is dropped rather than relocated.
|
;; cast is dropped rather than relocated.
|
||||||
(('cast . rest)
|
(('cast . rest)
|
||||||
@@ -209,15 +259,14 @@ forms, and what remains."
|
|||||||
;; and each is spliced or wrapped according to what the
|
;; and each is spliced or wrapped according to what the
|
||||||
;; corresponding fmt-c procedure accepts.
|
;; corresponding fmt-c procedure accepts.
|
||||||
(('do . stmts) (cons '%block-begin (walk-body stmts)))
|
(('do . stmts) (cons '%block-begin (walk-body stmts)))
|
||||||
(('if . clauses)
|
(('if . clauses) (cons 'if (map walk-expr (remove comment-form? clauses))))
|
||||||
(with-comments (filter comment-form? clauses)
|
|
||||||
(cons 'if (walk-if-clauses (remove comment-form? clauses)))))
|
|
||||||
(('while . rest)
|
(('while . rest)
|
||||||
(let-values (((comments slots body) (take-slots rest 1)))
|
(let-values (((comments slots body) (take-slots rest 1)))
|
||||||
(with-comments comments
|
(with-comments comments
|
||||||
(cons* 'while (walk-expr (car slots)) (walk-body body)))))
|
(cons* 'while (walk-expr (car slots)) (walk-body body)))))
|
||||||
(('for . rest)
|
(('for . rest)
|
||||||
(let-values (((comments slots body) (take-slots rest 3)))
|
(let-values (((comments slots body) (take-slots rest 3)))
|
||||||
|
(check-for-header form slots)
|
||||||
(with-comments comments
|
(with-comments comments
|
||||||
(cons* 'for (walk-expr (car slots)) (walk-expr (cadr slots))
|
(cons* 'for (walk-expr (car slots)) (walk-expr (cadr slots))
|
||||||
(walk-expr (caddr slots))
|
(walk-expr (caddr slots))
|
||||||
@@ -236,12 +285,12 @@ forms, and what remains."
|
|||||||
(('case . rest)
|
(('case . rest)
|
||||||
(let-values (((comments slots body) (take-slots rest 1)))
|
(let-values (((comments slots body) (take-slots rest 1)))
|
||||||
(with-comments comments
|
(with-comments comments
|
||||||
(cons* 'case (walk-expr (car slots)) (walk-body body)))))
|
(cons* 'case (walk-expr (car slots)) (walk-case-body body)))))
|
||||||
(('case/fallthrough . rest)
|
(('case/fallthrough . rest)
|
||||||
(let-values (((comments slots body) (take-slots rest 1)))
|
(let-values (((comments slots body) (take-slots rest 1)))
|
||||||
(with-comments comments
|
(with-comments comments
|
||||||
(cons* 'case/fallthrough (walk-expr (car slots)) (walk-body body)))))
|
(cons* 'case/fallthrough (walk-expr (car slots)) (walk-case-body body)))))
|
||||||
(('default . body) (cons 'default (walk-body body)))
|
(('default . body) (cons 'default (walk-case-body body)))
|
||||||
|
|
||||||
;; Drop comments so they will not generate additional comma
|
;; Drop comments so they will not generate additional comma
|
||||||
(else (map walk-expr (remove comment-form? form)))))
|
(else (map walk-expr (remove comment-form? form)))))
|
||||||
@@ -316,7 +365,7 @@ forms, and what remains."
|
|||||||
(match form
|
(match form
|
||||||
(('¤ . array-type)
|
(('¤ . array-type)
|
||||||
(if (array-bound? form)
|
(if (array-bound? form)
|
||||||
`(%array ,(walk-type (array-element-type form)) ,(last array-type))
|
`(%array ,(walk-type (array-element-type form)) ,(walk-expr (last array-type)))
|
||||||
;; sugar for pointer... Do we really need it? Guess why not,
|
;; sugar for pointer... Do we really need it? Guess why not,
|
||||||
;; it's a strong semantic cue
|
;; it's a strong semantic cue
|
||||||
`(%array ,(walk-type (array-element-type form)))))
|
`(%array ,(walk-type (array-element-type form)))))
|
||||||
@@ -447,11 +496,17 @@ forms, and what remains."
|
|||||||
(('enum (? symbol? name))
|
(('enum (? symbol? name))
|
||||||
`(enum ,(atom-to-fmt-c name)))
|
`(enum ,(atom-to-fmt-c name)))
|
||||||
(('enum (values ...))
|
(('enum (values ...))
|
||||||
`(enum ,(map atom-to-fmt-c values)))
|
`(enum ,(map walk-enumerator values)))
|
||||||
(('enum name (values ...))
|
(('enum name (values ...))
|
||||||
`(enum ,(atom-to-fmt-c name) ,(map atom-to-fmt-c values)))
|
`(enum ,(atom-to-fmt-c name) ,(map walk-enumerator values)))
|
||||||
(else (sex-error form "malformed enum" form))))
|
(else (sex-error form "malformed enum" form))))
|
||||||
|
|
||||||
|
;;; red -> red, (wire-varint 0) -> wire_varint = 0
|
||||||
|
(define (walk-enumerator value)
|
||||||
|
(match value
|
||||||
|
((name expr) (list (atom-to-fmt-c name) (walk-expr expr)))
|
||||||
|
(_ (atom-to-fmt-c value))))
|
||||||
|
|
||||||
(define (walk-extern form)
|
(define (walk-extern form)
|
||||||
(match form
|
(match form
|
||||||
(('fn . _)
|
(('fn . _)
|
||||||
@@ -499,7 +554,7 @@ forms, and what remains."
|
|||||||
;; A target is a filename and not an identifier: GL/gl.h is a path,
|
;; A target is a filename and not an identifier: GL/gl.h is a path,
|
||||||
;; and unkebabify would spell it GLːgl.h
|
;; and unkebabify would spell it GLːgl.h
|
||||||
(('include target) (list '%include target))
|
(('include target) (list '%include target))
|
||||||
(('define name . rest) `(%define ,(atom-to-fmt-c name) ,@rest))
|
(('define name . body) `(%define ,(walk-expr name) ,@(map walk-expr body)))
|
||||||
(else (walk-expr form))))
|
(else (walk-expr form))))
|
||||||
|
|
||||||
;;; Two toplevel forms are separated by a blank line only if they were
|
;;; Two toplevel forms are separated by a blank line only if they were
|
||||||
|
|||||||
@@ -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")
|
|
||||||
46
infer.scm
46
infer.scm
@@ -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
|
||||||
;;; ---------------------------------------------------------------
|
;;; ---------------------------------------------------------------
|
||||||
|
|||||||
@@ -1,6 +0,0 @@
|
|||||||
(module reader (read-from-file
|
|
||||||
read-raw-forms
|
|
||||||
|
|
||||||
current-features
|
|
||||||
platform-features)
|
|
||||||
"reader.scm")
|
|
||||||
53
reader.scm
53
reader.scm
@@ -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))
|
||||||
@@ -179,10 +190,37 @@
|
|||||||
(cond
|
(cond
|
||||||
((string=? s ".") dot-token)
|
((string=? s ".") dot-token)
|
||||||
((string->number s) => identity)
|
((string->number s) => identity)
|
||||||
|
((radix-number s) => identity)
|
||||||
|
((negated-name? s) (list '- (string->symbol (substring s 1))))
|
||||||
(else (string->symbol s))))
|
(else (string->symbol s))))
|
||||||
|
|
||||||
;;; #-dispatch: booleans, characters, vectors, block/datum comments,
|
;;; 0xE0 and 0b101 are C's spelling of 224 and 5, and are read as those
|
||||||
;;; feature expressions
|
;;; numbers: the C written from them says 224 and 5
|
||||||
|
(define (radix-number s)
|
||||||
|
(let* ((negative? (and (> (string-length s) 0) (char=? #\- (string-ref s 0))))
|
||||||
|
(digits (if negative? (substring s 1) s)))
|
||||||
|
(and (> (string-length digits) 2)
|
||||||
|
(char=? #\0 (string-ref digits 0))
|
||||||
|
(let ((radix (case (string-ref digits 1)
|
||||||
|
((#\x #\X) 16)
|
||||||
|
((#\b #\B) 2)
|
||||||
|
(else #f)))
|
||||||
|
(body (substring digits 2)))
|
||||||
|
(and radix
|
||||||
|
(not (memv (string-ref body 0) '(#\+ #\-)))
|
||||||
|
(let ((n (string->number body radix)))
|
||||||
|
(and (exact-integer? n) (if negative? (- n) n))))))))
|
||||||
|
|
||||||
|
;;; -INFINITY is (- INFINITY), as -1 is minus one. No name begins with
|
||||||
|
;;; a dash, and an operator that does goes on with a non-letter: -> --
|
||||||
|
(define (negated-name? s)
|
||||||
|
(and (> (string-length s) 1)
|
||||||
|
(char=? #\- (string-ref s 0))
|
||||||
|
(let ((c (string-ref s 1)))
|
||||||
|
(or (char-alphabetic? c) (char=? c #\_)))))
|
||||||
|
|
||||||
|
;;; #-dispatch: booleans, characters, vectors, numbers in a radix,
|
||||||
|
;;; block/datum comments, feature expressions
|
||||||
(define (read-hash port)
|
(define (read-hash port)
|
||||||
(let ((c (get-ch port)))
|
(let ((c (get-ch port)))
|
||||||
(cond
|
(cond
|
||||||
@@ -191,6 +229,9 @@
|
|||||||
((or (char=? c #\f) (char=? c #\F)) (read-bool port #f))
|
((or (char=? c #\f) (char=? c #\F)) (read-bool port #f))
|
||||||
((char=? c #\\) (read-char-lit port))
|
((char=? c #\\) (read-char-lit port))
|
||||||
((char=? c #\() (list->vector (read-list port close-paren)))
|
((char=? c #\() (list->vector (read-list port close-paren)))
|
||||||
|
((memv c '(#\x #\X)) (read-radix port 16))
|
||||||
|
((memv c '(#\b #\B)) (read-radix port 2))
|
||||||
|
((memv c '(#\o #\O)) (read-radix port 8))
|
||||||
((char=? c #\|) (skip-block-comment port 1) (next-token port))
|
((char=? c #\|) (skip-block-comment port 1) (next-token port))
|
||||||
((char=? c #\;) (read-datum port) (next-token port)) ; datum comment
|
((char=? c #\;) (read-datum port) (next-token port)) ; datum comment
|
||||||
((char=? c #\+) (read-conditional port #t))
|
((char=? c #\+) (read-conditional port #t))
|
||||||
@@ -244,6 +285,14 @@
|
|||||||
((datum-token? guarded) (next-token port))
|
((datum-token? guarded) (next-token port))
|
||||||
(else guarded))))
|
(else guarded))))
|
||||||
|
|
||||||
|
;;; #x80, #b101, #o10
|
||||||
|
(define (read-radix port radix)
|
||||||
|
(let* ((digits (read-atom-string port))
|
||||||
|
(n (string->number digits radix)))
|
||||||
|
(if (exact-integer? n)
|
||||||
|
n
|
||||||
|
(error "Malformed number" digits))))
|
||||||
|
|
||||||
;;; #t / #true / #f / #false. `val' is the boolean; consume any
|
;;; #t / #true / #f / #false. `val' is the boolean; consume any
|
||||||
;;; trailing name characters and validate
|
;;; trailing name characters and validate
|
||||||
(define (read-bool port val)
|
(define (read-bool port val)
|
||||||
|
|||||||
@@ -1,2 +0,0 @@
|
|||||||
(module semen ()
|
|
||||||
"semen.scm")
|
|
||||||
161
semen.scm
161
semen.scm
@@ -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
|
||||||
@@ -415,17 +419,28 @@
|
|||||||
;;; is (pub fn vec2/length ...) plus a registry entry, so that
|
;;; is (pub fn vec2/length ...) plus a registry entry, so that
|
||||||
;;; (. v length) can find it later. The block itself never reaches the
|
;;; (. v length) can find it later. The block itself never reaches the
|
||||||
;;; writer; `pub' inside it means what it means outside.
|
;;; writer; `pub' inside it means what it means outside.
|
||||||
|
;;;
|
||||||
|
;;; Every method is registered before any body is walked, so one may
|
||||||
|
;;; call another written below it; that one is then declared ahead of
|
||||||
|
;;; the first body, since C wants it declared before the call.
|
||||||
(define (process-ops form acc)
|
(define (process-ops form acc)
|
||||||
(let ((type (and (pair? (cdr form)) (cadr form))))
|
(let ((type (and (pair? (cdr form)) (cadr form))))
|
||||||
(unless (symbol? type)
|
(unless (symbol? type)
|
||||||
(sex-error form "ops needs a type name" form))
|
(sex-error form "ops needs a type name" form))
|
||||||
(fold (lambda (method acc) (process-op type method acc))
|
(let* ((methods (map (lambda (m) (register-op type m)) (cddr form)))
|
||||||
acc
|
(fns (remove comment-form? methods))
|
||||||
(cddr form))))
|
(bodies (map (lambda (m)
|
||||||
|
(if (comment-form? m) (list m) (process-fn m (list))))
|
||||||
|
methods))
|
||||||
|
(early (filter (lambda (m) (called-before? m methods bodies)) fns)))
|
||||||
|
(append (append-map identity (reverse bodies))
|
||||||
|
(fold (lambda (m acc) (process-fn (sex-fn-prototype m) acc)) acc early)))))
|
||||||
|
|
||||||
(define (process-op type form acc)
|
;;; The method's fn form, renamed to vec2/length, once it is registered
|
||||||
|
;;; under `length'; a comment is passed along
|
||||||
|
(define (register-op type form)
|
||||||
(if (comment-form? form)
|
(if (comment-form? form)
|
||||||
(cons form acc)
|
form
|
||||||
(let ((core (and (pair? form) (fn-core form))))
|
(let ((core (and (pair? form) (fn-core form))))
|
||||||
(unless (and (pair? core) (eq? (car core) 'fn)
|
(unless (and (pair? core) (eq? (car core) 'fn)
|
||||||
(pair? (cdr core)) (symbol? (second core)))
|
(pair? (cdr core)) (symbol? (second core)))
|
||||||
@@ -433,9 +448,25 @@
|
|||||||
(let ((name (second core)))
|
(let ((name (second core)))
|
||||||
(when (get-method type name)
|
(when (get-method type name)
|
||||||
(sex-error form "method is already defined on" type name))
|
(sex-error form "method is already defined on" type name))
|
||||||
(let ((qualified (qualify-op type name)))
|
(let* ((qualified (qualify-op type name))
|
||||||
|
(renamed (rename-fn form qualified)))
|
||||||
(add-method! type name qualified)
|
(add-method! type name qualified)
|
||||||
(process-fn (rename-fn form qualified) acc))))))
|
;; its signature too, which a call's receiver is fitted to
|
||||||
|
(process-fn (sex-fn-prototype renamed) (list))
|
||||||
|
renamed)))))
|
||||||
|
|
||||||
|
;;; Does a body written above METHOD call it?
|
||||||
|
(define (called-before? method methods bodies)
|
||||||
|
(let ((name (sex-fn-name method)))
|
||||||
|
(let loop ((ms methods) (bs bodies))
|
||||||
|
(and (not (eq? (car ms) method))
|
||||||
|
(or (mentions? (car bs) name)
|
||||||
|
(loop (cdr ms) (cdr bs)))))))
|
||||||
|
|
||||||
|
;;; (pub fn name args ret ...) -> (pub fn name args ret)
|
||||||
|
(define (sex-fn-prototype form)
|
||||||
|
(let ((header (strip-fn-header-comments form)))
|
||||||
|
(copy-form-source! form (take header (fn-header-length header)))))
|
||||||
|
|
||||||
(define (rename-fn form name)
|
(define (rename-fn form name)
|
||||||
(if (memq (first form) '(pub extern))
|
(if (memq (first form) '(pub extern))
|
||||||
@@ -458,25 +489,6 @@
|
|||||||
(set! (hash-table-ref env :scopes) (list parameters))
|
(set! (hash-table-ref env :scopes) (list parameters))
|
||||||
env))
|
env))
|
||||||
|
|
||||||
;;; `(p (* struct point))' writes the type in one piece and
|
|
||||||
;;; `(p * struct point)' spreads it over the rest; both are
|
|
||||||
;;; `(* struct point)'
|
|
||||||
(define (named-arg-type param)
|
|
||||||
(if (null? (cddr param))
|
|
||||||
(second param)
|
|
||||||
(cdr param)))
|
|
||||||
|
|
||||||
;;; (fn sum ((a int) (b int)) int ...) -> (fn ((int) (int)) int), and
|
|
||||||
;;; (self * const v) is ((* const v)) just as (self (* const v)) is.
|
|
||||||
;;; Anything that names nothing -- a variadic tail -- goes through
|
|
||||||
;;; untouched.
|
|
||||||
(define (arglist-types arglist)
|
|
||||||
(map (lambda (param)
|
|
||||||
(if (named-arg? param)
|
|
||||||
(list (named-arg-type param))
|
|
||||||
param))
|
|
||||||
arglist))
|
|
||||||
|
|
||||||
(define (fn-type-of fn-form)
|
(define (fn-type-of fn-form)
|
||||||
`(fn ,(arglist-types (sex-fn-arglist fn-form))
|
`(fn ,(arglist-types (sex-fn-arglist fn-form))
|
||||||
,(sex-fn-return-type fn-form)))
|
,(sex-fn-return-type fn-form)))
|
||||||
@@ -501,9 +513,8 @@
|
|||||||
;;; (while (< v 0) (var v char 1) ...)
|
;;; (while (< v 0) (var v char 1) ...)
|
||||||
;;; (var m _ (+ v 1)) ; double, not char
|
;;; (var m _ (+ v 1)) ; double, not char
|
||||||
;;;
|
;;;
|
||||||
;;; One frame per form, not one per arm: a `case' label opens no scope
|
;;; A `case' body gets one too, being written as a block. An `if' arm
|
||||||
;;; in C, and an `if' arm can only declare inside a `do', which brings
|
;;; gets none: it can only declare inside a `do', which brings its own.
|
||||||
;;; its own.
|
|
||||||
|
|
||||||
(define (declare-name! env name type)
|
(define (declare-name! env name type)
|
||||||
(hash-table-set! (car (hash-table-ref env :scopes)) name type))
|
(hash-table-set! (car (hash-table-ref env :scopes)) name type))
|
||||||
@@ -572,7 +583,7 @@
|
|||||||
|
|
||||||
(define (walk-statement form env)
|
(define (walk-statement form env)
|
||||||
(cond
|
(cond
|
||||||
((vector? form) (walk-literal form env))
|
((vector? form) (walk-initializer form env))
|
||||||
((not (list? form)) form)
|
((not (list? form)) form)
|
||||||
((null? form) form)
|
((null? form) form)
|
||||||
;; expanded here rather than before the walk, so the macro body can
|
;; expanded here rather than before the walk, so the macro body can
|
||||||
@@ -584,17 +595,13 @@
|
|||||||
(cons walk-embed-result (walk-body expansion env)))))
|
(cons walk-embed-result (walk-body expansion env)))))
|
||||||
(else
|
(else
|
||||||
(case (car form)
|
(case (car form)
|
||||||
((do)
|
((do case case/fallthrough default)
|
||||||
(parameterize ((operand-position? #f))
|
(parameterize ((operand-position? #f))
|
||||||
(with-scope env (lambda () (walk-parts form env)))))
|
(with-scope env (lambda () (walk-parts form env)))))
|
||||||
|
|
||||||
((for while if switch)
|
((for while if switch)
|
||||||
(with-scope env (lambda () (walk-control form env))))
|
(with-scope env (lambda () (walk-control form env))))
|
||||||
|
|
||||||
((case case/fallthrough default)
|
|
||||||
(parameterize ((operand-position? #f))
|
|
||||||
(walk-parts form env)))
|
|
||||||
|
|
||||||
((lambda)
|
((lambda)
|
||||||
(let ((name (aux-name! env make-lambda-name)))
|
(let ((name (aux-name! env make-lambda-name)))
|
||||||
(add-aux-code! env (lift-lambda name form))
|
(add-aux-code! env (lift-lambda name form))
|
||||||
@@ -665,6 +672,14 @@
|
|||||||
|
|
||||||
((dot-access) (walk-member-path form env))
|
((dot-access) (walk-member-path form env))
|
||||||
|
|
||||||
|
((sizeof)
|
||||||
|
(if (and (= 2 (length form)) (written-type? (second form) env))
|
||||||
|
(copy-form-source!
|
||||||
|
form
|
||||||
|
`(sizeof (%type ,(resolve-closure-types
|
||||||
|
(expand-type (second form) env)))))
|
||||||
|
(walk-parts form env)))
|
||||||
|
|
||||||
(else
|
(else
|
||||||
(cond
|
(cond
|
||||||
((method-head? (car form)) (walk-method-call form env))
|
((method-head? (car form)) (walk-method-call form env))
|
||||||
@@ -763,14 +778,33 @@
|
|||||||
(unresolve-closure-types (expression-type queried env)))))
|
(unresolve-closure-types (expression-type queried env)))))
|
||||||
(macroexpand form (list))))
|
(macroexpand form (list))))
|
||||||
|
|
||||||
;;; `#(v : (.get a))' is `(v){vːget(&a)}': the values are expressions
|
;;; #(struct point : (. p x) 2) -- the values are expressions, and the
|
||||||
;;; like any other, and the type before `:' is not one
|
;;; words before a `:' a type that is left as written
|
||||||
(define (walk-literal form env)
|
(define (walk-initializer form env)
|
||||||
(let* ((parts (vector->list form))
|
(let-values (((prefix rest) (break (lambda (e) (eq? e ':))
|
||||||
(elements (or (and (memq ': parts) (cdr (memq ': parts))) parts)))
|
(vector->list form))))
|
||||||
(list->vector
|
(let ((walk (lambda (es) (map (lambda (e) (walk-operand e env)) es))))
|
||||||
(append (drop-right parts (length elements))
|
(list->vector
|
||||||
(map (lambda (element) (walk-operand element env)) elements)))))
|
(if (null? rest)
|
||||||
|
(walk prefix)
|
||||||
|
(append prefix (list ':) (walk (cdr rest))))))))
|
||||||
|
|
||||||
|
;;; Is the operand of `sizeof' a type? `(* x)' and `(¤ x i)' are
|
||||||
|
;;; expressions when x is: a name in scope, or a list that is not
|
||||||
|
;;; itself a type. A name nothing declared is one only C knows of, and
|
||||||
|
;;; there that is a type:
|
||||||
|
;;;
|
||||||
|
;;; (* char) (* const char) (* size-t) (unsigned int) types
|
||||||
|
;;; (* p) (* (g)) (¤ xs 0) expressions
|
||||||
|
(define (written-type? form env)
|
||||||
|
(and (pair? form)
|
||||||
|
;; the length a dereference and a subscript have
|
||||||
|
(if (= (length form) (case (car form) ((*) 2) ((¤) 3) (else -1)))
|
||||||
|
(let ((operand (second form)))
|
||||||
|
(if (symbol? operand)
|
||||||
|
(not (or (lookup-name env operand) (get-name-type operand)))
|
||||||
|
(written-type? operand env)))
|
||||||
|
(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)
|
||||||
@@ -832,9 +866,11 @@
|
|||||||
|
|
||||||
(define (wildcard-type? type) (mentions? type '_))
|
(define (wildcard-type? type) (mentions? type '_))
|
||||||
|
|
||||||
(define (mentions? type word)
|
;;; WORD anywhere in FORM, a literal's contents included
|
||||||
(cond ((eq? type word) #t)
|
(define (mentions? form word)
|
||||||
((list? type) (any (lambda (part) (mentions? part word)) type))
|
(cond ((eq? form word) #t)
|
||||||
|
((pair? form) (or (mentions? (car form) word) (mentions? (cdr form) word)))
|
||||||
|
((vector? form) (mentions? (vector->list form) word))
|
||||||
(else #f)))
|
(else #f)))
|
||||||
|
|
||||||
;;; `(each xs compare)' where `each' takes a closure: the argument is
|
;;; `(each xs compare)' where `each' takes a closure: the argument is
|
||||||
@@ -1082,7 +1118,10 @@
|
|||||||
;;; (. l a x) l : (struct line), l.a : (* struct point) -> l.a->x
|
;;; (. l a x) l : (struct line), l.a : (* struct point) -> l.a->x
|
||||||
;;;
|
;;;
|
||||||
;;; A type we cannot work out is read as a value, which is what the
|
;;; A type we cannot work out is read as a value, which is what the
|
||||||
;;; path would have meant before any of this.
|
;;; path would have meant before any of this. The warning names what
|
||||||
|
;;; could not be typed: the base, or the path up to a field of a struct
|
||||||
|
;;; only C knows -- (. font handle x) warns of (. font handle), not
|
||||||
|
;;; font. A field a struct declared here lacks is an error.
|
||||||
(define (walk-member-path form env)
|
(define (walk-member-path form env)
|
||||||
(let ((base (second form)))
|
(let ((base (second form)))
|
||||||
(let step ((expr (walk-operand base env))
|
(let step ((expr (walk-operand base env))
|
||||||
@@ -1091,15 +1130,25 @@
|
|||||||
(told? #f))
|
(told? #f))
|
||||||
(if (null? fields)
|
(if (null? fields)
|
||||||
(copy-form-source! form expr)
|
(copy-form-source! form expr)
|
||||||
(let ((target (pointer-target type)))
|
(let* ((target (pointer-target type))
|
||||||
|
(known (type-fields (or target type))))
|
||||||
|
(when (and (pair? known) (not (assq (field-name (car fields)) known)))
|
||||||
|
(sex-error form "no field of this name in" (or target type) (car fields)))
|
||||||
(unless (or type told?)
|
(unless (or type told?)
|
||||||
(sex-warning form "cannot type this; `.' reads it as a value"
|
(let ((walked (drop-right (cddr form) (length fields))))
|
||||||
base))
|
(sex-warning form "cannot type this; `.' reads it as a value"
|
||||||
|
(if (null? walked) base `(|.| ,base ,@walked)))))
|
||||||
(step (list (if target '%-> 'dot-access) expr (car fields))
|
(step (list (if target '%-> 'dot-access) expr (car fields))
|
||||||
(field-type (or target type) (car fields))
|
(field-type (or target type) (car fields))
|
||||||
(cdr fields)
|
(cdr fields)
|
||||||
(or told? (not type))))))))
|
(or told? (not type))))))))
|
||||||
|
|
||||||
|
;;; A field of a path, indexed or not: xs, [xs 0] -> xs
|
||||||
|
(define (field-name field)
|
||||||
|
(if (and (pair? field) (eq? '¤ (car field)) (pair? (cdr field)))
|
||||||
|
(second field)
|
||||||
|
field))
|
||||||
|
|
||||||
(define (lvalue? expr)
|
(define (lvalue? expr)
|
||||||
(cond ((symbol? expr) #t)
|
(cond ((symbol? expr) #t)
|
||||||
((vector? expr) #t) ; a compound literal is one
|
((vector? expr) #t) ; a compound literal is one
|
||||||
@@ -1841,7 +1890,9 @@
|
|||||||
(name (and (pair? (cdr f)) (symbol? (cadr f)) (cadr f))))
|
(name (and (pair? (cdr f)) (symbol? (cadr f)) (cadr f))))
|
||||||
;; An anonymous aggregate has a field list where the name would be,
|
;; An anonymous aggregate has a field list where the name would be,
|
||||||
;; and nothing can refer to it by name anyway
|
;; and nothing can refer to it by name anyway
|
||||||
(when name
|
;; ...and a bodyless one adds nothing to what is known: an importer
|
||||||
|
;; may be handed a declaration after the definition it declares
|
||||||
|
(when (and name (not (and (null? (cddr f)) (get-tag-info name))))
|
||||||
(case (car f)
|
(case (car f)
|
||||||
((struct) (add-struct name form))
|
((struct) (add-struct name form))
|
||||||
((union) (add-union name form))
|
((union) (add-union name form))
|
||||||
@@ -1932,12 +1983,6 @@ Returns #f if the form is not a function, returns the form otherwise"
|
|||||||
(fmt #f "Form " fn-form " is not a function"))
|
(fmt #f "Form " fn-form " is not a function"))
|
||||||
(fourth (fn-core fn-form)))
|
(fourth (fn-core fn-form)))
|
||||||
|
|
||||||
(define (sex-fn-prototype fn-form)
|
|
||||||
"Returns all except body"
|
|
||||||
(assert (sex-fn? fn-form)
|
|
||||||
(fmt #f "Form " fn-form " is not a function"))
|
|
||||||
(take fn-form (fn-header-length fn-form)))
|
|
||||||
|
|
||||||
(define (sex-fn-body fn-form)
|
(define (sex-fn-body fn-form)
|
||||||
(assert (sex-fn? fn-form)
|
(assert (sex-fn? fn-form)
|
||||||
(fmt #f "Form " fn-form " is not a function"))
|
(fmt #f "Form " fn-form " is not a function"))
|
||||||
|
|||||||
123
sex-fmt-c.scm
123
sex-fmt-c.scm
@@ -36,6 +36,7 @@
|
|||||||
scheme
|
scheme
|
||||||
(scheme base) ; string->utf8, bytevector accessors
|
(scheme base) ; string->utf8, bytevector accessors
|
||||||
(chicken base)
|
(chicken base)
|
||||||
|
(chicken flonum) ; flonum-print-precision
|
||||||
fmt
|
fmt
|
||||||
srfi-1
|
srfi-1
|
||||||
srfi-13)
|
srfi-13)
|
||||||
@@ -80,6 +81,7 @@
|
|||||||
((dot arrow post-decrement post-increment) 10)
|
((dot arrow post-decrement post-increment) 10)
|
||||||
((**) 15) ; Perl
|
((**) 15) ; Perl
|
||||||
((unary+ unary- ! ~ cast unary-* unary-& sizeof) 20) ; ++ --
|
((unary+ unary- ! ~ cast unary-* unary-& sizeof) 20) ; ++ --
|
||||||
|
((cast-operand) 25) ; see c-cast
|
||||||
((=~ !~) 25) ; Perl
|
((=~ !~) 25) ; Perl
|
||||||
((* / %) 30)
|
((* / %) 30)
|
||||||
((+ -) 35)
|
((+ -) 35)
|
||||||
@@ -96,6 +98,7 @@
|
|||||||
;;((|\|\||) 75)
|
;;((|\|\||) 75)
|
||||||
;;((.. ...) 77) ; Perl
|
;;((.. ...) 77) ; Perl
|
||||||
((?) 80)
|
((?) 80)
|
||||||
|
((conditional-else) 81) ; see c-if/expr
|
||||||
((= *= /= %= &= ^= <<= >>=) 85) ; |\|=| ; += -=
|
((= *= /= %= &= ^= <<= >>=) 85) ; |\|=| ; += -=
|
||||||
((comma) 90)
|
((comma) 90)
|
||||||
((=>) 90) ; Perl
|
((=>) 90) ; Perl
|
||||||
@@ -110,6 +113,13 @@
|
|||||||
|
|
||||||
(define (c-paren x) (cat "(" (c-expr x) ")"))
|
(define (c-paren x) (cat "(" (c-expr x) ")"))
|
||||||
|
|
||||||
|
;; MODIFIED FROM UPSTREAM fmt-c: a subscript is postfix, tighter than
|
||||||
|
;; any operator, so what it subscripts is written at the level of
|
||||||
|
;; `.' -- (* q) comes out `(*q)[i]', not `*q[i]', which is *(q[i]).
|
||||||
|
;; The index has brackets of its own and starts afresh.
|
||||||
|
(define (c-subscript array index)
|
||||||
|
(cat (c-with-op "." (c-expr array)) "[" (c-with-op 'paren (c-expr index)) "]"))
|
||||||
|
|
||||||
;; `.' and `->' associate left, and their right operand is always a
|
;; `.' and `->' associate left, and their right operand is always a
|
||||||
;; name, so a member access written inside another is the left one
|
;; name, so a member access written inside another is the left one
|
||||||
;; and needs no parens of its own: `s.u.circle.r', not `((s.u).circle).r'
|
;; and needs no parens of its own: `s.u.circle.r', not `((s.u).circle).r'
|
||||||
@@ -183,7 +193,21 @@
|
|||||||
((8) (cat "0" (number->string x 8)))
|
((8) (cat "0" (number->string x 8)))
|
||||||
(else (dsp (number->string x))))
|
(else (dsp (number->string x))))
|
||||||
st))
|
st))
|
||||||
(dsp (number->string x))))
|
(dsp (c-format-flonum x))))
|
||||||
|
|
||||||
|
;; MODIFIED FROM UPSTREAM fmt-c: a double in as few digits as read
|
||||||
|
;; back as the same double. number->string stops at
|
||||||
|
;; flonum-print-precision, 15 unless set, and 0.16666666666666666 came
|
||||||
|
;; out 0.166666666666667: a different double in the C. Seventeen
|
||||||
|
;; always do.
|
||||||
|
(define (c-format-flonum x)
|
||||||
|
(let ((precision (flonum-print-precision)))
|
||||||
|
(let try ((digits 15))
|
||||||
|
(flonum-print-precision digits)
|
||||||
|
(let ((written (number->string x)))
|
||||||
|
(if (or (= digits 17) (= (string->number written) x))
|
||||||
|
(begin (flonum-print-precision precision) written)
|
||||||
|
(try (+ digits 1)))))))
|
||||||
|
|
||||||
(define (c-format-string x)
|
(define (c-format-string x)
|
||||||
(lambda (st) ((cat #\" (apply-cat (c-string-escaped x)) #\") st)))
|
(lambda (st) ((cat #\" (apply-cat (c-string-escaped x)) #\") st)))
|
||||||
@@ -248,12 +272,9 @@
|
|||||||
;; non C-keywords must have some character invalid in a C
|
;; non C-keywords must have some character invalid in a C
|
||||||
;; identifier to avoid conflicts - by default we prefix %
|
;; identifier to avoid conflicts - by default we prefix %
|
||||||
((vector-ref)
|
((vector-ref)
|
||||||
((c-wrap-stmt
|
((c-wrap-stmt (c-subscript (cadr x) (caddr x))) st))
|
||||||
(cat (c-expr (cadr x)) "[" (c-expr (caddr x)) "]"))
|
|
||||||
st))
|
|
||||||
((vector-set!)
|
((vector-set!)
|
||||||
((c= (c-in-expr
|
((c= (c-in-expr (c-subscript (cadr x) (caddr x)))
|
||||||
(cat (c-expr (cadr x)) "[" (c-expr (caddr x)) "]"))
|
|
||||||
(c-expr (cadddr x)))
|
(c-expr (cadddr x)))
|
||||||
st))
|
st))
|
||||||
((extern/C) ((apply c-extern/C (cdr x)) st))
|
((extern/C) ((apply c-extern/C (cdr x)) st))
|
||||||
@@ -287,6 +308,7 @@
|
|||||||
((%comment) ((apply c-comment (cdr x)) st))
|
((%comment) ((apply c-comment (cdr x)) st))
|
||||||
((:) ((apply c-label (cdr x)) st))
|
((:) ((apply c-label (cdr x)) st))
|
||||||
((%cast) ((apply c-cast (cdr x)) st))
|
((%cast) ((apply c-cast (cdr x)) st))
|
||||||
|
((%sizeof-type) ((cat "sizeof(" (c-type (cadr x)) ")") st))
|
||||||
((%compound) ((apply c-compound (cdr x)) st))
|
((%compound) ((apply c-compound (cdr x)) st))
|
||||||
((%designate) ((apply c-designate (cdr x)) st))
|
((%designate) ((apply c-designate (cdr x)) st))
|
||||||
((+ - & * / % ! ~ ^ && < > <= >= == != << >>
|
((+ - & * / % ! ~ ^ && < > <= >= == != << >>
|
||||||
@@ -432,6 +454,15 @@
|
|||||||
((pair? x) (cons (replace (car x)) (replace (cdr x))))
|
((pair? x) (cons (replace (car x)) (replace (cdr x))))
|
||||||
(else x))))
|
(else x))))
|
||||||
|
|
||||||
|
;; MODIFIED FROM UPSTREAM fmt-c: an object-like body is parenthesised
|
||||||
|
;; as a function-like one is. Upstream left it bare, so with
|
||||||
|
;; `#define D -1.0 / 6.0', `x * D' was `(x * -1.0) / 6.0'. Only a
|
||||||
|
;; name or a literal that cannot lose its sign to a neighbour is
|
||||||
|
;; left as written.
|
||||||
|
(define (c-atomic-body? x)
|
||||||
|
(or (symbol? x)
|
||||||
|
(and (c-literal? x) (not (and (number? x) (negative? x))))))
|
||||||
|
|
||||||
(define (cpp-define x . body)
|
(define (cpp-define x . body)
|
||||||
(define (name-of x) (c-expr (if (pair? x) (cadr x) x)))
|
(define (name-of x) (c-expr (if (pair? x) (cadr x) x)))
|
||||||
(lambda (st)
|
(lambda (st)
|
||||||
@@ -449,9 +480,8 @@
|
|||||||
(cat " "
|
(cat " "
|
||||||
(fmt-let 'writer (make-cpp-writer st)
|
(fmt-let 'writer (make-cpp-writer st)
|
||||||
(fmt-let 'macro-params params
|
(fmt-let 'macro-params params
|
||||||
((if (or (not (pair? x))
|
((if (and (null? (cdr body))
|
||||||
(and (null? (cdr body))
|
(c-atomic-body? (car body)))
|
||||||
(c-literal? (car body))))
|
|
||||||
(lambda (x) x)
|
(lambda (x) x)
|
||||||
c-paren)
|
c-paren)
|
||||||
(c-in-expr (apply c-begin body))))))
|
(c-in-expr (apply c-begin body))))))
|
||||||
@@ -805,32 +835,22 @@
|
|||||||
(fmt-join c-expr name ", ")
|
(fmt-join c-expr name ", ")
|
||||||
(c-expr name))))))
|
(c-expr name))))))
|
||||||
|
|
||||||
;; MODIFIED FROM UPSTREAM fmt-c: parenthesise a binary operand.
|
;; MODIFIED FROM UPSTREAM fmt-c: take part in precedence, both ways.
|
||||||
;;
|
;;
|
||||||
;; A cast binds tighter than every binary operator, so upstream's
|
;; Upstream's bare (cat "(" type ")" expr) cast only the first operand
|
||||||
;; unparenthesised (cat "(" type ")" expr) silently casts only the
|
;; of a looser one -- (cast (* 2 (sizeof GLfloat)) (* void)) came out
|
||||||
;; first operand: (cast (* 2 (sizeof GLfloat)) (* void)) came out as
|
;; `(void *)2 * sizeof(GLfloat)' -- and was itself taken apart by a
|
||||||
;; `(void *)2 * sizeof(GLfloat)'. Unary operands -- &x, *p, -n,
|
;; tighter operator around it: `(const wanted *)pa->distance'.
|
||||||
;; sizeof, calls, identifiers, literals -- are already
|
;;
|
||||||
;; unary-expressions and are left alone, so this changes nothing that
|
;; The operand is written at `cast-operand', just looser than a unary
|
||||||
;; was previously correct.
|
;; operator, so a unary operand stays bare -- `(int *)&a' -- while
|
||||||
(define c-binary-operators
|
;; anything looser parenthesises itself. The cast is parenthesised
|
||||||
'(+ - * / % << >> < > <= >= == != & ^ && %and %or
|
;; only under something strictly tighter: `-(int)a', `*(int *)v'.
|
||||||
= *= /= %= &= ^= <<= >>=))
|
|
||||||
|
|
||||||
(define (c-binary-expression? expr)
|
|
||||||
(and (pair? expr)
|
|
||||||
(memq (car expr) c-binary-operators)
|
|
||||||
(pair? (cdr expr))
|
|
||||||
(pair? (cddr expr)))) ; two or more operands
|
|
||||||
|
|
||||||
(define (c-cast type expr)
|
(define (c-cast type expr)
|
||||||
(cat "(" (c-type type) ")"
|
(lambda (st)
|
||||||
(if (c-binary-expression? expr)
|
(let ((cast (cat "(" (c-type type) ")"
|
||||||
;; c-with-op stops the operand from parenthesising itself
|
(c-with-op 'cast-operand (c-expr expr)))))
|
||||||
;; a second time inside the parens we just added.
|
((if (c-op< (fmt-op st) 'cast) (c-paren cast) cast) st))))
|
||||||
(cat "(" (c-with-op 'paren (c-expr expr)) ")")
|
|
||||||
(c-expr expr))))
|
|
||||||
|
|
||||||
;; { a, b, c } -- on one line if it fits, one element per line if not.
|
;; { a, b, c } -- on one line if it fits, one element per line if not.
|
||||||
(define (c-braced-list ls)
|
(define (c-braced-list ls)
|
||||||
@@ -881,20 +901,31 @@
|
|||||||
tail))))
|
tail))))
|
||||||
st))))
|
st))))
|
||||||
|
|
||||||
|
;; MODIFIED FROM UPSTREAM fmt-c: parenthesised as the operator it is.
|
||||||
|
;; Upstream wrote it bare whatever surrounded it, and `?:' binds
|
||||||
|
;; looser than every binary operator: (/ 300 (if c c 1)) came out
|
||||||
|
;; `300 / c ? c : 1', a conditional on the quotient.
|
||||||
|
;;
|
||||||
|
;; It groups to the right, so its else arm is written at
|
||||||
|
;; `conditional-else', just looser: a chain there stays bare,
|
||||||
|
;; `a ? x : b ? y : z', and an assignment does not.
|
||||||
(define (c-if/expr c p . rest)
|
(define (c-if/expr c p . rest)
|
||||||
(let lp ((c c) (p p) (ls rest))
|
(c-maybe-paren
|
||||||
(cond
|
'?
|
||||||
((or (eq? c 'else) (eq? c #t))
|
(let lp ((c c) (p p) (ls rest))
|
||||||
(if (not (null? ls))
|
(cond
|
||||||
(error "forms after else clause in IF" c p ls)
|
((or (eq? c 'else) (eq? c #t))
|
||||||
(c-expr p)))
|
(if (not (null? ls))
|
||||||
((pair? ls)
|
(error "forms after else clause in IF" c p ls)
|
||||||
(cat (c-in-test (c-expr c)) " ? " (c-expr p) " : "
|
(c-expr p)))
|
||||||
(if (pair? (cdr ls))
|
((pair? ls)
|
||||||
(lp (car ls) (cadr ls) (cddr ls))
|
(cat (c-in-test (c-expr c)) " ? " (c-expr p) " : "
|
||||||
(lp 'else (car ls) '()))))
|
(c-with-op 'conditional-else
|
||||||
(else
|
(if (pair? (cdr ls))
|
||||||
(c-or (c-in-test (c-expr c)) (c-expr p))))))
|
(lp (car ls) (cadr ls) (cddr ls))
|
||||||
|
(lp 'else (car ls) '())))))
|
||||||
|
(else
|
||||||
|
(c-or (c-in-test (c-expr c)) (c-expr p)))))))
|
||||||
|
|
||||||
(define (c-if . args)
|
(define (c-if . args)
|
||||||
(fmt-if fmt-expression?
|
(fmt-if fmt-expression?
|
||||||
|
|||||||
@@ -1,9 +0,0 @@
|
|||||||
(module sex-macros
|
|
||||||
(register-macro
|
|
||||||
cat
|
|
||||||
comment
|
|
||||||
get-macro
|
|
||||||
macro?
|
|
||||||
apply-macro
|
|
||||||
defmacro)
|
|
||||||
"sex-macros.scm")
|
|
||||||
@@ -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))
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +0,0 @@
|
|||||||
(module sex-modules
|
|
||||||
(get-modules-public-forms
|
|
||||||
load-persistent-module-paths
|
|
||||||
read-public-interface)
|
|
||||||
"sex-modules.scm")
|
|
||||||
227
sex-modules.scm
227
sex-modules.scm
@@ -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
|
||||||
@@ -78,17 +89,222 @@
|
|||||||
(file-readable? module-path)
|
(file-readable? module-path)
|
||||||
module-path))))
|
module-path))))
|
||||||
|
|
||||||
|
;;; Every module file read for this unit, with its forms, last read
|
||||||
|
;;; first: what its output depends on, besides the unit itself. A module
|
||||||
|
;;; that an import chain and the interface check both reach is read once
|
||||||
|
(define +modules-read+ (list))
|
||||||
|
|
||||||
|
(define (module-dependencies) (reverse (map car +modules-read+)))
|
||||||
|
|
||||||
|
(define (read-module path)
|
||||||
|
(cond ((assoc path +modules-read+) => cdr)
|
||||||
|
(else (let ((forms (read-from-file path)))
|
||||||
|
(set! +modules-read+ (cons (cons path forms) +modules-read+))
|
||||||
|
forms))))
|
||||||
|
|
||||||
(define (read-public-interface module-path)
|
(define (read-public-interface module-path)
|
||||||
;; pub fns are reduced to prototypes, other pub forms are just pasted.
|
;; pub fns are reduced to prototypes, other pub forms are just pasted.
|
||||||
;; Declaration order is kept: a module that declares a type and then a
|
;; Declaration order is kept: a module that declares a type and then a
|
||||||
;; pub fn taking it has to paste them that way round, or the prototype
|
;; pub fn taking it has to paste them that way round, or the prototype
|
||||||
;; names a type C has not seen.
|
;; names a type C has not seen. The declarations of the types its
|
||||||
(let ((raw-forms (read-from-file module-path)))
|
;; importers see only behind a pointer go first.
|
||||||
|
(let ((raw-forms (read-module module-path)))
|
||||||
|
(append (check-public-interface raw-forms)
|
||||||
|
(interface-forms raw-forms))))
|
||||||
|
|
||||||
|
(define (interface-forms raw-forms)
|
||||||
|
(reverse (fold process-public-interface-form (list) raw-forms)))
|
||||||
|
|
||||||
|
;;; What importers can see of a module's types
|
||||||
|
;;;
|
||||||
|
;;; They are given the pub forms and nothing else, so a pub form that
|
||||||
|
;;; names a type not among them compiled here and failed there:
|
||||||
|
;;; (pub record shape ((cells (* cell)))) with `cell' private told the
|
||||||
|
;;; importer `unknown type name cell'. A record behind a pointer needs
|
||||||
|
;;; only declaring, and its declaration goes out with the pub forms.
|
||||||
|
;;; Held by value it needs its body; and a typedef or an enum has no
|
||||||
|
;;; declaration short of its definition. Those are errors here, where
|
||||||
|
;;; they were written. A name Sex has no declaration for is one only C
|
||||||
|
;;; knows, and is left to C.
|
||||||
|
|
||||||
|
;;; The bodyless declarations FORMS' importers need, in the order the
|
||||||
|
;;; pub forms ask for them. An error when a pub form needs more than a
|
||||||
|
;;; declaration of a type they cannot see.
|
||||||
|
(define (check-public-interface forms)
|
||||||
|
(let ((visible (visible-types forms))
|
||||||
|
(hidden (hidden-types forms)))
|
||||||
(reverse
|
(reverse
|
||||||
(fold
|
(fold
|
||||||
process-public-interface-form
|
(lambda (form acc)
|
||||||
|
(fold (lambda (use acc) (check-type-use form use visible hidden acc))
|
||||||
|
acc
|
||||||
|
(append-map (lambda (type) (type-uses type #f))
|
||||||
|
(interface-types form))))
|
||||||
(list)
|
(list)
|
||||||
raw-forms))))
|
forms))))
|
||||||
|
|
||||||
|
;;; USE is (name . behind-a-pointer?)
|
||||||
|
(define (check-type-use form use visible hidden acc)
|
||||||
|
(let* ((name (car use))
|
||||||
|
(seen (assq name visible))
|
||||||
|
(kept (assq name hidden))
|
||||||
|
(remedy (if (and kept (fourth kept)) "import its module with pub" "make it pub")))
|
||||||
|
(cond
|
||||||
|
((and seen (third seen)) acc) ; pub with its body
|
||||||
|
((and seen (cdr use)) acc) ; a pub handle, held by one
|
||||||
|
(seen
|
||||||
|
(sex-error form "only a handle of this type is pub, so hold it through a pointer"
|
||||||
|
name))
|
||||||
|
((not kept) acc)
|
||||||
|
((not (memq (second kept) '(record struct union sum)))
|
||||||
|
(sex-error form (string-append "importers cannot see this "
|
||||||
|
(symbol->string (second kept))
|
||||||
|
", and C declares one only by defining it; "
|
||||||
|
remedy)
|
||||||
|
name))
|
||||||
|
((not (cdr use))
|
||||||
|
(sex-error form (string-append "importers cannot see this type, held by value; "
|
||||||
|
remedy ", or hold it through a pointer")
|
||||||
|
name))
|
||||||
|
((any (lambda (f) (eq? name (cadr f))) acc) acc)
|
||||||
|
(else
|
||||||
|
(cons (copy-form-source!
|
||||||
|
form
|
||||||
|
(list (if (memq (second kept) '(record sum)) 'record (second kept))
|
||||||
|
name))
|
||||||
|
acc)))))
|
||||||
|
|
||||||
|
;;; The type forms a pub form shows its importers
|
||||||
|
;;;
|
||||||
|
;;; (pub fn f ((c (* cell)) (n int)) shape) -> ((* cell) int shape)
|
||||||
|
(define (interface-types form)
|
||||||
|
(match form
|
||||||
|
(('pub 'fn . _)
|
||||||
|
(match (strip-header-comments form 5)
|
||||||
|
(('pub 'fn name (args ...) ret . _) (signature-types args ret))
|
||||||
|
(_ (list))))
|
||||||
|
(('pub 'var . _)
|
||||||
|
(match (strip-header-comments form 4)
|
||||||
|
(('pub 'var name type . _) (list type))
|
||||||
|
(_ (list))))
|
||||||
|
(('pub 'typedef name target) (list target))
|
||||||
|
(('pub 'define name . body) body)
|
||||||
|
(('pub (or 'record 'struct 'union) name . rest)
|
||||||
|
(field-types (find list? (remove comment-form? rest))))
|
||||||
|
(('pub 'sum name (variants ...))
|
||||||
|
(append-map (lambda (v)
|
||||||
|
(if (and (pair? v) (pair? (cdr v))) (field-types (second v)) (list)))
|
||||||
|
(remove comment-form? variants)))
|
||||||
|
(('ops type . methods)
|
||||||
|
(append-map interface-types
|
||||||
|
(filter (lambda (m) (and (pair? m) (eq? (car m) 'pub))) methods)))
|
||||||
|
(('pub 'realize spec . methods)
|
||||||
|
(append-map (lambda (m) (interface-types (cons 'pub m)))
|
||||||
|
(remove comment-form? methods)))
|
||||||
|
(_ (list))))
|
||||||
|
|
||||||
|
(define (field-types fields)
|
||||||
|
(if (list? fields) (map second (normalize-fields fields)) (list)))
|
||||||
|
|
||||||
|
(define (signature-types args ret)
|
||||||
|
(cons ret (arglist-types (remove comment-form? args))))
|
||||||
|
|
||||||
|
;;; Each name TYPE mentions, and whether it is behind a pointer:
|
||||||
|
;;;
|
||||||
|
;;; (* const cell) -> ((const . #t) (cell . #t))
|
||||||
|
;;; [cell 4] -> ((cell . #f))
|
||||||
|
;;; (fn ((cell)) int) -> ((cell . #f) (int . #f))
|
||||||
|
;;;
|
||||||
|
;;; A function type takes and returns by value whatever it is held
|
||||||
|
;;; through: C wants the body to define or call one of its functions.
|
||||||
|
(define (type-uses type behind?)
|
||||||
|
(cond
|
||||||
|
((symbol? type) (list (cons type behind?)))
|
||||||
|
((not (pair? type)) (list))
|
||||||
|
(else
|
||||||
|
(case (car type)
|
||||||
|
((*) (append-map (lambda (t) (type-uses t #t)) (cdr type)))
|
||||||
|
((¤) (let ((element (array-element-type type)))
|
||||||
|
(append (if element (type-uses element behind?) (list))
|
||||||
|
(if (array-bound? type) (bound-uses (last type)) (list)))))
|
||||||
|
((fn closure)
|
||||||
|
(if (and (pair? (cdr type)) (list? (second type)))
|
||||||
|
(append-map (lambda (t) (type-uses t #f))
|
||||||
|
(signature-types (second type) (last type)))
|
||||||
|
(list)))
|
||||||
|
((struct union enum)
|
||||||
|
(if (and (pair? (cdr type)) (symbol? (second type)))
|
||||||
|
(list (cons (second type) behind?))
|
||||||
|
(list)))
|
||||||
|
(else (append-map (lambda (t) (type-uses t behind?)) type))))))
|
||||||
|
|
||||||
|
;;; [int (* 2 N)] needs N, as a value: there is no declaring it
|
||||||
|
(define (bound-uses bound)
|
||||||
|
(cond ((symbol? bound) (list (cons bound #f)))
|
||||||
|
((pair? bound) (append-map bound-uses (cdr bound)))
|
||||||
|
(else (list))))
|
||||||
|
|
||||||
|
;;; The types and constants FORMS declare, as (name kind body?):
|
||||||
|
;;;
|
||||||
|
;;; (pub record tally) -> (tally record #f)
|
||||||
|
;;; (sum colour ((none ()))) -> (colour sum #t)
|
||||||
|
;;; (define (sq x) (* x x)) -> (sq define #t)
|
||||||
|
(define (declared-types forms)
|
||||||
|
(filter-map
|
||||||
|
(lambda (form)
|
||||||
|
(and (pair? form)
|
||||||
|
(let* ((core (strip-pub form))
|
||||||
|
(name (and (pair? core) (pair? (cdr core))
|
||||||
|
(if (and (eq? (car core) 'define) (pair? (second core)))
|
||||||
|
(car (second core))
|
||||||
|
(second core)))))
|
||||||
|
(and (memq (car core) '(record struct union enum sum typedef define))
|
||||||
|
(symbol? name)
|
||||||
|
(list name
|
||||||
|
(car core)
|
||||||
|
(or (memq (car core) '(typedef define))
|
||||||
|
(any list? (remove comment-form? (cddr core)))))))))
|
||||||
|
forms))
|
||||||
|
|
||||||
|
(define (pub-form? form) (and (pair? form) (eq? (car form) 'pub)))
|
||||||
|
|
||||||
|
;;; Pub here or through a pub import. A type declared pub both bodyless
|
||||||
|
;;; and whole is whole: its whole entries come first, for `assq'
|
||||||
|
(define (visible-types forms)
|
||||||
|
(let ((all (append (declared-types (filter pub-form? forms))
|
||||||
|
(append-map exported-types (imported-modules forms #t)))))
|
||||||
|
(append (filter third all) (remove third all))))
|
||||||
|
|
||||||
|
;;; Declared here without pub, or brought by an import without it, as
|
||||||
|
;;; (name kind body? imported?)
|
||||||
|
(define (hidden-types forms)
|
||||||
|
(append (map (lambda (entry) (append entry (list #f)))
|
||||||
|
(declared-types (remove pub-form? forms)))
|
||||||
|
(map (lambda (entry) (append entry (list #t)))
|
||||||
|
(append-map exported-types (imported-modules forms #f)))))
|
||||||
|
|
||||||
|
;;; The names of the modules FORMS import, with pub or without
|
||||||
|
(define (imported-modules forms pub?)
|
||||||
|
(append-map (lambda (form)
|
||||||
|
(match form
|
||||||
|
(('pub 'import . modules) (if pub? modules (list)))
|
||||||
|
(('import . modules) (if pub? (list) modules))
|
||||||
|
(_ (list))))
|
||||||
|
forms))
|
||||||
|
|
||||||
|
;;; The types a module gives its importers, its pub imports' included.
|
||||||
|
;;; Two modules may import each other, so each is read once
|
||||||
|
(define (exported-types module)
|
||||||
|
(let next ((modules (list module)) (seen (list)) (acc (list)))
|
||||||
|
(if (null? modules)
|
||||||
|
(concatenate (reverse acc))
|
||||||
|
(let ((path (locate-module (symbol->string (car modules)))))
|
||||||
|
(if (or (not path) (member path seen))
|
||||||
|
(next (cdr modules) seen acc)
|
||||||
|
(let ((forms (interface-forms (read-module path))))
|
||||||
|
(next (append (cdr modules) (imported-modules forms #f))
|
||||||
|
(cons path seen)
|
||||||
|
(cons (declared-types forms) acc))))))))
|
||||||
|
|
||||||
(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
|
||||||
@@ -159,6 +375,5 @@
|
|||||||
(get-env-var "SEX_MODULE_PATH")))
|
(get-env-var "SEX_MODULE_PATH")))
|
||||||
(when sex-module-path-env-var
|
(when sex-module-path-env-var
|
||||||
(set! +persistent-module-paths+
|
(set! +persistent-module-paths+
|
||||||
(map (lambda (p)
|
(map to-absolute-pathname
|
||||||
(make-absolute-pathname p #f #f))
|
|
||||||
(string-split sex-module-path-env-var ":"))))))
|
(string-split sex-module-path-env-var ":"))))))
|
||||||
|
|||||||
@@ -1 +0,0 @@
|
|||||||
(module sexc (main) "sexc.scm")
|
|
||||||
92
sexc.scm
92
sexc.scm
@@ -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
|
||||||
@@ -59,10 +67,16 @@
|
|||||||
(value #f)
|
(value #f)
|
||||||
(single-char #\m))
|
(single-char #\m))
|
||||||
(output ,(fmt #f "Write output to file. Default file name is a.out." nl
|
(output ,(fmt #f "Write output to file. Default file name is a.out." nl
|
||||||
(pad padding) "If -E or -m options are provided, defaults to stdout")
|
(pad padding) "With -C, -m or --public-interface, defaults to stdout," nl
|
||||||
|
(pad padding) "which `-' names too")
|
||||||
(required #f)
|
(required #f)
|
||||||
(value #t)
|
(value #t)
|
||||||
(single-char #\o))
|
(single-char #\o))
|
||||||
|
(depfile
|
||||||
|
,(fmt #f "Write a make rule for the output to FILE: it depends on" nl
|
||||||
|
(pad padding) "the source and on every module read for it. Needs -o")
|
||||||
|
(required #f)
|
||||||
|
(value #t))
|
||||||
(line-directives
|
(line-directives
|
||||||
,(fmt #f "How much #line information to emit: statement (default)," nl
|
,(fmt #f "How much #line information to emit: statement (default)," nl
|
||||||
(pad padding) "toplevel, or none. `statement' is what makes a debugger" nl
|
(pad padding) "toplevel, or none. `statement' is what makes a debugger" nl
|
||||||
@@ -100,6 +114,17 @@
|
|||||||
(list)
|
(list)
|
||||||
(cdr tail))))
|
(cdr tail))))
|
||||||
|
|
||||||
|
;;; getopt-long reads a lone `-' as an option, so `-o -' died there as
|
||||||
|
;;; one missing its value. `--output=-' is the spelling it takes
|
||||||
|
(define (spell-stdout-output argv)
|
||||||
|
(let loop ((as argv) (acc (list)))
|
||||||
|
(cond ((null? as) (reverse acc))
|
||||||
|
((and (member (car as) '("-o" "--output"))
|
||||||
|
(pair? (cdr as))
|
||||||
|
(string=? (cadr as) "-"))
|
||||||
|
(loop (cddr as) (cons "--output=-" acc)))
|
||||||
|
(else (loop (cdr as) (cons (car as) acc))))))
|
||||||
|
|
||||||
(define (get-rest-args args)
|
(define (get-rest-args args)
|
||||||
(cdr (assoc '@ args)))
|
(cdr (assoc '@ args)))
|
||||||
|
|
||||||
@@ -125,8 +150,17 @@
|
|||||||
'stdin
|
'stdin
|
||||||
(car rest-args))))
|
(car rest-args))))
|
||||||
|
|
||||||
|
(define (stdout-output? output)
|
||||||
|
(or (eq? output 'default) (equal? output "-")))
|
||||||
|
|
||||||
|
;;; Whether ARGS ask for text, which `-o -' can send to stdout. An
|
||||||
|
;;; executable or an object goes to cc's -o, where `-' is a file name
|
||||||
|
(define (text-output? args)
|
||||||
|
(any (lambda (option) (get-arg args option #f))
|
||||||
|
'(emit-c macro-expand public-interface)))
|
||||||
|
|
||||||
(define (write-to-file-or-stdout output what)
|
(define (write-to-file-or-stdout output what)
|
||||||
(if (eq? output 'default)
|
(if (stdout-output? output)
|
||||||
(what)
|
(what)
|
||||||
(with-output-to-file output
|
(with-output-to-file output
|
||||||
(fn (what)))))
|
(fn (what)))))
|
||||||
@@ -138,6 +172,28 @@
|
|||||||
(map pp sex-forms)
|
(map pp sex-forms)
|
||||||
(emit-c sex-forms)))))
|
(emit-c sex-forms)))))
|
||||||
|
|
||||||
|
;;; app.o: app.sex /m/greet.sex, and an empty rule per module, so that
|
||||||
|
;;; make takes a module deleted since as out of date rather than as a
|
||||||
|
;;; file it has no rule to make
|
||||||
|
(define (dependency-rule target source modules)
|
||||||
|
(let ((spell (lambda (path) (string-translate* path '((" " . "\\ "))))))
|
||||||
|
(apply string-append
|
||||||
|
(spell target) ":"
|
||||||
|
(append (map (lambda (path) (string-append " " (spell path)))
|
||||||
|
(if source (cons source modules) modules))
|
||||||
|
(list "\n")
|
||||||
|
(map (lambda (path) (string-append "\n" (spell path) ":\n"))
|
||||||
|
modules)))))
|
||||||
|
|
||||||
|
(define (write-dependencies depfile output input)
|
||||||
|
(when (stdout-output? output)
|
||||||
|
(error "--depfile needs -o, to name what depends on the modules"))
|
||||||
|
(with-output-to-file depfile
|
||||||
|
(lambda ()
|
||||||
|
(display (dependency-rule output
|
||||||
|
(and (not (eq? input 'stdin)) input)
|
||||||
|
(module-dependencies))))))
|
||||||
|
|
||||||
(define (compile-to-file sex-forms output args cc-args)
|
(define (compile-to-file sex-forms output args cc-args)
|
||||||
"Hand the generated C to the C compiler. Returns the compiler's exit
|
"Hand the generated C to the C compiler. Returns the compiler's exit
|
||||||
status, which is ours to pass on."
|
status, which is ours to pass on."
|
||||||
@@ -168,11 +224,15 @@ status, which is ours to pass on."
|
|||||||
(delete-file* c-file)
|
(delete-file* c-file)
|
||||||
(if normal-exit? status 1)))))))
|
(if normal-exit? status 1)))))))
|
||||||
|
|
||||||
(define (semantic-process-forms raw-forms input-source)
|
;;; The unit's own forms are checked for what its importers will need,
|
||||||
(if (eq? input-source 'stdin)
|
;;; the prelude's not: every unit has those
|
||||||
(semen-process raw-forms)
|
(define (semantic-process-forms unit-forms input-source)
|
||||||
(with-directory input-source
|
(let ((process (lambda ()
|
||||||
(semen-process raw-forms))))
|
(check-public-interface unit-forms)
|
||||||
|
(semen-process (append prelude unit-forms)))))
|
||||||
|
(if (eq? input-source 'stdin)
|
||||||
|
(process)
|
||||||
|
(with-directory input-source (process)))))
|
||||||
|
|
||||||
(define prelude
|
(define prelude
|
||||||
(append
|
(append
|
||||||
@@ -196,7 +256,7 @@ status, which is ours to pass on."
|
|||||||
(let* ((argv (command-line-arguments))
|
(let* ((argv (command-line-arguments))
|
||||||
(raw-args (args-before-separator argv))
|
(raw-args (args-before-separator argv))
|
||||||
(cc-args (args-after-separator argv))
|
(cc-args (args-after-separator argv))
|
||||||
(args (getopt-long raw-args
|
(args (getopt-long (spell-stdout-output raw-args)
|
||||||
opts-grammar))
|
opts-grammar))
|
||||||
(output (get-arg args 'output 'default))
|
(output (get-arg args 'output 'default))
|
||||||
(help (help-arg? args))
|
(help (help-arg? args))
|
||||||
@@ -208,6 +268,8 @@ status, which is ours to pass on."
|
|||||||
(when help
|
(when help
|
||||||
(print-help)
|
(print-help)
|
||||||
(return #f))
|
(return #f))
|
||||||
|
(when (and (equal? output "-") (not (text-output? args)))
|
||||||
|
(error "-o - is stdout, which takes only -C, -m or --public-interface"))
|
||||||
;; Read time comes before everything, so the features have to be
|
;; Read time comes before everything, so the features have to be
|
||||||
;; in place before the first form is read
|
;; in place before the first form is read
|
||||||
(current-features
|
(current-features
|
||||||
@@ -216,16 +278,18 @@ status, which is ours to pass on."
|
|||||||
(platform-features))
|
(platform-features))
|
||||||
(cli-features args)))
|
(cli-features args)))
|
||||||
|
|
||||||
|
;; before the interface too, which asks the modules it imports
|
||||||
|
;; what types they export
|
||||||
|
(load-persistent-module-paths)
|
||||||
|
|
||||||
(when (get-arg args 'public-interface #f)
|
(when (get-arg args 'public-interface #f)
|
||||||
(assert (not (eq? input 'stdin)) "Error: --public-interface requires file argument")
|
(assert (not (eq? input 'stdin)) "Error: --public-interface requires file argument")
|
||||||
|
|
||||||
(write-to-file-or-stdout
|
(write-to-file-or-stdout
|
||||||
output
|
output
|
||||||
(fn
|
(fn
|
||||||
(map pp (reverse
|
(map pp (read-public-interface input))))
|
||||||
(read-public-interface input)))))
|
|
||||||
(return #f))
|
(return #f))
|
||||||
(load-persistent-module-paths)
|
|
||||||
|
|
||||||
;; The file name in a #line directive now comes from the form's
|
;; The file name in a #line directive now comes from the form's
|
||||||
;; own recorded location, so imported modules report themselves
|
;; own recorded location, so imported modules report themselves
|
||||||
@@ -235,8 +299,10 @@ status, which is ours to pass on."
|
|||||||
(not (get-arg args 'line-directives #f)))
|
(not (get-arg args 'line-directives #f)))
|
||||||
(sex-line-directives 'none))
|
(sex-line-directives 'none))
|
||||||
|
|
||||||
(let* ((raw-forms (append prelude (read-raw-forms input)))
|
(let ((sex-forms (semantic-process-forms (read-raw-forms input) input)))
|
||||||
(sex-forms (semantic-process-forms raw-forms input)))
|
(let ((depfile (get-arg args 'depfile #f)))
|
||||||
|
(when depfile
|
||||||
|
(write-dependencies depfile output input)))
|
||||||
(if (or (get-arg args 'macro-expand #f)
|
(if (or (get-arg args 'macro-expand #f)
|
||||||
(get-arg args 'emit-c #f))
|
(get-arg args 'emit-c #f))
|
||||||
;; Emit processed and macro-expanded sex code, or emit C code
|
;; Emit processed and macro-expanded sex code, or emit C code
|
||||||
|
|||||||
@@ -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 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
|
|
||||||
@@ -52,4 +52,32 @@
|
|||||||
|
|
||||||
(test "an empty command line is handled"
|
(test "an empty command line is handled"
|
||||||
'()
|
'()
|
||||||
(args-before-separator '())))
|
(args-before-separator '()))
|
||||||
|
|
||||||
|
;; getopt-long reads a lone `-' as an option and refused `-o -', so
|
||||||
|
;; it is handed the one spelling it takes a dash in
|
||||||
|
(test "-o - reaches getopt-long as a value"
|
||||||
|
'("x.sex" "--output=-" "-C")
|
||||||
|
(spell-stdout-output '("x.sex" "-o" "-" "-C")))
|
||||||
|
(test "and so does --output -"
|
||||||
|
'("--output=-")
|
||||||
|
(spell-stdout-output '("--output" "-")))
|
||||||
|
(test "any other output is left alone"
|
||||||
|
'("-o" "out" "-")
|
||||||
|
(spell-stdout-output '("-o" "out" "-"))))
|
||||||
|
|
||||||
|
;;; --depfile: an import pastes another file's forms into the output, so
|
||||||
|
;;; the output depends on that file as a C object does on a header
|
||||||
|
(test-group "dependency rule"
|
||||||
|
(test "the target, the source and the modules, then a rule per module"
|
||||||
|
"app.o: app.sex /m/greet.sex /m/ledger.sex\n\n/m/greet.sex:\n\n/m/ledger.sex:\n"
|
||||||
|
(dependency-rule "app.o" "app.sex" '("/m/greet.sex" "/m/ledger.sex")))
|
||||||
|
(test "no modules, no rules of their own"
|
||||||
|
"app.o: app.sex\n"
|
||||||
|
(dependency-rule "app.o" "app.sex" '()))
|
||||||
|
(test "a space is escaped, the way make reads it"
|
||||||
|
"a\\ b.o: a\\ b.sex\n"
|
||||||
|
(dependency-rule "a b.o" "a b.sex" '()))
|
||||||
|
(test "stdin is no file to depend on"
|
||||||
|
"app.o: /m/greet.sex\n\n/m/greet.sex:\n"
|
||||||
|
(dependency-rule "app.o" #f '("/m/greet.sex"))))
|
||||||
|
|||||||
@@ -13,6 +13,13 @@
|
|||||||
(test '_this_->_member_ (unkebabify '-this-->-member-))
|
(test '_this_->_member_ (unkebabify '-this-->-member-))
|
||||||
(test '__->>> (unkebabify '--->>>))
|
(test '__->>> (unkebabify '--->>>))
|
||||||
|
|
||||||
|
;; A Lisp predicate and mutator, in letters C takes. The operators
|
||||||
|
;; spelled with the same characters are not names
|
||||||
|
(test 'ident_startʔ (unkebabify 'ident-start?))
|
||||||
|
(test 'resetꜝ (unkebabify 'reset!))
|
||||||
|
(test '! (unkebabify '!))
|
||||||
|
(test '!= (unkebabify '!=))
|
||||||
|
|
||||||
;; Non-ASCII identifiers must survive intact. The `regex' egg's
|
;; Non-ASCII identifiers must survive intact. The `regex' egg's
|
||||||
;; string-substitute drops one trailing character per multi-byte
|
;; string-substitute drops one trailing character per multi-byte
|
||||||
;; character, which renames things silently -- the C still compiles,
|
;; character, which renames things silently -- the C still compiles,
|
||||||
|
|||||||
@@ -15,12 +15,15 @@
|
|||||||
semen
|
semen
|
||||||
utils)
|
utils)
|
||||||
|
|
||||||
|
(define (read-source source)
|
||||||
|
(with-input-from-string source
|
||||||
|
(lambda ()
|
||||||
|
(parameterize ((current-source-file "codegen.sex"))
|
||||||
|
(parse-all (current-input-port))))))
|
||||||
|
|
||||||
(define (sex->c source)
|
(define (sex->c source)
|
||||||
"Compile SOURCE, a string of Sex, and return the generated C."
|
"Compile SOURCE, a string of Sex, and return the generated C."
|
||||||
(let ((forms (with-input-from-string source
|
(let ((forms (read-source source)))
|
||||||
(lambda ()
|
|
||||||
(parameterize ((current-source-file "codegen.sex"))
|
|
||||||
(parse-all (current-input-port)))))))
|
|
||||||
(with-output-to-string
|
(with-output-to-string
|
||||||
(lambda ()
|
(lambda ()
|
||||||
(parameterize ((sex-line-directives 'none))
|
(parameterize ((sex-line-directives 'none))
|
||||||
@@ -37,22 +40,34 @@
|
|||||||
(define (emits? source fragment)
|
(define (emits? source fragment)
|
||||||
(and (string-contains (silently (lambda () (sex->c source))) fragment) #t))
|
(and (string-contains (silently (lambda () (sex->c source))) fragment) #t))
|
||||||
|
|
||||||
(define (error-message source)
|
(define (failure-message thunk)
|
||||||
"Compile SOURCE and return the error text as the user sees it --
|
"The error THUNK raises as the user sees it -- message plus arguments,
|
||||||
message plus arguments, the way CHICKEN prints it -- or #f if SOURCE
|
the way CHICKEN prints it -- or #f if it raises none."
|
||||||
compiles."
|
|
||||||
(handle-exceptions e
|
(handle-exceptions e
|
||||||
(with-output-to-string
|
(with-output-to-string
|
||||||
(lambda ()
|
(lambda ()
|
||||||
(display ((condition-property-accessor 'exn 'message) e))
|
(display ((condition-property-accessor 'exn 'message) e))
|
||||||
(for-each (lambda (a) (display " ") (write a))
|
(for-each (lambda (a) (display " ") (write a))
|
||||||
((condition-property-accessor 'exn 'arguments) e))))
|
((condition-property-accessor 'exn 'arguments) e))))
|
||||||
(begin (sex->c source) #f)))
|
(begin (thunk) #f)))
|
||||||
|
|
||||||
|
(define (error-message source)
|
||||||
|
"Compile SOURCE and return its error text, or #f if it compiles."
|
||||||
|
(failure-message (lambda () (sex->c source))))
|
||||||
|
|
||||||
(define (reports? source fragment)
|
(define (reports? source fragment)
|
||||||
(let ((m (error-message source)))
|
(let ((m (error-message source)))
|
||||||
(and m (string-contains m fragment) #t)))
|
(and m (string-contains m fragment) #t)))
|
||||||
|
|
||||||
|
(define (followed-by? source first then)
|
||||||
|
"Is FIRST in the C compiled from SOURCE, and the next thing past it,
|
||||||
|
whitespace aside, THEN?"
|
||||||
|
(let* ((c (silently (lambda () (sex->c source))))
|
||||||
|
(at (string-contains c first)))
|
||||||
|
(and at
|
||||||
|
(let ((next (string-skip c char-whitespace? (+ at (string-length first)))))
|
||||||
|
(and next (string-prefix? then (substring c next)))))))
|
||||||
|
|
||||||
(define (in-fn body)
|
(define (in-fn body)
|
||||||
(string-append "(fn f ((a int) (b int)) void " body ")"))
|
(string-append "(fn f ((a int) (b int)) void " body ")"))
|
||||||
|
|
||||||
@@ -78,6 +93,22 @@ compiles."
|
|||||||
;; c-switch handed its scrutinee straight to `cat', which only works
|
;; c-switch handed its scrutinee straight to `cat', which only works
|
||||||
;; when it is an atom. Anything else was displayed as a raw
|
;; when it is an atom. Anything else was displayed as a raw
|
||||||
;; s-expression: `switch ((%. e type))'.
|
;; s-expression: `switch ((%. e type))'.
|
||||||
|
;; 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
|
||||||
|
;; declaration before C23. Only a body that declares needs a block
|
||||||
|
(test-group "case bodies"
|
||||||
|
(test-assert "are blocks"
|
||||||
|
(followed-by? (in-fn "(switch a (case 1 (var x int 1) (g x)))")
|
||||||
|
"case 1:" "{"))
|
||||||
|
(test-assert "so is a default"
|
||||||
|
(followed-by? (in-fn "(switch a (default (var x int 1) (g x)))")
|
||||||
|
"default:" "{"))
|
||||||
|
(test-assert "where they declare"
|
||||||
|
(followed-by? (in-fn "(switch a (case 1 (g a)))") "case 1:" "g(a)"))
|
||||||
|
(test-assert "and one already a block is not wrapped again"
|
||||||
|
(not (followed-by? (in-fn "(switch a (case 1 (do (var x int 1) (g x))))")
|
||||||
|
"{" "{"))))
|
||||||
|
|
||||||
(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))))"
|
||||||
@@ -114,7 +145,58 @@ compiles."
|
|||||||
"(int *)&a"))
|
"(int *)&a"))
|
||||||
(test-assert "sizeof is left bare"
|
(test-assert "sizeof is left bare"
|
||||||
(emits? (in-fn "(var n int (cast (sizeof int) int))")
|
(emits? (in-fn "(var n int (cast (sizeof int) int))")
|
||||||
"(int)sizeof(int)")))
|
"(int)sizeof(int)"))
|
||||||
|
;; `|' reaches fmt-c as a string, which a list of binary heads to
|
||||||
|
;; parenthesise did not have
|
||||||
|
(test-assert "bitwise operand is parenthesised"
|
||||||
|
(emits? (in-fn "(var f float (cast (| a b) float))")
|
||||||
|
"(float)(a | b)"))
|
||||||
|
(test-assert "conditional operand is parenthesised"
|
||||||
|
(emits? (in-fn "(var f float (cast (if a a b) float))")
|
||||||
|
"(float)(a ? a : b)"))
|
||||||
|
;; A cast is the operand here, and binds looser than `->'
|
||||||
|
(test-assert "cast under a member access is parenthesised"
|
||||||
|
(emits? (in-line "(var v (* void) pp) (var x int (. (cast v (* struct point)) x))")
|
||||||
|
"((struct point*)v)->x"))
|
||||||
|
(test-assert "cast under a unary operator is left bare"
|
||||||
|
(emits? (in-line "(var v (* void) pp) (var x int (* (cast v (* int))))")
|
||||||
|
"*(int *)v")))
|
||||||
|
|
||||||
|
;; `?:' binds looser than every binary operator, so as an operand it
|
||||||
|
;; has to be parenthesised, or C takes the operator into its test
|
||||||
|
(test-group "conditional expression precedence"
|
||||||
|
(test-assert "operand of a binary operator"
|
||||||
|
(emits? (in-fn "(var x int (/ a (if a a b)))")
|
||||||
|
"a / (a ? a : b)"))
|
||||||
|
(test-assert "left bare where it is the whole value"
|
||||||
|
(emits? (in-fn "(var x int (if a a b))")
|
||||||
|
"int x = a ? a : b"))
|
||||||
|
(test-assert "left bare as an argument"
|
||||||
|
(emits? (in-fn "(g (if a a b) b)")
|
||||||
|
"g(a ? a : b, b)"))
|
||||||
|
;; ?: groups to the right, so a chain needs none in its else arm
|
||||||
|
(test-assert "a chain in the else arm is left bare"
|
||||||
|
(emits? (in-fn "(var x int (if a a (if b b 0)))")
|
||||||
|
"a ? a : b ? b : 0"))
|
||||||
|
(test-assert "but an assignment there is not"
|
||||||
|
(emits? (in-fn "(var x int (if a a (= b 1)))")
|
||||||
|
"a ? a : (b = 1)")))
|
||||||
|
|
||||||
|
;; A subscript binds tighter than anything, so what it subscripts is
|
||||||
|
;; parenthesised whenever it is an operator: `*q[i]' is *(q[i])
|
||||||
|
(test-group "subscript precedence"
|
||||||
|
(test-assert "a dereference"
|
||||||
|
(emits? "(fn f ((q (* * int)) (i int)) int (return [(* q) i]))" "(*q)[i]"))
|
||||||
|
(test-assert "a sum"
|
||||||
|
(emits? "(fn f ((p (* int))) int (return [(+ p 1) 0]))" "(p + 1)[0]"))
|
||||||
|
(test-assert "a cast"
|
||||||
|
(emits? "(fn f ((v (* void))) int (return [(cast v (* int)) 0]))" "((int *)v)[0]"))
|
||||||
|
(test-assert "a path is left bare"
|
||||||
|
(emits? (in-line "(var x int [(. p x) 0])") "p.x[0]"))
|
||||||
|
;; ...while the index is in brackets of its own
|
||||||
|
(test-assert "an index is never parenthesised"
|
||||||
|
(emits? "(fn f ((p (* int)) (i int)) int (return (cast [p (+ i 1)] int)))"
|
||||||
|
"(int)p[i + 1]")))
|
||||||
|
|
||||||
;; A comment among a call's arguments used to become an argument,
|
;; A comment among a call's arguments used to become an argument,
|
||||||
;; and c-apply put a comma on each side of it -- which does not
|
;; and c-apply put a comma on each side of it -- which does not
|
||||||
@@ -172,6 +254,16 @@ compiles."
|
|||||||
(emits? "(pub enum color (red green blue))" "enum color"))
|
(emits? "(pub enum color (red green blue))" "enum color"))
|
||||||
(test-assert "with its values"
|
(test-assert "with its values"
|
||||||
(emits? "(pub enum color (red green blue))" "red"))
|
(emits? "(pub enum color (red green blue))" "red"))
|
||||||
|
;; A value made the enumerator a pair, which went out unconverted
|
||||||
|
(test-assert "explicit values"
|
||||||
|
(emits? "(pub enum wire ((wire-varint 0) (wire-fixed64 1)))"
|
||||||
|
"wire_varint = 0, wire_fixed64 = 1"))
|
||||||
|
(test-assert "a value is an expression"
|
||||||
|
(emits? "(enum wire ((wire-last (+ WIRE-BASE 1))))"
|
||||||
|
"wire_last = WIRE_BASE + 1"))
|
||||||
|
(test-assert "and an anonymous enum takes them too"
|
||||||
|
(emits? "(fn f () void (var e (enum ((up-one 1) down)) up-one))"
|
||||||
|
"up_one = 1, down"))
|
||||||
(test-assert "and a non-pub enum still is too"
|
(test-assert "and a non-pub enum still is too"
|
||||||
(emits? "(enum color (red green blue))" "enum color"))
|
(emits? "(enum color (red green blue))" "enum color"))
|
||||||
;; Naming an enum as a type, rather than defining it, had no
|
;; Naming an enum as a type, rather than defining it, had no
|
||||||
@@ -246,6 +338,25 @@ compiles."
|
|||||||
|
|
||||||
;; A nested pointer chain used to silently lose a level:
|
;; A nested pointer chain used to silently lose a level:
|
||||||
;; (var p (* (* char))) emitted `char *p'
|
;; (var p (* (* char))) emitted `char *p'
|
||||||
|
;; A grouped header, C's parens carried over, died in the writer with
|
||||||
|
;; `(caddr) bad argument type' and no location
|
||||||
|
(test-group "malformed for"
|
||||||
|
(test-assert "a grouped header is named"
|
||||||
|
(reports? (in-fn "(for ((var i int 0) (< i 4) (++ i)) (g i))")
|
||||||
|
"header is not grouped"))
|
||||||
|
(test-assert "whatever its init is"
|
||||||
|
(reports? (in-fn "(for ((= a 0) (< a 4) (++ a)) (g a))")
|
||||||
|
"header is not grouped"))
|
||||||
|
(test-assert "even when the body fills the slots it left"
|
||||||
|
(reports? (in-fn "(for ((var i int 0) (< i 4) (++ i)) (g i) (g i) (g i))")
|
||||||
|
"header is not grouped"))
|
||||||
|
(test-assert "a short one gets the shape"
|
||||||
|
(reports? (in-fn "(for (var i int 0) (< i 4))")
|
||||||
|
"(for init test step body ...)"))
|
||||||
|
(test-assert "and both name the line"
|
||||||
|
(reports? "(fn f () void\n (for ((var i int 0) (< i 4) (++ i)) (g i)))"
|
||||||
|
"codegen.sex:2:")))
|
||||||
|
|
||||||
(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"))
|
||||||
@@ -328,7 +439,7 @@ compiles."
|
|||||||
(emits? "(fn f ((c (closure ((long)) int))) int (return (c 1)))"
|
(emits? "(fn f ((c (closure ((long)) int))) int (return (c 1)))"
|
||||||
"ƛc.code(&ƛc.env, ƛa0)"))
|
"ƛc.code(&ƛc.env, ƛa0)"))
|
||||||
(test-assert "a subscript receiver is evaluated once"
|
(test-assert "a subscript receiver is evaluated once"
|
||||||
(emits? "(fn f ((t (¤ (closure ((int)) int) 4)) (i int)) int (return ((¤ t (++ i)) 1)))"
|
(emits? "(fn f ((t [(closure ((int)) int) 4]) (i int)) int (return ([t (++ i)] 1)))"
|
||||||
"ƛint_int_call(t[++i], 1)"))
|
"ƛint_int_call(t[++i], 1)"))
|
||||||
(test-assert "so is a member receiver"
|
(test-assert "so is a member receiver"
|
||||||
(emits? "(struct h ((cb (closure ((int)) int))))
|
(emits? "(struct h ((cb (closure ((int)) int))))
|
||||||
@@ -449,7 +560,7 @@ compiles."
|
|||||||
(test-group "macro list accessors"
|
(test-group "macro list accessors"
|
||||||
(test-assert "third reads an array's length"
|
(test-assert "third reads an array's length"
|
||||||
(emits? "(defmacro (len t) (third t))
|
(emits? "(defmacro (len t) (third t))
|
||||||
(fn f () int (return (len (¤ int 7))))"
|
(fn f () int (return (len [int 7])))"
|
||||||
"return 7;"))
|
"return 7;"))
|
||||||
(test-assert "second reads a tag"
|
(test-assert "second reads a tag"
|
||||||
(emits? "(defmacro (tag t) (symbol->string (second t)))
|
(emits? "(defmacro (tag t) (symbol->string (second t)))
|
||||||
@@ -523,7 +634,7 @@ compiles."
|
|||||||
(fn f ((s (struct p))) void (var x (* _) (& s)))"
|
(fn f ((s (struct p))) void (var x (* _) (& s)))"
|
||||||
"struct p* x = &s"))
|
"struct p* x = &s"))
|
||||||
(test-assert "a wildcard inside an array"
|
(test-assert "a wildcard inside an array"
|
||||||
(emits? (in-fn "(var t (¤ _ 3) #((¤ int 3) : 1 2 3))") "int t[3]"))
|
(emits? (in-fn "(var t [_ 3] #([int 3] : 1 2 3))") "int t[3]"))
|
||||||
;; a compound literal carries its own type, where a brace
|
;; a compound literal carries its own type, where a brace
|
||||||
;; initializer has none and takes one from its context
|
;; initializer has none and takes one from its context
|
||||||
(test-assert "a compound literal answers a bare wildcard"
|
(test-assert "a compound literal answers a bare wildcard"
|
||||||
@@ -534,11 +645,11 @@ compiles."
|
|||||||
(reports? (in-fn "(var x _ #(1 2))") "cannot infer the type"))
|
(reports? (in-fn "(var x _ #(1 2))") "cannot infer the type"))
|
||||||
;; ...but its elements still solve the hole in an array type
|
;; ...but its elements still solve the hole in an array type
|
||||||
(test-assert "elements solve an array's element type"
|
(test-assert "elements solve an array's element type"
|
||||||
(emits? (in-fn "(var t (¤ _ 4) #(0 1 4 9))") "int t[4] = {0, 1, 4, 9}"))
|
(emits? (in-fn "(var t [_ 4] #(0 1 4 9))") "int t[4] = {0, 1, 4, 9}"))
|
||||||
(test-assert "including when there are fewer than the length"
|
(test-assert "including when there are fewer than the length"
|
||||||
(emits? (in-fn "(var t (¤ _ 10) #(1 2))") "int t[10] = {1, 2}"))
|
(emits? (in-fn "(var t [_ 10] #(1 2))") "int t[10] = {1, 2}"))
|
||||||
(test-assert "and they have to agree with each other"
|
(test-assert "and they have to agree with each other"
|
||||||
(reports? (in-fn "(var t (¤ _ 2) #(1 \"s\"))") "type mismatch"))
|
(reports? (in-fn "(var t [_ 2] #(1 \"s\"))") "type mismatch"))
|
||||||
;; a closure type reaches the solver as the struct that stands for
|
;; a closure type reaches the solver as the struct that stands for
|
||||||
;; it, which is the spelling `parse-type' knows
|
;; it, which is the spelling `parse-type' knows
|
||||||
(test-assert "a closure, from the signature that produced it"
|
(test-assert "a closure, from the signature that produced it"
|
||||||
@@ -631,7 +742,8 @@ compiles."
|
|||||||
(not (emits? "(struct pt ((x int))) (fn f ((p (* (struct pt)))) int (return (. (* p) x)))"
|
(not (emits? "(struct pt ((x int))) (fn f ((p (* (struct pt)))) int (return (. (* p) x)))"
|
||||||
"*(p).x")))
|
"*(p).x")))
|
||||||
(test-assert "member access through a cast"
|
(test-assert "member access through a cast"
|
||||||
(emits? (in-fn "(var n int (. (* (cast a (* (struct s)))) f))")
|
(emits? (string-append "(struct s ((f int)))"
|
||||||
|
(in-fn "(var n int (. (* (cast a (* (struct s)))) f))"))
|
||||||
"(*(struct s*)a).f"))
|
"(*(struct s*)a).f"))
|
||||||
;; ...without gaining parens where none are due
|
;; ...without gaining parens where none are due
|
||||||
(test-assert "a bare dereference is left alone"
|
(test-assert "a bare dereference is left alone"
|
||||||
@@ -642,26 +754,113 @@ compiles."
|
|||||||
(emits? (in-fn "(var n int (+ (- a) b))") "-a + b")))
|
(emits? (in-fn "(var n int (+ (- a) b))") "-a + b")))
|
||||||
|
|
||||||
;; An array bound was taken only when it was an integer literal, so a
|
;; An array bound was taken only when it was an integer literal, so a
|
||||||
;; symbolic one fell into the type: `(¤ int N)' came out `int N a[]'.
|
;; symbolic one fell into the type: `[int N]' came out `int N a[]'.
|
||||||
(test-group "array bounds"
|
(test-group "array bounds"
|
||||||
(test-assert "a symbolic bound"
|
(test-assert "a symbolic bound"
|
||||||
(emits? "(define N 4) (struct s ((a (¤ int N))))" "int a[N]"))
|
(emits? "(define N 4) (struct s ((a [int N])))" "int a[N]"))
|
||||||
(test-assert "an expression bound"
|
(test-assert "an expression bound"
|
||||||
(emits? "(define N 4) (struct s ((a (¤ char (* 2 N)))))" "char a[2 * N]"))
|
(emits? "(define N 4) (struct s ((a [char (* 2 N)])))" "char a[2 * N]"))
|
||||||
(test-assert "an integer bound still works"
|
(test-assert "an integer bound still works"
|
||||||
(emits? "(struct s ((a (¤ int 4))))" "int a[4]"))
|
(emits? "(struct s ((a [int 4])))" "int a[4]"))
|
||||||
|
;; ...and it is spelled the way the same name is in an expression
|
||||||
|
(test-assert "a bound's name is converted"
|
||||||
|
(emits? (in-fn "(var xs [u16 CHUNK-AREA])") "u16 xs[CHUNK_AREA]"))
|
||||||
|
(test-assert "so is one inside an expression bound"
|
||||||
|
(emits? (in-fn "(var xs [u16 (* 2 CHUNK-AREA)])") "u16 xs[2 * CHUNK_AREA]"))
|
||||||
;; a multi-word type is keywords all the way down, so a trailing
|
;; a multi-word type is keywords all the way down, so a trailing
|
||||||
;; keyword belongs to the type and leaves the array unsized
|
;; keyword belongs to the type and leaves the array unsized
|
||||||
(test-assert "a multi-word type is not a bound"
|
(test-assert "a multi-word type is not a bound"
|
||||||
(emits? "(struct s ((a (¤ unsigned int))))" "unsigned int a[]"))
|
(emits? "(struct s ((a [unsigned int])))" "unsigned int a[]"))
|
||||||
;; ...and a tag always follows its keyword
|
;; ...and a tag always follows its keyword
|
||||||
(test-assert "nor is an aggregate tag"
|
(test-assert "nor is an aggregate tag"
|
||||||
(emits? "(struct t ((z int))) (struct s ((a (¤ struct t))))" "struct t a[]"))
|
(emits? "(struct t ((z int))) (struct s ((a [struct t])))" "struct t a[]"))
|
||||||
(test-assert "nor one behind a pointer"
|
(test-assert "nor one behind a pointer"
|
||||||
(emits? "(struct t ((z int))) (struct s ((a (¤ * struct t))))" "struct t* a[]")))
|
(emits? "(struct t ((z int))) (struct s ((a [* struct t])))" "struct t* a[]"))
|
||||||
|
;; a qualifier waits for a type name only when none came before it
|
||||||
|
(test-assert "a name after a qualified type is a bound"
|
||||||
|
(emits? (in-fn "(var b [int const N])") "int const b[N]"))
|
||||||
|
(test-assert "after a qualified name too"
|
||||||
|
(emits? (in-fn "(var b [mytype const N])") "mytype const b[N]"))
|
||||||
|
(test-assert "but a qualifier alone still waits for the type"
|
||||||
|
(emits? "(typedef mytype int) (struct s ((a [const mytype])))" "const mytype a[]")))
|
||||||
|
|
||||||
|
;; `sizeof' takes a type or an expression, and `(* x)' is either.
|
||||||
|
;; What is in scope says which: a name C alone knows is a type
|
||||||
|
(test-group "sizeof"
|
||||||
|
(test-assert "a pointer type"
|
||||||
|
(emits? (in-fn "(var n size-t (sizeof (* char)))") "sizeof(char *)"))
|
||||||
|
(test-assert "a qualified one"
|
||||||
|
(emits? (in-fn "(var n size-t (sizeof (* const char)))") "sizeof(const char *)"))
|
||||||
|
(test-assert "to a type only C knows of"
|
||||||
|
(emits? (in-fn "(var n size-t (sizeof (* size-t)))") "sizeof(size_t *)"))
|
||||||
|
(test-assert "a multi-word type"
|
||||||
|
(emits? (in-fn "(var n size-t (sizeof (unsigned int)))") "sizeof(unsigned int)"))
|
||||||
|
(test-assert "an array type"
|
||||||
|
(emits? (in-fn "(var n size-t (sizeof [int 4]))") "sizeof(int [4])"))
|
||||||
|
(test-assert "a dereference of a local"
|
||||||
|
(emits? (in-fn "(var p (* int) 0) (var n size-t (sizeof (* p)))") "sizeof(*p)"))
|
||||||
|
(test-assert "of a parameter"
|
||||||
|
(emits? "(fn f ((p (* int))) void (var n size-t (sizeof (* p))))" "sizeof(*p)"))
|
||||||
|
(test-assert "of a call"
|
||||||
|
(emits? (in-fn "(var n size-t (sizeof (* (g))))") "sizeof(*g())"))
|
||||||
|
(test-assert "a subscript"
|
||||||
|
(emits? (in-fn "(var xs [int 4]) (var n size-t (sizeof [xs 0]))") "sizeof(xs[0])")))
|
||||||
|
|
||||||
|
;; A double is written in as few digits as read back as the same
|
||||||
|
;; double: CHICKEN prints 15 by default, and 0.16666666666666666 came
|
||||||
|
;; out 0.166666666666667, another number
|
||||||
|
(test-group "double literals"
|
||||||
|
(test-assert "keep every digit they need"
|
||||||
|
(emits? (in-fn "(var d double 0.16666666666666666)") "d = 0.16666666666666666;"))
|
||||||
|
(test-assert "and gain none they do not"
|
||||||
|
(emits? (in-fn "(var d double 0.1)") "d = 0.1;"))
|
||||||
|
(test-assert "sixteen when that is enough"
|
||||||
|
(emits? (in-fn "(var d double 0.9871465422297019)") "d = 0.9871465422297019;")))
|
||||||
|
|
||||||
|
;; A define's body was pasted unwalked, and unparenthesised: `cast'
|
||||||
|
;; stayed a call, and (* x D) regrouped with D's own operator
|
||||||
|
(test-group "defines"
|
||||||
|
(test-assert "an atom is left bare"
|
||||||
|
(emits? "(define N 4)" "#define N 4"))
|
||||||
|
(test-assert "and its name converted"
|
||||||
|
(emits? "(define MAX-N MIN-N)" "#define MAX_N MIN_N"))
|
||||||
|
(test-assert "an expression is walked and parenthesised"
|
||||||
|
(emits? "(define K (/ (cast 255 float) (cast 60 float)))"
|
||||||
|
"#define K ((float)255 / (float)60)"))
|
||||||
|
(test-assert "and a negated name"
|
||||||
|
(emits? "(define LOWEST -INFINITY)" "#define LOWEST (-INFINITY)"))
|
||||||
|
(test-assert "so is a negative literal"
|
||||||
|
(emits? "(define NEG -1)" "#define NEG (-1)"))
|
||||||
|
(test-assert "a function-like one is converted too"
|
||||||
|
(emits? "(define (sq-of x) (* x x))" "#define sq_of(x) ((x) * (x))")))
|
||||||
|
|
||||||
|
;; A typedef's target is a type, and was walked as an expression: a
|
||||||
|
;; pointer read as a dereference, an array as a subscript
|
||||||
|
(test-group "typedefs"
|
||||||
|
(test-assert "of a name"
|
||||||
|
(emits? "(typedef small int)" "typedef int small;"))
|
||||||
|
(test-assert "of a pointer"
|
||||||
|
(emits? "(typedef cstr (* const char))" "typedef const char * cstr;"))
|
||||||
|
(test-assert "of an array"
|
||||||
|
(emits? "(typedef vec4 [float 4])" "typedef float vec4[4];"))
|
||||||
|
(test-assert "of a function pointer"
|
||||||
|
(emits? "(typedef colour-fn (fn ((i32)) u32))"
|
||||||
|
"typedef u32 (*colour_fn)(i32);")))
|
||||||
|
|
||||||
;; `.' is C's `.' or its `->' depending on what is being read from,
|
;; `.' is C's `.' or its `->' depending on what is being read from,
|
||||||
;; so a path reads the same however the thing it starts from is held
|
;; so a path reads the same however the thing it starts from is held
|
||||||
|
;; ...and inside a literal, which the walk used to pass by
|
||||||
|
(test-group "member access in a literal"
|
||||||
|
(test-assert "compound"
|
||||||
|
(emits? (in-line "(var q (struct point) #(struct point : (. pp x) (. pp y)))")
|
||||||
|
"(struct point){pp->x, pp->y}"))
|
||||||
|
(test-assert "brace initializer"
|
||||||
|
(emits? (in-line "(var q (struct point) #((. pp x) (. pp y)))")
|
||||||
|
"{pp->x, pp->y}"))
|
||||||
|
(test-assert "designated"
|
||||||
|
(emits? (in-line "(var q (struct point) #(.y (. pp x)))")
|
||||||
|
"{.y = pp->x}")))
|
||||||
|
|
||||||
(test-group "member access"
|
(test-group "member access"
|
||||||
(test-assert "a value reads with a dot"
|
(test-assert "a value reads with a dot"
|
||||||
(emits? (in-line "(= (. p x) 1)") "p.x = 1"))
|
(emits? (in-line "(= (. p x) 1)") "p.x = 1"))
|
||||||
@@ -685,6 +884,22 @@ compiles."
|
|||||||
(emits? (in-line "(= (. (guess) x) 1)") "guess().x = 1"))
|
(emits? (in-line "(= (. (guess) x) 1)") "guess().x = 1"))
|
||||||
(test-assert "and said so"
|
(test-assert "and said so"
|
||||||
(warns? (in-line "(= (. (guess) x) 1)") "cannot type this"))
|
(warns? (in-line "(= (. (guess) x) 1)") "cannot type this"))
|
||||||
|
;; Past a typed base it is a field that cannot be typed -- one of a
|
||||||
|
;; struct only C knows -- and the warning named the base regardless
|
||||||
|
(test-assert "naming the path read so far"
|
||||||
|
(warns? (in-fn "(var t (* struct tm) 0) (= (. t a b) 1)")
|
||||||
|
"as a value (. t a)\n"))
|
||||||
|
(test-assert "which one step through it does not need"
|
||||||
|
(not (warns? (in-fn "(var t (* struct tm) 0) (= (. t a) 1)")
|
||||||
|
"cannot type")))
|
||||||
|
(test-assert "but a struct declared here has only the fields it names"
|
||||||
|
(reports? (in-line "(= (. p z) 1)") "no field of this name"))
|
||||||
|
(test-assert "the last step included"
|
||||||
|
(reports? (in-line "(= (. lp a z) 1)") "no field of this name"))
|
||||||
|
(test-assert "an indexed one by its name"
|
||||||
|
(reports? (string-append "(struct v ((xs [int 2])))"
|
||||||
|
(in-fn "(var w (struct v)) (= (. w [ys 0]) 1)"))
|
||||||
|
"no field of this name"))
|
||||||
;; the arrow is `%->' and nothing else writes one, so a bare `->'
|
;; the arrow is `%->' and nothing else writes one, so a bare `->'
|
||||||
;; is an ordinary head -- a threading macro that nobody imported,
|
;; is an ordinary head -- a threading macro that nobody imported,
|
||||||
;; and a call like any other unknown
|
;; and a call like any other unknown
|
||||||
|
|||||||
@@ -5,8 +5,19 @@
|
|||||||
;;; 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', `emits?', `reports?' and `in-fn' come from codegen.scm,
|
;;; `sex->c', `read-source', `failure-message', `emits?', `reports?'
|
||||||
;;; which run.scm includes first.
|
;;; and `in-fn' come from codegen.scm, which run.scm includes first.
|
||||||
|
|
||||||
|
(import sex-modules)
|
||||||
|
|
||||||
|
(define (interface-of source)
|
||||||
|
"The declarations SOURCE, compiled as a module, hands its importers
|
||||||
|
ahead of its pub forms."
|
||||||
|
(check-public-interface (read-source source)))
|
||||||
|
|
||||||
|
(define (interface-reports? source fragment)
|
||||||
|
(let ((m (failure-message (lambda () (interface-of source)))))
|
||||||
|
(and m (string-contains m fragment) #t)))
|
||||||
|
|
||||||
(define (ops-src . body)
|
(define (ops-src . body)
|
||||||
(string-append
|
(string-append
|
||||||
@@ -19,6 +30,13 @@
|
|||||||
"(fn f () void (var v vec2 #(vec2 : 1.0 2.0)) (var p (* vec2) (& v)) "
|
"(fn f () void (var v vec2 #(vec2 : 1.0 2.0)) (var p (* vec2) (& v)) "
|
||||||
(apply string-append body) ")"))
|
(apply string-append body) ")"))
|
||||||
|
|
||||||
|
(define forward-src
|
||||||
|
(string-append
|
||||||
|
"(record pb ((n int)))"
|
||||||
|
"(ops pb"
|
||||||
|
" (fn twice ((self (* pb))) int (return (* 2 (.len self))))"
|
||||||
|
" (pub fn len ((self (* pb))) int (return (. self n))))"))
|
||||||
|
|
||||||
(define (sum-src . body)
|
(define (sum-src . body)
|
||||||
(string-append
|
(string-append
|
||||||
"(sum shape ((circle ((r float))) (rect ((w float) (h float))) (empty ())))"
|
"(sum shape ((circle ((r float))) (rect ((w float) (h float))) (empty ())))"
|
||||||
@@ -79,7 +97,15 @@
|
|||||||
" (fn g ((s v)) int (return 2)))")
|
" (fn g ((s v)) int (return 2)))")
|
||||||
"method is already defined on"))
|
"method is already defined on"))
|
||||||
(test-assert "pub goes on the methods, not the block"
|
(test-assert "pub goes on the methods, not the block"
|
||||||
(reports? "(record v ((x int)))(pub ops v)" "pub belongs on the methods")))
|
(reports? "(record v ((x int)))(pub ops v)" "pub belongs on the methods"))
|
||||||
|
;; Every method is known before any body is walked, and C is told of
|
||||||
|
;; one before a body above it calls it -- and only then
|
||||||
|
(test-assert "a method can call one written below it"
|
||||||
|
(emits? forward-src "return 2 * pbːlen(self)"))
|
||||||
|
(test-assert "which is declared ahead of the call"
|
||||||
|
(emits? forward-src "int pbːlen (pb * self);"))
|
||||||
|
(test-assert "and nothing else is"
|
||||||
|
(not (emits? (ops-src "") "vec2ːsum_of (vec2 self);"))))
|
||||||
|
|
||||||
;; The method sits in the head, so the shape of a form can be read
|
;; The method sits in the head, so the shape of a form can be read
|
||||||
;; without knowing the receiver's type.
|
;; without knowing the receiver's type.
|
||||||
@@ -466,4 +492,75 @@
|
|||||||
"switch (t.tag)"))
|
"switch (t.tag)"))
|
||||||
(test-assert "and a scrutinee that is not a sum"
|
(test-assert "and a scrutinee that is not a sum"
|
||||||
(reports? "(fn f () void (var n int 1) (match n ((a ()) (= n 2))))"
|
(reports? "(fn f () void (var n int 1) (match n ((a ()) (= n 2))))"
|
||||||
"match wants a sum"))))
|
"match wants a sum")))
|
||||||
|
|
||||||
|
;; An importer is given the pub forms and nothing else, so a type one
|
||||||
|
;; of them names has to be among them, or C is told no more of it
|
||||||
|
;; than a declaration can say
|
||||||
|
(test-group "what importers can see"
|
||||||
|
(test "a private record behind a pointer is declared for them"
|
||||||
|
'((record cell))
|
||||||
|
(interface-of "(record cell ((v int))) (pub record shape ((c (* cell))))"))
|
||||||
|
(test "once"
|
||||||
|
'((record cell))
|
||||||
|
(interface-of "(record cell ((v int))) (pub fn f ((a (* cell)) (b (* cell))) void)"))
|
||||||
|
(test "a struct as a struct"
|
||||||
|
'((struct cell))
|
||||||
|
(interface-of "(struct cell ((v int))) (pub fn f ((a (* struct cell))) void)"))
|
||||||
|
(test "a pub one needs nothing"
|
||||||
|
'()
|
||||||
|
(interface-of "(pub record cell ((v int))) (pub record shape ((c cell)))"))
|
||||||
|
(test "nor one only C knows"
|
||||||
|
'()
|
||||||
|
(interface-of "(pub fn f ((p (* FILE)) (n size-t)) void)"))
|
||||||
|
(test "nor a private fn's"
|
||||||
|
'()
|
||||||
|
(interface-of "(record cell ((v int))) (fn f ((c cell)) void)"))
|
||||||
|
(test-assert "held by value it is an error"
|
||||||
|
(interface-reports? "(record cell ((v int))) (pub record shape ((c cell)))"
|
||||||
|
"held by value"))
|
||||||
|
(test-assert "naming the line"
|
||||||
|
(interface-reports? "(record cell ((v int)))\n(pub record shape ((c cell)))"
|
||||||
|
"codegen.sex:2:"))
|
||||||
|
(test-assert "in an array too"
|
||||||
|
(interface-reports? "(record cell ((v int))) (pub record shape ((cs [cell 4])))"
|
||||||
|
"held by value"))
|
||||||
|
(test-assert "returned"
|
||||||
|
(interface-reports? "(record cell ((v int))) (pub fn f () cell)" "held by value"))
|
||||||
|
(test-assert "or taken by a function a pub one takes"
|
||||||
|
(interface-reports? "(record cell ((v int))) (pub fn take ((cb (fn ((c cell)) void))) void)"
|
||||||
|
"held by value"))
|
||||||
|
(test-assert "or returned by one"
|
||||||
|
(interface-reports? "(record cell ((v int))) (pub fn take ((cb (fn () cell))) void)"
|
||||||
|
"held by value"))
|
||||||
|
(test "but a pointer there is still a pointer"
|
||||||
|
'((record cell))
|
||||||
|
(interface-of "(record cell ((v int))) (pub fn take ((cb (fn ((c (* cell))) void))) void)"))
|
||||||
|
(test-assert "or taken by a pub method"
|
||||||
|
(interface-reports? "(record cell ((v int))) (ops cell (pub fn get ((self cell)) int))"
|
||||||
|
"held by value"))
|
||||||
|
(test-assert "a typedef has no declaration to give, pointer or not"
|
||||||
|
(interface-reports? "(typedef small int) (pub fn f ((p (* small))) void)"
|
||||||
|
"this typedef, and C declares one only by defining it"))
|
||||||
|
(test-assert "nor has an enum"
|
||||||
|
(interface-reports? "(enum mood (up down)) (pub var m (* enum mood))"
|
||||||
|
"this enum, and C declares"))
|
||||||
|
(test-assert "a pub handle is held through a pointer"
|
||||||
|
(interface-reports? "(pub record h) (record h ((n int))) (pub fn f ((x h)) void)"
|
||||||
|
"only a handle"))
|
||||||
|
(test "which is all it takes"
|
||||||
|
'()
|
||||||
|
(interface-of "(pub record h) (record h ((n int))) (pub fn f ((x (* h))) void)"))
|
||||||
|
;; A constant has no declaration either, and an array bound or a
|
||||||
|
;; pub define's body hands its name to importers all the same
|
||||||
|
(test-assert "a constant sizing a pub record's array"
|
||||||
|
(interface-reports? "(define N 4) (pub record r ((xs [int N])))"
|
||||||
|
"this define, and C declares one only by defining it"))
|
||||||
|
(test-assert "or used by a pub define"
|
||||||
|
(interface-reports? "(define N 4) (pub define M (* 2 N))" "this define"))
|
||||||
|
(test "is fine when pub"
|
||||||
|
'()
|
||||||
|
(interface-of "(pub define N 4) (pub record r ((xs [int N])))"))
|
||||||
|
(test "as is a literal bound"
|
||||||
|
'()
|
||||||
|
(interface-of "(pub record r ((xs [int 4])))"))))
|
||||||
|
|||||||
@@ -1,26 +0,0 @@
|
|||||||
# The failure paths.
|
|
||||||
#
|
|
||||||
# Both need a process to show themselves, so neither 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 --
|
|
||||||
# and it has to clean up its temporary .c when its own emission throws.
|
|
||||||
#
|
|
||||||
# Everything is built inside a scratch TMPDIR, 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; \
|
|
||||||
rm -rf $$d; echo "exit code ok"
|
|
||||||
|
|
||||||
.PHONY: check
|
|
||||||
62
tests/exit-code/check.scm
Normal file
62
tests/exit-code/check.scm
Normal 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)
|
||||||
@@ -1,3 +0,0 @@
|
|||||||
(module fmt-c-writer
|
|
||||||
*
|
|
||||||
"../fmt-c-writer.scm")
|
|
||||||
@@ -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")
|
|
||||||
@@ -86,7 +86,10 @@
|
|||||||
" (: done)" ; 52
|
" (: done)" ; 52
|
||||||
" (var svar u64 (sizeof (struct pt)))" ; 53
|
" (var svar u64 (sizeof (struct pt)))" ; 53
|
||||||
" (var cvar int (cast mvar int))" ; 54
|
" (var cvar int (cast mvar int))" ; 54
|
||||||
" (return 122))" ; 55
|
" (putchar (if (< mvar 123)" ; 55 a conditional as an
|
||||||
|
" (putchar 124)" ; 56 argument: an expression,
|
||||||
|
" 125))" ; 57 so no anchor inside it
|
||||||
|
" (return 122))" ; 58
|
||||||
))
|
))
|
||||||
|
|
||||||
(define fixture (string-intersperse fixture-lines "\n"))
|
(define fixture (string-intersperse fixture-lines "\n"))
|
||||||
@@ -231,7 +234,14 @@ after that is one more. Lines before the first directive get #f."
|
|||||||
;; Expressions that are their own statement
|
;; Expressions that are their own statement
|
||||||
(test-group "expressions"
|
(test-group "expressions"
|
||||||
(maps/tokens "sizeof" "(var svar" "sizeof")
|
(maps/tokens "sizeof" "(var svar" "sizeof")
|
||||||
(maps/tokens "cast" "(var cvar" "(int)mvar"))
|
(maps/tokens "cast" "(var cvar" "(int)mvar")
|
||||||
|
;; an `if' arm is a statement only where the `if' is one: a
|
||||||
|
;; directive between `?' and `:' does not compile
|
||||||
|
(maps "conditional argument" "123")
|
||||||
|
(test-assert "holds no directive"
|
||||||
|
(let ((start (string-contains c-out "123")))
|
||||||
|
(not (string-contains (substring c-out start (string-contains c-out "125" start))
|
||||||
|
"#line")))))
|
||||||
|
|
||||||
;; A location is (file . line); every directive must name the file the
|
;; A location is (file . line); every directive must name the file the
|
||||||
;; form was read from.
|
;; form was read from.
|
||||||
|
|||||||
22
tests/module-path.scm
Normal file
22
tests/module-path.scm
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
;;; Where imports are looked for. SEX_MODULE_PATH is read once, at the
|
||||||
|
;;; start, from the directory sexc was started in.
|
||||||
|
|
||||||
|
(import (chicken pathname)
|
||||||
|
(chicken process-context)
|
||||||
|
sex-modules)
|
||||||
|
|
||||||
|
(test-group "module path"
|
||||||
|
;; make-absolute-pathname took `src' for a directory under the root,
|
||||||
|
;; and an import searched /src/
|
||||||
|
(set-environment-variable! "SEX_MODULE_PATH" "src:/abs/lib")
|
||||||
|
(load-persistent-module-paths)
|
||||||
|
(test "a relative entry is from the working directory"
|
||||||
|
(make-absolute-pathname (current-directory) "src")
|
||||||
|
(second (get-module-paths)))
|
||||||
|
(test "an absolute one is as written"
|
||||||
|
"/abs/lib"
|
||||||
|
(third (get-module-paths)))
|
||||||
|
|
||||||
|
(set-environment-variable! "SEX_MODULE_PATH" "")
|
||||||
|
(load-persistent-module-paths)
|
||||||
|
(unset-environment-variable! "SEX_MODULE_PATH"))
|
||||||
@@ -1,39 +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.
|
|
||||||
|
|
||||||
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\ncolour 123
|
|
||||||
|
|
||||||
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
|
|
||||||
86
tests/modules/check.scm
Normal file
86
tests/modules/check.scm
Normal 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)
|
||||||
@@ -47,6 +47,11 @@
|
|||||||
(printf "tally %d\n" (.count t))
|
(printf "tally %d\n" (.count t))
|
||||||
(.drop t)
|
(.drop t)
|
||||||
|
|
||||||
|
;; neither cell nor ledger is visible here, only declared
|
||||||
|
(var b board (make-board))
|
||||||
|
(printf "board %d\n" (board-total (& b)))
|
||||||
|
(printf "ledger %d\n" (ledger-total (greet-ledger)))
|
||||||
|
|
||||||
(var c colour (colour/rgb 1 2 3))
|
(var c colour (colour/rgb 1 2 3))
|
||||||
(match c
|
(match c
|
||||||
((rgb (r g b)) (printf "colour %d%d%d\n" r g b))
|
((rgb (r g b)) (printf "colour %d%d%d\n" r g b))
|
||||||
|
|||||||
@@ -8,6 +8,8 @@
|
|||||||
(pub include stdio.h)
|
(pub include stdio.h)
|
||||||
(include stdlib.h)
|
(include stdlib.h)
|
||||||
|
|
||||||
|
(import ledger)
|
||||||
|
|
||||||
(pub var greet-count ;; a comment in the header of a public form is
|
(pub var greet-count ;; a comment in the header of a public form is
|
||||||
;; not part of it: what the importer is given has
|
;; not part of it: what the importer is given has
|
||||||
;; to be `extern int greet-count', not a form
|
;; to be `extern int greet-count', not a form
|
||||||
@@ -30,12 +32,36 @@
|
|||||||
(lambda (name field-type)
|
(lambda (name field-type)
|
||||||
`(printf "%s " ,(symbol->string name))))))
|
`(printf "%s " ,(symbol->string name))))))
|
||||||
|
|
||||||
|
(var greetings ledger #(ledger : 0))
|
||||||
|
|
||||||
(pub fn greet ;; ...and here the prototype would lose its return type
|
(pub fn greet ;; ...and here the prototype would lose its return type
|
||||||
((name (* const char))) void
|
((name (* const char))) void
|
||||||
"Print a greeting for NAME."
|
"Print a greeting for NAME."
|
||||||
(++ greet-count)
|
(++ greet-count)
|
||||||
|
(ledger-add (& greetings) 1)
|
||||||
(printf "hello, %s\n" name))
|
(printf "hello, %s\n" name))
|
||||||
|
|
||||||
|
;;; Types the importer cannot see, crossing behind a pointer. `cell' is
|
||||||
|
;;; private and `ledger' was imported without `pub', and the importer
|
||||||
|
;;; is handed a declaration of each and nothing else.
|
||||||
|
(record cell ((v int)))
|
||||||
|
|
||||||
|
(var the-cell cell #(cell : 40))
|
||||||
|
|
||||||
|
(pub record board ((first (* cell)) (size int)))
|
||||||
|
|
||||||
|
(pub fn make-board () board
|
||||||
|
(return #(board : (& the-cell) 2)))
|
||||||
|
|
||||||
|
(pub fn board-total ((b (* const board))) int
|
||||||
|
(return (+ (. b first v) (. b size))))
|
||||||
|
|
||||||
|
(pub fn greet-ledger () (* ledger)
|
||||||
|
(return (& greetings)))
|
||||||
|
|
||||||
|
(pub fn ledger-total ((l (* const ledger))) int
|
||||||
|
(return (. l total)))
|
||||||
|
|
||||||
;;; A closure type crossing the boundary. Both units generate the
|
;;; A closure type crossing the boundary. Both units generate the
|
||||||
;;; struct for this signature independently, so they have to agree on
|
;;; struct for this signature independently, so they have to agree on
|
||||||
;;; its tag and its layout, or the value is passed wrong and nothing
|
;;; its tag and its layout, or the value is passed wrong and nothing
|
||||||
|
|||||||
8
tests/modules/ledger.sex
Normal file
8
tests/modules/ledger.sex
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
;;; A module greet.sex imports without `pub'. greet-app.sex never
|
||||||
|
;;; imports it, yet holds a (* ledger) that greet hands out: what greet
|
||||||
|
;;; exports has to declare the type for it.
|
||||||
|
|
||||||
|
(pub record ledger ((total int)))
|
||||||
|
|
||||||
|
(pub fn ledger-add ((l (* ledger)) (n int)) void
|
||||||
|
(+= (. l total) n))
|
||||||
@@ -1,3 +0,0 @@
|
|||||||
(module reader
|
|
||||||
*
|
|
||||||
"../reader.scm")
|
|
||||||
@@ -38,6 +38,22 @@
|
|||||||
;; a `.'-prefixed symbol is an ordinary symbol, not dot-access
|
;; a `.'-prefixed symbol is an ordinary symbol, not dot-access
|
||||||
(reader-test '((.field obj)) "(.field obj)")
|
(reader-test '((.field obj)) "(.field obj)")
|
||||||
|
|
||||||
|
;; a leading `-' on a name is negation, the way it is on a number:
|
||||||
|
;; no name is spelled with one, and -INFINITY otherwise mangled into
|
||||||
|
;; _INFINITY. The operators that start with one are left alone
|
||||||
|
(reader-test '((- INFINITY)) "-INFINITY")
|
||||||
|
(reader-test '((var best double (- INFINITY))) "(var best double -INFINITY)")
|
||||||
|
(reader-test '((- _x)) "-_x")
|
||||||
|
(reader-test '(-1 -1.5) "-1 -1.5")
|
||||||
|
(reader-test '(- -> ->> -- -=) "- -> ->> -- -=")
|
||||||
|
|
||||||
|
;; a number in another radix, spelled the way C does or the way
|
||||||
|
;; Scheme does; either is an ordinary integer from here on
|
||||||
|
(reader-test '(224 224 5 5 -16) "0xE0 0Xe0 0b101 0B101 -0x10")
|
||||||
|
(reader-test '(128 128 5 8 -16) "#x80 #X80 #b101 #o10 #x-10")
|
||||||
|
;; ...and only a number: a prefix with nothing after it is a name
|
||||||
|
(reader-test '(0x 0xg) "0x 0xg")
|
||||||
|
|
||||||
;; `;' comments are preserved as (comment "...") forms
|
;; `;' comments are preserved as (comment "...") forms
|
||||||
(reader-test '((comment " hi")) "; hi")
|
(reader-test '((comment " hi")) "; hi")
|
||||||
(reader-test '((comment ";; Prototypes")) ";;; Prototypes")
|
(reader-test '((comment ";; Prototypes")) ";;; Prototypes")
|
||||||
|
|||||||
@@ -1,17 +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 "types.scm")
|
(include-relative "module-path.scm")
|
||||||
(include "infer.scm")
|
(include-relative "types.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)
|
||||||
|
|||||||
@@ -1,2 +0,0 @@
|
|||||||
(module semen *
|
|
||||||
"../semen.scm")
|
|
||||||
@@ -1,3 +0,0 @@
|
|||||||
(module sex-macros
|
|
||||||
*
|
|
||||||
"../sex-macros.scm")
|
|
||||||
@@ -1,3 +0,0 @@
|
|||||||
(module sex-modules
|
|
||||||
*
|
|
||||||
"../sex-modules.scm")
|
|
||||||
@@ -66,12 +66,12 @@
|
|||||||
|
|
||||||
(printf "Through a parameter: %d\n" (apply-twice (make-adder 50) 10))
|
(printf "Through a parameter: %d\n" (apply-twice (make-adder 50) 10))
|
||||||
|
|
||||||
(var table (¤ (closure ((int)) int) 3))
|
(var table [(closure ((int)) int) 3])
|
||||||
(var i int 0)
|
(var i int 0)
|
||||||
(for (= i 0) (< i 3) (++ i)
|
(for (= i 0) (< i 3) (++ i)
|
||||||
(= (¤ table i) (make-adder i)))
|
(= [table i] (make-adder i)))
|
||||||
(printf "From an array: %d %d %d\n"
|
(printf "From an array: %d %d %d\n"
|
||||||
((¤ table 0) 1) ((¤ table 1) 1) ((¤ table 2) 1))
|
([table 0] 1) ([table 1] 1) ([table 2] 1))
|
||||||
|
|
||||||
;; the index must be evaluated once, so `i' ends at 1 and not 2 --
|
;; the index must be evaluated once, so `i' ends at 1 and not 2 --
|
||||||
;; read in a separate statement, since reading and bumping it in one
|
;; read in a separate statement, since reading and bumping it in one
|
||||||
|
|||||||
@@ -5,7 +5,8 @@
|
|||||||
"through a pointer 9"
|
"through a pointer 9"
|
||||||
"array 10 20 30"
|
"array 10 20 30"
|
||||||
"argument 6"
|
"argument 6"
|
||||||
"mixed 1 5")
|
"mixed 1 5"
|
||||||
|
"read through a pointer 1 5")
|
||||||
(return 0)
|
(return 0)
|
||||||
|
|
||||||
;;; `:' inside #(...) ends a type and makes the rest a C99 compound
|
;;; `:' inside #(...) ends a type and makes the rest a C99 compound
|
||||||
@@ -38,7 +39,7 @@
|
|||||||
|
|
||||||
;; an array literal decays the way an array does
|
;; an array literal decays the way an array does
|
||||||
(var a (* int) #([int 3] : 10 20 30))
|
(var a (* int) #([int 3] : 10 20 30))
|
||||||
(printf "array %d %d %d\n" (¤ a 0) (¤ a 1) (¤ a 2))
|
(printf "array %d %d %d\n" [a 0] [a 1] [a 2])
|
||||||
|
|
||||||
(printf "argument %d\n" (sum #(struct point : 2 4)))
|
(printf "argument %d\n" (sum #(struct point : 2 4)))
|
||||||
|
|
||||||
@@ -46,4 +47,10 @@
|
|||||||
(var m (struct point) #(struct point : 1 .y 5))
|
(var m (struct point) #(struct point : 1 .y 5))
|
||||||
(printf "mixed %d %d\n" (. m x) (. m y))
|
(printf "mixed %d %d\n" (. m x) (. m y))
|
||||||
|
|
||||||
|
;; what a literal holds is walked like any expression, so a path in
|
||||||
|
;; it reads through a pointer as it does outside one
|
||||||
|
(var pm (* (struct point)) (& m))
|
||||||
|
(var c (struct point) #(struct point : (. pm x) (. pm y)))
|
||||||
|
(printf "read through a pointer %d %d\n" (. c x) (. c y))
|
||||||
|
|
||||||
(return 0))
|
(return 0))
|
||||||
|
|||||||
@@ -158,7 +158,7 @@
|
|||||||
;;;
|
;;;
|
||||||
;;; 1. SETTLED. `type-match' took `_' on the pattern side, so
|
;;; 1. SETTLED. `type-match' took `_' on the pattern side, so
|
||||||
;;; `(closure _ int)' above is one clause rather than one per
|
;;; `(closure _ int)' above is one clause rather than one per
|
||||||
;;; signature, and `(* _)' and `(¤ _ _)' say "any pointer" and "any
|
;;; signature, and `(* _)' and `[_ _]' say "any pointer" and "any
|
||||||
;;; array". A `_' written last takes the rest, since a type's words
|
;;; array". A `_' written last takes the rest, since a type's words
|
||||||
;;; are spread and not nested: `(* const char)' is three elements.
|
;;; are spread and not nested: `(* const char)' is three elements.
|
||||||
;;; Nothing destructures -- a macro body is Scheme and a type is a
|
;;; Nothing destructures -- a macro body is Scheme and a type is a
|
||||||
|
|||||||
@@ -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)
|
||||||
|
|||||||
@@ -7,7 +7,9 @@
|
|||||||
"unsigned wins either way: 4294967295 4294967295"
|
"unsigned wins either way: 4294967295 4294967295"
|
||||||
"toplevel: 1 2.5 hi 12"
|
"toplevel: 1 2.5 hi 12"
|
||||||
"closure in place: 5"
|
"closure in place: 5"
|
||||||
"a type is not a call: 7")
|
"a type is not a call: 7"
|
||||||
|
"radix literals: 224 5 128"
|
||||||
|
"a double reaches C whole: 1")
|
||||||
(return 0)
|
(return 0)
|
||||||
|
|
||||||
;;; The walk types an expression by its head, and the heads it had a
|
;;; The walk types an expression by its head, and the heads it had a
|
||||||
@@ -61,10 +63,10 @@
|
|||||||
(printf "increment: %d\n" inc)
|
(printf "increment: %d\n" inc)
|
||||||
|
|
||||||
;; an array operand decays, so this is a pointer and not an array
|
;; an array operand decays, so this is a pointer and not an array
|
||||||
(var xs (¤ int 4) #(1 2 3 4))
|
(var xs [int 4] #(1 2 3 4))
|
||||||
(var p _ (+ xs 1))
|
(var p _ (+ xs 1))
|
||||||
(var q _ (+ 1 xs))
|
(var q _ (+ 1 xs))
|
||||||
(var names (¤ (* const char) 2) #("hi" "there"))
|
(var names [(* const char) 2] #("hi" "there"))
|
||||||
(var np _ (+ names 1))
|
(var np _ (+ names 1))
|
||||||
(printf "decayed: %d %d %s\n" (* p) (* q) (* np))
|
(printf "decayed: %d %d %s\n" (* p) (* q) (* np))
|
||||||
|
|
||||||
@@ -87,4 +89,12 @@
|
|||||||
(var f (closure ((int)) int) (make-adder 1))
|
(var f (closure ((int)) int) (make-adder 1))
|
||||||
(var fp (fn ((f int) (g int)) int) area)
|
(var fp (fn ((f int) (g int)) int) area)
|
||||||
(printf "a type is not a call: %d\n" (f 6))
|
(printf "a type is not a call: %d\n" (f 6))
|
||||||
|
|
||||||
|
;; a mask reads best in the radix it is a mask in
|
||||||
|
(var lead int 0xE5)
|
||||||
|
(printf "radix literals: %d %d %d\n" (& lead 0xE0) (& lead 0b111) #x80)
|
||||||
|
|
||||||
|
;; ...and a double in all the digits it was written with
|
||||||
|
(var sixth double (/ 1.0 6.0))
|
||||||
|
(printf "a double reaches C whole: %d\n" (== 0.16666666666666666 sixth))
|
||||||
(return 0))
|
(return 0))
|
||||||
|
|||||||
@@ -2,7 +2,8 @@
|
|||||||
(input)
|
(input)
|
||||||
(output "5.0 5.0 10.0 14.0 14.0 6.0"
|
(output "5.0 5.0 10.0 14.0 14.0 6.0"
|
||||||
"struct receiver: 14"
|
"struct receiver: 14"
|
||||||
"private: 8.0")
|
"private: 8.0"
|
||||||
|
"forward call: 28.0")
|
||||||
(return 0)
|
(return 0)
|
||||||
|
|
||||||
;;; An ops block is mangled fns plus a registry entry, so a method is
|
;;; An ops block is mangled fns plus a registry entry, so a method is
|
||||||
@@ -22,6 +23,10 @@
|
|||||||
(record vec2 ((x float) (y float)))
|
(record vec2 ((x float) (y float)))
|
||||||
|
|
||||||
(ops vec2
|
(ops vec2
|
||||||
|
;; a method may call one written below it
|
||||||
|
(fn twice-sum ((self vec2)) float
|
||||||
|
(return (* 2.0 (.sum-of self))))
|
||||||
|
|
||||||
(pub fn length ((self (* const vec2))) float
|
(pub fn length ((self (* const vec2))) float
|
||||||
"Pointer self, so (.length v) has to take the address."
|
"Pointer self, so (.length v) has to take the address."
|
||||||
(return (sqrtf (+ (* (. self x) (. self x))
|
(return (sqrtf (+ (* (. self x) (. self x))
|
||||||
@@ -55,4 +60,5 @@
|
|||||||
|
|
||||||
(var u vec2 #(vec2 : 8.0 0.0))
|
(var u vec2 #(vec2 : 8.0 0.0))
|
||||||
(printf "private: %.1f\n" (vec2/sum-of u))
|
(printf "private: %.1f\n" (vec2/sum-of u))
|
||||||
|
(printf "forward call: %.1f\n" (.twice-sum v))
|
||||||
(return 0))
|
(return 0))
|
||||||
|
|||||||
32
tests/sex-programs/precedence.sex
Normal file
32
tests/sex-programs/precedence.sex
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
(input)
|
||||||
|
(output "conditional operand: 75"
|
||||||
|
"cast as object of .: 7"
|
||||||
|
"cast of bitwise: 1"
|
||||||
|
"cast of conditional: 44"
|
||||||
|
"define body: 6")
|
||||||
|
(return 0)
|
||||||
|
|
||||||
|
;;; A Sex form is parenthesised by construction, and C is not: whatever
|
||||||
|
;;; the writer leaves bare, C regroups by its own precedence. Each line
|
||||||
|
;;; below came out regrouped, and all but the member access still
|
||||||
|
;;; compiled -- computing 4, 257, 300, and 4 from `2 * 1 + 2'.
|
||||||
|
|
||||||
|
(include stdio.h)
|
||||||
|
|
||||||
|
(record wanted ((distance int)))
|
||||||
|
|
||||||
|
(define SUM (+ 1 2))
|
||||||
|
|
||||||
|
(pub fn main () int
|
||||||
|
(var compared int 4)
|
||||||
|
(printf "conditional operand: %d\n" (/ 300 (if (> compared 0) compared 1)))
|
||||||
|
|
||||||
|
(var w wanted #(wanted : 7))
|
||||||
|
(var pa (* const void) (& w))
|
||||||
|
(printf "cast as object of .: %d\n" (. (cast pa (* const wanted)) distance))
|
||||||
|
|
||||||
|
(var x int 1)
|
||||||
|
(printf "cast of bitwise: %d\n" (cast (| x 256) u8))
|
||||||
|
(printf "cast of conditional: %d\n" (cast (if x 300 1) u8))
|
||||||
|
(printf "define body: %d\n" (* 2 SUM))
|
||||||
|
(return 0))
|
||||||
@@ -5,12 +5,15 @@
|
|||||||
"through a pointer: 55"
|
"through a pointer: 55"
|
||||||
"unsized of a typedef: 1 2"
|
"unsized of a typedef: 1 2"
|
||||||
"unsized of a pointer: 5"
|
"unsized of a pointer: 5"
|
||||||
"unnamed parameters: 7 -1 2")
|
"named bound: 2 4 b"
|
||||||
|
"unnamed parameters: 7 -1 2"
|
||||||
|
"typedefs: hi 4 2"
|
||||||
|
"sizeof a pointer type: 1 1")
|
||||||
(return 0)
|
(return 0)
|
||||||
|
|
||||||
;;; Three questions about a written type that used to be answered in
|
;;; Three questions about a written type that used to be answered in
|
||||||
;;; three places and disagreed: is `(a b)' a named parameter or a bare
|
;;; three places and disagreed: is `(a b)' a named parameter or a bare
|
||||||
;;; type, is the last element of a `¤' its bound or the last word of
|
;;; type, is the last element of a `[...]' its bound or the last word of
|
||||||
;;; its element type, and what is one element of an array.
|
;;; its element type, and what is one element of an array.
|
||||||
;;;
|
;;;
|
||||||
;;; They are one question -- where does the type end -- so the answer
|
;;; They are one question -- where does the type end -- so the answer
|
||||||
@@ -21,6 +24,11 @@
|
|||||||
(struct point ((x int) (y int)))
|
(struct point ((x int) (y int)))
|
||||||
|
|
||||||
(typedef small int)
|
(typedef small int)
|
||||||
|
(typedef cstr (* const char))
|
||||||
|
(typedef vec4 [float 4])
|
||||||
|
(typedef unary (fn ((small)) small))
|
||||||
|
|
||||||
|
(define CAP 2)
|
||||||
|
|
||||||
;;; a parameter that names nothing is a type, however many words it
|
;;; a parameter that names nothing is a type, however many words it
|
||||||
;;; takes: `(unsigned int)' is one of them, not a `unsigned' called
|
;;; takes: `(unsigned int)' is one of them, not a `unsigned' called
|
||||||
@@ -37,36 +45,54 @@
|
|||||||
|
|
||||||
(pub fn main () int
|
(pub fn main () int
|
||||||
;; an element keeps every word of its type, tag and all
|
;; an element keeps every word of its type, tag and all
|
||||||
(var pts (¤ (struct point) 2) #(#((struct point) : 1 2)
|
(var pts [(struct point) 2] #(#((struct point) : 1 2)
|
||||||
#((struct point) : 3 4)))
|
#((struct point) : 3 4)))
|
||||||
(var p _ (¤ pts 1))
|
(var p _ [pts 1])
|
||||||
(printf "aggregate element: %d %d\n" (. p x) (. p y))
|
(printf "aggregate element: %d %d\n" (. p x) (. p y))
|
||||||
|
|
||||||
(var names (¤ (* const char) 2) #("hi" "there"))
|
(var names [(* const char) 2] #("hi" "there"))
|
||||||
(var s _ (¤ names 1))
|
(var s _ [names 1])
|
||||||
(printf "pointer element: %s\n" s)
|
(printf "pointer element: %s\n" s)
|
||||||
|
|
||||||
(var nums (¤ unsigned int 3) #(7 8 9))
|
(var nums [unsigned int 3] #(7 8 9))
|
||||||
(var u _ (¤ nums 2))
|
(var u _ [nums 2])
|
||||||
(printf "multi-word element: %u\n" u)
|
(printf "multi-word element: %u\n" u)
|
||||||
|
|
||||||
;; subscripting a pointer answers the same as subscripting an array
|
;; subscripting a pointer answers the same as subscripting an array
|
||||||
(var q (* (struct point)) (& (¤ pts 0)))
|
(var q (* (struct point)) (& [pts 0]))
|
||||||
(var r _ (¤ q 1))
|
(var r _ [q 1])
|
||||||
(printf "through a pointer: %d\n" (+ (. r x) (* 13 (. r y))))
|
(printf "through a pointer: %d\n" (+ (. r x) (* 13 (. r y))))
|
||||||
|
|
||||||
;; the last word of an unsized array's type is not its bound: neither
|
;; the last word of an unsized array's type is not its bound: neither
|
||||||
;; a typedef name nor the target of a `*' can be one
|
;; a typedef name nor the target of a `*' can be one
|
||||||
(var tail (¤ const small) #(1 2))
|
(var tail [const small] #(1 2))
|
||||||
(printf "unsized of a typedef: %d %d\n" (¤ tail 0) (¤ tail 1))
|
(printf "unsized of a typedef: %d %d\n" [tail 0] [tail 1])
|
||||||
|
|
||||||
(var one size-t 5)
|
(var one size-t 5)
|
||||||
(var sizes (¤ * size-t) #((& one)))
|
(var sizes [* size-t] #((& one)))
|
||||||
(var w _ (¤ sizes 0))
|
(var w _ [sizes 0])
|
||||||
(printf "unsized of a pointer: %d\n" (cast (* w) int))
|
(printf "unsized of a pointer: %d\n" (cast (* w) int))
|
||||||
|
|
||||||
|
;; ...but after a whole type, a name is: `size-t' is a typedef only C
|
||||||
|
;; knows of, and `(* const char)' a type grouped into one element
|
||||||
|
(var ws [size-t CAP] #(3 4))
|
||||||
|
(var gs [(* const char) CAP] #("a" "b"))
|
||||||
|
(printf "named bound: %zu %zu %s\n"
|
||||||
|
(/ (sizeof ws) (sizeof [ws 0])) [ws 1] [gs 1])
|
||||||
|
|
||||||
;; the same question in type position: `(fn ((unsigned int)) int)'
|
;; the same question in type position: `(fn ((unsigned int)) int)'
|
||||||
;; takes one parameter, not two
|
;; takes one parameter, not two
|
||||||
(var fp (fn ((unsigned int)) int) width)
|
(var fp (fn ((unsigned int)) int) width)
|
||||||
(printf "unnamed parameters: %d %d %d\n" (fp 7) (sign #\a) (twice 1))
|
(printf "unnamed parameters: %d %d %d\n" (fp 7) (sign #\a) (twice 1))
|
||||||
|
|
||||||
|
;; a typedef's target is every one of those shapes as well
|
||||||
|
(var greeting cstr "hi")
|
||||||
|
(var v vec4 #(1.0 2.0 3.0 4.0))
|
||||||
|
(var op unary twice)
|
||||||
|
(printf "typedefs: %s %zu %d\n" greeting (/ (sizeof v) (sizeof [v 0])) (op 1))
|
||||||
|
|
||||||
|
;; `(* T)' is a type or a dereference, and only what is in scope says
|
||||||
|
(printf "sizeof a pointer type: %d %d\n"
|
||||||
|
(== (sizeof (* const char)) (sizeof greeting))
|
||||||
|
(== (sizeof (* greeting)) 1))
|
||||||
(return 0))
|
(return 0))
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
(input)
|
(input)
|
||||||
(output "café 日本語 🍺" "café1" "naïve: 3")
|
(output "café 日本語 🍺" "café1" "naïve: 3" "predicate: 1 0")
|
||||||
(return 0)
|
(return 0)
|
||||||
|
|
||||||
;;; Non-ASCII string literals and identifiers.
|
;;; Non-ASCII string literals and identifiers.
|
||||||
@@ -13,12 +13,22 @@
|
|||||||
;;; non-ASCII characters must survive unkebabify intact -- getting this
|
;;; non-ASCII characters must survive unkebabify intact -- getting this
|
||||||
;;; wrong truncates the name silently, and the program still compiles
|
;;; wrong truncates the name silently, and the program still compiles
|
||||||
;;; and runs, just under a different name than the one written.
|
;;; and runs, just under a different name than the one written.
|
||||||
|
;;;
|
||||||
|
;;; `?' and `!' end Lisp predicates and mutators, and C takes neither:
|
||||||
|
;;; they become the letters ʔ and ꜝ.
|
||||||
|
|
||||||
(include stdio.h)
|
(include stdio.h)
|
||||||
|
|
||||||
|
(fn zero? ((n int)) bool (return (== n 0)))
|
||||||
|
(fn clear! ((n (* int))) void (= (* n) 0))
|
||||||
|
|
||||||
(pub fn main () int
|
(pub fn main () int
|
||||||
(puts "café 日本語 🍺")
|
(puts "café 日本語 🍺")
|
||||||
(puts "café1")
|
(puts "café1")
|
||||||
(var naïve-count int 3)
|
(var naïve-count int 3)
|
||||||
(printf "naïve: %d\n" naïve-count)
|
(printf "naïve: %d\n" naïve-count)
|
||||||
|
(var n int 5)
|
||||||
|
(var before bool (zero? n))
|
||||||
|
(clear! (& n))
|
||||||
|
(printf "predicate: %d %d\n" (zero? n) before)
|
||||||
(return 0))
|
(return 0))
|
||||||
|
|||||||
@@ -25,8 +25,8 @@
|
|||||||
(fn origin-x ((p (* (struct point)))) int
|
(fn origin-x ((p (* (struct point)))) int
|
||||||
(return (. (* p) x)))
|
(return (. (* p) x)))
|
||||||
|
|
||||||
(fn second-of ((xs (¤ float 4))) float
|
(fn second-of ((xs [float 4])) float
|
||||||
(return (¤ xs 1)))
|
(return [xs 1]))
|
||||||
|
|
||||||
(fn say ((fmt (* const char)) ...) void
|
(fn say ((fmt (* const char)) ...) void
|
||||||
(var ap va-list)
|
(var ap va-list)
|
||||||
@@ -43,8 +43,8 @@
|
|||||||
(var f (fn ((* (struct point))) int) origin-x)
|
(var f (fn ((* (struct point))) int) origin-x)
|
||||||
(printf "aggregate: %d\n" (f (& p)))
|
(printf "aggregate: %d\n" (f (& p)))
|
||||||
|
|
||||||
(var xs (¤ float 4) #(1.5 2.5 3.5 4.5))
|
(var xs [float 4] #(1.5 2.5 3.5 4.5))
|
||||||
(var g (fn ((¤ float 4)) float) second-of)
|
(var g (fn ([float 4]) float) second-of)
|
||||||
(printf "array: %g\n" (g xs))
|
(printf "array: %g\n" (g xs))
|
||||||
|
|
||||||
(say "variadic: %d %s\n" 1 "two")
|
(say "variadic: %d %s\n" 1 "two")
|
||||||
|
|||||||
@@ -7,6 +7,7 @@
|
|||||||
"loop: 0 1 2"
|
"loop: 0 1 2"
|
||||||
"shadowed: 9 then 42"
|
"shadowed: 9 then 42"
|
||||||
"still an int: 200"
|
"still an int: 200"
|
||||||
|
"case scope: 200"
|
||||||
"partial: 1 2.5"
|
"partial: 1 2.5"
|
||||||
"joined: 43.5 84 49 1"
|
"joined: 43.5 84 49 1"
|
||||||
"promoted: 200 60000 3705032704"
|
"promoted: 200 60000 3705032704"
|
||||||
@@ -44,11 +45,11 @@
|
|||||||
(var pp _ (& p))
|
(var pp _ (& p))
|
||||||
(printf "pointers: %d %g\n" (. pp x) (. pp y))
|
(printf "pointers: %d %g\n" (. pp x) (. pp y))
|
||||||
|
|
||||||
(var table (¤ int 3))
|
(var table [int 3])
|
||||||
(= (¤ table 0) 10)
|
(= [table 0] 10)
|
||||||
(= (¤ table 1) 20)
|
(= [table 1] 20)
|
||||||
(var first _ (¤ table 0))
|
(var first _ [table 0])
|
||||||
(var second _ (¤ table 1))
|
(var second _ [table 1])
|
||||||
(printf "arrays: %d\n" (+ first second))
|
(printf "arrays: %d\n" (+ first second))
|
||||||
|
|
||||||
;; a for opens a scope, and its initializer is declared inside it
|
;; a for opens a scope, and its initializer is declared inside it
|
||||||
@@ -67,14 +68,18 @@
|
|||||||
(var wide int 200)
|
(var wide int 200)
|
||||||
(while false (var wide char 1) (printf "%d" wide))
|
(while false (var wide char 1) (printf "%d" wide))
|
||||||
(if false (do (var wide char 1) (printf "%d" wide)))
|
(if false (do (var wide char 1) (printf "%d" wide)))
|
||||||
;; a statement before the declaration: a label may not be followed by
|
(switch a (case 1 (var wide char 1) (printf "%d" wide)))
|
||||||
;; one until C23
|
|
||||||
(switch a (case 1 (printf "") (var wide char 1) (printf "%d" wide) (break)))
|
|
||||||
;; a copy, not a sum: an arithmetic result would be promoted to `int'
|
;; a copy, not a sum: an arithmetic result would be promoted to `int'
|
||||||
;; whatever leaked, and say nothing
|
;; whatever leaked, and say nothing
|
||||||
(var copy _ wide)
|
(var copy _ wide)
|
||||||
(printf "still an int: %d\n" copy)
|
(printf "still an int: %d\n" copy)
|
||||||
|
|
||||||
|
;; each case is a block of its own: the second sees the outer `wide',
|
||||||
|
;; not the first one's
|
||||||
|
(switch 2
|
||||||
|
(case 1 (var wide double 1.5) (printf "%g" wide))
|
||||||
|
(case 2 (var seen _ wide) (printf "case scope: %d\n" seen)))
|
||||||
|
|
||||||
;; a wildcard inside a written type: only it is solved
|
;; a wildcard inside a written type: only it is solved
|
||||||
(var pp2 (* _) (& p))
|
(var pp2 (* _) (& p))
|
||||||
(printf "partial: %d %g\n" (. pp2 x) (. pp2 y))
|
(printf "partial: %d %g\n" (. pp2 x) (. pp2 y))
|
||||||
@@ -103,7 +108,7 @@
|
|||||||
;; a brace initializer has no type of its own, but its elements solve
|
;; a brace initializer has no type of its own, but its elements solve
|
||||||
;; the hole in the array type around it -- and the length stays as
|
;; the hole in the array type around it -- and the length stays as
|
||||||
;; written, whether or not every slot is initialized
|
;; written, whether or not every slot is initialized
|
||||||
(var squares (¤ _ 4) #(0 1 4 9))
|
(var squares [_ 4] #(0 1 4 9))
|
||||||
(var sparse (¤ _ 8) #(0 1))
|
(var sparse [_ 8] #(0 1))
|
||||||
(printf "from elements: %d %d %d\n" (¤ squares 2) (¤ squares 3) (¤ sparse 7))
|
(printf "from elements: %d %d %d\n" [squares 2] [squares 3] [sparse 7])
|
||||||
(return 0))
|
(return 0))
|
||||||
|
|||||||
@@ -1,3 +0,0 @@
|
|||||||
(module sexc
|
|
||||||
*
|
|
||||||
"../sexc.scm")
|
|
||||||
@@ -1,48 +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?
|
|
||||||
typedef-name?
|
|
||||||
array-bound?
|
|
||||||
array-element-type)
|
|
||||||
"../types.scm")
|
|
||||||
@@ -139,6 +139,10 @@
|
|||||||
(test "enum values are fields whose type is the enum"
|
(test "enum values are fields whose type is the enum"
|
||||||
'((red (enum t-color)) (green (enum t-color)) (blue (enum t-color)))
|
'((red (enum t-color)) (green (enum t-color)) (blue (enum t-color)))
|
||||||
(map-fields 't-color (lambda (name type) (list name type))))
|
(map-fields 't-color (lambda (name type) (list name type))))
|
||||||
|
(add-enum 't-wire '(enum t-wire ((t-varint 0) (t-fixed64 1) t-bytes)))
|
||||||
|
(test "an enumerator with a value is still named by its name"
|
||||||
|
'(t-varint t-fixed64 t-bytes)
|
||||||
|
(map-fields 't-wire (lambda (name type) name)))
|
||||||
(test "a typedef has no fields to map"
|
(test "a typedef has no fields to map"
|
||||||
#f
|
#f
|
||||||
(map-fields 't-u8 (lambda (name type) name)))
|
(map-fields 't-u8 (lambda (name type) name)))
|
||||||
@@ -217,4 +221,24 @@
|
|||||||
(get-name-type 't-never-declared))
|
(get-name-type 't-never-declared))
|
||||||
(test "nor a return type"
|
(test "nor a return type"
|
||||||
#f
|
#f
|
||||||
(get-return-type 't-never-declared)))
|
(get-return-type 't-never-declared))
|
||||||
|
|
||||||
|
;; Where an array's element type ends is a matter of shape, not of
|
||||||
|
;; what the database happens to hold: a typedef from a C header is
|
||||||
|
;; never in it
|
||||||
|
(test-group "array bounds"
|
||||||
|
(test-assert "a number" (array-bound? '(¤ int 4)))
|
||||||
|
(test-assert "a name after a specifier" (array-bound? '(¤ int N)))
|
||||||
|
(test-assert "a name after a type name unknown to Sex"
|
||||||
|
(array-bound? '(¤ pthread-t N)))
|
||||||
|
(test-assert "a name after a grouped type"
|
||||||
|
(array-bound? '(¤ (* t-point) N)))
|
||||||
|
(test-assert "a name after a tag" (array-bound? '(¤ struct t-point N)))
|
||||||
|
(test-assert "not a lone type" (not (array-bound? '(¤ pthread-t))))
|
||||||
|
(test-assert "not a specifier" (not (array-bound? '(¤ unsigned int))))
|
||||||
|
(test-assert "not a qualified name" (not (array-bound? '(¤ const t))))
|
||||||
|
(test-assert "not a pointer's target" (not (array-bound? '(¤ * size-t))))
|
||||||
|
(test-assert "not a tag" (not (array-bound? '(¤ struct t-point))))
|
||||||
|
(test "the element of a grouped type is the group"
|
||||||
|
'(* t-point)
|
||||||
|
(array-element-type '(¤ (* t-point) N)))))
|
||||||
|
|||||||
@@ -1,3 +0,0 @@
|
|||||||
(module utils
|
|
||||||
*
|
|
||||||
"../utils.scm")
|
|
||||||
130
tools/module-deps.scm
Normal file
130
tools/module-deps.scm
Normal 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))
|
||||||
@@ -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
|
|
||||||
@@ -1,6 +0,0 @@
|
|||||||
(module reader (read-from-file
|
|
||||||
read-raw-forms
|
|
||||||
|
|
||||||
current-features
|
|
||||||
platform-features)
|
|
||||||
"../../reader.scm")
|
|
||||||
@@ -6,6 +6,7 @@
|
|||||||
(chicken io)
|
(chicken io)
|
||||||
(chicken pathname)
|
(chicken pathname)
|
||||||
(chicken port)
|
(chicken port)
|
||||||
|
(chicken flonum) ; flonum-print-precision
|
||||||
(chicken process)
|
(chicken process)
|
||||||
(chicken process-context)
|
(chicken process-context)
|
||||||
(chicken string) ; string-split
|
(chicken string) ; string-split
|
||||||
@@ -93,6 +94,9 @@
|
|||||||
(let* ((proc (process compiler (append (list "-o" compiled-file) flags)))
|
(let* ((proc (process compiler (append (list "-o" compiled-file) flags)))
|
||||||
(sexc-stdin (process-input-port proc)))
|
(sexc-stdin (process-input-port proc)))
|
||||||
(symbol-escape #f)
|
(symbol-escape #f)
|
||||||
|
;; ...nor any double in the 15 digits CHICKEN prints by default,
|
||||||
|
;; which make 0.16666666666666666 another number
|
||||||
|
(flonum-print-precision 17)
|
||||||
(with-output-to-port sexc-stdin
|
(with-output-to-port sexc-stdin
|
||||||
(fn (map (fn (fmt #t x)) src)))
|
(fn (map (fn (fmt #t x)) src)))
|
||||||
(close-output-port sexc-stdin)
|
(close-output-port sexc-stdin)
|
||||||
|
|||||||
@@ -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")
|
|
||||||
@@ -1,48 +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?
|
|
||||||
typedef-name?
|
|
||||||
array-bound?
|
|
||||||
array-element-type)
|
|
||||||
"types.scm")
|
|
||||||
109
types.scm
109
types.scm
@@ -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
|
||||||
@@ -334,7 +385,8 @@
|
|||||||
(else (equal? pattern type))))
|
(else (equal? pattern type))))
|
||||||
|
|
||||||
;;; Map function to each field/value of a structure/union/enum
|
;;; Map function to each field/value of a structure/union/enum
|
||||||
;;; For enums, field-type is the type of the enum (since C 23)
|
;;; For enums, field-type is the type of the enum (since C 23), and
|
||||||
|
;;; field-name the enumerator's name, (wire-varint 0) or not
|
||||||
;;; (map-fields type-name
|
;;; (map-fields type-name
|
||||||
;;; (lambda (field-name field-type) ...))
|
;;; (lambda (field-name field-type) ...))
|
||||||
;;;
|
;;;
|
||||||
@@ -350,7 +402,8 @@
|
|||||||
;; declared, since `enum some-typedef' is not a C type.
|
;; declared, since `enum some-typedef' is not a C type.
|
||||||
((enum)
|
((enum)
|
||||||
(let ((type (list 'enum (cadr info))))
|
(let ((type (list 'enum (cadr info))))
|
||||||
(map (lambda (value) (fn value type))
|
(map (lambda (value)
|
||||||
|
(fn (if (pair? value) (car value) value) type))
|
||||||
(caddr info))))
|
(caddr info))))
|
||||||
(else #f)))))
|
(else #f)))))
|
||||||
|
|
||||||
@@ -387,22 +440,34 @@
|
|||||||
(pair? (cdr arg)) ; 1 element args are always type
|
(pair? (cdr arg)) ; 1 element args are always type
|
||||||
(not (type-head? arg))))
|
(not (type-head? arg))))
|
||||||
|
|
||||||
;;; A typedef and a `define' share +type-db+; only the typedef is part
|
;;; `(p (* struct point))' writes the type in one piece and
|
||||||
;;; of a type:
|
;;; `(p * struct point)' spreads it over the rest; both are
|
||||||
;;;
|
;;; `(* struct point)'
|
||||||
;;; (typedef small int) -> (¤ small N) is N of small
|
(define (named-arg-type param)
|
||||||
;;; (define CAP 4) -> (¤ int CAP) is CAP of int
|
(if (null? (cddr param))
|
||||||
(define (typedef-name? name)
|
(second param)
|
||||||
(let ((info (and (symbol? name) (get-type-info name))))
|
(cdr param)))
|
||||||
(and info (memq (car info) '(typedef struct union enum)) #t)))
|
|
||||||
|
|
||||||
;;; The last element is a bound only where what precedes it already
|
;;; (fn sum ((a int) (b int)) int ...) -> (fn ((int) (int)) int), and
|
||||||
;;; spells a whole type -- a specifier, a tag after its keyword, or a
|
;;; (self * const v) is ((* const v)) just as (self (* const v)) is.
|
||||||
;;; typedef we have seen declared:
|
;;; Anything that names nothing -- a variadic tail -- goes through
|
||||||
|
;;; untouched.
|
||||||
|
(define (arglist-types arglist)
|
||||||
|
(map (lambda (param)
|
||||||
|
(if (named-arg? param)
|
||||||
|
(list (named-arg-type param))
|
||||||
|
param))
|
||||||
|
arglist))
|
||||||
|
|
||||||
|
;;; The last element is a bound unless what precedes it is still
|
||||||
|
;;; waiting for a type name -- a `*', a tag's keyword, a qualifier with
|
||||||
|
;;; no type before it. C spells a type with one name at most, so a name
|
||||||
|
;;; after a name, or after a grouped type, can only be the bound:
|
||||||
;;;
|
;;;
|
||||||
;;; (¤ int 4) four of int (¤ unsigned int) unsized
|
;;; (¤ int 4) four of int (¤ unsigned int) unsized
|
||||||
;;; (¤ mytype CAP) CAP of mytype (¤ struct point) unsized
|
;;; (¤ pthread-t CAP) CAP of pthread-t (¤ struct point) unsized
|
||||||
;;; (¤ const mytype) unsized (¤ * size-t) unsized
|
;;; (¤ (* T) CAP) CAP of (* T) (¤ const mytype) unsized
|
||||||
|
;;; (¤ int const N) N of const int (¤ * size-t) unsized
|
||||||
;;;
|
;;;
|
||||||
;;; TYPE is the whole `(¤ ...)' form.
|
;;; TYPE is the whole `(¤ ...)' form.
|
||||||
(define (array-bound? type)
|
(define (array-bound? type)
|
||||||
@@ -412,11 +477,13 @@
|
|||||||
(cond
|
(cond
|
||||||
((not (symbol? bound)) #t)
|
((not (symbol? bound)) #t)
|
||||||
((or (memq bound +c-specifiers+) (memq bound +c-qualifiers+)) #f)
|
((or (memq bound +c-specifiers+) (memq bound +c-qualifiers+)) #f)
|
||||||
((memq preceding +c-specifiers+) #t)
|
((memq preceding '(* struct union enum)) #f)
|
||||||
;; a tag always follows its keyword, so `(¤ * struct tt)' ends
|
((memq preceding +c-qualifiers+)
|
||||||
;; in a name belonging to the type
|
(any (lambda (word)
|
||||||
((memq preceding '(struct union enum)) #f)
|
(not (or (memq word '(* struct union enum))
|
||||||
(else (typedef-name? preceding))))))
|
(memq word +c-qualifiers+))))
|
||||||
|
(drop-right (cdr type) 2)))
|
||||||
|
(else #t)))))
|
||||||
|
|
||||||
;;; What one element of a written array type is:
|
;;; What one element of a written array type is:
|
||||||
;;; (¤ struct point 2) -> (struct point), (¤ * const char 2) -> (* const char)
|
;;; (¤ struct point 2) -> (struct point), (¤ * const char 2) -> (* const char)
|
||||||
|
|||||||
@@ -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")
|
|
||||||
25
utils.scm
25
utils.scm
@@ -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)
|
||||||
|
|||||||
Reference in New Issue
Block a user