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 @@
from math import sqrt
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):
ij = i+j
return 1.0 / (ij * (ij + 1) / 2 + i + 1)
......@@ -30,7 +20,7 @@ def eval_A_times_u(u):
local_eval_A = eval_A
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))
......@@ -41,7 +31,7 @@ def eval_At_times_u(u):
local_eval_A = eval_A
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))
......
......@@ -297,6 +297,8 @@ public:
Box* operator*() const { return value; }
Box* operator*() { return value; }
void gcHandler(GCVisitor* v);
private:
Box* iter;
Box* value;
......
......@@ -471,23 +471,20 @@ static BoxedClass* makeBuiltinException(BoxedClass* base, const char* name) {
BoxedClass* enumerate_cls;
class BoxedEnumerate : public Box {
private:
Box* iterator;
BoxIterator iterator, iterator_end;
int64_t idx;
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) {
RELEASE_ASSERT(cls == enumerate_cls, "");
RELEASE_ASSERT(start->cls == int_cls, "");
int64_t idx = static_cast<BoxedInt*>(start)->n;
static const std::string iter_name("__iter__");
Box* iterator
= 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);
llvm::iterator_range<BoxIterator> range = obj->pyElements();
return new BoxedEnumerate(range.begin(), range.end(), idx);
}
static Box* iter(Box* _self) {
......@@ -497,35 +494,23 @@ public:
}
static Box* next(Box* _self) {
static const std::string next_name("next");
assert(_self->cls == enumerate_cls);
BoxedEnumerate* self = static_cast<BoxedEnumerate*>(_self);
Box* o = callattrInternal(self->iterator, &next_name, CLASS_ONLY, NULL, ArgPassSpec(0), NULL, NULL, NULL, NULL,
NULL);
RELEASE_ASSERT(o, "");
Box* r = new BoxedTuple({ boxInt(self->idx), o });
self->idx++;
return r;
return new BoxedTuple({ boxInt(self->idx++), *self->iterator++ });
}
static Box* hasnext(Box* _self) {
static const std::string hasnext_name("__hasnext__");
assert(_self->cls == enumerate_cls);
BoxedEnumerate* self = static_cast<BoxedEnumerate*>(_self);
Box* r = callattrInternal(self->iterator, &hasnext_name, CLASS_ONLY, NULL, ArgPassSpec(0), NULL, NULL, NULL,
NULL, NULL);
RELEASE_ASSERT(r, "%s", getTypeName(self->iterator)->c_str());
return r;
return boxBool(self->iterator != self->iterator_end);
}
static void gcHandler(GCVisitor* v, Box* b) {
boxGCHandler(v, 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++() {
return *this;
}
void BoxIterator::gcHandler(GCVisitor* v) {
v->visitPotential(iter);
v->visitPotential(value);
}
llvm::iterator_range<BoxIterator> Box::pyElements() {
static std::string iter_str("__iter__");
......
......@@ -46,3 +46,8 @@ try:
divmod(1, "")
except TypeError, 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