Skip to content

Commit a1a397a

Browse files
committed
Fix ast.Dump to output to be the same as Python
1 parent dc4ddaa commit a1a397a

File tree

4 files changed

+48
-45
lines changed

4 files changed

+48
-45
lines changed

ast/ast.go

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -88,17 +88,17 @@ const (
8888
func (o ExprContext) String() string {
8989
switch o {
9090
case Load:
91-
return "Load"
91+
return "Load()"
9292
case Store:
93-
return "Store"
93+
return "Store()"
9494
case Del:
95-
return "Del"
95+
return "Del()"
9696
case AugLoad:
97-
return "AugLoad"
97+
return "AugLoad()"
9898
case AugStore:
99-
return "AugStore"
99+
return "AugStore()"
100100
case Param:
101-
return "Param"
101+
return "Param()"
102102
}
103103
return fmt.Sprintf("UnknownExprContext(%d)", o)
104104
}
@@ -113,9 +113,9 @@ const (
113113
func (o BoolOpNumber) String() string {
114114
switch o {
115115
case And:
116-
return "And"
116+
return "And()"
117117
case Or:
118-
return "Or"
118+
return "Or()"
119119
}
120120
return fmt.Sprintf("UnknownBoolOpNumber(%d)", o)
121121
}
@@ -140,29 +140,29 @@ const (
140140
func (o OperatorNumber) String() string {
141141
switch o {
142142
case Add:
143-
return "Add"
143+
return "Add()"
144144
case Sub:
145-
return "Sub"
145+
return "Sub()"
146146
case Mult:
147-
return "Mult"
147+
return "Mult()"
148148
case Div:
149-
return "Div"
149+
return "Div()"
150150
case Modulo:
151-
return "Modulo"
151+
return "Modulo()"
152152
case Pow:
153-
return "Pow"
153+
return "Pow()"
154154
case LShift:
155-
return "LShift"
155+
return "LShift()"
156156
case RShift:
157-
return "RShift"
157+
return "RShift()"
158158
case BitOr:
159-
return "BitOr"
159+
return "BitOr()"
160160
case BitXor:
161-
return "BitXor"
161+
return "BitXor()"
162162
case BitAnd:
163-
return "BitAnd"
163+
return "BitAnd()"
164164
case FloorDiv:
165-
return "FloorDiv"
165+
return "FloorDiv()"
166166
}
167167
return fmt.Sprintf("UnknownOperatorNumber(%d)", o)
168168
}
@@ -179,13 +179,13 @@ const (
179179
func (o UnaryOpNumber) String() string {
180180
switch o {
181181
case Invert:
182-
return "Invert"
182+
return "Invert()"
183183
case Not:
184-
return "Not"
184+
return "Not()"
185185
case UAdd:
186-
return "UAdd"
186+
return "UAdd()"
187187
case USub:
188-
return "USub"
188+
return "USub()"
189189
}
190190
return fmt.Sprintf("UnknownUnaryOpNumber(%d)", o)
191191
}
@@ -208,25 +208,25 @@ const (
208208
func (o CmpOp) String() string {
209209
switch o {
210210
case Eq:
211-
return "Eq"
211+
return "Eq()"
212212
case NotEq:
213-
return "NotEq"
213+
return "NotEq()"
214214
case Lt:
215-
return "Lt"
215+
return "Lt()"
216216
case LtE:
217-
return "LtE"
217+
return "LtE()"
218218
case Gt:
219-
return "Gt"
219+
return "Gt()"
220220
case GtE:
221-
return "GtE"
221+
return "GtE()"
222222
case Is:
223-
return "Is"
223+
return "Is()"
224224
case IsNot:
225-
return "IsNot"
225+
return "IsNot()"
226226
case In:
227-
return "In"
227+
return "In()"
228228
case NotIn:
229-
return "NotIn"
229+
return "NotIn()"
230230
}
231231
return fmt.Sprintf("UnknownCmoOp(%d)", o)
232232
}

ast/dump.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,16 @@ func Dump(ast Ast) string {
1414
return "<nil>"
1515
}
1616
name := ast.Type().Name
17+
if name == "ExprStmt" {
18+
name = "Expr"
19+
}
1720
astValue := reflect.Indirect(reflect.ValueOf(ast))
1821
astType := astValue.Type()
1922
args := make([]string, 0)
2023
for i := 0; i < astType.NumField(); i++ {
2124
fieldType := astType.Field(i)
2225
fieldValue := astValue.Field(i)
23-
fname := fieldType.Name
26+
fname := strings.ToLower(fieldType.Name)
2427
if fieldValue.Kind() == reflect.Slice {
2528
strs := make([]string, fieldValue.Len())
2629
for i := 0; i < fieldValue.Len(); i++ {
@@ -35,7 +38,7 @@ func Dump(ast Ast) string {
3538
strs[i] = fmt.Sprintf("%v", element)
3639
}
3740
}
38-
args = append(args, fmt.Sprintf("%s=[%s]", fname, strings.Join(strs, ",")))
41+
args = append(args, fmt.Sprintf("%s=[%s]", fname, strings.Join(strs, ", ")))
3942
} else if fieldValue.CanInterface() {
4043
v := fieldValue.Interface()
4144
switch x := v.(type) {
@@ -53,5 +56,5 @@ func Dump(ast Ast) string {
5356
}
5457
}
5558
}
56-
return fmt.Sprintf("%s(%s)", name, strings.Join(args, ","))
59+
return fmt.Sprintf("%s(%s)", name, strings.Join(args, ", "))
5760
}

ast/dump_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ func TestDump(t *testing.T) {
1313
}{
1414
{nil, `<nil>`},
1515
{&Pass{}, `Pass()`},
16-
{&Str{S: py.String("potato")}, `Str(S="potato")`},
17-
{&Str{S: py.String("potato")}, `Str(S="potato")`},
16+
{&Str{S: py.String("potato")}, `Str(s="potato")`},
17+
{&Str{S: py.String("potato")}, `Str(s="potato")`},
1818
{&BinOp{Left: &Str{S: py.String("one")}, Op: Add, Right: &Str{S: py.String("two")}},
19-
`BinOp(Left=Str(S="one"),Op=Add,Right=Str(S="two"))`},
20-
{&Module{}, `Module(Body=[])`},
21-
{&Module{Body: []Stmt{&Pass{}}}, `Module(Body=[Pass()])`},
22-
{&Module{Body: []Stmt{&ExprStmt{Value: &Tuple{}}}}, `Module(Body=[ExprStmt(Value=Tuple(Elts=[],Ctx=UnknownExprContext(0)))])`},
19+
`BinOp(left=Str(s="one"), op=Add(), right=Str(s="two"))`},
20+
{&Module{}, `Module(body=[])`},
21+
{&Module{Body: []Stmt{&Pass{}}}, `Module(body=[Pass()])`},
22+
{&Module{Body: []Stmt{&ExprStmt{Value: &Tuple{}}}}, `Module(body=[Expr(value=Tuple(elts=[], ctx=UnknownExprContext(0)))])`},
2323
} {
2424
out := Dump(test.in)
2525
if out != test.out {

parser/grammar.y

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1059,7 +1059,7 @@ strings:
10591059
atom:
10601060
'(' ')'
10611061
{
1062-
$$ = &ast.Tuple{ExprBase: ast.ExprBase{$<pos>$}} // FIXME Ctx
1062+
$$ = &ast.Tuple{ExprBase: ast.ExprBase{$<pos>$}, Ctx: ast.Load}
10631063
}
10641064
| '(' yield_expr ')'
10651065
{
@@ -1073,7 +1073,7 @@ atom:
10731073
}
10741074
| '[' ']'
10751075
{
1076-
$$ = &ast.List{ExprBase: ast.ExprBase{$<pos>$}}
1076+
$$ = &ast.List{ExprBase: ast.ExprBase{$<pos>$}, Ctx: ast.Load}
10771077
}
10781078
| '[' testlist_comp ']'
10791079
{

0 commit comments

Comments
 (0)