Skip to content

Commit

Permalink
dev(pretty_print): remove reduntant parenthesis
Browse files Browse the repository at this point in the history
  • Loading branch information
nimdraugsael authored and antonmedv committed Aug 14, 2018
1 parent d895935 commit 18a2b9a
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 29 deletions.
2 changes: 1 addition & 1 deletion eval_test.go
Expand Up @@ -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"`,
Expand Down
38 changes: 36 additions & 2 deletions print.go
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
54 changes: 45 additions & 9 deletions print_test.go
Expand Up @@ -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"}},
Expand All @@ -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) {
Expand Down
34 changes: 17 additions & 17 deletions type_test.go
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand All @@ -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)",
},
}

Expand Down

0 comments on commit 18a2b9a

Please sign in to comment.