Skip to content

Commit

Permalink
add a printer to get a better understanding of how things are nested
Browse files Browse the repository at this point in the history
  • Loading branch information
jmoiron committed May 29, 2014
1 parent bdc97d4 commit 6241024
Showing 1 changed file with 35 additions and 1 deletion.
36 changes: 35 additions & 1 deletion neo/parse_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package jigo

import "testing"
import (
"fmt"
"testing"

"github.com/davecgh/go-spew/spew"
)

func (n NodeType) String() string {
switch n {
Expand All @@ -15,6 +20,34 @@ func (n NodeType) String() string {
}
}

// Recurisvely spew a whole tree of nodes.
func spewTree(n Node, indent string) {
switch n.(type) {
case *ListNode:
n := n.(*ListNode)
fmt.Printf("%s(ListNode) {\n", indent)
for _, n := range n.Nodes {
spewTree(n, indent+" ")
}
fmt.Printf("%s}\n", indent)
case *VarNode:
n := n.(*VarNode)
fmt.Printf("%s(VarNode) {\n", indent)
spewTree(n.Node, indent+" ")
fmt.Printf("%s}\n", indent)
case *AddExpr:
n := n.(*AddExpr)
fmt.Printf("%s(AddExpr) {\n", indent)
spewTree(n.lhs, indent+" ")
fmt.Printf("%s +\n", indent)
spewTree(n.rhs, indent+" ")
fmt.Printf("%s}\n", indent)
default:
fmt.Printf(indent)
spew.Dump(n)
}
}

type parseTest struct {
isError bool
nodeTypes []NodeType
Expand Down Expand Up @@ -65,6 +98,7 @@ func (p *parsetest) Test(input string, test parseTest) {
t.Errorf("Type mismatch: expecting %dth to be %s, but was %s", i, nt, rnt)
}
}
spewTree(tree.Root, "")
t.Log(tree.Root)
}

Expand Down

0 comments on commit 6241024

Please sign in to comment.