-
Notifications
You must be signed in to change notification settings - Fork 1
/
expr_eval.cpp
499 lines (452 loc) · 13.1 KB
/
expr_eval.cpp
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
#include "expr_eval.hh"
#define uint unsigned int
vector < table > dollar;
int var_test = false;
// converts infix logic and arithmetic expressions into reverse polish notation
// used for select queries
// @[<queries>](table)
vector < string > shunting_yard_adv (string cond) {
vector < string > output;
vector < char > stack;
string temp;
for (uint i = 0; i < cond.size(); i++) {
if (cond[i] == '&' || cond[i] == '|' || cond[i] == '!'
|| cond[i] == '*' || cond[i] == '/' || cond[i] == '+'
|| cond[i] == '-' || cond[i] == '>' || cond[i] == '<'
|| cond[i] == '=' || cond[i] == '~') {
if (temp.length() > 0) {
output.push_back(temp);
temp.clear();
}
while (stack.size() > 0 &&
prec_adv(cond[i], stack[stack.size()-1]) < 0) {
output.push_back(string(1, stack[stack.size()-1]));
stack.pop_back();
}
stack.push_back(cond[i]);
}
else if (cond[i] == '(') {
if (temp.length() > 0) {
output.push_back(temp);
temp.clear();
}
stack.push_back(cond[i]);
}
else if (cond[i] == ')') {
if (temp.length() > 0) {
output.push_back(temp);
temp.clear();
}
while (stack.size() > 0 && stack[stack.size()-1] != '(') {
output.push_back(string(1, stack[stack.size()-1]));
stack.pop_back();
}
stack.pop_back();
}
else
if (cond[i] != ' ')
temp += cond[i];
}
if (temp.length() > 0) {
output.push_back(temp);
temp.clear();
}
while (stack.size() > 0) {
output.push_back(string(1, stack[stack.size()-1]));
stack.pop_back();
}
return output;
}
// for determining the precedence of operators
int prec_adv (char a, char b) {
a = conv_adv(a);
b = conv_adv(b);
return a-b;
}
// for determining the precedence of operators
// higher return values correspond to higher
// precedence. eg: * > +; ! > & > |
int conv_adv (char a) {
if (a == '/')
return 12;
else if (a == '*')
return 11;
else if (a == '+')
return 10;
else if (a == '-')
return 9;
else if (a == '~')
return 8;
else if (a == '=')
return 7;
else if (a == '>')
return 6;
else if (a == '<')
return 5;
else if (a == '!')
return 3;
else if (a == '&')
return 2;
else if (a == '|')
return 1;
else
return 0;
}
// converts the nested into reverse polish notation
// eg: converts
// --------------------------------------
// #[list_col](@[query](table1 % table2))
// --------------------------------------
// |
// v
// ------------
// table1
// table2
// %
// @[query]
// #[list_col]
// ------------
// this reverse polish is then evaluated by the function
// eval_nest (see below)
vector < string > shunting_yard_nest (string cond) {
vector < string > output;
vector < string > stack;
string temp, temp2;
for (uint i = 0; i < cond.size(); i++) {
if (cond[i] == '#' || cond[i] == '@' || cond[i] == '`'
|| cond[i] == ':' || cond[i] == '^' || cond[i] == '%'
|| cond[i] == '{') {
if (cond[i] == '#' || cond[i] == '@' || cond[i] == '`') {
while (cond[i] != ']') {
if (cond[i] != ' ')
temp2 += cond[i];
i++;
}
temp2 += cond[i];
}
else if (cond[i] == '{') {
while (!(cond[i] == '}' && cond[i+1] == '(')) {
// {G1, G2, G3, ...}*{g(1), g(2), ...}(Table)
// the point till ^
if (cond[i] != ' ')
temp2 += cond[i];
i++;
}
temp2 += cond[i];
}
else
temp2 += cond[i];
if (temp.length() > 0) {
output.push_back(temp);
temp.clear();
}
while (stack.size() > 0 &&
prec_nest(temp2[0], stack[stack.size()-1][0]) < 0) {
output.push_back(stack[stack.size()-1]);
stack.pop_back();
}
stack.push_back(temp2);
temp2.clear();
}
else if (cond[i] == '(') {
if (temp.length() > 0) {
output.push_back(temp);
temp.clear();
}
temp2 += cond[i];
stack.push_back(temp2);
temp2.clear();
}
// There are two kinds of parenthesis in this shunting yard string.
// One is used for a fn: fn() and other is used to group things:
// ( (A and G) and (W or P) ).
// In the first case when a ')' is encountered fn. should also be
// popped from the shunting along with the arguments of the function.
// However if the matching '(' does not follow a fn. then preceding
// input on the shunting should not be popped.
// Note: fn () is possible.
// So is ( ( ).
// But ) ( ) should not be possible.
else if (cond[i] == ')') {
if (temp.length() > 0) {
output.push_back(temp);
temp.clear();
}
while (stack.size() > 0 && stack[stack.size()-1] != "(") {
output.push_back(stack[stack.size()-1]);
stack.pop_back();
}
stack.pop_back();
if (stack.size() > 0 && stack[stack.size()-1][0] != '(') {
output.push_back(stack[stack.size()-1]);
stack.pop_back();
}
}
else
if (cond[i] != ' ')
temp += cond[i];
}
if (temp.length() > 0) {
output.push_back(temp);
temp.clear();
}
while (stack.size() > 0) {
output.push_back(stack[stack.size()-1]);
stack.pop_back();
}
if (var_test == true) {
cout<<"________________________________________________________\n"
<<"SHUNTING YARD NEST REVERSE POLISH:\n"
<<"--------------------------------------------------------\n";
vector <string> A = output;
for (uint i = 0; i < A.size(); i++) {
if (A[i] == "\n") {
A.erase(A.begin()+i);
i--;
}
}
for (uint i = 0; i < A.size(); i++) {
cout<<"|"<<A[i]<<"| "<<"\n";
}
cout<<"________________________________________________________\n";
}
for (uint i = 0; i < output.size(); i++) {
if (output[i] == "\n") {
output.erase(output.begin()+i);
i--;
}
}
return output;
}
int prec_nest (char a, char b) {
a = conv_nest(a);
b = conv_nest(b);
return a-b;
}
int conv_nest (char a) {
if (a == '`')
return 13;
else if (a == '*')
return 12;
else if (a == '%')
return 11;
else if (a == '@')
return 10;
else if (a == '#')
return 9;
else if (a == '^')
return 8;
else if (a == ':')
return 7;
else
return 0;
}
// evaluates the reverse polish generated by shunting_yard_adv
// and return 1 (for True) if the query evaluates to true,
// otherwise 0 (for False)
int eval (vector < string > tuple_eval, table T, int tuple) {
// evaluate the conditions Part-2
for (uint x = 0; x < tuple_eval.size(); x++) {
if (tuple_eval[x] == "!") {
tuple_eval[x] = "T";
if (tuple_eval[x-1] == "T")
tuple_eval[x] = "F";
tuple_eval.erase(tuple_eval.begin()+x-1);
x--;
}
else if (tuple_eval[x] == "&") {
tuple_eval[x] = "F";
if (tuple_eval[x-1] == "T" && tuple_eval[x-2] == "T")
tuple_eval[x] = "T";
tuple_eval.erase(tuple_eval.begin()+x-1);
tuple_eval.erase(tuple_eval.begin()+x-2);
x = x-2;
}
else if (tuple_eval[x] == ":") {
tuple_eval[x] = "F";
if (tuple_eval[x-1] == "T" || tuple_eval[x-2] == "T")
tuple_eval[x] = "T";
tuple_eval.erase(tuple_eval.begin()+x-1);
tuple_eval.erase(tuple_eval.begin()+x-2);
x = x-2;
}
else if (tuple_eval[x] == ">" || tuple_eval[x] == "<" ||
tuple_eval[x] == "=" || tuple_eval[x] == "~") {
string lhs, rhs;
// get string values of lhs
if (tuple_eval[x-2][0] == '\'') {
lhs = tuple_eval[x-2].substr(1, tuple_eval[x-2].length()-2);
// cout<<"LHS :"<<lhs<<"\n";
}
else {
for (uint y = 0; y < T.columns.size(); y++)
if (T.columns[y].name == tuple_eval[x-2])
lhs = T.columns[y].values[tuple];
// cout<<"LHS :"<<lhs<<"\n";
}
// get string values of rhs
if (tuple_eval[x-1][0] == '\'') {
rhs = tuple_eval[x-1].substr(1, tuple_eval[x-1].length()-2);
// cout<<"RHS :"<<rhs<<"\n";
}
else {
for (uint y = 0; y < T.columns.size(); y++)
if (T.columns[y].name == tuple_eval[x-1])
rhs = T.columns[y].values[tuple];
// cout<<"RHS :"<<rhs<<"\n";
}
string val = "F";
if (tuple_eval[x] == ">" && lhs > rhs)
val = "T";
else if (tuple_eval[x] == "<" && lhs < rhs)
val = "T";
else if (tuple_eval[x] == "=" && lhs == rhs)
val = "T";
else if (tuple_eval[x] == "~" && lhs != rhs)
val = "T";
tuple_eval[x] = val;
tuple_eval.erase(tuple_eval.begin()+x-1);
tuple_eval.erase(tuple_eval.begin()+x-2);
x = x-2;
}
else if (tuple_eval[x] == "/" || tuple_eval[x] == "*" ||
tuple_eval[x] == "+" || tuple_eval[x] == "-") {
string lhs, rhs;
// get string values of lhs
if (tuple_eval[x-1][0] == '\'')
lhs = tuple_eval[x-1].substr(1, tuple_eval[x-1].length()-2);
else
for (uint y = 0; y < T.columns.size(); y++)
if (T.columns[y].name == tuple_eval[x-1])
lhs = T.columns[y].values[tuple];
// get string values of rhs
if (tuple_eval[x-2][0] == '\'')
rhs = tuple_eval[x-2].substr(1, tuple_eval[x-2].length()-2);
else
for (uint y = 0; y < T.columns.size(); y++)
if (T.columns[y].name == tuple_eval[x-2])
lhs = T.columns[y].values[tuple];
float val;
float lhs_int, rhs_int;
lhs_int = ::atof(lhs.c_str());
rhs_int = ::atof(rhs.c_str());
if (tuple_eval[x] == "/")
val = lhs_int/rhs_int;
else if (tuple_eval[x] == "*")
val = lhs_int*rhs_int;
else if (tuple_eval[x] == "+")
val = lhs_int+rhs_int;
else if (tuple_eval[x] == "-")
val = lhs_int-rhs_int;
tuple_eval[x] = '\''+SSTR(val)+'\'';
tuple_eval.erase(tuple_eval.begin()+x-1);
tuple_eval.erase(tuple_eval.begin()+x-2);
x = x-2;
}
// for (int z = 0; z < tuple_eval.size(); z++)
// cout<<tuple_eval[z]<<" ";
// cout<<endl;
}
if (tuple_eval[0] == "T")
return 1;
else if (tuple_eval[0] == "F")
return 0;
else
return -1;
}
// evaluates the reverse polish generated by shunting_yard_nest
// it calls the appropriate functions on tables generated
// by previous queries
int eval_nest (vector < string > table_eval) {
for (uint i = 0; i < table_eval.size(); i++) {
if (table_eval[i][0] == '#' || table_eval[i][0] == '@'
|| table_eval[i][0] == '`') {
string temp, temp2;
int index;
// extracting conditions of the operation
for (uint x = 2;
x < table_eval[i].size() && table_eval[i][x] != ']';
x++)
temp += table_eval[i][x];
// extracting the table on which the operator would function
temp2 = table_eval[i-1].substr(1, table_eval[i-1].length()-1);
index = ::atof(temp2.c_str());
// calling the specified operator
if (table_eval[i][0] == '#')
dollar[index] = project(temp, dollar[index]);
else if (table_eval[i][0] == '@')
dollar[index] = select(temp, dollar[index]);
else if (table_eval[i][0] == '`')
dollar[index] = rename(temp, dollar[index]);
// removing the operator from stack table_eval
table_eval.erase(table_eval.begin()+i);
i--;
}
else if (table_eval[i][0] == '{') {
// Aggregate function
string temp, temp2, temp3;
int index;
// extracting conditions of the operation
uint x;
for (x = 0;
x < table_eval[i].size() && table_eval[i][x] != '}';
x++)
temp += table_eval[i][x];
temp += table_eval[i][x];
for (x = x+2;
x < table_eval[i].size() && table_eval[i][x] != '}';
x++)
temp2 += table_eval[i][x];
temp2 += table_eval[i][x];
// extracting the table on which the operator would function
temp3 = table_eval[i-1].substr(1, table_eval[i-1].length()-1);
index = ::atof(temp2.c_str());
// calling the specified operator
dollar[index] = aggregate_fn(temp, temp2, dollar[index]);
// removing the operator from stack table_eval
table_eval.erase(table_eval.begin()+i);
i--;
}
else if ( table_eval[i][0] == ':'
|| table_eval[i][0] == '^' || table_eval[i][0] == '%'
|| table_eval[i][0] == '$') {
// extracting the table on which the operator would function
string temp;
int left, right;
// left table
temp = table_eval[i-2].substr(1, table_eval[i-2].length()-1);
left = ::atof(temp.c_str());
// right table
temp = table_eval[i-1].substr(1, table_eval[i-1].length()-1);
right = ::atof(temp.c_str());
// apply the appropriate operator
if (table_eval[i][0] == ':') {
if (set_diff(left, right) == -1)
cout<<"\n > ERROR: TABLES ARE NOT UNION-COMPATIBLE. SO ONLY FIRST TABLE IS PRINTED:\n"
<<" -----";
}
else if (table_eval[i][0] == '^') {
if (union_table(left, right) == -1)
cout<<"\n > ERROR: TABLES ARE NOT UNION-COMPATIBLE. SO ONLY FIRST TABLE IS PRINTED:\n"
<<" -----";
}
else if (table_eval[i][0] == '%')
cartesian(left, right);
// remove the right table and the operator from the stack table_eval
// left <- left op right [working of the operator]
table_eval.erase(table_eval.begin()+i);
table_eval.erase(table_eval.begin()+i-1);
i = i-2;
}
else {
for (uint x = 0; x < list_table.size(); x++)
if (table_eval[i] == list_table[x].name){
table temp = list_table[x];
dollar.push_back(temp);
table_eval[i] = "$"+SSTR(dollar.size()-1);
break;
}
}
}
return 0;
}