Skip to content

Instantly share code, notes, and snippets.

@xtagon
Created January 19, 2014 22:05
Show Gist options
  • Save xtagon/7358e7f963526a0ef954 to your computer and use it in GitHub Desktop.
Save xtagon/7358e7f963526a0ef954 to your computer and use it in GitHub Desktop.
import macros, strutils
macro autofmt(s: string): stmt =
result = newNimNode(nnkStmtList)
var
infixNode = newNimNode(nnkInfix)
opNode = newIdentNode("%")
fmtStrNode = newStrLitNode("Hello, $#!")
bracketNode = newNimNode(nnkBracket)
bracketNode.add(newStrLitNode("World"))
infixNode.add(opNode)
infixNode.add(fmtStrNode)
infixNode.add(bracketNode)
result.add(infixNode)
echo(autofmt"Hello, World!")
# This is what we're trying to recreate (baby steps)
dumpTree:
"Hello, $#" % ["World"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment