From ff560c63ff2411588cecf38ca2b5c9d2e9c9d800 Mon Sep 17 00:00:00 2001 From: PlexSheep Date: Tue, 12 Sep 2023 22:09:33 +0200 Subject: [PATCH] the start of parsing --- src/math/calculator/mod.rs | 4 ++-- src/math/calculator/term.rs | 44 +++++++++++++++++++++++++++---------- 2 files changed, 34 insertions(+), 14 deletions(-) diff --git a/src/math/calculator/mod.rs b/src/math/calculator/mod.rs index c78990d..f99e7b1 100644 --- a/src/math/calculator/mod.rs +++ b/src/math/calculator/mod.rs @@ -62,8 +62,8 @@ impl Calculator { /// This method only processes a single term at a time, without caching. pub fn calc(mut t: Term) -> Result { trace!("Calculating term {t:?}"); - t.prepare(); - t.process(); + t.prepare()?; + t.process()?; if t.result.is_none() { error!("Term was processed but no result was assigned."); return Err(Error::SyntaxError) diff --git a/src/math/calculator/term.rs b/src/math/calculator/term.rs index a698059..3bd6445 100644 --- a/src/math/calculator/term.rs +++ b/src/math/calculator/term.rs @@ -90,17 +90,18 @@ impl Term { // Storage for unfinished tokens let mut unfinished_chars: Vec = Vec::new(); - for c in self.original.chars() { - // FIXME: this completely ignores shunting yard, - // only being on the lookout for values - if Self::is_tok(&unfinished_chars) { - let tok = Self::to_tok(unfinished_chars)?; - // TODO: handle the token, depending on type, precedence and so on - self.output_queue.push_front(tok); - unfinished_chars = Vec::new(); + for (index, c) in self.original.chars().enumerate() { + if !c.is_alphanumeric() { + // TODO: allow any unicode char to be a variable + warn!("'{c}' is not a valid character, only alphanumeric input is allowed."); + return Err(Error::SyntaxError); } - else { - unfinished_chars.push(c); + // this will be a mess, but it has to be before i can sort the mess. + match c { + _ => { + warn!("The meaning of '{c}' could not be identified."); + return Err(Error::SyntaxError); + } } } Ok(()) @@ -123,11 +124,30 @@ impl Term { Ok(19.into()) } - fn is_tok(s: &Vec) -> bool { - false + /// only leave relevant chars for calculation + fn filter(s: String) -> String { + let mut filtered = String::new(); + for c in s.chars() { + if !Self::is_ignore(&c) { + filtered.push(c); + } + } + return filtered + } + + /// check if we should ignore this character + fn is_ignore(c: &char) -> bool { + match *c { + ' ' => true, + _ => false + } } } +//////////////////////////////////////////////////////////////////////////////////////////////////// +/// Helper methods for Tokens +impl Token { } + //////////////////////////////////////////////////////////////////////////////////////////////////// impl From for Token where T: Into,