Skip to content

Commit 18a2b9a

Browse files
nimdraugsaelantonmedv
authored andcommitted
dev(pretty_print): remove reduntant parenthesis
1 parent d895935 commit 18a2b9a

File tree

4 files changed

+99
-29
lines changed

4 files changed

+99
-29
lines changed

eval_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ var evalErrorTests = []evalErrorTest{
350350
{
351351
`1 matches "1" ~ "2"`,
352352
nil,
353-
"operator matches doesn't defined on (float64, string): (1 matches (\"1\" ~ \"2\"))",
353+
"operator matches doesn't defined on (float64, string): (1 matches \"1\" ~ \"2\")",
354354
},
355355
{
356356
`1 matches "1"`,

print.go

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,39 @@ func (n unaryNode) String() string {
4141
}
4242

4343
func (n binaryNode) String() string {
44-
return fmt.Sprintf("(%v %v %v)", n.left, n.operator, n.right)
44+
var leftOp, rightOp *info
45+
op := binaryOperators[n.operator]
46+
47+
switch n.left.(type) {
48+
case binaryNode:
49+
v := binaryOperators[n.left.(binaryNode).operator]
50+
leftOp = &v
51+
}
52+
switch n.right.(type) {
53+
case binaryNode:
54+
v := binaryOperators[n.right.(binaryNode).operator]
55+
rightOp = &v
56+
}
57+
58+
l, r := fmt.Sprintf("%v", n.left), fmt.Sprintf("%v", n.right)
59+
60+
if leftOp != nil {
61+
if leftOp.precedence < op.precedence && op.associativity == left {
62+
l = fmt.Sprintf("(%v)", n.left)
63+
} else if leftOp.precedence >= op.precedence && op.associativity == right {
64+
l = fmt.Sprintf("(%v)", n.left)
65+
}
66+
}
67+
68+
if rightOp != nil {
69+
if rightOp.precedence < op.precedence && op.associativity == left {
70+
r = fmt.Sprintf("(%v)", n.right)
71+
} else if rightOp.precedence >= op.precedence && op.associativity == right {
72+
r = fmt.Sprintf("(%v)", n.right)
73+
}
74+
}
75+
76+
return fmt.Sprintf("%v %v %v", l, n.operator, r)
4577
}
4678

4779
func (n matchesNode) String() string {
@@ -117,8 +149,10 @@ func (n mapNode) String() string {
117149

118150
func (n pairNode) String() string {
119151
switch n.key.(type) {
120-
case binaryNode, unaryNode:
152+
case unaryNode:
121153
return fmt.Sprintf("%v: %v", n.key, n.value)
154+
case binaryNode:
155+
return fmt.Sprintf("(%v): %v", n.key, n.value)
122156
}
123157
return fmt.Sprintf("%q: %v", n.key, n.value)
124158
}

print_test.go

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,9 @@ var printTests = []printTest{
3232
builtinNode{"len", []Node{nameNode{"array"}}},
3333
"len(array)",
3434
},
35-
{
36-
binaryNode{"or", binaryNode{"or", nameNode{"a"}, nameNode{"b"}}, nameNode{"c"}},
37-
"((a or b) or c)",
38-
},
39-
{
40-
binaryNode{"or", nameNode{"a"}, binaryNode{"and", nameNode{"b"}, nameNode{"c"}}},
41-
"(a or (b and c))",
42-
},
4335
{
4436
binaryNode{"and", binaryNode{"or", nameNode{"a"}, nameNode{"b"}}, nameNode{"c"}},
45-
"((a or b) and c)",
37+
"(a or b) and c",
4638
},
4739
{
4840
conditionalNode{nameNode{"a"}, nameNode{"a"}, nameNode{"b"}},
@@ -52,6 +44,50 @@ var printTests = []printTest{
5244
matchesNode{left: nameNode{"foo"}, right: textNode{"foobar"}},
5345
"(foo matches \"foobar\")",
5446
},
47+
{
48+
binaryNode{"or", binaryNode{"or", nameNode{"a"}, nameNode{"b"}}, nameNode{"c"}},
49+
"a or b or c",
50+
},
51+
{
52+
binaryNode{"and", binaryNode{"or", nameNode{"a"}, nameNode{"b"}}, nameNode{"c"}},
53+
"(a or b) and c",
54+
},
55+
{
56+
binaryNode{"or", binaryNode{"and", nameNode{"a"}, nameNode{"b"}}, nameNode{"c"}},
57+
"a and b or c",
58+
},
59+
{
60+
binaryNode{"and", nameNode{"a"}, binaryNode{"or", nameNode{"b"}, nameNode{"c"}}},
61+
"a and (b or c)",
62+
},
63+
{
64+
binaryNode{"*", nameNode{"a"}, binaryNode{"+", nameNode{"b"}, nameNode{"c"}}},
65+
"a * (b + c)",
66+
},
67+
{
68+
binaryNode{"*", binaryNode{"+", nameNode{"a"}, nameNode{"b"}}, binaryNode{"+", nameNode{"c"}, nameNode{"d"}}},
69+
"(a + b) * (c + d)",
70+
},
71+
//{
72+
// binaryNode{"+", binaryNode{"+", nameNode{"a"}, nameNode{"b"}}, binaryNode{"+", nameNode{"c"}, nameNode{"d"}}},
73+
// "a + b + c + d",
74+
//},
75+
{
76+
binaryNode{"**", binaryNode{"**", nameNode{"a"}, nameNode{"b"}}, nameNode{"c"}},
77+
"(a ** b) ** c",
78+
},
79+
{
80+
unaryNode{"-", unaryNode{"+", unaryNode{"-", nameNode{"b"}}}},
81+
"(-(+(-b)))",
82+
},
83+
{
84+
binaryNode{"or", binaryNode{"and", nameNode{"a"}, nameNode{"b"}}, nameNode{"c"}},
85+
"a and b or c",
86+
},
87+
{
88+
binaryNode{"or", nameNode{"a"}, binaryNode{"and", nameNode{"b"}, nameNode{"c"}}},
89+
"a or b and c",
90+
},
5591
}
5692

5793
func TestPrint(t *testing.T) {

type_test.go

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ var typeErrorTests = []typeErrorTest{
131131
},
132132
{
133133
"Ok && IntPtr",
134-
"invalid operation: (Ok && IntPtr) (mismatched types bool and *int)",
134+
"invalid operation: Ok && IntPtr (mismatched types bool and *int)",
135135
},
136136
{
137137
"No ? Any.Ok : Any.Not",
@@ -187,11 +187,11 @@ var typeErrorTests = []typeErrorTest{
187187
},
188188
{
189189
"1 and false",
190-
"invalid operation: (1 and false) (mismatched types float64 and bool)",
190+
"invalid operation: 1 and false (mismatched types float64 and bool)",
191191
},
192192
{
193193
"true or 0",
194-
"invalid operation: (true or 0) (mismatched types bool and float64)",
194+
"invalid operation: true or 0 (mismatched types bool and float64)",
195195
},
196196
{
197197
"not IntPtr",
@@ -203,59 +203,59 @@ var typeErrorTests = []typeErrorTest{
203203
},
204204
{
205205
"Int | Ok",
206-
"invalid operation: (Int | Ok) (mismatched types int and bool)",
206+
"invalid operation: Int | Ok (mismatched types int and bool)",
207207
},
208208
{
209209
"Int ^ Ok",
210-
"invalid operation: (Int ^ Ok) (mismatched types int and bool)",
210+
"invalid operation: Int ^ Ok (mismatched types int and bool)",
211211
},
212212
{
213213
"Int & Ok",
214-
"invalid operation: (Int & Ok) (mismatched types int and bool)",
214+
"invalid operation: Int & Ok (mismatched types int and bool)",
215215
},
216216
{
217217
"Int < Ok",
218-
"invalid operation: (Int < Ok) (mismatched types int and bool)",
218+
"invalid operation: Int < Ok (mismatched types int and bool)",
219219
},
220220
{
221221
"Int > Ok",
222-
"invalid operation: (Int > Ok) (mismatched types int and bool)",
222+
"invalid operation: Int > Ok (mismatched types int and bool)",
223223
},
224224
{
225225
"Int >= Ok",
226-
"invalid operation: (Int >= Ok) (mismatched types int and bool)",
226+
"invalid operation: Int >= Ok (mismatched types int and bool)",
227227
},
228228
{
229229
"Int <= Ok",
230-
"invalid operation: (Int <= Ok) (mismatched types int and bool)",
230+
"invalid operation: Int <= Ok (mismatched types int and bool)",
231231
},
232232
{
233233
"Int + Ok",
234-
"invalid operation: (Int + Ok) (mismatched types int and bool)",
234+
"invalid operation: Int + Ok (mismatched types int and bool)",
235235
},
236236
{
237237
"Int - Ok",
238-
"invalid operation: (Int - Ok) (mismatched types int and bool)",
238+
"invalid operation: Int - Ok (mismatched types int and bool)",
239239
},
240240
{
241241
"Int * Ok",
242-
"invalid operation: (Int * Ok) (mismatched types int and bool)",
242+
"invalid operation: Int * Ok (mismatched types int and bool)",
243243
},
244244
{
245245
"Int / Ok",
246-
"invalid operation: (Int / Ok) (mismatched types int and bool)",
246+
"invalid operation: Int / Ok (mismatched types int and bool)",
247247
},
248248
{
249249
"Int % Ok",
250-
"invalid operation: (Int % Ok) (mismatched types int and bool)",
250+
"invalid operation: Int % Ok (mismatched types int and bool)",
251251
},
252252
{
253253
"Int ** Ok",
254-
"invalid operation: (Int ** Ok) (mismatched types int and bool)",
254+
"invalid operation: Int ** Ok (mismatched types int and bool)",
255255
},
256256
{
257257
"Int .. Ok",
258-
"invalid operation: (Int .. Ok) (mismatched types int and bool)",
258+
"invalid operation: Int .. Ok (mismatched types int and bool)",
259259
},
260260
}
261261

0 commit comments

Comments
 (0)