1
0

Implement subtraction

This commit is contained in:
Jeff 2023-09-29 06:02:46 -04:00
parent 91745af149
commit 9994919127
2 changed files with 49 additions and 3 deletions

View File

@ -276,6 +276,7 @@ impl Operation {
let right = self.right.run(context, &mut cursor, source)?; let right = self.right.run(context, &mut cursor, source)?;
let result = match self.operator.as_str() { let result = match self.operator.as_str() {
"+" => left + right, "+" => left + right,
"-" => left - right,
"=" => { "=" => {
if let Expression::Identifier(key) = &self.left { if let Expression::Identifier(key) = &self.left {
context.set_value(key, right)?; context.set_value(key, right)?;

View File

@ -17,7 +17,7 @@ use std::{
convert::TryFrom, convert::TryFrom,
fmt::{self, Display, Formatter}, fmt::{self, Display, Formatter},
marker::PhantomData, marker::PhantomData,
ops::Add, ops::{Add, Sub},
}; };
pub mod function; pub mod function;
@ -256,8 +256,8 @@ impl Value {
impl Add for Value { impl Add for Value {
type Output = Result<Value>; type Output = Result<Value>;
fn add(self, right: Self) -> Self::Output { fn add(self, other: Self) -> Self::Output {
match (self, right) { match (self, other) {
(Value::String(left), Value::String(right)) => { (Value::String(left), Value::String(right)) => {
let concatenated = left + &right; let concatenated = left + &right;
@ -298,6 +298,51 @@ impl Add for Value {
} }
} }
impl Sub for Value {
type Output = Result<Self>;
fn sub(self, other: Self) -> Self::Output {
match (&self, &other) {
(Value::String(_), Value::String(_)) => Err(Error::ExpectedNumber {
actual: self.clone(),
}),
(Value::String(_), other) | (other, Value::String(_)) => Err(Error::ExpectedNumber {
actual: other.clone(),
}),
(Value::Float(left), Value::Float(right)) => {
let addition = left - right;
Ok(Value::Float(addition))
}
(Value::Float(_), other) | (other, Value::Float(_)) => Err(Error::ExpectedFloat {
actual: other.clone(),
}),
(Value::Integer(left), Value::Integer(right)) => Ok(Value::Integer(left - right)),
(Value::Integer(_), other) | (other, Value::Integer(_)) => Err(Error::ExpectedInt {
actual: other.clone(),
}),
(Value::Boolean(_), Value::Boolean(_)) => todo!(),
(Value::Boolean(_), other) | (other, Value::Boolean(_)) => {
Err(Error::ExpectedBoolean {
actual: other.clone(),
})
}
(Value::List(_), Value::List(_)) => todo!(),
(Value::List(_), other) | (other, Value::List(_)) => Err(Error::ExpectedList {
actual: other.clone(),
}),
(Value::Map(_), Value::Map(_)) => todo!(),
(Value::Map(_), other) | (other, Value::Map(_)) => Err(Error::ExpectedMap {
actual: other.clone(),
}),
(Value::Empty, Value::Empty) => Ok(Value::Empty),
_ => Err(Error::CustomMessage(
"Cannot add the given types.".to_string(),
)),
}
}
}
impl Eq for Value {} impl Eq for Value {}
impl PartialOrd for Value { impl PartialOrd for Value {