Compare commits
94 Commits
dcf02dd646
...
more-tests
| Author | SHA1 | Date | |
|---|---|---|---|
| 724f4dd3e9 | |||
| d890080103 | |||
| a2bbb73524 | |||
| 28aaaf637c | |||
| acbd4928f5 | |||
| 7973a4331d | |||
| a53c358113 | |||
| 8c92c245c5 | |||
| af8e49cdd3 | |||
| 9505b4f0a0 | |||
| 8d6d1855e2 | |||
| f66eaa7454 | |||
| 2fa1ed21d7 | |||
| 1dd1cf0ae9 | |||
| 7c897b6dc6 | |||
| b285174a22 | |||
| 4bdc0e57a7 | |||
| 2024d4d0f4 | |||
| 1bdeda6cc3 | |||
| 6ad8f8f56b | |||
| fb26b92fdc | |||
| 7649e47297 | |||
| 389e73323f | |||
| 83282adfb9 | |||
| 5afe32a888 | |||
| 8220c500df | |||
| dd5174f472 | |||
| 8e3e6fcdd1 | |||
| 4fa8a021de | |||
| e0ecde6517 | |||
| 0a0a3f6b25 | |||
| 3436c4ff3e | |||
| cb37821e3e | |||
| 94f41b5fad | |||
| b3d7decc6b | |||
| d9bc8967ba | |||
| 3d295f4cd0 | |||
| 20311a200d | |||
| 10de092ab0 | |||
| befe2ca25b | |||
| 8cb141de5d | |||
| c0ed7ad916 | |||
| ffa5e9b02c | |||
| 06bbe4b97d | |||
| 1b59848b42 | |||
| 32515c0eb8 | |||
| 42c76d07e6 | |||
| 04c3267bfe | |||
| c7990e6613 | |||
| c8a39b7e62 | |||
| 6e4ad81538 | |||
| 192ed2ba64 | |||
| 41945606f6 | |||
| ba08695355 | |||
| a4ab04995d | |||
| 9bbf2dcdaf | |||
| f30c0733e3 | |||
| ad1825e8a8 | |||
| 47b98e41df | |||
| 82fa8d1966 | |||
| 7c62e65eb0 | |||
| e9ee120383 | |||
| 9d5cd66514 | |||
| 65aaf69835 | |||
| 6b5a712837 | |||
| 4463f796f8 | |||
| 8ddeac7bab | |||
| c05904c15e | |||
| 27ef5ac805 | |||
| 0cb3956e02 | |||
| 0852079827 | |||
| 5cd931cb4e | |||
| 6d84553652 | |||
| 00efbc5442 | |||
| 635cc48aa3 | |||
| d2f36d35c6 | |||
| eb5bf3688b | |||
| bc4f9246c2 | |||
| bf8ba2f9fd | |||
| 13d20a102d | |||
| 600b4801de | |||
| ab881e4c31 | |||
| 6198c51e88 | |||
| 4758802810 | |||
| 8488a58be1 | |||
| 0628d1c212 | |||
| 49b463ac7b | |||
| 67749c8db0 | |||
| faa9036dcc | |||
| c33bfd704d | |||
| 397766822d | |||
| a504fc1410 | |||
| c65721a070 | |||
| dcb65c5504 |
9
.gitignore
vendored
9
.gitignore
vendored
@@ -5,10 +5,11 @@
|
|||||||
*.o
|
*.o
|
||||||
*.import.scm
|
*.import.scm
|
||||||
*.link
|
*.link
|
||||||
modules.mk
|
/modules.mk
|
||||||
sexc
|
/modules.stamp
|
||||||
sex-tests
|
/sexc
|
||||||
sextest
|
/sex-tests
|
||||||
|
/sextest
|
||||||
|
|
||||||
# Scrapped design docs, kept for reference
|
# Scrapped design docs, kept for reference
|
||||||
/attic
|
/attic
|
||||||
|
|||||||
57
Makefile
57
Makefile
@@ -1,5 +1,6 @@
|
|||||||
CHICKEN_C ?= csc
|
CHICKEN_C ?= csc
|
||||||
CSI ?= csi
|
# beside csc, whatever it is called: chicken-csc comes with chicken-csi
|
||||||
|
CSI ?= $(patsubst %csc,%csi,$(CHICKEN_C))
|
||||||
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.
|
||||||
@@ -38,28 +39,39 @@ include eggs.mk
|
|||||||
|
|
||||||
# Each program, and the source it is built from
|
# Each program, and the source it is built from
|
||||||
PROGRAMS = sexc=main.scm sextest=tools/sextest/sextest.scm sex-tests=tests/run.scm
|
PROGRAMS = sexc=main.scm sextest=tools/sextest/sextest.scm sex-tests=tests/run.scm
|
||||||
|
PROGRAM_NAMES = $(foreach p,$(PROGRAMS),$(firstword $(subst =, ,$(p))))
|
||||||
|
PROGRAM_OBJ = $(foreach p,$(PROGRAMS),$(patsubst %.scm,%.o,$(lastword $(subst =, ,$(p)))))
|
||||||
|
|
||||||
# MODULES, the programs' names and objects, and a line for each naming
|
# MODULES, and a line for each module and program naming what it
|
||||||
# what it imports, read off the import forms
|
# imports, read off the import forms. Not for clean, which has to work
|
||||||
|
# when they cannot be read
|
||||||
|
ifneq ($(MAKECMDGOALS),clean)
|
||||||
include modules.mk
|
include modules.mk
|
||||||
OBJ = $(MODULES:%=%.o)
|
endif
|
||||||
|
|
||||||
modules.mk: $(SOURCES) tools/module-deps.scm dependencies.txt
|
# The scan is redone whenever a source may have changed what it
|
||||||
$(CSI) -s tools/module-deps.scm $(PROGRAMS) > $@.tmp
|
# imports, or a NAME.scm may have come to be the module an import
|
||||||
mv $@.tmp $@
|
# names; but modules.mk is rewritten only when that changed, since make
|
||||||
|
# starts over each time it is, and a source dated in the future would
|
||||||
|
# have it start over for good. A stamp without modules.mk stands for
|
||||||
|
# nothing, and is redone whatever its date
|
||||||
|
modules.mk: modules.stamp ;
|
||||||
|
|
||||||
|
modules.stamp: $(SOURCES) $(filter-out %.import.scm,$(wildcard *.scm)) \
|
||||||
|
tools/module-deps.scm dependencies.txt Makefile \
|
||||||
|
$(if $(wildcard modules.mk),,FORCE)
|
||||||
|
$(CSI) -s tools/module-deps.scm $(PROGRAMS) > modules.mk.tmp \
|
||||||
|
|| { rm -f modules.mk.tmp; exit 1; }
|
||||||
|
if cmp -s modules.mk.tmp modules.mk; then rm modules.mk.tmp; \
|
||||||
|
else mv modules.mk.tmp modules.mk; fi
|
||||||
|
touch $@
|
||||||
|
|
||||||
#------------------------------------------------------------------
|
#------------------------------------------------------------------
|
||||||
|
|
||||||
# Everything is linked with the modules among its prerequisites
|
|
||||||
LINKS = $(addprefix -link ,$(basename $(filter-out $<,$(filter %.o,$^))))
|
|
||||||
|
|
||||||
# A module is one file that declares its own exports, made a module by
|
# A module is one file that declares its own exports, made a module by
|
||||||
# -module, except the vendored sex-fmt-c, a (module ...) form of its own
|
# -module, except the vendored sex-fmt-c, a (module ...) form of its own
|
||||||
MODULE = -module $*
|
|
||||||
sex-fmt-c.o: MODULE =
|
|
||||||
|
|
||||||
%.o: %.scm
|
%.o: %.scm
|
||||||
$(CHICKEN_C) $(CSC_FLAGS) $(MODULE_FLAGS) $< $(MODULE) -o $@ -unit $* $(LINKS)
|
$(CHICKEN_C) $(CSC_FLAGS) $(MODULE_FLAGS) $< $(if $(filter sex-fmt-c,$*),,-module $*) -o $@ -unit $*
|
||||||
|
|
||||||
# A program's object is named after its source: csc names its own after
|
# A program's object is named after its source: csc names its own after
|
||||||
# -o, and sexc.o is the module's
|
# -o, and sexc.o is the module's
|
||||||
@@ -67,7 +79,7 @@ $(PROGRAM_OBJ): %.o: %.scm
|
|||||||
$(CHICKEN_C) $(CSC_FLAGS) -c $< -o $@
|
$(CHICKEN_C) $(CSC_FLAGS) -c $< -o $@
|
||||||
|
|
||||||
$(PROGRAM_NAMES):
|
$(PROGRAM_NAMES):
|
||||||
$(CHICKEN_C) $(CSC_FLAGS) $< -o $@ $(LINKS)
|
$(CHICKEN_C) $(CSC_FLAGS) $< -o $@
|
||||||
|
|
||||||
# A program directly in tests/sex-programs is a test; lib/ holds what
|
# A program directly in tests/sex-programs is a test; lib/ holds what
|
||||||
# they import
|
# they import
|
||||||
@@ -76,8 +88,13 @@ SEX_TEST_PROGRAMS = $(wildcard tests/sex-programs/*.sex)
|
|||||||
check run-tests: sexc sex-tests sextest
|
check run-tests: sexc sex-tests sextest
|
||||||
./sex-tests
|
./sex-tests
|
||||||
./sextest --sexc=./sexc $(SEX_TEST_PROGRAMS)
|
./sextest --sexc=./sexc $(SEX_TEST_PROGRAMS)
|
||||||
cd tests/modules && SEXC=$(CURDIR)/sexc $(CSI) -s check.scm
|
@$(MAKE) --no-print-directory check-modules
|
||||||
cd tests/exit-code && SEXC=$(CURDIR)/sexc $(CSI) -s check.scm
|
@$(MAKE) --no-print-directory check-exit-code
|
||||||
|
@$(MAKE) --no-print-directory check-command-line
|
||||||
|
|
||||||
|
# The end-to-end checks, each on its own: run with the eggs on the path
|
||||||
|
check-modules check-exit-code check-command-line: check-%: sexc
|
||||||
|
cd tests/$* && 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
|
||||||
@@ -95,9 +112,9 @@ uninstall:
|
|||||||
-rmdir $(DESTDIR)$(sexlibdir)
|
-rmdir $(DESTDIR)$(sexlibdir)
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm -f $(OBJ) $(PROGRAM_OBJ) modules.mk
|
rm -f *.o $(PROGRAM_OBJ) modules.mk modules.stamp
|
||||||
rm -f *.import.scm
|
rm -f *.import.scm
|
||||||
rm -f *.link $(PROGRAM_OBJ:.o=.link)
|
rm -f *.link $(PROGRAM_OBJ:.o=.link)
|
||||||
rm -f sexc sex-tests sextest
|
rm -f $(PROGRAM_NAMES)
|
||||||
|
|
||||||
.PHONY: all check run-tests install install-strip installdirs uninstall clean
|
.PHONY: all check run-tests check-modules check-exit-code check-command-line install install-strip installdirs uninstall clean FORCE
|
||||||
|
|||||||
@@ -20,11 +20,7 @@
|
|||||||
|
|
||||||
(export
|
(export
|
||||||
emit-c
|
emit-c
|
||||||
sex-line-directives
|
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)
|
||||||
@@ -231,14 +227,6 @@ forms, and what remains."
|
|||||||
acc))))))
|
acc))))))
|
||||||
(else (loop (cdr fs) (cons (car fs) acc))))))
|
(else (loop (cdr fs) (cons (car fs) acc))))))
|
||||||
|
|
||||||
(define (walk-generic-toplevel form)
|
|
||||||
(cond ((atom? form) (name-to-fmt-c form))
|
|
||||||
((list? form) (cons (if (symbol? (car form))
|
|
||||||
(atom-to-fmt-c (car form))
|
|
||||||
(walk-generic-toplevel (car form)))
|
|
||||||
(map walk-generic-toplevel (cdr form))))
|
|
||||||
(else (sex-error form "malformed form" form))))
|
|
||||||
|
|
||||||
(define (walk-expr form)
|
(define (walk-expr form)
|
||||||
(match form
|
(match form
|
||||||
((? vector?) (walk-initializer (vector->list form)))
|
((? vector?) (walk-initializer (vector->list form)))
|
||||||
|
|||||||
301
infer.scm
301
infer.scm
@@ -1,8 +1,8 @@
|
|||||||
;;; Type inference, layer 0: the type representation and unification.
|
;;; Type inference, layer 0: the type representation and unification.
|
||||||
;;;
|
;;;
|
||||||
;;; Nothing in the compiler calls this unit yet. It is the ground floor
|
;;; semen solves the wildcards of a declared type with it -- `(var n _
|
||||||
;;; of the pass described in Type-inference.org -- built and tested on
|
;;; (strlen s))', `(var p (* _) (& x))' -- and asks it what an operand
|
||||||
;;; its own before a single form is routed through it.
|
;;; decays or promotes to.
|
||||||
;;;
|
;;;
|
||||||
;;; Two representations meet here. *Surface* types are the forms the
|
;;; Two representations meet here. *Surface* types are the forms the
|
||||||
;;; rest of the compiler passes around -- `int', `(* const char)',
|
;;; rest of the compiler passes around -- `int', `(* const char)',
|
||||||
@@ -28,32 +28,18 @@
|
|||||||
(chicken module)
|
(chicken module)
|
||||||
matchable
|
matchable
|
||||||
srfi-1
|
srfi-1
|
||||||
srfi-69
|
|
||||||
types
|
types
|
||||||
utils)
|
utils)
|
||||||
|
|
||||||
(export
|
(export
|
||||||
;; The IR
|
;; The IR
|
||||||
tvar?
|
tvar?
|
||||||
tvar-id
|
prim-type? prim-name
|
||||||
tvar-classes
|
ptr-type?
|
||||||
tvar-rigid?
|
array-type? array-elt
|
||||||
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
|
resolve
|
||||||
underlying
|
underlying
|
||||||
c-primitive?
|
c-primitive?
|
||||||
type-quals
|
|
||||||
free-tvars
|
|
||||||
decay
|
decay
|
||||||
unqualified
|
unqualified
|
||||||
|
|
||||||
@@ -61,22 +47,7 @@
|
|||||||
parse-type
|
parse-type
|
||||||
unparse-type
|
unparse-type
|
||||||
|
|
||||||
;; Constraints
|
unify)
|
||||||
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
|
||||||
@@ -84,29 +55,19 @@
|
|||||||
|
|
||||||
;;; A type variable is a mutable cell. `ref' is #f while unsolved and
|
;;; A type variable is a mutable cell. `ref' is #f while unsolved and
|
||||||
;;; the type it stands for once bound -- union-find, with the path
|
;;; the type it stands for once bound -- union-find, with the path
|
||||||
;;; compression done in `resolve'.
|
;;; compression done in `resolve'. `quals' are the ones written on it --
|
||||||
;;;
|
|
||||||
;;; `classes' is the list of type classes the variable must satisfy
|
|
||||||
;;; (`numeric', and one day `ord'); see "constraints" below. `rigid?'
|
|
||||||
;;; marks a variable that must not unify with anything but itself --
|
|
||||||
;;; unused until a `fn' grows type parameters, and five lines now
|
|
||||||
;;; against an IR change later. `quals' are the ones written on it --
|
|
||||||
;;; `(const _)' -- and go onto whatever it is bound to.
|
;;; `(const _)' -- and go onto whatever it is bound to.
|
||||||
(define-record-type <tvar>
|
(define-record-type <tvar>
|
||||||
(%make-tvar id ref classes rigid? quals)
|
(%make-tvar ref quals)
|
||||||
tvar?
|
tvar?
|
||||||
(id tvar-id)
|
|
||||||
(ref tvar-ref tvar-ref-set!)
|
(ref tvar-ref tvar-ref-set!)
|
||||||
(classes tvar-classes tvar-classes-set!)
|
|
||||||
(rigid? tvar-rigid?)
|
|
||||||
(quals tvar-quals tvar-quals-set!))
|
(quals tvar-quals tvar-quals-set!))
|
||||||
|
|
||||||
;;; A primitive or otherwise nominal type. `name' is the list of words
|
;;; A primitive or otherwise nominal type. `name' is the list of words
|
||||||
;;; making it up, so `int', `(unsigned int)' and `(long long)' are all
|
;;; making it up, so `int', `(unsigned int)' and `(long long)' are all
|
||||||
;;; one node, and so is a name we have never parsed a declaration for
|
;;; one node, and so is a name we have never parsed a declaration for
|
||||||
;;; (`size-t', `GLuint'). The two cases are told apart by
|
;;; (`size-t', `GLuint'). The two cases are told apart by
|
||||||
;;; `c-primitive?', which is what keeps a constraint over an unparsed C
|
;;; `c-primitive?'.
|
||||||
;;; typedef from being an error.
|
|
||||||
(define-record-type <prim>
|
(define-record-type <prim>
|
||||||
(make-prim name quals)
|
(make-prim name quals)
|
||||||
prim-type?
|
prim-type?
|
||||||
@@ -157,25 +118,8 @@
|
|||||||
(expansion alias-expansion)
|
(expansion alias-expansion)
|
||||||
(quals alias-quals))
|
(quals alias-quals))
|
||||||
|
|
||||||
;;; `?'. Sex has full C interop, so `printf', `SDL-CreateWindow' and
|
(define (fresh-tvar)
|
||||||
;;; `size-t' arrive from headers nobody parsed. Rather than reject
|
(%make-tvar #f (list)))
|
||||||
;;; every real program, the lattice gets a top element: `?' is
|
|
||||||
;;; consistent with every type and constrains nothing.
|
|
||||||
(define-record-type <unknown>
|
|
||||||
(%make-unknown)
|
|
||||||
unknown-type?)
|
|
||||||
|
|
||||||
(define the-unknown-type (%make-unknown))
|
|
||||||
|
|
||||||
(define tvar-counter 0)
|
|
||||||
|
|
||||||
(define (fresh-tvar . classes)
|
|
||||||
(set! tvar-counter (+ tvar-counter 1))
|
|
||||||
(%make-tvar tvar-counter #f (if (null? classes) (list) (car classes)) #f (list)))
|
|
||||||
|
|
||||||
(define (fresh-rigid-tvar . classes)
|
|
||||||
(set! tvar-counter (+ tvar-counter 1))
|
|
||||||
(%make-tvar tvar-counter #f (if (null? classes) (list) (car classes)) #t (list)))
|
|
||||||
|
|
||||||
;;; Follow a bound variable to what it stands for, compressing the path
|
;;; Follow a bound variable to what it stands for, compressing the path
|
||||||
;;; on the way out. Every procedure that looks at a type's shape starts
|
;;; on the way out. Every procedure that looks at a type's shape starts
|
||||||
@@ -221,16 +165,6 @@
|
|||||||
((alias-type? t) (make-alias (alias-name t) (alias-expansion t) (list)))
|
((alias-type? t) (make-alias (alias-name t) (alias-expansion t) (list)))
|
||||||
(else t))))
|
(else t))))
|
||||||
|
|
||||||
(define (free-tvars type)
|
|
||||||
(let collect ((t type) (acc (list)))
|
|
||||||
(let ((t (resolve t)))
|
|
||||||
(cond ((tvar? t) (if (memq t acc) acc (cons t acc)))
|
|
||||||
((ptr-type? t) (collect (ptr-target t) acc))
|
|
||||||
((array-type? t) (collect (array-elt t) acc))
|
|
||||||
((alias-type? t) (collect (alias-expansion t) acc))
|
|
||||||
((fn-type? t) (fold collect (collect (fn-ret t) acc) (fn-args t)))
|
|
||||||
(else acc)))))
|
|
||||||
|
|
||||||
;;; ---------------------------------------------------------------
|
;;; ---------------------------------------------------------------
|
||||||
;;; Surface -> IR
|
;;; Surface -> IR
|
||||||
;;; ---------------------------------------------------------------
|
;;; ---------------------------------------------------------------
|
||||||
@@ -373,7 +307,6 @@
|
|||||||
(let ((t (resolve type)))
|
(let ((t (resolve type)))
|
||||||
(cond
|
(cond
|
||||||
((tvar? t) '_)
|
((tvar? t) '_)
|
||||||
((unknown-type? t) '?)
|
|
||||||
((alias-type? t) (qualify (alias-quals t) (list (alias-name t))))
|
((alias-type? t) (qualify (alias-quals t) (list (alias-name t))))
|
||||||
((prim-type? t) (qualify (prim-quals t) (prim-name t)))
|
((prim-type? t) (qualify (prim-quals t) (prim-name t)))
|
||||||
((agg-type? t) (qualify (agg-quals t) (agg-spelling t)))
|
((agg-type? t) (qualify (agg-quals t) (agg-spelling t)))
|
||||||
@@ -413,69 +346,9 @@
|
|||||||
(else surface)))
|
(else surface)))
|
||||||
|
|
||||||
;;; ---------------------------------------------------------------
|
;;; ---------------------------------------------------------------
|
||||||
;;; Constraints
|
;;; C's own types
|
||||||
;;; ---------------------------------------------------------------
|
;;; ---------------------------------------------------------------
|
||||||
|
|
||||||
;;; `(numeric a)' is already a type class, so it is written as one from
|
|
||||||
;;; the start: one representation, one table, one entailment check. A
|
|
||||||
;;; trait bound `(ord (struct circle))' is the same shape, discharged
|
|
||||||
;;; the same way, and reported by the same procedure -- which is the
|
|
||||||
;;; whole reason to build it this way while there is only one kind of
|
|
||||||
;;; constraint to build.
|
|
||||||
;;;
|
|
||||||
;;; `default' is the type an unresolved constraint falls back to, the
|
|
||||||
;;; way Haskell defaults `Num a' to Integer. `test' is how the built-in
|
|
||||||
;;; classes say "every arithmetic type" without enumerating twenty
|
|
||||||
;;; spellings as instances; a user trait has no test and lives entirely
|
|
||||||
;;; in the instance table. `strict?' marks a class that must not be
|
|
||||||
;;; guessed at: static dispatch needs a real instance, so `?' fails it.
|
|
||||||
(define-record-type <type-class>
|
|
||||||
(%make-type-class name default test strict?)
|
|
||||||
type-class?
|
|
||||||
(name type-class-name)
|
|
||||||
(default type-class-default)
|
|
||||||
(test type-class-test)
|
|
||||||
(strict? type-class-strict?))
|
|
||||||
|
|
||||||
(define +classes+ (make-hash-table))
|
|
||||||
(define +instances+ (make-hash-table))
|
|
||||||
|
|
||||||
(define (register-class! name default test strict?)
|
|
||||||
(hash-table-set! +classes+ name (%make-type-class name default test strict?)))
|
|
||||||
|
|
||||||
(define (get-class name)
|
|
||||||
(or (hash-table-ref/default +classes+ name #f)
|
|
||||||
(error "no such type class" name)))
|
|
||||||
|
|
||||||
;;; Instances key on the *resolved* type, so `(impl show for size-t)'
|
|
||||||
;;; and `(impl show for unsigned long)' collide rather than quietly
|
|
||||||
;;; coexisting as two instances of one C type.
|
|
||||||
(define (instance-key type)
|
|
||||||
(unparse-type (underlying type)))
|
|
||||||
|
|
||||||
(define (add-instance! class-name type)
|
|
||||||
(hash-table-set! +instances+ (cons class-name (instance-key type)) #t))
|
|
||||||
|
|
||||||
(define (has-instance? class-name type)
|
|
||||||
(hash-table-exists? +instances+ (cons class-name (instance-key type))))
|
|
||||||
|
|
||||||
;;; #t, #f, or 'unknown -- and the third answer is the important one.
|
|
||||||
;;; A C name we never parsed a declaration for might well be numeric;
|
|
||||||
;;; saying #f there would reject working programs, and saying #t would
|
|
||||||
;;; invent knowledge. 'unknown means "do not constrain, do not
|
|
||||||
;;; complain".
|
|
||||||
(define (entails? class-name type)
|
|
||||||
(let ((cls (get-class class-name))
|
|
||||||
(t (underlying type)))
|
|
||||||
(cond
|
|
||||||
((tvar? t) 'unknown)
|
|
||||||
;; `?' is consistent with every type, but it entails nothing:
|
|
||||||
;; there is no instance to select and no name to mangle.
|
|
||||||
((unknown-type? t) (if (type-class-strict? cls) #f 'unknown))
|
|
||||||
((has-instance? class-name t) #t)
|
|
||||||
((type-class-test cls) => (lambda (test) (test t)))
|
|
||||||
(else #f))))
|
|
||||||
|
|
||||||
(define +integer-words+ '(char short int long signed unsigned bool _Bool))
|
(define +integer-words+ '(char short int long signed unsigned bool _Bool))
|
||||||
(define +float-words+ '(float double))
|
(define +float-words+ '(float double))
|
||||||
(define +known-words+ (append '(void) +integer-words+ +float-words+))
|
(define +known-words+ (append '(void) +integer-words+ +float-words+))
|
||||||
@@ -486,97 +359,18 @@
|
|||||||
(and (prim-type? t)
|
(and (prim-type? t)
|
||||||
(every (lambda (word) (memq word +known-words+)) (prim-name t))))
|
(every (lambda (word) (memq word +known-words+)) (prim-name t))))
|
||||||
|
|
||||||
(define (void-type? t)
|
|
||||||
(and (prim-type? t) (equal? (prim-name t) '(void))))
|
|
||||||
|
|
||||||
(define (arithmetic-type? t)
|
|
||||||
(cond ((and (agg-type? t) (eq? (agg-kind t) 'enum)) #t) ; an enum is an integer
|
|
||||||
((not (prim-type? t)) #f)
|
|
||||||
((not (c-primitive? t)) 'unknown)
|
|
||||||
((void-type? t) #f)
|
|
||||||
(else #t)))
|
|
||||||
|
|
||||||
(define (integral-type? t)
|
|
||||||
(cond ((and (agg-type? t) (eq? (agg-kind t) 'enum)) #t)
|
|
||||||
((not (prim-type? t)) #f)
|
|
||||||
((not (c-primitive? t)) 'unknown)
|
|
||||||
((void-type? t) #f)
|
|
||||||
((any (lambda (word) (memq word +float-words+)) (prim-name t)) #f)
|
|
||||||
(else #t)))
|
|
||||||
|
|
||||||
(define (floating-type? t)
|
|
||||||
(cond ((not (prim-type? t)) #f)
|
|
||||||
((not (c-primitive? t)) 'unknown)
|
|
||||||
(else (and (any (lambda (word) (memq word +float-words+)) (prim-name t))
|
|
||||||
#t))))
|
|
||||||
|
|
||||||
(define (scalar-type? t)
|
|
||||||
(cond ((ptr-type? t) #t)
|
|
||||||
((array-type? t) #t) ; decays to one
|
|
||||||
((fn-type? t) #t) ; likewise
|
|
||||||
(else (arithmetic-type? t))))
|
|
||||||
|
|
||||||
;;; The built-ins. They are ordinary classes, registered the same way a
|
|
||||||
;;; trait will be -- that is the point.
|
|
||||||
(register-class! 'numeric 'int arithmetic-type? #f)
|
|
||||||
(register-class! 'integral 'int integral-type? #f)
|
|
||||||
(register-class! 'floating 'double floating-type? #f)
|
|
||||||
(register-class! 'scalar #f scalar-type? #f)
|
|
||||||
|
|
||||||
;;; A constraint that survives to the end of a function is defaulted:
|
|
||||||
;;; `(numeric a)' with nothing else known is an `int'. A *strict*
|
|
||||||
;;; class has no default and no business guessing, so an unresolved one
|
|
||||||
;;; is an error -- the rule is worth stating while there is only one
|
|
||||||
;;; kind of constraint to state it about.
|
|
||||||
(define (default-tvar! v form)
|
|
||||||
(let ((strict (find (lambda (c) (type-class-strict? (get-class c)))
|
|
||||||
(tvar-classes v))))
|
|
||||||
(cond
|
|
||||||
(strict (sex-error form "unresolved constraint" (list strict (unparse-type v))))
|
|
||||||
((find (lambda (c) (type-class-default (get-class c))) (tvar-classes v))
|
|
||||||
=> (lambda (c)
|
|
||||||
(tvar-ref-set! v (parse-type (type-class-default (get-class c))))
|
|
||||||
#t))
|
|
||||||
(else #f))))
|
|
||||||
|
|
||||||
;;; Default every variable still open in TYPE. Returns #t when none is
|
|
||||||
;;; left unsolved, so a caller can tell "inferred" from "give up and
|
|
||||||
;;; ask for the type in writing".
|
|
||||||
(define (default-type-variables! type form)
|
|
||||||
(fold (lambda (v ok) (and (default-tvar! v form) ok))
|
|
||||||
#t
|
|
||||||
(free-tvars type)))
|
|
||||||
|
|
||||||
(define (check-classes classes type form)
|
|
||||||
(for-each
|
|
||||||
(lambda (c)
|
|
||||||
(when (eq? #f (entails? c type))
|
|
||||||
(sex-error form "type does not satisfy a constraint"
|
|
||||||
(list c (unparse-type type)))))
|
|
||||||
classes))
|
|
||||||
|
|
||||||
;;; ---------------------------------------------------------------
|
;;; ---------------------------------------------------------------
|
||||||
;;; Unification
|
;;; Unification
|
||||||
;;; ---------------------------------------------------------------
|
;;; ---------------------------------------------------------------
|
||||||
|
|
||||||
;;; Consistency in the gradual-typing sense rather than equality: `?'
|
|
||||||
;;; succeeds against anything and binds nothing, which is what keeps
|
|
||||||
;;; the pass from rejecting every program that includes a C header.
|
|
||||||
;;;
|
|
||||||
;;; FORM is carried only so a failure can say where it was written.
|
;;; FORM is carried only so a failure can say where it was written.
|
||||||
(define (unify t1 t2 form)
|
(define (unify t1 t2 form)
|
||||||
(let ((a (resolve t1))
|
(let ((a (resolve t1))
|
||||||
(b (resolve t2)))
|
(b (resolve t2)))
|
||||||
(cond
|
(cond
|
||||||
((eq? a b) #t)
|
((eq? a b) #t)
|
||||||
((unknown-type? a) #t)
|
((tvar? a) (bind-tvar! a b form))
|
||||||
((unknown-type? b) #t)
|
((tvar? b) (bind-tvar! b a form))
|
||||||
;; Whichever side is free takes the binding: `(unify a r)' and
|
|
||||||
;; `(unify r a)' both leave `a' bound to `r'. Two rigid and
|
|
||||||
;; distinct is the mismatch `eq?' above let through.
|
|
||||||
((and (tvar? a) (not (tvar-rigid? a))) (bind-tvar! a b form))
|
|
||||||
((and (tvar? b) (not (tvar-rigid? b))) (bind-tvar! b a form))
|
|
||||||
((or (tvar? a) (tvar? b)) (type-mismatch a b form))
|
|
||||||
;; A typedef unifies as what it stands for. Its name survives in
|
;; A typedef unifies as what it stands for. Its name survives in
|
||||||
;; whichever side is printed later, since neither side is rebuilt.
|
;; whichever side is printed later, since neither side is rebuilt.
|
||||||
((alias-type? a) (unify (alias-expansion a) b form))
|
((alias-type? a) (unify (alias-expansion a) b form))
|
||||||
@@ -630,16 +424,7 @@
|
|||||||
;; Without recursive types this cannot trigger. It is four lines,
|
;; Without recursive types this cannot trigger. It is four lines,
|
||||||
;; and the alternative to having it is a hang.
|
;; and the alternative to having it is a hang.
|
||||||
((occurs? v t) (sex-error form "recursive type" (unparse-type v)))
|
((occurs? v t) (sex-error form "recursive type" (unparse-type v)))
|
||||||
;; A rigid variable is a type *parameter*: inside a generic body it
|
(else (tvar-ref-set! v (with-quals t (tvar-quals v))) #t)))
|
||||||
;; stands for one specific unknown type and must not be solved.
|
|
||||||
((tvar-rigid? v) (type-mismatch v t form))
|
|
||||||
(else
|
|
||||||
(when (tvar? t)
|
|
||||||
(tvar-classes-set! t (lset-union eq? (tvar-classes t) (tvar-classes v))))
|
|
||||||
(tvar-ref-set! v (with-quals t (tvar-quals v)))
|
|
||||||
(unless (tvar? t)
|
|
||||||
(check-classes (tvar-classes v) t form))
|
|
||||||
#t)))
|
|
||||||
|
|
||||||
(define (occurs? v type)
|
(define (occurs? v type)
|
||||||
(let ((t (resolve type)))
|
(let ((t (resolve type)))
|
||||||
@@ -650,57 +435,3 @@
|
|||||||
((fn-type? t) (or (occurs? v (fn-ret t))
|
((fn-type? t) (or (occurs? v (fn-ret t))
|
||||||
(any (lambda (a) (occurs? v a)) (fn-args t))))
|
(any (lambda (a) (occurs? v a)) (fn-args t))))
|
||||||
(else #f))))
|
(else #f))))
|
||||||
|
|
||||||
;;; ---------------------------------------------------------------
|
|
||||||
;;; Type schemes
|
|
||||||
;;; ---------------------------------------------------------------
|
|
||||||
|
|
||||||
;;; Nothing generalizes yet -- every `fn' in Sex carries a written
|
|
||||||
;;; signature and there is no polymorphism to abstract over. These are
|
|
||||||
;;; here because they are ten lines on top of unification and because
|
|
||||||
;;; they are exactly what a `fn' with type parameters needs, and
|
|
||||||
;;; because a scheme without a constraint list is the wrong shape for
|
|
||||||
;;; every bounded generic. `(forall vars constraints type)' it is,
|
|
||||||
;;; from the start.
|
|
||||||
(define-record-type <scheme>
|
|
||||||
(make-scheme vars constraints type)
|
|
||||||
scheme?
|
|
||||||
(vars scheme-vars)
|
|
||||||
(constraints scheme-constraints)
|
|
||||||
(type scheme-type))
|
|
||||||
|
|
||||||
;;; Quantify over everything free in TYPE that is not also free in the
|
|
||||||
;;; environment, carrying each variable's class constraints along as
|
|
||||||
;;; the scheme's context.
|
|
||||||
(define (generalize type env-tvars)
|
|
||||||
(let ((vars (lset-difference eq? (free-tvars type) env-tvars)))
|
|
||||||
(make-scheme vars
|
|
||||||
(append-map (lambda (v)
|
|
||||||
(map (lambda (c) (cons c v)) (tvar-classes v)))
|
|
||||||
vars)
|
|
||||||
type)))
|
|
||||||
|
|
||||||
(define (instantiate scheme)
|
|
||||||
(let ((subst (map (lambda (v) (cons v (fresh-tvar (tvar-classes v))))
|
|
||||||
(scheme-vars scheme))))
|
|
||||||
(substitute (scheme-type scheme) subst)))
|
|
||||||
|
|
||||||
;;; Structural copy with the variables in SUBST replaced. Copying is
|
|
||||||
;;; how a generic body must be handled anyway -- `form-type' is keyed
|
|
||||||
;;; by cons cell, one form one type, so an instantiation gets fresh
|
|
||||||
;;; cells rather than a second type for the same cell.
|
|
||||||
(define (substitute type subst)
|
|
||||||
(let ((t (resolve type)))
|
|
||||||
(cond
|
|
||||||
((tvar? t) (let ((hit (assq t subst))) (if hit (cdr hit) t)))
|
|
||||||
((ptr-type? t) (make-ptr (substitute (ptr-target t) subst) (ptr-quals t)))
|
|
||||||
((array-type? t) (make-array-type (substitute (array-elt t) subst)
|
|
||||||
(array-size t)))
|
|
||||||
((alias-type? t) (make-alias (alias-name t)
|
|
||||||
(substitute (alias-expansion t) subst)
|
|
||||||
(alias-quals t)))
|
|
||||||
((fn-type? t) (make-fn-type (substitute (fn-ret t) subst)
|
|
||||||
(map (lambda (a) (substitute a subst))
|
|
||||||
(fn-args t))
|
|
||||||
(fn-variadic? t)))
|
|
||||||
(else t))))
|
|
||||||
|
|||||||
10
reader.scm
10
reader.scm
@@ -34,10 +34,7 @@
|
|||||||
|
|
||||||
number-literal?
|
number-literal?
|
||||||
number-literal-value
|
number-literal-value
|
||||||
number-literal-suffix
|
number-literal-suffix)
|
||||||
|
|
||||||
;; 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))
|
||||||
@@ -237,7 +234,9 @@
|
|||||||
;;; Scheme reads numbers C has no literal for -- 1/2, #e1.5, 1e400,
|
;;; Scheme reads numbers C has no literal for -- 1/2, #e1.5, 1e400,
|
||||||
;;; +nan.0, 1+2i -- and they reached C as written. An integer too wide
|
;;; +nan.0, 1+2i -- and they reached C as written. An integer too wide
|
||||||
;;; for long long is unsigned to C, which says so only by a warning
|
;;; for long long is unsigned to C, which says so only by a warning
|
||||||
;;; unless it is written with a `u'; so it is.
|
;;; unless it is written with a `u'; so it is. -9223372036854775808 is
|
||||||
|
;;; one too: C negates the unsigned 9223372036854775808, so it is
|
||||||
|
;;; spelled as LLONG_MIN is, -9223372036854775807 - 1.
|
||||||
(define +llong-min+ (- (expt 2 63)))
|
(define +llong-min+ (- (expt 2 63)))
|
||||||
(define +llong-max+ (- (expt 2 63) 1))
|
(define +llong-max+ (- (expt 2 63) 1))
|
||||||
(define +ullong-max+ (- (expt 2 64) 1))
|
(define +ullong-max+ (- (expt 2 64) 1))
|
||||||
@@ -248,6 +247,7 @@
|
|||||||
(error "too large for any C integer type" s))
|
(error "too large for any C integer type" s))
|
||||||
((> n +llong-max+) (make-number-literal n "u" #f))
|
((> n +llong-max+) (make-number-literal n "u" #f))
|
||||||
((< n +llong-min+) (list '- (make-number-literal (- n) "u" #f)))
|
((< n +llong-min+) (list '- (make-number-literal (- n) "u" #f)))
|
||||||
|
((= n +llong-min+) (list '- (+ n 1) 1))
|
||||||
(else n)))
|
(else n)))
|
||||||
((and (real? n) (inexact? n) (finite? n)) n)
|
((and (real? n) (inexact? n) (finite? n)) n)
|
||||||
(else (error "C has no literal for this number" s))))
|
(else (error "C has no literal for this number" s))))
|
||||||
|
|||||||
28
semen.scm
28
semen.scm
@@ -20,11 +20,7 @@
|
|||||||
)
|
)
|
||||||
|
|
||||||
(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
|
||||||
@@ -1331,8 +1327,7 @@
|
|||||||
;;; member access.
|
;;; member access.
|
||||||
|
|
||||||
;;; The type of an expression as a surface type, or #f when nothing
|
;;; The type of an expression as a surface type, or #f when nothing
|
||||||
;;; here can say. Every form it visits is recorded in `form-type', so
|
;;; here can say.
|
||||||
;;; asking once types the whole subtree.
|
|
||||||
;;;
|
;;;
|
||||||
;;; 42 -> int (. p x) -> that field's type
|
;;; 42 -> int (. p x) -> that field's type
|
||||||
;;; "hi" -> (* const char) (& p) -> (* (struct point))
|
;;; "hi" -> (* const char) (& p) -> (* (struct point))
|
||||||
@@ -1363,9 +1358,6 @@
|
|||||||
(and rank (first rank)))))
|
(and rank (first rank)))))
|
||||||
|
|
||||||
(define (expression-type expr env)
|
(define (expression-type expr env)
|
||||||
(set-form-type! expr (compute-expression-type expr env)))
|
|
||||||
|
|
||||||
(define (compute-expression-type expr env)
|
|
||||||
(cond
|
(cond
|
||||||
((number-literal? expr) (number-literal-type expr))
|
((number-literal? expr) (number-literal-type expr))
|
||||||
((and (number? expr) (exact? expr)) (integer-literal-type expr '()))
|
((and (number? expr) (exact? expr)) (integer-literal-type expr '()))
|
||||||
@@ -2039,6 +2031,11 @@
|
|||||||
(when (memq name parameters)
|
(when (memq name parameters)
|
||||||
(sex-error form "a capture and a parameter share a name" name)))
|
(sex-error form "a capture and a parameter share a name" name)))
|
||||||
names)
|
names)
|
||||||
|
(let twice ((names names))
|
||||||
|
(when (pair? names)
|
||||||
|
(when (memq (car names) (cdr names))
|
||||||
|
(sex-error form "two captures share a name" (car names)))
|
||||||
|
(twice (cdr names))))
|
||||||
(for-each (lambda (statement)
|
(for-each (lambda (statement)
|
||||||
(match statement
|
(match statement
|
||||||
(('var (? symbol? name) . _)
|
(('var (? symbol? name) . _)
|
||||||
@@ -2364,9 +2361,6 @@
|
|||||||
(string->symbol (substring s 1))))))
|
(string->symbol (substring s 1))))))
|
||||||
|
|
||||||
;;; Utils
|
;;; Utils
|
||||||
(define (non-empty-list? form)
|
|
||||||
(and (list? form)
|
|
||||||
(not (null? form))))
|
|
||||||
|
|
||||||
(define (sex-fn? form)
|
(define (sex-fn? form)
|
||||||
"The `form` must be toplevel.
|
"The `form` must be toplevel.
|
||||||
@@ -2377,9 +2371,6 @@ Returns #f if the form is not a function, returns the form otherwise"
|
|||||||
('extern 'fn . _)) form)
|
('extern 'fn . _)) form)
|
||||||
(else #f)))
|
(else #f)))
|
||||||
|
|
||||||
(define (sex-fn-public? fn-form)
|
|
||||||
(eq? (first fn-form) 'pub))
|
|
||||||
|
|
||||||
(define (sex-fn-name fn-form)
|
(define (sex-fn-name 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"))
|
||||||
@@ -2394,8 +2385,3 @@ Returns #f if the form is not a function, returns the form otherwise"
|
|||||||
(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"))
|
||||||
(fourth (fn-core fn-form)))
|
(fourth (fn-core fn-form)))
|
||||||
|
|
||||||
(define (sex-fn-body fn-form)
|
|
||||||
(assert (sex-fn? fn-form)
|
|
||||||
(fmt #f "Form " fn-form " is not a function"))
|
|
||||||
(drop fn-form (fn-header-length fn-form)))
|
|
||||||
|
|||||||
@@ -668,9 +668,6 @@
|
|||||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||||
;; data structures
|
;; data structures
|
||||||
|
|
||||||
;; MODIFIED FROM UPSTREAM fmt-c: behind a pointer, the rest of the
|
|
||||||
;; chain is O -- `struct p * * q' is body `*' and O (*) -- and it was
|
|
||||||
;; dropped, so the declaration lost all but its first star.
|
|
||||||
(define (c-struct/aux type x . o)
|
(define (c-struct/aux type x . o)
|
||||||
;; can be just pointer to SUC, need to support such case:
|
;; can be just pointer to SUC, need to support such case:
|
||||||
;; struct whatever * - body is just '*'
|
;; struct whatever * - body is just '*'
|
||||||
@@ -695,8 +692,14 @@
|
|||||||
(cat " " name)
|
(cat " " name)
|
||||||
"")
|
"")
|
||||||
(if (not (null? body))
|
(if (not (null? body))
|
||||||
(apply cat body (map (lambda (t) (cat " " t)) o))
|
(cat body)
|
||||||
""))))))
|
"")
|
||||||
|
(c-type-words o))))))
|
||||||
|
|
||||||
|
;; MODIFIED FROM UPSTREAM fmt-c: what follows the first star of a
|
||||||
|
;; tagged type, `* const' in (struct what * const), was dropped
|
||||||
|
(define (c-type-words words)
|
||||||
|
(fmt-join (lambda (word) (cat " " word)) words ""))
|
||||||
|
|
||||||
(define (c-struct . args) (apply c-struct/aux "struct" args))
|
(define (c-struct . args) (apply c-struct/aux "struct" args))
|
||||||
(define (c-union . args) (apply c-struct/aux "union" args))
|
(define (c-union . args) (apply c-struct/aux "union" args))
|
||||||
@@ -711,13 +714,15 @@
|
|||||||
(if (pair? x) (cat (car x) " = " (c-expr (cadr x))) (dsp x)))
|
(if (pair? x) (cat (car x) " = " (c-expr (cadr x))) (dsp x)))
|
||||||
(let* ((name (if (null? o) (if (or (symbol? x) (string? x)) x #f) x))
|
(let* ((name (if (null? o) (if (or (symbol? x) (string? x)) x #f) x))
|
||||||
(vals (if name (if (null? o) #f (car o)) x)))
|
(vals (if name (if (null? o) #f (car o)) x)))
|
||||||
(if vals
|
;; MODIFIED FROM UPSTREAM fmt-c: (enum mood * const) is a
|
||||||
|
;; pointer, and its star was taken for the list of values
|
||||||
|
(if (pair? vals)
|
||||||
(c-wrap-stmt
|
(c-wrap-stmt
|
||||||
(cat
|
(cat
|
||||||
(c-braced-block
|
(c-braced-block
|
||||||
(if name (cat "enum " name) (dsp "enum"))
|
(if name (cat "enum " name) (dsp "enum"))
|
||||||
(c-in-expr (apply c-begin (map c-enum-one vals))))))
|
(c-in-expr (apply c-begin (map c-enum-one vals))))))
|
||||||
(c-wrap-stmt (cat "enum " name)))))
|
(c-wrap-stmt (cat "enum " name (or vals "") (c-type-words (if vals (cdr o) '())))))))
|
||||||
|
|
||||||
(define (c-attribute . args)
|
(define (c-attribute . args)
|
||||||
(cat "__attribute__ ((" (fmt-join c-expr args ", ") "))"))
|
(cat "__attribute__ ((" (fmt-join c-expr args ", ") "))"))
|
||||||
|
|||||||
@@ -10,10 +10,8 @@
|
|||||||
utils)
|
utils)
|
||||||
|
|
||||||
(export
|
(export
|
||||||
register-macro
|
|
||||||
cat
|
cat
|
||||||
comment
|
comment
|
||||||
get-macro
|
|
||||||
macro?
|
macro?
|
||||||
apply-macro
|
apply-macro
|
||||||
defmacro
|
defmacro
|
||||||
|
|||||||
@@ -21,10 +21,7 @@
|
|||||||
load-persistent-module-paths
|
load-persistent-module-paths
|
||||||
check-public-interface
|
check-public-interface
|
||||||
module-dependencies
|
module-dependencies
|
||||||
read-public-interface
|
read-public-interface)
|
||||||
|
|
||||||
;; for the tests
|
|
||||||
get-module-paths)
|
|
||||||
|
|
||||||
(define +persistent-module-paths+ (list))
|
(define +persistent-module-paths+ (list))
|
||||||
|
|
||||||
@@ -133,10 +130,11 @@
|
|||||||
;;; macros are the module's own and those its imports give it, lent for
|
;;; macros are the module's own and those its imports give it, lent for
|
||||||
;;; the expanding and taken back after, so the importer is left with the
|
;;; the expanding and taken back after, so the importer is left with the
|
||||||
;;; pub ones alone. One that reads the type database sees what the
|
;;; pub ones alone. One that reads the type database sees what the
|
||||||
;;; importer knows, which a type private to the module is not; a call
|
;;; importer knows, which no declaration of the module's is yet; a call
|
||||||
;;; that cannot expand for that -- (print-struct box), box being the
|
;;; that fails after asking for one -- (field-count box), box being the
|
||||||
;;; module's -- gives importers nothing, as before, and is the module's
|
;;; module's record -- gives importers nothing, as before, and is the
|
||||||
;;; own compile's to check
|
;;; module's own compile's to check. Any other failure is the importer's
|
||||||
|
;;; too: (boom) raising "nope" says so wherever it is read
|
||||||
(define +modules-expanded+ (list))
|
(define +modules-expanded+ (list))
|
||||||
|
|
||||||
(define (module-forms raw-forms)
|
(define (module-forms raw-forms)
|
||||||
@@ -147,7 +145,8 @@
|
|||||||
forms))))
|
forms))))
|
||||||
|
|
||||||
(define (expanded raw-forms)
|
(define (expanded raw-forms)
|
||||||
(let ((lent (list)))
|
(let ((lent (list))
|
||||||
|
(declared (declared-names raw-forms)))
|
||||||
(define (lend! form)
|
(define (lend! form)
|
||||||
(let ((name (macro-name form)))
|
(let ((name (macro-name form)))
|
||||||
(unless (assq name lent)
|
(unless (assq name lent)
|
||||||
@@ -157,15 +156,37 @@
|
|||||||
(cond ((macro-name form) (lend! form) (list form))
|
(cond ((macro-name form) (lend! form) (list form))
|
||||||
((splice? form) (append-map expand (cdr form)))
|
((splice? form) (append-map expand (cdr form)))
|
||||||
((macro? form)
|
((macro? form)
|
||||||
(let ((result (handle-exceptions exn #f (apply-macro form))))
|
(let ((result (expand-call form declared)))
|
||||||
(cond ((or (not result) (null? result)) (list))
|
(cond ((or (not result) (null? result)) (list))
|
||||||
(else (stamp-form-source! result (form-source form))
|
(else (stamp-form-source! result (form-source form))
|
||||||
(expand result)))))
|
(expand result)))))
|
||||||
(else (list form))))
|
(else (list form))))
|
||||||
(for-each lend! (imported-macro-forms raw-forms))
|
(dynamic-wind
|
||||||
(let ((forms (append-map expand raw-forms)))
|
void
|
||||||
(for-each (lambda (entry) (restore-macro! (car entry) (cdr entry))) lent)
|
(lambda ()
|
||||||
forms)))
|
(for-each lend! (imported-macro-forms raw-forms))
|
||||||
|
(append-map expand raw-forms))
|
||||||
|
(lambda ()
|
||||||
|
(for-each (lambda (entry) (restore-macro! (car entry) (cdr entry)))
|
||||||
|
lent)))))
|
||||||
|
|
||||||
|
(define (expand-call form declared)
|
||||||
|
(let ((unknown (list)))
|
||||||
|
(handle-exceptions exn
|
||||||
|
(if (any (lambda (name) (memq name declared)) unknown) #f (abort exn))
|
||||||
|
(parameterize ((unknown-name (lambda (name) (set! unknown (cons name unknown)))))
|
||||||
|
(apply-macro form)))))
|
||||||
|
|
||||||
|
;;; The names FORMS declare, types and all
|
||||||
|
(define (declared-names forms)
|
||||||
|
(append (map car (declared-types forms))
|
||||||
|
(filter-map (lambda (form)
|
||||||
|
(match form
|
||||||
|
((or ('pub (or 'fn 'var) (? symbol? name) . _)
|
||||||
|
((or 'fn 'var) (? symbol? name) . _))
|
||||||
|
name)
|
||||||
|
(_ #f)))
|
||||||
|
forms)))
|
||||||
|
|
||||||
(define (splice? form)
|
(define (splice? form)
|
||||||
(and (pair? form) (eq? (car form) '$) (list? form)))
|
(and (pair? form) (eq? (car form) '$) (list? form)))
|
||||||
|
|||||||
6
sexc.scm
6
sexc.scm
@@ -23,11 +23,7 @@
|
|||||||
utils)
|
utils)
|
||||||
|
|
||||||
(export
|
(export
|
||||||
main
|
main)
|
||||||
|
|
||||||
;; for the tests
|
|
||||||
args-before-separator args-after-separator
|
|
||||||
dependency-rule spell-stdout-output)
|
|
||||||
|
|
||||||
;;; Main function facilities
|
;;; Main function facilities
|
||||||
|
|
||||||
|
|||||||
@@ -1,83 +0,0 @@
|
|||||||
;;; Splitting the command line at `--'.
|
|
||||||
;;;
|
|
||||||
;;; getopt-long cannot do this: it consumes the separator and merges
|
|
||||||
;;; everything after it into `@' alongside the input file. So sexc
|
|
||||||
;;; splits the raw argv first, and only the head is parsed as options.
|
|
||||||
;;; Everything else reaches the C compiler exactly as written --
|
|
||||||
;;; including the words that do not start with a dash, which a previous
|
|
||||||
;;; leading-dash heuristic used to drop.
|
|
||||||
|
|
||||||
(import sexc)
|
|
||||||
|
|
||||||
(define full '("foo.sex" "-o" "bar" "--" "-framework" "OpenGL" "-Wall"))
|
|
||||||
|
|
||||||
(test-group "argument separator"
|
|
||||||
|
|
||||||
(test "options and input file stay with sexc"
|
|
||||||
'("foo.sex" "-o" "bar")
|
|
||||||
(args-before-separator full))
|
|
||||||
|
|
||||||
(test "the tail reaches the compiler verbatim"
|
|
||||||
'("-framework" "OpenGL" "-Wall")
|
|
||||||
(args-after-separator full))
|
|
||||||
|
|
||||||
;; The case that motivated this: `OpenGL' has no leading dash and was
|
|
||||||
;; silently dropped, leaving `-framework' to swallow whatever flag
|
|
||||||
;; came next.
|
|
||||||
(test "a word without a dash survives"
|
|
||||||
'("-framework" "OpenGL")
|
|
||||||
(args-after-separator '("x.sex" "--" "-framework" "OpenGL")))
|
|
||||||
|
|
||||||
(test "no separator means nothing for the compiler"
|
|
||||||
'()
|
|
||||||
(args-after-separator '("foo.sex" "-o" "bar")))
|
|
||||||
|
|
||||||
(test "no separator leaves every argument with sexc"
|
|
||||||
'("foo.sex" "-o" "bar")
|
|
||||||
(args-before-separator '("foo.sex" "-o" "bar")))
|
|
||||||
|
|
||||||
(test "a trailing separator is allowed"
|
|
||||||
'()
|
|
||||||
(args-after-separator '("foo.sex" "--")))
|
|
||||||
|
|
||||||
(test "a leading separator leaves no input file"
|
|
||||||
'()
|
|
||||||
(args-before-separator '("--" "-lm")))
|
|
||||||
|
|
||||||
;; Only the first `--' separates; a later one is an ordinary compiler
|
|
||||||
;; argument (ld takes several).
|
|
||||||
(test "only the first separator counts"
|
|
||||||
'("-Wl,--as-needed" "--" "-lm")
|
|
||||||
(args-after-separator '("x.sex" "--" "-Wl,--as-needed" "--" "-lm")))
|
|
||||||
|
|
||||||
(test "an empty command line is handled"
|
|
||||||
'()
|
|
||||||
(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"))))
|
|
||||||
@@ -1,58 +0,0 @@
|
|||||||
(import fmt-c-writer)
|
|
||||||
|
|
||||||
(test-group "basic"
|
|
||||||
|
|
||||||
;; unkebabify
|
|
||||||
(test '- (unkebabify '-))
|
|
||||||
(test '-- (unkebabify '--))
|
|
||||||
(test '-> (unkebabify '->))
|
|
||||||
(test '-= (unkebabify '-=))
|
|
||||||
(test 'kebab_case (unkebabify 'kebab-case))
|
|
||||||
(test '_what_ (unkebabify '-what-))
|
|
||||||
(test 'this->member (unkebabify 'this->member))
|
|
||||||
(test '_this_->_member_ (unkebabify '-this-->-member-))
|
|
||||||
(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
|
|
||||||
;; string-substitute drops one trailing character per multi-byte
|
|
||||||
;; character, which renames things silently -- the C still compiles,
|
|
||||||
;; just under a different name than was written.
|
|
||||||
(test 'naïve_count (unkebabify 'naïve-count))
|
|
||||||
(test 'aï_b (unkebabify 'aï-b))
|
|
||||||
(test 'ïï (unkebabify 'ïï))
|
|
||||||
|
|
||||||
;; atom-to-fmt-c
|
|
||||||
(test '%fun (atom-to-fmt-c 'fn))
|
|
||||||
(test '%prototype (atom-to-fmt-c 'prototype))
|
|
||||||
(test '%block-begin (atom-to-fmt-c 'do))
|
|
||||||
(test '%define (atom-to-fmt-c 'define))
|
|
||||||
(test '%pointer (atom-to-fmt-c 'pointer))
|
|
||||||
(test '%array (atom-to-fmt-c 'array))
|
|
||||||
(test 'vector-ref (atom-to-fmt-c '¤))
|
|
||||||
(test '%include (atom-to-fmt-c 'include))
|
|
||||||
|
|
||||||
;; C99 onwards, true C spellings for bool
|
|
||||||
(test 'bool (atom-to-fmt-c 'bool))
|
|
||||||
(test 'true (atom-to-fmt-c 'true))
|
|
||||||
(test 'false (atom-to-fmt-c 'false))
|
|
||||||
|
|
||||||
;; dot-access -> %. member-access directive (kebab-converted operands)
|
|
||||||
(test '(%. a b) (walk-expr '(dot-access a b)))
|
|
||||||
(test '(%. a b c) (walk-expr '(dot-access a b c)))
|
|
||||||
(test '(%. a_b c) (walk-expr '(dot-access a-b c)))
|
|
||||||
|
|
||||||
;; comment -> %comment directive (rendered as /* ... */)
|
|
||||||
;; The reader eats only the `;' that introduced the line, so ";;; Foo"
|
|
||||||
;; arrives as ";; Foo"; and c-comment puts nothing between /* */ and
|
|
||||||
;; the text. Both are handled on the way out.
|
|
||||||
(test '(%comment " hi ") (walk-expr '(comment " hi")))
|
|
||||||
(test '(%comment " hi ") (process-toplevel-form '(comment " hi")))
|
|
||||||
(test '(%comment " Foo ") (walk-expr '(comment ";; Foo")))
|
|
||||||
(test '(%comment " Foo ") (walk-expr '(comment ";;; Foo "))))
|
|
||||||
@@ -1,71 +1,8 @@
|
|||||||
;;; Codegen details that are easy to get subtly wrong, and that the
|
;;; Codegen details that are easy to get subtly wrong: precedence,
|
||||||
;;; walk-* unit tests cannot see: they check the intermediate form we
|
;;; scopes, comments in odd places, and the shapes no earlier test
|
||||||
;;; hand to fmt-c, not the C that fmt-c renders from it.
|
;;; program happened to use.
|
||||||
;;;
|
|
||||||
;;; Both cases below were found by writing an OpenGL example, not by
|
|
||||||
;;; the existing suite, because both need an operand shape that no
|
|
||||||
;;; earlier test program happened to use.
|
|
||||||
|
|
||||||
(import (chicken condition)
|
(import srfi-13)
|
||||||
(chicken port)
|
|
||||||
(chicken string)
|
|
||||||
srfi-13
|
|
||||||
fmt-c-writer
|
|
||||||
reader
|
|
||||||
semen
|
|
||||||
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)
|
|
||||||
"Compile SOURCE, a string of Sex, and return the generated C."
|
|
||||||
(let ((forms (read-source source)))
|
|
||||||
(with-output-to-string
|
|
||||||
(lambda ()
|
|
||||||
(parameterize ((sex-line-directives 'none))
|
|
||||||
(emit-c (semen-process forms)))))))
|
|
||||||
|
|
||||||
;;; Warnings are what `warns?' is for; a test that happens to trip one
|
|
||||||
;;; should not print it
|
|
||||||
(define (silently thunk)
|
|
||||||
"THUNK's value, with whatever it wrote to stderr dropped."
|
|
||||||
(let ((value #f))
|
|
||||||
(with-error-output-to-string (lambda () (set! value (thunk))))
|
|
||||||
value))
|
|
||||||
|
|
||||||
(define (emits? source fragment)
|
|
||||||
(and (string-contains (silently (lambda () (sex->c source))) fragment) #t))
|
|
||||||
|
|
||||||
(define (failure-message thunk)
|
|
||||||
"The error THUNK raises as the user sees it -- message plus arguments,
|
|
||||||
the way CHICKEN prints it -- or #f if it raises none."
|
|
||||||
(handle-exceptions e
|
|
||||||
(with-output-to-string
|
|
||||||
(lambda ()
|
|
||||||
(display ((condition-property-accessor 'exn 'message) e))
|
|
||||||
(for-each (lambda (a) (display " ") (write a))
|
|
||||||
((condition-property-accessor 'exn 'arguments) e))))
|
|
||||||
(begin (thunk) #f)))
|
|
||||||
|
|
||||||
(define (fails-with? thunk fragment)
|
|
||||||
(let ((m (failure-message thunk)))
|
|
||||||
(and m (string-contains m fragment) #t)))
|
|
||||||
|
|
||||||
(define (reports? source fragment)
|
|
||||||
(fails-with? (lambda () (sex->c source)) fragment))
|
|
||||||
|
|
||||||
(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)))))))
|
|
||||||
|
|
||||||
;;; A record reached through a typedef of a pointer to it
|
;;; A record reached through a typedef of a pointer to it
|
||||||
(define pt-ref-src
|
(define pt-ref-src
|
||||||
@@ -96,13 +33,6 @@ whitespace aside, THEN?"
|
|||||||
" (var p (struct point)) (var pp (* struct point) (& p))"
|
" (var p (struct point)) (var pp (* struct point) (& p))"
|
||||||
" (var l (struct line)) " body ")"))
|
" (var l (struct line)) " body ")"))
|
||||||
|
|
||||||
(define (warnings source)
|
|
||||||
"What compiling SOURCE writes to stderr."
|
|
||||||
(with-error-output-to-string (lambda () (sex->c source))))
|
|
||||||
|
|
||||||
(define (warns? source fragment)
|
|
||||||
(and (string-contains (warnings source) fragment) #t))
|
|
||||||
|
|
||||||
(test-group "codegen"
|
(test-group "codegen"
|
||||||
|
|
||||||
;; A case body shared the switch's scope, so two cases could not
|
;; A case body shared the switch's scope, so two cases could not
|
||||||
@@ -412,7 +342,7 @@ whitespace aside, THEN?"
|
|||||||
"codegen.sex:2: a macro defined after a call to it"))
|
"codegen.sex:2: a macro defined after a call to it"))
|
||||||
(test-assert "naming where the call is"
|
(test-assert "naming where the call is"
|
||||||
(reports? "(fn f () int\n (return (later 1)))\n(defmacro (later x) x)"
|
(reports? "(fn f () int\n (return (later 1)))\n(defmacro (later x) x)"
|
||||||
"called at codegen.sex:2"))
|
"/codegen.sex:2\""))
|
||||||
(test-assert "an error in a macro's body names the call"
|
(test-assert "an error in a macro's body names the call"
|
||||||
(reports? "(defmacro (boom x) (error \"no good\" x))\n(fn f () int (return (boom 1)))"
|
(reports? "(defmacro (boom x) (error \"no good\" x))\n(fn f () int (return (boom 1)))"
|
||||||
"codegen.sex:2: expanding boom: no good")))
|
"codegen.sex:2: expanding boom: no good")))
|
||||||
@@ -568,6 +498,9 @@ whitespace aside, THEN?"
|
|||||||
(test-assert "a capture named like a parameter is refused"
|
(test-assert "a capture named like a parameter is refused"
|
||||||
(reports? (in-fn "(var c (closure ((int)) int) (closure ((a int)) int (a) (return a)))")
|
(reports? (in-fn "(var c (closure ((int)) int) (closure ((a int)) int (a) (return a)))")
|
||||||
"a capture and a parameter share a name"))
|
"a capture and a parameter share a name"))
|
||||||
|
(test-assert "so is one captured twice"
|
||||||
|
(reports? (in-fn "(var c (closure () int) (closure () int (a a) (return a)))")
|
||||||
|
"two captures share a name"))
|
||||||
(test-assert "so is one redeclared at the top of the body"
|
(test-assert "so is one redeclared at the top of the body"
|
||||||
(reports? (in-fn "(var c (closure () int) (closure () int (a) (var a int 5) (return a)))")
|
(reports? (in-fn "(var c (closure () int) (closure () int (a) (var a int 5) (return a)))")
|
||||||
"the body redeclares a capture"))
|
"the body redeclares a capture"))
|
||||||
@@ -814,6 +747,19 @@ whitespace aside, THEN?"
|
|||||||
"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]"))
|
||||||
|
;; each `_' is its own variable: solving one leaves the rest open
|
||||||
|
(test-assert "every wildcard in a signature is its own"
|
||||||
|
(emits? "(fn g ((x int)) char (return 0))
|
||||||
|
(fn f () void (var h (fn ((_)) _) g))"
|
||||||
|
"char (*h)(int) = g"))
|
||||||
|
(test-assert "and the signature's arity has to match"
|
||||||
|
(reports? "(fn g ((x int) (y int)) int (return 0))
|
||||||
|
(fn f () void (var h (fn ((_)) _) g))"
|
||||||
|
"type mismatch"))
|
||||||
|
(test-assert "a typedef answers as itself"
|
||||||
|
(emits? "(typedef count int) (fn g () count (return 0))
|
||||||
|
(fn f () void (var x _ (g)))"
|
||||||
|
"count x = g()"))
|
||||||
;; only an integer was taken for a length; a name was read as part
|
;; only an integer was taken for a length; a name was read as part
|
||||||
;; of the element's
|
;; of the element's
|
||||||
(test-assert "of a named length"
|
(test-assert "of a named length"
|
||||||
@@ -1096,14 +1042,6 @@ whitespace aside, THEN?"
|
|||||||
(emits? "(include stdio.h) (fn f () void (static-assert (== 1 1) \"x\"))"
|
(emits? "(include stdio.h) (fn f () void (static-assert (== 1 1) \"x\"))"
|
||||||
"_Static_assert(1 == 1, \"x\")")))
|
"_Static_assert(1 == 1, \"x\")")))
|
||||||
|
|
||||||
;; c-struct/aux wrote the tag and the first `*' after it, and
|
|
||||||
;; dropped the rest of the chain
|
|
||||||
(test-group "a pointer chain to a struct"
|
|
||||||
(test-assert "keeps every star"
|
|
||||||
(emits? (in-fn "(var q (* * struct p))") "struct p* * q;"))
|
|
||||||
(test-assert "and every qualifier between them"
|
|
||||||
(emits? (in-fn "(var q (* const * union u))") "union u* const * q;")))
|
|
||||||
|
|
||||||
;; Operands past what an operator takes were written as they came:
|
;; Operands past what an operator takes were written as they came:
|
||||||
;; (< a b c) as C's own chain, which compares a boolean with c
|
;; (< a b c) as C's own chain, which compares a boolean with c
|
||||||
(test-group "operator arity"
|
(test-group "operator arity"
|
||||||
@@ -1252,7 +1190,10 @@ whitespace aside, THEN?"
|
|||||||
(emits? (in-fn "(var n (unsigned int) -1u)") "n = -1u;"))
|
(emits? (in-fn "(var n (unsigned int) -1u)") "n = -1u;"))
|
||||||
(test-assert "a number too wide for long long is unsigned, and says so"
|
(test-assert "a number too wide for long long is unsigned, and says so"
|
||||||
(emits? (in-fn "(var n (unsigned long long) 0xFFFFFFFFFFFFFFFF)")
|
(emits? (in-fn "(var n (unsigned long long) 0xFFFFFFFFFFFFFFFF)")
|
||||||
"n = 18446744073709551615u;")))
|
"n = 18446744073709551615u;"))
|
||||||
|
(test-assert "the least long long is spelled as LLONG_MIN, and typed"
|
||||||
|
(emits? (in-fn "(var n _ -9223372036854775808)")
|
||||||
|
"long long n = -9223372036854775807 - 1;")))
|
||||||
|
|
||||||
;; A define's body was pasted unwalked, and unparenthesised: `cast'
|
;; A define's body was pasted unwalked, and unparenthesised: `cast'
|
||||||
;; stayed a call, and (* x D) regrouped with D's own operator
|
;; stayed a call, and (* x D) regrouped with D's own operator
|
||||||
@@ -1430,6 +1371,22 @@ whitespace aside, THEN?"
|
|||||||
"(var g (struct h2) #(#((struct pt) : 1)))")
|
"(var g (struct h2) #(#((struct pt) : 1)))")
|
||||||
"h2 g = {{1}}")))
|
"h2 g = {{1}}")))
|
||||||
|
|
||||||
|
;; fmt-c kept the first star after a tag and dropped what followed it,
|
||||||
|
;; and took an enum's star for its list of values
|
||||||
|
(test-group "pointers to tagged types"
|
||||||
|
(test-assert "a const pointer to a struct stays const"
|
||||||
|
(emits? "(var q (const * struct what))" "struct what* const q;"))
|
||||||
|
(test-assert "and one in an array"
|
||||||
|
(emits? "(var a [const * struct what 4])" "struct what* const a[4];"))
|
||||||
|
(test-assert "a pointer to a pointer keeps both stars"
|
||||||
|
(emits? "(var pp (* * struct what))" "struct what* * pp;"))
|
||||||
|
(test-assert "so does a union's"
|
||||||
|
(emits? "(var u (const * union val))" "union val* const u;"))
|
||||||
|
(test-assert "a pointer to an enum is a pointer"
|
||||||
|
(emits? "(enum mood (glad sad)) (var e (* enum mood))" "enum mood* e;"))
|
||||||
|
(test-assert "and a const one"
|
||||||
|
(emits? "(enum mood (glad sad)) (var f (const * enum mood))" "enum mood* const f;")))
|
||||||
|
|
||||||
;; Toplevel forms used to be separated by a blank line unconditionally,
|
;; Toplevel forms used to be separated by a blank line unconditionally,
|
||||||
;; which spread a block of adjacent typedefs over twice the lines its
|
;; which spread a block of adjacent typedefs over twice the lines its
|
||||||
;; author wrote
|
;; author wrote
|
||||||
|
|||||||
4
tests/command-line/cc
Executable file
4
tests/command-line/cc
Executable file
@@ -0,0 +1,4 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
# Stands in for the C compiler: writes the arguments it was given, one
|
||||||
|
# to a line, to $CC_ARGUMENTS
|
||||||
|
printf '%s\n' "$@" > "$CC_ARGUMENTS"
|
||||||
108
tests/command-line/check.scm
Normal file
108
tests/command-line/check.scm
Normal file
@@ -0,0 +1,108 @@
|
|||||||
|
;;; What sexc makes of its command line.
|
||||||
|
;;;
|
||||||
|
;;; Everything after the first `--' is the C compiler's, word for word;
|
||||||
|
;;; `-o -' is stdout; --depfile writes a make rule. Each shows only in
|
||||||
|
;;; what a sexc process hands on, so each is checked on one. `cc' here
|
||||||
|
;;; stands in for the compiler and writes down the arguments it got.
|
||||||
|
;;;
|
||||||
|
;;; make check-command-line
|
||||||
|
|
||||||
|
(import scheme
|
||||||
|
(chicken base)
|
||||||
|
(chicken file)
|
||||||
|
(chicken io)
|
||||||
|
(chicken pathname)
|
||||||
|
(chicken process)
|
||||||
|
(chicken process-context)
|
||||||
|
(chicken string)
|
||||||
|
srfi-1
|
||||||
|
test)
|
||||||
|
|
||||||
|
(define sexc (or (get-environment-variable "SEXC") "../../sexc"))
|
||||||
|
|
||||||
|
(define scratch (create-temporary-directory))
|
||||||
|
(define (built name) (make-pathname scratch name))
|
||||||
|
|
||||||
|
(define cc-arguments-file (built "cc-arguments"))
|
||||||
|
(set-environment-variable! "CC_ARGUMENTS" cc-arguments-file)
|
||||||
|
(define cc-option (string-append "--c-compiler=" (make-pathname (current-directory) "cc")))
|
||||||
|
|
||||||
|
;;; The lines sexc prints when run with ARGS and INPUT on its stdin, or
|
||||||
|
;;; #f when it fails
|
||||||
|
(define (output-with-input input . args)
|
||||||
|
(let ((proc (process sexc args)))
|
||||||
|
(display input (process-input-port proc))
|
||||||
|
(close-output-port (process-input-port proc))
|
||||||
|
(let ((lines (read-lines (process-output-port proc))))
|
||||||
|
(process-wait proc)
|
||||||
|
(and (process-returned-normally? proc)
|
||||||
|
(zero? (process-exit-status proc))
|
||||||
|
lines))))
|
||||||
|
|
||||||
|
(define (output-of . args)
|
||||||
|
(apply output-with-input "" args))
|
||||||
|
|
||||||
|
(define (cc-arguments)
|
||||||
|
(call-with-input-file cc-arguments-file read-lines))
|
||||||
|
|
||||||
|
;;; ("-o" "a" "/tmp/x.c" "-lm") -> ("-lm")
|
||||||
|
(define (past-c-file arguments)
|
||||||
|
(cdr (find-tail (lambda (a) (equal? (pathname-extension a) "c")) arguments)))
|
||||||
|
|
||||||
|
;;; What the compiler got past sexc's own -o and .c file, when sexc
|
||||||
|
;;; compiles with ARGS
|
||||||
|
(define (cc-tail . args)
|
||||||
|
(and (apply output-of cc-option args)
|
||||||
|
(past-c-file (cc-arguments))))
|
||||||
|
|
||||||
|
(define hello (string-intersperse (call-with-input-file "hello.sex" read-lines) "\n"))
|
||||||
|
|
||||||
|
(test-group "--"
|
||||||
|
;; `OpenGL' has no dash, and was once dropped for it, leaving
|
||||||
|
;; -framework to swallow whatever came next
|
||||||
|
(test "what follows reaches the compiler as written"
|
||||||
|
'("-framework" "OpenGL" "-Wall")
|
||||||
|
(cc-tail "hello.sex" "-o" (built "a") "--" "-framework" "OpenGL" "-Wall"))
|
||||||
|
(test "what precedes stays with sexc"
|
||||||
|
(list "-o" (built "a"))
|
||||||
|
(begin (cc-tail "hello.sex" "-o" (built "a") "--" "-lm")
|
||||||
|
(take (cc-arguments) 2)))
|
||||||
|
(test "without one the compiler gets nothing more"
|
||||||
|
'()
|
||||||
|
(cc-tail "hello.sex" "-o" (built "a")))
|
||||||
|
(test "nor after a trailing one"
|
||||||
|
'()
|
||||||
|
(cc-tail "hello.sex" "-o" (built "a") "--"))
|
||||||
|
;; ld takes a `--' of its own
|
||||||
|
(test "only the first one separates"
|
||||||
|
'("-Wl,--as-needed" "--" "-lm")
|
||||||
|
(cc-tail "hello.sex" "-o" (built "a") "--" "-Wl,--as-needed" "--" "-lm"))
|
||||||
|
(test "a file after it is the compiler's, and the source is stdin"
|
||||||
|
'("extra.o")
|
||||||
|
(and (output-with-input hello cc-option "-o" (built "a") "--" "extra.o")
|
||||||
|
(past-c-file (cc-arguments)))))
|
||||||
|
|
||||||
|
;;; getopt-long reads a lone `-' as an option, and refused `-o -'
|
||||||
|
(test-group "-o -"
|
||||||
|
(test-assert "writes the C to stdout"
|
||||||
|
(member "int main (void) {" (or (output-of "hello.sex" "-C" "-o" "-") '())))
|
||||||
|
(test-assert "and so does --output -"
|
||||||
|
(member "int main (void) {" (or (output-of "hello.sex" "-C" "--output" "-") '()))))
|
||||||
|
|
||||||
|
;;; The rules for the modules a unit imports are checked in tests/modules
|
||||||
|
(test-group "--depfile"
|
||||||
|
(copy-file "hello.sex" (built "a b.sex"))
|
||||||
|
(test "a space is escaped, the way make reads it"
|
||||||
|
(list (string-append (built "a\\ b.c") ": " (built "a\\ b.sex")))
|
||||||
|
(and (output-of (built "a b.sex") "-C" "-o" (built "a b.c")
|
||||||
|
(string-append "--depfile=" (built "a b.d")))
|
||||||
|
(call-with-input-file (built "a b.d") read-lines)))
|
||||||
|
(test "stdin is no file to depend on"
|
||||||
|
(list (string-append (built "hello.c") ":"))
|
||||||
|
(and (output-with-input hello "-C" "-o" (built "hello.c")
|
||||||
|
(string-append "--depfile=" (built "hello.d")))
|
||||||
|
(call-with-input-file (built "hello.d") read-lines))))
|
||||||
|
|
||||||
|
(delete-directory scratch #t)
|
||||||
|
|
||||||
|
(test-exit)
|
||||||
2
tests/command-line/hello.sex
Normal file
2
tests/command-line/hello.sex
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
(pub fn main () int
|
||||||
|
(return 0))
|
||||||
@@ -5,8 +5,7 @@
|
|||||||
;;; is checked here is what the compiler refuses, and that it refuses
|
;;; is checked here is what the compiler refuses, and that it refuses
|
||||||
;;; it with the name of whatever is wrong.
|
;;; it with the name of whatever is wrong.
|
||||||
;;;
|
;;;
|
||||||
;;; `sex->c', `read-source', `fails-with?', `emits?', `reports?'
|
;;; `in-fn' comes from codegen.scm, which run.scm includes first.
|
||||||
;;; and `in-fn' come from codegen.scm, which run.scm includes first.
|
|
||||||
|
|
||||||
(import sex-modules)
|
(import sex-modules)
|
||||||
|
|
||||||
|
|||||||
@@ -11,7 +11,7 @@
|
|||||||
;;; Everything is built inside a scratch directory, so nothing is left
|
;;; Everything is built inside a scratch directory, so nothing is left
|
||||||
;;; here to clean up.
|
;;; here to clean up.
|
||||||
;;;
|
;;;
|
||||||
;;; csi -s check.scm
|
;;; make check-exit-code
|
||||||
|
|
||||||
(import scheme
|
(import scheme
|
||||||
(chicken base)
|
(chicken base)
|
||||||
@@ -23,9 +23,11 @@
|
|||||||
(chicken string)
|
(chicken string)
|
||||||
test)
|
test)
|
||||||
|
|
||||||
|
;;; A path stays good past the change of directory below; a bare name
|
||||||
|
;;; is looked up on PATH
|
||||||
(define sexc
|
(define sexc
|
||||||
(let ((path (or (get-environment-variable "SEXC") "../../sexc")))
|
(let ((path (or (get-environment-variable "SEXC") "../../sexc")))
|
||||||
(if (absolute-pathname? path)
|
(if (or (absolute-pathname? path) (not (substring-index "/" path)))
|
||||||
path
|
path
|
||||||
(make-pathname (current-directory) path))))
|
(make-pathname (current-directory) path))))
|
||||||
|
|
||||||
@@ -55,12 +57,13 @@
|
|||||||
|
|
||||||
;;; Whether sexc exits 0 when run with ARGS, saying nothing of it
|
;;; Whether sexc exits 0 when run with ARGS, saying nothing of it
|
||||||
(define (sexc-succeeds? . args)
|
(define (sexc-succeeds? . args)
|
||||||
(let ((proc (process* sexc args)))
|
(zero? (system (string-append (string-intersperse (map qs (cons sexc args)))
|
||||||
(read-string #f (process-output-port proc))
|
" >/dev/null 2>&1"))))
|
||||||
(read-string #f (process-error-port proc))
|
|
||||||
(process-wait proc)
|
;;; Every check below expects a failure, which a sexc that cannot run
|
||||||
(and (process-returned-normally? proc)
|
;;; gives too
|
||||||
(zero? (process-exit-status proc)))))
|
(test "sexc compiles a program" #t
|
||||||
|
(sexc-succeeds? "hello.sex" "-o" out))
|
||||||
|
|
||||||
(test "a failed emission leaves no temporary .c" '()
|
(test "a failed emission leaves no temporary .c" '()
|
||||||
(begin
|
(begin
|
||||||
|
|||||||
@@ -1,192 +1,132 @@
|
|||||||
;;; Types
|
;;; The C each declaration comes out as: names, types, variables,
|
||||||
(test-group "fmt-writer"
|
;;; initializers, functions and structs. Non-pub ones are static, which
|
||||||
|
;;; the fragments leave out.
|
||||||
|
|
||||||
(test
|
(test-group "fmt-c-writer"
|
||||||
'(const int)
|
|
||||||
(walk-type '(const int)))
|
|
||||||
|
|
||||||
(test
|
(test-group "names"
|
||||||
'(%array (const char) 512)
|
(test-assert "kebab case is snake case"
|
||||||
(walk-type '(¤ const char 512)))
|
(emits? "(var kebab-case int)" "int kebab_case;"))
|
||||||
|
;; A Lisp predicate and mutator, in letters C takes
|
||||||
|
(test-assert "a predicate"
|
||||||
|
(emits? "(var ident-start? int)" "int ident_startʔ;"))
|
||||||
|
(test-assert "a mutator"
|
||||||
|
(emits? "(var reset! int)" "int resetꜝ;"))
|
||||||
|
;; The `regex' egg's string-substitute dropped one trailing character
|
||||||
|
;; per multi-byte one, and the C still compiled under the new name
|
||||||
|
(test-assert "a non-ASCII name survives"
|
||||||
|
(emits? "(var naïve-count int)" "int naïve_count;"))
|
||||||
|
(test-assert "wherever the dash is"
|
||||||
|
(emits? "(var aï-b int)" "int aï_b;"))
|
||||||
|
(test-assert "or with none"
|
||||||
|
(emits? "(var ïï int)" "int ïï;"))
|
||||||
|
(test-assert "an arrow inside a name is C's"
|
||||||
|
(emits? "(fn f () void (= this->member 1))" "this->member = 1;")))
|
||||||
|
|
||||||
(test
|
;; The operators spelled with the characters names are converted from
|
||||||
'(%array (float) 512)
|
(test-group "operators"
|
||||||
(walk-type '(¤ (float) 512)))
|
(test-assert "-="
|
||||||
|
(emits? "(fn f ((a int) (b int)) void (-= a b))" "a -= b;"))
|
||||||
|
(test-assert "!"
|
||||||
|
(emits? "(fn f ((a int)) void (if (! a) (return)))" "if (!a)"))
|
||||||
|
(test-assert "!="
|
||||||
|
(emits? "(fn f ((a int) (b int)) void (if (!= a b) (return)))" "if (a != b)")))
|
||||||
|
|
||||||
(test
|
(test-group "toplevel comments"
|
||||||
'(%array (const char) 512)
|
(test-assert "are C comments"
|
||||||
(walk-type '(¤ (const char) 512)))
|
(emits? ";; hi" "/* hi */"))
|
||||||
|
(test-assert "without the markers or the padding"
|
||||||
|
(emits? ";;; Foo " "/* Foo */")))
|
||||||
|
|
||||||
(test
|
(test-group "types"
|
||||||
'(%array (const char))
|
(test-assert "an array"
|
||||||
(walk-type '(¤ const char)))
|
(emits? "(var a [const char 512])" "const char a[512];"))
|
||||||
|
(test-assert "whose element type is grouped"
|
||||||
|
(emits? "(var c [(const char) 512])" "const char c[512];"))
|
||||||
|
(test-assert "an unsized one"
|
||||||
|
(emits? "(extern var d [const char])" "extern const char d[];"))
|
||||||
|
(test-assert "a pointer to const"
|
||||||
|
(emits? "(var p (* const char))" "const char * p;"))
|
||||||
|
(test-assert "a const pointer to const"
|
||||||
|
(emits? "(var q (const * const char))" "const char * const q;"))
|
||||||
|
(test-assert "a function pointer"
|
||||||
|
(emits? "(var fp (fn ((int) (float) [const * struct what]) void))"
|
||||||
|
"void (*fp)(int, float, struct what* const []);"))
|
||||||
|
(test-assert "taking an array"
|
||||||
|
(emits? "(var fq (fn ((int) [float]) void))"
|
||||||
|
"void (*fq)(int, float []);"))
|
||||||
|
(test-assert "an array of them"
|
||||||
|
(emits? "(extern var fa [(fn ((int) [float]) void)])"
|
||||||
|
"extern void (*fa[])(int, float []);")))
|
||||||
|
|
||||||
(test
|
(test-group "variables"
|
||||||
'(%array (const char))
|
(test-assert "a local array"
|
||||||
(walk-type '(¤ (const char))))
|
(emits? "(fn f () void (var a [float 8]))" "float a[8];"))
|
||||||
|
(test-assert "a pointer, initialized"
|
||||||
|
(emits? "(fn f () void (var n int) (var b (* int) (& n)))" "int * b = &n;"))
|
||||||
|
(test-assert "a pointer to const"
|
||||||
|
(emits? "(fn f () void (var n int) (var c (* const int) (& n)))"
|
||||||
|
"const int * c = &n;"))
|
||||||
|
(test-assert "a struct"
|
||||||
|
(emits? "(fn f () void (var s (struct suc)))" "struct suc s;"))
|
||||||
|
(test-assert "a pointer to a const struct"
|
||||||
|
(emits? "(fn f ((s1 (* const struct suc))) void (var t (* const struct suc) s1))"
|
||||||
|
"const struct suc * t = s1;"))
|
||||||
|
(test-assert "grouped"
|
||||||
|
(emits? "(fn f ((s1 (* const struct suc))) void (var u (* (const struct suc)) s1))"
|
||||||
|
"const struct suc * u = s1;"))
|
||||||
|
(test-assert "a struct from a call"
|
||||||
|
(emits? "(fn f () void (var v (struct suc) (hoge piyo)))"
|
||||||
|
"struct suc v = hoge(piyo);"))
|
||||||
|
(test-assert "a pointer to one"
|
||||||
|
(emits? "(fn f () void (var w (* struct suc) (hoge piyo)))"
|
||||||
|
"struct suc* w = hoge(piyo);")))
|
||||||
|
|
||||||
(test
|
(test-group "initializers"
|
||||||
'(%array float 8)
|
(test-assert "a bare one is braces"
|
||||||
(walk-type '(¤ float 8)))
|
(emits? "(fn f () void (var a [int 2] #(1 2)))" "int a[2] = {1, 2};"))
|
||||||
|
(test-assert "`:' ends the type and makes it a compound literal"
|
||||||
|
(emits? "(fn f () void (var p (struct point) #(struct point : 3 4)))"
|
||||||
|
"struct point p = (struct point){3, 4};"))
|
||||||
|
(test-assert "the type is bare words, as everywhere else"
|
||||||
|
(emits? "(fn f () void (var s (* const char) #(* const char : 65)))"
|
||||||
|
"const char * s = (const char *){65};"))
|
||||||
|
(test-assert "and may be an array type"
|
||||||
|
(emits? "(fn f () void (var t [int 3] #([int 3] : 10 20 30)))"
|
||||||
|
"int t[3] = (int [3]){10, 20, 30};"))
|
||||||
|
(test-assert "`.field value' is a designated initializer, kebab and all"
|
||||||
|
(emits? "(fn f () void (var n (struct named) #(struct named : .n 7 .first-name \"zoe\")))"
|
||||||
|
"(struct named){.n = 7, .first_name = \"zoe\"}"))
|
||||||
|
(test-assert "positional and designated may be mixed"
|
||||||
|
(emits? "(fn f () void (var m (struct point) #(struct point : 1 .y 5)))"
|
||||||
|
"(struct point){1, .y = 5}"))
|
||||||
|
(test-assert "designators work in an untyped initializer too"
|
||||||
|
(emits? "(fn f () void (var o (struct point) #(.y 5)))"
|
||||||
|
"struct point o = {.y = 5};")))
|
||||||
|
|
||||||
(test
|
(test-group "functions"
|
||||||
"Pointer to const char"
|
(test-assert "a prototype may leave its parameters nameless"
|
||||||
'(const char *)
|
(emits? "(fn puk ((int) [float 8]) void)" "void puk (int, float [8]);"))
|
||||||
(walk-type '(* const char)))
|
(test-assert "an array parameter"
|
||||||
|
(emits? "(fn g ((argc int) (argv [const char])) int (return 0))"
|
||||||
|
"int g (int argc, const char argv[]) {"))
|
||||||
|
(test-assert "a pointer parameter"
|
||||||
|
(emits? "(fn quxu ((bar (* struct piq))) int (return 0))"
|
||||||
|
"int quxu (struct piq * bar) {"))
|
||||||
|
(test-assert "an array of pointers"
|
||||||
|
(emits? "(fn quxu ((bar (* struct piq)) (ploop [* struct tt])) int (return 0))"
|
||||||
|
"int quxu (struct piq * bar, struct tt* ploop[]) {")))
|
||||||
|
|
||||||
(test
|
(test-group "structs"
|
||||||
"Const pointer to const char"
|
(test-assert "names are converted"
|
||||||
'(const char * const)
|
(emits? "(struct no-kebab ((a int) (f float)))"
|
||||||
(walk-type '(const * const char)))
|
"struct no_kebab {\n int a;\n float f;\n};"))
|
||||||
|
(test-assert "fields sharing a type share a line, and a field may be a struct"
|
||||||
(test
|
(emits? "(struct settings ((x y w h u32) (colors [struct ((r g b a float)) 4])))"
|
||||||
'(%fun void (int float (%array (struct what * const))))
|
"struct settings {\n u32 x, y, w, h;\n struct{\n float r, g, b, a;\n } colors[4];\n};"))
|
||||||
(walk-type '(fn ((int) (float) (¤ (const * struct what))) void)))
|
(test-assert "a named one"
|
||||||
|
(emits? "(struct settings ((x y w h u32) (colors [struct color ((r g b a float)) 4])))"
|
||||||
(test
|
" struct color{\n float r, g, b, a;\n } colors[4];"))
|
||||||
'(%fun void (int (%array float) (%array (struct what * const))))
|
(test-assert "or a function pointer"
|
||||||
(walk-type '(fn ((int) (¤ float) (¤ (const * struct what))) void)))
|
(emits? "(struct mega-kebab ((a int) (min (fn ((int) [int]) bool))))"
|
||||||
|
" bool (*min)(int, int []);"))))
|
||||||
(test
|
|
||||||
'(%array (%fun void (int (%array float) (%array (struct what * const)))))
|
|
||||||
(walk-type '(¤ (fn ((int) (¤ float) (¤ (const * struct what))) void))))
|
|
||||||
|
|
||||||
;; Type convert to C
|
|
||||||
(test
|
|
||||||
'(int)
|
|
||||||
(type-convert-to-c '(int)))
|
|
||||||
|
|
||||||
(test
|
|
||||||
'(* int)
|
|
||||||
(type-convert-to-c '(int *)))
|
|
||||||
|
|
||||||
(test
|
|
||||||
'(* const int)
|
|
||||||
(type-convert-to-c '(const int *)))
|
|
||||||
|
|
||||||
(test
|
|
||||||
'(const * const char)
|
|
||||||
(type-convert-to-c '(const char * const)))
|
|
||||||
|
|
||||||
;;; Variable defs
|
|
||||||
(test
|
|
||||||
'(%var (%array float 8) a)
|
|
||||||
(walk-var '(var a (¤ float 8))))
|
|
||||||
|
|
||||||
(test
|
|
||||||
'(%var (int *) a (& n))
|
|
||||||
(walk-var '(var a (* int) (& n))))
|
|
||||||
|
|
||||||
(test
|
|
||||||
'(%var (const int *) a (& n))
|
|
||||||
(walk-var '(var a (* const int) (& n))))
|
|
||||||
|
|
||||||
(test
|
|
||||||
'(%var (struct suc) s)
|
|
||||||
(walk-var '(var s (struct suc))))
|
|
||||||
|
|
||||||
(test
|
|
||||||
'(%var (const struct suc *) s s1)
|
|
||||||
(walk-var '(var s (* const struct suc) s1)))
|
|
||||||
|
|
||||||
(test
|
|
||||||
'(%var (const struct suc *) s s1)
|
|
||||||
(walk-var '(var s (* (const struct suc)) s1)))
|
|
||||||
|
|
||||||
(test
|
|
||||||
'(%var (struct suc) s (hoge piyo))
|
|
||||||
(walk-var '(var s (struct suc) (hoge piyo))))
|
|
||||||
|
|
||||||
(test
|
|
||||||
'(%var (struct suc *) s (hoge piyo))
|
|
||||||
(walk-var '(var s (* struct suc) (hoge piyo))))
|
|
||||||
|
|
||||||
;;; Initializers and compound literals
|
|
||||||
(test "a bare initializer is unchanged"
|
|
||||||
'#(1 2)
|
|
||||||
(walk-expr '#(1 2)))
|
|
||||||
|
|
||||||
(test "`:' ends the type and makes it a compound literal"
|
|
||||||
'(%compound (struct point) 3 4)
|
|
||||||
(walk-expr '#(struct point : 3 4)))
|
|
||||||
|
|
||||||
(test "the type is bare words, as everywhere else"
|
|
||||||
'(%compound (const char *) 65)
|
|
||||||
(walk-expr '#(* const char : 65)))
|
|
||||||
|
|
||||||
(test "and may be an array type"
|
|
||||||
'(%compound (%array int 3) 10 20 30)
|
|
||||||
(walk-expr '#(¤ int 3 : 10 20 30)))
|
|
||||||
|
|
||||||
(test "a grouped type is unwrapped the way a declaration's is"
|
|
||||||
'(%compound (%array int 3) 10)
|
|
||||||
(walk-expr '#((¤ int 3) : 10)))
|
|
||||||
|
|
||||||
(test "`.field value' is a designated initializer, kebab and all"
|
|
||||||
'(%compound (struct named) (%designate n 7) (%designate first_name "zoe"))
|
|
||||||
(walk-expr '#(struct named : .n 7 .first-name "zoe")))
|
|
||||||
|
|
||||||
(test "positional and designated may be mixed"
|
|
||||||
'(%compound (struct point) 1 (%designate y 5))
|
|
||||||
(walk-expr '#(struct point : 1 .y 5)))
|
|
||||||
|
|
||||||
(test "designators work in an untyped initializer too"
|
|
||||||
'#((%designate y 5))
|
|
||||||
(walk-expr '#(.y 5)))
|
|
||||||
|
|
||||||
;;; Fn defs
|
|
||||||
(test
|
|
||||||
'(%fun void puk ((int #f) ((%array float 8) #f)))
|
|
||||||
(walk-fn-def '(fn puk ((int) (¤ float 8)) void)))
|
|
||||||
|
|
||||||
(test
|
|
||||||
'(%fun int main ((int argc) ((%array (const char)) argv))
|
|
||||||
(return 0))
|
|
||||||
(walk-fn-def
|
|
||||||
'(fn main ((argc int) (argv (¤ const char))) int
|
|
||||||
(return 0))))
|
|
||||||
(test
|
|
||||||
'(%fun int quxu (((struct piq *) bar))
|
|
||||||
(return 0))
|
|
||||||
(walk-fn-def
|
|
||||||
'(fn quxu ((bar (* struct piq))) int
|
|
||||||
(return 0))))
|
|
||||||
|
|
||||||
(test
|
|
||||||
'(%fun int quxu (((struct piq *) bar) ((%array (struct tt *)) ploop))
|
|
||||||
(return 0))
|
|
||||||
(walk-fn-def
|
|
||||||
'(fn quxu ((bar (* struct piq)) (ploop (¤ * struct tt))) int
|
|
||||||
(return 0))))
|
|
||||||
|
|
||||||
;; Structs
|
|
||||||
(test
|
|
||||||
'(struct no_kebab ((int a) (float f)))
|
|
||||||
(walk-struct '(struct no-kebab ((a int) (f float)))))
|
|
||||||
|
|
||||||
(test
|
|
||||||
'(struct settings ((u32 x y w h)
|
|
||||||
((%array (struct ((float r g b a))) 4) colors)))
|
|
||||||
(walk-struct
|
|
||||||
'(struct settings
|
|
||||||
((x y w h u32)
|
|
||||||
(colors [¤ struct ((r g b a float)) 4])))))
|
|
||||||
|
|
||||||
(test
|
|
||||||
'(struct settings ((u32 x y w h)
|
|
||||||
((%array (struct color ((float r g b a))) 4) colors)))
|
|
||||||
(walk-struct
|
|
||||||
'(struct settings
|
|
||||||
((x y w h u32)
|
|
||||||
(colors [¤ struct color ((r g b a float)) 4])))))
|
|
||||||
|
|
||||||
(test
|
|
||||||
'(struct mega_kebab ((int a)
|
|
||||||
((struct ((int year) (int month) (int day))) dob)
|
|
||||||
((%fun bool (int (%array int))) min)))
|
|
||||||
(walk-struct '(struct mega-kebab
|
|
||||||
((a int)
|
|
||||||
(dob (struct ((year int)
|
|
||||||
(month int)
|
|
||||||
(day int))))
|
|
||||||
(min (fn ((int) (¤ int)) bool)))))))
|
|
||||||
|
|||||||
190
tests/infer.scm
190
tests/infer.scm
@@ -3,7 +3,7 @@
|
|||||||
;;; Names registered in the type database are prefixed, since the
|
;;; Names registered in the type database are prefixed, since the
|
||||||
;;; database is one table shared by every suite in the linked binary.
|
;;; database is one table shared by every suite in the linked binary.
|
||||||
|
|
||||||
(import infer types (chicken sort))
|
(import infer types)
|
||||||
|
|
||||||
;;; Parse and print a surface type again. Everything in this suite goes
|
;;; Parse and print a surface type again. Everything in this suite goes
|
||||||
;;; through this pair, which is deliberate: they are the only thing the
|
;;; through this pair, which is deliberate: they are the only thing the
|
||||||
@@ -85,23 +85,10 @@
|
|||||||
|
|
||||||
;; Each _ is its own variable: solving one must not solve the rest.
|
;; Each _ is its own variable: solving one must not solve the rest.
|
||||||
(let ((t (parse-type '(fn ((_)) _))))
|
(let ((t (parse-type '(fn ((_)) _))))
|
||||||
(unify (car (fn-args t)) (parse-type 'int) #f)
|
(unify t (parse-type '(fn ((int)) _)) #f)
|
||||||
(test "one hole at a time" '(fn ((int)) _) (unparse-type t))))
|
(test "one hole at a time" '(fn ((int)) _) (unparse-type t))))
|
||||||
|
|
||||||
(test-group "structure"
|
(test-group "decay"
|
||||||
(test-assert "a pointer is a pointer" (ptr-type? (parse-type '(* char))))
|
|
||||||
(test "and knows what it points at" 'char
|
|
||||||
(unparse-type (ptr-target (parse-type '(* char)))))
|
|
||||||
(test "quals sit on the level they were written at" '(const)
|
|
||||||
(ptr-quals (parse-type '(const * char))))
|
|
||||||
(test "an unsized array has no size" #f (array-size (parse-type '(¤ int))))
|
|
||||||
(test "a sized one does" 16 (array-size (parse-type '(¤ int 16))))
|
|
||||||
(test "an aggregate is nominal" 'point (agg-name (parse-type '(struct point))))
|
|
||||||
(test-assert "a variadic signature says so"
|
|
||||||
(fn-variadic? (parse-type '(fn ((* const char) (...)) int))))
|
|
||||||
(test-assert "and a plain one does not"
|
|
||||||
(not (fn-variadic? (parse-type '(fn ((int)) int)))))
|
|
||||||
|
|
||||||
;; decay: the conversion C performs at a call site, an operand of
|
;; decay: the conversion C performs at a call site, an operand of
|
||||||
;; `+', or the left half of a subscript.
|
;; `+', or the left half of a subscript.
|
||||||
(test "an array decays to a pointer" '(* int) (unparse-type (decay (parse-type '(¤ int 16)))))
|
(test "an array decays to a pointer" '(* int) (unparse-type (decay (parse-type '(¤ int 16)))))
|
||||||
@@ -115,7 +102,7 @@
|
|||||||
(test-error "and not with another one"
|
(test-error "and not with another one"
|
||||||
(unify (parse-type 'int) (parse-type 'char) #f))
|
(unify (parse-type 'int) (parse-type 'char) #f))
|
||||||
|
|
||||||
(let ((a (fresh-tvar)))
|
(let ((a (parse-type '_)))
|
||||||
(unify a (parse-type '(* const char)) #f)
|
(unify a (parse-type '(* const char)) #f)
|
||||||
(test "a variable takes the shape it is unified with"
|
(test "a variable takes the shape it is unified with"
|
||||||
'(* const char) (unparse-type a)))
|
'(* const char) (unparse-type a)))
|
||||||
@@ -125,8 +112,8 @@
|
|||||||
(unify p (parse-type '(* int)) #f)
|
(unify p (parse-type '(* int)) #f)
|
||||||
(test "a partial type is completed by one step" '(* int) (unparse-type p)))
|
(test "a partial type is completed by one step" '(* int) (unparse-type p)))
|
||||||
|
|
||||||
(let ((a (fresh-tvar))
|
(let ((a (parse-type '_))
|
||||||
(b (fresh-tvar)))
|
(b (parse-type '_)))
|
||||||
(unify a b #f)
|
(unify a b #f)
|
||||||
(unify b (parse-type 'double) #f)
|
(unify b (parse-type 'double) #f)
|
||||||
(test "two variables joined then solved" 'double (unparse-type a)))
|
(test "two variables joined then solved" 'double (unparse-type a)))
|
||||||
@@ -152,168 +139,7 @@
|
|||||||
(add-typedef 'i-count '(typedef i-count int))
|
(add-typedef 'i-count '(typedef i-count int))
|
||||||
(test-assert "a typedef unifies with its target"
|
(test-assert "a typedef unifies with its target"
|
||||||
(unify (parse-type 'i-count) (parse-type 'int) #f))
|
(unify (parse-type 'i-count) (parse-type 'int) #f))
|
||||||
(let ((a (fresh-tvar)))
|
(let ((a (parse-type '_)))
|
||||||
(unify a (parse-type 'i-count) #f)
|
(unify a (parse-type 'i-count) #f)
|
||||||
(test "and keeps its name when it is the one printed"
|
(test "and keeps its name when it is the one printed"
|
||||||
'i-count (unparse-type a)))
|
'i-count (unparse-type a)))))
|
||||||
|
|
||||||
(test-group "the unknown type"
|
|
||||||
(test-assert "? is consistent with anything"
|
|
||||||
(unify the-unknown-type (parse-type '(struct point)) #f))
|
|
||||||
(test-assert "in either order"
|
|
||||||
(unify (parse-type 'int) the-unknown-type #f))
|
|
||||||
;; ...and binds nothing. Degrading to ? is what keeps an
|
|
||||||
;; unparsed C declaration from poisoning everything it touches.
|
|
||||||
(let ((a (fresh-tvar)))
|
|
||||||
(unify a the-unknown-type #f)
|
|
||||||
(test "a variable met with ? stays open" '_ (unparse-type a))))
|
|
||||||
|
|
||||||
(test-group "occurs check"
|
|
||||||
;; Unreachable without recursive types, and the alternative to
|
|
||||||
;; having it is not an error but a hang.
|
|
||||||
(let ((a (fresh-tvar)))
|
|
||||||
(test-error "a variable may not contain itself"
|
|
||||||
(unify a (make-ptr a (list)) #f))))
|
|
||||||
|
|
||||||
(test-group "rigid variables"
|
|
||||||
(let ((r (fresh-rigid-tvar))
|
|
||||||
(a (fresh-tvar)))
|
|
||||||
(test-error "a type parameter does not unify with a type"
|
|
||||||
(unify r (parse-type 'int) #f))
|
|
||||||
(test-assert "an ordinary variable binds to it instead"
|
|
||||||
(unify a r #f))
|
|
||||||
;; An unsolved variable resolves to itself.
|
|
||||||
(test-assert "it is still open" (tvar? (resolve r))))
|
|
||||||
;; ...and the same the other way round: it is which side is free
|
|
||||||
;; that decides, not which side was written first.
|
|
||||||
(let ((r (fresh-rigid-tvar))
|
|
||||||
(a (fresh-tvar)))
|
|
||||||
(test-assert "rigid first binds the free one" (unify r a #f))
|
|
||||||
(test-assert "to the parameter itself" (eq? r (resolve a))))
|
|
||||||
(let ((r1 (fresh-rigid-tvar))
|
|
||||||
(r2 (fresh-rigid-tvar)))
|
|
||||||
(test-error "two parameters do not unify with each other"
|
|
||||||
(unify r1 r2 #f)))))
|
|
||||||
|
|
||||||
(test-group "constraints"
|
|
||||||
(test #t (entails? 'numeric (parse-type 'int)))
|
|
||||||
(test #t (entails? 'numeric (parse-type '(unsigned long))))
|
|
||||||
(test #t (entails? 'integral (parse-type 'char)))
|
|
||||||
(test #f (entails? 'integral (parse-type 'double)))
|
|
||||||
(test #t (entails? 'floating (parse-type 'double)))
|
|
||||||
(test #f (entails? 'floating (parse-type 'int)))
|
|
||||||
(test #f (entails? 'numeric (parse-type '(* char))))
|
|
||||||
(test #t (entails? 'scalar (parse-type '(* char))))
|
|
||||||
(test #f (entails? 'numeric (parse-type 'void)))
|
|
||||||
|
|
||||||
(add-enum 'i-mood '(enum i-mood (glad sad)))
|
|
||||||
(test "an enum is an integer" #t (entails? 'integral (parse-type '(enum i-mood))))
|
|
||||||
|
|
||||||
;; The third answer, and the important one. A name from a header
|
|
||||||
;; might well be numeric; #f would reject working programs and #t
|
|
||||||
;; would invent knowledge.
|
|
||||||
(test "an unparsed C name is not known either way"
|
|
||||||
'unknown (entails? 'numeric (parse-type 'size-t)))
|
|
||||||
(test "nor is an open variable" 'unknown (entails? 'numeric (fresh-tvar)))
|
|
||||||
(test "? entails nothing, but says so quietly"
|
|
||||||
'unknown (entails? 'numeric the-unknown-type))
|
|
||||||
|
|
||||||
;; A typedef is entailed by what it resolves to, so an alias cannot
|
|
||||||
;; sneak past a constraint its target would fail.
|
|
||||||
(add-typedef 'i-len '(typedef i-len int))
|
|
||||||
(test "a typedef is judged by its target" #t (entails? 'numeric (parse-type 'i-len)))
|
|
||||||
|
|
||||||
;; A constrained variable checks its classes at the moment it is
|
|
||||||
;; solved, not at the end.
|
|
||||||
(let ((a (fresh-tvar '(numeric))))
|
|
||||||
(test-error "solving to a type that fails the class is an error"
|
|
||||||
(unify a (parse-type '(* char)) #f)))
|
|
||||||
(let ((a (fresh-tvar '(numeric))))
|
|
||||||
(test-assert "and to one that satisfies it is not"
|
|
||||||
(unify a (parse-type 'double) #f)))
|
|
||||||
;; ...but an unparsed name is not a failure, it is an absence of
|
|
||||||
;; knowledge, and must stay silent.
|
|
||||||
(let ((a (fresh-tvar '(numeric))))
|
|
||||||
(test-assert "an unparsed C name does not trip a constraint"
|
|
||||||
(unify a (parse-type 'GLuint) #f)))
|
|
||||||
|
|
||||||
;; Joining two variables joins what is known about both.
|
|
||||||
(let ((a (fresh-tvar '(numeric)))
|
|
||||||
(b (fresh-tvar '(integral))))
|
|
||||||
(unify a b #f)
|
|
||||||
(test "constraints merge when variables do"
|
|
||||||
'("integral" "numeric")
|
|
||||||
(sort (map symbol->string (tvar-classes b)) string<?)))
|
|
||||||
|
|
||||||
(test-group "defaulting"
|
|
||||||
(let ((a (fresh-tvar '(numeric))))
|
|
||||||
(default-type-variables! a #f)
|
|
||||||
(test "an open numeric is an int" 'int (unparse-type a)))
|
|
||||||
(let ((a (fresh-tvar '(floating))))
|
|
||||||
(default-type-variables! a #f)
|
|
||||||
(test "an open floating is a double" 'double (unparse-type a)))
|
|
||||||
(let ((a (fresh-tvar)))
|
|
||||||
(test "a variable with nothing known about it cannot be defaulted"
|
|
||||||
#f (default-type-variables! a #f))
|
|
||||||
(test "and stays a hole, for the caller to complain about"
|
|
||||||
'_ (unparse-type a)))
|
|
||||||
;; Defaulting reaches into the structure, since the hole may be
|
|
||||||
;; anywhere: `(var p (* _) ...)'.
|
|
||||||
(let ((t (parse-type '(* _))))
|
|
||||||
(unify (ptr-target t) (fresh-tvar '(numeric)) #f)
|
|
||||||
(default-type-variables! t #f)
|
|
||||||
(test "and it reaches inside a type" '(* int) (unparse-type t))))
|
|
||||||
|
|
||||||
(test-group "user classes"
|
|
||||||
;; A trait bound is the same shape as `numeric', discharged by
|
|
||||||
;; the same procedure -- that is the point of one representation.
|
|
||||||
;; It differs in two rules, and both are stated while there is
|
|
||||||
;; still only one kind of constraint to state them about.
|
|
||||||
(register-class! 'i-ord #f #f #t)
|
|
||||||
(add-struct 'i-circle '(struct i-circle ((r int))))
|
|
||||||
(test "no instance, no entailment" #f (entails? 'i-ord (parse-type '(struct i-circle))))
|
|
||||||
(add-instance! 'i-ord (parse-type '(struct i-circle)))
|
|
||||||
(test "and with one, entailment" #t (entails? 'i-ord (parse-type '(struct i-circle))))
|
|
||||||
;; Instances key on the resolved type, so an alias cannot be
|
|
||||||
;; registered twice under two names.
|
|
||||||
(add-typedef 'i-circle-alias '(typedef i-circle-alias (struct i-circle)))
|
|
||||||
(test "an alias of an instance is the same instance"
|
|
||||||
#t (entails? 'i-ord (parse-type 'i-circle-alias)))
|
|
||||||
;; Decision 2: static dispatch cannot select an instance for a
|
|
||||||
;; type it does not know, so a strict class says no to ? rather
|
|
||||||
;; than shrugging the way `numeric' does.
|
|
||||||
(test "a strict class refuses ?" #f (entails? 'i-ord the-unknown-type))
|
|
||||||
(test "where a lenient one abstains" 'unknown (entails? 'numeric the-unknown-type))
|
|
||||||
;; ...and it has no default to fall back on.
|
|
||||||
(let ((a (fresh-tvar '(i-ord))))
|
|
||||||
(test-error "an unresolved user constraint is an error, not a guess"
|
|
||||||
(default-type-variables! a #f)))))
|
|
||||||
|
|
||||||
(test-group "schemes"
|
|
||||||
;; Nothing generalizes yet. The shape is here because a scheme
|
|
||||||
;; without a constraint list is the wrong shape for every bounded
|
|
||||||
;; generic, and because instantiation is how a generic body gets
|
|
||||||
;; fresh cells instead of a second type for the same one.
|
|
||||||
(let* ((a (fresh-tvar '(numeric)))
|
|
||||||
(id (make-fn-type a (list a) #f))
|
|
||||||
(s (generalize id (list))))
|
|
||||||
(test "the free variable is quantified" 1 (length (scheme-vars s)))
|
|
||||||
(test "carrying its class as the scheme's context"
|
|
||||||
'((numeric)) (list (map car (scheme-constraints s))))
|
|
||||||
|
|
||||||
(let ((one (instantiate s))
|
|
||||||
(two (instantiate s)))
|
|
||||||
(test "an instantiation is still open" '(fn ((_)) _) (unparse-type one))
|
|
||||||
(unify (fn-ret one) (parse-type 'int) #f)
|
|
||||||
(test "solving one instantiation" '(fn ((int)) int) (unparse-type one))
|
|
||||||
(test "leaves the other alone" '(fn ((_)) _) (unparse-type two))
|
|
||||||
(test "and the scheme itself untouched" '(fn ((_)) _) (unparse-type id))))
|
|
||||||
|
|
||||||
(let* ((a (fresh-tvar))
|
|
||||||
(b (fresh-tvar))
|
|
||||||
(s (generalize (make-fn-type a (list b) #f) (list b))))
|
|
||||||
(test "a variable free in the environment is not quantified"
|
|
||||||
1 (length (scheme-vars s)))
|
|
||||||
(let ((inst (instantiate s)))
|
|
||||||
(unify (car (fn-args inst)) (parse-type 'char) #f)
|
|
||||||
(test "so instantiating solves it for everyone" 'char (unparse-type b))))))
|
|
||||||
|
|||||||
@@ -17,14 +17,13 @@
|
|||||||
;;; get the true source line) and in the C output (to get the line the
|
;;; get the true source line) and in the C output (to get the line the
|
||||||
;;; directives claim). The two must agree.
|
;;; directives claim). The two must agree.
|
||||||
|
|
||||||
(import (chicken port)
|
(import (chicken pathname)
|
||||||
|
(chicken port)
|
||||||
(chicken string)
|
(chicken string)
|
||||||
srfi-1
|
srfi-1
|
||||||
srfi-13
|
srfi-13
|
||||||
fmt-c-writer
|
fmt-c-writer
|
||||||
reader
|
semen)
|
||||||
semen
|
|
||||||
utils)
|
|
||||||
|
|
||||||
;;; ------------------------------------------------------------------
|
;;; ------------------------------------------------------------------
|
||||||
;;; Fixture. Line numbers are in the trailing comments; keep them
|
;;; Fixture. Line numbers are in the trailing comments; keep them
|
||||||
@@ -98,15 +97,9 @@
|
|||||||
;;; Pipeline and #line accounting
|
;;; Pipeline and #line accounting
|
||||||
|
|
||||||
(define (compile-to-c source #!optional (file "fixture.sex"))
|
(define (compile-to-c source #!optional (file "fixture.sex"))
|
||||||
"Run reader -> semen -> writer on SOURCE, returning the generated C.
|
"Run reader -> semen -> writer on SOURCE, read from FILE,
|
||||||
parse-all is called directly rather than through read-raw-forms so the
|
returning the generated C."
|
||||||
fixture can be given a file name: a location carries one, and the
|
(let ((forms (read-source source file)))
|
||||||
file is what makes an imported module report itself rather than the unit
|
|
||||||
that imported it."
|
|
||||||
(let ((forms (with-input-from-string source
|
|
||||||
(lambda ()
|
|
||||||
(parameterize ((current-source-file file))
|
|
||||||
(parse-all (current-input-port)))))))
|
|
||||||
(with-output-to-string
|
(with-output-to-string
|
||||||
(lambda () (emit-c (semen-process forms))))))
|
(lambda () (emit-c (semen-process forms))))))
|
||||||
|
|
||||||
@@ -238,23 +231,26 @@ after that is one more. Lines before the first directive get #f."
|
|||||||
;; an `if' arm is a statement only where the `if' is one: a
|
;; an `if' arm is a statement only where the `if' is one: a
|
||||||
;; directive between `?' and `:' does not compile
|
;; directive between `?' and `:' does not compile
|
||||||
(maps "conditional argument" "123")
|
(maps "conditional argument" "123")
|
||||||
|
;; a bare 123 may be a digit run of the fixture's path, in every #line
|
||||||
(test-assert "holds no directive"
|
(test-assert "holds no directive"
|
||||||
(let ((start (string-contains c-out "123")))
|
(let ((start (string-contains c-out "mvar < 123")))
|
||||||
(not (string-contains (substring c-out start (string-contains c-out "125" start))
|
(not (string-contains (substring c-out start (string-contains c-out ": 125" start))
|
||||||
"#line")))))
|
"#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.
|
||||||
(test-group "file name"
|
(test-group "file name"
|
||||||
(test "every directive names the fixture"
|
(test "every directive names the fixture"
|
||||||
(list "\"fixture.sex\"")
|
(list "fixture.sex")
|
||||||
(delete-duplicates
|
(delete-duplicates
|
||||||
(filter values (map directive-file (split-lines c-out)))))
|
(map (lambda (quoted)
|
||||||
|
(pathname-strip-directory (string-trim-both quoted #\")))
|
||||||
|
(filter values (map directive-file (split-lines c-out))))))
|
||||||
;; it was pasted between quotes as it came: a quote ended it, a
|
;; it was pasted between quotes as it came: a quote ended it, a
|
||||||
;; backslash began an escape, and ??= was a trigraph
|
;; backslash began an escape, and ??= was a trigraph
|
||||||
(test "and is written the way C writes a string"
|
(test-assert "and is written the way C writes a string"
|
||||||
"\"we\\\"ird\\\\dir/a?\\?=.sex\""
|
(string-suffix? "/we\\\"ird\\\\a?\\?=.sex\""
|
||||||
(directive-file
|
(directive-file
|
||||||
(find directive-line
|
(find directive-line
|
||||||
(split-lines (compile-to-c "(fn f () int (return 1))"
|
(split-lines (compile-to-c "(fn f () int (return 1))"
|
||||||
"we\"ird\\dir/a??=.sex")))))))
|
"we\"ird\\a??=.sex"))))))))
|
||||||
|
|||||||
@@ -1,22 +1,38 @@
|
|||||||
;;; Where imports are looked for. SEX_MODULE_PATH is read once, at the
|
;;; Where imports are looked for. SEX_MODULE_PATH is read once, at the
|
||||||
;;; start, from the directory sexc was started in.
|
;;; start, from the directory sexc was started in.
|
||||||
|
|
||||||
(import (chicken pathname)
|
(import (chicken file)
|
||||||
|
(chicken pathname)
|
||||||
(chicken process-context)
|
(chicken process-context)
|
||||||
|
semen
|
||||||
sex-modules)
|
sex-modules)
|
||||||
|
|
||||||
|
(define (write-module dir name)
|
||||||
|
(create-directory dir #t)
|
||||||
|
(with-output-to-file (make-pathname dir name "sex")
|
||||||
|
(lambda () (display "(pub fn found () int)"))))
|
||||||
|
|
||||||
(test-group "module path"
|
(test-group "module path"
|
||||||
;; make-absolute-pathname took `src' for a directory under the root,
|
;; make-absolute-pathname took `src' for a directory under the root,
|
||||||
;; and an import searched /src/
|
;; and an import searched /src/
|
||||||
(set-environment-variable! "SEX_MODULE_PATH" "src:/abs/lib")
|
(let ((start (current-directory))
|
||||||
(load-persistent-module-paths)
|
(scratch (create-temporary-directory)))
|
||||||
(test "a relative entry is from the working directory"
|
(write-module (make-pathname scratch "src") "mp-relative")
|
||||||
(make-absolute-pathname (current-directory) "src")
|
(write-module (make-pathname scratch "lib") "mp-absolute")
|
||||||
(second (get-module-paths)))
|
(change-directory scratch)
|
||||||
(test "an absolute one is as written"
|
(set-environment-variable! "SEX_MODULE_PATH"
|
||||||
"/abs/lib"
|
(string-append "src:" (make-pathname scratch "lib")))
|
||||||
(third (get-module-paths)))
|
(load-persistent-module-paths)
|
||||||
|
(change-directory start)
|
||||||
|
|
||||||
(set-environment-variable! "SEX_MODULE_PATH" "")
|
(test "a relative entry is from the working directory"
|
||||||
(load-persistent-module-paths)
|
'((pub fn found () int))
|
||||||
(unset-environment-variable! "SEX_MODULE_PATH"))
|
(semen-process '((import mp-relative))))
|
||||||
|
(test "an absolute one is as written"
|
||||||
|
'((pub fn found () int))
|
||||||
|
(semen-process '((import mp-absolute))))
|
||||||
|
|
||||||
|
(set-environment-variable! "SEX_MODULE_PATH" "")
|
||||||
|
(load-persistent-module-paths)
|
||||||
|
(unset-environment-variable! "SEX_MODULE_PATH")
|
||||||
|
(delete-directory scratch #t)))
|
||||||
|
|||||||
@@ -38,14 +38,16 @@
|
|||||||
;;;
|
;;;
|
||||||
;;; gen/made writes its pub forms by macro calls, which are expanded when
|
;;; gen/made writes its pub forms by macro calls, which are expanded when
|
||||||
;;; its interface is read; the macros it used for that stay its own.
|
;;; its interface is read; the macros it used for that stay its own.
|
||||||
|
;;; gen/broken's call raises, which reading its interface reports.
|
||||||
;;;
|
;;;
|
||||||
;;; csi -s check.scm
|
;;; make check-modules
|
||||||
|
|
||||||
(import scheme
|
(import scheme
|
||||||
(chicken base)
|
(chicken base)
|
||||||
(chicken file)
|
(chicken file)
|
||||||
(chicken io)
|
(chicken io)
|
||||||
(chicken irregex)
|
(chicken irregex)
|
||||||
|
(chicken pathname)
|
||||||
(chicken process)
|
(chicken process)
|
||||||
(chicken process-context)
|
(chicken process-context)
|
||||||
srfi-1
|
srfi-1
|
||||||
@@ -53,6 +55,11 @@
|
|||||||
|
|
||||||
(define sexc (or (get-environment-variable "SEXC") "../../sexc"))
|
(define sexc (or (get-environment-variable "SEXC") "../../sexc"))
|
||||||
|
|
||||||
|
;;; Where the objects and the app go: not beside the sources, where what
|
||||||
|
;;; an earlier run left would pass for what this one did not build
|
||||||
|
(define scratch (create-temporary-directory))
|
||||||
|
(define (built name) (make-pathname scratch name))
|
||||||
|
|
||||||
;;; What PROGRAM run with ARGS writes to stderr when it fails, or #f
|
;;; What PROGRAM run with ARGS writes to stderr when it fails, or #f
|
||||||
(define (error-of program . args)
|
(define (error-of program . args)
|
||||||
(let* ((proc (process* program args))
|
(let* ((proc (process* program args))
|
||||||
@@ -75,36 +82,47 @@
|
|||||||
(take (or (output-of sexc "--public-interface" "greet.sex") '()) 3))
|
(take (or (output-of sexc "--public-interface" "greet.sex") '()) 3))
|
||||||
|
|
||||||
(test-assert "the app links with both modules"
|
(test-assert "the app links with both modules"
|
||||||
(and (output-of sexc "ledger.sex" "-c" "-o" "ledger.o")
|
(and (output-of sexc "ledger.sex" "-c" "-o" (built "ledger.o"))
|
||||||
(output-of sexc "greet.sex" "-c" "-o" "greet.o")
|
(output-of sexc "greet.sex" "-c" "-o" (built "greet.o"))
|
||||||
(output-of sexc "greet-app.sex" "-o" "greet-app" "--depfile=greet-app.d"
|
(output-of sexc "greet-app.sex" "-o" (built "greet-app")
|
||||||
"--" "greet.o" "ledger.o")))
|
(string-append "--depfile=" (built "greet-app.d"))
|
||||||
|
"--" (built "greet.o") (built "ledger.o"))))
|
||||||
|
|
||||||
(test-assert "the dependency rule names both modules"
|
(test-assert "the dependency rule names both modules"
|
||||||
(irregex-match "greet-app: greet-app\\.sex .*/greet\\.sex .*/ledger\\.sex"
|
(irregex-match ".*/greet-app: greet-app\\.sex .*/greet\\.sex .*/ledger\\.sex"
|
||||||
(call-with-input-file "greet-app.d" read-line)))
|
(call-with-input-file (built "greet-app.d") read-line)))
|
||||||
|
|
||||||
|
;;; Each module has a rule of its own, so make takes one deleted since as
|
||||||
|
;;; out of date rather than as a file it has no rule to make
|
||||||
|
(test "and gives each a rule of its own"
|
||||||
|
'("greet.sex:" "ledger.sex:")
|
||||||
|
(filter-map (lambda (line)
|
||||||
|
(and (irregex-match ".*\\.sex:" line) (pathname-strip-directory line)))
|
||||||
|
(call-with-input-file (built "greet-app.d") read-lines)))
|
||||||
|
|
||||||
(test "a module's interface does not depend on where sexc runs"
|
(test "a module's interface does not depend on where sexc runs"
|
||||||
'("(record spine)" "(import page)")
|
'("(record spine)" "(import page)")
|
||||||
(take (or (output-of sexc "--public-interface" "shelf/book.sex") '()) 2))
|
(take (or (output-of sexc "--public-interface" "shelf/book.sex") '()) 2))
|
||||||
|
|
||||||
(test-assert "a module in a directory finds its imports beside itself"
|
(test-assert "a module in a directory finds its imports beside itself"
|
||||||
(and (output-of sexc "shelf/page.sex" "-c" "-o" "page.o")
|
(and (output-of sexc "shelf/page.sex" "-c" "-o" (built "page.o"))
|
||||||
(output-of sexc "shelf/book.sex" "-c" "-o" "book.o")
|
(output-of sexc "shelf/book.sex" "-c" "-o" (built "book.o"))
|
||||||
(output-of sexc "shelf-app.sex" "-o" "shelf-app" "--" "book.o" "page.o")))
|
(output-of sexc "shelf-app.sex" "-o" (built "shelf-app")
|
||||||
|
"--" (built "book.o") (built "page.o"))))
|
||||||
|
|
||||||
(test "and the app runs through it"
|
(test "and the app runs through it"
|
||||||
'("pages 2 3" "number 2")
|
'("pages 2 3" "number 2")
|
||||||
(output-of "./shelf-app"))
|
(output-of (built "shelf-app")))
|
||||||
|
|
||||||
(test-assert "modules importing each other with pub compile"
|
(test-assert "modules importing each other with pub compile"
|
||||||
(and (output-of sexc "cycle/a.sex" "-c" "-o" "cycle-a.o")
|
(and (output-of sexc "cycle/a.sex" "-c" "-o" (built "cycle-a.o"))
|
||||||
(output-of sexc "cycle/b.sex" "-c" "-o" "cycle-b.o")
|
(output-of sexc "cycle/b.sex" "-c" "-o" (built "cycle-b.o"))
|
||||||
(output-of sexc "cycle-app.sex" "-o" "cycle-app" "--" "cycle-a.o" "cycle-b.o")))
|
(output-of sexc "cycle-app.sex" "-o" (built "cycle-app")
|
||||||
|
"--" (built "cycle-a.o") (built "cycle-b.o"))))
|
||||||
|
|
||||||
(test "and the app runs through them"
|
(test "and the app runs through them"
|
||||||
'("cycle 1 2")
|
'("cycle 1 2")
|
||||||
(output-of "./cycle-app"))
|
(output-of (built "cycle-app")))
|
||||||
|
|
||||||
;;; ...it lists pub forms a macro wrote, and not the macros that wrote them
|
;;; ...it lists pub forms a macro wrote, and not the macros that wrote them
|
||||||
(test "a module's interface holds what its macros wrote"
|
(test "a module's interface holds what its macros wrote"
|
||||||
@@ -113,12 +131,19 @@
|
|||||||
(output-of sexc "--public-interface" "gen/made.sex"))
|
(output-of sexc "--public-interface" "gen/made.sex"))
|
||||||
|
|
||||||
(test-assert "and an importer links with them"
|
(test-assert "and an importer links with them"
|
||||||
(and (output-of sexc "gen/made.sex" "-c" "-o" "made.o")
|
(and (output-of sexc "gen/made.sex" "-c" "-o" (built "made.o"))
|
||||||
(output-of sexc "made-app.sex" "-o" "made-app" "--" "made.o")))
|
(output-of sexc "made-app.sex" "-o" (built "made-app")
|
||||||
|
"--" (built "made.o"))))
|
||||||
|
|
||||||
(test "and calls them"
|
(test "and calls them"
|
||||||
'("made 42 7 1 2")
|
'("made 42 7 1 2")
|
||||||
(output-of "./made-app"))
|
(output-of (built "made-app")))
|
||||||
|
|
||||||
|
;;; ...but a call failing for other than a name its module declares fails
|
||||||
|
;;; there as well
|
||||||
|
(test-assert "a toplevel macro call that raises fails the interface too"
|
||||||
|
(let ((err (error-of sexc "--public-interface" "gen/broken.sex")))
|
||||||
|
(and err (irregex-search "broken\\.sex:8: expanding boom: nope" err))))
|
||||||
|
|
||||||
;;; ...while a realization of a spec importers cannot see is refused by
|
;;; ...while a realization of a spec importers cannot see is refused by
|
||||||
;;; the module that writes it, not by an importer naming it first
|
;;; the module that writes it, not by an importer naming it first
|
||||||
@@ -140,11 +165,8 @@
|
|||||||
"board 42"
|
"board 42"
|
||||||
"ledger 2"
|
"ledger 2"
|
||||||
"colour 123")
|
"colour 123")
|
||||||
(output-of "./greet-app"))
|
(output-of (built "greet-app")))
|
||||||
|
|
||||||
(for-each delete-file* '("greet.o" "ledger.o" "greet-app" "greet-app.d"
|
(delete-directory scratch #t)
|
||||||
"page.o" "book.o" "shelf-app"
|
|
||||||
"cycle-a.o" "cycle-b.o" "cycle-app"
|
|
||||||
"made.o" "made-app"))
|
|
||||||
|
|
||||||
(test-exit)
|
(test-exit)
|
||||||
|
|||||||
8
tests/modules/gen/broken.sex
Normal file
8
tests/modules/gen/broken.sex
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
;;; A toplevel macro call failing for what it is, not for asking after a
|
||||||
|
;;; name declared here: an importer is told so, as this module is
|
||||||
|
(defmacro (boom)
|
||||||
|
(error "nope"))
|
||||||
|
|
||||||
|
(pub fn fine () int (return 1))
|
||||||
|
|
||||||
|
(boom)
|
||||||
75
tests/pipeline.scm
Normal file
75
tests/pipeline.scm
Normal file
@@ -0,0 +1,75 @@
|
|||||||
|
;;; Sex source in, C or the error it raises out, the way sexc runs
|
||||||
|
;;; the pipeline: reader -> semen -> writer. The suites after this one
|
||||||
|
;;; check the compiler through these.
|
||||||
|
|
||||||
|
(import (chicken condition)
|
||||||
|
(chicken file)
|
||||||
|
(chicken pathname)
|
||||||
|
(chicken port)
|
||||||
|
srfi-13
|
||||||
|
fmt-c-writer
|
||||||
|
reader
|
||||||
|
semen)
|
||||||
|
|
||||||
|
(define (read-source source #!optional (name "codegen.sex"))
|
||||||
|
"The forms of SOURCE, read from a file NAME, which is what the
|
||||||
|
locations then name: (read-source \"(f)\") reads (f) from codegen.sex."
|
||||||
|
(let* ((dir (create-temporary-directory))
|
||||||
|
(file (make-pathname dir name)))
|
||||||
|
(with-output-to-file file (lambda () (display source)))
|
||||||
|
(let ((forms (read-from-file file)))
|
||||||
|
(delete-directory dir #t)
|
||||||
|
forms)))
|
||||||
|
|
||||||
|
(define (sex->c source)
|
||||||
|
"Compile SOURCE, a string of Sex, and return the generated C."
|
||||||
|
(let ((forms (read-source source)))
|
||||||
|
(with-output-to-string
|
||||||
|
(lambda ()
|
||||||
|
(parameterize ((sex-line-directives 'none))
|
||||||
|
(emit-c (semen-process forms)))))))
|
||||||
|
|
||||||
|
;;; Warnings are what `warns?' is for; a test that happens to trip one
|
||||||
|
;;; should not print it
|
||||||
|
(define (silently thunk)
|
||||||
|
"THUNK's value, with whatever it wrote to stderr dropped."
|
||||||
|
(let ((value #f))
|
||||||
|
(with-error-output-to-string (lambda () (set! value (thunk))))
|
||||||
|
value))
|
||||||
|
|
||||||
|
(define (emits? source fragment)
|
||||||
|
(and (string-contains (silently (lambda () (sex->c source))) fragment) #t))
|
||||||
|
|
||||||
|
(define (failure-message thunk)
|
||||||
|
"The error THUNK raises as the user sees it -- message plus arguments,
|
||||||
|
the way CHICKEN prints it -- or #f if it raises none."
|
||||||
|
(handle-exceptions e
|
||||||
|
(with-output-to-string
|
||||||
|
(lambda ()
|
||||||
|
(display ((condition-property-accessor 'exn 'message) e))
|
||||||
|
(for-each (lambda (a) (display " ") (write a))
|
||||||
|
((condition-property-accessor 'exn 'arguments) e))))
|
||||||
|
(begin (thunk) #f)))
|
||||||
|
|
||||||
|
(define (fails-with? thunk fragment)
|
||||||
|
(let ((m (failure-message thunk)))
|
||||||
|
(and m (string-contains m fragment) #t)))
|
||||||
|
|
||||||
|
(define (reports? source fragment)
|
||||||
|
(fails-with? (lambda () (sex->c source)) fragment))
|
||||||
|
|
||||||
|
(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 (warnings source)
|
||||||
|
"What compiling SOURCE writes to stderr."
|
||||||
|
(with-error-output-to-string (lambda () (sex->c source))))
|
||||||
|
|
||||||
|
(define (warns? source fragment)
|
||||||
|
(and (string-contains (warnings source) fragment) #t))
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
(import
|
(import
|
||||||
test)
|
test)
|
||||||
|
|
||||||
(include-relative "basic.scm")
|
(include-relative "pipeline.scm")
|
||||||
(include-relative "semen.scm")
|
(include-relative "semen.scm")
|
||||||
(include-relative "reader.scm")
|
(include-relative "reader.scm")
|
||||||
(include-relative "fmt-c-writer.scm")
|
(include-relative "fmt-c-writer.scm")
|
||||||
@@ -9,7 +9,6 @@
|
|||||||
(include-relative "line-directives.scm")
|
(include-relative "line-directives.scm")
|
||||||
(include-relative "codegen.scm")
|
(include-relative "codegen.scm")
|
||||||
(include-relative "encapsulation.scm")
|
(include-relative "encapsulation.scm")
|
||||||
(include-relative "args.scm")
|
|
||||||
(include-relative "module-path.scm")
|
(include-relative "module-path.scm")
|
||||||
(include-relative "types.scm")
|
(include-relative "types.scm")
|
||||||
(include-relative "infer.scm")
|
(include-relative "infer.scm")
|
||||||
|
|||||||
@@ -1,35 +1,8 @@
|
|||||||
(import srfi-69
|
(import semen
|
||||||
semen
|
|
||||||
types)
|
types)
|
||||||
|
|
||||||
(define print-str-fn
|
|
||||||
'(fn print-str ((s string)) void
|
|
||||||
(printf "%s" s)))
|
|
||||||
|
|
||||||
(define sum-fn
|
|
||||||
'(pub fn sum ((a int) (b int)) float
|
|
||||||
(return (cast (+ a b) float))))
|
|
||||||
|
|
||||||
(test-group "semen"
|
(test-group "semen"
|
||||||
(test-assert (sex-fn? print-str-fn))
|
;;; Macro expansion
|
||||||
(test #f (sex-fn-public? print-str-fn))
|
|
||||||
(test 'void (sex-fn-return-type print-str-fn))
|
|
||||||
(test 'print-str (sex-fn-name print-str-fn))
|
|
||||||
(test '((s string)) (sex-fn-arglist print-str-fn))
|
|
||||||
(test '(fn print-str ((s string)) void) (sex-fn-prototype print-str-fn))
|
|
||||||
(test '((printf "%s" s)) (sex-fn-body print-str-fn))
|
|
||||||
|
|
||||||
(test-assert (sex-fn? sum-fn))
|
|
||||||
(test #t (sex-fn-public? sum-fn))
|
|
||||||
(test 'float (sex-fn-return-type sum-fn))
|
|
||||||
(test 'sum (sex-fn-name sum-fn))
|
|
||||||
(test '((a int) (b int)) (sex-fn-arglist sum-fn))
|
|
||||||
(test '(pub fn sum ((a int) (b int)) float) (sex-fn-prototype sum-fn))
|
|
||||||
(test '((return (cast (+ a b) float))) (sex-fn-body sum-fn))
|
|
||||||
|
|
||||||
(test-assert (sex-fn? '(extern fn foo () void)))
|
|
||||||
(test 'foo (sex-fn-name '(extern fn foo () void)))
|
|
||||||
(test #f (sex-fn? '(struct point ((x int)))))
|
|
||||||
|
|
||||||
(let ((sex-code
|
(let ((sex-code
|
||||||
'((defmacro (sum-var name a b c)
|
'((defmacro (sum-var name a b c)
|
||||||
@@ -39,17 +12,6 @@
|
|||||||
|
|
||||||
(test '((var v 6)) (semen-process sex-code)))
|
(test '((var v 6)) (semen-process sex-code)))
|
||||||
|
|
||||||
;;; Macro expansion
|
|
||||||
|
|
||||||
(define (form-identity form env)
|
|
||||||
form)
|
|
||||||
|
|
||||||
(test 'a (walk-form 'a form-identity (make-hash-table)))
|
|
||||||
(test '(a b c) (walk-form '(a b c) form-identity (make-hash-table)))
|
|
||||||
|
|
||||||
(test 'a (macro-expand 'a))
|
|
||||||
(test '(a b c) (macro-expand '(a b c)))
|
|
||||||
|
|
||||||
(let ((sex-code-macro
|
(let ((sex-code-macro
|
||||||
'((defmacro (x10 a)
|
'((defmacro (x10 a)
|
||||||
`(* 10 ,a))
|
`(* 10 ,a))
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
(compilation "-- -std=c11 -pedantic-errors")
|
(compilation "-- -std=c11 -pedantic-errors")
|
||||||
(input)
|
(input)
|
||||||
(output "c11: 42 43 6 4 6 12 5 0"
|
(output "c11: 42 43 6 4 6 12 5 0 -9223372036854775808"
|
||||||
"trigraphs: what??! ??= ???( ??/")
|
"trigraphs: what??! ??= ???( ??/")
|
||||||
(return 0)
|
(return 0)
|
||||||
|
|
||||||
@@ -33,6 +33,7 @@
|
|||||||
(var origin point #())
|
(var origin point #())
|
||||||
(var unit _ (shape/square 1))
|
(var unit _ (shape/square 1))
|
||||||
(var pair [_ 2] #(#(point : 1 2) #(point : 3 4)))
|
(var pair [_ 2] #(#(point : 1 2) #(point : 3 4)))
|
||||||
|
(var least _ -9223372036854775808)
|
||||||
|
|
||||||
(fn sides ((s shape)) int
|
(fn sides ((s shape)) int
|
||||||
(var n int 0)
|
(var n int 0)
|
||||||
@@ -46,8 +47,8 @@
|
|||||||
(var k int 1)
|
(var k int 1)
|
||||||
(var add-k (closure ((int)) int)
|
(var add-k (closure ((int)) int)
|
||||||
(closure ((v int)) int (k) (return (+ v k))))
|
(closure ((v int)) int (k) (return (+ v k))))
|
||||||
(printf "c11: %d %d %d %d %d %d %d %d\n"
|
(printf "c11: %d %d %d %d %d %d %d %d %lld\n"
|
||||||
(.area p) (add-k (.area p)) (+ (sides (shape/square 1)) (sides (shape/dot)) 2)
|
(.area p) (add-k (.area p)) (+ (sides (shape/square 1)) (sides (shape/dot)) 2)
|
||||||
(sides unit) (.area corner) (.area [pair 1]) [primes 2] (. origin y))
|
(sides unit) (.area corner) (.area [pair 1]) [primes 2] (. origin y) least)
|
||||||
(printf "trigraphs: %s\n" "what??! ??= ???( ??/")
|
(printf "trigraphs: %s\n" "what??! ??= ???( ??/")
|
||||||
(return 0))
|
(return 0))
|
||||||
|
|||||||
@@ -1,23 +1,29 @@
|
|||||||
;;; The compiler's modules and the programs built from them, as make
|
;;; The compiler's modules and the programs built from them, as make
|
||||||
;;; reads them: what each one imports, read off its import forms. A
|
;;; 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
|
;;; module is a NAME.scm in the current directory; a program is named
|
||||||
;;; with its source, whose include-relative files count as its own.
|
;;; with its source. The files either includes with include-relative
|
||||||
|
;;; count as its own.
|
||||||
;;;
|
;;;
|
||||||
;;; csi -s tools/module-deps.scm sexc=main.scm > modules.mk
|
;;; csi -s tools/module-deps.scm sexc=main.scm > modules.mk
|
||||||
;;;
|
;;;
|
||||||
;;; prints
|
;;; prints
|
||||||
;;;
|
;;;
|
||||||
;;; MODULES = fmt-c-writer infer reader ...
|
;;; MODULES = fmt-c-writer infer reader ...
|
||||||
;;; PROGRAM_NAMES = sexc
|
|
||||||
;;; PROGRAM_OBJ = main.o
|
|
||||||
;;; SOURCES = fmt-c-writer.scm infer.scm ... main.scm
|
;;; SOURCES = fmt-c-writer.scm infer.scm ... main.scm
|
||||||
;;; infer.o: types.o utils.o
|
;;; infer.o: types.o utils.o
|
||||||
;;; ...
|
;;; ...
|
||||||
;;; main.o: sexc.o
|
;;; main.o: sexc.o
|
||||||
;;; sexc: main.o sexc.o
|
;;; sexc: main.o sexc.o
|
||||||
|
;;; fmt-c-writer.scm:
|
||||||
|
;;; ...
|
||||||
|
;;;
|
||||||
|
;;; and a source deleted since is then one make has nothing to do for,
|
||||||
|
;;; rather than one it has no rule to make
|
||||||
|
|
||||||
(import scheme
|
(import scheme
|
||||||
(chicken base)
|
(chicken base)
|
||||||
|
(chicken condition)
|
||||||
|
(chicken eval)
|
||||||
(chicken file)
|
(chicken file)
|
||||||
(chicken io)
|
(chicken io)
|
||||||
(chicken pathname)
|
(chicken pathname)
|
||||||
@@ -38,28 +44,59 @@
|
|||||||
;;; module the tests import
|
;;; module the tests import
|
||||||
(define eggs (call-with-input-file "dependencies.txt" read-list))
|
(define eggs (call-with-input-file "dependencies.txt" read-list))
|
||||||
|
|
||||||
|
;;; A scheme.scm is not the module every unit imports. Asked from /,
|
||||||
|
;;; since here a NAME.import.scm built from NAME.scm would answer
|
||||||
|
(define (chicken-module? name)
|
||||||
|
(let ((here (current-directory)))
|
||||||
|
(dynamic-wind
|
||||||
|
(lambda () (change-directory "/"))
|
||||||
|
(lambda () (handle-exceptions exn #f (module-environment name) #t))
|
||||||
|
(lambda () (change-directory here)))))
|
||||||
|
|
||||||
(define (module-file name)
|
(define (module-file name)
|
||||||
(and (symbol? name)
|
(and (symbol? name)
|
||||||
(not (memq name eggs))
|
(not (memq name eggs))
|
||||||
(let ((file (string-append (symbol->string name) ".scm")))
|
(let ((file (string-append (symbol->string name) ".scm")))
|
||||||
(and (file-exists? file) file))))
|
(and (file-exists? file)
|
||||||
|
(not (chicken-module? name))
|
||||||
|
file))))
|
||||||
|
|
||||||
;;; The names FILE imports, and the files it includes
|
;;; FILE's forms, or the end of the scan, naming the file that has none
|
||||||
(define (scan file)
|
(define (forms-of file)
|
||||||
(let loop ((forms (call-with-input-file file read-list)) (names '()) (files '()))
|
(handle-exceptions exn
|
||||||
|
(begin
|
||||||
|
(display (string-append "module-deps: " file ": "
|
||||||
|
((condition-property-accessor 'exn 'message "") exn)
|
||||||
|
"\n")
|
||||||
|
(current-error-port))
|
||||||
|
(exit 1))
|
||||||
|
(call-with-input-file file read-list)))
|
||||||
|
|
||||||
|
;;; The names FILE imports, and the files it includes, skipping those
|
||||||
|
;;; SEEN already, so two files that include each other are read once
|
||||||
|
;;; each; the vendored sex-fmt-c imports inside its (module name
|
||||||
|
;;; exports ...) form
|
||||||
|
(define (scan file #!optional (seen (list file)))
|
||||||
|
(let loop ((forms (forms-of file)) (names '()) (files '()))
|
||||||
(if (null? forms)
|
(if (null? forms)
|
||||||
(values names files)
|
(values names files)
|
||||||
(let ((form (car forms)))
|
(let ((form (car forms)))
|
||||||
(cond
|
(cond
|
||||||
((not (pair? form)) (loop (cdr forms) names files))
|
((not (pair? form)) (loop (cdr forms) names files))
|
||||||
|
((and (eq? (car form) 'module) (list? form) (>= (length form) 3))
|
||||||
|
(loop (append (cdddr form) (cdr forms)) names files))
|
||||||
((eq? (car form) 'import)
|
((eq? (car form) 'import)
|
||||||
(loop (cdr forms) (append (map spec-name (cdr form)) names) files))
|
(loop (cdr forms) (append (map spec-name (cdr form)) names) files))
|
||||||
((eq? (car form) 'include-relative)
|
((eq? (car form) 'include-relative)
|
||||||
(let ((included (make-pathname (pathname-directory file) (cadr form))))
|
(let ((included (make-pathname (pathname-directory file) (cadr form)))
|
||||||
(let-values (((more-names more-files) (scan included)))
|
(seen (append files seen)))
|
||||||
(loop (cdr forms)
|
(if (member included seen)
|
||||||
(append more-names names)
|
(loop (cdr forms) names files)
|
||||||
(append (cons included more-files) files)))))
|
(let-values (((more-names more-files)
|
||||||
|
(scan included (cons included seen))))
|
||||||
|
(loop (cdr forms)
|
||||||
|
(append more-names names)
|
||||||
|
(append (cons included more-files) files))))))
|
||||||
(else (loop (cdr forms) names files)))))))
|
(else (loop (cdr forms) names files)))))))
|
||||||
|
|
||||||
(define (symbol<? a b)
|
(define (symbol<? a b)
|
||||||
@@ -73,19 +110,16 @@
|
|||||||
(loop (cdr names) acc))
|
(loop (cdr names) acc))
|
||||||
(else (loop (cdr names) (cons (car names) acc))))))
|
(else (loop (cdr names) (cons (car names) acc))))))
|
||||||
|
|
||||||
(define (imported-modules file)
|
;;; Each module reached from ROOTS, as (name imported-modules . included-files)
|
||||||
(let-values (((names files) (scan file)))
|
|
||||||
(local-modules names)))
|
|
||||||
|
|
||||||
;;; Each module reached from ROOTS, with the modules it imports
|
|
||||||
(define (module-graph roots)
|
(define (module-graph roots)
|
||||||
(let loop ((pending roots) (graph '()))
|
(let loop ((pending roots) (graph '()))
|
||||||
(cond ((null? pending) (sort graph (lambda (a b) (symbol<? (car a) (car b)))))
|
(cond ((null? pending) (sort graph (lambda (a b) (symbol<? (car a) (car b)))))
|
||||||
((assq (car pending) graph) (loop (cdr pending) graph))
|
((assq (car pending) graph) (loop (cdr pending) graph))
|
||||||
(else
|
(else
|
||||||
(let ((deps (imported-modules (module-file (car pending)))))
|
(let-values (((names files) (scan (module-file (car pending)))))
|
||||||
(loop (append (cdr pending) deps)
|
(let ((deps (local-modules names)))
|
||||||
(cons (cons (car pending) deps) graph)))))))
|
(loop (append (cdr pending) deps)
|
||||||
|
(cons (cons (car pending) (cons deps files)) graph))))))))
|
||||||
|
|
||||||
(define (objects names)
|
(define (objects names)
|
||||||
(map (lambda (name) (string-append (symbol->string name) ".o")) names))
|
(map (lambda (name) (string-append (symbol->string name) ".o")) names))
|
||||||
@@ -96,9 +130,9 @@
|
|||||||
|
|
||||||
;;; sexc=main.scm, read off main.scm and the files it includes
|
;;; sexc=main.scm, read off main.scm and the files it includes
|
||||||
(define-record-type program
|
(define-record-type program
|
||||||
(make-program name object modules sources)
|
(make-program binary object modules sources)
|
||||||
program?
|
program?
|
||||||
(name program-name)
|
(binary program-binary)
|
||||||
(object program-object)
|
(object program-object)
|
||||||
(modules program-modules)
|
(modules program-modules)
|
||||||
(sources program-sources))
|
(sources program-sources))
|
||||||
@@ -114,17 +148,19 @@
|
|||||||
(let* ((programs (map read-program (command-line-arguments)))
|
(let* ((programs (map read-program (command-line-arguments)))
|
||||||
(graph (module-graph
|
(graph (module-graph
|
||||||
(local-modules (apply append (map program-modules programs))))))
|
(local-modules (apply append (map program-modules programs))))))
|
||||||
|
(define sources
|
||||||
|
(append (apply append (map (lambda (node) (cons (module-file (car node)) (cddr node)))
|
||||||
|
graph))
|
||||||
|
(apply append (map program-sources programs))))
|
||||||
(print "MODULES = " (string-intersperse (map (o symbol->string car) graph) " "))
|
(print "MODULES = " (string-intersperse (map (o symbol->string car) graph) " "))
|
||||||
(print "PROGRAM_NAMES = " (string-intersperse (map program-name programs) " "))
|
(print "SOURCES = " (string-intersperse sources " "))
|
||||||
(print "PROGRAM_OBJ = " (string-intersperse (map program-object programs) " "))
|
(for-each (lambda (node)
|
||||||
(print "SOURCES = "
|
(rule (car (objects (list (car node))))
|
||||||
(string-intersperse (append (map (o module-file car) graph)
|
(append (cddr node) (objects (cadr node)))))
|
||||||
(apply append (map program-sources programs)))
|
|
||||||
" "))
|
|
||||||
(for-each (lambda (node) (rule (car (objects (list (car node)))) (objects (cdr node))))
|
|
||||||
graph)
|
graph)
|
||||||
(for-each (lambda (p)
|
(for-each (lambda (p)
|
||||||
(let ((modules (objects (program-modules p))))
|
(let ((modules (objects (program-modules p))))
|
||||||
(rule (program-object p) (append (cdr (program-sources p)) modules))
|
(rule (program-object p) (append (cdr (program-sources p)) modules))
|
||||||
(rule (program-name p) (cons (program-object p) modules))))
|
(rule (program-binary p) (cons (program-object p) modules))))
|
||||||
programs))
|
programs)
|
||||||
|
(for-each (lambda (source) (print source ":")) sources))
|
||||||
|
|||||||
19
types.scm
19
types.scm
@@ -49,6 +49,7 @@
|
|||||||
|
|
||||||
add-name-type!
|
add-name-type!
|
||||||
get-name-type
|
get-name-type
|
||||||
|
unknown-name
|
||||||
type-of
|
type-of
|
||||||
current-type-of
|
current-type-of
|
||||||
get-return-type
|
get-return-type
|
||||||
@@ -156,11 +157,21 @@
|
|||||||
(define (add-name-type! name type)
|
(define (add-name-type! name type)
|
||||||
(hash-table-set! +name-db+ name type))
|
(hash-table-set! +name-db+ name type))
|
||||||
|
|
||||||
|
;;; Called with each name a lookup finds nothing for. A module's
|
||||||
|
;;; interface is read before its declarations are, so a macro call there
|
||||||
|
;;; asking after one -- (field-count box), box being its record -- is
|
||||||
|
;;; told nothing, and fails for that rather than for being wrong
|
||||||
|
(define unknown-name (make-parameter void))
|
||||||
|
|
||||||
|
(define (known name info)
|
||||||
|
(unless info ((unknown-name) name))
|
||||||
|
info)
|
||||||
|
|
||||||
;;; #f for a name never declared, which is what `printf' looks like
|
;;; #f for a name never declared, which is what `printf' looks like
|
||||||
;;; until something parses stdio.h. Not an error here; the caller
|
;;; until something parses stdio.h. Not an error here; the caller
|
||||||
;;; decides.
|
;;; decides.
|
||||||
(define (get-name-type name)
|
(define (get-name-type name)
|
||||||
(hash-table-ref/default +name-db+ name #f))
|
(known name (hash-table-ref/default +name-db+ name #f)))
|
||||||
|
|
||||||
;;; What `(make-adder 10)' has for a type: `make-adder's return type,
|
;;; What `(make-adder 10)' has for a type: `make-adder's return type,
|
||||||
;;; or #f when NAME is not a function with a signature on record
|
;;; or #f when NAME is not a function with a signature on record
|
||||||
@@ -172,7 +183,7 @@
|
|||||||
(third type))))
|
(third type))))
|
||||||
|
|
||||||
(define (get-tag-info name)
|
(define (get-tag-info name)
|
||||||
(hash-table-ref/default +tag-db+ name #f))
|
(known name (hash-table-ref/default +tag-db+ name #f)))
|
||||||
|
|
||||||
;;; First look up the ordinary identifier, then tag of that id when
|
;;; First look up the ordinary identifier, then tag of that id when
|
||||||
;;; no ordinary one was declared, as C does
|
;;; no ordinary one was declared, as C does
|
||||||
@@ -296,7 +307,7 @@
|
|||||||
(hash-table-set! +ops-db+ (cons (type-key type) method) qualified))
|
(hash-table-set! +ops-db+ (cons (type-key type) method) qualified))
|
||||||
|
|
||||||
(define (get-method type method)
|
(define (get-method type method)
|
||||||
(hash-table-ref/default +ops-db+ (cons (type-key type) method) #f))
|
(known type (hash-table-ref/default +ops-db+ (cons (type-key type) method) #f)))
|
||||||
|
|
||||||
;;; vec2 + length -> vec2/length, which `unkebabify' spells vec2ːlength
|
;;; vec2 + length -> vec2/length, which `unkebabify' spells vec2ːlength
|
||||||
(define (qualify-op type name)
|
(define (qualify-op type name)
|
||||||
@@ -338,7 +349,7 @@
|
|||||||
|
|
||||||
;;; What a generic macro asks before it expands: (realizes? 'ord 'circle)
|
;;; What a generic macro asks before it expands: (realizes? 'ord 'circle)
|
||||||
(define (realizes? spec type)
|
(define (realizes? spec type)
|
||||||
(hash-table-exists? +realize-db+ (cons spec (type-key type))))
|
(known type (hash-table-exists? +realize-db+ (cons spec (type-key type)))))
|
||||||
|
|
||||||
;;; Sums, constructors, methods and specs refuse a second declaration
|
;;; Sums, constructors, methods and specs refuse a second declaration
|
||||||
;;; of the same name, which is per translation unit rather than per
|
;;; of the same name, which is per translation unit rather than per
|
||||||
|
|||||||
19
utils.scm
19
utils.scm
@@ -10,7 +10,6 @@
|
|||||||
|
|
||||||
(export
|
(export
|
||||||
get-env-var
|
get-env-var
|
||||||
set-working-directory
|
|
||||||
to-absolute-pathname
|
to-absolute-pathname
|
||||||
comment-form?
|
comment-form?
|
||||||
strip-header-comments
|
strip-header-comments
|
||||||
@@ -25,12 +24,9 @@
|
|||||||
form-end-line
|
form-end-line
|
||||||
copy-form-source!
|
copy-form-source!
|
||||||
stamp-form-source!
|
stamp-form-source!
|
||||||
form-location
|
|
||||||
set-form-type!
|
|
||||||
form-type
|
|
||||||
sex-error
|
sex-error
|
||||||
sex-warning
|
sex-warning
|
||||||
with-directory)
|
(with-directory set-working-directory))
|
||||||
|
|
||||||
(define-syntax prog1
|
(define-syntax prog1
|
||||||
(syntax-rules ()
|
(syntax-rules ()
|
||||||
@@ -129,19 +125,6 @@
|
|||||||
;;; The file `parse-all' is currently reading. Bound by the reader
|
;;; The file `parse-all' is currently reading. Bound by the reader
|
||||||
(define current-source-file (make-parameter "<unknown>"))
|
(define current-source-file (make-parameter "<unknown>"))
|
||||||
|
|
||||||
;;; What type a form has, once something has worked it out. Keyed by
|
|
||||||
;;; cons cell like the sources above, so one form has one type: a body
|
|
||||||
;;; typed at two instantiations has to be copied before the second.
|
|
||||||
(define +form-types+ (make-hash-table eq?))
|
|
||||||
|
|
||||||
(define (set-form-type! form type)
|
|
||||||
(when (pair? form)
|
|
||||||
(hash-table-set! +form-types+ form type))
|
|
||||||
type)
|
|
||||||
|
|
||||||
(define (form-type form)
|
|
||||||
(hash-table-ref/default +form-types+ form #f))
|
|
||||||
|
|
||||||
(define (set-form-source! form file line #!optional (end-line line))
|
(define (set-form-source! form file line #!optional (end-line line))
|
||||||
(hash-table-set! +form-sources+ form (list file line end-line)))
|
(hash-table-set! +form-sources+ form (list file line end-line)))
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user