diff --git a/src/parser.rs b/src/parser.rs index 10b2250..b135e11 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -51,6 +51,8 @@ pub enum Token { } impl Token { + + #[inline(always)] fn as_number(&self) -> Token { match self { Token::Number(l,v) => { @@ -65,15 +67,6 @@ impl Token { pub fn eval(&self) -> Token { match self { - Token::Root(ref v) => { - if v.len() != 1 {panic!()}; - let v = v[0].as_number(); - - if let Token::Number(l, v) = v { - Token::Number(l, v) - } else { panic!(); } - }, - Token::Negative(ref v) => { if v.len() != 1 {panic!()}; let v = v[0].as_number(); @@ -179,7 +172,7 @@ impl Token { }, Token::Factorial(ref _v) => { todo!() }, - _ => panic!() + _ => self.as_number() } } } diff --git a/src/parser/evaluate.rs b/src/parser/evaluate.rs index 5354bba..ec93e90 100644 --- a/src/parser/evaluate.rs +++ b/src/parser/evaluate.rs @@ -1,3 +1,5 @@ +use std::collections::VecDeque; + use crate::parser::Token; use crate::parser::LineLocation; use crate::parser::ParserError; @@ -15,7 +17,6 @@ fn get_at_coords<'a>(g: &'a mut Token, coords: &Vec) -> &'a mut Token { Token::Negative(ref mut v) => v, Token::Power(ref mut v) => v, Token::Modulo(ref mut v) => v, - Token::Root(ref mut v) => v, _ => panic!() }; @@ -36,19 +37,19 @@ pub fn p_evaluate( let mut h = &mut g; for t in coords.iter() { - let inner = match h { - Token::Multiply(ref mut v) => v, - Token::Divide(ref mut v) => v, - Token::Add(ref mut v) => v, - Token::Factorial(ref mut v) => v, - Token::Negative(ref mut v) => v, - Token::Power(ref mut v) => v, - Token::Modulo(ref mut v) => v, - Token::Root(ref mut v) => v, - _ => panic!() + let inner: Option<&mut VecDeque> = match h { + Token::Multiply(ref mut v) + | Token::Divide(ref mut v) + | Token::Add(ref mut v) + | Token::Factorial(ref mut v) + | Token::Negative(ref mut v) + | Token::Power(ref mut v) + | Token::Modulo(ref mut v) + => Some(v), + _ => None }; - if *t >= inner.len() { + if inner.is_none() || *t >= inner.as_ref().unwrap().len() { coords.pop(); if coords.len() == 0 { break 'outer; } @@ -62,7 +63,7 @@ pub fn p_evaluate( continue 'outer; } - h = &mut inner[*t]; + h = &mut inner.unwrap()[*t]; } match h { @@ -90,8 +91,7 @@ pub fn p_evaluate( Token::PreOperator(_,_) | Token::PreGroup(_,_) | Token::PreGroupStart(_) | - Token::PreGroupEnd(_) | - Token::Root(_) + Token::PreGroupEnd(_) => panic!() }; } diff --git a/src/parser/groupify.rs b/src/parser/groupify.rs index 8a808e6..80668b0 100644 --- a/src/parser/groupify.rs +++ b/src/parser/groupify.rs @@ -141,5 +141,5 @@ pub fn p_groupify(mut g: VecDeque) -> Result k, @@ -218,7 +218,7 @@ fn treeify_unaryleft( if right_val.is_none() || this_val > right_val.unwrap() { let this = g_inner.remove(i).unwrap(); let mut right = g_inner.remove(i).unwrap(); - if let Token::PreGroup(_, _) = right { right = inner_treeify(right)?; } + if let Token::PreGroup(_, _) = right { right = p_treeify(right)?; } let k = match this { Token::PreOperator(_, k) => k, @@ -318,7 +318,7 @@ fn treeify_unaryright( if left_val.is_none() || this_val > left_val.unwrap() { let this = g_inner.remove(i).unwrap(); let mut left = g_inner.remove(i-1).unwrap(); - if let Token::PreGroup(_, _) = left { left = inner_treeify(left)?; } + if let Token::PreGroup(_, _) = left { left = p_treeify(left)?; } let k = match this { Token::PreOperator(_, k) => k, @@ -341,12 +341,12 @@ fn treeify_unaryright( }; } -fn inner_treeify( +pub fn p_treeify( mut g: Token, ) -> Result { let g_inner: &mut VecDeque = match g { - Token::Root(ref mut x) => x, + Token::PreGroup(_, ref mut x) => x, _ => panic!() }; @@ -386,19 +386,10 @@ fn inner_treeify( return Err((l, ParserError::Syntax)); }, Token::PreGroup(_,_) => { - g = inner_treeify(g)?; + g = p_treeify(g)?; } _ => {} }; - return Ok(g); -} - -pub fn p_treeify( - mut g: Token, -) -> Result { - let mut v: VecDeque = VecDeque::new(); - v.push_back(inner_treeify(g)?); - g = Token::Root(v); return Ok(g); } \ No newline at end of file