summaryrefslogtreecommitdiffstats
path: root/profile/test-eval.c
blob: 509eab566296b0307e48bd72bdf0ac950b34f48a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
 * 1. build netdata (as normally)
 * 2. cd profile/
 * 3. compile with:
 *    gcc -O1 -ggdb -Wall -Wextra -I ../src/ -I ../ -o test-eval test-eval.c ../src/log.o ../src/eval.o ../src/common.o -pthread
 *
 */

#include "common.h"

void netdata_cleanup_and_exit(int ret) { exit(ret); }

void indent(int level, int show) {
	int i = level;
	while(i--) printf(" |  ");
	if(show) printf(" \\_ ");
	else printf(" \\_  ");
}

void print_operand(EVAL_OPERAND *op, int level);

void print_value(EVAL_VALUE *v, int level) {
	indent(level, 0);

	switch(v->type) {
		case EVAL_VALUE_INVALID:
			printf("VALUE (NOP)\n");
			break;

		case EVAL_VALUE_NUMBER:
			printf("VALUE %Lf (NUMBER)\n", v->number);
			break;

		case EVAL_VALUE_EXPRESSION:
			printf("VALUE (SUB-EXPRESSION)\n");
			print_operand(v->expression, level+1);
			break;

		default:
			printf("VALUE (INVALID type %d)\n", v->type);
			break;

	}
}

void print_operand(EVAL_OPERAND *op, int level) {

//	if(op->operator != EVAL_OPERATOR_NOP) {
		indent(level, 1);
		if(op->operator) printf("%c (OPERATOR %d, prec: %d)\n", op->operator, op->id, op->precedence);
		else printf("NOP (OPERATOR %d, prec: %d)\n", op->id, op->precedence);
//	}

	int i = op->count;
	while(i--) print_value(&op->ops[i], level + 1);
}

calculated_number evaluate(EVAL_OPERAND *op);

calculated_number evaluate_value(EVAL_VALUE *v) {
	switch(v->type) {
		case EVAL_VALUE_NUMBER:
			return v->number;

		case EVAL_VALUE_EXPRESSION:
			return evaluate(v->expression);

		default:
			fatal("I don't know how to handle EVAL_VALUE type %d", v->type);
	}
}

calculated_number evaluate(EVAL_OPERAND *op) {
	calculated_number n1, n2, r;

	switch(op->operator) {
		case EVAL_OPERATOR_SIGN_PLUS:
			r = evaluate_value(&op->ops[0]);
			break;

		case EVAL_OPERATOR_SIGN_MINUS:
			r = -evaluate_value(&op->ops[0]);
			break;

		case EVAL_OPERATOR_PLUS:
			if(op->count != 2)
				fatal("Operator '%c' requires 2 values, but we have %d", op->operator, op->count);
			n1 = evaluate_value(&op->ops[0]);
			n2 = evaluate_value(&op->ops[1]);
			r = n1 + n2;
			printf("%Lf = %Lf %c %Lf\n", r, n1, op->operator, n2);
			break;

		case EVAL_OPERATOR_MINUS:
			if(op->count != 2)
				fatal("Operator '%c' requires 2 values, but we have %d", op->operator, op->count);
			n1 = evaluate_value(&op->ops[0]);
			n2 = evaluate_value(&op->ops[1]);
			r = n1 - n2;
			printf("%Lf = %Lf %c %Lf\n", r, n1, op->operator, n2);
			break;

		case EVAL_OPERATOR_MULTIPLY:
			if(op->count != 2)
				fatal("Operator '%c' requires 2 values, but we have %d", op->operator, op->count);
			n1 = evaluate_value(&op->ops[0]);
			n2 = evaluate_value(&op->ops[1]);
			r = n1 * n2;
			printf("%Lf = %Lf %c %Lf\n", r, n1, op->operator, n2);
			break;

		case EVAL_OPERATOR_DIVIDE:
			if(op->count != 2)
				fatal("Operator '%c' requires 2 values, but we have %d", op->operator, op->count);
			n1 = evaluate_value(&op->ops[0]);
			n2 = evaluate_value(&op->ops[1]);
			r = n1 / n2;
			printf("%Lf = %Lf %c %Lf\n", r, n1, op->operator, n2);
			break;

		case EVAL_OPERATOR_EXPRESSION_OPEN:
			printf("BEGIN SUB-EXPRESSION\n");
			r = evaluate_value(&op->ops[0]);
			printf("END SUB-EXPRESSION\n");
			break;

		case EVAL_OPERATOR_NOP:
		case EVAL_OPERATOR_VALUE:
			r = evaluate_value(&op->ops[0]);
			break;

		default:
			fatal("I don't know how to handle operator '%c'", op->operator);
			break;
	}

	return r;
}

void print_expression(EVAL_OPERAND *op, const char *failed_at, int error) {
	if(op) {
		printf("<expression root>\n");
		print_operand(op, 0);
		evaluate(op);
		free_expression(op);
	}
	else {
		printf("error: %d, failed_at: '%s'\n", error, (failed_at)?failed_at:"<NONE>");
	}
}


int main(int argc, char **argv) {
	if(argc != 2) {
		fprintf(stderr, "I need an epxression\n");
		exit(1);
	}

	const char *failed_at = NULL;
	int error;
	EVAL_OPERAND *op = parse_expression(argv[1], &failed_at, &error);
	print_expression(op, failed_at, error);
	return 0;
}