This commit is contained in:
blueloveTH 2023-12-19 14:44:09 +08:00
parent 9cc0c4a504
commit 63b0a592bd
2 changed files with 6 additions and 24 deletions

View File

@ -130,33 +130,15 @@ struct FuncDecl {
CodeObject_ code; // code object of this function
std::vector<int> args; // indices in co->varnames
std::vector<KwArg> kwargs; // indices in co->varnames
int starred_arg; // index in co->varnames, -1 if no *arg
int starred_kwarg; // index in co->varnames, -1 if no **kwarg
bool nested; // whether this function is nested
static const int MAX_K2I_CACHE = 16;
int _keyword_to_index[MAX_K2I_CACHE]; // map from `kwargs[i].key` to `kwargs[i].index`
int starred_arg = -1; // index in co->varnames, -1 if no *arg
int starred_kwarg = -1; // index in co->varnames, -1 if no **kwarg
bool nested = false; // whether this function is nested
Str signature; // signature of this function
Str docstring; // docstring of this function
bool is_simple;
FuncDecl(){
starred_arg = -1;
starred_kwarg = -1;
nested = false;
for(int i=0; i<MAX_K2I_CACHE; i++) _keyword_to_index[i] = -1;
is_simple = false;
}
void add_kwarg_item(int index, StrName key, PyObject* value){
kwargs.push_back(KwArg{index, key, value});
if(key.index < MAX_K2I_CACHE) _keyword_to_index[key.index] = index;
}
int keyword_to_index(StrName key) const{
if(key.index < MAX_K2I_CACHE) return _keyword_to_index[key.index];
for(const KwArg& item: kwargs) if(item.key == key) return item.index;
return -1;
}

View File

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