blueloveTH %!s(int64=2) %!d(string=hai) anos
pai
achega
d1080aab1f
Modificáronse 3 ficheiros con 9 adicións e 6 borrados
  1. 6 3
      include/pocketpy/codeobject.h
  2. 1 1
      src/compiler.cpp
  3. 2 2
      src/vm.cpp

+ 6 - 3
include/pocketpy/codeobject.h

@@ -138,10 +138,13 @@ struct FuncDecl {
     Str docstring;              // docstring of this function
     Str docstring;              // docstring of this function
     bool is_simple;
     bool is_simple;
 
 
-    int keyword_to_index(StrName key) const{
-        for(const KwArg& item: kwargs) if(item.key == key) return item.index;
-        return -1;
+    NameDictInt kw_to_index;
+
+    void add_kwarg(int index, StrName key, PyObject* value){
+        kw_to_index.set(key, index);
+        kwargs.push_back({index, key, value});
     }
     }
+    
     void _gc_mark() const;
     void _gc_mark() const;
 };
 };
 
 

+ 1 - 1
src/compiler.cpp

@@ -1037,7 +1037,7 @@ __EAT_DOTS_END:
                     if(value == nullptr){
                     if(value == nullptr){
                         SyntaxError(Str("default argument must be a literal"));
                         SyntaxError(Str("default argument must be a literal"));
                     }
                     }
-                    decl->kwargs.push_back(FuncDecl::KwArg{index, name, value});
+                    decl->add_kwarg(index, name, value);
                 } break;
                 } break;
                 case 3:
                 case 3:
                     decl->starred_kwarg = index;
                     decl->starred_kwarg = index;

+ 2 - 2
src/vm.cpp

@@ -844,9 +844,9 @@ void VM::_prepare_py_call(PyObject** buffer, ArgsView args, ArgsView kwargs, con
 
 
     for(int j=0; j<kwargs.size(); j+=2){
     for(int j=0; j<kwargs.size(); j+=2){
         StrName key(CAST(int, kwargs[j]));
         StrName key(CAST(int, kwargs[j]));
-        int index = decl->keyword_to_index(key);
+        int index = decl->kw_to_index.try_get_likely_found(key);
         // if key is an explicit key, set as local variable
         // if key is an explicit key, set as local variable
-        if(index != -1){
+        if(index >= 0){
             buffer[index] = kwargs[j+1];
             buffer[index] = kwargs[j+1];
         }else{
         }else{
             // otherwise, set as **kwargs if possible
             // otherwise, set as **kwargs if possible