mirror of
https://github.com/rm-dr/daisy
synced 2025-07-05 01:59:30 -07:00
Added Quantity type
This commit is contained in:
@ -12,6 +12,8 @@ use crate::parser::groupify::groupify;
|
||||
use crate::parser::treeify::treeify;
|
||||
use crate::parser::find_subs::find_subs;
|
||||
|
||||
use crate::quantity::Quantity;
|
||||
|
||||
use crate::tokens::Token;
|
||||
|
||||
/// Specifies the location of a token in an input string.
|
||||
@ -71,21 +73,24 @@ impl PreToken {
|
||||
pub fn to_token(self) -> Result<Token, (LineLocation, ParserError)>{
|
||||
match self {
|
||||
PreToken::PreNumber(l, s) => {
|
||||
let n = match s.parse() {
|
||||
let n: f64 = match s.parse() {
|
||||
Ok(n) => n,
|
||||
Err(_) => return Err((l, ParserError::BadNumber))
|
||||
};
|
||||
return Ok(Token::Number(n));
|
||||
return Ok(Token::Number(Quantity::new_rational_from_f64(n).unwrap()));
|
||||
},
|
||||
PreToken::PreWord(l, s) => {
|
||||
return Ok(match &s[..] {
|
||||
// Mathematical constants
|
||||
"π"|"pi" => { Token::Constant(3.141592653, String::from("π")) },
|
||||
"e" => { Token::Constant(2.71828, String::from("e")) },
|
||||
"phi"|"φ" => { Token::Constant(1.61803, String::from("φ")) },
|
||||
// 100 digits of each.
|
||||
"π"|"pi" => { Token::Constant(Quantity::float_from_string("3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067"), String::from("π")) },
|
||||
"e" => { Token::Constant(Quantity::float_from_string("2.7182818284590452353602874713526624977572470936999595749669676277240766303535475945713 8217852516642"), String::from("e")) },
|
||||
"phi"|"φ" => { Token::Constant(Quantity::float_from_string("1.618033988749894848204586834365638117720309179805762862135448622705260462818902449707207204189391137"), String::from("φ")) },
|
||||
|
||||
_ => { return Err((l, ParserError::Undefined(s))); }
|
||||
});
|
||||
}
|
||||
|
||||
PreToken::Container(v) => { return Ok(v); }
|
||||
|
||||
PreToken::PreOperator(_,_)
|
||||
|
Reference in New Issue
Block a user