Minor cleanup

pull/2/head
Mark 2023-03-29 10:39:15 -07:00
parent 52d848cc09
commit 3d5dcf0694
Signed by: Mark
GPG Key ID: AD62BB059C2AAEE4
5 changed files with 24 additions and 17 deletions

View File

@ -5,7 +5,7 @@ fn get_at_coords<'a>(g: &'a mut Token, coords: &Vec<usize>) -> &'a mut Token {
let mut h = &mut *g; let mut h = &mut *g;
for t in coords.iter() { for t in coords.iter() {
let inner = h.get_args().unwrap(); let inner = h.get_args_mut().unwrap();
h = &mut inner[*t]; h = &mut inner[*t];
} }
@ -23,7 +23,7 @@ pub fn evaluate(
let mut h = &mut g; let mut h = &mut g;
for t in coords.iter() { for t in coords.iter() {
let inner = h.get_args(); let inner = h.get_args_mut();
if inner.is_none() || *t >= inner.as_ref().unwrap().len() { if inner.is_none() || *t >= inner.as_ref().unwrap().len() {
coords.pop(); coords.pop();

View File

@ -61,8 +61,8 @@ fn lookback(
o.is_some() && o.is_some() &&
( (
o.unwrap().is_binary() || o.as_ref().unwrap().is_binary() ||
!o.unwrap().is_left_associative() !o.as_ref().unwrap().is_left_associative()
) )
} { } {
g.push_back(a); g.push_back(a);

View File

@ -80,9 +80,9 @@ impl PreToken {
PreToken::PreWord(l, s) => { PreToken::PreWord(l, s) => {
return Ok(match &s[..] { return Ok(match &s[..] {
// Mathematical constants // Mathematical constants
"π"|"pi" => { Token::Constant(3.141592653, String::from("pi")) }, "π"|"pi" => { Token::Constant(3.141592653, String::from("π")) },
"e" => { Token::Constant(2.71828, String::from("e")) }, "e" => { Token::Constant(2.71828, String::from("e")) },
"phi"|"φ" => { Token::Constant(1.61803, String::from("phi")) }, "phi"|"φ" => { Token::Constant(1.61803, String::from("φ")) },
_ => { return Err((l, ParserError::Undefined(s))); } _ => { return Err((l, ParserError::Undefined(s))); }
}); });
} }
@ -148,9 +148,9 @@ pub fn parse(
let tokens = tokenize(s); let tokens = tokenize(s);
let (_, tokens) = find_subs(tokens); let (_, tokens) = find_subs(tokens);
let g = groupify(tokens)?; let g = groupify(tokens)?;
let t = treeify(g)?; let g = treeify(g)?;
return Ok(t); return Ok(g);
} }

View File

@ -1,4 +1,3 @@
use std::collections::VecDeque; use std::collections::VecDeque;
use crate::parser::PreToken; use crate::parser::PreToken;
@ -97,7 +96,7 @@ fn treeify_binary(
let PreToken::PreOperator(l, s) = this else {panic!()}; let PreToken::PreOperator(l, s) = this else {panic!()};
let o = Operator::from_string(s); let o = Operator::from_string(s);
if o.is_none() { return Err((*l, ParserError::Syntax)); } if o.is_none() { return Err((*l, ParserError::Syntax)); }
o.unwrap() as isize o.unwrap().as_int()
}; };
// Precedence of the operators contesting our arguments // Precedence of the operators contesting our arguments
@ -105,14 +104,14 @@ fn treeify_binary(
let PreToken::PreOperator(l, s) = &g_inner[i-2] else {panic!()}; let PreToken::PreOperator(l, s) = &g_inner[i-2] else {panic!()};
let o = Operator::from_string(s); let o = Operator::from_string(s);
if o.is_none() { return Err((*l, ParserError::Syntax)); } if o.is_none() { return Err((*l, ParserError::Syntax)); }
Some(o.unwrap() as isize) Some(o.unwrap().as_int())
} else { None }; } else { None };
let right_val = if i < g_inner.len()-2 { let right_val = if i < g_inner.len()-2 {
let PreToken::PreOperator(l, s) = &g_inner[i+2] else {panic!()}; let PreToken::PreOperator(l, s) = &g_inner[i+2] else {panic!()};
let o = Operator::from_string(s); let o = Operator::from_string(s);
if o.is_none() { return Err((*l, ParserError::Syntax)); } if o.is_none() { return Err((*l, ParserError::Syntax)); }
Some(o.unwrap() as isize) Some(o.unwrap().as_int())
} else { None }; } else { None };
if { if {
@ -213,7 +212,7 @@ fn treeify_unary(
let PreToken::PreOperator(l, s) = this else {panic!()}; let PreToken::PreOperator(l, s) = this else {panic!()};
let o = Operator::from_string(s); let o = Operator::from_string(s);
if o.is_none() { return Err((*l, ParserError::Syntax)); } if o.is_none() { return Err((*l, ParserError::Syntax)); }
o.unwrap() as isize o.unwrap().as_int()
}; };
// Precedence of the operator contesting its argument // Precedence of the operator contesting its argument
@ -222,14 +221,14 @@ fn treeify_unary(
let PreToken::PreOperator(l, s) = &g_inner[i-2] else {panic!()}; let PreToken::PreOperator(l, s) = &g_inner[i-2] else {panic!()};
let o = Operator::from_string(s); let o = Operator::from_string(s);
if o.is_none() { return Err((*l, ParserError::Syntax)); } if o.is_none() { return Err((*l, ParserError::Syntax)); }
Some(o.unwrap() as isize) Some(o.unwrap().as_int())
} else { None } } else { None }
} else { } else {
if i < g_inner.len()-2 { if i < g_inner.len()-2 {
let PreToken::PreOperator(l, s) = &g_inner[i+2] else {panic!()}; let PreToken::PreOperator(l, s) = &g_inner[i+2] else {panic!()};
let o = Operator::from_string(s); let o = Operator::from_string(s);
if o.is_none() { return Err((*l, ParserError::Syntax)); } if o.is_none() { return Err((*l, ParserError::Syntax)); }
Some(o.unwrap() as isize) Some(o.unwrap().as_int())
} else { None } } else { None }
}; };

View File

@ -17,7 +17,9 @@ pub enum Token {
impl Token { impl Token {
#[inline(always)] #[inline(always)]
pub fn get_args(&mut self) -> Option<&mut VecDeque<Token>> {
#[inline(always)]
pub fn get_args_mut(&mut self) -> Option<&mut VecDeque<Token>> {
match self { match self {
Token::Operator(_, ref mut a) => Some(a), Token::Operator(_, ref mut a) => Some(a),
_ => None _ => None
@ -47,7 +49,8 @@ impl Token {
/// Operator types, in order of increasing priority. /// Operator types, in order of increasing priority.
#[derive(Debug)] #[derive(Debug)]
#[derive(Copy, Clone)] #[derive(Clone)]
#[repr(usize)]
pub enum Operator { pub enum Operator {
ModuloLong = 0, // Mod invoked with "mod" ModuloLong = 0, // Mod invoked with "mod"
Subtract, Subtract,
@ -105,6 +108,11 @@ impl Operator {
} }
} }
#[inline(always)]
pub fn as_int(&self) -> usize {
unsafe { *<*const _>::from(self).cast::<usize>() }
}
#[inline(always)] #[inline(always)]
pub fn into_token(self, mut args: VecDeque<Token>) -> Token { pub fn into_token(self, mut args: VecDeque<Token>) -> Token {