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