Given a string with the results of an `astGenRepr` in it, how do I convert it
back to a NimNode that matches the original?
Consider this:
import macros
macro astFor*(s:untyped):untyped =
result = newLit(s.astGenRepr)
macro start(): untyped =
var x = astFor:
proc foo() =
echo "hi"
echo "x: ", x
var parsed = x.parseStmt()
echo "parsed: ", parsed.astGenRepr
# why is parsed.astGenRepr not the same as x?
start
Run
I'm looking for something like `parseStmt` that would make `parsed.astGenRepr`
and `x` be the same. Instead, it produces this:
x: nnkStmtList.newTree(
nnkProcDef.newTree(
newIdentNode("foo"),
newEmptyNode(),
newEmptyNode(),
nnkFormalParams.newTree(
newEmptyNode()
),
newEmptyNode(),
newEmptyNode(),
nnkStmtList.newTree(
nnkCommand.newTree(
newIdentNode("echo"),
newLit("hi")
)
)
)
)
parsed: nnkStmtList.newTree(
nnkCall.newTree(
nnkDotExpr.newTree(
newIdentNode("nnkStmtList"),
newIdentNode("newTree")
),
nnkCall.newTree(
nnkDotExpr.newTree(
newIdentNode("nnkProcDef"),
newIdentNode("newTree")
),
nnkCall.newTree(
newIdentNode("newIdentNode"),
newLit("foo")
),
nnkCall.newTree(
newIdentNode("newEmptyNode")
),
nnkCall.newTree(
newIdentNode("newEmptyNode")
),
nnkCall.newTree(
nnkDotExpr.newTree(
newIdentNode("nnkFormalParams"),
newIdentNode("newTree")
),
nnkCall.newTree(
newIdentNode("newEmptyNode")
)
),
nnkCall.newTree(
newIdentNode("newEmptyNode")
),
nnkCall.newTree(
newIdentNode("newEmptyNode")
),
nnkCall.newTree(
nnkDotExpr.newTree(
newIdentNode("nnkStmtList"),
newIdentNode("newTree")
),
nnkCall.newTree(
nnkDotExpr.newTree(
newIdentNode("nnkCommand"),
newIdentNode("newTree")
),
nnkCall.newTree(
newIdentNode("newIdentNode"),
newLit("echo")
),
nnkCall.newTree(
newIdentNode("newLit"),
newLit("hi")
)
)
)
)
)
)
Run