mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-20 03:20:18 +00:00
impl StrName
This commit is contained in:
parent
9107e22e7f
commit
e5e6c49b29
16
src/ceval.h
16
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);
|
||||
|
@ -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++){
|
||||
|
@ -23,6 +23,7 @@
|
||||
#include <memory>
|
||||
#include <functional>
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
// #include <filesystem>
|
||||
// namespace fs = std::filesystem;
|
||||
|
||||
|
10
src/error.h
10
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();
|
||||
}
|
||||
};
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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) {}
|
||||
|
@ -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));
|
||||
});
|
||||
|
||||
|
@ -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;
|
||||
|
@ -33,7 +33,7 @@ public:
|
||||
using std::vector<PyVar>::vector;
|
||||
};
|
||||
|
||||
typedef emhash8::HashMap<Str, PyVar> NameDict;
|
||||
typedef emhash8::HashMap<StrName, PyVar> NameDict;
|
||||
|
||||
}
|
||||
|
||||
|
109
src/str.h
109
src/str.h
@ -147,39 +147,6 @@ namespace std {
|
||||
};
|
||||
}
|
||||
|
||||
const Str __class__ = Str("__class__");
|
||||
const Str __base__ = Str("__base__");
|
||||
const Str __new__ = Str("__new__");
|
||||
const Str __iter__ = Str("__iter__");
|
||||
const Str __str__ = Str("__str__");
|
||||
const Str __repr__ = Str("__repr__");
|
||||
const Str __getitem__ = Str("__getitem__");
|
||||
const Str __setitem__ = Str("__setitem__");
|
||||
const Str __delitem__ = Str("__delitem__");
|
||||
const Str __contains__ = Str("__contains__");
|
||||
const Str __init__ = Str("__init__");
|
||||
const Str __json__ = Str("__json__");
|
||||
const Str __name__ = Str("__name__");
|
||||
const Str __len__ = Str("__len__");
|
||||
|
||||
const Str m_append = Str("append");
|
||||
const Str m_eval = Str("eval");
|
||||
const Str m_self = Str("self");
|
||||
const Str __enter__ = Str("__enter__");
|
||||
const Str __exit__ = Str("__exit__");
|
||||
|
||||
const Str CMP_SPECIAL_METHODS[] = {
|
||||
"__lt__", "__le__", "__eq__", "__ne__", "__gt__", "__ge__"
|
||||
};
|
||||
|
||||
const Str BINARY_SPECIAL_METHODS[] = {
|
||||
"__add__", "__sub__", "__mul__", "__truediv__", "__floordiv__", "__mod__", "__pow__"
|
||||
};
|
||||
|
||||
const Str BITWISE_SPECIAL_METHODS[] = {
|
||||
"__lshift__", "__rshift__", "__and__", "__or__", "__xor__"
|
||||
};
|
||||
|
||||
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};
|
||||
|
||||
@ -190,3 +157,79 @@ bool is_unicode_Lo_char(uint32_t c) {
|
||||
if(index < 0) return false;
|
||||
return c >= kLoRangeA[index] && c <= kLoRangeB[index];
|
||||
}
|
||||
|
||||
|
||||
struct StrName {
|
||||
const int index;
|
||||
StrName(int index): index(index) {}
|
||||
StrName(const char* s): index(get(s).index) {}
|
||||
StrName(const Str& s): index(get(s).index) {}
|
||||
inline const Str& str() const { return _r_interned[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::vector<Str> _r_interned;
|
||||
|
||||
inline static StrName get(const Str& s){
|
||||
return get(s.c_str());
|
||||
}
|
||||
|
||||
static StrName get(const char* s){
|
||||
auto it = _interned.find(s);
|
||||
if(it != _interned.end()) return StrName(it->second);
|
||||
int index = _r_interned.size();
|
||||
_interned[s] = index;
|
||||
_r_interned.push_back(s);
|
||||
return StrName(index);
|
||||
}
|
||||
};
|
||||
|
||||
template<>
|
||||
struct std::hash<StrName> {
|
||||
inline size_t operator()(const StrName& name) const {
|
||||
return name.index;
|
||||
}
|
||||
};
|
||||
|
||||
const StrName __class__ = StrName::get("__class__");
|
||||
const StrName __base__ = StrName::get("__base__");
|
||||
const StrName __new__ = StrName::get("__new__");
|
||||
const StrName __iter__ = StrName::get("__iter__");
|
||||
const StrName __str__ = StrName::get("__str__");
|
||||
const StrName __repr__ = StrName::get("__repr__");
|
||||
const StrName __getitem__ = StrName::get("__getitem__");
|
||||
const StrName __setitem__ = StrName::get("__setitem__");
|
||||
const StrName __delitem__ = StrName::get("__delitem__");
|
||||
const StrName __contains__ = StrName::get("__contains__");
|
||||
const StrName __init__ = StrName::get("__init__");
|
||||
const StrName __json__ = StrName::get("__json__");
|
||||
const StrName __name__ = StrName::get("__name__");
|
||||
const StrName __len__ = StrName::get("__len__");
|
||||
|
||||
const StrName m_eval = StrName::get("eval");
|
||||
const StrName m_self = StrName::get("self");
|
||||
const StrName __enter__ = StrName::get("__enter__");
|
||||
const StrName __exit__ = StrName::get("__exit__");
|
||||
|
||||
const StrName CMP_SPECIAL_METHODS[] = {
|
||||
StrName::get("__lt__"), StrName::get("__le__"), StrName::get("__eq__"),
|
||||
StrName::get("__ne__"), StrName::get("__gt__"), StrName::get("__ge__")
|
||||
};
|
||||
|
||||
const StrName BINARY_SPECIAL_METHODS[] = {
|
||||
StrName::get("__add__"), StrName::get("__sub__"), StrName::get("__mul__"),
|
||||
StrName::get("__truediv__"), StrName::get("__floordiv__"),
|
||||
StrName::get("__mod__"), StrName::get("__pow__")
|
||||
};
|
||||
|
||||
const StrName BITWISE_SPECIAL_METHODS[] = {
|
||||
StrName::get("__lshift__"), StrName::get("__rshift__"),
|
||||
StrName::get("__and__"), StrName::get("__or__"), StrName::get("__xor__")
|
||||
};
|
46
src/vm.h
46
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){
|
||||
|
Loading…
x
Reference in New Issue
Block a user