Quick actions

cmd+k|ctrl+k

Navigation

Languages

Test

Snippet info

Language

Nim

Visibility

unlisted

Author

legacy-anonymous

Created

2017-02-05T20:29:09Z

Updated

2017-02-05T20:29:09Z

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()
INFO