c-dsl/codegen
c-raw
(c-raw [s : cstr] :)
identity function; marks a string as a raw C fragment.
| s | any :cstr value |
s unchanged.
(c-raw "/* hand-written C */") ; => "/* hand-written C */"
Since: P1
c-int->str
(c-int->str [n : int] :)
convert an integer to a decimal string.
| n | integer value |
Heap-allocated decimal representation of n.
(c-int->str 42) ; => "42"
Since: P1
c-float->str
(c-float->str [f : float] :)
convert a float to a string.
| f | float value |
Heap-allocated string representation using %g format.
(c-float->str 3.14) ; => "3.14"
Since: P1
c-cat2
(c-cat2 [a : cstr b : cstr] :)
concatenate two C source strings.
| a | first string | |
| b | second string |
New heap-allocated string with a followed by b.
(c-cat2 "int32_t" "*") ; => "int32_t*"
Since: P1
c-cat3
(c-cat3 [a : cstr b : cstr c : cstr] :)
concatenate three C source strings.
| a | first string | |
| b | second string | |
| c | third string |
New heap-allocated string a+b+c.
Since: P1
c-cat4
(c-cat4 [a : cstr b : cstr c : cstr d : cstr] :)
concatenate four C source strings.
Since: P1
c-sprintf1
(c-sprintf1 [fmt : cstr a : cstr] :)
format a string with one %s substitution.
| fmt | format string with one %s placeholder | |
| a | substitution string |
Formatted string.
(c-sprintf1 "return %s;" "x") ; => "return x;"
Since: P1
c-sprintf2
(c-sprintf2 [fmt : cstr a : cstr b : cstr] :)
format a string with two %s substitutions.
Since: P1
c-sprintf3
(c-sprintf3 [fmt : cstr a : cstr b : cstr c : cstr] :)
format a string with three %s substitutions.
Since: P1
c-sprintf4
(c-sprintf4 [fmt : cstr a : cstr b : cstr c : cstr d : cstr] :)
format a string with four %s substitutions.
Since: P1
c-join
(c-join [items : int sep : cstr] :)
join a cons list of :cstr items with a separator.
| items | cons list of :cstr values (nil-terminated, tail = 0) | |
| sep | separator string inserted between items |
Single heap-allocated string with items joined by sep.
(c-join (cons "int32_t a" (cons "float b" 0)) ", ") ; => "int32_t a, float b"
Since: P1
c-lines
(c-lines [items : int] :)
join a cons list of :cstr items with newlines.
| items | cons list of :cstr values |
Items joined by "\n".
Since: P1
c-indent
(c-indent [code : cstr] :)
prefix every line of a multi-line string with 4 spaces.
| code | C source string, possibly containing newlines |
Each line of code prefixed with " " (4 spaces).
(c-indent "int x = 0;\nreturn x;") ; => " int x = 0;\n return x;"
Since: P1
c-join-vec
(c-join-vec [items : int sep : cstr] :)
join a Vec[cstr] of strings with a separator.
| items | Vec[cstr] of :cstr values | |
| sep | separator |
Items joined by sep.
(c-join-vec (vec-of "int32_t a" "float b") ", ") ; => "int32_t a, float b"
Since: v0.2.0
c-stmts
(c-stmts [stmts : int] :)
join a Vec[cstr] of statement strings with newlines.
| stmts | Vec[cstr] of statement strings, built with (vec-of ...). | |
| (Was a cons list of :cstr prior to v0.2.0.) |
Statements joined by "\n".
Since: P1
c-block
(c-block [stmts : int] :)
wrap a cons list of statement strings in a braced block.
| stmts | cons list of :cstr statement strings |
"{\n stmt1\n stmt2\n}" with each statement indented 4 spaces.
(c-block (cons "return 0;" 0)) ; => "{\n return 0;\n}"
Since: P1
compile-c
(compile-c [fragments : int] :)
join a cons list of top-level C declarations into a source string.
| fragments | cons list of :cstr top-level C declarations/definitions |
Single C source string with each fragment separated by "\n\n".
(compile-c (cons (c-include "stdio.h")
(cons (c-defn ...) 0)))
Since: P1
emit-c-file
(emit-c-file [path : cstr fragments : int] :)
write compiled C source to a file on disk.
| path | file path to write (NUL-terminated) | |
| fragments | cons list of :cstr C declarations/definitions |
true on success, false if the file could not be opened.
(emit-c-file "out.c" my-fragments) ; => true
Since: P1