3std::string
Poliz::ReplaceAll(std::string str,
const std::string& from,
const std::string& to) {
5 while ((start_pos = str.find(from, start_pos)) != std::string::npos) {
6 str.replace(start_pos, from.length(), to);
7 start_pos += to.length();
33 rnd = std::mt19937((std::chrono::high_resolution_clock::now().time_since_epoch().count() + 13));
37 for (
int i = 0; i <
lexes.size(); ++i) {
39 delete lexes[i].second;
62 return new bool(*((
bool*)op.second));
65 return new uint8_t(*((
bool*)op.second));
68 return new char(*((
bool*)op.second));
71 return new double(*((
bool*)op.second));
74 return new float(*((
bool*)op.second));
77 return new int(*((
bool*)op.second));
80 return new long long(*((
bool*)op.second));
83 return new short(*((
bool*)op.second));
86 return new std::string(*((
bool*)op.second) ?
"true" :
"false");
89 return new uint32_t(*((
bool*)op.second));
92 return new uint64_t(*((
bool*)op.second));
95 return new large(*((
bool*)op.second));
102 return new bool(*((uint8_t*)op.second));
105 return new uint8_t(*((uint8_t*)op.second));
108 return new char(*((uint8_t*)op.second));
111 return new double(*((uint8_t*)op.second));
114 return new float(*((uint8_t*)op.second));
117 return new int(*((uint8_t*)op.second));
120 return new long long(*((uint8_t*)op.second));
123 return new short(*((uint8_t*)op.second));
126 return new std::string(std::to_string(*((uint8_t*)op.second)));
129 return new uint32_t(*((uint8_t*)op.second));
132 return new uint64_t(*((uint8_t*)op.second));
135 return new large(*((uint8_t*)op.second));
142 return new bool(*((
char*)op.second));
145 return new uint8_t(*((
char*)op.second));
148 return new char(*((
char*)op.second));
151 return new double(*((
char*)op.second));
154 return new float(*((
char*)op.second));
157 return new int(*((
char*)op.second));
160 return new long long(*((
char*)op.second));
163 return new short(*((
char*)op.second));
166 return new std::string(1, *((
char*)op.second));
169 return new uint32_t(*((
char*)op.second));
172 return new uint64_t(*((
char*)op.second));
175 return new large(*((
char*)op.second));
182 return new bool(*((
double*)op.second));
185 return new uint8_t(*((
double*)op.second));
188 return new char(*((
double*)op.second));
191 return new double(*((
double*)op.second));
194 return new float(*((
double*)op.second));
197 return new int(*((
double*)op.second));
200 return new long long(*((
double*)op.second));
203 return new short(*((
double*)op.second));
206 return new std::string(std::to_string(*((
double*)op.second)));
209 return new uint32_t(*((
double*)op.second));
212 return new uint64_t(*((
double*)op.second));
215 return new large(*((
double*)op.second));
222 return new bool(*((
float*)op.second));
225 return new uint8_t(*((
float*)op.second));
228 return new char(*((
float*)op.second));
231 return new double(*((
float*)op.second));
234 return new float(*((
float*)op.second));
237 return new int(*((
float*)op.second));
240 return new long long(*((
float*)op.second));
243 return new short(*((
float*)op.second));
246 return new std::string(std::to_string(*((
float*)op.second)));
249 return new uint32_t(*((
float*)op.second));
252 return new uint64_t(*((
float*)op.second));
255 return new large(*((
float*)op.second));
262 return new bool(*((
int*)op.second));
265 return new uint8_t(*((
int*)op.second));
268 return new char(*((
int*)op.second));
271 return new double(*((
int*)op.second));
274 return new float(*((
int*)op.second));
277 return new int(*((
int*)op.second));
280 return new long long(*((
int*)op.second));
283 return new short(*((
int*)op.second));
286 return new std::string(std::to_string(*((
int*)op.second)));
289 return new uint32_t(*((
int*)op.second));
292 return new uint64_t(*((
int*)op.second));
295 return new large(*((
int*)op.second));
302 return new bool(*((
long long*)op.second));
305 return new uint8_t(*((
long long*)op.second));
308 return new char(*((
long long*)op.second));
311 return new double(*((
long long*)op.second));
314 return new float(*((
long long*)op.second));
317 return new int(*((
long long*)op.second));
320 return new long long(*((
long long*)op.second));
323 return new short(*((
long long*)op.second));
326 return new std::string(std::to_string(*((
long long*)op.second)));
329 return new uint32_t(*((
long long*)op.second));
332 return new uint64_t(*((
long long*)op.second));
335 return new large(*((
long long*)op.second));
342 return new bool(*((
short*)op.second));
345 return new uint8_t(*((
short*)op.second));
348 return new char(*((
short*)op.second));
351 return new double(*((
short*)op.second));
354 return new float(*((
short*)op.second));
357 return new int(*((
short*)op.second));
360 return new long long(*((
short*)op.second));
363 return new short(*((
short*)op.second));
366 return new std::string(std::to_string(*((
short*)op.second)));
369 return new uint32_t(*((
short*)op.second));
372 return new uint64_t(*((
short*)op.second));
375 return new large(*((
short*)op.second));
382 return new bool(*((std::string*)op.second) ==
"true");
385 return new uint8_t(std::stoll(*((std::string*)op.second)));
388 return new char((*((std::string*)op.second))[0]);
391 return new double(std::stold(*((std::string*)op.second)));
394 return new float(std::stold(*((std::string*)op.second)));
397 return new int(std::stoll(*((std::string*)op.second)));
400 return new long long(std::stoll(*((std::string*)op.second)));
403 return new short(std::stoll(*((std::string*)op.second)));
406 return new std::string(*((std::string*)op.second));
409 return new uint32_t(std::stoll(*((std::string*)op.second)));
412 return new uint64_t(std::stoll(*((std::string*)op.second)));
415 return new large(*((std::string*)op.second));
421 return new bool(*((uint32_t*)op.second));
424 return new uint8_t(*((uint32_t*)op.second));
427 return new char(*((uint32_t*)op.second));
430 return new double(*((uint32_t*)op.second));
433 return new float(*((uint32_t*)op.second));
436 return new int(*((uint32_t*)op.second));
439 return new long long(*((uint32_t*)op.second));
442 return new short(*((uint32_t*)op.second));
445 return new std::string(std::to_string(*((uint32_t*)op.second)));
448 return new uint32_t(*((uint32_t*)op.second));
451 return new uint64_t(*((uint32_t*)op.second));
454 return new large(*((uint32_t*)op.second));
460 return new bool(*((uint64_t*)op.second));
463 return new uint8_t(*((uint64_t*)op.second));
466 return new char(*((uint64_t*)op.second));
469 return new double(*((uint64_t*)op.second));
472 return new float(*((uint64_t*)op.second));
475 return new int(*((uint64_t*)op.second));
478 return new long long(*((uint64_t*)op.second));
481 return new short(*((uint64_t*)op.second));
484 return new std::string(std::to_string(*((uint64_t*)op.second)));
487 return new uint32_t(*((uint64_t*)op.second));
490 return new uint64_t(*((uint64_t*)op.second));
493 return new large(*((uint64_t*)op.second));
500 return new bool(std::stoll(((
large*)op.second)->ToString()));
503 return new uint8_t(std::stoll(((
large*)op.second)->ToString()));
506 return new char(std::stoll(((
large*)op.second)->ToString()));
509 return new double(std::stoll(((
large*)op.second)->ToString()));
512 return new float(std::stoll(((
large*)op.second)->ToString()));
515 return new int(std::stoll(((
large*)op.second)->ToString()));
518 return new long long(std::stoll(((
large*)op.second)->ToString()));
521 return new short(std::stoll(((
large*)op.second)->ToString()));
524 return new std::string(((
large*)op.second)->ToString());
527 return new uint32_t(std::stoll(((
large*)op.second)->ToString()));
530 return new uint64_t(std::stoll(((
large*)op.second)->ToString()));
538 throw std::exception(
"Cannot convert types");
540std::pair<Poliz::PolizType, std::pair<void*, void*>>
Poliz::make_same(std::pair<Poliz::PolizType, void*> op1, std::pair<Poliz::PolizType, void*> op2) {
543 return std::pair<Poliz::PolizType, std::pair<void*, void*>>(op1.first, { op1.second,
convert(op2, op1.first) });
545 return std::pair<Poliz::PolizType, std::pair<void*, void*>>(op2.first, {
convert(op1, op2.first), op2.second });
550 if (ident->
value ==
nullptr) {
551 throw std::exception((
"Variable '" + ident->
name +
"' was not initialized").c_str());
579 throw std::exception((
"Variable '" + ident->
name +
"' has unconvertable value").c_str());
584 if (ident->
value ==
nullptr)
588 return new bool(*(
bool*)(ident->
value));
590 return new uint8_t(*(uint8_t*)(ident->
value));
592 return new char(*(
char*)(ident->
value));
594 return new double(*(
double*)(ident->
value));
596 return new float(*(
float*)(ident->
value));
598 return new int(*(
int*)(ident->
value));
600 return new long long(*(
long long*)(ident->
value));
602 return new short(*(
short*)(ident->
value));
604 return new std::string(*(std::string*)(ident->
value));
606 return new uint32_t(*(uint32_t*)(ident->
value));
608 return new uint64_t(*(uint64_t*)(ident->
value));
612 throw std::exception((
"Variable '" + ident->
name +
"' has unconvertable value").c_str());
645std::pair<Poliz::PolizType, void*>
Poliz::execute_operation(std::pair<Poliz::PolizType, void*> op1, std::pair<Poliz::PolizType, void*> op2, std::string operation) {
651 if (operation ==
"+") {
652 switch (conv.first) {
654 return { conv.first,
new bool(*((
bool*)conv.second.first) + *((
bool*)conv.second.second)) };
656 return { conv.first,
new uint8_t(*((uint8_t*)conv.second.first) + *((uint8_t*)conv.second.second)) };
658 return { conv.first,
new char(*((
char*)conv.second.first) + *((
char*)conv.second.second)) };
660 return { conv.first,
new double(*((
double*)conv.second.first) + *((
double*)conv.second.second)) };
662 return { conv.first,
new float(*((
float*)conv.second.first) + *((
float*)conv.second.second)) };
664 return { conv.first,
new int(*((
int*)conv.second.first) + *((
int*)conv.second.second)) };
666 return { conv.first,
new long long(*((
long long*)conv.second.first) + *((
long long*)conv.second.second)) };
668 return { conv.first,
new short(*((
short*)conv.second.first) + *((
short*)conv.second.second)) };
670 return { conv.first,
new std::string(*((std::string*)conv.second.first) + *((std::string*)conv.second.second)) };
672 return { conv.first,
new uint32_t(*((uint32_t*)conv.second.first) + *((uint32_t*)conv.second.second)) };
674 return { conv.first,
new uint64_t(*((uint64_t*)conv.second.first) + *((uint64_t*)conv.second.second)) };
676 return { conv.first,
new large(*((
large*)conv.second.first) + *((
large*)conv.second.second)) };
678 throw std::exception(
"Cannot use this operation");
680 }
else if (operation ==
"-") {
681 switch (conv.first) {
683 return { conv.first,
new bool(*((
bool*)conv.second.first) - *((
bool*)conv.second.second)) };
685 return { conv.first,
new uint8_t(*((uint8_t*)conv.second.first) - *((uint8_t*)conv.second.second)) };
687 return { conv.first,
new char(*((
char*)conv.second.first) - *((
char*)conv.second.second)) };
689 return { conv.first,
new double(*((
double*)conv.second.first) - *((
double*)conv.second.second)) };
691 return { conv.first,
new float(*((
float*)conv.second.first) - *((
float*)conv.second.second)) };
693 return { conv.first,
new int(*((
int*)conv.second.first) - *((
int*)conv.second.second)) };
695 return { conv.first,
new long long(*((
long long*)conv.second.first) - *((
long long*)conv.second.second)) };
697 return { conv.first,
new short(*((
short*)conv.second.first) - *((
short*)conv.second.second)) };
699 return { conv.first,
new uint32_t(*((uint32_t*)conv.second.first) - *((uint32_t*)conv.second.second)) };
701 return { conv.first,
new uint64_t(*((uint64_t*)conv.second.first) - *((uint64_t*)conv.second.second)) };
703 return { conv.first,
new large(*((
large*)conv.second.first) - *((
large*)conv.second.second)) };
705 throw std::exception(
"Cannot use this operation");
707 }
else if (operation ==
"*") {
708 switch (conv.first) {
710 return { conv.first,
new bool(*((
bool*)conv.second.first) * *((
bool*)conv.second.second)) };
712 return { conv.first,
new uint8_t(*((uint8_t*)conv.second.first) * *((uint8_t*)conv.second.second)) };
714 return { conv.first,
new char(*((
char*)conv.second.first) * *((
char*)conv.second.second)) };
716 return { conv.first,
new double(*((
double*)conv.second.first) * *((
double*)conv.second.second)) };
718 return { conv.first,
new float(*((
float*)conv.second.first) * *((
float*)conv.second.second)) };
720 return { conv.first,
new int(*((
int*)conv.second.first) * *((
int*)conv.second.second)) };
722 return { conv.first,
new long long(*((
long long*)conv.second.first) * *((
long long*)conv.second.second)) };
724 return { conv.first,
new short(*((
short*)conv.second.first) * *((
short*)conv.second.second)) };
726 return { conv.first,
new uint32_t(*((uint32_t*)conv.second.first) * *((uint32_t*)conv.second.second)) };
728 return { conv.first,
new uint64_t(*((uint64_t*)conv.second.first) * *((uint64_t*)conv.second.second)) };
730 return { conv.first,
new large(*((
large*)conv.second.first) * *((
large*)conv.second.second)) };
732 throw std::exception(
"Cannot use this operation");
734 }
else if (operation ==
"/") {
735 switch (conv.first) {
737 return { conv.first,
new bool(*((
bool*)conv.second.first) / *((
bool*)conv.second.second)) };
739 return { conv.first,
new uint8_t(*((uint8_t*)conv.second.first) / *((uint8_t*)conv.second.second)) };
741 return { conv.first,
new char(*((
char*)conv.second.first) / *((
char*)conv.second.second)) };
743 return { conv.first,
new double(*((
double*)conv.second.first) / *((
double*)conv.second.second)) };
745 return { conv.first,
new float(*((
float*)conv.second.first) / *((
float*)conv.second.second)) };
747 return { conv.first,
new int(*((
int*)conv.second.first) / *((
int*)conv.second.second)) };
749 return { conv.first,
new long long(*((
long long*)conv.second.first) / *((
long long*)conv.second.second)) };
751 return { conv.first,
new short(*((
short*)conv.second.first) / *((
short*)conv.second.second)) };
753 return { conv.first,
new uint32_t(*((uint32_t*)conv.second.first) / *((uint32_t*)conv.second.second)) };
755 return { conv.first,
new uint64_t(*((uint64_t*)conv.second.first) / *((uint64_t*)conv.second.second)) };
757 return { conv.first,
new large(*((
large*)conv.second.first) / *((
large*)conv.second.second)) };
759 throw std::exception(
"Cannot use this operation");
761 }
else if (operation ==
"%") {
762 switch (conv.first) {
764 return { conv.first,
new bool(*((
bool*)conv.second.first) % *((
bool*)conv.second.second)) };
766 return { conv.first,
new uint8_t(*((uint8_t*)conv.second.first) % *((uint8_t*)conv.second.second)) };
768 return { conv.first,
new char(*((
char*)conv.second.first) % *((
char*)conv.second.second)) };
770 return { conv.first,
new int(*((
int*)conv.second.first) % *((
int*)conv.second.second)) };
772 return { conv.first,
new long long(*((
long long*)conv.second.first) % *((
long long*)conv.second.second)) };
774 return { conv.first,
new short(*((
short*)conv.second.first) % *((
short*)conv.second.second)) };
776 return { conv.first,
new uint32_t(*((uint32_t*)conv.second.first) % *((uint32_t*)conv.second.second)) };
778 return { conv.first,
new uint64_t(*((uint64_t*)conv.second.first) % *((uint64_t*)conv.second.second)) };
780 return { conv.first,
new large(*((
large*)conv.second.first) % *((
large*)conv.second.second)) };
782 throw std::exception(
"Cannot use this operation");
784 }
else if (operation ==
">>") {
785 switch (conv.first) {
787 return { conv.first,
new bool(*((
bool*)conv.second.first) >> *((
bool*)conv.second.second)) };
789 return { conv.first,
new uint8_t(*((uint8_t*)conv.second.first) >> *((uint8_t*)conv.second.second)) };
791 return { conv.first,
new char(*((
char*)conv.second.first) >> *((
char*)conv.second.second)) };
793 return { conv.first,
new int(*((
int*)conv.second.first) >> *((
int*)conv.second.second)) };
795 return { conv.first,
new long long(*((
long long*)conv.second.first) >> *((
long long*)conv.second.second)) };
797 return { conv.first,
new short(*((
short*)conv.second.first) >> *((
short*)conv.second.second)) };
799 return { conv.first,
new uint32_t(*((uint32_t*)conv.second.first) >> *((uint32_t*)conv.second.second)) };
801 return { conv.first,
new uint64_t(*((uint64_t*)conv.second.first) >> *((uint64_t*)conv.second.second)) };
803 throw std::exception(
"Cannot use this operation");
805 }
else if (operation ==
"<<") {
806 switch (conv.first) {
808 return { conv.first,
new bool(*((
bool*)conv.second.first) << *((
bool*)conv.second.second)) };
810 return { conv.first,
new uint8_t(*((uint8_t*)conv.second.first) << *((uint8_t*)conv.second.second)) };
812 return { conv.first,
new char(*((
char*)conv.second.first) << *((
char*)conv.second.second)) };
814 return { conv.first,
new int(*((
int*)conv.second.first) << *((
int*)conv.second.second)) };
816 return { conv.first,
new long long(*((
long long*)conv.second.first) << *((
long long*)conv.second.second)) };
818 return { conv.first,
new short(*((
short*)conv.second.first) << *((
short*)conv.second.second)) };
820 return { conv.first,
new uint32_t(*((uint32_t*)conv.second.first) << *((uint32_t*)conv.second.second)) };
822 return { conv.first,
new uint64_t(*((uint64_t*)conv.second.first) << *((uint64_t*)conv.second.second)) };
824 throw std::exception(
"Cannot use this operation");
826 }
else if (operation ==
"<") {
827 switch (conv.first) {
829 return {
BOOL_LITERAL,
new bool(*((
bool*)conv.second.first) < *((
bool*)conv.second.second)) };
831 return {
BOOL_LITERAL,
new bool(*((uint8_t*)conv.second.first) < *((uint8_t*)conv.second.second)) };
833 return {
BOOL_LITERAL,
new bool(*((
char*)conv.second.first) < *((
char*)conv.second.second)) };
835 return {
BOOL_LITERAL,
new bool(*((
double*)conv.second.first) < *((
double*)conv.second.second)) };
837 return {
BOOL_LITERAL,
new bool(*((
float*)conv.second.first) < *((
float*)conv.second.second)) };
839 return {
BOOL_LITERAL,
new bool(*((
int*)conv.second.first) < *((
int*)conv.second.second)) };
841 return {
BOOL_LITERAL,
new bool(*((
long long*)conv.second.first) < *((
long long*)conv.second.second)) };
843 return {
BOOL_LITERAL,
new bool(*((
short*)conv.second.first) < *((
short*)conv.second.second)) };
845 return {
BOOL_LITERAL,
new bool(*((std::string*)conv.second.first) < *((std::string*)conv.second.second)) };
847 return {
BOOL_LITERAL,
new bool(*((uint32_t*)conv.second.first) < *((uint32_t*)conv.second.second)) };
849 return {
BOOL_LITERAL,
new bool(*((uint64_t*)conv.second.first) < *((uint64_t*)conv.second.second)) };
851 return { conv.first,
new large(*((
large*)conv.second.first) < *((
large*)conv.second.second)) };
853 throw std::exception(
"Cannot use this operation");
855 }
else if (operation ==
"<=") {
856 switch (conv.first) {
858 return {
BOOL_LITERAL,
new bool(*((
bool*)conv.second.first) <= *((
bool*)conv.second.second)) };
860 return {
BOOL_LITERAL,
new bool(*((uint8_t*)conv.second.first) <= *((uint8_t*)conv.second.second)) };
862 return {
BOOL_LITERAL,
new bool(*((
char*)conv.second.first) <= *((
char*)conv.second.second)) };
864 return {
BOOL_LITERAL,
new bool(*((
double*)conv.second.first) <= *((
double*)conv.second.second)) };
866 return {
BOOL_LITERAL,
new bool(*((
float*)conv.second.first) <= *((
float*)conv.second.second)) };
868 return {
BOOL_LITERAL,
new bool(*((
int*)conv.second.first) <= *((
int*)conv.second.second)) };
870 return {
BOOL_LITERAL,
new bool(*((
long long*)conv.second.first) <= *((
long long*)conv.second.second)) };
872 return {
BOOL_LITERAL,
new bool(*((
short*)conv.second.first) <= *((
short*)conv.second.second)) };
874 return {
BOOL_LITERAL,
new bool(*((std::string*)conv.second.first) <= *((std::string*)conv.second.second)) };
876 return {
BOOL_LITERAL,
new bool(*((uint32_t*)conv.second.first) <= *((uint32_t*)conv.second.second)) };
878 return {
BOOL_LITERAL,
new bool(*((uint64_t*)conv.second.first) <= *((uint64_t*)conv.second.second)) };
880 return { conv.first,
new large(*((
large*)conv.second.first) <= *((
large*)conv.second.second)) };
882 throw std::exception(
"Cannot use this operation");
884 }
else if (operation ==
">") {
885 switch (conv.first) {
887 return {
BOOL_LITERAL,
new bool(*((
bool*)conv.second.first) > *((
bool*)conv.second.second)) };
889 return {
BOOL_LITERAL,
new bool(*((uint8_t*)conv.second.first) > *((uint8_t*)conv.second.second)) };
891 return {
BOOL_LITERAL,
new bool(*((
char*)conv.second.first) > *((
char*)conv.second.second)) };
893 return {
BOOL_LITERAL,
new bool(*((
double*)conv.second.first) > *((
double*)conv.second.second)) };
895 return {
BOOL_LITERAL,
new bool(*((
float*)conv.second.first) > *((
float*)conv.second.second)) };
897 return {
BOOL_LITERAL,
new bool(*((
int*)conv.second.first) > *((
int*)conv.second.second)) };
899 return {
BOOL_LITERAL,
new bool(*((
long long*)conv.second.first) > *((
long long*)conv.second.second)) };
901 return {
BOOL_LITERAL,
new bool(*((
short*)conv.second.first) > *((
short*)conv.second.second)) };
903 return {
BOOL_LITERAL,
new bool(*((std::string*)conv.second.first) > *((std::string*)conv.second.second)) };
905 return {
BOOL_LITERAL,
new bool(*((uint32_t*)conv.second.first) > *((uint32_t*)conv.second.second)) };
907 return {
BOOL_LITERAL,
new bool(*((uint64_t*)conv.second.first) > *((uint64_t*)conv.second.second)) };
909 return { conv.first,
new large(*((
large*)conv.second.first) > *((
large*)conv.second.second)) };
911 throw std::exception(
"Cannot use this operation");
913 }
else if (operation ==
">=") {
914 switch (conv.first) {
916 return {
BOOL_LITERAL,
new bool(*((
bool*)conv.second.first) >= *((
bool*)conv.second.second)) };
918 return {
BOOL_LITERAL,
new bool(*((uint8_t*)conv.second.first) >= *((uint8_t*)conv.second.second)) };
920 return {
BOOL_LITERAL,
new bool(*((
char*)conv.second.first) >= *((
char*)conv.second.second)) };
922 return {
BOOL_LITERAL,
new bool(*((
double*)conv.second.first) >= *((
double*)conv.second.second)) };
924 return {
BOOL_LITERAL,
new bool(*((
float*)conv.second.first) >= *((
float*)conv.second.second)) };
926 return {
BOOL_LITERAL,
new bool(*((
int*)conv.second.first) >= *((
int*)conv.second.second)) };
928 return {
BOOL_LITERAL,
new bool(*((
long long*)conv.second.first) >= *((
long long*)conv.second.second)) };
930 return {
BOOL_LITERAL,
new bool(*((
short*)conv.second.first) >= *((
short*)conv.second.second)) };
932 return {
BOOL_LITERAL,
new bool(*((std::string*)conv.second.first) >= *((std::string*)conv.second.second)) };
934 return {
BOOL_LITERAL,
new bool(*((uint32_t*)conv.second.first) >= *((uint32_t*)conv.second.second)) };
936 return {
BOOL_LITERAL,
new bool(*((uint64_t*)conv.second.first) >= *((uint64_t*)conv.second.second)) };
938 return { conv.first,
new large(*((
large*)conv.second.first) >= *((
large*)conv.second.second)) };
940 throw std::exception(
"Cannot use this operation");
942 }
else if (operation ==
"==") {
943 switch (conv.first) {
945 return {
BOOL_LITERAL,
new bool(*((
bool*)conv.second.first) == *((
bool*)conv.second.second)) };
947 return {
BOOL_LITERAL,
new bool(*((uint8_t*)conv.second.first) == *((uint8_t*)conv.second.second)) };
949 return {
BOOL_LITERAL,
new bool(*((
char*)conv.second.first) == *((
char*)conv.second.second)) };
951 return {
BOOL_LITERAL,
new bool(*((
double*)conv.second.first) == *((
double*)conv.second.second)) };
953 return {
BOOL_LITERAL,
new bool(*((
float*)conv.second.first) == *((
float*)conv.second.second)) };
955 return {
BOOL_LITERAL,
new bool(*((
int*)conv.second.first) == *((
int*)conv.second.second)) };
957 return {
BOOL_LITERAL,
new bool(*((
long long*)conv.second.first) == *((
long long*)conv.second.second)) };
959 return {
BOOL_LITERAL,
new bool(*((
short*)conv.second.first) == *((
short*)conv.second.second)) };
961 return {
BOOL_LITERAL,
new bool(*((std::string*)conv.second.first) == *((std::string*)conv.second.second)) };
963 return {
BOOL_LITERAL,
new bool(*((uint32_t*)conv.second.first) == *((uint32_t*)conv.second.second)) };
965 return {
BOOL_LITERAL,
new bool(*((uint64_t*)conv.second.first) == *((uint64_t*)conv.second.second)) };
967 return { conv.first,
new large(*((
large*)conv.second.first) == *((
large*)conv.second.second)) };
969 throw std::exception(
"Cannot use this operation");
971 }
else if (operation ==
"!=") {
972 switch (conv.first) {
974 return {
BOOL_LITERAL,
new bool(*((
bool*)conv.second.first) != *((
bool*)conv.second.second)) };
976 return {
BOOL_LITERAL,
new bool(*((uint8_t*)conv.second.first) != *((uint8_t*)conv.second.second)) };
978 return {
BOOL_LITERAL,
new bool(*((
char*)conv.second.first) != *((
char*)conv.second.second)) };
980 return {
BOOL_LITERAL,
new bool(*((
double*)conv.second.first) != *((
double*)conv.second.second)) };
982 return {
BOOL_LITERAL,
new bool(*((
float*)conv.second.first) != *((
float*)conv.second.second)) };
984 return {
BOOL_LITERAL,
new bool(*((
int*)conv.second.first) != *((
int*)conv.second.second)) };
986 return {
BOOL_LITERAL,
new bool(*((
long long*)conv.second.first) != *((
long long*)conv.second.second)) };
988 return {
BOOL_LITERAL,
new bool(*((
short*)conv.second.first) != *((
short*)conv.second.second)) };
990 return {
BOOL_LITERAL,
new bool(*((std::string*)conv.second.first) != *((std::string*)conv.second.second)) };
992 return {
BOOL_LITERAL,
new bool(*((uint32_t*)conv.second.first) != *((uint32_t*)conv.second.second)) };
994 return {
BOOL_LITERAL,
new bool(*((uint64_t*)conv.second.first) != *((uint64_t*)conv.second.second)) };
996 return { conv.first,
new large(*((
large*)conv.second.first) != *((
large*)conv.second.second)) };
998 throw std::exception(
"Cannot use this operation");
1000 }
else if (operation ==
"&") {
1001 switch (conv.first) {
1003 return { conv.first,
new bool(*((
bool*)conv.second.first) & *((
bool*)conv.second.second)) };
1005 return { conv.first,
new uint8_t(*((uint8_t*)conv.second.first) & *((uint8_t*)conv.second.second)) };
1007 return { conv.first,
new char(*((
char*)conv.second.first) & *((
char*)conv.second.second)) };
1009 return { conv.first,
new int(*((
int*)conv.second.first) & *((
int*)conv.second.second)) };
1011 return { conv.first,
new long long(*((
long long*)conv.second.first) & *((
long long*)conv.second.second)) };
1013 return { conv.first,
new short(*((
short*)conv.second.first) & *((
short*)conv.second.second)) };
1015 return { conv.first,
new uint32_t(*((uint32_t*)conv.second.first) & *((uint32_t*)conv.second.second)) };
1017 return { conv.first,
new uint64_t(*((uint64_t*)conv.second.first) & *((uint64_t*)conv.second.second)) };
1019 throw std::exception(
"Cannot use this operation");
1021 }
else if (operation ==
"|") {
1022 switch (conv.first) {
1024 return { conv.first,
new bool(*((
bool*)conv.second.first) | *((
bool*)conv.second.second)) };
1026 return { conv.first,
new uint8_t(*((uint8_t*)conv.second.first) | *((uint8_t*)conv.second.second)) };
1028 return { conv.first,
new char(*((
char*)conv.second.first) | *((
char*)conv.second.second)) };
1030 return { conv.first,
new int(*((
int*)conv.second.first) | *((
int*)conv.second.second)) };
1032 return { conv.first,
new long long(*((
long long*)conv.second.first) | *((
long long*)conv.second.second)) };
1034 return { conv.first,
new short(*((
short*)conv.second.first) | *((
short*)conv.second.second)) };
1036 return { conv.first,
new uint32_t(*((uint32_t*)conv.second.first) | *((uint32_t*)conv.second.second)) };
1038 return { conv.first,
new uint64_t(*((uint64_t*)conv.second.first) | *((uint64_t*)conv.second.second)) };
1040 throw std::exception(
"Cannot use this operation");
1042 }
else if (operation ==
"^") {
1043 switch (conv.first) {
1045 return { conv.first,
new bool(*((
bool*)conv.second.first) ^ *((
bool*)conv.second.second)) };
1047 return { conv.first,
new uint8_t(*((uint8_t*)conv.second.first) ^ *((uint8_t*)conv.second.second)) };
1049 return { conv.first,
new char(*((
char*)conv.second.first) ^ *((
char*)conv.second.second)) };
1051 return { conv.first,
new int(*((
int*)conv.second.first) ^ *((
int*)conv.second.second)) };
1053 return { conv.first,
new long long(*((
long long*)conv.second.first) ^ *((
long long*)conv.second.second)) };
1055 return { conv.first,
new short(*((
short*)conv.second.first) ^ *((
short*)conv.second.second)) };
1057 return { conv.first,
new uint32_t(*((uint32_t*)conv.second.first) ^ *((uint32_t*)conv.second.second)) };
1059 return { conv.first,
new uint64_t(*((uint64_t*)conv.second.first) ^ *((uint64_t*)conv.second.second)) };
1061 throw std::exception(
"Cannot use this operation");
1063 }
else if (operation ==
"->") {
1064 switch (conv.first) {
1066 return { conv.first,
new bool(~*((
bool*)conv.second.first) | *((
bool*)conv.second.second)) };
1068 return { conv.first,
new uint8_t(~*((uint8_t*)conv.second.first) | *((uint8_t*)conv.second.second)) };
1070 return { conv.first,
new char(~*((
char*)conv.second.first) | *((
char*)conv.second.second)) };
1072 return { conv.first,
new int(~*((
int*)conv.second.first) | *((
int*)conv.second.second)) };
1074 return { conv.first,
new long long(~*((
long long*)conv.second.first) | *((
long long*)conv.second.second)) };
1076 return { conv.first,
new short(~*((
short*)conv.second.first) | *((
short*)conv.second.second)) };
1078 return { conv.first,
new uint32_t(~*((uint32_t*)conv.second.first) | *((uint32_t*)conv.second.second)) };
1080 return { conv.first,
new uint64_t(~*((uint64_t*)conv.second.first) | *((uint64_t*)conv.second.second)) };
1082 throw std::exception(
"Cannot use this operation");
1084 }
else if (operation ==
"&&") {
1085 switch (conv.first) {
1087 return {
BOOL_LITERAL,
new bool(*((
bool*)conv.second.first) && *((
bool*)conv.second.second)) };
1089 return {
BOOL_LITERAL,
new bool(*((uint8_t*)conv.second.first) && *((uint8_t*)conv.second.second)) };
1091 return {
BOOL_LITERAL,
new bool(*((
char*)conv.second.first) && *((
char*)conv.second.second)) };
1093 return {
BOOL_LITERAL,
new bool(*((
double*)conv.second.first) && *((
double*)conv.second.second)) };
1095 return {
BOOL_LITERAL,
new bool(*((
float*)conv.second.first) && *((
float*)conv.second.second)) };
1097 return {
BOOL_LITERAL,
new bool(*((
int*)conv.second.first) && *((
int*)conv.second.second)) };
1099 return {
BOOL_LITERAL,
new bool(*((
long long*)conv.second.first) && *((
long long*)conv.second.second)) };
1101 return {
BOOL_LITERAL,
new bool(*((
short*)conv.second.first) && *((
short*)conv.second.second)) };
1103 return {
BOOL_LITERAL,
new bool(*((uint32_t*)conv.second.first) && *((uint32_t*)conv.second.second)) };
1105 return {
BOOL_LITERAL,
new bool(*((uint64_t*)conv.second.first) && *((uint64_t*)conv.second.second)) };
1107 throw std::exception(
"Cannot use this operation");
1109 }
else if (operation ==
"||") {
1110 switch (conv.first) {
1112 return {
BOOL_LITERAL,
new bool(*((
bool*)conv.second.first) || *((
bool*)conv.second.second)) };
1114 return {
BOOL_LITERAL,
new bool(*((uint8_t*)conv.second.first) || *((uint8_t*)conv.second.second)) };
1116 return {
BOOL_LITERAL,
new bool(*((
char*)conv.second.first) || *((
char*)conv.second.second)) };
1118 return {
BOOL_LITERAL,
new bool(*((
double*)conv.second.first) || *((
double*)conv.second.second)) };
1120 return {
BOOL_LITERAL,
new bool(*((
float*)conv.second.first) || *((
float*)conv.second.second)) };
1122 return {
BOOL_LITERAL,
new bool(*((
int*)conv.second.first) || *((
int*)conv.second.second)) };
1124 return {
BOOL_LITERAL,
new bool(*((
long long*)conv.second.first) || *((
long long*)conv.second.second)) };
1126 return {
BOOL_LITERAL,
new bool(*((
short*)conv.second.first) || *((
short*)conv.second.second)) };
1128 return {
BOOL_LITERAL,
new bool(*((uint32_t*)conv.second.first) || *((uint32_t*)conv.second.second)) };
1130 return {
BOOL_LITERAL,
new bool(*((uint64_t*)conv.second.first) || *((uint64_t*)conv.second.second)) };
1132 throw std::exception(
"Cannot use this operation");
1136std::pair<Poliz::PolizType, void*>
Poliz::execute_operation(std::pair<Poliz::PolizType, void*> op, std::string operation) {
1142 if (operation ==
"++") {
1145 auto val = *((
bool*)op.second);
1147 ident->
value =
new bool(val);
1151 auto val = *((uint8_t*)op.second);
1153 ident->
value =
new uint8_t(val);
1157 auto val = *((
char*)op.second);
1159 ident->
value =
new char(val);
1163 auto val = *((
double*)op.second);
1165 ident->
value =
new double(val);
1169 auto val = *((
float*)op.second);
1171 ident->
value =
new float(val);
1175 auto val = *((
int*)op.second);
1177 ident->
value =
new int(val);
1181 auto val = *((
long long*)op.second);
1183 ident->
value =
new long long(val);
1187 auto val = *((
short*)op.second);
1189 ident->
value =
new short(val);
1193 auto val = *((uint32_t*)op.second);
1195 ident->
value =
new uint32_t(val);
1199 auto val = *((uint64_t*)op.second);
1201 ident->
value =
new uint64_t(val);
1205 auto val = *((
large*)op.second);
1211 }
else if (operation ==
"--") {
1214 auto val = *((uint8_t*)op.second);
1216 ident->
value =
new uint8_t(val);
1220 auto val = *((
char*)op.second);
1222 ident->
value =
new char(val);
1226 auto val = *((
double*)op.second);
1228 ident->
value =
new double(val);
1232 auto val = *((
float*)op.second);
1234 ident->
value =
new float(val);
1238 auto val = *((
int*)op.second);
1240 ident->
value =
new int(val);
1244 auto val = *((
long long*)op.second);
1246 ident->
value =
new long long(val);
1250 auto val = *((
short*)op.second);
1252 ident->
value =
new short(val);
1256 auto val = *((uint32_t*)op.second);
1258 ident->
value =
new uint32_t(val);
1262 auto val = *((uint64_t*)op.second);
1264 ident->
value =
new uint64_t(val);
1268 auto val = *((
large*)op.second);
1274 }
else if (operation ==
"+") {
1276 }
else if (operation ==
"-") {
1279 return { op.first,
new bool(-*((
bool*)op.second)) };
1281 return { op.first,
new uint8_t(-*((uint8_t*)op.second)) };
1283 return { op.first,
new char(-*((
char*)op.second)) };
1285 return { op.first,
new double(-*((
double*)op.second)) };
1287 return { op.first,
new float(-*((
float*)op.second)) };
1289 return { op.first,
new int(-*((
int*)op.second)) };
1291 return { op.first,
new long long(-*((
long long*)op.second)) };
1293 return { op.first,
new short(-*((
short*)op.second)) };
1298 }
else if (operation ==
"!") {
1301 return { op.first,
new bool(!*((
bool*)op.second)) };
1303 return { op.first,
new uint8_t(!*((uint8_t*)op.second)) };
1305 return { op.first,
new char(!*((
char*)op.second)) };
1307 return { op.first,
new double(!*((
double*)op.second)) };
1309 return { op.first,
new float(!*((
float*)op.second)) };
1311 return { op.first,
new int(!*((
int*)op.second)) };
1313 return { op.first,
new long long(!*((
long long*)op.second)) };
1315 return { op.first,
new short(!*((
short*)op.second)) };
1317 return { op.first,
new uint32_t(!*((uint32_t*)op.second)) };
1319 return { op.first,
new uint64_t(!*((uint64_t*)op.second)) };
1321 }
else if (operation ==
"~") {
1324 return { op.first,
new bool(~*((
bool*)op.second)) };
1326 return { op.first,
new uint8_t(~*((uint8_t*)op.second)) };
1328 return { op.first,
new char(~*((
char*)op.second)) };
1330 return { op.first,
new int(~*((
int*)op.second)) };
1332 return { op.first,
new long long(~*((
long long*)op.second)) };
1334 return { op.first,
new short(~*((
short*)op.second)) };
1336 return { op.first,
new uint32_t(~*((uint32_t*)op.second)) };
1338 return { op.first,
new uint64_t(~*((uint64_t*)op.second)) };
1343 int sz = *(
int*)st.top().second;
1350 std::stack<std::pair<Poliz::PolizType, void*>> args;
1351 for (
int i = 0; i < sz; ++i) {
1352 args.push(st.top());
1357 while (!args.empty()) {
1365 std::stack<std::pair<Poliz::PolizType, void*>> st;
1367 int label_balance = 0;
1400 if (st.top().second ==
nullptr)
1401 throw std::exception(
"Cannot access the element of an uninitialized array");
1402 auto var = (std::vector<void*>*)((
Identifier*)st.top().second)->value;
1404 if (var->size() <= ind || ind < 0)
1405 throw std::exception(
"Array index out of range");
1413 if (st.top().second ==
nullptr)
1414 throw std::exception(
"Cannot access the character of an uninitialized string");
1415 auto var = (std::string*)((
Identifier*)st.top().second)->value;
1416 auto name = ((
Identifier*)st.top().second)->name;
1418 if (var->size() <= ind || ind < 0)
1419 throw std::exception(
"String index out of range");
1421 ptr->value = &(var->operator[](ind));
1434 std::pair<std::string, Type> val = *((std::pair<std::string, Type>*)
global_lexes[p].second);
1440 std::string s = val.first.substr(1, val.first.size() - 2);
1443 else if (s ==
"\\\"")
1445 else if (s ==
"\\n")
1447 else if (s ==
"\\t")
1449 else if (s ==
"\\0")
1476 auto op2 = st.top();
1478 auto op1 = st.top();
1485 auto op2 = st.top();
1487 auto op1 = st.top();
1490 auto assign_val = op2.second;
1492 assign_val = ((
Identifier*)op2.second)->value;
1493 if (assign_val ==
nullptr)
1494 throw std::exception((
"Cannot use a variable '" + ((
Identifier*)op2.second)->name +
"' that has not been assigned a value").c_str());
1498 if (var->type.is_array)
1499 var->value = ((
Identifier*)op2.second)->value;
1500 else if (var->type.expr_type ==
ExprType::Char && *var->name.rbegin() ==
']')
1505 std::string op = *((std::string*)
global_lexes[p].second);
1521 throw std::exception(
"convert() function is restricted in the global scope");
1524 throw std::exception(
"rand() function is restricted in the global scope");
1527 throw std::exception(
"strlen() function is restricted in the global scope");
1530 throw std::exception(
"function calls are restricted in the global scope");
1533 throw std::exception(
"time() function is restricted in the global scope");
1536 auto op2 = st.top();
1538 auto op1 = st.top();
1546 while (st.size() > 1)
1552 while (p <
lexes.size()) {
1553 if (
func_calls.size() > 1e3)
throw std::exception(
"Stack overflow");
1555 switch (
lexes[p].first) {
1557 int ind = *((
int*)st.top().second);
1563 int ind = *((
int*)st.top().second);
1565 bool val = *((
bool*)st.top().second) ==
false;
1573 int ind = *((
int*)st.top().second);
1575 bool val = *((
bool*)st.top().second) ==
true;
1590 throw std::exception(
"The return statement was not called");
1615 if (st.top().second ==
nullptr)
1616 throw std::exception(
"Cannot access the element of an uninitialized array");
1617 auto var = (std::vector<void*>*)((
Identifier*)st.top().second)->value;
1619 if (var->size() <= ind)
1620 throw std::exception(
"Array index out of range");
1628 if (st.top().second ==
nullptr)
1629 throw std::exception(
"Cannot access the character of an uninitialized string");
1630 auto var = (std::string*)((
Identifier*)st.top().second)->value;
1631 auto name = ((
Identifier*)st.top().second)->name;
1633 if (var->size() <= ind)
1634 throw std::exception(
"String index out of range");
1636 ptr->value = &(var->operator[](ind));
1649 std::pair<std::string, Type> val = *((std::pair<std::string, Type>*)
lexes[p].second);
1655 std::string s = val.first.substr(1, val.first.size() - 2);
1658 else if (s ==
"\\\"")
1660 else if (s ==
"\\n")
1662 else if (s ==
"\\t")
1664 else if (s ==
"\\0")
1679 val.first = val.first.substr(1, val.first.size() - 2);
1680 val.first =
ReplaceAll(val.first,
"\\\"",
"\"");
1681 val.first =
ReplaceAll(val.first,
"\\n",
"\n");
1682 val.first =
ReplaceAll(val.first,
"\\t",
"\t");
1683 val.first =
ReplaceAll(val.first,
"\\0",
"\0");
1696 auto op2 = st.top();
1698 auto op1 = st.top();
1705 auto op2 = st.top();
1707 auto op1 = st.top();
1710 auto assign_val = op2.second;
1712 assign_val = ((
Identifier*)op2.second)->value;
1713 if (assign_val ==
nullptr)
1714 throw std::exception((
"Cannot use a variable '" + ((
Identifier*)op2.second)->name +
"' that has not been assigned a value").c_str());
1717 if (*((std::string*)
lexes[p].second) ==
"=") {
1718 if (var->type.is_array)
1719 var->value = ((
Identifier*)op2.second)->value;
1720 else if (var->type.expr_type ==
ExprType::Char && *var->name.rbegin() ==
']')
1725 std::string op = *((std::string*)
lexes[p].second);
1741 auto top = st.top();
1778 auto op2 = st.top();
1781 auto op1 = st.top();
1812 std::getline(std::cin, value);
1825 auto type =
lexes[p];
1826 auto val = st.top();
1829 st.push({ poliz_type,
convert(val, poliz_type) });
1847 const auto p1 = std::chrono::system_clock::now();
1853 auto op2 = st.top();
1855 auto op1 = st.top();
1864 throw std::exception(
"The return statement was not called");
1870 tid_vals.push(std::vector<std::pair<Identifier*, void*>>());
1878 for (
auto& u : current_tid->
children) {
1885 if (!u.second->type.is_array)
1886 u.second->value =
nullptr;
1888 for (
int i = 0; i < u.second->type.array_size; ++i) {
1889 ((std::vector<void*>*)u.second->value)->operator[](i) =
nullptr;
1893 for (
auto& u : current_tid->
children) {
1901 for (
auto& u : top) {
1902 u.first->value = u.second;