Commit d98f6eb0 authored by Marius Wachtler's avatar Marius Wachtler

Make bool logical ops more similar to cpythons

parent 01fb5d0b
......@@ -14,6 +14,7 @@
#include "core/common.h"
#include "core/types.h"
#include "runtime/int.h"
#include "runtime/objmodel.h"
#include "runtime/types.h"
......@@ -54,33 +55,33 @@ extern "C" Box* boolNew(Box* cls, Box* val) {
}
extern "C" Box* boolAnd(BoxedBool* lhs, BoxedBool* rhs) {
if (lhs->cls != bool_cls)
if (!PyBool_Check(lhs))
raiseExcHelper(TypeError, "descriptor '__and__' requires a 'bool' object but received a '%s'",
getTypeName(lhs));
if (rhs->cls != bool_cls)
return NotImplemented;
if (!PyBool_Check(rhs))
return intAnd(lhs, rhs);
return boxBool(lhs->n && rhs->n);
}
extern "C" Box* boolOr(BoxedBool* lhs, BoxedBool* rhs) {
if (lhs->cls != bool_cls)
if (!PyBool_Check(lhs))
raiseExcHelper(TypeError, "descriptor '__or__' requires a 'bool' object but received a '%s'", getTypeName(lhs));
if (rhs->cls != bool_cls)
return NotImplemented;
if (!PyBool_Check(rhs))
return intOr(lhs, rhs);
return boxBool(lhs->n || rhs->n);
}
extern "C" Box* boolXor(BoxedBool* lhs, BoxedBool* rhs) {
if (lhs->cls != bool_cls)
if (!PyBool_Check(lhs))
raiseExcHelper(TypeError, "descriptor '__xor__' requires a 'bool' object but received a '%s'",
getTypeName(lhs));
if (rhs->cls != bool_cls)
return NotImplemented;
if (!PyBool_Check(rhs))
return intXor(lhs, rhs);
return boxBool(lhs->n ^ rhs->n);
}
......@@ -97,9 +98,10 @@ void setupBool() {
bool_cls->giveAttr("__new__",
new BoxedFunction(FunctionMetadata::create((void*)boolNew, UNKNOWN, 2, false, false), { None }));
bool_cls->giveAttr("__and__", new BoxedFunction(FunctionMetadata::create((void*)boolAnd, BOXED_BOOL, 2)));
bool_cls->giveAttr("__or__", new BoxedFunction(FunctionMetadata::create((void*)boolOr, BOXED_BOOL, 2)));
bool_cls->giveAttr("__xor__", new BoxedFunction(FunctionMetadata::create((void*)boolXor, BOXED_BOOL, 2)));
// TODO: type specialize
bool_cls->giveAttr("__and__", new BoxedFunction(FunctionMetadata::create((void*)boolAnd, UNKNOWN, 2)));
bool_cls->giveAttr("__or__", new BoxedFunction(FunctionMetadata::create((void*)boolOr, UNKNOWN, 2)));
bool_cls->giveAttr("__xor__", new BoxedFunction(FunctionMetadata::create((void*)boolXor, UNKNOWN, 2)));
bool_cls->freeze();
bool_cls->tp_hash = (hashfunc)bool_hash;
......
......@@ -46,8 +46,10 @@ extern "C" Box* intGe(BoxedInt* lhs, Box* rhs);
extern "C" Box* intLShift(BoxedInt* lhs, Box* rhs);
extern "C" Box* intMod(BoxedInt* lhs, Box* rhs);
extern "C" Box* intMul(BoxedInt* lhs, Box* rhs);
extern "C" Box* intOr(BoxedInt* lhs, Box* rhs);
extern "C" Box* intRShift(BoxedInt* lhs, Box* rhs);
extern "C" Box* intSub(BoxedInt* lhs, Box* rhs);
extern "C" Box* intXor(BoxedInt* lhs, Box* rhs);
extern "C" Box* intInvert(BoxedInt* v);
extern "C" Box* intPos(BoxedInt* v);
extern "C" Box* intNeg(BoxedInt* v);
......
......@@ -31,10 +31,14 @@ 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
for rhs in (True, False, 1, 1.0):
try:
print lhs.__and__(rhs), lhs.__or__(rhs), lhs.__xor__(rhs)
print lhs | rhs
print lhs & rhs
print lhs ^ rhs
except Exception as e:
print e
print range(False, True, True)
......
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