Compare commits

..
Author SHA1 Message Date
69d8d111ba
refactor: improve eval error handling (end)
now most of the panics come from Vec::remove,
which can be fixed with Vec::try_remove when it's stabolized
2026-10-06 15:49:16 -07:00
d6b775abfb
refactor: improve eval error handling (start) 2026-10-06 15:49:07 -07:00

View file

@ -7,6 +7,8 @@ use crate::nodes::{Function, Node, Nodes, Operator};
#[derive(Clone, Debug, thiserror::Error)] #[derive(Clone, Debug, thiserror::Error)]
pub enum Error { pub enum Error {
#[error("out of bounds: index {0}, {1}")]
OutOfBounds(Nodes, usize),
#[error("invalid operand: {0}")] #[error("invalid operand: {0}")]
InvalidOperand(Node), InvalidOperand(Node),
#[error("missing argument, got: {0}")] #[error("missing argument, got: {0}")]
@ -48,8 +50,15 @@ fn collapse_toplevel(nodes: &mut Vec<Node>, mut end: usize) -> Result<(), Error>
}) })
.next() .next()
{ {
match &mut nodes[start..end][index + 1] { let Some(node) = nodes.get_mut(start + index + 1) else {
Node::Number(n) => match variant { return Err(Error::OutOfBounds(Nodes(nodes.clone()), start + index + 1));
};
let Node::Number(n) = node else {
return Err(Error::MissingArgument(node.clone()));
};
match variant {
Function::Sqrt => n.sqrt_mut(), Function::Sqrt => n.sqrt_mut(),
Function::Ln => n.ln_mut(), Function::Ln => n.ln_mut(),
@ -62,8 +71,6 @@ fn collapse_toplevel(nodes: &mut Vec<Node>, mut end: usize) -> Result<(), Error>
Function::ArcTangent => n.atan_mut(), Function::ArcTangent => n.atan_mut(),
Function::Secant => n.sec_mut(), Function::Secant => n.sec_mut(),
Function::CoSecant => n.csc_mut(), Function::CoSecant => n.csc_mut(),
},
n => return Err(Error::MissingArgument(n.clone())),
}; };
nodes.remove(start + index); nodes.remove(start + index);
@ -79,9 +86,11 @@ fn collapse_toplevel(nodes: &mut Vec<Node>, mut end: usize) -> Result<(), Error>
n => return Err(Error::InvalidOperand(n)), n => return Err(Error::InvalidOperand(n)),
}; };
end -= 1; end -= 1;
match &mut nodes[start..end][operator] { match nodes.get_mut(start + operator) {
Node::Number(n) => n.pow_from(lhs), None => return Err(Error::OutOfBounds(Nodes(nodes.clone()), start + operator)),
n => return Err(Error::InvalidOperand(n.clone())),
Some(Node::Number(n)) => n.pow_from(lhs),
Some(n) => return Err(Error::InvalidOperand(n.clone())),
}; };
nodes.remove(start + operator - 1); nodes.remove(start + operator - 1);
@ -97,9 +106,11 @@ fn collapse_toplevel(nodes: &mut Vec<Node>, mut end: usize) -> Result<(), Error>
n => return Err(Error::InvalidOperand(n)), n => return Err(Error::InvalidOperand(n)),
}; };
end -= 1; end -= 1;
match &mut nodes[start..end][operator] { match nodes.get_mut(start + operator) {
Node::Number(n) => n.div_from(lhs), None => return Err(Error::OutOfBounds(Nodes(nodes.clone()), start + operator)),
n => return Err(Error::InvalidOperand(n.clone())),
Some(Node::Number(n)) => n.div_from(lhs),
Some(n) => return Err(Error::InvalidOperand(n.clone())),
}; };
nodes.remove(start + operator - 1); nodes.remove(start + operator - 1);
@ -115,9 +126,11 @@ fn collapse_toplevel(nodes: &mut Vec<Node>, mut end: usize) -> Result<(), Error>
n => return Err(Error::InvalidOperand(n)), n => return Err(Error::InvalidOperand(n)),
}; };
end -= 1; end -= 1;
match &mut nodes[start..end][operator] { match nodes.get_mut(start + operator) {
Node::Number(n) => n.mul_from(lhs), None => return Err(Error::OutOfBounds(Nodes(nodes.clone()), start + operator)),
n => return Err(Error::InvalidOperand(n.clone())),
Some(Node::Number(n)) => n.mul_from(lhs),
Some(n) => return Err(Error::InvalidOperand(n.clone())),
}; };
nodes.remove(start + operator - 1); nodes.remove(start + operator - 1);
@ -133,9 +146,11 @@ fn collapse_toplevel(nodes: &mut Vec<Node>, mut end: usize) -> Result<(), Error>
n => return Err(Error::InvalidOperand(n)), n => return Err(Error::InvalidOperand(n)),
}; };
end -= 1; end -= 1;
match &mut nodes[start..end][operator] { match nodes.get_mut(start + operator) {
Node::Number(n) => n.sub_from(lhs), None => return Err(Error::OutOfBounds(Nodes(nodes.clone()), start + operator)),
n => return Err(Error::InvalidOperand(n.clone())),
Some(Node::Number(n)) => n.sub_from(lhs),
Some(n) => return Err(Error::InvalidOperand(n.clone())),
}; };
nodes.remove(start + operator - 1); nodes.remove(start + operator - 1);
@ -151,9 +166,11 @@ fn collapse_toplevel(nodes: &mut Vec<Node>, mut end: usize) -> Result<(), Error>
n => return Err(Error::InvalidOperand(n)), n => return Err(Error::InvalidOperand(n)),
}; };
end -= 1; end -= 1;
match &mut nodes[start..end][operator] { match nodes.get_mut(start + operator) {
Node::Number(n) => n.add_from(lhs), None => return Err(Error::OutOfBounds(Nodes(nodes.clone()), start + operator)),
n => return Err(Error::InvalidOperand(n.clone())),
Some(Node::Number(n)) => n.add_from(lhs),
Some(n) => return Err(Error::InvalidOperand(n.clone())),
}; };
nodes.remove(start + operator - 1); nodes.remove(start + operator - 1);