// ----------------------------------------------------------------------------
// evaluation of expressions
-static inline calculated_number eval_check_number(calculated_number n, int *error) {
- if(unlikely(isnan(n))) {
- *error = EVAL_ERROR_VALUE_IS_NAN;
- return 0;
- }
-
- if(unlikely(isinf(n))) {
- *error = EVAL_ERROR_VALUE_IS_INFINITE;
- return 0;
- }
-
- return n;
-}
-
static inline calculated_number eval_variable(EVAL_EXPRESSION *exp, EVAL_VARIABLE *v, int *error) {
static uint32_t this_hash = 0, now_hash = 0, after_hash = 0, before_hash = 0, status_hash = 0, removed_hash = 0, uninitialized_hash = 0, undefined_hash = 0, clear_hash = 0, warning_hash = 0, critical_hash = 0;
calculated_number n;
}
if(unlikely(v->hash == now_hash && !strcmp(v->name, "now"))) {
- n = time(NULL);
+ n = now_realtime_sec();
buffer_strcat(exp->error_msg, "[ $now = ");
print_parsed_as_constant(exp->error_msg, n);
buffer_strcat(exp->error_msg, " ] ");
}
*error = EVAL_ERROR_UNKNOWN_VARIABLE;
- buffer_sprintf(exp->error_msg, "unknown variable '%s'", v->name);
+ buffer_sprintf(exp->error_msg, "[ undefined variable '%s' ] ", v->name);
return 0;
}
break;
}
- // return eval_check_number(n, error);
return n;
}
// this is a random access array
// we always access it with a known EVAL_OPERATOR_X
- [EVAL_OPERATOR_IF_THEN_ELSE] = { "?", 1, 3, 0, eval_if_then_else },
[EVAL_OPERATOR_AND] = { "&&", 2, 2, 0, eval_and },
[EVAL_OPERATOR_OR] = { "||", 2, 2, 0, eval_or },
[EVAL_OPERATOR_GREATER_THAN_OR_EQUAL] = { ">=", 3, 2, 0, eval_greater_than_or_equal },
[EVAL_OPERATOR_SIGN_PLUS] = { "+", 6, 1, 0, eval_sign_plus },
[EVAL_OPERATOR_SIGN_MINUS] = { "-", 6, 1, 0, eval_sign_minus },
[EVAL_OPERATOR_ABS] = { "abs(",6,1, 1, eval_abs },
- [EVAL_OPERATOR_NOP] = { NULL, 7, 1, 0, eval_nop },
- [EVAL_OPERATOR_EXPRESSION_OPEN] = { NULL, 7, 1, 0, eval_nop },
+ [EVAL_OPERATOR_IF_THEN_ELSE] = { "?", 7, 3, 0, eval_if_then_else },
+ [EVAL_OPERATOR_NOP] = { NULL, 8, 1, 0, eval_nop },
+ [EVAL_OPERATOR_EXPRESSION_OPEN] = { NULL, 8, 1, 0, eval_nop },
// this should exist in our evaluation list
[EVAL_OPERATOR_EXPRESSION_CLOSE] = { NULL, 99, 1, 0, eval_nop }
calculated_number n = operators[op->operator].eval(exp, op, error);
- // return eval_check_number(n, error);
return n;
}
// high level function to parse an expression or a sub-expression
static inline EVAL_NODE *parse_full_expression(const char **string, int *error) {
- EVAL_NODE *op1 = NULL;
-
- op1 = parse_one_full_operand(string, error);
+ EVAL_NODE *op1 = parse_one_full_operand(string, error);
if(!op1) {
*error = EVAL_ERROR_MISSING_OPERAND;
return NULL;
buffer_reset(exp->error_msg);
exp->result = eval_node(exp, (EVAL_NODE *)exp->nodes, &exp->error);
- if(exp->error == EVAL_ERROR_OK)
- exp->result = eval_check_number(exp->result, &exp->error);
+ if(unlikely(isnan(exp->result))) {
+ if(exp->error == EVAL_ERROR_OK)
+ exp->error = EVAL_ERROR_VALUE_IS_NAN;
+ }
+ else if(unlikely(isinf(exp->result))) {
+ if(exp->error == EVAL_ERROR_OK)
+ exp->error = EVAL_ERROR_VALUE_IS_INFINITE;
+ }
+ else if(unlikely(exp->error == EVAL_ERROR_UNKNOWN_VARIABLE)) {
+ // although there is an unknown variable
+ // the expression was evaluated successfully
+ exp->error = EVAL_ERROR_OK;
+ }
if(exp->error != EVAL_ERROR_OK) {
exp->result = NAN;
EVAL_EXPRESSION *expression_parse(const char *string, const char **failed_at, int *error) {
const char *s = string;
int err = EVAL_ERROR_OK;
- unsigned long pos = 0;
EVAL_NODE *op = parse_full_expression(&s, &err);
if (error) *error = err;
if(!op) {
- pos = s - string + 1;
+ unsigned long pos = s - string + 1;
error("failed to parse expression '%s': %s at character %lu (i.e.: '%s').", string, expression_strerror(err), pos, s);
return NULL;
}