iter.cpp 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #include "pocketpy/iter.h"
  2. namespace pkpy{
  3. void RangeIter::_register(VM* vm, PyObject* mod, PyObject* type){
  4. vm->_all_types[PK_OBJ_GET(Type, type)].subclass_enabled = false;
  5. vm->bind_notimplemented_constructor<RangeIter>(type);
  6. vm->bind__iter__(PK_OBJ_GET(Type, type), [](VM* vm, PyObject* obj){ return obj; });
  7. vm->bind__next__(PK_OBJ_GET(Type, type), [](VM* vm, PyObject* obj){
  8. RangeIter& self = _CAST(RangeIter&, obj);
  9. bool has_next = self.r.step > 0 ? self.current < self.r.stop : self.current > self.r.stop;
  10. if(!has_next) return vm->StopIteration;
  11. self.current += self.r.step;
  12. return VAR(self.current - self.r.step);
  13. });
  14. }
  15. void ArrayIter::_register(VM* vm, PyObject* mod, PyObject* type){
  16. vm->_all_types[PK_OBJ_GET(Type, type)].subclass_enabled = false;
  17. vm->bind_notimplemented_constructor<ArrayIter>(type);
  18. vm->bind__iter__(PK_OBJ_GET(Type, type), [](VM* vm, PyObject* obj){ return obj; });
  19. vm->bind__next__(PK_OBJ_GET(Type, type), [](VM* vm, PyObject* obj){
  20. ArrayIter& self = _CAST(ArrayIter&, obj);
  21. if(self.current == self.end) return vm->StopIteration;
  22. return *self.current++;
  23. });
  24. }
  25. void StringIter::_register(VM* vm, PyObject* mod, PyObject* type){
  26. vm->_all_types[PK_OBJ_GET(Type, type)].subclass_enabled = false;
  27. vm->bind_notimplemented_constructor<StringIter>(type);
  28. vm->bind__iter__(PK_OBJ_GET(Type, type), [](VM* vm, PyObject* obj){ return obj; });
  29. vm->bind__next__(PK_OBJ_GET(Type, type), [](VM* vm, PyObject* obj){
  30. StringIter& self = _CAST(StringIter&, obj);
  31. // TODO: optimize this... operator[] is of O(n) complexity
  32. if(self.index == self.str->u8_length()) return vm->StopIteration;
  33. return VAR(self.str->u8_getitem(self.index++));
  34. });
  35. }
  36. PyObject* Generator::next(VM* vm){
  37. if(state == 2) return vm->StopIteration;
  38. // reset frame._sp_base
  39. frame._sp_base = frame._s->_sp;
  40. frame._locals.a = frame._s->_sp;
  41. // restore the context
  42. for(PyObject* obj: s_backup) frame._s->push(obj);
  43. s_backup.clear();
  44. vm->callstack.push(std::move(frame));
  45. PyObject* ret = vm->_run_top_frame();
  46. if(ret == PY_OP_YIELD){
  47. // backup the context
  48. frame = std::move(vm->callstack.top());
  49. ret = frame._s->popx();
  50. for(PyObject* obj: frame.stack_view()) s_backup.push_back(obj);
  51. vm->_pop_frame();
  52. state = 1;
  53. if(ret == vm->StopIteration) state = 2;
  54. return ret;
  55. }else{
  56. state = 2;
  57. return vm->StopIteration;
  58. }
  59. }
  60. void Generator::_register(VM* vm, PyObject* mod, PyObject* type){
  61. vm->_all_types[PK_OBJ_GET(Type, type)].subclass_enabled = false;
  62. vm->bind_notimplemented_constructor<Generator>(type);
  63. vm->bind__iter__(PK_OBJ_GET(Type, type), [](VM* vm, PyObject* obj){ return obj; });
  64. vm->bind__next__(PK_OBJ_GET(Type, type), [](VM* vm, PyObject* obj){
  65. Generator& self = _CAST(Generator&, obj);
  66. return self.next(vm);
  67. });
  68. }
  69. PyObject* VM::_py_generator(Frame&& frame, ArgsView buffer){
  70. return VAR_T(Generator, std::move(frame), buffer);
  71. }
  72. } // namespace pkpy