-
Notifications
You must be signed in to change notification settings - Fork 0
/
Expression.h
233 lines (203 loc) · 6.48 KB
/
Expression.h
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
/**
* 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/>.
*/
class Expression;
class Until;
class Case;
class For;
class While;
class If;
class Try;
#ifndef _EXPRESSION_H
#define _EXPRESSION_H
typedef struct _Block {
_Block() : elems(0), length(0) { }
Expression** elems;
int length;
} Block;
#include "Interpreter.h"
#include "Exceptions.h"
#include "Element.h"
#include "Value.h"
#include <list>
#include <map>
#include <vector>
#include <stack>
#include <iostream>
//#define debug(x) std::cout << x << '\n'
#define debug(x)
class Expression {
public:
Expression(std::list<Element*> postfix, int offset);
virtual ~Expression();
const int offset;
virtual void eval(Frame* frame, std::stack<Value*>& stack) throw(InterpEx*, int);
//virtual void native(std::stack<Value*> &stack, std::vector<std::string> &native);
protected:
Expression(int offset);
private:
Element** elems;
int length;
};
inline Value* evalExpr(Expression* expr, Frame* frame, std::stack<Value*>& stack) throw(InterpEx*,int) {
int start = stack.size();
expr->eval(frame,stack);
Value* res = stack.top();
stack.pop();
int num = stack.size() - start;
for (int i = 0; i < num; i++) {
Value::decref(stack.top());
stack.pop();
}
return res;
}
inline void cleanStack(std::stack<Value*>& stack) {
while (!stack.empty()) {
Value::decref(stack.top());
stack.pop();
}
}
inline void evalBlock(Block* block, Frame* frame, std::stack<Value*>& stack) throw(InterpEx*,int) {
Expression** elems = block->elems;
Expression** end = elems + block->length;
debug("evalBlock{" << elems << "}");
while (elems != end) {
debug("eval " << *elems);
try {
int start = stack.size();
(*elems)->eval(frame,stack);
int num = stack.size() - start;
for (int i = 0; i < num; i++) {
Value::decref(stack.top());
stack.pop();
}
elems++;
} catch (InterpEx* ex) {
debug("tossed");
ex->addTrace((*elems)->offset);
cleanStack(stack);
throw ex;
} catch (int exi) {
debug("caught");
InterpEx* ex = new InterpEx(exi);
ex->addTrace((*elems)->offset);
cleanStack(stack);
throw ex;
}
}
}
inline void evalBlock(Block* block, Frame* frame) throw(InterpEx*,int) {
std::stack<Value*> stack;
evalBlock(block,frame,stack);
cleanStack(stack);
}
inline void cleanBlock(Block* block) {
if (block->elems) {
Expression** elems = block->elems;
int numElems = block->length;
for (int i = 0; i < numElems; i++) {
delete elems[i];
}
delete [] elems;
}
block->elems = 0;
block->length = 0;
}
inline void fillBlock(Block* block, std::list<Expression*>* exprs) {
cleanBlock(block);
block->length = exprs->size();
block->elems = new Expression*[block->length];
std::list<Expression*>::iterator iter = exprs->begin();
std::list<Expression*>::iterator end = exprs->end();
for (Expression** exprs = block->elems; iter != end; exprs++, iter++)
*exprs = *iter;
}
class Until : public Expression {
public:
Until(std::list<Expression*> block, Expression* condition, int offset);
~Until();
void eval(Frame* frame, std::stack<Value*>& stack) throw(InterpEx*, int);
//void native(std::stack<Value*> &stack, std::vector<std::string> &native) throw(int,InterpEx*);
private:
Expression* cond;
Block block;
};
class Case : public Expression {
public:
Case(Expression* condition, int offset);
~Case();
void setDefault(std::list<Expression*> branch);
void addBranch(int value, std::list<Expression*> branch);
void eval(Frame* frame, std::stack<Value*>& stack) throw(InterpEx*, int);
//void native(std::stack<Value*> &stack, std::vector<std::string> &native) throw(int,InterpEx*);
private:
Expression* value;
std::map<int,Block*> branches;
Block def;
};
class For : public Expression {
public:
For(int var, Expression* begin, bool inc, Expression* end, std::list<Expression*> block, int offset);
~For();
void eval(Frame* frame, std::stack<Value*>& stack) throw(InterpEx*, int);
//void native(std::stack<Value*> &stack, std::vector<std::string> &native) throw(int,InterpEx*);
private:
int var;
Expression* begin;
Expression* end;
bool inc;
Block block;
};
class While : public Expression {
public:
While(Expression* condition, std::list<Expression*> block, int offset);
~While();
void eval(Frame* frame, std::stack<Value*>& stack) throw(InterpEx*, int);
//void native(std::stack<Value*> &stack, std::vector<std::string> &native) throw(int,InterpEx*);
private:
Expression* cond;
Block block;
};
class If : public Expression {
public:
If(Expression* condition, std::list<Expression*> block, int offset);
~If();
void addBranch(Expression* cond, std::list<Expression*> block);
void setDefault(std::list<Expression*> block);
void eval(Frame* frame, std::stack<Value*>& stack) throw(InterpEx*, int);
//void native(std::stack<Value*> &stack, std::vector<std::string> &native) throw(int,InterpEx*);
private:
typedef struct {
Expression* cond;
Block* block;
} Condition;
std::vector<Condition*> branches;
Block def;
};
class Try : public Expression {
public:
Try(std::list<Expression*> danger, std::list<Expression*> saftey, std::list<Expression*> always, int offset);
~Try();
void eval(Frame* frame, std::stack<Value*>& stack) throw(InterpEx*, int);
//void native(std::stack<Value*> &stack, std::vector<std::string> &native) throw(int,InterpEx*);
private:
Block danger;
Block saftey;
Block always;
};
#endif /* _EXPRESSION_H */