the start of parsing

This commit is contained in:
Christoph J. Scherr 2023-09-12 22:09:33 +02:00
parent 8fc8432bdb
commit ff560c63ff
2 changed files with 34 additions and 14 deletions

View File

@ -62,8 +62,8 @@ impl Calculator {
/// This method only processes a single term at a time, without caching. /// This method only processes a single term at a time, without caching.
pub fn calc(mut t: Term) -> Result<Value> { pub fn calc(mut t: Term) -> Result<Value> {
trace!("Calculating term {t:?}"); trace!("Calculating term {t:?}");
t.prepare(); t.prepare()?;
t.process(); t.process()?;
if t.result.is_none() { if t.result.is_none() {
error!("Term was processed but no result was assigned."); error!("Term was processed but no result was assigned.");
return Err(Error::SyntaxError) return Err(Error::SyntaxError)

View File

@ -90,17 +90,18 @@ impl Term {
// Storage for unfinished tokens // Storage for unfinished tokens
let mut unfinished_chars: Vec<char> = Vec::new(); let mut unfinished_chars: Vec<char> = Vec::new();
for c in self.original.chars() { for (index, c) in self.original.chars().enumerate() {
// FIXME: this completely ignores shunting yard, if !c.is_alphanumeric() {
// only being on the lookout for values // TODO: allow any unicode char to be a variable
if Self::is_tok(&unfinished_chars) { warn!("'{c}' is not a valid character, only alphanumeric input is allowed.");
let tok = Self::to_tok(unfinished_chars)?; return Err(Error::SyntaxError);
// TODO: handle the token, depending on type, precedence and so on }
self.output_queue.push_front(tok); // this will be a mess, but it has to be before i can sort the mess.
unfinished_chars = Vec::new(); match c {
_ => {
warn!("The meaning of '{c}' could not be identified.");
return Err(Error::SyntaxError);
} }
else {
unfinished_chars.push(c);
} }
} }
Ok(()) Ok(())
@ -123,10 +124,29 @@ impl Term {
Ok(19.into()) Ok(19.into())
} }
fn is_tok(s: &Vec<char>) -> bool { /// only leave relevant chars for calculation
false 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<T> From<T> for Token where impl<T> From<T> for Token where