3 // ----------------------------------------------------------------------------
4 // data structures for storing the parsed expression in memory
6 typedef struct eval_value {
10 calculated_number number;
11 EVAL_VARIABLE *variable;
12 struct eval_node *expression;
16 typedef struct eval_node {
18 unsigned char operator;
25 // these are used for EVAL_NODE.operator
26 // they are used as internal IDs to identify an operator
27 // THEY ARE NOT USED FOR PARSING OPERATORS LIKE THAT
28 #define EVAL_OPERATOR_NOP '\0'
29 #define EVAL_OPERATOR_EXPRESSION_OPEN '('
30 #define EVAL_OPERATOR_EXPRESSION_CLOSE ')'
31 #define EVAL_OPERATOR_NOT '!'
32 #define EVAL_OPERATOR_PLUS '+'
33 #define EVAL_OPERATOR_MINUS '-'
34 #define EVAL_OPERATOR_AND '&'
35 #define EVAL_OPERATOR_OR '|'
36 #define EVAL_OPERATOR_GREATER_THAN_OR_EQUAL 'G'
37 #define EVAL_OPERATOR_LESS_THAN_OR_EQUAL 'L'
38 #define EVAL_OPERATOR_NOT_EQUAL '~'
39 #define EVAL_OPERATOR_EQUAL '='
40 #define EVAL_OPERATOR_LESS '<'
41 #define EVAL_OPERATOR_GREATER '>'
42 #define EVAL_OPERATOR_MULTIPLY '*'
43 #define EVAL_OPERATOR_DIVIDE '/'
44 #define EVAL_OPERATOR_SIGN_PLUS 'P'
45 #define EVAL_OPERATOR_SIGN_MINUS 'M'
46 #define EVAL_OPERATOR_ABS 'A'
47 #define EVAL_OPERATOR_IF_THEN_ELSE '?'
49 // ----------------------------------------------------------------------------
50 // forward function definitions
52 static inline void eval_node_free(EVAL_NODE *op);
53 static inline EVAL_NODE *parse_full_expression(const char **string, int *error);
54 static inline EVAL_NODE *parse_one_full_operand(const char **string, int *error);
55 static inline calculated_number eval_node(EVAL_EXPRESSION *exp, EVAL_NODE *op, int *error);
56 static inline void print_parsed_as_node(BUFFER *out, EVAL_NODE *op, int *error);
57 static inline void print_parsed_as_constant(BUFFER *out, calculated_number n);
59 // ----------------------------------------------------------------------------
60 // evaluation of expressions
62 static inline calculated_number eval_variable(EVAL_EXPRESSION *exp, EVAL_VARIABLE *v, int *error) {
63 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;
66 if(unlikely(this_hash == 0)) {
67 this_hash = simple_hash("this");
68 now_hash = simple_hash("now");
69 after_hash = simple_hash("after");
70 before_hash = simple_hash("before");
71 status_hash = simple_hash("status");
72 removed_hash = simple_hash("REMOVED");
73 uninitialized_hash = simple_hash("UNINITIALIZED");
74 undefined_hash = simple_hash("UNDEFINED");
75 clear_hash = simple_hash("CLEAR");
76 warning_hash = simple_hash("WARNING");
77 critical_hash = simple_hash("CRITICAL");
80 if(unlikely(v->hash == this_hash && !strcmp(v->name, "this"))) {
81 n = (exp->this)?*exp->this:NAN;
82 buffer_strcat(exp->error_msg, "[ $this = ");
83 print_parsed_as_constant(exp->error_msg, n);
84 buffer_strcat(exp->error_msg, " ] ");
88 if(unlikely(v->hash == after_hash && !strcmp(v->name, "after"))) {
89 n = (exp->after && *exp->after)?*exp->after:NAN;
90 buffer_strcat(exp->error_msg, "[ $after = ");
91 print_parsed_as_constant(exp->error_msg, n);
92 buffer_strcat(exp->error_msg, " ] ");
96 if(unlikely(v->hash == before_hash && !strcmp(v->name, "before"))) {
97 n = (exp->before && *exp->before)?*exp->before:NAN;
98 buffer_strcat(exp->error_msg, "[ $before = ");
99 print_parsed_as_constant(exp->error_msg, n);
100 buffer_strcat(exp->error_msg, " ] ");
104 if(unlikely(v->hash == now_hash && !strcmp(v->name, "now"))) {
105 n = now_realtime_sec();
106 buffer_strcat(exp->error_msg, "[ $now = ");
107 print_parsed_as_constant(exp->error_msg, n);
108 buffer_strcat(exp->error_msg, " ] ");
112 if(unlikely(v->hash == status_hash && !strcmp(v->name, "status"))) {
113 n = (exp->status)?*exp->status:RRDCALC_STATUS_UNINITIALIZED;
114 buffer_strcat(exp->error_msg, "[ $status = ");
115 print_parsed_as_constant(exp->error_msg, n);
116 buffer_strcat(exp->error_msg, " ] ");
120 if(unlikely(v->hash == removed_hash && !strcmp(v->name, "REMOVED"))) {
121 n = RRDCALC_STATUS_REMOVED;
122 buffer_strcat(exp->error_msg, "[ $REMOVED = ");
123 print_parsed_as_constant(exp->error_msg, n);
124 buffer_strcat(exp->error_msg, " ] ");
128 if(unlikely(v->hash == uninitialized_hash && !strcmp(v->name, "UNINITIALIZED"))) {
129 n = RRDCALC_STATUS_UNINITIALIZED;
130 buffer_strcat(exp->error_msg, "[ $UNINITIALIZED = ");
131 print_parsed_as_constant(exp->error_msg, n);
132 buffer_strcat(exp->error_msg, " ] ");
136 if(unlikely(v->hash == undefined_hash && !strcmp(v->name, "UNDEFINED"))) {
137 n = RRDCALC_STATUS_UNDEFINED;
138 buffer_strcat(exp->error_msg, "[ $UNDEFINED = ");
139 print_parsed_as_constant(exp->error_msg, n);
140 buffer_strcat(exp->error_msg, " ] ");
144 if(unlikely(v->hash == clear_hash && !strcmp(v->name, "CLEAR"))) {
145 n = RRDCALC_STATUS_CLEAR;
146 buffer_strcat(exp->error_msg, "[ $CLEAR = ");
147 print_parsed_as_constant(exp->error_msg, n);
148 buffer_strcat(exp->error_msg, " ] ");
152 if(unlikely(v->hash == warning_hash && !strcmp(v->name, "WARNING"))) {
153 n = RRDCALC_STATUS_WARNING;
154 buffer_strcat(exp->error_msg, "[ $WARNING = ");
155 print_parsed_as_constant(exp->error_msg, n);
156 buffer_strcat(exp->error_msg, " ] ");
160 if(unlikely(v->hash == critical_hash && !strcmp(v->name, "CRITICAL"))) {
161 n = RRDCALC_STATUS_CRITICAL;
162 buffer_strcat(exp->error_msg, "[ $CRITICAL = ");
163 print_parsed_as_constant(exp->error_msg, n);
164 buffer_strcat(exp->error_msg, " ] ");
168 if(exp->rrdcalc && health_variable_lookup(v->name, v->hash, exp->rrdcalc, &n)) {
169 buffer_sprintf(exp->error_msg, "[ $%s = ", v->name);
170 print_parsed_as_constant(exp->error_msg, n);
171 buffer_strcat(exp->error_msg, " ] ");
175 *error = EVAL_ERROR_UNKNOWN_VARIABLE;
176 buffer_sprintf(exp->error_msg, "[ undefined variable '%s' ] ", v->name);
180 static inline calculated_number eval_value(EVAL_EXPRESSION *exp, EVAL_VALUE *v, int *error) {
184 case EVAL_VALUE_EXPRESSION:
185 n = eval_node(exp, v->expression, error);
188 case EVAL_VALUE_NUMBER:
192 case EVAL_VALUE_VARIABLE:
193 n = eval_variable(exp, v->variable, error);
197 *error = EVAL_ERROR_INVALID_VALUE;
205 static inline int is_true(calculated_number n) {
206 if(isnan(n)) return 0;
207 if(isinf(n)) return 1;
212 calculated_number eval_and(EVAL_EXPRESSION *exp, EVAL_NODE *op, int *error) {
213 return is_true(eval_value(exp, &op->ops[0], error)) && is_true(eval_value(exp, &op->ops[1], error));
215 calculated_number eval_or(EVAL_EXPRESSION *exp, EVAL_NODE *op, int *error) {
216 return is_true(eval_value(exp, &op->ops[0], error)) || is_true(eval_value(exp, &op->ops[1], error));
218 calculated_number eval_greater_than_or_equal(EVAL_EXPRESSION *exp, EVAL_NODE *op, int *error) {
219 calculated_number n1 = eval_value(exp, &op->ops[0], error);
220 calculated_number n2 = eval_value(exp, &op->ops[1], error);
221 return isgreaterequal(n1, n2);
223 calculated_number eval_less_than_or_equal(EVAL_EXPRESSION *exp, EVAL_NODE *op, int *error) {
224 calculated_number n1 = eval_value(exp, &op->ops[0], error);
225 calculated_number n2 = eval_value(exp, &op->ops[1], error);
226 return islessequal(n1, n2);
228 calculated_number eval_equal(EVAL_EXPRESSION *exp, EVAL_NODE *op, int *error) {
229 calculated_number n1 = eval_value(exp, &op->ops[0], error);
230 calculated_number n2 = eval_value(exp, &op->ops[1], error);
231 if(isnan(n1) && isnan(n2)) return 1;
232 if(isinf(n1) && isinf(n2)) return 1;
233 if(isnan(n1) || isnan(n2)) return 0;
234 if(isinf(n1) || isinf(n2)) return 0;
237 calculated_number eval_not_equal(EVAL_EXPRESSION *exp, EVAL_NODE *op, int *error) {
238 return !eval_equal(exp, op, error);
240 calculated_number eval_less(EVAL_EXPRESSION *exp, EVAL_NODE *op, int *error) {
241 calculated_number n1 = eval_value(exp, &op->ops[0], error);
242 calculated_number n2 = eval_value(exp, &op->ops[1], error);
243 return isless(n1, n2);
245 calculated_number eval_greater(EVAL_EXPRESSION *exp, EVAL_NODE *op, int *error) {
246 calculated_number n1 = eval_value(exp, &op->ops[0], error);
247 calculated_number n2 = eval_value(exp, &op->ops[1], error);
248 return isgreater(n1, n2);
250 calculated_number eval_plus(EVAL_EXPRESSION *exp, EVAL_NODE *op, int *error) {
251 calculated_number n1 = eval_value(exp, &op->ops[0], error);
252 calculated_number n2 = eval_value(exp, &op->ops[1], error);
253 if(isnan(n1) || isnan(n2)) return NAN;
254 if(isinf(n1) || isinf(n2)) return INFINITY;
257 calculated_number eval_minus(EVAL_EXPRESSION *exp, EVAL_NODE *op, int *error) {
258 calculated_number n1 = eval_value(exp, &op->ops[0], error);
259 calculated_number n2 = eval_value(exp, &op->ops[1], error);
260 if(isnan(n1) || isnan(n2)) return NAN;
261 if(isinf(n1) || isinf(n2)) return INFINITY;
264 calculated_number eval_multiply(EVAL_EXPRESSION *exp, EVAL_NODE *op, int *error) {
265 calculated_number n1 = eval_value(exp, &op->ops[0], error);
266 calculated_number n2 = eval_value(exp, &op->ops[1], error);
267 if(isnan(n1) || isnan(n2)) return NAN;
268 if(isinf(n1) || isinf(n2)) return INFINITY;
271 calculated_number eval_divide(EVAL_EXPRESSION *exp, EVAL_NODE *op, int *error) {
272 calculated_number n1 = eval_value(exp, &op->ops[0], error);
273 calculated_number n2 = eval_value(exp, &op->ops[1], error);
274 if(isnan(n1) || isnan(n2)) return NAN;
275 if(isinf(n1) || isinf(n2)) return INFINITY;
278 calculated_number eval_nop(EVAL_EXPRESSION *exp, EVAL_NODE *op, int *error) {
279 return eval_value(exp, &op->ops[0], error);
281 calculated_number eval_not(EVAL_EXPRESSION *exp, EVAL_NODE *op, int *error) {
282 return !is_true(eval_value(exp, &op->ops[0], error));
284 calculated_number eval_sign_plus(EVAL_EXPRESSION *exp, EVAL_NODE *op, int *error) {
285 return eval_value(exp, &op->ops[0], error);
287 calculated_number eval_sign_minus(EVAL_EXPRESSION *exp, EVAL_NODE *op, int *error) {
288 calculated_number n1 = eval_value(exp, &op->ops[0], error);
289 if(isnan(n1)) return NAN;
290 if(isinf(n1)) return INFINITY;
293 calculated_number eval_abs(EVAL_EXPRESSION *exp, EVAL_NODE *op, int *error) {
294 calculated_number n1 = eval_value(exp, &op->ops[0], error);
295 if(isnan(n1)) return NAN;
296 if(isinf(n1)) return INFINITY;
299 calculated_number eval_if_then_else(EVAL_EXPRESSION *exp, EVAL_NODE *op, int *error) {
300 if(is_true(eval_value(exp, &op->ops[0], error)))
301 return eval_value(exp, &op->ops[1], error);
303 return eval_value(exp, &op->ops[2], error);
306 static struct operator {
307 const char *print_as;
311 calculated_number (*eval)(EVAL_EXPRESSION *exp, EVAL_NODE *op, int *error);
313 // this is a random access array
314 // we always access it with a known EVAL_OPERATOR_X
316 [EVAL_OPERATOR_AND] = { "&&", 2, 2, 0, eval_and },
317 [EVAL_OPERATOR_OR] = { "||", 2, 2, 0, eval_or },
318 [EVAL_OPERATOR_GREATER_THAN_OR_EQUAL] = { ">=", 3, 2, 0, eval_greater_than_or_equal },
319 [EVAL_OPERATOR_LESS_THAN_OR_EQUAL] = { "<=", 3, 2, 0, eval_less_than_or_equal },
320 [EVAL_OPERATOR_NOT_EQUAL] = { "!=", 3, 2, 0, eval_not_equal },
321 [EVAL_OPERATOR_EQUAL] = { "==", 3, 2, 0, eval_equal },
322 [EVAL_OPERATOR_LESS] = { "<", 3, 2, 0, eval_less },
323 [EVAL_OPERATOR_GREATER] = { ">", 3, 2, 0, eval_greater },
324 [EVAL_OPERATOR_PLUS] = { "+", 4, 2, 0, eval_plus },
325 [EVAL_OPERATOR_MINUS] = { "-", 4, 2, 0, eval_minus },
326 [EVAL_OPERATOR_MULTIPLY] = { "*", 5, 2, 0, eval_multiply },
327 [EVAL_OPERATOR_DIVIDE] = { "/", 5, 2, 0, eval_divide },
328 [EVAL_OPERATOR_NOT] = { "!", 6, 1, 0, eval_not },
329 [EVAL_OPERATOR_SIGN_PLUS] = { "+", 6, 1, 0, eval_sign_plus },
330 [EVAL_OPERATOR_SIGN_MINUS] = { "-", 6, 1, 0, eval_sign_minus },
331 [EVAL_OPERATOR_ABS] = { "abs(",6,1, 1, eval_abs },
332 [EVAL_OPERATOR_IF_THEN_ELSE] = { "?", 7, 3, 0, eval_if_then_else },
333 [EVAL_OPERATOR_NOP] = { NULL, 8, 1, 0, eval_nop },
334 [EVAL_OPERATOR_EXPRESSION_OPEN] = { NULL, 8, 1, 0, eval_nop },
336 // this should exist in our evaluation list
337 [EVAL_OPERATOR_EXPRESSION_CLOSE] = { NULL, 99, 1, 0, eval_nop }
340 #define eval_precedence(operator) (operators[(unsigned char)(operator)].precedence)
342 static inline calculated_number eval_node(EVAL_EXPRESSION *exp, EVAL_NODE *op, int *error) {
343 if(unlikely(op->count != operators[op->operator].parameters)) {
344 *error = EVAL_ERROR_INVALID_NUMBER_OF_OPERANDS;
348 calculated_number n = operators[op->operator].eval(exp, op, error);
353 // ----------------------------------------------------------------------------
354 // parsed-as generation
356 static inline void print_parsed_as_variable(BUFFER *out, EVAL_VARIABLE *v, int *error) {
358 buffer_sprintf(out, "$%s", v->name);
361 static inline void print_parsed_as_constant(BUFFER *out, calculated_number n) {
362 if(unlikely(isnan(n))) {
363 buffer_strcat(out, "nan");
367 if(unlikely(isinf(n))) {
368 buffer_strcat(out, "inf");
373 snprintfz(b, 100, CALCULATED_NUMBER_FORMAT, n);
375 s = &b[strlen(b) - 1];
376 while(s > b && *s == '0') {
381 if(s > b && *s == '.')
384 buffer_strcat(out, b);
387 static inline void print_parsed_as_value(BUFFER *out, EVAL_VALUE *v, int *error) {
389 case EVAL_VALUE_EXPRESSION:
390 print_parsed_as_node(out, v->expression, error);
393 case EVAL_VALUE_NUMBER:
394 print_parsed_as_constant(out, v->number);
397 case EVAL_VALUE_VARIABLE:
398 print_parsed_as_variable(out, v->variable, error);
402 *error = EVAL_ERROR_INVALID_VALUE;
407 static inline void print_parsed_as_node(BUFFER *out, EVAL_NODE *op, int *error) {
408 if(unlikely(op->count != operators[op->operator].parameters)) {
409 *error = EVAL_ERROR_INVALID_NUMBER_OF_OPERANDS;
413 if(operators[op->operator].parameters == 1) {
415 if(operators[op->operator].print_as)
416 buffer_sprintf(out, "%s", operators[op->operator].print_as);
418 //if(op->operator == EVAL_OPERATOR_EXPRESSION_OPEN)
419 // buffer_strcat(out, "(");
421 print_parsed_as_value(out, &op->ops[0], error);
423 //if(op->operator == EVAL_OPERATOR_EXPRESSION_OPEN)
424 // buffer_strcat(out, ")");
427 else if(operators[op->operator].parameters == 2) {
428 buffer_strcat(out, "(");
429 print_parsed_as_value(out, &op->ops[0], error);
431 if(operators[op->operator].print_as)
432 buffer_sprintf(out, " %s ", operators[op->operator].print_as);
434 print_parsed_as_value(out, &op->ops[1], error);
435 buffer_strcat(out, ")");
437 else if(op->operator == EVAL_OPERATOR_IF_THEN_ELSE && operators[op->operator].parameters == 3) {
438 buffer_strcat(out, "(");
439 print_parsed_as_value(out, &op->ops[0], error);
441 if(operators[op->operator].print_as)
442 buffer_sprintf(out, " %s ", operators[op->operator].print_as);
444 print_parsed_as_value(out, &op->ops[1], error);
445 buffer_strcat(out, " : ");
446 print_parsed_as_value(out, &op->ops[2], error);
447 buffer_strcat(out, ")");
450 if(operators[op->operator].isfunction)
451 buffer_strcat(out, ")");
454 // ----------------------------------------------------------------------------
455 // parsing expressions
458 static inline void skip_spaces(const char **string) {
459 const char *s = *string;
460 while(isspace(*s)) s++;
464 // what character can appear just after an operator keyword
466 static inline int isoperatorterm_word(const char s) {
467 if(isspace(s) || s == '(' || s == '$' || s == '!' || s == '-' || s == '+' || isdigit(s) || !s)
473 // what character can appear just after an operator symbol?
474 static inline int isoperatorterm_symbol(const char s) {
475 if(isoperatorterm_word(s) || isalpha(s))
481 // return 1 if the character should never appear in a variable
482 static inline int isvariableterm(const char s) {
483 if(isalnum(s) || s == '.' || s == '_')
489 // ----------------------------------------------------------------------------
492 static inline int parse_and(const char **string) {
493 const char *s = *string;
496 if((s[0] == 'A' || s[0] == 'a') && (s[1] == 'N' || s[1] == 'n') && (s[2] == 'D' || s[2] == 'd') && isoperatorterm_word(s[3])) {
502 if(s[0] == '&' && s[1] == '&' && isoperatorterm_symbol(s[2])) {
510 static inline int parse_or(const char **string) {
511 const char *s = *string;
514 if((s[0] == 'O' || s[0] == 'o') && (s[1] == 'R' || s[1] == 'r') && isoperatorterm_word(s[2])) {
520 if(s[0] == '|' && s[1] == '|' && isoperatorterm_symbol(s[2])) {
528 static inline int parse_greater_than_or_equal(const char **string) {
529 const char *s = *string;
532 if(s[0] == '>' && s[1] == '=' && isoperatorterm_symbol(s[2])) {
540 static inline int parse_less_than_or_equal(const char **string) {
541 const char *s = *string;
544 if (s[0] == '<' && s[1] == '=' && isoperatorterm_symbol(s[2])) {
552 static inline int parse_greater(const char **string) {
553 const char *s = *string;
556 if(s[0] == '>' && isoperatorterm_symbol(s[1])) {
564 static inline int parse_less(const char **string) {
565 const char *s = *string;
568 if(s[0] == '<' && isoperatorterm_symbol(s[1])) {
576 static inline int parse_equal(const char **string) {
577 const char *s = *string;
580 if(s[0] == '=' && s[1] == '=' && isoperatorterm_symbol(s[2])) {
586 if(s[0] == '=' && isoperatorterm_symbol(s[1])) {
594 static inline int parse_not_equal(const char **string) {
595 const char *s = *string;
598 if(s[0] == '!' && s[1] == '=' && isoperatorterm_symbol(s[2])) {
604 if(s[0] == '<' && s[1] == '>' && isoperatorterm_symbol(s[2])) {
611 static inline int parse_not(const char **string) {
612 const char *s = *string;
615 if((s[0] == 'N' || s[0] == 'n') && (s[1] == 'O' || s[1] == 'o') && (s[2] == 'T' || s[2] == 't') && isoperatorterm_word(s[3])) {
628 static inline int parse_multiply(const char **string) {
629 const char *s = *string;
632 if(s[0] == '*' && isoperatorterm_symbol(s[1])) {
640 static inline int parse_divide(const char **string) {
641 const char *s = *string;
644 if(s[0] == '/' && isoperatorterm_symbol(s[1])) {
652 static inline int parse_minus(const char **string) {
653 const char *s = *string;
656 if(s[0] == '-' && isoperatorterm_symbol(s[1])) {
664 static inline int parse_plus(const char **string) {
665 const char *s = *string;
668 if(s[0] == '+' && isoperatorterm_symbol(s[1])) {
676 static inline int parse_open_subexpression(const char **string) {
677 const char *s = *string;
688 #define parse_close_function(x) parse_close_subexpression(x)
690 static inline int parse_close_subexpression(const char **string) {
691 const char *s = *string;
702 static inline int parse_variable(const char **string, char *buffer, size_t len) {
703 const char *s = *string;
710 while(*s && !isvariableterm(*s) && i < len)
724 static inline int parse_constant(const char **string, calculated_number *number) {
726 calculated_number n = strtold(*string, &end);
727 if(unlikely(!end || *string == end)) {
736 static inline int parse_abs(const char **string) {
737 const char *s = *string;
740 if((s[0] == 'A' || s[0] == 'a') && (s[1] == 'B' || s[1] == 'b') && (s[2] == 'S' || s[2] == 's') && s[3] == '(') {
748 static inline int parse_if_then_else(const char **string) {
749 const char *s = *string;
760 static struct operator_parser {
762 int (*parse)(const char **);
763 } operator_parsers[] = {
764 // the order in this list is important!
765 // the first matching will be used
766 // so place the longer of overlapping ones
769 { EVAL_OPERATOR_AND, parse_and },
770 { EVAL_OPERATOR_OR, parse_or },
771 { EVAL_OPERATOR_GREATER_THAN_OR_EQUAL, parse_greater_than_or_equal },
772 { EVAL_OPERATOR_LESS_THAN_OR_EQUAL, parse_less_than_or_equal },
773 { EVAL_OPERATOR_NOT_EQUAL, parse_not_equal },
774 { EVAL_OPERATOR_EQUAL, parse_equal },
775 { EVAL_OPERATOR_LESS, parse_less },
776 { EVAL_OPERATOR_GREATER, parse_greater },
777 { EVAL_OPERATOR_PLUS, parse_plus },
778 { EVAL_OPERATOR_MINUS, parse_minus },
779 { EVAL_OPERATOR_MULTIPLY, parse_multiply },
780 { EVAL_OPERATOR_DIVIDE, parse_divide },
781 { EVAL_OPERATOR_IF_THEN_ELSE, parse_if_then_else },
783 /* we should not put in this list the following:
789 * these are handled in code
793 { EVAL_OPERATOR_NOP, NULL }
796 static inline unsigned char parse_operator(const char **string, int *precedence) {
800 for(i = 0 ; operator_parsers[i].parse != NULL ; i++)
801 if(operator_parsers[i].parse(string)) {
802 if(precedence) *precedence = eval_precedence(operator_parsers[i].id);
803 return operator_parsers[i].id;
806 return EVAL_OPERATOR_NOP;
809 // ----------------------------------------------------------------------------
812 static inline EVAL_NODE *eval_node_alloc(int count) {
815 EVAL_NODE *op = callocz(1, sizeof(EVAL_NODE) + (sizeof(EVAL_VALUE) * count));
818 op->operator = EVAL_OPERATOR_NOP;
819 op->precedence = eval_precedence(EVAL_OPERATOR_NOP);
824 static inline void eval_node_set_value_to_node(EVAL_NODE *op, int pos, EVAL_NODE *value) {
826 fatal("Invalid request to set position %d of OPERAND that has only %d values", pos + 1, op->count + 1);
828 op->ops[pos].type = EVAL_VALUE_EXPRESSION;
829 op->ops[pos].expression = value;
832 static inline void eval_node_set_value_to_constant(EVAL_NODE *op, int pos, calculated_number value) {
834 fatal("Invalid request to set position %d of OPERAND that has only %d values", pos + 1, op->count + 1);
836 op->ops[pos].type = EVAL_VALUE_NUMBER;
837 op->ops[pos].number = value;
840 static inline void eval_node_set_value_to_variable(EVAL_NODE *op, int pos, const char *variable) {
842 fatal("Invalid request to set position %d of OPERAND that has only %d values", pos + 1, op->count + 1);
844 op->ops[pos].type = EVAL_VALUE_VARIABLE;
845 op->ops[pos].variable = callocz(1, sizeof(EVAL_VARIABLE));
846 op->ops[pos].variable->name = strdupz(variable);
847 op->ops[pos].variable->hash = simple_hash(op->ops[pos].variable->name);
850 static inline void eval_variable_free(EVAL_VARIABLE *v) {
855 static inline void eval_value_free(EVAL_VALUE *v) {
857 case EVAL_VALUE_EXPRESSION:
858 eval_node_free(v->expression);
861 case EVAL_VALUE_VARIABLE:
862 eval_variable_free(v->variable);
870 static inline void eval_node_free(EVAL_NODE *op) {
873 for(i = op->count - 1; i >= 0 ;i--)
874 eval_value_free(&op->ops[i]);
880 // ----------------------------------------------------------------------------
883 // helper function to avoid allocations all over the place
884 static inline EVAL_NODE *parse_next_operand_given_its_operator(const char **string, unsigned char operator_type, int *error) {
885 EVAL_NODE *sub = parse_one_full_operand(string, error);
886 if(!sub) return NULL;
888 EVAL_NODE *op = eval_node_alloc(1);
889 op->operator = operator_type;
890 eval_node_set_value_to_node(op, 0, sub);
894 // parse a full operand, including its sign or other associative operator (e.g. NOT)
895 static inline EVAL_NODE *parse_one_full_operand(const char **string, int *error) {
896 char variable_buffer[EVAL_MAX_VARIABLE_NAME_LENGTH + 1];
897 EVAL_NODE *op1 = NULL;
898 calculated_number number;
900 *error = EVAL_ERROR_OK;
904 *error = EVAL_ERROR_MISSING_OPERAND;
908 if(parse_not(string)) {
909 op1 = parse_next_operand_given_its_operator(string, EVAL_OPERATOR_NOT, error);
910 op1->precedence = eval_precedence(EVAL_OPERATOR_NOT);
912 else if(parse_plus(string)) {
913 op1 = parse_next_operand_given_its_operator(string, EVAL_OPERATOR_SIGN_PLUS, error);
914 op1->precedence = eval_precedence(EVAL_OPERATOR_SIGN_PLUS);
916 else if(parse_minus(string)) {
917 op1 = parse_next_operand_given_its_operator(string, EVAL_OPERATOR_SIGN_MINUS, error);
918 op1->precedence = eval_precedence(EVAL_OPERATOR_SIGN_MINUS);
920 else if(parse_abs(string)) {
921 op1 = parse_next_operand_given_its_operator(string, EVAL_OPERATOR_ABS, error);
922 op1->precedence = eval_precedence(EVAL_OPERATOR_ABS);
924 else if(parse_open_subexpression(string)) {
925 EVAL_NODE *sub = parse_full_expression(string, error);
927 op1 = eval_node_alloc(1);
928 op1->operator = EVAL_OPERATOR_EXPRESSION_OPEN;
929 op1->precedence = eval_precedence(EVAL_OPERATOR_EXPRESSION_OPEN);
930 eval_node_set_value_to_node(op1, 0, sub);
931 if(!parse_close_subexpression(string)) {
932 *error = EVAL_ERROR_MISSING_CLOSE_SUBEXPRESSION;
938 else if(parse_variable(string, variable_buffer, EVAL_MAX_VARIABLE_NAME_LENGTH)) {
939 op1 = eval_node_alloc(1);
940 op1->operator = EVAL_OPERATOR_NOP;
941 eval_node_set_value_to_variable(op1, 0, variable_buffer);
943 else if(parse_constant(string, &number)) {
944 op1 = eval_node_alloc(1);
945 op1->operator = EVAL_OPERATOR_NOP;
946 eval_node_set_value_to_constant(op1, 0, number);
949 *error = EVAL_ERROR_UNKNOWN_OPERAND;
951 *error = EVAL_ERROR_MISSING_OPERAND;
956 // parse an operator and the rest of the expression
957 // precedence processing is handled here
958 static inline EVAL_NODE *parse_rest_of_expression(const char **string, int *error, EVAL_NODE *op1) {
959 EVAL_NODE *op2 = NULL;
960 unsigned char operator;
963 operator = parse_operator(string, &precedence);
966 if(operator != EVAL_OPERATOR_NOP) {
967 op2 = parse_one_full_operand(string, error);
969 // error is already reported
974 EVAL_NODE *op = eval_node_alloc(operators[operator].parameters);
975 op->operator = operator;
976 op->precedence = precedence;
978 if(operator == EVAL_OPERATOR_IF_THEN_ELSE && op->count == 3) {
981 if(**string != ':') {
985 *error = EVAL_ERROR_IF_THEN_ELSE_MISSING_ELSE;
992 EVAL_NODE *op3 = parse_one_full_operand(string, error);
997 // error is already reported
1001 eval_node_set_value_to_node(op, 2, op3);
1004 eval_node_set_value_to_node(op, 1, op2);
1006 // precedence processing
1007 // if this operator has a higher precedence compared to its next
1008 // put the next operator on top of us (top = evaluated later)
1009 // function recursion does the rest...
1010 if(op->precedence > op1->precedence && op1->count == 2 && op1->operator != '(' && op1->ops[1].type == EVAL_VALUE_EXPRESSION) {
1011 eval_node_set_value_to_node(op, 0, op1->ops[1].expression);
1012 op1->ops[1].expression = op;
1016 eval_node_set_value_to_node(op, 0, op1);
1018 return parse_rest_of_expression(string, error, op);
1020 else if(**string == ')') {
1024 eval_node_free(op1);
1026 *error = EVAL_ERROR_MISSING_OPERATOR;
1032 // high level function to parse an expression or a sub-expression
1033 static inline EVAL_NODE *parse_full_expression(const char **string, int *error) {
1034 EVAL_NODE *op1 = parse_one_full_operand(string, error);
1036 *error = EVAL_ERROR_MISSING_OPERAND;
1040 return parse_rest_of_expression(string, error, op1);
1043 // ----------------------------------------------------------------------------
1046 int expression_evaluate(EVAL_EXPRESSION *exp) {
1047 exp->error = EVAL_ERROR_OK;
1049 buffer_reset(exp->error_msg);
1050 exp->result = eval_node(exp, (EVAL_NODE *)exp->nodes, &exp->error);
1052 if(unlikely(isnan(exp->result))) {
1053 if(exp->error == EVAL_ERROR_OK)
1054 exp->error = EVAL_ERROR_VALUE_IS_NAN;
1056 else if(unlikely(isinf(exp->result))) {
1057 if(exp->error == EVAL_ERROR_OK)
1058 exp->error = EVAL_ERROR_VALUE_IS_INFINITE;
1060 else if(unlikely(exp->error == EVAL_ERROR_UNKNOWN_VARIABLE)) {
1061 // although there is an unknown variable
1062 // the expression was evaluated successfully
1063 exp->error = EVAL_ERROR_OK;
1066 if(exp->error != EVAL_ERROR_OK) {
1069 if(buffer_strlen(exp->error_msg))
1070 buffer_strcat(exp->error_msg, "; ");
1072 buffer_sprintf(exp->error_msg, "failed to evaluate expression with error %d (%s)", exp->error, expression_strerror(exp->error));
1079 EVAL_EXPRESSION *expression_parse(const char *string, const char **failed_at, int *error) {
1080 const char *s = string;
1081 int err = EVAL_ERROR_OK;
1083 EVAL_NODE *op = parse_full_expression(&s, &err);
1090 err = EVAL_ERROR_REMAINING_GARBAGE;
1093 if (failed_at) *failed_at = s;
1094 if (error) *error = err;
1097 unsigned long pos = s - string + 1;
1098 error("failed to parse expression '%s': %s at character %lu (i.e.: '%s').", string, expression_strerror(err), pos, s);
1102 BUFFER *out = buffer_create(1024);
1103 print_parsed_as_node(out, op, &err);
1104 if(err != EVAL_ERROR_OK) {
1105 error("failed to re-generate expression '%s' with reason: %s", string, expression_strerror(err));
1111 EVAL_EXPRESSION *exp = callocz(1, sizeof(EVAL_EXPRESSION));
1113 exp->source = strdupz(string);
1114 exp->parsed_as = strdupz(buffer_tostring(out));
1117 exp->error_msg = buffer_create(100);
1118 exp->nodes = (void *)op;
1123 void expression_free(EVAL_EXPRESSION *exp) {
1126 if(exp->nodes) eval_node_free((EVAL_NODE *)exp->nodes);
1127 freez((void *)exp->source);
1128 freez((void *)exp->parsed_as);
1129 buffer_free(exp->error_msg);
1133 const char *expression_strerror(int error) {
1138 case EVAL_ERROR_MISSING_CLOSE_SUBEXPRESSION:
1139 return "missing closing parenthesis";
1141 case EVAL_ERROR_UNKNOWN_OPERAND:
1142 return "unknown operand";
1144 case EVAL_ERROR_MISSING_OPERAND:
1145 return "expected operand";
1147 case EVAL_ERROR_MISSING_OPERATOR:
1148 return "expected operator";
1150 case EVAL_ERROR_REMAINING_GARBAGE:
1151 return "remaining characters after expression";
1153 case EVAL_ERROR_INVALID_VALUE:
1154 return "invalid value structure - internal error";
1156 case EVAL_ERROR_INVALID_NUMBER_OF_OPERANDS:
1157 return "wrong number of operands for operation - internal error";
1159 case EVAL_ERROR_VALUE_IS_NAN:
1160 return "value is unset";
1162 case EVAL_ERROR_VALUE_IS_INFINITE:
1163 return "computed value is infinite";
1165 case EVAL_ERROR_UNKNOWN_VARIABLE:
1166 return "undefined variable";
1168 case EVAL_ERROR_IF_THEN_ELSE_MISSING_ELSE:
1169 return "missing second sub-expression of inline conditional";
1172 return "unknown error";