From a3078cf1c1b7bf100705f27d03ddb0162e3ea640 Mon Sep 17 00:00:00 2001 From: PlexSheep Date: Tue, 14 Feb 2023 16:37:35 +0100 Subject: [PATCH] shunting yard works --- Cargo.toml | 2 +- src/expression_parser.rs | 2 +- src/expression_parser/shunting_yard.rs | 51 +++++++++++++++++++------- 3 files changed, 39 insertions(+), 16 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 39f80cf..f5d1369 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "rust_command_line_calculator" -version = "0.1.2" +version = "0.2.0" edition = "2021" authors = ["Christoph J. Scherr "] license = "GPL3" diff --git a/src/expression_parser.rs b/src/expression_parser.rs index 1268f24..79d0e81 100644 --- a/src/expression_parser.rs +++ b/src/expression_parser.rs @@ -301,7 +301,7 @@ impl Expression { match rpn { Ok(valid_rpn) => { dbg!(&valid_rpn); - return shunting_yard::calc_reverse_polish_notation(&valid_rpn); + return shunting_yard::calc_reverse_polish_notation(valid_rpn); }, Err(err) => { eprintln!("Could not calculate a result for expression '{}': {err}", self.text); diff --git a/src/expression_parser/shunting_yard.rs b/src/expression_parser/shunting_yard.rs index bb3d476..ccc03fd 100644 --- a/src/expression_parser/shunting_yard.rs +++ b/src/expression_parser/shunting_yard.rs @@ -76,10 +76,13 @@ const EXPONENTIATION: Operator = Operator { const OPERATORS: [Operator; 5] = [ADDITION, SUBTRACTION, MULTIPLICATION, DIVISION, EXPONENTIATION]; -pub fn form_reverse_polish_notation(regular_math: &str) -> Result { - let mut output_queue: Vec = Vec::new(); +pub fn form_reverse_polish_notation(regular_math: &str) -> Result, String> { + let mut output_queue: Vec> = Vec::new(); let mut input_queue: Vec = regular_math.chars().rev().collect(); let mut operator_stack: Vec = Vec::new(); + let mut currently_processing_numeric_group = false; + let mut current_numeric_group: Vec = Vec::new(); + let mut current_numeric_group_has_point = false; // while there are tokens to br read: while !(input_queue.is_empty()) { @@ -89,35 +92,48 @@ pub fn form_reverse_polish_notation(regular_math: &str) -> Result valid_op, - None => {panic!("Operator '{token}' not found.");}, + None => {panic!("Operator '{}' not found.", token);}, }; // while there is an operator o2 at the top of the stack if !operator_stack.is_empty() { - dbg!(&operator_stack); dbg!(&operator_stack); let o2 = match Operator::get_operator(*(operator_stack.clone().last().clone().unwrap())) { Some(valid_op) => valid_op, - None => {panic!("Operator '{token}' not found.");}, + None => {panic!("Operator '{}' not found.", token);}, }; // and // (o2 has greater precedence than o1 or (o1 and o2 have the same precedence and o1 // is left associative)) while ((operator_stack.last().is_some()) & ((o2.precedence > o1.precedence) | ((o1.precedence == o2.precedence) & (o1.associativity == Associativity::Left)))) { - dbg!(&operator_stack); - println!("REACHED THE MAGIC WHILE"); // pop o2 from the operator stack into the output queue. // after this debug statement, the operator_stack is empty for no reason!!!! // FIXME @@ -125,7 +141,7 @@ pub fn form_reverse_polish_notation(regular_math: &str) -> Result c, None => {panic!("weirdly gone!")}, }; - output_queue.push(my_c); + output_queue.push(vec![my_c]); } } operator_stack.push(o1.character); @@ -140,22 +156,29 @@ pub fn form_reverse_polish_notation(regular_math: &str) -> Result(); + let mut rpn: Vec = Vec::new(); + for group in output_queue { + rpn.push(group.iter().cloned().collect::()); + } Ok(rpn) } // after we have the rpn, we may want to calculate the values with it. -pub fn calc_reverse_polish_notation(rpn: &str) -> Result { +pub fn calc_reverse_polish_notation(rpn: Vec) -> Result { Ok(0.0) }