-
Notifications
You must be signed in to change notification settings - Fork 0
/
statement.cpp
421 lines (301 loc) · 12.7 KB
/
statement.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
#include "statement.h"
#include <iostream>
#include <sstream>
using namespace std;
namespace ast {
using runtime::Closure;
using runtime::Context;
using runtime::ObjectHolder;
namespace {
const string ADD_METHOD = "__add__"s;
const string INIT_METHOD = "__init__"s;
} // namespace
VariableValue::VariableValue(std::string var_name) {
dotted_ids_.push_back(std::move(var_name));
}
VariableValue::VariableValue(std::vector<std::string> dotted_ids)
: dotted_ids_(std::move(dotted_ids)) {
}
ObjectHolder VariableValue::Execute(Closure& closure, Context& /* context */) {
Closure* closure_ptr = &closure;
runtime::Closure::iterator current_obj_it;
for (const auto& var : dotted_ids_) {
current_obj_it = closure_ptr->find(var);
if (current_obj_it == closure_ptr->end()) {
throw std::runtime_error("var is not found");
}
auto class_inst_current_ptr = current_obj_it->second.TryAs<runtime::ClassInstance>();
if (class_inst_current_ptr == nullptr) {
return current_obj_it->second;
}
closure_ptr = &class_inst_current_ptr->Fields();
}
return current_obj_it->second;
}
Assignment::Assignment(std::string var, std::unique_ptr<Statement> rv)
: var_(std::move(var))
, rv_(std::move(rv)) {
}
ObjectHolder Assignment::Execute(Closure& closure, Context& context) {
closure[var_] = std::move(rv_->Execute(closure, context));
return closure.at(var_);
}
FieldAssignment::FieldAssignment(VariableValue object, std::string field_name, std::unique_ptr<Statement> rv)
: object_(std::move(object))
, field_name_(std::move(field_name))
, rv_(std::move(rv)) {
}
ObjectHolder FieldAssignment::Execute(Closure& closure, Context& context) {
auto obj = object_.Execute(closure, context);
auto clacc_inst_ptr = obj.TryAs<runtime::ClassInstance>();
clacc_inst_ptr->Fields()[field_name_] = std::move(rv_->Execute(closure, context));
return clacc_inst_ptr->Fields().at(field_name_);
}
NewInstance::NewInstance(const runtime::Class& class_)
: class_inst_(class_) {
}
NewInstance::NewInstance(const runtime::Class& class_, std::vector<std::unique_ptr<Statement>> args)
: class_inst_(class_)
, args_(std::move(args)) {
}
ObjectHolder NewInstance::Execute(Closure& closure, Context& context) {
std::vector<runtime::ObjectHolder> actual_args;
for (const auto& arg : args_) {
actual_args.push_back(std::move(arg->Execute(closure, context)));
}
if (class_inst_.HasMethod(INIT_METHOD, args_.size())) {
class_inst_.Call(INIT_METHOD, std::move(actual_args), context);
}
return runtime::ObjectHolder::Share(class_inst_);
}
MethodCall::MethodCall(std::unique_ptr<Statement> object, std::string method_name,
std::vector<std::unique_ptr<Statement>> args)
: object_(std::move(object))
, method_name_(std::move(method_name))
, args_(std::move(args)) {
}
ObjectHolder MethodCall::Execute(Closure& closure, Context& context) {
auto obj = object_->Execute(closure, context);
auto class_ptr = obj.TryAs<runtime::ClassInstance>();
std::vector<runtime::ObjectHolder> actual_args;
for (const auto& arg : args_) {
actual_args.push_back(std::move(arg->Execute(closure, context)));
}
auto res = class_ptr->Call(method_name_, actual_args, context);
return res;
}
void Compound::AddStatement(std::unique_ptr<Statement> stmt) {
statements_.push_back(std::move(stmt));
}
ObjectHolder Compound::Execute(Closure& closure, Context& context) {
for (const auto& statement : statements_) {
statement->Execute(closure, context);
}
return {};
}
RuntimeReturnExeption::RuntimeReturnExeption(const runtime::ObjectHolder& obj)
: obj_(obj) {
}
runtime::ObjectHolder RuntimeReturnExeption::GetValue() {
return obj_;
}
Return::Return(std::unique_ptr<Statement> statement)
: statement_(std::move(statement)) {
}
ObjectHolder Return::Execute(Closure& closure, Context& context) {
auto obj = statement_->Execute(closure, context);
throw RuntimeReturnExeption(obj);
}
MethodBody::MethodBody(std::unique_ptr<Statement>&& body)
: body_(std::move(body)) {
}
ObjectHolder MethodBody::Execute(Closure& closure, Context& context) {
try {
body_->Execute(closure, context);
} catch (RuntimeReturnExeption& obj) {
return obj.GetValue();
}
return {};
}
ClassDefinition::ClassDefinition(ObjectHolder cls)
: cls_(cls) {
}
ObjectHolder ClassDefinition::Execute(Closure& closure, Context& /* context */) {
auto obj = cls_.TryAs<runtime::Class>();
closure[obj->GetName()] = std::move(cls_);
return {};
}
Print::Print(unique_ptr<Statement> argument) {
args_.push_back(std::move(argument));
}
Print::Print(vector<unique_ptr<Statement>> args)
: args_(std::move(args)) {
}
std::unique_ptr<Print> Print::Variable(const std::string& name) {
return std::make_unique<Print>(std::make_unique<VariableValue>(name));
}
ObjectHolder Print::Execute(Closure& closure, Context& context) {
ObjectHolder obj;
for (const auto& arg : args_) {
if (arg != args_.front()) {
context.GetOutputStream() << " "s;
}
obj = arg->Execute(closure, context);
if (obj) {
obj->Print(context.GetOutputStream(), context);
} else {
context.GetOutputStream() << "None"s;
}
}
context.GetOutputStream() << "\n"s;
return obj;
}
ObjectHolder Stringify::Execute(Closure& closure, Context& context) {
auto obj = argument_->Execute(closure, context);
if (!obj) {
return ObjectHolder::Own(runtime::String{ "None"s });
}
runtime::DummyContext dummy_context;
obj->Print(dummy_context.GetOutputStream(), dummy_context);
return ObjectHolder::Own(runtime::String{ dummy_context.output.str() });
}
ObjectHolder Add::Execute(Closure& closure, Context& context) {
if (!rhs_ || !lhs_) {
throw std::runtime_error("null operands are not supported"s);
}
auto obj_lhs = lhs_->Execute(closure, context);
auto obj_rhs = rhs_->Execute(closure, context);
auto ptr_lhs_n = obj_lhs.TryAs<runtime::Number>();
auto ptr_rhs_n = obj_rhs.TryAs<runtime::Number>();
if (ptr_lhs_n != nullptr && ptr_rhs_n != nullptr) {
auto l_num = ptr_lhs_n->GetValue();
auto r_num = ptr_rhs_n->GetValue();
return ObjectHolder::Own(runtime::Number{ l_num + r_num });
}
auto ptr_lhs_s = obj_lhs.TryAs<runtime::String>();
auto ptr_rhs_s = obj_rhs.TryAs<runtime::String>();
if (ptr_lhs_s != nullptr && ptr_rhs_s != nullptr) {
auto l_str = ptr_lhs_s->GetValue();
auto r_str = ptr_rhs_s->GetValue();
return ObjectHolder::Own(runtime::String{ l_str + r_str });
}
auto ptr_lhs_class_inst = obj_lhs.TryAs<runtime::ClassInstance>();
if (ptr_lhs_class_inst != nullptr) {
constexpr int ADD_METHOD_ARGS_COUNT = 1;
if (ptr_lhs_class_inst->HasMethod(ADD_METHOD, ADD_METHOD_ARGS_COUNT)) {
return ptr_lhs_class_inst->Call(ADD_METHOD, { obj_rhs }, context);
}
}
throw std::runtime_error("incorrect add operands"s);
}
ObjectHolder Sub::Execute(Closure& closure, Context& context) {
if (!rhs_ || !lhs_) {
throw std::runtime_error("null operands are not supported"s);
}
auto obj_lhs = lhs_->Execute(closure, context);
auto obj_rhs = rhs_->Execute(closure, context);
auto ptr_lhs_n = obj_lhs.TryAs<runtime::Number>();
auto ptr_rhs_n = obj_rhs.TryAs<runtime::Number>();
if (ptr_lhs_n != nullptr && ptr_rhs_n != nullptr) {
auto l_num = ptr_lhs_n->GetValue();
auto r_num = ptr_rhs_n->GetValue();
return ObjectHolder::Own(runtime::Number{ l_num - r_num });
}
throw std::runtime_error("incorrect sub operands"s);
}
ObjectHolder Mult::Execute(Closure& closure, Context& context) {
if (!rhs_ || !lhs_) {
throw std::runtime_error("null operands are not supported"s);
}
auto obj_lhs = lhs_->Execute(closure, context);
auto obj_rhs = rhs_->Execute(closure, context);
auto ptr_lhs_n = obj_lhs.TryAs<runtime::Number>();
auto ptr_rhs_n = obj_rhs.TryAs<runtime::Number>();
if (ptr_lhs_n != nullptr && ptr_rhs_n != nullptr) {
auto l_num = ptr_lhs_n->GetValue();
auto r_num = ptr_rhs_n->GetValue();
return ObjectHolder::Own(runtime::Number{ l_num * r_num });
}
throw std::runtime_error("incorrect mult operands"s);
}
ObjectHolder Div::Execute(Closure& closure, Context& context) {
if (!rhs_ || !lhs_) {
throw std::runtime_error("null operands are not supported"s);
}
auto obj_lhs = lhs_->Execute(closure, context);
auto obj_rhs = rhs_->Execute(closure, context);
auto ptr_lhs_n = obj_lhs.TryAs<runtime::Number>();
auto ptr_rhs_n = obj_rhs.TryAs<runtime::Number>();
if (ptr_lhs_n != nullptr && ptr_rhs_n != nullptr) {
auto l_num = ptr_lhs_n->GetValue();
auto r_num = ptr_rhs_n->GetValue();
if (r_num == 0) {
throw std::runtime_error("division by zero"s);
}
return ObjectHolder::Own(runtime::Number{ l_num / r_num });
}
throw std::runtime_error("incorrect div operands"s);
}
ObjectHolder Or::Execute(Closure& closure, Context& context) {
if (!rhs_ || !lhs_) {
throw std::runtime_error("null operands are not supported"s);
}
auto l_obj = lhs_->Execute(closure, context);
auto r_obj = rhs_->Execute(closure, context);
if (runtime::IsTrue(l_obj)) {
return ObjectHolder::Own(runtime::Bool{ true });
}
if (runtime::IsTrue(r_obj)) {
return ObjectHolder::Own(runtime::Bool{ true });
}
return ObjectHolder::Own(runtime::Bool{ false });
}
ObjectHolder And::Execute(Closure& closure, Context& context) {
if (!rhs_ || !lhs_) {
throw std::runtime_error("null operands are not supported"s);
}
auto l_obj = lhs_->Execute(closure, context);
auto r_obj = rhs_->Execute(closure, context);
if (runtime::IsTrue(l_obj) && runtime::IsTrue(r_obj)) {
return ObjectHolder::Own(runtime::Bool{ true });
}
return ObjectHolder::Own(runtime::Bool{ false });
}
ObjectHolder Not::Execute(Closure& closure, Context& context) {
if (!argument_) {
throw std::runtime_error("null operands are not supported"s);
}
auto obj = argument_->Execute(closure, context);
auto res = runtime::IsTrue(obj);
return ObjectHolder::Own(runtime::Bool{ !res });
}
Comparison::Comparison(Comparator cmp, unique_ptr<Statement> lhs, unique_ptr<Statement> rhs)
: BinaryOperation(std::move(lhs), std::move(rhs))
, cmp_(std::move(cmp)) {
}
ObjectHolder Comparison::Execute(Closure& closure, Context& context) {
if (!rhs_ || !lhs_) {
throw std::runtime_error("null operands are not supported"s);
}
auto l_obj = lhs_->Execute(closure, context);
auto r_obj = rhs_->Execute(closure, context);
bool res = cmp_(l_obj, r_obj, context);
return ObjectHolder::Own(runtime::Bool{ res });
}
IfElse::IfElse(std::unique_ptr<Statement> condition, std::unique_ptr<Statement> if_body,
std::unique_ptr<Statement> else_body)
: condition_(std::move(condition))
, if_body_(std::move(if_body))
, else_body_(std::move(else_body)) {
}
ObjectHolder IfElse::Execute(Closure& closure, Context& context) {
auto bool_condition = condition_->Execute(closure, context);
if (runtime::IsTrue(bool_condition)) {
return if_body_->Execute(closure, context);
} else if (else_body_) { // may be empty !!!
return else_body_->Execute(closure, context);
} else {
return {};
}
}
} // namespace ast