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
|
use std::ops::Add;
use super::super::types::Type;
use super::super::types::Type::*;
use super::super::types::Number;
impl Add for Type {
type Output = Result<Type, String>;
fn add(self, other: Type) -> Result<Type, String> {
match (self, other) {
(Bool(_), _) | (_, Bool(_)) |
(Str(_), _) | (_, Str(_)) |
(Operator(_), _) | (_, Operator(_)) => {
Err("Cannot add these types".to_string())
},
(Number(Number::Int(i)), Number(Number::Float(f))) |
(Number(Number::Float(f)), Number(Number::Int(i))) => {
Ok(Number(Number::Float(f + i as f32)))
},
(Number(Number::Int(a)), Number(Number::Int(b))) => {
Ok(Number(Number::Int(a + b)))
},
(Number(Number::Float(a)), Number(Number::Float(b))) => {
Ok(Number(Number::Float(a + b)))
},
(Symbol(_), Number(_)) |
(Number(_), Symbol(_)) |
(Symbol(_), Symbol(_)) => Err("Not yet implemented".to_string())
}
}
}
|