From ba0ade22078b41597371f2618be15b491ea62cae Mon Sep 17 00:00:00 2001 From: whonore Date: Mon, 13 Dec 2021 17:41:10 -0500 Subject: [PATCH 1/2] Allow parsing unary - --- src/shunting-yard.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/shunting-yard.c b/src/shunting-yard.c index c30f821..85534f4 100644 --- a/src/shunting-yard.c +++ b/src/shunting-yard.c @@ -53,6 +53,7 @@ static const Token NO_TOKEN = {TOKEN_NONE, NULL}; static const Operator OPERATORS[] = { {"!", 1, 1, OPERATOR_UNARY, OPERATOR_RIGHT}, {"~", 1, 1, OPERATOR_UNARY, OPERATOR_RIGHT}, + {"-", 1, 1, OPERATOR_UNARY, OPERATOR_RIGHT}, {"*", 1, 2, OPERATOR_BINARY, OPERATOR_LEFT}, {"/", 1, 2, OPERATOR_BINARY, OPERATOR_LEFT}, {"%", 1, 2, OPERATOR_BINARY, OPERATOR_LEFT}, From 9bd24f827e247ba4ff5d06a8683952c7aba2aea5 Mon Sep 17 00:00:00 2001 From: whonore Date: Mon, 13 Dec 2021 17:47:33 -0500 Subject: [PATCH 2/2] Add tests --- tests/test-shunting-yard.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/test-shunting-yard.c b/tests/test-shunting-yard.c index 4345b8d..f9c636a 100644 --- a/tests/test-shunting-yard.c +++ b/tests/test-shunting-yard.c @@ -71,6 +71,15 @@ static void test_modulus() ASSERT_RESULT("5 %5", 0); } +static void test_negation() +{ + ASSERT_RESULT("-1", -1); + ASSERT_RESULT("(-1)", -1); + ASSERT_RESULT("(-1) + 1", 0); + ASSERT_RESULT("(-1) + (-1)", -2); + ASSERT_RESULT("-1 + -1", -2); + ASSERT_RESULT("-1 + --1", 0); +} static void test_functions() {