-
Notifications
You must be signed in to change notification settings - Fork 0
/
CSE.cpp
883 lines (756 loc) · 37.3 KB
/
CSE.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
//
// Created by nisal on 7/19/2023.
//
#include "CSE.h"
#pragma clang diagnostic push
#pragma ide diagnostic ignored "OCDFAInspection"
std::vector<std::string> built_in_functions = {"Print", "Order", "Y*", "Conc", "Stem", "Stern", "Isinteger", "Isstring",
"Istuple", "Isempty", "ItoS"};
/*
* CseNode class
*/
CseNode::CseNode(ObjType node_type, std::string node_value, int cs_index, int env) {
this->node_type = node_type;
this->node_value = std::move(node_value);
this->cs_index = cs_index;
this->env = env;
}
#pragma clang diagnostic pop
CseNode::CseNode(ObjType node_type, std::string node_value, int cs_index) {
this->node_type = node_type;
this->node_value = std::move(node_value);
this->cs_index = cs_index;
}
CseNode::CseNode(ObjType node_type, std::string node_value) {
this->node_type = node_type;
this->node_value = std::move(node_value);
}
CseNode::CseNode(ObjType node_type, int cs_index, std::vector<std::string> bound_variables) {
is_single_bound_var = false;
this->node_type = node_type;
this->cs_index = cs_index;
this->bound_variables = std::move(bound_variables);
}
CseNode::CseNode(ObjType node_type, int cs_index, std::vector<std::string> bound_variables, int env) {
is_single_bound_var = false;
this->node_type = node_type;
this->node_value = std::move(node_value);
this->cs_index = cs_index;
this->env = env;
this->bound_variables = std::move(bound_variables);
}
CseNode::CseNode(ObjType node_type, std::vector<CseNode> list_elements) {
this->node_type = node_type;
this->list_elements = std::move(list_elements);
}
ObjType CseNode::get_node_type() const {
return node_type;
}
std::string CseNode::get_node_value() const {
return node_value;
}
int CseNode::get_env() const {
return env;
}
int CseNode::get_cs_index() const {
return cs_index;
}
bool CseNode::get_is_single_bound_var() const {
return is_single_bound_var;
}
std::vector<std::string> CseNode::get_var_list() const {
return bound_variables;
}
std::vector<CseNode> CseNode::get_list_elements() {
return list_elements;
}
CseNode CseNode::set_env(int env_) {
this->env = env_;
return *this;
}
/*
* ControlStructure class
*/
ControlStructure::ControlStructure(int cs_index) {
this->cs_index = cs_index;
}
void ControlStructure::add_node(const CseNode &node) {
nodes.push_back(node);
}
[[maybe_unused]] int ControlStructure::get_cs_index() const {
return cs_index;
}
[[maybe_unused]] CseNode ControlStructure::get_last_node() const {
return nodes.back();
}
void ControlStructure::pop_last_node() {
nodes.pop_back();
}
CseNode ControlStructure::pop_and_return_last_node() {
CseNode node = nodes.back();
nodes.pop_back();
return node;
}
void ControlStructure::push_control_structure(const ControlStructure &cs) {
for (auto &node: cs.nodes) {
nodes.push_back(node);
}
}
void Stack::add_node(const CseNode &node) {
nodes.push_back(node);
}
[[maybe_unused]] void Stack::pop_last_node() {
nodes.pop_back();
}
CseNode Stack::pop_and_return_last_node() {
CseNode node = nodes.back();
nodes.pop_back();
return node;
}
[[maybe_unused]] int Stack::length() const {
return static_cast<int>(nodes.size());
}
/*
* Env class
*/
Env::Env() {
parent_env = nullptr;
}
Env::Env(Env *parent_env) {
this->parent_env = parent_env;
}
void Env::add_variable(const std::string &identifier, const CseNode &value) {
variables[identifier] = value;
}
[[maybe_unused]] void Env::add_variables(const std::vector<std::string> &identifiers,
const std::vector<CseNode> &values) {
for (int i = 0; i < identifiers.size(); i++) {
variables[identifiers[i]] = values[i];
}
}
void Env::add_list(const std::string &identifier, const std::vector<CseNode> &list_elements) {
lists[identifier] = list_elements;
}
void Env::add_lambda(const std::string &identifier, const CseNode &lambda) {
is_lambda = true;
// check the node type and create new object
if (lambda.get_node_type() == ObjType::LAMBDA) {
if (lambda.get_is_single_bound_var()) {
lambdas[identifier] = CseNode(ObjType::LAMBDA,
lambda.get_node_value(), lambda.get_cs_index(), lambda.get_env());
} else {
lambdas[identifier] = CseNode(ObjType::LAMBDA, lambda.get_cs_index(),
lambda.get_var_list(), lambda.get_env());
}
} else if (lambda.get_node_type() == ObjType::EETA) {
if (lambda.get_is_single_bound_var()) {
lambdas[identifier] = CseNode(ObjType::EETA,
lambda.get_node_value(), lambda.get_cs_index(), lambda.get_env());
} else {
lambdas[identifier] = CseNode(ObjType::EETA, lambda.get_cs_index(),
lambda.get_var_list(), lambda.get_env());
}
} else {
throw std::runtime_error("Invalid lambda node type");
}
}
// NOLINTNEXTLINE
CseNode Env::get_variable(const std::string &identifier) {
if (variables.find(identifier) != variables.end()) {
return variables[identifier];
} else if (parent_env != nullptr) {
return parent_env->get_variable(identifier);
} else {
throw std::runtime_error("Identifier: " + identifier + " not found");
}
}
// NOLINTNEXTLINE
CseNode Env::get_lambda(const std::string &identifier) {
if (lambdas.find(identifier) != lambdas.end()) {
return lambdas[identifier];
} else if (parent_env != nullptr) {
return parent_env->get_lambda(identifier);
} else {
throw std::runtime_error("Identifier: " + identifier + " not found");
}
}
// NOLINTNEXTLINE
std::vector<CseNode> Env::get_list(const std::string &identifier) {
if (lists.find(identifier) != lists.end()) {
return lists[identifier];
} else if (parent_env != nullptr) {
return parent_env->get_list(identifier);
} else {
throw std::runtime_error("Identifier: " + identifier + " not found");
}
}
// NOLINTNEXTLINE
void CSE::create_cs(TreeNode *root, ControlStructure *current_cs, int current_cs_index) {
ControlStructure *cs;
if (next_cs == -1) {
next_cs++;
cs = new ControlStructure(next_cs++);
control_structures.push_back(cs);
current_cs_index = 0;
} else {
cs = current_cs;
}
if (root->getLabel() == "lambda") {
CseNode *lambda;
if (root->getChildren()[0]->getLabel() == ",") {
std::vector<std::string> vars;
for (auto &child: root->getChildren()[0]->getChildren()) {
vars.push_back(child->getValue());
}
lambda = new CseNode(ObjType::LAMBDA, next_cs, vars);
} else {
std::string var = root->getChildren()[0]->getValue();
lambda = new CseNode(ObjType::LAMBDA, var, next_cs);
}
cs->add_node(*lambda);
auto *new_cs = new ControlStructure(next_cs);
control_structures.push_back(new_cs);
create_cs(root->getChildren()[1], new_cs, next_cs++);
} else if (root->getLabel() == "tau") {
auto *tau = new CseNode(ObjType::TAU, std::to_string(root->getChildren().size()));
cs->add_node(*tau);
for (auto &child: root->getChildren()) {
create_cs(child, cs, current_cs_index);
}
} else if (root->getLabel() == "->") {
int then_index = next_cs++;
int else_index = next_cs++;
auto *delta_then = new CseNode(ObjType::DELTA, std::to_string(then_index));
auto *delta_else = new CseNode(ObjType::DELTA, std::to_string(else_index));
auto *beta = new CseNode(ObjType::BETA, "");
cs->add_node(*delta_then);
cs->add_node(*delta_else);
cs->add_node(*beta);
auto *then_cs = new ControlStructure(then_index);
control_structures.push_back(then_cs);
create_cs(root->getChildren()[1], then_cs, then_index);
auto *else_cs = new ControlStructure(else_index);
control_structures.push_back(else_cs);
create_cs(root->getChildren()[2], else_cs, else_index);
create_cs(root->getChildren()[0], cs, current_cs_index);
} else if (isOperator(root->getLabel())) {
auto *op = new CseNode(ObjType::OPERATOR, root->getLabel());
cs->add_node(*op);
for (auto &child: root->getChildren()) {
create_cs(child, cs, current_cs_index);
}
} else if (root->getLabel() == "gamma") {
auto *gamma = new CseNode(ObjType::GAMMA, "");
cs->add_node(*gamma);
for (auto &child: root->getChildren()) {
create_cs(child, cs, current_cs_index);
}
} else if (root->getLabel() == "identifier" || root->getLabel() == "integer" || root->getLabel() == "string") {
std::string value = root->getValue();
std::string type = root->getLabel();
CseNode *leaf;
if (type == "identifier") {
leaf = new CseNode(ObjType::IDENTIFIER, value);
} else if (type == "integer") {
leaf = new CseNode(ObjType::INTEGER, value);
} else if (type == "string") {
leaf = new CseNode(ObjType::STRING, value);
} else {
throw std::runtime_error("Invalid leaf type: " + type);
}
cs->add_node(*leaf);
} else {
throw std::runtime_error("Invalid node type: " + root->getLabel() + "Value: " + root->getValue());
}
}
void CSE::evaluate() {
auto *e0 = new CseNode(ObjType::ENV, "0");
main_control_structure.add_node(*e0);
stack.add_node(*e0);
env_stack.push_back(next_env++);
envs[0] = new Env(nullptr);
main_control_structure.push_control_structure(*control_structures[0]);
CseNode top_of_cs = main_control_structure.pop_and_return_last_node();
while ((top_of_cs.get_node_type() != ObjType::ENV) || (top_of_cs.get_node_value() != "0")) {
if (top_of_cs.get_node_type() == ObjType::INTEGER || top_of_cs.get_node_type() == ObjType::STRING) {
stack.add_node(top_of_cs);
top_of_cs = main_control_structure.pop_and_return_last_node();
} else if (top_of_cs.get_node_type() == ObjType::IDENTIFIER) {
CseNode value;
CseNode value_l;
std::vector<CseNode> list;
try {
value = envs[env_stack.back()]->get_variable(top_of_cs.get_node_value());
stack.add_node(CseNode(value.get_node_type(), value.get_node_value()));
}
catch (std::runtime_error &e) {
try {
value_l = envs[env_stack.back()]->get_lambda(top_of_cs.get_node_value());
stack.add_node(value_l);
}
catch (std::runtime_error &e) {
try {
list = envs[env_stack.back()]->get_list(top_of_cs.get_node_value());
stack.add_node(CseNode(ObjType::LIST, list));
}
catch (std::runtime_error &e) {
// if node value is in built_in_functions add the node to the stack
if (std::find(built_in_functions.begin(), built_in_functions.end(),
top_of_cs.get_node_value()) !=
built_in_functions.end()) {
stack.add_node(top_of_cs);
} else if (top_of_cs.get_node_value() == "nil") {
stack.add_node(CseNode(ObjType::LIST, std::vector<CseNode>()));
} else {
throw std::runtime_error("Variable not found: " + top_of_cs.get_node_value());
}
}
}
}
top_of_cs = main_control_structure.pop_and_return_last_node();
} else if (top_of_cs.get_node_type() == ObjType::LAMBDA) {
int current_env = env_stack.back();
stack.add_node(top_of_cs.set_env(current_env));
top_of_cs = main_control_structure.pop_and_return_last_node();
} else if (top_of_cs.get_node_type() == ObjType::GAMMA) {
CseNode top_of_stack = stack.pop_and_return_last_node();
if (top_of_stack.get_node_type() == ObjType::LAMBDA) {
Env *new_env = new Env(envs[top_of_stack.get_env()]);
envs[next_env++] = new_env;
CseNode value = stack.pop_and_return_last_node();
if (value.get_node_type() == ObjType::LAMBDA || value.get_node_type() == ObjType::EETA) {
new_env->add_lambda(top_of_stack.get_node_value(), value);
} else if (value.get_node_type() == ObjType::STRING || value.get_node_type() == ObjType::INTEGER) {
new_env->add_variable(top_of_stack.get_node_value(), value);
} else if (value.get_node_type() == ObjType::LIST && !top_of_stack.get_is_single_bound_var()) {
// TODO
std::vector<std::string> var_list = top_of_stack.get_var_list();
std::vector<CseNode> list_items = value.get_list_elements();
// std::vector<std::string> non_list_var;
std::vector<std::string> list_var;
std::vector<CseNode> temp_list = std::vector<CseNode>();
int var_count = 0;
int list_element_count = 0;
bool creating_list = false;
for (const auto &i: list_items) {
if (creating_list) {
temp_list.push_back(i);
list_element_count--;
if (list_element_count == 0) {
new_env->add_list(var_list[var_count++], temp_list);
temp_list = std::vector<CseNode>();
creating_list = false;
}
} else {
if (i.get_node_type() == ObjType::LIST) {
list_element_count = std::stoi(i.get_node_value());
if (list_element_count == 0) {
new_env->add_list(var_list[var_count++], temp_list);
temp_list = std::vector<CseNode>();
} else {
creating_list = true;
}
} else if (i.get_node_type() == ObjType::LAMBDA) {
new_env->add_lambda(var_list[var_count++], i);
} else {
new_env->add_variable(var_list[var_count++], i);
}
}
}
if (creating_list) {
new_env->add_list(var_list[var_count], temp_list);
}
} else if (value.get_node_type() == ObjType::LIST) {
new_env->add_list(top_of_stack.get_node_value(), value.get_list_elements());
} else {
throw std::runtime_error("Invalid object for gamma: " + value.get_node_value());
}
env_stack.push_back(next_env - 1);
auto *env_obj = new CseNode(ObjType::ENV, std::to_string(next_env - 1));
main_control_structure.add_node(*env_obj);
stack.add_node(*env_obj);
main_control_structure.push_control_structure(*control_structures[top_of_stack.get_cs_index()]);
} else if (top_of_stack.get_node_type() == ObjType::IDENTIFIER) {
// TODO: built-in functions should be handled here
std::string identifier = top_of_stack.get_node_value();
if (identifier == "Print") {
CseNode value = stack.pop_and_return_last_node();
std::vector<CseNode> list_elements = value.get_list_elements();
if (value.get_node_type() == ObjType::LIST) {
std::cout << "(";
std::vector<int> count_stack;
for (int i = 0; i < value.get_list_elements().size(); i++) {
if (list_elements[i].get_node_type() == ObjType::LIST) {
count_stack.push_back(std::stoi(list_elements[i].get_node_value()));
std::cout << "(";
} else {
std::cout << list_elements[i].get_node_value();
if (!count_stack.empty()) {
// reduce 1 from all elements in count_stack
for (int &count: count_stack) {
count--;
}
if (count_stack[count_stack.size() - 1] == 0) {
if (i != value.get_list_elements().size() - 1)
std::cout << "), ";
else
std::cout << ")";
count_stack.pop_back();
} else {
if (i != value.get_list_elements().size() - 1)
std::cout << ", ";
}
} else {
if (i != value.get_list_elements().size() - 1)
std::cout << ", ";
}
}
}
std::cout << ")";
} else if (value.get_node_type() == ObjType::ENV || value.get_node_value() == "dummy") {
std::cout << "dummy";
} else if (value.get_node_type() == ObjType::LAMBDA) {
std::cout << "[lambda closure: ";
std::cout << value.get_node_value() << ": ";
std::cout << value.get_cs_index() << "]";
} else {
std::cout << value.get_node_value();
}
} else if (identifier == "Isinteger") {
CseNode value = stack.pop_and_return_last_node();
if (value.get_node_type() == ObjType::INTEGER) {
stack.add_node(CseNode(ObjType::BOOLEAN, "true"));
} else {
stack.add_node(CseNode(ObjType::BOOLEAN, "false"));
}
} else if (identifier == "Isstring") {
CseNode value = stack.pop_and_return_last_node();
if (value.get_node_type() == ObjType::STRING) {
stack.add_node(CseNode(ObjType::BOOLEAN, "true"));
} else {
stack.add_node(CseNode(ObjType::BOOLEAN, "false"));
}
} else if (identifier == "Isempty") {
CseNode value = stack.pop_and_return_last_node();
if (value.get_node_type() == ObjType::LIST) {
if (value.get_list_elements().empty()) {
stack.add_node(CseNode(ObjType::BOOLEAN, "true"));
} else {
stack.add_node(CseNode(ObjType::BOOLEAN, "false"));
}
} else {
throw std::runtime_error("Invalid type for IsEmpty: " + value.get_node_value());
}
} else if (identifier == "Istuple") {
CseNode value = stack.pop_and_return_last_node();
if (value.get_node_type() == ObjType::LIST) {
stack.add_node(CseNode(ObjType::BOOLEAN, "true"));
} else {
stack.add_node(CseNode(ObjType::BOOLEAN, "false"));
}
} else if (identifier == "Order") {
CseNode value = stack.pop_and_return_last_node();
if (value.get_node_type() == ObjType::LIST) {
int count = 0;
int list_elem_skip = 0;
for (const auto &i: value.get_list_elements()) {
if (i.get_node_type() == ObjType::LIST && list_elem_skip == 0) {
list_elem_skip += std::stoi(i.get_node_value());
count++;
} else if (list_elem_skip == 0) {
count++;
} else {
list_elem_skip--;
continue;
}
}
stack.add_node(CseNode(ObjType::INTEGER, std::to_string(count)));
} else {
throw std::runtime_error("Invalid type for Order: " + value.get_node_value());
}
} else if (identifier == "Conc") {
CseNode first_arg = stack.pop_and_return_last_node();
CseNode second_arg = stack.pop_and_return_last_node();
main_control_structure.pop_last_node();
if (first_arg.get_node_type() == ObjType::STRING &&
(second_arg.get_node_type() == ObjType::STRING ||
second_arg.get_node_type() == ObjType::INTEGER)) {
stack.add_node(
CseNode(ObjType::STRING, first_arg.get_node_value() + second_arg.get_node_value()));
} else {
throw std::runtime_error("Invalid type for Conc: " + first_arg.get_node_value());
}
} else if (identifier == "Stem") {
CseNode arg = stack.pop_and_return_last_node();
if (arg.get_node_type() == ObjType::STRING) {
stack.add_node(CseNode(ObjType::STRING, arg.get_node_value().substr(0, 1)));
} else {
throw std::runtime_error("Invalid type for Stem: " + top_of_stack.get_node_value());
}
} else if (identifier == "Stern") {
CseNode arg = stack.pop_and_return_last_node();
if (arg.get_node_type() == ObjType::STRING) {
stack.add_node(CseNode(ObjType::STRING, arg.get_node_value().substr(1)));
} else {
throw std::runtime_error("Invalid type for Stern: " + top_of_stack.get_node_value());
}
} else if (identifier == "Y*") {
CseNode lambda = stack.pop_and_return_last_node();
if (lambda.get_node_type() == ObjType::LAMBDA) {
if (lambda.get_is_single_bound_var()) {
stack.add_node(CseNode(ObjType::EETA, lambda.get_node_value(), lambda.get_cs_index(),
lambda.get_env()));
} else {
stack.add_node(CseNode(ObjType::EETA, lambda.get_cs_index(), lambda.get_var_list(),
lambda.get_env()));
}
} else {
throw std::runtime_error("Invalid type for Y*: " + lambda.get_node_value());
}
} else if (identifier == "ItoS") {
CseNode arg = stack.pop_and_return_last_node();
if (arg.get_node_type() == ObjType::INTEGER) {
stack.add_node(CseNode(ObjType::STRING, arg.get_node_value()));
} else {
throw std::runtime_error("Invalid type for ItoS: " + arg.get_node_value());
}
}
} else if (top_of_stack.get_node_type() == ObjType::EETA) {
stack.add_node(top_of_stack);
if (top_of_stack.get_is_single_bound_var()) {
stack.add_node(
CseNode(ObjType::LAMBDA, top_of_stack.get_node_value(), top_of_stack.get_cs_index(),
top_of_stack.get_env()));
} else {
stack.add_node(
CseNode(ObjType::LAMBDA, top_of_stack.get_cs_index(), top_of_stack.get_var_list(),
top_of_stack.get_env()));
}
main_control_structure.add_node(CseNode(ObjType::GAMMA, ""));
main_control_structure.add_node(CseNode(ObjType::GAMMA, ""));
} else if (top_of_stack.get_node_type() == ObjType::LIST) {
CseNode second_arg = stack.pop_and_return_last_node();
if (second_arg.get_node_type() == ObjType::INTEGER) {
int index = std::stoi(second_arg.get_node_value());
int current_index = 0;
int list_element_pos = 0;
int list_elem_skip = 0;
bool is_list = false;
for (const auto &i: top_of_stack.get_list_elements()) {
if (i.get_node_type() == ObjType::LIST && list_elem_skip == 0) {
list_elem_skip = std::stoi(i.get_node_value());
current_index++;
if (index == current_index) {
is_list = true;
break;
}
} else if (list_elem_skip == 0) {
current_index++;
if (index == current_index) {
break;
}
} else {
list_elem_skip--;
}
list_element_pos++;
}
std::vector<CseNode> list_elements = std::vector<CseNode>();
if (is_list) {
int length = std::stoi(top_of_stack.get_list_elements()[list_element_pos].get_node_value());
for (int i = 0; i < length; i++) {
list_elements.push_back(top_of_stack.get_list_elements()[list_element_pos + i + 1]);
}
stack.add_node(CseNode(ObjType::LIST, list_elements));
} else {
stack.add_node(top_of_stack.get_list_elements()[list_element_pos]);
}
} else {
throw std::runtime_error("Invalid type for Index: " + second_arg.get_node_value());
}
}
top_of_cs = main_control_structure.pop_and_return_last_node();
} else if (top_of_cs.get_node_type() == ObjType::ENV) {
std::vector<CseNode> env_nodes = {};
CseNode st_node = stack.pop_and_return_last_node();
while (st_node.get_node_type() != ObjType::ENV) {
env_nodes.push_back(st_node);
st_node = stack.pop_and_return_last_node();
}
// push back the stack from vector
for (auto it = env_nodes.rbegin(); it != env_nodes.rend(); ++it) {
stack.add_node(*it);
}
env_stack.pop_back();
top_of_cs = main_control_structure.pop_and_return_last_node();
} else if (top_of_cs.get_node_type() == ObjType::OPERATOR) {
std::string operator_ = top_of_cs.get_node_value();
CseNode first = stack.pop_and_return_last_node();
CseNode second = stack.pop_and_return_last_node();
if (operator_ == "+") {
stack.add_node(CseNode(ObjType::INTEGER, std::to_string(
std::stoi(first.get_node_value()) + std::stoi(second.get_node_value()))));
} else if (operator_ == "-") {
stack.add_node(CseNode(ObjType::INTEGER, std::to_string(
std::stoi(first.get_node_value()) - std::stoi(second.get_node_value()))));
} else if (operator_ == "/") {
stack.add_node(CseNode(ObjType::INTEGER, std::to_string(
std::stoi(first.get_node_value()) / std::stoi(second.get_node_value()))));
} else if (operator_ == "*") {
stack.add_node(CseNode(ObjType::INTEGER, std::to_string(
std::stoi(first.get_node_value()) * std::stoi(second.get_node_value()))));
} else if (operator_ == "neg") {
stack.add_node(second);
stack.add_node(CseNode(ObjType::INTEGER, std::to_string(-std::stoi(first.get_node_value()))));
} else if (operator_ == "not") {
stack.add_node(second);
if (first.get_node_value() == "true") {
stack.add_node(CseNode(ObjType::BOOLEAN, "false"));
} else {
stack.add_node(CseNode(ObjType::BOOLEAN, "true"));
}
} else if (operator_ == "eq") {
if (first.get_node_value() == second.get_node_value()) {
stack.add_node(CseNode(ObjType::BOOLEAN, "true"));
} else {
stack.add_node(CseNode(ObjType::BOOLEAN, "false"));
}
} else if (operator_ == "gr") {
if (std::stoi(first.get_node_value()) > std::stoi(second.get_node_value())) {
stack.add_node(CseNode(ObjType::BOOLEAN, "true"));
} else {
stack.add_node(CseNode(ObjType::BOOLEAN, "false"));
}
} else if (operator_ == "ge") {
if (std::stoi(first.get_node_value()) >= std::stoi(second.get_node_value())) {
stack.add_node(CseNode(ObjType::BOOLEAN, "true"));
} else {
stack.add_node(CseNode(ObjType::BOOLEAN, "false"));
}
} else if (operator_ == "ls") {
if (std::stoi(first.get_node_value()) < std::stoi(second.get_node_value())) {
stack.add_node(CseNode(ObjType::BOOLEAN, "true"));
} else {
stack.add_node(CseNode(ObjType::BOOLEAN, "false"));
}
} else if (operator_ == "le") {
if (std::stoi(first.get_node_value()) <= std::stoi(second.get_node_value())) {
stack.add_node(CseNode(ObjType::BOOLEAN, "true"));
} else {
stack.add_node(CseNode(ObjType::BOOLEAN, "false"));
}
} else if (operator_ == "ne") {
if (first.get_node_value() != second.get_node_value()) {
stack.add_node(CseNode(ObjType::BOOLEAN, "true"));
} else {
stack.add_node(CseNode(ObjType::BOOLEAN, "false"));
}
} else if (operator_ == "aug") {
if (first.get_node_type() == ObjType::LIST) {
if (second.get_node_type() == ObjType::LIST) {
std::vector<CseNode> elements = first.get_list_elements();
std::vector<CseNode> elements_2 = second.get_list_elements();
elements.emplace_back(ObjType::LIST, std::to_string(elements_2.size()));
for (auto &element: elements_2) {
elements.push_back(element);
}
stack.add_node(CseNode(ObjType::LIST, elements));
} else if (second.get_node_type() == ObjType::INTEGER ||
second.get_node_type() == ObjType::BOOLEAN ||
second.get_node_type() == ObjType::STRING) {
std::vector<CseNode> elements = first.get_list_elements();
elements.emplace_back(second.get_node_type(), second.get_node_value());
stack.add_node(CseNode(ObjType::LIST, elements));
} else {
throw std::runtime_error("Invalid type for aug: " + second.get_node_value());
}
} else {
throw std::runtime_error("Invalid type for aug: " + first.get_node_value());
}
} else if (operator_ == "or") {
if (first.get_node_value() == "true" || second.get_node_value() == "true") {
stack.add_node(CseNode(ObjType::BOOLEAN, "true"));
} else {
stack.add_node(CseNode(ObjType::BOOLEAN, "false"));
}
} else if (operator_ == "&") {
if (first.get_node_value() == "true" && second.get_node_value() == "true") {
stack.add_node(CseNode(ObjType::BOOLEAN, "true"));
} else {
stack.add_node(CseNode(ObjType::BOOLEAN, "false"));
}
} else {
throw std::runtime_error("Invalid operator: " + operator_);
}
top_of_cs = main_control_structure.pop_and_return_last_node();
} else if (top_of_cs.get_node_type() == ObjType::TAU) {
std::vector<CseNode> tau_elements;
int tau_size = std::stoi(top_of_cs.get_node_value());
for (int i = 0; i < tau_size; i++) {
CseNode node = stack.pop_and_return_last_node();
if (node.get_node_type() == ObjType::LIST) {
std::vector<CseNode> elements = node.get_list_elements();
tau_elements.emplace_back(ObjType::LIST, std::to_string(elements.size()));
for (auto &element: elements) {
tau_elements.push_back(element);
}
} else {
tau_elements.push_back(node);
}
}
stack.add_node(CseNode(ObjType::LIST, tau_elements));
top_of_cs = main_control_structure.pop_and_return_last_node();
} else if (top_of_cs.get_node_type() == ObjType::BETA) {
CseNode node = stack.pop_and_return_last_node();
if (node.get_node_type() == ObjType::BOOLEAN) {
if (node.get_node_value() == "true") {
main_control_structure.pop_last_node();
CseNode true_node = main_control_structure.pop_and_return_last_node();
if (true_node.get_node_type() == ObjType::DELTA) {
main_control_structure.push_control_structure(
*control_structures[std::stoi(true_node.get_node_value())]);
} else {
throw std::runtime_error("Invalid type for beta: " + true_node.get_node_value());
}
} else {
CseNode false_node = main_control_structure.pop_and_return_last_node();
main_control_structure.pop_last_node();
if (false_node.get_node_type() == ObjType::DELTA) {
main_control_structure.push_control_structure(
*control_structures[std::stoi(false_node.get_node_value())]);
} else {
throw std::runtime_error("Invalid type for beta: " + false_node.get_node_value());
}
}
} else if (node.get_node_type() == ObjType::INTEGER) {
if (node.get_node_value() != "0") {
main_control_structure.pop_last_node();
CseNode true_node = main_control_structure.pop_and_return_last_node();
if (true_node.get_node_type() == ObjType::DELTA) {
main_control_structure.push_control_structure(
*control_structures[std::stoi(true_node.get_node_value())]);
} else {
throw std::runtime_error("Invalid type for beta: " + true_node.get_node_value());
}
} else {
CseNode false_node = main_control_structure.pop_and_return_last_node();
main_control_structure.pop_last_node();
if (false_node.get_node_type() == ObjType::DELTA) {
main_control_structure.push_control_structure(
*control_structures[std::stoi(false_node.get_node_value())]);
} else {
throw std::runtime_error("Invalid type for beta: " + false_node.get_node_value());
}
}
} else {
throw std::runtime_error("Invalid type for beta: " + node.get_node_value());
}
top_of_cs = main_control_structure.pop_and_return_last_node();
}
}
}
bool isOperator(const std::string &label) {
std::vector<std::string> operators_ = {"+", "-", "/", "*", "aug", "neg", "not", "eq", "gr", "ge", "ls", "le", "ne",
"or", "&"};
auto it = std::find(operators_.begin(), operators_.end(), label);
return it != operators_.end();
}