debug logs only in debug binary, precedence fix

This commit is contained in:
Christoph J. Scherr 2023-02-14 22:34:04 +01:00
parent 71d7626311
commit 698eb51be9
2 changed files with 19 additions and 4 deletions

View File

@ -221,7 +221,8 @@ impl Expression {
}
stop_at = index;
}
dbg!(&stop_at);
#[cfg(debug_assertions)]
{dbg!(&stop_at);}
// needed for none task: '1 + (1 + 1)'
let fixup = if stop_at == 0 { 0 } else { 1 };
task_text_full = possible_task.clone()[..stop_at+ fixup].chars().rev().collect::<String>();
@ -289,18 +290,24 @@ impl Expression {
std::process::exit(2);
}
};
#[cfg(debug_assertions)]{
dbg!(&child.full_text);
dbg!(&child_full_text);
}
normalized_text = normalized_text.replace(child.full_text.as_str(), child_full_text.as_str());
}
#[cfg(debug_assertions)]{
dbg!(&normalized_text);
}
// TODO Shunting yards algorithm, as we now have only calculatable values left.
// Implement this as public module in shunting_yard.rs
// self.result = MYRESULT
let rpn = shunting_yard::form_reverse_polish_notation(&normalized_text);
match rpn {
Ok(valid_rpn) => {
#[cfg(debug_assertions)]{
dbg!(&valid_rpn);
}
return shunting_yard::calc_reverse_polish_notation(valid_rpn);
},
Err(err) => {

View File

@ -78,19 +78,19 @@ const SUBTRACTION: Operator = Operator {
const MULTIPLICATION: Operator = Operator {
character: '*',
precedence: 2,
precedence: 3,
associativity: Associativity::Left
};
const DIVISION: Operator = Operator {
character: '/',
precedence: 2,
precedence: 3,
associativity: Associativity::Left
};
const EXPONENTIATION: Operator = Operator {
character: '^',
precedence: 2,
precedence: 4,
associativity: Associativity::Right
};
@ -108,6 +108,7 @@ pub fn form_reverse_polish_notation(regular_math: &str) -> Result<Vec<String>, S
while !(input_queue.is_empty()) {
// read a token
let token: char = input_queue.pop().unwrap();
#[cfg(debug_assertions)]
dbg!(&token);
// if the token is:
@ -145,6 +146,7 @@ pub fn form_reverse_polish_notation(regular_math: &str) -> Result<Vec<String>, S
// while there is an operator o2 at the top of the stack
if !operator_stack.is_empty() {
#[cfg(debug_assertions)]
dbg!(&operator_stack);
let o2 = match Operator::get_operator(*(operator_stack.clone().last().clone().unwrap())) {
Some(valid_op) => valid_op,
@ -183,6 +185,7 @@ pub fn form_reverse_polish_notation(regular_math: &str) -> Result<Vec<String>, S
if currently_processing_numeric_group {
output_queue.push(current_numeric_group);
}
#[cfg(debug_assertions)]
dbg!(&output_queue);
// afterwards, process any operators still on the operator_stack
@ -190,6 +193,7 @@ pub fn form_reverse_polish_notation(regular_math: &str) -> Result<Vec<String>, S
output_queue.push(vec![operator_stack.pop().unwrap()]);
}
#[cfg(debug_assertions)]
dbg!(&output_queue);
let mut rpn: Vec<String> = Vec::new();
for group in output_queue {
@ -231,6 +235,7 @@ pub fn calc_reverse_polish_notation(rpn: Vec<String>) -> Result<f64, String> {
let mut stack: Vec<f64> = Vec::new();
for group in rpn {
#[cfg(debug_assertions)]
dbg!(&group);
// find out what the group is, an operator, a number, or a variable.
// TODO add variables
@ -243,10 +248,12 @@ pub fn calc_reverse_polish_notation(rpn: Vec<String>) -> Result<f64, String> {
std::process::exit(2);
},
}
#[cfg(debug_assertions)]
dbg!(&stack);
}
else {
let op: Operator = Operator::get_operator(group.chars().last().unwrap()).unwrap();
#[cfg(debug_assertions)]
dbg!(&op);
let right = stack.pop().unwrap();
let left = stack.pop().unwrap();
@ -282,6 +289,7 @@ pub fn calc_reverse_polish_notation(rpn: Vec<String>) -> Result<f64, String> {
return Err("result stack empty".to_string());
}
if stack.len() > 1 {
#[cfg(debug_assertions)]
dbg!(stack);
return Err("result stack has too many results.".to_string());
}