doc: tut2: simplify debug macro example

This commit is contained in:
Simon Ruderich
2016-09-15 04:21:25 +02:00
parent 21433477df
commit 6a0d288cae

View File

@@ -674,17 +674,18 @@ variable number of arguments:
macro debug(n: varargs[expr]): stmt =
# `n` is a Nim AST that contains a list of expressions;
# this macro returns a list of statements:
# this macro returns a list of statements (n is passed for proper line
# information):
result = newNimNode(nnkStmtList, n)
# iterate over any argument that is passed to this macro:
for i in 0..n.len-1:
for x in n:
# add a call to the statement list that writes the expression;
# `toStrLit` converts an AST to its string representation:
result.add(newCall("write", newIdentNode("stdout"), toStrLit(n[i])))
result.add(newCall("write", newIdentNode("stdout"), toStrLit(x)))
# add a call to the statement list that writes ": "
result.add(newCall("write", newIdentNode("stdout"), newStrLitNode(": ")))
# add a call to the statement list that writes the expressions value:
result.add(newCall("writeLine", newIdentNode("stdout"), n[i]))
result.add(newCall("writeLine", newIdentNode("stdout"), x))
var
a: array[0..10, int]