-
Notifications
You must be signed in to change notification settings - Fork 0
/
object.cpp
534 lines (490 loc) · 20.8 KB
/
object.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
#include "object.h"
#include <cmath>
#include <random>
void Scope::CreateGlobalScope() {
functions_["+"] = std::make_shared<SumFunction>();
functions_["-"] = std::make_shared<SubtractFunction>();
functions_["*"] = std::make_shared<MultiplyFunction>();
functions_["/"] = std::make_shared<DivideFunction>();
functions_["max"] = std::make_shared<MaxFunction>();
functions_["min"] = std::make_shared<MinFunction>();
functions_["abs"] = std::make_shared<AbsFunction>();
functions_["number?"] = std::make_shared<IsNumberFunction>();
functions_["quote"] = std::make_shared<QuoteFunction>();
functions_["="] = std::make_shared<EqualFunction>();
functions_["<"] = std::make_shared<LessFunction>();
functions_[">"] = std::make_shared<GreaterFunction>();
functions_["<="] = std::make_shared<LessOrEqualFunction>();
functions_[">="] = std::make_shared<GreaterOrEqualFunction>();
functions_["boolean?"] = std::make_shared<IsBoolFunction>();
functions_["and"] = std::make_shared<AndFunction>();
functions_["or"] = std::make_shared<OrFunction>();
functions_["not"] = std::make_shared<NotFunction>();
functions_["null?"] = std::make_shared<IsNullFunction>();
functions_["pair?"] = std::make_shared<IsPairFunction>();
functions_["list?"] = std::make_shared<IsListFunction>();
functions_["symbol?"] = std::make_shared<IsSymbolFunction>();
functions_["cons"] = std::make_shared<ConstructPairFunction>();
functions_["car"] = std::make_shared<GetFirstElementFunction>();
functions_["cdr"] = std::make_shared<GetSecondElementFunction>();
functions_["list"] = std::make_shared<ConstructListFunction>();
functions_["list-ref"] = std::make_shared<GetElementFunction>();
functions_["list-tail"] = std::make_shared<GetTailFunction>();
functions_["if"] = std::make_shared<IfFunction>();
functions_["define"] = std::make_shared<DefineFunction>();
functions_["set!"] = std::make_shared<SetFunction>();
functions_["set-car!"] = std::make_shared<SetFirstFunction>();
functions_["set-cdr!"] = std::make_shared<SetSecondFunction>();
functions_["lambda"] = std::make_shared<LambdaFunction>();
global_scope_ = this->shared_from_this();
}
std::shared_ptr<Object> Scope::CallFunction(std::shared_ptr<Object> func,
std::shared_ptr<Object> object,
std::shared_ptr<Scope> scope) {
if (!Is<Symbol>(func) && !Is<FunctionObject>(func)) {
throw RuntimeError("Invalid function name");
}
if (Is<FunctionObject>(func)) {
if (object && object->object_scope) {
scope = object->object_scope;
}
return As<FunctionObject>(func)->GetFunction()->Execute(object, scope);
}
if (functions_.contains(As<Symbol>(func)->GetName())) {
if (object && object->object_scope) {
scope = object->object_scope;
}
return functions_[As<Symbol>(func)->GetName()]->Execute(object, scope);
}
if (!parent_scope_) {
throw NameError("Unknown function: " + As<Symbol>(func)->GetName());
}
return parent_scope_->CallFunction(func, object, scope);
}
std::shared_ptr<Object> Scope::GetVariable(const std::string& name) {
if (variables_.contains(name)) {
return variables_[name];
}
if (!parent_scope_) {
throw NameError("Unknown variable: " + name);
}
return parent_scope_->GetVariable(name);
}
std::shared_ptr<Object> Scope::AddVariable(const std::string& name,
std::shared_ptr<Object> variable) {
return variables_[name] = variable;
}
std::shared_ptr<Object> Scope::UpdateVariable(const std::string& name,
std::shared_ptr<Object> variable) {
if (variables_.contains(name)) {
return variables_[name] = variable;
}
if (!parent_scope_) {
throw NameError("Unknown variable: " + name);
}
return parent_scope_->UpdateVariable(name, variable);
}
std::shared_ptr<Object> Scope::AddFunction(const std::string& name,
std::shared_ptr<IFunction> func) {
functions_[name] = func;
global_scope_->all_functions_.insert(name);
return std::make_shared<Symbol>(name);
}
void Scope::AddParentScope(std::shared_ptr<Scope> parent_scope) {
parent_scope_ = parent_scope;
global_scope_ = parent_scope->global_scope_;
}
std::shared_ptr<IFunction> Scope::GetFunction(const std::string& name) {
if (functions_.contains(name)) {
return functions_[name];
}
if (!parent_scope_) {
throw NameError("Unknown function: " + name);
}
return parent_scope_->GetFunction(name);
}
bool Scope::IsFunctionExists(const std::string& name) {
return global_scope_->all_functions_.contains(name);
}
std::shared_ptr<Symbol> BoolToSymbol(bool statement) {
if (statement) {
return std::make_shared<Symbol>("#t");
}
return std::make_shared<Symbol>("#f");
}
bool ObjectToBool(std::shared_ptr<Object> object) {
if (!Is<Symbol>(object) || As<Symbol>(object)->GetName() != "#f") {
return true;
}
return false;
};
bool IsBool(std::shared_ptr<Object> object) {
return Is<Symbol>(object) &&
(As<Symbol>(object)->GetName() == "#t" || As<Symbol>(object)->GetName() == "#f");
}
std::shared_ptr<Object> Evaluate(std::shared_ptr<Object> object, std::shared_ptr<Scope> scope) {
if (object) {
if (object->object_scope) {
scope = object->object_scope;
}
return object->Evaluate(scope);
}
throw RuntimeError("Bad list");
}
std::shared_ptr<Object> UserFunction::Execute(std::shared_ptr<Object> object,
std::shared_ptr<Scope> scope) {
std::vector<std::shared_ptr<Object>> input_args;
while (object) {
if (!Is<Cell>(object)) {
throw RuntimeError("Bad list");
}
input_args.push_back(As<Cell>(object)->GetFirst());
object = As<Cell>(object)->GetSecond();
}
if (input_args.size() != args_.size()) {
throw RuntimeError("Invalid argument count");
}
auto new_scope = std::make_shared<Scope>();
new_scope->AddParentScope(parent_scope_);
for (size_t i = 0; i < args_.size(); ++i) {
new_scope->AddVariable(args_[i], Evaluate(input_args[i], scope));
}
for (size_t i = 0; i + 1 < executables_.size(); ++i) {
Evaluate(executables_[i], new_scope);
}
auto ans = Evaluate(executables_.back(), new_scope);
ans->object_scope = new_scope;
return ans;
}
std::shared_ptr<Object> ArithmeticFunction::Execute(std::shared_ptr<Object> object,
std::shared_ptr<Scope> scope) {
int64_t ans = 0;
bool is_first = true;
std::shared_ptr<Object> lhs;
while (Is<Cell>(object)) {
lhs = Evaluate(As<Cell>(object)->GetFirst(), scope);
if (!Is<Number>(lhs)) {
throw RuntimeError("Bad list");
}
if (is_first) {
ans = As<Number>(lhs)->GetValue();
} else {
ans = Operation(ans, As<Number>(lhs)->GetValue());
}
is_first = false;
object = As<Cell>(object)->GetSecond();
}
if (object) {
throw RuntimeError("Bad list");
}
if (is_first) {
return std::make_shared<Number>(GetDefaultValue());
}
return std::make_shared<Number>(ans);
}
std::shared_ptr<Object> BooleanFunction::Execute(std::shared_ptr<Object> object,
std::shared_ptr<Scope> scope) {
std::shared_ptr<Object> lhs, prev = BoolToSymbol(GetDefaultValue());
while (Is<Cell>(object)) {
lhs = Evaluate(As<Cell>(object)->GetFirst(), scope);
if (ObjectToBool(lhs) != GetDefaultValue()) {
return lhs;
}
prev = lhs;
object = As<Cell>(object)->GetSecond();
}
if (object) {
throw RuntimeError("Bad list");
}
return prev;
}
std::shared_ptr<Object> ComparisonFunction::Execute(std::shared_ptr<Object> object,
std::shared_ptr<Scope> scope) {
bool ans = true, is_first = true;
int64_t prev;
std::shared_ptr<Object> lhs;
while (Is<Cell>(object)) {
lhs = Evaluate(As<Cell>(object)->GetFirst(), scope);
if (!Is<Number>(lhs)) {
throw RuntimeError("Bad list");
}
if (!is_first) {
ans = (ans && Compare(prev, As<Number>(lhs)->GetValue()));
}
prev = As<Number>(lhs)->GetValue();
is_first = false;
object = As<Cell>(object)->GetSecond();
}
if (object) {
throw RuntimeError("Bad list");
}
return BoolToSymbol(ans);
}
std::shared_ptr<Object> OneArgumentFunction::Execute(std::shared_ptr<Object> object,
std::shared_ptr<Scope> scope) {
if (!Is<Cell>(object) || As<Cell>(object)->GetSecond()) {
throw RuntimeError("Invalid argument");
}
return Function(As<Cell>(object)->GetFirst(), scope);
}
std::shared_ptr<Object> AbsFunction::Function(std::shared_ptr<Object> object,
std::shared_ptr<Scope> scope) {
object = Evaluate(object, scope);
if (!Is<Number>(object)) {
throw RuntimeError("Invalid argument");
}
return std::make_shared<Number>(std::abs(As<Number>(object)->GetValue()));
}
std::shared_ptr<Object> IsNumberFunction::Function(std::shared_ptr<Object> object,
std::shared_ptr<Scope> scope) {
return BoolToSymbol(Is<Number>(Evaluate(object, scope)));
}
std::shared_ptr<Object> IsBoolFunction::Function(std::shared_ptr<Object> object,
std::shared_ptr<Scope> scope) {
return BoolToSymbol(IsBool(Evaluate(object, scope)));
}
std::shared_ptr<Object> IsNullFunction::Function(std::shared_ptr<Object> object,
std::shared_ptr<Scope> scope) {
return BoolToSymbol(!Evaluate(object, scope));
}
std::shared_ptr<Object> IsPairFunction::Function(std::shared_ptr<Object> object,
std::shared_ptr<Scope> scope) {
object = Evaluate(object, scope);
return BoolToSymbol(Is<Cell>(object));
}
std::shared_ptr<Object> IsListFunction::Function(std::shared_ptr<Object> object,
std::shared_ptr<Scope> scope) {
object = Evaluate(object, scope);
while (Is<Cell>(object)) {
object = As<Cell>(object)->GetSecond();
}
return BoolToSymbol(!object);
}
std::shared_ptr<Object> IsSymbolFunction::Function(std::shared_ptr<Object> object,
std::shared_ptr<Scope> scope) {
object = Evaluate(object, scope);
return BoolToSymbol(Is<Symbol>(object) && !IsBool(object));
}
std::shared_ptr<Object> QuoteFunction::Function(std::shared_ptr<Object> object,
std::shared_ptr<Scope>) {
return object;
}
std::shared_ptr<Object> NotFunction::Function(std::shared_ptr<Object> object,
std::shared_ptr<Scope> scope) {
return BoolToSymbol(!ObjectToBool(Evaluate(object, scope)));
}
std::shared_ptr<Object> GetFirstElementFunction::Function(std::shared_ptr<Object> object,
std::shared_ptr<Scope> scope) {
object = Evaluate(object, scope);
if (!Is<Cell>(object)) {
throw RuntimeError("Invalid argument");
}
auto ans = As<Cell>(object)->GetFirst();
if (ans && object->object_scope) {
ans->object_scope = object->object_scope;
}
return ans;
}
std::shared_ptr<Object> GetSecondElementFunction::Function(std::shared_ptr<Object> object,
std::shared_ptr<Scope> scope) {
object = Evaluate(object, scope);
if (!Is<Cell>(object)) {
throw RuntimeError("Invalid argument");
}
auto ans = As<Cell>(object)->GetSecond();
if (ans && object->object_scope) {
ans->object_scope = object->object_scope;
}
return ans;
}
std::shared_ptr<Object> GetElementFunction::Execute(std::shared_ptr<Object> object,
std::shared_ptr<Scope> scope) {
if (!Is<Cell>(object) || !Is<Cell>(As<Cell>(object)->GetSecond()) ||
As<Cell>(As<Cell>(object)->GetSecond())->GetSecond() ||
!Is<Number>(As<Cell>(As<Cell>(object)->GetSecond())->GetFirst())) {
throw RuntimeError("Invalid argument");
}
std::shared_ptr<Object> list = Evaluate(As<Cell>(object)->GetFirst(), scope);
size_t index = As<Number>(As<Cell>(As<Cell>(object)->GetSecond())->GetFirst())->GetValue();
while (Is<Cell>(list) && index > 0) {
list = As<Cell>(list)->GetSecond();
--index;
}
if (!Is<Cell>(list)) {
throw RuntimeError("Out of bounds");
}
return As<Cell>(list)->GetFirst();
}
std::shared_ptr<Object> GetTailFunction::Execute(std::shared_ptr<Object> object,
std::shared_ptr<Scope> scope) {
if (!Is<Cell>(object) || !Is<Cell>(As<Cell>(object)->GetSecond()) ||
As<Cell>(As<Cell>(object)->GetSecond())->GetSecond() ||
!Is<Number>(As<Cell>(As<Cell>(object)->GetSecond())->GetFirst())) {
throw RuntimeError("Invalid argument");
}
std::shared_ptr<Object> list = Evaluate(As<Cell>(object)->GetFirst(), scope);
size_t index = As<Number>(As<Cell>(As<Cell>(object)->GetSecond())->GetFirst())->GetValue();
while (Is<Cell>(list) && index > 0) {
list = As<Cell>(list)->GetSecond();
--index;
}
if (index > 0) {
throw RuntimeError("Out of bounds");
}
return list;
}
std::shared_ptr<Object> ConstructPairFunction::Execute(std::shared_ptr<Object> object,
std::shared_ptr<Scope>) {
if (!Is<Cell>(object) || !Is<Cell>(As<Cell>(object)->GetSecond()) ||
As<Cell>(As<Cell>(object)->GetSecond())->GetSecond()) {
throw RuntimeError("Invalid argument");
}
return std::make_shared<Cell>(As<Cell>(object)->GetFirst(),
As<Cell>(As<Cell>(object)->GetSecond())->GetFirst());
}
std::shared_ptr<Object> ConstructListFunction::Execute(std::shared_ptr<Object> object,
std::shared_ptr<Scope>) {
return object;
}
std::shared_ptr<Object> IfFunction::Execute(std::shared_ptr<Object> object,
std::shared_ptr<Scope> scope) {
if (!Is<Cell>(object) || !Is<Cell>(As<Cell>(object)->GetSecond()) ||
(As<Cell>(As<Cell>(object)->GetSecond())->GetSecond() &&
(!Is<Cell>(As<Cell>(As<Cell>(object)->GetSecond())->GetSecond()) ||
As<Cell>(As<Cell>(As<Cell>(object)->GetSecond())->GetSecond())->GetSecond()))) {
throw SyntaxError("Invalid argument");
}
if (ObjectToBool(Evaluate(As<Cell>(object)->GetFirst(), scope))) {
return Evaluate(As<Cell>(As<Cell>(object)->GetSecond())->GetFirst(), scope);
}
if (!As<Cell>(As<Cell>(object)->GetSecond())->GetSecond()) {
return nullptr;
}
return Evaluate(As<Cell>(As<Cell>(As<Cell>(object)->GetSecond())->GetSecond())->GetFirst(),
scope);
}
std::shared_ptr<Object> DefineFunction::Execute(std::shared_ptr<Object> object,
std::shared_ptr<Scope> scope) {
if (!Is<Cell>(object)) {
throw SyntaxError("Invalid argument");
}
if (!Is<Symbol>(As<Cell>(object)->GetFirst())) {
std::shared_ptr<Object> list = As<Cell>(object)->GetFirst();
if (!Is<Cell>(list) || !Is<Symbol>(As<Cell>(list)->GetFirst())) {
throw SyntaxError("Invalid argument");
}
std::string name = As<Symbol>(As<Cell>(list)->GetFirst())->GetName();
std::vector<std::string> args;
list = As<Cell>(list)->GetSecond();
while (list) {
if (!Is<Cell>(list) || !Is<Symbol>(As<Cell>(list)->GetFirst())) {
throw SyntaxError("Invalid argument");
}
args.push_back(As<Symbol>(As<Cell>(list)->GetFirst())->GetName());
list = As<Cell>(list)->GetSecond();
}
object = As<Cell>(object)->GetSecond();
std::vector<std::shared_ptr<Object>> executables;
while (object) {
if (!Is<Cell>(object)) {
throw SyntaxError("Invalid argument");
}
executables.push_back(As<Cell>(object)->GetFirst());
object = As<Cell>(object)->GetSecond();
}
if (executables.empty()) {
throw SyntaxError("Lambda should have at least 1 expression");
}
return scope->AddFunction(name, std::make_shared<UserFunction>(args, executables, scope));
}
if (!Is<Cell>(As<Cell>(object)->GetSecond()) ||
As<Cell>(As<Cell>(object)->GetSecond())->GetSecond()) {
throw SyntaxError("Invalid argument");
}
std::shared_ptr<Object> second =
Evaluate(As<Cell>(As<Cell>(object)->GetSecond())->GetFirst(), scope);
if (Is<FunctionObject>(second)) {
return scope->AddFunction(As<Symbol>(As<Cell>(object)->GetFirst())->GetName(),
As<FunctionObject>(second)->GetFunction());
}
return scope->AddVariable(As<Symbol>(As<Cell>(object)->GetFirst())->GetName(),
Evaluate(As<Cell>(As<Cell>(object)->GetSecond())->GetFirst(), scope));
}
std::shared_ptr<Object> SetFunction::Execute(std::shared_ptr<Object> object,
std::shared_ptr<Scope> scope) {
if (!Is<Cell>(object) || !Is<Cell>(As<Cell>(object)->GetSecond()) ||
As<Cell>(As<Cell>(object)->GetSecond())->GetSecond() ||
!Is<Symbol>(As<Cell>(object)->GetFirst())) {
throw SyntaxError("Invalid argument");
}
return scope->UpdateVariable(
As<Symbol>(As<Cell>(object)->GetFirst())->GetName(),
Evaluate(As<Cell>(As<Cell>(object)->GetSecond())->GetFirst(), scope));
}
std::shared_ptr<Object> SetFirstFunction::Execute(std::shared_ptr<Object> object,
std::shared_ptr<Scope> scope) {
if (!Is<Cell>(object) || !Is<Cell>(As<Cell>(object)->GetSecond()) ||
As<Cell>(As<Cell>(object)->GetSecond())->GetSecond()) {
throw SyntaxError("Invalid argument");
}
auto pair = Evaluate(As<Cell>(object)->GetFirst(), scope);
if (!Is<Cell>(pair)) {
throw SyntaxError("Invalid argument");
}
As<Cell>(pair)->GetFirst() =
Evaluate(As<Cell>(As<Cell>(object)->GetSecond())->GetFirst(), scope);
return pair;
}
std::shared_ptr<Object> SetSecondFunction::Execute(std::shared_ptr<Object> object,
std::shared_ptr<Scope> scope) {
if (!Is<Cell>(object) || !Is<Cell>(As<Cell>(object)->GetSecond()) ||
As<Cell>(As<Cell>(object)->GetSecond())->GetSecond()) {
throw SyntaxError("Invalid argument");
}
auto pair = Evaluate(As<Cell>(object)->GetFirst(), scope);
if (!Is<Cell>(pair)) {
throw SyntaxError("Invalid argument");
}
As<Cell>(pair)->GetSecond() =
Evaluate(As<Cell>(As<Cell>(object)->GetSecond())->GetFirst(), scope);
return pair;
}
std::shared_ptr<Object> LambdaFunction::Execute(std::shared_ptr<Object> object,
std::shared_ptr<Scope> scope) {
if (!Is<Cell>(object)) {
throw SyntaxError("Invalid argument");
}
std::shared_ptr<Object> list = As<Cell>(object)->GetFirst();
std::vector<std::string> args;
while (list) {
if (!Is<Cell>(list) || !Is<Symbol>(As<Cell>(list)->GetFirst())) {
throw SyntaxError("Invalid argument");
}
args.push_back(As<Symbol>(As<Cell>(list)->GetFirst())->GetName());
list = As<Cell>(list)->GetSecond();
}
object = As<Cell>(object)->GetSecond();
std::vector<std::shared_ptr<Object>> executables;
while (object) {
if (!Is<Cell>(object)) {
throw SyntaxError("Invalid argument");
}
executables.push_back(As<Cell>(object)->GetFirst());
object = As<Cell>(object)->GetSecond();
}
if (executables.empty()) {
throw SyntaxError("Lambda should have at least 1 expression");
}
return std::make_shared<FunctionObject>(
std::make_shared<UserFunction>(args, executables, scope));
}
Cell::Cell(std::shared_ptr<Object> first, std::shared_ptr<Object> second)
: first_(first), second_(second) {
}
std::shared_ptr<Object> Cell::Evaluate(std::shared_ptr<Scope> scope) {
auto lhs = first_;
while (!Is<Number>(lhs) && !Is<Symbol>(lhs) && !Is<FunctionObject>(lhs)) {
lhs = ::Evaluate(lhs, scope);
}
return scope->CallFunction(lhs, second_, scope);
}