Skip to content

Commit e97d00d

Browse files
#TDP - 19 Refactoring
Small refactor Create method skip comment add check identifier and numbers
1 parent 1b1fc7b commit e97d00d

File tree

9 files changed

+147
-64
lines changed

9 files changed

+147
-64
lines changed

ReversePolishNotation.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -616,7 +616,7 @@ void ReversePolishNotation::reversePolishNotationAnalyze(std::string fileName_le
616616
{
617617
std::cout << " Exception opening/reading file";
618618
std::cout << exep.what();
619-
//System::Windows::Forms::MessageBox((std::string)exep.what());
619+
System::Windows::Forms::MessageBox::Show("File don't open", "error", System::Windows::Forms::MessageBoxButtons::OK, System::Windows::Forms::MessageBoxIcon::Error);
620620
}
621621

622622
lexical.close();

ReversePolishNotation.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class ReversePolishNotation : public Translator
2626
bool isBeginFunctionName(std::string word);
2727
bool isExistsFunctionExpression(std::vector<std::map<std::string, int>>& stack);
2828
bool isEmptyArray(std::string line);
29+
2930
};
3031

3132
#endif

SyntaxAnalisator.cpp

Lines changed: 99 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include "Translator_LanguageC.h"
12
#include "SyntaxAnalisator.h"
23
#include "function.h"
34

@@ -9,6 +10,14 @@ SyntaxAnalisator::SyntaxAnalisator()
910
SyntaxAnalisator::~SyntaxAnalisator()
1011
{
1112
}
13+
14+
System::String^ StlWStringToString(std::string const& os)
15+
{
16+
System::String^ str = gcnew System::String(os.c_str());
17+
//String^ str = gcnew String("");
18+
return str;
19+
}
20+
1221
std::string SyntaxAnalisator::getServiceWordCode(std::string str)
1322
{
1423
for (int i = 0; i < SIZE_serviceWord; i++)
@@ -57,6 +66,7 @@ std::string SyntaxAnalisator::getSymbolsConstCode(std::string str)
5766
return "\0";
5867
}
5968

69+
6070
void SyntaxAnalisator::addCode(std::string str, std::map<std::string, std::string> & table, int numTable)
6171
{
6272
int indexCode = 0;
@@ -66,9 +76,28 @@ void SyntaxAnalisator::addCode(std::string str, std::map<std::string, std::strin
6676
}
6777
indexCode++;
6878
if (numTable == 1)
69-
table.insert(std::pair<std::string, std::string>(str, "I" + std::to_string(indexCode)));
79+
{
80+
if(isIdentifier(str)==true)
81+
table.insert(std::pair<std::string, std::string>(str, "I" + std::to_string(indexCode)));
82+
else
83+
{
84+
85+
System::String^ temp = StlWStringToString(str);
86+
System::Windows::Forms::MessageBox::Show("Error with identifier", temp, System::Windows::Forms::MessageBoxButtons::OK, System::Windows::Forms::MessageBoxIcon::Error);
87+
return;
88+
}
89+
}
7090
if (numTable == 2)
71-
table.insert(std::pair<std::string, std::string>(str, "N" + std::to_string(indexCode)));
91+
{
92+
if(isNumber(str)==true)
93+
table.insert(std::pair<std::string, std::string>(str, "N" + std::to_string(indexCode)));
94+
else
95+
{
96+
System::String^ temp = StlWStringToString(str);
97+
System::Windows::Forms::MessageBox::Show("Error with number", temp, System::Windows::Forms::MessageBoxButtons::OK, System::Windows::Forms::MessageBoxIcon::Error);
98+
return;
99+
}
100+
}
72101
if (numTable == 3)
73102
table.insert(std::pair<std::string, std::string>(str, "C" + std::to_string(indexCode)));
74103
}
@@ -149,6 +178,35 @@ std::string SyntaxAnalisator::getCodeWord(std::string word)
149178
return getCodeWordLengthGreaterOne(word);
150179
}
151180

181+
bool SyntaxAnalisator::skipAnalyzeOneLineComment(bool readComment, std::string line, __int64 index,std::ofstream& file)
182+
{
183+
if (readComment == false && isOneStringComment((int)line[index], (int)line[index + 1]) == true)
184+
{
185+
std::string oneLineComment = "";
186+
oneLineComment.assign(line, index, line.length() - index);
187+
file << oneLineComment << " ";
188+
return true;
189+
}
190+
return false;
191+
}
192+
193+
bool skipAnalyzeComment(bool& readComment, std::string line, __int64& index, std::ofstream& file, std::string& word)
194+
{
195+
if (readComment == true && isComment((int)line[index + 1], (int)line[index]) == true)
196+
{
197+
readComment = false;
198+
word += line[index];
199+
word += line[index + 1];
200+
if (word != "\0" && word != "")
201+
file << word << " ";
202+
word = "";
203+
index++;
204+
return true;
205+
}
206+
return false;
207+
}
208+
209+
152210
void SyntaxAnalisator::analyze(std::string filePathOrName_C, std::string fileName_Path_SaveAnalis)
153211
{
154212
std::ifstream fileC;
@@ -161,45 +219,32 @@ void SyntaxAnalisator::analyze(std::string filePathOrName_C, std::string fileNam
161219
if (fileC.is_open())
162220
{
163221
bool readComment = false;
164-
std::string temp = "";
222+
std::string word = "";
165223
while (!fileC.eof())
166224
{
167225
std::string stringLanguageC = "";
168226
getline(fileC, stringLanguageC);
169-
for (unsigned int i = 0; i < stringLanguageC.length(); i++)
227+
for (__int64 i = 0; i < stringLanguageC.length(); i++)
170228
{
171229
if (isServiceSymbols((int)stringLanguageC[i]) == true)
172230
continue;
173231
if (isComment((int)stringLanguageC[i], (int)stringLanguageC[i + 1]) == true)
174232
readComment = true;
175-
if (readComment == false && isOneStringComment((int)stringLanguageC[i], (int)stringLanguageC[i + 1]) == true)
176-
{
177-
std::string oneLineComment = "";
178-
oneLineComment.assign(stringLanguageC, i, stringLanguageC.length() - i);
179-
fileAnalysis << oneLineComment << " ";
233+
if (skipAnalyzeOneLineComment(readComment,stringLanguageC,i,fileAnalysis)==true)
180234
break;
181-
}
182-
if (readComment == true && isComment((int)stringLanguageC[i + 1], (int)stringLanguageC[i]) == true)
183-
{
184-
readComment = false;
185-
temp += stringLanguageC[i];
186-
temp += stringLanguageC[i + 1];
187-
if (temp != "\0" && temp != "/**/")
188-
fileAnalysis << temp << " ";
189-
temp = "";
190-
i++;
235+
236+
if (skipAnalyzeComment(readComment, stringLanguageC, i, fileAnalysis,word))
191237
continue;
192-
}
193238

194239
if (readComment == false)
195240
{
196-
if (isSeparators((int)stringLanguageC[i]) == true && temp[0] != '\"')
241+
if (isSeparators((int)stringLanguageC[i]) == true && word[0] != '\"')
197242
{
198-
if (temp.length() != 0)
199-
fileAnalysis << getCodeWord(temp) << " ";
200-
temp = stringLanguageC[i];
201-
fileAnalysis << getCodeWord(temp) << " ";
202-
temp = "";
243+
if (word.length() != 0)
244+
fileAnalysis << getCodeWord(word) << " ";
245+
word = stringLanguageC[i];
246+
fileAnalysis << getCodeWord(word) << " ";
247+
word = "";
203248
continue;
204249
}
205250

@@ -216,26 +261,26 @@ void SyntaxAnalisator::analyze(std::string filePathOrName_C, std::string fileNam
216261
if (posClose != -1)
217262
{
218263
countSymbols = posClose + 1 - i;
219-
temp.assign(stringLanguageC, i, countSymbols);
220-
if (temp.find(".h") != -1)
264+
word.assign(stringLanguageC, i, countSymbols);
265+
if (word.find(".h") != -1)
221266
{
222-
fileAnalysis << getCodeWord(temp) << " ";
223-
temp = "";
224-
if (stringLanguageC[posClose + 1] == '\0')
267+
fileAnalysis << getCodeWord(word) << " ";
268+
word = "";
269+
if (stringLanguageC[static_cast<__int64>(posClose) + 1] == '\0')
225270
break;
226271
else
227272
i = posClose;
228273
}
229274
else
230275
{
231-
if (temp[0] == '\"')
276+
if (word[0] == '\"')
232277
{
233-
fileAnalysis << getCodeWord(temp) << " ";
234-
i = posClose + 1;
278+
fileAnalysis << getCodeWord(word) << " ";
279+
i = static_cast<__int64>(posClose) + 1;
235280

236281
}
237282
}
238-
temp = "";
283+
word = "";
239284
}
240285
}
241286

@@ -245,64 +290,63 @@ void SyntaxAnalisator::analyze(std::string filePathOrName_C, std::string fileNam
245290
isDoubleOperation((int)stringLanguageC[i], (int)stringLanguageC[i + 1] == true) ||
246291
isLogicalDoubleOperation((int)stringLanguageC[i], (int)stringLanguageC[i + 1]) == true)
247292
{
248-
temp += stringLanguageC[i];
293+
word += stringLanguageC[i];
249294
i++;
250295
}
251-
temp += stringLanguageC[i];
252-
fileAnalysis << getCodeWord(temp) << " ";
253-
temp = "";
296+
word += stringLanguageC[i];
297+
fileAnalysis << getCodeWord(word) << " ";
298+
word = "";
254299
continue;
255300
}
256301

257302
if (stringLanguageC[i] != ' ')
258303
{
259304
if (isLetter((int)stringLanguageC[i]) == true && (isLetter((int)stringLanguageC[i + 1]) == false && isDigit((int)stringLanguageC[i + 1]) == false))
260305
{
261-
262-
temp += stringLanguageC[i];
263-
if ((temp == "int" || temp == "float" || temp == "double" || temp == "char") && stringLanguageC[i + 1] == '*')
306+
word += stringLanguageC[i];
307+
if (isType(word) && stringLanguageC[i + 1] == '*')
264308
{
265-
temp += stringLanguageC[i + 1];
309+
word += stringLanguageC[i + 1];
266310
i++;
267311
}
268-
fileAnalysis << getCodeWord(temp) << " ";
269-
temp = "";
312+
fileAnalysis << getCodeWord(word) << " ";
313+
word = "";
270314
continue;
271315
}
272316
else
273317
{
274318
if (stringLanguageC[i] == '#')
275319
{
276-
temp += stringLanguageC[i];
320+
word += stringLanguageC[i];
277321
continue;
278322
}
279323

280324
}
281-
temp += stringLanguageC[i];
325+
word += stringLanguageC[i];
282326
}
283327
else
284328
{
285-
if (temp == "\0")
329+
if (word == "\0")
286330
continue;
287331
else
288332
{
289-
fileAnalysis << getCodeWord(temp) << " ";
290-
temp = "";
333+
fileAnalysis << getCodeWord(word) << " ";
334+
word = "";
291335
}
292336
}
293337
}
294338
else
295339
{
296-
temp += stringLanguageC[i];
340+
word += stringLanguageC[i];
297341
}
298342

299343
}
300-
if (temp != "\0")
344+
if (word != "\0")
301345
{
302346
if (readComment == false)
303-
fileAnalysis << getCodeWord(temp);
347+
fileAnalysis << getCodeWord(word);
304348
else
305-
temp += '\n';
349+
word += '\n';
306350
}
307351
if (readComment == false)
308352
fileAnalysis << "\n";
@@ -314,6 +358,7 @@ void SyntaxAnalisator::analyze(std::string filePathOrName_C, std::string fileNam
314358
{
315359
std::cout << " Exception opening/reading file";
316360
std::cout << exep.what();
361+
System::Windows::Forms::MessageBox::Show("File don't open", "error", System::Windows::Forms::MessageBoxButtons::OK, System::Windows::Forms::MessageBoxIcon::Error);
317362
}
318363

319364
fileC.close();

SyntaxAnalisator.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,7 @@ class SyntaxAnalisator :
2222
std::string getCodeWordLength_1(std::string word);
2323
std::string getCodeWordLengthGreaterOne(std::string word);
2424
std::string getCodeWord(std::string word);
25+
//bool skipAnalyzeComment(bool& readComment, std::string line, __int64& index, std::ofstream& file, std::string& word);
26+
bool skipAnalyzeOneLineComment(bool readComment, std::string line, __int64 index, std::ofstream& file);
2527
};
2628
#endif

Translator_LanguageC.cpp

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ void marshalString(String^ s, std::string& std_string)
2525
std_string = chars;
2626
Marshal::FreeHGlobal(IntPtr((void*)chars));
2727
}
28+
2829
System::Void MethodsDevelopmentTranslator::Translator_LanguageC::Btn_loadFile_Click(System::Object^ sender, System::EventArgs^ e)
2930
{
3031
String^ filePathName = "";
@@ -70,17 +71,27 @@ System::Void MethodsDevelopmentTranslator::Translator_LanguageC::Btn_analisator_
7071
{
7172
MessageBox::Show(this, "Write to file name! Or error to extension file, (.txt uses)", "error", MessageBoxButtons::OK, MessageBoxIcon::Error);
7273
}
73-
7474
}
7575

7676
System::Void MethodsDevelopmentTranslator::Translator_LanguageC::Btn_reversePolishNotation_Click(System::Object^ sender, System::EventArgs^ e)
7777
{
78-
ReversePolishNotation RPN;
79-
std::string file = "";
80-
marshalString(tb_nameFileAnylize->Text, file);
81-
RPN.reversePolishNotationAnalyze(file, "RPN.txt");
78+
if (tb_nameFileAnylize->Text != "" && isExtensionTXT(tb_nameFileAnylize->Text) == true)
79+
{
80+
ReversePolishNotation RPN;
81+
std::string file = "";
82+
marshalString(tb_nameFileAnylize->Text, file);
83+
RPN.reversePolishNotationAnalyze(file, "RPN.txt");
8284

83-
StreamReader^ fileAnalyze = gcnew StreamReader("RPN.txt", System::Text::Encoding::GetEncoding(1251));//File::OpenText("RPN.txt");
84-
tb_reversePolishNotation->Text = fileAnalyze->ReadToEnd();
85-
fileAnalyze->Close();
85+
StreamReader^ fileAnalyze = gcnew StreamReader("RPN.txt", System::Text::Encoding::GetEncoding(1251));
86+
tb_reversePolishNotation->Text = fileAnalyze->ReadToEnd();
87+
fileAnalyze->Close();
88+
btn_analisator->Enabled = false;
89+
}
90+
else
91+
{
92+
MessageBox::Show(this, "Write to file name! Or error to extension file, (.txt uses)", "error", MessageBoxButtons::OK, MessageBoxIcon::Error);
93+
}
94+
95+
96+
8697
}

Translator_LanguageC.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,5 +175,6 @@ namespace MethodsDevelopmentTranslator {
175175
private: System::Void Btn_loadFile_Click(System::Object^ sender, System::EventArgs^ e);
176176
System::Void Btn_analisator_Click(System::Object^ sender, System::EventArgs^ e);
177177
System::Void Btn_reversePolishNotation_Click(System::Object^ sender, System::EventArgs^ e);
178+
178179
};
179180
}

function.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,34 @@ bool isDigit(int const &elem)
6565

6666
bool isNumber(std::string const &num)
6767
{
68+
if (std::count(num.begin(), num.end(), '.') > 1)
69+
return false;
6870
for (unsigned int i = 0; i < num.length(); i++)
71+
{
72+
if (num[i] == '.')
73+
continue;
6974
if (isDigit((int)num[i]) == false)
7075
return false;
76+
}
7177
return true;
7278
}
7379

7480
bool isLibrary_header(std::string const &word)
7581
{
7682
return (int)word[0] == 34 && (int)word[word.length() - 1] == 34 && (int)word[word.length() - 2] == 104 && (int)word[word.length() - 3] == 46 ? true : false;
7783
}
84+
85+
bool isType(std::string const& word)
86+
{
87+
return word == "int" || word == "float" || word == "double" || word == "char";
88+
}
89+
90+
bool isIdentifier(std::string const& word)
91+
{
92+
if (word.find('%') != std::string::npos || word.find('-') != std::string::npos || word.find('*') != std::string::npos
93+
|| word.find('/') != std::string::npos || word.find('\\') != std::string::npos || word.find('[') != std::string::npos
94+
|| word.find(']') != std::string::npos || word.find('(') != std::string::npos || word.find(')') != std::string::npos
95+
|| word.find('{') != std::string::npos || word.find('}') != std::string::npos)
96+
return false;
97+
return true;
98+
}

function.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,6 @@ bool isDigit(int const &);
1616
bool isLetter(int const &);
1717
bool isLibrary_header(std::string const &);
1818
bool isNumber(std::string const& num);
19+
bool isType(std::string const& word);
20+
bool isIdentifier(std::string const& word);
1921
#endif

0 commit comments

Comments
 (0)