-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPreProcessor.hpp
More file actions
1153 lines (1083 loc) · 35.7 KB
/
PreProcessor.hpp
File metadata and controls
1153 lines (1083 loc) · 35.7 KB
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifndef _PREPROCESSOR_H
#define _PREPROCESSOR_H
#endif
#include "TokenAnalyzer.hpp"
#define _FILENAME 1
#define PreProcessorFileName "."
#define INCLUDEPATHPREFIX "/usr/include/"
#define EeraseSubString(substring) do{\
size_t pos = PreProcesstext.find(std::string(substring));\
if(pos != PreProcesstext.npos)\
PreProcesstext.erase(pos);\
}while(0);
#define EraseSubString_(substring) do{\
auto pos = text.find(std::string(substring));\
auto length = std::string(substring).length();\
if(pos != text.npos){\
text.erase(pos,length);\
}\
}while(0);
#define ReplaceSubstring(str,repstring,substring) do{\
for(auto pos = str.find(substring);pos != str.npos; pos = str.find(substring,pos+repstring.length())){\
if(!LETTER_(str[pos - 1]) && !LETTER_(str[pos + substring.length()])){\
str.erase(pos,substring.length());\
str.insert(pos,repstring);\
}\
}\
}while(0);
#define ReplaceMacroFunction(text,pos_,length_,name,argumentlist) do{\
text.erase(pos_,length_);\
std::string replacetext = MacroValue[name].textpart;\
int index = 0;\
for(auto argument : argumentlist){\
ReplaceSubstring(replacetext,argument,MacroValue[name].arguments[index])\
index++;\
}\
text.insert(pos_,replacetext);\
}while(0);
//#define MacroIf 1
class PreProcessor{
public:
PreProcessor();
~PreProcessor();
void getFileName(const std::string filename); // 获取源代码文件名
void setFileName(const std::string _filename); // 设置中间文件文件名
void StartPreProcessor(void); // 开始预处理
void ProcessInclude(void); // 处理#include
void ProcessDefine(void); // 处理#define
void ProcessUndef(void); // 处理#Undef
void ProcessLine(void); // 处理#Line
void ProcessError(void); // 处理#Error
void ProcessPragma(void); // 处理#pragma
void ProcessPreProcessorConditionInstruction(void); // 处理预处理器条件指令(#if,#elif,#else,#endif)
void ProcessConstorlInstruction(void); // 处理控制指令
void do_macroif(void);
void do_macroifdef(void);
void do_macroifndef(void);
bool macroExpress(void);
bool AssignmentExpress(void);
bool LogicOrExpress(void);
bool LogicOrExpress1(bool preresult);
bool LogicAndExpress(void);
bool LogicAndExpress1(long result);
long BitOrExpress(void);
long BitOrExpress1(long result);
long BitXorExpress(void);
long BitXorExpress1(long result);
long BitAndExpress(void);
long BitAndExpress1(long result);
long EqualClassExpress(void);
long EqualClassExpress1(long result);
long RelationExpress(void);
long RelationExpress1(long result);
long ShiftExpress(void);
long ShiftExpress1(long result);
long AddClassExpress(void);
long AddClassExpress1(long result);
long MultiClassExpress(void);
long MultiClassExpress1(long result);
long ForceTransExpress(void);
long UnaryExpress(void);
long PostfixExpress(void);
long PostfixExpress1(long result);
long BaseExpress(void);
bool ConditionExpress(void);
void do_macro(bool BooleanValue);
void do_replace(void);
void copytoFile(std::ofstream &midfile);
void WriteToMidFile(void);
void ClearFile(void);
void matchToken(void); // 获取Token
private:
TokenAnalyzer tokenanalyzer;
bool &PreProcess = tokenanalyzer.getPreProcessStatus();
bool &Global = tokenanalyzer.getGlobalStatus();
std::ofstream MidFile;
std::string filename;
int CurrentTokenType;
std::ifstream _midfile;
std::ofstream Objectfile;
};
PreProcessor::PreProcessor(){
/*
Nothing to do
*/
}
PreProcessor::~PreProcessor(){
/*
Nothing to do
*/
}
void PreProcessor::matchToken(void){
TOKEN token = tokenanalyzer.NextToken();
CurrentTokenType = token.TokenType;
return;
}
static bool isMacro(int type){
switch (type)
{
case SYN_MACRO_DEFINE:
case SYN_MACRO_INCLUDE:
case SYN_MACRO_UNDEF:
case SYN_MACRO_IF:
case SYN_MACRO_IFDEF:
case SYN_MACRO_IFNDEF:
case SYN_LINE:
case SYN_ERROR:
case SYN_PRAGMA:
case SYN_MACRO_ENDIF:
return true;
default:
return false;
}
}
void PreProcessor::do_replace(void){
if(Global){
auto macro_iter = MacroValue.cbegin();
std::string macroname;
while(macro_iter != MacroValue.cend()){
if(text.find(macro_iter->first) != text.npos){
if(macro_iter->second.MacroType == 1){
ReplaceSubstring(text,macro_iter->second.textpart,macro_iter->first)
}
else if(macro_iter->second.MacroType == 2){
bool preProcessStatus = PreProcess,preGlobalStatus = Global;
PreProcess = false;
Global = false;
macroname = macro_iter->first + '(';
char postchar = tokenanalyzer.ReadAChar();
text.push_back(postchar);
if(postchar == '(' && text.find(macroname) != text.npos){
int SubStringLength = macroname.length(), paren = 1;
std::string str;
std::vector<std::string> ArgumentString;
for(;paren != 0;){
postchar = tokenanalyzer.ReadAChar();
if(postchar == '('){
paren++;
str.push_back(postchar);
}
else if(postchar == ')'){
paren--;
if(paren){
str.push_back(postchar);
char temp = postchar;
postchar = tokenanalyzer.ReadAChar();
if(postchar == ','){
SubStringLength++;
text.push_back(temp);
ArgumentString.push_back(str);
str = "";
}
else{
postchar = temp;
tokenanalyzer.SeekFileHandler(-1L);
}
}
else{
ArgumentString.push_back(str);
str = "";
}
}
else if(postchar == ','){
ArgumentString.push_back(str);
str = "";
}
else if(BLANCK(postchar)){
continue;
}
else if(postchar == '\\'){
char temp = postchar;
postchar = tokenanalyzer.ReadAChar();
if(postchar == '\n'){
continue;
}
else{
postchar = temp;
tokenanalyzer.SeekFileHandler(-1L);
str.push_back(postchar);
continue;
}
}
else{
str.push_back(postchar);
}
SubStringLength++;
text.push_back(postchar);
}
for(auto pos_ = text.find(macroname);pos_ != text.npos ; pos_ = text.find(macroname,pos_ + macroname.length())){
ReplaceMacroFunction(text,pos_,SubStringLength,macro_iter->first,ArgumentString)
}
}
else{
text.pop_back();
tokenanalyzer.SeekFileHandler(-1L);
}
PreProcess = preProcessStatus;
Global = preGlobalStatus;
}
}
macro_iter++;
}
}
}
void PreProcessor::copytoFile(std::ofstream &midfile){
do_replace();
midfile << text << std::flush;
text = "";
return;
}
void PreProcessor::StartPreProcessor(void){
matchToken();
//
// Process
while(!tokenanalyzer.isEOF()){
if(isMacro(CurrentTokenType)){
Global = false;
switch(CurrentTokenType){
case SYN_MACRO_DEFINE:
EraseSubString_("#define")
copytoFile(midfile);
ProcessDefine();
break;
case SYN_MACRO_INCLUDE:
EraseSubString_("#include")
copytoFile(midfile);
ProcessInclude();
break;
case SYN_MACRO_UNDEF:
EraseSubString_("#undef")
copytoFile(midfile);
ProcessUndef();
break;
case SYN_MACRO_IF:
EraseSubString_("#if")
copytoFile(midfile);
ProcessPreProcessorConditionInstruction();
break;
case SYN_MACRO_IFDEF:
EraseSubString_("#ifdef")
copytoFile(midfile);
ProcessPreProcessorConditionInstruction();
break;
case SYN_MACRO_IFNDEF:
EraseSubString_("#ifndef")
copytoFile(midfile);
ProcessPreProcessorConditionInstruction();
break;
case SYN_LINE:
EraseSubString_("#line")
copytoFile(midfile);
ProcessLine();
break;
case SYN_ERROR:
EraseSubString_("#error")
copytoFile(midfile);
ProcessError();
break;
case SYN_PRAGMA:
EraseSubString_("#pragma")
copytoFile(midfile);
ProcessPragma();
break;
case SYN_MACRO_ENDIF:
EraseSubString_("#endif")
break;
}
Global = true;
}
else{
copytoFile(midfile);
}
matchToken();
}
return;
}
void PreProcessor::getFileName(const std::string filename){
tokenanalyzer.SetFileName(filename);
return;
}
void PreProcessor::ClearFile(void){
char error_char = 0xff;
char CharReadFromFile;
midfile.close();
std::ifstream _midfile;
_midfile.open(filename,std::ios_base::in);
while(_midfile){
CharReadFromFile = _midfile.get();
if(CharReadFromFile == error_char){
CharReadFromFile = ' ';
}
Objectfile << CharReadFromFile;
}
_midfile.close();
Objectfile.close();
return;
}
void PreProcessor::setFileName(const std::string _filename){
filename = PreProcessorFileName + _filename ;
midfile.open(filename,std::ios_base::out);
Objectfile.open(".source_" + _filename,std::ios_base::out);
return;
}
void PreProcessor::WriteToMidFile(void){
midfile << PreProcesstext << std::flush;
PreProcesstext = "";
}
static bool isProcessDefine(const std::string macroname){
if(MacroValue.find(macroname) != MacroValue.end()){
return true;
}
return false;
}
void PreProcessor::ProcessUndef(void){
matchToken();
std::string MacroName = tokenanalyzer.tokenString();
if(isProcessDefine(MacroName)){
MacroValue.erase(MacroName);
}
else{
;
}
} // 处理#Undef
void PreProcessor::ProcessLine(void){
;
} // 处理#Line
void PreProcessor::ProcessError(void){
;
} // 处理#Error
void PreProcessor::ProcessPragma(void){
;
}
void PreProcessor::ProcessInclude(void){
matchToken();
std::string _filename ;
PreProcessor preprocess;
if(CurrentTokenType == SYN_STRING){
std::string tempstr = tokenanalyzer.tokenString();
for(auto str : tempstr)
if(str != '\"')
_filename.push_back(str);
preprocess.getFileName(_filename);
preprocess.StartPreProcessor();
return;
}
else if(CurrentTokenType == SYN_LT){
matchToken();
_filename = INCLUDEPATHPREFIX + tokenanalyzer.tokenString();
preprocess.getFileName(_filename);
preprocess.StartPreProcessor();
matchToken();
return;
}
}
bool PreProcessor::macroExpress(void){
#if defined(MacroIf)
std::cout << "macroExpres-> AssignmentExpress" << std::endl;
#endif
return AssignmentExpress();
}
bool PreProcessor::AssignmentExpress(void){
#if defined(MacroIf)
std::cout << "AssignmentExpress-> ConditionExpress" << std::endl;
#endif
return ConditionExpress();
}
bool PreProcessor::ConditionExpress(void){
#if defined(MacroIf)
std::cout << "ConditionExpress-> LogicOrExpress[?macroExpress:ConditionExpress]" << std::endl;
#endif
auto result = LogicOrExpress();
if(CurrentTokenType == SYN_QUES){
matchToken();
if(result){
result = macroExpress();
matchToken();
ConditionExpress();
return result;
}
else{
macroExpress();
matchToken();
result = ConditionExpress();
return result;
}
}
else{
return result;
}
}
bool PreProcessor::LogicOrExpress(void){
#if defined(MacroIf)
std::cout << "LogicOrExpress-> LogicAndExpress LogicOrExpress1" << std::endl;
#endif
auto result = LogicAndExpress();
return LogicOrExpress1(result);
}
bool PreProcessor::LogicOrExpress1(bool preresult){
#if defined(MacroIf)
std::cout << "LogicOrExpress1->|| LogicAndExpress LogicOrExpress1" << std::endl;
#endif
if(preresult){
if(CurrentTokenType == SYN_OR){
matchToken();
LogicAndExpress();
LogicOrExpress1(true);
return preresult;
}
else{
return preresult;
}
}
else{
if(CurrentTokenType == SYN_OR){
matchToken();
long result = LogicAndExpress();
result = LogicOrExpress1(result);
return result || preresult;
}
else{
return preresult;
}
}
}
bool PreProcessor::LogicAndExpress(void){
#if defined(MacroIf)
std::cout << "LogicAndExpress->BitOrExpress LogicAndExpress1" << std::endl;
#endif
long result = BitOrExpress();
return LogicAndExpress1(result);
}
bool PreProcessor::LogicAndExpress1(long result){
#if defined(MacroIf)
std::cout << "LogicAndExpress1-> && BitOrExpress LogicAndExpress1" << std::endl;
#endif
if(result){
if(CurrentTokenType == SYN_AND){
matchToken();
long result_ = BitOrExpress();
result_ = LogicAndExpress1(result_);
return result_ && result;
}
else{
return result;
}
}
else{
if(CurrentTokenType == SYN_AND){
matchToken();
BitOrExpress();
LogicOrExpress1(1);
}
return result;
}
}
long PreProcessor::BitOrExpress(void){
#if defined(MacroIf)
std::cout << "BitOrExpress->BitXorExpress BitOrExpress1" << std::endl;
#endif
long result = BitXorExpress();
return BitOrExpress1(result);
}
long PreProcessor::BitOrExpress1(long result){
#if defined(MacroIf)
std::cout << "BitOrExpress1-> | BitXorExpress BitOrExpress1" << std::endl;
#endif
if(CurrentTokenType == SYN_BIT_OR){
matchToken();
long result_ = BitXorExpress();
result_ = BitOrExpress1(result_);
return result | result_;
}
else{
return result;
}
}
long PreProcessor::BitXorExpress(void){
#if defined(MacroIf)
std::cout << "BitXorExpress->BitAndExpress BitXorExpress1" << std::endl;
#endif
long result = BitAndExpress();
return BitXorExpress1(result);
}
long PreProcessor::BitXorExpress1(long result){
#if defined(MacroIf)
std::cout << "BitXorExpress1-> ^ BitAndExpress BitXorExpress1" << std::endl;
#endif
if(CurrentTokenType == SYN_BIT_XOR){
matchToken();
long result_ = BitAndExpress();
result_ = BitXorExpress1(result_);
return result_ ^ result;
}
else{
return result;
}
}
long PreProcessor::BitAndExpress(void){
#if defined(MacroIf)
std::cout << "BitAndExpress-> EqualClassExpress BitAndExpress1" << std::endl;
#endif
long result = EqualClassExpress();
return BitAndExpress1(result);
}
long PreProcessor::BitAndExpress1(long result){
#if defined(MacroIf)
std::cout << "BitAndExpress1-> & EqualClassExpress BitAndExpress1" << std::endl;
#endif
if(CurrentTokenType == SYN_BIT_AND){
matchToken();
long result_ = EqualClassExpress();
result_ = BitAndExpress1(result_);
return result & result_;
}
else{
return result;
}
}
long PreProcessor::EqualClassExpress(void){
#if defined(MacroIf)
std::cout << "EqualClassExpress-> RelationExpress EqualClassExpress1" << std::endl;
#endif
long result = RelationExpress();
return EqualClassExpress1(result);
}
long PreProcessor::EqualClassExpress1(long result){
#if defined(MacroIf)
std::cout << "EqualClassExpress-> (== | != )RelationExpress EqualClassExpress1" << std::endl;
#endif
if(CurrentTokenType == SYN_EQ){
matchToken();
long result_ = RelationExpress();
result_ = EqualClassExpress1(result_);
return result_ == result;
}
else if(CurrentTokenType == SYN_NOTEQ){
matchToken();
long result_ = RelationExpress();
result_ = EqualClassExpress1(result_);
return result != result_;
}
else{
return result;
}
}
long PreProcessor::RelationExpress(void){
#if defined(MacroIf)
std::cout << "RelationExpress-> ShiftExpress RelationExpress1" << std::endl;
#endif
long result = ShiftExpress();
return RelationExpress1(result);
}
long PreProcessor::RelationExpress1(long result){
#if defined(MacroIf)
std::cout << "RelationExpress1-> (< |> |<= | >=) ShiftExpress RelationExpress1" << std::endl;
#endif
if(CurrentTokenType == SYN_LT) {
matchToken();
long result_ = ShiftExpress();
result_ = RelationExpress1(result_);
return result_ < result;
}
else if(CurrentTokenType == SYN_GT) {
matchToken();
long result_ = ShiftExpress();
result_ = RelationExpress1(result_);
return result_ > result;
}
else if(CurrentTokenType == SYN_LE) {
matchToken();
long result_ = ShiftExpress();
result_ = RelationExpress1(result_);
return result_ <= result;
}
else if(CurrentTokenType == SYN_GE) {
matchToken();
long result_ = ShiftExpress();
result_ = RelationExpress1(result_);
return result_ >= result;
}
else{
return result;
}
}
long PreProcessor::ShiftExpress(void){
#if defined(MacroIf)
std::cout << "ShiftExpress->AddClassExpress ShiftExpress1" << std::endl;
#endif
long result = AddClassExpress();
return ShiftExpress1(result);
}
long PreProcessor::ShiftExpress1(long result){
#if defined(MacroIf)
std::cout << "ShiftExpress1-> (<< |>>) AddClassExpress ShiftExpress1" << std::endl;
#endif
if(CurrentTokenType == SYN_BIT_SHL){
matchToken();
long result_ = AddClassExpress();
result_ = ShiftExpress1(result_);
return result << result_;
}
else if(CurrentTokenType == SYN_BIT_SHR){
matchToken();
long result_ = AddClassExpress();
result_ = ShiftExpress1(result_);
return result >> result_;
}
else{
return result;
}
}
long PreProcessor::AddClassExpress(void){
#if defined(MacroIf)
std::cout << "AddClassExpress->MultiExpress AddClassExpress1" << std::endl;
#endif
long result = MultiClassExpress();
return AddClassExpress1(result);
}
long PreProcessor::AddClassExpress1(long result){
#if defined(MacroIf)
std::cout << "AddClassExpress1-> (+|-) MultiExpress AddClassExpress1" << std::endl;
#endif
if(CurrentTokenType == SYN_ADD){
matchToken();
long result_ = MultiClassExpress();
result_ = AddClassExpress1(result_);
return result + result_;
}
else if(CurrentTokenType == SYN_SUB){
matchToken();
long result_ = MultiClassExpress();
result_ = AddClassExpress1(result_);
return result - result_;
}
else{
return result;
}
}
long PreProcessor::MultiClassExpress(void){
#if defined(MacroIf)
std::cout << "ForceTransExpress-> ForceTransExpress MultiClassExpress1" << std::endl;
#endif
long result = ForceTransExpress();
return MultiClassExpress1(result);
}
long PreProcessor::MultiClassExpress1(long result){
#if defined(MacroIf)
std::cout << "MultiClassExpress1-> (*|/|%) ForceTransExpress MultiClassExpress1" << std::endl;
#endif
if(CurrentTokenType == SYN_MUL){
matchToken();
long result_ = ForceTransExpress();
result_ = MultiClassExpress1(result_);
return result * result_;
}
else if(CurrentTokenType == SYN_DIV){
matchToken();
long result_ = ForceTransExpress();
result_ = MultiClassExpress1(result_);
return result / result_;
}
else if(CurrentTokenType == SYN_MOD){
matchToken();
long result_ = ForceTransExpress();
result_ = MultiClassExpress1(result_);
return result % result_;
}
else{
return result;
}
}
long PreProcessor::ForceTransExpress(void){
#if defined(MacroIf)
std::cout << "ForceTransExpress-> ..." << std::endl;
#endif
if(CurrentTokenType == SYN_PAREN_L){
matchToken();
matchToken();
matchToken();
ForceTransExpress();
}
else{
return UnaryExpress();
}
}
static bool isUnaryOperator(int tokentype){
switch(tokentype){
case SYN_BIT_AND:
case SYN_MUL:
case SYN_ADD:
case SYN_SUB:
case SYN_BIT_NOT:
case SYN_NOT:
return true;
default:
return false;
}
}
long PreProcessor::UnaryExpress(void){
long result;
switch(CurrentTokenType){
case SYN_INC:
matchToken();
result = UnaryExpress();
result++;
return result;
case SYN_DEC:
matchToken();
result = UnaryExpress();
result--;
return result;
case SYN_KEYWORD:
if(tokenanalyzer.tokenString() == std::string("defined")){
if(CurrentTokenType == SYN_KEYWORD){
if(MacroValue.find(tokenanalyzer.tokenString()) != MacroValue.end()){
matchToken();
return true;
}
else{
matchToken();
return false;
}
}
}
case SYN_FUNCTION:
matchToken();
if(CurrentTokenType == '('){
matchToken();
if(MacroValue.find(tokenanalyzer.tokenString()) != MacroValue.end()){
//result = atol(MacroValue[tokenanalyzer.tokenString()].textpart.c_str());
matchToken();
return true;
}
else{
matchToken();
return false;
}
}
default:
if(isUnaryOperator(CurrentTokenType)){
switch(CurrentTokenType){
case SYN_NOT:
matchToken();
return ! UnaryExpress();
}
}
else{
return PostfixExpress();
}
}
}
long PreProcessor::PostfixExpress(void){
long result = BaseExpress();
return PostfixExpress1(result);
}
long PreProcessor::PostfixExpress1(long result){
if(CurrentTokenType == SYN_INC){
matchToken();
long result_ = PostfixExpress1(result);
result_++;
return result_;
}
else if(CurrentTokenType == SYN_DEC){
matchToken();
long result_ = PostfixExpress1(result);
result_--;
return result_;
}
else{
return BaseExpress();
}
}
long PreProcessor::BaseExpress(void){
long result;
switch(CurrentTokenType){
case SYN_KEYWORD:
if(MacroValue.find(tokenanalyzer.tokenString()) != MacroValue.end()){
result = atol(MacroValue[tokenanalyzer.tokenString()].textpart.c_str());
matchToken();
return result;
}
else{
std::cout << "Not define " << tokenanalyzer.tokenString() << std::endl;
exit(1);
}
case SYN_STRING:
if(tokenanalyzer.tokenString() == ""){
matchToken();
return 0;
}
else{
matchToken();
return 1;
}
case SYN_PAREN_L:
matchToken();
result = macroExpress();
matchToken();
return result;
case SYN_NUMBER_LONG:
result = atol(tokenanalyzer.tokenString().c_str());
matchToken();
return result;
case SYN_NUMBER_DOUBLE:
result = std::stod(tokenanalyzer.tokenString().c_str());
matchToken();
return result;
}
}
void PreProcessor::do_macro(bool BooleanValue){
if(CurrentTokenType == SYN_MACRO_DEFINE){
EeraseSubString("#define")
PreProcess = false;
ProcessDefine();
PreProcess = true;
}
else if(CurrentTokenType == SYN_MACRO_ELIF && ! BooleanValue){
do_macroif();
}
else if(CurrentTokenType == SYN_MACRO_ELSE && ! BooleanValue){
PreProcess = true;
while(CurrentTokenType != SYN_MACRO_ENDIF)
matchToken();
PreProcess = false;
}
else if(CurrentTokenType == SYN_MACRO_IF){
do_macroif();
}
else if(CurrentTokenType == SYN_MACRO_IFDEF){
do_macroifdef();
}
else if(CurrentTokenType == SYN_MACRO_IFNDEF){
do_macroifndef();
}
else if(CurrentTokenType == SYN_MACRO_INCLUDE && BooleanValue){
EeraseSubString("#include")
PreProcess = false;
ProcessInclude();
PreProcess = true;
}
}
void PreProcessor::do_macroif(void){
matchToken();
bool isExpressisTrue = macroExpress();
long offset = tokenanalyzer.tokenLength();
offset = 0 - offset;
tokenanalyzer.SeekFileHandler(offset);
if(isExpressisTrue){
PreProcess = true;
while(CurrentTokenType != SYN_MACRO_ENDIF){
matchToken();
do_macro(true);
if(CurrentTokenType == SYN_MACRO_ENDIF)
break;
}
if(CurrentTokenType == SYN_MACRO_ENDIF){
EeraseSubString("#endif")
}
PreProcess = false;
}
else{
PreProcess = false;
while(CurrentTokenType != SYN_MACRO_ENDIF){
matchToken();
do_macro(false);
if(CurrentTokenType == SYN_MACRO_ENDIF)
break;
}
if(CurrentTokenType == SYN_MACRO_ENDIF){
EeraseSubString("#endif")
}
PreProcess = false;
}
WriteToMidFile();
}
void PreProcessor::do_macroifdef(void){
matchToken();
std::string macroname = tokenanalyzer.tokenString();
EraseSubString_("#ifdef");
if(isProcessDefine(macroname)){
PreProcess = true;
while(CurrentTokenType != SYN_MACRO_ELSE && CurrentTokenType != SYN_MACRO_ENDIF){
matchToken();
if(CurrentTokenType == SYN_MACRO_DEFINE){
EeraseSubString("#define")
PreProcess = false;
ProcessDefine();
PreProcess = true;
}
else if(CurrentTokenType == SYN_MACRO_INCLUDE){
EeraseSubString("#include")
PreProcess = false;
ProcessInclude();
PreProcess = true;
}
else if(CurrentTokenType == SYN_MACRO_IFDEF){
EeraseSubString("#ifdef")
PreProcess = false;
ProcessPreProcessorConditionInstruction();
PreProcess = true;
}
else if(CurrentTokenType == SYN_MACRO_IFNDEF){
EeraseSubString("#ifndef")
PreProcess = false;
ProcessPreProcessorConditionInstruction();
PreProcess = true;
}
else if(CurrentTokenType == SYN_MACRO_IF){
EeraseSubString("#if")
PreProcess = false;
ProcessPreProcessorConditionInstruction();
PreProcess = true;
}
}
if(CurrentTokenType == SYN_MACRO_ELSE){
EeraseSubString("#else")
PreProcess = false;
while(CurrentTokenType != SYN_MACRO_ENDIF)