Added square root, fixed many parser bugs

This commit is contained in:
2023-03-28 10:46:29 -07:00
parent 6db5137b56
commit 71d9aaa039
7 changed files with 277 additions and 239 deletions

View File

@ -12,9 +12,17 @@ use crate::parser::groupify::groupify;
use crate::parser::treeify::treeify;
use crate::parser::find_subs::find_subs;
use crate::tokens::LineLocation;
use crate::tokens::Token;
/// Specifies the location of a token in an input string.
/// Used to locate ParserErrors.
#[derive(Debug)]
#[derive(Copy, Clone)]
pub struct LineLocation {
pub pos: usize,
pub len: usize
}
#[derive(Debug)]
enum PreToken {
PreNumber(LineLocation, String),
@ -67,14 +75,14 @@ impl PreToken {
Ok(n) => n,
Err(_) => return Err((l, ParserError::BadNumber))
};
return Ok(Token::Number(l, n));
return Ok(Token::Number(n));
},
PreToken::PreWord(l, s) => {
return Ok(match &s[..] {
// Mathematical constants
"π"|"pi" => { Token::Constant(l, 3.141592653, String::from("pi")) },
"e" => { Token::Constant(l, 2.71828, String::from("e")) },
"phi"|"φ" => { Token::Constant(l, 1.61803, String::from("phi")) },
"π"|"pi" => { Token::Constant(3.141592653, String::from("pi")) },
"e" => { Token::Constant(2.71828, String::from("e")) },
"phi"|"φ" => { Token::Constant(1.61803, String::from("phi")) },
_ => { return Err((l, ParserError::Undefined(s))); }
});
}