import strutils,macros
type NodeKind* = enum
nkP
type Node* = ref object
case kind: NodeKind
of nkP:
text:string
else:
discard
proc `$`(n:Node):string = $n[]
proc p *(x:string):Node = Node(kind:nkP, text: x)
macro ast(name,t:untyped): proc():Node =
result = newstmtlist( newProc(
name,
@[newidentnode("Node")],
newstmtlist(
newAssignment(newIdentNode("result"),t))
))
echo "my-ast-follows"
echo treerepr result
dumptree:
proc n (): Node =
result = p("hi")
# This works
proc n (): Node =
result = p("hi")
echo n()
#This does not
ast( s, p("hi"))
echo s()