From a4506e775afdc10cb7338fc8516552ed8dae2eeb Mon Sep 17 00:00:00 2001 From: Mark Date: Sun, 19 Mar 2023 19:56:58 -0700 Subject: [PATCH] Treefold edits --- src/main.rs | 82 ++++++++++++++----------------------------------- src/tokenize.rs | 4 +-- 2 files changed, 25 insertions(+), 61 deletions(-) diff --git a/src/main.rs b/src/main.rs index 1e1b351..f032633 100644 --- a/src/main.rs +++ b/src/main.rs @@ -97,8 +97,16 @@ fn treefold( 0 => {}, 1 => {}, 2 => { - let last: tokenize::Token = new.pop_back().unwrap(); - let next: tokenize::Token = g_inner.pop_front().unwrap().clone(); + let mut last: tokenize::Token = new.pop_back().unwrap(); + let mut next: tokenize::Token = g_inner.pop_front().unwrap().clone(); + + // TODO: append to t_vec, do this without recursion. + if let tokenize::Token::Group(_) = last { + last = treefold(last, check, op_type, new_token).unwrap(); + } + if let tokenize::Token::Group(_) = next { + next = treefold(next, check, op_type, new_token).unwrap(); + } let mut new_token_args: VecDeque = VecDeque::with_capacity(2); new_token_args.push_back(last); @@ -129,6 +137,17 @@ fn new_mult(v: VecDeque) -> tokenize::Token { tokenize::Token::Mult(v) } +fn is_add(t: &tokenize::Token) -> bool { + match t { + tokenize::Token::Operator(s) => {s == "+"}, + _ => false + } +} + +fn new_add(v: VecDeque) -> tokenize::Token { + tokenize::Token::Add(v) +} + fn main() -> Result<(), std::io::Error> { let mut stdout = StandardStream::stdout(ColorChoice::Always); @@ -164,64 +183,9 @@ fn main() -> Result<(), std::io::Error> { //writeln!(stdout, "Tokenized: {exp:#?}")?; - let q = treefold( - exp, - is_mult, - 2, - new_mult - ); + let mut q = treefold(exp, is_mult, 2, new_mult).unwrap(); + q = treefold(q, is_add, 2, new_add).unwrap(); writeln!(stdout, "{q:#?}")?; - - - /* - // Groups to process - let mut t_vec: VecDeque = VecDeque::with_capacity(32); - t_vec.push_back(exp); - - while t_vec.len() > 0 { - let g: tokenize::Token = t_vec.pop_front().unwrap(); - let mut g_inner: Vec = match g { - tokenize::Token::Group(x) => x, - _ => panic!() - }; - - let mut new: Vec = Vec::with_capacity(8); - - // Parse binary operators - for o in ["*", "/", "+", "-"] { - let mut i = g_inner.iter(); - loop { - let t = match i.next() { - Some(o) => o, - None => break - }; - - match t { - tokenize::Token::Operator(s) => { - if s == o { - let last = new.pop().unwrap(); - let next = i.next().unwrap(); - - new.push(tokenize::Token::Op( - String::from(s), - Box::new(last.clone()), - Box::new(next.clone()) - )) - } else { - new.push(t.clone()); - } - }, - _ => { - new.push(t.clone()); - } - } - } - g_inner = new.clone(); - new = Vec::with_capacity(8); - } - writeln!(stdout, "{:?}", g_inner)?; - } - */ } writeln!(stdout, "Exiting.")?; diff --git a/src/tokenize.rs b/src/tokenize.rs index 514316a..adb2407 100644 --- a/src/tokenize.rs +++ b/src/tokenize.rs @@ -16,8 +16,8 @@ pub enum Token { Word(String), // Only used in tree - Op(String, Box, Box), - Mult(VecDeque) + Mult(VecDeque), + Add(VecDeque) }