refactor: removed unneeded mutability

This commit is contained in:
Yandrik 2021-11-30 18:06:43 +01:00
parent 04c1e93500
commit 9a58b1cd01

View File

@ -41,14 +41,14 @@ fn expect_token(maybe_token: &Option<Token>) -> Result<&Token> {
} }
} }
pub fn parse(mut lexer: lexer::Lexer) -> Result<f64> { pub fn parse(lexer: lexer::Lexer) -> Result<f64> {
let mut tbox = TokenBox { lexer, cur_token: None, regress: false }; let mut tbox = TokenBox { lexer, cur_token: None, regress: false };
s_proc(&mut tbox) s_proc(&mut tbox)
} }
pub fn s_proc(tbox: &mut TokenBox) -> Result<f64> { pub fn s_proc(tbox: &mut TokenBox) -> Result<f64> {
println!("Proc S"); println!("Proc S");
let mut result = a_proc(tbox)?; let result = a_proc(tbox)?;
match tbox.read_token()? { match tbox.read_token()? {
None => Ok(result), None => Ok(result),
Some(token) => Err(ParserErrors::UnexpectedTokenError(token.clone(), String::from("S"))), Some(token) => Err(ParserErrors::UnexpectedTokenError(token.clone(), String::from("S"))),
@ -57,12 +57,12 @@ pub fn s_proc(tbox: &mut TokenBox) -> Result<f64> {
fn a_proc(tbox: &mut TokenBox) -> Result<f64> { fn a_proc(tbox: &mut TokenBox) -> Result<f64> {
println!("Proc A"); println!("Proc A");
let mut result = m_proc(tbox)?; let result = m_proc(tbox)?;
match &tbox.read_token()? { match &tbox.read_token()? {
Some(Token::OP(_, OpType::ADD)) => Ok(result + a_proc(tbox)?), Some(Token::OP(_, OpType::ADD)) => Ok(result + a_proc(tbox)?),
Some(Token::OP(_, OpType::SUB)) => Ok(result - a_proc(tbox)?), Some(Token::OP(_, OpType::SUB)) => Ok(result - a_proc(tbox)?),
None => Ok(result), None => Ok(result),
Some(token) => { Some(_) => {
tbox.regress(); tbox.regress();
Ok(result) Ok(result)
} }
@ -71,12 +71,12 @@ fn a_proc(tbox: &mut TokenBox) -> Result<f64> {
fn m_proc(tbox: &mut TokenBox) -> Result<f64> { fn m_proc(tbox: &mut TokenBox) -> Result<f64> {
println!("Proc M"); println!("Proc M");
let mut result = g_proc(tbox)?; let result = g_proc(tbox)?;
match &tbox.read_token()? { match &tbox.read_token()? {
Some(Token::OP(_, OpType::MUL)) => Ok(result * m_proc(tbox)?), Some(Token::OP(_, OpType::MUL)) => Ok(result * m_proc(tbox)?),
Some(Token::OP(_, OpType::DIV)) => Ok(result / m_proc(tbox)?), Some(Token::OP(_, OpType::DIV)) => Ok(result / m_proc(tbox)?),
None => Ok(result), None => Ok(result),
Some(token) => { Some(_) => {
tbox.regress(); tbox.regress();
Ok(result) Ok(result)
} }
@ -85,11 +85,11 @@ fn m_proc(tbox: &mut TokenBox) -> Result<f64> {
fn g_proc(tbox: &mut TokenBox) -> Result<f64> { fn g_proc(tbox: &mut TokenBox) -> Result<f64> {
println!("Proc G"); println!("Proc G");
let mut result = p_proc(tbox)?; let result = p_proc(tbox)?;
match tbox.read_token()? { match tbox.read_token()? {
Some(Token::OP(_, OpType::POW)) => Ok(result.powf(g_proc(tbox)?)), Some(Token::OP(_, OpType::POW)) => Ok(result.powf(g_proc(tbox)?)),
None => Ok(result), None => Ok(result),
Some(token) => { Some(_) => {
tbox.regress(); tbox.regress();
Ok(result) Ok(result)
} }
@ -123,6 +123,7 @@ mod test {
Ok(()) Ok(())
} }
//noinspection ALL
#[test] #[test]
fn skript_formula_1() -> Result<()> { fn skript_formula_1() -> Result<()> {
let res = parse(lexer::Lexer::new("2+5/3*2"))?; let res = parse(lexer::Lexer::new("2+5/3*2"))?;