-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompiler.hpp
executable file
·2110 lines (2087 loc) · 87.5 KB
/
compiler.hpp
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
#pragma once
#include <ctime>
#include <iostream>
#include <chrono>
#include <algorithm>
#include <new>
#include <ostream>
#include <sstream>
#include <initializer_list>
#include <unordered_map>
#include <fstream>
#include <unordered_map>
#include <memory>
#include "data_types/instructions.hpp"
#include "data_types/ast.hpp"
#include "compiler_def_1.hpp"
#include "data_types/exception.hpp"
#include "headers/colors.hpp"
#include "headers/stringUtilities.hpp"
#include "headers/to_string.hpp"
#include "headers/isInVector.hpp"
#define HALF_RAND (((rand() & 0x7FFF) << 1) + (rand() & 0x1))
#define FULL_RAND ((HALF_RAND << 16) + HALF_RAND)
using std::chrono::duration_cast;
using std::chrono::milliseconds;
using std::chrono::seconds;
using std::chrono::system_clock;
class Compiler final {
public:
class pppi {
public:
pppi_t type;
std::string str;
pppi(pppi_t type, std::string str) : type(type), str(str) {};
pppi(pppi_t type) : type(type) {};
pppi(std::string str) : type(pppi_t::Token), str(str) {};
operator std::string() const {
std::stringstream ss;
ss << "pppi(";
switch (type) {
case pppi_t::Char:
ss << "Char";
break;
case pppi_t::Token:
ss << "Token";
break;
case pppi_t::String:
ss << "String";
break;
case pppi_t::NewLine:
ss << "NewLine";
break;
}
ss << ", " << str << ")";
return ss.str();
}
};
class ppi {
public:
ppi_t type;
std::string str;
ppi(ppi_t type, std::string str) : type(type), str(str) {};
ppi(ppi_t type) : type(type) {};
ppi(std::string str) : type(ppi_t::Token), str(str) {};
static ppi parseppi(std::string str) {
if (str == "define") return ppi(ppi_t::ppDef,str);
else if (str == "if") return ppi(ppi_t::ppIf,str);
else if (str == "elif") return ppi(ppi_t::ppElif,str);
else if (str == "else") return ppi(ppi_t::ppElse,str);
else if (str == "endif") return ppi(ppi_t::ppEndif,str);
else if (str == "ifndef") return ppi(ppi_t::ppIfndef,str);
else if (str == "ifdef") return ppi(ppi_t::ppIfdef,str);
else if (str == "elifndef") return ppi(ppi_t::ppElifndef,str);
else if (str == "elifdef") return ppi(ppi_t::ppElifdef,str);
else if (str == "pragma") return ppi(ppi_t::ppPragma,str);
else if (str == "include") return ppi(ppi_t::ppInclude,str);
else if (str == "opdef") return ppi(ppi_t::ppOpDef,str);
else if (str == "undef") return ppi(ppi_t::ppUndef,str);
else if (str == "error") return ppi(ppi_t::ppError,str);
else if (str == "warning") return ppi(ppi_t::ppWarning,str);
throw Exception("UnknownPreProcessorDirective", str);
};
static ppi frompppi(pppi p) {
switch (p.type) {
case pppi_t::Char:
return ppi(ppi_t::Char, p.str);
case pppi_t::String:
return ppi(ppi_t::String, p.str);
case pppi_t::Token:
return ppi(ppi_t::Token, p.str);
case pppi_t::NewLine:
return ppi(ppi_t::NewLine, p.str);
default:
return {0};//null
}
}
ppi removeelse() {
switch (this->type) {
case ppi_t::ppElif:
return ppi(ppi_t::ppIf, this->str);
case ppi_t::ppElifdef:
return ppi(ppi_t::ppIfdef, this->str);
case ppi_t::ppElifndef:
return ppi(ppi_t::ppIfndef, this->str);
default:
return *this;
}
}
operator std::string() const {
std::stringstream ss;
ss << "ppi(";
switch (type) {
case ppi_t::Char:
ss << "Char";
break;
case ppi_t::String:
ss << "String";
break;
case ppi_t::Token:
ss << "Token";
break;
case ppi_t::NewLine:
ss << "NewLine";
break;
case ppi_t::ppDef:
ss << "ppDef";
break;
case ppi_t::ppIf:
ss << "ppIf";
break;
case ppi_t::ppElif:
ss << "ppElif";
break;
case ppi_t::ppElse:
ss << "ppElse";
break;
case ppi_t::ppEndif:
ss << "ppEndif";
break;
case ppi_t::ppIfndef:
ss << "ppIfndef";
break;
case ppi_t::ppIfdef:
ss << "ppIfdef";
break;
case ppi_t::ppElifndef:
ss << "ppElifndef";
break;
case ppi_t::ppElifdef:
ss << "ppElifdef";
break;
case ppi_t::ppPragma:
ss << "ppPragma";
break;
case ppi_t::ppInclude:
ss << "ppInclude";
break;
case ppi_t::ppOpDef:
ss << "ppOpDef";
break;
case ppi_t::ppUndef:
ss << "ppUndef";
break;
case ppi_t::ppError:
ss << "ppError";
break;
case ppi_t::ppWarning:
ss << "ppWarning";
break;
case ppi_t::ppConcat:
ss << "ppConcat";
break;
case ppi_t::ppdef_name:
ss << "ppdef_name";
break;
case ppi_t::ppdef_value_start:
ss << "ppdef_value_start";
break;
case ppi_t::ppdef_value_end:
ss << "ppdef_value_end";
break;
case ppi_t::ppdef_arg:
ss << "ppdef_arg";
break;
case ppi_t::Operator:
ss << "Operator";
break;
case ppi_t::Delimiter:
ss << "Delimiter";
break;
case ppi_t::groupStart:
ss << "groupStart";
break;
case ppi_t::groupEnd:
ss << "groupEnd";
break;
case ppi_t::ppEndExpr:
ss << "ppEndExpr";
break;
case ppi_t::Integer:
ss << "Integer";
break;
case ppi_t::Float:
ss << "Float";
break;
}
ss << ", " << str << ")";
return ss.str();
}
bool operator== (const ppi& other) const {
return this->type == other.type && this->str == other.str;
}
bool operator!= (const ppi& other) const {
return !(*this == other);
}
bool operator== (const std::string& other) const {
return this->str == other;
}
bool operator!= (const std::string& other) const {
return !(*this == other);
}
};
class Definition {
public:
std::vector<std::string> args;
std::vector<ppi> val;
virtual std::vector<ppi> parse(std::vector<std::vector<ppi>> args_a) {
std::vector<ppi> ret;
std::vector<ppi>* args_aap = nullptr;
if ((args_a.size() != args.size()) && args.back() != "__va_args__")
throw Exception("DefinitionParsingError", "args_a.size() != args.size(); not enough/too many arguments given to macro call.");
if (args.back() == "__va_args__") {
args_aap = new std::vector<ppi>;
unsigned short optargs = args_a.size() - (args.size() - 1);
for (unsigned short argp = (args.size() - 1); argp < (args_a.size() + 1);argp++) {
for (auto i : args_a[argp]) args_aap->push_back(i);
args_aap->push_back(ppi(ppi_t::Delimiter,","));
}
if (optargs > 0) args_aap->pop_back();
for (unsigned short i = 0; i < optargs; i++) args_a.pop_back();
}
for (ppi& p : val) {
if (p.type == ppi_t::Token) {
if (p.str == "__VA_ARGS__") {
if (args_aap != nullptr) {
for (auto& p : *args_aap) {
ret.push_back(p);
}
}
goto next;
}
auto findout = std::find(args.begin(),
args.end(),
p.str);
if (findout != args.end()) {
auto index = std::distance(args.begin(), findout);
for (auto& p : args_a[index]) {
ret.push_back(p);
}
} else goto notfound;
} else {
notfound:
ret.push_back(p);
}
next: ;
}
if (args_aap != nullptr) delete args_aap;
return ret;
}
Definition(const std::vector<std::string>& args, const std::vector<ppi>& val) : args(args), val(val) {};
Definition(const std::initializer_list<std::string>& args, const std::initializer_list<ppi>& val) : args(args), val(val) {};
Definition() = default;
};
std::vector<std::string> includepath;
bool nobase = false;
enum class OptimizationLevel {
None,
Minimal,
Full,
Maximum
} olevel = OptimizationLevel::None;
private:
const char* arr = "0123456789abcdef";
class Logger {
int64_t currt;
bool& verbose;
public:
Logger(Compiler& c) : verbose(c.verbose) {}
void begin() {
currt = duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count();
}
void debug(const std::string& str) {
if (verbose) {
std::cout << COLOR_GREEN "[DEBUG " << (double)(duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count() - currt) / 1000 << "] " << str << COLOR_RESET << std::endl;
}
}
void info(const std::string& str) {
std::cout << COLOR_BLUE "[INFO " << (double)(duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count() - currt) / 1000 << "] " << str << COLOR_RESET << std::endl;
}
void warn(const std::string& str) {
std::cout << COLOR_YELLOW "[WARN " << (double)(duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count() - currt) / 1000 << "] " << str << COLOR_RESET << std::endl;
}
void error(const std::string& str) {
std::cout << COLOR_RED "[ERROR " << (double)(duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count() - currt) / 1000 << "] " << str << COLOR_RESET << std::endl;
}
void fatal(const std::string& str) {
std::cout << COLOR_DARK_RED "[FATAL " << (double)(duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count() - currt) / 1000 << "] " << str << COLOR_RESET << std::endl;
exit(1);
}
} logger;
class ForEveryDefinition : public Definition {
public:
virtual std::vector<ppi> parse(std::vector<std::vector<ppi>> args_a) {
std::vector<ppi> ret;
if (args_a.size() > 1) {
if (args_a[0].size() != 1) return ret;
if (args_a[0][0].type != ppi_t::Token) return ret;
for (unsigned short i = 1; i < args_a.size(); i++) {
ret.push_back(args_a[0][0]);
ret.push_back(ppi(ppi_t::groupStart,"("));
for (auto& iv : args_a[i]) ret.push_back(iv);
ret.push_back(ppi(ppi_t::groupEnd,")"));
}
}
return ret;
}
};
class GetTDefDefinition : public Definition {
virtual std::vector<ppi> parse(std::vector<std::vector<ppi>> args_a) {
std::vector<ppi> ret;
if (args_a.size() > 1) {
if (args_a[0].size() != 1) return ret;
if (args_a[0][0].type != ppi_t::Token) return ret;
unsigned short c = std::stoi(args_a[0][0].str);
args_a.erase(args_a.begin());
if (c >= args_a.size()) return ret;
ret = std::vector<ppi>(args_a[c].begin(),args_a[c].end());
}
return ret;
}
};
class HasArgsDefinition : public Definition {
virtual std::vector<ppi> parse(std::vector<std::vector<ppi>> args_a) {
std::vector<ppi> ret = {
ppi(ppi_t::ppIfdef,"ifdef"),
ppi(ppi_t::Token,"HAS_ARGS_CHK"),
ppi(ppi_t::ppEndExpr),
ppi(ppi_t::ppUndef,"undef"),
ppi(ppi_t::ppdef_name,"HAS_ARGS_CHK"),
ppi(ppi_t::ppEndif,"endif")
};
if (args_a.size() > 0) {
ret.push_back(ppi(ppi_t::ppDef,"define"));
ret.push_back(ppi(ppi_t::ppdef_name,"HAS_ARGS_CHK"));
ret.push_back(ppi(ppi_t::ppdef_value_start));
ret.push_back(ppi(ppi_t::ppdef_value_end));
}
return ret;
}
};
std::vector<std::string> codes;
std::vector<std::string> includestack;
std::vector<std::string> defstack;
struct IncludedAlready_t {
int64_t hash;
bool reuseavail = true;
IncludedAlready_t(int64_t hash) : hash(hash) {}
IncludedAlready_t() : hash(0) {}
IncludedAlready_t(const IncludedAlready_t& other) : hash(other.hash), reuseavail(other.reuseavail) {}
IncludedAlready_t& operator=(const IncludedAlready_t& other) {
hash = other.hash;
reuseavail = other.reuseavail;
return *this;
}
bool operator==(const IncludedAlready_t& other) const {
return hash == other.hash;
}
bool operator!=(const IncludedAlready_t& other) const {
return hash != other.hash;
}
bool operator==(int64_t other) const {
return hash == other;
}
bool operator!=(int64_t other) const {
return hash != other;
}
};
std::vector<IncludedAlready_t> includedalready;
enum class def_arg_stg : uint8_t {
waiting_start,
next_arg
};
std::unordered_map<std::string, Definition> definitions;
std::vector<ppi> def_args;
std::vector<pppi> uncommentedcode;
std::vector<ppi> code_interPPI;
std::vector<ppi> code_runPPI;
std::vector<ppi> code_afterPPI;
std::vector<ppi> code_numliteral;
AST code_stg6;
AST& current = code_stg6;
friend class blr_;
void ast_back() {
if (current.getParent() == nullptr) throw Exception("InvalidInternalOperation", "Attempted to do an AST back operation on root of tree.");
current = *(current.getParent());
}
void ast_forward() {
if (current.getChildren().size() == 0) throw Exception("InvalidInternalOperation", "Attempted to do an AST forward operation on an object without children");
current = current.getChildren().back();
}
void ast_forward_new() {
current = AST().setParent(¤t);
}
void ast_setInst(Instruction_t t) {
current.setType(t);
}
void ast_setCurrent(AST& a) {
current = a;
}
void ast_addAttr(std::string a) {
current.pushAttr(a);
}
void ast_setToken(std::string s) {
current.setToken(s);
}
void ast_new__(Instruction_t t, std::string s) {
ast_forward_new();
ast_setInst(t);
ast_setToken(s);
}
void ast_new___(Instruction_t t) {
ast_forward_new();
ast_setInst(t);
}
void ast_newString(std::string s) {
ast_new__(Instruction_t::String,s);
}
void ast_newChar(std::string s) {
ast_new__(Instruction_t::Char,s);
}
void ast_newToken(std::string s) {
ast_new__(Instruction_t::String,s);
}
void ast_newOperator(std::string s) {
ast_new__(Instruction_t::Operator,s);
}
void ast_newInteger(std::string s) {
ast_new__(Instruction_t::Integer,s);
}
void ast_newFloat(std::string s) {
ast_new__(Instruction_t::Float,s);
}
void ast_newClass(std::string s) {
ast_new__(Instruction_t::Class,s);
}
void ast_newThis() {
ast_new___(Instruction_t::This);
}
class Operators {
public:
struct op {
std::string opr;
signed short order;
std::vector<std::string> attr;
op(std::string o, signed short v) : opr(o), order(v) {}
op() = default;
bool isbraces() {
if (opr.size() == 2) {
if (opr == "()") return true;
if (opr == "[]") return true;
if (opr == "{}") return true;
}
return false;
}
};
std::vector<op> ops;
void push_back(const std::string& s) {
ops.push_back(op(s,0));
}
void push_back(const std::string& s, signed short v) {
ops.push_back(op(s,v));
}
void push_back(const std::string& s, signed short v, const std::vector<std::string>& a) {
ops.push_back(op(s, v));
op& c = ops.back();
for (auto& i : a) {
c.attr.push_back(i);
}
}
auto front() {
return ops.front();
}
auto back() {
return ops.back();
}
auto size() {
return ops.size();
}
auto begin() {
return ops.begin();
}
auto end() {
return ops.end();
} //allow looping
auto findbyattr(const std::string& c) {
for (auto& i : ops) {
if (i.attr.begin() != i.attr.end())
if (std::find(i.attr.begin(),i.attr.end(),c) != i.attr.end())
return i;
}
return op();
}
} def_ops;
using op = Operators::op;
struct temp_t {
std::string s;
unsigned long long l;
bool isstring = false;
temp_t(const std::string& s) : s(s), isstring(true) {}
temp_t(unsigned long long l) : l(l) {}
temp_t() = default;
} temp[4];
template <typename T>
static std::string str_code(const std::vector<T>& v) {
std::stringstream ss;
for (auto& i : v) {
ss << (std::string)i;
}
return ss.str();
}
struct IncludeFileOutput {
std::string code;
enum class MsgType : uint8_t {
OK,
NotFound,
AlreadyIncluded,
CyclicInclude
} msg;
uint64_t hash = 0;
};
IncludeFileOutput include1(const std::string& filename) {
IncludeFileOutput ret;
if (includestack.size() > 0) {
for (auto& i : includestack) {
if (i == filename) {
ret.msg = IncludeFileOutput::MsgType::CyclicInclude;
return ret;
}
}
}
std::ifstream file;
bool err = 1;
for (auto& path : includepath) {
std::string f = path + "/" + filename;
file.open(f);
if (file.good()) {
err = 0; // no error
break;
}
}
if (err) {
ret.msg = IncludeFileOutput::MsgType::NotFound;
return ret;
}
std::stringstream ss;
ss << file.rdbuf();
ret.code = ss.str();
uint64_t h = std::hash<std::string>()(ret.code);
ret.hash = h;
ret.msg = (std::find(includedalready.begin(), includedalready.end(), h) != includedalready.end()) ? IncludeFileOutput::MsgType::AlreadyIncluded : IncludeFileOutput::MsgType::OK;
return ret;
}
void includefile(const std::string& filename) {
IncludeFileOutput o = include1(filename);
switch (o.msg) {
case IncludeFileOutput::MsgType::OK:
goto includeanyway;
case IncludeFileOutput::MsgType::AlreadyIncluded:
if (std::find(includedalready.begin(), includedalready.end(), o.hash)->reuseavail) {
goto includeanyway;
} else {
logger.debug("Already included " + filename);
}
return;
case IncludeFileOutput::MsgType::CyclicInclude:
logger.fatal("Cyclic include " + filename);
throw Exception("CyclicInclude", "Cyclic include " + filename);
case IncludeFileOutput::MsgType::NotFound:
logger.fatal("Include file not found " + filename);
throw Exception("NotFoundInclude", "Include file not found " + filename);
default:
logger.fatal("Unknown case during include, memory corruption?");
abort();
}
includeanyway:
includestack.push_back(filename);
logger.debug("Begin compilation of " + filename);
build_stage_1(o.code);
build_stage_2();
build_stage_3();
build_stage_4();
includestack.pop_back();
logger.debug("End compilation of " + filename);
return;
}
public:
void addDef(const std::string& name, const std::vector<std::string>& args, const std::vector<ppi>& val) {
definitions[name] = Definition(args, val);
}
void addDef(const std::string& name, const std::initializer_list<std::string>& args, const std::initializer_list<ppi>& val) {
definitions[name] = Definition(args, val);
}
void addOp(const std::string& oper) {
def_ops.push_back(oper);
}
void addOp(const std::string& oper, const unsigned short v) {
def_ops.push_back(oper,v);
}
void addOp(const std::string& oper, const unsigned short v, std::vector<std::string> attr) {
def_ops.push_back(oper,v,attr);
}
void build_stage_1(std::string code) {
logger.debug("Uncommenting code and doing first token split...");
std::string buffer;
std::string buffer_esc;
unsigned char t = 0;
enum class bl {
str,
str_esc,
chr,
chr_esc,
none,
comment,
block_comment,
str_esc_bin,
str_esc_oct,
str_esc_hex,
chr_esc_bin,
chr_esc_oct,
chr_esc_hex
} building = bl::none;
code += '\n';
for (auto ch : code) {
switch (building) {
case bl::str:
case bl::chr:
if (ch == '\\')
building = (building == bl::str) ? bl::str_esc : bl::chr_esc;
else if (ch == '"' && building == bl::str)
goto token_break;
else if (ch == '\'' && building == bl::chr)
goto token_break;
else if (ch == '\n')
throw Exception("UnendedQuote","Unended quote for char/string literal.");
else
buffer += ch;
break;
case bl::str_esc:
case bl::chr_esc:
switch (ch) {
case 'n':
buffer += '\n';
building = (building == bl::str_esc) ? bl::str : bl::chr;
break;
case 't':
buffer += '\t';
building = (building == bl::str_esc) ? bl::str : bl::chr;
break;
case 'r':
buffer += '\r';
building = (building == bl::str_esc) ? bl::str : bl::chr;
break;
case '0':
case '1':
case '2':
case '3':
buffer_esc += ch;
t = 1;
building = (building == bl::str_esc) ? bl::str_esc_oct : bl::chr_esc_oct;
break;
case 'x':
t = 0;
building = (building == bl::str_esc) ? bl::str_esc_hex : bl::chr_esc_hex;
break;
case 'b':
t = 0;
building = (building == bl::str_esc) ? bl::str_esc_bin : bl::chr_esc_bin;
break;
default:
buffer += ch;
building = (building == bl::str_esc) ? bl::str : bl::chr;
break;
}
break;
case bl::str_esc_bin:
case bl::chr_esc_bin:
if (ch == '0' || ch == '1') {
buffer_esc += ch;
t++;
if (t == 8) {
buffer += (char)std::stoi(buffer_esc, nullptr, 2);
t = 0;
buffer_esc.clear();
building = (building == bl::str_esc_bin) ? bl::str : bl::chr;
}
} else {
throw Exception("InvalidBinEscape","Invalid binary escape.");
}
break;
case bl::str_esc_oct:
case bl::chr_esc_oct:
if (ch >= '0' && ch <= '7') {
buffer_esc += ch;
t++;
if (t == 3) {
buffer += (char)std::stoi(buffer_esc, nullptr, 8);
t = 0;
buffer_esc.clear();
building = (building == bl::str_esc_oct) ? bl::str : bl::chr;
}
} else {
throw Exception("InvalidOctEscape","Invalid octal escape.");
}
break;
case bl::str_esc_hex:
case bl::chr_esc_hex:
if ((ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F')) {
buffer_esc += ch;
t++;
if (t == 2) {
buffer += (char)std::stoi(buffer_esc, nullptr, 16);
t = 0;
buffer_esc.clear();
building = (building == bl::str_esc_hex) ? bl::str : bl::chr;
}
} else {
throw Exception("InvalidHexEscape","Invalid hexadecimal escape.");
}
break;
default:
switch (ch) {
case '\r':
break;
case ' ':
case '\t':
case '\n':
token_break:
if (buffer.size() > 0) {
pppi p(buffer);
switch (building) {
case bl::str:
p.type = pppi_t::String;
p.str += ch;
break;
case bl::chr:
p.type = pppi_t::Char;
p.str += ch;
break;
default:
break;
}
uncommentedcode.push_back(p);
buffer.clear();
if (ch == '\n') uncommentedcode.push_back(pppi(pppi_t::NewLine));
}
break;
case '/':
if (building == bl::none) {
if (buffer.back() == '/') {
building = bl::comment;
buffer.pop_back();
if (buffer.size() > 0) {
uncommentedcode.push_back(pppi(buffer));
buffer.clear();
}
} else if (buffer.back() == '*') {
building = bl::block_comment;
buffer.pop_back();
if (buffer.size() > 0) {
uncommentedcode.push_back(pppi(buffer));
buffer.clear();
}
} else {
buffer += ch;
}
} else if (building == bl::block_comment) {
if (buffer.back() == '*') {
building = bl::none;
buffer.clear();
} else {
buffer += ch;
}
} else {
buffer += ch;
}
break;
case '"':
if (building == bl::none) {
building = bl::str;
} else if (building == bl::str) {
building = bl::none;
uncommentedcode.push_back(pppi(pppi_t::String,buffer));
buffer.clear();
} else if (building == bl::str_esc) {
building = bl::str;
buffer += ch;
} else {
buffer += ch;
}
break;
case '\'':
if (building == bl::none) {
building = bl::chr;
} else if (building == bl::chr) {
building = bl::none;
uncommentedcode.push_back(pppi(pppi_t::Char,buffer));
buffer.clear();
} else if (building == bl::chr_esc) {
building = bl::chr;
buffer += ch;
} else {
buffer += ch;
}
break;
default:
buffer += ch;
break;
}
}
next:
(void)0;
}
logger.debug("Uncommenting code and doing first token split... done");
}
void build_stage_2() {
logger.debug("Splitting further...");
std::string buffer;
std::vector<pppi> oldcode = uncommentedcode;
uncommentedcode.clear();
enum class bl {
none,
ppi_,
def_block,
def_block_nl,
def_name,
def_args,
undef_name,
if_expr,
} building = bl::none;
enum class bl2 : uint8_t {
none,
oper,
tok,
} bbl2 = bl2::none;
#define flush_build2_buffer_compiler__comp_bl2_stg if (bbl2 != bl2::none) {\
ppi_t typep = (ppi_t)0;\
switch (bbl2) {\
case bl2::oper:\
typep = ppi_t::Operator;\
break;\
case bl2::tok:\
typep = ppi_t::Token;\
break;\
default:\
break;\
}\
code_interPPI.push_back(ppi(typep,buffer));\
buffer.clear();\
bbl2 = bl2::none;\
}
unsigned long char_index = 0;
for (auto& p : oldcode) {
statementReset:
if (char_index >= p.str.size() && p.type != pppi_t::NewLine) goto nextStatement;
switch (building) {
case bl::none:
switch (p.type) {
case pppi_t::NewLine:
flush_build2_buffer_compiler__comp_bl2_stg
goto nextStatement;
case pppi_t::Char:
flush_build2_buffer_compiler__comp_bl2_stg
code_interPPI.push_back(ppi(ppi_t::Char,p.str));
goto nextStatement;
case pppi_t::String:
flush_build2_buffer_compiler__comp_bl2_stg
code_interPPI.push_back(ppi(ppi_t::String,p.str));
goto nextStatement;
default: break;
}
while (char_index < p.str.size()) {
evalch:
char ch = *(p.str.begin() + char_index);
switch (ch) {
case '#':
if (buffer.empty()) {
building = bl::ppi_;
char_index++;
goto statementReset;
}
case '+':
case '-':
case '*':
case '/':
case '%':
case '^':
case '&':
case '|':
case '<':
case '>':
case '=':
case '!':
case '~':
case '?':
case ':':
case '$':
case '`':
case '@':
case '.':
if (bbl2 == bl2::tok) {
bbl2 = bl2::oper;
code_interPPI.push_back(ppi(ppi_t::Token,buffer));
buffer.clear();
buffer.push_back(ch);
}
break;
case ',':
case ';':
flush_build2_buffer_compiler__comp_bl2_stg
buffer.push_back(ch);
code_interPPI.push_back(ppi(ppi_t::Delimiter,buffer));
buffer.clear();
break;
case '\\':
throw Exception("UnexpectedCharacter","Unexpected escape character.");
case '(':
case '[':
case '{':
flush_build2_buffer_compiler__comp_bl2_stg
buffer.push_back(ch);
code_interPPI.push_back(ppi(ppi_t::groupStart,buffer));
buffer.clear();
break;
case ')':
case ']':
case '}':
flush_build2_buffer_compiler__comp_bl2_stg
buffer.push_back(ch);
code_interPPI.push_back(ppi(ppi_t::groupEnd,buffer));
buffer.clear();
break;
default:
if (bbl2 == bl2::oper) {
bbl2 = bl2::tok;
code_interPPI.push_back(ppi(ppi_t::Operator,buffer));
buffer.clear();
}
buffer.push_back(ch);
break;
}
char_index++;
}
break;
case bl::ppi_:
if (true){
if (p.type == pppi_t::NewLine)
throw Exception("UnexpectedNewLineInPreProcessor","");
else if (p.type == pppi_t::Char)
throw Exception("UnexpectedCharInPreProcessor","");
else if (p.type == pppi_t::String)
throw Exception("UnexpectedStringInPreProcessor","");
else if (*(p.str.begin() + char_index) == '#') {
building = bl::none;
char_index++;
code_interPPI.push_back(ppi(ppi_t::ppConcat,"##"));
goto statementReset;
} else {
ppi np = ppi::parseppi(p.str.substr(char_index));
code_interPPI.push_back(np);
switch (np.type) {
case ppi_t::ppDef:
building = bl::def_name;
goto nextStatement;
case ppi_t::ppUndef:
building = bl::undef_name;
goto nextStatement;
case ppi_t::ppIf:
case ppi_t::ppIfdef: