tdumpast.nim 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. # Dump the contents of a NimNode
  2. import macros
  3. block:
  4. template plus(a, b: untyped): untyped {.dirty} =
  5. a + b
  6. macro call(e: untyped): untyped =
  7. result = newCall("foo", newStrLitNode("bar"))
  8. macro dumpAST(n: untyped): string =
  9. var msg = ""
  10. msg.add "lispRepr:\n" & n.lispRepr & "\n"
  11. msg.add "treeRepr:\n" & n.treeRepr & "\n"
  12. var plusAst = getAst(plus(1, 2))
  13. msg.add "lispRepr:\n" & n.lispRepr & "\n"
  14. var callAst = getAst(call(4))
  15. msg.add "callAst.lispRepr:\n" & callAst.lispRepr & "\n"
  16. var e = parseExpr("foo(bar + baz)")
  17. msg.add "e.lispRepr:\n" & e.lispRepr & "\n"
  18. result = msg.newLit
  19. let a = dumpAST:
  20. proc add(x, y: int): int =
  21. return x + y
  22. const foo = 3
  23. doAssert a == """
  24. lispRepr:
  25. (StmtList (ProcDef (Ident "add") (Empty) (Empty) (FormalParams (Ident "int") (IdentDefs (Ident "x") (Ident "y") (Ident "int") (Empty))) (Empty) (Empty) (StmtList (ReturnStmt (Infix (Ident "+") (Ident "x") (Ident "y"))))) (ConstSection (ConstDef (Ident "foo") (Empty) (IntLit 3))))
  26. treeRepr:
  27. StmtList
  28. ProcDef
  29. Ident "add"
  30. Empty
  31. Empty
  32. FormalParams
  33. Ident "int"
  34. IdentDefs
  35. Ident "x"
  36. Ident "y"
  37. Ident "int"
  38. Empty
  39. Empty
  40. Empty
  41. StmtList
  42. ReturnStmt
  43. Infix
  44. Ident "+"
  45. Ident "x"
  46. Ident "y"
  47. ConstSection
  48. ConstDef
  49. Ident "foo"
  50. Empty
  51. IntLit 3
  52. lispRepr:
  53. (StmtList (ProcDef (Ident "add") (Empty) (Empty) (FormalParams (Ident "int") (IdentDefs (Ident "x") (Ident "y") (Ident "int") (Empty))) (Empty) (Empty) (StmtList (ReturnStmt (Infix (Ident "+") (Ident "x") (Ident "y"))))) (ConstSection (ConstDef (Ident "foo") (Empty) (IntLit 3))))
  54. callAst.lispRepr:
  55. (Call (Ident "foo") (StrLit "bar"))
  56. e.lispRepr:
  57. (Call (Ident "foo") (Infix (Ident "+") (Ident "bar") (Ident "baz")))
  58. """
  59. macro fun() =
  60. let n = quote do:
  61. 1+1 == 2
  62. doAssert n.repr == "1 + 1 == 2", n.repr
  63. fun()
  64. macro fun2(): untyped =
  65. let n = quote do:
  66. 1 + 2 * 3 == 1 + 6
  67. doAssert n.repr == "1 + 2 * 3 == 1 + 6", n.repr
  68. fun2()
  69. macro fun3(): untyped =
  70. let n = quote do:
  71. int | float | array | seq | object | ptr | pointer | float32
  72. doAssert n.repr == "int | float | array | seq | object | ptr | pointer | float32", n.repr
  73. fun3()
  74. macro fun4() =
  75. let n = quote do:
  76. (a: 1)
  77. doAssert n.repr == "(a: 1)", n.repr
  78. fun4()
  79. # nkTupleConstr vs nkPar tests:
  80. block: # lispRepr
  81. macro lispRepr2(a: untyped): string = newLit a.lispRepr
  82. doAssert lispRepr2(()) == """(TupleConstr)"""
  83. doAssert lispRepr2((a: 1)) == """(TupleConstr (ExprColonExpr (Ident "a") (IntLit 1)))"""
  84. doAssert lispRepr2((a: 1, b: 2)) == """(TupleConstr (ExprColonExpr (Ident "a") (IntLit 1)) (ExprColonExpr (Ident "b") (IntLit 2)))"""
  85. doAssert lispRepr2((1,)) == """(TupleConstr (IntLit 1))"""
  86. doAssert lispRepr2((1, 2)) == """(TupleConstr (IntLit 1) (IntLit 2))"""
  87. doAssert lispRepr2((1, 2, 3.0)) == """(TupleConstr (IntLit 1) (IntLit 2) (FloatLit 3.0))"""
  88. doAssert lispRepr2((1)) == """(Par (IntLit 1))"""
  89. doAssert lispRepr2((1+2)) == """(Par (Infix (Ident "+") (IntLit 1) (IntLit 2)))"""
  90. block: # repr
  91. macro repr2(a: untyped): string = newLit a.repr
  92. doAssert repr2(()) == "()"
  93. doAssert repr2((a: 1)) == "(a: 1)"
  94. doAssert repr2((a: 1, b: 2)) == "(a: 1, b: 2)"
  95. doAssert repr2((1,)) == "(1,)"
  96. doAssert repr2((1, 2)) == "(1, 2)"
  97. doAssert repr2((1, 2, 3.0)) == "(1, 2, 3.0)"
  98. doAssert repr2((1)) == "(1)"
  99. doAssert repr2((1+2)) == "(1 + 2)"
  100. block: # treeRepr
  101. macro treeRepr2(a: untyped): string = newLit a.treeRepr
  102. macro treeRepr3(a: typed): string = newLit a.treeRepr
  103. doAssert treeRepr2(1+1 == 2) == """
  104. Infix
  105. Ident "=="
  106. Infix
  107. Ident "+"
  108. IntLit 1
  109. IntLit 1
  110. IntLit 2"""
  111. proc baz() = discard
  112. proc baz(a: int) = discard
  113. proc baz(a: float) = discard
  114. doAssert treeRepr3(baz()) == """
  115. Call
  116. Sym "baz""""
  117. let a = treeRepr3(block:
  118. proc bar(a: auto) = baz())
  119. doAssert a == """
  120. BlockStmt
  121. Empty
  122. ProcDef
  123. Sym "bar"
  124. Empty
  125. GenericParams
  126. Sym "a:type"
  127. FormalParams
  128. Empty
  129. IdentDefs
  130. Sym "a"
  131. Sym "auto"
  132. Empty
  133. Empty
  134. Bracket
  135. Empty
  136. Empty
  137. StmtList
  138. Call
  139. OpenSymChoice 3 "baz""""