From af9e9c9797261c81ea65cfcfd62119d8da1980c9 Mon Sep 17 00:00:00 2001 From: electria Date: Mon, 13 Jul 2026 19:15:00 -0700 Subject: [PATCH] refactor: function parsing into a single loop --- src/parse.rs | 33 +++++++++++++++------------------ 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/src/parse.rs b/src/parse.rs index 18dd3c9..13a656e 100644 --- a/src/parse.rs +++ b/src/parse.rs @@ -164,28 +164,25 @@ fn collapse_toplevel(nodes: &mut Vec, mut end: usize) { .rposition(|n| *n == Node::ParenthesisStart) .unwrap_or(0); - while let Some(operator) = nodes[start..end] + while let Some((index, variant)) = nodes[start..end] .iter() - .position(|n| *n == Node::Function(Function::Sine)) + .filter_map(|n| match n { + Node::Function(f) => Some(f), + _ => None, + }) + .cloned() + .enumerate() + .next() { - match &mut nodes[start..end][operator + 1] { - Node::Number(n) => n.sin_mut(), - _ => panic!("operator is missing an operand"), + match &mut nodes[start..end][index + 1] { + Node::Number(n) => match variant { + Function::Sine => n.sin_mut(), + Function::Cosine => n.cos_mut(), + }, + _ => panic!("function is missing an argument"), }; - nodes.remove(start + operator); - end -= 1; - } - while let Some(operator) = nodes[start..end] - .iter() - .position(|n| *n == Node::Function(Function::Cosine)) - { - match &mut nodes[start..end][operator + 1] { - Node::Number(n) => n.cos_mut(), - _ => panic!("operator is missing an operand"), - }; - - nodes.remove(start + operator); + nodes.remove(start + index); end -= 1; }