fix: recognize unary operators at ParenthesisStart

This commit is contained in:
electria 2026-10-06 12:30:13 -07:00
commit 3d4e9d8067
Signed by: electria
SSH key fingerprint: SHA256:8LlB3ucPbBHqozqkhsNbaV5oG3SlzzqUj8FZDL6IPQs

View file

@ -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,
],
)
}