Skip to content

Commit 45d7c38

Browse files
committed
Implement bool.__ror__, __rand__ and __rxor__
1 parent 6f66e84 commit 45d7c38

1 file changed

Lines changed: 41 additions & 8 deletions

File tree

vm/src/obj/objbool.rs

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,11 @@ The class bool is a subclass of the class int, and cannot be subclassed.";
4343
"__new__" => context.new_rustfunc(bool_new),
4444
"__repr__" => context.new_rustfunc(bool_repr),
4545
"__or__" => context.new_rustfunc(bool_or),
46+
"__ror__" => context.new_rustfunc(bool_ror),
4647
"__and__" => context.new_rustfunc(bool_and),
48+
"__rand__" => context.new_rustfunc(bool_rand),
4749
"__xor__" => context.new_rustfunc(bool_xor),
50+
"__rxor__" => context.new_rustfunc(bool_rxor),
4851
"__doc__" => context.new_str(bool_doc.to_string())
4952
});
5053
}
@@ -74,10 +77,10 @@ fn bool_repr(vm: &VirtualMachine, args: PyFuncArgs) -> Result<PyObjectRef, PyObj
7477
Ok(vm.new_str(s))
7578
}
7679

77-
fn bool_or(vm: &VirtualMachine, args: PyFuncArgs) -> Result<PyObjectRef, PyObjectRef> {
78-
arg_check!(vm, args, required = [(lhs, None), (rhs, None)]);
79-
80-
if objtype::isinstance(lhs, &vm.ctx.bool_type()) && objtype::isinstance(rhs, &vm.ctx.bool_type()) {
80+
fn do_bool_or(vm: &VirtualMachine, lhs: &PyObjectRef, rhs: &PyObjectRef) -> PyResult {
81+
if objtype::isinstance(lhs, &vm.ctx.bool_type())
82+
&& objtype::isinstance(rhs, &vm.ctx.bool_type())
83+
{
8184
let lhs = get_value(lhs);
8285
let rhs = get_value(rhs);
8386
(lhs || rhs).into_pyobject(vm)
@@ -86,10 +89,20 @@ fn bool_or(vm: &VirtualMachine, args: PyFuncArgs) -> Result<PyObjectRef, PyObjec
8689
}
8790
}
8891

89-
fn bool_and(vm: &VirtualMachine, args: PyFuncArgs) -> Result<PyObjectRef, PyObjectRef> {
92+
fn bool_or(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
9093
arg_check!(vm, args, required = [(lhs, None), (rhs, None)]);
94+
do_bool_or(vm, lhs, rhs)
95+
}
9196

92-
if objtype::isinstance(lhs, &vm.ctx.bool_type()) && objtype::isinstance(rhs, &vm.ctx.bool_type()) {
97+
fn bool_ror(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
98+
arg_check!(vm, args, required = [(rhs, None), (lhs, None)]);
99+
do_bool_or(vm, lhs, rhs)
100+
}
101+
102+
fn do_bool_and(vm: &VirtualMachine, lhs: &PyObjectRef, rhs: &PyObjectRef) -> PyResult {
103+
if objtype::isinstance(lhs, &vm.ctx.bool_type())
104+
&& objtype::isinstance(rhs, &vm.ctx.bool_type())
105+
{
93106
let lhs = get_value(lhs);
94107
let rhs = get_value(rhs);
95108
(lhs && rhs).into_pyobject(vm)
@@ -98,10 +111,20 @@ fn bool_and(vm: &VirtualMachine, args: PyFuncArgs) -> Result<PyObjectRef, PyObje
98111
}
99112
}
100113

101-
fn bool_xor(vm: &VirtualMachine, args: PyFuncArgs) -> Result<PyObjectRef, PyObjectRef> {
114+
fn bool_and(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
102115
arg_check!(vm, args, required = [(lhs, None), (rhs, None)]);
116+
do_bool_and(vm, lhs, rhs)
117+
}
103118

104-
if objtype::isinstance(lhs, &vm.ctx.bool_type()) && objtype::isinstance(rhs, &vm.ctx.bool_type()) {
119+
fn bool_rand(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
120+
arg_check!(vm, args, required = [(rhs, None), (lhs, None)]);
121+
do_bool_and(vm, lhs, rhs)
122+
}
123+
124+
fn do_bool_xor(vm: &VirtualMachine, lhs: &PyObjectRef, rhs: &PyObjectRef) -> PyResult {
125+
if objtype::isinstance(lhs, &vm.ctx.bool_type())
126+
&& objtype::isinstance(rhs, &vm.ctx.bool_type())
127+
{
105128
let lhs = get_value(lhs);
106129
let rhs = get_value(rhs);
107130
(lhs ^ rhs).into_pyobject(vm)
@@ -110,6 +133,16 @@ fn bool_xor(vm: &VirtualMachine, args: PyFuncArgs) -> Result<PyObjectRef, PyObje
110133
}
111134
}
112135

136+
fn bool_xor(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
137+
arg_check!(vm, args, required = [(lhs, None), (rhs, None)]);
138+
do_bool_xor(vm, lhs, rhs)
139+
}
140+
141+
fn bool_rxor(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
142+
arg_check!(vm, args, required = [(rhs, None), (lhs, None)]);
143+
do_bool_xor(vm, lhs, rhs)
144+
}
145+
113146
fn bool_new(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
114147
arg_check!(
115148
vm,

0 commit comments

Comments
 (0)