-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOperator.cpp
599 lines (563 loc) · 17.8 KB
/
Operator.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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
/**
* Copyright 2010 by Benjamin J. Land (a.k.a. BenLand100)
*
* This file is part of CPascal.
*
* CPascal is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* CPascal is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with CPascal. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stack>
#include <iostream>
#include "Value.h"
#include "Type.h"
#include "Expression.h"
#include "Operator.h"
#include "lexer.h"
#include "Exceptions.h"
class IDiv : public Operator {
public:
IDiv() : Operator(OP_IDIV) { };
~IDiv() { };
void preform(std::stack<Value*> &stack, Frame* frame) throw(int,InterpEx*) {
Value* b = stack.top();
stack.pop();
Value* a = stack.top();
stack.pop();
int res;
if (b->type == TYPE_REAL) {
double fdiv = (int)b->asReal();
if (fdiv == 0) throw E_DIV_ZERO;
res = (a->type == TYPE_REAL ? ((int)a->asReal()) : a->asInteger()) / fdiv;
} else {
int idiv = b->asInteger();
if (idiv == 0) throw E_DIV_ZERO;
res = (a->type == TYPE_REAL ? ((int)a->asReal()) : a->asInteger()) / idiv;
}
stack.push(new IntegerValue(res));
Value::decref(a);
Value::decref(b);
}
};
class FDiv : public Operator {
public:
FDiv() : Operator(OP_FDIV) { };
~FDiv() { };
void preform(std::stack<Value*> &stack, Frame* frame) throw(int,InterpEx*) {
Value* b = stack.top();
stack.pop();
Value* a = stack.top();
stack.pop();
if (b->asReal() == 0) throw E_DIV_ZERO;
double res = a->asReal() / b->asReal();
stack.push(new RealValue(res));
Value::decref(a);
Value::decref(b);
}
};
#include <math.h>
class Mod : public Operator {
public:
Mod() : Operator(OP_MOD) { };
~Mod() { };
void preform(std::stack<Value*> &stack, Frame* frame) throw(int,InterpEx*) {
Value* b = stack.top();
stack.pop();
Value* a = stack.top();
stack.pop();
if (a->type == TYPE_REAL || b->type == TYPE_REAL) {
if (b->asReal() == 0) throw E_DIV_ZERO;
double res = fmod(a->asReal(),b->asReal());
stack.push(new RealValue(res));
} else {
if (b->asInteger() == 0) throw E_DIV_ZERO;
int res = a->asInteger() % b->asInteger();
stack.push(new IntegerValue(res));
}
Value::decref(a);
Value::decref(b);
}
};
class Mul : public Operator {
public:
Mul() : Operator(OP_MUL) { };
~Mul() { };
void preform(std::stack<Value*> &stack, Frame* frame) throw(int,InterpEx*) {
Value* b = stack.top();
stack.pop();
Value* a = stack.top();
stack.pop();
if (a->type == TYPE_REAL || b->type == TYPE_REAL) {
double res = a->asReal() * b->asReal();
stack.push(new RealValue(res));
} else {
int res = a->asInteger() * b->asInteger();
stack.push(new IntegerValue(res));
}
Value::decref(a);
Value::decref(b);
}
};
class Add : public Operator {
public:
Add() : Operator(OP_ADD) { };
~Add() { };
void preform(std::stack<Value*> &stack, Frame* frame) throw(int,InterpEx*) {
Value* b = stack.top();
stack.pop();
Value* a = stack.top();
stack.pop();
if (a->type == TYPE_STRING || b->type == TYPE_STRING) {
std::string str(a->asString());
str.append(b->asString());
stack.push(new StringValue(str));
} else if (a->type == TYPE_REAL || b->type == TYPE_REAL) {
double res = a->asReal() + b->asReal();
stack.push(new RealValue(res));
} else {
int res = a->asInteger() + b->asInteger();
stack.push(new IntegerValue(res));
}
Value::decref(a);
Value::decref(b);
}
};
class Sub : public Operator {
public:
Sub() : Operator(OP_SUB) { };
~Sub() { };
void preform(std::stack<Value*> &stack, Frame* frame) throw(int,InterpEx*) {
Value* b = stack.top();
stack.pop();
Value* a = stack.top();
stack.pop();
if (a->type == TYPE_REAL || b->type == TYPE_REAL) {
double res = a->asReal() - b->asReal();
stack.push(new RealValue(res));
} else {
int res = a->asInteger() - b->asInteger();
stack.push(new IntegerValue(res));
}
Value::decref(a);
Value::decref(b);
}
};
class Not : public Operator {
public:
Not() : Operator(OP_NOT) { };
~Not() { };
void preform(std::stack<Value*> &stack, Frame* frame) throw(int,InterpEx*) {
Value* a = stack.top();
stack.pop();
stack.push(new BooleanValue(!a->asBoolean()));
Value::decref(a);
}
};
class Or : public Operator {
public:
Or() : Operator(OP_OR) { };
~Or() { };
void preform(std::stack<Value*> &stack, Frame* frame) throw(int,InterpEx*) {
Value* b = stack.top();
stack.pop();
Value* a = stack.top();
stack.pop();
stack.push(new BooleanValue(a->asBoolean() || b->asBoolean()));
Value::decref(a);
Value::decref(b);
}
};
class And : public Operator {
public:
And() : Operator(OP_AND) { };
~And() { };
void preform(std::stack<Value*> &stack, Frame* frame) throw(int,InterpEx*) {
Value* b = stack.top();
stack.pop();
Value* a = stack.top();
stack.pop();
stack.push(new BooleanValue(a->asBoolean() && b->asBoolean()));
Value::decref(a);
Value::decref(b);
}
};
class Equ : public Operator {
public:
Equ() : Operator(OP_EQU) { };
~Equ() { };
void preform(std::stack<Value*> &stack, Frame* frame) throw(int,InterpEx*) {
Value* b = stack.top();
stack.pop();
Value* a = stack.top();
stack.pop();
if (a->type == TYPE_REAL || b->type == TYPE_REAL) {
stack.push(new BooleanValue(a->asReal() == b->asReal()));
} else {
stack.push(new BooleanValue(a->asInteger() == b->asInteger()));
}
Value::decref(a);
Value::decref(b);
}
};
class Neq : public Operator {
public:
Neq() : Operator(OP_NEQ) { };
~Neq() { };
void preform(std::stack<Value*> &stack, Frame* frame) throw(int,InterpEx*) {
Value* b = stack.top();
stack.pop();
Value* a = stack.top();
stack.pop();
if (a->type == TYPE_REAL || b->type == TYPE_REAL) {
stack.push(new BooleanValue(a->asReal() != b->asReal()));
} else {
stack.push(new BooleanValue(a->asInteger() != b->asInteger()));
}
Value::decref(a);
Value::decref(b);
}
};
class Less : public Operator {
public:
Less() : Operator(OP_LESS) { };
~Less() { };
void preform(std::stack<Value*> &stack, Frame* frame) throw(int,InterpEx*) {
Value* b = stack.top();
stack.pop();
Value* a = stack.top();
stack.pop();
if (a->type == TYPE_REAL || b->type == TYPE_REAL) {
stack.push(new BooleanValue(a->asReal() < b->asReal()));
} else {
stack.push(new BooleanValue(a->asInteger() < b->asInteger()));
}
Value::decref(a);
Value::decref(b);
}
};
class LessEq : public Operator {
public:
LessEq() : Operator(OP_LESSEQ) { };
~LessEq() { };
void preform(std::stack<Value*> &stack, Frame* frame) throw(int,InterpEx*) {
Value* b = stack.top();
stack.pop();
Value* a = stack.top();
stack.pop();
if (a->type == TYPE_REAL || b->type == TYPE_REAL) {
stack.push(new BooleanValue(a->asReal() <= b->asReal()));
} else {
stack.push(new BooleanValue(a->asInteger() <= b->asInteger()));
}
Value::decref(a);
Value::decref(b);
}
};
class Great : public Operator {
public:
Great() : Operator(OP_GREAT) { };
~Great() { };
void preform(std::stack<Value*> &stack, Frame* frame) throw(int,InterpEx*) {
Value* b = stack.top();
stack.pop();
Value* a = stack.top();
stack.pop();
if (a->type == TYPE_REAL || b->type == TYPE_REAL) {
stack.push(new BooleanValue(a->asReal() > b->asReal()));
} else {
stack.push(new BooleanValue(a->asInteger() > b->asInteger()));
}
Value::decref(a);
Value::decref(b);
}
};
class GreatEq : public Operator {
public:
GreatEq() : Operator(OP_GREATEQ) { };
~GreatEq() { };
void preform(std::stack<Value*> &stack, Frame* frame) throw(int,InterpEx*) {
Value* b = stack.top();
stack.pop();
Value* a = stack.top();
stack.pop();
if (a->type == TYPE_REAL || b->type == TYPE_REAL) {
stack.push(new BooleanValue(a->asReal() >= b->asReal()));
} else {
stack.push(new BooleanValue(a->asInteger() >= b->asInteger()));
}
Value::decref(a);
Value::decref(b);
}
};
class Addr : public Operator {
public:
Addr() : Operator(OP_ADDR) { };
~Addr() { };
void preform(std::stack<Value*> &stack, Frame* frame) throw(int,InterpEx*) {
Value* val = stack.top();
stack.pop();
Pointer* pt = Type::getPointerType((Type*)val->typeObj);
PointerValue* ref = new PointerValue(pt);
ref->setRef(val);
stack.push(ref);
Value::decref(val);
}
};
class DerefGet : public Operator {
public:
DerefGet() : Operator(OP_DEREFGET) { };
~DerefGet() { };
void preform(std::stack<Value*> &stack, Frame* frame) throw(int,InterpEx*) {
Value* ref = stack.top();
stack.pop();
stack.push(Value::incref(ref->getRef()));
Value::decref(ref);
}
};
class DerefSet : public Operator {
public:
DerefSet() : Operator(OP_DEREFSET) { };
~DerefSet() { };
void preform(std::stack<Value*> &stack, Frame* frame) throw(int,InterpEx*) {
Value* val = stack.top();
stack.pop();
Value* pval = stack.top();
stack.pop();
pval->getRef()->set(val);
stack.push(val);
Value::decref(pval);
}
};
class Neg : public Operator {
public:
Neg() : Operator(OP_NEG) { };
~Neg() { };
void preform(std::stack<Value*> &stack, Frame* frame) throw(int,InterpEx*) {
Value* a = stack.top()->clone();
Value::decref(stack.top());
stack.pop();
a->negate();
stack.push(a);
}
};
Throw::Throw(int number_impl) : Operator(OP_THROW), number(number_impl) { }
Throw::~Throw() { }
void Throw::preform(std::stack<Value*> &stack, Frame* frame) throw(int,InterpEx*) {
throw number;
}
Operator* Operator::get(int type) {
switch (type) {
case OP_IDIV:
return new IDiv();
case OP_FDIV:
return new FDiv();
case OP_MOD:
return new Mod();
case OP_MUL:
return new Mul();
case OP_ADD:
return new Add();
case OP_SUB:
return new Sub();
case OP_NOT:
return new Not();
case OP_OR:
return new Or();
case OP_AND:
return new And();
case OP_EQU:
return new Equ();
case OP_NEQ:
return new Neq();
case OP_LESS:
return new Less();
case OP_LESSEQ:
return new LessEq();
case OP_GREAT:
return new Great();
case OP_GREATEQ:
return new GreatEq();
case OP_DEREFGET:
return new DerefGet();
case OP_DEREFSET:
return new DerefSet();
case OP_ADDR:
return new Addr();
case OP_NEG:
return new Neg();
default:
return 0;
}
}
Operator::Operator(int op) : Element(ELEM_OPERATOR), type(op) { }
Operator::~Operator() { }
Asgn::Asgn(int name_impl) : Operator(OP_ASGN), name(name_impl) { }
Asgn::~Asgn() { }
void Asgn::preform(std::stack<Value*> &stack, Frame* frame) throw(int,InterpEx*) {
Value* val = stack.top();
Value* var = frame->resolve(name);
var->set(val);
Value::decref(var);
}
Invoke::Invoke(std::list<Expression*> args_impl) : Operator(OP_SYMBOL), numArgs(args_impl.size()), args(new Expression*[args_impl.size()]) {
std::list<Expression*>::iterator iter = args_impl.begin();
for (int i = 0; i < numArgs; i++) {
args[i] = *iter;
iter++;
}
}
Invoke::~Invoke() {
for (int i = 0; i < numArgs; i++) {
delete args[i];
}
delete [] args;
}
void Invoke::preform(std::stack<Value*>& stack, Frame* frame) throw(int,InterpEx*) {
Value* val = stack.top();
stack.pop();
if (val->type != TYPE_METH) throw E_NOT_METHOD;
Value** vals = new Value*[numArgs];
for (int i = 0; i < numArgs; i++) {
vals[i] = evalExpr(args[i],frame,stack); //Beware of corrupting the stack
}
stack.push(val->invoke(vals, numArgs, frame));
Value::decref(val);
for (int i = 0; i < numArgs; i++) {
Value::decref(vals[i]);
}
delete [] vals;
}
Symbol::Symbol(int name_impl) : Operator(OP_SYMBOL), name(name_impl) { }
Symbol::~Symbol() { }
void Symbol::preform(std::stack<Value*>& stack, Frame* frame) throw(int,InterpEx*) {
stack.push(frame->resolve(name));
}
Size::Size(Expression* array_impl) : Operator(OP_SIZE), array(array_impl) { }
Size::~Size() { delete array; }
void Size::preform(std::stack<Value*>& stack, Frame* frame) throw(int,InterpEx*) {
Value* arr = evalExpr(array,frame,stack);
stack.push(new IntegerValue(arr->size()));
Value::decref(arr);
}
ArrayDef::ArrayDef(std::list<Expression*> elems_impl) : Operator(OP_ARRAYDEF), elems(new Expression*[elems_impl.size()]), numElems(elems_impl.size()) {
std::list<Expression*>::iterator iter = elems_impl.begin();
for (int i = 0; i < numElems; i++) {
elems[i] = *iter;
iter++;
}
}
ArrayDef::~ArrayDef() {
for (int i = 0; i < numElems; i++) {
delete elems[i];
}
delete [] elems;
}
void ArrayDef::preform(std::stack<Value*>& stack, Frame* frame) throw(int,InterpEx*) {
Value** vals = new Value*[numElems];
for (int i = 0; i < numElems; i++)
vals[i] = evalExpr(elems[i],frame,stack);
Type* arrType = Type::getDynamicArrayType((Type*)vals[0]->typeObj);
ArrayValue* val = new ArrayValue((Array*)arrType);
val->resize(numElems);
for (int i = 0; i < numElems; i++) {
val->setIndex(i, vals[i]);
Value::decref(vals[i]);
}
stack.push(val);
}
Resize::Resize(Expression* array_impl, Expression* dim_impl) : Operator(OP_RESIZE), array(array_impl), dim(dim_impl) { }
Resize::~Resize() { delete array; delete dim; }
void Resize::preform(std::stack<Value*>& stack, Frame* frame) throw(int,InterpEx*) {
Value* arr = evalExpr(array,frame,stack);
Value* size = evalExpr(dim,frame,stack);
arr->resize(size->asInteger());
Value::decref(size);
stack.push(arr);
}
ArrayGet::ArrayGet(std::list<Expression*> indexes_impl) : Operator(OP_ARRAYGET), indexes(new Expression*[indexes_impl.size()]), numIndexes(indexes_impl.size()) {
std::list<Expression*>::iterator iter = indexes_impl.begin();
for (int i = 0; i < numIndexes; i++) {
indexes[i] = *iter;
iter++;
}
}
ArrayGet::~ArrayGet() {
for (int i = 0; i < numIndexes; i++) {
delete indexes[i];
}
delete [] indexes;
}
void ArrayGet::preform(std::stack<Value*>& stack, Frame* frame) throw(int,InterpEx*) {
Value* arr = stack.top();
Value* res = 0;
stack.pop();
for (int i = 0; i < numIndexes; i++) {
Value* index = evalExpr(indexes[i],frame,stack);
res = arr->getIndex(index->asInteger());
Value::decref(arr);
Value::decref(index);
arr = res;
}
stack.push(res);
}
ArraySet::ArraySet(std::list<Expression*> indexes_impl) : Operator(OP_ARRAYSET), indexes(new Expression*[indexes_impl.size()]), numIndexes(indexes_impl.size()) {
std::list<Expression*>::iterator iter = indexes_impl.begin();
for (int i = 0; i < numIndexes; i++) {
indexes[i] = *iter;
iter++;
}
}
ArraySet::~ArraySet() {
for (int i = 0; i < numIndexes; i++) {
delete indexes[i];
}
delete [] indexes;
}
void ArraySet::preform(std::stack<Value*>& stack, Frame* frame) throw(int,InterpEx*) {
Value* value = stack.top();
stack.pop();
Value* arr = stack.top();
Value* res = arr;
stack.pop();
for (int i = 0; i < numIndexes - 1; i++) {
Value* index = evalExpr(indexes[i],frame,stack);
res = arr->getIndex(index->asInteger());
Value::decref(arr);
Value::decref(index);
arr = res;
}
Value* index = evalExpr(indexes[numIndexes - 1],frame,stack);
res->setIndex(index->asInteger(),value);
Value::decref(index);
Value::decref(res);
stack.push(value);
}
FieldGet::FieldGet(int name_impl) : Operator(OP_FIELDGET), name(name_impl) { }
FieldGet::~FieldGet() { }
void FieldGet::preform(std::stack<Value*>& stack, Frame* frame) throw(int,InterpEx*) {
Value* record = stack.top();
stack.pop();
Value* res = record->getField(name);
Value::decref(record);
stack.push(Value::incref(res));
}
FieldSet::FieldSet(int name_impl) : Operator(OP_FIELDSET), name(name_impl) { }
FieldSet::~FieldSet() { }
void FieldSet::preform(std::stack<Value*>& stack, Frame* frame) throw(int,InterpEx*) {
Value* val = stack.top();
stack.pop();
Value* record = stack.top();
stack.pop();
record->setField(name,val);
Value::decref(record);
stack.push(val);
}