This commit is contained in:
2025-09-06 01:11:35 +10:00
parent d0163020f9
commit 8a511e20e2

View File

@@ -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<Eval, String> {
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<Eval, String> {
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<Eval, String> {
match val {
Eval::Literal(Literal::Boolean(it)) => Ok(Eval::Literal(Literal::Boolean(!it))),
_ => Err("Evaluation error: expected boolean type for NEG function.".to_string()),
}
}