blueloveTH 3 rokov pred
rodič
commit
e5e6c49b29
11 zmenil súbory, kde vykonal 51 pridanie a 83 odobranie
  1. 8 8
      src/ceval.h
  2. 5 5
      src/codeobject.h
  3. 1 0
      src/common.h
  4. 5 5
      src/error.h
  5. 1 1
      src/frame.h
  6. 1 1
      src/obj.h
  7. 2 2
      src/pocketpy.h
  8. 4 4
      src/ref.h
  9. 1 1
      src/safestl.h
  10. 0 33
      src/str.h
  11. 23 23
      src/vm.h

+ 8 - 8
src/ceval.h

@@ -91,7 +91,7 @@ PyVar VM::run_frame(Frame* frame){
             list.push_back(std::move(obj));
         } continue;
         case OP_BUILD_CLASS: {
-            const Str& clsName = frame->co->names[byte.arg].first;
+            const Str& clsName = frame->co->names[byte.arg].first.str();
             PyVar clsBase = frame->pop_value(this);
             if(clsBase == None) clsBase = _t(tp_object);
             check_type(clsBase, tp_type);
@@ -179,13 +179,13 @@ PyVar VM::run_frame(Frame* frame){
         } continue;
         case OP_EXCEPTION_MATCH: {
             const auto& e = PyException_AS_C(frame->top());
-            Str name = frame->co->names[byte.arg].first;
+            StrName name = frame->co->names[byte.arg].first;
             frame->push(PyBool(e.match_type(name)));
         } continue;
         case OP_RAISE: {
             PyVar obj = frame->pop_value(this);
             Str msg = obj == None ? "" : PyStr_AS_C(asStr(obj));
-            Str type = frame->co->names[byte.arg].first;
+            StrName type = frame->co->names[byte.arg].first;
             _error(type, msg);
         } continue;
         case OP_RE_RAISE: _raise(); continue;
@@ -222,9 +222,9 @@ PyVar VM::run_frame(Frame* frame){
         case OP_JUMP_ABSOLUTE: frame->jump_abs(byte.arg); continue;
         case OP_SAFE_JUMP_ABSOLUTE: frame->jump_abs_safe(byte.arg); continue;
         case OP_GOTO: {
-            const Str& label = frame->co->names[byte.arg].first;
+            StrName label = frame->co->names[byte.arg].first;
             int* target = frame->co->labels.try_get(label);
-            if(target == nullptr) _error("KeyError", "label '" + label + "' not found");
+            if(target == nullptr) _error("KeyError", "label " + label.str().escape(true) + " not found");
             frame->jump_abs_safe(*target);
         } continue;
         case OP_GET_ITER: {
@@ -271,15 +271,15 @@ PyVar VM::run_frame(Frame* frame){
             frame->push(PySlice(s));
         } continue;
         case OP_IMPORT_NAME: {
-            const Str& name = frame->co->names[byte.arg].first;
+            StrName name = frame->co->names[byte.arg].first;
             auto it = _modules.find(name);
             if(it == _modules.end()){
                 auto it2 = _lazy_modules.find(name);
                 if(it2 == _lazy_modules.end()){
-                    _error("ImportError", "module " + name.escape(true) + " not found");
+                    _error("ImportError", "module " + name.str().escape(true) + " not found");
                 }else{
                     const Str& source = it2->second;
-                    CodeObject_ code = compile(source, name, EXEC_MODE);
+                    CodeObject_ code = compile(source, name.str(), EXEC_MODE);
                     PyVar _m = new_module(name);
                     _exec(code, _m);
                     frame->push(_m);

+ 5 - 5
src/codeobject.h

@@ -60,20 +60,20 @@ struct CodeObject {
 
     std::vector<Bytecode> codes;
     pkpy::List consts;
-    std::vector<std::pair<Str, NameScope>> names;
-    emhash8::HashMap<Str, int> global_names;
+    std::vector<std::pair<StrName, NameScope>> names;
+    emhash8::HashMap<StrName, int> global_names;
     std::vector<CodeBlock> blocks = { CodeBlock{NO_BLOCK, -1} };
-    emhash8::HashMap<Str, int> labels;
+    emhash8::HashMap<StrName, int> labels;
 
     void optimize(VM* vm);
 
-    bool add_label(const Str& label){
+    bool add_label(StrName label){
         if(labels.contains(label)) return false;
         labels[label] = codes.size();
         return true;
     }
 
-    int add_name(Str name, NameScope scope){
+    int add_name(StrName name, NameScope scope){
         if(scope == NAME_LOCAL && global_names.contains(name)) scope = NAME_GLOBAL;
         auto p = std::make_pair(name, scope);
         for(int i=0; i<names.size(); i++){

+ 1 - 0
src/common.h

@@ -23,6 +23,7 @@
 #include <memory>
 #include <functional>
 #include <iostream>
+#include <map>
 // #include <filesystem>
 // namespace fs = std::filesystem;
 

+ 5 - 5
src/error.h

@@ -68,12 +68,12 @@ struct SourceData {
 
 namespace pkpy{
 class Exception {
-    Str type;
+    StrName type;
     Str msg;
     std::stack<Str> stacktrace;
 public:
-    Exception(Str type, Str msg): type(type), msg(msg) {}
-    bool match_type(const Str& type) const { return this->type == type;}
+    Exception(StrName type, Str msg): type(type), msg(msg) {}
+    bool match_type(StrName type) const { return this->type == type;}
     bool is_re = true;
 
     void st_push(Str snapshot){
@@ -86,8 +86,8 @@ public:
         StrStream ss;
         if(is_re) ss << "Traceback (most recent call last):\n";
         while(!st.empty()) { ss << st.top() << '\n'; st.pop(); }
-        if (!msg.empty()) ss << type << ": " << msg;
-        else ss << type;
+        if (!msg.empty()) ss << type.str() << ": " << msg;
+        else ss << type.str();
         return ss.str();
     }
 };

+ 1 - 1
src/frame.h

@@ -19,7 +19,7 @@ struct Frame {
     inline pkpy::NameDict& f_locals() noexcept { return _locals != nullptr ? *_locals : _module->attr(); }
     inline pkpy::NameDict& f_globals() noexcept { return _module->attr(); }
 
-    inline PyVar* f_closure_try_get(const Str& name) noexcept {
+    inline PyVar* f_closure_try_get(StrName name) noexcept {
         if(_closure == nullptr) return nullptr;
         return _closure->try_get(name);
     }

+ 1 - 1
src/obj.h

@@ -82,7 +82,7 @@ struct PyObject {
 
     inline bool is_attr_valid() const noexcept { return _attr != nullptr; }
     inline pkpy::NameDict& attr() noexcept { return *_attr; }
-    inline PyVar& attr(const Str& name) noexcept { return (*_attr)[name]; }
+    inline PyVar& attr(StrName name) noexcept { return (*_attr)[name]; }
     virtual void* value() = 0;
 
     PyObject(Type type) : type(type) {}

+ 2 - 2
src/pocketpy.h

@@ -123,7 +123,7 @@ void init_builtins(VM* _vm) {
     });
 
     _vm->bind_builtin_func<1>("dir", [](VM* vm, pkpy::Args& args) {
-        std::vector<Str> names;
+        std::vector<StrName> names;
         if(args[0]->is_attr_valid()){
             for (auto& [k, _] : args[0]->attr()) names.push_back(k);
         }
@@ -131,7 +131,7 @@ void init_builtins(VM* _vm) {
             if (std::find(names.begin(), names.end(), k) == names.end()) names.push_back(k);
         }
         pkpy::List ret;
-        for (const auto& name : names) ret.push_back(vm->PyStr(name));
+        for (const auto& name : names) ret.push_back(vm->PyStr(name.str()));
         return vm->PyList(std::move(ret));
     });
 

+ 4 - 4
src/ref.h

@@ -17,10 +17,10 @@ enum NameScope {
 };
 
 struct NameRef : BaseRef {
-    std::pair<Str, NameScope>* _pair;
-    inline const Str& name() const { return _pair->first; }
-    inline NameScope scope() const { return _pair->second; }
-    NameRef(std::pair<Str, NameScope>& pair) : _pair(&pair) {}
+    const std::pair<StrName, NameScope> pair;
+    inline StrName name() const { return pair.first; }
+    inline NameScope scope() const { return pair.second; }
+    NameRef(std::pair<StrName, NameScope>& pair) : pair(pair) {}
 
     PyVar get(VM* vm, Frame* frame) const;
     void set(VM* vm, Frame* frame, PyVar val) const;

+ 1 - 1
src/safestl.h

@@ -33,7 +33,7 @@ public:
     using std::vector<PyVar>::vector;
 };
 
-typedef emhash8::HashMap<Str, PyVar> NameDict;
+typedef emhash8::HashMap<StrName, PyVar> NameDict;
 
 }
 

Rozdielové dáta súboru neboli zobrazené, pretože súbor je príliš veľký
+ 0 - 33
src/str.h


+ 23 - 23
src/vm.h

@@ -24,8 +24,8 @@ public:
     PyVar run_frame(Frame* frame);
 
     pkpy::NameDict _types;
-    pkpy::NameDict _modules;                             // loaded modules
-    emhash8::HashMap<Str, Str> _lazy_modules;     // lazy loaded modules
+    pkpy::NameDict _modules;                            // loaded modules
+    emhash8::HashMap<StrName, Str> _lazy_modules;       // lazy loaded modules
     PyVar None, True, False, Ellipsis;
 
     bool use_stdio;
@@ -93,7 +93,7 @@ public:
         return call(_t(tp_list), pkpy::one_arg(iterable));
     }
 
-    PyVar fast_call(const Str& name, pkpy::Args&& args){
+    PyVar fast_call(StrName name, pkpy::Args&& args){
         PyObject* cls = _t(args[0]).get();
         while(cls != None.get()) {
             PyVar* val = cls->attr().try_get(name);
@@ -116,12 +116,12 @@ public:
 
     template<typename ArgT>
     inline std::enable_if_t<std::is_same_v<RAW(ArgT), pkpy::Args>, PyVar>
-    call(const PyVar& obj, const Str& func, ArgT&& args){
-        return call(getattr(obj, func), std::forward<ArgT>(args), pkpy::no_arg(), false);
+    call(const PyVar& obj, const StrName name, ArgT&& args){
+        return call(getattr(obj, name), std::forward<ArgT>(args), pkpy::no_arg(), false);
     }
 
-    inline PyVar call(const PyVar& obj, const Str& func){
-        return call(getattr(obj, func), pkpy::no_arg(), pkpy::no_arg(), false);
+    inline PyVar call(const PyVar& obj, StrName name){
+        return call(getattr(obj, name), pkpy::no_arg(), pkpy::no_arg(), false);
     }
 
     PyVar call(const PyVar& _callable, pkpy::Args args, const pkpy::Args& kwargs, bool opCall){
@@ -284,19 +284,19 @@ public:
         }
     }
 
-    PyVar new_type_object(PyVar mod, Str name, PyVar base){
+    PyVar new_type_object(PyVar mod, StrName name, PyVar base){
         if(!is_type(base, tp_type)) UNREACHABLE();
         PyVar obj = pkpy::make_shared<PyObject, Py_<Type>>(tp_type, _all_types.size());
         setattr(obj, __base__, base);
-        Str fullName = name;
-        if(mod != builtins) fullName = OBJ_NAME(mod) + "." + name;
+        Str fullName = name.str();
+        if(mod != builtins) fullName = OBJ_NAME(mod) + "." + name.str();
         setattr(obj, __name__, PyStr(fullName));
         setattr(mod, name, obj);
         _all_types.push_back(obj);
         return obj;
     }
 
-    Type _new_type_object(Str name, Type base=0) {
+    Type _new_type_object(StrName name, Type base=0) {
         PyVar obj = pkpy::make_shared<PyObject, Py_<Type>>(tp_type, _all_types.size());
         setattr(obj, __base__, _t(base));
         _types[name] = obj;
@@ -329,14 +329,14 @@ public:
         return new_object(T::_type(this), T(std::forward<Args>(args)...));
     }
 
-    PyVar new_module(const Str& name) {
+    PyVar new_module(StrName name) {
         PyVar obj = new_object(tp_module, DUMMY_VAL);
-        setattr(obj, __name__, PyStr(name));
+        setattr(obj, __name__, PyStr(name.str()));
         _modules[name] = obj;
         return obj;
     }
 
-    PyVarOrNull getattr(const PyVar& obj, const Str& name, bool throw_err=true) {
+    PyVarOrNull getattr(const PyVar& obj, StrName name, bool throw_err=true) {
         pkpy::NameDict::iterator it;
         PyObject* cls;
 
@@ -377,7 +377,7 @@ public:
     }
 
     template<typename T>
-    inline void setattr(PyVar& obj, const Str& name, T&& value) {
+    inline void setattr(PyVar& obj, StrName name, T&& value) {
         if(obj.is_tagged()) TypeError("cannot set attribute");
         PyObject* p = obj.get();
         while(p->type == tp_super) p = static_cast<PyVar*>(p->value())->get();
@@ -484,12 +484,12 @@ public:
                 argStr += " (" + PyStr_AS_C(asRepr(co->consts[byte.arg])) + ")";
             }
             if(byte.op == OP_LOAD_NAME_REF || byte.op == OP_LOAD_NAME || byte.op == OP_RAISE || byte.op == OP_STORE_NAME){
-                argStr += " (" + co->names[byte.arg].first.escape(true) + ")";
+                argStr += " (" + co->names[byte.arg].first.str().escape(true) + ")";
             }
             if(byte.op == OP_FAST_INDEX || byte.op == OP_FAST_INDEX_REF){
                 auto& a = co->names[byte.arg & 0xFFFF];
                 auto& x = co->names[(byte.arg >> 16) & 0xFFFF];
-                argStr += " (" + a.first + '[' + x.first + "])";
+                argStr += " (" + a.first.str() + '[' + x.first.str() + "])";
             }
             ss << pad(argStr, 20);      // may overflow
             ss << co->blocks[byte.block].to_string();
@@ -503,7 +503,7 @@ public:
         names << "co_names: ";
         pkpy::List list;
         for(int i=0; i<co->names.size(); i++){
-            list.push_back(PyStr(co->names[i].first));
+            list.push_back(PyStr(co->names[i].first.str()));
         }
         names << PyStr_AS_C(asRepr(PyList(list)));
         ss << '\n' << consts.str() << '\n' << names.str() << '\n';
@@ -635,7 +635,7 @@ public:
         setattr(_t(tp_object), __base__, None);
         
         for (auto& [name, type] : _types) {
-            setattr(type, __name__, PyStr(name));
+            setattr(type, __name__, PyStr(name.str()));
         }
 
         std::vector<Str> pb_types = {"type", "object", "bool", "int", "float", "str", "list", "tuple", "range"};
@@ -668,7 +668,7 @@ public:
 
     /***** Error Reporter *****/
 private:
-    void _error(const Str& name, const Str& msg){
+    void _error(StrName name, const Str& msg){
         _error(pkpy::Exception(name, msg));
     }
 
@@ -694,10 +694,10 @@ public:
     void ZeroDivisionError(){ _error("ZeroDivisionError", "division by zero"); }
     void IndexError(const Str& msg){ _error("IndexError", msg); }
     void ValueError(const Str& msg){ _error("ValueError", msg); }
-    void NameError(const Str& name){ _error("NameError", "name " + name.escape(true) + " is not defined"); }
+    void NameError(StrName name){ _error("NameError", "name " + name.str().escape(true) + " is not defined"); }
 
-    void AttributeError(PyVar obj, const Str& name){
-        _error("AttributeError", "type " +  OBJ_NAME(_t(obj)).escape(true) + " has no attribute " + name.escape(true));
+    void AttributeError(PyVar obj, StrName name){
+        _error("AttributeError", "type " +  OBJ_NAME(_t(obj)).escape(true) + " has no attribute " + name.str().escape(true));
     }
 
     inline void check_type(const PyVar& obj, Type type){

Niektoré súbory nie sú zobrazené, pretože je v týchto rozdielových dátach zmenené mnoho súborov