diff --git a/src/codeobject.h b/src/codeobject.h index ea19fbe5..d0ae7d38 100644 --- a/src/codeobject.h +++ b/src/codeobject.h @@ -28,16 +28,17 @@ _Str pad(const _Str& s, const int n){ struct CodeObject { _Source src; - _Str co_name; + _Str name; - CodeObject(_Source src, _Str co_name, CompileMode mode=EXEC_MODE) { + CodeObject(_Source src, _Str name, CompileMode mode=EXEC_MODE) { this->src = src; - this->co_name = co_name; + this->name = name; } std::vector co_code; PyVarList co_consts; std::vector> co_names; + std::vector<_Str> co_global_names; // for goto use // note: some opcodes moves the bytecode, such as listcomp @@ -53,9 +54,12 @@ struct CodeObject { } int addName(const _Str& name, NameScope scope){ + if(scope == NAME_LOCAL && std::find(co_global_names.begin(), co_global_names.end(), name) != co_global_names.end()){ + scope = NAME_GLOBAL; + } auto p = std::make_shared(name, scope); for(int i=0; iname == p->name) return i; + if(*co_names[i] == *p) return i; } co_names.push_back(p); return co_names.size() - 1; @@ -106,7 +110,7 @@ struct CodeObject { ss << '\n' << consts.str() << '\n' << names.str() << '\n'; for(int i=0; i(&co_consts[i]->_native); - if(fn) ss << '\n' << (*fn)->code->co_name << ":\n" << (*fn)->code->toString(); + if(fn) ss << '\n' << (*fn)->code->name << ":\n" << (*fn)->code->toString(); } return _Str(ss); } diff --git a/src/compiler.h b/src/compiler.h index a7877674..782ffa1a 100644 --- a/src/compiler.h +++ b/src/compiler.h @@ -777,7 +777,7 @@ __LISTCOMP: consumeEndStatement(); } else if(match(TK("global"))){ consume(TK("@id")); - int index = getCode()->addName(parser->previous.str(), NAME_GLOBAL); + getCode()->co_global_names.push_back(parser->previous.str()); consumeEndStatement(); } else if(match(TK("pass"))){ consumeEndStatement(); diff --git a/src/obj.h b/src/obj.h index 43b2cc56..64f4d9ca 100644 --- a/src/obj.h +++ b/src/obj.h @@ -76,7 +76,7 @@ typedef std::variant<_Int,_Float,bool,_Str,PyVarList,_CppFunc,_Func,std::shared_ const int _SIZEOF_VALUE = sizeof(_Value); #ifdef POCKETPY_H -#define UNREACHABLE() throw std::runtime_error( "L" + std::to_string(__LINE__) + ": UNREACHABLE()! This should be a bug, please report it"); +#define UNREACHABLE() throw std::runtime_error( "L" + std::to_string(__LINE__) + " UNREACHABLE()! This should be a bug, please report it"); #else #define UNREACHABLE() throw std::runtime_error( __FILE__ + std::string(":") + std::to_string(__LINE__) + " UNREACHABLE()!"); #endif diff --git a/src/pointer.h b/src/pointer.h index 8db647ba..d2004d87 100644 --- a/src/pointer.h +++ b/src/pointer.h @@ -21,6 +21,10 @@ struct NamePointer : BasePointer { const NameScope scope; NamePointer(const _Str& name, NameScope scope) : name(name), scope(scope) {} + bool operator==(const NamePointer& other) const { + return name == other.name && scope == other.scope; + } + PyVar get(VM* vm, Frame* frame) const; void set(VM* vm, Frame* frame, PyVar val) const; void del(VM* vm, Frame* frame) const;