Ok, I reworked the syntax and it covers all my use cases including single,
multiple, or no variables. It also supports raw string literals or raw C string
outputs.
I'm considering it basically stable except for bug fixes. Hopefully this helps
other Nim users in the future with similar needs! It's already simpler for me,
and nice to know the emit code is unit tested at least for the basics.
Example with some C macro that declares a variable that we want to use in Nim:
/* define example C Macro for testing */
#define C_DEFINE_VAR(NM, SZ) int32_t NM[SZ]
Run
import cdecl
import cdecl/cdeclapi
export cdeclapi # this is needed clients to use the declared apis
proc CDefineVar*(name: CToken, size: static[int]) {.
cdeclmacro: "C_DEFINE_VAR", cdeclsVar(name -> array[size, int32]).}
Run
Then users of the wrapper can use it like below to declare a stack local
variable from C. Note that you need to be careful to make the lengths match,
etc.
const cVarSz = 4
CDefineVar(myVar, cVarSz)
test "test myVar declaration":
let testVal = [1'i32,2,3,4]
myVar[0..3] = testVal
check myVar.len() == cVarSz
echo "myVar: ", repr myVar
let res = myVar == testVal
check res
Run
Globals can be created by adding `global` to the pragmas `cdeclmacro` list.