Commit 95539215 authored by Kevin Modzelewski's avatar Kevin Modzelewski

Merge pull request #484 from tjhance/realimag

implement `real` and `imag` for `int`, `long`, and `float`
parents 6faeff5c 5c99f654
# expected: fail
"""Unit tests for numbers.py."""
import math
......
......@@ -728,6 +728,19 @@ static void _addFuncPow(const char* name, ConcreteCompilerType* rtn_type, void*
float_cls->giveAttr(name, new BoxedFunction(cl, { None }));
}
static Box* floatFloat(Box* b, void*) {
if (b->cls == float_cls) {
return b;
} else {
assert(PyFloat_Check(b));
return boxFloat(static_cast<BoxedFloat*>(b)->d);
}
}
static Box* float0(Box*, void*) {
return boxFloat(0.0);
}
void setupFloat() {
_addFunc("__add__", BOXED_FLOAT, (void*)floatAddFloat, (void*)floatAddInt, (void*)floatAdd);
float_cls->giveAttr("__radd__", float_cls->getattr("__add__"));
......@@ -768,6 +781,10 @@ void setupFloat() {
float_cls->giveAttr("__trunc__", new BoxedFunction(boxRTFunction((void*)floatTrunc, BOXED_INT, 1)));
float_cls->giveAttr("real", new (pyston_getset_cls) BoxedGetsetDescriptor(floatFloat, NULL, NULL));
float_cls->giveAttr("imag", new (pyston_getset_cls) BoxedGetsetDescriptor(float0, NULL, NULL));
float_cls->giveAttr("conjugate", new BoxedFunction(boxRTFunction((void*)floatFloat, BOXED_FLOAT, 1)));
float_cls->freeze();
}
......
......@@ -1063,6 +1063,23 @@ static void _addFuncIntUnknown(const char* name, ConcreteCompilerType* rtn_type,
int_cls->giveAttr(name, new BoxedFunction(cl));
}
static Box* intInt(Box* b, void*) {
if (b->cls == int_cls) {
return b;
} else {
assert(PyInt_Check(b));
return boxInt(static_cast<BoxedInt*>(b)->n);
}
}
static Box* int0(Box*, void*) {
return boxInt(0);
}
static Box* int1(Box*, void*) {
return boxInt(1);
}
void setupInt() {
for (int i = 0; i < NUM_INTERNED_INTS; i++) {
interned_ints[i] = new BoxedInt(i);
......@@ -1112,6 +1129,12 @@ void setupInt() {
int_cls->giveAttr("__init__",
new BoxedFunction(boxRTFunction((void*)intInit, NONE, 2, 1, true, false), { boxInt(0) }));
int_cls->giveAttr("real", new (pyston_getset_cls) BoxedGetsetDescriptor(intInt, NULL, NULL));
int_cls->giveAttr("imag", new (pyston_getset_cls) BoxedGetsetDescriptor(int0, NULL, NULL));
int_cls->giveAttr("conjugate", new BoxedFunction(boxRTFunction((void*)intInt, BOXED_INT, 1)));
int_cls->giveAttr("numerator", new (pyston_getset_cls) BoxedGetsetDescriptor(intInt, NULL, NULL));
int_cls->giveAttr("denominator", new (pyston_getset_cls) BoxedGetsetDescriptor(int1, NULL, NULL));
int_cls->freeze();
}
......
......@@ -1296,6 +1296,25 @@ static PyObject* long_pow(PyObject* v, PyObject* w, PyObject* x) noexcept {
}
}
static Box* longLong(Box* b, void*) {
if (b->cls == long_cls) {
return b;
} else {
assert(PyLong_Check(b));
BoxedLong* l = new BoxedLong();
mpz_init_set(l->n, static_cast<BoxedLong*>(b)->n);
return l;
}
}
static Box* long0(Box* b, void*) {
return boxLong(0);
}
static Box* long1(Box* b, void*) {
return boxLong(1);
}
void setupLong() {
mp_set_memory_functions(customised_allocation, customised_realloc, customised_free);
......@@ -1356,6 +1375,12 @@ void setupLong() {
long_cls->giveAttr("__trunc__", new BoxedFunction(boxRTFunction((void*)longTrunc, UNKNOWN, 1)));
long_cls->giveAttr("__index__", new BoxedFunction(boxRTFunction((void*)longIndex, LONG, 1)));
long_cls->giveAttr("real", new (pyston_getset_cls) BoxedGetsetDescriptor(longLong, NULL, NULL));
long_cls->giveAttr("imag", new (pyston_getset_cls) BoxedGetsetDescriptor(long0, NULL, NULL));
long_cls->giveAttr("conjugate", new BoxedFunction(boxRTFunction((void*)longLong, UNKNOWN, 1)));
long_cls->giveAttr("numerator", new (pyston_getset_cls) BoxedGetsetDescriptor(longLong, NULL, NULL));
long_cls->giveAttr("denominator", new (pyston_getset_cls) BoxedGetsetDescriptor(long1, NULL, NULL));
long_cls->freeze();
long_cls->tp_as_number->nb_power = long_pow;
......
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