Skip to content

Instantly share code, notes, and snippets.

@treeform
Created December 17, 2017 20:22
Show Gist options
  • Save treeform/7a41296e7c341c10c97cfe6181c7fe4c to your computer and use it in GitHub Desktop.
Save treeform/7a41296e7c341c10c97cfe6181c7fe4c to your computer and use it in GitHub Desktop.
import macros
macro print*(n: varargs[typed]): untyped =
result = newNimNode(nnkStmtList, n)
for i in 0..n.len-1:
if n[i].kind == nnkStrLit:
# pure string literals are written diretly
result.add(newCall("write", newIdentNode("stdout"), n[i]))
else:
# other expressions are written in <expression>: <value> syntax
result.add(newCall("write", newIdentNode("stdout"), toStrLit(n[i])))
result.add(newCall("write", newIdentNode("stdout"), newStrLitNode(": ")))
result.add(newCall("write", newIdentNode("stdout"), n[i]))
if i != n.len-1:
# separate by " "
result.add(newCall("write", newIdentNode("stdout"), newStrLitNode(" ")))
else:
# add newline
result.add(newCall("writeLine", newIdentNode("stdout"), newStrLitNode("")))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment