1
0

Implement string concatenation

This commit is contained in:
Jeff 2023-10-22 17:04:14 -04:00
parent ecdac6fab5
commit c0bafe577e

View File

@ -218,22 +218,30 @@ impl Add for Value {
type Output = Result<Value>;
fn add(self, other: Self) -> Self::Output {
let non_number = match (self, other) {
(Value::Integer(left), Value::Integer(right)) => {
return Ok(Value::Integer(left + right))
match (self.as_int(), other.as_int()) {
(Ok(left), Ok(right)) => return Ok(Value::Integer(left + right)),
_ => {}
}
(Value::Float(left), Value::Float(right)) => return Ok(Value::Float(left + right)),
(Value::Integer(left), Value::Float(right)) => {
return Ok(Value::Float(left as f64 + right))
match (self.as_number(), other.as_number()) {
(Ok(left), Ok(right)) => return Ok(Value::Float(left + right)),
_ => {}
}
(Value::Float(left), Value::Integer(right)) => {
return Ok(Value::Float(left + right as f64))
match (self.as_string(), other.as_string()) {
(Ok(left), Ok(right)) => return Ok(Value::String(left.to_string() + right)),
_ => {}
}
(non_number, Value::Integer(_)) | (non_number, Value::Float(_)) => non_number,
(non_number, _) => non_number,
let non_number_or_string = if !self.is_number() == !self.is_string() {
self
} else {
other
};
Err(Error::ExpectedNumber { actual: non_number })
Err(Error::ExpectedNumberOrString {
actual: non_number_or_string,
})
}
}