Commit ef3644bb authored by Marius Wachtler's avatar Marius Wachtler

Implement 'or, xor, and' for bool and setiterator.__iter__

parent 7e26d109
......@@ -47,6 +47,39 @@ extern "C" Box* boolNew(Box* cls, Box* val) {
return boxBool(b);
}
extern "C" Box* boolAnd(BoxedBool* lhs, BoxedBool* rhs) {
if (lhs->cls != bool_cls)
raiseExcHelper(TypeError, "descriptor '__and__' requires a 'bool' object but received a '%s'",
getTypeName(lhs));
if (rhs->cls != bool_cls)
return NotImplemented;
return boxBool(lhs->n && rhs->n);
}
extern "C" Box* boolOr(BoxedBool* lhs, BoxedBool* rhs) {
if (lhs->cls != bool_cls)
raiseExcHelper(TypeError, "descriptor '__or__' requires a 'bool' object but received a '%s'", getTypeName(lhs));
if (rhs->cls != bool_cls)
return NotImplemented;
return boxBool(lhs->n || rhs->n);
}
extern "C" Box* boolXor(BoxedBool* lhs, BoxedBool* rhs) {
if (lhs->cls != bool_cls)
raiseExcHelper(TypeError, "descriptor '__xor__' requires a 'bool' object but received a '%s'",
getTypeName(lhs));
if (rhs->cls != bool_cls)
return NotImplemented;
return boxBool(lhs->n ^ rhs->n);
}
void setupBool() {
bool_cls->giveAttr("__nonzero__", new BoxedFunction(boxRTFunction((void*)boolNonzero, BOXED_BOOL, 1)));
bool_cls->giveAttr("__repr__", new BoxedFunction(boxRTFunction((void*)boolRepr, STR, 1)));
......@@ -55,6 +88,9 @@ void setupBool() {
bool_cls->giveAttr("__new__",
new BoxedFunction(boxRTFunction((void*)boolNew, UNKNOWN, 2, 1, false, false), { None }));
bool_cls->giveAttr("__and__", new BoxedFunction(boxRTFunction((void*)boolAnd, BOXED_BOOL, 2)));
bool_cls->giveAttr("__or__", new BoxedFunction(boxRTFunction((void*)boolOr, BOXED_BOOL, 2)));
bool_cls->giveAttr("__xor__", new BoxedFunction(boxRTFunction((void*)boolXor, BOXED_BOOL, 2)));
bool_cls->freeze();
......
......@@ -66,6 +66,11 @@ Box* setiteratorNext(BoxedSetIterator* self) {
return self->next();
}
Box* setiteratorIter(BoxedSetIterator* self) {
assert(self->cls == set_iterator_cls);
return self;
}
Box* setAdd2(Box* _self, Box* b) {
assert(_self->cls == set_cls || _self->cls == frozenset_cls);
BoxedSet* self = static_cast<BoxedSet*>(_self);
......@@ -272,6 +277,8 @@ using namespace pyston::set;
void setupSet() {
set_iterator_cls = BoxedHeapClass::create(type_cls, object_cls, &setIteratorGCHandler, 0, 0, sizeof(BoxedSet),
false, "setiterator");
set_iterator_cls->giveAttr(
"__iter__", new BoxedFunction(boxRTFunction((void*)setiteratorIter, typeFromClass(set_iterator_cls), 1)));
set_iterator_cls->giveAttr("__hasnext__",
new BoxedFunction(boxRTFunction((void*)setiteratorHasnext, BOXED_BOOL, 1)));
set_iterator_cls->giveAttr("next", new BoxedFunction(boxRTFunction((void*)setiteratorNext, UNKNOWN, 1)));
......
......@@ -30,6 +30,12 @@ print hash(1) == hash(True)
print isinstance(True, int)
print isinstance(False, int)
for lhs in (True, False):
for rhs in (True, False):
print lhs | rhs
print lhs & rhs
print lhs ^ rhs
print range(False, True, True)
print abs(True), abs(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