impl UNPACK_SEQUENCE

This commit is contained in:
blueloveTH 2022-11-06 16:57:33 +08:00
parent a4dc943c7b
commit 52ec435775
2 changed files with 11 additions and 0 deletions

View File

@ -26,6 +26,7 @@ OPCODE(BUILD_LIST)
OPCODE(BUILD_TUPLE) OPCODE(BUILD_TUPLE)
OPCODE(BUILD_MAP) OPCODE(BUILD_MAP)
OPCODE(BUILD_SLICE) OPCODE(BUILD_SLICE)
OPCODE(UNPACK_SEQUENCE)
OPCODE(BINARY_SUBSCR) OPCODE(BINARY_SUBSCR)
OPCODE(STORE_SUBSCR) OPCODE(STORE_SUBSCR)

View File

@ -203,6 +203,16 @@ public:
callstack.pop(); callstack.pop();
return ret; return ret;
} break; } break;
case OP_UNPACK_SEQUENCE:
{
PyVar seq = frame->popValue();
bool iterable = (seq->isType(_tp_tuple) || seq->isType(_tp_list));
if(!iterable) _error("TypeError", "only tuple and list can be unpacked");
const PyVarList& objs = std::get<PyVarList>(seq->_native);
if(objs.size() > byte.arg) _error("ValueError", "too many values to unpack (expected " + std::to_string(byte.arg) + ")");
if(objs.size() < byte.arg) _error("ValueError", "not enough values to unpack (expected " + std::to_string(byte.arg) + ", got " + std::to_string(objs.size()) + ")");
for(auto it=objs.rbegin(); it!=objs.rend(); it++) frame->pushValue(*it);
} break;
case OP_PRINT_EXPR: case OP_PRINT_EXPR:
{ {
const PyVar& expr = frame->topValue(); const PyVar& expr = frame->topValue();