fix a bug

This commit is contained in:
blueloveTH 2022-11-15 19:27:34 +08:00
parent a0d52f52d1
commit 542ffccd23
4 changed files with 15 additions and 7 deletions

View File

@ -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<ByteCode> co_code;
PyVarList co_consts;
std::vector<std::shared_ptr<NamePointer>> 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<NamePointer>(name, scope);
for(int i=0; i<co_names.size(); i++){
if(co_names[i]->name == 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.size(); i++){
auto fn = std::get_if<_Func>(&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);
}

View File

@ -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();

View File

@ -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

View File

@ -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;