@@ -11,12 +11,9 @@ ReversePolishNotation::~ReversePolishNotation()
11
11
12
12
int ReversePolishNotation::getPriority (std::string word)
13
13
{
14
- // priority
15
14
for (auto & item : priority)
16
- {
17
15
if (item.first == word)
18
16
return item.second ;
19
- }
20
17
return -1 ;
21
18
}
22
19
@@ -28,7 +25,7 @@ bool ReversePolishNotation::isFunctionDeclaration(std::string line)
28
25
{
29
26
std::string token = line.substr (0 , pos);
30
27
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
32
29
count++;
33
30
else
34
31
{
@@ -90,6 +87,52 @@ size_t ReversePolishNotation::positionTypeConversion(std::string line)
90
87
return -1 ;
91
88
}
92
89
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
+
93
136
void ReversePolishNotation::reversePolishNotationAnalyze (std::string fileName_lexical, std::string fileName_RPN)
94
137
{
95
138
std::ifstream lexical;
@@ -122,17 +165,10 @@ void ReversePolishNotation::reversePolishNotationAnalyze(std::string fileName_le
122
165
}
123
166
if (manyLineComment == true )
124
167
{
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)
133
170
manyLineComment = false ;
134
- continue ;
135
- }
171
+ continue ;
136
172
}
137
173
size_t pos = 0 ;
138
174
std::string token=" " , nameFunctionInExpression=" " ;
@@ -157,15 +193,15 @@ void ReversePolishNotation::reversePolishNotationAnalyze(std::string fileName_le
157
193
else
158
194
{
159
195
token = lineLexical.substr (0 , pos);
160
- if (token == " R1 " )
196
+ if (isOpenSquareBracket ( token) )
161
197
isEmptyArrayCurrent = isEmptyArray (lineLexical);
162
198
lineLexical.erase (0 , pos + 1 );
163
199
}
164
200
165
201
}
166
202
// TODO one line comment in expression
167
203
168
- if (token != " R5 " )
204
+ if (isOpenCurlyBracket ( token)== false )
169
205
{
170
206
if (stack.size () != 0 && lastCommndIFFORWHILE_withoutTHEN != " " )
171
207
{
@@ -205,7 +241,7 @@ void ReversePolishNotation::reversePolishNotationAnalyze(std::string fileName_le
205
241
{
206
242
if (stack.size () == 0 )
207
243
{
208
- if (token == " R7 " )
244
+ if (isSemicolon ( token) )
209
245
continue ;
210
246
std::map<std::string, int > tempMap;
211
247
if (isReadFunctionInExpresiion == false )
@@ -245,7 +281,7 @@ void ReversePolishNotation::reversePolishNotationAnalyze(std::string fileName_le
245
281
continue ;
246
282
}
247
283
248
- if (token== " R5 " ) // {
284
+ if (isOpenCurlyBracket ( token) ) // {
249
285
{
250
286
auto upElemStack = stack.back ().rbegin ();
251
287
if (upElemStack->first == " PROC" )
@@ -275,7 +311,7 @@ void ReversePolishNotation::reversePolishNotationAnalyze(std::string fileName_le
275
311
continue ;
276
312
}
277
313
}
278
- if (token== " R6 " ) // }
314
+ if (isCloseCurlyBracket ( token) ) // }
279
315
{
280
316
auto upElemStack = stack.back ().rbegin ();
281
317
if (isIFCondition (upElemStack->first )==true )
@@ -292,17 +328,14 @@ void ReversePolishNotation::reversePolishNotationAnalyze(std::string fileName_le
292
328
stack.pop_back ();
293
329
continue ;
294
330
}
295
- if (token == " R7 " )// ;
331
+ if (isSemicolon ( token) )// ;
296
332
{
297
- auto upElemStack = stack.back ().rbegin ();
333
+ std::map<std::string, int >::reverse_iterator upElemStack = stack.back ().rbegin ();
298
334
while (stack.size () != 0 && upElemNotNameExpression (upElemStack->first )==true )
299
335
{
300
- if (isFor == true && upElemStack->first == " R3 " )
336
+ if (isFor == true && isOpenBracket ( upElemStack->first ) )
301
337
break ;
302
- if (isTypeDeclarationByCode (upElemStack->first ) == true )
303
- fileAnalysis << upElemStack->second << " " << nameType (upElemStack->first ) << " " ;
304
- else
305
- fileAnalysis << upElemStack->first << " " ;
338
+ recordFileFromStack (fileAnalysis, upElemStack);
306
339
307
340
stack.pop_back ();
308
341
if (stack.size () != 0 )
@@ -318,32 +351,24 @@ void ReversePolishNotation::reversePolishNotationAnalyze(std::string fileName_le
318
351
{
319
352
320
353
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
+
325
356
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 ();
330
360
lastCommndIFFORWHILE_withoutTHEN = " " ;
331
-
332
361
}
333
362
continue ;
334
363
}
335
- if (token == " R3 " )// (
364
+ if (isOpenBracket ( token) )// (
336
365
{
337
366
if (isDeclareFunction == true )
338
367
{
339
368
while (stack.size () != 0 )
340
369
{
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);
347
372
stack.pop_back ();
348
373
}
349
374
}
@@ -357,23 +382,20 @@ void ReversePolishNotation::reversePolishNotationAnalyze(std::string fileName_le
357
382
stack.push_back (tempMap);
358
383
continue ;
359
384
}
360
- if (token == " R4 " )// )
385
+ if (isCloseBracket ( token) )// )
361
386
{
362
387
std::string end = " R3" ;
363
388
if (isReadFunctionInExpresiion == true )
364
389
end = " Ô" ;
365
390
while (stack.size () != 0 && stack.back ().rbegin ()->first != end)
366
391
{
367
392
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);
373
395
stack.pop_back ();
374
396
}
375
397
if (stack.size ()!=0 )
376
- if (stack.back ().rbegin ()->first == " R3 " )
398
+ if (isOpenBracket ( stack.back ().rbegin ()->first ) )
377
399
stack.pop_back ();
378
400
if (isReadFunctionInExpresiion == true && stack.size () != 0 )
379
401
{
@@ -388,12 +410,8 @@ void ReversePolishNotation::reversePolishNotationAnalyze(std::string fileName_le
388
410
{
389
411
while (stack.size () != 0 )
390
412
{
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);
397
415
stack.pop_back ();
398
416
}
399
417
std::map<std::string, int > tempMap;
@@ -413,14 +431,14 @@ void ReversePolishNotation::reversePolishNotationAnalyze(std::string fileName_le
413
431
isFor = false ;
414
432
continue ;
415
433
}
416
- if (token == " R1 " )
434
+ if (isOpenSquareBracket ( token) )
417
435
{
418
436
std::map<std::string, int > tempMap;
419
437
tempMap.insert (std::pair<std::string, int >(" ÀÝÌ" , 2 ));
420
438
stack.push_back (tempMap);
421
439
continue ;
422
440
}
423
- if (token == " R8 " )// ,
441
+ if (isComma ( token) )// ,
424
442
{
425
443
auto upElemStack = stack.back ().rbegin ();
426
444
if (isReadFunctionInExpresiion == false && isDeclareFunction == false && isDeclareSomeVarOneType == false )
@@ -455,13 +473,9 @@ void ReversePolishNotation::reversePolishNotationAnalyze(std::string fileName_le
455
473
456
474
if (isDeclareFunction == true )
457
475
{
458
- while (stack.size () != 0 && upElemStack->first != " R3 " )
476
+ while (stack.size () != 0 && isOpenBracket ( upElemStack->first )== false )
459
477
{
460
- if (isTypeDeclarationByCode (upElemStack->first ))
461
- fileAnalysis << upElemStack->second << " " << nameType (upElemStack->first ) << " " ;
462
- else
463
- fileAnalysis << upElemStack->first << " " ;
464
-
478
+ recordFileFromStack (fileAnalysis, upElemStack);
465
479
stack.pop_back ();
466
480
if (stack.size () != 0 )
467
481
upElemStack = stack.back ().rbegin ();
@@ -490,7 +504,7 @@ void ReversePolishNotation::reversePolishNotationAnalyze(std::string fileName_le
490
504
}
491
505
492
506
}
493
- if (token == " R2 " )
507
+ if (isClosesquareBracket ( token) )
494
508
{
495
509
auto upElemStack = stack.back ().rbegin ();
496
510
while (stack.size () != 0 && upElemStack->first != " ÀÝÌ" )
@@ -522,16 +536,12 @@ void ReversePolishNotation::reversePolishNotationAnalyze(std::string fileName_le
522
536
}
523
537
if (stack.size ()!=0 && isTypeDeclarationByCode (stack.back ().rbegin ()->first ) == true && isDeclareSomeVarOneType == false )
524
538
{
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 ();
527
540
while (stack.size () != 0 && upElemNotNameExpression (upElemStack->first )==true )
528
541
{
529
542
if (isFor == true && upElemStack->first == " R3" )
530
543
break ;
531
- if (isTypeDeclarationByCode (upElemStack->first ) == true )
532
- fileAnalysis << upElemStack->second << " " << nameType (upElemStack->first ) << " " ;
533
- else
534
- fileAnalysis << upElemStack->first << " " ;
544
+ recordFileFromStack (fileAnalysis, upElemStack);
535
545
536
546
stack.pop_back ();
537
547
if (stack.size () != 0 )
@@ -557,7 +567,7 @@ void ReversePolishNotation::reversePolishNotationAnalyze(std::string fileName_le
557
567
else
558
568
{
559
569
std::map<std::string, int > tempMap;
560
- if (token == " W19 " )
570
+ if (isConst ( token) )
561
571
{
562
572
pos = lineLexical.find (' ' );
563
573
token += " " + lineLexical.substr (0 , pos);
0 commit comments