blueloveTH пре 3 година
родитељ
комит
bf6383b5ee
3 измењених фајлова са 37 додато и 27 уклоњено
  1. 9 9
      src/codeobject.h
  2. 7 7
      src/compiler.h
  3. 21 11
      src/vm.h

+ 9 - 9
src/codeobject.h

@@ -32,15 +32,15 @@ public:
     _Str co_name;
 
     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);
-        for(int i=0; i<co_name_ptrs.size(); i++){
-            if(co_name_ptrs[i] == p) return i;
+        for(int i=0; i<co_names.size(); i++){
+            if(co_names[i] == p) return i;
         }
-        co_name_ptrs.push_back(p);
-        return co_name_ptrs.size() - 1;
+        co_names.push_back(p);
+        return co_names.size() - 1;
     }
 
     int addConst(PyVar v){
@@ -73,9 +73,9 @@ public:
 
         _StrStream names;
         names << "co_names: ";
-        for(int i=0; i<co_name_ptrs.size(); i++){
-            names << co_name_ptrs[i].name;
-            if(i != co_name_ptrs.size() - 1) names << ", ";
+        for(int i=0; i<co_names.size(); i++){
+            names << co_names[i].name;
+            if(i != co_names.size() - 1) names << ", ";
         }
         ss << '\n' << consts.str() << '\n' << names.str() << '\n';
         for(int i=0; i<co_consts.size(); i++){

+ 7 - 7
src/compiler.h

@@ -404,7 +404,7 @@ public:
 
     void exprName() {
         Token tkname = parser->previous;
-        int index = getCode()->addNamePtr(
+        int index = getCode()->addName(
             tkname.str(),
             codes.size()>1 ? NAME_LOCAL : NAME_GLOBAL
         );
@@ -414,7 +414,7 @@ public:
     void exprAttrib() {
         consume(TK("@id"));
         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);
     }
 
@@ -521,7 +521,7 @@ public:
     Token compileImportPath() {
         consume(TK("@id"));
         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);
         return tkmodule;
     }
@@ -534,7 +534,7 @@ public:
                 consume(TK("@id"));
                 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);
         } while (match(TK(",")) && (matchNewLines(), true));
         consumeEndStatement();
@@ -602,7 +602,7 @@ public:
 
     void compileForStatement() {
         consume(TK("@id"));
-        int iterIndex = getCode()->addNamePtr(
+        int iterIndex = getCode()->addName(
             parser->previous.str(),
             codes.size()>1 ? NAME_LOCAL : NAME_GLOBAL
         );
@@ -680,11 +680,11 @@ public:
 
     void compileClass(){
         consume(TK("@id"));
-        int clsNameIdx = getCode()->addNamePtr(parser->previous.str(), NAME_GLOBAL);
+        int clsNameIdx = getCode()->addName(parser->previous.str(), NAME_GLOBAL);
         int superClsNameIdx = -1;
         if(match(TK("("))){
             consume(TK("@id"));
-            superClsNameIdx = getCode()->addNamePtr(parser->previous.str(), NAME_GLOBAL);
+            superClsNameIdx = getCode()->addName(parser->previous.str(), NAME_GLOBAL);
             consume(TK(")"));
         }
         emitCode(OP_LOAD_NONE);

+ 21 - 11
src/vm.h

@@ -138,15 +138,15 @@ public:
             {
             case OP_LOAD_CONST: frame->pushValue(frame->code->co_consts[byte.arg]); break;
             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)));
             } break;
             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());
             } break;
             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());
                 frame->pushValue(PyPointer(
                     std::make_shared<AttrPointer>(root, p)
@@ -171,7 +171,7 @@ public:
                 } break;
             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();
                     if(clsBase == None) clsBase = _tp_object;
                     __checkType(clsBase, _tp_type);
@@ -324,7 +324,7 @@ public:
                 } break;
             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);
                     if(it == _modules.end()){
                         _error("ImportError", "module '" + name + "' not found");
@@ -567,17 +567,27 @@ public:
 /**************** Pointers' Impl ****************/
 
 PyVar NamePointer::get(VM* vm, Frame* frame) const{
-    switch(scope) {
-        case NAME_LOCAL: frame->f_locals[name] = frame->popValue(); break;
-        case NAME_GLOBAL: frame->f_globals->operator[](name) = frame->popValue(); break;
-    }
-    UNREACHABLE();
+    auto it = frame->f_locals.find(name);
+    if(it != frame->f_locals.end()) return it->second;
+    it = frame->f_globals->find(name);
+    if(it != frame->f_globals->end()) return it->second;
+    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{
     switch(scope) {
         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();
 }