Skip to content

Instantly share code, notes, and snippets.

Created August 24, 2017 10:04
Show Gist options
  • Save anonymous/ef01c377c45e615fdc163dd53d328f7f to your computer and use it in GitHub Desktop.
Save anonymous/ef01c377c45e615fdc163dd53d328f7f to your computer and use it in GitHub Desktop.
import strutils, macros
proc printImpl*(objects: openarray[string], sep = " ", endl = "\n",
file=stdout, flush=false) =
file.write(objects.join(sep))
file.write(endl)
if flush:
file.flushFile()
macro print*(data: varargs[untyped]): untyped =
var objects = newTree(nnkBracket)
var args = newTree(nnkArglist)
for arg in data:
if arg.kind == nnkExprEqExpr:
# Add keyword argument
args.add arg
else:
# Stringify automatically
objects.add newCall("$", arg)
objects = prefix(objects, "@")
result = quote do:
printImpl(`objects`, `args`)
print 5
print(1, 2, 3, 4)
print("hello world", 1, 2, sep = "XXX", endl="\n\n")
print(3, endl="test")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment