[icinga-checkins] icinga.org: icinga2/fix/add-missing-suse-build-requirement-7741: Fix unary minus operator

git at icinga.org git at icinga.org
Tue Nov 25 12:10:22 CET 2014


Module: icinga2
Branch: fix/add-missing-suse-build-requirement-7741
Commit: c29d6a99ac6fdafe7d9a29c8f4452e9f09f1dca4
URL:    https://git.icinga.org/?p=icinga2.git;a=commit;h=c29d6a99ac6fdafe7d9a29c8f4452e9f09f1dca4

Author: Gunnar Beutner <gunnar.beutner at netways.de>
Date:   Sun Nov 23 23:19:46 2014 +0100

Fix unary minus operator

fixes #7815

---

 lib/config/config_lexer.ll  |   12 ++++++------
 lib/config/config_parser.yy |    5 +++++
 test/config-ops.cpp         |   12 ++++++++++++
 3 files changed, 23 insertions(+), 6 deletions(-)

diff --git a/lib/config/config_lexer.ll b/lib/config/config_lexer.ll
index 14ce03a..8b93df5 100644
--- a/lib/config/config_lexer.ll
+++ b/lib/config/config_lexer.ll
@@ -265,12 +265,12 @@ in				return T_IN;
 [a-zA-Z_][a-zA-Z0-9\-_]*	{ yylval->text = strdup(yytext); return T_IDENTIFIER; }
 @[a-zA-Z_][a-zA-Z0-9\-_]*	{ yylval->text = strdup(yytext + 1); return T_IDENTIFIER; }
 \<[^\>]*\>			{ yytext[yyleng-1] = '\0'; yylval->text = strdup(yytext + 1); return T_STRING_ANGLE; }
--?[0-9]+(\.[0-9]+)?ms		{ yylval->num = strtod(yytext, NULL) / 1000; return T_NUMBER; }
--?[0-9]+(\.[0-9]+)?d		{ yylval->num = strtod(yytext, NULL) * 60 * 60 * 24; return T_NUMBER; }
--?[0-9]+(\.[0-9]+)?h		{ yylval->num = strtod(yytext, NULL) * 60 * 60; return T_NUMBER; }
--?[0-9]+(\.[0-9]+)?m		{ yylval->num = strtod(yytext, NULL) * 60; return T_NUMBER; }
--?[0-9]+(\.[0-9]+)?s		{ yylval->num = strtod(yytext, NULL); return T_NUMBER; }
--?[0-9]+(\.[0-9]+)?		{ yylval->num = strtod(yytext, NULL); return T_NUMBER; }
+[0-9]+(\.[0-9]+)?ms		{ yylval->num = strtod(yytext, NULL) / 1000; return T_NUMBER; }
+[0-9]+(\.[0-9]+)?d		{ yylval->num = strtod(yytext, NULL) * 60 * 60 * 24; return T_NUMBER; }
+[0-9]+(\.[0-9]+)?h		{ yylval->num = strtod(yytext, NULL) * 60 * 60; return T_NUMBER; }
+[0-9]+(\.[0-9]+)?m		{ yylval->num = strtod(yytext, NULL) * 60; return T_NUMBER; }
+[0-9]+(\.[0-9]+)?s		{ yylval->num = strtod(yytext, NULL); return T_NUMBER; }
+[0-9]+(\.[0-9]+)?		{ yylval->num = strtod(yytext, NULL); return T_NUMBER; }
 =				{ yylval->csop = OpSetLiteral; return T_SET; }
 \+=				{ yylval->csop = OpSetAdd; return T_SET_ADD; }
 -=				{ yylval->csop = OpSetSubtract; return T_SET_SUBTRACT; }
diff --git a/lib/config/config_parser.yy b/lib/config/config_parser.yy
index 2b98b01..30896fd 100644
--- a/lib/config/config_parser.yy
+++ b/lib/config/config_parser.yy
@@ -208,6 +208,7 @@ static void MakeRBinaryOp(Expression** result, Expression *left, Expression *rig
 %left T_SHIFT_LEFT T_SHIFT_RIGHT
 %left T_PLUS T_MINUS
 %left T_MULTIPLY T_DIVIDE_OP
+%left UNARY_MINUS
 %right '!' '~'
 %left '.' '(' '['
 %right ';' ','
@@ -781,6 +782,10 @@ rterm_without_indexer: T_STRING
 	{
 		$$ = new NegateExpression($2, DebugInfoRange(@1, @2));
 	}
+	| T_MINUS rterm %prec UNARY_MINUS
+	{
+		$$ = new SubtractExpression(MakeLiteral(0), $2, DebugInfoRange(@1, @2));
+	}
 	| rterm_array
 	{
 		$$ = $1;
diff --git a/test/config-ops.cpp b/test/config-ops.cpp
index 4ec5d16..cffdec5 100644
--- a/test/config-ops.cpp
+++ b/test/config-ops.cpp
@@ -98,6 +98,18 @@ BOOST_AUTO_TEST_CASE(simple)
 	BOOST_CHECK(expr->Evaluate(frame) == 20);
 	delete expr;
 
+	expr = ConfigCompiler::CompileText("<test>", "2 * - 3");
+	BOOST_CHECK(expr->Evaluate(frame) == -6);
+	delete expr;
+
+	expr = ConfigCompiler::CompileText("<test>", "-(2 + 3)");
+	BOOST_CHECK(expr->Evaluate(frame) == -5);
+	delete expr;
+
+	expr = ConfigCompiler::CompileText("<test>", "- 2 * 2 - 2 * 3 - 4 * - 5");
+	BOOST_CHECK(expr->Evaluate(frame) == 10);
+	delete expr;
+
 	expr = ConfigCompiler::CompileText("<test>", "!0 == true");
 	BOOST_CHECK(expr->Evaluate(frame));
 	delete expr;



More information about the icinga-checkins mailing list