From 3d5dcf06948bb85402f009600485b3b536fbbfee Mon Sep 17 00:00:00 2001 From: Mark Date: Wed, 29 Mar 2023 10:39:15 -0700 Subject: [PATCH] Minor cleanup --- src/evaluate.rs | 4 ++-- src/parser/groupify.rs | 4 ++-- src/parser/mod.rs | 8 ++++---- src/parser/treeify.rs | 13 ++++++------- src/tokens.rs | 12 ++++++++++-- 5 files changed, 24 insertions(+), 17 deletions(-) diff --git a/src/evaluate.rs b/src/evaluate.rs index 5a0590e..4d7b0c0 100644 --- a/src/evaluate.rs +++ b/src/evaluate.rs @@ -5,7 +5,7 @@ fn get_at_coords<'a>(g: &'a mut Token, coords: &Vec) -> &'a mut Token { let mut h = &mut *g; for t in coords.iter() { - let inner = h.get_args().unwrap(); + let inner = h.get_args_mut().unwrap(); h = &mut inner[*t]; } @@ -23,7 +23,7 @@ pub fn evaluate( let mut h = &mut g; 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() { coords.pop(); diff --git a/src/parser/groupify.rs b/src/parser/groupify.rs index ecd46c2..beb06ff 100644 --- a/src/parser/groupify.rs +++ b/src/parser/groupify.rs @@ -61,8 +61,8 @@ fn lookback( o.is_some() && ( - o.unwrap().is_binary() || - !o.unwrap().is_left_associative() + o.as_ref().unwrap().is_binary() || + !o.as_ref().unwrap().is_left_associative() ) } { g.push_back(a); diff --git a/src/parser/mod.rs b/src/parser/mod.rs index 9c0ad4d..8d0cfb5 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -80,9 +80,9 @@ impl PreToken { PreToken::PreWord(l, s) => { return Ok(match &s[..] { // 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")) }, - "phi"|"φ" => { Token::Constant(1.61803, String::from("phi")) }, + "phi"|"φ" => { Token::Constant(1.61803, String::from("φ")) }, _ => { return Err((l, ParserError::Undefined(s))); } }); } @@ -148,9 +148,9 @@ pub fn parse( let tokens = tokenize(s); let (_, tokens) = find_subs(tokens); let g = groupify(tokens)?; - let t = treeify(g)?; + let g = treeify(g)?; - return Ok(t); + return Ok(g); } diff --git a/src/parser/treeify.rs b/src/parser/treeify.rs index dd5dbcf..a4f1b95 100644 --- a/src/parser/treeify.rs +++ b/src/parser/treeify.rs @@ -1,4 +1,3 @@ - use std::collections::VecDeque; use crate::parser::PreToken; @@ -97,7 +96,7 @@ fn treeify_binary( let PreToken::PreOperator(l, s) = this else {panic!()}; let o = Operator::from_string(s); if o.is_none() { return Err((*l, ParserError::Syntax)); } - o.unwrap() as isize + o.unwrap().as_int() }; // 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 o = Operator::from_string(s); if o.is_none() { return Err((*l, ParserError::Syntax)); } - Some(o.unwrap() as isize) + Some(o.unwrap().as_int()) } else { None }; let right_val = if i < g_inner.len()-2 { let PreToken::PreOperator(l, s) = &g_inner[i+2] else {panic!()}; let o = Operator::from_string(s); if o.is_none() { return Err((*l, ParserError::Syntax)); } - Some(o.unwrap() as isize) + Some(o.unwrap().as_int()) } else { None }; if { @@ -213,7 +212,7 @@ fn treeify_unary( let PreToken::PreOperator(l, s) = this else {panic!()}; let o = Operator::from_string(s); if o.is_none() { return Err((*l, ParserError::Syntax)); } - o.unwrap() as isize + o.unwrap().as_int() }; // 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 o = Operator::from_string(s); if o.is_none() { return Err((*l, ParserError::Syntax)); } - Some(o.unwrap() as isize) + Some(o.unwrap().as_int()) } else { None } } else { if i < g_inner.len()-2 { let PreToken::PreOperator(l, s) = &g_inner[i+2] else {panic!()}; let o = Operator::from_string(s); if o.is_none() { return Err((*l, ParserError::Syntax)); } - Some(o.unwrap() as isize) + Some(o.unwrap().as_int()) } else { None } }; diff --git a/src/tokens.rs b/src/tokens.rs index a31868c..8556483 100644 --- a/src/tokens.rs +++ b/src/tokens.rs @@ -17,7 +17,9 @@ pub enum Token { impl Token { #[inline(always)] - pub fn get_args(&mut self) -> Option<&mut VecDeque> { + + #[inline(always)] + pub fn get_args_mut(&mut self) -> Option<&mut VecDeque> { match self { Token::Operator(_, ref mut a) => Some(a), _ => None @@ -47,7 +49,8 @@ impl Token { /// Operator types, in order of increasing priority. #[derive(Debug)] -#[derive(Copy, Clone)] +#[derive(Clone)] +#[repr(usize)] pub enum Operator { ModuloLong = 0, // Mod invoked with "mod" Subtract, @@ -105,6 +108,11 @@ impl Operator { } } + #[inline(always)] + pub fn as_int(&self) -> usize { + unsafe { *<*const _>::from(self).cast::() } + } + #[inline(always)] pub fn into_token(self, mut args: VecDeque) -> Token {