doc: tut2: simplify debug macro example
This commit is contained in:
parent
21433477df
commit
6a0d288cae
1 changed files with 5 additions and 4 deletions
|
|
@ -674,17 +674,18 @@ variable number of arguments:
|
||||||
|
|
||||||
macro debug(n: varargs[expr]): stmt =
|
macro debug(n: varargs[expr]): stmt =
|
||||||
# `n` is a Nim AST that contains a list of expressions;
|
# `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)
|
result = newNimNode(nnkStmtList, n)
|
||||||
# iterate over any argument that is passed to this macro:
|
# 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;
|
# add a call to the statement list that writes the expression;
|
||||||
# `toStrLit` converts an AST to its string representation:
|
# `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 ": "
|
# add a call to the statement list that writes ": "
|
||||||
result.add(newCall("write", newIdentNode("stdout"), newStrLitNode(": ")))
|
result.add(newCall("write", newIdentNode("stdout"), newStrLitNode(": ")))
|
||||||
# add a call to the statement list that writes the expressions value:
|
# 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
|
var
|
||||||
a: array[0..10, int]
|
a: array[0..10, int]
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue