From 899e19591ac33a5b25b5d930d36a860e1360a4b0 Mon Sep 17 00:00:00 2001 From: Alexander Mohr Date: Tue, 1 Jul 2025 16:55:49 +0200 Subject: [PATCH] fix shift in math mode --- worf/src/lib/modes/math.rs | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/worf/src/lib/modes/math.rs b/worf/src/lib/modes/math.rs index 97504d6..6cf69c9 100644 --- a/worf/src/lib/modes/math.rs +++ b/worf/src/lib/modes/math.rs @@ -71,6 +71,7 @@ enum Token { Power, } +#[derive(Debug)] enum Value { Int(i64), Float(f64), @@ -120,21 +121,18 @@ fn tokenize(expr: &str) -> Result, String> { if i + 1 < chars.len() { match &expr[i..=i + 1] { "<<" => { - insert_implicit_multiplication(&mut tokens, last_token.as_ref()); tokens.push_back(Token::ShiftLeft); last_token = Some(Token::ShiftLeft); i += 2; continue; } ">>" => { - insert_implicit_multiplication(&mut tokens, last_token.as_ref()); tokens.push_back(Token::ShiftRight); last_token = Some(Token::ShiftRight); i += 2; continue; } "**" => { - insert_implicit_multiplication(&mut tokens, last_token.as_ref()); tokens.push_back(Token::Power); last_token = Some(Token::Power); i += 2; @@ -309,16 +307,22 @@ fn eval_expr(tokens: &mut VecDeque) -> Result { } } + // Final reduction: check if there are enough values for the remaining operators + if !ops.is_empty() && values.len() < 2 { + return Err(format!( + "Not enough values for the remaining operators (values: {values:?}, ops: {ops:?})", + )); + } while let Some(op) = ops.pop() { if let Token::Op('(') = op { return Err("Mismatched parentheses".to_owned()); } - let b = values - .pop() - .ok_or("Missing right operand in final evaluation")?; - let a = values - .pop() - .ok_or("Missing left operand in final evaluation")?; + let b = values.pop().ok_or_else(|| { + format!("Missing right operand in final evaluation (values: {values:?}, ops: {ops:?})",) + })?; + let a = values.pop().ok_or_else(|| { + format!("Missing left operand in final evaluation (values: {values:?}, ops: {ops:?})",) + })?; values.push(apply_op(&a, &b, &op)); }