tdumpast.nim 631 B

12345678910111213141516171819202122232425262728293031323334
  1. # Dump the contents of a NimNode
  2. import macros
  3. template plus(a, b: untyped): untyped {.dirty} =
  4. a + b
  5. macro call(e: untyped): untyped =
  6. result = newCall("foo", newStrLitNode("bar"))
  7. macro dumpAST(n: untyped): untyped =
  8. # dump AST as a side-effect and return the inner node
  9. let n = callsite()
  10. echo n.lispRepr
  11. echo n.treeRepr
  12. var plusAst = getAst(plus(1, 2))
  13. echo plusAst.lispRepr
  14. var callAst = getAst(call(4))
  15. echo callAst.lispRepr
  16. var e = parseExpr("foo(bar + baz)")
  17. echo e.lispRepr
  18. result = n[1]
  19. dumpAST:
  20. proc add(x, y: int): int =
  21. return x + y
  22. proc sub(x, y: int): int = return x - y