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()), + } +}