helper templates static, eval and emit for easier compile-time code evaluation
This commit is contained in:
parent
96e7ee91cc
commit
a3f16968a7
3 changed files with 34 additions and 1 deletions
|
|
@ -208,7 +208,18 @@ proc getAst*(macroOrTemplate: expr): expr {.magic: "ExpandToAst".}
|
|||
##
|
||||
## macro FooMacro() =
|
||||
## var ast = getAst(BarTemplate())
|
||||
|
||||
|
||||
template emit*(s: expr): stmt =
|
||||
## accepts a single sting argument and treats it as nimrod code
|
||||
## that should be inserted verbatim in the program
|
||||
## Example:
|
||||
##
|
||||
## emit("echo " & '"' & "hello world".toUpper & '"')
|
||||
##
|
||||
block:
|
||||
const evaluated = s
|
||||
eval: result = evaluated.parseStmt
|
||||
|
||||
proc expectKind*(n: PNimrodNode, k: TNimrodNodeKind) {.compileTime.} =
|
||||
## checks that `n` is of kind `k`. If this is not the case,
|
||||
## compilation aborts with an error message. This is useful for writing
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue