From 8a511e20e26410309a3a8740c6b97d9b049f0cf8 Mon Sep 17 00:00:00 2001 From: Lloyd Date: Sat, 6 Sep 2025 01:11:35 +1000 Subject: [PATCH] =?UTF-8?q?=F0=9F=99=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/src/evaluator.rs | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/backend/src/evaluator.rs b/backend/src/evaluator.rs index f23fc7c..aa95340 100644 --- a/backend/src/evaluator.rs +++ b/backend/src/evaluator.rs @@ -104,6 +104,17 @@ impl Evaluator { _ => return Err(format!("Evaluation error: Unsupported operator {:?}", op)), } } + Expr::Prefix { op, expr } => { + let val = self.evaluate_expr(expr)?; + + match op { + PrefixOp::POS => eval_pos(&val)?, + PrefixOp::NEG => eval_neg(&val)?, + PrefixOp::NOT => eval_not(&val)?, + _ => return Err(format!("Evaluation error: Unsupported operator {:?}", op)), + } + } + Expr::Group(g) => self.evaluate_expr(g)?, it => return Err(format!("Evaluation error: Unsupported expression {:?}", it)), }; @@ -194,3 +205,25 @@ where _ => None, } } + +fn eval_pos(val: &Eval) -> Result { + match val { + Eval::Literal(Literal::Integer(it)) => Ok(Eval::Literal(Literal::Integer(*it))), + Eval::Literal(Literal::Double(it)) => Ok(Eval::Literal(Literal::Double(*it))), + _ => Err("Evaluation error: expected numeric type for POS function.".to_string()), + } +} + +fn eval_neg(val: &Eval) -> Result { + match val { + Eval::Literal(Literal::Integer(it)) => Ok(Eval::Literal(Literal::Integer(-it))), + Eval::Literal(Literal::Double(it)) => Ok(Eval::Literal(Literal::Double(-it))), + _ => Err("Evaluation error: expected numeric type for NEG function.".to_string()), + } +} +fn eval_not(val: &Eval) -> Result { + match val { + Eval::Literal(Literal::Boolean(it)) => Ok(Eval::Literal(Literal::Boolean(!it))), + _ => Err("Evaluation error: expected boolean type for NEG function.".to_string()), + } +}