aboutsummaryrefslogtreecommitdiff
path: root/src/lisp/number.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/lisp/number.rs')
-rw-r--r--src/lisp/number.rs24
1 files changed, 21 insertions, 3 deletions
diff --git a/src/lisp/number.rs b/src/lisp/number.rs
index ddadbe6..18a41f7 100644
--- a/src/lisp/number.rs
+++ b/src/lisp/number.rs
@@ -1,14 +1,32 @@
1use std::{ 1use std::{
2 fmt::*, 2 fmt,
3 ops::{Add, Div, Mul, Sub}, 3 ops::{Add, Div, Mul, Sub},
4}; 4};
5 5
6use crate::lisp::error::LispError;
7
6#[derive(Debug, Copy, Clone)] 8#[derive(Debug, Copy, Clone)]
7pub enum LispNumber { 9pub enum LispNumber {
8 Integer(i64), 10 Integer(i64),
9 Float(f64), 11 Float(f64),
10} 12}
11 13
14impl LispNumber {
15 pub fn div(self, rhs: Self) -> Result<LispNumber, LispError> {
16 use LispNumber::*;
17 if rhs == Integer(0) || rhs == Float(0.) {
18 return Err(LispError::EvalError);
19 } else {
20 return Ok(match (self, rhs) {
21 (Integer(a), Integer(b)) => Float(a as f64 / b as f64),
22 (Float(a), Integer(b)) => Float(a / b as f64),
23 (Integer(a), Float(b)) => Float(a as f64 / b),
24 (Float(a), Float(b)) => Float(a / b),
25 });
26 }
27 }
28}
29
12impl Add for LispNumber { 30impl Add for LispNumber {
13 type Output = Self; 31 type Output = Self;
14 fn add(self, rhs: Self) -> Self::Output { 32 fn add(self, rhs: Self) -> Self::Output {
@@ -60,8 +78,8 @@ impl PartialEq for LispNumber {
60 } 78 }
61} 79}
62 80
63impl Display for LispNumber { 81impl fmt::Display for LispNumber {
64 fn fmt(&self, f: &mut Formatter<'_>) -> Result { 82 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
65 match self { 83 match self {
66 LispNumber::Integer(v) => write!(f, "{}", v), 84 LispNumber::Integer(v) => write!(f, "{}", v),
67 LispNumber::Float(v) => write!(f, "{}", v), 85 LispNumber::Float(v) => write!(f, "{}", v),