Initial implementation of the parseExpr, parseStmt, getAst macro routines:

proc parseExpr*(s: string) : expr {.magic: "ParseExprToAst".}
  ## Compiles the passed string to its AST representation
  ## Expects a single expression

proc parseStmt*(s: string) : stmt {.magic: "ParseStmtToAst".}
  ## Compiles the passed string to its AST representation
  ## Expects one or more statements

proc getAst*(macroOrTemplate: expr): expr {.magic: "ExpandMacroToAst".}
  ## Obtains the AST nodes returned from a macro or template invocation
  ## example:
  ## macro FooMacro() = 
  ##   var ast = getAst(BarTemplate())

Handling of the node.toYaml magic moved to the evaluation engine.
This commit is contained in:
Zahary Karadjov 2011-09-07 04:25:18 +03:00
commit 91351e5996
6 changed files with 144 additions and 37 deletions

View file

@ -1,12 +1,27 @@
# Dump the contents of a PNimrodNode
import macros
template plus(a, b: expr): expr =
a + b
macro call(e: expr): expr =
return newCall("foo", newStrLitNode("bar"))
macro dumpAST(n: stmt): stmt =
macro dumpAST(n: stmt): stmt =
# dump AST as a side-effect and return the inner node
echo n.prettyPrint
echo n.toYaml
var plusAst = getAst(plus(1, 2))
echo plusAst.prettyPrint
var callAst = getAst(call())
echo callAst.prettyPrint
var e = parseExpr("foo(bar + baz)")
echo e.prettyPrint
result = n[1]
dumpAST:
@ -15,4 +30,3 @@ dumpAST:
proc sub(x, y: int): int = return x - y