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 -pthread
12 void netdata_cleanup_and_exit(int ret) { exit(ret); }
14 void indent(int level, int show) {
16 while(i--) printf(" | ");
17 if(show) printf(" \\_ ");
21 void print_operand(EVAL_OPERAND *op, int level);
23 void print_value(EVAL_VALUE *v, int level) {
27 case EVAL_VALUE_INVALID:
28 printf("VALUE (NOP)\n");
31 case EVAL_VALUE_NUMBER:
32 printf("VALUE %Lf (NUMBER)\n", v->number);
35 case EVAL_VALUE_EXPRESSION:
36 printf("VALUE (SUB-EXPRESSION)\n");
37 print_operand(v->expression, level+1);
41 printf("VALUE (INVALID type %d)\n", v->type);
47 void print_operand(EVAL_OPERAND *op, int level) {
49 // if(op->operator != EVAL_OPERATOR_NOP) {
51 if(op->operator) printf("%c (OPERATOR %d, prec: %d)\n", op->operator, op->id, op->precedence);
52 else printf("NOP (OPERATOR %d, prec: %d)\n", op->id, op->precedence);
56 while(i--) print_value(&op->ops[i], level + 1);
59 calculated_number evaluate(EVAL_OPERAND *op, int depth);
61 calculated_number evaluate_value(EVAL_VALUE *v, int depth) {
63 case EVAL_VALUE_NUMBER:
66 case EVAL_VALUE_EXPRESSION:
67 return evaluate(v->expression, depth);
70 fatal("I don't know how to handle EVAL_VALUE type %d", v->type);
74 void print_depth(int depth) {
77 printf("%d. ", ++count);
78 while(depth--) printf(" ");
81 calculated_number evaluate(EVAL_OPERAND *op, int depth) {
82 calculated_number n1, n2, r;
84 switch(op->operator) {
85 case EVAL_OPERATOR_SIGN_PLUS:
86 r = evaluate_value(&op->ops[0], depth);
89 case EVAL_OPERATOR_SIGN_MINUS:
90 r = -evaluate_value(&op->ops[0], depth);
93 case EVAL_OPERATOR_PLUS:
95 fatal("Operator '%c' requires 2 values, but we have %d", op->operator, op->count);
96 n1 = evaluate_value(&op->ops[0], depth);
97 n2 = evaluate_value(&op->ops[1], depth);
100 printf("%Lf = %Lf + %Lf\n", r, n1, n2);
103 case EVAL_OPERATOR_MINUS:
105 fatal("Operator '%c' requires 2 values, but we have %d", op->operator, op->count);
106 n1 = evaluate_value(&op->ops[0], depth);
107 n2 = evaluate_value(&op->ops[1], depth);
110 printf("%Lf = %Lf - %Lf\n", r, n1, n2);
113 case EVAL_OPERATOR_MULTIPLY:
115 fatal("Operator '%c' requires 2 values, but we have %d", op->operator, op->count);
116 n1 = evaluate_value(&op->ops[0], depth);
117 n2 = evaluate_value(&op->ops[1], depth);
120 printf("%Lf = %Lf * %Lf\n", r, n1, n2);
123 case EVAL_OPERATOR_DIVIDE:
125 fatal("Operator '%c' requires 2 values, but we have %d", op->operator, op->count);
126 n1 = evaluate_value(&op->ops[0], depth);
127 n2 = evaluate_value(&op->ops[1], depth);
130 printf("%Lf = %Lf / %Lf\n", r, n1, n2);
133 case EVAL_OPERATOR_NOT:
134 n1 = evaluate_value(&op->ops[0], depth);
137 printf("%Lf = NOT %Lf\n", r, n1);
140 case EVAL_OPERATOR_AND:
142 fatal("Operator '%c' requires 2 values, but we have %d", op->operator, op->count);
143 n1 = evaluate_value(&op->ops[0], depth);
144 n2 = evaluate_value(&op->ops[1], depth);
147 printf("%Lf = %Lf AND %Lf\n", r, n1, n2);
150 case EVAL_OPERATOR_OR:
152 fatal("Operator '%c' requires 2 values, but we have %d", op->operator, op->count);
153 n1 = evaluate_value(&op->ops[0], depth);
154 n2 = evaluate_value(&op->ops[1], depth);
157 printf("%Lf = %Lf OR %Lf\n", r, n1, n2);
160 case EVAL_OPERATOR_GREATER_THAN_OR_EQUAL:
162 fatal("Operator '%c' requires 2 values, but we have %d", op->operator, op->count);
163 n1 = evaluate_value(&op->ops[0], depth);
164 n2 = evaluate_value(&op->ops[1], depth);
167 printf("%Lf = %Lf >= %Lf\n", r, n1, n2);
170 case EVAL_OPERATOR_LESS_THAN_OR_EQUAL:
172 fatal("Operator '%c' requires 2 values, but we have %d", op->operator, op->count);
173 n1 = evaluate_value(&op->ops[0], depth);
174 n2 = evaluate_value(&op->ops[1], depth);
177 printf("%Lf = %Lf <= %Lf\n", r, n1, n2);
180 case EVAL_OPERATOR_GREATER:
182 fatal("Operator '%c' requires 2 values, but we have %d", op->operator, op->count);
183 n1 = evaluate_value(&op->ops[0], depth);
184 n2 = evaluate_value(&op->ops[1], depth);
187 printf("%Lf = %Lf > %Lf\n", r, n1, n2);
190 case EVAL_OPERATOR_LESS:
192 fatal("Operator '%c' requires 2 values, but we have %d", op->operator, op->count);
193 n1 = evaluate_value(&op->ops[0], depth);
194 n2 = evaluate_value(&op->ops[1], depth);
197 printf("%Lf = %Lf < %Lf\n", r, n1, n2);
200 case EVAL_OPERATOR_NOT_EQUAL:
202 fatal("Operator '%c' requires 2 values, but we have %d", op->operator, op->count);
203 n1 = evaluate_value(&op->ops[0], depth);
204 n2 = evaluate_value(&op->ops[1], depth);
207 printf("%Lf = %Lf <> %Lf\n", r, n1, n2);
210 case EVAL_OPERATOR_EQUAL:
212 fatal("Operator '%c' requires 2 values, but we have %d", op->operator, op->count);
213 n1 = evaluate_value(&op->ops[0], depth);
214 n2 = evaluate_value(&op->ops[1], depth);
217 printf("%Lf = %Lf == %Lf\n", r, n1, n2);
220 case EVAL_OPERATOR_EXPRESSION_OPEN:
221 printf("BEGIN SUB-EXPRESSION\n");
222 r = evaluate_value(&op->ops[0], depth + 1);
223 printf("END SUB-EXPRESSION\n");
226 case EVAL_OPERATOR_NOP:
227 case EVAL_OPERATOR_VALUE:
228 r = evaluate_value(&op->ops[0], depth);
232 error("I don't know how to handle operator '%c'", op->operator);
240 void print_expression(EVAL_OPERAND *op, const char *failed_at, int error) {
242 printf("expression tree:\n");
243 print_operand(op, 0);
245 printf("\nevaluation steps:\n");
249 calculated_number ret = evaluate_expression(op, &error);
250 printf("\ninternal evaluator:\nSTATUS: %d, RESULT = %Lf\n", error, ret);
255 printf("error: %d, failed_at: '%s'\n", error, (failed_at)?failed_at:"<NONE>");
260 int main(int argc, char **argv) {
262 fprintf(stderr, "I need an epxression\n");
266 const char *failed_at = NULL;
268 EVAL_OPERAND *op = parse_expression(argv[1], &failed_at, &error);
269 print_expression(op, failed_at, error);