Added debug code to the op_t calculation routine.
This commit is contained in:
parent
ffba456113
commit
aebfc92a4d
1 changed files with 58 additions and 30 deletions
86
src/op.cc
86
src/op.cc
|
|
@ -78,21 +78,28 @@ value_t expr_t::op_t::calc(scope_t& scope, ptr_op_t * context)
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
|
|
||||||
|
value_t result;
|
||||||
|
|
||||||
|
DEBUG("op.calc", "calculating '" << op_context(this) << "'");
|
||||||
|
|
||||||
switch (kind) {
|
switch (kind) {
|
||||||
case VALUE:
|
case VALUE:
|
||||||
return as_value();
|
result = as_value();
|
||||||
|
break;
|
||||||
|
|
||||||
case IDENT:
|
case IDENT:
|
||||||
if (! left())
|
if (! left())
|
||||||
throw_(calc_error, "Unknown identifier '" << as_ident() << "'");
|
throw_(calc_error, "Unknown identifier '" << as_ident() << "'");
|
||||||
return left()->calc(scope, context);
|
result = left()->calc(scope, context);
|
||||||
|
break;
|
||||||
|
|
||||||
case FUNCTION: {
|
case FUNCTION: {
|
||||||
// Evaluating a FUNCTION is the same as calling it directly; this happens
|
// Evaluating a FUNCTION is the same as calling it directly; this happens
|
||||||
// when certain functions-that-look-like-variables (such as "amount") are
|
// when certain functions-that-look-like-variables (such as "amount") are
|
||||||
// resolved.
|
// resolved.
|
||||||
call_scope_t call_args(scope);
|
call_scope_t call_args(scope);
|
||||||
return as_function()(call_args);
|
result = as_function()(call_args);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
case O_LOOKUP:
|
case O_LOOKUP:
|
||||||
|
|
@ -102,18 +109,19 @@ value_t expr_t::op_t::calc(scope_t& scope, ptr_op_t * context)
|
||||||
if (value_t obj = left()->left()->as_function()(call_args)) {
|
if (value_t obj = left()->left()->as_function()(call_args)) {
|
||||||
if (obj.is_pointer()) {
|
if (obj.is_pointer()) {
|
||||||
scope_t& objscope(obj.as_ref_lval<scope_t>());
|
scope_t& objscope(obj.as_ref_lval<scope_t>());
|
||||||
if (ptr_op_t member = objscope.lookup(right()->as_ident()))
|
if (ptr_op_t member = objscope.lookup(right()->as_ident())) {
|
||||||
return member->calc(objscope);
|
result = member->calc(objscope);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (right()->kind != IDENT) {
|
}
|
||||||
|
if (right()->kind != IDENT)
|
||||||
throw_(calc_error,
|
throw_(calc_error,
|
||||||
"Right operand of . operator must be an identifier");
|
"Right operand of . operator must be an identifier");
|
||||||
} else {
|
else
|
||||||
throw_(calc_error,
|
throw_(calc_error,
|
||||||
"Failed to lookup member '" << right()->as_ident() << "'");
|
"Failed to lookup member '" << right()->as_ident() << "'");
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case O_CALL: {
|
case O_CALL: {
|
||||||
|
|
@ -129,7 +137,8 @@ value_t expr_t::op_t::calc(scope_t& scope, ptr_op_t * context)
|
||||||
if (! func || func->kind != FUNCTION)
|
if (! func || func->kind != FUNCTION)
|
||||||
throw_(calc_error, "Calling non-function '" << name << "'");
|
throw_(calc_error, "Calling non-function '" << name << "'");
|
||||||
|
|
||||||
return func->as_function()(call_args);
|
result = func->as_function()(call_args);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
case O_MATCH:
|
case O_MATCH:
|
||||||
|
|
@ -137,47 +146,63 @@ value_t expr_t::op_t::calc(scope_t& scope, ptr_op_t * context)
|
||||||
if (! right()->is_value() || ! right()->as_value().is_mask())
|
if (! right()->is_value() || ! right()->as_value().is_mask())
|
||||||
throw_(calc_error, "Right-hand argument to match operator must be a regex");
|
throw_(calc_error, "Right-hand argument to match operator must be a regex");
|
||||||
#endif
|
#endif
|
||||||
return (right()->calc(scope, context).as_mask()
|
result = (right()->calc(scope, context).as_mask()
|
||||||
.match(left()->calc(scope, context).to_string()));
|
.match(left()->calc(scope, context).to_string()));
|
||||||
|
break;
|
||||||
|
|
||||||
case O_EQ:
|
case O_EQ:
|
||||||
return left()->calc(scope, context) == right()->calc(scope, context);
|
result = left()->calc(scope, context) == right()->calc(scope, context);
|
||||||
|
break;
|
||||||
case O_LT:
|
case O_LT:
|
||||||
return left()->calc(scope, context) < right()->calc(scope, context);
|
result = left()->calc(scope, context) < right()->calc(scope, context);
|
||||||
|
break;
|
||||||
case O_LTE:
|
case O_LTE:
|
||||||
return left()->calc(scope, context) <= right()->calc(scope, context);
|
result = left()->calc(scope, context) <= right()->calc(scope, context);
|
||||||
|
break;
|
||||||
case O_GT:
|
case O_GT:
|
||||||
return left()->calc(scope, context) > right()->calc(scope, context);
|
result = left()->calc(scope, context) > right()->calc(scope, context);
|
||||||
|
break;
|
||||||
case O_GTE:
|
case O_GTE:
|
||||||
return left()->calc(scope, context) >= right()->calc(scope, context);
|
result = left()->calc(scope, context) >= right()->calc(scope, context);
|
||||||
|
break;
|
||||||
|
|
||||||
case O_ADD:
|
case O_ADD:
|
||||||
return left()->calc(scope, context) + right()->calc(scope, context);
|
result = left()->calc(scope, context) + right()->calc(scope, context);
|
||||||
|
break;
|
||||||
case O_SUB:
|
case O_SUB:
|
||||||
return left()->calc(scope, context) - right()->calc(scope, context);
|
result = left()->calc(scope, context) - right()->calc(scope, context);
|
||||||
|
break;
|
||||||
case O_MUL:
|
case O_MUL:
|
||||||
return left()->calc(scope, context) * right()->calc(scope, context);
|
result = left()->calc(scope, context) * right()->calc(scope, context);
|
||||||
|
break;
|
||||||
case O_DIV:
|
case O_DIV:
|
||||||
return left()->calc(scope, context) / right()->calc(scope, context);
|
result = left()->calc(scope, context) / right()->calc(scope, context);
|
||||||
|
break;
|
||||||
|
|
||||||
case O_NEG:
|
case O_NEG:
|
||||||
return left()->calc(scope, context).negate();
|
result = left()->calc(scope, context).negate();
|
||||||
|
break;
|
||||||
|
|
||||||
case O_NOT:
|
case O_NOT:
|
||||||
return ! left()->calc(scope, context);
|
result = ! left()->calc(scope, context);
|
||||||
|
break;
|
||||||
|
|
||||||
case O_AND:
|
case O_AND:
|
||||||
return (! left()->calc(scope, context) ? value_t(false) :
|
if (left()->calc(scope, context))
|
||||||
right()->calc(scope, context));
|
result = right()->calc(scope, context);
|
||||||
|
else
|
||||||
|
result = false;
|
||||||
|
break;
|
||||||
|
|
||||||
case O_OR:
|
case O_OR:
|
||||||
if (value_t temp = left()->calc(scope, context))
|
if (value_t temp = left()->calc(scope, context))
|
||||||
return temp;
|
result = temp;
|
||||||
else
|
else
|
||||||
return right()->calc(scope, context);
|
result = right()->calc(scope, context);
|
||||||
|
break;
|
||||||
|
|
||||||
case O_COMMA: {
|
case O_COMMA: {
|
||||||
value_t result(left()->calc(scope, context));
|
value_t temp(left()->calc(scope, context));
|
||||||
|
|
||||||
ptr_op_t next = right();
|
ptr_op_t next = right();
|
||||||
while (next) {
|
while (next) {
|
||||||
|
|
@ -190,9 +215,10 @@ value_t expr_t::op_t::calc(scope_t& scope, ptr_op_t * context)
|
||||||
next = NULL;
|
next = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
result.push_back(value_op->calc(scope, context));
|
temp.push_back(value_op->calc(scope, context));
|
||||||
}
|
}
|
||||||
return result;
|
result = temp;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
case LAST:
|
case LAST:
|
||||||
|
|
@ -201,7 +227,9 @@ value_t expr_t::op_t::calc(scope_t& scope, ptr_op_t * context)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
return NULL_VALUE;
|
DEBUG("op.calc", "result is '" << result << "'");
|
||||||
|
|
||||||
|
return result;
|
||||||
|
|
||||||
}
|
}
|
||||||
catch (const std::exception& err) {
|
catch (const std::exception& err) {
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue