From ad5f39db56b5b93ffb7cb39c7dc1f62237d7e164 Mon Sep 17 00:00:00 2001 From: Sahil Jain Date: Mon, 30 Jun 2025 23:57:31 +0530 Subject: [PATCH] Support SLICE opcodes --- ASTree.cpp | 67 +++++++++++++++++++++++++++ tests/compiled/binary_slice.3.12.pyc | Bin 0 -> 412 bytes tests/compiled/store_slice.3.12.pyc | Bin 0 -> 358 bytes tests/input/binary_slice.py | 7 +++ tests/input/store_slice.py | 8 ++++ tests/tokenized/binary_slice.txt | 7 +++ tests/tokenized/store_slice.txt | 7 +++ 7 files changed, 96 insertions(+) create mode 100644 tests/compiled/binary_slice.3.12.pyc create mode 100644 tests/compiled/store_slice.3.12.pyc create mode 100644 tests/input/binary_slice.py create mode 100644 tests/input/store_slice.py create mode 100644 tests/tokenized/binary_slice.txt create mode 100644 tests/tokenized/store_slice.txt diff --git a/ASTree.cpp b/ASTree.cpp index 1f419d0..281d224 100644 --- a/ASTree.cpp +++ b/ASTree.cpp @@ -2473,6 +2473,73 @@ PycRef BuildFromCode(PycRef code, PycModule* mod) stack.push(next_tup); } break; + case Pyc::BINARY_SLICE: + { + PycRef end = stack.top(); + stack.pop(); + PycRef start = stack.top(); + stack.pop(); + PycRef dest = stack.top(); + stack.pop(); + + if (start.type() == ASTNode::NODE_OBJECT + && start.cast()->object() == Pyc_None) { + start = NULL; + } + + if (end.type() == ASTNode::NODE_OBJECT + && end.cast()->object() == Pyc_None) { + end = NULL; + } + + PycRef slice; + if (start == NULL && end == NULL) { + slice = new ASTSlice(ASTSlice::SLICE0); + } else if (start == NULL) { + slice = new ASTSlice(ASTSlice::SLICE2, start, end); + } else if (end == NULL) { + slice = new ASTSlice(ASTSlice::SLICE1, start, end); + } else { + slice = new ASTSlice(ASTSlice::SLICE3, start, end); + } + stack.push(new ASTSubscr(dest, slice)); + } + break; + case Pyc::STORE_SLICE: + { + PycRef end = stack.top(); + stack.pop(); + PycRef start = stack.top(); + stack.pop(); + PycRef dest = stack.top(); + stack.pop(); + PycRef values = stack.top(); + stack.pop(); + + if (start.type() == ASTNode::NODE_OBJECT + && start.cast()->object() == Pyc_None) { + start = NULL; + } + + if (end.type() == ASTNode::NODE_OBJECT + && end.cast()->object() == Pyc_None) { + end = NULL; + } + + PycRef slice; + if (start == NULL && end == NULL) { + slice = new ASTSlice(ASTSlice::SLICE0); + } else if (start == NULL) { + slice = new ASTSlice(ASTSlice::SLICE2, start, end); + } else if (end == NULL) { + slice = new ASTSlice(ASTSlice::SLICE1, start, end); + } else { + slice = new ASTSlice(ASTSlice::SLICE3, start, end); + } + + curblock->append(new ASTStore(values, new ASTSubscr(dest, slice))); + } + break; default: fprintf(stderr, "Unsupported opcode: %s (%d)\n", Pyc::OpcodeName(opcode), opcode); cleanBuild = false; diff --git a/tests/compiled/binary_slice.3.12.pyc b/tests/compiled/binary_slice.3.12.pyc new file mode 100644 index 0000000000000000000000000000000000000000..24cc0f2f81a267dfc0a819000ef4f8d51af39dcd GIT binary patch literal 412 zcmX@j%ge<81R|G{G7^FGV-N=hSfPy16M&5A4CxFh42u|}7?>DR8B-Zj7*m*}7*;bv z)G)$CnNwJ>h%%)xV-aP=D$1O~hD9wis@h6sO?FMTmmu3;GJyzY5WxZ>K(2nt1|*6= z5=9^}zap^opa1{=zx)RzG?{KO=GR?7FW9|@wb2=FtQd+r-c!5R(0QW~*_W%F@ literal 0 HcmV?d00001 diff --git a/tests/compiled/store_slice.3.12.pyc b/tests/compiled/store_slice.3.12.pyc new file mode 100644 index 0000000000000000000000000000000000000000..252f5af5f78ffd0479cbdcf5adf17aca3ee23abc GIT binary patch literal 358 zcmXwzF;BxV5QXm&yG;raFjovMQx5VI)up)Ldrj2h7Ivk zn2|y@#FP!G8x!Y3Z+QOh`RVT7JZ}J0Pw!VZ=k$J=W>f4RR+j{iARt14cN|~_5eVxU zXDCsEumu}};6%MCB}HF2kwqO7&JZk+mcUp1o3(Z+O|C6r<06#IcRLg5c1e7@Gx^Rk zVjJ;DPCIjINBcpU)082fwK#7vX}OxGk!*RU;ODV~s7_(%85Ije4vPbg${Vgrka) fS5Iob_9vQu^tbD?`%+^y((Dtso +print ( l [ 1 : 3 ] ) +print ( l [ : 2 ] ) +print ( l [ 3 : ] ) +print ( l [ - 4 : ] ) +print ( l [ : - 2 ] ) +print ( l [ : ] ) diff --git a/tests/tokenized/store_slice.txt b/tests/tokenized/store_slice.txt new file mode 100644 index 0000000..2842070 --- /dev/null +++ b/tests/tokenized/store_slice.txt @@ -0,0 +1,7 @@ +a = [ 0 ] * 16 +l = [ 1 , 2 , 3 ] +a [ 13 : ] = l +a [ 5 ] = 10 +a [ : 2 ] = [ 1 , 2 ] +a [ : ] = range ( 16 ) +print ( a )