Repository navigation
Expand file tree
/
Copy pathparser.cpp
More file actions
292 lines (239 loc) · 8.15 KB
/
Copy pathparser.cpp
File metadata and controls
292 lines (239 loc) · 8.15 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
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
#include "parser.h"
#include <iostream>
#include <sstream>
#include <map>
#define INS_ARR INSTRUCTION_IDS
#define INS_SIZE INSTRUCTION_SIZE
std::string parseStr(const std::string& s) {
std::stringstream sout;
for(size_t i = 1; i < s.length() - 1; i++) {
if (s.at(i) == '\\') {
switch(s.at(i + 1)) {
case 'n': sout << "\n"; i++; break;
case '"': sout << "\""; i++; break;
case 't': sout << "\t"; i++; break;
default: sout << "\\"; break;
}
} else {
sout << s.at(i);
}
}
return sout.str();
}
std::string parseChr(const std::string& s) {
if (s.size() == 4) { //'\x'
switch(s[2]) {
case 'n': return "\n"; break;
case '"': return "\""; break;
case 't': return "\t"; break;
case '\\': return "\\"; break;
default: return "\0";
}
}
return s.substr(1,1);
}
// 0 - is not num, 1 - is num, 2 - is float
int isNum(std::string str) {
bool com = false;
if (str == "-") return 0;
for (size_t i = str[0] == '-' ? 1 : 0; i < str.size(); i++) {
if (str[i] == '.') {
if (com) return 0;
com = true;
continue;
}
if (!std::isdigit(str[i])) {
return 0;
}
}
if (com) return 2;
return 1;
}
std::vector<int> getArgTypeList(std::vector<std::string> args, size_t ln = 1) {
std::vector<int> argtypes;
for (std::string arg : args) {
if (arg.size() == 3 && arg[0] == '\'' && arg[2] == '\'') {
argtypes.push_back(v_CHR);
continue;
}
if (arg.size() == 4 && arg[0] == '\'' && arg[1] == '\\' && arg[3] == '\'') {
argtypes.push_back(v_CHR);
continue;
}
const int isnres = isNum(arg);
if (isnres != 0) {
int toPush = v_NUM;
if (isnres == 2) toPush = v_NUM_F;
argtypes.push_back(toPush);
continue;
}
if (arg.size() < 2) {
std::cerr << "[Parse Error] Invalid symbol on line " << ln << std::endl;
exit(1);
}
int argid = v_DAT;
switch(arg[0]) {
case '"': argid = v_STR; break;
case '$': argid = v_REG; break;
case '#': argid = v_VAR; break;
case '*': argid = v_MEM; break;
}
argtypes.push_back(argid);
}
return argtypes;
}
int getInsType(std::string str) {
std::string search = str;
if (str[0] == ':') search = "LBL";
if (str == ".data") search = "FLG_1";
if (str == ".main") search = "FLG_2";
if (str[str.size() - 1] == ':') search = "VAR";
for (size_t i = 0; i < INS_SIZE; i++) {
if (INS_ARR[i] == search) return i;
}
return -1;
}
void parseVar(std::vector<std::string> line, Instruction& result, size_t ln = 1) {
if (line.size() != 2) {
std::cerr << "[Parse Error] variable name expected 1 argument. On line " << ln << std::endl;
exit(1);
}
if (line[0].substr(0, line[0].size() - 1).find_first_not_of("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890_") != std::string::npos) {
std::cerr << "[Parse Error] Variable name contains invalid characters. On line " << ln << std::endl;
exit(1);
}
if (line[0] == ":") {
std::cerr << "[Parse Error] Variable does not have a name. On line " << ln << std::endl;
exit(1);
}
result.uid = line[0].substr(0, line[0].size() - 1);
line.erase(line.begin());
result.arg_types = getArgTypeList(line, ln);
if (result.arg_types[0] == v_STR) {
line[0] = parseStr(line[0]);
result.args = line;
} else if (result.arg_types[0] == v_CHR) {
line[0] = parseChr(line[0]);
result.args = line;
} else if (result.arg_types[0] == v_DAT || result.arg_types[0] == v_VAR || result.arg_types[0] == v_REG) {
std::cerr << "[Parse Error] Variable arg type invalid on line " << ln << std::endl;
exit(1);
} else {
result.args = line;
}
result.size = result.args[0].size();
}
std::map<std::string, size_t> labelList;
void parseLabel(std::vector<std::string> line, Instruction& result, size_t ln = 1) {
if (line.size() != 1) {
std::cerr << "[Parse Error] Label expects no arguments. On line " << ln << std::endl;
exit(1);
}
if (line[0] == ":") {
std::cerr << "[Parse Error] Label declaration expects a name. On line " << ln << std::endl;
exit(1);
}
result.uid = line[0].substr(1);
if (labelList.find(result.uid) != labelList.end()) {
std::cerr << "[Parse Error] Label \"" << result.uid << "\" has alredy been declared. On line " << ln << std::endl;
exit(1);
}
labelList[result.uid] = ln - 1;
}
void parseJmpGeneral(std::vector<std::string> line, Instruction& result, std::vector<size_t>& toLink, size_t ln = 1) {
line.erase(line.begin());
if (line.size() != 1) {
std::cerr << "[Parse Error] Jump instruction expected a label name. On line " << ln << std::endl;
exit(1);
}
result.args = line;
result.arg_types.push_back(v_LBL);
if (labelList.find(result.args[0]) == labelList.end()) {
result.jmp_index = 0;
toLink.push_back(ln - 1);
} else {
result.jmp_index = labelList[result.args[0]];
}
}
void parseGeneric(std::vector<std::string> line, Instruction& result, size_t ln = 1) {
const std::string iname(line[0]);
line.erase(line.begin());
if (line.size() == 0) {
std::cerr << "[Parse Error] Instruction " << iname << " expected at least 1 argument. On line " << ln << std::endl;
exit(1);
}
result.arg_types = getArgTypeList(line, ln);
//.find_first_not_of("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") != std::string::npos
std::vector<std::string> arglist;
for (size_t i = 0; i < line.size(); i++) {
if (result.arg_types[i] == v_DAT) {
if (line[i].find_first_not_of("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") != std::string::npos) {
std::cerr << "[Parse Error] Argument contains illegal characters. On line " << ln << std::endl;
exit(1);
}
}
if (result.arg_types[i] == v_STR) {
std::string pstring = parseStr(line[i]);
arglist.push_back(pstring);
continue;
}
if (result.arg_types[i] == v_CHR) {
std::string pstring = parseChr(line[i]);
arglist.push_back(pstring);
continue;
}
arglist.push_back(line[i]);
}
result.args = arglist;
}
std::vector<Instruction> parse(std::vector<std::vector<std::string>> adt) {
std::vector<Instruction> result;
std::vector<size_t> toLink;
size_t ln = 0;
for (std::vector<std::string> line : adt) {
ln += 1;
if (line.size() == 1 && line[0].empty()) continue;
if (line.size() == 0) continue;
const int ins = getInsType(line[0]);
if (ins == -1) {
std::cerr << "[Parse Error] Instruction \"" << line[0] << "\" undefined on line " << ln << ".\n";
exit(1);
}
Instruction toPush;
toPush.type = ins;
bool ccomp = false;
if (ins == i_VAR) {
parseVar(line, toPush, ln);
ccomp = true;
}
if (ins == i_LBL) {
parseLabel(line, toPush, ln);
ccomp = true;
}
// jumps
if (ins >= i_jeq && ins <= i_jmp) {
parseJmpGeneral(line, toPush, toLink, ln);
ccomp = true;
}
if (!ccomp) {
if (!(ins == i_return || ins == i_syscall || ins == i_FLG_1 || ins == i_FLG_2)) {
parseGeneric(line, toPush, ln);
} else {
if (line.size() != 1) {
std::cout << "[Parse Error] Instruction " << line[0] << " expected no arguments. On line " << ln << std::endl;
exit(1);
}
}
}
result.push_back(toPush);
}
for (size_t i : toLink) {
if (labelList.find(result[i].args[0]) == labelList.end()) {
std::cerr << "[Parse Error] Label \"" << result[i].args[0] << "\" undefined. On line " << (i + 1) << std::endl;;
exit(1);
} else {
result[i].jmp_index = labelList[result[i].args[0]];
}
}
return result;
}