Commit 6d8b2e91 authored by Marius Wachtler's avatar Marius Wachtler

Add pyElements method.

parent 0cf0c7fe
...@@ -19,6 +19,8 @@ ...@@ -19,6 +19,8 @@
// over having them spread randomly in different files, this should probably be split again // over having them spread randomly in different files, this should probably be split again
// but in a way that makes more sense. // but in a way that makes more sense.
#include <llvm/ADT/iterator_range.h>
#include "core/common.h" #include "core/common.h"
#include "core/stats.h" #include "core/stats.h"
...@@ -283,7 +285,7 @@ public: ...@@ -283,7 +285,7 @@ public:
}; };
class Box; class Box;
class BoxIterator : public std::iterator<std::forward_iterator_tag, Box*> { class BoxIterator {
public: public:
BoxIterator(Box* iter) : iter(iter), value(nullptr) {} BoxIterator(Box* iter) : iter(iter), value(nullptr) {}
...@@ -291,7 +293,6 @@ public: ...@@ -291,7 +293,6 @@ public:
bool operator!=(BoxIterator const& rhs) const { return !(*this == rhs); } bool operator!=(BoxIterator const& rhs) const { return !(*this == rhs); }
BoxIterator& operator++(); BoxIterator& operator++();
BoxIterator operator++(int) { BoxIterator operator++(int) {
BoxIterator tmp(*this); BoxIterator tmp(*this);
operator++(); operator++();
...@@ -313,8 +314,7 @@ class Box : public GCObject { ...@@ -313,8 +314,7 @@ class Box : public GCObject {
public: public:
BoxedClass* cls; BoxedClass* cls;
BoxIterator begin() const; llvm::iterator_range<BoxIterator> pyElements();
BoxIterator end() const { return BoxIterator(nullptr); }
constexpr Box(const ObjectFlavor* flavor, BoxedClass* c) __attribute__((visibility("default"))) constexpr Box(const ObjectFlavor* flavor, BoxedClass* c) __attribute__((visibility("default")))
: GCObject(flavor), cls(c) { : GCObject(flavor), cls(c) {
......
...@@ -51,7 +51,7 @@ extern "C" Box* abs_(Box* x) { ...@@ -51,7 +51,7 @@ extern "C" Box* abs_(Box* x) {
extern "C" Box* min1(Box* container) { extern "C" Box* min1(Box* container) {
Box* minElement = nullptr; Box* minElement = nullptr;
for (Box* e : *container) { for (Box* e : container->pyElements()) {
if (!minElement) { if (!minElement) {
minElement = e; minElement = e;
} else { } else {
...@@ -80,7 +80,7 @@ extern "C" Box* min2(Box* o0, Box* o1) { ...@@ -80,7 +80,7 @@ extern "C" Box* min2(Box* o0, Box* o1) {
extern "C" Box* max1(Box* container) { extern "C" Box* max1(Box* container) {
Box* maxElement = nullptr; Box* maxElement = nullptr;
for (Box* e : *container) { for (Box* e : container->pyElements()) {
if (!maxElement) { if (!maxElement) {
maxElement = e; maxElement = e;
} else { } else {
...@@ -266,7 +266,7 @@ Box* getattr3(Box* obj, Box* _str, Box* default_value) { ...@@ -266,7 +266,7 @@ Box* getattr3(Box* obj, Box* _str, Box* default_value) {
Box* map2(Box* f, Box* container) { Box* map2(Box* f, Box* container) {
Box* rtn = new BoxedList(); Box* rtn = new BoxedList();
for (Box* e : *container) { for (Box* e : container->pyElements()) {
listAppendInternal(rtn, runtimeCall(f, 1, e, NULL, NULL, NULL)); listAppendInternal(rtn, runtimeCall(f, 1, e, NULL, NULL, NULL));
} }
return rtn; return rtn;
......
...@@ -355,7 +355,7 @@ extern "C" Box* listNew2(Box* cls, Box* container) { ...@@ -355,7 +355,7 @@ extern "C" Box* listNew2(Box* cls, Box* container) {
assert(cls == list_cls); assert(cls == list_cls);
BoxedList* rtn = new BoxedList(); BoxedList* rtn = new BoxedList();
for (Box* e : *container) { for (Box* e : container->pyElements()) {
listAppendInternal(rtn, e); listAppendInternal(rtn, e);
} }
return rtn; return rtn;
......
...@@ -73,7 +73,7 @@ Box* setNew2(Box* cls, Box* container) { ...@@ -73,7 +73,7 @@ Box* setNew2(Box* cls, Box* container) {
assert(cls == set_cls); assert(cls == set_cls);
Box* rtn = new BoxedSet(); Box* rtn = new BoxedSet();
for (Box* e : *container) { for (Box* e : container->pyElements()) {
setAdd2(rtn, e); setAdd2(rtn, e);
} }
......
...@@ -50,10 +50,14 @@ BoxIterator& BoxIterator::operator++() { ...@@ -50,10 +50,14 @@ BoxIterator& BoxIterator::operator++() {
return *this; return *this;
} }
BoxIterator Box::begin() const { llvm::iterator_range<BoxIterator> Box::pyElements() {
static std::string iter_str("__iter__"); static std::string iter_str("__iter__");
Box* iter = callattr(const_cast<Box*>(this), &iter_str, true, 0, NULL, NULL, NULL, NULL); Box* iter = callattr(const_cast<Box*>(this), &iter_str, true, 0, NULL, NULL, NULL, NULL);
return ++BoxIterator(iter); if (iter) {
return llvm::iterator_range<BoxIterator>(++BoxIterator(iter), BoxIterator(nullptr));
}
return llvm::iterator_range<BoxIterator>(BoxIterator(nullptr), BoxIterator(nullptr));
} }
extern "C" BoxedFunction::BoxedFunction(CLFunction* f) : HCBox(&function_flavor, function_cls), f(f) { extern "C" BoxedFunction::BoxedFunction(CLFunction* f) : HCBox(&function_flavor, function_cls), f(f) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment