mirror of
https://github.com/Icinga/icinga2.git
synced 2025-07-25 22:54:57 +02:00
parent
27955843c0
commit
ddc548bb5d
@ -24,6 +24,7 @@
|
|||||||
#include "base/context.h"
|
#include "base/context.h"
|
||||||
#include "base/scriptfunction.h"
|
#include "base/scriptfunction.h"
|
||||||
#include "base/scriptvariable.h"
|
#include "base/scriptvariable.h"
|
||||||
|
#include "base/utility.h"
|
||||||
#include <boost/foreach.hpp>
|
#include <boost/foreach.hpp>
|
||||||
#include <boost/exception_ptr.hpp>
|
#include <boost/exception_ptr.hpp>
|
||||||
#include <boost/exception/errinfo_nested_exception.hpp>
|
#include <boost/exception/errinfo_nested_exception.hpp>
|
||||||
@ -41,7 +42,7 @@ AExpression::AExpression(OpCallback op, const Value& operand1, const Value& oper
|
|||||||
Value AExpression::Evaluate(const Dictionary::Ptr& locals) const
|
Value AExpression::Evaluate(const Dictionary::Ptr& locals) const
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
return (this->*m_Operator)(locals);
|
return m_Operator(this, locals);
|
||||||
} catch (const std::exception& ex) {
|
} catch (const std::exception& ex) {
|
||||||
if (boost::get_error_info<boost::errinfo_nested_exception>(ex))
|
if (boost::get_error_info<boost::errinfo_nested_exception>(ex))
|
||||||
throw;
|
throw;
|
||||||
@ -59,20 +60,23 @@ void AExpression::ExtractPath(const std::vector<String>& path, const Array::Ptr&
|
|||||||
BOOST_FOREACH(const AExpression::Ptr& expr, exprl) {
|
BOOST_FOREACH(const AExpression::Ptr& expr, exprl) {
|
||||||
expr->ExtractPath(path, result);
|
expr->ExtractPath(path, result);
|
||||||
}
|
}
|
||||||
} else if (m_Operator == &AExpression::OpSet && path[0] == m_Operand1) {
|
} else if ((m_Operator == &AExpression::OpSet || m_Operator == &AExpression::OpSetPlus ||
|
||||||
|
m_Operator == &AExpression::OpSetMinus || m_Operator == &AExpression::OpSetMultiply ||
|
||||||
|
m_Operator == &AExpression::OpSetDivide) && path[0] == m_Operand1) {
|
||||||
AExpression::Ptr exprl = m_Operand2;
|
AExpression::Ptr exprl = m_Operand2;
|
||||||
|
|
||||||
if (path.size() == 1) {
|
if (path.size() == 1) {
|
||||||
result->Add(m_Operand1);
|
VERIFY(exprl->m_Operator == &AExpression::OpDict);
|
||||||
|
Array::Ptr subexprl = exprl->m_Operand1;
|
||||||
|
BOOST_FOREACH(const AExpression::Ptr& expr, subexprl) {
|
||||||
|
result->Add(expr);
|
||||||
|
}
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<String> sub_path(path.begin() + 1, path.end());
|
std::vector<String> sub_path(path.begin() + 1, path.end());
|
||||||
exprl->ExtractPath(sub_path, result);
|
exprl->ExtractPath(sub_path, result);
|
||||||
} else if (m_Operator == &AExpression::OpDict && static_cast<bool>(m_Operand2)) {
|
|
||||||
AExpression::Ptr exprl = m_Operand1;
|
|
||||||
exprl->ExtractPath(path, result);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -85,7 +89,9 @@ void AExpression::FindDebugInfoPath(const std::vector<String>& path, DebugInfo&
|
|||||||
BOOST_FOREACH(const AExpression::Ptr& expr, exprl) {
|
BOOST_FOREACH(const AExpression::Ptr& expr, exprl) {
|
||||||
expr->FindDebugInfoPath(path, result);
|
expr->FindDebugInfoPath(path, result);
|
||||||
}
|
}
|
||||||
} else if (m_Operator == &AExpression::OpSet && path[0] == m_Operand1) {
|
} else if ((m_Operator == &AExpression::OpSet || m_Operator == &AExpression::OpSetPlus ||
|
||||||
|
m_Operator == &AExpression::OpSetMinus || m_Operator == &AExpression::OpSetMultiply ||
|
||||||
|
m_Operator == &AExpression::OpSetDivide) && path[0] == m_Operand1) {
|
||||||
AExpression::Ptr exprl = m_Operand2;
|
AExpression::Ptr exprl = m_Operand2;
|
||||||
|
|
||||||
if (path.size() == 1) {
|
if (path.size() == 1) {
|
||||||
@ -94,9 +100,6 @@ void AExpression::FindDebugInfoPath(const std::vector<String>& path, DebugInfo&
|
|||||||
std::vector<String> sub_path(path.begin() + 1, path.end());
|
std::vector<String> sub_path(path.begin() + 1, path.end());
|
||||||
exprl->FindDebugInfoPath(sub_path, result);
|
exprl->FindDebugInfoPath(sub_path, result);
|
||||||
}
|
}
|
||||||
} else if (m_Operator == &AExpression::OpDict && static_cast<bool>(m_Operand2)) {
|
|
||||||
AExpression::Ptr exprl = m_Operand1;
|
|
||||||
exprl->FindDebugInfoPath(path, result);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -106,6 +109,32 @@ void AExpression::MakeInline(void)
|
|||||||
m_Operand2 = true;
|
m_Operand2 = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void AExpression::DumpOperand(std::ostream& stream, const Value& operand, int indent) {
|
||||||
|
if (operand.IsObjectType<Array>()) {
|
||||||
|
Array::Ptr arr = operand;
|
||||||
|
stream << String(indent, ' ') << "Array:\n";
|
||||||
|
BOOST_FOREACH(const Value& elem, arr) {
|
||||||
|
DumpOperand(stream, elem, indent + 1);
|
||||||
|
}
|
||||||
|
} else if (operand.IsObjectType<AExpression>()) {
|
||||||
|
AExpression::Ptr left = operand;
|
||||||
|
left->Dump(stream, indent);
|
||||||
|
} else {
|
||||||
|
stream << String(indent, ' ') << JsonSerialize(operand) << "\n";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void AExpression::Dump(std::ostream& stream, int indent)
|
||||||
|
{
|
||||||
|
String sym = Utility::GetSymbolName(reinterpret_cast<const void *>(m_Operator));
|
||||||
|
stream << String(indent, ' ') << "op: " << Utility::DemangleSymbolName(sym) << "\n";
|
||||||
|
stream << String(indent, ' ') << "left:\n";
|
||||||
|
DumpOperand(stream, m_Operand1, indent + 1);
|
||||||
|
|
||||||
|
stream << String(indent, ' ') << "right:\n";
|
||||||
|
DumpOperand(stream, m_Operand2, indent + 1);
|
||||||
|
}
|
||||||
|
|
||||||
Value AExpression::EvaluateOperand1(const Dictionary::Ptr& locals) const
|
Value AExpression::EvaluateOperand1(const Dictionary::Ptr& locals) const
|
||||||
{
|
{
|
||||||
return static_cast<AExpression::Ptr>(m_Operand1)->Evaluate(locals);
|
return static_cast<AExpression::Ptr>(m_Operand1)->Evaluate(locals);
|
||||||
@ -116,102 +145,102 @@ Value AExpression::EvaluateOperand2(const Dictionary::Ptr& locals) const
|
|||||||
return static_cast<AExpression::Ptr>(m_Operand2)->Evaluate(locals);
|
return static_cast<AExpression::Ptr>(m_Operand2)->Evaluate(locals);
|
||||||
}
|
}
|
||||||
|
|
||||||
Value AExpression::OpLiteral(const Dictionary::Ptr& locals) const
|
Value AExpression::OpLiteral(const AExpression *expr, const Dictionary::Ptr& locals)
|
||||||
{
|
{
|
||||||
return m_Operand1;
|
return expr->m_Operand1;
|
||||||
}
|
}
|
||||||
|
|
||||||
Value AExpression::OpVariable(const Dictionary::Ptr& locals) const
|
Value AExpression::OpVariable(const AExpression *expr, const Dictionary::Ptr& locals)
|
||||||
{
|
{
|
||||||
if (locals && locals->Contains(m_Operand1))
|
if (locals && locals->Contains(expr->m_Operand1))
|
||||||
return locals->Get(m_Operand1);
|
return locals->Get(expr->m_Operand1);
|
||||||
else
|
else
|
||||||
return ScriptVariable::Get(m_Operand1);
|
return ScriptVariable::Get(expr->m_Operand1);
|
||||||
}
|
}
|
||||||
|
|
||||||
Value AExpression::OpNegate(const Dictionary::Ptr& locals) const
|
Value AExpression::OpNegate(const AExpression *expr, const Dictionary::Ptr& locals)
|
||||||
{
|
{
|
||||||
return ~(long)EvaluateOperand1(locals);
|
return ~(long)expr->EvaluateOperand1(locals);
|
||||||
}
|
}
|
||||||
|
|
||||||
Value AExpression::OpAdd(const Dictionary::Ptr& locals) const
|
Value AExpression::OpAdd(const AExpression *expr, const Dictionary::Ptr& locals)
|
||||||
{
|
{
|
||||||
return EvaluateOperand1(locals) + EvaluateOperand2(locals);
|
return expr->EvaluateOperand1(locals) + expr->EvaluateOperand2(locals);
|
||||||
}
|
}
|
||||||
|
|
||||||
Value AExpression::OpSubtract(const Dictionary::Ptr& locals) const
|
Value AExpression::OpSubtract(const AExpression *expr, const Dictionary::Ptr& locals)
|
||||||
{
|
{
|
||||||
return EvaluateOperand1(locals) - EvaluateOperand2(locals);
|
return expr->EvaluateOperand1(locals) - expr->EvaluateOperand2(locals);
|
||||||
}
|
}
|
||||||
|
|
||||||
Value AExpression::OpMultiply(const Dictionary::Ptr& locals) const
|
Value AExpression::OpMultiply(const AExpression *expr, const Dictionary::Ptr& locals)
|
||||||
{
|
{
|
||||||
return EvaluateOperand1(locals) * EvaluateOperand2(locals);
|
return expr->EvaluateOperand1(locals) * expr->EvaluateOperand2(locals);
|
||||||
}
|
}
|
||||||
|
|
||||||
Value AExpression::OpDivide(const Dictionary::Ptr& locals) const
|
Value AExpression::OpDivide(const AExpression *expr, const Dictionary::Ptr& locals)
|
||||||
{
|
{
|
||||||
return EvaluateOperand1(locals) / EvaluateOperand2(locals);
|
return expr->EvaluateOperand1(locals) / expr->EvaluateOperand2(locals);
|
||||||
}
|
}
|
||||||
|
|
||||||
Value AExpression::OpBinaryAnd(const Dictionary::Ptr& locals) const
|
Value AExpression::OpBinaryAnd(const AExpression *expr, const Dictionary::Ptr& locals)
|
||||||
{
|
{
|
||||||
return EvaluateOperand1(locals) & EvaluateOperand2(locals);
|
return expr->EvaluateOperand1(locals) & expr->EvaluateOperand2(locals);
|
||||||
}
|
}
|
||||||
|
|
||||||
Value AExpression::OpBinaryOr(const Dictionary::Ptr& locals) const
|
Value AExpression::OpBinaryOr(const AExpression *expr, const Dictionary::Ptr& locals)
|
||||||
{
|
{
|
||||||
return EvaluateOperand1(locals) | EvaluateOperand2(locals);
|
return expr->EvaluateOperand1(locals) | expr->EvaluateOperand2(locals);
|
||||||
}
|
}
|
||||||
|
|
||||||
Value AExpression::OpShiftLeft(const Dictionary::Ptr& locals) const
|
Value AExpression::OpShiftLeft(const AExpression *expr, const Dictionary::Ptr& locals)
|
||||||
{
|
{
|
||||||
return EvaluateOperand1(locals) << EvaluateOperand2(locals);
|
return expr->EvaluateOperand1(locals) << expr->EvaluateOperand2(locals);
|
||||||
}
|
}
|
||||||
|
|
||||||
Value AExpression::OpShiftRight(const Dictionary::Ptr& locals) const
|
Value AExpression::OpShiftRight(const AExpression *expr, const Dictionary::Ptr& locals)
|
||||||
{
|
{
|
||||||
return EvaluateOperand1(locals) >> EvaluateOperand2(locals);
|
return expr->EvaluateOperand1(locals) >> expr->EvaluateOperand2(locals);
|
||||||
}
|
}
|
||||||
|
|
||||||
Value AExpression::OpEqual(const Dictionary::Ptr& locals) const
|
Value AExpression::OpEqual(const AExpression *expr, const Dictionary::Ptr& locals)
|
||||||
{
|
{
|
||||||
return EvaluateOperand1(locals) == EvaluateOperand2(locals);
|
return expr->EvaluateOperand1(locals) == expr->EvaluateOperand2(locals);
|
||||||
}
|
}
|
||||||
|
|
||||||
Value AExpression::OpNotEqual(const Dictionary::Ptr& locals) const
|
Value AExpression::OpNotEqual(const AExpression *expr, const Dictionary::Ptr& locals)
|
||||||
{
|
{
|
||||||
return EvaluateOperand1(locals) != EvaluateOperand2(locals);
|
return expr->EvaluateOperand1(locals) != expr->EvaluateOperand2(locals);
|
||||||
}
|
}
|
||||||
|
|
||||||
Value AExpression::OpLessThan(const Dictionary::Ptr& locals) const
|
Value AExpression::OpLessThan(const AExpression *expr, const Dictionary::Ptr& locals)
|
||||||
{
|
{
|
||||||
return EvaluateOperand1(locals) < EvaluateOperand2(locals);
|
return expr->EvaluateOperand1(locals) < expr->EvaluateOperand2(locals);
|
||||||
}
|
}
|
||||||
|
|
||||||
Value AExpression::OpGreaterThan(const Dictionary::Ptr& locals) const
|
Value AExpression::OpGreaterThan(const AExpression *expr, const Dictionary::Ptr& locals)
|
||||||
{
|
{
|
||||||
return EvaluateOperand1(locals) > EvaluateOperand2(locals);
|
return expr->EvaluateOperand1(locals) > expr->EvaluateOperand2(locals);
|
||||||
}
|
}
|
||||||
|
|
||||||
Value AExpression::OpLessThanOrEqual(const Dictionary::Ptr& locals) const
|
Value AExpression::OpLessThanOrEqual(const AExpression *expr, const Dictionary::Ptr& locals)
|
||||||
{
|
{
|
||||||
return EvaluateOperand1(locals) <= EvaluateOperand2(locals);
|
return expr->EvaluateOperand1(locals) <= expr->EvaluateOperand2(locals);
|
||||||
}
|
}
|
||||||
|
|
||||||
Value AExpression::OpGreaterThanOrEqual(const Dictionary::Ptr& locals) const
|
Value AExpression::OpGreaterThanOrEqual(const AExpression *expr, const Dictionary::Ptr& locals)
|
||||||
{
|
{
|
||||||
return EvaluateOperand1(locals) >= EvaluateOperand2(locals);
|
return expr->EvaluateOperand1(locals) >= expr->EvaluateOperand2(locals);
|
||||||
}
|
}
|
||||||
|
|
||||||
Value AExpression::OpIn(const Dictionary::Ptr& locals) const
|
Value AExpression::OpIn(const AExpression *expr, const Dictionary::Ptr& locals)
|
||||||
{
|
{
|
||||||
Value right = EvaluateOperand2(locals);
|
Value right = expr->EvaluateOperand2(locals);
|
||||||
|
|
||||||
if (!right.IsObjectType<Array>())
|
if (!right.IsObjectType<Array>())
|
||||||
BOOST_THROW_EXCEPTION(ConfigError("Invalid right side argument for 'in' operator: " + JsonSerialize(right)));
|
BOOST_THROW_EXCEPTION(ConfigError("Invalid right side argument for 'in' operator: " + JsonSerialize(right)));
|
||||||
|
|
||||||
Value left = EvaluateOperand1(locals);
|
Value left = expr->EvaluateOperand1(locals);
|
||||||
|
|
||||||
Array::Ptr arr = right;
|
Array::Ptr arr = right;
|
||||||
bool found = false;
|
bool found = false;
|
||||||
@ -225,30 +254,30 @@ Value AExpression::OpIn(const Dictionary::Ptr& locals) const
|
|||||||
return found;
|
return found;
|
||||||
}
|
}
|
||||||
|
|
||||||
Value AExpression::OpNotIn(const Dictionary::Ptr& locals) const
|
Value AExpression::OpNotIn(const AExpression *expr, const Dictionary::Ptr& locals)
|
||||||
{
|
{
|
||||||
return !OpIn(locals);
|
return !OpIn(expr, locals);
|
||||||
}
|
}
|
||||||
|
|
||||||
Value AExpression::OpLogicalAnd(const Dictionary::Ptr& locals) const
|
Value AExpression::OpLogicalAnd(const AExpression *expr, const Dictionary::Ptr& locals)
|
||||||
{
|
{
|
||||||
return EvaluateOperand1(locals).ToBool() && EvaluateOperand2(locals).ToBool();
|
return expr->EvaluateOperand1(locals).ToBool() && expr->EvaluateOperand2(locals).ToBool();
|
||||||
}
|
}
|
||||||
|
|
||||||
Value AExpression::OpLogicalOr(const Dictionary::Ptr& locals) const
|
Value AExpression::OpLogicalOr(const AExpression *expr, const Dictionary::Ptr& locals)
|
||||||
{
|
{
|
||||||
return EvaluateOperand1(locals).ToBool() || EvaluateOperand2(locals).ToBool();
|
return expr->EvaluateOperand1(locals).ToBool() || expr->EvaluateOperand2(locals).ToBool();
|
||||||
}
|
}
|
||||||
|
|
||||||
Value AExpression::OpFunctionCall(const Dictionary::Ptr& locals) const
|
Value AExpression::OpFunctionCall(const AExpression *expr, const Dictionary::Ptr& locals)
|
||||||
{
|
{
|
||||||
String funcName = m_Operand1;
|
String funcName = expr->m_Operand1;
|
||||||
ScriptFunction::Ptr func = ScriptFunctionRegistry::GetInstance()->GetItem(funcName);
|
ScriptFunction::Ptr func = ScriptFunctionRegistry::GetInstance()->GetItem(funcName);
|
||||||
|
|
||||||
if (!func)
|
if (!func)
|
||||||
BOOST_THROW_EXCEPTION(ConfigError("Function '" + funcName + "' does not exist."));
|
BOOST_THROW_EXCEPTION(ConfigError("Function '" + funcName + "' does not exist."));
|
||||||
|
|
||||||
Array::Ptr arr = EvaluateOperand2(locals);
|
Array::Ptr arr = expr->EvaluateOperand2(locals);
|
||||||
std::vector<Value> arguments;
|
std::vector<Value> arguments;
|
||||||
BOOST_FOREACH(const AExpression::Ptr& aexpr, arr) {
|
BOOST_FOREACH(const AExpression::Ptr& aexpr, arr) {
|
||||||
arguments.push_back(aexpr->Evaluate(locals));
|
arguments.push_back(aexpr->Evaluate(locals));
|
||||||
@ -257,9 +286,9 @@ Value AExpression::OpFunctionCall(const Dictionary::Ptr& locals) const
|
|||||||
return func->Invoke(arguments);
|
return func->Invoke(arguments);
|
||||||
}
|
}
|
||||||
|
|
||||||
Value AExpression::OpArray(const Dictionary::Ptr& locals) const
|
Value AExpression::OpArray(const AExpression *expr, const Dictionary::Ptr& locals)
|
||||||
{
|
{
|
||||||
Array::Ptr arr = m_Operand1;
|
Array::Ptr arr = expr->m_Operand1;
|
||||||
Array::Ptr result = make_shared<Array>();
|
Array::Ptr result = make_shared<Array>();
|
||||||
|
|
||||||
if (arr) {
|
if (arr) {
|
||||||
@ -271,10 +300,10 @@ Value AExpression::OpArray(const Dictionary::Ptr& locals) const
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
Value AExpression::OpDict(const Dictionary::Ptr& locals) const
|
Value AExpression::OpDict(const AExpression *expr, const Dictionary::Ptr& locals)
|
||||||
{
|
{
|
||||||
Array::Ptr arr = m_Operand1;
|
Array::Ptr arr = expr->m_Operand1;
|
||||||
bool in_place = m_Operand2;
|
bool in_place = expr->m_Operand2;
|
||||||
Dictionary::Ptr result = make_shared<Dictionary>();
|
Dictionary::Ptr result = make_shared<Dictionary>();
|
||||||
|
|
||||||
if (arr) {
|
if (arr) {
|
||||||
@ -286,71 +315,87 @@ Value AExpression::OpDict(const Dictionary::Ptr& locals) const
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
Value AExpression::OpSet(const Dictionary::Ptr& locals) const
|
Value AExpression::OpSet(const AExpression *expr, const Dictionary::Ptr& locals)
|
||||||
{
|
{
|
||||||
Value right = EvaluateOperand2(locals);
|
Value right = expr->EvaluateOperand2(locals);
|
||||||
locals->Set(m_Operand1, right);
|
locals->Set(expr->m_Operand1, right);
|
||||||
return right;
|
return right;
|
||||||
}
|
}
|
||||||
|
|
||||||
Value AExpression::OpSetPlus(const Dictionary::Ptr& locals) const
|
Value AExpression::OpSetPlus(const AExpression *expr, const Dictionary::Ptr& locals)
|
||||||
{
|
{
|
||||||
Value left = locals->Get(m_Operand1);
|
Value left = locals->Get(expr->m_Operand1);
|
||||||
AExpression::Ptr exp_right = m_Operand2;
|
AExpression::Ptr exp_right = expr->m_Operand2;
|
||||||
Dictionary::Ptr xlocals = locals;
|
Dictionary::Ptr xlocals = locals;
|
||||||
|
|
||||||
if (exp_right->m_Operator == &AExpression::OpDict)
|
if (exp_right->m_Operator == &AExpression::OpDict) {
|
||||||
xlocals = left;
|
xlocals = left;
|
||||||
|
|
||||||
Value result = left + EvaluateOperand2(xlocals);
|
if (!xlocals)
|
||||||
locals->Set(m_Operand1, result);
|
xlocals = make_shared<Dictionary>();
|
||||||
|
}
|
||||||
|
|
||||||
|
Value result = left + expr->EvaluateOperand2(xlocals);
|
||||||
|
locals->Set(expr->m_Operand1, result);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
Value AExpression::OpSetMinus(const Dictionary::Ptr& locals) const
|
Value AExpression::OpSetMinus(const AExpression *expr, const Dictionary::Ptr& locals)
|
||||||
{
|
{
|
||||||
Value left = locals->Get(m_Operand1);
|
Value left = locals->Get(expr->m_Operand1);
|
||||||
AExpression::Ptr exp_right = m_Operand2;
|
AExpression::Ptr exp_right = expr->m_Operand2;
|
||||||
Dictionary::Ptr xlocals = locals;
|
Dictionary::Ptr xlocals = locals;
|
||||||
|
|
||||||
if (exp_right->m_Operator == &AExpression::OpDict)
|
if (exp_right->m_Operator == &AExpression::OpDict) {
|
||||||
xlocals = left;
|
xlocals = left;
|
||||||
|
|
||||||
Value result = left - EvaluateOperand2(xlocals);
|
if (!xlocals)
|
||||||
locals->Set(m_Operand1, result);
|
xlocals = make_shared<Dictionary>();
|
||||||
|
}
|
||||||
|
|
||||||
|
Value result = left - expr->EvaluateOperand2(xlocals);
|
||||||
|
locals->Set(expr->m_Operand1, result);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
Value AExpression::OpSetMultiply(const Dictionary::Ptr& locals) const
|
Value AExpression::OpSetMultiply(const AExpression *expr, const Dictionary::Ptr& locals)
|
||||||
{
|
{
|
||||||
Value left = locals->Get(m_Operand1);
|
Value left = locals->Get(expr->m_Operand1);
|
||||||
AExpression::Ptr exp_right = m_Operand2;
|
AExpression::Ptr exp_right = expr->m_Operand2;
|
||||||
Dictionary::Ptr xlocals = locals;
|
Dictionary::Ptr xlocals = locals;
|
||||||
|
|
||||||
if (exp_right->m_Operator == &AExpression::OpDict)
|
if (exp_right->m_Operator == &AExpression::OpDict) {
|
||||||
xlocals = left;
|
xlocals = left;
|
||||||
|
|
||||||
Value result = left * EvaluateOperand2(xlocals);
|
if (!xlocals)
|
||||||
locals->Set(m_Operand1, result);
|
xlocals = make_shared<Dictionary>();
|
||||||
|
}
|
||||||
|
|
||||||
|
Value result = left * expr->EvaluateOperand2(xlocals);
|
||||||
|
locals->Set(expr->m_Operand1, result);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
Value AExpression::OpSetDivide(const Dictionary::Ptr& locals) const
|
Value AExpression::OpSetDivide(const AExpression *expr, const Dictionary::Ptr& locals)
|
||||||
{
|
{
|
||||||
Value left = locals->Get(m_Operand1);
|
Value left = locals->Get(expr->m_Operand1);
|
||||||
AExpression::Ptr exp_right = m_Operand2;
|
AExpression::Ptr exp_right = expr->m_Operand2;
|
||||||
Dictionary::Ptr xlocals = locals;
|
Dictionary::Ptr xlocals = locals;
|
||||||
|
|
||||||
if (exp_right->m_Operator == &AExpression::OpDict)
|
if (exp_right->m_Operator == &AExpression::OpDict) {
|
||||||
xlocals = left;
|
xlocals = left;
|
||||||
|
|
||||||
Value result = left / EvaluateOperand2(xlocals);
|
if (!xlocals)
|
||||||
locals->Set(m_Operand1, result);
|
xlocals = make_shared<Dictionary>();
|
||||||
|
}
|
||||||
|
|
||||||
|
Value result = left / expr->EvaluateOperand2(xlocals);
|
||||||
|
locals->Set(expr->m_Operand1, result);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
Value AExpression::OpIndexer(const Dictionary::Ptr& locals) const
|
Value AExpression::OpIndexer(const AExpression *expr, const Dictionary::Ptr& locals)
|
||||||
{
|
{
|
||||||
Dictionary::Ptr dict = locals->Get(m_Operand1);
|
Dictionary::Ptr dict = locals->Get(expr->m_Operand1);
|
||||||
return dict->Get(m_Operand2);
|
return dict->Get(expr->m_Operand2);
|
||||||
}
|
}
|
||||||
|
@ -36,7 +36,7 @@ class I2_CONFIG_API AExpression : public Object
|
|||||||
public:
|
public:
|
||||||
DECLARE_PTR_TYPEDEFS(AExpression);
|
DECLARE_PTR_TYPEDEFS(AExpression);
|
||||||
|
|
||||||
typedef Value (AExpression::*OpCallback)(const Dictionary::Ptr&) const;
|
typedef Value (*OpCallback)(const AExpression *, const Dictionary::Ptr&);
|
||||||
|
|
||||||
AExpression(OpCallback op, const Value& operand1, const DebugInfo& di);
|
AExpression(OpCallback op, const Value& operand1, const DebugInfo& di);
|
||||||
AExpression(OpCallback op, const Value& operand1, const Value& operand2, const DebugInfo& di);
|
AExpression(OpCallback op, const Value& operand1, const Value& operand2, const DebugInfo& di);
|
||||||
@ -47,36 +47,38 @@ public:
|
|||||||
|
|
||||||
void MakeInline(void);
|
void MakeInline(void);
|
||||||
|
|
||||||
Value OpLiteral(const Dictionary::Ptr& locals) const;
|
void Dump(std::ostream& stream, int indent = 0);
|
||||||
Value OpVariable(const Dictionary::Ptr& locals) const;
|
|
||||||
Value OpNegate(const Dictionary::Ptr& locals) const;
|
static Value OpLiteral(const AExpression *expr, const Dictionary::Ptr& locals);
|
||||||
Value OpAdd(const Dictionary::Ptr& locals) const;
|
static Value OpVariable(const AExpression *expr, const Dictionary::Ptr& locals);
|
||||||
Value OpSubtract(const Dictionary::Ptr& locals) const;
|
static Value OpNegate(const AExpression *expr, const Dictionary::Ptr& locals);
|
||||||
Value OpMultiply(const Dictionary::Ptr& locals) const;
|
static Value OpAdd(const AExpression *expr, const Dictionary::Ptr& locals);
|
||||||
Value OpDivide(const Dictionary::Ptr& locals) const;
|
static Value OpSubtract(const AExpression *expr, const Dictionary::Ptr& locals);
|
||||||
Value OpBinaryAnd(const Dictionary::Ptr& locals) const;
|
static Value OpMultiply(const AExpression *expr, const Dictionary::Ptr& locals);
|
||||||
Value OpBinaryOr(const Dictionary::Ptr& locals) const;
|
static Value OpDivide(const AExpression *expr, const Dictionary::Ptr& locals);
|
||||||
Value OpShiftLeft(const Dictionary::Ptr& locals) const;
|
static Value OpBinaryAnd(const AExpression *expr, const Dictionary::Ptr& locals);
|
||||||
Value OpShiftRight(const Dictionary::Ptr& locals) const;
|
static Value OpBinaryOr(const AExpression *expr, const Dictionary::Ptr& locals);
|
||||||
Value OpEqual(const Dictionary::Ptr& locals) const;
|
static Value OpShiftLeft(const AExpression *expr, const Dictionary::Ptr& locals);
|
||||||
Value OpNotEqual(const Dictionary::Ptr& locals) const;
|
static Value OpShiftRight(const AExpression *expr, const Dictionary::Ptr& locals);
|
||||||
Value OpLessThan(const Dictionary::Ptr& locals) const;
|
static Value OpEqual(const AExpression *expr, const Dictionary::Ptr& locals);
|
||||||
Value OpGreaterThan(const Dictionary::Ptr& locals) const;
|
static Value OpNotEqual(const AExpression *expr, const Dictionary::Ptr& locals);
|
||||||
Value OpLessThanOrEqual(const Dictionary::Ptr& locals) const;
|
static Value OpLessThan(const AExpression *expr, const Dictionary::Ptr& locals);
|
||||||
Value OpGreaterThanOrEqual(const Dictionary::Ptr& locals) const;
|
static Value OpGreaterThan(const AExpression *expr, const Dictionary::Ptr& locals);
|
||||||
Value OpIn(const Dictionary::Ptr& locals) const;
|
static Value OpLessThanOrEqual(const AExpression *expr, const Dictionary::Ptr& locals);
|
||||||
Value OpNotIn(const Dictionary::Ptr& locals) const;
|
static Value OpGreaterThanOrEqual(const AExpression *expr, const Dictionary::Ptr& locals);
|
||||||
Value OpLogicalAnd(const Dictionary::Ptr& locals) const;
|
static Value OpIn(const AExpression *expr, const Dictionary::Ptr& locals);
|
||||||
Value OpLogicalOr(const Dictionary::Ptr& locals) const;
|
static Value OpNotIn(const AExpression *expr, const Dictionary::Ptr& locals);
|
||||||
Value OpFunctionCall(const Dictionary::Ptr& locals) const;
|
static Value OpLogicalAnd(const AExpression *expr, const Dictionary::Ptr& locals);
|
||||||
Value OpArray(const Dictionary::Ptr& locals) const;
|
static Value OpLogicalOr(const AExpression *expr, const Dictionary::Ptr& locals);
|
||||||
Value OpDict(const Dictionary::Ptr& locals) const;
|
static Value OpFunctionCall(const AExpression *expr, const Dictionary::Ptr& locals);
|
||||||
Value OpSet(const Dictionary::Ptr& locals) const;
|
static Value OpArray(const AExpression *expr, const Dictionary::Ptr& locals);
|
||||||
Value OpSetPlus(const Dictionary::Ptr& locals) const;
|
static Value OpDict(const AExpression *expr, const Dictionary::Ptr& locals);
|
||||||
Value OpSetMinus(const Dictionary::Ptr& locals) const;
|
static Value OpSet(const AExpression *expr, const Dictionary::Ptr& locals);
|
||||||
Value OpSetMultiply(const Dictionary::Ptr& locals) const;
|
static Value OpSetPlus(const AExpression *expr, const Dictionary::Ptr& locals);
|
||||||
Value OpSetDivide(const Dictionary::Ptr& locals) const;
|
static Value OpSetMinus(const AExpression *expr, const Dictionary::Ptr& locals);
|
||||||
Value OpIndexer(const Dictionary::Ptr& locals) const;
|
static Value OpSetMultiply(const AExpression *expr, const Dictionary::Ptr& locals);
|
||||||
|
static Value OpSetDivide(const AExpression *expr, const Dictionary::Ptr& locals);
|
||||||
|
static Value OpIndexer(const AExpression *expr, const Dictionary::Ptr& locals);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
OpCallback m_Operator;
|
OpCallback m_Operator;
|
||||||
@ -86,6 +88,8 @@ private:
|
|||||||
|
|
||||||
Value EvaluateOperand1(const Dictionary::Ptr& locals) const;
|
Value EvaluateOperand1(const Dictionary::Ptr& locals) const;
|
||||||
Value EvaluateOperand2(const Dictionary::Ptr& locals) const;
|
Value EvaluateOperand2(const Dictionary::Ptr& locals) const;
|
||||||
|
|
||||||
|
static void DumpOperand(std::ostream& stream, const Value& operand, int indent);
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -520,7 +520,7 @@ lterm_items: lterm_items_inner
|
|||||||
|
|
||||||
lterm_items_inner: /* empty */
|
lterm_items_inner: /* empty */
|
||||||
{
|
{
|
||||||
$$ = NULL;
|
$$ = new Array();
|
||||||
}
|
}
|
||||||
| lterm
|
| lterm
|
||||||
{
|
{
|
||||||
@ -540,13 +540,14 @@ lterm_items_inner: /* empty */
|
|||||||
}
|
}
|
||||||
;
|
;
|
||||||
|
|
||||||
lterm: T_IDENTIFIER lbinary_op rterm
|
lterm: identifier lbinary_op rterm
|
||||||
{
|
{
|
||||||
AExpression::Ptr aexpr = static_cast<AExpression::Ptr>(*$3);
|
AExpression::Ptr aexpr = static_cast<AExpression::Ptr>(*$3);
|
||||||
if ($2 == &AExpression::OpSetPlus || $2 == &AExpression::OpSetMinus || $2 == &AExpression::OpSetMultiply || $2 == &AExpression::OpSetDivide)
|
if ($2 == &AExpression::OpSetPlus || $2 == &AExpression::OpSetMinus || $2 == &AExpression::OpSetMultiply || $2 == &AExpression::OpSetDivide)
|
||||||
aexpr->MakeInline();
|
aexpr->MakeInline();
|
||||||
|
|
||||||
$$ = new Value(make_shared<AExpression>($2, $1, aexpr, DebugInfoRange(@1, @3)));
|
$$ = new Value(make_shared<AExpression>($2, $1, aexpr, DebugInfoRange(@1, @3)));
|
||||||
|
free($1);
|
||||||
delete $3;
|
delete $3;
|
||||||
}
|
}
|
||||||
| identifier '[' T_STRING ']' lbinary_op rterm
|
| identifier '[' T_STRING ']' lbinary_op rterm
|
||||||
@ -606,7 +607,7 @@ rterm_items: rterm_items_inner
|
|||||||
|
|
||||||
rterm_items_inner: /* empty */
|
rterm_items_inner: /* empty */
|
||||||
{
|
{
|
||||||
$$ = NULL;
|
$$ = new Array();
|
||||||
}
|
}
|
||||||
| rterm
|
| rterm
|
||||||
{
|
{
|
||||||
|
Loading…
x
Reference in New Issue
Block a user