refactor: improve eval error handling (start)
This commit is contained in:
parent
3d4e9d8067
commit
d6b775abfb
1 changed files with 23 additions and 16 deletions
|
|
@ -7,6 +7,8 @@ use crate::nodes::{Function, Node, Nodes, Operator};
|
|||
|
||||
#[derive(Clone, Debug, thiserror::Error)]
|
||||
pub enum Error {
|
||||
#[error("out of bounds: index {0}, {1}")]
|
||||
OutOfBounds(Nodes, usize),
|
||||
#[error("invalid operand: {0}")]
|
||||
InvalidOperand(Node),
|
||||
#[error("missing argument, got: {0}")]
|
||||
|
|
@ -48,8 +50,15 @@ fn collapse_toplevel(nodes: &mut Vec<Node>, mut end: usize) -> Result<(), Error>
|
|||
})
|
||||
.next()
|
||||
{
|
||||
match &mut nodes[start..end][index + 1] {
|
||||
Node::Number(n) => match variant {
|
||||
let Some(node) = nodes.get_mut(start + index + 1) else {
|
||||
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::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::Secant => n.sec_mut(),
|
||||
Function::CoSecant => n.csc_mut(),
|
||||
},
|
||||
n => return Err(Error::MissingArgument(n.clone())),
|
||||
};
|
||||
|
||||
nodes.remove(start + index);
|
||||
|
|
|
|||
Loading…
Reference in a new issue