-
Notifications
You must be signed in to change notification settings - Fork 0
/
eval_upmc.c
378 lines (298 loc) · 7.15 KB
/
eval_upmc.c
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/*
Authors:
Remy El Sibaïe <remybesognet@gmail.com>
Pierrick Couderc <pierrick.couderc@gmail.com>
students at Université Pierre et Marie Curie
*/
#include <stdlib.h>
#include <stdio.h>
#include "sash.h"
#include "eval_upmc.h"
#include "y.tab.h"
#define SIZE_STACK 100
/* Releases the memory allocated for the AST and its subnodes */
void free_ast(ast_st* a) {
switch(a->en) {
case Plus:
case Minus:
case Mult:
case Div:
case Eq:
case Ne:
case Lt:
case Le:
case Gt:
case Ge:
free_ast(a->childs.left);
free_ast(a->childs.right);
free(a);
break;
case Integer:
case Real:
free(a);
break;
default: break;
}
}
/* Creates a integer node */
ast_st* create_int(int value) {
ast_st *a;
if ((a = (ast_st*) malloc(sizeof(ast_st))) == NULL) {
exit(1);
}
a->num = value;
a->en = Integer;
return a;
}
/* Creates a floating point number node */
ast_st* create_float(double value) {
ast_st *a;
if ((a = (ast_st*) malloc(sizeof(ast_st))) == NULL) {
exit(1);
}
a->flo = value;
a->en = Real;
return a;
}
/* Creates an ast node from the given operation and the two child nodes */
ast_st* create_node(kind_en op, ast_st* left, ast_st* right) {
ast_st *a;
if ((a = (ast_st*) malloc(sizeof(ast_st))) == NULL) {
free_ast(left);
free_ast(right);
exit(1);
}
a->childs.left = left;
a->childs.right = right;
a->en = op;
return a;
}
/* Evaluates an AST to render its corresponding value */
double eval(ast_st* ast) {
double tmp1, tmp2;
switch (ast->en) {
case Real:
return ast->flo;
break;
case Integer:
return (double) ast->num;
break;
case Plus:
tmp1 = eval(ast->childs.left);
tmp2 = eval(ast->childs.right);
return tmp1 + tmp2;
break;
case Minus:
tmp1 = eval(ast->childs.left);
tmp2 = eval(ast->childs.right);
return tmp1 - tmp2;
break;
case Mult:
tmp1 = eval(ast->childs.left);
tmp2 = eval(ast->childs.right);
return tmp1 * tmp2;
break;
case Div:
tmp1 = eval(ast->childs.left);
tmp2 = eval(ast->childs.right);
if (tmp2 == 0) {
printf("Division by zero\n");
}
return tmp1 / tmp2;
break;
case Eq:
tmp1 = eval(ast->childs.left);
tmp2 = eval(ast->childs.right);
return tmp1 == tmp2;
break;
case Ne:
tmp1 = eval(ast->childs.left);
tmp2 = eval(ast->childs.right);
return tmp1 != tmp2;
break;
case Lt:
tmp1 = eval(ast->childs.left);
tmp2 = eval(ast->childs.right);
return tmp1 < tmp2;
break;
case Le:
tmp1 = eval(ast->childs.left);
tmp2 = eval(ast->childs.right);
return tmp1 <= tmp2;
break;
case Gt:
tmp1 = eval(ast->childs.left);
tmp2 = eval(ast->childs.right);
return tmp1 > tmp2;
break;
case Ge:
tmp1 = eval(ast->childs.left);
tmp2 = eval(ast->childs.right);
return tmp1 >= tmp2;
break;
case And:
tmp1 = eval(ast->childs.left);
tmp2 = eval(ast->childs.right);
return tmp1 && tmp2;
break;
case Or:
tmp1 = eval(ast->childs.left);
tmp2 = eval(ast->childs.right);
return tmp1 || tmp2;
break;
default:
return 0.;
break;
}
}
/** Unused features **/
/* char * ops; */
/* char * numbers; */
/* int ops_pt = -1; */
/* int numbers_pt = -1; */
/* void push (char e, char * st, int * i) { */
/* if(*i==(SIZE_STACK-1)) */
/* { */
/* printf("\n Stack overflow"); */
/* return; */
/* } */
/* else */
/* { */
/* st[++(*i)] = e; */
/* } */
/* } */
/* int pop(char * st, int * i) */
/* { */
/* if(*i==-1) */
/* { */
/* printf("\n Stack underflow"); */
/* return -1; */
/* } */
/* else */
/* { */
/* return st[(*i)--]; */
/* } */
/* } */
/* int opcompare (char c1, char c2); */
/* int valueop (char c); */
/* void print_stack (char * st, int pt){ */
/* int i = 0; */
/* for (i = 0; i <= pt; i++) */
/* printf("%c ", st[i]); */
/* } */
/* int valueop (char c){ */
/* if (c == '/' || c == '*') */
/* return 1; */
/* else return 0; */
/* } */
/* int opcompare (char c1, char c2){ */
/* if (valueop (c1) > valueop (c1)) */
/* return 1; */
/* else if (valueop (c1) < valueop (c1)) */
/* return -1; */
/* else return 0; */
/* } */
/* Takes a "${<calculus>}" and render the value associated */
int parseArithToValue (const char * arith) {
int val = 0;
yy_scan_string(arith);
yylex();
yyparse ();
val = eval(result);
free_ast(result);
yylex_destroy();
return val;
}
/* Returns if the char* given corresponds to an operator */
int is_op(const char* val) {
return !strcmp(val, "-lt") || !strcmp(val, "-le") || !strcmp(val,"-gt") ||
!strcmp(val, "-ge") || !strcmp(val, "-eq") || !strcmp(val, "-ne");
}
/* Returns the operator that matches the char* given */
kind_en get_op(const char* op) {
if (!strcmp(op, "-lt")) return Lt;
else if (!strcmp(op, "-le")) return Le;
else if (!strcmp(op, "-gt")) return Gt;
else if (!strcmp(op, "-ge")) return Ge;
else if (!strcmp(op, "-eq")) return Eq;
else if (!strcmp(op, "-ne")) return Ne;
else return Nothing;
}
/* Eval the args given to -test and computes the function, unused */
ast_st* eval_args(int argc, const char ** argv, int par, int * index) {
ast_st *tmp1, *tmp2;
kind_en op = Nothing;
int i;
for (i = 0; i < argc; i++) {
if (!strcmp(argv[i],"(")) {
tmp2 = eval_args(argc-i, argv+i+1, 1, index);
if (tmp2 == NULL) return NULL;
i += *index;
}
else if (!strcmp(argv[i], ")"))
if (par) {
*index = i+1;
return tmp1;
}
else {
printf("Closing parenthesis unmatched\n");
return NULL;
}
else if (is_op(argv[i])) {
op = get_op(argv[i]);
}
else {
int val = !strcmp(argv[i], "") ? 0 : atoi(argv[i]);
tmp2 = create_int(val);
if (op != Nothing) {
tmp1 = create_node(op, tmp1, tmp2);
op = Nothing;
} else tmp1 = tmp2;
}
}
if (!par)
return tmp1;
else {
printf("Opening parenthesis unmatched\n");
return NULL;
}
}
/* Does the action for the -test command */
int do_test (int argc, const char ** argv) {
int i, val, size = 0;
char * buffer;
for (i=0; i<argc; i++) size += strlen(argv[i]) + 1;
buffer = (char*) malloc(sizeof(char) * size);
strcpy(buffer, argv[0]);
for (i=1; i<argc; i++) {
strcat(buffer, " ");
strcat(buffer, argv[i]);
}
yy_scan_string(buffer);
yylex();
yyparse ();
if (result == NULL) return;
val = eval(result);
/* printf("%d\n", val); */
free_ast(result);
free(buffer);
yylex_destroy();
return val;
}
/* int main(char **argc, int argv) { */
/* ast_st* a = create_int(2); */
/* ast_st* b = create_int(3); */
/* ast_st* p = create_node(Eq, a, b); */
/* ast_st* m = create_node(Lt, a, b); */
/* ast_st* o = create_node(Or, p, m); */
/* int eva = eval(a); */
/* int evb = eval(b); */
/* int evp = eval(p); */
/* int evm = eval(m); */
/* int evo = eval(o); */
/* printf("a: %d\n", eva); */
/* printf("b: %d\n", evb); */
/* printf("p=a==b: %d\n", evp); */
/* printf("m=a<=b: %d \n", evm); */
/* printf("o=p or m: %d\n", evo); */
/* } */