Merge pull request #437 from kako57/master

Fixes #321 for arithmetic operators
This commit is contained in:
Michael Hansen
2024-02-14 21:40:58 -08:00
committed by GitHub
6 changed files with 31 additions and 2 deletions

View File

@@ -2504,8 +2504,18 @@ static int cmp_prec(PycRef<ASTNode> parent, PycRef<ASTNode> child)
return 1; // Always parenthesize not(x)
if (child.type() == ASTNode::NODE_BINARY) {
PycRef<ASTBinary> binChild = child.cast<ASTBinary>();
if (parent.type() == ASTNode::NODE_BINARY)
return binChild->op() - parent.cast<ASTBinary>()->op();
if (parent.type() == ASTNode::NODE_BINARY) {
PycRef<ASTBinary> binParent = parent.cast<ASTBinary>();
if (binParent->right() == child) {
if (binParent->op() == ASTBinary::BIN_SUBTRACT &&
binChild->op() == ASTBinary::BIN_ADD)
return 1;
else if (binParent->op() == ASTBinary::BIN_DIVIDE &&
binChild->op() == ASTBinary::BIN_MULTIPLY)
return 1;
}
return binChild->op() - binParent->op();
}
else if (parent.type() == ASTNode::NODE_COMPARE)
return (binChild->op() == ASTBinary::BIN_LOG_AND ||
binChild->op() == ASTBinary::BIN_LOG_OR) ? 1 : -1;

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -0,0 +1,8 @@
def foo(a, b, c):
return c - (a + b)
def bar(a, b, c, d):
return a / ((b ** c) * d)
def baz(a, b, c, d):
return a - ((b ^ c) + d)

View File

@@ -0,0 +1,11 @@
def foo ( a , b , c ) : <EOL>
<INDENT>
return c - ( a + b ) <EOL>
<OUTDENT>
def bar ( a , b , c , d ) : <EOL>
<INDENT>
return a / ( b ** c * d ) <EOL>
<OUTDENT>
def baz ( a , b , c , d ) : <EOL>
<INDENT>
return a - ( ( b ^ c ) + d ) <EOL>