-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOperator.cpp
More file actions
executable file
·470 lines (422 loc) · 12.4 KB
/
Operator.cpp
File metadata and controls
executable file
·470 lines (422 loc) · 12.4 KB
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
#include "Operator.hpp"
#include "Object.hpp"
#include "Array.hpp"
#include "LanguageCollection.hpp"
#include "LanguageStack.hpp"
#include "LanguageQueue.hpp"
#include <iostream>
#include <cstdlib>
Variable* assignOp(Variable* var1, Variable* var2) {
Types::TypeName t = var2->getType();
Variable* rtn;
if(t == Types::INT) {
var1->setValue(new int(*(int*)(var2->getValue())));
var1->setType(var2->getType());
rtn = new Variable((void*)new int(*(int*)(var1->getValue())), var1->getType(), -1);
} else if( t == Types::DOUBLE) {
var1->setValue(new double(*(double*)(var2->getValue())));
var1->setType(var2->getType());
rtn = new Variable((void*)new double(*(double*)(var1->getValue())), var1->getType(), -1);
} else if( t == Types::BOOLEAN) {
var1->setValue(new bool(*(bool*)(var2->getValue())));
var1->setType(var2->getType());
rtn = new Variable((void*)new bool(*(bool*)(var1->getValue())), var1->getType(), -1);
} else if( t == Types::STRING) {
var1->setValue(new std::string(*(std::string*)(var2->getValue())));
var1->setType(var2->getType());
rtn = new Variable((void*)new std::string(*(std::string*)(var1->getValue())), var1->getType(), -1);
} else if(t == Types::OBJECT) {
var1->setValue(var2->getValue());
((Object*)var1->getValue())->createReference();
var1->setType(t);
rtn = new Variable(var1->getValue(), t, -1);
} else if( t== Types::COMMALIST) {
throw RuntimeException("Cannot assign a Comma List to a variable");
}
return rtn;
}
Variable* negOp(Variable* var1, Variable* var2) {
if(var2->getType() == Types::INT) {
return new Variable(
new int(-*(int*)(var2->getValue())),
Types::INT,
-1
);
} else {
throw RuntimeException("Negation requires an INT");
}
}
Variable* addOp(Variable* var1, Variable* var2) {
if(var1->getType() == Types::INT && var2->getType() == Types::INT) {
return new Variable(
new int(*(int*)(var1->getValue()) + *(int*)(var2->getValue())),
Types::INT,
-1
);
} else {
throw RuntimeException("Addition requires two INTS");
}
}
Variable* subOp(Variable* var1, Variable* var2) {
if(var1->getType() == Types::INT && var2->getType() == Types::INT) {
return new Variable(
new int(*(int*)(var1->getValue()) - *(int*)(var2->getValue())),
Types::INT,
-1
);
} else {
throw RuntimeException("Subtraction requires two INTS");
}
}
Variable* multOp(Variable* var1, Variable* var2) {
if(var1->getType() == Types::INT && var2->getType() == Types::INT) {
return new Variable(
new int(*(int*)(var1->getValue()) * *(int*)(var2->getValue())),
Types::INT,
-1
);
} else {
throw RuntimeException("Multiplication requires two INTS");
}
}
Variable* divOp(Variable* var1, Variable* var2) {
if(var1->getType() == Types::INT && var2->getType() == Types::INT) {
return new Variable(
new int(*(int*)(var1->getValue()) / *(int*)(var2->getValue())),
Types::INT,
-1
);
} else {
throw RuntimeException("Division requires two INTS");
}
}
Variable* modOp(Variable* var1, Variable* var2) {
if(var1->getType() == Types::INT && var2->getType() == Types::INT) {
return new Variable(
new int(*(int*)(var1->getValue()) % *(int*)(var2->getValue())),
Types::INT,
-1
);
} else {
throw RuntimeException("Division requires two INTS");
}
}
Variable* andOp(Variable* var1, Variable* var2) {
if(var1->getType() == Types::BOOLEAN && var2->getType() == Types::BOOLEAN) {
return new Variable(
new bool(*(bool*)(var1->getValue()) && *(bool*)(var2->getValue())),
Types::BOOLEAN,
-1
);
} else {
throw RuntimeException("Boolean Operations require two BOOLEANS");
}
}
Variable* orOp(Variable* var1, Variable* var2) {
if(var1->getType() == Types::BOOLEAN && var2->getType() == Types::BOOLEAN) {
return new Variable(
new bool(*(bool*)(var1->getValue()) || *(bool*)(var2->getValue())),
Types::BOOLEAN,
-1
);
} else {
throw RuntimeException("Boolean Operations require two BOOLEANS");
}
}
Variable* notOp(Variable* var1, Variable* var2) {
if(var2->getType() == Types::BOOLEAN) {
return new Variable(
new bool(!(*(bool*)(var2->getValue()))),
Types::BOOLEAN,
-1
);
} else {
throw RuntimeException("NOT Operation requires a BOOLEANS");
}
}
Variable* ltOp(Variable* var1, Variable* var2) {
if(var1->getType() == Types::INT && var2->getType() == Types::INT) {
return new Variable(
new bool(*(int*)(var1->getValue()) < *(int*)(var2->getValue())),
Types::BOOLEAN,
-1
);
} else {
throw RuntimeException("Less Than Operation requires two numeric values");
}
}
Variable* gtOp(Variable* var1, Variable* var2) {
if(var1->getType() == Types::INT && var2->getType() == Types::INT) {
return new Variable(
new bool(*(int*)(var1->getValue()) > *(int*)(var2->getValue())),
Types::BOOLEAN,
-1
);
} else {
throw RuntimeException("Greater Than Operation requires two numeric values");
}
}
Variable* lteOp(Variable* var1, Variable* var2) {
if(var1->getType() == Types::INT && var2->getType() == Types::INT) {
return new Variable(
new bool(*(int*)(var1->getValue()) <= *(int*)(var2->getValue())),
Types::BOOLEAN,
-1
);
} else {
std::cout << var1->getType() << " " << var2->getType() << std::endl;
throw RuntimeException("Less Than or Equal To Operation requires two numeric values");
}
}
Variable* gteOp(Variable* var1, Variable* var2) {
if(var1->getType() == Types::INT && var2->getType() == Types::INT) {
return new Variable(
new bool(*(int*)(var1->getValue()) >= *(int*)(var2->getValue())),
Types::BOOLEAN,
-1
);
} else {
throw RuntimeException("Greater Than or Equal To Operation requires two numeric values");
}
}
Variable* eqOp(Variable* var1, Variable* var2) {
if(var1->getType() == Types::INT && var2->getType() == Types::INT) {
return new Variable(
new bool(*(int*)(var1->getValue()) == *(int*)(var2->getValue())),
Types::BOOLEAN,
-1
);
} else {
throw RuntimeException("Equality Operation requires two numeric values");
}
}
Variable* neqOp(Variable* var1, Variable* var2) {
if(var1->getType() == Types::INT && var2->getType() == Types::INT) {
return new Variable(
new bool(*(int*)(var1->getValue()) != *(int*)(var2->getValue())),
Types::BOOLEAN,
-1
);
} else {
throw RuntimeException("Inequality Operation requires two numeric values");
}
}
Variable* dotOp(Variable* var1, Variable* var2) {
if(var1->getType() == Types::OBJECT && var2->getType() == Types::LANGUAGEELEMENT) {
return (Variable*)(((Object*)(var1->getValue()))->getFunction(var2));
} else if(var1->getType() == Types::FUNCTION) {
return ((Function*)(var1))->evaluate(var2);
} else {
throw RuntimeException("Invalid operands for dot (.) operation (" + Types::getTypeName(var1->getType()) + " " + Types::getTypeName(var2->getType()) + ")");
}
}
Variable* commaOp(Variable* var1, Variable* var2) {
if(var2->getType() == Types::COMMALIST) {
throw RuntimeException("Cannot append Comma List to Comma List");
}
if(var1->getType() != Types::COMMALIST) {
return new CommaList(var1, var2);
} else {
((CommaList*)var1)->add(var2);
return var1;
}
}
Variable* arrayOp(Variable* var1, Variable* var2) {
if(!(var2->getType() == Types::INT || var2->getType() == Types::LANGUAGEELEMENT)) {
throw RuntimeException("Expected INT or LANGUAGEELEMENT for object initialization");
}
if(var1 == nullptr) {
Object* rtn;
if(var2->getType() == Types::INT) {
rtn = new Array(*(int*)(var2->getValue()));
} else if(var2->getType() == Types::LANGUAGEELEMENT) {
std::string type = *((std::string*)(var2->getValue()));
if(type.compare("stack")==0) {
rtn = new LanguageStack();
} else if(type.compare("queue")==0) {
rtn = new LanguageQueue();
} else if(type.compare("collection")==0) {
rtn = new LanguageCollection();
} else {
throw new RuntimeException("Unrecognized object type \"" + type + "\"");
}
}
return new Variable((void*)rtn, Types::OBJECT, -1);
} else if(var1->getType() == Types::OBJECT) {
if(((Object*)(var1->getValue()))->getType() == Object::ARRAY) {
return ((Array*)(var1->getValue()))->get(*(int*)(var2->getValue()));
} else {
throw RuntimeException("Variable accessed with [] must be an array");
}
} else {
throw RuntimeException("Variable accessed with [] must be an array");
}
}
Operator::Operator(std::string* op, std::string* before, int idx, int dpt) {
index = idx;
depth = dpt;
if(op->compare("-")==0) {
if(((before == nullptr) || (Types::determineType(*before) == Types::OTHER) || (Types::determineType(*before) == Types::OPERATOR))&& (!Variable::isVariable(*before))) {
precedence = 0;
type = NEG;
} else {
precedence = 2;
type = SUB;
}
} else if(op->compare(".")==0) {
precedence = 0;
type = DOT;
} else if(op->compare("[")==0) {
precedence = 0;
type = ARRAY;
} else if(op->compare("*")==0) {
precedence = 1;
type = MULT;
} else if(op->compare("/")==0) {
precedence = 1;
type = DIV;
} else if(op->compare("mod")==0) {
precedence = 1;
type = MOD;
} else if(op->compare("+")==0) {
precedence = 2;
type = ADD;
} else if(op->compare("<")==0) {
precedence = 3;
type = LT;
} else if(op->compare(">")==0) {
precedence = 3;
type = GT;
} else if(op->compare("<=")==0) {
precedence = 3;
type = LTE;
} else if(op->compare(">=")==0) {
precedence = 3;
type = GTE;
} else if(op->compare("==")==0) {
precedence = 3;
type = EQ;
} else if(op->compare("!=")==0) {
precedence = 3;
type = NEQ;
} else if(op->compare("AND")==0) {
precedence = 4;
type = AND;
} else if(op->compare("OR")==0) {
precedence = 4;
type = OR;
} else if(op->compare("NOT")==0) {
precedence = 4;
type = NOT;
} else if(op->compare("=")==0) {
precedence = 5;
type = ASSIGN;
} else if(op->compare(",")==0) {
precedence = 6;
type = COMMA;
} else {
throw RuntimeException(std::string(std::string("Unrecognized operator: ") + "\"" + *op + "\""));
}
}
int Operator::compare(Operator& otherOp) {
if(otherOp.depth > depth) {
return 1;
} else if(otherOp.depth < depth) {
return -1;
} else {
if(otherOp.precedence < precedence) {
return 1;
} else
return -1;
}
}
Variable* Operator::evaluate(Variable* var1, Variable* var2) {
switch(type) {
case ASSIGN:
return assignOp(var1, var2);
case ADD:
return addOp(var1, var2);
case SUB:
return subOp(var1, var2);
case MULT:
return multOp(var1, var2);
case DIV:
return divOp(var1, var2);
case MOD:
return modOp(var1, var2);
case NEG:
return negOp(var1, var2);
case AND:
return andOp(var1, var2);
case OR:
return orOp(var1, var2);
case NOT:
return notOp(var1, var2);
case LT:
return ltOp(var1, var2);
case GT:
return gtOp(var1, var2);
case LTE:
return lteOp(var1, var2);
case GTE:
return gteOp(var1, var2);
case NEQ:
return neqOp(var1, var2);
case EQ:
return eqOp(var1, var2);
case DOT:
return dotOp(var1, var2);
case COMMA:
return commaOp(var1, var2);
case ARRAY:
return arrayOp(var1, var2);
}
}
void Operator::printDetails() {
std::cout << "Index: " << index << " Precedence: " << precedence << " Depth: " << depth << " Type: ";
print();
std::cout << " | ";
}
void Operator::print() {
switch(type) {
case ASSIGN:
std::cout << "="; break;
case ADD:
std::cout << "+"; break;
case SUB:
std::cout << "-"; break;
case MULT:
std::cout << "*"; break;
case DIV:
std::cout << "/"; break;
case MOD:
std::cout << "mod"; break;
case NEG:
std::cout << "NEG"; break;
case AND:
std::cout << "AND"; break;
case OR:
std::cout << "OR"; break;
case NOT:
std::cout << "NOT"; break;
case LT:
std::cout << "<"; break;
case GT:
std::cout << ">"; break;
case LTE:
std::cout << "<="; break;
case GTE:
std::cout << ">="; break;
case NEQ:
std::cout << "!="; break;
case EQ:
std::cout << "=="; break;
case DOT:
std::cout << "."; break;
case COMMA:
std::cout << ","; break;
case ARRAY:
std::cout << "["; break;
}
}
const std::regex Operator::operatorRegex = std::regex("[^a-zA-Z\\d\\s]+");