aboutsummaryrefslogtreecommitdiff
path: root/src/lisp/number.rs
diff options
context:
space:
mode:
authorAkshay <[email protected]>2021-03-19 16:27:34 +0000
committerAkshay <[email protected]>2021-03-19 16:27:34 +0000
commitae79d5a8b0166d43d3ff48aa593db6038b40410b (patch)
treead61f60b4e19f9701823bb6968973fb55939d098 /src/lisp/number.rs
parent149bc3627b886c306a877ac802c62089714326eb (diff)
begin work on scripting lisp
Diffstat (limited to 'src/lisp/number.rs')
-rw-r--r--src/lisp/number.rs70
1 files changed, 70 insertions, 0 deletions
diff --git a/src/lisp/number.rs b/src/lisp/number.rs
new file mode 100644
index 0000000..ddadbe6
--- /dev/null
+++ b/src/lisp/number.rs
@@ -0,0 +1,70 @@
1use std::{
2 fmt::*,
3 ops::{Add, Div, Mul, Sub},
4};
5
6#[derive(Debug, Copy, Clone)]
7pub enum LispNumber {
8 Integer(i64),
9 Float(f64),
10}
11
12impl Add for LispNumber {
13 type Output = Self;
14 fn add(self, rhs: Self) -> Self::Output {
15 use LispNumber::*;
16 match (self, rhs) {
17 (Integer(a), Integer(b)) => Integer(a + b),
18 (Float(a), Integer(b)) => Float(a + b as f64),
19 (Integer(a), Float(b)) => Float(a as f64 + b),
20 (Float(a), Float(b)) => Float(a + b),
21 }
22 }
23}
24
25impl Sub for LispNumber {
26 type Output = Self;
27 fn sub(self, rhs: Self) -> Self::Output {
28 use LispNumber::*;
29 match (self, rhs) {
30 (Integer(a), Integer(b)) => Integer(a - b),
31 (Float(a), Integer(b)) => Float(a - b as f64),
32 (Integer(a), Float(b)) => Float(a as f64 - b),
33 (Float(a), Float(b)) => Float(a - b),
34 }
35 }
36}
37
38impl Mul for LispNumber {
39 type Output = Self;
40 fn mul(self, rhs: Self) -> Self::Output {
41 use LispNumber::*;
42 match (self, rhs) {
43 (Integer(a), Integer(b)) => Integer(a * b),
44 (Float(a), Integer(b)) => Float(a * b as f64),
45 (Integer(a), Float(b)) => Float(a as f64 * b),
46 (Float(a), Float(b)) => Float(a * b),
47 }
48 }
49}
50
51impl PartialEq for LispNumber {
52 fn eq(&self, other: &Self) -> bool {
53 use LispNumber::*;
54 match (self, other) {
55 (Integer(a), Integer(b)) => *a == *b,
56 (Float(a), Integer(b)) => *a == *b as f64,
57 (Integer(a), Float(b)) => *a as f64 == *b,
58 (Float(a), Float(b)) => *a == *b,
59 }
60 }
61}
62
63impl Display for LispNumber {
64 fn fmt(&self, f: &mut Formatter<'_>) -> Result {
65 match self {
66 LispNumber::Integer(v) => write!(f, "{}", v),
67 LispNumber::Float(v) => write!(f, "{}", v),
68 }
69 }
70}