Commit 6c38b210 authored by Kevin Modzelewski's avatar Kevin Modzelewski

Merge branch 'enumerate_generator' of https://github.com/undingen/pyston

Conflicts:
	test/tests/builtins.py
parents 15f24443 5d8d680e
...@@ -11,16 +11,6 @@ ...@@ -11,16 +11,6 @@
from math import sqrt from math import sqrt
from sys import argv from sys import argv
# pyston does not yet support enumerate
def _enumerate(sequence):
n = 0
rtn = []
for elem in sequence:
rtn.append((n, elem))
n += 1
return rtn
def eval_A(i, j): def eval_A(i, j):
ij = i+j ij = i+j
return 1.0 / (ij * (ij + 1) / 2 + i + 1) return 1.0 / (ij * (ij + 1) / 2 + i + 1)
...@@ -30,7 +20,7 @@ def eval_A_times_u(u): ...@@ -30,7 +20,7 @@ def eval_A_times_u(u):
local_eval_A = eval_A local_eval_A = eval_A
return [ sum([ local_eval_A(i, j) * u_j return [ sum([ local_eval_A(i, j) * u_j
for j, u_j in _enumerate(u) for j, u_j in enumerate(u)
] ]
) )
for i in range(len(u)) for i in range(len(u))
...@@ -41,7 +31,7 @@ def eval_At_times_u(u): ...@@ -41,7 +31,7 @@ def eval_At_times_u(u):
local_eval_A = eval_A local_eval_A = eval_A
return [ sum([ local_eval_A(j, i) * u_j return [ sum([ local_eval_A(j, i) * u_j
for j, u_j in _enumerate(u) for j, u_j in enumerate(u)
] ]
) )
for i in range(len(u)) for i in range(len(u))
......
...@@ -297,6 +297,8 @@ public: ...@@ -297,6 +297,8 @@ public:
Box* operator*() const { return value; } Box* operator*() const { return value; }
Box* operator*() { return value; } Box* operator*() { return value; }
void gcHandler(GCVisitor* v);
private: private:
Box* iter; Box* iter;
Box* value; Box* value;
......
...@@ -471,23 +471,20 @@ static BoxedClass* makeBuiltinException(BoxedClass* base, const char* name) { ...@@ -471,23 +471,20 @@ static BoxedClass* makeBuiltinException(BoxedClass* base, const char* name) {
BoxedClass* enumerate_cls; BoxedClass* enumerate_cls;
class BoxedEnumerate : public Box { class BoxedEnumerate : public Box {
private: private:
Box* iterator; BoxIterator iterator, iterator_end;
int64_t idx; int64_t idx;
public: public:
BoxedEnumerate(Box* iterator, int64_t idx) : Box(enumerate_cls), iterator(iterator), idx(idx) {} BoxedEnumerate(BoxIterator iterator_begin, BoxIterator iterator_end, int64_t idx)
: Box(enumerate_cls), iterator(iterator_begin), iterator_end(iterator_end), idx(idx) {}
static Box* new_(Box* cls, Box* obj, Box* start) { static Box* new_(Box* cls, Box* obj, Box* start) {
RELEASE_ASSERT(cls == enumerate_cls, ""); RELEASE_ASSERT(cls == enumerate_cls, "");
RELEASE_ASSERT(start->cls == int_cls, ""); RELEASE_ASSERT(start->cls == int_cls, "");
int64_t idx = static_cast<BoxedInt*>(start)->n; int64_t idx = static_cast<BoxedInt*>(start)->n;
static const std::string iter_name("__iter__"); llvm::iterator_range<BoxIterator> range = obj->pyElements();
Box* iterator return new BoxedEnumerate(range.begin(), range.end(), idx);
= callattrInternal(obj, &iter_name, CLASS_ONLY, NULL, ArgPassSpec(0), NULL, NULL, NULL, NULL, NULL);
if (!iterator)
raiseNotIterableError(getTypeName(iterator)->c_str());
return new BoxedEnumerate(iterator, idx);
} }
static Box* iter(Box* _self) { static Box* iter(Box* _self) {
...@@ -497,35 +494,23 @@ public: ...@@ -497,35 +494,23 @@ public:
} }
static Box* next(Box* _self) { static Box* next(Box* _self) {
static const std::string next_name("next");
assert(_self->cls == enumerate_cls); assert(_self->cls == enumerate_cls);
BoxedEnumerate* self = static_cast<BoxedEnumerate*>(_self); BoxedEnumerate* self = static_cast<BoxedEnumerate*>(_self);
Box* o = callattrInternal(self->iterator, &next_name, CLASS_ONLY, NULL, ArgPassSpec(0), NULL, NULL, NULL, NULL, return new BoxedTuple({ boxInt(self->idx++), *self->iterator++ });
NULL);
RELEASE_ASSERT(o, "");
Box* r = new BoxedTuple({ boxInt(self->idx), o });
self->idx++;
return r;
} }
static Box* hasnext(Box* _self) { static Box* hasnext(Box* _self) {
static const std::string hasnext_name("__hasnext__");
assert(_self->cls == enumerate_cls); assert(_self->cls == enumerate_cls);
BoxedEnumerate* self = static_cast<BoxedEnumerate*>(_self); BoxedEnumerate* self = static_cast<BoxedEnumerate*>(_self);
Box* r = callattrInternal(self->iterator, &hasnext_name, CLASS_ONLY, NULL, ArgPassSpec(0), NULL, NULL, NULL, return boxBool(self->iterator != self->iterator_end);
NULL, NULL);
RELEASE_ASSERT(r, "%s", getTypeName(self->iterator)->c_str());
return r;
} }
static void gcHandler(GCVisitor* v, Box* b) { static void gcHandler(GCVisitor* v, Box* b) {
boxGCHandler(v, b); boxGCHandler(v, b);
BoxedEnumerate* it = (BoxedEnumerate*)b; BoxedEnumerate* it = (BoxedEnumerate*)b;
v->visit(it->iterator); it->iterator.gcHandler(v);
it->iterator_end.gcHandler(v);
} }
}; };
......
...@@ -73,6 +73,11 @@ BoxIterator& BoxIterator::operator++() { ...@@ -73,6 +73,11 @@ BoxIterator& BoxIterator::operator++() {
return *this; return *this;
} }
void BoxIterator::gcHandler(GCVisitor* v) {
v->visitPotential(iter);
v->visitPotential(value);
}
llvm::iterator_range<BoxIterator> Box::pyElements() { llvm::iterator_range<BoxIterator> Box::pyElements() {
static std::string iter_str("__iter__"); static std::string iter_str("__iter__");
......
...@@ -46,3 +46,8 @@ try: ...@@ -46,3 +46,8 @@ try:
divmod(1, "") divmod(1, "")
except TypeError, e: except TypeError, e:
print e print e
def G():
yield "A"; yield "B"; yield "C"
print list(enumerate(G()))
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