-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExpression.cpp
278 lines (267 loc) · 8.46 KB
/
Expression.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
/**
* 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 "Expression.h"
#include "Value.h"
#include "Operator.h"
#include <stack>
Expression::Expression(int offset_impl) : offset(offset_impl), elems(0), length(0) { }
Expression::Expression(std::list<Element*> postfix,int offset_impl) : offset(offset_impl), elems(new Element*[postfix.size()]), length(postfix.size()) {
std::list<Element*>::iterator iter = postfix.begin();
for (int i = 0; i < length; i++)
elems[i] = *(iter++);
}
Expression::~Expression() {
if (elems) {
for (int i = 0; i < length; i++)
delete elems[i];
delete [] elems;
}
}
void Expression::eval(Frame* frame, std::stack<Value*>& stack) throw(InterpEx*, int) {
for (int i = 0; i < length; i++) {
Element* elem = elems[i];
switch(elem->eltype) {
case ELEM_VALUE:
debug("\t-Value{" << ((Value*)elem)->type << "}");
stack.push(Value::incref(((Value*)elem)));
break;
case ELEM_OPERATOR:
debug("\t-Operator{" << ((Operator*)elem)->type << "}");
((Operator*)elem)->preform(stack,frame);
break;
}
}
}
Until::Until(std::list<Expression*> block_impl, Expression* cond_impl, int offset_impl) : Expression(offset_impl), cond(cond_impl) {
fillBlock(&block,&block_impl);
}
Until::~Until() {
cleanBlock(&block);
delete cond;
}
void Until::eval(Frame* frame, std::stack<Value*>& stack) throw(InterpEx*, int) {
Value* res = 0;
try {
do {
if (res) Value::decref(res);
evalBlock(&block,frame,stack);
res = evalExpr(cond,frame,stack);
} while (!res->asBoolean());
Value::decref(res);
} catch (int exi) {
if (exi == E_BREAK)
return;
throw exi;
} catch (InterpEx* ex) {
if (ex->getCause() == E_BREAK) {
delete ex;
return;
}
throw ex;
}
}
Case::Case(Expression* condition, int offset_impl) : Expression(offset_impl), value(condition) { }
Case::~Case() {
delete value;
std::map<int,Block*>::iterator iter = branches.begin();
while (iter != branches.end()) {
cleanBlock(iter->second);
delete iter->second;
iter++;
}
cleanBlock(&def);
}
void Case::setDefault(std::list<Expression*> branch) {
fillBlock(&def,&branch);
}
void Case::addBranch(int value, std::list<Expression*> branch) {
Block* block = new Block;
fillBlock(block, &branch);
if (branches.find(value) != branches.end()) {
Block* old = branches[value];
cleanBlock(old);
delete old;
}
branches[value] = block;
}
void Case::eval(Frame* frame, std::stack<Value*>& stack) throw(InterpEx*, int) {
Value* val = evalExpr(value,frame,stack);
int i = val->asInteger();
Value::decref(val);
try {
if (branches.find(i) != branches.end()) {
evalBlock(branches[i],frame,stack);
} else if (def.elems) {
evalBlock(&def,frame,stack);
}
} catch (int exi) {
if (exi == E_BREAK)
return;
throw exi;
} catch (InterpEx* ex) {
if (ex->getCause() == E_BREAK) {
delete ex;
return;
}
throw ex;
}
}
For::For(int var_impl, Expression* begin_impl, bool inc_impl, Expression* end_impl, std::list<Expression*> block_impl, int offset_impl) : Expression(offset_impl), var(var_impl), begin(begin_impl), end(end_impl), inc(inc_impl) {
fillBlock(&block,&block_impl);
}
For::~For() {
cleanBlock(&block);
delete begin;
delete end;
}
void For::eval(Frame* frame, std::stack<Value*>& stack) throw(InterpEx*, int) {
Value* varval = frame->resolve(var);
Value* temp = evalExpr(begin,frame,stack);
varval->set(temp);
Value::decref(temp);
try {
if (inc) { //simply reduces tests and increases code size
temp = evalExpr(end,frame,stack);
while (varval->asInteger() <= temp->asInteger()) {
Value::decref(temp);
evalBlock(&block,frame,stack);
temp = evalExpr(end,frame,stack);
varval->incr();
}
Value::decref(temp);
} else {
temp = evalExpr(end,frame,stack);
while (varval->asInteger() >= temp->asInteger()) {
Value::decref(temp);
evalBlock(&block,frame,stack);
temp = evalExpr(end,frame,stack);
varval->decr();
}
Value::decref(temp);
}
Value::decref(varval);
} catch (int exi) {
Value::decref(varval);
if (exi == E_BREAK)
return;
throw exi;
} catch (InterpEx* ex) {
Value::decref(varval);
if (ex->getCause() == E_BREAK) {
delete ex;
return;
}
throw ex;
}
}
While::While(Expression* cond_impl, std::list<Expression*> block_impl, int offset_impl) : Expression(offset_impl), cond(cond_impl) {
fillBlock(&block,&block_impl);
}
While::~While() {
cleanBlock(&block);
delete cond;
}
void While::eval(Frame* frame, std::stack<Value*>& stack) throw(InterpEx*, int) {
Value* res = evalExpr(cond,frame,stack);
debug("while_restype=" << res->type);
try {
while (res->asBoolean()) {
Value::decref(res);
evalBlock(&block,frame,stack);
res = evalExpr(cond,frame,stack);
}
Value::decref(res);
} catch (int exi) {
if (exi == E_BREAK)
return;
throw exi;
} catch (InterpEx* ex) {
if (ex->getCause() == E_BREAK) {
delete ex;
return;
}
throw ex;
}
}
If::If(Expression* cond_impl, std::list<Expression*> block_impl, int offset_impl) : Expression(offset_impl) {
addBranch(cond_impl,block_impl);
}
If::~If() {
int numBranches = branches.size();
for (int i = 0; i < numBranches; i++) {
Condition* cond = branches[i];
cleanBlock(cond->block);
delete cond->block;
delete cond->cond;
delete cond;
}
cleanBlock(&def);
}
void If::addBranch(Expression* cond_impl, std::list<Expression*> block_impl) {
Condition* cond = new Condition();
cond->cond = cond_impl;
cond->block = new Block;
fillBlock(cond->block,&block_impl);
branches.push_back(cond);
}
void If::setDefault(std::list<Expression*> block) {
fillBlock(&def,&block);
}
void If::eval(Frame* frame, std::stack<Value*>& stack) throw(InterpEx*, int) {
int numBranches = branches.size();
for (int i = 0; i < numBranches; i++) {
Value* test = evalExpr(branches[i]->cond,frame,stack);
if (test->asBoolean()) {
Value::decref(test);
evalBlock(branches[i]->block,frame,stack);
return;
}
Value::decref(test);
}
evalBlock(&def,frame,stack);
}
Try::Try(std::list<Expression*> danger_impl, std::list<Expression*> saftey_impl, std::list<Expression*> always_impl, int offset_impl) : Expression(offset_impl) {
fillBlock(&danger,&danger_impl);
fillBlock(&saftey,&saftey_impl);
fillBlock(&always,&always_impl);
}
Try::~Try() {
cleanBlock(&danger);
cleanBlock(&saftey);
cleanBlock(&always);
}
void Try::eval(Frame* frame, std::stack<Value*>& stack) throw(InterpEx*, int) {
try {
evalBlock(&danger,frame,stack);
} catch (int exi) {
if (InterpEx::getType(exi) == E_NOCATCH) {
evalBlock(&always,frame,stack);
throw exi;
}
evalBlock(&saftey,frame,stack);
} catch (InterpEx* ex) {
if (ex->getType() == E_NOCATCH) {
evalBlock(&always,frame,stack);
throw ex;
}
delete ex;
evalBlock(&saftey,frame,stack);
}
evalBlock(&always,frame,stack);
}