add basic arithmatic operations
Signed-off-by: Ava Hahn <ava@aidanis.online>
This commit is contained in:
parent
f8ab31e9aa
commit
a429b546d0
5 changed files with 364 additions and 6 deletions
104
src/segment.rs
104
src/segment.rs
|
|
@ -16,7 +16,7 @@
|
|||
*/
|
||||
use std::fmt;
|
||||
use std::marker::PhantomData;
|
||||
use std::ops::Index;
|
||||
use std::ops::{Add, Sub, Mul, Div, Index};
|
||||
|
||||
// Container
|
||||
#[derive(Debug, Default)]
|
||||
|
|
@ -268,6 +268,108 @@ impl PartialEq for Ctr {
|
|||
}
|
||||
}
|
||||
|
||||
impl Add<Ctr> for Ctr {
|
||||
type Output = Ctr;
|
||||
|
||||
fn add(self, rh: Ctr) -> Ctr {
|
||||
match self {
|
||||
Ctr::Float(lhn) => match rh {
|
||||
Ctr::Float(rhn) => Ctr::Float(lhn + rhn),
|
||||
Ctr::Integer(rhn) => Ctr::Float(lhn + rhn as f64),
|
||||
_ => unimplemented!("non-numeral on right hand side of add"),
|
||||
},
|
||||
|
||||
Ctr::Integer(lhn) => match rh {
|
||||
Ctr::Float(rhn) => Ctr::Float(lhn as f64 + rhn),
|
||||
Ctr::Integer(rhn) => Ctr::Integer(lhn + rhn),
|
||||
_ => unimplemented!("non-numeral on right hand side of add"),
|
||||
},
|
||||
|
||||
Ctr::String(lhs) => match rh {
|
||||
Ctr::String(rhs) => Ctr::String(lhs + &rhs),
|
||||
_ => unimplemented!("add to string only implemented for other strings"),
|
||||
},
|
||||
|
||||
_ => {
|
||||
unimplemented!("datum does not support add")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Sub<Ctr> for Ctr {
|
||||
type Output = Ctr;
|
||||
|
||||
fn sub(self, rh: Ctr) -> Ctr {
|
||||
match self {
|
||||
Ctr::Float(lhn) => match rh {
|
||||
Ctr::Float(rhn) => Ctr::Float(lhn - rhn),
|
||||
Ctr::Integer(rhn) => Ctr::Float(lhn - rhn as f64),
|
||||
_ => unimplemented!("non-numeral on right hand side of sub"),
|
||||
},
|
||||
|
||||
Ctr::Integer(lhn) => match rh {
|
||||
Ctr::Float(rhn) => Ctr::Float(lhn as f64 - rhn),
|
||||
Ctr::Integer(rhn) => Ctr::Integer(lhn - rhn),
|
||||
_ => unimplemented!("non-numeral on right hand side of sub"),
|
||||
},
|
||||
|
||||
_ => {
|
||||
unimplemented!("datum does not support sub")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
impl Mul<Ctr> for Ctr {
|
||||
type Output = Ctr;
|
||||
|
||||
fn mul(self, rh: Ctr) -> Ctr {
|
||||
match self {
|
||||
Ctr::Float(lhn) => match rh {
|
||||
Ctr::Float(rhn) => Ctr::Float(lhn * rhn),
|
||||
Ctr::Integer(rhn) => Ctr::Float(lhn * rhn as f64),
|
||||
_ => unimplemented!("non-numeral on right hand side of mul"),
|
||||
},
|
||||
|
||||
Ctr::Integer(lhn) => match rh {
|
||||
Ctr::Float(rhn) => Ctr::Float(lhn as f64 * rhn),
|
||||
Ctr::Integer(rhn) => Ctr::Integer(lhn * rhn),
|
||||
_ => unimplemented!("non-numeral on right hand side of mul"),
|
||||
},
|
||||
|
||||
_ => {
|
||||
unimplemented!("datum does not support mul")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Div<Ctr> for Ctr {
|
||||
type Output = Ctr;
|
||||
|
||||
fn div(self, rh: Ctr) -> Ctr {
|
||||
match self {
|
||||
Ctr::Float(lhn) => match rh {
|
||||
Ctr::Float(rhn) => Ctr::Float(lhn / rhn),
|
||||
Ctr::Integer(rhn) => Ctr::Float(lhn / rhn as f64),
|
||||
_ => unimplemented!("non-numeral on right hand side of div"),
|
||||
},
|
||||
|
||||
Ctr::Integer(lhn) => match rh {
|
||||
Ctr::Float(rhn) => Ctr::Float(lhn as f64 / rhn),
|
||||
Ctr::Integer(rhn) => Ctr::Integer(lhn / rhn),
|
||||
_ => unimplemented!("non-numeral on right hand side of div"),
|
||||
},
|
||||
|
||||
_ => {
|
||||
unimplemented!("datum does not support div")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for Seg {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "{}", seg_to_string(self, true))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue