blob: 4ceeb525e075c871c179df9b66f092121fa4d36a (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
package main
import (
"fmt"
)
type Tree struct {
name string
args []interface{}
}
func NewTree(name string, args ...interface{}) *Tree {
return &Tree{name, args}
}
func (t *Tree) String() string {
s := "(" + t.name
for _, arg := range t.args {
if arg, ok := arg.(*Tree); ok {
s += " " + (*arg).String()
continue
}
if arg, ok := arg.(string); ok {
s += fmt.Sprintf(" %q", arg)
continue
}
s += fmt.Sprintf(" %v", arg)
}
return s + ")"
}
func (t *Tree) Visit(nodename string, action func(t *Tree)) {
if t.name == nodename {
action(t)
}
for _, arg := range t.args {
if child, ok := arg.(*Tree); ok {
child.Visit(nodename, action)
}
}
}
|