-
-
Save xtagon/7358e7f963526a0ef954 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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