Nim/tests/accept/compile/tdumpast.nim
Zahary Karadjov 91351e5996 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.
2011-09-20 14:13:45 +03:00

32 lines
600 B
Nim
Executable file

# 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 =
# 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:
proc add(x, y: int): int =
return x + y
proc sub(x, y: int): int = return x - y