mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-20 19:40:18 +00:00
fix a bug
This commit is contained in:
parent
a0d52f52d1
commit
542ffccd23
@ -28,16 +28,17 @@ _Str pad(const _Str& s, const int n){
|
|||||||
|
|
||||||
struct CodeObject {
|
struct CodeObject {
|
||||||
_Source src;
|
_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->src = src;
|
||||||
this->co_name = co_name;
|
this->name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<ByteCode> co_code;
|
std::vector<ByteCode> co_code;
|
||||||
PyVarList co_consts;
|
PyVarList co_consts;
|
||||||
std::vector<std::shared_ptr<NamePointer>> co_names;
|
std::vector<std::shared_ptr<NamePointer>> co_names;
|
||||||
|
std::vector<_Str> co_global_names;
|
||||||
|
|
||||||
// for goto use
|
// for goto use
|
||||||
// note: some opcodes moves the bytecode, such as listcomp
|
// note: some opcodes moves the bytecode, such as listcomp
|
||||||
@ -53,9 +54,12 @@ struct CodeObject {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int addName(const _Str& name, NameScope scope){
|
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);
|
auto p = std::make_shared<NamePointer>(name, scope);
|
||||||
for(int i=0; i<co_names.size(); i++){
|
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);
|
co_names.push_back(p);
|
||||||
return co_names.size() - 1;
|
return co_names.size() - 1;
|
||||||
@ -106,7 +110,7 @@ struct CodeObject {
|
|||||||
ss << '\n' << consts.str() << '\n' << names.str() << '\n';
|
ss << '\n' << consts.str() << '\n' << names.str() << '\n';
|
||||||
for(int i=0; i<co_consts.size(); i++){
|
for(int i=0; i<co_consts.size(); i++){
|
||||||
auto fn = std::get_if<_Func>(&co_consts[i]->_native);
|
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);
|
return _Str(ss);
|
||||||
}
|
}
|
||||||
|
@ -777,7 +777,7 @@ __LISTCOMP:
|
|||||||
consumeEndStatement();
|
consumeEndStatement();
|
||||||
} else if(match(TK("global"))){
|
} else if(match(TK("global"))){
|
||||||
consume(TK("@id"));
|
consume(TK("@id"));
|
||||||
int index = getCode()->addName(parser->previous.str(), NAME_GLOBAL);
|
getCode()->co_global_names.push_back(parser->previous.str());
|
||||||
consumeEndStatement();
|
consumeEndStatement();
|
||||||
} else if(match(TK("pass"))){
|
} else if(match(TK("pass"))){
|
||||||
consumeEndStatement();
|
consumeEndStatement();
|
||||||
|
@ -76,7 +76,7 @@ typedef std::variant<_Int,_Float,bool,_Str,PyVarList,_CppFunc,_Func,std::shared_
|
|||||||
const int _SIZEOF_VALUE = sizeof(_Value);
|
const int _SIZEOF_VALUE = sizeof(_Value);
|
||||||
|
|
||||||
#ifdef POCKETPY_H
|
#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
|
#else
|
||||||
#define UNREACHABLE() throw std::runtime_error( __FILE__ + std::string(":") + std::to_string(__LINE__) + " UNREACHABLE()!");
|
#define UNREACHABLE() throw std::runtime_error( __FILE__ + std::string(":") + std::to_string(__LINE__) + " UNREACHABLE()!");
|
||||||
#endif
|
#endif
|
||||||
|
@ -21,6 +21,10 @@ struct NamePointer : BasePointer {
|
|||||||
const NameScope scope;
|
const NameScope scope;
|
||||||
NamePointer(const _Str& name, NameScope scope) : name(name), scope(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;
|
PyVar get(VM* vm, Frame* frame) const;
|
||||||
void set(VM* vm, Frame* frame, PyVar val) const;
|
void set(VM* vm, Frame* frame, PyVar val) const;
|
||||||
void del(VM* vm, Frame* frame) const;
|
void del(VM* vm, Frame* frame) const;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user