-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathExecParser.cpp
More file actions
227 lines (204 loc) · 5.7 KB
/
Copy pathExecParser.cpp
File metadata and controls
227 lines (204 loc) · 5.7 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
/*
Zachary Weaver
12/09/2011
ExecParser.cpp
*/
#include "Interpretter.h"
#include "ExecParser.h"
using namespace std;
using namespace Dcm;
PushCallback::PushCallback(string stackName) {
stkName = stackName;
}
Callback *PushCallback::run(Interpretter *interpretter) {
// The parser will increment this so it begins at the
//+ start of the string (as opposed to one after)
int i = -1;
interpretter->push(stkName, i);
return NULL;
}
bool PushCallback::mustDestroy() {
return true;
}
void Interpretter::ex_push(string& commands, int& i) {
int start , end;
start = i;
end = findEnd(commands, i);
cont.top()->push_back(new DcmPrimFun(new PushCallback(
commands.substr(start, end))));
}
PopCallback::PopCallback(string stackName) {
stkName = stackName;
}
Callback *PopCallback::run(Interpretter *interpretter) {
// The parser will increment this so it begins at the
//+ start of the string (as opposed to one after)
int i = -1;
interpretter->pop(stkName, i);
return NULL;
}
bool PopCallback::mustDestroy() {
return true;
}
void Interpretter::ex_pop(string& commands, int& i) {
int start, end;
start = i;
end = findEnd(commands, i);
cont.top()->push_back(new DcmPrimFun(new PopCallback(
commands.substr(start, end))));
}
SwapCallback::SwapCallback(string stackName) {
stkName = stackName;
}
Callback *SwapCallback::run(Interpretter *interpretter) {
// The parser will increment this so it begins at the
//+ start of the string (as opposed to one after)
int i = -1;
interpretter->swap(stkName, i);
return NULL;
}
bool SwapCallback::mustDestroy() {
return true;
}
void Interpretter::ex_swap(string& commands, int& i) {
int start, end;
start = i;
end = findEnd(commands, i);
cont.top()->push_back(new DcmPrimFun(new SwapCallback(
commands.substr(start, end))));
}
EmptyCallback::EmptyCallback(string stackName) {
stkName = stackName;
}
Callback *EmptyCallback::run(Interpretter *interpretter) {
int i = 0;
interpretter->empty(stkName, i);
return NULL;
}
bool EmptyCallback::mustDestroy() {
return true;
}
void Interpretter::ex_empty(string& commands, int& i) {
int start, end;
start = i;
end = findEnd(commands, i);
cont.top()->push_back(new DcmPrimFun(new EmptyCallback(
commands.substr(start, end))));
}
PeekCallback::PeekCallback(string stackName, bool checkScope) {
stkName = stackName;
chkScope = checkScope;
}
Callback *PeekCallback::run(Interpretter *interpretter) {
int i = 0;
interpretter->peek(stkName, i, chkScope);
return NULL;
}
bool PeekCallback::mustDestroy() {
return true;
}
void Interpretter::ex_peek(string& commands, int& i, bool checkScope) {
int start, end;
start = i;
end = findEnd(commands, i);
cont.top()->push_back(new DcmPrimFun(new PeekCallback(
commands.substr(start, end), checkScope)));
}
AttribCallback::AttribCallback(string stackName) {
stkName = stackName;
}
Callback *AttribCallback::run(Interpretter *interpretter) {
int i = 0;
interpretter->attrib(stkName, i);
return NULL;
}
bool AttribCallback::mustDestroy() {
return true;
}
void Interpretter::ex_attrib(string& commands, int& i) {
int start, end;
start = i;
end = findEnd(commands, i);
cont.top()->push_back(new DcmPrimFun(new AttribCallback(
commands.substr(start, end))));
}
void Interpretter::exec(string& execStr, int& i) {
if (strCont != "") {
strCont += '\n';
str(execStr, i);
if (i < execStr.size()) {
i++;
mainStack.push(new DcmString(strCont));
strCont = "";
}
}
skipWhitespace(execStr, i);
while (i < execStr.size()) {
if (execStr[i] <= '9' && execStr[i] >= '0'
|| execStr[i] == '-') {
DcmElem* num = number(execStr, i);
if (num) {
cont.top()->push_back(num);
continue;
}
}
switch (execStr[i]) {
case '#':
return;
case ']':
// Close exec
DcmExec *top;
i++;
top = cont.top();
cont.pop();
if (cont.empty()) {
mainStack.push(top);
return;
}
else {
skipWhitespace(execStr, i);
cont.top()->push_back(top);
}
break;
case '$':
ex_push(execStr, ++i);
break;
case '@':
ex_pop(execStr, ++i);
break;
case '~':
ex_swap(execStr, ++i);
break;
case '?':
ex_empty(execStr, ++i);
break;
case '.':
ex_attrib(execStr, ++i);
break;
case '[':
cont.push(new DcmExec());
skipWhitespace(execStr, ++i);
break;
case '\"':
str(execStr, ++i);
if (i < execStr.size()) {
cont.top()->push_back(new DcmString(strCont));
strCont = "";
i++;
}
skipWhitespace(execStr, i);
break;
case ',':
cont.top()->push_back(sym(execStr, i));
break;
case '\'':
cont.top()->push_back(ch(execStr, i));
break;
case '^':
ex_peek(execStr, ++i, false);
break;
default:
ex_peek(execStr, i, true);
}
}
}