From 6ad3ceb67ec150cc50b7a8dc796ac495b857f6db Mon Sep 17 00:00:00 2001 From: easyz <130207156+ddouworld@users.noreply.github.com> Date: Mon, 24 Jun 2024 02:59:30 +0800 Subject: [PATCH] Add support for `swap` bytecode and simple `WITH_EXCEPT_START` bytecode support. (#488) * Modify .gitignore * Added support for SWAP and WITH_EXCEPT_START, WITH_EXCEPT_START is simply added on top of SETUP_WITH_A so that it works properly. * Resolve the warning about comparing size_t and int. * Revert "Resolve the warning about comparing size_t and int." This reverts commit 54dfe36629855ca557277572d307dacaf6a64fe9. * Reapply "Resolve the warning about comparing size_t and int." This reverts commit d21d1681ed6496856ea65151e5fd798f0a718416. * Modify decompyle_test.sh * Modify .gitignore * Fix the logic error by placing the assignment inside the tuple * Re-adding test files * Fixing redundant brackets * Add support for swap bytecode and simple WITH_EXCEPT_START bytecode support. * Clean up some formatting issues --------- Co-authored-by: Michael Hansen --- ASTree.cpp | 19 +++++++++++++++++++ tests/compiled/swap.3.11.pyc | Bin 0 -> 456 bytes tests/input/swap.py | 12 ++++++++++++ tests/tokenized/swap.txt | 6 ++++++ 4 files changed, 37 insertions(+) create mode 100644 tests/compiled/swap.3.11.pyc create mode 100644 tests/input/swap.py create mode 100644 tests/tokenized/swap.txt diff --git a/ASTree.cpp b/ASTree.cpp index c58b7be..f85f7f7 100644 --- a/ASTree.cpp +++ b/ASTree.cpp @@ -1884,6 +1884,7 @@ PycRef BuildFromCode(PycRef code, PycModule* mod) // Ignore break; case Pyc::SETUP_WITH_A: + case Pyc::WITH_EXCEPT_START: { PycRef withblock = new ASTWithBlock(pos+operand); blocks.push(withblock); @@ -2463,6 +2464,24 @@ PycRef BuildFromCode(PycRef code, PycModule* mod) case Pyc::GEN_START_A: stack.pop(); break; + case Pyc::SWAP_A: + { + unpack = operand; + ASTTuple::value_t values; + ASTTuple::value_t next_tuple; + values.resize(operand); + for (int i = 0; i < operand; i++) { + values[operand - i - 1] = stack.top(); + stack.pop(); + } + auto tup = new ASTTuple(values); + tup->setRequireParens(false); + auto next_tup = new ASTTuple(next_tuple); + next_tup->setRequireParens(false); + stack.push(tup); + stack.push(next_tup); + } + break; default: fprintf(stderr, "Unsupported opcode: %s\n", Pyc::OpcodeName(opcode & 0xFF)); cleanBuild = false; diff --git a/tests/compiled/swap.3.11.pyc b/tests/compiled/swap.3.11.pyc new file mode 100644 index 0000000000000000000000000000000000000000..f1e631bf309b4db8902f36be1194ab418f5e15ff GIT binary patch literal 456 zcmZ3^%ge<81bHT*X^e~v439w^7y$CY;4=@9n$D2I(83VKkir$?&K$01V zKSu$H=?v*WHH#Q)8B&;P8B>^RnQ9nnm?XgpK?IP;h|Ei2LGY59L9#XwRZI{%g%w1n zFaT|3^V4L133Aj+CJ+I#@+Aw106E|#8;IZl5=9`1B9K@SNUR8?WF>vx-6L6ciMGv6rM4m*^E#-eL(3cMK@v2MU91DfR{u4GbTc*af&hFu;fg zj}HP2oLmi#H#j(fgpSFEz%|}GBn~L_q|~f)7ai;;_lhPbtkwwJQ3Rs0~sHf85tQrFmW>m MF@9jcPJ#^q0LD>Ss{jB1 literal 0 HcmV?d00001 diff --git a/tests/input/swap.py b/tests/input/swap.py new file mode 100644 index 0000000..cc0bbe4 --- /dev/null +++ b/tests/input/swap.py @@ -0,0 +1,12 @@ +def SWAP(): + my_array = [ + 1, + 2, + 3, + 4, + 5, + 6, + 8] + i = 1 + j = 3 + my_array[i], my_array[j], my_array[2] = my_array[j], my_array[i], my_array[4] diff --git a/tests/tokenized/swap.txt b/tests/tokenized/swap.txt new file mode 100644 index 0000000..5070b40 --- /dev/null +++ b/tests/tokenized/swap.txt @@ -0,0 +1,6 @@ +def SWAP ( ) : + +my_array = [ 1 , 2 , 3 , 4 , 5 , 6 , 8 ] +i = 1 +j = 3 +my_array [ i ] , my_array [ j ] , my_array [ 2 ] = my_array [ j ] , my_array [ i ] , my_array [ 4 ]