fix: flawed function parsing

enumerating after filtering made the index unusable
This commit is contained in:
electria 2026-07-16 00:22:36 -07:00
commit a88ec153ce
Signed by: electria
SSH key fingerprint: SHA256:8LlB3ucPbBHqozqkhsNbaV5oG3SlzzqUj8FZDL6IPQs
2 changed files with 8 additions and 9 deletions

View file

@ -31,12 +31,11 @@ fn collapse_toplevel(nodes: &mut Vec<Node>, mut end: usize) {
while let Some((index, variant)) = nodes[start..end]
.iter()
.filter_map(|n| match n {
Node::Function(f) => Some(f),
.enumerate()
.filter_map(|(i, n)| match n {
Node::Function(f) => Some((i, *f)),
_ => None,
})
.cloned()
.enumerate()
.next()
{
match &mut nodes[start..end][index + 1] {
@ -50,7 +49,7 @@ fn collapse_toplevel(nodes: &mut Vec<Node>, mut end: usize) {
Function::Secant => n.sec_mut(),
Function::CoSecant => n.csc_mut(),
},
_ => panic!("function is missing an argument"),
n => panic!("function is missing an argument, got: {n:?}"),
};
nodes.remove(start + index);

View file

@ -11,7 +11,7 @@ mod tests;
pub const PREC: u32 = 512;
#[derive(Default, Debug, Clone, PartialEq)]
#[derive(Debug, Default, Clone, PartialEq)]
pub struct Nodes(Vec<Node>);
impl AsRef<[Node]> for Nodes {
fn as_ref(&self) -> &[Node] {
@ -19,7 +19,7 @@ impl AsRef<[Node]> for Nodes {
}
}
#[derive(Clone, Debug, PartialEq)]
#[derive(Debug, Clone, PartialEq)]
enum Node {
Function(Function),
ParenthesisStart,
@ -28,7 +28,7 @@ enum Node {
Operator(Operator),
}
#[derive(Default, Clone, Debug, PartialEq)]
#[derive(Debug, Default, Copy, Clone, PartialEq)]
enum Operator {
Plus,
Minus,
@ -38,7 +38,7 @@ enum Operator {
Exp,
}
#[derive(Clone, Debug, PartialEq)]
#[derive(Debug, Copy, Clone, PartialEq)]
enum Function {
Sine,
ArcSine,