Skip to main content

nanvm_lib/vm/any/
add.rs

1use std::ops::Add;
2
3use crate::vm::{Any, IVm, ToAny, numeric::Numeric, primitive::Primitive};
4
5impl<A: IVm> Add for Any<A> {
6    type Output = Result<Self, Self>;
7
8    fn add(self, rhs: Self) -> Self::Output {
9        let lhs = self.to_primitive(None)?;
10        let rhs = rhs.to_primitive(None)?;
11        let concat = matches!(&lhs, Primitive::String(_)) || matches!(&rhs, Primitive::String(_));
12        let lhs: Any<A> = lhs.into();
13        let rhs: Any<A> = rhs.into();
14        if concat {
15            return Ok((lhs.to_string()? + rhs.to_string()?).to_any());
16        }
17        lhs.numeric_op(rhs, Numeric::add)
18    }
19}