Commit 69903def authored by Joris Vankerschaver's avatar Joris Vankerschaver

Factor out tupleGetitemInt.

parent 32ccd8a8
...@@ -33,23 +33,27 @@ extern "C" Box* createTuple(int64_t nelts, Box** elts) { ...@@ -33,23 +33,27 @@ extern "C" Box* createTuple(int64_t nelts, Box** elts) {
return new BoxedTuple(std::move(velts)); return new BoxedTuple(std::move(velts));
} }
Box* tupleGetitem(BoxedTuple* self, Box* slice) { Box* tupleGetitemInt(BoxedTuple* self, BoxedInt* slice)
assert(self->cls == tuple_cls); {
i64 n = slice->n;
i64 size = self->elts.size(); i64 size = self->elts.size();
if (slice->cls == int_cls) { if (n < 0)
i64 n = static_cast<BoxedInt*>(slice)->n; n = size - n;
if (n < 0 || n >= size) {
fprintf(stderr, "IndexError: tuple index out of range\n");
raiseExcHelper(IndexError, "");
}
if (n < 0) Box* rtn = self->elts[n];
n = size - n; return rtn;
if (n < 0 || n >= size) { }
fprintf(stderr, "IndexError: tuple index out of range\n");
raiseExcHelper(IndexError, "");
}
Box* rtn = self->elts[n]; Box* tupleGetitem(BoxedTuple* self, Box* slice) {
return rtn; assert(self->cls == tuple_cls);
if (slice->cls == int_cls) {
return tupleGetitemInt(self, static_cast<BoxedInt*>(slice));
} else { } else {
RELEASE_ASSERT(0, ""); RELEASE_ASSERT(0, "");
} }
...@@ -214,7 +218,11 @@ void setupTuple() { ...@@ -214,7 +218,11 @@ void setupTuple() {
tuple_cls->giveAttr("__name__", boxStrConstant("tuple")); tuple_cls->giveAttr("__name__", boxStrConstant("tuple"));
tuple_cls->giveAttr("__getitem__", new BoxedFunction(boxRTFunction((void*)tupleGetitem, UNKNOWN, 2))); CLFunction* getitem = createRTFunction(2, 0, 0, 0);
addRTFunction(getitem, (void*)tupleGetitemInt, UNKNOWN, std::vector<ConcreteCompilerType*>{ BOXED_TUPLE, BOXED_INT });
addRTFunction(getitem, (void*)tupleGetitem, UNKNOWN, std::vector<ConcreteCompilerType*>{ BOXED_TUPLE, UNKNOWN });
tuple_cls->giveAttr("__getitem__", new BoxedFunction(getitem));
tuple_cls->giveAttr("__contains__", new BoxedFunction(boxRTFunction((void*)tupleContains, BOXED_BOOL, 2))); tuple_cls->giveAttr("__contains__", new BoxedFunction(boxRTFunction((void*)tupleContains, BOXED_BOOL, 2)));
tuple_cls->giveAttr("__iter__", tuple_cls->giveAttr("__iter__",
......
...@@ -76,3 +76,8 @@ print () + () ...@@ -76,3 +76,8 @@ print () + ()
print (1, 2, 3) + () print (1, 2, 3) + ()
print () + (1, 2, 3) print () + (1, 2, 3)
print (1, 2) + (2, 3) print (1, 2) + (2, 3)
# __getitem__
t = (1, "2")
assert t[0] == 1
assert t[1] == "2"
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