-
Notifications
You must be signed in to change notification settings - Fork 0
/
object.cpp
82 lines (76 loc) · 1.96 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
#include "functional_object.h"
void Heap::CleanUp(Object* root) {
used_.clear();
for (Object* cur : objects_) {
cur->Unmark();
}
Mark(root);
std::vector<Object*> alive_objects;
for (Object* cur : objects_) {
if (cur != nullptr && !cur->Marked()) {
delete cur;
} else if (cur != nullptr) {
alive_objects.push_back(cur);
}
}
objects_ = alive_objects;
}
void Heap::Mark(Object* root) {
if (!root) {
return;
}
if (used_.count(root)) {
return;
}
used_.insert(root);
root->Mark();
for (Object* nxt : root->Dep()) {
Mark(nxt);
}
}
Heap& Hp() {
static Heap heap;
return heap;
}
Object* Symbol::Eval(Object* scope) const {
if (!Is<Scope>(scope)) {
throw std::logic_error("Scope is not a scope in Symbol::Eval");
}
if (!As<Scope>(scope)->Exists(name_)) {
throw NameError("can not eval symbol: no such name " + name_);
}
Object* me = As<Scope>(scope)->Find(name_);
return me;
}
Object* Cell::Eval(Object* scope) const {
if (!first_) {
throw RuntimeError("cant recognize operator while evaluation");
}
Object* first_eval = first_->Eval(scope);
if (!Is<FunctionalObject>(first_eval)) {
throw RuntimeError("cant evaluate cell");
}
return As<FunctionalObject>(first_eval)->Calc(second_, scope);
}
std::string Cell::Serialize() const {
std::string res = "(";
Cell* cur = Hp().Make<Cell>(GetFirst(), GetSecond());
while (true) {
if (!cur->GetFirst()) {
res += "()";
} else {
res += cur->GetFirst()->Serialize();
}
if (!cur->GetSecond()) {
break;
}
res += " ";
if (!Is<Cell>(cur->GetSecond())) {
res += ". " + cur->GetSecond()->Serialize();
break;
}
Cell* next = As<Cell>(cur->GetSecond());
cur = next;
}
return res + ")";
}