@@ -1,192 +1,132 @@
;;; Types
( test-group "fmt-writer"
;;; 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
' ( const int )
( walk-type ' ( const int ) ) )
( test-group "fmt-c-writer"
( test
' ( %array ( const char ) 512 )
( walk-type ' ( ¤ const char 512 ) ) )
( 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;" ) ) )
( test
' ( %array ( float ) 512 )
( walk-type ' ( ¤ ( float ) 512 ) ) )
;; 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
' ( %array ( const char ) 512 )
( walk-type ' ( ¤ ( const char ) 512 ) ) )
( test-group "toplevel comments"
( test-assert "are C comments"
( emits? ";; hi" "/* hi */" ) )
( test-assert "without the markers or the padding"
( emits? ";;; Foo " "/* Foo */" ) ) )
( test
' ( %array ( const char ) )
( walk-type ' ( ¤ const char ) ) )
( 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
' ( %array ( const char ) )
( walk-type ' ( ¤ ( const char ) ) ) )
( 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
' ( %array float 8 )
( walk-type ' ( ¤ float 8 ) ) )
( 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
"Pointer to const char "
' ( const char * )
( walk-type ' ( * const char ) ) )
( 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
"Const pointer to const char "
' ( const char * const )
( walk-type ' ( const * const char ) ) )
( test
' ( %fun void ( int float ( %array ( struct what * const ) ) ) )
( walk-type ' ( fn ( ( int ) ( float ) ( ¤ ( const * struct what ) ) ) void ) ) )
( test
' ( %fun void ( int ( %array float ) ( %array ( struct what * const ) ) ) )
( walk-type ' ( fn ( ( int ) ( ¤ float ) ( ¤ ( const * struct what ) ) ) void ) ) )
( 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 ) ) ) ) ) ) )
( 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 []);" ) ) ) )