diff --git a/src/nodes/eval.rs b/src/nodes/eval.rs index d813e7c..cd37cb3 100644 --- a/src/nodes/eval.rs +++ b/src/nodes/eval.rs @@ -86,9 +86,11 @@ fn collapse_toplevel(nodes: &mut Vec, 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, 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, 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, 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, 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);