From 5c2352fa90aaa87d6b7b37617d4615fad22a848c Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Fri, 27 Jan 2023 04:04:32 +0800 Subject: [PATCH] up --- src/compiler.h | 8 ++++---- src/opcodes.h | 4 ++-- src/vm.h | 9 +++++---- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/src/compiler.h b/src/compiler.h index e2a5c75a..91e95525 100644 --- a/src/compiler.h +++ b/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); } diff --git a/src/opcodes.h b/src/opcodes.h index b3bcdb2b..69f3df91 100644 --- a/src/opcodes.h +++ b/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) diff --git a/src/vm.h b/src/vm.h index 2921c81c..ce21d61c 100644 --- a/src/vm.h +++ b/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);