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
This commit is contained in:
electria 2026-10-06 15:49:16 -07:00
commit 69d8d111ba
Signed by: electria
SSH key fingerprint: SHA256:8LlB3ucPbBHqozqkhsNbaV5oG3SlzzqUj8FZDL6IPQs

View file

@ -86,9 +86,11 @@ fn collapse_toplevel(nodes: &mut Vec<Node>, mut end: usize) -> Result<(), Error>
n => return Err(Error::InvalidOperand(n)),
};
end -= 1;
match &mut nodes[start..end][operator] {
Node::Number(n) => n.pow_from(lhs),
n => return Err(Error::InvalidOperand(n.clone())),
match nodes.get_mut(start + operator) {
None => return Err(Error::OutOfBounds(Nodes(nodes.clone()), start + operator)),
Some(Node::Number(n)) => n.pow_from(lhs),
Some(n) => return Err(Error::InvalidOperand(n.clone())),
};
nodes.remove(start + operator - 1);
@ -104,9 +106,11 @@ fn collapse_toplevel(nodes: &mut Vec<Node>, mut end: usize) -> Result<(), Error>
n => return Err(Error::InvalidOperand(n)),
};
end -= 1;
match &mut nodes[start..end][operator] {
Node::Number(n) => n.div_from(lhs),
n => return Err(Error::InvalidOperand(n.clone())),
match nodes.get_mut(start + operator) {
None => return Err(Error::OutOfBounds(Nodes(nodes.clone()), start + operator)),
Some(Node::Number(n)) => n.div_from(lhs),
Some(n) => return Err(Error::InvalidOperand(n.clone())),
};
nodes.remove(start + operator - 1);
@ -122,9 +126,11 @@ fn collapse_toplevel(nodes: &mut Vec<Node>, mut end: usize) -> Result<(), Error>
n => return Err(Error::InvalidOperand(n)),
};
end -= 1;
match &mut nodes[start..end][operator] {
Node::Number(n) => n.mul_from(lhs),
n => return Err(Error::InvalidOperand(n.clone())),
match nodes.get_mut(start + operator) {
None => return Err(Error::OutOfBounds(Nodes(nodes.clone()), start + operator)),
Some(Node::Number(n)) => n.mul_from(lhs),
Some(n) => return Err(Error::InvalidOperand(n.clone())),
};
nodes.remove(start + operator - 1);
@ -140,9 +146,11 @@ fn collapse_toplevel(nodes: &mut Vec<Node>, mut end: usize) -> Result<(), Error>
n => return Err(Error::InvalidOperand(n)),
};
end -= 1;
match &mut nodes[start..end][operator] {
Node::Number(n) => n.sub_from(lhs),
n => return Err(Error::InvalidOperand(n.clone())),
match nodes.get_mut(start + operator) {
None => return Err(Error::OutOfBounds(Nodes(nodes.clone()), start + operator)),
Some(Node::Number(n)) => n.sub_from(lhs),
Some(n) => return Err(Error::InvalidOperand(n.clone())),
};
nodes.remove(start + operator - 1);
@ -158,9 +166,11 @@ fn collapse_toplevel(nodes: &mut Vec<Node>, mut end: usize) -> Result<(), Error>
n => return Err(Error::InvalidOperand(n)),
};
end -= 1;
match &mut nodes[start..end][operator] {
Node::Number(n) => n.add_from(lhs),
n => return Err(Error::InvalidOperand(n.clone())),
match nodes.get_mut(start + operator) {
None => return Err(Error::OutOfBounds(Nodes(nodes.clone()), start + operator)),
Some(Node::Number(n)) => n.add_from(lhs),
Some(n) => return Err(Error::InvalidOperand(n.clone())),
};
nodes.remove(start + operator - 1);