From db92811da8d13d5d1b78b6e0a985a919165b4c07 Mon Sep 17 00:00:00 2001 From: kako57 <71569154+kako57@users.noreply.github.com> Date: Tue, 21 Feb 2023 19:40:54 -0500 Subject: [PATCH 1/2] Added extra rules for operator precedence --- ASTree.cpp | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/ASTree.cpp b/ASTree.cpp index 7af32d9ea..217283e3b 100644 --- a/ASTree.cpp +++ b/ASTree.cpp @@ -2614,8 +2614,18 @@ static int cmp_prec(PycRef parent, PycRef child) return 1; // Always parenthesize not(x) if (child.type() == ASTNode::NODE_BINARY) { PycRef binChild = child.cast(); - if (parent.type() == ASTNode::NODE_BINARY) - return binChild->op() - parent.cast()->op(); + if (parent.type() == ASTNode::NODE_BINARY) { + PycRef binParent = parent.cast(); + 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; From 7929dbe6e08386fac9ad64b38fc7f15a1aa737d0 Mon Sep 17 00:00:00 2001 From: Michael Hansen Date: Wed, 14 Feb 2024 21:36:27 -0800 Subject: [PATCH 2/2] Add test case for #437 --- tests/compiled/op_precedence.2.7.pyc | Bin 0 -> 516 bytes tests/compiled/op_precedence.3.10.pyc | Bin 0 -> 453 bytes tests/compiled/op_precedence.3.5.pyc | Bin 0 -> 458 bytes tests/input/op_precedence.py | 8 ++++++++ tests/tokenized/op_precedence.txt | 11 +++++++++++ 5 files changed, 19 insertions(+) create mode 100644 tests/compiled/op_precedence.2.7.pyc create mode 100644 tests/compiled/op_precedence.3.10.pyc create mode 100644 tests/compiled/op_precedence.3.5.pyc create mode 100644 tests/input/op_precedence.py create mode 100644 tests/tokenized/op_precedence.txt diff --git a/tests/compiled/op_precedence.2.7.pyc b/tests/compiled/op_precedence.2.7.pyc new file mode 100644 index 0000000000000000000000000000000000000000..5df6fe19d844ba5070ba8f44936164ecddcbf0f6 GIT binary patch literal 516 zcma)&zYYOG5XNV3{SS!*q0?UF2}Foqu{w>gyDHqpQIIP=hu2Yg8ZThJL4+volKFP_ zcE0)Tj=t9_*UQaYdVPx5AxF|>aYSdL87E2-MJ|veAg5^>=D5acmUoQdtg@s5rL2nD zooR@klaPf%6z;KKh%QC#^9%(^oh6Fr@y=WC{MPzjeB8seiX!-ufQ6I=8i6CB!uUio zyOC;Yw!X+HHXLNo#-NSBT^9Iqv4cMwe8ljdXa9j4{L98Z6+i5WjL_iOCJ!lmwULJZ Vhgjmm{w8vVwI9eNae=Zf>j!kmMREWD literal 0 HcmV?d00001 diff --git a/tests/compiled/op_precedence.3.10.pyc b/tests/compiled/op_precedence.3.10.pyc new file mode 100644 index 0000000000000000000000000000000000000000..daa8101d3c92479d621d8bd2ad81a21f74ef392e GIT binary patch literal 453 zcma)&u}%Xq42JFa+MeJlodgm{@B+8+1PFoHyL4kgZ5$>bCsz!hjJyCZz9YOV(y*^Go$rqmo>Lga>d4a~N6m^^Ft+8Db^w*m5jWB%h+ycxhUU1jn|^s ze*VL|P~OjY8=lnK{0HF4hLBwo+s&+_GFD-go2{O*oAPJ%>0RW9+i+IaVOKTO)X+DL CAwF{e literal 0 HcmV?d00001 diff --git a/tests/compiled/op_precedence.3.5.pyc b/tests/compiled/op_precedence.3.5.pyc new file mode 100644 index 0000000000000000000000000000000000000000..41cf6a0073e72a15d4723882e81f76d181ca1f69 GIT binary patch literal 458 zcma)&Jx&8b42A9W<_ENqB7u}hT!3jf0YV`9H0fwiGBZ?xU3olnn(1*4p#NW6w9<*<^C~@%9;CiRcU7TLEb%;!}`O@*?otz|IC0-}?$|%U;ds;WQjYc0seDLwCc}j9u z*LL38lZbVQ=u6WuTYJnDn=J#dqfY4f#8CNSSE}`^6{~e64tuN)EF + +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 )