-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.cpp
More file actions
793 lines (753 loc) · 26.4 KB
/
parser.cpp
File metadata and controls
793 lines (753 loc) · 26.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
#include "parser.h"
#include "scheme.h"
/// Evaluate для нод в дереве (вытащить тип ноды из скопа)
std::shared_ptr<Object> SymbolNode::Evaluate(std::shared_ptr<Scope> scp) {
return scp->Lookup(name_);
}
SymbolNode::SymbolNode(std::string name) : name_(name) {}
void SymbolNode::PrintTo(std::ostream *out) { *out << name_; }
std::string SymbolNode::GetName() { return name_; }
////
std::shared_ptr<Object> CellNode::Evaluate(std::shared_ptr<Scope> scp) {
auto base_op = number_first_->Evaluate(scp);
auto fn = std::dynamic_pointer_cast<Function>(base_op);
auto syntax = std::dynamic_pointer_cast<Syntax>(base_op);
if (!fn && !syntax) {
throw RuntimeError("first element must be a function or syntax");
}
std::vector<std::shared_ptr<Object>> args = ToVector(number_second_);
if (fn) {
for (auto &arg : args) {
arg = arg->Evaluate(scp);
}
return fn->Apply(scp, args);
}
return syntax->Apply(scp, args);
}
CellNode::CellNode() {}
CellNode::CellNode(Object first, Object second)
: number_first_(std::make_shared<Object>(first)),
number_second_(std::make_shared<Object>(second)) {}
void CellNode::PrintTo(std::ostream *out) {
*out << "(";
::PrintTo(number_first_, out);
auto next = std::dynamic_pointer_cast<CellNode>(number_second_);
if (!next && number_second_) {
*out << " . ";
::PrintTo(number_second_, out);
} else {
while (next) {
*out << " ";
::PrintTo(next->number_first_, out);
auto next_obj = std::dynamic_pointer_cast<CellNode>(next->number_second_);
if (!next_obj && next->number_second_) {
*out << " . ";
::PrintTo(next->number_second_, out);
}
next = next_obj;
}
}
*out << ")";
}
auto CellNode::GetFirst() { return number_first_; }
auto CellNode::GetSecond() { return number_second_; }
void CellNode::SetFirst(std::shared_ptr<Object> first) {
number_first_ = first;
}
void CellNode::SetSecond(std::shared_ptr<Object> second) {
number_second_ = second;
}
//// Методы Function
void Function::PrintTo(std::ostream *out) { *out << "<function>"; }
std::shared_ptr<Object> Function::Evaluate(std::shared_ptr<Scope> scp) {
throw std::runtime_error("can't eval function");
}
std::shared_ptr<Object>
Plus::Apply(const std::shared_ptr<Scope> &scp,
const std::vector<std::shared_ptr<Object>> &args) {
int64_t value = 0;
for (const auto &arg : args) {
auto number = std::dynamic_pointer_cast<NumberNode>(arg);
if (!number) {
throw RuntimeError("Wrong type for plus func value");
}
value += number->GetValue();
}
return std::make_shared<NumberNode>(value);
}
std::shared_ptr<Object>
Minus::Apply(const std::shared_ptr<Scope> &scp,
const std::vector<std::shared_ptr<Object>> &args) {
if (!args.empty()) {
if (auto number = std::dynamic_pointer_cast<NumberNode>(args[0])) {
int64_t value = number->GetValue();
for (auto arg = args.begin() + 1; arg != args.end(); ++arg) {
number = std::dynamic_pointer_cast<NumberNode>(*arg);
if (!number) {
throw RuntimeError("Wrong type for minus func value");
}
value -= number->GetValue();
}
return std::make_shared<NumberNode>(value);
} else {
throw RuntimeError("Wrong type for minus func value");
}
} else {
throw RuntimeError("Wrong size for minus func value");
}
}
std::shared_ptr<Object>
Divide::Apply(const std::shared_ptr<Scope> &scp,
const std::vector<std::shared_ptr<Object>> &args) {
if (!args.empty()) {
if (auto number = std::dynamic_pointer_cast<NumberNode>(args[0])) {
int64_t value = number->GetValue();
for (auto arg = args.begin() + 1; arg != args.end(); ++arg) {
number = std::dynamic_pointer_cast<NumberNode>(*arg);
if (!number) {
throw RuntimeError("Wrong type for divide func value");
}
value /= number->GetValue();
}
return std::make_shared<NumberNode>(value);
} else {
throw RuntimeError("Wrong type for divide func value");
}
} else {
throw RuntimeError("Wrong size for divide func value");
}
}
std::shared_ptr<Object>
Multiply::Apply(const std::shared_ptr<Scope> &scp,
const std::vector<std::shared_ptr<Object>> &args) {
int64_t value = 1;
for (const auto &arg : args) {
auto number = std::dynamic_pointer_cast<NumberNode>(arg);
if (!number) {
throw RuntimeError("Wrong type for multiply func value");
}
value *= number->GetValue();
}
return std::make_shared<NumberNode>(value);
}
//// Предикаты
std::shared_ptr<Object>
Equal::Apply(const std::shared_ptr<Scope> &scp,
const std::vector<std::shared_ptr<Object>> &args) {
bool value = true;
if (!args.empty()) {
if (auto last_value = std::dynamic_pointer_cast<NumberNode>(args[0])) {
for (auto arg = args.begin() + 1; arg != args.end(); ++arg) {
auto number = std::dynamic_pointer_cast<NumberNode>(*arg);
if (!number) {
throw RuntimeError("Wrong type for Equal func value");
}
value = ((last_value->GetValue() == number->GetValue()) && value);
last_value = number;
}
} else {
throw RuntimeError("Wrong type for Equal func value");
}
}
return std::make_shared<Boolean>(value);
}
std::shared_ptr<Object>
Greater::Apply(const std::shared_ptr<Scope> &scp,
const std::vector<std::shared_ptr<Object>> &args) {
bool value = true;
if (!args.empty()) {
if (auto last_value = std::dynamic_pointer_cast<NumberNode>(args[0])) {
for (auto arg = args.begin() + 1; arg != args.end(); ++arg) {
auto number = std::dynamic_pointer_cast<NumberNode>(*arg);
if (!number) {
throw RuntimeError("Wrong type for Greater func value");
}
value = ((last_value->GetValue() > number->GetValue()) && value);
last_value = number;
}
} else {
throw RuntimeError("Wrong type for Greater func value");
}
}
return std::make_shared<Boolean>(value);
}
std::shared_ptr<Object>
GreaterOrEqual::Apply(const std::shared_ptr<Scope> &scp,
const std::vector<std::shared_ptr<Object>> &args) {
bool value = true;
if (!args.empty()) {
if (auto last_value = std::dynamic_pointer_cast<NumberNode>(args[0])) {
for (auto arg = args.begin() + 1; arg != args.end(); ++arg) {
auto number = std::dynamic_pointer_cast<NumberNode>(*arg);
if (!number) {
throw RuntimeError("Wrong type for GreaterOrEqual func value");
}
value = ((last_value->GetValue() >= number->GetValue()) && value);
last_value = number;
}
} else {
throw RuntimeError("Wrong type for GreaterOrEqual func value");
}
}
return std::make_shared<Boolean>(value);
}
std::shared_ptr<Object>
Less::Apply(const std::shared_ptr<Scope> &scp,
const std::vector<std::shared_ptr<Object>> &args) {
bool value = true;
if (!args.empty()) {
if (auto last_value = std::dynamic_pointer_cast<NumberNode>(args[0])) {
for (auto arg = args.begin() + 1; arg != args.end(); ++arg) {
auto number = std::dynamic_pointer_cast<NumberNode>(*arg);
if (!number) {
throw RuntimeError("Wrong type for Less func value");
}
value = ((last_value->GetValue() < number->GetValue()) && value);
last_value = number;
}
} else {
throw RuntimeError("Wrong type for Less func value");
}
}
return std::make_shared<Boolean>(value);
}
std::shared_ptr<Object>
LessOrEqual::Apply(const std::shared_ptr<Scope> &scp,
const std::vector<std::shared_ptr<Object>> &args) {
bool value = true;
if (!args.empty()) {
if (auto last_value = std::dynamic_pointer_cast<NumberNode>(args[0])) {
for (auto arg = args.begin() + 1; arg != args.end(); ++arg) {
auto number = std::dynamic_pointer_cast<NumberNode>(*arg);
if (!number) {
throw RuntimeError("Wrong type for LessOrEqual func value");
}
value = ((last_value->GetValue() <= number->GetValue()) && value);
last_value = number;
}
} else {
throw RuntimeError("Wrong type for LessOrEqual func value");
}
}
return std::make_shared<Boolean>(value);
}
////
//// Min/Max
std::shared_ptr<Object>
Max::Apply(const std::shared_ptr<Scope> &scp,
const std::vector<std::shared_ptr<Object>> &args) {
if (!args.empty()) {
if (auto cast = std::dynamic_pointer_cast<NumberNode>(args[0])) {
auto max_val = cast->GetValue();
for (const auto &arg : args) {
auto number = std::dynamic_pointer_cast<NumberNode>(arg);
if (!number) {
throw RuntimeError("Wrong type for max func value");
}
max_val = std::max(max_val, number->GetValue());
}
return std::make_shared<NumberNode>(max_val);
} else {
throw RuntimeError("Wrong type for max func value");
}
} else {
throw RuntimeError("Max func should have at least one argument");
}
}
std::shared_ptr<Object>
Min::Apply(const std::shared_ptr<Scope> &scp,
const std::vector<std::shared_ptr<Object>> &args) {
if (!args.empty()) {
if (auto cast = std::dynamic_pointer_cast<NumberNode>(args[0])) {
auto max_val = cast->GetValue();
for (const auto &arg : args) {
auto number = std::dynamic_pointer_cast<NumberNode>(arg);
if (!number) {
throw RuntimeError("Wrong type for min func value");
}
max_val = std::min(max_val, number->GetValue());
}
return std::make_shared<NumberNode>(max_val);
} else {
throw RuntimeError("Wrong type for max func value");
}
} else {
throw RuntimeError("Min func should have at least one argument");
}
}
////
std::shared_ptr<Object>
Abs::Apply(const std::shared_ptr<Scope> &scp,
const std::vector<std::shared_ptr<Object>> &args) {
if (args.size() == 1) {
if (auto number = std::dynamic_pointer_cast<NumberNode>(args[0])) {
return std::make_shared<NumberNode>(std::abs(number->GetValue()));
} else {
throw RuntimeError("Wrong type for abs func value");
}
} else {
throw RuntimeError("Abs func should have strictly one argument");
}
}
std::shared_ptr<Object>
NumberCheck::Apply(const std::shared_ptr<Scope> &scp,
const std::vector<std::shared_ptr<Object>> &args) {
if (args.size() == 1) {
if (auto number = std::dynamic_pointer_cast<NumberNode>(args[0])) {
return std::make_shared<Boolean>(true);
} else {
return std::make_shared<Boolean>(false);
}
} else {
throw RuntimeError("Too many arguments in Number check");
}
}
std::shared_ptr<Object>
PairCheck::Apply(const std::shared_ptr<Scope> &scp,
const std::vector<std::shared_ptr<Object>> &args) {
if (auto cast = std::dynamic_pointer_cast<CellNode>(args[0])) {
return std::make_shared<Boolean>(true);
} else {
return std::make_shared<Boolean>(false);
}
}
std::shared_ptr<Object>
NullCheck::Apply(const std::shared_ptr<Scope> &scp,
const std::vector<std::shared_ptr<Object>> &args) {
if (auto cast = std::dynamic_pointer_cast<CellNode>(args[0])) {
return std::make_shared<Boolean>(false);
} else {
return std::make_shared<Boolean>(true);
}
}
std::shared_ptr<Object>
ListCheck::Apply(const std::shared_ptr<Scope> &scp,
const std::vector<std::shared_ptr<Object>> &args) {
auto cur_head = std::dynamic_pointer_cast<CellNode>(args[0]);
while (cur_head && cur_head->GetSecond()) {
if (auto cast =
std::dynamic_pointer_cast<CellNode>(cur_head->GetSecond())) {
cur_head = cast;
} else {
return std::make_shared<Boolean>(false);
}
}
return std::make_shared<Boolean>(true);
}
std::shared_ptr<Object>
BooleanCheck::Apply(const std::shared_ptr<Scope> &scp,
const std::vector<std::shared_ptr<Object>> &args) {
if (auto cast = std::dynamic_pointer_cast<Boolean>(args[0])) {
return std::make_shared<Boolean>(true);
}
return std::make_shared<Boolean>(false);
}
std::shared_ptr<Object>
SymbolCheck::Apply(const std::shared_ptr<Scope> &scp,
const std::vector<std::shared_ptr<Object>> &args) {
if (auto cast = std::dynamic_pointer_cast<SymbolNode>(args[0])) {
return std::make_shared<Boolean>(true);
}
return std::make_shared<Boolean>(false);
}
std::shared_ptr<Object>
Not::Apply(const std::shared_ptr<Scope> &scp,
const std::vector<std::shared_ptr<Object>> &args) {
if (args.size() == 1) {
if (auto cast = std::dynamic_pointer_cast<Boolean>(args[0])) {
return std::make_shared<Boolean>(!cast->GetVal());
}
return std::make_shared<Boolean>(false);
} else {
throw RuntimeError("too many arguments for not func");
}
}
std::shared_ptr<Object>
And::Apply(const std::shared_ptr<Scope> &scp,
const std::vector<std::shared_ptr<Object>> &args) {
if (!args.empty()) {
for (auto &arg : args) {
auto new_arg = arg->Evaluate(scp);
auto cast_bool = std::dynamic_pointer_cast<Boolean>(new_arg);
auto cast_cell = std::dynamic_pointer_cast<CellNode>(new_arg);
if (cast_bool && !cast_bool->GetVal()) {
return cast_bool;
}
if (cast_cell) {
return cast_cell;
}
}
}
return std::make_shared<Boolean>(true);
}
std::shared_ptr<Object>
Or::Apply(const std::shared_ptr<Scope> &scp,
const std::vector<std::shared_ptr<Object>> &args) {
if (!args.empty()) {
bool flg = true;
for (auto &arg : args) {
auto new_arg = arg->Evaluate(scp);
auto cast_bool = std::dynamic_pointer_cast<Boolean>(new_arg);
auto cast_cell = std::dynamic_pointer_cast<CellNode>(new_arg);
if (cast_bool && cast_bool->GetVal()) {
return cast_bool;
} else if (!cast_bool) {
return new_arg;
}
}
}
return std::make_shared<Boolean>(false);
}
std::shared_ptr<Object>
Cons::Apply(const std::shared_ptr<Scope> &scp,
const std::vector<std::shared_ptr<Object>> &args) {
auto cell_node = std::make_shared<CellNode>();
cell_node->SetFirst(args[0]);
cell_node->SetSecond(args[1]);
return cell_node;
}
//// Методы Syntax
void Syntax::PrintTo(std::ostream *out) { *out << "<syntax>"; }
std::shared_ptr<Object> Syntax::Evaluate(std::shared_ptr<Scope> scp) {
throw RuntimeError("can't eval syntax");
}
std::shared_ptr<Object>
Quote::Apply(const std::shared_ptr<Scope> &scp,
const std::vector<std::shared_ptr<Object>> &args) {
if (args.size() != 1) {
throw SyntaxError("Wrong size for list");
}
return args[0];
}
void Quote::PrintTo(std::ostream *out) { Syntax::PrintTo(out); }
std::shared_ptr<Object>
If::Apply(const std::shared_ptr<Scope> &scp,
const std::vector<std::shared_ptr<Object>> &args) {
if (args.size() >= 2 && args.size() <= 3) {
auto condition = args[0];
auto true_branch = args[1];
std::shared_ptr<Object> false_branch = nullptr;
if (args.size() == 3) {
false_branch = args[2];
}
auto result = std::dynamic_pointer_cast<Boolean>(condition->Evaluate(scp));
if (result->GetVal()) {
return true_branch->Evaluate(scp);
} else if (false_branch) {
return false_branch->Evaluate(scp);
} else {
return false_branch;
}
} else {
throw SyntaxError("Incorrect number of arguments");
}
}
std::shared_ptr<Object>
Car::Apply(const std::shared_ptr<Scope> &scp,
const std::vector<std::shared_ptr<Object>> &args) {
if (auto cast = std::dynamic_pointer_cast<CellNode>(args[0])) {
return std::dynamic_pointer_cast<CellNode>(args[0]->Evaluate(scp))
->GetFirst();
} else if (auto cast = std::dynamic_pointer_cast<SymbolNode>(args[0])) {
return args[0]->Evaluate(scp);
}
}
std::shared_ptr<Object>
Cdr::Apply(const std::shared_ptr<Scope> &scp,
const std::vector<std::shared_ptr<Object>> &args) {
if (auto cast = std::dynamic_pointer_cast<CellNode>(args[0])) {
return std::dynamic_pointer_cast<CellNode>(args[0]->Evaluate(scp))
->GetSecond();
} else if (auto cast = std::dynamic_pointer_cast<SymbolNode>(args[0])) {
return args[0]->Evaluate(scp);
}
}
std::shared_ptr<Object>
Define::Apply(const std::shared_ptr<Scope> &scp,
const std::vector<std::shared_ptr<Object>> &args) {
if (args.size() == 2) {
if (auto arg_cast = std::dynamic_pointer_cast<SymbolNode>(args[0])) {
scp->scope_[std::dynamic_pointer_cast<SymbolNode>(args[0])->GetName()] =
args[1]->Evaluate(scp);
return shared_from_this();
} else if (auto arg_cast = std::dynamic_pointer_cast<CellNode>(args[0])) {
std::shared_ptr<LambdaFunc> new_func = std::make_shared<LambdaFunc>();
for (size_t i = 1; i < args.size(); ++i) {
new_func->lambda_func.push_back(args[i]);
}
new_func->local_scope_ = std::make_shared<Scope>(*scp);
new_func->params_ = ToVector(arg_cast->GetSecond());
scp->scope_[std::dynamic_pointer_cast<SymbolNode>(arg_cast->GetFirst())
->GetName()] = new_func;
return shared_from_this();
}
} else {
throw SyntaxError("not enough arguments");
}
}
void Define::PrintTo(std::ostream *out) {}
std::shared_ptr<Object>
SetCar::Apply(const std::shared_ptr<Scope> &scp,
const std::vector<std::shared_ptr<Object>> &args) {
scp->scope_[std::dynamic_pointer_cast<SymbolNode>(args[0])->GetName()] =
args[1]->Evaluate(scp);
return shared_from_this();
}
void SetCar::PrintTo(std::ostream *out) {}
std::shared_ptr<Object>
SetCdr::Apply(const std::shared_ptr<Scope> &scp,
const std::vector<std::shared_ptr<Object>> &args) {
scp->scope_[std::dynamic_pointer_cast<SymbolNode>(args[0])->GetName()] =
args[1]->Evaluate(scp);
return shared_from_this();
}
void SetCdr::PrintTo(std::ostream *out) {}
std::shared_ptr<Object>
Set::Apply(const std::shared_ptr<Scope> &scp,
const std::vector<std::shared_ptr<Object>> &args) {
if (args.size() == 2) {
if (scp->scope_.find(
std::dynamic_pointer_cast<SymbolNode>(args[0])->GetName()) !=
scp->scope_.end()) {
scp->scope_[std::dynamic_pointer_cast<SymbolNode>(args[0])->GetName()] =
args[1]->Evaluate(scp);
return shared_from_this();
} else {
throw NameError("Can't find variable");
}
} else {
throw SyntaxError("not enough arguments");
}
}
void Set::PrintTo(std::ostream *out) {}
std::shared_ptr<Object>
ListCmd::Apply(const std::shared_ptr<Scope> &scp,
const std::vector<std::shared_ptr<Object>> &args) {
if (!args.empty()) {
auto root_node = std::make_shared<CellNode>();
auto head = root_node;
for (auto iter = args.begin(); iter != args.end(); ++iter) {
head->SetFirst(*iter);
if (iter != (args.end() - 1)) {
head->SetSecond(std::make_shared<CellNode>());
head = std::dynamic_pointer_cast<CellNode>(head->GetSecond());
}
}
return root_node;
} else {
return nullptr;
}
}
std::shared_ptr<Object>
ListTail::Apply(const std::shared_ptr<Scope> &scp,
const std::vector<std::shared_ptr<Object>> &args) {
auto iter_num = std::dynamic_pointer_cast<NumberNode>(args[1])->GetValue();
auto cur_head = args[0]->Evaluate(scp);
auto next_head = std::dynamic_pointer_cast<CellNode>(cur_head)->GetSecond();
while (iter_num > 0 && next_head) {
cur_head = next_head;
next_head = std::dynamic_pointer_cast<CellNode>(cur_head)->GetSecond();
--iter_num;
}
if (iter_num == 0) {
return cur_head;
} else if (iter_num == 1) {
return nullptr;
} else {
throw RuntimeError("too big index");
}
}
std::shared_ptr<Object>
ListRef::Apply(const std::shared_ptr<Scope> &scp,
const std::vector<std::shared_ptr<Object>> &args) {
auto iter_num = std::dynamic_pointer_cast<NumberNode>(args[1])->GetValue();
auto cur_head = args[0]->Evaluate(scp);
auto next_head = std::dynamic_pointer_cast<CellNode>(cur_head)->GetSecond();
while (iter_num > 0 && next_head) {
cur_head = next_head;
next_head = std::dynamic_pointer_cast<CellNode>(cur_head)->GetSecond();
--iter_num;
}
if (iter_num == 0) {
return std::dynamic_pointer_cast<CellNode>(cur_head)->GetFirst();
} else {
throw RuntimeError("too big index");
}
}
std::shared_ptr<Object>
Lambda::Apply(const std::shared_ptr<Scope> &scp,
const std::vector<std::shared_ptr<Object>> &args) {
if (args.size() >= 2) {
std::shared_ptr<LambdaFunc> new_func = std::make_shared<LambdaFunc>();
for (size_t i = 1; i < args.size(); ++i) {
new_func->lambda_func.push_back(args[i]);
}
new_func->local_scope_ = std::make_shared<Scope>(*scp);
new_func->params_ = ToVector(args[0]);
return new_func;
} else {
throw SyntaxError("Invalid number of arguments in lambda function");
}
}
std::shared_ptr<Object>
LambdaFunc::Apply(const std::shared_ptr<Scope> &scp,
const std::vector<std::shared_ptr<Object>> &args) {
for (size_t i = 0; i < params_.size(); ++i) {
local_scope_
->scope_[std::dynamic_pointer_cast<SymbolNode>(params_[i])->GetName()] =
args[i]->Evaluate(scp);
}
auto original_scope = std::make_shared<Scope>(*local_scope_);
local_scope_->scope_.insert(scp->scope_.begin(), scp->scope_.end());
std::shared_ptr<Object> last_val = nullptr;
for (auto &func : lambda_func) {
last_val = func->Evaluate(local_scope_);
}
std::vector<std::string> keys_to_remove;
for (auto &el : local_scope_->scope_) {
if (original_scope->scope_.find(el.first) == local_scope_->scope_.end()) {
keys_to_remove.push_back(el.first);
}
}
for (auto &key : keys_to_remove) {
local_scope_->scope_.erase(key);
}
// local_scope_ = original_scope;
return last_val;
}
// LambdaFunc::~LambdaFunc() {
// if (local_scope_) {
// local_scope_->scope_.clear();
// }
// params_.clear();
// lambda_func.clear();
//}
////
//// Boolean
void Boolean::PrintTo(std::ostream *out) { *out << (val_ ? "#t" : "#f"); }
bool Boolean::GetVal() { return val_; }
Boolean::Boolean(bool val) : val_(val) {}
std::vector<std::shared_ptr<Object>>
ToVector(const std::shared_ptr<Object> &head) {
std::vector<std::shared_ptr<Object>> elements;
if (head) {
elements.push_back(std::dynamic_pointer_cast<CellNode>(head)->GetFirst());
auto cur_head = std::dynamic_pointer_cast<CellNode>(head)->GetSecond();
while (cur_head && (!std::dynamic_pointer_cast<Function>(cur_head)) &&
(!std::dynamic_pointer_cast<Syntax>(cur_head))) {
elements.push_back(
std::dynamic_pointer_cast<CellNode>(cur_head)->GetFirst());
cur_head = std::dynamic_pointer_cast<CellNode>(cur_head)->GetSecond();
}
}
return elements;
}
void Object::PrintTo(std::ostream *out) { throw RuntimeError("WTF?"); }
std::shared_ptr<Object> Object::Evaluate(std::shared_ptr<Scope> scp) {
throw RuntimeError("Placeholder");
}
std::shared_ptr<Object>
Object::Apply(Scope &scp, std::vector<std::shared_ptr<Object>> ¶ms) {
throw RuntimeError("Placeholder");
}
std::shared_ptr<Object> NumberNode::Evaluate(std::shared_ptr<Scope> scp) {
return shared_from_this();
}
void NumberNode::PrintTo(std::ostream *out) { *out << number_; }
int64_t NumberNode::GetValue() { return number_; }
NumberNode::NumberNode(int num) : number_(num) {}
std::shared_ptr<Object> ReadList(Tokenizer *tokenizer) {
std::shared_ptr<CellNode> head = nullptr;
std::shared_ptr<CellNode> tail = nullptr;
auto cur_token = tokenizer->GetToken();
auto bracket_token = std::get_if<BracketToken>(&cur_token);
tokenizer->Next();
cur_token = tokenizer->GetToken();
bracket_token = std::get_if<BracketToken>(&cur_token);
while ((!bracket_token || (*bracket_token) == BracketToken::OPEN) &&
!tokenizer->IsEnd()) {
if (auto dot_token = std::get_if<DotToken>(&cur_token)) {
tokenizer->Next();
auto token = Read(tokenizer);
if (!tail && head) {
head->SetSecond(token);
} else if (head && tail) {
tail->SetSecond(token);
} else {
throw SyntaxError("Help!");
}
cur_token = tokenizer->GetToken();
bracket_token = std::get_if<BracketToken>(&cur_token);
if (!bracket_token || (*bracket_token) != BracketToken::CLOSE ||
tokenizer->IsEnd()) {
throw SyntaxError("Pair finish error");
}
return head;
} else {
if (!head) {
head = std::make_shared<CellNode>();
head->SetFirst(Read(tokenizer));
} else if (!tail) {
tail = std::make_shared<CellNode>();
tail->SetFirst(Read(tokenizer));
head->SetSecond(tail);
} else {
auto token = std::make_shared<CellNode>();
token->SetFirst(Read(tokenizer));
tail->SetSecond(token);
tail = token;
}
}
cur_token = tokenizer->GetToken();
bracket_token = std::get_if<BracketToken>(&cur_token);
}
if (tokenizer->IsEnd()) {
throw SyntaxError{"Lost closing bracket"};
}
return head;
}
std::shared_ptr<Object> Read(Tokenizer *tokenizer) {
auto cur_token = tokenizer->GetToken();
if (auto val = std::get_if<ConstantToken>(&cur_token)) {
auto new_node = std::make_shared<NumberNode>(val->value);
tokenizer->Next();
return new_node;
} else if (auto val = std::get_if<SymbolToken>(&cur_token)) {
tokenizer->Next();
if (val->name != "\n") {
auto new_node = std::make_shared<SymbolNode>(val->name);
return new_node;
}
return Read(tokenizer);
} else if (auto val = std::get_if<QuoteToken>(&cur_token)) {
tokenizer->Next();
auto cur_token = tokenizer->GetToken();
auto bracket_token = std::get_if<BracketToken>(&cur_token);
std::shared_ptr<Object> res_list;
if (bracket_token) {
res_list = ReadList(tokenizer);
} else {
res_list = std::make_shared<SymbolNode>(
std::get_if<SymbolToken>(&cur_token)->name);
}
auto quote_node = std::make_shared<SymbolNode>("\'");
auto root_node = std::make_shared<CellNode>();
auto main_node_for_list = std::make_shared<CellNode>();
root_node->SetFirst(quote_node);
main_node_for_list->SetFirst(res_list);
root_node->SetSecond(main_node_for_list);
tokenizer->Next();
return root_node;
} else if (std::get_if<BracketToken>(&cur_token) &&
(*std::get_if<BracketToken>(&cur_token)) == BracketToken::OPEN) {
auto new_node = ReadList(tokenizer);
tokenizer->Next();
if (tokenizer->BracketBalance() < 0 ||
(tokenizer->BracketBalance() != 0 && tokenizer->IsEnd())) {
throw SyntaxError("No closing bracket");
}
return new_node;
} else {
throw SyntaxError{"Krivoi vvod"};
}
}