@@ -8,59 +8,67 @@ import (
8
8
"github.com/ncw/gpython/py"
9
9
)
10
10
11
- // Dump an Ast node as a string
12
- func Dump (ast Ast ) string {
13
- if ast == nil {
14
- return "<nil>"
15
- }
16
- name := ast .Type ().Name
17
- if name == "ExprStmt" {
18
- name = "Expr"
11
+ func dumpItem (v interface {}) string {
12
+ switch x := v .(type ) {
13
+ case py.String :
14
+ return fmt .Sprintf ("'%s'" , string (x ))
15
+ case py.Bytes :
16
+ return fmt .Sprintf ("b'%s'" , string (x ))
17
+ case Identifier :
18
+ return fmt .Sprintf ("'%s'" , string (x ))
19
+ case ModBase :
20
+ case StmtBase :
21
+ case ExprBase :
22
+ case SliceBase :
23
+ case Pos :
24
+ case Ast :
25
+ return Dump (x )
26
+ case py.I__str__ :
27
+ return string (x .M__str__ ().(py.String ))
28
+ case Comprehension :
29
+ return dump (v , "comprehension" )
19
30
}
31
+ return fmt .Sprintf ("%v" , v )
32
+ }
33
+
34
+ // Dump ast as a string with name
35
+ func dump (ast interface {}, name string ) string {
20
36
astValue := reflect .Indirect (reflect .ValueOf (ast ))
21
37
astType := astValue .Type ()
22
38
args := make ([]string , 0 )
23
39
for i := 0 ; i < astType .NumField (); i ++ {
24
40
fieldType := astType .Field (i )
25
41
fieldValue := astValue .Field (i )
26
42
fname := strings .ToLower (fieldType .Name )
43
+ if fname == "stmtbase" || fname == "exprbase" || fname == "modbase" {
44
+ continue
45
+ }
27
46
if fieldValue .Kind () == reflect .Slice && fieldValue .Type ().Elem ().Kind () != reflect .Uint8 {
28
47
strs := make ([]string , fieldValue .Len ())
29
48
for i := 0 ; i < fieldValue .Len (); i ++ {
30
49
element := fieldValue .Index (i )
31
50
if element .CanInterface () {
32
- if x , ok := element .Interface ().(Ast ); ok {
33
- strs [i ] = Dump (x )
34
- } else {
35
- strs [i ] = fmt .Sprintf ("%v" , element )
36
- }
37
- } else {
38
- strs [i ] = fmt .Sprintf ("%v" , element )
51
+ v := element .Interface ()
52
+ strs [i ] = dumpItem (v )
39
53
}
40
54
}
41
55
args = append (args , fmt .Sprintf ("%s=[%s]" , fname , strings .Join (strs , ", " )))
42
56
} else if fieldValue .CanInterface () {
43
57
v := fieldValue .Interface ()
44
- switch x := v .(type ) {
45
- case py.String :
46
- args = append (args , fmt .Sprintf ("%s='%s'" , fname , string (x )))
47
- case py.Bytes :
48
- args = append (args , fmt .Sprintf ("%s=b'%s'" , fname , string (x )))
49
- case Identifier :
50
- args = append (args , fmt .Sprintf ("%s='%s'" , fname , string (x )))
51
- case ModBase :
52
- case StmtBase :
53
- case ExprBase :
54
- case SliceBase :
55
- case Pos :
56
- case Ast :
57
- args = append (args , fmt .Sprintf ("%s=%s" , fname , Dump (x )))
58
- case py.I__str__ :
59
- args = append (args , fmt .Sprintf ("%s=%s" , fname , x .M__str__ ()))
60
- default :
61
- args = append (args , fmt .Sprintf ("%s=%v" , fname , x ))
62
- }
58
+ args = append (args , fmt .Sprintf ("%s=%s" , fname , dumpItem (v )))
63
59
}
64
60
}
65
61
return fmt .Sprintf ("%s(%s)" , name , strings .Join (args , ", " ))
66
62
}
63
+
64
+ // Dump an Ast node as a string
65
+ func Dump (ast Ast ) string {
66
+ if ast == nil {
67
+ return "<nil>"
68
+ }
69
+ name := ast .Type ().Name
70
+ if name == "ExprStmt" {
71
+ name = "Expr"
72
+ }
73
+ return dump (ast , name )
74
+ }
0 commit comments