Arithmatic into Arithmetic.

This commit is contained in:
jfh
2021-10-02 03:42:59 +03:00
parent 7ee6350285
commit fbd3641f5d
10 changed files with 59 additions and 59 deletions

View File

@@ -3,7 +3,7 @@ use crate::{
function::{OptionalArg, OptionalOption},
slots::{Comparable, Hashable, PyComparisonOp, SlotConstructor},
IdProtocol, IntoPyObject,
PyArithmaticValue::{self, *},
PyArithmeticValue::{self, *},
PyClassImpl, PyComparisonValue, PyContext, PyObjectRef, PyRef, PyResult, PyValue,
TryFromObject, TypeProtocol, VirtualMachine,
};
@@ -193,7 +193,7 @@ impl PyComplex {
other: PyObjectRef,
op: F,
vm: &VirtualMachine,
) -> PyResult<PyArithmaticValue<Complex64>>
) -> PyResult<PyArithmeticValue<Complex64>>
where
F: Fn(Complex64, Complex64) -> PyResult<Complex64>,
{
@@ -209,7 +209,7 @@ impl PyComplex {
&self,
other: PyObjectRef,
vm: &VirtualMachine,
) -> PyResult<PyArithmaticValue<Complex64>> {
) -> PyResult<PyArithmeticValue<Complex64>> {
self.op(other, |a, b| Ok(a + b), vm)
}
@@ -218,7 +218,7 @@ impl PyComplex {
&self,
other: PyObjectRef,
vm: &VirtualMachine,
) -> PyResult<PyArithmaticValue<Complex64>> {
) -> PyResult<PyArithmeticValue<Complex64>> {
self.op(other, |a, b| Ok(a - b), vm)
}
@@ -227,7 +227,7 @@ impl PyComplex {
&self,
other: PyObjectRef,
vm: &VirtualMachine,
) -> PyResult<PyArithmaticValue<Complex64>> {
) -> PyResult<PyArithmeticValue<Complex64>> {
self.op(other, |a, b| Ok(b - a), vm)
}
@@ -252,7 +252,7 @@ impl PyComplex {
&self,
other: PyObjectRef,
vm: &VirtualMachine,
) -> PyResult<PyArithmaticValue<Complex64>> {
) -> PyResult<PyArithmeticValue<Complex64>> {
self.op(other, |a, b| Ok(a * b), vm)
}
@@ -261,7 +261,7 @@ impl PyComplex {
&self,
other: PyObjectRef,
vm: &VirtualMachine,
) -> PyResult<PyArithmaticValue<Complex64>> {
) -> PyResult<PyArithmeticValue<Complex64>> {
self.op(other, |a, b| inner_div(a, b, vm), vm)
}
@@ -270,7 +270,7 @@ impl PyComplex {
&self,
other: PyObjectRef,
vm: &VirtualMachine,
) -> PyResult<PyArithmaticValue<Complex64>> {
) -> PyResult<PyArithmeticValue<Complex64>> {
self.op(other, |a, b| inner_div(b, a, vm), vm)
}
@@ -318,7 +318,7 @@ impl PyComplex {
other: PyObjectRef,
mod_val: OptionalOption<PyObjectRef>,
vm: &VirtualMachine,
) -> PyResult<PyArithmaticValue<Complex64>> {
) -> PyResult<PyArithmeticValue<Complex64>> {
if mod_val.flatten().is_some() {
Err(vm.new_value_error("complex modulo not allowed".to_owned()))
} else {
@@ -331,7 +331,7 @@ impl PyComplex {
&self,
other: PyObjectRef,
vm: &VirtualMachine,
) -> PyResult<PyArithmaticValue<Complex64>> {
) -> PyResult<PyArithmeticValue<Complex64>> {
self.op(other, |a, b| inner_pow(b, a, vm), vm)
}

View File

@@ -10,7 +10,7 @@ use crate::{
},
vm::{ReprGuard, VirtualMachine},
IdProtocol, IntoPyObject, ItemProtocol,
PyArithmaticValue::*,
PyArithmeticValue::*,
PyAttributes, PyClassDef, PyClassImpl, PyComparisonValue, PyContext, PyObjectRef, PyRef,
PyResult, PyValue, TryFromObject, TypeProtocol,
};

View File

@@ -5,7 +5,7 @@ use crate::{
function::{OptionalArg, OptionalOption},
slots::{Comparable, Hashable, PyComparisonOp, SlotConstructor},
IdProtocol, IntoPyObject,
PyArithmaticValue::{self, *},
PyArithmeticValue::{self, *},
PyClassImpl, PyComparisonValue, PyContext, PyObjectRef, PyRef, PyResult, PyValue,
TryFromObject, TypeProtocol, VirtualMachine,
};
@@ -220,7 +220,7 @@ impl PyFloat {
other: PyObjectRef,
op: F,
vm: &VirtualMachine,
) -> PyResult<PyArithmaticValue<f64>>
) -> PyResult<PyArithmeticValue<f64>>
where
F: Fn(f64, f64) -> PyResult<f64>,
{
@@ -247,7 +247,7 @@ impl PyFloat {
other: PyObjectRef,
op: F,
vm: &VirtualMachine,
) -> PyResult<PyArithmaticValue<(f64, f64)>>
) -> PyResult<PyArithmeticValue<(f64, f64)>>
where
F: Fn(f64, f64) -> PyResult<(f64, f64)>,
{
@@ -259,7 +259,7 @@ impl PyFloat {
#[pymethod(name = "__radd__")]
#[pymethod(magic)]
fn add(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult<PyArithmaticValue<f64>> {
fn add(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult<PyArithmeticValue<f64>> {
self.simple_op(other, |a, b| Ok(a + b), vm)
}
@@ -273,7 +273,7 @@ impl PyFloat {
&self,
other: PyObjectRef,
vm: &VirtualMachine,
) -> PyResult<PyArithmaticValue<(f64, f64)>> {
) -> PyResult<PyArithmeticValue<(f64, f64)>> {
self.tuple_op(other, |a, b| inner_divmod(a, b, vm), vm)
}
@@ -282,7 +282,7 @@ impl PyFloat {
&self,
other: PyObjectRef,
vm: &VirtualMachine,
) -> PyResult<PyArithmaticValue<(f64, f64)>> {
) -> PyResult<PyArithmeticValue<(f64, f64)>> {
self.tuple_op(other, |a, b| inner_divmod(b, a, vm), vm)
}
@@ -291,7 +291,7 @@ impl PyFloat {
&self,
other: PyObjectRef,
vm: &VirtualMachine,
) -> PyResult<PyArithmaticValue<f64>> {
) -> PyResult<PyArithmeticValue<f64>> {
self.simple_op(other, |a, b| inner_floordiv(a, b, vm), vm)
}
@@ -300,17 +300,17 @@ impl PyFloat {
&self,
other: PyObjectRef,
vm: &VirtualMachine,
) -> PyResult<PyArithmaticValue<f64>> {
) -> PyResult<PyArithmeticValue<f64>> {
self.simple_op(other, |a, b| inner_floordiv(b, a, vm), vm)
}
#[pymethod(name = "__mod__")]
fn mod_(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult<PyArithmaticValue<f64>> {
fn mod_(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult<PyArithmeticValue<f64>> {
self.simple_op(other, |a, b| inner_mod(a, b, vm), vm)
}
#[pymethod(magic)]
fn rmod(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult<PyArithmaticValue<f64>> {
fn rmod(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult<PyArithmeticValue<f64>> {
self.simple_op(other, |a, b| inner_mod(b, a, vm), vm)
}
@@ -344,12 +344,12 @@ impl PyFloat {
}
#[pymethod(magic)]
fn sub(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult<PyArithmaticValue<f64>> {
fn sub(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult<PyArithmeticValue<f64>> {
self.simple_op(other, |a, b| Ok(a - b), vm)
}
#[pymethod(magic)]
fn rsub(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult<PyArithmaticValue<f64>> {
fn rsub(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult<PyArithmeticValue<f64>> {
self.simple_op(other, |a, b| Ok(b - a), vm)
}
@@ -359,7 +359,7 @@ impl PyFloat {
}
#[pymethod(magic)]
fn truediv(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult<PyArithmaticValue<f64>> {
fn truediv(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult<PyArithmeticValue<f64>> {
self.simple_op(other, |a, b| inner_div(a, b, vm), vm)
}
@@ -368,13 +368,13 @@ impl PyFloat {
&self,
other: PyObjectRef,
vm: &VirtualMachine,
) -> PyResult<PyArithmaticValue<f64>> {
) -> PyResult<PyArithmeticValue<f64>> {
self.simple_op(other, |a, b| inner_div(b, a, vm), vm)
}
#[pymethod(name = "__rmul__")]
#[pymethod(magic)]
fn mul(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult<PyArithmaticValue<f64>> {
fn mul(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult<PyArithmeticValue<f64>> {
self.simple_op(other, |a, b| Ok(a * b), vm)
}

View File

@@ -5,7 +5,7 @@ use crate::{
format::FormatSpec,
function::{OptionalArg, OptionalOption},
slots::{Comparable, Hashable, PyComparisonOp, SlotConstructor},
try_value_from_borrowed_object, IdProtocol, IntoPyObject, IntoPyResult, PyArithmaticValue,
try_value_from_borrowed_object, IdProtocol, IntoPyObject, IntoPyResult, PyArithmeticValue,
PyClassImpl, PyComparisonValue, PyContext, PyObjectRef, PyRef, PyResult, PyValue,
TryFromBorrowedObject, TypeProtocol, VirtualMachine,
};
@@ -310,14 +310,14 @@ impl PyInt {
}
#[inline]
fn int_op<F>(&self, other: PyObjectRef, op: F, vm: &VirtualMachine) -> PyArithmaticValue<BigInt>
fn int_op<F>(&self, other: PyObjectRef, op: F, vm: &VirtualMachine) -> PyArithmeticValue<BigInt>
where
F: Fn(&BigInt, &BigInt) -> BigInt,
{
let r = other
.payload_if_subclass::<PyInt>(vm)
.map(|other| op(&self.value, &other.value));
PyArithmaticValue::from_option(r)
PyArithmeticValue::from_option(r)
}
#[inline]
@@ -337,23 +337,23 @@ impl PyInt {
impl PyInt {
#[pymethod(name = "__radd__")]
#[pymethod(magic)]
fn add(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyArithmaticValue<BigInt> {
fn add(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyArithmeticValue<BigInt> {
self.int_op(other, |a, b| a + b, vm)
}
#[pymethod(magic)]
fn sub(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyArithmaticValue<BigInt> {
fn sub(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyArithmeticValue<BigInt> {
self.int_op(other, |a, b| a - b, vm)
}
#[pymethod(magic)]
fn rsub(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyArithmaticValue<BigInt> {
fn rsub(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyArithmeticValue<BigInt> {
self.int_op(other, |a, b| b - a, vm)
}
#[pymethod(name = "__rmul__")]
#[pymethod(magic)]
fn mul(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyArithmaticValue<BigInt> {
fn mul(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyArithmeticValue<BigInt> {
self.int_op(other, |a, b| a * b, vm)
}
@@ -399,19 +399,19 @@ impl PyInt {
#[pymethod(name = "__rxor__")]
#[pymethod(magic)]
pub fn xor(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyArithmaticValue<BigInt> {
pub fn xor(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyArithmeticValue<BigInt> {
self.int_op(other, |a, b| a ^ b, vm)
}
#[pymethod(name = "__ror__")]
#[pymethod(magic)]
pub fn or(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyArithmaticValue<BigInt> {
pub fn or(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyArithmeticValue<BigInt> {
self.int_op(other, |a, b| a | b, vm)
}
#[pymethod(name = "__rand__")]
#[pymethod(magic)]
pub fn and(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyArithmaticValue<BigInt> {
pub fn and(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyArithmeticValue<BigInt> {
self.int_op(other, |a, b| a & b, vm)
}

View File

@@ -2,7 +2,7 @@ use super::{PyDict, PyDictRef, PyList, PyStr, PyStrRef, PyType, PyTypeRef};
use crate::common::hash::PyHash;
use crate::{
function::FuncArgs, slots::PyComparisonOp, utils::Either, IdProtocol, ItemProtocol,
PyArithmaticValue, PyAttributes, PyClassImpl, PyComparisonValue, PyContext, PyObject,
PyArithmeticValue, PyAttributes, PyClassImpl, PyComparisonValue, PyContext, PyObject,
PyObjectRef, PyResult, PyValue, TypeProtocol, VirtualMachine,
};
@@ -68,7 +68,7 @@ impl PyBaseObject {
.mro_find_map(|cls| cls.slots.richcompare.load())
.unwrap();
let value = match cmp(zelf, other, PyComparisonOp::Eq, vm)? {
Either::A(obj) => PyArithmaticValue::from_object(vm, obj)
Either::A(obj) => PyArithmeticValue::from_object(vm, obj)
.map(|obj| obj.try_to_bool(vm))
.transpose()?,
Either::B(value) => value,

View File

@@ -15,7 +15,7 @@ use crate::{
},
utils::Either,
vm::{ReprGuard, VirtualMachine},
IdProtocol, IntoPyObject, PyArithmaticValue, PyClassDef, PyClassImpl, PyComparisonValue,
IdProtocol, IntoPyObject, PyArithmeticValue, PyClassDef, PyClassImpl, PyComparisonValue,
PyContext, PyObjectRef, PyRef, PyResult, PyValue, TransmuteFromObject, TryFromObject,
TypeProtocol,
};
@@ -129,7 +129,7 @@ impl PyTuple {
zelf: PyRef<Self>,
other: PyObjectRef,
vm: &VirtualMachine,
) -> PyArithmaticValue<PyRef<Self>> {
) -> PyArithmeticValue<PyRef<Self>> {
let added = other.downcast::<Self>().map(|other| {
if other.elements.is_empty() && zelf.class().is(&vm.ctx.types.tuple_type) {
zelf
@@ -145,7 +145,7 @@ impl PyTuple {
Self { elements }.into_ref(vm)
}
});
PyArithmaticValue::from_option(added.ok())
PyArithmeticValue::from_option(added.ok())
}
#[pymethod(magic)]

View File

@@ -210,7 +210,7 @@ macro_rules! class_or_notimplemented {
($t:ty, $obj:expr) => {
match $crate::PyObjectRef::downcast_ref::<$t>($obj) {
Some(pyref) => pyref,
None => return Ok($crate::PyArithmaticValue::NotImplemented),
None => return Ok($crate::PyArithmeticValue::NotImplemented),
}
};
}

View File

@@ -1080,9 +1080,9 @@ pub trait PyStructSequence: StaticType + PyClassImpl + Sized + 'static {
}
}
result_like::option_like!(pub PyArithmaticValue, Implemented, NotImplemented);
result_like::option_like!(pub PyArithmeticValue, Implemented, NotImplemented);
impl PyArithmaticValue<PyObjectRef> {
impl PyArithmeticValue<PyObjectRef> {
pub fn from_object(vm: &VirtualMachine, obj: PyObjectRef) -> Self {
if obj.is(&vm.ctx.not_implemented) {
Self::NotImplemented
@@ -1092,27 +1092,27 @@ impl PyArithmaticValue<PyObjectRef> {
}
}
impl<T: TryFromObject> TryFromObject for PyArithmaticValue<T> {
impl<T: TryFromObject> TryFromObject for PyArithmeticValue<T> {
fn try_from_object(vm: &VirtualMachine, obj: PyObjectRef) -> PyResult<Self> {
PyArithmaticValue::from_object(vm, obj)
PyArithmeticValue::from_object(vm, obj)
.map(|x| T::try_from_object(vm, x))
.transpose()
}
}
impl<T> IntoPyObject for PyArithmaticValue<T>
impl<T> IntoPyObject for PyArithmeticValue<T>
where
T: IntoPyObject,
{
fn into_pyobject(self, vm: &VirtualMachine) -> PyObjectRef {
match self {
PyArithmaticValue::Implemented(v) => v.into_pyobject(vm),
PyArithmaticValue::NotImplemented => vm.ctx.not_implemented(),
PyArithmeticValue::Implemented(v) => v.into_pyobject(vm),
PyArithmeticValue::NotImplemented => vm.ctx.not_implemented(),
}
}
}
pub type PyComparisonValue = PyArithmaticValue<bool>;
pub type PyComparisonValue = PyArithmeticValue<bool>;
#[derive(Clone)]
pub struct PySequence<T = PyObjectRef>(Vec<T>);

View File

@@ -32,7 +32,7 @@ mod builtins {
slots::PyComparisonOp,
stdlib::sys,
utils::Either,
IdProtocol, ItemProtocol, PyArithmaticValue, PyClassImpl, PyObjectRef, PyRef, PyResult,
IdProtocol, ItemProtocol, PyArithmeticValue, PyClassImpl, PyObjectRef, PyRef, PyResult,
PyValue, TryFromObject, TypeProtocol, VirtualMachine,
};
use num_traits::{Signed, Zero};
@@ -600,8 +600,8 @@ mod builtins {
Ok(x) => x,
Err(e) => return Some(Err(e)),
};
if let PyArithmaticValue::Implemented(x) =
PyArithmaticValue::from_object(vm, result)
if let PyArithmeticValue::Implemented(x) =
PyArithmeticValue::from_object(vm, result)
{
return Some(Ok(x));
}

View File

@@ -28,7 +28,7 @@ use crate::{
slots::PyComparisonOp,
stdlib,
utils::Either,
IdProtocol, IntoPyObject, ItemProtocol, PyArithmaticValue, PyContext, PyLease, PyMethod,
IdProtocol, IntoPyObject, ItemProtocol, PyArithmeticValue, PyContext, PyLease, PyMethod,
PyObject, PyObjectRef, PyRef, PyRefExact, PyResult, PyValue, TryFromObject, TryIntoRef,
TypeProtocol,
};
@@ -1450,7 +1450,7 @@ impl VirtualMachine {
if let Some(method_or_err) = self.get_method(obj.clone(), method) {
let method = method_or_err?;
let result = self.invoke(&method, (arg.clone(),))?;
if let PyArithmaticValue::Implemented(x) = PyArithmaticValue::from_object(self, result)
if let PyArithmeticValue::Implemented(x) = PyArithmeticValue::from_object(self, result)
{
return Ok(x);
}
@@ -1832,7 +1832,7 @@ impl VirtualMachine {
.mro_find_map(|cls| cls.slots.richcompare.load())
.unwrap();
Ok(match cmp(obj, other, op, self)? {
Either::A(obj) => PyArithmaticValue::from_object(self, obj).map(Either::A),
Either::A(obj) => PyArithmeticValue::from_object(self, obj).map(Either::A),
Either::B(arithmetic) => arithmetic.map(Either::B),
})
};
@@ -1846,16 +1846,16 @@ impl VirtualMachine {
if is_strict_subclass {
let res = call_cmp(w, v, swapped)?;
checked_reverse_op = true;
if let PyArithmaticValue::Implemented(x) = res {
if let PyArithmeticValue::Implemented(x) = res {
return Ok(x);
}
}
if let PyArithmaticValue::Implemented(x) = call_cmp(v, w, op)? {
if let PyArithmeticValue::Implemented(x) = call_cmp(v, w, op)? {
return Ok(x);
}
if !checked_reverse_op {
let res = call_cmp(w, v, swapped)?;
if let PyArithmaticValue::Implemented(x) = res {
if let PyArithmeticValue::Implemented(x) = res {
return Ok(x);
}
}