config: Implement AST support for const expressions.

This commit is contained in:
Gunnar Beutner 2013-10-20 15:06:05 +02:00
parent 103499bc7d
commit 599429ec93
8 changed files with 301 additions and 37 deletions

View File

@ -23,6 +23,10 @@ AM_YFLAGS = -d
config_lexer.cc: config_parser.h config_lexer.cc: config_parser.h
libconfig_la_SOURCES = \ libconfig_la_SOURCES = \
aexpression.cpp \
aexpression.h \
avalue.cpp \
avalue.h \
base-type.conf \ base-type.conf \
configcompiler.cpp \ configcompiler.cpp \
configcompiler.h \ configcompiler.h \

View File

@ -0,0 +1,69 @@
/******************************************************************************
* Icinga 2 *
* Copyright (C) 2012-2013 Icinga Development Team (http://www.icinga.org/) *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
* as published by the Free Software Foundation; either version 2 *
* of the License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the Free Software Foundation *
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
******************************************************************************/
#include "config/aexpression.h"
using namespace icinga;
AExpression::AExpression(AOperator op, const AValue& operand1)
: m_Operator(op), m_Operand1(operand1)
{
ASSERT(op == AEReturn);
}
AExpression::AExpression(AOperator op, const AValue& operand1, const AValue& operand2)
: m_Operator(op), m_Operand1(operand1), m_Operand2(operand2)
{
ASSERT(op == AEAdd || op == AESubtract || op == AEMultiply || op == AEDivide ||
op == AEBinaryAnd || op == AEBinaryOr || op == AEShiftLeft || op == AEShiftRight);
}
Value AExpression::Evaluate(const Object::Ptr& thisRef) const
{
Value left, right;
left = m_Operand1.Evaluate(thisRef);
right = m_Operand2.Evaluate(thisRef);
switch (m_Operator) {
case AEReturn:
return left;
case AEAdd:
if (left.GetType() == ValueString || right.GetType() == ValueString)
return (String)left + (String)right;
else
return (double)left + (double)right;
case AESubtract:
return (double)left + (double)right;
case AEMultiply:
return (double)left * (double)right;
case AEDivide:
return (double)left / (double)right;
case AEBinaryAnd:
return (long)left & (long)right;
case AEBinaryOr:
return (long)left | (long)right;
case AEShiftLeft:
return (long)left << (long)right;
case AEShiftRight:
return (long)left >> (long)right;
default:
ASSERT(!"Invalid operator.");
}
}

67
lib/config/aexpression.h Normal file
View File

@ -0,0 +1,67 @@
/******************************************************************************
* Icinga 2 *
* Copyright (C) 2012-2013 Icinga Development Team (http://www.icinga.org/) *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
* as published by the Free Software Foundation; either version 2 *
* of the License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the Free Software Foundation *
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
******************************************************************************/
#ifndef AEXPRESSION_H
#define AEXPRESSION_H
#include "config/i2-config.h"
#include "config/avalue.h"
#include "base/object.h"
namespace icinga
{
/**
* @ingroup config
*/
enum AOperator
{
AEReturn,
AEAdd,
AESubtract,
AEMultiply,
AEDivide,
AEBinaryAnd,
AEBinaryOr,
AEShiftLeft,
AEShiftRight
};
/**
* @ingroup config
*/
class AExpression : public Object
{
public:
DECLARE_PTR_TYPEDEFS(AExpression);
AExpression(AOperator op, const AValue& operand1);
AExpression(AOperator op, const AValue& operand1, const AValue& operand2);
Value Evaluate(const Object::Ptr& thisRef) const;
private:
AOperator m_Operator;
AValue m_Operand1;
AValue m_Operand2;
};
}
#endif /* TYPERULE_H */

52
lib/config/avalue.cpp Normal file
View File

@ -0,0 +1,52 @@
/******************************************************************************
* Icinga 2 *
* Copyright (C) 2012-2013 Icinga Development Team (http://www.icinga.org/) *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
* as published by the Free Software Foundation; either version 2 *
* of the License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the Free Software Foundation *
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
******************************************************************************/
#include "config/avalue.h"
#include "config/aexpression.h"
#include "base/scriptvariable.h"
using namespace icinga;
AValue::AValue(void)
: m_Type(ATSimple), m_Expression(NULL)
{ }
AValue::AValue(const AExpression::Ptr& expr)
: m_Type(ATExpression), m_Expression(expr)
{ }
AValue::AValue(AValueType type, const Value& value)
: m_Type(type), m_Value(value), m_Expression(NULL)
{ }
Value AValue::Evaluate(const Object::Ptr& thisRef) const
{
switch (m_Type) {
case ATSimple:
return m_Value;
case ATVariable:
return ScriptVariable::Get(m_Value);
case ATThisRef:
VERIFY(!"Not implemented.");
case ATExpression:
return m_Expression->Evaluate(thisRef);
default:
ASSERT(!"Invalid type.");
}
}

63
lib/config/avalue.h Normal file
View File

@ -0,0 +1,63 @@
/******************************************************************************
* Icinga 2 *
* Copyright (C) 2012-2013 Icinga Development Team (http://www.icinga.org/) *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
* as published by the Free Software Foundation; either version 2 *
* of the License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the Free Software Foundation *
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
******************************************************************************/
#ifndef AVALUE_H
#define AVALUE_H
#include "config/i2-config.h"
#include "base/value.h"
#include "base/object.h"
namespace icinga
{
/**
* @ingroup config
*/
enum AValueType
{
ATSimple,
ATVariable,
ATThisRef,
ATExpression
};
class AExpression;
/**
* @ingroup config
*/
class AValue
{
public:
AValue(void);
AValue(const shared_ptr<AExpression>& expr);
AValue(AValueType type, const Value& value);
Value Evaluate(const Object::Ptr& thisRef) const;
private:
AValueType m_Type;
Value m_Value;
shared_ptr<AExpression> m_Expression;
};
}
#endif /* AVALUE_H */

View File

@ -20,6 +20,8 @@
</ProjectConfiguration> </ProjectConfiguration>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="aexpression.h" />
<ClInclude Include="avalue.h" />
<ClInclude Include="configcompiler.h" /> <ClInclude Include="configcompiler.h" />
<ClInclude Include="configcompilercontext.h" /> <ClInclude Include="configcompilercontext.h" />
<ClInclude Include="configitem.h" /> <ClInclude Include="configitem.h" />
@ -34,6 +36,8 @@
<ClInclude Include="typerulelist.h" /> <ClInclude Include="typerulelist.h" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="aexpression.cpp" />
<ClCompile Include="avalue.cpp" />
<ClCompile Include="base-type.cpp"> <ClCompile Include="base-type.cpp">
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">NotUsing</PrecompiledHeader> <PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">NotUsing</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">NotUsing</PrecompiledHeader> <PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">NotUsing</PrecompiledHeader>

View File

@ -37,6 +37,12 @@
<ClCompile Include="base-type.cpp"> <ClCompile Include="base-type.cpp">
<Filter>Quelldateien</Filter> <Filter>Quelldateien</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="avalue.cpp">
<Filter>Quelldateien</Filter>
</ClCompile>
<ClCompile Include="aexpression.cpp">
<Filter>Quelldateien</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="config_parser.h"> <ClInclude Include="config_parser.h">
@ -75,6 +81,12 @@
<ClInclude Include="typerule.h"> <ClInclude Include="typerule.h">
<Filter>Headerdateien</Filter> <Filter>Headerdateien</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="avalue.h">
<Filter>Headerdateien</Filter>
</ClInclude>
<ClInclude Include="aexpression.h">
<Filter>Headerdateien</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="packages.config" /> <None Include="packages.config" />

View File

@ -26,6 +26,7 @@
#include "config/configcompilercontext.h" #include "config/configcompilercontext.h"
#include "config/typerule.h" #include "config/typerule.h"
#include "config/typerulelist.h" #include "config/typerulelist.h"
#include "config/aexpression.h"
#include "base/value.h" #include "base/value.h"
#include "base/utility.h" #include "base/utility.h"
#include "base/array.h" #include "base/array.h"
@ -61,6 +62,7 @@ using namespace icinga;
Expression *expr; Expression *expr;
ExpressionList *exprl; ExpressionList *exprl;
Array *array; Array *array;
Value *aexpr;
} }
%token <text> T_STRING %token <text> T_STRING
@ -109,8 +111,8 @@ using namespace icinga;
%type <num> partial_specifier %type <num> partial_specifier
%type <slist> object_inherits_list %type <slist> object_inherits_list
%type <slist> object_inherits_specifier %type <slist> object_inherits_specifier
%type <variant> constterm %type <aexpr> aterm
%type <variant> constexpression %type <aexpr> aexpression
%left '+' '-' %left '+' '-'
%left '*' '/' %left '*' '/'
%left '&' %left '&'
@ -535,85 +537,74 @@ simplevalue: T_STRING
} }
; ;
constterm: '(' constexpression ')' aterm: '(' aexpression ')'
{ {
$$ = $2; $$ = $2;
} }
constexpression: T_STRING aexpression: T_STRING
{ {
$$ = new Value($1); $$ = new Value(boost::make_shared<AExpression>(AEReturn, AValue(ATSimple, $1)));
free($1); free($1);
} }
| T_NUMBER | T_NUMBER
{ {
$$ = new Value($1); $$ = new Value(boost::make_shared<AExpression>(AEReturn, AValue(ATSimple, $1)));
} }
| T_IDENTIFIER | T_IDENTIFIER
{ {
$$ = new Value(ScriptVariable::Get($1)); $$ = new Value(boost::make_shared<AExpression>(AEReturn, AValue(ATVariable, $1)));
free($1); free($1);
} }
| constexpression '+' constexpression | aexpression '+' aexpression
{ {
if ($1->GetType() == ValueString || $3->GetType() == ValueString) $$ = new Value(boost::make_shared<AExpression>(AEAdd, static_cast<AExpression::Ptr>(*$1), static_cast<AExpression::Ptr>(*$3)));
$$ = new Value((String)*$1 + (String)*$3);
else
$$ = new Value((double)*$1 + (double)*$3);
delete $1; delete $1;
delete $3; delete $3;
} }
| constexpression '-' constexpression | aexpression '-' aexpression
{ {
$$ = new Value((double)*$1 - (double)*$3); $$ = new Value(boost::make_shared<AExpression>(AESubtract, static_cast<AExpression::Ptr>(*$1), static_cast<AExpression::Ptr>(*$3)));
delete $1; delete $1;
delete $3; delete $3;
} }
| constexpression '*' constexpression | aexpression '*' aexpression
{ {
$$ = new Value((double)*$1 * (double)*$3); $$ = new Value(boost::make_shared<AExpression>(AEMultiply, static_cast<AExpression::Ptr>(*$1), static_cast<AExpression::Ptr>(*$3)));
delete $1; delete $1;
delete $3; delete $3;
} }
| constexpression '/' constexpression | aexpression '/' aexpression
{ {
$$ = new Value((double)*$1 / (double)*$3); $$ = new Value(boost::make_shared<AExpression>(AEDivide, static_cast<AExpression::Ptr>(*$1), static_cast<AExpression::Ptr>(*$3)));
delete $1; delete $1;
delete $3; delete $3;
} }
| constexpression '&' constexpression | aexpression '&' aexpression
{ {
$$ = new Value((long)*$1 & (long)*$3); $$ = new Value(boost::make_shared<AExpression>(AEBinaryAnd, static_cast<AExpression::Ptr>(*$1), static_cast<AExpression::Ptr>(*$3)));
delete $1; delete $1;
delete $3; delete $3;
} }
| constexpression '|' constexpression | aexpression '|' aexpression
{ {
$$ = new Value((long)*$1 | (long)*$3); $$ = new Value(boost::make_shared<AExpression>(AEBinaryOr, static_cast<AExpression::Ptr>(*$1), static_cast<AExpression::Ptr>(*$3)));
delete $1; delete $1;
delete $3; delete $3;
} }
| constexpression T_SHIFT_LEFT constexpression | aexpression T_SHIFT_LEFT aexpression
{ {
$$ = new Value((long)*$1 << (long)*$3); $$ = new Value(boost::make_shared<AExpression>(AEShiftLeft, static_cast<AExpression::Ptr>(*$1), static_cast<AExpression::Ptr>(*$3)));
delete $1; delete $1;
delete $3; delete $3;
} }
| constexpression T_SHIFT_RIGHT constexpression | aexpression T_SHIFT_RIGHT aexpression
{ {
$$ = new Value((long)*$1 >> (long)*$3); $$ = new Value(boost::make_shared<AExpression>(AEShiftRight, static_cast<AExpression::Ptr>(*$1), static_cast<AExpression::Ptr>(*$3)));
delete $1; delete $1;
delete $3; delete $3;
} }
| '(' constexpression ')' | '(' aexpression ')'
{ {
$$ = $2; $$ = $2;
} }
@ -625,9 +616,11 @@ value: simplevalue
ExpressionList::Ptr exprl = ExpressionList::Ptr($1); ExpressionList::Ptr exprl = ExpressionList::Ptr($1);
$$ = new Value(exprl); $$ = new Value(exprl);
} }
| constterm | aterm
{ {
$$ = $1; AExpression::Ptr aexpr = *$1;
$$ = new Value(aexpr->Evaluate(Object::Ptr()));
delete $1;
} }
; ;
%% %%