No matching definitions.

c-dsl/codegen

src/c-dsl/codegen.tur
defn

c-raw

(c-raw [s :cstr] :cstr)

identity function; marks a string as a raw C fragment.

sany :cstr value

s unchanged.

(c-raw "/* hand-written C */")  ; => "/* hand-written C */"

Since: P1

defn

c-int->str

(c-int->str [n :int] :cstr)

convert an integer to a decimal string.

ninteger value

Heap-allocated decimal representation of n.

(c-int->str 42)  ; => "42"

Since: P1

defn

c-float->str

(c-float->str [f :float] :cstr)

convert a float to a string.

ffloat value

Heap-allocated string representation using %g format.

(c-float->str 3.14)  ; => "3.14"

Since: P1

defn

c-cat2

(c-cat2 [a :cstr b :cstr] :cstr)

concatenate two C source strings.

afirst string
bsecond string

New heap-allocated string with a followed by b.

(c-cat2 "int32_t" "*")  ; => "int32_t*"

Since: P1

defn

c-cat3

(c-cat3 [a :cstr b :cstr c :cstr] :cstr)

concatenate three C source strings.

afirst string
bsecond string
cthird string

New heap-allocated string a+b+c.

Since: P1

defn

c-cat4

(c-cat4 [a :cstr b :cstr c :cstr d :cstr] :cstr)

concatenate four C source strings.

Since: P1

defn

c-sprintf1

(c-sprintf1 [fmt :cstr a :cstr] :cstr)

format a string with one %s substitution.

fmtformat string with one %s placeholder
asubstitution string

Formatted string.

(c-sprintf1 "return %s;" "x")  ; => "return x;"

Since: P1

defn

c-sprintf2

(c-sprintf2 [fmt :cstr a :cstr b :cstr] :cstr)

format a string with two %s substitutions.

Since: P1

defn

c-sprintf3

(c-sprintf3 [fmt :cstr a :cstr b :cstr c :cstr] :cstr)

format a string with three %s substitutions.

Since: P1

defn

c-sprintf4

(c-sprintf4 [fmt :cstr a :cstr b :cstr c :cstr d :cstr] :cstr)

format a string with four %s substitutions.

Since: P1

defn

c-join

(c-join [items :int sep :cstr] :cstr)

join a cons list of :cstr items with a separator.

itemscons list of :cstr values (nil-terminated, tail = 0)
sepseparator 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

defn

c-lines

(c-lines [items :int] :cstr)

join a cons list of :cstr items with newlines.

itemscons list of :cstr values

Items joined by "\n".

Since: P1

defn

c-indent

(c-indent [code :cstr] :cstr)

prefix every line of a multi-line string with 4 spaces.

codeC 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

defn

c-join-vec

(c-join-vec [items :int sep :cstr] :cstr)

join a Vec[cstr] of strings with a separator.

itemsVec[cstr] of :cstr values
sepseparator

Items joined by sep.

(c-join-vec (vec-of "int32_t a" "float b") ", ")
  ; => "int32_t a, float b"

Since: v0.2.0

defn

c-stmts

(c-stmts [stmts :int] :cstr)

join a Vec[cstr] of statement strings with newlines.

stmtsVec[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

defn

c-block

(c-block [stmts :int] :cstr)

wrap a cons list of statement strings in a braced block.

stmtscons 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

defn

compile-c

(compile-c [fragments :int] :cstr)

join a cons list of top-level C declarations into a source string.

fragmentscons 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

defn

emit-c-file

(emit-c-file [path :cstr fragments :int] :bool)

write compiled C source to a file on disk.

pathfile path to write (NUL-terminated)
fragmentscons 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