1
0
forked from alex-eg/sex
Files
sex/tests/fmt-c-writer.scm
alex-eg bf8ba2f9fd test the writer on the C it writes
The walk-* tests pinned the forms handed to fmt-c, which were right
while the C they became lost a const; spelled as C, they check what fmt-c
makes of them too. The cases no source can reach, an atom no form holds
and a name the reader takes for a negation, go.
2026-10-20 11:56:02 +03:00

133 lines
6.5 KiB
Scheme
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
;;; The C each declaration comes out as: names, types, variables,
;;; initializers, functions and structs. Non-pub ones are static, which
;;; the fragments leave out.
(test-group "fmt-c-writer"
(test-group "names"
(test-assert "kebab case is snake case"
(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;")))
;; The operators spelled with the characters names are converted from
(test-group "operators"
(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-group "toplevel comments"
(test-assert "are C comments"
(emits? ";; hi" "/* hi */"))
(test-assert "without the markers or the padding"
(emits? ";;; Foo " "/* Foo */")))
(test-group "types"
(test-assert "an array"
(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-group "variables"
(test-assert "a local array"
(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-group "initializers"
(test-assert "a bare one is braces"
(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-group "functions"
(test-assert "a prototype may leave its parameters nameless"
(emits? "(fn puk ((int) [float 8]) void)" "void puk (int, float [8]);"))
(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-group "structs"
(test-assert "names are converted"
(emits? "(struct no-kebab ((a int) (f float)))"
"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"
(emits? "(struct settings ((x y w h u32) (colors [struct ((r g b a float)) 4])))"
"struct settings {\n u32 x, y, w, h;\n struct{\n float r, g, b, a;\n } colors[4];\n};"))
(test-assert "a named one"
(emits? "(struct settings ((x y w h u32) (colors [struct color ((r g b a float)) 4])))"
" struct color{\n float r, g, b, a;\n } colors[4];"))
(test-assert "or a function pointer"
(emits? "(struct mega-kebab ((a int) (min (fn ((int) [int]) bool))))"
" bool (*min)(int, int []);"))))