From 36e1bf722a3d366cea20ab7315d63d588d23dc48 Mon Sep 17 00:00:00 2001 From: Dominick Allen Date: Sat, 20 Jun 2020 13:38:03 -0500 Subject: Working on a simple LISP/scheme interpreter. --- src/lib/eval/arith.rs | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 src/lib/eval/arith.rs (limited to 'src/lib/eval/arith.rs') 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; + + fn add(self, other: Type) -> Result { + 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()) + } + } +} -- cgit v1.2.3