From 90815b47ca2cc3980f36d9db95c21191e97c2560 Mon Sep 17 00:00:00 2001 From: Michael Hansen Date: Thu, 1 Jun 2023 13:18:22 -0700 Subject: [PATCH] Squash merge of #348 (Plus some whitespace cleanup) commit ca302777438a6ac0ff00b9dbc87d2cf9e816b807 Author: TheHellTower Date: Thu Jun 1 14:55:26 2023 -0400 Test commit 1764514e1dc4018abf050e2e2c3378b6ebaca2a2 Author: TheHellTower Date: Thu Jun 1 14:46:49 2023 -0400 Semi-Support commit 104fa62355a18a071dae2d09db00af3b2fd22b6c Author: TheHellTower Date: Thu Jun 1 14:46:30 2023 -0400 Semi-Support commit 0809a71c725e3a92ece9348265f26f72fdb6664d Author: TheHellTower Date: Thu Jun 1 14:46:11 2023 -0400 Semi-Support commit bdf1695a57d2ad9eb1505a92ff4fa46c6f5ea90f Author: TheHellTower Date: Thu Jun 1 00:06:34 2023 -0400 Updated Test commit ea69a0b3be10568b70d02d744174e3bfc1d3546a Author: TheHellTower Date: Thu Jun 1 00:06:23 2023 -0400 Updated Test commit ecde2146208ea7a533267ace7293a49ce7a132bb Author: TheHellTower Date: Thu Jun 1 00:06:12 2023 -0400 Updated Test commit d5bbbe6224fc0aa8c5c3bc39b4f8845c8f941c7f Author: TheHellTower Date: Wed May 31 23:59:46 2023 -0400 Feat: @zrax Co-authored-by: Michael Hansen commit f8d2299d14a3a0f69b40486006d629ccc5c719df Author: TheHellTower Date: Wed May 31 23:59:03 2023 -0400 Feat: @zrax Co-authored-by: Michael Hansen commit 3fa8691f4c379dd10026227778ac60587fd07e68 Author: TheHellTower Date: Wed May 31 23:58:53 2023 -0400 Feat: @zrax Co-authored-by: Michael Hansen commit 6af17f0cffee31bc03ed8151e82b1720119d6a8d Author: TheHellTower Date: Wed May 31 23:58:45 2023 -0400 Feat: @zrax Co-authored-by: Michael Hansen commit c7c76097fce1f45da0308b9f5e9cfffb5a323fd9 Author: TheHellTower Date: Wed May 31 16:55:20 2023 -0400 Remove `iostream` commit 2edaec022f8cbcb36bffd7ec86b5f71b391fb372 Author: TheHellTower Date: Wed May 31 16:54:48 2023 -0400 Mistake commit a36d4c5e62afe24691701f6d0b7c758b4113db91 Author: TheHellTower Date: Wed May 31 16:52:12 2023 -0400 Remove `iostream` commit 1c3a5b02c95352a8e90247757df17d1563698fb6 Author: TheHellTower Date: Wed May 31 16:43:30 2023 -0400 Should be a bit better commit 11603f1bb244c4d60ec34b675e1cd9c10945abf5 Author: TheHellTower Date: Wed May 31 04:07:47 2023 -0400 Update commit 689b544fc1023ec60e4df257f3448064fdf84c2b Author: TheHellTower Date: Wed May 31 04:07:38 2023 -0400 Update commit 20bc5b820fb2ef6fd9199546e11ac02d78e5b6da Author: TheHellTower Date: Wed May 31 04:07:24 2023 -0400 Update commit c01e8251fac1577d25ab440ade4bfe2b42eb1860 Author: TheHellTower Date: Wed May 31 04:05:28 2023 -0400 Format problem ? commit f35c494dd0d943323ec899cd8b2a236d11de67fc Author: TheHellTower Date: Wed May 31 04:02:03 2023 -0400 Fix commit 7f3265e4192e2b6eec81dc30a630691cead59694 Author: TheHellTower Date: Mon May 29 00:57:01 2023 -0400 BINARY_OP | Simplified commit 045a6041f7a5abb7b5cdeaa4882b2620462e1927 Author: TheHellTower Date: Mon May 29 00:56:53 2023 -0400 BINARY_OP | Simplified commit 0bfb76280e466e15c32ef42851d9981b9df57059 Author: TheHellTower Date: Mon May 29 00:56:41 2023 -0400 BINARY_OP | Simplified commit 31159d69e8f802d7955244b258795563018c3eda Author: TheHellTower Date: Mon May 29 00:50:19 2023 -0400 Fix Matching... commit e4a978aeadebe9c574856c30928ec58da9e32639 Author: TheHellTower Date: Mon May 29 00:50:06 2023 -0400 Fix Matching... commit d7a1b25b8f85886fcdc0a7c10c885e7e9e011281 Author: TheHellTower Date: Mon May 29 00:41:43 2023 -0400 Add `BINARY_OP` support commit 6527299a67f043eb2b63e13ae43c4e9f060456e2 Author: TheHellTower Date: Mon May 29 00:40:32 2023 -0400 Add `BINARY_OP` test tokenized commit c053cab7895f49a085a0797a98e28e87f4874d39 Author: TheHellTower Date: Mon May 29 00:40:11 2023 -0400 Add `BINARY_OP` test commit e32f006f90a4e450099edd7d2f0bdbb0f1bbc612 Author: TheHellTower Date: Mon May 29 00:39:53 2023 -0400 Add `BINARY_OP` test compiled Closes #333 Closes #348 Co-authored-by: TheHellTower --- ASTNode.cpp | 62 +++++++++++++++++++++++++++++- ASTNode.h | 3 ++ ASTree.cpp | 12 ++++++ tests/compiled/BINARY_OP.3.11.pyc | Bin 0 -> 1679 bytes tests/input/BINARY_OP.py | 25 ++++++++++++ tests/tokenized/BINARY_OP.txt | 23 +++++++++++ 6 files changed, 124 insertions(+), 1 deletion(-) create mode 100644 tests/compiled/BINARY_OP.3.11.pyc create mode 100644 tests/input/BINARY_OP.py create mode 100644 tests/tokenized/BINARY_OP.txt diff --git a/ASTNode.cpp b/ASTNode.cpp index 36d36dc..8eb3cdd 100644 --- a/ASTNode.cpp +++ b/ASTNode.cpp @@ -31,11 +31,71 @@ const char* ASTBinary::op_str() const ".", " ** ", " * ", " / ", " // ", " % ", " + ", " - ", " << ", " >> ", " & ", " ^ ", " | ", " and ", " or ", " @ ", " += ", " -= ", " *= ", " /= ", " %= ", " **= ", " <<= ", - " >>= ", " &= ", " ^= ", " |= ", " //= ", " @= ", + " >>= ", " &= ", " ^= ", " |= ", " //= ", " @= ", " " + }; return s_op_strings[op()]; } +ASTBinary::BinOp ASTBinary::getBinOpFromOperand(int operand) +{ + switch (operand) { + case 0: + return BIN_ADD; + case 1: + return BIN_AND; + case 2: + return BIN_FLOOR; + case 3: + return BIN_LSHIFT; + case 4: + return BIN_MAT_MULTIPLY; + case 5: + return BIN_MULTIPLY; + case 6: + return BIN_MODULO; + case 7: + return BIN_OR; + case 8: + return BIN_POWER; + case 9: + return BIN_RSHIFT; + case 10: + return BIN_SUBTRACT; + case 11: + return BIN_DIVIDE; + case 12: + return BIN_XOR; + case 13: + return BIN_IP_ADD; + case 14: + return BIN_IP_AND; + case 15: + return BIN_IP_FLOOR; + case 16: + return BIN_IP_LSHIFT; + case 17: + return BIN_MAT_MULTIPLY; + case 18: + return BIN_IP_MULTIPLY; + case 19: + return BIN_IP_MODULO; + case 20: + return BIN_IP_OR; + case 21: + return BIN_IP_POWER; + case 22: + return BIN_IP_RSHIFT; + case 23: + return BIN_IP_SUBTRACT; + case 24: + return BIN_IP_DIVIDE; + case 25: + return BIN_IP_XOR; + default: + return BIN_INVALID; // Return BIN_INVALID for out-of-range operand + } +} /* ASTCompare */ const char* ASTCompare::op_str() const diff --git a/ASTNode.h b/ASTNode.h index 8d5079a..29e6a94 100644 --- a/ASTNode.h +++ b/ASTNode.h @@ -136,6 +136,8 @@ public: BIN_IP_ADD, BIN_IP_SUBTRACT, BIN_IP_MULTIPLY, BIN_IP_DIVIDE, BIN_IP_MODULO, BIN_IP_POWER, BIN_IP_LSHIFT, BIN_IP_RSHIFT, BIN_IP_AND, BIN_IP_XOR, BIN_IP_OR, BIN_IP_FLOOR, BIN_IP_MAT_MULTIPLY, + /* Error Case */ + BIN_INVALID }; ASTBinary(PycRef left, PycRef right, int op, @@ -147,6 +149,7 @@ public: int op() const { return m_op; } bool is_inplace() const { return m_op >= BIN_IP_ADD; } virtual const char* op_str() const; + static BinOp getBinOpFromOperand(int operand); protected: int m_op; diff --git a/ASTree.cpp b/ASTree.cpp index d1baadd..a1eb341 100644 --- a/ASTree.cpp +++ b/ASTree.cpp @@ -157,6 +157,18 @@ PycRef BuildFromCode(PycRef code, PycModule* mod) } switch (opcode) { + case Pyc::BINARY_OP_A: + { + ASTBinary::BinOp op = ASTBinary::getBinOpFromOperand(operand); + if (op == ASTBinary::BIN_INVALID) + fprintf(stderr, "Unsupported `BINARY_OP` operand value: %d\n", operand); + PycRef right = stack.top(); + stack.pop(); + PycRef left = stack.top(); + stack.pop(); + stack.push(new ASTBinary(left, right, op)); + } + break; case Pyc::BINARY_ADD: { PycRef right = stack.top(); diff --git a/tests/compiled/BINARY_OP.3.11.pyc b/tests/compiled/BINARY_OP.3.11.pyc new file mode 100644 index 0000000000000000000000000000000000000000..261d4537a077566187a642b92f696d895fd3b0b3 GIT binary patch literal 1679 zcmaKs&rcdb6vy9!peTs=1IiC^jT(ao6AtZxG)*vSV^gu%v}sL~W_8!P68gC}vJauMvp_uNvfiUm;dG9;(9s@r@pN9tvi_&d@8Mn_zz zk5Ru1^)pINBFiLiIbRK6k;6f)TJ$9~~WQ=okouaJnTo+~RzdAAY8ZwVE zo@;uiuj!x~fxIZnnyi#_jo|D0uBHlmRutZ>mo&LjlJ^8l2&~G-a*bsUZYpBEq~sdm z^^&5f{67+2JE(z4Q^ls?s5go1cOr>8D{VCMEUGAZ0=dLyD=`A43sTY)=_5W5NuEnTIT7J)##|BsXS4 z3`4BXLTE<~;7#B$@DIsNMX#dlS>1qX6Q(gtTlu&lW;@vn?)-@X zOC~H~ShD4GhwBv2{RS+Wu!v#N@*r&!50c#P!S>6a9UG7~A&ntz<=KZ%N++4frl=1rK#FmInNg@jJFy>GyR2@4n&Y&nWJR7B*+{bR=d1j=`c i=*9g`EIESK4VW`w4#QlZcNKj$AY(!XcV8;`SBAgIxtSjT literal 0 HcmV?d00001 diff --git a/tests/input/BINARY_OP.py b/tests/input/BINARY_OP.py new file mode 100644 index 0000000..9a1dfa8 --- /dev/null +++ b/tests/input/BINARY_OP.py @@ -0,0 +1,25 @@ +(a, b) = (10, 3) +print('Addition:', a + b) +print('Subtraction:', a - b) +print('Multiplication:', a * b) +print('Division:', a / b) +print('Modulo:', a % b) +print('Floor Division:', a // b) +print('Exponentiation:', a ** b) +print('Greater than:', a > b) +print('Less than:', a < b) +print('Greater than or equal to:', a >= b) +print('Less than or equal to:', a <= b) +print('Equal to:', a == b) +print('Not equal to:', a != b) +#print('Logical AND:', a and b) +#print('Logical OR:', a or b) +print('Bitwise AND:', a & b) +print('Bitwise OR:', a | b) +print('Bitwise XOR:', a ^ b) +print('Bitwise left shift:', a << b) +print('Bitwise right shift:', a >> b) +print('Membership in:', a in b) +print('Membership not in:', a not in b) +print('Identity equality:', a is b) +print('Identity inequality:', a is not b) \ No newline at end of file diff --git a/tests/tokenized/BINARY_OP.txt b/tests/tokenized/BINARY_OP.txt new file mode 100644 index 0000000..a837b79 --- /dev/null +++ b/tests/tokenized/BINARY_OP.txt @@ -0,0 +1,23 @@ +( a , b ) = ( 10 , 3 ) +print ( 'Addition:' , a + b ) +print ( 'Subtraction:' , a - b ) +print ( 'Multiplication:' , a * b ) +print ( 'Division:' , a / b ) +print ( 'Modulo:' , a % b ) +print ( 'Floor Division:' , a // b ) +print ( 'Exponentiation:' , a ** b ) +print ( 'Greater than:' , a > b ) +print ( 'Less than:' , a < b ) +print ( 'Greater than or equal to:' , a >= b ) +print ( 'Less than or equal to:' , a <= b ) +print ( 'Equal to:' , a == b ) +print ( 'Not equal to:' , a != b ) +print ( 'Bitwise AND:' , a & b ) +print ( 'Bitwise OR:' , a | b ) +print ( 'Bitwise XOR:' , a ^ b ) +print ( 'Bitwise left shift:' , a << b ) +print ( 'Bitwise right shift:' , a >> b ) +print ( 'Membership in:' , a in b ) +print ( 'Membership not in:' , a not in b ) +print ( 'Identity equality:' , a is b ) +print ( 'Identity inequality:' , a is not b )