From a38293c2473710937e203329fb2057b06a0a5952 Mon Sep 17 00:00:00 2001 From: Mark Date: Sun, 9 Apr 2023 08:39:36 -0700 Subject: [PATCH] Added unit fromstring --- src/parser/mod.rs | 44 ++++++++++++++++++++++++++------------------ src/quantity/unit.rs | 16 ++++++++++++++++ 2 files changed, 42 insertions(+), 18 deletions(-) diff --git a/src/parser/mod.rs b/src/parser/mod.rs index 85cf281..1622165 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -12,7 +12,8 @@ use crate::parser::groupify::groupify; use crate::parser::treeify::treeify; use crate::parser::find_subs::find_subs; -use crate::quantity::{Quantity, BaseUnit}; +use crate::quantity::Quantity; +use crate::quantity::Unit; use crate::tokens::Token; @@ -89,28 +90,35 @@ impl PreToken { }, PreToken::PreWord(l, s) => { - return Ok(match &s[..] { + let c = match &s[..] { // Mathematical constants // 100 digits of each. - "π"|"pi" => { Token::Constant(Quantity::new_float_from_string("3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067").unwrap(), String::from("π")) }, - "e" => { Token::Constant(Quantity::new_float_from_string("2.718281828459045235360287471352662497757247093699959574966967627724076630353547594571382178525166427").unwrap(), String::from("e")) }, - "phi"|"φ" => { Token::Constant(Quantity::new_float_from_string("1.618033988749894848204586834365638117720309179805762862135448622705260462818902449707207204189391137").unwrap(), String::from("φ")) }, + "π"|"pi" => { Some(Token::Constant(Quantity::new_float_from_string( + "3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067" + ).unwrap(), String::from("π")))}, - // Units - "m" => { - let mut u = Quantity::new_rational(1f64).unwrap(); - u.add_unit(BaseUnit::Meter, 1f64); - Token::Number(u) - }, + "e" => { Some(Token::Constant(Quantity::new_float_from_string( + "2.718281828459045235360287471352662497757247093699959574966967627724076630353547594571382178525166427" + ).unwrap(), String::from("e"))) }, - "s" => { - let mut u = Quantity::new_rational(1f64).unwrap(); - u.add_unit(BaseUnit::Second, 1f64); - Token::Number(u) - } + "phi"|"φ" => { Some(Token::Constant(Quantity::new_float_from_string( + "1.618033988749894848204586834365638117720309179805762862135448622705260462818902449707207204189391137" + ).unwrap(), String::from("φ"))) }, - _ => { return Err((l, ParserError::Undefined(s))); } - }); + _ => { 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))); } PreToken::Container(v) => { return Ok(v); } diff --git a/src/quantity/unit.rs b/src/quantity/unit.rs index da92019..a015780 100644 --- a/src/quantity/unit.rs +++ b/src/quantity/unit.rs @@ -95,6 +95,22 @@ impl Unit { return u; } + pub fn from_string(s: &str) -> Option { + 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); + } }