I'm trying to expand a template and write the resulting code into a nim file. 
I've tried this approach, inspired by `expandMacros`: 
    
    
    import macros
    
    template test(msg: string, body: untyped): typed =
      body
      proc testproc(s: string) =
        echo s & ": " & $rows
      testproc(msg)
    
    macro wrapper(body: typed): untyped =
      template inner(x: untyped): untyped = x
      let
        ast = getAst(inner(body))
        code = $toStrLit(ast)
      writeFile("result.nim", code)
    
    static:
      wrapper():
        test("string"):
          let rows = 9
    

This sort of works: the file `result.nim` contains recognizable nim code, but 
it is not properly indented and has weird features: 
    
    
    let rows = 9
      proc testproc(s150024: string) =
      echo [&(s150024, ": ", $ rows)]
      
      testproc("string")
    

Needless to say it does not compile.

I've looked into the code of `c2nim` and it takes a different approach, 
importing `compiler` modules and using `renderModule`. I've tried that too but 
I can't make it work. It seems that `renderModule` expects a `PNode` and 
`getAst` returns a `NimNode`, but I don't understand the details.

Reply via email to