mirror of
https://github.com/hanzoai/expr.git
synced 2026-09-24 16:58:58 +00:00
dev(pretty_print): remove reduntant parenthesis
This commit is contained in:
committed by
Anton Medvedev
parent
e745dcc3ee
commit
4e61df139b
+1
-1
@@ -350,7 +350,7 @@ var evalErrorTests = []evalErrorTest{
|
||||
{
|
||||
`1 matches "1" ~ "2"`,
|
||||
nil,
|
||||
"operator matches doesn't defined on (float64, string): (1 matches (\"1\" ~ \"2\"))",
|
||||
"operator matches doesn't defined on (float64, string): (1 matches \"1\" ~ \"2\")",
|
||||
},
|
||||
{
|
||||
`1 matches "1"`,
|
||||
|
||||
@@ -41,7 +41,39 @@ func (n unaryNode) String() string {
|
||||
}
|
||||
|
||||
func (n binaryNode) String() string {
|
||||
return fmt.Sprintf("(%v %v %v)", n.left, n.operator, n.right)
|
||||
var leftOp, rightOp *info
|
||||
op := binaryOperators[n.operator]
|
||||
|
||||
switch n.left.(type) {
|
||||
case binaryNode:
|
||||
v := binaryOperators[n.left.(binaryNode).operator]
|
||||
leftOp = &v
|
||||
}
|
||||
switch n.right.(type) {
|
||||
case binaryNode:
|
||||
v := binaryOperators[n.right.(binaryNode).operator]
|
||||
rightOp = &v
|
||||
}
|
||||
|
||||
l, r := fmt.Sprintf("%v", n.left), fmt.Sprintf("%v", n.right)
|
||||
|
||||
if leftOp != nil {
|
||||
if leftOp.precedence < op.precedence && op.associativity == left {
|
||||
l = fmt.Sprintf("(%v)", n.left)
|
||||
} else if leftOp.precedence >= op.precedence && op.associativity == right {
|
||||
l = fmt.Sprintf("(%v)", n.left)
|
||||
}
|
||||
}
|
||||
|
||||
if rightOp != nil {
|
||||
if rightOp.precedence < op.precedence && op.associativity == left {
|
||||
r = fmt.Sprintf("(%v)", n.right)
|
||||
} else if rightOp.precedence >= op.precedence && op.associativity == right {
|
||||
r = fmt.Sprintf("(%v)", n.right)
|
||||
}
|
||||
}
|
||||
|
||||
return fmt.Sprintf("%v %v %v", l, n.operator, r)
|
||||
}
|
||||
|
||||
func (n matchesNode) String() string {
|
||||
@@ -117,8 +149,10 @@ func (n mapNode) String() string {
|
||||
|
||||
func (n pairNode) String() string {
|
||||
switch n.key.(type) {
|
||||
case binaryNode, unaryNode:
|
||||
case unaryNode:
|
||||
return fmt.Sprintf("%v: %v", n.key, n.value)
|
||||
case binaryNode:
|
||||
return fmt.Sprintf("(%v): %v", n.key, n.value)
|
||||
}
|
||||
return fmt.Sprintf("%q: %v", n.key, n.value)
|
||||
}
|
||||
|
||||
+45
-9
@@ -32,17 +32,9 @@ var printTests = []printTest{
|
||||
builtinNode{"len", []Node{nameNode{"array"}}},
|
||||
"len(array)",
|
||||
},
|
||||
{
|
||||
binaryNode{"or", binaryNode{"or", nameNode{"a"}, nameNode{"b"}}, nameNode{"c"}},
|
||||
"((a or b) or c)",
|
||||
},
|
||||
{
|
||||
binaryNode{"or", nameNode{"a"}, binaryNode{"and", nameNode{"b"}, nameNode{"c"}}},
|
||||
"(a or (b and c))",
|
||||
},
|
||||
{
|
||||
binaryNode{"and", binaryNode{"or", nameNode{"a"}, nameNode{"b"}}, nameNode{"c"}},
|
||||
"((a or b) and c)",
|
||||
"(a or b) and c",
|
||||
},
|
||||
{
|
||||
conditionalNode{nameNode{"a"}, nameNode{"a"}, nameNode{"b"}},
|
||||
@@ -52,6 +44,50 @@ var printTests = []printTest{
|
||||
matchesNode{left: nameNode{"foo"}, right: textNode{"foobar"}},
|
||||
"(foo matches \"foobar\")",
|
||||
},
|
||||
{
|
||||
binaryNode{"or", binaryNode{"or", nameNode{"a"}, nameNode{"b"}}, nameNode{"c"}},
|
||||
"a or b or c",
|
||||
},
|
||||
{
|
||||
binaryNode{"and", binaryNode{"or", nameNode{"a"}, nameNode{"b"}}, nameNode{"c"}},
|
||||
"(a or b) and c",
|
||||
},
|
||||
{
|
||||
binaryNode{"or", binaryNode{"and", nameNode{"a"}, nameNode{"b"}}, nameNode{"c"}},
|
||||
"a and b or c",
|
||||
},
|
||||
{
|
||||
binaryNode{"and", nameNode{"a"}, binaryNode{"or", nameNode{"b"}, nameNode{"c"}}},
|
||||
"a and (b or c)",
|
||||
},
|
||||
{
|
||||
binaryNode{"*", nameNode{"a"}, binaryNode{"+", nameNode{"b"}, nameNode{"c"}}},
|
||||
"a * (b + c)",
|
||||
},
|
||||
{
|
||||
binaryNode{"*", binaryNode{"+", nameNode{"a"}, nameNode{"b"}}, binaryNode{"+", nameNode{"c"}, nameNode{"d"}}},
|
||||
"(a + b) * (c + d)",
|
||||
},
|
||||
//{
|
||||
// binaryNode{"+", binaryNode{"+", nameNode{"a"}, nameNode{"b"}}, binaryNode{"+", nameNode{"c"}, nameNode{"d"}}},
|
||||
// "a + b + c + d",
|
||||
//},
|
||||
{
|
||||
binaryNode{"**", binaryNode{"**", nameNode{"a"}, nameNode{"b"}}, nameNode{"c"}},
|
||||
"(a ** b) ** c",
|
||||
},
|
||||
{
|
||||
unaryNode{"-", unaryNode{"+", unaryNode{"-", nameNode{"b"}}}},
|
||||
"(-(+(-b)))",
|
||||
},
|
||||
{
|
||||
binaryNode{"or", binaryNode{"and", nameNode{"a"}, nameNode{"b"}}, nameNode{"c"}},
|
||||
"a and b or c",
|
||||
},
|
||||
{
|
||||
binaryNode{"or", nameNode{"a"}, binaryNode{"and", nameNode{"b"}, nameNode{"c"}}},
|
||||
"a or b and c",
|
||||
},
|
||||
}
|
||||
|
||||
func TestPrint(t *testing.T) {
|
||||
|
||||
+17
-17
@@ -131,7 +131,7 @@ var typeErrorTests = []typeErrorTest{
|
||||
},
|
||||
{
|
||||
"Ok && IntPtr",
|
||||
"invalid operation: (Ok && IntPtr) (mismatched types bool and *int)",
|
||||
"invalid operation: Ok && IntPtr (mismatched types bool and *int)",
|
||||
},
|
||||
{
|
||||
"No ? Any.Ok : Any.Not",
|
||||
@@ -187,11 +187,11 @@ var typeErrorTests = []typeErrorTest{
|
||||
},
|
||||
{
|
||||
"1 and false",
|
||||
"invalid operation: (1 and false) (mismatched types float64 and bool)",
|
||||
"invalid operation: 1 and false (mismatched types float64 and bool)",
|
||||
},
|
||||
{
|
||||
"true or 0",
|
||||
"invalid operation: (true or 0) (mismatched types bool and float64)",
|
||||
"invalid operation: true or 0 (mismatched types bool and float64)",
|
||||
},
|
||||
{
|
||||
"not IntPtr",
|
||||
@@ -203,59 +203,59 @@ var typeErrorTests = []typeErrorTest{
|
||||
},
|
||||
{
|
||||
"Int | Ok",
|
||||
"invalid operation: (Int | Ok) (mismatched types int and bool)",
|
||||
"invalid operation: Int | Ok (mismatched types int and bool)",
|
||||
},
|
||||
{
|
||||
"Int ^ Ok",
|
||||
"invalid operation: (Int ^ Ok) (mismatched types int and bool)",
|
||||
"invalid operation: Int ^ Ok (mismatched types int and bool)",
|
||||
},
|
||||
{
|
||||
"Int & Ok",
|
||||
"invalid operation: (Int & Ok) (mismatched types int and bool)",
|
||||
"invalid operation: Int & Ok (mismatched types int and bool)",
|
||||
},
|
||||
{
|
||||
"Int < Ok",
|
||||
"invalid operation: (Int < Ok) (mismatched types int and bool)",
|
||||
"invalid operation: Int < Ok (mismatched types int and bool)",
|
||||
},
|
||||
{
|
||||
"Int > Ok",
|
||||
"invalid operation: (Int > Ok) (mismatched types int and bool)",
|
||||
"invalid operation: Int > Ok (mismatched types int and bool)",
|
||||
},
|
||||
{
|
||||
"Int >= Ok",
|
||||
"invalid operation: (Int >= Ok) (mismatched types int and bool)",
|
||||
"invalid operation: Int >= Ok (mismatched types int and bool)",
|
||||
},
|
||||
{
|
||||
"Int <= Ok",
|
||||
"invalid operation: (Int <= Ok) (mismatched types int and bool)",
|
||||
"invalid operation: Int <= Ok (mismatched types int and bool)",
|
||||
},
|
||||
{
|
||||
"Int + Ok",
|
||||
"invalid operation: (Int + Ok) (mismatched types int and bool)",
|
||||
"invalid operation: Int + Ok (mismatched types int and bool)",
|
||||
},
|
||||
{
|
||||
"Int - Ok",
|
||||
"invalid operation: (Int - Ok) (mismatched types int and bool)",
|
||||
"invalid operation: Int - Ok (mismatched types int and bool)",
|
||||
},
|
||||
{
|
||||
"Int * Ok",
|
||||
"invalid operation: (Int * Ok) (mismatched types int and bool)",
|
||||
"invalid operation: Int * Ok (mismatched types int and bool)",
|
||||
},
|
||||
{
|
||||
"Int / Ok",
|
||||
"invalid operation: (Int / Ok) (mismatched types int and bool)",
|
||||
"invalid operation: Int / Ok (mismatched types int and bool)",
|
||||
},
|
||||
{
|
||||
"Int % Ok",
|
||||
"invalid operation: (Int % Ok) (mismatched types int and bool)",
|
||||
"invalid operation: Int % Ok (mismatched types int and bool)",
|
||||
},
|
||||
{
|
||||
"Int ** Ok",
|
||||
"invalid operation: (Int ** Ok) (mismatched types int and bool)",
|
||||
"invalid operation: Int ** Ok (mismatched types int and bool)",
|
||||
},
|
||||
{
|
||||
"Int .. Ok",
|
||||
"invalid operation: (Int .. Ok) (mismatched types int and bool)",
|
||||
"invalid operation: Int .. Ok (mismatched types int and bool)",
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user