node: elide some extra parentheses for commutative binary ops (plus/concat)

This commit is contained in:
mappu 2020-04-09 19:40:18 +12:00
parent 314d65a459
commit 91e39e7e2f

11
node.go
View File

@ -1490,6 +1490,17 @@ func (this *conversionState) convertBinaryCommon(left, right node.Node, goBinary
rhs = "(" + rhs + ")" rhs = "(" + rhs + ")"
} }
// We can elide even more parens in case of some commutative operators
// We can only do this in the left-associative case
// FIXME Plus and Concat aren't necessarily commutative together even though they both have + here(!!)
if _, ok := left.(*binary.Plus); ok && goBinaryOperator == `+` {
lhs = removeParens(lhs)
}
if _, ok := left.(*binary.Concat); ok && goBinaryOperator == `+` {
lhs = removeParens(lhs)
}
// Done
return "(" + lhs + " " + goBinaryOperator + " " + rhs + ")", nil return "(" + lhs + " " + goBinaryOperator + " " + rhs + ")", nil
} }