-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathruntime.cpp
282 lines (209 loc) · 7.81 KB
/
runtime.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
#include "runtime.h"
#include <cassert>
#include <iostream>
#include <optional>
#include <sstream>
using namespace std;
namespace {
const string SELF_OBJECT = "self"s;
const string STR_METHOD = "__str__"s;
const string EQ_METHOD = "__eq__"s;
const string LT_METHOD = "__lt__"s;
} // namespace
namespace runtime {
ObjectHolder::ObjectHolder(std::shared_ptr<Object> data)
: data_(std::move(data)) {
}
void ObjectHolder::AssertIsValid() const {
assert(data_ != nullptr);
}
ObjectHolder ObjectHolder::Share(Object& object) {
return ObjectHolder(std::shared_ptr<Object>(&object, [](auto* /*p*/) { /* do nothing */ }));
}
ObjectHolder ObjectHolder::None() {
return ObjectHolder();
}
Object& ObjectHolder::operator*() const {
AssertIsValid();
return *Get();
}
Object* ObjectHolder::operator->() const {
AssertIsValid();
return Get();
}
Object* ObjectHolder::Get() const {
return data_.get();
}
ObjectHolder::operator bool() const {
return Get() != nullptr;
}
bool IsTrue(const ObjectHolder& object) {
if (!object) {
return false;
}
auto ptr_b = object.TryAs<runtime::Bool>();
if (ptr_b != nullptr) {
return ptr_b->GetValue();
}
auto ptr_n = object.TryAs<runtime::Number>();
if (ptr_n != nullptr) {
return ptr_n->GetValue() != 0;
}
auto ptr_s = object.TryAs<runtime::String>();
if (ptr_s != nullptr) {
if (ptr_s->GetValue() == ""s) {
return false;
} else {
return true;
}
}
auto ptr_vo_bool = object.TryAs<runtime::ValueObject<bool>>();
if (ptr_vo_bool != nullptr) {
return ptr_vo_bool->GetValue();
}
return false;
}
void ClassInstance::Print(std::ostream& os, Context& context) {
auto method_ptr = this->cls_.GetMethod(STR_METHOD);
if (method_ptr != nullptr) {
auto res = Call(method_ptr->name, {}, context);
res->Print(os, context);
} else {
os << this;
}
}
bool ClassInstance::HasMethod(const std::string& method, size_t argument_count) const {
auto ptr = cls_.GetMethod(method);
if (ptr != nullptr && ptr->formal_params.size() == argument_count) {
return true;
}
return false;
}
Closure& ClassInstance::Fields() {
return fields_;
}
const Closure& ClassInstance::Fields() const {
return fields_;
}
ClassInstance::ClassInstance(const Class& cls)
: cls_(cls) {
}
ObjectHolder ClassInstance::Call(const std::string& method,
const std::vector<ObjectHolder>& actual_args,
Context& context) {
if (!this->HasMethod(method, actual_args.size())) {
throw std::runtime_error("No method found");
}
auto* method_ptr = cls_.GetMethod(method);
Closure cl;
cl[SELF_OBJECT] = ObjectHolder::Share(*this);
size_t params_size = method_ptr->formal_params.size();
for (size_t i = 0; i < params_size; ++i) {
auto arg = method_ptr->formal_params[i];
cl[arg] = actual_args[i];
}
return method_ptr->body->Execute(cl, context);
}
Class::Class(std::string name, std::vector<Method> methods, const Class* parent)
: name_(std::move(name))
, methods_(std::move(methods))
, parent_(parent) {
if (parent_ != nullptr) {
for (const auto& method : parent_->methods_) {
name_to_method_[method.name] = &method;
}
}
for (const auto& method : methods_) {
name_to_method_[method.name] = &method;
}
}
const Class* Class::GetParent() const {
return parent_;
}
const Method* Class::GetMethod(const std::string& name) const {
auto it = name_to_method_.find(name);
if (it != name_to_method_.end()) {
return it->second;
}
return nullptr;
}
const std::string& Class::GetName() const {
return this->name_;
}
void Class::Print(ostream& os, Context& /* context */) {
os << "Class "sv;
os << this->GetName();
}
void Bool::Print(std::ostream& os, Context& /* context */) {
os << (GetValue() ? "True"sv : "False"sv);
}
bool Equal(const ObjectHolder& lhs, const ObjectHolder& rhs, Context& context) {
if (!lhs && !rhs) {
return true;
}
if (!lhs) {
throw std::runtime_error("Cannot compare objects for equality"s);
}
auto l_ptr_n = lhs.TryAs<runtime::Number>();
auto r_ptr_n = rhs.TryAs<runtime::Number>();
if (l_ptr_n != nullptr && r_ptr_n != nullptr) {
return l_ptr_n->GetValue() == r_ptr_n->GetValue();
}
auto l_ptr_s = lhs.TryAs<runtime::String>();
auto r_ptr_s = rhs.TryAs<runtime::String>();
if (l_ptr_s != nullptr && r_ptr_s != nullptr) {
return l_ptr_s->GetValue() == r_ptr_s->GetValue();
}
auto l_ptr_b = lhs.TryAs<runtime::Bool>();
auto r_ptr_b = rhs.TryAs<runtime::Bool>();
if (l_ptr_b != nullptr && r_ptr_b != nullptr) {
return l_ptr_b->GetValue() == r_ptr_b->GetValue();
}
auto l_ptr_class_inst = lhs.TryAs<runtime::ClassInstance>();
constexpr int EQ_METHOD_ARGS_COUNT = 1;
if (l_ptr_class_inst != nullptr && l_ptr_class_inst->HasMethod(EQ_METHOD, EQ_METHOD_ARGS_COUNT)) {
auto res = l_ptr_class_inst->Call(EQ_METHOD, { rhs }, context);
return res.TryAs<Bool>()->GetValue();
}
throw std::runtime_error("Cannot compare objects for equality"s);
}
bool Less(const ObjectHolder& lhs, const ObjectHolder& rhs, Context& context) {
if (!lhs) {
throw std::runtime_error("Cannot compare objects for equality"s);
}
auto l_ptr_n = lhs.TryAs<runtime::Number>();
auto r_ptr_n = rhs.TryAs<runtime::Number>();
if (l_ptr_n != nullptr && r_ptr_n != nullptr) {
return l_ptr_n->GetValue() < r_ptr_n->GetValue();
}
auto l_ptr_s = lhs.TryAs<runtime::String>();
auto r_ptr_s = rhs.TryAs<runtime::String>();
if (l_ptr_s != nullptr && r_ptr_s != nullptr) {
return l_ptr_s->GetValue() < r_ptr_s->GetValue();
}
auto l_ptr_b = lhs.TryAs<runtime::Bool>();
auto r_ptr_b = rhs.TryAs<runtime::Bool>();
if (l_ptr_b != nullptr && r_ptr_b != nullptr) {
return l_ptr_b->GetValue() < r_ptr_b->GetValue();
}
auto l_ptr_class_inst = lhs.TryAs<runtime::ClassInstance>();
constexpr int LT_METHOD_ARGS_COUNT = 1;
if (l_ptr_class_inst != nullptr && l_ptr_class_inst->HasMethod(LT_METHOD, LT_METHOD_ARGS_COUNT)) {
auto res = l_ptr_class_inst->Call(LT_METHOD, { rhs }, context);
return res.TryAs<Bool>()->GetValue();
}
throw std::runtime_error("Cannot compare objects for less"s);
}
bool NotEqual(const ObjectHolder& lhs, const ObjectHolder& rhs, Context& context) {
return !Equal(lhs, rhs, context);
}
bool Greater(const ObjectHolder& lhs, const ObjectHolder& rhs, Context& context) {
return !Less(lhs, rhs, context) && !Equal(lhs, rhs, context);
}
bool LessOrEqual(const ObjectHolder& lhs, const ObjectHolder& rhs, Context& context) {
return Less(lhs, rhs, context) || Equal(lhs, rhs, context);
}
bool GreaterOrEqual(const ObjectHolder& lhs, const ObjectHolder& rhs, Context& context) {
return !Less(lhs, rhs, context);
}
} // namespace runtime