summaryrefslogtreecommitdiff
path: root/src/lib/eval/arith.rs
diff options
context:
space:
mode:
authorDominick Allen <dominick.allen1989@gmail.com>2020-06-20 13:38:03 -0500
committerDominick Allen <dominick.allen1989@gmail.com>2020-06-20 13:38:03 -0500
commit36e1bf722a3d366cea20ab7315d63d588d23dc48 (patch)
tree84ef440e398978bfd4467a99d375d7710f016031 /src/lib/eval/arith.rs
Working on a simple LISP/scheme interpreter.
Diffstat (limited to 'src/lib/eval/arith.rs')
-rw-r--r--src/lib/eval/arith.rs34
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())
+ }
+ }
+}