Commit 31f9357e authored by Kevin Modzelewski's avatar Kevin Modzelewski

Merge pull request #47 from undingen/specialice

Type specialice more runtime methods
parents a7de7041 132fc875
......@@ -137,6 +137,12 @@ extern "C" Box* intAdd(BoxedInt* lhs, Box *rhs) {
}
}
extern "C" Box* intAndInt(BoxedInt* lhs, BoxedInt *rhs) {
assert(lhs->cls == int_cls);
assert(rhs->cls == int_cls);
return boxInt(lhs->n & rhs->n);
}
extern "C" Box* intAnd(BoxedInt* lhs, Box *rhs) {
assert(lhs->cls == int_cls);
if (rhs->cls != int_cls) {
......@@ -174,6 +180,12 @@ extern "C" Box* intDiv(BoxedInt* lhs, Box *rhs) {
}
}
extern "C" Box* intEqInt(BoxedInt* lhs, BoxedInt *rhs) {
assert(lhs->cls == int_cls);
assert(rhs->cls == int_cls);
return boxBool(lhs->n == rhs->n);
}
extern "C" Box* intEq(BoxedInt* lhs, Box *rhs) {
assert(lhs->cls == int_cls);
if (rhs->cls != int_cls) {
......@@ -183,6 +195,12 @@ extern "C" Box* intEq(BoxedInt* lhs, Box *rhs) {
return boxBool(lhs->n == rhs_int->n);
}
extern "C" Box* intNeInt(BoxedInt* lhs, BoxedInt *rhs) {
assert(lhs->cls == int_cls);
assert(rhs->cls == int_cls);
return boxBool(lhs->n != rhs->n);
}
extern "C" Box* intNe(BoxedInt* lhs, Box *rhs) {
assert(lhs->cls == int_cls);
if (rhs->cls != int_cls) {
......@@ -192,6 +210,12 @@ extern "C" Box* intNe(BoxedInt* lhs, Box *rhs) {
return boxBool(lhs->n != rhs_int->n);
}
extern "C" Box* intLtInt(BoxedInt* lhs, BoxedInt *rhs) {
assert(lhs->cls == int_cls);
assert(rhs->cls == int_cls);
return boxBool(lhs->n < rhs->n);
}
extern "C" Box* intLt(BoxedInt* lhs, Box *rhs) {
assert(lhs->cls == int_cls);
if (rhs->cls != int_cls) {
......@@ -201,6 +225,12 @@ extern "C" Box* intLt(BoxedInt* lhs, Box *rhs) {
return boxBool(lhs->n < rhs_int->n);
}
extern "C" Box* intLeInt(BoxedInt* lhs, BoxedInt *rhs) {
assert(lhs->cls == int_cls);
assert(rhs->cls == int_cls);
return boxBool(lhs->n <= rhs->n);
}
extern "C" Box* intLe(BoxedInt* lhs, Box *rhs) {
assert(lhs->cls == int_cls);
if (rhs->cls != int_cls) {
......@@ -210,6 +240,12 @@ extern "C" Box* intLe(BoxedInt* lhs, Box *rhs) {
return boxBool(lhs->n <= rhs_int->n);
}
extern "C" Box* intGtInt(BoxedInt* lhs, BoxedInt *rhs) {
assert(lhs->cls == int_cls);
assert(rhs->cls == int_cls);
return boxBool(lhs->n > rhs->n);
}
extern "C" Box* intGt(BoxedInt* lhs, Box *rhs) {
assert(lhs->cls == int_cls);
if (rhs->cls != int_cls) {
......@@ -219,6 +255,12 @@ extern "C" Box* intGt(BoxedInt* lhs, Box *rhs) {
return boxBool(lhs->n > rhs_int->n);
}
extern "C" Box* intGeInt(BoxedInt* lhs, BoxedInt *rhs) {
assert(lhs->cls == int_cls);
assert(rhs->cls == int_cls);
return boxBool(lhs->n >= rhs->n);
}
extern "C" Box* intGe(BoxedInt* lhs, Box *rhs) {
assert(lhs->cls == int_cls);
if (rhs->cls != int_cls) {
......@@ -228,6 +270,12 @@ extern "C" Box* intGe(BoxedInt* lhs, Box *rhs) {
return boxBool(lhs->n >= rhs_int->n);
}
extern "C" Box* intLShiftInt(BoxedInt* lhs, BoxedInt *rhs) {
assert(lhs->cls == int_cls);
assert(rhs->cls == int_cls);
return boxInt(lhs->n << rhs->n);
}
extern "C" Box* intLShift(BoxedInt* lhs, Box *rhs) {
assert(lhs->cls == int_cls);
if (rhs->cls != int_cls) {
......@@ -237,13 +285,18 @@ extern "C" Box* intLShift(BoxedInt* lhs, Box *rhs) {
return boxInt(lhs->n << rhs_int->n);
}
extern "C" Box* intModInt(BoxedInt* lhs, BoxedInt *rhs) {
assert(lhs->cls == int_cls);
assert(rhs->cls == int_cls);
return boxInt(mod_i64_i64(lhs->n, rhs->n));
}
extern "C" Box* intMod(BoxedInt* lhs, Box *rhs) {
assert(lhs->cls == int_cls);
if (rhs->cls != int_cls) {
return NotImplemented;
}
BoxedInt *rhs_int = static_cast<BoxedInt*>(rhs);
return boxInt(mod_i64_i64(lhs->n, rhs_int->n));
}
......@@ -272,6 +325,19 @@ extern "C" Box* intMul(BoxedInt* lhs, Box *rhs) {
}
}
extern "C" Box* intPowInt(BoxedInt* lhs, BoxedInt *rhs) {
assert(lhs->cls == int_cls);
assert(rhs->cls == int_cls);
BoxedInt *rhs_int = static_cast<BoxedInt*>(rhs);
return boxInt(pow_i64_i64(lhs->n, rhs_int->n));
}
extern "C" Box* intPowFloat(BoxedInt* lhs, BoxedFloat *rhs) {
assert(lhs->cls == int_cls);
assert(rhs->cls == float_cls);
return boxFloat(pow(lhs->n, rhs->d));
}
extern "C" Box* intPow(BoxedInt* lhs, Box *rhs) {
assert(lhs->cls == int_cls);
if (rhs->cls == int_cls) {
......@@ -285,6 +351,12 @@ extern "C" Box* intPow(BoxedInt* lhs, Box *rhs) {
}
}
extern "C" Box* intRShiftInt(BoxedInt* lhs, BoxedInt *rhs) {
assert(lhs->cls == int_cls);
assert(rhs->cls == int_cls);
return boxInt(lhs->n >> rhs->n);
}
extern "C" Box* intRShift(BoxedInt* lhs, Box *rhs) {
assert(lhs->cls == int_cls);
if (rhs->cls != int_cls) {
......@@ -390,7 +462,7 @@ extern "C" Box* intInit2(BoxedInt* self, Box* val) {
return None;
}
static void _addFunc(const char* name, void* int_func, void* float_func, void* boxed_func) {
static void _addFuncIntFloatUnknown(const char* name, void* int_func, void* float_func, void* boxed_func) {
std::vector<ConcreteCompilerType*> v_ii, v_if, v_iu;
assert(BOXED_INT);
v_ii.push_back(BOXED_INT); v_ii.push_back(BOXED_INT);
......@@ -398,8 +470,20 @@ static void _addFunc(const char* name, void* int_func, void* float_func, void* b
v_iu.push_back(BOXED_INT); v_iu.push_back(NULL);
CLFunction *cl = createRTFunction();
addRTFunction(cl, int_func, NULL, v_ii, false);
addRTFunction(cl, float_func, NULL, v_if, false);
addRTFunction(cl, int_func, BOXED_INT, v_ii, false);
addRTFunction(cl, float_func, BOXED_FLOAT, v_if, false);
addRTFunction(cl, boxed_func, NULL, v_iu, false);
int_cls->giveAttr(name, new BoxedFunction(cl));
}
static void _addFuncIntUnknown(const char* name, ConcreteCompilerType* rtn_type, void* int_func, void* boxed_func) {
std::vector<ConcreteCompilerType*> v_ii, v_iu;
assert(BOXED_INT);
v_ii.push_back(BOXED_INT); v_ii.push_back(BOXED_INT);
v_iu.push_back(BOXED_INT); v_iu.push_back(NULL);
CLFunction *cl = createRTFunction();
addRTFunction(cl, int_func, rtn_type, v_ii, false);
addRTFunction(cl, boxed_func, NULL, v_iu, false);
int_cls->giveAttr(name, new BoxedFunction(cl));
}
......@@ -407,33 +491,31 @@ static void _addFunc(const char* name, void* int_func, void* float_func, void* b
void setupInt() {
int_cls->giveAttr("__name__", boxStrConstant("int"));
//int_cls->giveAttr("__add__", new BoxedFunction(boxRTFunction((void*)intAdd, NULL, 2, false)));
_addFunc("__add__", (void*)intAddInt, (void*)intAddFloat, (void*)intAdd);
int_cls->giveAttr("__and__", new BoxedFunction(boxRTFunction((void*)intAnd, NULL, 2, false)));
_addFunc("__div__", (void*)intDivInt, (void*)intDivFloat, (void*)intDiv);
//int_cls->giveAttr("__div__", new BoxedFunction(boxRTFunction((void*)intDiv, NULL, 2, false)));
int_cls->giveAttr("__eq__", new BoxedFunction(boxRTFunction((void*)intEq, NULL, 2, false)));
int_cls->giveAttr("__ne__", new BoxedFunction(boxRTFunction((void*)intNe, NULL, 2, false)));
int_cls->giveAttr("__lt__", new BoxedFunction(boxRTFunction((void*)intLt, NULL, 2, false)));
int_cls->giveAttr("__le__", new BoxedFunction(boxRTFunction((void*)intLe, NULL, 2, false)));
int_cls->giveAttr("__gt__", new BoxedFunction(boxRTFunction((void*)intGt, NULL, 2, false)));
int_cls->giveAttr("__ge__", new BoxedFunction(boxRTFunction((void*)intGe, NULL, 2, false)));
int_cls->giveAttr("__lshift__", new BoxedFunction(boxRTFunction((void*)intLShift, NULL, 2, false)));
int_cls->giveAttr("__mod__", new BoxedFunction(boxRTFunction((void*)intMod, NULL, 2, false)));
_addFunc("__mul__", (void*)intMulInt, (void*)intMulFloat, (void*)intMul);
//int_cls->giveAttr("__mul__", new BoxedFunction(boxRTFunction((void*)intMul, NULL, 2, false)));
int_cls->giveAttr("__pow__", new BoxedFunction(boxRTFunction((void*)intPow, NULL, 2, false)));
int_cls->giveAttr("__rshift__", new BoxedFunction(boxRTFunction((void*)intRShift, NULL, 2, false)));
_addFunc("__sub__", (void*)intSubInt, (void*)intSubFloat, (void*)intSub);
//int_cls->giveAttr("__sub__", new BoxedFunction(boxRTFunction((void*)intSub, NULL, 2, false)));
int_cls->giveAttr("__invert__", new BoxedFunction(boxRTFunction((void*)intInvert, NULL, 1, false)));
int_cls->giveAttr("__pos__", new BoxedFunction(boxRTFunction((void*)intPos, NULL, 1, false)));
int_cls->giveAttr("__neg__", new BoxedFunction(boxRTFunction((void*)intNeg, NULL, 1, false)));
int_cls->giveAttr("__nonzero__", new BoxedFunction(boxRTFunction((void*)intNonzero, NULL, 1, false)));
int_cls->giveAttr("__repr__", new BoxedFunction(boxRTFunction((void*)intRepr, NULL, 1, false)));
_addFuncIntFloatUnknown("__add__", (void*)intAddInt, (void*)intAddFloat, (void*)intAdd);
_addFuncIntUnknown("__and__", BOXED_INT, (void*)intAndInt, (void*)intAnd);
_addFuncIntFloatUnknown("__sub__", (void*)intSubInt, (void*)intSubFloat, (void*)intSub);
_addFuncIntFloatUnknown("__div__", (void*)intDivInt, (void*)intDivFloat, (void*)intDiv);
_addFuncIntFloatUnknown("__mul__", (void*)intMulInt, (void*)intMulFloat, (void*)intMul);
_addFuncIntUnknown("__mod__", BOXED_INT, (void*)intModInt, (void*)intMod);
_addFuncIntFloatUnknown("__pow__", (void*)intPowInt, (void*)intPowFloat, (void*)intPow);
_addFuncIntUnknown("__eq__", BOXED_BOOL, (void*)intEqInt, (void*)intEq);
_addFuncIntUnknown("__ne__", BOXED_BOOL, (void*)intNeInt, (void*)intNe);
_addFuncIntUnknown("__lt__", BOXED_BOOL, (void*)intLtInt, (void*)intLt);
_addFuncIntUnknown("__le__", BOXED_BOOL, (void*)intLeInt, (void*)intLe);
_addFuncIntUnknown("__gt__", BOXED_BOOL, (void*)intGtInt, (void*)intGt);
_addFuncIntUnknown("__ge__", BOXED_BOOL, (void*)intGeInt, (void*)intGe);
_addFuncIntUnknown("__lshift__", BOXED_INT, (void*)intLShiftInt, (void*)intLShift);
_addFuncIntUnknown("__rshift__", BOXED_INT, (void*)intRShiftInt, (void*)intRShift);
int_cls->giveAttr("__invert__", new BoxedFunction(boxRTFunction((void*)intInvert, BOXED_INT, 1, false)));
int_cls->giveAttr("__pos__", new BoxedFunction(boxRTFunction((void*)intPos, BOXED_INT, 1, false)));
int_cls->giveAttr("__neg__", new BoxedFunction(boxRTFunction((void*)intNeg, BOXED_INT, 1, false)));
int_cls->giveAttr("__nonzero__", new BoxedFunction(boxRTFunction((void*)intNonzero, BOXED_BOOL, 1, false)));
int_cls->giveAttr("__repr__", new BoxedFunction(boxRTFunction((void*)intRepr, STR, 1, false)));
int_cls->setattr("__str__", int_cls->peekattr("__repr__"), NULL, NULL);
int_cls->giveAttr("__hash__", new BoxedFunction(boxRTFunction((void*)intHash, NULL, 1, false)));
int_cls->giveAttr("__hash__", new BoxedFunction(boxRTFunction((void*)intHash, BOXED_INT, 1, false)));
CLFunction *__new__ = boxRTFunction((void*)intNew1, NULL, 1, false);
addRTFunction(__new__, (void*)intNew2, NULL, 2, false);
......
......@@ -113,74 +113,93 @@ Box* _listSlice(BoxedList *self, i64 start, i64 stop, i64 step) {
return rtn;
}
extern "C" Box* listGetitemInt(BoxedList* self, BoxedInt* slice) {
assert(self->cls == list_cls);
assert(slice->cls == int_cls);
int64_t n = slice->n;
if (n < 0)
n = self->size + n;
if (n < 0 || n >= self->size) {
fprintf(stderr, "IndexError: list index out of range\n");
raiseExc();
}
Box* rtn = self->elts->elts[n];
return rtn;
}
extern "C" Box* listGetitemSlice(BoxedList* self, BoxedSlice* slice) {
assert(self->cls == list_cls);
assert(slice->cls == slice_cls);
i64 start, stop, step;
parseSlice(slice, self->size, &start, &stop, &step);
return _listSlice(self, start, stop, step);
}
extern "C" Box* listGetitem(BoxedList* self, Box* slice) {
assert(self->cls == list_cls);
if (slice->cls == int_cls) {
BoxedInt* islice = static_cast<BoxedInt*>(slice);
int64_t n = islice->n;
if (n < 0)
n = self->size + n;
if (n < 0 || n >= self->size) {
fprintf(stderr, "IndexError: list index out of range\n");
raiseExc();
}
Box* rtn = self->elts->elts[n];
return rtn;
return listGetitemInt(self, static_cast<BoxedInt*>(slice));
} else if (slice->cls == slice_cls) {
BoxedSlice *sslice = static_cast<BoxedSlice*>(slice);
i64 start, stop, step;
parseSlice(sslice, self->size, &start, &stop, &step);
return _listSlice(self, start, stop, step);
return listGetitemSlice(self, static_cast<BoxedSlice*>(slice));
} else {
fprintf(stderr, "TypeError: list indices must be integers, not %s\n", getTypeName(slice)->c_str());
raiseExc();
}
}
extern "C" Box* listSetitem(BoxedList* self, Box* slice, Box* v) {
if (slice->cls == int_cls) {
BoxedInt* islice = static_cast<BoxedInt*>(slice);
int64_t n = islice->n;
if (n < 0)
n = self->size + n;
if (n < 0 || n >= self->size) {
fprintf(stderr, "IndexError: list index out of range\n");
raiseExc();
}
Box* prev = self->elts->elts[n];
self->elts->elts[n] = v;
return None;
} else if (slice->cls == slice_cls) {
BoxedSlice *sslice = static_cast<BoxedSlice*>(slice);
i64 start, stop, step;
parseSlice(sslice, self->size, &start, &stop, &step);
RELEASE_ASSERT(step == 1, "step sizes must be 1 for now");
extern "C" Box* listSetitemInt(BoxedList* self, BoxedInt* slice, Box* v) {
assert(self->cls == list_cls);
assert(slice->cls == int_cls);
int64_t n = slice->n;
if (n < 0)
n = self->size + n;
assert(0 <= start && start < self->size);
ASSERT(0 <= stop && stop <= self->size, "%ld %ld", self->size, stop);
assert(start <= stop);
if (n < 0 || n >= self->size) {
fprintf(stderr, "IndexError: list index out of range\n");
raiseExc();
}
ASSERT(v->cls == list_cls, "unsupported %s", getTypeName(v)->c_str());
BoxedList *lv = static_cast<BoxedList*>(v);
self->elts->elts[n] = v;
return None;
}
int delts = lv->size - (stop - start);
int remaining_elts = self->size - stop;
self->ensure(delts);
extern "C" Box* listSetitemSlice(BoxedList* self, BoxedSlice* slice, Box* v) {
assert(self->cls == list_cls);
assert(slice->cls == slice_cls);
i64 start, stop, step;
parseSlice(slice, self->size, &start, &stop, &step);
RELEASE_ASSERT(step == 1, "step sizes must be 1 for now");
assert(0 <= start && start < self->size);
ASSERT(0 <= stop && stop <= self->size, "%ld %ld", self->size, stop);
assert(start <= stop);
ASSERT(v->cls == list_cls, "unsupported %s", getTypeName(v)->c_str());
BoxedList *lv = static_cast<BoxedList*>(v);
int delts = lv->size - (stop - start);
int remaining_elts = self->size - stop;
self->ensure(delts);
memmove(self->elts->elts + start + lv->size, self->elts->elts + stop, remaining_elts * sizeof(Box*));
for (int i = 0; i < lv->size; i++) {
Box* r = lv->elts->elts[i];
self->elts->elts[start + i] = r;
}
memmove(self->elts->elts + start + lv->size, self->elts->elts + stop, remaining_elts * sizeof(Box*));
for (int i = 0; i < lv->size; i++) {
Box* r = lv->elts->elts[i];
self->elts->elts[start + i] = r;
}
self->size += delts;
self->size += delts;
return None;
}
return None;
extern "C" Box* listSetitem(BoxedList* self, Box* slice, Box* v) {
assert(self->cls == list_cls);
if (slice->cls == int_cls) {
return listSetitemInt(self, static_cast<BoxedInt*>(slice), v);
} else if (slice->cls == slice_cls) {
return listSetitemSlice(self, static_cast<BoxedSlice*>(slice), v);
} else {
fprintf(stderr, "TypeError: list indices must be integers, not %s\n", getTypeName(slice)->c_str());
raiseExc();
......@@ -347,20 +366,32 @@ void setupList() {
list_cls->giveAttr("__name__", boxStrConstant("list"));
list_cls->giveAttr("__len__", new BoxedFunction(boxRTFunction((void*)listLen, NULL, 1, false)));
list_cls->giveAttr("__getitem__", new BoxedFunction(boxRTFunction((void*)listGetitem, NULL, 2, false)));
list_cls->giveAttr("__len__", new BoxedFunction(boxRTFunction((void*)listLen, BOXED_INT, 1, false)));
CLFunction *getitem = createRTFunction();
addRTFunction(getitem, (void*)listGetitemInt, NULL, std::vector<ConcreteCompilerType*>{LIST, BOXED_INT}, false);
addRTFunction(getitem, (void*)listGetitemSlice, NULL, std::vector<ConcreteCompilerType*>{LIST, SLICE}, false);
addRTFunction(getitem, (void*)listGetitem, NULL, std::vector<ConcreteCompilerType*>{LIST, NULL}, false);
list_cls->giveAttr("__getitem__", new BoxedFunction(getitem));
list_cls->giveAttr("__iter__", new BoxedFunction(boxRTFunction((void*)listIter, typeFromClass(list_iterator_cls), 1, false)));
list_cls->giveAttr("__repr__", new BoxedFunction(boxRTFunction((void*)listRepr, NULL, 1, false)));
list_cls->giveAttr("__repr__", new BoxedFunction(boxRTFunction((void*)listRepr, STR, 1, false)));
list_cls->setattr("__str__", list_cls->peekattr("__repr__"), NULL, NULL);
list_cls->giveAttr("__nonzero__", new BoxedFunction(boxRTFunction((void*)listNonzero, NULL, 1, false)));
list_cls->giveAttr("__nonzero__", new BoxedFunction(boxRTFunction((void*)listNonzero, BOXED_BOOL, 1, false)));
CLFunction *pop = boxRTFunction((void*)listPop1, NULL, 1, false);
addRTFunction(pop, (void*)listPop2, NULL, 2, false);
list_cls->giveAttr("pop", new BoxedFunction(pop));
list_cls->giveAttr("append", new BoxedFunction(boxRTFunction((void*)listAppend, NULL, 2, false)));
list_cls->giveAttr("__setitem__", new BoxedFunction(boxRTFunction((void*)listSetitem, NULL, 3, false)));
CLFunction *setitem = createRTFunction();
addRTFunction(setitem, (void*)listSetitemInt, NULL, std::vector<ConcreteCompilerType*>{LIST, BOXED_INT, NULL}, false);
addRTFunction(setitem, (void*)listSetitemSlice, NULL, std::vector<ConcreteCompilerType*>{LIST, SLICE, NULL}, false);
addRTFunction(setitem, (void*)listSetitem, NULL, std::vector<ConcreteCompilerType*>{LIST, NULL, NULL}, false);
list_cls->giveAttr("__setitem__", new BoxedFunction(setitem));
list_cls->giveAttr("insert", new BoxedFunction(boxRTFunction((void*)listInsert, NULL, 3, false)));
list_cls->giveAttr("__mul__", new BoxedFunction(boxRTFunction((void*)listMul, NULL, 2, false)));
......
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