c-dsl/core
c-let1
(c-let1 [name :cstr type :cstr init :cstr] :cstr)
declare a single initialized local variable.
| name | C identifier string | |
| type | Turmeric type keyword or C type string | |
| init | initializer expression string |
"type name = init;"
(c-let1 "x" ":int" "42") ; => "int32_t x = 42;"
Since: P1
c-let1-uninit
(c-let1-uninit [name :cstr type :cstr] :cstr)
declare a single uninitialized local variable.
| name | C identifier string | |
| type | Turmeric type keyword or C type string |
"type name;"
(c-let1-uninit "ptr" ":[:int]") ; => "int32_t* ptr;"
Since: P1
c-const1
(c-const1 [name :cstr type :cstr init :cstr] :cstr)
declare a single const variable.
| name | C identifier string | |
| type | Turmeric type keyword or C type string | |
| init | initializer expression string |
"const type name = init;"
(c-const1 "MAX_SIZE" ":int" "100") ; => "const int32_t MAX_SIZE = 100;"
Since: P1
c-set!
(c-set! [lhs :cstr rhs :cstr] :cstr)
emit an assignment statement.
| lhs | left-hand side expression string | |
| rhs | right-hand side expression string |
"lhs = rhs;"
(c-set! "x" "42") ; => "x = 42;" (c-set! "arr[i]" "v") ; => "arr[i] = v;"
Since: P1
c-return
(c-return [expr :cstr] :cstr)
emit a return statement.
| expr | expression to return |
"return expr;"
(c-return "x + 1") ; => "return x + 1;"
Since: P1
c-return-void
(c-return-void :cstr)
emit a bare "return;" statement.
"return;"
Since: P1
c-break
(c-break :cstr)
emit a break statement.
"break;"
Since: P1
c-continue
(c-continue :cstr)
emit a continue statement.
"continue;"
Since: P1
c-inc!
(c-inc! [var :cstr] :cstr)
emit a post-increment statement.
| var | variable name string |
"var++;"
(c-inc! "i") ; => "i++;"
Since: P1
c-dec!
(c-dec! [var :cstr] :cstr)
emit a post-decrement statement.
| var | variable name string |
"var--;"
Since: P1
c-if
(c-if [cond-expr :cstr then-body :cstr else-body :cstr] :cstr)
emit an if/else statement.
| cond-expr | condition expression string | |
| then-body | body string for the true branch (one or more statements) | |
| else-body | body string for the false branch (use "" to omit) |
"if (cond) {\n then\n} else {\n else\n}" or without else branch.
(c-if "x > 0" (c-set! "y" "1") (c-set! "y" "-1"))
; => "if (x > 0) {\n y = 1;\n} else {\n y = -1;\n}"
Since: P1
c-if-only
(c-if-only [cond-expr :cstr then-body :cstr] :cstr)
emit an if statement without an else branch.
| cond-expr | condition expression string | |
| then-body | body string for the true branch |
"if (cond) {\n body\n}"
Since: P1
c-while
(c-while [cond-expr :cstr body :cstr] :cstr)
emit a while loop.
| cond-expr | condition expression string | |
| body | body string (one or more statements) |
"while (cond) {\n body\n}"
(c-while "i < len" (c-inc! "i"))
Since: P1
c-do-while
(c-do-while [body :cstr cond-expr :cstr] :cstr)
emit a do-while loop.
| body | body string | |
| cond-expr | condition expression string |
"do {\n body\n} while (cond);"
Since: P1
c-for1
(c-for1 [var :cstr type :cstr init :cstr cond :cstr step :cstr body :cstr] :cstr)
emit a for loop with a typed loop variable declaration.
| var | loop variable name | |
| type | type keyword for the loop variable | |
| init | initial value expression string | |
| cond | condition expression string | |
| step | step expression string (e.g. "i++" or "i += 2") | |
| body | body string |
"for (type var = init; cond; step) {\n body\n}"
(c-for1 "i" ":int" "0" "i < 10" "i++" (c-call "printf" ...))
Since: P1
c-for-raw
(c-for-raw [init :cstr cond :cstr step :cstr body :cstr] :cstr)
emit a for loop with a raw (pre-formatted) init clause.
| init | full init clause string (e.g. "int32_t i = 0, j = n - 1") | |
| cond | condition expression string | |
| step | step expression string | |
| body | body string |
"for (init; cond; step) {\n body\n}"
Since: P1
c-case
(c-case [val :cstr body :cstr] :cstr)
emit a single switch case fragment.
| val | case value string (e.g. "1" or "RED") | |
| body | body statements string |
"case val:\n body"
(c-case "1" (c-break)) ; => "case 1:\n break;"
Since: P1
c-default-case
(c-default-case [body :cstr] :cstr)
emit the default switch case.
| body | body statements string |
"default:\n body"
Since: P1
c-switch
(c-switch [expr :cstr cases :int] :cstr)
emit a switch statement.
| expr | expression to switch on | |
| cases | cons list of case fragment strings (from c-case / c-default-case) |
"switch (expr) {\ncases\n}"
(c-switch "x" (cons (c-case "0" "break;") (cons (c-default-case "break;") 0)))
Since: P1
c-label
(c-label [name :cstr] :cstr)
emit a C label.
| name | label identifier string |
"name:"
Since: P1
c-goto
(c-goto [name :cstr] :cstr)
emit a goto statement.
| name | target label string |
"goto name;"
Since: P1
c-binop
(c-binop [op :cstr a :cstr b :cstr] :cstr)
emit a binary expression wrapped in parentheses.
| op | operator string (e.g. "+", "&&", "==") | |
| a | left operand string | |
| b | right operand string |
"(a op b)"
(c-binop "+" "a" "b") ; => "(a + b)" (c-binop "<" "i" "10") ; => "(i < 10)"
Since: P1
c-unop
(c-unop [op :cstr expr :cstr] :cstr)
emit a prefix unary expression.
| op | operator string (e.g. "!", "-", "~") | |
| expr | operand string |
"(op expr)"
(c-unop "!" "flag") ; => "(!flag)"
Since: P1
c-ternary
(c-ternary [cond-expr :cstr then-expr :cstr else-expr :cstr] :cstr)
emit a ternary conditional expression.
| cond-expr | condition expression string | |
| then-expr | value when true | |
| else-expr | value when false |
"(cond ? then : else)"
(c-ternary "x > 0" "1" "-1") ; => "(x > 0 ? 1 : -1)"
Since: P1