[icinga-checkins] icinga.org: icinga2/fix/add-missing-suse-build-requirement-7741: Implement the __if and __else keywords

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: 2ec499be178b7ffa18be0a871fdc907d2a4ccd0b
URL:    https://git.icinga.org/?p=icinga2.git;a=commit;h=2ec499be178b7ffa18be0a871fdc907d2a4ccd0b

Author: Gunnar Beutner <gunnar.beutner at netways.de>
Date:   Mon Nov 24 09:53:45 2014 +0100

Implement the __if and __else keywords

fixes #7824

---

 lib/config/config_lexer.ll  |    2 ++
 lib/config/config_parser.yy |   19 +++++++++++++++++++
 lib/config/expression.cpp   |    8 ++++++++
 lib/config/expression.hpp   |   23 +++++++++++++++++++++++
 4 files changed, 52 insertions(+)

diff --git a/lib/config/config_lexer.ll b/lib/config/config_lexer.ll
index 8b93df5..f24c8d2 100644
--- a/lib/config/config_lexer.ll
+++ b/lib/config/config_lexer.ll
@@ -251,6 +251,8 @@ __function			return T_FUNCTION;
 __return			return T_RETURN;
 __for				return T_FOR;
 __signal			return T_SIGNAL;
+__if				return T_IF;
+__else				return T_ELSE;
 =\>				return T_FOLLOWS;
 \<\<				return T_SHIFT_LEFT;
 \>\>				return T_SHIFT_RIGHT;
diff --git a/lib/config/config_parser.yy b/lib/config/config_parser.yy
index 92989ef..f38a255 100644
--- a/lib/config/config_parser.yy
+++ b/lib/config/config_parser.yy
@@ -165,6 +165,8 @@ static void MakeRBinaryOp(Expression** result, Expression *left, Expression *rig
 %token T_RETURN "return (T_RETURN)"
 %token T_FOR "for (T_FOR)"
 %token T_SIGNAL "signal (T_SIGNAL)"
+%token T_IF "if (T_IF)"
+%token T_ELSE "else (T_ELSE)"
 %token T_FOLLOWS "=> (T_FOLLOWS)"
 
 %type <text> identifier
@@ -855,6 +857,23 @@ rterm_without_indexer: T_STRING
 		$$ = new ForExpression($3, "", $5, aexpr, DebugInfoRange(@1, @7));
 		free($3);
 	}
+	| T_IF '(' rterm ')' rterm_scope
+	{
+		DictExpression *atrue = dynamic_cast<DictExpression *>($5);
+		atrue->MakeInline();
+
+		$$ = new ConditionalExpression($3, atrue, NULL, DebugInfoRange(@1, @5));
+	}
+	| T_IF '(' rterm ')' rterm_scope T_ELSE rterm_scope
+	{
+		DictExpression *atrue = dynamic_cast<DictExpression *>($5);
+		atrue->MakeInline();
+
+		DictExpression *afalse = dynamic_cast<DictExpression *>($7);
+		afalse->MakeInline();
+
+		$$ = new ConditionalExpression($3, atrue, afalse, DebugInfoRange(@1, @7));
+	}
 	;
 
 target_type_specifier: /* empty */
diff --git a/lib/config/expression.cpp b/lib/config/expression.cpp
index 063b922..bd5bb78 100644
--- a/lib/config/expression.cpp
+++ b/lib/config/expression.cpp
@@ -346,6 +346,14 @@ Value SetExpression::DoEvaluate(VMFrame& frame, DebugHint *dhint) const
 	return right;
 }
 
+Value ConditionalExpression::DoEvaluate(VMFrame& frame, DebugHint *dhint) const
+{
+	if (m_Condition->Evaluate(frame, dhint))
+		return m_TrueBranch->Evaluate(frame, dhint);
+	else if (m_FalseBranch)
+		return m_FalseBranch->Evaluate(frame, dhint);
+}
+
 Value ReturnExpression::DoEvaluate(VMFrame& frame, DebugHint *dhint) const
 {
 	BOOST_THROW_EXCEPTION(InterruptExecutionError(m_Operand->Evaluate(frame)));
diff --git a/lib/config/expression.hpp b/lib/config/expression.hpp
index d9d9ca5..73ea30f 100644
--- a/lib/config/expression.hpp
+++ b/lib/config/expression.hpp
@@ -556,6 +556,29 @@ private:
 
 };
 
+class I2_CONFIG_API ConditionalExpression : public DebuggableExpression
+{
+public:
+	ConditionalExpression(Expression *condition, Expression *true_branch, Expression *false_branch, const DebugInfo& debugInfo = DebugInfo())
+		: DebuggableExpression(debugInfo), m_Condition(condition), m_TrueBranch(true_branch), m_FalseBranch(false_branch)
+	{ }
+
+	~ConditionalExpression(void)
+	{
+		delete m_Condition;
+		delete m_TrueBranch;
+		delete m_FalseBranch; 
+	}
+
+protected:
+	virtual Value DoEvaluate(VMFrame& frame, DebugHint *dhint) const;
+
+private:
+	Expression *m_Condition;
+	Expression *m_TrueBranch;
+	Expression *m_FalseBranch;
+};
+
 class I2_CONFIG_API ReturnExpression : public UnaryExpression
 {
 public:



More information about the icinga-checkins mailing list