-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCallback.cpp
More file actions
206 lines (180 loc) · 4.81 KB
/
Copy pathCallback.cpp
File metadata and controls
206 lines (180 loc) · 4.81 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
/*
Zachary Weaver
27/08/2011
Callback.cpp
*/
#include "Callback.h"
using namespace std;
using namespace Dcm;
static unsigned char type[] = {0};
DcmType *Dcm::raw_peek(string& sym, Scope *scope) {
Namespace *ns;
DcmType *ret = NULL;
DcmStack *stk;
if (scope->empty()) {
return NULL;
}
else {
ns = scope->top();
scope->pop();
// if is a DcmClass ...
if (ns->id() == 'c') {
ret = static_cast<DcmClass*>(ns)->peek(sym);
if (!ret) {
ret = raw_peek(sym, scope);
}
}
else {
stk = &(*ns)[sym];
if (stk->empty()) {
ret = raw_peek(sym, scope);
}
else {
ret = stk->top();
}
}
scope->push(ns);
return ret;
}
}
// Utilities
void Dcm::callbackLoop(Callback* cb, Interpretter* interpretter) {
Callback* cbNext;
while (cb) {
cbNext = cb->run(interpretter);
if (cb->mustDestroy())
delete cb;
cb = cbNext;
}
}
void Dcm::checkTypes(DcmType* dcm, vector<TypeVal> types)
throw (DcmTypeError*) {
if (types.size() == 0) return;
for (auto type : types) {
if (dcm->isType(type)) return;
}
throw new DcmTypeError(types, dcm->type());
}
DcmType *Dcm::safePeekMain(Interpretter* interpretter, vector<TypeVal> types)
throw (DcmError*) {
return safePeekMain(interpretter->mainStack, types);
}
DcmType *Dcm::safePeekMain(Interpretter* interpretter) throw (DcmError*) {
return safePeekMain(interpretter->mainStack, {});
}
DcmType *Dcm::safePeekMain(DcmStack& stk, vector<TypeVal> types)
throw (DcmError*) {
DcmType *dcm;
if (!stk.empty()) {
dcm = stk.top();
}
else {
throw new DcmStackError(new DcmSymbol("main stack"),
DcmPrimFun::typeVal());
}
checkTypes(dcm, types);
return dcm;
}
DcmType *Dcm::safePeekMain(DcmStack& stk) throw (DcmError*) {
return safePeekMain(stk, {});
}
DcmType **Dcm::popN(DcmStack& stk, unsigned int n) {
int pos = 0;
DcmType **rets = new DcmType*[n];
for (; pos < n; pos++) {
try {
rets[pos] = safePeekMain(stk);
stk.pop();
}
catch (DcmStackError *ex) {
// Let's free all the stuff we took first
for(pos--; pos >= 0; pos--) {
stk.push(rets[pos]);
}
delete rets;
throw ex;
}
}
return rets;
}
// Callback {
DcmType *Callback::Peek(Scope *scope, string& sym)
throw (DcmStackError*) {
DcmType *ret=NULL;
if (ret = raw_peek(sym, scope)) {
return ret;
}
else {
throw new DcmStackError(new DcmSymbol(sym), type);
}
}
DcmType *Callback::Pop(Scope *scope, string& sym)
throw (DcmStackError*) {
DcmStack *stk;
DcmType *ret;
stk = &(*scope->top())[sym];
if (stk->empty()) {
throw new DcmStackError(new DcmSymbol(sym), type);
}
else {
ret = stk->top();
stk->pop();
return ret;
}
}
void Callback::Push(Scope *scope, string& sym, DcmType *item){
(*scope->top())[sym].push(item);
}
bool Callback::mustDestroy() {
return false;
}
// };
// SimpleCallback {
SimpleCallback::SimpleCallback(function<void(DcmStack&)>callback) {
cb = callback;
}
Callback *SimpleCallback::run(Interpretter *interpretter) {
cb(interpretter->mainStack);
return NULL;
}
// };
// ExecCallback {
ExecCallback::ExecCallback() {
dcmRun = NULL;
}
ExecCallback::ExecCallback(DcmExec *exec) {
if (exec) dcmRun = static_cast<DcmExec*>(dup(exec));
}
ExecCallback::~ExecCallback() {
if (dcmRun) del(dcmRun);
}
Callback *ExecCallback::run(Interpretter *interpretter) {
if (!dcmRun) return NULL;
if (dcmRun->size() == 0) {
return NULL;
}
auto end = dcmRun->end();
end--;
for (auto i = dcmRun->begin(); i != end; i++) {
if ((*i)->isType(DcmPrimFun::typeVal())) {
Callback *cb = static_cast<DcmPrimFun*>(*i)
->run(interpretter);
callbackLoop(cb, interpretter);
}
else {
interpretter->mainStack.push(dup(*i));
}
}
if ((*end)->isType(DcmPrimFun::typeVal())) {
return static_cast<DcmPrimFun*>(*end)
->run(interpretter);
}
else {
interpretter->mainStack.push(dup(*end));
return NULL;
}
}
bool ExecCallback::mustDestroy() {
return true;
}
// };