From 8ae2346e414d63ca307f8fabcfc3364717c0fe39 Mon Sep 17 00:00:00 2001 From: alex-eg Date: Wed, 27 May 2026 18:00:18 +0300 Subject: [PATCH] add small programs for compile testing --- Makefile | 16 ++++++++-- tests/sex-programs/hello-world.sex | 13 ++++++++ tests/sex-programs/list-macros.sex | 48 ++++++++++++++++++++++++++++ tests/sex-programs/lists.sex | 50 ++++++++++++++++++++++++++++++ tools/sextest/sextest.scm | 4 +++ 5 files changed, 129 insertions(+), 2 deletions(-) create mode 100644 tests/sex-programs/hello-world.sex create mode 100644 tests/sex-programs/list-macros.sex create mode 100644 tests/sex-programs/lists.sex diff --git a/Makefile b/Makefile index dbe895a..bf72a2e 100644 --- a/Makefile +++ b/Makefile @@ -49,12 +49,24 @@ fmt-c-writer.o: fmt-c-writer.module.scm fmt-c-writer.scm sex-fmt-c.o utils.o sexc.o: sexc.module.scm sexc.scm fmt-c-writer.o sex-macros.o sex-modules.o reader.o semen.o utils.o $(CHICKEN_C) $(CSC_FLAGS) $(MODULE_FLAGS) sexc.module.scm -o sexc.o -unit sexc -link fmt-c-writer,sex-macros,sex-modules,reader,semen,utils +# Unit testing sex-tests: - $(MAKE) -C tests sex-tests + $(MAKE) -C ./tests sex-tests cp ./tests/sex-tests ./ +sextest: + $(MAKE) -C ./tools/sextest sextest + cp ./tools/sextest/sextest . + +SEX_TEST_PROGRAMS = hello-world lists + +run-tests: sexc sex-tests sextest + ./sex-tests && ./sextest --sexc=./sexc $(SEX_TEST_PROGRAMS:%=./tests/sex-programs/%.sex) + clean: rm -f $(OBJ) main.o rm -f *.import.scm rm -f *.link - rm -f sexc sex-tests + rm -f sexc sex-tests sextest + +.PHONY: clean run-tests diff --git a/tests/sex-programs/hello-world.sex b/tests/sex-programs/hello-world.sex new file mode 100644 index 0000000..02452c2 --- /dev/null +++ b/tests/sex-programs/hello-world.sex @@ -0,0 +1,13 @@ +(input "Sextest") +(output "Hello from Sex!" "What is your name?" "Hello, Sextest!") +(return 255) + +(include stdio.h) + +(pub fn main ((argc int) (argv [* const char])) int + (puts "Hello from Sex!") + (var name [char 512]) + (puts "What is your name?") + (scanf "%s" (cast (& name) (* char))) + (printf "Hello, %s!\n" name) + (return 255)) diff --git a/tests/sex-programs/list-macros.sex b/tests/sex-programs/list-macros.sex new file mode 100644 index 0000000..8355f73 --- /dev/null +++ b/tests/sex-programs/list-macros.sex @@ -0,0 +1,48 @@ +(pub defmacro (list-T type) + (let ((list-type (cat 'list- type))) + `(struct ,list-type + ((value ,type) + (next (* struct ,list-type)))))) + +(pub defmacro (make-list-T type is-public?) + (let ((list-type (list 'struct (cat 'list- type))) + (fn-name (cat 'make-list- type))) + `(,@(if is-public? '(pub) '()) fn ,fn-name () (* ,list-type) + (var list (* ,list-type) (cast (malloc (sizeof ,list-type)) (* ,list-type))) + (= (-> list next) NULL) + (return list)))) + +(pub defmacro (add-value-list-T type is-public?) + (let ((list-type (list 'struct (cat 'list- type))) + (fn-name (cat 'add-value-list- type))) + `(,@(if is-public? '(pub) '()) fn ,fn-name ((list * ,list-type) (value ,type)) void + (while (!= (-> list next) NULL) + (= list (-> list next))) + (= (-> list next) (,(cat 'make-list- type))) + (= (-> list value) value)))) + +(pub defmacro (length-list-T type is-public?) + (let ((fn-name (cat 'length-list- type)) + (list-type (list 'struct (cat 'list- type)))) + `(,@(if is-public? '(pub) '()) fn ,fn-name ((list ,(cons '* list-type))) size-t + (var n size-t 0) + (while (!= (-> list next) NULL) + (= list (-> list next)) + (++ n)) + (return n)))) + +(pub defmacro (is-empty-list-T type is-public?) + `(,@(if is-public? '(pub) '()) fn ,(cat 'is-empty-list- type) + ((list ,(list '* 'struct (cat 'list- type)))) + bool + (return (== (-> list next) NULL)))) + +(pub defmacro (list-for-each list-type list-var elt-type elt-var what-do) + (let ((list-var-2 (cat list-var '-2))) + `(begin + (var ,list-var-2 (* ,list-type) ,list-var) + (var ,elt-var ,elt-type (-> ,list-var-2 value)) + (while (!= (-> ,list-var-2 next) NULL) + ,what-do + (= ,list-var-2 (-> ,list-var-2 next)) + (= ,elt-var (-> ,list-var-2 value)))))) diff --git a/tests/sex-programs/lists.sex b/tests/sex-programs/lists.sex new file mode 100644 index 0000000..e428a88 --- /dev/null +++ b/tests/sex-programs/lists.sex @@ -0,0 +1,50 @@ +(input) +(output "Size of the list: 0" + "Size of the list: 2" + "3 4 " + "Size of the list: 2") +(return 0) + +(include stdlib.h) +(include stddef.h) +(include stdio.h) + +(import list-macros) + +(struct foo + ((a-field float) + (b int) + (c (* const char)) + (not (fn ((val bool)) bool)))) + +(var f (struct foo)) + +(list-T int) +(make-list-T int #f) +(add-value-list-T int #f) +(length-list-T int #f) +(is-empty-list-T int #f) + +(extern fn puk ((a int) (b float)) void) +(pub fn baz () bool + (return true)) + +(extern var i int) +(var j int) +(pub var k int) + +(pub fn main () int + (var l (* struct list-int) (make-list-int)) + (printf "Size of the list: %lu\n" (length-list-int l)) + (add-value-list-int l 3) + (add-value-list-int l 4) + (printf "Size of the list: %lu\n" (length-list-int l)) + (list-for-each (struct list-int) l int v + (printf "%d " v)) + (printf "\n") + (printf "Size of the list: %lu\n" (length-list-int l)) + (return 0)) + +(pub fn print-list ((l (* const struct list-int))) void + (list-for-each (const struct list-int) l int v (printf "%d " v)) + (printf "\n")) diff --git a/tools/sextest/sextest.scm b/tools/sextest/sextest.scm index 1f1c84c..d49a62b 100644 --- a/tools/sextest/sextest.scm +++ b/tools/sextest/sextest.scm @@ -166,6 +166,10 @@ (single-char #\h)))) (define (process-test-file sexc path) + (set-environment-variable! "SEX_MODULE_PATH" + (normalize-pathname (make-absolute-pathname + (current-directory) + (pathname-directory path)))) (let* ((settings-and-src (process-file path)) (settings (car settings-and-src)) (src (cdr settings-and-src))