From fbd3641f5d2c6da79549c9b6ce6f240acfd44a1f Mon Sep 17 00:00:00 2001 From: jfh Date: Sat, 2 Oct 2021 03:42:59 +0300 Subject: [PATCH] Arithmatic into Arithmetic. --- vm/src/builtins/complex.rs | 20 ++++++++++---------- vm/src/builtins/dict.rs | 2 +- vm/src/builtins/float.rs | 30 +++++++++++++++--------------- vm/src/builtins/int.rs | 20 ++++++++++---------- vm/src/builtins/object.rs | 4 ++-- vm/src/builtins/tuple.rs | 6 +++--- vm/src/macros.rs | 2 +- vm/src/pyobject.rs | 16 ++++++++-------- vm/src/stdlib/builtins.rs | 6 +++--- vm/src/vm.rs | 12 ++++++------ 10 files changed, 59 insertions(+), 59 deletions(-) diff --git a/vm/src/builtins/complex.rs b/vm/src/builtins/complex.rs index 4d683cea2..dc87963d3 100644 --- a/vm/src/builtins/complex.rs +++ b/vm/src/builtins/complex.rs @@ -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> + ) -> PyResult> where F: Fn(Complex64, Complex64) -> PyResult, { @@ -209,7 +209,7 @@ impl PyComplex { &self, other: PyObjectRef, vm: &VirtualMachine, - ) -> PyResult> { + ) -> PyResult> { self.op(other, |a, b| Ok(a + b), vm) } @@ -218,7 +218,7 @@ impl PyComplex { &self, other: PyObjectRef, vm: &VirtualMachine, - ) -> PyResult> { + ) -> PyResult> { self.op(other, |a, b| Ok(a - b), vm) } @@ -227,7 +227,7 @@ impl PyComplex { &self, other: PyObjectRef, vm: &VirtualMachine, - ) -> PyResult> { + ) -> PyResult> { self.op(other, |a, b| Ok(b - a), vm) } @@ -252,7 +252,7 @@ impl PyComplex { &self, other: PyObjectRef, vm: &VirtualMachine, - ) -> PyResult> { + ) -> PyResult> { self.op(other, |a, b| Ok(a * b), vm) } @@ -261,7 +261,7 @@ impl PyComplex { &self, other: PyObjectRef, vm: &VirtualMachine, - ) -> PyResult> { + ) -> PyResult> { self.op(other, |a, b| inner_div(a, b, vm), vm) } @@ -270,7 +270,7 @@ impl PyComplex { &self, other: PyObjectRef, vm: &VirtualMachine, - ) -> PyResult> { + ) -> PyResult> { self.op(other, |a, b| inner_div(b, a, vm), vm) } @@ -318,7 +318,7 @@ impl PyComplex { other: PyObjectRef, mod_val: OptionalOption, vm: &VirtualMachine, - ) -> PyResult> { + ) -> PyResult> { 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> { + ) -> PyResult> { self.op(other, |a, b| inner_pow(b, a, vm), vm) } diff --git a/vm/src/builtins/dict.rs b/vm/src/builtins/dict.rs index 31ab82f10..c35b100c7 100644 --- a/vm/src/builtins/dict.rs +++ b/vm/src/builtins/dict.rs @@ -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, }; diff --git a/vm/src/builtins/float.rs b/vm/src/builtins/float.rs index 57a78b241..94f75201d 100644 --- a/vm/src/builtins/float.rs +++ b/vm/src/builtins/float.rs @@ -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> + ) -> PyResult> where F: Fn(f64, f64) -> PyResult, { @@ -247,7 +247,7 @@ impl PyFloat { other: PyObjectRef, op: F, vm: &VirtualMachine, - ) -> PyResult> + ) -> PyResult> 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> { + fn add(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult> { self.simple_op(other, |a, b| Ok(a + b), vm) } @@ -273,7 +273,7 @@ impl PyFloat { &self, other: PyObjectRef, vm: &VirtualMachine, - ) -> PyResult> { + ) -> PyResult> { self.tuple_op(other, |a, b| inner_divmod(a, b, vm), vm) } @@ -282,7 +282,7 @@ impl PyFloat { &self, other: PyObjectRef, vm: &VirtualMachine, - ) -> PyResult> { + ) -> PyResult> { self.tuple_op(other, |a, b| inner_divmod(b, a, vm), vm) } @@ -291,7 +291,7 @@ impl PyFloat { &self, other: PyObjectRef, vm: &VirtualMachine, - ) -> PyResult> { + ) -> PyResult> { self.simple_op(other, |a, b| inner_floordiv(a, b, vm), vm) } @@ -300,17 +300,17 @@ impl PyFloat { &self, other: PyObjectRef, vm: &VirtualMachine, - ) -> PyResult> { + ) -> PyResult> { self.simple_op(other, |a, b| inner_floordiv(b, a, vm), vm) } #[pymethod(name = "__mod__")] - fn mod_(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult> { + fn mod_(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult> { self.simple_op(other, |a, b| inner_mod(a, b, vm), vm) } #[pymethod(magic)] - fn rmod(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult> { + fn rmod(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult> { 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> { + fn sub(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult> { self.simple_op(other, |a, b| Ok(a - b), vm) } #[pymethod(magic)] - fn rsub(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult> { + fn rsub(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult> { 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> { + fn truediv(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult> { self.simple_op(other, |a, b| inner_div(a, b, vm), vm) } @@ -368,13 +368,13 @@ impl PyFloat { &self, other: PyObjectRef, vm: &VirtualMachine, - ) -> PyResult> { + ) -> PyResult> { 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> { + fn mul(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyResult> { self.simple_op(other, |a, b| Ok(a * b), vm) } diff --git a/vm/src/builtins/int.rs b/vm/src/builtins/int.rs index e344357a9..5798ba6b1 100644 --- a/vm/src/builtins/int.rs +++ b/vm/src/builtins/int.rs @@ -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(&self, other: PyObjectRef, op: F, vm: &VirtualMachine) -> PyArithmaticValue + fn int_op(&self, other: PyObjectRef, op: F, vm: &VirtualMachine) -> PyArithmeticValue where F: Fn(&BigInt, &BigInt) -> BigInt, { let r = other .payload_if_subclass::(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 { + fn add(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyArithmeticValue { self.int_op(other, |a, b| a + b, vm) } #[pymethod(magic)] - fn sub(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyArithmaticValue { + fn sub(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyArithmeticValue { self.int_op(other, |a, b| a - b, vm) } #[pymethod(magic)] - fn rsub(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyArithmaticValue { + fn rsub(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyArithmeticValue { self.int_op(other, |a, b| b - a, vm) } #[pymethod(name = "__rmul__")] #[pymethod(magic)] - fn mul(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyArithmaticValue { + fn mul(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyArithmeticValue { 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 { + pub fn xor(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyArithmeticValue { self.int_op(other, |a, b| a ^ b, vm) } #[pymethod(name = "__ror__")] #[pymethod(magic)] - pub fn or(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyArithmaticValue { + pub fn or(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyArithmeticValue { self.int_op(other, |a, b| a | b, vm) } #[pymethod(name = "__rand__")] #[pymethod(magic)] - pub fn and(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyArithmaticValue { + pub fn and(&self, other: PyObjectRef, vm: &VirtualMachine) -> PyArithmeticValue { self.int_op(other, |a, b| a & b, vm) } diff --git a/vm/src/builtins/object.rs b/vm/src/builtins/object.rs index 098fbcaf8..7cfa6242c 100644 --- a/vm/src/builtins/object.rs +++ b/vm/src/builtins/object.rs @@ -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, diff --git a/vm/src/builtins/tuple.rs b/vm/src/builtins/tuple.rs index f733f222a..03c11d4e5 100644 --- a/vm/src/builtins/tuple.rs +++ b/vm/src/builtins/tuple.rs @@ -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, other: PyObjectRef, vm: &VirtualMachine, - ) -> PyArithmaticValue> { + ) -> PyArithmeticValue> { let added = other.downcast::().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)] diff --git a/vm/src/macros.rs b/vm/src/macros.rs index 085856073..7a57a99e0 100644 --- a/vm/src/macros.rs +++ b/vm/src/macros.rs @@ -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), } }; } diff --git a/vm/src/pyobject.rs b/vm/src/pyobject.rs index 0eabfe0d7..514a88823 100644 --- a/vm/src/pyobject.rs +++ b/vm/src/pyobject.rs @@ -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 { +impl PyArithmeticValue { pub fn from_object(vm: &VirtualMachine, obj: PyObjectRef) -> Self { if obj.is(&vm.ctx.not_implemented) { Self::NotImplemented @@ -1092,27 +1092,27 @@ impl PyArithmaticValue { } } -impl TryFromObject for PyArithmaticValue { +impl TryFromObject for PyArithmeticValue { fn try_from_object(vm: &VirtualMachine, obj: PyObjectRef) -> PyResult { - PyArithmaticValue::from_object(vm, obj) + PyArithmeticValue::from_object(vm, obj) .map(|x| T::try_from_object(vm, x)) .transpose() } } -impl IntoPyObject for PyArithmaticValue +impl IntoPyObject for PyArithmeticValue 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; +pub type PyComparisonValue = PyArithmeticValue; #[derive(Clone)] pub struct PySequence(Vec); diff --git a/vm/src/stdlib/builtins.rs b/vm/src/stdlib/builtins.rs index 879bf4c68..5a0c88021 100644 --- a/vm/src/stdlib/builtins.rs +++ b/vm/src/stdlib/builtins.rs @@ -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)); } diff --git a/vm/src/vm.rs b/vm/src/vm.rs index 5575def1d..f1b64ae3f 100644 --- a/vm/src/vm.rs +++ b/vm/src/vm.rs @@ -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); } }