From 52ec43577586bc3c9423580333cf93ac9f6039fd Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Sun, 6 Nov 2022 16:57:33 +0800 Subject: [PATCH] impl UNPACK_SEQUENCE --- src/opcodes.h | 1 + src/vm.h | 10 ++++++++++ 2 files changed, 11 insertions(+) diff --git a/src/opcodes.h b/src/opcodes.h index d5cf48e8..c255bc37 100644 --- a/src/opcodes.h +++ b/src/opcodes.h @@ -26,6 +26,7 @@ OPCODE(BUILD_LIST) OPCODE(BUILD_TUPLE) OPCODE(BUILD_MAP) OPCODE(BUILD_SLICE) +OPCODE(UNPACK_SEQUENCE) OPCODE(BINARY_SUBSCR) OPCODE(STORE_SUBSCR) diff --git a/src/vm.h b/src/vm.h index 1b30b55b..4ffd20f1 100644 --- a/src/vm.h +++ b/src/vm.h @@ -203,6 +203,16 @@ public: callstack.pop(); return ret; } 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(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: { const PyVar& expr = frame->topValue();