I'm trying to learn macros and am playing with `fusion/matching` too.
Here, I've attempted to basically create a macro that stupidly inserts an
exising pragma:
import macros, options, strformat
import fusion/matching
macro exportc_pragma(prefix: string, val: untyped): untyped =
echo prefix
if val.matches(
ProcDef[@name, Empty(), @generic, [@returnType, all @arguments],
@pragmas, _, @body]
):
# let export_name = fmt"\"{prefix}\""
let export_name = "\"oops\""
let export_pragma = newColonExpr(ident"exportc", ident(export_name))
var new_pragma = newTree(nnkPragma, export_pragma)
val.addPragma(new_pragma)
echo val.repr
return val
proc f(x: int){.exportc_pragma: "my_export_name".} =
echo "hello"
Run
as you can see I've commented out the used of the defined prefix as this gave:
`cannot create null element for: tyProxy`
without that line I just put in `"oops"`
the last echo shows the result is as I expect:
proc f(x: int) {.exportc: "oops".} =
echo "hello"
Run
but the error is `invalid pragma: {.exportc: "oops".}` and `identifier
expected, but found ' {.exportc: "oops".}'`
of course no problem with it copy and pasted
proc f(x: int) {.exportc: "oops".} =
echo "hello"
Run
would someone try explaining this to me?
Thanks a lot