WIP: added constructor

This commit is contained in:
S. Mahmudul Hasan 2023-10-14 17:13:04 -04:00
parent d17e1368da
commit fcd159ae7f
2 changed files with 73 additions and 29 deletions

View File

@ -14,27 +14,37 @@ namespace pkpy
struct PyDeque struct PyDeque
{ {
PY_CLASS(PyDeque, mycollections, deque); PY_CLASS(PyDeque, mycollections, deque);
PyDeque(VM *vm, PyObject *iterable, PyObject* maxlen);
std::deque<PyObject*>dequeItems; // PyDeque members
std::deque<PyObject *> dequeItems;
int maxlen=-1;
bool bounded=false;
// PyDeque methods: add, remove, insert, etc.
void appendLeft(PyObject *item); void appendLeft(PyObject *item);
void append(PyObject *item); void append(PyObject *item);
PyObject *popLeft(); PyObject *popLeft();
PyObject *pop(); PyObject *pop();
bool insert(int index, PyObject *item); bool insert(int index, PyObject *item);
bool remove(VM *vm, PyObject *item); bool remove(VM *vm, PyObject *item);
void rotate(int n); void rotate(int n);
std::stringstream getRepr(VM *vm);
void reverse(); void reverse();
int findIndex(VM *vm, PyObject *obj, int startPos, int endPos); // vm is needed for the py_equals
int count(VM *vm, PyObject *obj); // vm is needed for the py_equals
void clear(); void clear();
PyDeque(){}
static void _register(VM *vm, PyObject *mod, PyObject *type);
int count(VM *vm, PyObject *obj); // vm is needed for the py_equals
int findIndex(VM *vm, PyObject *obj, int startPos, int endPos); // vm is needed for the py_equals
std::stringstream getRepr(VM *vm);
// Special methods
static void _register(VM *vm, PyObject *mod, PyObject *type);
void _gc_mark() const; // needed for container types void _gc_mark() const; // needed for container types
}; };

View File

@ -4,19 +4,13 @@ namespace pkpy
{ {
void PyDeque::_register(VM *vm, PyObject *mod, PyObject *type) void PyDeque::_register(VM *vm, PyObject *mod, PyObject *type)
{ {
vm->bind_default_constructor<PyDeque>(type); vm->bind(type, "__new__(cls, iterable=None, maxlen=None)",
vm->bind(type, "__init__(self) -> None",
[](VM *vm, ArgsView args) [](VM *vm, ArgsView args)
{ {
PyDeque &self = _CAST(PyDeque &, args[0]); Type cls_t = PK_OBJ_GET(Type, args[0]);
PyObject *maxlen = args[1]; PyObject *iterable = args[1];
if (maxlen != vm->None) PyObject *maxlen = args[2];
{ return vm->heap.gcnew<PyDeque>(cls_t, vm, iterable, maxlen);
// printf("TODO: implement deque.__init__(maxlen) -> None: %d\n", CAST(int, maxlen));
}
return vm->None;
}); });
vm->bind(type, "__len__(self) -> int", vm->bind(type, "__len__(self) -> int",
@ -55,9 +49,9 @@ namespace pkpy
vm->bind(type, "copy(self) -> deque", vm->bind(type, "copy(self) -> deque",
[](VM *vm, ArgsView args) [](VM *vm, ArgsView args)
{ {
// TODO: STILL MEMORY LEAKING?? // TODO: FIX after implementing the __iter__ method
PyDeque &self = _CAST(PyDeque &, args[0]); PyDeque &self = _CAST(PyDeque &, args[0]);
PyDeque *newDeque = new PyDeque(); PyDeque *newDeque = new PyDeque(vm, vm->None, vm->None);
for (auto it = self.dequeItems.begin(); it != self.dequeItems.end(); ++it) for (auto it = self.dequeItems.begin(); it != self.dequeItems.end(); ++it)
{ {
newDeque->append(*it); newDeque->append(*it);
@ -77,8 +71,8 @@ namespace pkpy
vm->bind(type, "extend(self, iterable) -> None", vm->bind(type, "extend(self, iterable) -> None",
[](VM *vm, ArgsView args) [](VM *vm, ArgsView args)
{ {
auto _lock = vm->heap.gc_scope_lock();
PyDeque &self = _CAST(PyDeque &, args[0]); PyDeque &self = _CAST(PyDeque &, args[0]);
PyObject *it = vm->py_iter(args[1]); // strong ref PyObject *it = vm->py_iter(args[1]); // strong ref
PyObject *obj = vm->py_next(it); PyObject *obj = vm->py_next(it);
while (obj != vm->StopIteration) while (obj != vm->StopIteration)
@ -92,8 +86,8 @@ namespace pkpy
vm->bind(type, "extendleft(self, iterable) -> None", vm->bind(type, "extendleft(self, iterable) -> None",
[](VM *vm, ArgsView args) [](VM *vm, ArgsView args)
{ {
auto _lock = vm->heap.gc_scope_lock();
PyDeque &self = _CAST(PyDeque &, args[0]); PyDeque &self = _CAST(PyDeque &, args[0]);
PyObject *it = vm->py_iter(args[1]); // strong ref PyObject *it = vm->py_iter(args[1]); // strong ref
PyObject *obj = vm->py_next(it); PyObject *obj = vm->py_next(it);
while (obj != vm->StopIteration) while (obj != vm->StopIteration)
@ -185,11 +179,44 @@ namespace pkpy
return vm->None; return vm->None;
}); });
// vm->bind(type,"maxlen", // getter and setter of property `maxlen`
// [](VM *vm, ArgsView args) vm->bind_property(
// { type, "maxlen: int",
// return VAR(-1); [](VM *vm, ArgsView args)
// }); {
PyDeque &self = _CAST(PyDeque &, args[0]);
if (self.bounded)
return VAR(self.maxlen);
else
return vm->None;
},
[](VM *vm, ArgsView args)
{
vm->AttributeError("attribute 'maxlen' of 'collections.deque' objects is not writable");
return vm->None;
});
}
PyDeque::PyDeque(VM *vm, PyObject* iterable, PyObject* maxlen)
{
if(maxlen!=vm->None){
this->maxlen = CAST(int, maxlen);
this->bounded = true;
}else{
this->bounded = false;
this->maxlen = -1;
}
if(iterable!=vm->None){
auto _lock = vm->heap.gc_scope_lock();
PyObject *it = vm->py_iter(iterable); // strong ref
PyObject *obj = vm->py_next(it);
while (obj != vm->StopIteration)
{
this->append(obj);
obj = vm->py_next(it);
}
}
} }
void PyDeque::rotate(int n) void PyDeque::rotate(int n)
@ -244,7 +271,10 @@ namespace pkpy
void PyDeque::_gc_mark() const void PyDeque::_gc_mark() const
{ {
// TODO: HOW TO IMPLEMENT THIS? for (PyObject *obj : this->dequeItems)
{
PK_OBJ_MARK(obj);
}
} }
std::stringstream PyDeque::getRepr(VM *vm) std::stringstream PyDeque::getRepr(VM *vm)
@ -260,6 +290,10 @@ namespace pkpy
ss << ", "; ss << ", ";
} }
} }
if (this->bounded)
{
ss << ", maxlen=" << this->maxlen;
}
ss << "])"; ss << "])";
return ss; return ss;
} }