Removed Root Token

pull/2/head
Mark 2023-03-25 09:27:15 -07:00
parent e780c90233
commit cea5a1db22
Signed by: Mark
GPG Key ID: AD62BB059C2AAEE4
4 changed files with 26 additions and 42 deletions

View File

@ -51,6 +51,8 @@ pub enum Token {
} }
impl Token { impl Token {
#[inline(always)]
fn as_number(&self) -> Token { fn as_number(&self) -> Token {
match self { match self {
Token::Number(l,v) => { Token::Number(l,v) => {
@ -65,15 +67,6 @@ impl Token {
pub fn eval(&self) -> Token { pub fn eval(&self) -> Token {
match self { 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) => { Token::Negative(ref v) => {
if v.len() != 1 {panic!()}; if v.len() != 1 {panic!()};
let v = v[0].as_number(); let v = v[0].as_number();
@ -179,7 +172,7 @@ impl Token {
}, },
Token::Factorial(ref _v) => { todo!() }, Token::Factorial(ref _v) => { todo!() },
_ => panic!() _ => self.as_number()
} }
} }
} }

View File

@ -1,3 +1,5 @@
use std::collections::VecDeque;
use crate::parser::Token; use crate::parser::Token;
use crate::parser::LineLocation; use crate::parser::LineLocation;
use crate::parser::ParserError; use crate::parser::ParserError;
@ -15,7 +17,6 @@ fn get_at_coords<'a>(g: &'a mut Token, coords: &Vec<usize>) -> &'a mut Token {
Token::Negative(ref mut v) => v, Token::Negative(ref mut v) => v,
Token::Power(ref mut v) => v, Token::Power(ref mut v) => v,
Token::Modulo(ref mut v) => v, Token::Modulo(ref mut v) => v,
Token::Root(ref mut v) => v,
_ => panic!() _ => panic!()
}; };
@ -36,19 +37,19 @@ pub fn p_evaluate(
let mut h = &mut g; let mut h = &mut g;
for t in coords.iter() { for t in coords.iter() {
let inner = match h { let inner: Option<&mut VecDeque<Token>> = match h {
Token::Multiply(ref mut v) => v, Token::Multiply(ref mut v)
Token::Divide(ref mut v) => v, | Token::Divide(ref mut v)
Token::Add(ref mut v) => v, | Token::Add(ref mut v)
Token::Factorial(ref mut v) => v, | Token::Factorial(ref mut v)
Token::Negative(ref mut v) => v, | Token::Negative(ref mut v)
Token::Power(ref mut v) => v, | Token::Power(ref mut v)
Token::Modulo(ref mut v) => v, | Token::Modulo(ref mut v)
Token::Root(ref mut v) => v, => Some(v),
_ => panic!() _ => None
}; };
if *t >= inner.len() { if inner.is_none() || *t >= inner.as_ref().unwrap().len() {
coords.pop(); coords.pop();
if coords.len() == 0 { break 'outer; } if coords.len() == 0 { break 'outer; }
@ -62,7 +63,7 @@ pub fn p_evaluate(
continue 'outer; continue 'outer;
} }
h = &mut inner[*t]; h = &mut inner.unwrap()[*t];
} }
match h { match h {
@ -90,8 +91,7 @@ pub fn p_evaluate(
Token::PreOperator(_,_) | Token::PreOperator(_,_) |
Token::PreGroup(_,_) | Token::PreGroup(_,_) |
Token::PreGroupStart(_) | Token::PreGroupStart(_) |
Token::PreGroupEnd(_) | Token::PreGroupEnd(_)
Token::Root(_)
=> panic!() => panic!()
}; };
} }

View File

@ -141,5 +141,5 @@ pub fn p_groupify(mut g: VecDeque<Token>) -> Result<Token, (LineLocation, Parser
} }
let (_, v) = levels.pop().unwrap(); let (_, v) = levels.pop().unwrap();
return Ok(Token::Root(v)); return Ok(Token::PreGroup(LineLocation{pos:0, len:0}, v));
} }

View File

@ -123,8 +123,8 @@ fn treeify_binary(
let mut left = g_inner.remove(i-1).unwrap(); let mut left = g_inner.remove(i-1).unwrap();
let this = g_inner.remove(i-1).unwrap(); let this = g_inner.remove(i-1).unwrap();
let mut right = g_inner.remove(i-1).unwrap(); let mut right = g_inner.remove(i-1).unwrap();
if let Token::PreGroup(_, _) = right { right = inner_treeify(right)?; } if let Token::PreGroup(_, _) = right { right = p_treeify(right)?; }
if let Token::PreGroup(_, _) = left { left = inner_treeify(left)?; } if let Token::PreGroup(_, _) = left { left = p_treeify(left)?; }
let k = match this { let k = match this {
Token::PreOperator(_, k) => k, Token::PreOperator(_, k) => k,
@ -218,7 +218,7 @@ fn treeify_unaryleft(
if right_val.is_none() || this_val > right_val.unwrap() { if right_val.is_none() || this_val > right_val.unwrap() {
let this = g_inner.remove(i).unwrap(); let this = g_inner.remove(i).unwrap();
let mut right = 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 { let k = match this {
Token::PreOperator(_, k) => k, Token::PreOperator(_, k) => k,
@ -318,7 +318,7 @@ fn treeify_unaryright(
if left_val.is_none() || this_val > left_val.unwrap() { if left_val.is_none() || this_val > left_val.unwrap() {
let this = g_inner.remove(i).unwrap(); let this = g_inner.remove(i).unwrap();
let mut left = g_inner.remove(i-1).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 { let k = match this {
Token::PreOperator(_, k) => k, Token::PreOperator(_, k) => k,
@ -341,12 +341,12 @@ fn treeify_unaryright(
}; };
} }
fn inner_treeify( pub fn p_treeify(
mut g: Token, mut g: Token,
) -> Result<Token, (LineLocation, ParserError)> { ) -> Result<Token, (LineLocation, ParserError)> {
let g_inner: &mut VecDeque<Token> = match g { let g_inner: &mut VecDeque<Token> = match g {
Token::Root(ref mut x) => x, Token::PreGroup(_, ref mut x) => x,
_ => panic!() _ => panic!()
}; };
@ -386,19 +386,10 @@ fn inner_treeify(
return Err((l, ParserError::Syntax)); return Err((l, ParserError::Syntax));
}, },
Token::PreGroup(_,_) => { Token::PreGroup(_,_) => {
g = inner_treeify(g)?; g = p_treeify(g)?;
} }
_ => {} _ => {}
}; };
return Ok(g); return Ok(g);
} }
pub fn p_treeify(
mut g: Token,
) -> Result<Token, (LineLocation, ParserError)> {
let mut v: VecDeque<Token> = VecDeque::new();
v.push_back(inner_treeify(g)?);
g = Token::Root(v);
return Ok(g);
}