diff --git a/ASTree.cpp b/ASTree.cpp index 6e010e0..e6e6c7e 100644 --- a/ASTree.cpp +++ b/ASTree.cpp @@ -2506,6 +2506,79 @@ 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; + case Pyc::COPY_A: + { + PycRef value = stack.top(operand); + stack.push(value); + } + break; default: fprintf(stderr, "Unsupported opcode: %s (%d)\n", Pyc::OpcodeName(opcode), opcode); cleanBuild = false; diff --git a/FastStack.h b/FastStack.h index 3f9c2c8..b91ec71 100644 --- a/FastStack.h +++ b/FastStack.h @@ -37,14 +37,21 @@ public: } } - PycRef top() const + PycRef top(int i = 1) const { - if (m_ptr > -1) - return m_stack[m_ptr]; + if (i > 0) { + int idx = m_ptr + 1 - i; + if ((m_ptr > -1) && (idx >= 0)) + return m_stack[idx]; + else { + #ifdef BLOCK_DEBUG + fprintf(stderr, "insufficient values on stack\n"); + #endif + return nullptr; + } + } else { - #ifdef BLOCK_DEBUG - fprintf(stderr, "top on empty stack\n"); - #endif + fprintf(stderr, "incorrect operand %i\n", i); return nullptr; } } diff --git a/tests/compiled/binary_slice.3.12.pyc b/tests/compiled/binary_slice.3.12.pyc new file mode 100644 index 0000000..24cc0f2 Binary files /dev/null and b/tests/compiled/binary_slice.3.12.pyc differ diff --git a/tests/compiled/store_slice.3.12.pyc b/tests/compiled/store_slice.3.12.pyc new file mode 100644 index 0000000..252f5af Binary files /dev/null and b/tests/compiled/store_slice.3.12.pyc differ diff --git a/tests/input/binary_slice.py b/tests/input/binary_slice.py new file mode 100644 index 0000000..361d922 --- /dev/null +++ b/tests/input/binary_slice.py @@ -0,0 +1,7 @@ +l = [1,2,3,4,5,6] +print(l[1:3]) +print(l[:2]) +print(l[3:]) +print(l[-4:]) +print(l[:-2]) +print(l[:]) diff --git a/tests/input/store_slice.py b/tests/input/store_slice.py new file mode 100644 index 0000000..8d8369a --- /dev/null +++ b/tests/input/store_slice.py @@ -0,0 +1,8 @@ +a = [0] * 16 +l = [1,2,3] +a[13:] = l +a[5] = 10 +a[:2] = [1,2] +a[:] = range(16) + +print(a) diff --git a/tests/tokenized/binary_slice.txt b/tests/tokenized/binary_slice.txt new file mode 100644 index 0000000..bb3d1a0 --- /dev/null +++ b/tests/tokenized/binary_slice.txt @@ -0,0 +1,7 @@ +l = [ 1 , 2 , 3 , 4 , 5 , 6 ] +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 )