This commit is contained in:
blueloveTH 2023-02-10 01:58:06 +08:00
parent 8f63ac89a6
commit 90687f4488
4 changed files with 9 additions and 12 deletions

View File

@ -11,14 +11,13 @@ public:
this->current = r.start; this->current = r.start;
} }
bool hasNext(){ bool has_next(){
return r.step > 0 ? current < r.stop : current > r.stop; return r.step > 0 ? current < r.stop : current > r.stop;
} }
PyVar next(){ PyVar next(){
PyVar val = vm->PyInt(current);
current += r.step; current += r.step;
return val; return vm->PyInt(current-r.step);
} }
}; };
@ -28,7 +27,7 @@ class ArrayIter : public BaseIter {
const T* p; const T* p;
public: public:
ArrayIter(VM* vm, PyVar _ref) : BaseIter(vm, _ref) { p = &OBJ_GET(T, _ref);} ArrayIter(VM* vm, PyVar _ref) : BaseIter(vm, _ref) { p = &OBJ_GET(T, _ref);}
bool hasNext(){ return index < p->size(); } bool has_next(){ return index < p->size(); }
PyVar next(){ return p->operator[](index++); } PyVar next(){ return p->operator[](index++); }
}; };
@ -40,6 +39,6 @@ public:
str = OBJ_GET(_Str, _ref); str = OBJ_GET(_Str, _ref);
} }
bool hasNext(){ return index < str.u8_length(); } bool has_next(){ return index < str.u8_length(); }
PyVar next() { return vm->PyStr(str.u8_getitem(index++)); } PyVar next() { return vm->PyStr(str.u8_getitem(index++)); }
}; };

View File

@ -65,7 +65,7 @@ protected:
PyVar _ref; // keep a reference to the object so it will not be deleted while iterating PyVar _ref; // keep a reference to the object so it will not be deleted while iterating
public: public:
virtual PyVar next() = 0; virtual PyVar next() = 0;
virtual bool hasNext() = 0; virtual bool has_next() = 0;
PyVarRef var; PyVarRef var;
BaseIter(VM* vm, PyVar _ref) : vm(vm), _ref(_ref) {} BaseIter(VM* vm, PyVar _ref) : vm(vm), _ref(_ref) {}
virtual ~BaseIter() = default; virtual ~BaseIter() = default;

View File

@ -38,7 +38,7 @@ 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 emhash8::HashMap<std::string_view, TokenIndex> __KW_MAP = [](){ const emhash8::HashMap<std::string_view, TokenIndex> kTokenKwMap = [](){
emhash8::HashMap<std::string_view, TokenIndex> map; emhash8::HashMap<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(__KW_MAP.count(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;
@ -245,7 +245,7 @@ struct Parser {
return 0; return 0;
} }
} }
set_next_token(__KW_MAP.at(name)); set_next_token(kTokenKwMap.at(name));
} else { } else {
set_next_token(TK("@id")); set_next_token(TK("@id"));
} }

View File

@ -20,7 +20,6 @@
class VM { class VM {
// std::vector<PyVar> _small_integers; // [-5, 256]
std::stack< std::unique_ptr<Frame> > callstack; std::stack< std::unique_ptr<Frame> > callstack;
PyVar _py_op_call; PyVar _py_op_call;
@ -259,7 +258,7 @@ class VM {
{ {
// top() must be PyIter, so no need to try_deref() // top() must be PyIter, so no need to try_deref()
auto& it = PyIter_AS_C(frame->top()); auto& it = PyIter_AS_C(frame->top());
if(it->hasNext()){ if(it->has_next()){
PyRef_AS_C(it->var)->set(this, frame, it->next()); PyRef_AS_C(it->var)->set(this, frame, it->next());
}else{ }else{
int blockEnd = frame->co->blocks[byte.block].end; int blockEnd = frame->co->blocks[byte.block].end;
@ -365,7 +364,6 @@ public:
} }
init_builtin_types(); init_builtin_types();
// for(i64 i=-5; i<=256; i++) _small_integers.push_back(new_object(_tp_int, i));
} }
PyVar asStr(const PyVar& obj){ PyVar asStr(const PyVar& obj){