Compare commits
1 changed files with 31 additions and 48 deletions
|
|
@ -7,8 +7,6 @@ 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}")]
|
||||||
|
|
@ -50,27 +48,22 @@ fn collapse_toplevel(nodes: &mut Vec<Node>, mut end: usize) -> Result<(), Error>
|
||||||
})
|
})
|
||||||
.next()
|
.next()
|
||||||
{
|
{
|
||||||
let Some(node) = nodes.get_mut(start + index + 1) else {
|
match &mut nodes[start..end][index + 1] {
|
||||||
return Err(Error::OutOfBounds(Nodes(nodes.clone()), start + index + 1));
|
Node::Number(n) => match variant {
|
||||||
};
|
Function::Sqrt => n.sqrt_mut(),
|
||||||
|
|
||||||
let Node::Number(n) = node else {
|
Function::Ln => n.ln_mut(),
|
||||||
return Err(Error::MissingArgument(node.clone()));
|
|
||||||
};
|
|
||||||
|
|
||||||
match variant {
|
Function::Sine => n.sin_mut(),
|
||||||
Function::Sqrt => n.sqrt_mut(),
|
Function::ArcSine => n.asin_mut(),
|
||||||
|
Function::Cosine => n.cos_mut(),
|
||||||
Function::Ln => n.ln_mut(),
|
Function::ArcCosine => n.acos_mut(),
|
||||||
|
Function::Tangent => n.tan_mut(),
|
||||||
Function::Sine => n.sin_mut(),
|
Function::ArcTangent => n.atan_mut(),
|
||||||
Function::ArcSine => n.asin_mut(),
|
Function::Secant => n.sec_mut(),
|
||||||
Function::Cosine => n.cos_mut(),
|
Function::CoSecant => n.csc_mut(),
|
||||||
Function::ArcCosine => n.acos_mut(),
|
},
|
||||||
Function::Tangent => n.tan_mut(),
|
n => return Err(Error::MissingArgument(n.clone())),
|
||||||
Function::ArcTangent => n.atan_mut(),
|
|
||||||
Function::Secant => n.sec_mut(),
|
|
||||||
Function::CoSecant => n.csc_mut(),
|
|
||||||
};
|
};
|
||||||
|
|
||||||
nodes.remove(start + index);
|
nodes.remove(start + index);
|
||||||
|
|
@ -86,11 +79,9 @@ fn collapse_toplevel(nodes: &mut Vec<Node>, mut end: usize) -> Result<(), Error>
|
||||||
n => return Err(Error::InvalidOperand(n)),
|
n => return Err(Error::InvalidOperand(n)),
|
||||||
};
|
};
|
||||||
end -= 1;
|
end -= 1;
|
||||||
match nodes.get_mut(start + operator) {
|
match &mut nodes[start..end][operator] {
|
||||||
None => return Err(Error::OutOfBounds(Nodes(nodes.clone()), start + operator)),
|
Node::Number(n) => n.pow_from(lhs),
|
||||||
|
n => return Err(Error::InvalidOperand(n.clone())),
|
||||||
Some(Node::Number(n)) => n.pow_from(lhs),
|
|
||||||
Some(n) => return Err(Error::InvalidOperand(n.clone())),
|
|
||||||
};
|
};
|
||||||
|
|
||||||
nodes.remove(start + operator - 1);
|
nodes.remove(start + operator - 1);
|
||||||
|
|
@ -106,11 +97,9 @@ fn collapse_toplevel(nodes: &mut Vec<Node>, mut end: usize) -> Result<(), Error>
|
||||||
n => return Err(Error::InvalidOperand(n)),
|
n => return Err(Error::InvalidOperand(n)),
|
||||||
};
|
};
|
||||||
end -= 1;
|
end -= 1;
|
||||||
match nodes.get_mut(start + operator) {
|
match &mut nodes[start..end][operator] {
|
||||||
None => return Err(Error::OutOfBounds(Nodes(nodes.clone()), start + operator)),
|
Node::Number(n) => n.div_from(lhs),
|
||||||
|
n => return Err(Error::InvalidOperand(n.clone())),
|
||||||
Some(Node::Number(n)) => n.div_from(lhs),
|
|
||||||
Some(n) => return Err(Error::InvalidOperand(n.clone())),
|
|
||||||
};
|
};
|
||||||
|
|
||||||
nodes.remove(start + operator - 1);
|
nodes.remove(start + operator - 1);
|
||||||
|
|
@ -126,11 +115,9 @@ fn collapse_toplevel(nodes: &mut Vec<Node>, mut end: usize) -> Result<(), Error>
|
||||||
n => return Err(Error::InvalidOperand(n)),
|
n => return Err(Error::InvalidOperand(n)),
|
||||||
};
|
};
|
||||||
end -= 1;
|
end -= 1;
|
||||||
match nodes.get_mut(start + operator) {
|
match &mut nodes[start..end][operator] {
|
||||||
None => return Err(Error::OutOfBounds(Nodes(nodes.clone()), start + operator)),
|
Node::Number(n) => n.mul_from(lhs),
|
||||||
|
n => return Err(Error::InvalidOperand(n.clone())),
|
||||||
Some(Node::Number(n)) => n.mul_from(lhs),
|
|
||||||
Some(n) => return Err(Error::InvalidOperand(n.clone())),
|
|
||||||
};
|
};
|
||||||
|
|
||||||
nodes.remove(start + operator - 1);
|
nodes.remove(start + operator - 1);
|
||||||
|
|
@ -146,11 +133,9 @@ fn collapse_toplevel(nodes: &mut Vec<Node>, mut end: usize) -> Result<(), Error>
|
||||||
n => return Err(Error::InvalidOperand(n)),
|
n => return Err(Error::InvalidOperand(n)),
|
||||||
};
|
};
|
||||||
end -= 1;
|
end -= 1;
|
||||||
match nodes.get_mut(start + operator) {
|
match &mut nodes[start..end][operator] {
|
||||||
None => return Err(Error::OutOfBounds(Nodes(nodes.clone()), start + operator)),
|
Node::Number(n) => n.sub_from(lhs),
|
||||||
|
n => return Err(Error::InvalidOperand(n.clone())),
|
||||||
Some(Node::Number(n)) => n.sub_from(lhs),
|
|
||||||
Some(n) => return Err(Error::InvalidOperand(n.clone())),
|
|
||||||
};
|
};
|
||||||
|
|
||||||
nodes.remove(start + operator - 1);
|
nodes.remove(start + operator - 1);
|
||||||
|
|
@ -166,11 +151,9 @@ fn collapse_toplevel(nodes: &mut Vec<Node>, mut end: usize) -> Result<(), Error>
|
||||||
n => return Err(Error::InvalidOperand(n)),
|
n => return Err(Error::InvalidOperand(n)),
|
||||||
};
|
};
|
||||||
end -= 1;
|
end -= 1;
|
||||||
match nodes.get_mut(start + operator) {
|
match &mut nodes[start..end][operator] {
|
||||||
None => return Err(Error::OutOfBounds(Nodes(nodes.clone()), start + operator)),
|
Node::Number(n) => n.add_from(lhs),
|
||||||
|
n => return Err(Error::InvalidOperand(n.clone())),
|
||||||
Some(Node::Number(n)) => n.add_from(lhs),
|
|
||||||
Some(n) => return Err(Error::InvalidOperand(n.clone())),
|
|
||||||
};
|
};
|
||||||
|
|
||||||
nodes.remove(start + operator - 1);
|
nodes.remove(start + operator - 1);
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue