diff --git a/Cargo.lock b/Cargo.lock index a48b2dc..55aec00 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -22,7 +22,7 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "daisy" -version = "0.2.1" +version = "0.2.2" dependencies = [ "cfg-if", "rug", diff --git a/Cargo.toml b/Cargo.toml index 31ee3c1..e66fcda 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "daisy" -version = "0.2.1" +version = "0.2.2" edition = "2021" [profile.release] diff --git a/src/evaluate/constant.rs b/src/evaluate/constant.rs index 801065b..539eefc 100644 --- a/src/evaluate/constant.rs +++ b/src/evaluate/constant.rs @@ -1,6 +1,7 @@ use crate::parser::Token; use crate::parser::Constant; use crate::quantity::Quantity; +use crate::quantity::Unit; use super::EvalError; @@ -19,5 +20,27 @@ pub fn eval_constant(c: &Constant) -> Result { Constant::Phi => { Token::Quantity(Quantity::new_float_from_string( "1.618033988749894848204586834365638117720309179805762862135448622705260462818902449707207204189391137" ).unwrap()) }, + + Constant::MPG => { + let mut q = Quantity::new_float_from_string("1").unwrap(); + q.set_unit( + ( + Unit::from_string("mile").unwrap() / + Unit::from_string("gallon").unwrap() + ).unit + ); + Token::Quantity(q) + }, + + Constant::MPH => { + let mut q = Quantity::new_float_from_string("1").unwrap(); + q.set_unit( + ( + Unit::from_string("mile").unwrap() / + Unit::from_string("hour").unwrap() + ).unit + ); + Token::Quantity(q) + }, }) } \ No newline at end of file diff --git a/src/parser/pretoken.rs b/src/parser/pretoken.rs index 4d3e337..6247db4 100644 --- a/src/parser/pretoken.rs +++ b/src/parser/pretoken.rs @@ -78,6 +78,8 @@ impl PreToken { "π"|"pi" => { Some(Constant::Pi)}, "e" => { Some(Constant::E) }, "phi"|"φ" => { Some(Constant::Phi) }, + "mpg" => { Some(Constant::MPG) }, + "mph" => { Some(Constant::MPH) }, _ => { None } }; diff --git a/src/parser/token/constant.rs b/src/parser/token/constant.rs index 15a1ae7..f303cd4 100644 --- a/src/parser/token/constant.rs +++ b/src/parser/token/constant.rs @@ -1,17 +1,27 @@ #[derive(Debug)] #[derive(Clone)] pub enum Constant { + // Fake units + MPG, + MPH, + + // Mathematics Pi, Phi, - E + E, } impl Constant { pub fn to_string(&self) -> String { match self { + // Fake units + Constant::MPG => { String::from("mpg") }, + Constant::MPH => { String::from("mph") }, + + // Mathematics Constant::Pi => { String::from("π") }, Constant::Phi => { String::from("φ") }, - Constant::E => { String::from("e") }, + Constant::E => { String::from("e") } } } } \ No newline at end of file diff --git a/src/quantity/unit/mod.rs b/src/quantity/unit/mod.rs index 3c950c2..7407d1d 100644 --- a/src/quantity/unit/mod.rs +++ b/src/quantity/unit/mod.rs @@ -39,8 +39,6 @@ pub enum UnitBase { // Area Barn, Hectare, - MilesPerHour, - MilesPerGallon, Acre, @@ -190,8 +188,6 @@ macro_rules! fromstring_db { (UnitBase::JulianYear, "julianYears"), // Misc - (UnitBase::MilesPerHour, "mph"), - (UnitBase::MilesPerGallon, "mpg"), (UnitBase::Barn, "b"), (UnitBase::Barn, "barn"), (UnitBase::Hectare, "ha"), @@ -346,7 +342,7 @@ macro_rules! unit_db { ), UnitBase::Hour => $X!( - UnitBase::Hour, "hour", + UnitBase::Hour, "h", rational, "3600", (UnitBase::Second, 1f64) ), @@ -457,21 +453,6 @@ macro_rules! unit_db { (UnitBase::Meter, 1f64) ), - - // Misc - UnitBase::MilesPerHour => $X!( - UnitBase::MilesPerHour, "mph", - float, "0.44704", - (UnitBase::Meter, 1f64), - (UnitBase::Second, -1f64) - ), - - UnitBase::MilesPerGallon => $X!( - UnitBase::MilesPerGallon, "mpg", - float, "425144", - (UnitBase::Meter, -2f64) - ), - UnitBase::Barn => $X!( UnitBase::Barn, "b", rational, "1e-28", diff --git a/src/tests.rs b/src/tests.rs index 358403b..60e9aa3 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -206,5 +206,6 @@ fn basic_units() { fn complex_units() { good_expr("0.62137 mi", "1km to mi"); good_expr("3280.8 ft", "1km to ft"); - good_expr("62.137 mph", "100 km/h to mph"); + good_expr("62.137 mi/h", "100 km/h to mph"); + good_expr("20 mi", "10 mph * 2 hours"); } \ No newline at end of file