Commit 36f59af3 authored by Kevin Modzelewski's avatar Kevin Modzelewski

Implement set iterator

parent 6922f0e7
......@@ -761,7 +761,6 @@ static void _reparse(const char* fn, const std::string &cache_fn) {
llvm::sys::path::append(parse_ast_fn, "codegen/parse_ast.py");
std::string cmdline = std::string("python -S ") + parse_ast_fn.str().str() + " " + fn;
printf("%s\n", cmdline.c_str());
FILE *parser = popen(cmdline.c_str(), "r");
FILE *cache_fp = fopen(cache_fn.c_str(), "w");
assert(cache_fp);
......
......@@ -14,6 +14,7 @@
#include <cstring>
#include <sstream>
#include <algorithm>
#include "core/common.h"
#include "core/stats.h"
......@@ -267,6 +268,14 @@ Box* listAdd(BoxedList* self, Box* _rhs) {
return rtn;
}
Box* listSort1(BoxedList* self) {
assert(self->cls == list_cls);
std::sort<Box**, PyLt>(self->elts->elts, self->elts->elts + self->size, PyLt());
return None;
}
BoxedClass *list_iterator_cls = NULL;
extern "C" void listIteratorGCHandler(GCVisitor *v, void* p) {
boxGCHandler(v, p);
......@@ -337,6 +346,8 @@ void setupList() {
list_cls->giveAttr("__iadd__", new BoxedFunction(boxRTFunction((void*)listIAdd, NULL, 2, false)));
list_cls->giveAttr("__add__", new BoxedFunction(boxRTFunction((void*)listAdd, NULL, 2, false)));
list_cls->giveAttr("sort", new BoxedFunction(boxRTFunction((void*)listSort1, NULL, 1, false)));
CLFunction *new_ = boxRTFunction((void*)listNew1, NULL, 1, false);
addRTFunction(new_, (void*)listNew2, NULL, 2, false);
list_cls->giveAttr("__new__", new BoxedFunction(new_));
......
......@@ -22,8 +22,42 @@
namespace pyston {
BoxedClass *set_cls;
BoxedClass *set_cls, *set_iterator_cls;
const ObjectFlavor set_flavor(&boxGCHandler, NULL);
const ObjectFlavor set_iterator_flavor(&boxGCHandler, NULL);
namespace set {
class BoxedSetIterator : public Box {
private:
BoxedSet *s;
decltype(BoxedSet::s)::iterator it;
public:
BoxedSetIterator(BoxedSet *s) : Box(&set_iterator_flavor, set_iterator_cls), s(s), it(s->s.begin()) {
}
bool hasNext() {
return it != s->s.end();
}
Box* next() {
Box* rtn = *it;
++it;
return rtn;
}
};
Box* setiteratorHasnext(BoxedSetIterator *self) {
assert(self->cls == set_iterator_cls);
return boxBool(self->hasNext());
}
Box* setiteratorNext(BoxedSetIterator *self) {
assert(self->cls == set_iterator_cls);
return self->next();
}
Box* setAdd2(Box* _self, Box* b) {
assert(_self->cls == set_cls);
......@@ -142,9 +176,24 @@ Box* setXorSet(BoxedSet *lhs, BoxedSet *rhs) {
return rtn;
}
Box* setIter(BoxedSet *self) {
assert(self->cls == set_cls);
return new BoxedSetIterator(self);
}
} // namespace set
using namespace pyston::set;
void setupSet() {
set_cls->giveAttr("__name__", boxStrConstant("set"));
set_iterator_cls = new BoxedClass(false, NULL);
set_iterator_cls->giveAttr("__name__", boxStrConstant("setiterator"));
set_iterator_cls->giveAttr("__hasnext__", new BoxedFunction(boxRTFunction((void*)setiteratorHasnext, BOXED_BOOL, 1, false)));
set_iterator_cls->giveAttr("next", new BoxedFunction(boxRTFunction((void*)setiteratorNext, UNKNOWN, 1, false)));
set_iterator_cls->freeze();
CLFunction *new_ = boxRTFunction((void*)setNew1, SET, 1, false);
addRTFunction(new_, (void*)setNew2, SET, 2, false);
set_cls->giveAttr("__new__", new BoxedFunction(new_));
......@@ -173,6 +222,8 @@ void setupSet() {
addRTFunction(and_, (void*)setAndSet, SET, v_ss, false);
set_cls->giveAttr("__and__", new BoxedFunction(and_));
set_cls->giveAttr("__iter__", new BoxedFunction(boxRTFunction((void*)setIter, typeFromClass(set_iterator_cls), 1, false)));
set_cls->freeze();
}
......
......@@ -15,7 +15,3 @@ print sorted(s2 - s1)
print sorted(s1 ^ s2)
print sorted(s1 & s2)
print sorted(s1 | s2)
s3 = s1
s1 -= s2
print sorted(s1), sorted(s2), sorted(s3)
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