refactor: function parsing into a single loop

This commit is contained in:
electria 2026-07-13 19:15:00 -07:00
commit af9e9c9797
Signed by: electria
SSH key fingerprint: SHA256:8LlB3ucPbBHqozqkhsNbaV5oG3SlzzqUj8FZDL6IPQs

View file

@ -164,28 +164,25 @@ fn collapse_toplevel(nodes: &mut Vec<Node>, mut end: usize) {
.rposition(|n| *n == Node::ParenthesisStart) .rposition(|n| *n == Node::ParenthesisStart)
.unwrap_or(0); .unwrap_or(0);
while let Some(operator) = nodes[start..end] while let Some((index, variant)) = nodes[start..end]
.iter() .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] { match &mut nodes[start..end][index + 1] {
Node::Number(n) => n.sin_mut(), Node::Number(n) => match variant {
_ => panic!("operator is missing an operand"), Function::Sine => n.sin_mut(),
Function::Cosine => n.cos_mut(),
},
_ => panic!("function is missing an argument"),
}; };
nodes.remove(start + operator); nodes.remove(start + index);
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);
end -= 1; end -= 1;
} }