-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlexer.cpp
More file actions
2784 lines (2755 loc) · 194 KB
/
lexer.cpp
File metadata and controls
2784 lines (2755 loc) · 194 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
// split keyword into tokens(lexems)
// analyze tokens so that keywords match exatly as i expect
#include <memory>
#include <vector>
#include <tuple>
#include <fstream>
#include <cstring>
#include <cctype>
#include <climits>
#include "errors.h" // includes string
#include "cmds.h"
#include "hash_function.h"
#include "ops.h"
#define Addition 0
#define Subtraction 1
#define Multiplication 2
#define Division 3
#define Remainder 4
#define And 5
#define Or 6
#define Xor 7
#define LeftShift 8
#define RightShift 9
#define EQ 0
#define NEQ 1
#define L 2
#define LE 3
#define G 4
#define GE 5
#define Int8_t 0
#define Int16_t 1
#define Int32_t 2
#define Int64_t 3
#define String8_t 4
#define String16_t 5
#define String32_t 6
#define String64_t 7
#define RETURN 8
#define PRINT 9
#define PRINTLN 10
#define READ 11
#define GOTO 12
#define GOTO_IF 13
ErrorMsg ManualReport;
class Function_vals {
public:
bool Called(const std::string &name) const noexcept {
for (auto &p : func_values) {
if (std::get<0>(p) == name) { // example: 'func()' - correct syntax 'func ()' - not correct syntax
if (name.find(' ') != std::string::npos) {
ManualReport.ErrorID = 2;
ManualReport.Message = "Incorrect syntax for calling function";
ManualReport.PrintError(ManualReport);
return false;
}
else {
return true;
}
}
}
return false;
}
std::string Get_code(const std::string &name) const {
for (auto &p : func_values) {
if (std::get<0>(p) == name) {
return std::get<1>(p);
}
}
std::exit(2); // Critical error
return "";
}
std::string Get_Arguments(const std::string &name) const {
for (auto &p : func_values) {
if (std::get<0>(p) == name) {
return std::get<2>(p);
}
}
std::exit(2); // Critical error
return "";
}
void Add_function(const std::string &name, const std::string &code, const std::string &arguments) noexcept {
func_values.push_back(std::tuple <std::string, std::string, std::string> (name, code, arguments));
}
private:
// name code arguments
std::vector <std::tuple<std::string, std::string, std::string>> func_values;
}Functions;
class Variable_vals {
public:
std::string Get_value(const std::string &name) const {
for (auto &p : vars_values) {
if (std::get<0>(p) == name) {
return std::get<1>(p);
}
}
std::exit(2); // Critical error
return "";
}
char Get_size(const std::string &name) const {
for (auto &p : vars_values) {
if (std::get<0>(p) == name) {
return std::get<2>(p);
}
}
std::exit(2); // Critical error
return ' ';
}
bool Exists(const std::string &name) const noexcept {
for (auto &p : vars_values) {
if (std::get<0>(p) == name) {
return true;
}
}
return false;
}
void Change_value(const std::string &name, const std::string &value) noexcept {
for (auto &p : vars_values) {
if (std::get<0>(p) == name) {
std::get<1>(p) = value;
break;
}
}
}
void Add_value(const std::string &name, const std::string &value, const char &c) noexcept {
vars_values.push_back(std::tuple <std::string, std::string, char> (name, value, c));
}
void Clear(const std::vector <std::string> &&names = {}) noexcept {
bool not_remove = false;
if (names.empty()) {
vars_values.clear();
}
else {
for (size_t i = 0; i < vars_values.size(); ++i) {
for (const std::string &s : names) {
if (s == std::get<0>(vars_values[i])) {
not_remove = true;
break;
}
}
if (not_remove) {
not_remove = false;
}
else {
vars_values.erase(vars_values.begin() + i);
}
}
}
}
private:
// name value size
std::vector <std::tuple<std::string, std::string, char>> vars_values;
}Variables;
bool SplitIntoWords(const std::string &sentence, std::vector <std::string> &words) noexcept {
std::string word {};
for (size_t i = 0; i <= sentence.length(); ++i) { // looking a little bit further to determine end of the string
if ((std::isspace(sentence[i]) || sentence[i] == '\0') && !word.empty()) {
if (std::isblank(sentence[i]) && (i + 1 < sentence.length()) && std::isblank(sentence[i+1])) {
ManualReport.ErrorID = 1;
ManualReport.Message = "Cannot have more then 1 space";
ManualReport.PrintError(ManualReport);
return false;
}
words.push_back(word);
word.assign(word.length(), '\0');
word.clear();
}
else if (std::isgraph(sentence[i]) && !std::iscntrl(sentence[i])) {
word += sentence[i];
}
}
return true;
}
bool IsInList(const char *input) noexcept {
for (const char *cs : Commands) {
if (strcmp(input, cs) == 0) {
return true;
}
}
return false;
}
bool IsDigit(const std::string &input) noexcept {
if (input.empty()) {
return false;
}
for (const char &c : input) {
if (!std::isdigit(c)) {
return false;
}
}
return true;
}
bool IsString(const std::string &input) noexcept {
if (input.empty()) {
return false;
}
if (IsDigit(input)) {
return false;
}
for (const char &c : input) {
if (!std::isalnum(c)) {
return false;
}
}
return true;
}
bool IsSpecialString(const std::string &input) noexcept {
if (input.front() != '\"' || input.back() != '\"') {
return false;
}
return true;
}
bool IsVariable(const std::string &input) noexcept {
if (input.front() != '(' || input.back() != ')') {
return false;
}
return true;
}
bool IsCorrectArguments(const std::string &arguments) noexcept {
if (arguments == " ") {
return true;
}
std::vector <std::string> vars {};
std::string word {};
for (size_t i = 0; i < arguments.length(); ++i) {
if (arguments[i] == ',' && std::isblank(arguments[i+1]) && i+1 < arguments.length()) {
vars.push_back(word);
word.assign(word.length(), '\0');
word.clear();
++i; // skip space
}
else {
word += arguments[i];
}
}
vars.push_back(word); // adding last word
if (vars.size() > 1 && arguments.find(',') != std::string::npos) {
return true;
}
else if (vars.size() == 1 && arguments.find(',') == std::string::npos && arguments.find(' ') == std::string::npos) {
return true;
}
return false;
}
bool IsCorrectSyntax(const std::string &input) noexcept {
bool found_chars = false;
for (const char &c : input) {
if (std::isblank(c) && found_chars == false) {
return false;
}
else if (std::isblank(c) && found_chars) {
found_chars = false;
}
else if (std::isalnum(c)) {
found_chars = true;
}
}
return true;
}
bool FindStart(std::unique_ptr <char[]> &buffer) {
std::string fun_name {};
std::string arguments {};
bool wrong_place = false;
bool foundcb = false;
bool found_pars = false;
size_t first_pos {0};
size_t last_pos {0};
for (size_t i = 0; buffer[i] != '\0'; ++i) {
if (buffer[i] == '(' && (i+1 < strlen(buffer.get())) && found_pars == false) {
for (size_t j = 0; j < fun_name.length(); ++j) {
if (std::isblank(fun_name[j]) && std::isalnum(fun_name[j+1]) && j+1 < fun_name.length()) {
wrong_place = true;
break;
}
}
if (wrong_place) {
ManualReport.ErrorID = 1;
ManualReport.Message = "Parenthesis for function is not found";
ManualReport.PrintError(ManualReport);
return false;
}
if (buffer[i+1] != ')') {
++i; // skip to what after bracket
while(buffer[i] != '\n' && buffer[i] != '\v' && i < strlen(buffer.get())) {
if (buffer[i] == ')') {
found_pars = true;
break;
}
arguments += buffer[i];
++i;
}
if (found_pars == false) {
break;
}
}
else {
arguments = " ";
found_pars = true;
}
}
else if (buffer[i] == ')' && found_pars == false) {
fun_name = "start "; // needed so you get correct error message
break;
}
else if (buffer[i] == '{' && (i+1 < strlen(buffer.get())) && foundcb == false && found_pars) {
size_t j = 1;
while (i+j <= strlen(buffer.get()) && std::iscntrl(buffer[i+j])) {
++j;
}
first_pos = i + j;
while(buffer[i] != '\0') {
if (buffer[i] == '}') {
foundcb = true;
last_pos = i - 1;
break;
}
++i;
}
if (foundcb == false) {
break;
}
std::string code {};
for (size_t t = 0; t < (last_pos - first_pos + 1); ++t) {
code += buffer[first_pos + t];
}
if (fun_name == "start ") {
if (arguments != " ") {
ManualReport.ErrorID = 1;
ManualReport.Message = "Start function cannot have arguments";
ManualReport.PrintError(ManualReport);
return false;
}
break;
}
if (IsCorrectSyntax(fun_name) == false) {
ManualReport.ErrorID = 1;
ManualReport.Message = "Functions can only have 1 space";
ManualReport.PrintError(ManualReport);
return false;
}
if (IsCorrectArguments(arguments) == false) {
ManualReport.ErrorID = 1;
ManualReport.Message = "Incorrect syntax for arguments declaration";
ManualReport.PrintError(ManualReport);
return false;
}
// (substr working not inclusive last character)
Functions.Add_function(fun_name.substr(0, fun_name.length()-1), code, arguments); // was 'func ' become 'func' after substr
fun_name.assign(fun_name.length(), '\0');
fun_name.clear();
code.assign(code.length(), '\0');
code.clear();
foundcb = false;
found_pars = false;
wrong_place = false;
arguments.assign(arguments.length(), '\0');
arguments.clear();
first_pos = 0;
last_pos = 0;
continue;
}
else if (buffer[i] == '}' && foundcb == false) {
break;
}
// Look into it if new version of this have bugs, if so can change to arguments.empty()
else if (found_pars == false && !std::iscntrl(buffer[i])) { // not done !std::isspace because of syntax
fun_name += buffer[i];
}
}
if (fun_name == "start " && found_pars && foundcb) {
std::unique_ptr <char[]> copy_buffer = std::make_unique <char[]> (last_pos - first_pos + 2); // +2(+1 cuz of index, +1 cuz of end character)
size_t j = 0;
for (; j <= (last_pos - first_pos); ++j) {
copy_buffer[j] = buffer[first_pos + j];
}
copy_buffer[j] = '\0';
buffer.reset();
buffer = std::make_unique <char[]> (last_pos - first_pos + 2);
strcpy(buffer.get(), copy_buffer.get());
}
else if (fun_name != "start ") {
ManualReport.ErrorID = 1;
ManualReport.Message = "Start function not found or written incorrectly";
ManualReport.PrintError(ManualReport);
return false;
}
else if (found_pars == false) {
ManualReport.ErrorID = 1;
ManualReport.Message = "Function doesn't have parenthesis";
ManualReport.PrintError(ManualReport);
return false;
}
else {
ManualReport.ErrorID = 1;
ManualReport.Message = "Function doesn't have curly braces";
ManualReport.PrintError(ManualReport);
return false;
}
return true;
}
bool Tokenize(const std::unique_ptr <char[]> &buffer, std::string &nasm_vars, std::string &nasm_arrs, std::string &nasm_cmds) {
bool isfirst = true;
std::vector <std::string> words {};
std::string sentence {};
for (size_t t = 0; buffer[t] != '\0'; ++t) {
if (buffer[t] == '\n' || buffer[t] == '\v' || buffer[t] == '\0') {
sentence += '\0';
if (SplitIntoWords(sentence, words) == false) {
return false;
}
if (words.empty()) {
sentence.assign(sentence.length(), '\0');
sentence.clear();
continue;
}
// Main loop
auto check_loop = [&]() -> bool {
for (const char *s : Commands) {
if (strcmp(words[0].c_str(), s) < 0 || strcmp(words[0].c_str(), s) > 0) {
if (IsInList(words[0].c_str())) {
continue;
}
else if (Variables.Exists(words[0])) {
if (words.size() == 3) {
if (IsDigit(Variables.Get_value(words[0])) && IsDigit(words[2])) {
switch (HashFunc(words[1].c_str())) {
case HashFunc("="): {
nasm_cmds += "\tmov rax, " + words[2] + "\n";
nasm_cmds += "\tmov [rel " + words[0] + "], rax\n";
Variables.Change_value(words[0], words[2]);
return true;
}
case HashFunc(AssignOP[Addition]): {
nasm_cmds += "\tmov rax, [rel " + words[0] + "]\n";
nasm_cmds += "\tadd rax, " + words[2] + "\n";
nasm_cmds += "\tmov [rel " + words[0] + "], rax\n";
Variables.Change_value(words[0], std::to_string(std::stoull(Variables.Get_value(words[0])) + std::stoull(words[2])));
return true;
}
case HashFunc(AssignOP[Subtraction]): {
nasm_cmds += "\tmov rax, [rel " + words[0] + "]\n";
nasm_cmds += "\tsub rax, " + words[2] + "\n";
nasm_cmds += "\tmov [rel " + words[0] + "], rax\n";
Variables.Change_value(words[0], std::to_string(std::stoull(Variables.Get_value(words[0])) - std::stoull(words[2])));
return true;
}
case HashFunc(AssignOP[Multiplication]): {
nasm_cmds += "\tmov rax, [rel " + words[0] + "]\n";
nasm_cmds += "\timul rax, " + words[2] + "\n";
nasm_cmds += "\tmov [rel " + words[0] + "], rax\n";
Variables.Change_value(words[0], std::to_string(std::stoull(Variables.Get_value(words[0])) * std::stoull(words[2])));
return true;
}
case HashFunc(AssignOP[Division]): {
nasm_cmds += "\tmov rax, [rel " + words[0] + "]\n";
nasm_cmds += "\tmov rbx, " + words[2] + "\n";
nasm_cmds += "\txor rdx, rdx\n";
nasm_cmds += "\tdiv rbx\n";
nasm_cmds += "\tmov [rel " + words[0] + "], rax\n"; // quotient in rax, remainder in rdx
Variables.Change_value(words[0], std::to_string(std::stoull(Variables.Get_value(words[0])) / std::stoull(words[2])));
return true;
}
case HashFunc(AssignOP[Remainder]): {
nasm_cmds += "\tmov rax, [rel " + words[0] + "]\n";
nasm_cmds += "\tmov rbx, " + words[2] + "\n";
nasm_cmds += "\txor rdx, rdx\n";
nasm_cmds += "\tdiv rbx\n";
nasm_cmds += "\tmov [rel " + words[0] + "], rdx\n"; // quotient in rax, remainder in rdx
Variables.Change_value(words[0], std::to_string(std::stoull(Variables.Get_value(words[0])) % std::stoull(words[2])));
return true;
}
case HashFunc(AssignOP[And]): {
nasm_cmds += "\tmov rax, [rel " + words[0] + "]\n";
nasm_cmds += "\tand rax, " + words[2] + "\n";
nasm_cmds += "\tmov [rel " + words[0] + "], rax\n";
Variables.Change_value(words[0], std::to_string(std::stoull(Variables.Get_value(words[0])) & std::stoull(words[2])));
return true;
}
case HashFunc(AssignOP[Or]): {
nasm_cmds += "\tmov rax, [rel " + words[0] + "]\n";
nasm_cmds += "\tor rax, " + words[2] + "\n";
nasm_cmds += "\tmov [rel " + words[0] + "], rax\n";
Variables.Change_value(words[0], std::to_string(std::stoull(Variables.Get_value(words[0])) | std::stoull(words[2])));
return true;
}
case HashFunc(AssignOP[Xor]): {
nasm_cmds += "\tmov rax, [rel " + words[0] + "]\n";
nasm_cmds += "\txor rax, " + words[2] + "\n";
nasm_cmds += "\tmov [rel " + words[0] + "], rax\n";
Variables.Change_value(words[0], std::to_string(std::stoull(Variables.Get_value(words[0])) ^ std::stoull(words[2])));
return true;
}
case HashFunc(AssignOP[LeftShift]): {
switch (Variables.Get_size(words[0])) {
case 'b': {
nasm_cmds += "\tmov al, [rel " + words[0] + "]\n";
nasm_cmds += "\tshl al, " + words[2] + "\n";
nasm_cmds += "\tmov [rel " + words[0] + "], al\n";
Variables.Change_value(words[0], std::to_string(std::stoull(Variables.Get_value(words[0])) << std::stoull(words[2])));
break;
}
case 'w': {
nasm_cmds += "\tmov ah, [rel " + words[0] + "]\n";
nasm_cmds += "\tshl ah, " + words[2] + "\n";
nasm_cmds += "\tmov [rel " + words[0] + "], ah\n";
Variables.Change_value(words[0], std::to_string(std::stoull(Variables.Get_value(words[0])) << std::stoull(words[2])));
break;
}
case 'd': {
nasm_cmds += "\tmov eax, [rel " + words[0] + "]\n";
nasm_cmds += "\tshl eax, " + words[2] + "\n";
nasm_cmds += "\tmov [rel " + words[0] + "], eax\n";
Variables.Change_value(words[0], std::to_string(std::stoull(Variables.Get_value(words[0])) << std::stoull(words[2])));
break;
}
case 'q': {
nasm_cmds += "\tmov rax, [rel " + words[0] + "]\n";
nasm_cmds += "\tshl rax, " + words[2] + "\n";
nasm_cmds += "\tmov [rel " + words[0] + "], rax\n";
Variables.Change_value(words[0], std::to_string(std::stoull(Variables.Get_value(words[0])) << std::stoull(words[2])));
break;
}
}
return true;
}
case HashFunc(AssignOP[RightShift]): {
switch (Variables.Get_size(words[0])) {
case 'b': {
nasm_cmds += "\tmov al, [rel " + words[0] + "]\n";
nasm_cmds += "\tshr al, " + words[2] + "\n";
nasm_cmds += "\tmov [rel " + words[0] + "], al\n";
Variables.Change_value(words[0], std::to_string(std::stoull(Variables.Get_value(words[0])) >> std::stoull(words[2])));
break;
}
case 'w': {
nasm_cmds += "\tmov ah, [rel " + words[0] + "]\n";
nasm_cmds += "\tshr ah, " + words[2] + "\n";
nasm_cmds += "\tmov [rel " + words[0] + "], ah\n";
Variables.Change_value(words[0], std::to_string(std::stoull(Variables.Get_value(words[0])) >> std::stoull(words[2])));
break;
}
case 'd': {
nasm_cmds += "\tmov eax, [rel " + words[0] + "]\n";
nasm_cmds += "\tshr eax, " + words[2] + "\n";
nasm_cmds += "\tmov [rel " + words[0] + "], eax\n";
Variables.Change_value(words[0], std::to_string(std::stoull(Variables.Get_value(words[0])) >> std::stoull(words[2])));
break;
}
case 'q': {
nasm_cmds += "\tmov rax, [rel " + words[0] + "]\n";
nasm_cmds += "\tshr rax, " + words[2] + "\n";
nasm_cmds += "\tmov [rel " + words[0] + "], rax\n";
Variables.Change_value(words[0], std::to_string(std::stoull(Variables.Get_value(words[0])) >> std::stoull(words[2])));
break;
}
}
return true;
}
default: {
ManualReport.ErrorID = 1;
ManualReport.Message = "Invalid operator: " + words[1];
ManualReport.PrintError(ManualReport);
return false;
}
}
}
else if(IsSpecialString(Variables.Get_value(words[0])) && words[1] == "=" && IsSpecialString(words[2])) {
if ((strlen(Variables.Get_value(words[0]).c_str())-2) >= (strlen(words[2].c_str())-2)) { // -2 cause of "" characters
nasm_cmds += "\tmov rax, " + words[2] + "\n";
nasm_cmds += "\tmov [rel " + words[0] + "], rax\n";
Variables.Change_value(words[0], words[2]);
return true;
}
else {
ManualReport.ErrorID = 1;
ManualReport.Message = "String variable cannot have larger size then already declared";
ManualReport.PrintError(ManualReport);
return false;
}
}
else if (IsDigit(Variables.Get_value(words[0])) && Variables.Exists(words[2])) {
if (IsSpecialString(Variables.Get_value(words[2]))) { // this works because variables need to be with quotes("") in initialization and assign operations
ManualReport.ErrorID = 1;
ManualReport.Message = "There can be no operations between integer variable and string variable";
ManualReport.PrintError(ManualReport);
return false;
}
switch (HashFunc(words[1].c_str())) {
case HashFunc("="): {
nasm_cmds += "\tmov rax, [rel " + words[2] + "]\n";
nasm_cmds += "\tmov [rel " + words[0] + "], rax\n";
Variables.Change_value(words[0], Variables.Get_value(words[2]));
return true;
}
case HashFunc(AssignOP[Addition]): {
nasm_cmds += "\tmov rax, [rel " + words[0] + "]\n";
nasm_cmds += "\tmov rbx, [rel " + words[2] + "]\n";
nasm_cmds += "\tadd rax, rbx\n";
nasm_cmds += "\tmov [rel " + words[0] + "], rax\n";
Variables.Change_value(words[0], std::to_string(std::stoull(Variables.Get_value(words[0])) + std::stoull(Variables.Get_value(words[2]))));
return true;
}
case HashFunc(AssignOP[Subtraction]): {
nasm_cmds += "\tmov rax, [rel " + words[0] + "]\n";
nasm_cmds += "\tmov rbx, [rel " + words[2] + "]\n";
nasm_cmds += "\tsub rax, rbx\n";
nasm_cmds += "\tmov [rel " + words[0] + "], rax\n";
Variables.Change_value(words[0], std::to_string(std::stoull(Variables.Get_value(words[0])) - std::stoull(Variables.Get_value(words[2]))));
return true;
}
case HashFunc(AssignOP[Multiplication]): {
nasm_cmds += "\tmov rax, [rel " + words[0] + "]\n";
nasm_cmds += "\tmov rbx, [rel " + words[2] + "]\n";
nasm_cmds += "\timul rax, rbx\n";
nasm_cmds += "\tmov [rel " + words[0] + "], rax\n";
Variables.Change_value(words[0], std::to_string(std::stoull(Variables.Get_value(words[0])) * std::stoull(Variables.Get_value(words[2]))));
return true;
}
case HashFunc(AssignOP[Division]): {
nasm_cmds += "\tmov rax, [rel " + words[0] + "]\n";
nasm_cmds += "\tmov rbx, [rel " + words[2] + "]\n";
nasm_cmds += "\txor rdx, rdx\n";
nasm_cmds += "\tdiv rbx\n";
nasm_cmds += "\tmov [rel " + words[0] + "], rax\n"; // quotient in rax, remainder in rdx
Variables.Change_value(words[0], std::to_string(std::stoull(Variables.Get_value(words[0])) / std::stoull(Variables.Get_value(words[2]))));
return true;
}
case HashFunc(AssignOP[Remainder]): {
nasm_cmds += "\tmov rax, [rel " + words[0] + "]\n";
nasm_cmds += "\tmov rbx, [rel " + words[2] + "]\n";
nasm_cmds += "\txor rdx, rdx\n";
nasm_cmds += "\tdiv rbx\n";
nasm_cmds += "\tmov [rel " + words[0] + "], rdx\n"; // quotient in rax, remainder in rdx
Variables.Change_value(words[0], std::to_string(std::stoull(Variables.Get_value(words[0])) % std::stoull(Variables.Get_value(words[2]))));
return true;
}
case HashFunc(AssignOP[And]): {
nasm_cmds += "\tmov rax, [rel " + words[0] + "]\n";
nasm_cmds += "\tmov rbx, [rel " + words[2] + "]\n";
nasm_cmds += "\tand rax, rbx\n";
nasm_cmds += "\tmov [rel " + words[0] + "], rax\n";
Variables.Change_value(words[0], std::to_string(std::stoull(Variables.Get_value(words[0])) & std::stoull(Variables.Get_value(words[2]))));
return true;
}
case HashFunc(AssignOP[Or]): {
nasm_cmds += "\tmov rax, [rel " + words[0] + "]\n";
nasm_cmds += "\tmov rbx, [rel " + words[2] + "]\n";
nasm_cmds += "\tor rax, rbx\n";
nasm_cmds += "\tmov [rel " + words[0] + "], rax\n";
Variables.Change_value(words[0], std::to_string(std::stoull(Variables.Get_value(words[0])) | std::stoull(Variables.Get_value(words[2]))));
return true;
}
case HashFunc(AssignOP[Xor]): {
nasm_cmds += "\tmov rax, [rel " + words[0] + "]\n";
nasm_cmds += "\tmov rbx, [rel " + words[2] + "]\n";
nasm_cmds += "\txor rax, rbx\n";
nasm_cmds += "\tmov [rel " + words[0] + "], rax\n";
Variables.Change_value(words[0], std::to_string(std::stoull(Variables.Get_value(words[0])) ^ std::stoull(Variables.Get_value(words[2]))));
return true;
}
case HashFunc(AssignOP[LeftShift]): {
if (Variables.Get_size(words[0]) != 'b' || Variables.Get_size(words[2]) != 'b') {
ManualReport.ErrorID = 1;
ManualReport.Message = "Unfortunately, you cannot bit shift on non-byte integer variable";
ManualReport.PrintError(ManualReport);
return false;
}
nasm_cmds += "\tmov al, [rel " + words[0] + "]\n";
nasm_cmds += "\tmov cl, [rel " + words[2] + "]\n";
nasm_cmds += "\tshl al, cl\n";
nasm_cmds += "\tmov [rel " + words[0] + "], al\n";
Variables.Change_value(words[0], std::to_string(std::stoull(Variables.Get_value(words[0])) << std::stoull(Variables.Get_value(words[2]))));
return true;
}
case HashFunc(AssignOP[RightShift]): {
if (Variables.Get_size(words[0]) != 'b' || Variables.Get_size(words[2]) != 'b') {
ManualReport.ErrorID = 1;
ManualReport.Message = "Unfortunately, you cannot bit shift on non-byte integer variable";
ManualReport.PrintError(ManualReport);
return false;
}
nasm_cmds += "\tmov al, [rel " + words[0] + "]\n";
nasm_cmds += "\tmov cl, [rel " + words[2] + "]\n";
nasm_cmds += "\tshr al, cl\n";
nasm_cmds += "\tmov [rel " + words[0] + "], al\n";
Variables.Change_value(words[0], std::to_string(std::stoull(Variables.Get_value(words[0])) >> std::stoull(Variables.Get_value(words[2]))));
return true;
}
default: {
ManualReport.ErrorID = 1;
ManualReport.Message = "Invalid operator: " + words[1];
ManualReport.PrintError(ManualReport);
return false;
}
}
}
else {
ManualReport.ErrorID = 1;
ManualReport.Message = "Invalid operation with variable";
ManualReport.PrintError(ManualReport);
return false;
}
}
else if (words.size() > 3) {
if (IsSpecialString(Variables.Get_value(words[0])) && words[1] == "=") {
if (words[words.size()-1] != "\"" || words[2] != "\"") {
ManualReport.ErrorID = 1;
ManualReport.Message = "Incorrect variable operation syntax";
ManualReport.PrintError(ManualReport);
return false;
}
size_t j = 3;
std::string full_word {};
while(words[j] != "\"" && j < words.size()-1) {
full_word += words[j];
if ((j+1) != words.size()-1) {
full_word += ' ';
}
++j;
}
words[2] = '\"' + full_word + '\"';
words.resize(3);
full_word.assign(full_word.length(), '\0');
full_word.clear();
nasm_cmds += "\tmov rax, " + words[2] + "\n";
nasm_cmds += "\tmov [rel " + words[0] + "], rax\n";
Variables.Change_value(words[0], words[2]);
return true;
}
else {
ManualReport.ErrorID = 1;
ManualReport.Message = "Too many arguments for operation with digit variable";
ManualReport.PrintError(ManualReport);
return false;
}
}
else {
ManualReport.ErrorID = 1;
ManualReport.Message = "Too few arguments for variable operation";
ManualReport.PrintError(ManualReport);
return false;
}
}
else if (words[0].find('(') != std::string::npos && Functions.Called(words[0].substr(0, words[0].find('(')))) {
std::string arguments {};
if (words[0].find(')') == std::string::npos) {
for (size_t i = 0; i < words.size(); ++i) {
arguments += words[i];
if (i+1 != words.size()) {
arguments += " ";
}
}
if (arguments.find(')') == std::string::npos) {
ManualReport.ErrorID = 1;
ManualReport.Message = "Second parenthesis not found";
ManualReport.PrintError(ManualReport);
return false;
}
arguments.erase(0, arguments.find('(')+1); // removes all behind '(' including bracket itself
arguments.erase(arguments.length()-1, 1); // removes ')'
}
else {
arguments = " ";
}
words[0].erase(words[0].find('('), words[0].length()); // removes all except function name
std::string func_arguments = Functions.Get_Arguments(words[0]);
nasm_cmds += words[0] + ": \n";
std::string temp1 = Functions.Get_code(words[0]);
std::unique_ptr <char[]> temp2 = std::make_unique <char[]> (temp1.length() + 1);
std::vector <std::string> clear_arguments {};
strcpy(temp2.get(), temp1.c_str());
if (!IsCorrectArguments(arguments)) {
ManualReport.ErrorID = 1;
ManualReport.Message = "Incorrect syntax for passing arguments";
ManualReport.PrintError(ManualReport);
return false;
}
else if (arguments != func_arguments) {
ManualReport.ErrorID = 1;
ManualReport.Message = "The arguments that you are provided, must match with the arguments defined in the function declaration";
ManualReport.PrintError(ManualReport);
return false;
}
if (func_arguments != " ") { // i did that, because it will be bigger code if i do with .empty() case
std::string::size_type pos = func_arguments.find(',');
if (pos == std::string::npos) {
clear_arguments.push_back(func_arguments);
}
while (pos != std::string::npos) {
func_arguments.erase(pos, 1);
pos = func_arguments.find(',');
}
for (size_t i = 0; i <= func_arguments.length(); ++i) { // looking a little bit further to determine end of the string
if (std::isblank(func_arguments[i])) {
clear_arguments.push_back(func_arguments.substr(0, i));
func_arguments.erase(0, i+1);
i = 0;
}
else if (func_arguments[i] == '\0') {
clear_arguments.push_back(func_arguments.substr(0, i));
break;
}
}
for (std::string &s : clear_arguments) {
if (!Variables.Exists(s)) {
ManualReport.ErrorID = 1;
ManualReport.Message = "A non-existent variable has been passed";
ManualReport.PrintError(ManualReport);
return false;
}
}
Variables.Clear(std::move(clear_arguments)); // this saves arguments only that are passed, by default clear all
}
else {
Variables.Clear();
}
if (Tokenize(temp2, nasm_vars, nasm_arrs, nasm_cmds)) {
return true;
}
else {
return false;
}
}
else {
ManualReport.ErrorID = 1;
ManualReport.Message = "Unknown command: " + words[0];
ManualReport.PrintError(ManualReport);
return false;
}
}
else {
switch (HashFunc(words[0].c_str())) {
case HashFunc(Commands[Int8_t]): {
if (words.size() == 4) {
if (IsString(words[1]) && words[2] == "=" && IsDigit(words[3]) && !Variables.Exists(words[1])) {
if (std::stoull(words[3]) <= UCHAR_MAX) {
nasm_vars += "\t" + words[1] + ": db " + words[3] + "\n";
Variables.Add_value(words[1], words[3], 'b');
return true;
}
else {
ManualReport.ErrorID = 1;
ManualReport.Message = "Size overflow";
ManualReport.PrintError(ManualReport);
return false;
}
}
else {
ManualReport.ErrorID = 1;
ManualReport.Message = "Incorrect variable syntax";
ManualReport.PrintError(ManualReport);
return false;
}
}
else if (words.size() > 4) {
if (IsString(words[1]) && words[2] == "=" && !Variables.Exists(words[1])) {
long long counter = 0;
isfirst = true;
for (size_t j = 3; j < words.size() - 1; ++j) { // -1 needed cuz of we go out the scope if we do so
if (isfirst) {
if (IsDigit(words[j-1]) && IsDigit(words[j+1]) && (j+1 < words.size())) {
switch (HashFunc(words[j].c_str())) {
case HashFunc(Operator[Addition]): {
counter = std::stoi(words[j-1]) + std::stoi(words[j+1]);
isfirst = false;
break;
}
case HashFunc(Operator[Subtraction]): {
counter = std::stoi(words[j-1]) - std::stoi(words[j+1]);
isfirst = false;
break;
}
case HashFunc(Operator[Multiplication]): {
counter = std::stoi(words[j-1]) * std::stoi(words[j+1]);
isfirst = false;
break;
}
case HashFunc(Operator[Division]): {
counter = std::stoi(words[j-1]) / std::stoi(words[j+1]);
isfirst = false;
break;
}
case HashFunc(Operator[Remainder]): {
counter = std::stoi(words[j-1]) % std::stoi(words[j+1]);
isfirst = false;
break;
}
case HashFunc(Operator[And]): {
counter = std::stoi(words[j-1]) & std::stoi(words[j+1]);
isfirst = false;
break;
}
case HashFunc(Operator[Or]): {
counter = std::stoi(words[j-1]) | std::stoi(words[j+1]);
isfirst = false;
break;
}
case HashFunc(Operator[Xor]): {
counter = std::stoi(words[j-1]) ^ std::stoi(words[j+1]);
isfirst = false;
break;
}
case HashFunc(Operator[LeftShift]): {
counter = std::stoi(words[j-1]) << std::stoi(words[j+1]);
isfirst = false;
break;
}
case HashFunc(Operator[RightShift]): {
counter = std::stoi(words[j-1]) >> std::stoi(words[j+1]);
isfirst = false;
break;
}
default: {
ManualReport.ErrorID = 1;
ManualReport.Message = "Incorrect operation with integer variable";
ManualReport.PrintError(ManualReport);
return false;
}
}
}
}
else {
if (IsDigit(words[j+1]) && (j+1 < words.size())) {
switch (HashFunc(words[j].c_str())) {
case HashFunc(Operator[Addition]): {
counter += std::stoi(words[j+1]);
break;
}
case HashFunc(Operator[Subtraction]): {
counter -= std::stoi(words[j+1]);
break;
}
case HashFunc(Operator[Multiplication]): {
counter *= std::stoi(words[j+1]);
break;
}
case HashFunc(Operator[Division]): {
counter /= std::stoi(words[j+1]);
break;
}
case HashFunc(Operator[Remainder]): {
counter %= std::stoi(words[j+1]);
break;
}
case HashFunc(Operator[And]): {
counter &= std::stoi(words[j+1]);
break;
}
case HashFunc(Operator[Or]): {
counter |= std::stoi(words[j+1]);
break;
}
case HashFunc(Operator[Xor]): {
counter ^= std::stoi(words[j+1]);
break;
}
case HashFunc(Operator[LeftShift]): {
counter <<= std::stoi(words[j+1]);
break;
}
case HashFunc(Operator[RightShift]): {
counter >>= std::stoi(words[j+1]);
break;
}
default: {
ManualReport.ErrorID = 1;
ManualReport.Message = "Incorrect operation with integer variable";
ManualReport.PrintError(ManualReport);
return false;
}
}
}
}
}
if (counter > CHAR_MAX) {
ManualReport.ErrorID = 1;
ManualReport.Message = "Size overflow";
ManualReport.PrintError(ManualReport);
return false;
}
nasm_vars += "\t" + words[1] + ": db " + std::to_string(counter) + "\n";
Variables.Add_value(words[1], std::to_string(counter), 'b');
return true;
}
else {
ManualReport.ErrorID = 1;
ManualReport.Message = "Incorrect variable syntax";