Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #321 for arithmetic operators #437

Merged
merged 4 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions ASTree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Binary file added tests/compiled/op_precedence.2.7.pyc
Binary file not shown.
Binary file added tests/compiled/op_precedence.3.10.pyc
Binary file not shown.
Binary file added tests/compiled/op_precedence.3.5.pyc
Binary file not shown.
8 changes: 8 additions & 0 deletions tests/input/op_precedence.py
Original file line number Diff line number Diff line change
@@ -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)
11 changes: 11 additions & 0 deletions tests/tokenized/op_precedence.txt
Original file line number Diff line number Diff line change
@@ -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>
Loading