-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.cpp
677 lines (617 loc) · 21.3 KB
/
functions.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
#include "functions.h"
#include <cstddef>
#include <vector>
#include "error.h"
#include "object.h"
#include "scheme.h"
#include "scope.h"
#include "garbage_collector.h"
Function* GetVariable(Object* ptr, Scope* scope) {
if (Is<Number>(ptr)) {
return GetGC().New<Variable>(ptr);
} else if (Is<Symbol>(ptr)) {
auto gen = scope;
auto ans = gen->Get(As<Symbol>(ptr)->GetName());
if (ans != nullptr) {
if (!Is<Variable>(ans)) {
ans = GetGC().New<Variable>(ans);
}
return As<Variable>(ans);
}
if (As<Symbol>(ptr)->GetName() == "lambda") {
return GetGC().New<Variable>(GetGC().New<LambdaGenerator>(scope));
}
} else if (Is<Bool>(ptr)) {
return GetGC().New<Variable>(ptr);
}
if (scope->par_scope_ == nullptr) {
throw NameError("There is no variable with such name");
} else {
return GetVariable(ptr, scope->par_scope_);
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////
/// general
template <class T>
std::vector<Object*> CalcVector(const std::vector<Object*>& input, Scope* scope) {
std::vector<Object*> ans;
for (size_t i = 1; i < input.size(); ++i) {
ans.push_back(CalcExpression(input[i], scope));
if (!Is<T>(ans.back())) {
throw RuntimeError("Bad input");
}
}
return ans;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Number
Object* IsNumberFunction::Invoke(Object* ptr, Scope* scope) {
auto input = Convert(ptr);
if (input.size() != 2) {
throw RuntimeError("Is Number function should have only 1 parameter");
}
if (Is<Number>(CalcExpression(input[1], scope))) {
return GetGC().New<Bool>(true);
} else {
return GetGC().New<Bool>(false);
}
}
Object* SumFunction::Invoke(Object* ptr, Scope* scope) {
auto input = Convert(ptr);
auto params = CalcVector<Number>(input, scope);
int64_t sum = 0;
for (const auto& el : params) {
sum += As<Number>(el)->GetValue();
}
return GetGC().New<Number>(sum);
}
Object* SubtractFunction::Invoke(Object* ptr, Scope* scope) {
auto input = Convert(ptr);
auto params = CalcVector<Number>(input, scope);
if (params.empty()) {
throw RuntimeError("Subtrack function sould have > 0 parameters");
}
int64_t sum = As<Number>(params[0])->GetValue();
for (size_t i = 1; i < params.size(); ++i) {
sum -= As<Number>(params[i])->GetValue();
}
return GetGC().New<Number>(sum);
}
Object* EqualFunction::Invoke(Object* ptr, Scope* scope) {
auto input = Convert(ptr);
auto params = CalcVector<Number>(input, scope);
for (size_t i = 1; i < params.size(); ++i) {
if (As<Number>(params[0])->GetValue() != As<Number>(params[i])->GetValue()) {
return GetGC().New<Bool>(false);
}
}
return GetGC().New<Bool>(true);
}
Object* GreaterFunction::Invoke(Object* ptr, Scope* scope) {
auto input = Convert(ptr);
auto params = CalcVector<Number>(input, scope);
for (size_t i = 1; i < params.size(); ++i) {
if (As<Number>(params[i - 1])->GetValue() <= As<Number>(params[i])->GetValue()) {
return GetGC().New<Bool>(false);
}
}
return GetGC().New<Bool>(true);
}
Object* GreaterOrEqualFunction::Invoke(Object* ptr, Scope* scope) {
auto input = Convert(ptr);
auto params = CalcVector<Number>(input, scope);
for (size_t i = 1; i < params.size(); ++i) {
if (As<Number>(params[i - 1])->GetValue() < As<Number>(params[i])->GetValue()) {
return GetGC().New<Bool>(false);
}
}
return GetGC().New<Bool>(true);
}
Object* LessFunction::Invoke(Object* ptr, Scope* scope) {
auto input = Convert(ptr);
auto params = CalcVector<Number>(input, scope);
for (size_t i = 1; i < params.size(); ++i) {
if (As<Number>(params[i - 1])->GetValue() >= As<Number>(params[i])->GetValue()) {
return GetGC().New<Bool>(false);
}
}
return GetGC().New<Bool>(true);
}
Object* LessOrEqualFunction::Invoke(Object* ptr, Scope* scope) {
auto input = Convert(ptr);
auto params = CalcVector<Number>(input, scope);
for (size_t i = 1; i < params.size(); ++i) {
if (As<Number>(params[i - 1])->GetValue() > As<Number>(params[i])->GetValue()) {
return GetGC().New<Bool>(false);
}
}
return GetGC().New<Bool>(true);
}
Object* MultFunction::Invoke(Object* ptr, Scope* scope) {
auto input = Convert(ptr);
auto params = CalcVector<Number>(input, scope);
int64_t prod = 1;
for (const auto& el : params) {
prod *= As<Number>(el)->GetValue();
}
return GetGC().New<Number>(prod);
}
Object* DivFunction::Invoke(Object* ptr, Scope* scope) {
auto input = Convert(ptr);
auto params = CalcVector<Number>(input, scope);
if (params.empty()) {
throw RuntimeError("Div function should have >= 2 parameters");
}
int64_t ans = As<Number>(params[0])->GetValue();
for (size_t i = 1; i < params.size(); ++i) {
ans /= As<Number>(params[i])->GetValue();
}
return GetGC().New<Number>(ans);
}
Object* MaxFunction::Invoke(Object* ptr, Scope* scope) {
auto input = Convert(ptr);
auto params = CalcVector<Number>(input, scope);
if (params.empty()) {
throw RuntimeError("Max function should have >= 1 parameters");
}
int64_t ans = As<Number>(params[0])->GetValue();
for (size_t i = 1; i < params.size(); ++i) {
ans = std::max(ans, As<Number>(params[i])->GetValue());
}
return GetGC().New<Number>(ans);
}
Object* MinFunction::Invoke(Object* ptr, Scope* scope) {
auto input = Convert(ptr);
auto params = CalcVector<Number>(input, scope);
if (params.empty()) {
throw RuntimeError("Min function should have >= 1 parameters");
}
int64_t ans = As<Number>(params[0])->GetValue();
for (size_t i = 1; i < params.size(); ++i) {
ans = std::min(ans, As<Number>(params[i])->GetValue());
}
return GetGC().New<Number>(ans);
}
Object* AbsFunction::Invoke(Object* ptr, Scope* scope) {
auto input = Convert(ptr);
auto params = CalcVector<Number>(input, scope);
if (params.size() != 1) {
throw RuntimeError("Abs function should have 1 parameter");
}
return GetGC().New<Number>(std::abs(As<Number>(params[0])->GetValue()));
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Bool
Bool* GenerateBool(Object* ptr) {
if (Is<Bool>(ptr)) {
return As<Bool>(ptr);
} else {
return GetGC().New<Bool>(true);
}
}
Object* IsBoolFunction::Invoke(Object* ptr, Scope* scope) {
auto input = Convert(ptr);
if (input.size() != 2) {
throw RuntimeError("Is bool function should have 1 parameter");
}
if (Is<Bool>(CalcExpression(input[1], scope))) {
return GetGC().New<Bool>(true);
} else {
return GetGC().New<Bool>(false);
}
}
Object* NotFunction::Invoke(Object* ptr, Scope* scope) {
auto input = Convert(ptr);
if (input.size() != 2) {
throw RuntimeError("Not function should have 1 parameter");
}
auto obj = CalcExpression(input[1], scope);
auto ans = GenerateBool(obj);
return GetGC().New<Bool>(!ans->GetValue());
}
Object* AndFunction::Invoke(Object* ptr, Scope* scope) {
auto input = Convert(ptr);
if (input.size() == 1) {
return GetGC().New<Bool>(true);
}
for (size_t i = 1; i < input.size(); ++i) {
auto obj = CalcExpression(input[i], scope);
if (!GenerateBool(obj)->GetValue()) {
return obj;
}
}
return CalcExpression(input.back(), scope);
}
Object* OrFunction::Invoke(Object* ptr, Scope* scope) {
auto input = Convert(ptr);
if (input.size() == 1) {
return GetGC().New<Bool>(false);
}
for (size_t i = 1; i < input.size(); ++i) {
auto obj = CalcExpression(input[i], scope);
if (GenerateBool(obj)->GetValue()) {
return obj;
}
}
return CalcExpression(input.back(), scope);
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Quotes
int64_t CntParameters(Object* ptr) {
int64_t ans = 0;
Object* last = As<Cell>(ptr)->GetSecond();
while (last) {
if (!Is<Cell>(last)) {
throw RuntimeError("Bad quote expression");
}
++ans;
last = As<Cell>(last)->GetSecond();
}
return ans;
}
Object* QuoteFunction::Invoke(Object* ptr, Scope*) {
if (!Is<Cell>(ptr)) {
throw RuntimeError("Quote must have a parameter");
}
if (CntParameters(ptr) != 1) {
throw RuntimeError("Quote must have 1 parameter");
}
return As<Cell>(As<Cell>(ptr)->GetSecond())->GetFirst();
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Lists
Object* IsNullFunction::Invoke(Object* ptr, Scope* scope) {
auto input = Convert(ptr);
if (input.size() != 2) {
throw RuntimeError("Is null function should have 1 parameter");
}
auto obj = CalcExpression(input[1], scope);
return GetGC().New<Bool>(obj == nullptr);
}
Object* IsPairFunction::Invoke(Object* ptr, Scope* scope) {
auto input = Convert(ptr);
if (input.size() != 2) {
throw RuntimeError("Is pair function should have 1 parameter");
}
auto obj = CalcExpression(input[1], scope);
auto list = Convert(obj);
return GetGC().New<Bool>(Is<Cell>(obj) && list.size() == 2);
}
Object* IsListFunction::Invoke(Object* ptr, Scope* scope) {
auto input = Convert(ptr);
if (input.size() != 2) {
throw RuntimeError("Is list function should have 1 parameter");
}
ptr = CalcExpression(input[1], scope);
while (Is<Cell>(ptr)) {
ptr = As<Cell>(ptr)->GetSecond();
}
return GetGC().New<Bool>(ptr == nullptr);
}
Object* ConsFunction::Invoke(Object* ptr, Scope* scope) {
auto input = Convert(ptr);
if (input.size() != 3) {
throw RuntimeError("Cons function should have 2 parameters");
}
auto car = CalcExpression(input[1], scope);
auto cdr = CalcExpression(input[2], scope);
Cell* ans(GetGC().New<Cell>());
ans->GetFirst() = car;
ans->GetSecond() = cdr;
return ans;
}
Object* CarFunction::Invoke(Object* ptr, Scope* scope) {
auto input = Convert(ptr);
if (input.size() != 2) {
throw RuntimeError("Car function must have 1 parameter");
}
ptr = CalcExpression(input[1], scope);
if (!ptr || !Is<Cell>(ptr)) {
throw RuntimeError("Car function's parameter should be a pair or a non-empty list");
}
return As<Cell>(ptr)->GetFirst();
}
Object* CdrFunction::Invoke(Object* ptr, Scope* scope) {
auto input = Convert(ptr);
auto params = CalcVector<Object>(input, scope);
if (input.size() != 2) {
throw RuntimeError("Cdr function must have 1 parameter");
}
if (!params[0] || !Is<Cell>(params[0])) {
throw RuntimeError("Cdr function's parameter should be a pair or a non-empty list");
}
return As<Cell>(params[0])->GetSecond();
}
Object* ListFunction::Invoke(Object* ptr, Scope* scope) {
auto input = Convert(ptr);
auto params = CalcVector<Number>(input, scope);
if (params.empty()) {
return nullptr;
}
Cell* start(GetGC().New<Cell>());
auto last = start;
for (size_t i = 0; i < params.size(); ++i) {
last->GetFirst() = params[i];
if (i + 1 < params.size()) {
last->GetSecond() = GetGC().New<Cell>();
last = As<Cell>(last->GetSecond());
}
}
return start;
}
Object* ListRefFunction::Invoke(Object* ptr, Scope* scope) {
auto input = Convert(ptr);
auto params = CalcVector<Object>(input, scope);
if (params.size() != 2) {
throw RuntimeError("List ref function should have 2 params");
}
if (!(Is<Cell>(params[0]) && Is<Number>(params[1]))) {
throw RuntimeError("List ref function bad params");
}
input = Convert(params[0]);
size_t ind = As<Number>(params[1])->GetValue();
if (ind >= input.size()) {
throw RuntimeError("List ref function is not in the correct range");
} else {
return input[ind];
}
}
Object* ListTailFunction::Invoke(Object* ptr, Scope* scope) {
auto input = Convert(ptr);
auto params = CalcVector<Object>(input, scope);
if (params.size() != 2) {
throw RuntimeError("List tail function should have 2 params");
}
if (!(Is<Cell>(params[0]) && Is<Number>(params[1]))) {
throw RuntimeError("List tail function bad params");
}
input = Convert(params[0]);
size_t ind = As<Number>(params[1])->GetValue();
if (ind > input.size()) {
throw RuntimeError("List ref function is not in the correct range");
} else {
input.erase(input.begin(), input.begin() + ind);
if (input.empty()) {
return nullptr;
}
Cell* start(GetGC().New<Cell>());
auto last = start;
for (size_t i = 0; i < input.size(); ++i) {
last->GetFirst() = input[i];
if (i + 1 < input.size()) {
last->GetSecond() = GetGC().New<Cell>();
last = As<Cell>(last->GetSecond());
}
}
return start;
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////
/// If
Object* IfFunction::Invoke(Object* ptr, Scope* scope) {
auto input = Convert(ptr);
if (input.size() <= 2) {
throw SyntaxError("If must have at least 1 statement and 1 branch");
}
auto stat_ans = CalcExpression(input[1], scope);
if (!Is<Bool>(stat_ans)) {
throw SyntaxError("If's statement should convert to bool");
}
if (As<Bool>(stat_ans)->GetValue()) {
return CalcExpression(input[2], scope);
} else {
if (input.size() < 4) {
return nullptr;
} else {
return CalcExpression(input[3], scope);
}
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Variable
Variable::Variable(Object* ptr) : var_{ptr} {
}
Object* Variable::Invoke(Object* ptr, Scope*) {
auto input = Convert(ptr);
if (input.size() > 1) {
throw RuntimeError("Variables evaluates to temselfs, they need no parameters");
}
return var_;
}
Object* Variable::GetVal() {
return var_;
}
Object* DefineLambda(Object* ptr, Scope* scope) {
auto input = Convert(ptr);
auto lambda_capture = Convert(input[1]);
std::vector<Object*> lambda_capture_params;
for (size_t i = 0; i < lambda_capture.size(); ++i) {
if (!Is<Symbol>(lambda_capture[i])) {
throw RuntimeError(
"Define with lambda sugar should has only symbols in first parameter");
}
lambda_capture_params.push_back(lambda_capture[i]);
}
auto name = lambda_capture_params[0];
lambda_capture_params.erase(lambda_capture_params.begin());
auto lambda_actions = input;
lambda_actions.erase(lambda_actions.begin(), lambda_actions.begin() + 2);
auto rhs = GetGC().New<Lambda>(scope, lambda_capture_params, lambda_actions);
scope->Set(As<Symbol>(name)->GetName(), rhs);
return rhs;
}
Object* DefineFunction::Invoke(Object* ptr, Scope* scope) {
auto input = Convert(ptr);
if (input.size() < 2) {
throw SyntaxError("Any define should have 2 or more parameters");
}
if (Is<Cell>(input[1])) {
return DefineLambda(ptr, scope);
}
if (input.size() != 3) {
throw SyntaxError("Define should have 2 parameters");
}
auto lhs = input[1];
auto rhs = CalcExpression(input[2], scope);
if (!Is<Symbol>(lhs)) {
throw RuntimeError("Define should have a string as a first parameter");
}
if (Is<Lambda>(rhs)) {
scope->Set(As<Symbol>(lhs)->GetName(), rhs);
} else {
scope->Set(As<Symbol>(lhs)->GetName(), GetGC().New<Variable>(rhs));
}
return rhs;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Set
void TrySet(Object* name, Object* val, Scope* scope) {
bool is_set = scope->TrySet(As<Symbol>(name)->GetName(), val);
if (is_set) {
return;
}
if (scope->par_scope_ == nullptr) {
throw NameError("There is no variable with such name");
}
TrySet(name, val, scope->par_scope_);
}
Object* SetFunction::Invoke(Object* ptr, Scope* scope) {
auto input = Convert(ptr);
if (input.size() != 3) {
throw SyntaxError("Set function should have 2 parameters");
}
if (!Is<Symbol>(input[1])) {
throw RuntimeError("Set function should have symbol as the first parameter");
}
auto name = input[1];
auto val = CalcExpression(input[2], scope);
auto ret_val = val;
if (!Is<Lambda>(val)) {
val = GetGC().New<Variable>(val);
}
TrySet(name, val, scope);
return ret_val;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////
/// SetCar
void TrySetCar(Object* name, Object* val, Scope* scope) {
bool is_set = scope->TrySetCar(As<Symbol>(name)->GetName(), val);
if (is_set) {
return;
}
if (scope->par_scope_ == nullptr) {
throw NameError("There is no variable with such name");
}
TrySetCar(name, val, scope->par_scope_);
}
Object* SetCarFunction::Invoke(Object* ptr, Scope* scope) {
auto input = Convert(ptr);
if (input.size() != 3) {
throw RuntimeError("Set-car function should have 2 parameters");
}
auto name = CalcExpression(input[1], scope);
if (!Is<Cell>(name)) {
throw RuntimeError("Set-car function should have pair or list as the first parameter");
}
auto val = CalcExpression(input[2], scope);
As<Cell>(name)->GetFirst() = val;
return val;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////
/// SetCdr
void TrySetCdr(Object* name, Object* val, Scope* scope) {
bool is_set = scope->TrySetCdr(As<Symbol>(name)->GetName(), val);
if (is_set) {
return;
}
if (scope->par_scope_ == nullptr) {
throw NameError("There is no variable with such name");
}
TrySetCdr(name, val, scope->par_scope_);
}
Object* SetCdrFunction::Invoke(Object* ptr, Scope* scope) {
auto input = Convert(ptr);
if (input.size() != 3) {
throw RuntimeError("Set-cdr function should have 2 parameters");
}
auto name = CalcExpression(input[1], scope);
if (!Is<Cell>(name)) {
throw RuntimeError("Set-cdr function should have symbol as the first parameter");
}
auto val = CalcExpression(input[2], scope);
As<Cell>(name)->GetSecond() = val;
return val;
}
Object* IsSymbolFunction::Invoke(Object* ptr, Scope* scope) {
auto input = Convert(ptr);
if (input.size() != 2) {
throw RuntimeError("Is Symbol function should have only 1 parameter");
}
if (Is<Symbol>(CalcExpression(input[1], scope))) {
return GetGC().New<Bool>(true);
} else {
return GetGC().New<Bool>(false);
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Lambda
Lambda::Lambda(Scope* scope, const std::vector<Object*>& params,
const std::vector<Object*>& actions)
: par_scope_{scope}, params_{params}, actions_{actions} {
}
Object* Lambda::Invoke(Object* ptr, Scope* scope) {
auto input = Convert(ptr);
if (input.size() != 1 + params_.size()) {
throw RuntimeError("Lambda's parameter pack and call pack lengths differ");
}
auto input_params = CalcVector<Object>(input, scope);
Scope* inner_scope(GetGC().New<Scope>(par_scope_));
for (size_t i = 0; i < input_params.size(); ++i) {
if (Is<Lambda>(input_params[i])) {
inner_scope->Set(As<Symbol>(params_[i])->GetName(), input_params[i]);
} else {
inner_scope->Set(As<Symbol>(params_[i])->GetName(),
GetGC().New<Variable>(input_params[i]));
}
}
Object* ans;
for (size_t i = 0; i < actions_.size(); ++i) {
ans = CalcExpression(actions_[i], inner_scope);
}
return ans;
}
LambdaGenerator::LambdaGenerator(Scope* scope) : scope_{scope} {
}
std::vector<Object*> GetParams(Object* ptr) {
if (ptr && !Is<Cell>(ptr)) {
throw RuntimeError("Incorrect lambda parameters");
}
std::vector<Object*> ans;
while (Is<Cell>(ptr)) {
ans.push_back(As<Cell>(ptr)->GetFirst());
if (!Is<Symbol>(ans.back())) {
throw RuntimeError("Lambda should has only symbols in parameters");
}
ptr = As<Cell>(ptr)->GetSecond();
}
if (ptr != nullptr) {
ans.push_back(ptr);
if (!Is<Symbol>(ans.back())) {
throw RuntimeError("Lambda should has only symbols in parameters");
}
}
return ans;
}
std::vector<Object*> GetActions(std::vector<Object*> obj) {
obj.erase(obj.begin(), obj.begin() + 2);
return obj;
}
Object* LambdaGenerator::Invoke(Object* ptr, Scope*) {
auto input = Convert(ptr);
if (input.size() < 3) {
throw SyntaxError("Lambda should have 1 parameter pack and at least 1 action");
}
auto params = GetParams(input[1]);
auto actions = GetActions(input);
return GetGC().New<Lambda>(scope_, params, actions);
}