Update vm.h

up

up

Update common.h
This commit is contained in:
blueloveTH 2023-02-21 19:41:05 +08:00
parent 57fd320ae2
commit 8eaa80da2e
11 changed files with 84 additions and 97 deletions

View File

@ -1,6 +1,6 @@
g++ -o pocketpy src/main.cpp --std=c++17 -pg -O2 -fno-rtti g++ -o pocketpy src/main.cpp --std=c++17 -pg -O2 -fno-rtti
./pocketpy benchmarks/loop_1.py ./pocketpy benchmarks/fib.py
gprof pocketpy gmon.out > gprof.txt gprof pocketpy gmon.out > gprof.txt

View File

@ -223,9 +223,9 @@ PyVar VM::run_frame(Frame* frame){
case OP_SAFE_JUMP_ABSOLUTE: frame->jump_abs_safe(byte.arg); continue; case OP_SAFE_JUMP_ABSOLUTE: frame->jump_abs_safe(byte.arg); continue;
case OP_GOTO: { case OP_GOTO: {
StrName label = frame->co->names[byte.arg].first; StrName label = frame->co->names[byte.arg].first;
int* target = frame->co->labels.try_get(label); auto it = frame->co->labels.find(label);
if(target == nullptr) _error("KeyError", "label " + label.str().escape(true) + " not found"); if(it == frame->co->labels.end()) _error("KeyError", "label " + label.str().escape(true) + " not found");
frame->jump_abs_safe(*target); frame->jump_abs_safe(it->second);
} continue; } continue;
case OP_GET_ITER: { case OP_GET_ITER: {
PyVar obj = frame->pop_value(this); PyVar obj = frame->pop_value(this);

View File

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

View File

@ -28,10 +28,11 @@
// namespace fs = std::filesystem; // namespace fs = std::filesystem;
#define EMH_EXT 1 #define EMH_EXT 1
#include "hash_table8.hpp" #define EMH_FIND_HIT 1
#include "hash_table5.hpp"
namespace pkpy { namespace pkpy {
template<typename... Args> template<typename... Args>
using HashMap = emhash8::HashMap<Args...>; using HashMap = emhash5::HashMap<Args...>;
} }
#ifdef POCKETPY_H #ifdef POCKETPY_H

View File

@ -23,7 +23,7 @@ class Compiler {
int lexing_count = 0; int lexing_count = 0;
bool used = false; bool used = false;
VM* vm; VM* vm;
pkpy::HashMap<TokenIndex, GrammarRule> rules; std::map<TokenIndex, GrammarRule> rules;
CodeObject_ co() const{ return codes.top(); } CodeObject_ co() const{ return codes.top(); }
CompileMode mode() const{ return parser->src->mode; } CompileMode mode() const{ return parser->src->mode; }

View File

@ -115,4 +115,35 @@ namespace pkpy{
static_assert(sizeof(i64) == sizeof(pkpy::shared_ptr<PyObject>)); static_assert(sizeof(i64) == sizeof(pkpy::shared_ptr<PyObject>));
static_assert(sizeof(f64) == sizeof(pkpy::shared_ptr<PyObject>)); static_assert(sizeof(f64) == sizeof(pkpy::shared_ptr<PyObject>));
static_assert(std::numeric_limits<float>::is_iec559); static_assert(std::numeric_limits<float>::is_iec559);
static_assert(std::numeric_limits<double>::is_iec559); static_assert(std::numeric_limits<double>::is_iec559);
template<typename T, int __Bucket, int __BucketSize=32>
struct SmallArrayPool {
std::deque<T*> buckets[__Bucket+1];
T* alloc(int n){
if(n == 0) return nullptr;
if(n > __Bucket || buckets[n].empty()){
return new T[n];
}else{
T* p = buckets[n].back();
buckets[n].pop_back();
return p;
}
}
void dealloc(T* p, int n){
if(n == 0) return;
if(n > __Bucket){
delete[] p;
}else{
buckets[n].push_back(p);
}
}
~SmallArrayPool(){
for(int i=1; i<=__Bucket; i++){
for(auto p: buckets[i]) delete[] p;
}
}
};

View File

@ -23,10 +23,10 @@ struct NativeFunc {
struct Function { struct Function {
Str name; Str name;
CodeObject_ code; CodeObject_ code;
std::vector<Str> args; std::vector<StrName> args;
Str starred_arg; // empty if no *arg StrName starred_arg; // empty if no *arg
pkpy::NameDict kwargs; // empty if no k=v pkpy::NameDict kwargs; // empty if no k=v
std::vector<Str> kwargs_order; std::vector<StrName> kwargs_order;
// runtime settings // runtime settings
PyVar _module; PyVar _module;
@ -35,7 +35,7 @@ struct Function {
bool has_name(const Str& val) const { bool has_name(const Str& val) const {
bool _0 = std::find(args.begin(), args.end(), val) != args.end(); bool _0 = std::find(args.begin(), args.end(), val) != args.end();
bool _1 = starred_arg == val; bool _1 = starred_arg == val;
bool _2 = kwargs.find(val) != kwargs.end(); bool _2 = kwargs.contains(val);
return _0 || _1 || _2; return _0 || _1 || _2;
} }
}; };

View File

@ -38,8 +38,8 @@ constexpr TokenIndex TK(const char* const token) {
const TokenIndex kTokenKwBegin = TK("class"); const TokenIndex kTokenKwBegin = TK("class");
const TokenIndex kTokenKwEnd = TK("raise"); const TokenIndex kTokenKwEnd = TK("raise");
const pkpy::HashMap<std::string_view, TokenIndex> kTokenKwMap = [](){ const std::map<std::string_view, TokenIndex> kTokenKwMap = [](){
pkpy::HashMap<std::string_view, TokenIndex> map; std::map<std::string_view, TokenIndex> map;
for(int k=kTokenKwBegin; k<=kTokenKwEnd; k++) map[kTokens[k]] = k; for(int k=kTokenKwBegin; k<=kTokenKwEnd; k++) map[kTokens[k]] = k;
return map; return map;
}(); }();
@ -231,7 +231,7 @@ struct Parser {
return 0; return 0;
} }
if(kTokenKwMap.contains(name)){ if(kTokenKwMap.count(name)){
if(name == "not"){ if(name == "not"){
if(strncmp(curr_char, " in", 3) == 0){ if(strncmp(curr_char, " in", 3) == 0){
curr_char += 3; curr_char += 3;

View File

@ -33,42 +33,21 @@ public:
using std::vector<PyVar>::vector; using std::vector<PyVar>::vector;
}; };
typedef pkpy::HashMap<StrName, PyVar> NameDict;
} }
namespace pkpy { namespace pkpy {
const int kMaxPoolSize = 10; typedef HashMap<StrName, PyVar> NameDict;
static THREAD_LOCAL std::vector<PyVar*>* _args_pool = new std::vector<PyVar*>[kMaxPoolSize];
class Args { class Args {
static THREAD_LOCAL SmallArrayPool<PyVar, 10> _pool;
PyVar* _args; PyVar* _args;
int _size; int _size;
void _alloc(int n){ inline void _alloc(int n){
if(n == 0){ this->_args = _pool.alloc(n);
this->_args = nullptr; this->_size = n;
this->_size = 0;
return;
}
if(n >= kMaxPoolSize || _args_pool[n].empty()){
this->_args = new PyVar[n];
this->_size = n;
}else{
this->_args = _args_pool[n].back();
this->_size = n;
_args_pool[n].pop_back();
}
}
void _dealloc(){
if(_size == 0 || _args == nullptr) return;
if(_size >= kMaxPoolSize || _args_pool[_size].size() > 32){
delete[] _args;
}else{
for(int i = 0; i < _size; i++) _args[i].reset();
_args_pool[_size].push_back(_args);
}
} }
public: public:
@ -103,7 +82,7 @@ namespace pkpy {
const PyVar& operator[](int i) const { return _args[i]; } const PyVar& operator[](int i) const { return _args[i]; }
Args& operator=(Args&& other) noexcept { Args& operator=(Args&& other) noexcept {
_dealloc(); _pool.dealloc(_args, _size);
this->_args = other._args; this->_args = other._args;
this->_size = other._size; this->_size = other._size;
other._args = nullptr; other._args = nullptr;
@ -130,14 +109,10 @@ namespace pkpy {
memcpy((void*)(_args+1), (void*)old_args, sizeof(PyVar)*old_size); memcpy((void*)(_args+1), (void*)old_args, sizeof(PyVar)*old_size);
memset((void*)old_args, 0, sizeof(PyVar)*old_size); memset((void*)old_args, 0, sizeof(PyVar)*old_size);
if(old_size >= kMaxPoolSize || _args_pool[old_size].size() > 32){ _pool.dealloc(old_args, old_size);
delete[] old_args;
}else{
_args_pool[old_size].push_back(old_args);
}
} }
~Args(){ _dealloc(); } ~Args(){ _pool.dealloc(_args, _size); }
}; };
static const Args _zero(0); static const Args _zero(0);
@ -159,4 +134,8 @@ namespace pkpy {
} }
typedef Args Tuple; typedef Args Tuple;
}
// declare static members
THREAD_LOCAL SmallArrayPool<PyVar, 10> Args::_pool;
// THREAD_LOCAL SmallArrayPool<NameDictNode, 1> NameDict::_pool;
} // namespace pkpy

View File

@ -6,8 +6,6 @@ typedef std::stringstream StrStream;
class Str : public std::string { class Str : public std::string {
mutable std::vector<uint16_t>* _u8_index = nullptr; mutable std::vector<uint16_t>* _u8_index = nullptr;
mutable bool hash_initialized = false;
mutable size_t _hash;
void utf8_lazy_init() const{ void utf8_lazy_init() const{
if(_u8_index != nullptr) return; if(_u8_index != nullptr) return;
@ -28,27 +26,11 @@ public:
if(s._u8_index != nullptr){ if(s._u8_index != nullptr){
_u8_index = new std::vector<uint16_t>(*s._u8_index); _u8_index = new std::vector<uint16_t>(*s._u8_index);
} }
if(s.hash_initialized){
_hash = s._hash;
hash_initialized = true;
}
} }
Str(Str&& s) : std::string(std::move(s)) { Str(Str&& s) : std::string(std::move(s)) {
delete _u8_index; delete _u8_index;
_u8_index = s._u8_index; _u8_index = s._u8_index;
s._u8_index = nullptr; s._u8_index = nullptr;
if(s.hash_initialized){
_hash = s._hash;
hash_initialized = true;
}
}
inline size_t hash() const{
if(!hash_initialized){
_hash = std::hash<std::string>()(*this);
hash_initialized = true;
}
return _hash;
} }
i64 _to_u8_index(i64 index) const{ i64 _to_u8_index(i64 index) const{
@ -83,6 +65,10 @@ public:
return Str(copy); return Str(copy);
} }
size_t hash() const {
return std::hash<std::string>()(*this);
}
Str escape(bool single_quote) const { Str escape(bool single_quote) const {
StrStream ss; StrStream ss;
ss << (single_quote ? '\'' : '"'); ss << (single_quote ? '\'' : '"');
@ -120,8 +106,6 @@ public:
if(s._u8_index != nullptr){ if(s._u8_index != nullptr){
_u8_index = new std::vector<uint16_t>(*s._u8_index); _u8_index = new std::vector<uint16_t>(*s._u8_index);
} }
this->hash_initialized = s.hash_initialized;
this->_hash = s._hash;
return *this; return *this;
} }
@ -130,23 +114,12 @@ public:
delete _u8_index; delete _u8_index;
this->_u8_index = s._u8_index; this->_u8_index = s._u8_index;
s._u8_index = nullptr; s._u8_index = nullptr;
this->hash_initialized = s.hash_initialized;
this->_hash = s._hash;
return *this; return *this;
} }
~Str(){ delete _u8_index;} ~Str(){ delete _u8_index;}
}; };
namespace std {
template<>
struct hash<Str> {
inline std::size_t operator()(const Str& s) const {
return s.hash();
}
};
}
const uint32_t kLoRangeA[] = {170,186,443,448,660,1488,1519,1568,1601,1646,1649,1749,1774,1786,1791,1808,1810,1869,1969,1994,2048,2112,2144,2208,2230,2308,2365,2384,2392,2418,2437,2447,2451,2474,2482,2486,2493,2510,2524,2527,2544,2556,2565,2575,2579,2602,2610,2613,2616,2649,2654,2674,2693,2703,2707,2730,2738,2741,2749,2768,2784,2809,2821,2831,2835,2858,2866,2869,2877,2908,2911,2929,2947,2949,2958,2962,2969,2972,2974,2979,2984,2990,3024,3077,3086,3090,3114,3133,3160,3168,3200,3205,3214,3218,3242,3253,3261,3294,3296,3313,3333,3342,3346,3389,3406,3412,3423,3450,3461,3482,3507,3517,3520,3585,3634,3648,3713,3716,3718,3724,3749,3751,3762,3773,3776,3804,3840,3904,3913,3976,4096,4159,4176,4186,4193,4197,4206,4213,4238,4352,4682,4688,4696,4698,4704,4746,4752,4786,4792,4800,4802,4808,4824,4882,4888,4992,5121,5743,5761,5792,5873,5888,5902,5920,5952,5984,5998,6016,6108,6176,6212,6272,6279,6314,6320,6400,6480,6512,6528,6576,6656,6688,6917,6981,7043,7086,7098,7168,7245,7258,7401,7406,7413,7418,8501,11568,11648,11680,11688,11696,11704,11712,11720,11728,11736,12294,12348,12353,12447,12449,12543,12549,12593,12704,12784,13312,19968,40960,40982,42192,42240,42512,42538,42606,42656,42895,42999,43003,43011,43015,43020,43072,43138,43250,43259,43261,43274,43312,43360,43396,43488,43495,43514,43520,43584,43588,43616,43633,43642,43646,43697,43701,43705,43712,43714,43739,43744,43762,43777,43785,43793,43808,43816,43968,44032,55216,55243,63744,64112,64285,64287,64298,64312,64318,64320,64323,64326,64467,64848,64914,65008,65136,65142,65382,65393,65440,65474,65482,65490,65498,65536,65549,65576,65596,65599,65616,65664,66176,66208,66304,66349,66370,66384,66432,66464,66504,66640,66816,66864,67072,67392,67424,67584,67592,67594,67639,67644,67647,67680,67712,67808,67828,67840,67872,67968,68030,68096,68112,68117,68121,68192,68224,68288,68297,68352,68416,68448,68480,68608,68864,69376,69415,69424,69600,69635,69763,69840,69891,69956,69968,70006,70019,70081,70106,70108,70144,70163,70272,70280,70282,70287,70303,70320,70405,70415,70419,70442,70450,70453,70461,70480,70493,70656,70727,70751,70784,70852,70855,71040,71128,71168,71236,71296,71352,71424,71680,71935,72096,72106,72161,72163,72192,72203,72250,72272,72284,72349,72384,72704,72714,72768,72818,72960,72968,72971,73030,73056,73063,73066,73112,73440,73728,74880,77824,82944,92160,92736,92880,92928,93027,93053,93952,94032,94208,100352,110592,110928,110948,110960,113664,113776,113792,113808,123136,123214,123584,124928,126464,126469,126497,126500,126503,126505,126516,126521,126523,126530,126535,126537,126539,126541,126545,126548,126551,126553,126555,126557,126559,126561,126564,126567,126572,126580,126585,126590,126592,126603,126625,126629,126635,131072,173824,177984,178208,183984,194560}; const uint32_t kLoRangeA[] = {170,186,443,448,660,1488,1519,1568,1601,1646,1649,1749,1774,1786,1791,1808,1810,1869,1969,1994,2048,2112,2144,2208,2230,2308,2365,2384,2392,2418,2437,2447,2451,2474,2482,2486,2493,2510,2524,2527,2544,2556,2565,2575,2579,2602,2610,2613,2616,2649,2654,2674,2693,2703,2707,2730,2738,2741,2749,2768,2784,2809,2821,2831,2835,2858,2866,2869,2877,2908,2911,2929,2947,2949,2958,2962,2969,2972,2974,2979,2984,2990,3024,3077,3086,3090,3114,3133,3160,3168,3200,3205,3214,3218,3242,3253,3261,3294,3296,3313,3333,3342,3346,3389,3406,3412,3423,3450,3461,3482,3507,3517,3520,3585,3634,3648,3713,3716,3718,3724,3749,3751,3762,3773,3776,3804,3840,3904,3913,3976,4096,4159,4176,4186,4193,4197,4206,4213,4238,4352,4682,4688,4696,4698,4704,4746,4752,4786,4792,4800,4802,4808,4824,4882,4888,4992,5121,5743,5761,5792,5873,5888,5902,5920,5952,5984,5998,6016,6108,6176,6212,6272,6279,6314,6320,6400,6480,6512,6528,6576,6656,6688,6917,6981,7043,7086,7098,7168,7245,7258,7401,7406,7413,7418,8501,11568,11648,11680,11688,11696,11704,11712,11720,11728,11736,12294,12348,12353,12447,12449,12543,12549,12593,12704,12784,13312,19968,40960,40982,42192,42240,42512,42538,42606,42656,42895,42999,43003,43011,43015,43020,43072,43138,43250,43259,43261,43274,43312,43360,43396,43488,43495,43514,43520,43584,43588,43616,43633,43642,43646,43697,43701,43705,43712,43714,43739,43744,43762,43777,43785,43793,43808,43816,43968,44032,55216,55243,63744,64112,64285,64287,64298,64312,64318,64320,64323,64326,64467,64848,64914,65008,65136,65142,65382,65393,65440,65474,65482,65490,65498,65536,65549,65576,65596,65599,65616,65664,66176,66208,66304,66349,66370,66384,66432,66464,66504,66640,66816,66864,67072,67392,67424,67584,67592,67594,67639,67644,67647,67680,67712,67808,67828,67840,67872,67968,68030,68096,68112,68117,68121,68192,68224,68288,68297,68352,68416,68448,68480,68608,68864,69376,69415,69424,69600,69635,69763,69840,69891,69956,69968,70006,70019,70081,70106,70108,70144,70163,70272,70280,70282,70287,70303,70320,70405,70415,70419,70442,70450,70453,70461,70480,70493,70656,70727,70751,70784,70852,70855,71040,71128,71168,71236,71296,71352,71424,71680,71935,72096,72106,72161,72163,72192,72203,72250,72272,72284,72349,72384,72704,72714,72768,72818,72960,72968,72971,73030,73056,73063,73066,73112,73440,73728,74880,77824,82944,92160,92736,92880,92928,93027,93053,93952,94032,94208,100352,110592,110928,110948,110960,113664,113776,113792,113808,123136,123214,123584,124928,126464,126469,126497,126500,126503,126505,126516,126521,126523,126530,126535,126537,126539,126541,126545,126548,126551,126553,126555,126557,126559,126561,126564,126567,126572,126580,126585,126590,126592,126603,126625,126629,126635,131072,173824,177984,178208,183984,194560};
const uint32_t kLoRangeB[] = {170,186,443,451,660,1514,1522,1599,1610,1647,1747,1749,1775,1788,1791,1808,1839,1957,1969,2026,2069,2136,2154,2228,2237,2361,2365,2384,2401,2432,2444,2448,2472,2480,2482,2489,2493,2510,2525,2529,2545,2556,2570,2576,2600,2608,2611,2614,2617,2652,2654,2676,2701,2705,2728,2736,2739,2745,2749,2768,2785,2809,2828,2832,2856,2864,2867,2873,2877,2909,2913,2929,2947,2954,2960,2965,2970,2972,2975,2980,2986,3001,3024,3084,3088,3112,3129,3133,3162,3169,3200,3212,3216,3240,3251,3257,3261,3294,3297,3314,3340,3344,3386,3389,3406,3414,3425,3455,3478,3505,3515,3517,3526,3632,3635,3653,3714,3716,3722,3747,3749,3760,3763,3773,3780,3807,3840,3911,3948,3980,4138,4159,4181,4189,4193,4198,4208,4225,4238,4680,4685,4694,4696,4701,4744,4749,4784,4789,4798,4800,4805,4822,4880,4885,4954,5007,5740,5759,5786,5866,5880,5900,5905,5937,5969,5996,6000,6067,6108,6210,6264,6276,6312,6314,6389,6430,6509,6516,6571,6601,6678,6740,6963,6987,7072,7087,7141,7203,7247,7287,7404,7411,7414,7418,8504,11623,11670,11686,11694,11702,11710,11718,11726,11734,11742,12294,12348,12438,12447,12538,12543,12591,12686,12730,12799,19893,40943,40980,42124,42231,42507,42527,42539,42606,42725,42895,42999,43009,43013,43018,43042,43123,43187,43255,43259,43262,43301,43334,43388,43442,43492,43503,43518,43560,43586,43595,43631,43638,43642,43695,43697,43702,43709,43712,43714,43740,43754,43762,43782,43790,43798,43814,43822,44002,55203,55238,55291,64109,64217,64285,64296,64310,64316,64318,64321,64324,64433,64829,64911,64967,65019,65140,65276,65391,65437,65470,65479,65487,65495,65500,65547,65574,65594,65597,65613,65629,65786,66204,66256,66335,66368,66377,66421,66461,66499,66511,66717,66855,66915,67382,67413,67431,67589,67592,67637,67640,67644,67669,67702,67742,67826,67829,67861,67897,68023,68031,68096,68115,68119,68149,68220,68252,68295,68324,68405,68437,68466,68497,68680,68899,69404,69415,69445,69622,69687,69807,69864,69926,69956,70002,70006,70066,70084,70106,70108,70161,70187,70278,70280,70285,70301,70312,70366,70412,70416,70440,70448,70451,70457,70461,70480,70497,70708,70730,70751,70831,70853,70855,71086,71131,71215,71236,71338,71352,71450,71723,71935,72103,72144,72161,72163,72192,72242,72250,72272,72329,72349,72440,72712,72750,72768,72847,72966,72969,73008,73030,73061,73064,73097,73112,73458,74649,75075,78894,83526,92728,92766,92909,92975,93047,93071,94026,94032,100343,101106,110878,110930,110951,111355,113770,113788,113800,113817,123180,123214,123627,125124,126467,126495,126498,126500,126503,126514,126519,126521,126523,126530,126535,126537,126539,126543,126546,126548,126551,126553,126555,126557,126559,126562,126564,126570,126578,126583,126588,126590,126601,126619,126627,126633,126651,173782,177972,178205,183969,191456,195101}; const uint32_t kLoRangeB[] = {170,186,443,451,660,1514,1522,1599,1610,1647,1747,1749,1775,1788,1791,1808,1839,1957,1969,2026,2069,2136,2154,2228,2237,2361,2365,2384,2401,2432,2444,2448,2472,2480,2482,2489,2493,2510,2525,2529,2545,2556,2570,2576,2600,2608,2611,2614,2617,2652,2654,2676,2701,2705,2728,2736,2739,2745,2749,2768,2785,2809,2828,2832,2856,2864,2867,2873,2877,2909,2913,2929,2947,2954,2960,2965,2970,2972,2975,2980,2986,3001,3024,3084,3088,3112,3129,3133,3162,3169,3200,3212,3216,3240,3251,3257,3261,3294,3297,3314,3340,3344,3386,3389,3406,3414,3425,3455,3478,3505,3515,3517,3526,3632,3635,3653,3714,3716,3722,3747,3749,3760,3763,3773,3780,3807,3840,3911,3948,3980,4138,4159,4181,4189,4193,4198,4208,4225,4238,4680,4685,4694,4696,4701,4744,4749,4784,4789,4798,4800,4805,4822,4880,4885,4954,5007,5740,5759,5786,5866,5880,5900,5905,5937,5969,5996,6000,6067,6108,6210,6264,6276,6312,6314,6389,6430,6509,6516,6571,6601,6678,6740,6963,6987,7072,7087,7141,7203,7247,7287,7404,7411,7414,7418,8504,11623,11670,11686,11694,11702,11710,11718,11726,11734,11742,12294,12348,12438,12447,12538,12543,12591,12686,12730,12799,19893,40943,40980,42124,42231,42507,42527,42539,42606,42725,42895,42999,43009,43013,43018,43042,43123,43187,43255,43259,43262,43301,43334,43388,43442,43492,43503,43518,43560,43586,43595,43631,43638,43642,43695,43697,43702,43709,43712,43714,43740,43754,43762,43782,43790,43798,43814,43822,44002,55203,55238,55291,64109,64217,64285,64296,64310,64316,64318,64321,64324,64433,64829,64911,64967,65019,65140,65276,65391,65437,65470,65479,65487,65495,65500,65547,65574,65594,65597,65613,65629,65786,66204,66256,66335,66368,66377,66421,66461,66499,66511,66717,66855,66915,67382,67413,67431,67589,67592,67637,67640,67644,67669,67702,67742,67826,67829,67861,67897,68023,68031,68096,68115,68119,68149,68220,68252,68295,68324,68405,68437,68466,68497,68680,68899,69404,69415,69445,69622,69687,69807,69864,69926,69956,70002,70006,70066,70084,70106,70108,70161,70187,70278,70280,70285,70301,70312,70366,70412,70416,70440,70448,70451,70457,70461,70480,70497,70708,70730,70751,70831,70853,70855,71086,71131,71215,71236,71338,71352,71450,71723,71935,72103,72144,72161,72163,72192,72242,72250,72272,72329,72349,72440,72712,72750,72768,72847,72966,72969,73008,73030,73061,73064,73097,73112,73458,74649,75075,78894,83526,92728,92766,92909,92975,93047,93071,94026,94032,100343,101106,110878,110930,110951,111355,113770,113788,113800,113817,123180,123214,123627,125124,126467,126495,126498,126500,126503,126514,126519,126521,126523,126530,126535,126537,126539,126543,126546,126548,126551,126553,126555,126557,126559,126562,126564,126570,126578,126583,126588,126590,126601,126619,126627,126633,126651,173782,177972,178205,183969,191456,195101};
@ -161,10 +134,13 @@ bool is_unicode_Lo_char(uint32_t c) {
struct StrName { struct StrName {
int index; int index;
StrName(): index(-1) {}
StrName(int index): index(index) {} StrName(int index): index(index) {}
StrName(const char* s): index(get(s).index) {} StrName(const char* s): index(get(s).index) {}
StrName(const Str& s): index(get(s).index) {} StrName(const Str& s): index(get(s).index) {}
inline const Str& str() const { return _r_interned[index]; } inline const Str& str() const { return _r_interned[index]; }
inline bool empty() const { return index == -1; }
inline void reset() { index = -1; }
inline bool operator==(const StrName& other) const noexcept { inline bool operator==(const StrName& other) const noexcept {
return this->index == other.index; return this->index == other.index;
@ -174,6 +150,14 @@ struct StrName {
return this->index != other.index; return this->index != other.index;
} }
inline bool operator<(const StrName& other) const noexcept {
return this->index < other.index;
}
inline bool operator>(const StrName& other) const noexcept {
return this->index > other.index;
}
static std::map<Str, int, std::less<>> _interned; static std::map<Str, int, std::less<>> _interned;
static std::vector<Str> _r_interned; static std::vector<Str> _r_interned;

View File

@ -155,26 +155,24 @@ public:
pkpy::NameDict& locals = *_locals; pkpy::NameDict& locals = *_locals;
int i = 0; int i = 0;
for(const auto& name : fn.args){ for(StrName name : fn.args){
if(i < args.size()){ if(i < args.size()){
locals.emplace(name, args[i++]); locals.emplace(name, args[i++]);
continue; continue;
} }
TypeError("missing positional argument '" + name + "'"); TypeError("missing positional argument " + name.str().escape(true));
} }
locals.insert(fn.kwargs.begin(), fn.kwargs.end()); locals.insert(fn.kwargs.begin(), fn.kwargs.end());
std::vector<Str> positional_overrided_keys;
if(!fn.starred_arg.empty()){ if(!fn.starred_arg.empty()){
pkpy::List vargs; // handle *args pkpy::List vargs; // handle *args
while(i < args.size()) vargs.push_back(args[i++]); while(i < args.size()) vargs.push_back(args[i++]);
locals.emplace(fn.starred_arg, PyTuple(std::move(vargs))); locals.emplace(fn.starred_arg, PyTuple(std::move(vargs)));
}else{ }else{
for(const auto& key : fn.kwargs_order){ for(StrName key : fn.kwargs_order){
if(i < args.size()){ if(i < args.size()){
locals[key] = args[i++]; locals[key] = args[i++];
positional_overrided_keys.push_back(key);
}else{ }else{
break; break;
} }
@ -188,12 +186,6 @@ public:
TypeError(key.escape(true) + " is an invalid keyword argument for " + fn.name + "()"); TypeError(key.escape(true) + " is an invalid keyword argument for " + fn.name + "()");
} }
const PyVar& val = kwargs[i+1]; const PyVar& val = kwargs[i+1];
if(!positional_overrided_keys.empty()){
auto it = std::find(positional_overrided_keys.begin(), positional_overrided_keys.end(), key);
if(it != positional_overrided_keys.end()){
TypeError("multiple values for argument '" + key + "'");
}
}
locals[key] = val; locals[key] = val;
} }
PyVar _module = fn._module != nullptr ? fn._module : top_frame()->_module; PyVar _module = fn._module != nullptr ? fn._module : top_frame()->_module;
@ -634,7 +626,7 @@ public:
setattr(_t(tp_type), __base__, _t(tp_object)); setattr(_t(tp_type), __base__, _t(tp_object));
setattr(_t(tp_object), __base__, None); setattr(_t(tp_object), __base__, None);
for(auto it = _types.begin(); it != _types.end(); it++){ for(auto it = _types.begin(); it != _types.end(); ++it){
setattr(it->second, __name__, PyStr(it->first.str())); setattr(it->second, __name__, PyStr(it->first.str()));
} }