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] while let Some((index, variant)) = nodes[start..end]
.iter() .iter()
.filter_map(|n| match n { .enumerate()
Node::Function(f) => Some(f), .filter_map(|(i, n)| match n {
Node::Function(f) => Some((i, *f)),
_ => None, _ => None,
}) })
.cloned()
.enumerate()
.next() .next()
{ {
match &mut nodes[start..end][index + 1] { 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::Secant => n.sec_mut(),
Function::CoSecant => n.csc_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); nodes.remove(start + index);

View file

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