Added unit fromstring

pull/2/head
Mark 2023-04-09 08:39:36 -07:00
parent e9645208d9
commit a38293c247
Signed by: Mark
GPG Key ID: AD62BB059C2AAEE4
2 changed files with 42 additions and 18 deletions

View File

@ -12,7 +12,8 @@ use crate::parser::groupify::groupify;
use crate::parser::treeify::treeify; use crate::parser::treeify::treeify;
use crate::parser::find_subs::find_subs; use crate::parser::find_subs::find_subs;
use crate::quantity::{Quantity, BaseUnit}; use crate::quantity::Quantity;
use crate::quantity::Unit;
use crate::tokens::Token; use crate::tokens::Token;
@ -89,28 +90,35 @@ impl PreToken {
}, },
PreToken::PreWord(l, s) => { PreToken::PreWord(l, s) => {
return Ok(match &s[..] { let c = match &s[..] {
// Mathematical constants // Mathematical constants
// 100 digits of each. // 100 digits of each.
"π"|"pi" => { Token::Constant(Quantity::new_float_from_string("3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067").unwrap(), String::from("π")) }, "π"|"pi" => { Some(Token::Constant(Quantity::new_float_from_string(
"e" => { Token::Constant(Quantity::new_float_from_string("2.718281828459045235360287471352662497757247093699959574966967627724076630353547594571382178525166427").unwrap(), String::from("e")) }, "3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067"
"phi"|"φ" => { Token::Constant(Quantity::new_float_from_string("1.618033988749894848204586834365638117720309179805762862135448622705260462818902449707207204189391137").unwrap(), String::from("φ")) }, ).unwrap(), String::from("π")))},
// Units "e" => { Some(Token::Constant(Quantity::new_float_from_string(
"m" => { "2.718281828459045235360287471352662497757247093699959574966967627724076630353547594571382178525166427"
let mut u = Quantity::new_rational(1f64).unwrap(); ).unwrap(), String::from("e"))) },
u.add_unit(BaseUnit::Meter, 1f64);
Token::Number(u)
},
"s" => { "phi"|"φ" => { Some(Token::Constant(Quantity::new_float_from_string(
let mut u = Quantity::new_rational(1f64).unwrap(); "1.618033988749894848204586834365638117720309179805762862135448622705260462818902449707207204189391137"
u.add_unit(BaseUnit::Second, 1f64); ).unwrap(), String::from("φ"))) },
Token::Number(u)
_ => { None }
};
if c.is_some() { return Ok(c.unwrap()); }
let c = Unit::from_string(&s);
if c.is_some() {
let mut q = Quantity::new_rational(1f64).unwrap();
q.set_unit(c.unwrap());
return Ok(Token::Number(q));
} }
_ => { return Err((l, ParserError::Undefined(s))); } return Err((l, ParserError::Undefined(s)));
});
} }
PreToken::Container(v) => { return Ok(v); } PreToken::Container(v) => { return Ok(v); }

View File

@ -95,6 +95,22 @@ impl Unit {
return u; return u;
} }
pub fn from_string(s: &str) -> Option<Unit> {
let b = match s {
"m" => BaseUnit::Meter,
"s" => BaseUnit::Second,
"kg" => BaseUnit::Kilogram,
"a" => BaseUnit::Ampere,
"k" => BaseUnit::Kelvin,
"mol" => BaseUnit::Mole,
"c" => BaseUnit::Candela,
_ => { return None; }
};
let mut u = Unit::new();
u.insert(b, 1f64);
return Some(u);
}
} }