rhai/src/packages/arithmetic.rs

365 lines
12 KiB
Rust
Raw Normal View History

2020-04-21 17:01:10 +02:00
use crate::def_package;
2020-05-13 13:21:42 +02:00
use crate::module::FuncReturn;
use crate::parser::INT;
use crate::result::EvalAltResult;
use crate::token::Position;
#[cfg(not(feature = "no_float"))]
use crate::parser::FLOAT;
use num_traits::{
identities::Zero, CheckedAdd, CheckedDiv, CheckedMul, CheckedNeg, CheckedRem, CheckedShl,
CheckedShr, CheckedSub,
};
use crate::stdlib::{
2020-04-24 06:39:24 +02:00
boxed::Box,
fmt::Display,
format,
2020-04-21 17:01:10 +02:00
ops::{Add, BitAnd, BitOr, BitXor, Div, Mul, Neg, Rem, Shl, Shr, Sub},
};
// Checked add
2020-05-23 12:59:28 +02:00
pub(crate) fn add<T: Display + CheckedAdd>(x: T, y: T) -> FuncReturn<T> {
x.checked_add(&y).ok_or_else(|| {
Box::new(EvalAltResult::ErrorArithmetic(
format!("Addition overflow: {} + {}", x, y),
Position::none(),
))
})
}
// Checked subtract
2020-05-23 12:59:28 +02:00
pub(crate) fn sub<T: Display + CheckedSub>(x: T, y: T) -> FuncReturn<T> {
x.checked_sub(&y).ok_or_else(|| {
Box::new(EvalAltResult::ErrorArithmetic(
format!("Subtraction underflow: {} - {}", x, y),
Position::none(),
))
})
}
// Checked multiply
2020-05-23 12:59:28 +02:00
pub(crate) fn mul<T: Display + CheckedMul>(x: T, y: T) -> FuncReturn<T> {
x.checked_mul(&y).ok_or_else(|| {
Box::new(EvalAltResult::ErrorArithmetic(
format!("Multiplication overflow: {} * {}", x, y),
Position::none(),
))
})
}
// Checked divide
2020-05-23 12:59:28 +02:00
pub(crate) fn div<T>(x: T, y: T) -> FuncReturn<T>
where
T: Display + CheckedDiv + PartialEq + Zero,
{
// Detect division by zero
if y == T::zero() {
return Err(Box::new(EvalAltResult::ErrorArithmetic(
format!("Division by zero: {} / {}", x, y),
Position::none(),
)));
}
x.checked_div(&y).ok_or_else(|| {
Box::new(EvalAltResult::ErrorArithmetic(
format!("Division overflow: {} / {}", x, y),
Position::none(),
))
})
}
// Checked negative - e.g. -(i32::MIN) will overflow i32::MAX
2020-05-23 12:59:28 +02:00
pub(crate) fn neg<T: Display + CheckedNeg>(x: T) -> FuncReturn<T> {
x.checked_neg().ok_or_else(|| {
Box::new(EvalAltResult::ErrorArithmetic(
format!("Negation overflow: -{}", x),
Position::none(),
))
})
}
// Checked absolute
2020-05-23 12:59:28 +02:00
pub(crate) fn abs<T: Display + CheckedNeg + PartialOrd + Zero>(x: T) -> FuncReturn<T> {
// FIX - We don't use Signed::abs() here because, contrary to documentation, it panics
// when the number is ::MIN instead of returning ::MIN itself.
if x >= <T as Zero>::zero() {
Ok(x)
} else {
x.checked_neg().ok_or_else(|| {
Box::new(EvalAltResult::ErrorArithmetic(
format!("Negation overflow: -{}", x),
Position::none(),
))
})
}
}
// Unchecked add - may panic on overflow
2020-05-13 13:21:42 +02:00
fn add_u<T: Add>(x: T, y: T) -> FuncReturn<<T as Add>::Output> {
Ok(x + y)
}
// Unchecked subtract - may panic on underflow
2020-05-13 13:21:42 +02:00
fn sub_u<T: Sub>(x: T, y: T) -> FuncReturn<<T as Sub>::Output> {
Ok(x - y)
}
// Unchecked multiply - may panic on overflow
2020-05-13 13:21:42 +02:00
fn mul_u<T: Mul>(x: T, y: T) -> FuncReturn<<T as Mul>::Output> {
Ok(x * y)
}
// Unchecked divide - may panic when dividing by zero
2020-05-13 13:21:42 +02:00
fn div_u<T: Div>(x: T, y: T) -> FuncReturn<<T as Div>::Output> {
Ok(x / y)
}
// Unchecked negative - may panic on overflow
2020-05-13 13:21:42 +02:00
fn neg_u<T: Neg>(x: T) -> FuncReturn<<T as Neg>::Output> {
Ok(-x)
}
// Unchecked absolute - may panic on overflow
2020-05-13 13:21:42 +02:00
fn abs_u<T>(x: T) -> FuncReturn<<T as Neg>::Output>
where
T: Neg + PartialOrd + Default + Into<<T as Neg>::Output>,
{
// Numbers should default to zero
if x < Default::default() {
2020-05-13 13:21:42 +02:00
Ok(-x)
} else {
2020-05-13 13:21:42 +02:00
Ok(x.into())
}
}
// Bit operators
2020-05-13 13:21:42 +02:00
fn binary_and<T: BitAnd>(x: T, y: T) -> FuncReturn<<T as BitAnd>::Output> {
Ok(x & y)
}
2020-05-13 13:21:42 +02:00
fn binary_or<T: BitOr>(x: T, y: T) -> FuncReturn<<T as BitOr>::Output> {
Ok(x | y)
}
2020-05-13 13:21:42 +02:00
fn binary_xor<T: BitXor>(x: T, y: T) -> FuncReturn<<T as BitXor>::Output> {
Ok(x ^ y)
}
// Checked left-shift
2020-05-23 12:59:28 +02:00
pub(crate) fn shl<T: Display + CheckedShl>(x: T, y: INT) -> FuncReturn<T> {
// Cannot shift by a negative number of bits
if y < 0 {
return Err(Box::new(EvalAltResult::ErrorArithmetic(
format!("Left-shift by a negative number: {} << {}", x, y),
Position::none(),
)));
}
CheckedShl::checked_shl(&x, y as u32).ok_or_else(|| {
Box::new(EvalAltResult::ErrorArithmetic(
format!("Left-shift by too many bits: {} << {}", x, y),
Position::none(),
))
})
}
// Checked right-shift
2020-05-23 12:59:28 +02:00
pub(crate) fn shr<T: Display + CheckedShr>(x: T, y: INT) -> FuncReturn<T> {
// Cannot shift by a negative number of bits
if y < 0 {
return Err(Box::new(EvalAltResult::ErrorArithmetic(
format!("Right-shift by a negative number: {} >> {}", x, y),
Position::none(),
)));
}
CheckedShr::checked_shr(&x, y as u32).ok_or_else(|| {
Box::new(EvalAltResult::ErrorArithmetic(
format!("Right-shift by too many bits: {} % {}", x, y),
Position::none(),
))
})
}
// Unchecked left-shift - may panic if shifting by a negative number of bits
2020-05-23 12:59:28 +02:00
pub(crate) fn shl_u<T: Shl<T>>(x: T, y: T) -> FuncReturn<<T as Shl<T>>::Output> {
2020-05-13 13:21:42 +02:00
Ok(x.shl(y))
}
// Unchecked right-shift - may panic if shifting by a negative number of bits
2020-05-23 12:59:28 +02:00
pub(crate) fn shr_u<T: Shr<T>>(x: T, y: T) -> FuncReturn<<T as Shr<T>>::Output> {
2020-05-13 13:21:42 +02:00
Ok(x.shr(y))
}
// Checked modulo
2020-05-23 12:59:28 +02:00
pub(crate) fn modulo<T: Display + CheckedRem>(x: T, y: T) -> FuncReturn<T> {
x.checked_rem(&y).ok_or_else(|| {
Box::new(EvalAltResult::ErrorArithmetic(
format!("Modulo division by zero or overflow: {} % {}", x, y),
Position::none(),
))
})
}
// Unchecked modulo - may panic if dividing by zero
2020-05-13 13:21:42 +02:00
fn modulo_u<T: Rem>(x: T, y: T) -> FuncReturn<<T as Rem>::Output> {
Ok(x % y)
}
// Checked power
2020-05-23 12:59:28 +02:00
pub(crate) fn pow_i_i(x: INT, y: INT) -> FuncReturn<INT> {
#[cfg(not(feature = "only_i32"))]
{
if y > (u32::MAX as INT) {
Err(Box::new(EvalAltResult::ErrorArithmetic(
format!("Integer raised to too large an index: {} ~ {}", x, y),
Position::none(),
)))
} else if y < 0 {
Err(Box::new(EvalAltResult::ErrorArithmetic(
format!("Integer raised to a negative index: {} ~ {}", x, y),
Position::none(),
)))
} else {
x.checked_pow(y as u32).ok_or_else(|| {
Box::new(EvalAltResult::ErrorArithmetic(
format!("Power overflow: {} ~ {}", x, y),
Position::none(),
))
})
}
}
#[cfg(feature = "only_i32")]
{
if y < 0 {
Err(Box::new(EvalAltResult::ErrorArithmetic(
format!("Integer raised to a negative index: {} ~ {}", x, y),
Position::none(),
)))
} else {
x.checked_pow(y as u32).ok_or_else(|| {
Box::new(EvalAltResult::ErrorArithmetic(
format!("Power overflow: {} ~ {}", x, y),
Position::none(),
))
})
}
}
}
// Unchecked integer power - may panic on overflow or if the power index is too high (> u32::MAX)
2020-05-23 12:59:28 +02:00
pub(crate) fn pow_i_i_u(x: INT, y: INT) -> FuncReturn<INT> {
2020-05-13 13:21:42 +02:00
Ok(x.pow(y as u32))
}
// Floating-point power - always well-defined
#[cfg(not(feature = "no_float"))]
2020-05-23 12:59:28 +02:00
pub(crate) fn pow_f_f(x: FLOAT, y: FLOAT) -> FuncReturn<FLOAT> {
2020-05-13 13:21:42 +02:00
Ok(x.powf(y))
}
// Checked power
#[cfg(not(feature = "no_float"))]
2020-05-23 12:59:28 +02:00
pub(crate) fn pow_f_i(x: FLOAT, y: INT) -> FuncReturn<FLOAT> {
// Raise to power that is larger than an i32
if y > (i32::MAX as INT) {
return Err(Box::new(EvalAltResult::ErrorArithmetic(
format!("Number raised to too large an index: {} ~ {}", x, y),
Position::none(),
)));
}
Ok(x.powi(y as i32))
}
// Unchecked power - may be incorrect if the power index is too high (> i32::MAX)
#[cfg(feature = "unchecked")]
#[cfg(not(feature = "no_float"))]
2020-05-23 12:59:28 +02:00
pub(crate) fn pow_f_i_u(x: FLOAT, y: INT) -> FuncReturn<FLOAT> {
2020-05-13 13:21:42 +02:00
Ok(x.powi(y as i32))
}
2020-05-13 13:21:42 +02:00
macro_rules! reg_unary {
($lib:expr, $op:expr, $func:ident, $($par:ty),*) => {
$( $lib.set_fn_1($op, $func::<$par>); )*
};
}
2020-05-13 13:21:42 +02:00
macro_rules! reg_op {
($lib:expr, $op:expr, $func:ident, $($par:ty),*) => {
$( $lib.set_fn_2($op, $func::<$par>); )*
};
}
2020-04-22 08:55:40 +02:00
def_package!(crate:ArithmeticPackage:"Basic arithmetic", lib, {
2020-05-26 08:14:03 +02:00
#[cfg(not(feature = "only_i32"))]
#[cfg(not(feature = "only_i64"))]
2020-04-21 17:01:10 +02:00
{
2020-05-26 08:14:03 +02:00
#[cfg(not(feature = "unchecked"))]
{
2020-05-26 08:14:03 +02:00
// Checked basic arithmetic
2020-05-23 12:59:28 +02:00
reg_op!(lib, "+", add, i8, u8, i16, u16, i32, u32, u64, i128, u128);
reg_op!(lib, "-", sub, i8, u8, i16, u16, i32, u32, u64, i128, u128);
reg_op!(lib, "*", mul, i8, u8, i16, u16, i32, u32, u64, i128, u128);
reg_op!(lib, "/", div, i8, u8, i16, u16, i32, u32, u64, i128, u128);
2020-05-26 08:14:03 +02:00
// Checked bit shifts
reg_op!(lib, "<<", shl, i8, u8, i16, u16, i32, u32, u64, i128, u128);
reg_op!(lib, ">>", shr, i8, u8, i16, u16, i32, u32, u64, i128, u128);
reg_op!(lib, "%", modulo, i8, u8, i16, u16, i32, u32, u64, i128, u128);
}
2020-05-26 08:14:03 +02:00
#[cfg(feature = "unchecked")]
{
2020-05-26 08:14:03 +02:00
// Unchecked basic arithmetic
2020-05-23 12:59:28 +02:00
reg_op!(lib, "+", add_u, i8, u8, i16, u16, i32, u32, u64, i128, u128);
reg_op!(lib, "-", sub_u, i8, u8, i16, u16, i32, u32, u64, i128, u128);
reg_op!(lib, "*", mul_u, i8, u8, i16, u16, i32, u32, u64, i128, u128);
reg_op!(lib, "/", div_u, i8, u8, i16, u16, i32, u32, u64, i128, u128);
2020-05-26 08:14:03 +02:00
// Unchecked bit shifts
reg_op!(lib, "<<", shl_u, i64, i8, u8, i16, u16, i32, u32, u64, i128, u128);
reg_op!(lib, ">>", shr_u, i64, i8, u8, i16, u16, i32, u32, u64, i128, u128);
reg_op!(lib, "%", modulo_u, i8, u8, i16, u16, i32, u32, u64, i128, u128);
}
2020-04-21 17:01:10 +02:00
}
// Basic arithmetic for floating-point - no need to check
#[cfg(not(feature = "no_float"))]
{
2020-05-23 12:59:28 +02:00
reg_op!(lib, "+", add_u, f32);
reg_op!(lib, "-", sub_u, f32);
reg_op!(lib, "*", mul_u, f32);
reg_op!(lib, "/", div_u, f32);
2020-04-21 17:01:10 +02:00
}
#[cfg(not(feature = "only_i32"))]
#[cfg(not(feature = "only_i64"))]
{
2020-05-23 12:59:28 +02:00
reg_op!(lib, "|", binary_or, i8, u8, i16, u16, i32, u32, u64, i128, u128);
reg_op!(lib, "&", binary_and, i8, u8, i16, u16, i32, u32, u64, i128, u128);
reg_op!(lib, "^", binary_xor, i8, u8, i16, u16, i32, u32, u64, i128, u128);
2020-04-21 17:01:10 +02:00
}
2020-05-26 08:14:03 +02:00
#[cfg(not(feature = "no_float"))]
2020-04-21 17:01:10 +02:00
{
2020-05-26 08:14:03 +02:00
// Checked power
#[cfg(not(feature = "unchecked"))]
2020-05-13 13:21:42 +02:00
lib.set_fn_2("~", pow_f_i);
2020-05-26 08:14:03 +02:00
// Unchecked power
#[cfg(feature = "unchecked")]
2020-05-13 13:21:42 +02:00
lib.set_fn_2("~", pow_f_i_u);
2020-05-26 08:14:03 +02:00
// Floating-point modulo and power
2020-05-23 12:59:28 +02:00
reg_op!(lib, "%", modulo_u, f32);
2020-05-26 08:14:03 +02:00
// Floating-point unary
reg_unary!(lib, "-", neg_u, f32, f64);
reg_unary!(lib, "abs", abs_u, f32, f64);
2020-04-21 17:01:10 +02:00
}
// Checked unary
#[cfg(not(feature = "unchecked"))]
{
2020-05-13 13:21:42 +02:00
reg_unary!(lib, "-", neg, INT);
reg_unary!(lib, "abs", abs, INT);
2020-04-21 17:01:10 +02:00
#[cfg(not(feature = "only_i32"))]
#[cfg(not(feature = "only_i64"))]
{
2020-05-13 13:21:42 +02:00
reg_unary!(lib, "-", neg, i8, i16, i32, i64, i128);
reg_unary!(lib, "abs", abs, i8, i16, i32, i64, i128);
}
2020-04-21 17:01:10 +02:00
}
2020-04-21 17:01:10 +02:00
// Unchecked unary
#[cfg(feature = "unchecked")]
{
reg_unary!(lib, "-", neg_u, INT);
reg_unary!(lib, "abs", abs_u, INT);
#[cfg(not(feature = "only_i32"))]
#[cfg(not(feature = "only_i64"))]
{
2020-04-21 17:01:10 +02:00
reg_unary!(lib, "-", neg_u, i8, i16, i32, i64, i128);
reg_unary!(lib, "abs", abs_u, i8, i16, i32, i64, i128);
}
}
2020-04-21 17:01:10 +02:00
});