Explorar el Código

impl UNPACK_SEQUENCE

blueloveTH hace 3 años
padre
commit
52ec435775
Se han modificado 2 ficheros con 11 adiciones y 0 borrados
  1. 1 0
      src/opcodes.h
  2. 10 0
      src/vm.h

+ 1 - 0
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)

+ 10 - 0
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<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:
                 {
                     const PyVar& expr = frame->topValue();