8000 Add []Ast to ast.Dump · go-python/gpython@a112b80 · GitHub
[go: up one dir, main page]

Skip to content

Commit a112b80

Browse files
committed
Add []Ast to ast.Dump
1 parent 5084eeb commit a112b80

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

ast/dump.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
)
1010

1111
// Dump an Ast node as a string
12-
func AstDump(ast Ast) string {
12+
func Dump(ast Ast) string {
1313
if ast == nil {
1414
return "<nil>"
1515
}
@@ -21,7 +21,18 @@ func AstDump(ast Ast) string {
2121
fieldType := astType.Field(i)
2222
fieldValue := astValue.Field(i)
2323
fname := fieldType.Name
24-
if fieldValue.CanInterface() {
24+
if fieldValue.Kind() == reflect.Slice {
25+
strs := make([]string, fieldValue.Len())
26+
for i := 0; i < fieldValue.Len(); i++ {
27+
element := fieldValue.Index(i)
28+
if element.CanInterface() {
29+
if x, ok := element.Interface().(Ast); ok {
30+
strs[i] = Dump(x)
31+
}
32+
}
33+
}
34+
args = append(args, fmt.Sprintf("%s=[%s]", fname, strings.Join(strs, ",")))
35+
} else if fieldValue.CanInterface() {
2536
v := fieldValue.Interface()
2637
switch x := v.(type) {
2738
case py.String:
@@ -32,7 +43,7 @@ func AstDump(ast Ast) string {
3243
case SliceBase:
3344
case Pos:
3445
case Ast:
35-
args = append(args, fmt.Sprintf("%s=%s", fname, AstDump(x)))
46+
args = append(args, fmt.Sprintf("%s=%s", fname, Dump(x)))
3647
default:
3748
args = append(args, fmt.Sprintf("%s=%v", fname, x))
3849
}

ast/dump_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@ func TestDump(t *testing.T) {
1717
{&Str{S: py.String("potato")}, `Str(S="potato")`},
1818
{&BinOp{Left: &Str{S: py.String("one")}, Op: Add, Right: &Str{S: py.String("two")}},
1919
`BinOp(Left=Str(S="one"),Op=Add,Right=Str(S="two"))`},
20+
{&Module{}, `Module(Body=[])`},
21+
{&Module{Body: []Stmt{&Pass{}}}, `Module(Body=[Pass()])`},
2022
} {
21-
out := AstDump(test.in)
23+
out := Dump(test.in)
2224
if out != test.out {
23-
t.Errorf("AstDump(%#v) got %q expected %q", test.in, out, test.out)
25+
t.Errorf("Dump(%#v) got %q expected %q", test.in, out, test.out)
2426
}
2527
}
2628
}

0 commit comments

Comments
 (0)
0