Allowing wrapping math.
This commit is contained in:
@@ -45,22 +45,11 @@ pub enum PrimOpError {
|
||||
macro_rules! run_op {
|
||||
($op: ident, $left: expr, $right: expr) => {
|
||||
match $op {
|
||||
"+" => $left
|
||||
.checked_add($right)
|
||||
.ok_or(PrimOpError::MathFailure("+"))
|
||||
.map(Into::into),
|
||||
"-" => $left
|
||||
.checked_sub($right)
|
||||
.ok_or(PrimOpError::MathFailure("-"))
|
||||
.map(Into::into),
|
||||
"*" => $left
|
||||
.checked_mul($right)
|
||||
.ok_or(PrimOpError::MathFailure("*"))
|
||||
.map(Into::into),
|
||||
"/" => $left
|
||||
.checked_div($right)
|
||||
.ok_or(PrimOpError::MathFailure("/"))
|
||||
.map(Into::into),
|
||||
"+" => Ok($left.wrapping_add($right).into()),
|
||||
"-" => Ok($left.wrapping_sub($right).into()),
|
||||
"*" => Ok($left.wrapping_mul($right).into()),
|
||||
"/" if $right == 0 => Err(PrimOpError::MathFailure("/")),
|
||||
"/" => Ok($left.wrapping_div($right).into()),
|
||||
_ => Err(PrimOpError::UnknownPrimOp($op.to_string())),
|
||||
}
|
||||
};
|
||||
@@ -70,10 +59,10 @@ impl Value {
|
||||
fn unary_op(operation: &str, value: &Value) -> Result<Value, PrimOpError> {
|
||||
match operation {
|
||||
"-" => match value {
|
||||
Value::I8(x) => Ok(Value::I8(-x)),
|
||||
Value::I16(x) => Ok(Value::I16(-x)),
|
||||
Value::I32(x) => Ok(Value::I32(-x)),
|
||||
Value::I64(x) => Ok(Value::I64(-x)),
|
||||
Value::I8(x) => Ok(Value::I8(x.wrapping_neg())),
|
||||
Value::I16(x) => Ok(Value::I16(x.wrapping_neg())),
|
||||
Value::I32(x) => Ok(Value::I32(x.wrapping_neg())),
|
||||
Value::I64(x) => Ok(Value::I64(x.wrapping_neg())),
|
||||
_ => Err(PrimOpError::BadTypeFor("-", value.clone())),
|
||||
},
|
||||
_ => Err(PrimOpError::BadArgCount(operation.to_owned(), 1)),
|
||||
|
||||
Reference in New Issue
Block a user