blueloveTH 3 년 전
부모
커밋
5c2352fa90
3개의 변경된 파일11개의 추가작업 그리고 10개의 파일을 삭제
  1. 4 4
      src/compiler.h
  2. 2 2
      src/opcodes.h
  3. 5 4
      src/vm.h

+ 4 - 4
src/compiler.h

@@ -726,7 +726,7 @@ __LISTCOMP:
                 tkmodule = parser->prev;
             }
             int index = co()->add_name(tkmodule.str(), NAME_GLOBAL);
-            emit(OP_STORE_NAME_REF, index);
+            emit(OP_STORE_NAME, index);
         } while (match(TK(",")));
         consumeEndStatement();
     }
@@ -746,7 +746,7 @@ __LISTCOMP:
                 tkname = parser->prev;
             }
             index = co()->add_name(tkname.str(), NAME_GLOBAL);
-            emit(OP_STORE_NAME_REF, index);
+            emit(OP_STORE_NAME, index);
         } while (match(TK(",")));
         emit(OP_POP_TOP);
         consumeEndStatement();
@@ -873,7 +873,7 @@ __LISTCOMP:
                 tkname.str(),
                 codes.size()>1 ? NAME_LOCAL : NAME_GLOBAL
             );
-            emit(OP_STORE_NAME_REF, index);
+            emit(OP_STORE_NAME, index);
             emit(OP_LOAD_NAME_REF, index);
             emit(OP_WITH_ENTER);
             compileBlockBody();
@@ -919,7 +919,7 @@ __LISTCOMP:
             consumeEndStatement();
             // If last op is not an assignment, pop the result.
             uint8_t lastOp = co()->co_code.back().op;
-            if( lastOp!=OP_STORE_NAME_REF && lastOp!=OP_STORE_REF){
+            if( lastOp!=OP_STORE_NAME && lastOp!=OP_STORE_REF){
                 if(mode()==SINGLE_MODE && parser->indents.top()==0) emit(OP_PRINT_EXPR);
                 emit(OP_POP_TOP);
             }

+ 2 - 2
src/opcodes.h

@@ -51,11 +51,11 @@ OPCODE(LOAD_NAME_REF)
 OPCODE(ASSERT)
 OPCODE(RAISE_ERROR)
 
-OPCODE(STORE_FUNCTION)
 OPCODE(BUILD_CLASS)
 OPCODE(BUILD_ATTR_REF)
+OPCODE(STORE_NAME)
+OPCODE(STORE_FUNCTION)
 OPCODE(BUILD_INDEX_REF)
-OPCODE(STORE_NAME_REF)
 OPCODE(STORE_REF)
 OPCODE(DELETE_REF)
 

+ 5 - 4
src/vm.h

@@ -48,7 +48,7 @@ protected:
             case OP_LOAD_NAME: {
                 frame->push(NameRef(frame->code->co_names[byte.arg]).get(this, frame));
             } break;
-            case OP_STORE_NAME_REF: {
+            case OP_STORE_NAME: {
                 const auto& p = frame->code->co_names[byte.arg];
                 NameRef(p).set(this, frame, frame->pop_value(this));
             } break;
@@ -960,8 +960,9 @@ void NameRef::set(VM* vm, Frame* frame, PyVar val) const{
         case NAME_LOCAL: frame->f_locals[pair->first] = std::move(val); break;
         case NAME_GLOBAL:
         {
-            if(frame->f_locals.contains(pair->first)){
-                frame->f_locals[pair->first] = std::move(val);
+            PyVar* existing = frame->f_locals.try_get(pair->first);
+            if(existing != nullptr){
+                *existing = std::move(val);
             }else{
                 frame->f_globals()[pair->first] = std::move(val);
             }
@@ -973,7 +974,7 @@ void NameRef::set(VM* vm, Frame* frame, PyVar val) const{
 void NameRef::del(VM* vm, Frame* frame) const{
     switch(pair->second) {
         case NAME_LOCAL: {
-            if(frame->f_locals.count(pair->first) > 0){
+            if(frame->f_locals.contains(pair->first)){
                 frame->f_locals.erase(pair->first);
             }else{
                 vm->nameError(pair->first);