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.
This commit is contained in:
@@ -19,11 +19,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)
|
||||||
|
|||||||
@@ -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,10 +1,6 @@
|
|||||||
;;; 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 srfi-13)
|
(import srfi-13)
|
||||||
|
|
||||||
|
|||||||
@@ -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)))))))
|
|
||||||
|
|||||||
@@ -2,7 +2,6 @@
|
|||||||
test)
|
test)
|
||||||
|
|
||||||
(include-relative "pipeline.scm")
|
(include-relative "pipeline.scm")
|
||||||
(include-relative "basic.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")
|
||||||
|
|||||||
Reference in New Issue
Block a user