55 lines
1.2 KiB
Go
55 lines
1.2 KiB
Go
package main
|
|
|
|
type debugVisitor struct{}
|
|
|
|
func debug(node node) string {
|
|
d := new(debugVisitor)
|
|
v, _ := node.accept(d)
|
|
return v
|
|
}
|
|
|
|
// visitASTDefinitionsNode implements visitor.
|
|
func (d *debugVisitor) visitASTDefinitionsNode(a *astDefinitionsNode) (string, error) {
|
|
v := "#" + a.name + "\n\n"
|
|
|
|
for _, defNode := range a.definitions {
|
|
def, _ := defNode.accept(d)
|
|
v += def + "\n\n"
|
|
}
|
|
|
|
return v, nil
|
|
}
|
|
|
|
// visitDefinition implements visitor.
|
|
func (d *debugVisitor) visitDefinition(def *definitionNode) (string, error) {
|
|
id, _ := def.identifier.accept(d)
|
|
v := id + " [\n"
|
|
|
|
for _, fNode := range def.fields {
|
|
field, _ := fNode.accept(d)
|
|
v += "\t" + field + "\n"
|
|
}
|
|
|
|
v += "]\n\n"
|
|
|
|
return v, nil
|
|
}
|
|
|
|
// visitField implements visitor.
|
|
func (d *debugVisitor) visitField(g *fieldNode) (string, error) {
|
|
left, _ := g.left.accept(d)
|
|
right, _ := g.right.accept(d)
|
|
return left + " = " + right + ";", nil
|
|
}
|
|
|
|
// visitIdentifier implements visitor.
|
|
func (d *debugVisitor) visitIdentifier(i *identifierNode) (string, error) {
|
|
return i.value, nil
|
|
}
|
|
|
|
// visitName implements visitor.
|
|
func (d *debugVisitor) visitName(n *nameNode) (string, error) {
|
|
return n.value, nil
|
|
}
|
|
|
|
var _ visitor = new(debugVisitor)
|