/
MacroExtender.cpp
347 lines (333 loc) · 11.5 KB
/
MacroExtender.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/**
* @author Emanuel B.
* Macro Extender class source code and side functions
*
**/
#include "MacroExtender.h"
MacroExtender::MacroExtender(string inName,string outName,bool hasDebug) : Assembler(inName,outName,hasDebug) {
//cout << "File(s): \"" << this->fileIn << "\" and \"" << this->fileOut << "\"." << endl;
}
int MacroExtender::macroExtend() {
bool canWrite = false;
int numLine;
int macroBound = 0;
int macroLastStart = 0,macroLastEnd = 0;
bool textSegment = false;
bool dataSegment = false;
bool isMacroBody = false;
smatch auxMatch;
string curLine;
vector<string> currentCode;
vector<string> macroParams;
vector<string> macroLines;
fileIn.append(".pre");
fileOut.append(".mcr");
ifstream inputFile(fileIn,ios::in);
if (!inputFile) {
cerr << "Fatal Error: Could not open file " << fileIn << endl;
return -1;
}
cout << "Opened " << fileIn << " for macro-extending..." << endl;
ofstream outputFile(fileOut,ios::out);
if (!outputFile) {
cerr << "Fatal Error: Could not create file " << fileOut << endl;
return -1;
}
while (getline(inputFile,curLine)) {
// prepareLine(curLine);
if (curLine.find("SECTION ")!=string::npos && regex_search(curLine,auxMatch,regex("\\bSECTION "))) {
if (regex_search(curLine,auxMatch,regex("\\bTEXT$"))) {
textSegment = true;
}
else {
textSegment = false;
}
if (regex_search(curLine,auxMatch,regex("\\bDATA$"))) {
dataSegment = true;
}
else {
dataSegment = false;
}
}
else if (curLine.find(" MACRO ")!=string::npos || regex_search(curLine,auxMatch,regex(" MACRO$")) || regex_search(curLine,auxMatch,regex("\\bMACRO "))) {
macroBound++;
macroLastStart = numLine;
if (textSegment || dataSegment) {
addError(numLine,string("Semantical Error: MACRO definitions must go before all"));
}
}
else if (regex_search(curLine,auxMatch,regex("\\bMACRO "))) {
macroBound++;
macroLastStart = numLine;
if (textSegment || dataSegment) {
addError(numLine,string("Semantical Error: MACRO definitions must go before all"));
}
}
else if (regex_search(curLine,auxMatch,regex("\\bENDMACRO$"))) {
macroBound--;
macroLastEnd = numLine;
if (textSegment || dataSegment) {
addError(numLine,string("Semantical Error: ENDMACRO must go before all"));
}
}
currentCode.push_back(curLine);
currentCode.shrink_to_fit();
numLine++;
}
//cout << "Macro Bound: " << macroBound << endl;
if (macroBound>0) {
addError(macroLastStart,string("Semantical Error: Caught at least one MACRO without a corresponding ENDMACRO"));
}
else if (macroBound<0) {
addError(macroLastEnd,string("Semantical Error: Caught at least one ENDMACRO without a corresponding MACRO"));
}
vector<string>::iterator codeIterator;
for (numLine=1,codeIterator=currentCode.begin();codeIterator!=currentCode.end();codeIterator++,numLine++) {
curLine = *codeIterator;
canWrite = true;
//cout << "@" << hasMacro(curLine) << endl;
if (curLine.find("SECTION ")!=string::npos && regex_search(curLine,auxMatch,regex("\\bSECTION "))) {
if (regex_search(curLine,auxMatch,regex("\\bTEXT$"))) {
textSegment = true;
}
else {
textSegment = false;
}
outputFile << curLine << "\n";
}
else if (curLine.find(" MACRO ")!=string::npos || regex_search(curLine,auxMatch,regex(" MACRO$")) || regex_search(curLine,auxMatch,regex("\\bMACRO "))) {
macroParams.clear();
macroLines.clear();
if (regex_search(curLine,auxMatch,regex("\\bMACRO ")) && curLine.find("MACRO ")==0) {
addError(numLine,string("Syntactical Error: MACRO must have a definition label"));
}
else if (!regex_search(curLine,auxMatch,regex(labelReg))) {
addError(numLine,string("Syntactical Error: MACRO must have a definition label"));
}
else if (macroBound<=0 || (macroBound>0 && numLine!=macroLastStart)) {
int macroDefLine = numLine;
int numParameters;
string macroLabel = curLine.substr(0,curLine.find_last_of(':'));
if (directiveTable.isDirective(macroLabel) || opcodeTable.isInstruction(macroLabel)) {
addError(numLine,string("Semantical Error: Invalid use of keyword"));
}
if (macroLabel.find(':')!=string::npos) {
addError(numLine,string("Syntactical Error: Too many definitions for MACRO constructor"));
}
if (curLine.find(" MACRO ") == string::npos) {
numParameters = 0;
}
else {
string auxParameters = curLine.substr(curLine.find(" MACRO ")+7);
splitTokenize(auxParameters,macroParams,",");
if (hasDuplicates(macroParams)) {
addError(numLine,"Semantical Errors: Duplicate parameter detected");
}
for (const string ¯oRef : macroParams) {
if (!regex_match(macroRef,regex("\\&[\\w]"))) {
addError(numLine,"Syntactical Error: Invalid parameter(s) for MACRO statement");
break;
}
}
numParameters = macroParams.size();
if (numParameters > MACRO_MAX_PARAMS) {
string pError = "Semantical Error: Too many parameters for MACRO definition (max at ";
pError.append(to_string(MACRO_MAX_PARAMS));
pError.append(" )");
pError.shrink_to_fit();
addError(numLine,pError);
}
//cout << macroParams << endl;
}
if (MNT.find(macroLabel) != MNT.end()) {
addError(numLine,"Semantical Error: Macro redefinition detected");
}
MNT.emplace(macroLabel,make_pair(macroDefLine,numParameters));
isMacroBody = true;
codeIterator++;
while (isMacroBody && codeIterator != currentCode.end()) {
curLine = *codeIterator;
if (curLine.find(" EXTERN") != string::npos && regex_search(curLine,auxMatch,regex(" EXTERN$"))) {
addError(numLine,"Semantical Error: EXTERN directive inside a macro body is forbidden");
}
if (curLine.find(" BEGIN") != string::npos && regex_search(curLine,auxMatch,regex(" BEGIN$"))) {
addError(numLine,"Semantical Error: BEGIN directive inside a macro body is forbidden");
}
if (regex_search(curLine,auxMatch,regex("^\\bEND[\\b]?$"))) {
addError(numLine,"Semantical Error: END directive inside a macro body is forbidden");
}
if (curLine.find("PUBLIC ") != string::npos && regex_search(curLine,auxMatch,regex("^PUBLIC "))) {
addError(numLine,"Semantical Error: PUBLIC directive inside a macro body is forbidden");
}
if (curLine.find(':') != string::npos) {
addError(numLine,"Semantical Error: MACRO does not support labels inside");
continue;
}
if (regex_search(curLine,auxMatch,regex("\\bENDMACRO$"))) {
isMacroBody = false;
break;
}
if (curLine.find("SECTION ")!=string::npos && regex_search(curLine,auxMatch,regex("^SECTION "))) {
addError(numLine,"Semantical Error: SECTION directive inside a macro body is forbidden");
}
else {
for (unsigned i=0;i<macroParams.size();i++) {
string paramRegex = macroParams[i];
string paramSub = string("#") + to_string(i+1);
curLine = regex_replace(curLine,regex(paramRegex),paramSub);
}
if(regex_search(curLine,auxMatch,regex("\\&[\\w]"))) {
addError(numLine,"Semantical Error: MACRO has less parameters than it uses");
}
macroLines.push_back(curLine);
}
codeIterator++;
numLine++;
}
numLine++;
MDT.emplace(macroLabel,macroLines);
}
}
//else if (regex_search(curLine,auxMatch,regex("\\bENDMACRO\\b"))) {
// canWrite = false;
//}
else if (hasMacro(curLine)!=";") {
string macroFound = hasMacro(curLine);
//cout << "Found Macro \"" << macroFound << "\"at line " << numLine << endl;
map<string,std::pair<int,int> >::iterator MNTCell = MNT.find(macroFound);
map<string,vector<string> >::iterator MDTCell = MDT.find(macroFound);
std::pair<int,int> MNTDefs = MNTCell->second;
vector<string> auxiliarLines = MDTCell->second;
vector<string> paramsToSub;
try {
string finalLine = curLine.substr(curLine.find(macroFound)+macroFound.length()+1);
if (curLine.find(':') != string::npos) {
string curLabel = curLine.substr(0,curLine.find(':')+1);
outputFile << curLabel << " ";
}
splitTokenize(finalLine,paramsToSub,",");
if (paramsToSub.size() != static_cast<unsigned>(MNTDefs.second)) {
addError(numLine,"Syntactical Error: Wrong number of parameters for current MACRO");
}
else {
for (const string &subLines : auxiliarLines) {
string auxSubLine = subLines;
for (int j=0;j<MNTDefs.second;j++) {
string MDTParamRegex = string("#") + to_string(j+1);
auxSubLine = regex_replace(auxSubLine,regex(MDTParamRegex),paramsToSub[j]);
}
outputFile << auxSubLine << "\n";
}
}
}
catch (std::out_of_range &e) {
//cout << "Macro without params: " << e.what() << endl;
if (curLine.find(':') != string::npos) {
string curLabel = curLine.substr(0,curLine.find(':')+1);
outputFile << curLabel << " ";
}
for (const string &subLines : auxiliarLines) {
outputFile << subLines << "\n";
}
}
}
else {
if (!canWrite) {
canWrite = true;
}
else {
if (curLine.size()) {
outputFile << curLine << "\n";
}
}
}
}
//cout << "MNT: " << endl;
//cout << MNT << endl;
//cout << "MDT: " << endl;
//cout << MDT << endl;
if (this->errorList.size()) {
printErrors();
outputFile.close();
remove(fileOut.c_str());
}
return this->errorList.size();
}
void MacroExtender::printErrors() const {
cout << "Macro-extending not successful due to following error(s):" << endl;
cout << errorList << endl;
cout << "Macro-Extended File not generated.\n" << endl;
}
string MacroExtender::hasMacro(string line) {
string consideredLine;
if (regex_match(line,regex(labelReg))) {
return ";";
}
size_t labelPos = line.find_first_of(':');
if (labelPos != string::npos && labelPos+1<line.length()) {
consideredLine = line.substr(labelPos+2);
}
else {
consideredLine = line;
}
for (const auto &i : MNT) {
string auxFinder = string(" ") + i.first + string(" ");
//cout << "Testing line [" << consideredLine << "] to [" << i.first << "]" << endl;
if (consideredLine.find(auxFinder)!=string::npos) {
return string(i.first);
}
auxFinder = string(" ") + i.first;
size_t auxPos = consideredLine.find(auxFinder);
size_t sumLen = (i.first).length()+1;
if (auxPos!=string::npos && (auxPos+sumLen+1)<consideredLine.length()) {
return string(i.first);
}
auxFinder = i.first + string(" ");
auxPos = consideredLine.find(auxFinder);
if (auxPos==0) {
return string(i.first);
}
auxFinder = string("\\b") + i.first + string("\\b");
if (regex_match(consideredLine,regex(auxFinder))) {
return string(i.first);
}
}
return string(";");
}
bool MacroExtender::hasDuplicates(const vector<string> listParams) {
bool result = false;
vector<string> auxVector = listParams;
sort(auxVector.begin(),auxVector.end());
const string empty = {""};
const string *ptr = ∅
for (const string &i : auxVector) {
if (*ptr == i) {
result = true;
break;
}
ptr = &i;
}
return result;
}
ostream &operator<<(ostream &output,const map<string,std::pair<int,int> > &MNTCell) {
for (const auto &i : MNTCell) {
output << "(" << i.first << ")" << i.second << endl;
}
return output;
}
ostream &operator<<(ostream &output,const std::pair<int,int> &twoVals) {
output << "<" << twoVals.first << "," << twoVals.second << ">";
return output;
}
ostream &operator<<(ostream &output,const vector<string> &arrayString) {
for (const auto &i : arrayString) {
output << "@\t" << i << endl;
}
return output;
}
ostream &operator<<(ostream &output,const map<string,vector<string> > &MDTCell) {
for (const auto &i : MDTCell) {
output << "$[" << i.first << "]\n" << i.second << endl;
}
return output;
}