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:
parent
d6b775abfb
commit
69d8d111ba
1 changed files with 25 additions and 15 deletions
|
|
@ -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);
|
||||
|
|
|
|||
Loading…
Reference in a new issue