Skip to content

Commit

Permalink
Add support for swap bytecode and simple WITH_EXCEPT_START byteco…
Browse files Browse the repository at this point in the history
…de support. (zrax#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 54dfe36.

* Reapply "Resolve the warning about comparing size_t and int."

This reverts commit d21d168.

* 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 <[email protected]>
  • Loading branch information
ddouworld and zrax authored Jun 23, 2024
1 parent b9f3d14 commit 6ad3ceb
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 0 deletions.
19 changes: 19 additions & 0 deletions ASTree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1884,6 +1884,7 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
// Ignore
break;
case Pyc::SETUP_WITH_A:
case Pyc::WITH_EXCEPT_START:
{
PycRef<ASTBlock> withblock = new ASTWithBlock(pos+operand);
blocks.push(withblock);
Expand Down Expand Up @@ -2463,6 +2464,24 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> 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;
Expand Down
Binary file added tests/compiled/swap.3.11.pyc
Binary file not shown.
12 changes: 12 additions & 0 deletions tests/input/swap.py
Original file line number Diff line number Diff line change
@@ -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]
6 changes: 6 additions & 0 deletions tests/tokenized/swap.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
def SWAP ( ) : <EOL>
<INDENT>
my_array = [ 1 , 2 , 3 , 4 , 5 , 6 , 8 ] <EOL>
i = 1 <EOL>
j = 3 <EOL>
my_array [ i ] , my_array [ j ] , my_array [ 2 ] = my_array [ j ] , my_array [ i ] , my_array [ 4 ] <EOL>

0 comments on commit 6ad3ceb

Please sign in to comment.