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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
package ast
import (
"fmt"
"reflect"
)
// PrintAST prints ast to stdout.
func PrintAST(n interface{}, indent string) {
if n == nil {
fmt.Printf("%snil\n", indent)
return
}
typeOf := reflect.TypeOf(n)
valueOf := reflect.ValueOf(n)
name := ""
if typeOf.Kind() == reflect.Ptr {
if valueOf.IsNil() {
fmt.Printf("%snil\n", indent)
return
}
name = "*"
valueOf = valueOf.Elem()
typeOf = typeOf.Elem()
}
name = name + typeOf.Name()
if op, ok := n.(Optional); ok {
fmt.Printf("%s%s", indent, name)
m := op.GetOption()
if m == nil {
fmt.Printf("\n")
return
}
fmt.Printf(":\n")
for k := range m {
fmt.Printf("%s %s:\n", indent, k)
PrintAST(m[k], indent+" ")
}
return
}
if op, ok := n.(UnaryOperator); ok {
fmt.Printf("%s%s:\n", indent, name)
fmt.Printf("%s Target:\n", indent)
PrintAST(op.GetTarget(), indent+" ")
return
}
if op, ok := n.(BinaryOperator); ok {
fmt.Printf("%s%s:\n", indent, name)
fmt.Printf("%s Object:\n", indent)
PrintAST(op.GetObject(), indent+" ")
fmt.Printf("%s Subject:\n", indent)
PrintAST(op.GetSubject(), indent+" ")
return
}
if arr, ok := n.([]interface{}); ok {
fmt.Printf("%s[\n", indent)
for idx := range arr {
PrintAST(arr[idx], indent+" ")
}
fmt.Printf("%s]\n", indent)
return
}
if stringer, ok := n.(fmt.Stringer); ok {
fmt.Printf("%s%s: %s\n", indent, name, stringer.String())
return
}
fmt.Printf("%s%s: %+v\n", indent, name, valueOf.Interface())
}
|