tdumpast2.nim 945 B

12345678910111213141516171819202122232425262728293031323334353637
  1. # Dump the contents of a NimNode
  2. import macros
  3. proc dumpit(n: NimNode): string {.compileTime.} =
  4. if n == nil: return "nil"
  5. result = $n.kind
  6. add(result, "(")
  7. case n.kind
  8. of nnkEmpty: discard # same as nil node in this representation
  9. of nnkNilLit: add(result, "nil")
  10. of nnkCharLit..nnkInt64Lit: add(result, $n.intVal)
  11. of nnkFloatLit..nnkFloat64Lit: add(result, $n.floatVal)
  12. of nnkStrLit..nnkTripleStrLit: add(result, $n.strVal)
  13. of nnkIdent: add(result, $n.ident)
  14. of nnkSym, nnkNone: assert false
  15. else:
  16. add(result, dumpit(n[0]))
  17. for j in 1..n.len-1:
  18. add(result, ", ")
  19. add(result, dumpit(n[j]))
  20. add(result, ")")
  21. macro dumpAST(n): untyped =
  22. # dump AST as a side-effect and return the inner node
  23. let n = callsite()
  24. echo dumpit(n)
  25. result = n[1]
  26. dumpAST:
  27. proc add(x, y: int): int =
  28. return x + y
  29. proc sub(x, y: int): int = return x - y