Re: Macro that generates procs

2018-11-16 Thread bingobangobongo
thanks, I got it working without passing params to the macro function, what I 
cannot seem to figure out is how to do it when trying to dynamically generate 
the string that will get converted to a macro.


Macro that generates procs

2018-11-15 Thread bingobangobongo
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))

Run

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)

Run

generate the following output:


proc newProc*(id: int, name: string): string =
  #do some work here
  doStuff(id, name)

Run

Any ideas on how could I accomplish it?