Implement support for local variables in AExpressions.

Refs #5789
This commit is contained in:
Gunnar Beutner 2014-03-18 08:36:40 +01:00
parent 04b8724cb8
commit 3587dbbd13
5 changed files with 14 additions and 11 deletions

View File

@ -34,12 +34,12 @@ AExpression::AExpression(AOperator op, const AValue& operand1, const AValue& ope
op == AEBinaryAnd || op == AEBinaryOr || op == AEShiftLeft || op == AEShiftRight);
}
Value AExpression::Evaluate(const Object::Ptr& thisRef) const
Value AExpression::Evaluate(const Dictionary::Ptr& locals) const
{
Value left, right;
left = m_Operand1.Evaluate(thisRef);
right = m_Operand2.Evaluate(thisRef);
left = m_Operand1.Evaluate(locals);
right = m_Operand2.Evaluate(locals);
switch (m_Operator) {
case AEReturn:

View File

@ -22,7 +22,7 @@
#include "config/i2-config.h"
#include "config/avalue.h"
#include "base/object.h"
#include "base/dictionary.h"
namespace icinga
{
@ -57,7 +57,7 @@ public:
AExpression(AOperator op, const AValue& operand1);
AExpression(AOperator op, const AValue& operand1, const AValue& operand2);
Value Evaluate(const Object::Ptr& thisRef) const;
Value Evaluate(const Dictionary::Ptr& locals) const;
private:
AOperator m_Operator;

View File

@ -35,17 +35,20 @@ AValue::AValue(AValueType type, const Value& value)
: m_Type(type), m_Value(value)
{ }
Value AValue::Evaluate(const Object::Ptr& thisRef) const
Value AValue::Evaluate(const Dictionary::Ptr& locals) const
{
switch (m_Type) {
case ATSimple:
return m_Value;
case ATVariable:
return ScriptVariable::Get(m_Value);
if (locals && locals->Contains(m_Value))
return locals->Get(m_Value);
else
return ScriptVariable::Get(m_Value);
case ATThisRef:
VERIFY(!"Not implemented.");
case ATExpression:
return m_Expression->Evaluate(thisRef);
return m_Expression->Evaluate(locals);
default:
ASSERT(!"Invalid type.");
}

View File

@ -22,7 +22,7 @@
#include "config/i2-config.h"
#include "base/value.h"
#include "base/object.h"
#include "base/dictionary.h"
namespace icinga
{
@ -50,7 +50,7 @@ public:
AValue(const shared_ptr<AExpression>& expr);
AValue(AValueType type, const Value& value);
Value Evaluate(const Object::Ptr& thisRef) const;
Value Evaluate(const Dictionary::Ptr& locals) const;
private:
AValueType m_Type;

View File

@ -668,7 +668,7 @@ value: simplevalue
| aterm
{
AExpression::Ptr aexpr = *$1;
$$ = new Value(aexpr->Evaluate(Object::Ptr()));
$$ = new Value(aexpr->Evaluate(Dictionary::Ptr()));
delete $1;
}
;