From 3d4e9d8067f24d8035505bfd70c657b1a4a13219 Mon Sep 17 00:00:00 2001 From: electria Date: Tue, 6 Oct 2026 12:30:13 -0700 Subject: [PATCH] fix: recognize unary operators at ParenthesisStart --- src/nodes/parse.rs | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/nodes/parse.rs b/src/nodes/parse.rs index 50d0f13..31a2088 100644 --- a/src/nodes/parse.rs +++ b/src/nodes/parse.rs @@ -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, + ], + ) +}