Added a dump macro for debugging

This commit is contained in:
Andrea Ferretti
2016-09-22 12:07:36 +02:00
parent 723bc158ce
commit 15f7094fde
2 changed files with 34 additions and 0 deletions

View File

@@ -177,3 +177,24 @@ macro `[]`*(lc: ListComprehension, comp, typ: untyped): untyped =
newIdentNode("@"),
newNimNode(nnkBracket))),
result))))
macro dump*(x: typed): untyped =
## Dumps the content of an expression, useful for debugging.
## It accepts any expression and prints a textual representation
## of the tree representing the expression - as it would appear in
## source code - together with the value of the expression.
##
## As an example,
##
## .. code-block:: nim
## let
## x = 10
## y = 20
## dump(x + y)
##
## will print ``x + y = 30``.
let s = x.toStrLit
let r = quote do:
debugEcho `s`, " = ", `x`
return r

13
tests/macros/tdump.nim Normal file
View File

@@ -0,0 +1,13 @@
discard """
output: '''x = 10
x + y = 30
'''
"""
import future
let
x = 10
y = 20
dump x
dump(x + y)