mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-21 12:00:18 +00:00
some renames
This commit is contained in:
parent
c623646501
commit
bf6383b5ee
@ -32,15 +32,15 @@ public:
|
|||||||
_Str co_name;
|
_Str co_name;
|
||||||
|
|
||||||
PyVarList co_consts;
|
PyVarList co_consts;
|
||||||
std::vector<NamePointer> co_name_ptrs;
|
std::vector<NamePointer> co_names;
|
||||||
|
|
||||||
int addNamePtr(const _Str& name, NameScope scope){
|
int addName(const _Str& name, NameScope scope){
|
||||||
auto p = NamePointer(name, scope);
|
auto p = NamePointer(name, scope);
|
||||||
for(int i=0; i<co_name_ptrs.size(); i++){
|
for(int i=0; i<co_names.size(); i++){
|
||||||
if(co_name_ptrs[i] == p) return i;
|
if(co_names[i] == p) return i;
|
||||||
}
|
}
|
||||||
co_name_ptrs.push_back(p);
|
co_names.push_back(p);
|
||||||
return co_name_ptrs.size() - 1;
|
return co_names.size() - 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int addConst(PyVar v){
|
int addConst(PyVar v){
|
||||||
@ -73,9 +73,9 @@ public:
|
|||||||
|
|
||||||
_StrStream names;
|
_StrStream names;
|
||||||
names << "co_names: ";
|
names << "co_names: ";
|
||||||
for(int i=0; i<co_name_ptrs.size(); i++){
|
for(int i=0; i<co_names.size(); i++){
|
||||||
names << co_name_ptrs[i].name;
|
names << co_names[i].name;
|
||||||
if(i != co_name_ptrs.size() - 1) names << ", ";
|
if(i != co_names.size() - 1) names << ", ";
|
||||||
}
|
}
|
||||||
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++){
|
||||||
|
@ -404,7 +404,7 @@ public:
|
|||||||
|
|
||||||
void exprName() {
|
void exprName() {
|
||||||
Token tkname = parser->previous;
|
Token tkname = parser->previous;
|
||||||
int index = getCode()->addNamePtr(
|
int index = getCode()->addName(
|
||||||
tkname.str(),
|
tkname.str(),
|
||||||
codes.size()>1 ? NAME_LOCAL : NAME_GLOBAL
|
codes.size()>1 ? NAME_LOCAL : NAME_GLOBAL
|
||||||
);
|
);
|
||||||
@ -414,7 +414,7 @@ public:
|
|||||||
void exprAttrib() {
|
void exprAttrib() {
|
||||||
consume(TK("@id"));
|
consume(TK("@id"));
|
||||||
const _Str& name = parser->previous.str();
|
const _Str& name = parser->previous.str();
|
||||||
int index = getCode()->addNamePtr(name, NAME_ATTR);
|
int index = getCode()->addName(name, NAME_ATTR);
|
||||||
emitCode(OP_BUILD_ATTR_PTR, index);
|
emitCode(OP_BUILD_ATTR_PTR, index);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -521,7 +521,7 @@ public:
|
|||||||
Token compileImportPath() {
|
Token compileImportPath() {
|
||||||
consume(TK("@id"));
|
consume(TK("@id"));
|
||||||
Token tkmodule = parser->previous;
|
Token tkmodule = parser->previous;
|
||||||
int index = getCode()->addNamePtr(tkmodule.str(), NAME_GLOBAL);
|
int index = getCode()->addName(tkmodule.str(), NAME_GLOBAL);
|
||||||
emitCode(OP_IMPORT_NAME, index);
|
emitCode(OP_IMPORT_NAME, index);
|
||||||
return tkmodule;
|
return tkmodule;
|
||||||
}
|
}
|
||||||
@ -534,7 +534,7 @@ public:
|
|||||||
consume(TK("@id"));
|
consume(TK("@id"));
|
||||||
tkmodule = parser->previous;
|
tkmodule = parser->previous;
|
||||||
}
|
}
|
||||||
int index = getCode()->addNamePtr(tkmodule.str(), NAME_GLOBAL);
|
int index = getCode()->addName(tkmodule.str(), NAME_GLOBAL);
|
||||||
emitCode(OP_STORE_NAME_PTR, index);
|
emitCode(OP_STORE_NAME_PTR, index);
|
||||||
} while (match(TK(",")) && (matchNewLines(), true));
|
} while (match(TK(",")) && (matchNewLines(), true));
|
||||||
consumeEndStatement();
|
consumeEndStatement();
|
||||||
@ -602,7 +602,7 @@ public:
|
|||||||
|
|
||||||
void compileForStatement() {
|
void compileForStatement() {
|
||||||
consume(TK("@id"));
|
consume(TK("@id"));
|
||||||
int iterIndex = getCode()->addNamePtr(
|
int iterIndex = getCode()->addName(
|
||||||
parser->previous.str(),
|
parser->previous.str(),
|
||||||
codes.size()>1 ? NAME_LOCAL : NAME_GLOBAL
|
codes.size()>1 ? NAME_LOCAL : NAME_GLOBAL
|
||||||
);
|
);
|
||||||
@ -680,11 +680,11 @@ public:
|
|||||||
|
|
||||||
void compileClass(){
|
void compileClass(){
|
||||||
consume(TK("@id"));
|
consume(TK("@id"));
|
||||||
int clsNameIdx = getCode()->addNamePtr(parser->previous.str(), NAME_GLOBAL);
|
int clsNameIdx = getCode()->addName(parser->previous.str(), NAME_GLOBAL);
|
||||||
int superClsNameIdx = -1;
|
int superClsNameIdx = -1;
|
||||||
if(match(TK("("))){
|
if(match(TK("("))){
|
||||||
consume(TK("@id"));
|
consume(TK("@id"));
|
||||||
superClsNameIdx = getCode()->addNamePtr(parser->previous.str(), NAME_GLOBAL);
|
superClsNameIdx = getCode()->addName(parser->previous.str(), NAME_GLOBAL);
|
||||||
consume(TK(")"));
|
consume(TK(")"));
|
||||||
}
|
}
|
||||||
emitCode(OP_LOAD_NONE);
|
emitCode(OP_LOAD_NONE);
|
||||||
|
32
src/vm.h
32
src/vm.h
@ -138,15 +138,15 @@ public:
|
|||||||
{
|
{
|
||||||
case OP_LOAD_CONST: frame->pushValue(frame->code->co_consts[byte.arg]); break;
|
case OP_LOAD_CONST: frame->pushValue(frame->code->co_consts[byte.arg]); break;
|
||||||
case OP_LOAD_NAME_PTR: {
|
case OP_LOAD_NAME_PTR: {
|
||||||
const NamePointer* p = &frame->code->co_name_ptrs[byte.arg];
|
const NamePointer* p = &frame->code->co_names[byte.arg];
|
||||||
frame->pushValue(PyPointer(_Pointer(p)));
|
frame->pushValue(PyPointer(_Pointer(p)));
|
||||||
} break;
|
} break;
|
||||||
case OP_STORE_NAME_PTR: {
|
case OP_STORE_NAME_PTR: {
|
||||||
const NamePointer& p = frame->code->co_name_ptrs[byte.arg];
|
const NamePointer& p = frame->code->co_names[byte.arg];
|
||||||
p.set(this, frame.get(), frame->popValue());
|
p.set(this, frame.get(), frame->popValue());
|
||||||
} break;
|
} break;
|
||||||
case OP_BUILD_ATTR_PTR: {
|
case OP_BUILD_ATTR_PTR: {
|
||||||
const NamePointer* p = &frame->code->co_name_ptrs[byte.arg];
|
const NamePointer* p = &frame->code->co_names[byte.arg];
|
||||||
_Pointer root = PyPointer_AS_C(frame->popValue());
|
_Pointer root = PyPointer_AS_C(frame->popValue());
|
||||||
frame->pushValue(PyPointer(
|
frame->pushValue(PyPointer(
|
||||||
std::make_shared<AttrPointer>(root, p)
|
std::make_shared<AttrPointer>(root, p)
|
||||||
@ -171,7 +171,7 @@ public:
|
|||||||
} break;
|
} break;
|
||||||
case OP_BUILD_CLASS:
|
case OP_BUILD_CLASS:
|
||||||
{
|
{
|
||||||
const _Str& clsName = frame->code->co_name_ptrs[byte.arg].name;
|
const _Str& clsName = frame->code->co_names[byte.arg].name;
|
||||||
PyVar clsBase = frame->popValue();
|
PyVar clsBase = frame->popValue();
|
||||||
if(clsBase == None) clsBase = _tp_object;
|
if(clsBase == None) clsBase = _tp_object;
|
||||||
__checkType(clsBase, _tp_type);
|
__checkType(clsBase, _tp_type);
|
||||||
@ -324,7 +324,7 @@ public:
|
|||||||
} break;
|
} break;
|
||||||
case OP_IMPORT_NAME:
|
case OP_IMPORT_NAME:
|
||||||
{
|
{
|
||||||
const _Str& name = frame->code->co_name_ptrs[byte.arg].name;
|
const _Str& name = frame->code->co_names[byte.arg].name;
|
||||||
auto it = _modules.find(name);
|
auto it = _modules.find(name);
|
||||||
if(it == _modules.end()){
|
if(it == _modules.end()){
|
||||||
_error("ImportError", "module '" + name + "' not found");
|
_error("ImportError", "module '" + name + "' not found");
|
||||||
@ -567,17 +567,27 @@ public:
|
|||||||
/**************** Pointers' Impl ****************/
|
/**************** Pointers' Impl ****************/
|
||||||
|
|
||||||
PyVar NamePointer::get(VM* vm, Frame* frame) const{
|
PyVar NamePointer::get(VM* vm, Frame* frame) const{
|
||||||
switch(scope) {
|
auto it = frame->f_locals.find(name);
|
||||||
case NAME_LOCAL: frame->f_locals[name] = frame->popValue(); break;
|
if(it != frame->f_locals.end()) return it->second;
|
||||||
case NAME_GLOBAL: frame->f_globals->operator[](name) = frame->popValue(); break;
|
it = frame->f_globals->find(name);
|
||||||
}
|
if(it != frame->f_globals->end()) return it->second;
|
||||||
UNREACHABLE();
|
it = vm->builtins->attribs.find(name);
|
||||||
|
if(it != vm->builtins->attribs.end()) return it->second;
|
||||||
|
vm->nameError(name);
|
||||||
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
void NamePointer::set(VM* vm, Frame* frame, PyVar val) const{
|
void NamePointer::set(VM* vm, Frame* frame, PyVar val) const{
|
||||||
switch(scope) {
|
switch(scope) {
|
||||||
case NAME_LOCAL: frame->f_locals[name] = val; break;
|
case NAME_LOCAL: frame->f_locals[name] = val; break;
|
||||||
case NAME_GLOBAL: frame->f_globals->operator[](name) = val; break;
|
case NAME_GLOBAL:
|
||||||
|
{
|
||||||
|
if(frame->f_locals.find(name) != frame->f_locals.end()){
|
||||||
|
frame->f_locals[name] = frame->popValue();
|
||||||
|
}else{
|
||||||
|
frame->f_globals->operator[](name) = frame->popValue();
|
||||||
|
}
|
||||||
|
} break;
|
||||||
}
|
}
|
||||||
UNREACHABLE();
|
UNREACHABLE();
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user