mirror of
https://github.com/nim-lang/Nim.git
synced 2025-12-30 09:54:49 +00:00
30 lines
924 B
Nim
30 lines
924 B
Nim
discard """
|
|
output: '''a[0]: 42
|
|
a[1]: 45
|
|
x: some string'''
|
|
"""
|
|
|
|
import macros
|
|
|
|
macro debug(n: varargs[untyped]): untyped =
|
|
# `n` is a Nim AST that contains the whole macro invocation
|
|
# this macro returns a list of statements:
|
|
result = newNimNode(nnkStmtList, n)
|
|
# iterate over any argument that is passed to this macro:
|
|
for i in 0..n.len-1:
|
|
# add a call to the statement list that writes the expression;
|
|
# `toStrLit` converts an AST to its string representation:
|
|
add(result, newCall("write", newIdentNode("stdout"), toStrLit(n[i])))
|
|
# add a call to the statement list that writes ": "
|
|
add(result, newCall("write", newIdentNode("stdout"), newStrLitNode(": ")))
|
|
# add a call to the statement list that writes the expressions value:
|
|
add(result, newCall("writeLine", newIdentNode("stdout"), n[i]))
|
|
|
|
var
|
|
a: array[0..10, int]
|
|
x = "some string"
|
|
a[0] = 42
|
|
a[1] = 45
|
|
|
|
debug(a[0], a[1], x)
|