/
Preprocessor.cpp
180 lines (166 loc) · 7.76 KB
/
Preprocessor.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
#include "Preprocessor.h"
Preprocessor::Preprocessor() {
this->includeDirectory = "";
}
Preprocessor::Preprocessor(string includeDirectory) {
this->includeDirectory = includeDirectory;
}
int Preprocessor::setIncludeDirectory(string includeDirectory) {
this->includeDirectory = includeDirectory;
return 0;
}
string Preprocessor::preprocess(string file_name) {
string resultCode = fileToString(file_name);
Preprocessor preprocessor = Preprocessor();
resultCode = preprocessor.deleteComments(resultCode);
resultCode = preprocessor.applyDefines(resultCode);
resultCode = preprocessor.applyIncludes(resultCode, "", includeDirectory);
return resultCode;
}
string Preprocessor::preprocess(string file_name, string filePath, string includeDirectory) {
this->includeDirectory = includeDirectory;
println(this->includeDirectory);
string resultCode = fileToString(file_name);
Preprocessor preprocessor = Preprocessor();
resultCode = preprocessor.deleteComments(resultCode);
resultCode = preprocessor.applyDefines(resultCode);
resultCode = preprocessor.applyIncludes(resultCode, filePath, includeDirectory);
return resultCode;
}
string Preprocessor::applyDefines(string code) {
const string definePointer = "#define";
string resultCode(code);
string::size_type definePositionBegin = code.find(definePointer, 0);
string::size_type definePosition;
while (definePositionBegin != code.npos) {
definePosition = definePositionBegin + definePointer.length();
while (code[definePosition] == ' ') {
definePosition++;
}
if (!this->inBrackets(code, definePositionBegin)) {
string::size_type macroEndPosition = code.find(' ', definePosition);
string macroValue = code.substr(definePosition, macroEndPosition - definePosition);
string::size_type constantEndPosition = code.find(' ', macroEndPosition + 1);
if (code.find('\n', macroEndPosition + 1) < constantEndPosition) {
constantEndPosition = code.find('\n', macroEndPosition + 1);
}
string constantValue = code.substr(macroEndPosition + 1, constantEndPosition - macroEndPosition - 1);
string::size_type macroPosition = resultCode.find(macroValue);
while (macroPosition != resultCode.npos) {
resultCode.replace(macroPosition, macroValue.length(), constantValue);
macroPosition = resultCode.find(macroValue, macroPosition);
}
}
definePositionBegin = code.find(definePointer, definePosition);
}
definePositionBegin = resultCode.find(definePointer, 0);
while (definePositionBegin != resultCode.npos) {
definePosition = definePositionBegin + definePointer.length();
while (code[definePosition] == ' ') {
definePosition++;
}
if (!this->inBrackets(code, definePositionBegin)) {
string::size_type definePositionEnd = resultCode.find('\n', definePositionBegin);
resultCode.replace(definePositionBegin, definePositionEnd - definePositionBegin, "");
}
definePositionBegin = resultCode.find(definePointer, definePositionBegin);
}
return resultCode;
}
string Preprocessor::applyIncludes(string code, string filePath, string specialDirectory) {
const string includePointer = "#include";
const string format = ".cmm";
string resultCode(code);
string::size_type includePositionBegin = code.find(includePointer, 0);
string::size_type includePosition;
while (includePositionBegin != resultCode.npos) {
includePosition = resultCode.find(' ', includePositionBegin) + 1;
if (!this->inBrackets(resultCode, includePositionBegin)) {
if (resultCode[includePosition] == '<') {
includePosition++;
string::size_type fileNameEndPosition = resultCode.find('>', includePosition);
string fileName = resultCode.substr(includePosition, fileNameEndPosition - includePosition);
string fileSource = this->preprocess(specialDirectory + fileName + format);
if (fileSource == "") {
println("No file in directory:" + fileName + " in " + specialDirectory);
}
string::size_type replaceSizeCount = fileNameEndPosition - includePositionBegin + 1;
resultCode.replace(includePositionBegin, replaceSizeCount, fileSource);
} else if (resultCode[includePosition] == '"'){
includePosition++;
string::size_type fileNameEndPosition = resultCode.find('"', includePosition);
string fileName = resultCode.substr(includePosition, fileNameEndPosition - includePosition);
string fileSource = this->preprocess(filePath + fileName, filePath + fileName.substr(0, fileName.find_last_of('/')), specialDirectory);
if (fileSource == "") {
println("No file in directory:" + fileName + " in " + specialDirectory);
}
string::size_type replaceSizeCount = fileNameEndPosition - includePositionBegin + 1;
resultCode.replace(includePositionBegin, replaceSizeCount, fileSource);
}
}
includePositionBegin = resultCode.find(includePointer, includePosition);
}
return resultCode;
}
bool Preprocessor::inBrackets(string code, int position) {
int bracketBalance = 0, lineBeginnig = position;
std::vector<int> bracketPosition;
while (lineBeginnig > -1) {
if (code[lineBeginnig] == '"') {
bracketPosition.push_back(lineBeginnig);
}
if (code[lineBeginnig] == '\n') {
break;
}
lineBeginnig--;
}
string block = "'";
block += '"';
block += "'";
int blockPosition = code.find(block, lineBeginnig);
for (string::size_type i = 0; i < bracketPosition.size(); i++) {
if (blockPosition > bracketPosition[i]) {
bracketBalance = 1 - bracketBalance;
} else if (bracketBalance == 1) {
bracketBalance = 1 - bracketBalance;
blockPosition = code.find(block, lineBeginnig);
}
}
return (bracketBalance == 1);
}
string Preprocessor::deleteComments(string code) {
const string lineCommentPointer = "//";
const string commentBeginPointer = "/*";
const string commentEndPointer = "*/";
string resultCode (code);
string::size_type commentPosition = resultCode.find(lineCommentPointer, 0);
string::size_type commentEndPosition;
while (commentPosition != string::npos) {
commentEndPosition = resultCode.find('\n', commentPosition);
if (commentEndPosition == resultCode.npos) {
commentEndPosition = resultCode.length();
}
if (!inBrackets(resultCode, commentPosition)) {
resultCode.replace(commentPosition, commentEndPosition - commentPosition, "");
} else {
commentPosition += lineCommentPointer.length();
}
commentPosition = resultCode.find(lineCommentPointer, commentPosition);
}
commentPosition = resultCode.find(commentBeginPointer, 0);
while (commentPosition != string::npos) {
commentEndPosition = resultCode.find(commentEndPointer, commentPosition);
if (commentEndPosition == resultCode.npos) {
commentEndPosition = resultCode.length();
} else {
commentEndPosition += commentEndPointer.length();
}
if (!inBrackets(resultCode, commentPosition)) {
resultCode.replace(commentPosition, commentEndPosition - commentPosition, "");
} else {
commentPosition += commentBeginPointer.length();
}
commentPosition = resultCode.find(commentBeginPointer, commentPosition);
}
return resultCode;
}