Commit 18873875 authored by Kevin Modzelewski's avatar Kevin Modzelewski

Implement list.remove

parent bf51b63a
......@@ -108,15 +108,10 @@ Box* dictPop2(BoxedDict* self, Box* k) {
auto it = self->d.find(k);
if (it == self->d.end()) {
Box* s = NULL;
try {
s = repr(k);
} catch (Box* e) {
s = NULL;
}
BoxedString* s = reprOrNull(k);
if (s)
raiseExcHelper(KeyError, "%s", static_cast<BoxedString*>(s)->s.c_str());
raiseExcHelper(KeyError, "%s", s->s.c_str());
else
raiseExcHelper(KeyError, "");
}
......
......@@ -323,6 +323,24 @@ Box* listCount(BoxedList* self, Box* elt) {
return new BoxedInt(count);
}
Box* listRemove(BoxedList* self, Box* elt) {
assert(self->cls == list_cls);
for (int i = 0; i < self->size; i++) {
Box* e = self->elts->elts[i];
Box* cmp = compareInternal(e, elt, AST_TYPE::Eq, NULL);
bool b = nonzero(cmp);
if (b) {
memmove(self->elts->elts + i, self->elts->elts + i + 1, (self->size - i - 1) * sizeof(Box*));
self->size--;
return None;
}
}
raiseExcHelper(ValueError, "list.remove(x): x not in list");
}
BoxedClass* list_iterator_cls = NULL;
extern "C" void listIteratorGCHandler(GCVisitor* v, void* p) {
......@@ -396,6 +414,7 @@ void setupList() {
list_cls->giveAttr("__new__", new BoxedFunction(new_));
list_cls->giveAttr("count", new BoxedFunction(boxRTFunction((void*)listCount, BOXED_INT, 2, false)));
list_cls->giveAttr("remove", new BoxedFunction(boxRTFunction((void*)listRemove, NONE, 2, false)));
list_cls->freeze();
......
......@@ -1028,6 +1028,25 @@ extern "C" Box* repr(Box* obj) {
return static_cast<BoxedString*>(obj);
}
extern "C" BoxedString* reprOrNull(Box* obj) {
try {
Box* r = repr(obj);
assert(r->cls == str_cls); // this should be checked by repr()
return static_cast<BoxedString*>(r);
} catch (Box* b) {
return nullptr;
}
}
extern "C" BoxedString* strOrNull(Box* obj) {
try {
BoxedString* r = str(obj);
return static_cast<BoxedString*>(r);
} catch (Box* b) {
return nullptr;
}
}
extern "C" bool isinstance(Box* obj, Box* cls, int64_t flags) {
bool false_on_noncls = (flags & 0x1);
......
......@@ -43,6 +43,8 @@ extern "C" Box* runtimeCall(Box*, int64_t, Box*, Box*, Box*, Box**);
extern "C" Box* callattr(Box*, std::string*, bool, int64_t, Box*, Box*, Box*, Box**);
extern "C" BoxedString* str(Box* obj);
extern "C" Box* repr(Box* obj);
extern "C" BoxedString* reprOrNull(Box* obj); // similar to repr, but returns NULL on exception
extern "C" BoxedString* strOrNull(Box* obj); // similar to str, but returns NULL on exception
extern "C" bool isinstance(Box* obj, Box* cls, int64_t flags);
extern "C" BoxedInt* hash(Box* obj);
// extern "C" Box* abs_(Box* obj);
......
......@@ -144,16 +144,12 @@ std::string formatException(Box* b) {
if (attr == nullptr)
return *name;
Box* r;
try {
r = str(attr);
}
catch (Box* b) {
BoxedString* r = strOrNull(attr);
if (!r)
return *name;
}
assert(r->cls == str_cls);
const std::string* msg = &static_cast<BoxedString*>(r)->s;
const std::string* msg = &r->s;
return *name + ": " + *msg;
}
}
......@@ -19,6 +19,7 @@ print d.pop(4)
try:
d.pop(5)
assert 0
except KeyError:
except KeyError, e:
print e.message
print "ok"
print sorted(d.items())
......@@ -33,3 +33,13 @@ print [1, 2, 3, 4, 5]
l = [1, 2, 1, 2, 3]
print l.count(1)
print l.count(42)
print l.remove(1)
print l
try:
l.remove(54)
assert 0
except ValueError, e:
print e
print "ok"
print l
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