Usually when writing macros, I code the helper procs that traverse the AST 
returning `NimNode`, as I find returning easier to reason with:
    
    
    proc x(n: NimNode): NimNode =
      case n.kind
      of nnkIdent:
        result = newStmtList(ident(n.strVal))
      else:
        result = copyNimNode(n)
        for ni in n:
          result.add x(ni)
    
    
    Run

However I recently hit a case, that I think it can't be worked easily in this 
way. I have two NimNodes that need to be merged and am wondering if it'd be 
easier to refactor my code to use an output parameter, than any other way?
    
    
    proc x(n: NimNode): NimNode =
      case n.kind
      of nnkIdent:
        result = newStmtList()
        if eqIdent(n, "foo"):
          result.add x(n) # This adds a StmtList to the previous one
        result.add ident(n.strVal)
      else:
        result = copyNimNode(n)
        for ni in n:
          result.add x(ni)
    
    
    Run

Reply via email to