How to print Nim's AST at runtime?

pietroppeter

dumpTree and similar macros print Nim's AST at compile time. I was wondering how to print the same content at runtime. The use case is to be able to capture the output to document it in a nimib document.

To have a concrete example:

import macros

dumpTree:
  25 ⬇️
  10 ➖

produces at compile time:

StmtList
  Command
    IntLit 25
    Ident "⬇️"
  Command
    IntLit 10
    Ident "➖"

I would like a dumpTreeRuntime that is equivalent for the above case to:

echo """
StmtList
  Command
    IntLit 25
    Ident "⬇️"
  Command
    IntLit 10
    Ident "➖"
"""
jtv
import macros


macro treeToString(code: untyped): untyped =
   return newLit(treeRepr(code))


when isMainModule:
  let x = treeToString:
    25 ⬇️
    10 ➖

   echo(x)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related