From 15f7094fde2a5677b6566dfa1a883237cac676ec Mon Sep 17 00:00:00 2001 From: Andrea Ferretti Date: Thu, 22 Sep 2016 12:07:36 +0200 Subject: Added a dump macro for debugging --- lib/pure/future.nim | 21 +++++++++++++++++++++ tests/macros/tdump.nim | 13 +++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 tests/macros/tdump.nim diff --git a/lib/pure/future.nim b/lib/pure/future.nim index 67975cfcb..2a6d29933 100644 --- a/lib/pure/future.nim +++ b/lib/pure/future.nim @@ -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 \ No newline at end of file diff --git a/tests/macros/tdump.nim b/tests/macros/tdump.nim new file mode 100644 index 000000000..e4c14dc6b --- /dev/null +++ b/tests/macros/tdump.nim @@ -0,0 +1,13 @@ +discard """ + output: '''x = 10 +x + y = 30 +''' +""" + +import future + +let + x = 10 + y = 20 +dump x +dump(x + y) \ No newline at end of file -- cgit 1.4.1-2-gfad0 From 235fc29834bf820e720bbbfdd3fb5f0c684d53ec Mon Sep 17 00:00:00 2001 From: Andrea Ferretti Date: Thu, 22 Sep 2016 12:26:51 +0200 Subject: Added expandMacros macro --- lib/core/macros.nim | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/lib/core/macros.nim b/lib/core/macros.nim index 19452b4a8..af346e2b3 100644 --- a/lib/core/macros.nim +++ b/lib/core/macros.nim @@ -890,6 +890,30 @@ proc boolVal*(n: NimNode): bool {.compileTime, noSideEffect.} = if n.kind == nnkIntLit: n.intVal != 0 else: n == bindSym"true" # hacky solution for now +macro expandMacros*(body: stmt): untyped = + ## Expands one level of macro - useful for debugging. + ## Can be used to inspect what happens when a macro call is expanded, + ## without altering its result. + ## + ## For instance, + ## + ## .. code-block:: nim + ## import future, macros + ## + ## let + ## x = 10 + ## y = 20 + ## expandMacros: + ## dump(x + y) + ## + ## will actually dump `x + y`, but at the same time will print at + ## compile time the expansion of the ``dump`` macro, which in this + ## case is ``debugEcho ["x + y", " = ", x + y]``. + template inner(x: untyped): untyped = x + + result = getAst(inner(body)) + echo result.toStrLit + when not defined(booting): template emit*(e: static[string]): untyped {.deprecated.} = ## accepts a single string argument and treats it as nim code -- cgit 1.4.1-2-gfad0 From a9e5f5d7e226302135c6b68af274190058993759 Mon Sep 17 00:00:00 2001 From: Andrea Ferretti Date: Sun, 25 Sep 2016 15:53:32 +0200 Subject: Removed use of stmt --- lib/core/macros.nim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/core/macros.nim b/lib/core/macros.nim index af346e2b3..1a26d5522 100644 --- a/lib/core/macros.nim +++ b/lib/core/macros.nim @@ -890,7 +890,7 @@ proc boolVal*(n: NimNode): bool {.compileTime, noSideEffect.} = if n.kind == nnkIntLit: n.intVal != 0 else: n == bindSym"true" # hacky solution for now -macro expandMacros*(body: stmt): untyped = +macro expandMacros*(body: typed): untyped = ## Expands one level of macro - useful for debugging. ## Can be used to inspect what happens when a macro call is expanded, ## without altering its result. -- cgit 1.4.1-2-gfad0