refactor: function parsing into a single loop
This commit is contained in:
parent
e8dc03c761
commit
af9e9c9797
1 changed files with 15 additions and 18 deletions
33
src/parse.rs
33
src/parse.rs
|
|
@ -164,28 +164,25 @@ fn collapse_toplevel(nodes: &mut Vec<Node>, 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;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue