diff options
Diffstat (limited to 'src/lib/eval/arith.rs')
-rw-r--r-- | src/lib/eval/arith.rs | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/src/lib/eval/arith.rs b/src/lib/eval/arith.rs new file mode 100644 index 0000000..fae69de --- /dev/null +++ b/src/lib/eval/arith.rs @@ -0,0 +1,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()) + } + } +} |