1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
use std::ops::{Add, AddAssign};
use ::*;

impl <T> Add<T> for Expr where T: Into<Expr> {
    type Output = Expr;
    fn add(self, rhs: T) -> Expr {
        Expr::Add(Box::new(self.clone()), Box::new(rhs.into()))
    }
}

impl <T> AddAssign<T> for Expr where T: Into<Expr> {
    fn add_assign(&mut self, rhs: T) {
        *self = self.clone() + rhs.into()
    }
}

impl Add<Expr> for f64 {
    type Output = Expr;
    fn add(self, rhs: Expr) -> Expr {
        Expr::Num(self) + rhs
    }
}

impl Add<Expr> for i64 {
    type Output = Expr;
    fn add(self, rhs: Expr) -> Expr {
        Expr::Num(self as f64) + rhs
    }
}

#[test]
fn expr_f64() {
    let added = s!(x) + 3;
    assert_eq!(format!("{:?}", added), "Add(Symbol(\"x\"), Num(3.0))");
}

#[test]
fn f64_expr() {
    let added = 3 + s!(x);
    assert_eq!(format!("{:?}", added), "Add(Num(3.0), Symbol(\"x\"))");
}

#[test]
fn expr_expr() {
    let added = s!(x) + s!(y);
    assert_eq!(format!("{:?}", added), "Add(Symbol(\"x\"), Symbol(\"y\"))");
}