mirror of
https://github.com/nim-lang/Nim.git
synced 2026-06-04 10:54:42 +00:00
doc: tut2: simplify debug macro example
This commit is contained in:
@@ -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]
|
||||
|
||||
Reference in New Issue
Block a user