Implement len() and the operators >, >=, < and <=.

Refs #5789
This commit is contained in:
Gunnar Beutner 2014-03-19 13:25:06 +01:00
parent a0cd49e3dd
commit 46d7cf3d6a
7 changed files with 60 additions and 2 deletions

View File

@ -320,7 +320,7 @@ Simple calculations can be performed using the constant expression syntax:
check_interval = 30 + 60 check_interval = 30 + 60
} }
Valid operators include ~, !, +, -, *, /, ==, !=, in and !in. The default precedence rules can be Valid operators include ~, !, +, -, *, /, >, >=, <, <=, ==, !=, in and !in. The default precedence rules can be
overridden by grouping expressions using parentheses: overridden by grouping expressions using parentheses:
{ {

View File

@ -139,6 +139,14 @@ Value AExpression::Evaluate(const Dictionary::Ptr& locals) const
} }
return arr2; return arr2;
case AELessThan:
return (long)left < (long)right;
case AEGreaterThan:
return (long)left > (long)right;
case AELessThanOrEqual:
return (long)left <= (long)right;
case AEGreaterThanOrEqual:
return (long)left >= (long)right;
default: default:
ASSERT(!"Invalid operator."); ASSERT(!"Invalid operator.");
} }

View File

@ -50,7 +50,11 @@ enum AOperator
AELogicalAnd, AELogicalAnd,
AELogicalOr, AELogicalOr,
AEFunctionCall, AEFunctionCall,
AEArray AEArray,
AELessThan,
AEGreaterThan,
AELessThanOrEqual,
AEGreaterThanOrEqual
}; };
/** /**

View File

@ -227,6 +227,8 @@ to return T_TO;
where return T_WHERE; where return T_WHERE;
\<\< return T_SHIFT_LEFT; \<\< return T_SHIFT_LEFT;
\>\> return T_SHIFT_RIGHT; \>\> return T_SHIFT_RIGHT;
\<= return T_LESS_THAN_OR_EQUAL;
\>= return T_GREATER_THAN_OR_EQUAL;
== return T_EQUAL; == return T_EQUAL;
!= return T_NOT_EQUAL; != return T_NOT_EQUAL;
!in return T_NOT_IN; !in return T_NOT_IN;

View File

@ -86,6 +86,8 @@ using namespace icinga;
%token T_NOT_IN "!in (T_NOT_IN)" %token T_NOT_IN "!in (T_NOT_IN)"
%token T_LOGICAL_AND "&& (T_LOGICAL_AND)" %token T_LOGICAL_AND "&& (T_LOGICAL_AND)"
%token T_LOGICAL_OR "|| (T_LOGICAL_OR)" %token T_LOGICAL_OR "|| (T_LOGICAL_OR)"
%token T_LESS_THAN_OR_EQUAL "<= (T_LESS_THAN_OR_EQUAL)"
%token T_GREATER_THAN_OR_EQUAL ">= (T_GREATER_THAN_OR_EQUAL)"
%token <type> T_TYPE_DICTIONARY "dictionary (T_TYPE_DICTIONARY)" %token <type> T_TYPE_DICTIONARY "dictionary (T_TYPE_DICTIONARY)"
%token <type> T_TYPE_ARRAY "array (T_TYPE_ARRAY)" %token <type> T_TYPE_ARRAY "array (T_TYPE_ARRAY)"
%token <type> T_TYPE_NUMBER "number (T_TYPE_NUMBER)" %token <type> T_TYPE_NUMBER "number (T_TYPE_NUMBER)"
@ -627,6 +629,30 @@ aexpression: T_STRING
delete $1; delete $1;
delete $3; delete $3;
} }
| aexpression T_LESS_THAN_OR_EQUAL aexpression
{
$$ = new Value(make_shared<AExpression>(AELessThanOrEqual, static_cast<AExpression::Ptr>(*$1), static_cast<AExpression::Ptr>(*$3), yylloc));
delete $1;
delete $3;
}
| aexpression T_GREATER_THAN_OR_EQUAL aexpression
{
$$ = new Value(make_shared<AExpression>(AEGreaterThanOrEqual, static_cast<AExpression::Ptr>(*$1), static_cast<AExpression::Ptr>(*$3), yylloc));
delete $1;
delete $3;
}
| aexpression '<' aexpression
{
$$ = new Value(make_shared<AExpression>(AELessThan, static_cast<AExpression::Ptr>(*$1), static_cast<AExpression::Ptr>(*$3), yylloc));
delete $1;
delete $3;
}
| aexpression '>' aexpression
{
$$ = new Value(make_shared<AExpression>(AEGreaterThan, static_cast<AExpression::Ptr>(*$1), static_cast<AExpression::Ptr>(*$3), yylloc));
delete $1;
delete $3;
}
| aexpression T_EQUAL aexpression | aexpression T_EQUAL aexpression
{ {
$$ = new Value(make_shared<AExpression>(AEEqual, static_cast<AExpression::Ptr>(*$1), static_cast<AExpression::Ptr>(*$3), yylloc)); $$ = new Value(make_shared<AExpression>(AEEqual, static_cast<AExpression::Ptr>(*$1), static_cast<AExpression::Ptr>(*$3), yylloc));

View File

@ -20,12 +20,16 @@
#include "methods/utilityfuncs.h" #include "methods/utilityfuncs.h"
#include "base/scriptfunction.h" #include "base/scriptfunction.h"
#include "base/utility.h" #include "base/utility.h"
#include "base/convert.h"
#include "base/array.h"
#include "base/dictionary.h"
#include <boost/regex.hpp> #include <boost/regex.hpp>
using namespace icinga; using namespace icinga;
REGISTER_SCRIPTFUNCTION(regex, &UtilityFuncs::Regex); REGISTER_SCRIPTFUNCTION(regex, &UtilityFuncs::Regex);
REGISTER_SCRIPTFUNCTION(match, &Utility::Match); REGISTER_SCRIPTFUNCTION(match, &Utility::Match);
REGISTER_SCRIPTFUNCTION(len, &UtilityFuncs::Len);
bool UtilityFuncs::Regex(const String& pattern, const String& text) bool UtilityFuncs::Regex(const String& pattern, const String& text)
{ {
@ -33,3 +37,16 @@ bool UtilityFuncs::Regex(const String& pattern, const String& text)
boost::smatch what; boost::smatch what;
return boost::regex_search(text.GetData(), what, expr); return boost::regex_search(text.GetData(), what, expr);
} }
int UtilityFuncs::Len(const Value& value)
{
if (value.IsObjectType<Dictionary>()) {
Dictionary::Ptr dict = value;
return dict->GetLength();
} else if (value.IsObjectType<Array>()) {
Array::Ptr array = value;
return array->GetLength();
} else {
return Convert::ToString(value).GetLength();
}
}

View File

@ -33,6 +33,7 @@ class I2_METHODS_API UtilityFuncs
{ {
public: public:
static bool Regex(const String& pattern, const String& text); static bool Regex(const String& pattern, const String& text);
static int Len(const Value& value);
private: private:
UtilityFuncs(void); UtilityFuncs(void);