refactor: nicer operator/function eval functions

This commit is contained in:
electria 2026-10-20 17:58:57 -07:00
commit 56e8fbca99
Signed by: electria
SSH key fingerprint: SHA256:8LlB3ucPbBHqozqkhsNbaV5oG3SlzzqUj8FZDL6IPQs

View file

@ -83,7 +83,7 @@ fn collapse_toplevel(nodes: &mut Vec<Node>, mut end: usize) -> Result<(), Error>
return Err(Error::MissingArgument(node.clone())); return Err(Error::MissingArgument(node.clone()));
}; };
function.for_float()(n); function.apply_to(n);
nodes.remove(start + index); nodes.remove(start + index);
end -= 1; end -= 1;
@ -103,7 +103,7 @@ fn collapse_toplevel(nodes: &mut Vec<Node>, mut end: usize) -> Result<(), Error>
match nodes.get_mut(start + operator) { match nodes.get_mut(start + operator) {
None => return Err(Error::OutOfBounds(Nodes(nodes.clone()), start + operator)), None => return Err(Error::OutOfBounds(Nodes(nodes.clone()), start + operator)),
Some(Node::Number(n)) => $operator.for_float()(n, lhs), Some(Node::Number(n)) => $operator.apply_to(n, lhs),
Some(n) => return Err(Error::InvalidOperand(n.clone())), Some(n) => return Err(Error::InvalidOperand(n.clone())),
}; };
@ -128,31 +128,31 @@ fn collapse_toplevel(nodes: &mut Vec<Node>, mut end: usize) -> Result<(), Error>
} }
impl Function { impl Function {
fn for_float(&self) -> impl FnMut(&mut Float) { fn apply_to(&self, n: &mut Float) {
match self { match self {
Self::Sqrt => Float::sqrt_mut, Self::Sqrt => Float::sqrt_mut(n),
Self::Ln => Float::ln_mut, Self::Ln => Float::ln_mut(n),
Self::Sine => Float::sin_mut, Self::Sine => Float::sin_mut(n),
Self::ArcSine => Float::asin_mut, Self::ArcSine => Float::asin_mut(n),
Self::Cosine => Float::cos_mut, Self::Cosine => Float::cos_mut(n),
Self::ArcCosine => Float::acos_mut, Self::ArcCosine => Float::acos_mut(n),
Self::Tangent => Float::tan_mut, Self::Tangent => Float::tan_mut(n),
Self::ArcTangent => Float::atan_mut, Self::ArcTangent => Float::atan_mut(n),
Self::Secant => Float::sec_mut, Self::Secant => Float::sec_mut(n),
Self::CoSecant => Float::csc_mut, Self::CoSecant => Float::csc_mut(n),
} }
} }
} }
impl Operator { impl Operator {
fn for_float(&self) -> impl FnMut(&mut Float, Float) { fn apply_to(&self, rhs: &mut Float, lhs: Float) {
match self { match self {
Self::Plus => Float::add_from, Self::Plus => Float::add_from(rhs, lhs),
Self::Minus => Float::sub_from, Self::Minus => Float::sub_from(rhs, lhs),
Self::Mult => Float::mul_from, Self::Mult => Float::mul_from(rhs, lhs),
Self::Div => Float::div_from, Self::Div => Float::div_from(rhs, lhs),
Self::Exp => Float::pow_from, Self::Exp => Float::pow_from(rhs, lhs),
} }
} }
} }