3 * 1. build netdata (as normally)
6 * gcc -O1 -ggdb -Wall -Wextra -I ../src/ -I ../ -o test-eval test-eval.c ../src/log.o ../src/eval.o ../src/common.o ../src/web_buffer.o ../src/storage_number.o -pthread -lm
12 void netdata_cleanup_and_exit(int ret) { exit(ret); }
15 void indent(int level, int show) {
17 while(i--) printf(" | ");
18 if(show) printf(" \\_ ");
22 void print_node(EVAL_NODE *op, int level);
24 void print_value(EVAL_VALUE *v, int level) {
28 case EVAL_VALUE_INVALID:
29 printf("value (NOP)\n");
32 case EVAL_VALUE_NUMBER:
33 printf("value %Lf (NUMBER)\n", v->number);
36 case EVAL_VALUE_EXPRESSION:
37 printf("value (SUB-EXPRESSION)\n");
38 print_node(v->expression, level+1);
42 printf("value (INVALID type %d)\n", v->type);
48 void print_node(EVAL_NODE *op, int level) {
50 // if(op->operator != EVAL_OPERATOR_NOP) {
52 if(op->operator) printf("%c (node %d, precedence: %d)\n", op->operator, op->id, op->precedence);
53 else printf("NOP (node %d, precedence: %d)\n", op->id, op->precedence);
57 while(i--) print_value(&op->ops[i], level + 1);
60 calculated_number evaluate(EVAL_NODE *op, int depth);
62 calculated_number evaluate_value(EVAL_VALUE *v, int depth) {
64 case EVAL_VALUE_NUMBER:
67 case EVAL_VALUE_EXPRESSION:
68 return evaluate(v->expression, depth);
71 fatal("I don't know how to handle EVAL_VALUE type %d", v->type);
75 void print_depth(int depth) {
78 printf("%d. ", ++count);
79 while(depth--) printf(" ");
82 calculated_number evaluate(EVAL_NODE *op, int depth) {
83 calculated_number n1, n2, r;
85 switch(op->operator) {
86 case EVAL_OPERATOR_SIGN_PLUS:
87 r = evaluate_value(&op->ops[0], depth);
90 case EVAL_OPERATOR_SIGN_MINUS:
91 r = -evaluate_value(&op->ops[0], depth);
94 case EVAL_OPERATOR_PLUS:
96 fatal("Operator '%c' requires 2 values, but we have %d", op->operator, op->count);
97 n1 = evaluate_value(&op->ops[0], depth);
98 n2 = evaluate_value(&op->ops[1], depth);
101 printf("%Lf = %Lf + %Lf\n", r, n1, n2);
104 case EVAL_OPERATOR_MINUS:
106 fatal("Operator '%c' requires 2 values, but we have %d", op->operator, op->count);
107 n1 = evaluate_value(&op->ops[0], depth);
108 n2 = evaluate_value(&op->ops[1], depth);
111 printf("%Lf = %Lf - %Lf\n", r, n1, n2);
114 case EVAL_OPERATOR_MULTIPLY:
116 fatal("Operator '%c' requires 2 values, but we have %d", op->operator, op->count);
117 n1 = evaluate_value(&op->ops[0], depth);
118 n2 = evaluate_value(&op->ops[1], depth);
121 printf("%Lf = %Lf * %Lf\n", r, n1, n2);
124 case EVAL_OPERATOR_DIVIDE:
126 fatal("Operator '%c' requires 2 values, but we have %d", op->operator, op->count);
127 n1 = evaluate_value(&op->ops[0], depth);
128 n2 = evaluate_value(&op->ops[1], depth);
131 printf("%Lf = %Lf / %Lf\n", r, n1, n2);
134 case EVAL_OPERATOR_NOT:
135 n1 = evaluate_value(&op->ops[0], depth);
138 printf("%Lf = NOT %Lf\n", r, n1);
141 case EVAL_OPERATOR_AND:
143 fatal("Operator '%c' requires 2 values, but we have %d", op->operator, op->count);
144 n1 = evaluate_value(&op->ops[0], depth);
145 n2 = evaluate_value(&op->ops[1], depth);
148 printf("%Lf = %Lf AND %Lf\n", r, n1, n2);
151 case EVAL_OPERATOR_OR:
153 fatal("Operator '%c' requires 2 values, but we have %d", op->operator, op->count);
154 n1 = evaluate_value(&op->ops[0], depth);
155 n2 = evaluate_value(&op->ops[1], depth);
158 printf("%Lf = %Lf OR %Lf\n", r, n1, n2);
161 case EVAL_OPERATOR_GREATER_THAN_OR_EQUAL:
163 fatal("Operator '%c' requires 2 values, but we have %d", op->operator, op->count);
164 n1 = evaluate_value(&op->ops[0], depth);
165 n2 = evaluate_value(&op->ops[1], depth);
168 printf("%Lf = %Lf >= %Lf\n", r, n1, n2);
171 case EVAL_OPERATOR_LESS_THAN_OR_EQUAL:
173 fatal("Operator '%c' requires 2 values, but we have %d", op->operator, op->count);
174 n1 = evaluate_value(&op->ops[0], depth);
175 n2 = evaluate_value(&op->ops[1], depth);
178 printf("%Lf = %Lf <= %Lf\n", r, n1, n2);
181 case EVAL_OPERATOR_GREATER:
183 fatal("Operator '%c' requires 2 values, but we have %d", op->operator, op->count);
184 n1 = evaluate_value(&op->ops[0], depth);
185 n2 = evaluate_value(&op->ops[1], depth);
188 printf("%Lf = %Lf > %Lf\n", r, n1, n2);
191 case EVAL_OPERATOR_LESS:
193 fatal("Operator '%c' requires 2 values, but we have %d", op->operator, op->count);
194 n1 = evaluate_value(&op->ops[0], depth);
195 n2 = evaluate_value(&op->ops[1], depth);
198 printf("%Lf = %Lf < %Lf\n", r, n1, n2);
201 case EVAL_OPERATOR_NOT_EQUAL:
203 fatal("Operator '%c' requires 2 values, but we have %d", op->operator, op->count);
204 n1 = evaluate_value(&op->ops[0], depth);
205 n2 = evaluate_value(&op->ops[1], depth);
208 printf("%Lf = %Lf <> %Lf\n", r, n1, n2);
211 case EVAL_OPERATOR_EQUAL:
213 fatal("Operator '%c' requires 2 values, but we have %d", op->operator, op->count);
214 n1 = evaluate_value(&op->ops[0], depth);
215 n2 = evaluate_value(&op->ops[1], depth);
218 printf("%Lf = %Lf == %Lf\n", r, n1, n2);
221 case EVAL_OPERATOR_EXPRESSION_OPEN:
222 printf("BEGIN SUB-EXPRESSION\n");
223 r = evaluate_value(&op->ops[0], depth + 1);
224 printf("END SUB-EXPRESSION\n");
227 case EVAL_OPERATOR_NOP:
228 case EVAL_OPERATOR_VALUE:
229 r = evaluate_value(&op->ops[0], depth);
233 error("I don't know how to handle operator '%c'", op->operator);
242 void print_expression(EVAL_NODE *op, const char *failed_at, int error) {
244 printf("expression tree:\n");
247 printf("\nevaluation steps:\n");
251 calculated_number ret = expression_evaluate(op, &error);
252 printf("\ninternal evaluator:\nSTATUS: %d, RESULT = %Lf\n", error, ret);
257 printf("error: %d, failed_at: '%s'\n", error, (failed_at)?failed_at:"<NONE>");
262 int health_variable_lookup(const char *variable, uint32_t hash, RRDCALC *rc, calculated_number *result) {
271 int main(int argc, char **argv) {
273 fprintf(stderr, "I need an epxression (enclose it in single-quotes (') as a single parameter)\n");
277 const char *failed_at = NULL;
280 EVAL_EXPRESSION *exp = expression_parse(argv[1], &failed_at, &error);
282 printf("\nPARSING FAILED\nExpression: '%s'\nParsing stopped at: '%s'\nParsing error code: %d (%s)\n", argv[1], (failed_at)?((*failed_at)?failed_at:"<END OF EXPRESSION>"):"<NONE>", error, expression_strerror(error));
285 printf("\nPARSING OK\nExpression: '%s'\nParsed as : '%s'\nParsing error code: %d (%s)\n", argv[1], exp->parsed_as, error, expression_strerror(error));
287 if(expression_evaluate(exp)) {
288 printf("\nEvaluates to: %Lf\n\n", exp->result);
291 printf("\nEvaluation failed with code %d and message: %s\n\n", exp->error, buffer_tostring(exp->error_msg));
293 expression_free(exp);