Implement string concatenation
This commit is contained in:
parent
ecdac6fab5
commit
c0bafe577e
@ -218,22 +218,30 @@ impl Add for Value {
|
|||||||
type Output = Result<Value>;
|
type Output = Result<Value>;
|
||||||
|
|
||||||
fn add(self, other: Self) -> Self::Output {
|
fn add(self, other: Self) -> Self::Output {
|
||||||
let non_number = match (self, other) {
|
match (self.as_int(), other.as_int()) {
|
||||||
(Value::Integer(left), Value::Integer(right)) => {
|
(Ok(left), Ok(right)) => return Ok(Value::Integer(left + 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)) => {
|
match (self.as_number(), other.as_number()) {
|
||||||
return Ok(Value::Float(left as f64 + right))
|
(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,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user