AALanguage
The best language for those who have nothing to do
Loading...
Searching...
No Matches
Poliz.cpp
Go to the documentation of this file.
1#include "Poliz.h"
2
3std::string Poliz::ReplaceAll(std::string str, const std::string& from, const std::string& to) {
4 size_t start_pos = 0;
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(); // Handles case where 'to' is a substring of 'from'
8 }
9 return str;
10}
11
13 if (str == "double") return ExprType::Double;
14 if (str == "udouble") return ExprType::UDouble;
15 if (str == "long") return ExprType::Long;
16 if (str == "ulong") return ExprType::ULong;
17 if (str == "float") return ExprType::Float;
18 if (str == "ufloat") return ExprType::UFloat;
19 if (str == "int") return ExprType::Int;
20 if (str == "uint") return ExprType::UInt;
21 if (str == "short") return ExprType::Short;
22 if (str == "ushort") return ExprType::UShort;
23 if (str == "byte") return ExprType::Byte;
24 if (str == "char") return ExprType::Char;
25 if (str == "string") return ExprType::String;
26 if (str == "bool") return ExprType::Bool;
27 if (str == "void") return ExprType::Void;
28 if (str == "large") return ExprType::Large;
29 return ExprType::Unknown;
30}
31
32Poliz::Poliz(Type* current_function_) {
33 rnd = std::mt19937((std::chrono::high_resolution_clock::now().time_since_epoch().count() + 13));
34 current_function = current_function_;
35}
37 for (int i = 0; i < lexes.size(); ++i) {
38 if (lexes[i].first != Poliz::PolizType::ADDRESS && lexes[i].first != Poliz::PolizType::POINTER && lexes[i].second != nullptr)
39 delete lexes[i].second;
40 }
41}
42void Poliz::put_lex(std::pair<Poliz::PolizType, void*> l) {
43 if (*current_function != Type())
44 lexes.push_back(l);
45 else
46 global_lexes.push_back(l);
47}
49 lexes.push_back({ Poliz::PolizType::BLANK, nullptr });
50}
52 return lexes.size();
53}
54void* Poliz::convert(std::pair<Poliz::PolizType, void*> op, Poliz::PolizType to) {
55 if (op.first == Poliz::PolizType::ADDRESS)
56 op = address_to_value(op.second);
57
58 switch (op.first) {
59 case BOOL_LITERAL: {
60 switch (to) {
61 case BOOL_LITERAL:
62 return new bool(*((bool*)op.second));
63 break;
64 case BYTE_LITERAL:
65 return new uint8_t(*((bool*)op.second));
66 break;
67 case CHAR_LITERAL:
68 return new char(*((bool*)op.second));
69 break;
70 case DOUBLE_LITERAL:
71 return new double(*((bool*)op.second));
72 break;
73 case FLOAT_LITERAL:
74 return new float(*((bool*)op.second));
75 break;
76 case INT_LITERAL:
77 return new int(*((bool*)op.second));
78 break;
79 case LONG_LITERAL:
80 return new long long(*((bool*)op.second));
81 break;
82 case SHORT_LITERAL:
83 return new short(*((bool*)op.second));
84 break;
85 case STRING_LITERAL:
86 return new std::string(*((bool*)op.second) ? "true" : "false");
87 break;
88 case UINT_LITERAL:
89 return new uint32_t(*((bool*)op.second));
90 break;
91 case ULONG_LITERAL:
92 return new uint64_t(*((bool*)op.second));
93 break;
94 case LARGE_LITERAL:
95 return new large(*((bool*)op.second));
96 }
97 break;
98 }
99 case BYTE_LITERAL: {
100 switch (to) {
101 case BOOL_LITERAL:
102 return new bool(*((uint8_t*)op.second));
103 break;
104 case BYTE_LITERAL:
105 return new uint8_t(*((uint8_t*)op.second));
106 break;
107 case CHAR_LITERAL:
108 return new char(*((uint8_t*)op.second));
109 break;
110 case DOUBLE_LITERAL:
111 return new double(*((uint8_t*)op.second));
112 break;
113 case FLOAT_LITERAL:
114 return new float(*((uint8_t*)op.second));
115 break;
116 case INT_LITERAL:
117 return new int(*((uint8_t*)op.second));
118 break;
119 case LONG_LITERAL:
120 return new long long(*((uint8_t*)op.second));
121 break;
122 case SHORT_LITERAL:
123 return new short(*((uint8_t*)op.second));
124 break;
125 case STRING_LITERAL:
126 return new std::string(std::to_string(*((uint8_t*)op.second)));
127 break;
128 case UINT_LITERAL:
129 return new uint32_t(*((uint8_t*)op.second));
130 break;
131 case ULONG_LITERAL:
132 return new uint64_t(*((uint8_t*)op.second));
133 break;
134 case LARGE_LITERAL:
135 return new large(*((uint8_t*)op.second));
136 }
137 break;
138 }
139 case CHAR_LITERAL: {
140 switch (to) {
141 case BOOL_LITERAL:
142 return new bool(*((char*)op.second));
143 break;
144 case BYTE_LITERAL:
145 return new uint8_t(*((char*)op.second));
146 break;
147 case CHAR_LITERAL:
148 return new char(*((char*)op.second));
149 break;
150 case DOUBLE_LITERAL:
151 return new double(*((char*)op.second));
152 break;
153 case FLOAT_LITERAL:
154 return new float(*((char*)op.second));
155 break;
156 case INT_LITERAL:
157 return new int(*((char*)op.second));
158 break;
159 case LONG_LITERAL:
160 return new long long(*((char*)op.second));
161 break;
162 case SHORT_LITERAL:
163 return new short(*((char*)op.second));
164 break;
165 case STRING_LITERAL:
166 return new std::string(1, *((char*)op.second));
167 break;
168 case UINT_LITERAL:
169 return new uint32_t(*((char*)op.second));
170 break;
171 case ULONG_LITERAL:
172 return new uint64_t(*((char*)op.second));
173 break;
174 case LARGE_LITERAL:
175 return new large(*((char*)op.second));
176 }
177 break;
178 }
179 case DOUBLE_LITERAL: {
180 switch (to) {
181 case BOOL_LITERAL:
182 return new bool(*((double*)op.second));
183 break;
184 case BYTE_LITERAL:
185 return new uint8_t(*((double*)op.second));
186 break;
187 case CHAR_LITERAL:
188 return new char(*((double*)op.second));
189 break;
190 case DOUBLE_LITERAL:
191 return new double(*((double*)op.second));
192 break;
193 case FLOAT_LITERAL:
194 return new float(*((double*)op.second));
195 break;
196 case INT_LITERAL:
197 return new int(*((double*)op.second));
198 break;
199 case LONG_LITERAL:
200 return new long long(*((double*)op.second));
201 break;
202 case SHORT_LITERAL:
203 return new short(*((double*)op.second));
204 break;
205 case STRING_LITERAL:
206 return new std::string(std::to_string(*((double*)op.second)));
207 break;
208 case UINT_LITERAL:
209 return new uint32_t(*((double*)op.second));
210 break;
211 case ULONG_LITERAL:
212 return new uint64_t(*((double*)op.second));
213 break;
214 case LARGE_LITERAL:
215 return new large(*((double*)op.second));
216 }
217 break;
218 }
219 case FLOAT_LITERAL: {
220 switch (to) {
221 case BOOL_LITERAL:
222 return new bool(*((float*)op.second));
223 break;
224 case BYTE_LITERAL:
225 return new uint8_t(*((float*)op.second));
226 break;
227 case CHAR_LITERAL:
228 return new char(*((float*)op.second));
229 break;
230 case DOUBLE_LITERAL:
231 return new double(*((float*)op.second));
232 break;
233 case FLOAT_LITERAL:
234 return new float(*((float*)op.second));
235 break;
236 case INT_LITERAL:
237 return new int(*((float*)op.second));
238 break;
239 case LONG_LITERAL:
240 return new long long(*((float*)op.second));
241 break;
242 case SHORT_LITERAL:
243 return new short(*((float*)op.second));
244 break;
245 case STRING_LITERAL:
246 return new std::string(std::to_string(*((float*)op.second)));
247 break;
248 case UINT_LITERAL:
249 return new uint32_t(*((float*)op.second));
250 break;
251 case ULONG_LITERAL:
252 return new uint64_t(*((float*)op.second));
253 break;
254 case LARGE_LITERAL:
255 return new large(*((float*)op.second));
256 }
257 break;
258 }
259 case INT_LITERAL: {
260 switch (to) {
261 case BOOL_LITERAL:
262 return new bool(*((int*)op.second));
263 break;
264 case BYTE_LITERAL:
265 return new uint8_t(*((int*)op.second));
266 break;
267 case CHAR_LITERAL:
268 return new char(*((int*)op.second));
269 break;
270 case DOUBLE_LITERAL:
271 return new double(*((int*)op.second));
272 break;
273 case FLOAT_LITERAL:
274 return new float(*((int*)op.second));
275 break;
276 case INT_LITERAL:
277 return new int(*((int*)op.second));
278 break;
279 case LONG_LITERAL:
280 return new long long(*((int*)op.second));
281 break;
282 case SHORT_LITERAL:
283 return new short(*((int*)op.second));
284 break;
285 case STRING_LITERAL:
286 return new std::string(std::to_string(*((int*)op.second)));
287 break;
288 case UINT_LITERAL:
289 return new uint32_t(*((int*)op.second));
290 break;
291 case ULONG_LITERAL:
292 return new uint64_t(*((int*)op.second));
293 break;
294 case LARGE_LITERAL:
295 return new large(*((int*)op.second));
296 }
297 break;
298 }
299 case LONG_LITERAL: {
300 switch (to) {
301 case BOOL_LITERAL:
302 return new bool(*((long long*)op.second));
303 break;
304 case BYTE_LITERAL:
305 return new uint8_t(*((long long*)op.second));
306 break;
307 case CHAR_LITERAL:
308 return new char(*((long long*)op.second));
309 break;
310 case DOUBLE_LITERAL:
311 return new double(*((long long*)op.second));
312 break;
313 case FLOAT_LITERAL:
314 return new float(*((long long*)op.second));
315 break;
316 case INT_LITERAL:
317 return new int(*((long long*)op.second));
318 break;
319 case LONG_LITERAL:
320 return new long long(*((long long*)op.second));
321 break;
322 case SHORT_LITERAL:
323 return new short(*((long long*)op.second));
324 break;
325 case STRING_LITERAL:
326 return new std::string(std::to_string(*((long long*)op.second)));
327 break;
328 case UINT_LITERAL:
329 return new uint32_t(*((long long*)op.second));
330 break;
331 case ULONG_LITERAL:
332 return new uint64_t(*((long long*)op.second));
333 break;
334 case LARGE_LITERAL:
335 return new large(*((long long*)op.second));
336 }
337 break;
338 }
339 case SHORT_LITERAL: {
340 switch (to) {
341 case BOOL_LITERAL:
342 return new bool(*((short*)op.second));
343 break;
344 case BYTE_LITERAL:
345 return new uint8_t(*((short*)op.second));
346 break;
347 case CHAR_LITERAL:
348 return new char(*((short*)op.second));
349 break;
350 case DOUBLE_LITERAL:
351 return new double(*((short*)op.second));
352 break;
353 case FLOAT_LITERAL:
354 return new float(*((short*)op.second));
355 break;
356 case INT_LITERAL:
357 return new int(*((short*)op.second));
358 break;
359 case LONG_LITERAL:
360 return new long long(*((short*)op.second));
361 break;
362 case SHORT_LITERAL:
363 return new short(*((short*)op.second));
364 break;
365 case STRING_LITERAL:
366 return new std::string(std::to_string(*((short*)op.second)));
367 break;
368 case UINT_LITERAL:
369 return new uint32_t(*((short*)op.second));
370 break;
371 case ULONG_LITERAL:
372 return new uint64_t(*((short*)op.second));
373 break;
374 case LARGE_LITERAL:
375 return new large(*((short*)op.second));
376 }
377 break;
378 }
379 case STRING_LITERAL: {
380 switch (to) {
381 case BOOL_LITERAL:
382 return new bool(*((std::string*)op.second) == "true");
383 break;
384 case BYTE_LITERAL:
385 return new uint8_t(std::stoll(*((std::string*)op.second)));
386 break;
387 case CHAR_LITERAL:
388 return new char((*((std::string*)op.second))[0]);
389 break;
390 case DOUBLE_LITERAL:
391 return new double(std::stold(*((std::string*)op.second)));
392 break;
393 case FLOAT_LITERAL:
394 return new float(std::stold(*((std::string*)op.second)));
395 break;
396 case INT_LITERAL:
397 return new int(std::stoll(*((std::string*)op.second)));
398 break;
399 case LONG_LITERAL:
400 return new long long(std::stoll(*((std::string*)op.second)));
401 break;
402 case SHORT_LITERAL:
403 return new short(std::stoll(*((std::string*)op.second)));
404 break;
405 case STRING_LITERAL:
406 return new std::string(*((std::string*)op.second));
407 break;
408 case UINT_LITERAL:
409 return new uint32_t(std::stoll(*((std::string*)op.second)));
410 break;
411 case ULONG_LITERAL:
412 return new uint64_t(std::stoll(*((std::string*)op.second)));
413 break;
414 case LARGE_LITERAL:
415 return new large(*((std::string*)op.second));
416 }
417 break;
418 } case UINT_LITERAL: {
419 switch (to) {
420 case BOOL_LITERAL:
421 return new bool(*((uint32_t*)op.second));
422 break;
423 case BYTE_LITERAL:
424 return new uint8_t(*((uint32_t*)op.second));
425 break;
426 case CHAR_LITERAL:
427 return new char(*((uint32_t*)op.second));
428 break;
429 case DOUBLE_LITERAL:
430 return new double(*((uint32_t*)op.second));
431 break;
432 case FLOAT_LITERAL:
433 return new float(*((uint32_t*)op.second));
434 break;
435 case INT_LITERAL:
436 return new int(*((uint32_t*)op.second));
437 break;
438 case LONG_LITERAL:
439 return new long long(*((uint32_t*)op.second));
440 break;
441 case SHORT_LITERAL:
442 return new short(*((uint32_t*)op.second));
443 break;
444 case STRING_LITERAL:
445 return new std::string(std::to_string(*((uint32_t*)op.second)));
446 break;
447 case UINT_LITERAL:
448 return new uint32_t(*((uint32_t*)op.second));
449 break;
450 case ULONG_LITERAL:
451 return new uint64_t(*((uint32_t*)op.second));
452 break;
453 case LARGE_LITERAL:
454 return new large(*((uint32_t*)op.second));
455 }
456 break;
457 } case ULONG_LITERAL: {
458 switch (to) {
459 case BOOL_LITERAL:
460 return new bool(*((uint64_t*)op.second));
461 break;
462 case BYTE_LITERAL:
463 return new uint8_t(*((uint64_t*)op.second));
464 break;
465 case CHAR_LITERAL:
466 return new char(*((uint64_t*)op.second));
467 break;
468 case DOUBLE_LITERAL:
469 return new double(*((uint64_t*)op.second));
470 break;
471 case FLOAT_LITERAL:
472 return new float(*((uint64_t*)op.second));
473 break;
474 case INT_LITERAL:
475 return new int(*((uint64_t*)op.second));
476 break;
477 case LONG_LITERAL:
478 return new long long(*((uint64_t*)op.second));
479 break;
480 case SHORT_LITERAL:
481 return new short(*((uint64_t*)op.second));
482 break;
483 case STRING_LITERAL:
484 return new std::string(std::to_string(*((uint64_t*)op.second)));
485 break;
486 case UINT_LITERAL:
487 return new uint32_t(*((uint64_t*)op.second));
488 break;
489 case ULONG_LITERAL:
490 return new uint64_t(*((uint64_t*)op.second));
491 break;
492 case LARGE_LITERAL:
493 return new large(*((uint64_t*)op.second));
494 }
495 break;
496 }
497 case LARGE_LITERAL: {
498 switch (to) {
499 case BOOL_LITERAL:
500 return new bool(std::stoll(((large*)op.second)->ToString()));
501 break;
502 case BYTE_LITERAL:
503 return new uint8_t(std::stoll(((large*)op.second)->ToString()));
504 break;
505 case CHAR_LITERAL:
506 return new char(std::stoll(((large*)op.second)->ToString()));
507 break;
508 case DOUBLE_LITERAL:
509 return new double(std::stoll(((large*)op.second)->ToString()));
510 break;
511 case FLOAT_LITERAL:
512 return new float(std::stoll(((large*)op.second)->ToString()));
513 break;
514 case INT_LITERAL:
515 return new int(std::stoll(((large*)op.second)->ToString()));
516 break;
517 case LONG_LITERAL:
518 return new long long(std::stoll(((large*)op.second)->ToString()));
519 break;
520 case SHORT_LITERAL:
521 return new short(std::stoll(((large*)op.second)->ToString()));
522 break;
523 case STRING_LITERAL:
524 return new std::string(((large*)op.second)->ToString());
525 break;
526 case UINT_LITERAL:
527 return new uint32_t(std::stoll(((large*)op.second)->ToString()));
528 break;
529 case ULONG_LITERAL:
530 return new uint64_t(std::stoll(((large*)op.second)->ToString()));
531 break;
532 case LARGE_LITERAL:
533 return new large(*((large*)op.second));
534 }
535 break;
536 }
537 }
538 throw std::exception("Cannot convert types");
539}
540std::pair<Poliz::PolizType, std::pair<void*, void*>> Poliz::make_same(std::pair<Poliz::PolizType, void*> op1, std::pair<Poliz::PolizType, void*> op2) {
541 int pr1 = literal_prior[(int)op1.first - (int)Poliz::PolizType::BOOL_LITERAL], pr2 = literal_prior[(int)op2.first - (int)Poliz::PolizType::BOOL_LITERAL];
542 if (pr1 >= pr2)
543 return std::pair<Poliz::PolizType, std::pair<void*, void*>>(op1.first, { op1.second, convert(op2, op1.first) });
544 else
545 return std::pair<Poliz::PolizType, std::pair<void*, void*>>(op2.first, { convert(op1, op2.first), op2.second });
546
547}
548std::pair<Poliz::PolizType, void*> Poliz::address_to_value(void* ptr) {
549 Identifier* ident = (Identifier*)ptr;
550 if (ident->value == nullptr) {
551 throw std::exception(("Variable '" + ident->name + "' was not initialized").c_str());
552 }
553 switch (ident->type.expr_type) {
554 case ExprType::Bool:
555 return { Poliz::PolizType::BOOL_LITERAL, (bool*)(ident->value) };
556 case ExprType::Byte:
557 return { Poliz::PolizType::BYTE_LITERAL, (uint8_t*)(ident->value) };
558 case ExprType::Char:
559 return { Poliz::PolizType::CHAR_LITERAL, (char*)(ident->value) };
560 case ExprType::Double:
561 return { Poliz::PolizType::DOUBLE_LITERAL, (double*)(ident->value) };
562 case ExprType::Float:
563 return { Poliz::PolizType::FLOAT_LITERAL, (float*)(ident->value) };
564 case ExprType::Int:
565 return { Poliz::PolizType::INT_LITERAL, (int*)(ident->value) };
566 case ExprType::Long:
567 return { Poliz::PolizType::LONG_LITERAL, (long long*)(ident->value) };
568 case ExprType::Short:
569 return { Poliz::PolizType::SHORT_LITERAL, (short*)(ident->value) };
570 case ExprType::String:
571 return { Poliz::PolizType::STRING_LITERAL, (std::string*)(ident->value) };
572 case ExprType::UInt:
573 return { Poliz::PolizType::UINT_LITERAL, (uint32_t*)(ident->value) };
574 case ExprType::ULong:
575 return { Poliz::PolizType::ULONG_LITERAL, (uint64_t*)(ident->value) };
576 case ExprType::Large:
577 return { Poliz::PolizType::LARGE_LITERAL, (large*)(ident->value) };
578 default:
579 throw std::exception(("Variable '" + ident->name + "' has unconvertable value").c_str());
580 }
581}
582void* Poliz::copy_value(void* ptr) {
583 Identifier* ident = (Identifier*)ptr;
584 if (ident->value == nullptr)
585 return nullptr;
586 switch (ident->type.expr_type) {
587 case ExprType::Bool:
588 return new bool(*(bool*)(ident->value));
589 case ExprType::Byte:
590 return new uint8_t(*(uint8_t*)(ident->value));
591 case ExprType::Char:
592 return new char(*(char*)(ident->value));
593 case ExprType::Double:
594 return new double(*(double*)(ident->value));
595 case ExprType::Float:
596 return new float(*(float*)(ident->value));
597 case ExprType::Int:
598 return new int(*(int*)(ident->value));
599 case ExprType::Long:
600 return new long long(*(long long*)(ident->value));
601 case ExprType::Short:
602 return new short(*(short*)(ident->value));
603 case ExprType::String:
604 return new std::string(*(std::string*)(ident->value));
605 case ExprType::UInt:
606 return new uint32_t(*(uint32_t*)(ident->value));
607 case ExprType::ULong:
608 return new uint64_t(*(uint64_t*)(ident->value));
609 case ExprType::Large:
610 return new large(*(large*)(ident->value));
611 default:
612 throw std::exception(("Variable '" + ident->name + "' has unconvertable value").c_str());
613 }
614}
616 switch (type) {
617 case Bool:
619 case Char:
621 case Double:
623 case Float:
625 case Int:
627 case UInt:
629 case Long:
631 case ULong:
633 case Short:
635 case Byte:
637 case String:
639 case Large:
641 default:
643 }
644}
645std::pair<Poliz::PolizType, void*> Poliz::execute_operation(std::pair<Poliz::PolizType, void*> op1, std::pair<Poliz::PolizType, void*> op2, std::string operation) {
646 if (op1.first == Poliz::PolizType::ADDRESS)
647 op1 = address_to_value(op1.second);
648 if (op2.first == Poliz::PolizType::ADDRESS)
649 op2 = address_to_value(op2.second);
650 auto conv = make_same(op1, op2);
651 if (operation == "+") {
652 switch (conv.first) {
653 case BOOL_LITERAL:
654 return { conv.first, new bool(*((bool*)conv.second.first) + *((bool*)conv.second.second)) };
655 case BYTE_LITERAL:
656 return { conv.first, new uint8_t(*((uint8_t*)conv.second.first) + *((uint8_t*)conv.second.second)) };
657 case CHAR_LITERAL:
658 return { conv.first, new char(*((char*)conv.second.first) + *((char*)conv.second.second)) };
659 case DOUBLE_LITERAL:
660 return { conv.first, new double(*((double*)conv.second.first) + *((double*)conv.second.second)) };
661 case FLOAT_LITERAL:
662 return { conv.first, new float(*((float*)conv.second.first) + *((float*)conv.second.second)) };
663 case INT_LITERAL:
664 return { conv.first, new int(*((int*)conv.second.first) + *((int*)conv.second.second)) };
665 case LONG_LITERAL:
666 return { conv.first, new long long(*((long long*)conv.second.first) + *((long long*)conv.second.second)) };
667 case SHORT_LITERAL:
668 return { conv.first, new short(*((short*)conv.second.first) + *((short*)conv.second.second)) };
669 case STRING_LITERAL:
670 return { conv.first, new std::string(*((std::string*)conv.second.first) + *((std::string*)conv.second.second)) };
671 case UINT_LITERAL:
672 return { conv.first, new uint32_t(*((uint32_t*)conv.second.first) + *((uint32_t*)conv.second.second)) };
673 case ULONG_LITERAL:
674 return { conv.first, new uint64_t(*((uint64_t*)conv.second.first) + *((uint64_t*)conv.second.second)) };
675 case LARGE_LITERAL:
676 return { conv.first, new large(*((large*)conv.second.first) + *((large*)conv.second.second)) };
677 default:
678 throw std::exception("Cannot use this operation");
679 }
680 } else if (operation == "-") {
681 switch (conv.first) {
682 case BOOL_LITERAL:
683 return { conv.first, new bool(*((bool*)conv.second.first) - *((bool*)conv.second.second)) };
684 case BYTE_LITERAL:
685 return { conv.first, new uint8_t(*((uint8_t*)conv.second.first) - *((uint8_t*)conv.second.second)) };
686 case CHAR_LITERAL:
687 return { conv.first, new char(*((char*)conv.second.first) - *((char*)conv.second.second)) };
688 case DOUBLE_LITERAL:
689 return { conv.first, new double(*((double*)conv.second.first) - *((double*)conv.second.second)) };
690 case FLOAT_LITERAL:
691 return { conv.first, new float(*((float*)conv.second.first) - *((float*)conv.second.second)) };
692 case INT_LITERAL:
693 return { conv.first, new int(*((int*)conv.second.first) - *((int*)conv.second.second)) };
694 case LONG_LITERAL:
695 return { conv.first, new long long(*((long long*)conv.second.first) - *((long long*)conv.second.second)) };
696 case SHORT_LITERAL:
697 return { conv.first, new short(*((short*)conv.second.first) - *((short*)conv.second.second)) };
698 case UINT_LITERAL:
699 return { conv.first, new uint32_t(*((uint32_t*)conv.second.first) - *((uint32_t*)conv.second.second)) };
700 case ULONG_LITERAL:
701 return { conv.first, new uint64_t(*((uint64_t*)conv.second.first) - *((uint64_t*)conv.second.second)) };
702 case LARGE_LITERAL:
703 return { conv.first, new large(*((large*)conv.second.first) - *((large*)conv.second.second)) };
704 default:
705 throw std::exception("Cannot use this operation");
706 }
707 } else if (operation == "*") {
708 switch (conv.first) {
709 case BOOL_LITERAL:
710 return { conv.first, new bool(*((bool*)conv.second.first) * *((bool*)conv.second.second)) };
711 case BYTE_LITERAL:
712 return { conv.first, new uint8_t(*((uint8_t*)conv.second.first) * *((uint8_t*)conv.second.second)) };
713 case CHAR_LITERAL:
714 return { conv.first, new char(*((char*)conv.second.first) * *((char*)conv.second.second)) };
715 case DOUBLE_LITERAL:
716 return { conv.first, new double(*((double*)conv.second.first) * *((double*)conv.second.second)) };
717 case FLOAT_LITERAL:
718 return { conv.first, new float(*((float*)conv.second.first) * *((float*)conv.second.second)) };
719 case INT_LITERAL:
720 return { conv.first, new int(*((int*)conv.second.first) * *((int*)conv.second.second)) };
721 case LONG_LITERAL:
722 return { conv.first, new long long(*((long long*)conv.second.first) * *((long long*)conv.second.second)) };
723 case SHORT_LITERAL:
724 return { conv.first, new short(*((short*)conv.second.first) * *((short*)conv.second.second)) };
725 case UINT_LITERAL:
726 return { conv.first, new uint32_t(*((uint32_t*)conv.second.first) * *((uint32_t*)conv.second.second)) };
727 case ULONG_LITERAL:
728 return { conv.first, new uint64_t(*((uint64_t*)conv.second.first) * *((uint64_t*)conv.second.second)) };
729 case LARGE_LITERAL:
730 return { conv.first, new large(*((large*)conv.second.first) * *((large*)conv.second.second)) };
731 default:
732 throw std::exception("Cannot use this operation");
733 }
734 } else if (operation == "/") {
735 switch (conv.first) {
736 case BOOL_LITERAL:
737 return { conv.first, new bool(*((bool*)conv.second.first) / *((bool*)conv.second.second)) };
738 case BYTE_LITERAL:
739 return { conv.first, new uint8_t(*((uint8_t*)conv.second.first) / *((uint8_t*)conv.second.second)) };
740 case CHAR_LITERAL:
741 return { conv.first, new char(*((char*)conv.second.first) / *((char*)conv.second.second)) };
742 case DOUBLE_LITERAL:
743 return { conv.first, new double(*((double*)conv.second.first) / *((double*)conv.second.second)) };
744 case FLOAT_LITERAL:
745 return { conv.first, new float(*((float*)conv.second.first) / *((float*)conv.second.second)) };
746 case INT_LITERAL:
747 return { conv.first, new int(*((int*)conv.second.first) / *((int*)conv.second.second)) };
748 case LONG_LITERAL:
749 return { conv.first, new long long(*((long long*)conv.second.first) / *((long long*)conv.second.second)) };
750 case SHORT_LITERAL:
751 return { conv.first, new short(*((short*)conv.second.first) / *((short*)conv.second.second)) };
752 case UINT_LITERAL:
753 return { conv.first, new uint32_t(*((uint32_t*)conv.second.first) / *((uint32_t*)conv.second.second)) };
754 case ULONG_LITERAL:
755 return { conv.first, new uint64_t(*((uint64_t*)conv.second.first) / *((uint64_t*)conv.second.second)) };
756 case LARGE_LITERAL:
757 return { conv.first, new large(*((large*)conv.second.first) / *((large*)conv.second.second)) };
758 default:
759 throw std::exception("Cannot use this operation");
760 }
761 } else if (operation == "%") {
762 switch (conv.first) {
763 case BOOL_LITERAL:
764 return { conv.first, new bool(*((bool*)conv.second.first) % *((bool*)conv.second.second)) };
765 case BYTE_LITERAL:
766 return { conv.first, new uint8_t(*((uint8_t*)conv.second.first) % *((uint8_t*)conv.second.second)) };
767 case CHAR_LITERAL:
768 return { conv.first, new char(*((char*)conv.second.first) % *((char*)conv.second.second)) };
769 case INT_LITERAL:
770 return { conv.first, new int(*((int*)conv.second.first) % *((int*)conv.second.second)) };
771 case LONG_LITERAL:
772 return { conv.first, new long long(*((long long*)conv.second.first) % *((long long*)conv.second.second)) };
773 case SHORT_LITERAL:
774 return { conv.first, new short(*((short*)conv.second.first) % *((short*)conv.second.second)) };
775 case UINT_LITERAL:
776 return { conv.first, new uint32_t(*((uint32_t*)conv.second.first) % *((uint32_t*)conv.second.second)) };
777 case ULONG_LITERAL:
778 return { conv.first, new uint64_t(*((uint64_t*)conv.second.first) % *((uint64_t*)conv.second.second)) };
779 case LARGE_LITERAL:
780 return { conv.first, new large(*((large*)conv.second.first) % *((large*)conv.second.second)) };
781 default:
782 throw std::exception("Cannot use this operation");
783 }
784 } else if (operation == ">>") {
785 switch (conv.first) {
786 case BOOL_LITERAL:
787 return { conv.first, new bool(*((bool*)conv.second.first) >> *((bool*)conv.second.second)) };
788 case BYTE_LITERAL:
789 return { conv.first, new uint8_t(*((uint8_t*)conv.second.first) >> *((uint8_t*)conv.second.second)) };
790 case CHAR_LITERAL:
791 return { conv.first, new char(*((char*)conv.second.first) >> *((char*)conv.second.second)) };
792 case INT_LITERAL:
793 return { conv.first, new int(*((int*)conv.second.first) >> *((int*)conv.second.second)) };
794 case LONG_LITERAL:
795 return { conv.first, new long long(*((long long*)conv.second.first) >> *((long long*)conv.second.second)) };
796 case SHORT_LITERAL:
797 return { conv.first, new short(*((short*)conv.second.first) >> *((short*)conv.second.second)) };
798 case UINT_LITERAL:
799 return { conv.first, new uint32_t(*((uint32_t*)conv.second.first) >> *((uint32_t*)conv.second.second)) };
800 case ULONG_LITERAL:
801 return { conv.first, new uint64_t(*((uint64_t*)conv.second.first) >> *((uint64_t*)conv.second.second)) };
802 default:
803 throw std::exception("Cannot use this operation");
804 }
805 } else if (operation == "<<") {
806 switch (conv.first) {
807 case BOOL_LITERAL:
808 return { conv.first, new bool(*((bool*)conv.second.first) << *((bool*)conv.second.second)) };
809 case BYTE_LITERAL:
810 return { conv.first, new uint8_t(*((uint8_t*)conv.second.first) << *((uint8_t*)conv.second.second)) };
811 case CHAR_LITERAL:
812 return { conv.first, new char(*((char*)conv.second.first) << *((char*)conv.second.second)) };
813 case INT_LITERAL:
814 return { conv.first, new int(*((int*)conv.second.first) << *((int*)conv.second.second)) };
815 case LONG_LITERAL:
816 return { conv.first, new long long(*((long long*)conv.second.first) << *((long long*)conv.second.second)) };
817 case SHORT_LITERAL:
818 return { conv.first, new short(*((short*)conv.second.first) << *((short*)conv.second.second)) };
819 case UINT_LITERAL:
820 return { conv.first, new uint32_t(*((uint32_t*)conv.second.first) << *((uint32_t*)conv.second.second)) };
821 case ULONG_LITERAL:
822 return { conv.first, new uint64_t(*((uint64_t*)conv.second.first) << *((uint64_t*)conv.second.second)) };
823 default:
824 throw std::exception("Cannot use this operation");
825 }
826 } else if (operation == "<") {
827 switch (conv.first) {
828 case BOOL_LITERAL:
829 return { BOOL_LITERAL, new bool(*((bool*)conv.second.first) < *((bool*)conv.second.second)) };
830 case BYTE_LITERAL:
831 return { BOOL_LITERAL, new bool(*((uint8_t*)conv.second.first) < *((uint8_t*)conv.second.second)) };
832 case CHAR_LITERAL:
833 return { BOOL_LITERAL, new bool(*((char*)conv.second.first) < *((char*)conv.second.second)) };
834 case DOUBLE_LITERAL:
835 return { BOOL_LITERAL, new bool(*((double*)conv.second.first) < *((double*)conv.second.second)) };
836 case FLOAT_LITERAL:
837 return { BOOL_LITERAL, new bool(*((float*)conv.second.first) < *((float*)conv.second.second)) };
838 case INT_LITERAL:
839 return { BOOL_LITERAL, new bool(*((int*)conv.second.first) < *((int*)conv.second.second)) };
840 case LONG_LITERAL:
841 return { BOOL_LITERAL, new bool(*((long long*)conv.second.first) < *((long long*)conv.second.second)) };
842 case SHORT_LITERAL:
843 return { BOOL_LITERAL, new bool(*((short*)conv.second.first) < *((short*)conv.second.second)) };
844 case STRING_LITERAL:
845 return { BOOL_LITERAL, new bool(*((std::string*)conv.second.first) < *((std::string*)conv.second.second)) };
846 case UINT_LITERAL:
847 return { BOOL_LITERAL, new bool(*((uint32_t*)conv.second.first) < *((uint32_t*)conv.second.second)) };
848 case ULONG_LITERAL:
849 return { BOOL_LITERAL, new bool(*((uint64_t*)conv.second.first) < *((uint64_t*)conv.second.second)) };
850 case LARGE_LITERAL:
851 return { conv.first, new large(*((large*)conv.second.first) < *((large*)conv.second.second)) };
852 default:
853 throw std::exception("Cannot use this operation");
854 }
855 } else if (operation == "<=") {
856 switch (conv.first) {
857 case BOOL_LITERAL:
858 return { BOOL_LITERAL, new bool(*((bool*)conv.second.first) <= *((bool*)conv.second.second)) };
859 case BYTE_LITERAL:
860 return { BOOL_LITERAL, new bool(*((uint8_t*)conv.second.first) <= *((uint8_t*)conv.second.second)) };
861 case CHAR_LITERAL:
862 return { BOOL_LITERAL, new bool(*((char*)conv.second.first) <= *((char*)conv.second.second)) };
863 case DOUBLE_LITERAL:
864 return { BOOL_LITERAL, new bool(*((double*)conv.second.first) <= *((double*)conv.second.second)) };
865 case FLOAT_LITERAL:
866 return { BOOL_LITERAL, new bool(*((float*)conv.second.first) <= *((float*)conv.second.second)) };
867 case INT_LITERAL:
868 return { BOOL_LITERAL, new bool(*((int*)conv.second.first) <= *((int*)conv.second.second)) };
869 case LONG_LITERAL:
870 return { BOOL_LITERAL, new bool(*((long long*)conv.second.first) <= *((long long*)conv.second.second)) };
871 case SHORT_LITERAL:
872 return { BOOL_LITERAL, new bool(*((short*)conv.second.first) <= *((short*)conv.second.second)) };
873 case STRING_LITERAL:
874 return { BOOL_LITERAL, new bool(*((std::string*)conv.second.first) <= *((std::string*)conv.second.second)) };
875 case UINT_LITERAL:
876 return { BOOL_LITERAL, new bool(*((uint32_t*)conv.second.first) <= *((uint32_t*)conv.second.second)) };
877 case ULONG_LITERAL:
878 return { BOOL_LITERAL, new bool(*((uint64_t*)conv.second.first) <= *((uint64_t*)conv.second.second)) };
879 case LARGE_LITERAL:
880 return { conv.first, new large(*((large*)conv.second.first) <= *((large*)conv.second.second)) };
881 default:
882 throw std::exception("Cannot use this operation");
883 }
884 } else if (operation == ">") {
885 switch (conv.first) {
886 case BOOL_LITERAL:
887 return { BOOL_LITERAL, new bool(*((bool*)conv.second.first) > *((bool*)conv.second.second)) };
888 case BYTE_LITERAL:
889 return { BOOL_LITERAL, new bool(*((uint8_t*)conv.second.first) > *((uint8_t*)conv.second.second)) };
890 case CHAR_LITERAL:
891 return { BOOL_LITERAL, new bool(*((char*)conv.second.first) > *((char*)conv.second.second)) };
892 case DOUBLE_LITERAL:
893 return { BOOL_LITERAL, new bool(*((double*)conv.second.first) > *((double*)conv.second.second)) };
894 case FLOAT_LITERAL:
895 return { BOOL_LITERAL, new bool(*((float*)conv.second.first) > *((float*)conv.second.second)) };
896 case INT_LITERAL:
897 return { BOOL_LITERAL, new bool(*((int*)conv.second.first) > *((int*)conv.second.second)) };
898 case LONG_LITERAL:
899 return { BOOL_LITERAL, new bool(*((long long*)conv.second.first) > *((long long*)conv.second.second)) };
900 case SHORT_LITERAL:
901 return { BOOL_LITERAL, new bool(*((short*)conv.second.first) > *((short*)conv.second.second)) };
902 case STRING_LITERAL:
903 return { BOOL_LITERAL, new bool(*((std::string*)conv.second.first) > *((std::string*)conv.second.second)) };
904 case UINT_LITERAL:
905 return { BOOL_LITERAL, new bool(*((uint32_t*)conv.second.first) > *((uint32_t*)conv.second.second)) };
906 case ULONG_LITERAL:
907 return { BOOL_LITERAL, new bool(*((uint64_t*)conv.second.first) > *((uint64_t*)conv.second.second)) };
908 case LARGE_LITERAL:
909 return { conv.first, new large(*((large*)conv.second.first) > *((large*)conv.second.second)) };
910 default:
911 throw std::exception("Cannot use this operation");
912 }
913 } else if (operation == ">=") {
914 switch (conv.first) {
915 case BOOL_LITERAL:
916 return { BOOL_LITERAL, new bool(*((bool*)conv.second.first) >= *((bool*)conv.second.second)) };
917 case BYTE_LITERAL:
918 return { BOOL_LITERAL, new bool(*((uint8_t*)conv.second.first) >= *((uint8_t*)conv.second.second)) };
919 case CHAR_LITERAL:
920 return { BOOL_LITERAL, new bool(*((char*)conv.second.first) >= *((char*)conv.second.second)) };
921 case DOUBLE_LITERAL:
922 return { BOOL_LITERAL, new bool(*((double*)conv.second.first) >= *((double*)conv.second.second)) };
923 case FLOAT_LITERAL:
924 return { BOOL_LITERAL, new bool(*((float*)conv.second.first) >= *((float*)conv.second.second)) };
925 case INT_LITERAL:
926 return { BOOL_LITERAL, new bool(*((int*)conv.second.first) >= *((int*)conv.second.second)) };
927 case LONG_LITERAL:
928 return { BOOL_LITERAL, new bool(*((long long*)conv.second.first) >= *((long long*)conv.second.second)) };
929 case SHORT_LITERAL:
930 return { BOOL_LITERAL, new bool(*((short*)conv.second.first) >= *((short*)conv.second.second)) };
931 case STRING_LITERAL:
932 return { BOOL_LITERAL, new bool(*((std::string*)conv.second.first) >= *((std::string*)conv.second.second)) };
933 case UINT_LITERAL:
934 return { BOOL_LITERAL, new bool(*((uint32_t*)conv.second.first) >= *((uint32_t*)conv.second.second)) };
935 case ULONG_LITERAL:
936 return { BOOL_LITERAL, new bool(*((uint64_t*)conv.second.first) >= *((uint64_t*)conv.second.second)) };
937 case LARGE_LITERAL:
938 return { conv.first, new large(*((large*)conv.second.first) >= *((large*)conv.second.second)) };
939 default:
940 throw std::exception("Cannot use this operation");
941 }
942 } else if (operation == "==") {
943 switch (conv.first) {
944 case BOOL_LITERAL:
945 return { BOOL_LITERAL, new bool(*((bool*)conv.second.first) == *((bool*)conv.second.second)) };
946 case BYTE_LITERAL:
947 return { BOOL_LITERAL, new bool(*((uint8_t*)conv.second.first) == *((uint8_t*)conv.second.second)) };
948 case CHAR_LITERAL:
949 return { BOOL_LITERAL, new bool(*((char*)conv.second.first) == *((char*)conv.second.second)) };
950 case DOUBLE_LITERAL:
951 return { BOOL_LITERAL, new bool(*((double*)conv.second.first) == *((double*)conv.second.second)) };
952 case FLOAT_LITERAL:
953 return { BOOL_LITERAL, new bool(*((float*)conv.second.first) == *((float*)conv.second.second)) };
954 case INT_LITERAL:
955 return { BOOL_LITERAL, new bool(*((int*)conv.second.first) == *((int*)conv.second.second)) };
956 case LONG_LITERAL:
957 return { BOOL_LITERAL, new bool(*((long long*)conv.second.first) == *((long long*)conv.second.second)) };
958 case SHORT_LITERAL:
959 return { BOOL_LITERAL, new bool(*((short*)conv.second.first) == *((short*)conv.second.second)) };
960 case STRING_LITERAL:
961 return { BOOL_LITERAL, new bool(*((std::string*)conv.second.first) == *((std::string*)conv.second.second)) };
962 case UINT_LITERAL:
963 return { BOOL_LITERAL, new bool(*((uint32_t*)conv.second.first) == *((uint32_t*)conv.second.second)) };
964 case ULONG_LITERAL:
965 return { BOOL_LITERAL, new bool(*((uint64_t*)conv.second.first) == *((uint64_t*)conv.second.second)) };
966 case LARGE_LITERAL:
967 return { conv.first, new large(*((large*)conv.second.first) == *((large*)conv.second.second)) };
968 default:
969 throw std::exception("Cannot use this operation");
970 }
971 } else if (operation == "!=") {
972 switch (conv.first) {
973 case BOOL_LITERAL:
974 return { BOOL_LITERAL, new bool(*((bool*)conv.second.first) != *((bool*)conv.second.second)) };
975 case BYTE_LITERAL:
976 return { BOOL_LITERAL, new bool(*((uint8_t*)conv.second.first) != *((uint8_t*)conv.second.second)) };
977 case CHAR_LITERAL:
978 return { BOOL_LITERAL, new bool(*((char*)conv.second.first) != *((char*)conv.second.second)) };
979 case DOUBLE_LITERAL:
980 return { BOOL_LITERAL, new bool(*((double*)conv.second.first) != *((double*)conv.second.second)) };
981 case FLOAT_LITERAL:
982 return { BOOL_LITERAL, new bool(*((float*)conv.second.first) != *((float*)conv.second.second)) };
983 case INT_LITERAL:
984 return { BOOL_LITERAL, new bool(*((int*)conv.second.first) != *((int*)conv.second.second)) };
985 case LONG_LITERAL:
986 return { BOOL_LITERAL, new bool(*((long long*)conv.second.first) != *((long long*)conv.second.second)) };
987 case SHORT_LITERAL:
988 return { BOOL_LITERAL, new bool(*((short*)conv.second.first) != *((short*)conv.second.second)) };
989 case STRING_LITERAL:
990 return { BOOL_LITERAL, new bool(*((std::string*)conv.second.first) != *((std::string*)conv.second.second)) };
991 case UINT_LITERAL:
992 return { BOOL_LITERAL, new bool(*((uint32_t*)conv.second.first) != *((uint32_t*)conv.second.second)) };
993 case ULONG_LITERAL:
994 return { BOOL_LITERAL, new bool(*((uint64_t*)conv.second.first) != *((uint64_t*)conv.second.second)) };
995 case LARGE_LITERAL:
996 return { conv.first, new large(*((large*)conv.second.first) != *((large*)conv.second.second)) };
997 default:
998 throw std::exception("Cannot use this operation");
999 }
1000 } else if (operation == "&") {
1001 switch (conv.first) {
1002 case BOOL_LITERAL:
1003 return { conv.first, new bool(*((bool*)conv.second.first) & *((bool*)conv.second.second)) };
1004 case BYTE_LITERAL:
1005 return { conv.first, new uint8_t(*((uint8_t*)conv.second.first) & *((uint8_t*)conv.second.second)) };
1006 case CHAR_LITERAL:
1007 return { conv.first, new char(*((char*)conv.second.first) & *((char*)conv.second.second)) };
1008 case INT_LITERAL:
1009 return { conv.first, new int(*((int*)conv.second.first) & *((int*)conv.second.second)) };
1010 case LONG_LITERAL:
1011 return { conv.first, new long long(*((long long*)conv.second.first) & *((long long*)conv.second.second)) };
1012 case SHORT_LITERAL:
1013 return { conv.first, new short(*((short*)conv.second.first) & *((short*)conv.second.second)) };
1014 case UINT_LITERAL:
1015 return { conv.first, new uint32_t(*((uint32_t*)conv.second.first) & *((uint32_t*)conv.second.second)) };
1016 case ULONG_LITERAL:
1017 return { conv.first, new uint64_t(*((uint64_t*)conv.second.first) & *((uint64_t*)conv.second.second)) };
1018 default:
1019 throw std::exception("Cannot use this operation");
1020 }
1021 } else if (operation == "|") {
1022 switch (conv.first) {
1023 case BOOL_LITERAL:
1024 return { conv.first, new bool(*((bool*)conv.second.first) | *((bool*)conv.second.second)) };
1025 case BYTE_LITERAL:
1026 return { conv.first, new uint8_t(*((uint8_t*)conv.second.first) | *((uint8_t*)conv.second.second)) };
1027 case CHAR_LITERAL:
1028 return { conv.first, new char(*((char*)conv.second.first) | *((char*)conv.second.second)) };
1029 case INT_LITERAL:
1030 return { conv.first, new int(*((int*)conv.second.first) | *((int*)conv.second.second)) };
1031 case LONG_LITERAL:
1032 return { conv.first, new long long(*((long long*)conv.second.first) | *((long long*)conv.second.second)) };
1033 case SHORT_LITERAL:
1034 return { conv.first, new short(*((short*)conv.second.first) | *((short*)conv.second.second)) };
1035 case UINT_LITERAL:
1036 return { conv.first, new uint32_t(*((uint32_t*)conv.second.first) | *((uint32_t*)conv.second.second)) };
1037 case ULONG_LITERAL:
1038 return { conv.first, new uint64_t(*((uint64_t*)conv.second.first) | *((uint64_t*)conv.second.second)) };
1039 default:
1040 throw std::exception("Cannot use this operation");
1041 }
1042 } else if (operation == "^") {
1043 switch (conv.first) {
1044 case BOOL_LITERAL:
1045 return { conv.first, new bool(*((bool*)conv.second.first) ^ *((bool*)conv.second.second)) };
1046 case BYTE_LITERAL:
1047 return { conv.first, new uint8_t(*((uint8_t*)conv.second.first) ^ *((uint8_t*)conv.second.second)) };
1048 case CHAR_LITERAL:
1049 return { conv.first, new char(*((char*)conv.second.first) ^ *((char*)conv.second.second)) };
1050 case INT_LITERAL:
1051 return { conv.first, new int(*((int*)conv.second.first) ^ *((int*)conv.second.second)) };
1052 case LONG_LITERAL:
1053 return { conv.first, new long long(*((long long*)conv.second.first) ^ *((long long*)conv.second.second)) };
1054 case SHORT_LITERAL:
1055 return { conv.first, new short(*((short*)conv.second.first) ^ *((short*)conv.second.second)) };
1056 case UINT_LITERAL:
1057 return { conv.first, new uint32_t(*((uint32_t*)conv.second.first) ^ *((uint32_t*)conv.second.second)) };
1058 case ULONG_LITERAL:
1059 return { conv.first, new uint64_t(*((uint64_t*)conv.second.first) ^ *((uint64_t*)conv.second.second)) };
1060 default:
1061 throw std::exception("Cannot use this operation");
1062 }
1063 } else if (operation == "->") {
1064 switch (conv.first) {
1065 case BOOL_LITERAL:
1066 return { conv.first, new bool(~*((bool*)conv.second.first) | *((bool*)conv.second.second)) };
1067 case BYTE_LITERAL:
1068 return { conv.first, new uint8_t(~*((uint8_t*)conv.second.first) | *((uint8_t*)conv.second.second)) };
1069 case CHAR_LITERAL:
1070 return { conv.first, new char(~*((char*)conv.second.first) | *((char*)conv.second.second)) };
1071 case INT_LITERAL:
1072 return { conv.first, new int(~*((int*)conv.second.first) | *((int*)conv.second.second)) };
1073 case LONG_LITERAL:
1074 return { conv.first, new long long(~*((long long*)conv.second.first) | *((long long*)conv.second.second)) };
1075 case SHORT_LITERAL:
1076 return { conv.first, new short(~*((short*)conv.second.first) | *((short*)conv.second.second)) };
1077 case UINT_LITERAL:
1078 return { conv.first, new uint32_t(~*((uint32_t*)conv.second.first) | *((uint32_t*)conv.second.second)) };
1079 case ULONG_LITERAL:
1080 return { conv.first, new uint64_t(~*((uint64_t*)conv.second.first) | *((uint64_t*)conv.second.second)) };
1081 default:
1082 throw std::exception("Cannot use this operation");
1083 }
1084 } else if (operation == "&&") {
1085 switch (conv.first) {
1086 case BOOL_LITERAL:
1087 return { BOOL_LITERAL, new bool(*((bool*)conv.second.first) && *((bool*)conv.second.second)) };
1088 case BYTE_LITERAL:
1089 return { BOOL_LITERAL, new bool(*((uint8_t*)conv.second.first) && *((uint8_t*)conv.second.second)) };
1090 case CHAR_LITERAL:
1091 return { BOOL_LITERAL, new bool(*((char*)conv.second.first) && *((char*)conv.second.second)) };
1092 case DOUBLE_LITERAL:
1093 return { BOOL_LITERAL, new bool(*((double*)conv.second.first) && *((double*)conv.second.second)) };
1094 case FLOAT_LITERAL:
1095 return { BOOL_LITERAL, new bool(*((float*)conv.second.first) && *((float*)conv.second.second)) };
1096 case INT_LITERAL:
1097 return { BOOL_LITERAL, new bool(*((int*)conv.second.first) && *((int*)conv.second.second)) };
1098 case LONG_LITERAL:
1099 return { BOOL_LITERAL, new bool(*((long long*)conv.second.first) && *((long long*)conv.second.second)) };
1100 case SHORT_LITERAL:
1101 return { BOOL_LITERAL, new bool(*((short*)conv.second.first) && *((short*)conv.second.second)) };
1102 case UINT_LITERAL:
1103 return { BOOL_LITERAL, new bool(*((uint32_t*)conv.second.first) && *((uint32_t*)conv.second.second)) };
1104 case ULONG_LITERAL:
1105 return { BOOL_LITERAL, new bool(*((uint64_t*)conv.second.first) && *((uint64_t*)conv.second.second)) };
1106 default:
1107 throw std::exception("Cannot use this operation");
1108 }
1109 } else if (operation == "||") {
1110 switch (conv.first) {
1111 case BOOL_LITERAL:
1112 return { BOOL_LITERAL, new bool(*((bool*)conv.second.first) || *((bool*)conv.second.second)) };
1113 case BYTE_LITERAL:
1114 return { BOOL_LITERAL, new bool(*((uint8_t*)conv.second.first) || *((uint8_t*)conv.second.second)) };
1115 case CHAR_LITERAL:
1116 return { BOOL_LITERAL, new bool(*((char*)conv.second.first) || *((char*)conv.second.second)) };
1117 case DOUBLE_LITERAL:
1118 return { BOOL_LITERAL, new bool(*((double*)conv.second.first) || *((double*)conv.second.second)) };
1119 case FLOAT_LITERAL:
1120 return { BOOL_LITERAL, new bool(*((float*)conv.second.first) || *((float*)conv.second.second)) };
1121 case INT_LITERAL:
1122 return { BOOL_LITERAL, new bool(*((int*)conv.second.first) || *((int*)conv.second.second)) };
1123 case LONG_LITERAL:
1124 return { BOOL_LITERAL, new bool(*((long long*)conv.second.first) || *((long long*)conv.second.second)) };
1125 case SHORT_LITERAL:
1126 return { BOOL_LITERAL, new bool(*((short*)conv.second.first) || *((short*)conv.second.second)) };
1127 case UINT_LITERAL:
1128 return { BOOL_LITERAL, new bool(*((uint32_t*)conv.second.first) || *((uint32_t*)conv.second.second)) };
1129 case ULONG_LITERAL:
1130 return { BOOL_LITERAL, new bool(*((uint64_t*)conv.second.first) || *((uint64_t*)conv.second.second)) };
1131 default:
1132 throw std::exception("Cannot use this operation");
1133 }
1134 }
1135}
1136std::pair<Poliz::PolizType, void*> Poliz::execute_operation(std::pair<Poliz::PolizType, void*> op, std::string operation) {
1137 Identifier* ident = nullptr;
1138 if (op.first == Poliz::PolizType::ADDRESS) {
1139 ident = (Identifier*)op.second;
1140 op = address_to_value(op.second);
1141 }
1142 if (operation == "++") {
1143 switch (op.first) {
1144 case BOOL_LITERAL: {
1145 auto val = *((bool*)op.second);
1146 ++val;
1147 ident->value = new bool(val);
1148 return { ADDRESS, ident };
1149 }
1150 case BYTE_LITERAL: {
1151 auto val = *((uint8_t*)op.second);
1152 ++val;
1153 ident->value = new uint8_t(val);
1154 return { ADDRESS, ident };
1155 }
1156 case CHAR_LITERAL: {
1157 auto val = *((char*)op.second);
1158 ++val;
1159 ident->value = new char(val);
1160 return { ADDRESS, ident };
1161 }
1162 case DOUBLE_LITERAL: {
1163 auto val = *((double*)op.second);
1164 ++val;
1165 ident->value = new double(val);
1166 return { ADDRESS, ident };
1167 }
1168 case FLOAT_LITERAL: {
1169 auto val = *((float*)op.second);
1170 ++val;
1171 ident->value = new float(val);
1172 return { ADDRESS, ident };
1173 }
1174 case INT_LITERAL: {
1175 auto val = *((int*)op.second);
1176 ++val;
1177 ident->value = new int(val);
1178 return { ADDRESS, ident };
1179 }
1180 case LONG_LITERAL: {
1181 auto val = *((long long*)op.second);
1182 ++val;
1183 ident->value = new long long(val);
1184 return { ADDRESS, ident };
1185 }
1186 case SHORT_LITERAL: {
1187 auto val = *((short*)op.second);
1188 ++val;
1189 ident->value = new short(val);
1190 return { ADDRESS, ident };
1191 }
1192 case UINT_LITERAL: {
1193 auto val = *((uint32_t*)op.second);
1194 ++val;
1195 ident->value = new uint32_t(val);
1196 return { ADDRESS, ident };
1197 }
1198 case ULONG_LITERAL: {
1199 auto val = *((uint64_t*)op.second);
1200 ++val;
1201 ident->value = new uint64_t(val);
1202 return { ADDRESS, ident };
1203 }
1204 case LARGE_LITERAL: {
1205 auto val = *((large*)op.second);
1206 ++val;
1207 ident->value = new large(val);
1208 return { ADDRESS, ident };
1209 }
1210 }
1211 } else if (operation == "--") {
1212 switch (op.first) {
1213 case BYTE_LITERAL: {
1214 auto val = *((uint8_t*)op.second);
1215 --val;
1216 ident->value = new uint8_t(val);
1217 return { ADDRESS, ident };
1218 }
1219 case CHAR_LITERAL: {
1220 auto val = *((char*)op.second);
1221 --val;
1222 ident->value = new char(val);
1223 return { ADDRESS, ident };
1224 }
1225 case DOUBLE_LITERAL: {
1226 auto val = *((double*)op.second);
1227 --val;
1228 ident->value = new double(val);
1229 return { ADDRESS, ident };
1230 }
1231 case FLOAT_LITERAL: {
1232 auto val = *((float*)op.second);
1233 --val;
1234 ident->value = new float(val);
1235 return { ADDRESS, ident };
1236 }
1237 case INT_LITERAL: {
1238 auto val = *((int*)op.second);
1239 --val;
1240 ident->value = new int(val);
1241 return { ADDRESS, ident };
1242 }
1243 case LONG_LITERAL: {
1244 auto val = *((long long*)op.second);
1245 --val;
1246 ident->value = new long long(val);
1247 return { ADDRESS, ident };
1248 }
1249 case SHORT_LITERAL: {
1250 auto val = *((short*)op.second);
1251 --val;
1252 ident->value = new short(val);
1253 return { ADDRESS, ident };
1254 }
1255 case UINT_LITERAL: {
1256 auto val = *((uint32_t*)op.second);
1257 --val;
1258 ident->value = new uint32_t(val);
1259 return { ADDRESS, ident };
1260 }
1261 case ULONG_LITERAL: {
1262 auto val = *((uint64_t*)op.second);
1263 --val;
1264 ident->value = new uint64_t(val);
1265 return { ADDRESS, ident };
1266 }
1267 case LARGE_LITERAL: {
1268 auto val = *((large*)op.second);
1269 --val;
1270 ident->value = new large(val);
1271 return { ADDRESS, ident };
1272 }
1273 }
1274 } else if (operation == "+") {
1275 return op;
1276 } else if (operation == "-") {
1277 switch (op.first) {
1278 case BOOL_LITERAL:
1279 return { op.first, new bool(-*((bool*)op.second)) };
1280 case BYTE_LITERAL:
1281 return { op.first, new uint8_t(-*((uint8_t*)op.second)) };
1282 case CHAR_LITERAL:
1283 return { op.first, new char(-*((char*)op.second)) };
1284 case DOUBLE_LITERAL:
1285 return { op.first, new double(-*((double*)op.second)) };
1286 case FLOAT_LITERAL:
1287 return { op.first, new float(-*((float*)op.second)) };
1288 case INT_LITERAL:
1289 return { op.first, new int(-*((int*)op.second)) };
1290 case LONG_LITERAL:
1291 return { op.first, new long long(-*((long long*)op.second)) };
1292 case SHORT_LITERAL:
1293 return { op.first, new short(-*((short*)op.second)) };
1294 case LARGE_LITERAL: {
1295 return { op.first, new large(large(0) - *((large*)op.second)) };
1296 }
1297 }
1298 } else if (operation == "!") {
1299 switch (op.first) {
1300 case BOOL_LITERAL:
1301 return { op.first, new bool(!*((bool*)op.second)) };
1302 case BYTE_LITERAL:
1303 return { op.first, new uint8_t(!*((uint8_t*)op.second)) };
1304 case CHAR_LITERAL:
1305 return { op.first, new char(!*((char*)op.second)) };
1306 case DOUBLE_LITERAL:
1307 return { op.first, new double(!*((double*)op.second)) };
1308 case FLOAT_LITERAL:
1309 return { op.first, new float(!*((float*)op.second)) };
1310 case INT_LITERAL:
1311 return { op.first, new int(!*((int*)op.second)) };
1312 case LONG_LITERAL:
1313 return { op.first, new long long(!*((long long*)op.second)) };
1314 case SHORT_LITERAL:
1315 return { op.first, new short(!*((short*)op.second)) };
1316 case UINT_LITERAL:
1317 return { op.first, new uint32_t(!*((uint32_t*)op.second)) };
1318 case ULONG_LITERAL:
1319 return { op.first, new uint64_t(!*((uint64_t*)op.second)) };
1320 }
1321 } else if (operation == "~") {
1322 switch (op.first) {
1323 case BOOL_LITERAL:
1324 return { op.first, new bool(~*((bool*)op.second)) };
1325 case BYTE_LITERAL:
1326 return { op.first, new uint8_t(~*((uint8_t*)op.second)) };
1327 case CHAR_LITERAL:
1328 return { op.first, new char(~*((char*)op.second)) };
1329 case INT_LITERAL:
1330 return { op.first, new int(~*((int*)op.second)) };
1331 case LONG_LITERAL:
1332 return { op.first, new long long(~*((long long*)op.second)) };
1333 case SHORT_LITERAL:
1334 return { op.first, new short(~*((short*)op.second)) };
1335 case UINT_LITERAL:
1336 return { op.first, new uint32_t(~*((uint32_t*)op.second)) };
1337 case ULONG_LITERAL:
1338 return { op.first, new uint64_t(~*((uint64_t*)op.second)) };
1339 }
1340 }
1341}
1342void Poliz::call_function(std::stack<std::pair<Poliz::PolizType, void*>>& st, int& p) {
1343 int sz = *(int*)st.top().second;
1344 st.pop();
1345 Function* func = (Function*)st.top().second;
1346 add_tid_values(func->TID);
1347 clear_tid_values(func->TID);
1348 st.pop();
1349 p = func->ptr;
1350 std::stack<std::pair<Poliz::PolizType, void*>> args;
1351 for (int i = 0; i < sz; ++i) {
1352 args.push(st.top());
1353 st.pop();
1354 }
1355 st.push({ Poliz::PolizType::STACK_PLUG, nullptr });
1356 int q = 0;
1357 while (!args.empty()) {
1358 func->params_ptrs[q]->value = convert(args.top(), type_to_poliz(func->params_ptrs[q]->type.expr_type));
1359 args.pop();
1360 p = func->params_init[q];
1361 ++q;
1362 }
1363}
1364std::pair<Poliz::PolizType, void*> Poliz::execute(Function entry_func) {
1365 std::stack<std::pair<Poliz::PolizType, void*>> st;
1366 int p = 0;
1367 int label_balance = 0;
1368 st.push({ Poliz::PolizType::STACK_PLUG, nullptr });
1369 func_calls.push({ lexes.size(), type_to_poliz(entry_func.type.expr_type) });
1370 add_tid_values(entry_func.TID);
1371
1372 while (p < global_lexes.size()) {
1373 switch (global_lexes[p].first) {
1374 case BLANK: {
1375 ++p;
1376 break;
1377 }
1378 case BOOL_LITERAL:
1379 case BYTE_LITERAL:
1380 case CHAR_LITERAL:
1381 case DOUBLE_LITERAL:
1382 case FLOAT_LITERAL:
1383 case INT_LITERAL:
1384 case LONG_LITERAL:
1385 case SHORT_LITERAL:
1386 case STRING_LITERAL:
1387 case UINT_LITERAL:
1388 case ULONG_LITERAL:
1389 case ADDRESS:
1390 case STACK_PLUG:
1391 case LARGE_LITERAL:
1392 case POINTER: {
1393 st.push(global_lexes[p]);
1394 ++p;
1395 break;
1396 }
1397 case GETARR: {
1398 int ind = *(int*)convert(st.top(), Poliz::PolizType::INT_LITERAL);
1399 st.pop();
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;
1403 st.pop();
1404 if (var->size() <= ind || ind < 0)
1405 throw std::exception("Array index out of range");
1406 st.push({ Poliz::PolizType::ADDRESS, var->operator[](ind) });
1407 ++p;
1408 break;
1409 }
1410 case GETSTR: {
1411 int ind = *(int*)convert(st.top(), Poliz::PolizType::INT_LITERAL);
1412 st.pop();
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;
1417 st.pop();
1418 if (var->size() <= ind || ind < 0)
1419 throw std::exception("String index out of range");
1420 auto ptr = new Identifier(name + "[" + std::to_string(ind) + "]", Type(ExprType::Char, false, false), nullptr);
1421 ptr->value = &(var->operator[](ind));
1422 st.push({ Poliz::PolizType::ADDRESS, ptr });
1423 ++p;
1424 break;
1425 }
1426 case SEMICOLON: {
1427 while (!st.empty() && st.top().first != Poliz::PolizType::STACK_PLUG) {
1428 st.pop();
1429 }
1430 ++p;
1431 break;
1432 }
1433 case LITERAL: {
1434 std::pair<std::string, Type> val = *((std::pair<std::string, Type>*)global_lexes[p].second);
1435 if (val.second.expr_type == ExprType::Bool) {
1436 st.push({ Poliz::PolizType::BOOL_LITERAL, new bool(val.first == "true") });
1437 } else if (val.second.expr_type == ExprType::Byte || val.second.expr_type == ExprType::UShort) {
1438 st.push({ Poliz::PolizType::BYTE_LITERAL, new uint8_t(std::stoll(val.first)) });
1439 } else if (val.second.expr_type == ExprType::Char) {
1440 std::string s = val.first.substr(1, val.first.size() - 2);
1441 if (s == "\\\'")
1442 st.push({ Poliz::PolizType::CHAR_LITERAL, new char('\'') });
1443 else if (s == "\\\"")
1444 st.push({ Poliz::PolizType::CHAR_LITERAL, new char('\"') });
1445 else if (s == "\\n")
1446 st.push({ Poliz::PolizType::CHAR_LITERAL, new char('\n') });
1447 else if (s == "\\t")
1448 st.push({ Poliz::PolizType::CHAR_LITERAL, new char('\t') });
1449 else if (s == "\\0")
1450 st.push({ Poliz::PolizType::CHAR_LITERAL, new char('\0') });
1451 else
1452 st.push({ Poliz::PolizType::CHAR_LITERAL, new char(s[0]) });
1453 } else if (val.second.expr_type == ExprType::Double) {
1454 st.push({ Poliz::PolizType::DOUBLE_LITERAL, new double(std::stold(val.first)) });
1455 } else if (val.second.expr_type == ExprType::Float) {
1456 st.push({ Poliz::PolizType::FLOAT_LITERAL, new float(std::stold(val.first)) });
1457 } else if (val.second.expr_type == ExprType::Int) {
1458 st.push({ Poliz::PolizType::INT_LITERAL, new int(std::stoll(val.first)) });
1459 } else if (val.second.expr_type == ExprType::Long) {
1460 st.push({ Poliz::PolizType::LONG_LITERAL, new long long(std::stoll(val.first)) });
1461 } else if (val.second.expr_type == ExprType::Short) {
1462 st.push({ Poliz::PolizType::SHORT_LITERAL, new short(std::stoll(val.first)) });
1463 } else if (val.second.expr_type == ExprType::String) {
1464 st.push({ Poliz::PolizType::STRING_LITERAL, new std::string(val.first.substr(1, val.first.size() - 2)) });
1465 } else if (val.second.expr_type == ExprType::UInt) {
1466 st.push({ Poliz::PolizType::UINT_LITERAL, new uint32_t(std::stoll(val.first)) });
1467 } else if (val.second.expr_type == ExprType::ULong) {
1468 st.push({ Poliz::PolizType::ULONG_LITERAL, new uint64_t(std::stoll(val.first)) });
1469 } else if (val.second.expr_type == ExprType::Large) {
1470 st.push({ Poliz::PolizType::LARGE_LITERAL, new large(val.first) });
1471 }
1472 ++p;
1473 break;
1474 }
1475 case COMMA: {
1476 auto op2 = st.top();
1477 st.pop();
1478 auto op1 = st.top();
1479 st.pop();
1480 st.push(op2);
1481 ++p;
1482 break;
1483 }
1484 case ASSIGN: {
1485 auto op2 = st.top();
1486 st.pop();
1487 auto op1 = st.top();
1488 st.pop();
1489 auto var = (Identifier*)op1.second;
1490 auto assign_val = op2.second;
1491 if (op2.first == Poliz::PolizType::ADDRESS) {
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());
1495 }
1496
1497 if (*((std::string*)global_lexes[p].second) == "=") {
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() == ']')
1501 *(char*)var->value = *(char*)convert(op2, type_to_poliz(var->type.expr_type));
1502 else
1503 var->value = convert(op2, type_to_poliz(var->type.expr_type));
1504 } else {
1505 std::string op = *((std::string*)global_lexes[p].second);
1506 auto val = execute_operation(op1, op2, op.substr(0, op.size() - 1));
1507 var->value = convert(val, type_to_poliz(var->type.expr_type));
1508 }
1509 st.push(op1);
1510 ++p;
1511 break;
1512 }
1513 case UNARY: {
1514 auto op = st.top();
1515 st.pop();
1516 st.push(execute_operation(op, *((std::string*)global_lexes[p].second)));
1517 ++p;
1518 break;
1519 }
1520 case CONVERT: {
1521 throw std::exception("convert() function is restricted in the global scope");
1522 }
1523 case RAND: {
1524 throw std::exception("rand() function is restricted in the global scope");
1525 }
1526 case STRLEN: {
1527 throw std::exception("strlen() function is restricted in the global scope");
1528 }
1529 case CALL: {
1530 throw std::exception("function calls are restricted in the global scope");
1531 }
1532 case TIME: {
1533 throw std::exception("time() function is restricted in the global scope");
1534 }
1535 default:
1536 auto op2 = st.top();
1537 st.pop();
1538 auto op1 = st.top();
1539 st.pop();
1540 st.push(execute_operation(op1, op2, *(std::string*)(global_lexes[p].second)));
1541 ++p;
1542 break;
1543 }
1544 }
1545
1546 while (st.size() > 1)
1547 st.pop();
1548
1549 label_balance = 0;
1550 p = entry_func.ptr;
1551
1552 while (p < lexes.size()) {
1553 if (func_calls.size() > 1e3) throw std::exception("Stack overflow");
1554
1555 switch (lexes[p].first) {
1556 case GO: {
1557 int ind = *((int*)st.top().second);
1558 st.pop();
1559 p = ind;
1560 break;
1561 }
1562 case FGO: {
1563 int ind = *((int*)st.top().second);
1564 st.pop();
1565 bool val = *((bool*)st.top().second) == false;
1566 st.pop();
1567 if (val) {
1568 p = ind;
1569 } else ++p;
1570 break;
1571 }
1572 case TGO: {
1573 int ind = *((int*)st.top().second);
1574 st.pop();
1575 bool val = *((bool*)st.top().second) == true;
1576 st.pop();
1577 if (val) {
1578 p = ind;
1579 } else ++p;
1580 break;
1581 }
1582 case BLANK: {
1583 ++p;
1584 break;
1585 }
1586 case LABEL: {
1587 ++label_balance;
1588 ++p;
1589 if (func_calls.size() != label_balance)
1590 throw std::exception("The return statement was not called");
1591 break;
1592 }
1593 case BOOL_LITERAL:
1594 case BYTE_LITERAL:
1595 case CHAR_LITERAL:
1596 case DOUBLE_LITERAL:
1597 case FLOAT_LITERAL:
1598 case INT_LITERAL:
1599 case LONG_LITERAL:
1600 case SHORT_LITERAL:
1601 case STRING_LITERAL:
1602 case UINT_LITERAL:
1603 case ULONG_LITERAL:
1604 case ADDRESS:
1605 case STACK_PLUG:
1606 case LARGE_LITERAL:
1607 case POINTER: {
1608 st.push(lexes[p]);
1609 ++p;
1610 break;
1611 }
1612 case GETARR: {
1613 int ind = *(int*)convert(st.top(), Poliz::PolizType::INT_LITERAL);
1614 st.pop();
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;
1618 st.pop();
1619 if (var->size() <= ind)
1620 throw std::exception("Array index out of range");
1621 st.push({ Poliz::PolizType::ADDRESS, var->operator[](ind) });
1622 ++p;
1623 break;
1624 }
1625 case GETSTR: {
1626 int ind = *(int*)convert(st.top(), Poliz::PolizType::INT_LITERAL);
1627 st.pop();
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;
1632 st.pop();
1633 if (var->size() <= ind)
1634 throw std::exception("String index out of range");
1635 auto ptr = new Identifier(name + "[" + std::to_string(ind) + "]", Type(ExprType::Char, false, false), nullptr);
1636 ptr->value = &(var->operator[](ind));
1637 st.push({ Poliz::PolizType::ADDRESS, ptr });
1638 ++p;
1639 break;
1640 }
1641 case SEMICOLON: {
1642 while (!st.empty() && st.top().first != Poliz::PolizType::STACK_PLUG) {
1643 st.pop();
1644 }
1645 ++p;
1646 break;
1647 }
1648 case LITERAL: {
1649 std::pair<std::string, Type> val = *((std::pair<std::string, Type>*)lexes[p].second);
1650 if (val.second.expr_type == ExprType::Bool) {
1651 st.push({ Poliz::PolizType::BOOL_LITERAL, new bool(val.first == "true") });
1652 } else if (val.second.expr_type == ExprType::Byte || val.second.expr_type == ExprType::UShort) {
1653 st.push({ Poliz::PolizType::BYTE_LITERAL, new uint8_t(std::stoll(val.first)) });
1654 } else if (val.second.expr_type == ExprType::Char) {
1655 std::string s = val.first.substr(1, val.first.size() - 2);
1656 if (s == "\\\'")
1657 st.push({ Poliz::PolizType::CHAR_LITERAL, new char('\'') });
1658 else if (s == "\\\"")
1659 st.push({ Poliz::PolizType::CHAR_LITERAL, new char('\"') });
1660 else if (s == "\\n")
1661 st.push({ Poliz::PolizType::CHAR_LITERAL, new char('\n') });
1662 else if (s == "\\t")
1663 st.push({ Poliz::PolizType::CHAR_LITERAL, new char('\t') });
1664 else if (s == "\\0")
1665 st.push({ Poliz::PolizType::CHAR_LITERAL, new char('\0') });
1666 else
1667 st.push({ Poliz::PolizType::CHAR_LITERAL, new char(s[0]) });
1668 } else if (val.second.expr_type == ExprType::Double) {
1669 st.push({ Poliz::PolizType::DOUBLE_LITERAL, new double(std::stold(val.first)) });
1670 } else if (val.second.expr_type == ExprType::Float) {
1671 st.push({ Poliz::PolizType::FLOAT_LITERAL, new float(std::stold(val.first)) });
1672 } else if (val.second.expr_type == ExprType::Int) {
1673 st.push({ Poliz::PolizType::INT_LITERAL, new int(std::stoll(val.first)) });
1674 } else if (val.second.expr_type == ExprType::Long) {
1675 st.push({ Poliz::PolizType::LONG_LITERAL, new long long(std::stoll(val.first)) });
1676 } else if (val.second.expr_type == ExprType::Short) {
1677 st.push({ Poliz::PolizType::SHORT_LITERAL, new short(std::stoll(val.first)) });
1678 } else if (val.second.expr_type == ExprType::String) {
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");
1684 st.push({ Poliz::PolizType::STRING_LITERAL, new std::string(val.first) });
1685 } else if (val.second.expr_type == ExprType::UInt) {
1686 st.push({ Poliz::PolizType::UINT_LITERAL, new uint32_t(std::stoll(val.first)) });
1687 } else if (val.second.expr_type == ExprType::ULong) {
1688 st.push({ Poliz::PolizType::ULONG_LITERAL, new uint64_t(std::stoll(val.first)) });
1689 } else if (val.second.expr_type == ExprType::Large) {
1690 st.push({ Poliz::PolizType::LARGE_LITERAL, new large(val.first) });
1691 }
1692 ++p;
1693 break;
1694 }
1695 case COMMA: {
1696 auto op2 = st.top();
1697 st.pop();
1698 auto op1 = st.top();
1699 st.pop();
1700 st.push(op2);
1701 ++p;
1702 break;
1703 }
1704 case ASSIGN: {
1705 auto op2 = st.top();
1706 st.pop();
1707 auto op1 = st.top();
1708 st.pop();
1709 auto var = (Identifier*)op1.second;
1710 auto assign_val = op2.second;
1711 if (op2.first == Poliz::PolizType::ADDRESS) {
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());
1715 }
1716
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() == ']')
1721 *(char*)var->value = *(char*)convert(op2, type_to_poliz(var->type.expr_type));
1722 else
1723 var->value = convert(op2, type_to_poliz(var->type.expr_type));
1724 } else {
1725 std::string op = *((std::string*)lexes[p].second);
1726 auto val = execute_operation(op1, op2, op.substr(0, op.size() - 1));
1727 var->value = convert(val, type_to_poliz(var->type.expr_type));
1728 }
1729 st.push(op1);
1730 ++p;
1731 break;
1732 }
1733 case UNARY: {
1734 auto op = st.top();
1735 st.pop();
1736 st.push(execute_operation(op, *((std::string*)lexes[p].second)));
1737 ++p;
1738 break;
1739 }
1740 case RET: {
1741 auto top = st.top();
1742 while (st.top().first != Poliz::PolizType::STACK_PLUG) {
1743 st.pop();
1744 }
1745 st.pop();
1746 if (top.first == Poliz::PolizType::ADDRESS)
1747 top = address_to_value(top.second);
1748 top.second = convert(top, func_calls.top().second);
1749 top.first = func_calls.top().second;
1750 st.push(top);
1751 p = func_calls.top().first + 1;
1752 func_calls.pop();
1754 --label_balance;
1755 break;
1756 }
1757 case NORET: {
1758 while (st.top().first != Poliz::PolizType::STACK_PLUG) {
1759 st.pop();
1760 }
1761 st.pop();
1762 p = func_calls.top().first + 1;
1763 func_calls.pop();
1765 --label_balance;
1766 break;
1767 }
1768 case CALL: {
1769 func_calls.push({ p, type_to_poliz(((Function*)st.top().second)->type.expr_type) });
1770 st.push(lexes[p]);
1771 call_function(st, p);
1772 break;
1773 }
1774 case EXIT: {
1775 return st.top();
1776 }
1777 case SWITCH_CMP: {
1778 auto op2 = st.top();
1779 st.pop();
1780 st.pop();
1781 auto op1 = st.top();
1782 st.pop();
1783 st.push(op1);
1784 st.push({ Poliz::PolizType::STACK_PLUG, nullptr });
1785 st.push(execute_operation(op1, op2, "=="));
1786 ++p;
1787 break;
1788 }
1789 case SWITCH_POP: {
1790 while (st.top().first != Poliz::PolizType::STACK_PLUG) {
1791 st.pop();
1792 }
1793 st.pop();
1794 st.pop();
1795 ++p;
1796 break;
1797 }
1798 case READ: {
1799 auto var = (Identifier*)st.top().second;
1800 st.pop();
1801 std::string value;
1802 std::cin >> value;
1803 var->value = convert({ Poliz::PolizType::STRING_LITERAL, new std::string(value) }, type_to_poliz(var->type.expr_type));
1804 ++p;
1805 break;
1806 }
1807 case READLN: {
1808 auto var = (Identifier*)st.top().second;
1809 st.pop();
1810 std::string value;
1811 std::cin.clear();
1812 std::getline(std::cin, value);
1813 var->value = convert({ Poliz::PolizType::STRING_LITERAL, new std::string(value) }, type_to_poliz(var->type.expr_type));
1814 ++p;
1815 break;
1816 }
1817 case PRINT: {
1818 auto op = st.top();
1819 st.pop();
1820 std::cout << *(std::string*)(convert(op, Poliz::PolizType::STRING_LITERAL));
1821 ++p;
1822 break;
1823 }
1824 case CONVERT: {
1825 auto type = lexes[p];
1826 auto val = st.top();
1827 st.pop();
1828 auto poliz_type = type_to_poliz(string_to_type(*(std::string*)type.second));
1829 st.push({ poliz_type, convert(val, poliz_type) });
1830 ++p;
1831 break;
1832 }
1833 case RAND: {
1834 st.push({ Poliz::PolizType::INT_LITERAL, new int(rnd()) });
1835 ++p;
1836 break;
1837 }
1838 case STRLEN: {
1839 auto op = st.top();
1840 st.pop();
1842 st.push({ Poliz::PolizType::INT_LITERAL, new int(((std::string*)conv)->size()) });
1843 ++p;
1844 break;
1845 }
1846 case TIME: {
1847 const auto p1 = std::chrono::system_clock::now();
1848 st.push({ Poliz::PolizType::LONG_LITERAL, new long long(std::chrono::duration_cast<std::chrono::seconds>(p1.time_since_epoch()).count()) });
1849 ++p;
1850 break;
1851 }
1852 default:
1853 auto op2 = st.top();
1854 st.pop();
1855 auto op1 = st.top();
1856 st.pop();
1857 st.push(execute_operation(op1, op2, *(std::string*)(lexes[p].second)));
1858 ++p;
1859 break;
1860 }
1861 }
1862
1863 if (!func_calls.empty())
1864 throw std::exception("The return statement was not called");
1865 return st.empty() ? std::pair<Poliz::PolizType, void*>(Poliz::PolizType::BLANK, nullptr) : st.top();
1866}
1867
1868void Poliz::add_tid_values(TableIdentifiers* current_tid, bool is_first) {
1869 if (is_first) {
1870 tid_vals.push(std::vector<std::pair<Identifier*, void*>>());
1871 add_tid_values(current_tid, false);
1872 return;
1873 }
1874
1875 for (auto& u : current_tid->identifiers) {
1876 tid_vals.top().push_back({ u.second, copy_value(u.second) });
1877 }
1878 for (auto& u : current_tid->children) {
1879 add_tid_values(u, false);
1880 }
1881}
1882
1884 for (auto& u : current_tid->identifiers) {
1885 if (!u.second->type.is_array)
1886 u.second->value = nullptr;
1887 else {
1888 for (int i = 0; i < u.second->type.array_size; ++i) {
1889 ((std::vector<void*>*)u.second->value)->operator[](i) = nullptr;
1890 }
1891 }
1892 }
1893 for (auto& u : current_tid->children) {
1895 }
1896}
1897
1899 auto top = tid_vals.top();
1900 tid_vals.pop();
1901 for (auto& u : top) {
1902 u.first->value = u.second;
1903 }
1904}