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)]
|
#[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,22 +50,27 @@ 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));
|
||||||
Function::Sqrt => n.sqrt_mut(),
|
};
|
||||||
|
|
||||||
Function::Ln => n.ln_mut(),
|
let Node::Number(n) = node else {
|
||||||
|
return Err(Error::MissingArgument(node.clone()));
|
||||||
|
};
|
||||||
|
|
||||||
Function::Sine => n.sin_mut(),
|
match variant {
|
||||||
Function::ArcSine => n.asin_mut(),
|
Function::Sqrt => n.sqrt_mut(),
|
||||||
Function::Cosine => n.cos_mut(),
|
|
||||||
Function::ArcCosine => n.acos_mut(),
|
Function::Ln => n.ln_mut(),
|
||||||
Function::Tangent => n.tan_mut(),
|
|
||||||
Function::ArcTangent => n.atan_mut(),
|
Function::Sine => n.sin_mut(),
|
||||||
Function::Secant => n.sec_mut(),
|
Function::ArcSine => n.asin_mut(),
|
||||||
Function::CoSecant => n.csc_mut(),
|
Function::Cosine => n.cos_mut(),
|
||||||
},
|
Function::ArcCosine => n.acos_mut(),
|
||||||
n => return Err(Error::MissingArgument(n.clone())),
|
Function::Tangent => n.tan_mut(),
|
||||||
|
Function::ArcTangent => n.atan_mut(),
|
||||||
|
Function::Secant => n.sec_mut(),
|
||||||
|
Function::CoSecant => n.csc_mut(),
|
||||||
};
|
};
|
||||||
|
|
||||||
nodes.remove(start + index);
|
nodes.remove(start + index);
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue