@@ -11,12 +11,9 @@ ReversePolishNotation::~ReversePolishNotation()
1111
1212int ReversePolishNotation::getPriority (std::string word)
1313{
14- // priority
1514 for (auto & item : priority)
16- {
1715 if (item.first == word)
1816 return item.second ;
19- }
2017 return -1 ;
2118}
2219
@@ -28,7 +25,7 @@ bool ReversePolishNotation::isFunctionDeclaration(std::string line)
2825 {
2926 std::string token = line.substr (0 , pos);
3027 line.erase (0 , pos + 1 );
31- if ((token == " W1 " || token == " W2 " || token == " W3 " || token == " W14 " ) && count==0 ) // INT FLOAT CHAR DOUBLE
28+ if (isTypeDeclarationByCode (token) && count==0 ) // INT FLOAT CHAR DOUBLE
3229 count++;
3330 else
3431 {
@@ -90,6 +87,52 @@ size_t ReversePolishNotation::positionTypeConversion(std::string line)
9087 return -1 ;
9188}
9289
90+ void ReversePolishNotation::recordFileFromStack (std::ofstream & file, std::map<std::string, int >::reverse_iterator upElemStack)
91+ {
92+ if (isTypeDeclarationByCode (upElemStack->first ) == true )
93+ file << upElemStack->second << " " << nameType (upElemStack->first ) << " " ;
94+ else
95+ file << upElemStack->first << " " ;
96+
97+ };
98+
99+ bool isOpenCurlyBracket (std::string token)
100+ {
101+ return token == " R5" ;
102+ }
103+ bool isCloseCurlyBracket (std::string token)
104+ {
105+ return token == " R6" ;
106+ }
107+ bool isOpenBracket (std::string token)
108+ {
109+ return token == " R3" ;
110+ }
111+ bool isCloseBracket (std::string token)
112+ {
113+ return token == " R4" ;
114+ }
115+ bool isOpenSquareBracket (std::string token)
116+ {
117+ return token == " R1" ;
118+ }
119+ bool isClosesquareBracket (std::string token)
120+ {
121+ return token == " R2" ;
122+ }
123+ bool isComma (std::string token)
124+ {
125+ return token == " R8" ;
126+ }
127+ bool isSemicolon (std::string token)
128+ {
129+ return token == " R7" ;
130+ }
131+ void recordFileEndCondition (std::ofstream& file, std::string nameCondition, int number)
132+ {
133+ file << std::endl << nameCondition << number << " :" << " " ;
134+ }
135+
93136void ReversePolishNotation::reversePolishNotationAnalyze (std::string fileName_lexical, std::string fileName_RPN)
94137{
95138 std::ifstream lexical;
@@ -122,17 +165,10 @@ void ReversePolishNotation::reversePolishNotationAnalyze(std::string fileName_le
122165 }
123166 if (manyLineComment == true )
124167 {
125- if (lineLexical.find (" */" ) == std::string::npos)
126- {
127- fileAnalysis << lineLexical << std::endl;
128- continue ;
129- }
130- else
131- {
132- fileAnalysis << lineLexical << std::endl;
168+ fileAnalysis << lineLexical << std::endl;
169+ if (lineLexical.find (" */" ) != std::string::npos)
133170 manyLineComment = false ;
134- continue ;
135- }
171+ continue ;
136172 }
137173 size_t pos = 0 ;
138174 std::string token=" " , nameFunctionInExpression=" " ;
@@ -157,15 +193,15 @@ void ReversePolishNotation::reversePolishNotationAnalyze(std::string fileName_le
157193 else
158194 {
159195 token = lineLexical.substr (0 , pos);
160- if (token == " R1 " )
196+ if (isOpenSquareBracket ( token) )
161197 isEmptyArrayCurrent = isEmptyArray (lineLexical);
162198 lineLexical.erase (0 , pos + 1 );
163199 }
164200
165201 }
166202 // TODO one line comment in expression
167203
168- if (token != " R5 " )
204+ if (isOpenCurlyBracket ( token)== false )
169205 {
170206 if (stack.size () != 0 && lastCommndIFFORWHILE_withoutTHEN != " " )
171207 {
@@ -205,7 +241,7 @@ void ReversePolishNotation::reversePolishNotationAnalyze(std::string fileName_le
205241 {
206242 if (stack.size () == 0 )
207243 {
208- if (token == " R7 " )
244+ if (isSemicolon ( token) )
209245 continue ;
210246 std::map<std::string, int > tempMap;
211247 if (isReadFunctionInExpresiion == false )
@@ -245,7 +281,7 @@ void ReversePolishNotation::reversePolishNotationAnalyze(std::string fileName_le
245281 continue ;
246282 }
247283
248- if (token== " R5 " ) // {
284+ if (isOpenCurlyBracket ( token) ) // {
249285 {
250286 auto upElemStack = stack.back ().rbegin ();
251287 if (upElemStack->first == " PROC" )
@@ -275,7 +311,7 @@ void ReversePolishNotation::reversePolishNotationAnalyze(std::string fileName_le
275311 continue ;
276312 }
277313 }
278- if (token== " R6 " ) // }
314+ if (isCloseCurlyBracket ( token) ) // }
279315 {
280316 auto upElemStack = stack.back ().rbegin ();
281317 if (isIFCondition (upElemStack->first )==true )
@@ -292,17 +328,14 @@ void ReversePolishNotation::reversePolishNotationAnalyze(std::string fileName_le
292328 stack.pop_back ();
293329 continue ;
294330 }
295- if (token == " R7 " )// ;
331+ if (isSemicolon ( token) )// ;
296332 {
297- auto upElemStack = stack.back ().rbegin ();
333+ std::map<std::string, int >::reverse_iterator upElemStack = stack.back ().rbegin ();
298334 while (stack.size () != 0 && upElemNotNameExpression (upElemStack->first )==true )
299335 {
300- if (isFor == true && upElemStack->first == " R3 " )
336+ if (isFor == true && isOpenBracket ( upElemStack->first ) )
301337 break ;
302- if (isTypeDeclarationByCode (upElemStack->first ) == true )
303- fileAnalysis << upElemStack->second << " " << nameType (upElemStack->first ) << " " ;
304- else
305- fileAnalysis << upElemStack->first << " " ;
338+ recordFileFromStack (fileAnalysis, upElemStack);
306339
307340 stack.pop_back ();
308341 if (stack.size () != 0 )
@@ -318,32 +351,24 @@ void ReversePolishNotation::reversePolishNotationAnalyze(std::string fileName_le
318351 {
319352
320353 if (isIFCondition (stack.back ().rbegin ()->first ) == true && lastCommndIFFORWHILE_withoutTHEN == " End IF" )
321- {
322- fileAnalysis << std::endl << " Ì" << stack.back ().rbegin ()->second << " :" << " " ;
323- stack.pop_back ();
324- }
354+ recordFileEndCondition (fileAnalysis, " Ì" , stack.back ().rbegin ()->second );
355+
325356 if (isCycle (stack.back ().rbegin ()->first ) == true && lastCommndIFFORWHILE_withoutTHEN == " End Cycle" )
326- {
327- fileAnalysis << std::endl << " ÌÖ" << stack.back ().rbegin ()->second << " :" << " " ;
328- stack.pop_back ();
329- }
357+ recordFileEndCondition (fileAnalysis, " ÌÖ" , stack.back ().rbegin ()->second );
358+
359+ stack.pop_back ();
330360 lastCommndIFFORWHILE_withoutTHEN = " " ;
331-
332361 }
333362 continue ;
334363 }
335- if (token == " R3 " )// (
364+ if (isOpenBracket ( token) )// (
336365 {
337366 if (isDeclareFunction == true )
338367 {
339368 while (stack.size () != 0 )
340369 {
341- auto upElemStack = stack.back ().rbegin ();
342- if (isTypeDeclarationByCode (upElemStack->first ) == true )
343- fileAnalysis << upElemStack->second << " " << nameType (upElemStack->first ) << " " ;
344- else
345- fileAnalysis << upElemStack->first << " " ;
346-
370+ std::map<std::string, int >::reverse_iterator upElemStack = stack.back ().rbegin ();
371+ recordFileFromStack (fileAnalysis, upElemStack);
347372 stack.pop_back ();
348373 }
349374 }
@@ -357,23 +382,20 @@ void ReversePolishNotation::reversePolishNotationAnalyze(std::string fileName_le
357382 stack.push_back (tempMap);
358383 continue ;
359384 }
360- if (token == " R4 " )// )
385+ if (isCloseBracket ( token) )// )
361386 {
362387 std::string end = " R3" ;
363388 if (isReadFunctionInExpresiion == true )
364389 end = " Ô" ;
365390 while (stack.size () != 0 && stack.back ().rbegin ()->first != end)
366391 {
367392
368- auto upElemStack = stack.back ().rbegin ();
369- if (isTypeDeclarationByCode (upElemStack->first )==true )
370- fileAnalysis << upElemStack->second << " " << nameType (upElemStack->first ) << " " ;
371- else
372- fileAnalysis << upElemStack->first << " " ;
393+ std::map<std::string, int >::reverse_iterator upElemStack = stack.back ().rbegin ();
394+ recordFileFromStack (fileAnalysis, upElemStack);
373395 stack.pop_back ();
374396 }
375397 if (stack.size ()!=0 )
376- if (stack.back ().rbegin ()->first == " R3 " )
398+ if (isOpenBracket ( stack.back ().rbegin ()->first ) )
377399 stack.pop_back ();
378400 if (isReadFunctionInExpresiion == true && stack.size () != 0 )
379401 {
@@ -388,12 +410,8 @@ void ReversePolishNotation::reversePolishNotationAnalyze(std::string fileName_le
388410 {
389411 while (stack.size () != 0 )
390412 {
391- auto upElemStack = stack.back ().rbegin ();
392- if (isTypeDeclarationByCode (upElemStack->first )==true )
393- fileAnalysis << upElemStack->second << " " << nameType (upElemStack->first ) << " " ;
394- else
395- fileAnalysis << upElemStack->first << " " ;
396-
413+ std::map<std::string, int >::reverse_iterator upElemStack = stack.back ().rbegin ();
414+ recordFileFromStack (fileAnalysis, upElemStack);
397415 stack.pop_back ();
398416 }
399417 std::map<std::string, int > tempMap;
@@ -413,14 +431,14 @@ void ReversePolishNotation::reversePolishNotationAnalyze(std::string fileName_le
413431 isFor = false ;
414432 continue ;
415433 }
416- if (token == " R1 " )
434+ if (isOpenSquareBracket ( token) )
417435 {
418436 std::map<std::string, int > tempMap;
419437 tempMap.insert (std::pair<std::string, int >(" ÀÝÌ" , 2 ));
420438 stack.push_back (tempMap);
421439 continue ;
422440 }
423- if (token == " R8 " )// ,
441+ if (isComma ( token) )// ,
424442 {
425443 auto upElemStack = stack.back ().rbegin ();
426444 if (isReadFunctionInExpresiion == false && isDeclareFunction == false && isDeclareSomeVarOneType == false )
@@ -455,13 +473,9 @@ void ReversePolishNotation::reversePolishNotationAnalyze(std::string fileName_le
455473
456474 if (isDeclareFunction == true )
457475 {
458- while (stack.size () != 0 && upElemStack->first != " R3 " )
476+ while (stack.size () != 0 && isOpenBracket ( upElemStack->first )== false )
459477 {
460- if (isTypeDeclarationByCode (upElemStack->first ))
461- fileAnalysis << upElemStack->second << " " << nameType (upElemStack->first ) << " " ;
462- else
463- fileAnalysis << upElemStack->first << " " ;
464-
478+ recordFileFromStack (fileAnalysis, upElemStack);
465479 stack.pop_back ();
466480 if (stack.size () != 0 )
467481 upElemStack = stack.back ().rbegin ();
@@ -490,7 +504,7 @@ void ReversePolishNotation::reversePolishNotationAnalyze(std::string fileName_le
490504 }
491505
492506 }
493- if (token == " R2 " )
507+ if (isClosesquareBracket ( token) )
494508 {
495509 auto upElemStack = stack.back ().rbegin ();
496510 while (stack.size () != 0 && upElemStack->first != " ÀÝÌ" )
@@ -522,16 +536,12 @@ void ReversePolishNotation::reversePolishNotationAnalyze(std::string fileName_le
522536 }
523537 if (stack.size ()!=0 && isTypeDeclarationByCode (stack.back ().rbegin ()->first ) == true && isDeclareSomeVarOneType == false )
524538 {
525- auto upElemStack = stack.back ().rbegin ();
526- // fileAnalysis << upElemStack->second << " " << nameType(upElemStack->first) << " "; stack.pop_back();
539+ std::map<std::string, int >::reverse_iterator upElemStack = stack.back ().rbegin ();
527540 while (stack.size () != 0 && upElemNotNameExpression (upElemStack->first )==true )
528541 {
529542 if (isFor == true && upElemStack->first == " R3" )
530543 break ;
531- if (isTypeDeclarationByCode (upElemStack->first ) == true )
532- fileAnalysis << upElemStack->second << " " << nameType (upElemStack->first ) << " " ;
533- else
534- fileAnalysis << upElemStack->first << " " ;
544+ recordFileFromStack (fileAnalysis, upElemStack);
535545
536546 stack.pop_back ();
537547 if (stack.size () != 0 )
@@ -557,7 +567,7 @@ void ReversePolishNotation::reversePolishNotationAnalyze(std::string fileName_le
557567 else
558568 {
559569 std::map<std::string, int > tempMap;
560- if (token == " W19 " )
570+ if (isConst ( token) )
561571 {
562572 pos = lineLexical.find (' ' );
563573 token += " " + lineLexical.substr (0 , pos);
0 commit comments