-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscheme.cpp
More file actions
767 lines (708 loc) · 26.8 KB
/
scheme.cpp
File metadata and controls
767 lines (708 loc) · 26.8 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
#include "scheme.h"
bool IsDigit(const std::string &s) {
return !s.empty() && std::find_if(s.begin(), s.end(), [](unsigned char c) {
return !(std::isdigit(c) || c == '-');
}) == s.end();
}
std::shared_ptr<Object> AllOperations(std::shared_ptr<Object> list) {
auto first = As<Cell>(list)->GetFirst();
if (!first) {
throw RuntimeError("Nothing to operate");
}
if (first->GetString() == "number?") {
list = As<Cell>(list)->GetSecond();
auto second = As<Cell>(list)->GetFirst();
return IsNumber(second).Do();
} else {
if (As<Cell>(list)->GetFirst()->GetString()[0] <= 62 &&
As<Cell>(list)->GetFirst()->GetString()[0] >= 60) {
return Compare(list).Do();
} else if (As<Cell>(list)->GetFirst()->GetString() == "max") {
return Max(list).Do();
} else if (As<Cell>(list)->GetFirst()->GetString() == "min") {
return Min(list).Do();
} else if (As<Cell>(list)->GetFirst()->GetString() == "+" ||
As<Cell>(list)->GetFirst()->GetString() == "-" ||
As<Cell>(list)->GetFirst()->GetString() == "*" ||
As<Cell>(list)->GetFirst()->GetString() == "/") {
if (As<Cell>(list)->GetSecond()) {
auto tmp = As<Cell>(list)->GetSecond();
if (Is<Cell>(tmp)) {
auto temp = As<Cell>(tmp);
if (!temp->GetFirst() && !temp->GetSecond()) {
throw RuntimeError("nothing to +");
}
}
}
return MathOperations(list, As<Cell>(list)->GetFirst()->GetString()).Do();
} else if (As<Cell>(list)->GetFirst()->GetString() == "abs") {
return Abs(list).Do();
} else if (As<Cell>(list)->GetFirst()->GetString() == "boolean?") {
list = As<Cell>(list)->GetSecond();
auto second = As<Cell>(list)->GetFirst();
return IsBool(second).Do();
} else if (As<Cell>(list)->GetFirst()->GetString() == "not") {
if (!As<Cell>(list)->GetSecond()) {
throw RuntimeError("Nothing to NOT");
}
list = As<Cell>(list)->GetSecond();
return Not(list).Do();
} else if (As<Cell>(list)->GetFirst()->GetString() == "and") {
return And(list).Do();
} else if (As<Cell>(list)->GetFirst()->GetString() == "quote") {
return Quote(list).Do();
} else if (As<Cell>(list)->GetFirst()->GetString() == "or") {
return Or(list).Do();
} else if (As<Cell>(list)->GetFirst()->GetString() == "pair?") {
if (!Is<Cell>(list)) {
throw RuntimeError("pair? isn't good");
}
list = As<Cell>(list)->GetSecond();
auto second = As<Cell>(list)->GetFirst();
return Pair(second).Do();
} else if (As<Cell>(list)->GetFirst()->GetString() == "null?") {
if (!Is<Cell>(list)) {
throw RuntimeError("null? isn't good");
}
list = As<Cell>(list)->GetSecond();
auto second = As<Cell>(list)->GetFirst();
return Null(second).Do();
} else if (As<Cell>(list)->GetFirst()->GetString() == "list?") {
if (!Is<Cell>(list)) {
throw RuntimeError("list? isn't good");
}
list = As<Cell>(list)->GetSecond();
auto second = As<Cell>(list)->GetFirst();
return IsList(second).Do();
} else if (As<Cell>(list)->GetFirst()->GetString() == "list") {
return List(list).Do();
} else if (As<Cell>(list)->GetFirst()->GetString() == "car") {
if (!As<Cell>(list)->GetSecond()) {
throw RuntimeError("Unable to car from empty");
}
list = As<Cell>(list)->GetSecond();
auto second = As<Cell>(list)->GetFirst();
return Car(As<Cell>(second)->GetSecond()).Do();
} else if (As<Cell>(list)->GetFirst()->GetString() == "cdr") {
if (!As<Cell>(list)->GetSecond()) {
throw RuntimeError("Unable to cdr from empty");
}
list = As<Cell>(list)->GetSecond();
auto second = As<Cell>(list)->GetFirst();
return Cdr(As<Cell>(second)->GetSecond()).Do();
} else if (As<Cell>(list)->GetFirst()->GetString() == "cons") {
if (!As<Cell>(list)->GetSecond()) {
throw RuntimeError("Unable to cons from empty");
}
list = As<Cell>(list)->GetSecond();
return Cons(list).Do();
} else if (As<Cell>(list)->GetFirst()->GetString() == "list-ref") {
if (!As<Cell>(list)->GetSecond()) {
throw RuntimeError("Unable to ref from empty");
}
list = As<Cell>(list)->GetSecond();
return Ref(list).Do();
} else if (As<Cell>(list)->GetFirst()->GetString() == "list-tail") {
if (!As<Cell>(list)->GetSecond()) {
throw RuntimeError("Unable to tail from empty");
}
list = As<Cell>(list)->GetSecond();
return Tail(list).Do();
} else {
if (IsDigit(first->GetString())) {
throw RuntimeError("First elem in list cannot be int");
}
return first;
}
}
}
std::string Interpreter::Run(const std::string &str) {
std::stringstream my_stream;
my_stream.str(std::move(str));
Tokenizer tokenizer{&my_stream};
if (std::holds_alternative<ConstantToken>(tokenizer.GetToken()) ||
std::holds_alternative<SymbolToken>(tokenizer.GetToken())) {
return Read(&tokenizer)->GetString();
}
auto list = Read(&tokenizer);
if (!tokenizer.IsEnd()) {
throw SyntaxError("Tokenizer isn't end after read");
}
if (!list) {
throw RuntimeError("Empty object");
}
auto first = As<Cell>(list)->GetFirst();
if (Is<Cell>(first)) {
throw RuntimeError("First element is a cell");
}
auto el = AllOperations(list);
if (!el) {
return "()";
}
return AllOperations(list)->GetString();
}
std::shared_ptr<Object> IsNumber::Do() {
if (IsDigit(obj_->GetString())) {
return std::make_shared<Symbol>("#t");
} else {
return std::make_shared<Symbol>("#f");
}
}
std::shared_ptr<Object> Compare::Do() {
if (As<Cell>(obj_)->GetFirst()->GetString() == "=") {
obj_ = As<Cell>(obj_)->GetSecond();
if (!obj_) {
return std::make_shared<Symbol>("#t");
}
while (As<Cell>(obj_)->GetSecond()) {
auto temp = As<Cell>(obj_)->GetSecond();
auto first = As<Cell>(temp)->GetFirst();
auto second = As<Cell>(obj_)->GetFirst();
if (Is<Cell>(first)) {
first = MathOperations(first, As<Cell>(first)->GetFirst()->GetString()).Do();
}
if (Is<Cell>(second)) {
second = MathOperations(second, As<Cell>(second)->GetFirst()->GetString()).Do();
}
if (!IsDigit(first->GetString()) || !IsDigit(second->GetString())) {
throw RuntimeError("Unable to get int in comparison");
}
if (std::stoi(first->GetString()) != std::stoi(second->GetString())) {
return std::make_shared<Symbol>("#f");
}
obj_ = temp;
}
return std::make_shared<Symbol>("#t");
} else if (As<Cell>(obj_)->GetFirst()->GetString() == ">") {
obj_ = As<Cell>(obj_)->GetSecond();
if (!obj_) {
return std::make_shared<Symbol>("#t");
}
while (As<Cell>(obj_)->GetSecond()) {
auto temp = As<Cell>(obj_)->GetSecond();
auto first = As<Cell>(temp)->GetFirst();
auto second = As<Cell>(obj_)->GetFirst();
if (Is<Cell>(first)) {
first = MathOperations(first, As<Cell>(first)->GetFirst()->GetString()).Do();
}
if (Is<Cell>(second)) {
second = MathOperations(second, As<Cell>(second)->GetFirst()->GetString()).Do();
}
if (!IsDigit(first->GetString()) || !IsDigit(second->GetString())) {
throw RuntimeError("Unable to get int in comparison");
}
if (std::stoi(first->GetString()) >= std::stoi(second->GetString())) {
return std::make_shared<Symbol>("#f");
}
obj_ = temp;
}
return std::make_shared<Symbol>("#t");
} else if (As<Cell>(obj_)->GetFirst()->GetString() == "<") {
obj_ = As<Cell>(obj_)->GetSecond();
if (!obj_) {
return std::make_shared<Symbol>("#t");
}
while (As<Cell>(obj_)->GetSecond()) {
auto temp = As<Cell>(obj_)->GetSecond();
auto first = As<Cell>(temp)->GetFirst();
auto second = As<Cell>(obj_)->GetFirst();
if (Is<Cell>(first)) {
first = MathOperations(first, As<Cell>(first)->GetFirst()->GetString()).Do();
}
if (Is<Cell>(second)) {
second = MathOperations(second, As<Cell>(second)->GetFirst()->GetString()).Do();
}
if (!IsDigit(first->GetString()) || !IsDigit(second->GetString())) {
throw RuntimeError("Unable to get int in comparison");
}
if (std::stoi(first->GetString()) <= std::stoi(second->GetString())) {
return std::make_shared<Symbol>("#f");
}
obj_ = temp;
}
return std::make_shared<Symbol>("#t");
} else if (As<Cell>(obj_)->GetFirst()->GetString() == "<") {
obj_ = As<Cell>(obj_)->GetSecond();
if (!obj_) {
return std::make_shared<Symbol>("#t");
}
while (As<Cell>(obj_)->GetSecond()) {
auto temp = As<Cell>(obj_)->GetSecond();
auto first = As<Cell>(temp)->GetFirst();
auto second = As<Cell>(obj_)->GetFirst();
if (Is<Cell>(first)) {
first = MathOperations(first, As<Cell>(first)->GetFirst()->GetString()).Do();
}
if (Is<Cell>(second)) {
second = MathOperations(second, As<Cell>(second)->GetFirst()->GetString()).Do();
}
if (!IsDigit(first->GetString()) || !IsDigit(second->GetString())) {
throw RuntimeError("Unable to get int in comparison");
}
if (std::stoi(first->GetString()) <= std::stoi(second->GetString())) {
return std::make_shared<Symbol>("#f");
}
obj_ = temp;
}
return std::make_shared<Symbol>("#t");
} else if (As<Cell>(obj_)->GetFirst()->GetString() == "<=") {
obj_ = As<Cell>(obj_)->GetSecond();
if (!obj_) {
return std::make_shared<Symbol>("#t");
}
while (As<Cell>(obj_)->GetSecond()) {
auto temp = As<Cell>(obj_)->GetSecond();
auto first = As<Cell>(temp)->GetFirst();
auto second = As<Cell>(obj_)->GetFirst();
if (Is<Cell>(first)) {
first = MathOperations(first, As<Cell>(first)->GetFirst()->GetString()).Do();
}
if (Is<Cell>(second)) {
second = MathOperations(second, As<Cell>(second)->GetFirst()->GetString()).Do();
}
if (!IsDigit(first->GetString()) || !IsDigit(second->GetString())) {
throw RuntimeError("Unable to get int in comparison");
}
if (std::stoi(first->GetString()) < std::stoi(second->GetString())) {
return std::make_shared<Symbol>("#f");
}
obj_ = temp;
}
return std::make_shared<Symbol>("#t");
} else {
obj_ = As<Cell>(obj_)->GetSecond();
if (!obj_) {
return std::make_shared<Symbol>("#t");
}
while (As<Cell>(obj_)->GetSecond()) {
auto temp = As<Cell>(obj_)->GetSecond();
auto first = As<Cell>(temp)->GetFirst();
auto second = As<Cell>(obj_)->GetFirst();
if (Is<Cell>(first)) {
first = MathOperations(first, As<Cell>(first)->GetFirst()->GetString()).Do();
}
if (Is<Cell>(second)) {
second = MathOperations(second, As<Cell>(second)->GetFirst()->GetString()).Do();
}
if (!IsDigit(first->GetString()) || !IsDigit(second->GetString())) {
throw RuntimeError("Unable to get int in comparison");
}
if (std::stoi(first->GetString()) > std::stoi(second->GetString())) {
return std::make_shared<Symbol>("#f");
}
obj_ = temp;
}
return std::make_shared<Symbol>("#t");
}
}
std::shared_ptr<Object> MathOperations::Do() {
if (!As<Cell>(obj_)->GetSecond() && (operation_ == "-" || operation_ == "/")) {
throw RuntimeError("Runtime error");
}
if (!As<Cell>(obj_)->GetSecond() && (operation_ == "+")) {
return std::make_shared<Number>(0);
}
if (!As<Cell>(obj_)->GetSecond() && (operation_ == "*")) {
return std::make_shared<Number>(1);
}
int res;
if (operation_ == "+") {
res = 0;
while (As<Cell>(obj_)->GetSecond()) {
obj_ = As<Cell>(obj_)->GetSecond();
if (Is<Cell>(obj_)) {
auto temp = As<Cell>(obj_)->GetFirst();
if (Is<Cell>(temp)) {
auto cell = As<Cell>(obj_)->GetFirst();
if (!IsDigit(AllOperations(As<Cell>(obj_)->GetFirst())->GetString())) {
throw RuntimeError("unable to math");
}
res += std::stoi(AllOperations(As<Cell>(obj_)->GetFirst())->GetString());
} else {
if (!As<Cell>(obj_)->GetFirst()) {
throw RuntimeError("Unable to math");
}
if (!IsDigit(As<Cell>(obj_)->GetFirst()->GetString())) {
throw RuntimeError("Unable to math");
}
if (As<Cell>(obj_)->GetFirst()->GetString() == "-") {
throw RuntimeError("Unable to math");
}
res += std::stoi(As<Cell>(obj_)->GetFirst()->GetString());
}
} else {
if (!IsDigit(obj_->GetString())) {
throw RuntimeError("Unable to math");
}
res += std::stoi(obj_->GetString());
break;
}
}
}
if (operation_ == "*") {
res = 1;
while (As<Cell>(obj_)->GetSecond()) {
obj_ = As<Cell>(obj_)->GetSecond();
auto temp = As<Cell>(obj_)->GetFirst();
if (Is<Cell>(temp)) {
auto cell = As<Cell>(obj_)->GetFirst();
if (!IsDigit(AllOperations(As<Cell>(obj_)->GetFirst())->GetString())) {
throw RuntimeError("unable to math");
}
res *= std::stoi(AllOperations(As<Cell>(obj_)->GetFirst())->GetString());
} else {
if (!IsDigit(As<Cell>(obj_)->GetFirst()->GetString())) {
throw RuntimeError("Unable to math");
}
res *= std::stoi(As<Cell>(obj_)->GetFirst()->GetString());
}
}
}
if (operation_ == "-") {
int cnt = 0;
while (As<Cell>(obj_)->GetSecond()) {
obj_ = As<Cell>(obj_)->GetSecond();
if (Is<Cell>(obj_)) {
auto temp = As<Cell>(obj_)->GetFirst();
if (Is<Cell>(temp)) {
if (As<Cell>(temp)->GetFirst()->GetString() != "/" &&
As<Cell>(temp)->GetFirst()->GetString() != "+" &&
As<Cell>(temp)->GetFirst()->GetString() != "-" &&
As<Cell>(temp)->GetFirst()->GetString() != "*") {
throw RuntimeError("unable to math");
}
if (!IsDigit(AllOperations(temp)->GetString())) {
throw RuntimeError("unable to math");
}
if (cnt == 0) {
res = std::stoi(AllOperations(temp)->GetString());
} else {
res -= std::stoi(AllOperations(temp)->GetString());
}
} else {
if (!As<Cell>(obj_)->GetFirst()) {
throw RuntimeError("Unable to math");
}
if (!IsDigit(As<Cell>(obj_)->GetFirst()->GetString())) {
throw RuntimeError("Unable to math");
}
if (cnt == 0) {
res = std::stoi(As<Cell>(obj_)->GetFirst()->GetString());
} else {
res -= std::stoi(As<Cell>(obj_)->GetFirst()->GetString());
}
}
} else {
if (!IsDigit(obj_->GetString())) {
throw RuntimeError("Unable to math");
}
if (cnt == 0) {
res = std::stoi(obj_->GetString());
} else {
res -= std::stoi(obj_->GetString());
}
break;
}
++cnt;
}
}
if (operation_ == "/") {
int cnt = 0;
while (As<Cell>(obj_)->GetSecond()) {
obj_ = As<Cell>(obj_)->GetSecond();
if (Is<Cell>(obj_)) {
auto temp = As<Cell>(obj_)->GetFirst();
if (Is<Cell>(temp)) {
auto cell = As<Cell>(obj_)->GetFirst();
if (!IsDigit(AllOperations(As<Cell>(obj_)->GetFirst())->GetString())) {
throw RuntimeError("unable to math");
}
if (cnt == 0) {
res = std::stoi(AllOperations(As<Cell>(obj_)->GetFirst())->GetString());
} else {
res /= std::stoi(AllOperations(As<Cell>(obj_)->GetFirst())->GetString());
}
} else {
if (!IsDigit(As<Cell>(obj_)->GetFirst()->GetString())) {
throw RuntimeError("Unable to math");
}
if (cnt == 0) {
res = std::stoi(As<Cell>(obj_)->GetFirst()->GetString());
} else {
res /= std::stoi(As<Cell>(obj_)->GetFirst()->GetString());
}
}
} else {
if (!IsDigit(obj_->GetString())) {
throw RuntimeError("unable to /");
}
if (cnt == 0) {
res = std::stoi(obj_->GetString());
} else {
res /= std::stoi(obj_->GetString());
}
}
++cnt;
}
}
return std::make_shared<Number>(res);
}
std::shared_ptr<Object> Max::Do() {
int max_elem;
obj_ = As<Cell>(obj_)->GetSecond();
if (!obj_) {
throw RuntimeError("No");
}
auto first = As<Cell>(obj_)->GetFirst();
if (Is<Cell>(first)) {
first = MathOperations(first, As<Cell>(first)->GetFirst()->GetString()).Do();
}
if (IsDigit(first->GetString())) {
max_elem = std::stoi(first->GetString());
} else {
throw RuntimeError("Unable to Max");
}
while (As<Cell>(obj_)->GetSecond()) {
obj_ = As<Cell>(obj_)->GetSecond();
first = As<Cell>(obj_)->GetFirst();
if (Is<Cell>(first)) {
first = MathOperations(first, As<Cell>(first)->GetFirst()->GetString()).Do();
}
if (IsDigit(first->GetString())) {
max_elem = std::max(std::stoi(first->GetString()), max_elem);
} else {
throw RuntimeError("Unable to Max");
}
}
return std::make_shared<Number>(max_elem);
}
std::shared_ptr<Object> Min::Do() {
int min_elem;
obj_ = As<Cell>(obj_)->GetSecond();
if (!obj_) {
throw RuntimeError("No");
}
auto first = As<Cell>(obj_)->GetFirst();
if (Is<Cell>(first)) {
first = MathOperations(first, As<Cell>(first)->GetFirst()->GetString()).Do();
}
if (IsDigit(first->GetString())) {
min_elem = std::stoi(first->GetString());
} else {
throw RuntimeError("Unable to min");
}
while (As<Cell>(obj_)->GetSecond()) {
obj_ = As<Cell>(obj_)->GetSecond();
first = As<Cell>(obj_)->GetFirst();
if (Is<Cell>(first)) {
first = MathOperations(first, As<Cell>(first)->GetFirst()->GetString()).Do();
}
if (IsDigit(first->GetString())) {
min_elem = std::min(std::stoi(first->GetString()), min_elem);
} else {
throw RuntimeError("Unable to min");
}
}
return std::make_shared<Number>(min_elem);
}
std::shared_ptr<Object> Abs::Do() {
if (!As<Cell>(obj_)->GetSecond()) {
throw RuntimeError("No");
}
obj_ = As<Cell>(obj_)->GetSecond();
auto first = As<Cell>(obj_)->GetFirst();
if (Is<Cell>(first)) {
first = MathOperations(first, As<Cell>(first)->GetFirst()->GetString()).Do();
}
if (As<Cell>(obj_)->GetSecond()) {
throw RuntimeError("No");
}
if (!IsDigit(first->GetString())) {
throw RuntimeError("No");
}
return std::make_shared<Number>(std::abs(std::stoi(first->GetString())));
}
std::shared_ptr<Object> IsBool::Do() {
std::string str;
if (Is<Cell>(obj_)) {
obj_ = AllOperations(obj_);
}
if (!obj_) {
str = "()";
} else {
str = obj_->GetString();
}
if (str.size() != 2) {
return std::make_shared<Symbol>("#f");
}
if (str[0] != '#' || (str[1] != 't' && str[1] != 'f')) {
return std::make_shared<Symbol>("#f");
}
return std::make_shared<Symbol>("#t");
}
std::shared_ptr<Object> Not::Do() {
if (As<Cell>(obj_)->GetSecond()) {
throw RuntimeError("error");
}
if (IsBool(As<Cell>(obj_)->GetFirst()).Do()->GetString() == "#t" &&
As<Cell>(obj_)->GetFirst()->GetString() == "#f") {
return std::make_shared<Symbol>("#t");
}
return std::make_shared<Symbol>("#f");
}
std::shared_ptr<Object> Quote::Do() {
obj_ = As<Cell>(obj_)->GetSecond();
return As<Cell>(obj_)->GetFirst();
}
std::shared_ptr<Object> And::Do() {
if (!As<Cell>(obj_)->GetSecond()) {
return std::make_shared<Symbol>("#t");
}
bool flag = true;
obj_ = As<Cell>(obj_)->GetSecond();
std::shared_ptr<Object> answer;
while (obj_) {
auto first = As<Cell>(obj_)->GetFirst();
if (Is<Cell>(first)) {
first = AllOperations(first);
}
if (first->GetString() == "#f") {
flag = false;
}
answer = first;
obj_ = As<Cell>(obj_)->GetSecond();
}
if (!flag) {
return std::make_shared<Symbol>("#f");
}
return answer;
}
std::shared_ptr<Object> Or::Do() {
if (!As<Cell>(obj_)->GetSecond()) {
return std::make_shared<Symbol>("#f");
}
bool flag = false;
obj_ = As<Cell>(obj_)->GetSecond();
std::shared_ptr<Object> answer;
while (obj_) {
auto first = As<Cell>(obj_)->GetFirst();
if (Is<Cell>(first)) {
first = AllOperations(first);
}
if (first->GetString() == "#t") {
flag = true;
}
answer = first;
obj_ = As<Cell>(obj_)->GetSecond();
}
if (flag) {
return std::make_shared<Symbol>("#t");
}
return answer;
}
std::shared_ptr<Object> Pair::Do() {
obj_ = AllOperations(obj_);
if (!obj_ || !Is<Cell>(obj_)) {
return std::make_shared<Symbol>("#f");
}
if (!As<Cell>(obj_)->GetSecond() || !As<Cell>(obj_)->GetFirst()) {
return std::make_shared<Symbol>("#f");
}
obj_ = As<Cell>(obj_)->GetSecond();
if (Is<Cell>(obj_)) {
if (!As<Cell>(obj_)->GetFirst() || As<Cell>(obj_)->GetSecond()) {
return std::make_shared<Symbol>("#f");
}
obj_ = As<Cell>(obj_)->GetSecond();
if (Is<Cell>(obj_)) {
return std::make_shared<Symbol>("#f");
}
}
return std::make_shared<Symbol>("#t");
}
std::shared_ptr<Object> Null::Do() {
obj_ = AllOperations(obj_);
if (!obj_) {
return std::make_shared<Symbol>("#t");
}
return std::make_shared<Symbol>("#f");
}
std::shared_ptr<Object> IsList::Do() {
obj_ = AllOperations(obj_);
if (!obj_) {
return std::make_shared<Symbol>("#t");
}
if (!Is<Cell>(obj_)) {
return std::make_shared<Symbol>("#f");
}
auto str = obj_->GetString();
for (auto elem : str) {
if (elem == '.') {
return std::make_shared<Symbol>("#f");
}
}
return std::make_shared<Symbol>("#t");
}
std::shared_ptr<Object> List::Do() {
return As<Cell>(obj_)->GetSecond();
}
std::shared_ptr<Object> Car::Do() {
obj_ = AllOperations(obj_);
return As<Cell>(obj_)->GetFirst();
}
std::shared_ptr<Object> Cdr::Do() {
obj_ = AllOperations(obj_);
return As<Cell>(obj_)->GetSecond();
}
std::shared_ptr<Object> Cons::Do() {
if (!Is<Cell>(obj_)) {
throw RuntimeError("cannot covert to pair");
}
if (!As<Cell>(obj_)->GetFirst() || !As<Cell>(obj_)->GetSecond()) {
throw RuntimeError("cannot covert to pair");
}
std::shared_ptr<Object> first = As<Cell>(obj_)->GetFirst();
std::shared_ptr<Object> second = As<Cell>(obj_)->GetSecond();
if (As<Cell>(second)->GetSecond()) {
throw RuntimeError("cannot covert to pair");
}
second = As<Cell>(second)->GetFirst();
auto new_cell = std::make_shared<Cell>(first, second);
return new_cell;
}
std::shared_ptr<Object> Ref::Do() {
auto first = As<Cell>(obj_)->GetFirst();
first = AllOperations(first);
auto second = As<Cell>(obj_)->GetSecond();
second = As<Cell>(second)->GetFirst();
int cnt = 0;
if (!IsDigit(second->GetString())) {
throw RuntimeError("Ref with no int");
}
while (cnt != std::stoi(second->GetString())) {
++cnt;
first = As<Cell>(first)->GetSecond();
if (!first) {
throw RuntimeError("very big ref");
}
}
return As<Cell>(first)->GetFirst();
}
std::shared_ptr<Object> Tail::Do() {
auto first = As<Cell>(obj_)->GetFirst();
first = AllOperations(first);
auto second = As<Cell>(obj_)->GetSecond();
second = As<Cell>(second)->GetFirst();
int cnt = 0;
if (!IsDigit(second->GetString())) {
throw RuntimeError("Tail with no int");
}
while (cnt != std::stoi(second->GetString())) {
++cnt;
first = As<Cell>(first)->GetSecond();
if (!first && cnt != std::stoi(second->GetString())) {
throw RuntimeError("very big tail");
}
}
return first;
}