Hi, I recently started toying with templates and macros in nim and even though they seem very powerful I haven't been able to correctly use macros yet.
I'm trying to create a macro that would generate a proc with some fixed parts in its body when being called, something in the lines of (not working code):
import macros, strformat
macro genProc(name, types, worker, workerParms: string): untyped =
let body = &"""
proc {name}*({types}): string =
# do some work here...
{worker}({workerParams})
"""
result = newStmtList()
result.add(parseStmt(body))
That hypothetical macro should, when called like this:
let procName = "newProc"
let procTypes = "id: int, name: string"
let workerProc = "doStuff"
let workerParams = "id, name"
genProc(procName, procTypes, workerProc, workerParams)
generate the following output:
proc newProc*(id: int, name: string): string =
#do some work here
doStuff(id, name)
Any ideas on how could I accomplish it?
More examples on the same folder.