fix: recognize unary operators at ParenthesisStart
This commit is contained in:
parent
f1c6073708
commit
3d4e9d8067
1 changed files with 18 additions and 1 deletions
|
|
@ -151,7 +151,11 @@ impl FromStr for Nodes {
|
|||
} else if c == ')' {
|
||||
nodes.push(Node::ParenthesisEnd);
|
||||
} else {
|
||||
if matches!(nodes.last(), Some(Node::Operator(_)) | None) {
|
||||
if matches!(
|
||||
nodes.last(),
|
||||
Some(Node::Operator(_) | Node::ParenthesisStart) | None
|
||||
) {
|
||||
// it must be a unary operator
|
||||
state = ParsingState::ReadingNumber(i);
|
||||
} else {
|
||||
nodes.push(Node::Operator(
|
||||
|
|
@ -218,3 +222,16 @@ fn test_e() {
|
|||
],
|
||||
)
|
||||
}
|
||||
#[test]
|
||||
fn test_unary_op() {
|
||||
assert_eq!(
|
||||
Nodes::from_str("5(-5)").unwrap().as_ref(),
|
||||
&[
|
||||
Node::from(5),
|
||||
Node::Operator(Operator::Mult),
|
||||
Node::ParenthesisStart,
|
||||
Node::from(-5),
|
||||
Node::ParenthesisEnd,
|
||||
],
|
||||
)
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue