Merge pull request #557 from whoami730/new-opcodes
Support py 3.12 opcodes: part 2
This commit is contained in:
73
ASTree.cpp
73
ASTree.cpp
@@ -2506,6 +2506,79 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
|
||||
stack.push(next_tup);
|
||||
}
|
||||
break;
|
||||
case Pyc::BINARY_SLICE:
|
||||
{
|
||||
PycRef<ASTNode> end = stack.top();
|
||||
stack.pop();
|
||||
PycRef<ASTNode> start = stack.top();
|
||||
stack.pop();
|
||||
PycRef<ASTNode> dest = stack.top();
|
||||
stack.pop();
|
||||
|
||||
if (start.type() == ASTNode::NODE_OBJECT
|
||||
&& start.cast<ASTObject>()->object() == Pyc_None) {
|
||||
start = NULL;
|
||||
}
|
||||
|
||||
if (end.type() == ASTNode::NODE_OBJECT
|
||||
&& end.cast<ASTObject>()->object() == Pyc_None) {
|
||||
end = NULL;
|
||||
}
|
||||
|
||||
PycRef<ASTNode> 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<ASTNode> end = stack.top();
|
||||
stack.pop();
|
||||
PycRef<ASTNode> start = stack.top();
|
||||
stack.pop();
|
||||
PycRef<ASTNode> dest = stack.top();
|
||||
stack.pop();
|
||||
PycRef<ASTNode> values = stack.top();
|
||||
stack.pop();
|
||||
|
||||
if (start.type() == ASTNode::NODE_OBJECT
|
||||
&& start.cast<ASTObject>()->object() == Pyc_None) {
|
||||
start = NULL;
|
||||
}
|
||||
|
||||
if (end.type() == ASTNode::NODE_OBJECT
|
||||
&& end.cast<ASTObject>()->object() == Pyc_None) {
|
||||
end = NULL;
|
||||
}
|
||||
|
||||
PycRef<ASTNode> 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<ASTNode> value = stack.top(operand);
|
||||
stack.push(value);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
fprintf(stderr, "Unsupported opcode: %s (%d)\n", Pyc::OpcodeName(opcode), opcode);
|
||||
cleanBuild = false;
|
||||
|
19
FastStack.h
19
FastStack.h
@@ -37,14 +37,21 @@ public:
|
||||
}
|
||||
}
|
||||
|
||||
PycRef<ASTNode> top() const
|
||||
PycRef<ASTNode> 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;
|
||||
}
|
||||
}
|
||||
|
BIN
tests/compiled/binary_slice.3.12.pyc
Normal file
BIN
tests/compiled/binary_slice.3.12.pyc
Normal file
Binary file not shown.
BIN
tests/compiled/store_slice.3.12.pyc
Normal file
BIN
tests/compiled/store_slice.3.12.pyc
Normal file
Binary file not shown.
7
tests/input/binary_slice.py
Normal file
7
tests/input/binary_slice.py
Normal file
@@ -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[:])
|
8
tests/input/store_slice.py
Normal file
8
tests/input/store_slice.py
Normal file
@@ -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)
|
7
tests/tokenized/binary_slice.txt
Normal file
7
tests/tokenized/binary_slice.txt
Normal file
@@ -0,0 +1,7 @@
|
||||
l = [ 1 , 2 , 3 , 4 , 5 , 6 ] <EOL>
|
||||
print ( l [ 1 : 3 ] ) <EOL>
|
||||
print ( l [ : 2 ] ) <EOL>
|
||||
print ( l [ 3 : ] ) <EOL>
|
||||
print ( l [ - 4 : ] ) <EOL>
|
||||
print ( l [ : - 2 ] ) <EOL>
|
||||
print ( l [ : ] ) <EOL>
|
7
tests/tokenized/store_slice.txt
Normal file
7
tests/tokenized/store_slice.txt
Normal file
@@ -0,0 +1,7 @@
|
||||
a = [ 0 ] * 16 <EOL>
|
||||
l = [ 1 , 2 , 3 ] <EOL>
|
||||
a [ 13 : ] = l <EOL>
|
||||
a [ 5 ] = 10 <EOL>
|
||||
a [ : 2 ] = [ 1 , 2 ] <EOL>
|
||||
a [ : ] = range ( 16 ) <EOL>
|
||||
print ( a ) <EOL>
|
Reference in New Issue
Block a user