-
Notifications
You must be signed in to change notification settings - Fork 0
/
statement.cpp
353 lines (280 loc) · 12.1 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
#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
ObjectThrow::ObjectThrow(runtime::ObjectHolder obj)
: obj_(obj) {
}
runtime::ObjectHolder ObjectThrow::Get(){
return obj_;
}
ObjectHolder Assignment::Execute(Closure& closure, Context& context) {
closure[var_] = rv_.get()->Execute(closure, context);
return closure[var_];
}
Assignment::Assignment(std::string var, std::shared_ptr<Statement> rv)
: var_(var), rv_(rv) {
}
VariableValue::VariableValue(const std::string& var_name){
dotted_ids_.push_back(var_name);
}
VariableValue::VariableValue(std::vector<std::string> dotted_ids)
: dotted_ids_(dotted_ids) {
}
ObjectHolder VariableValue::Execute(runtime::Closure& closure, [[maybe_unused]]runtime::Context& context) {
runtime::Closure *closure_ = &closure;
runtime::ClassInstance *obj;
for (size_t i = 0; i < dotted_ids_.size() - 1; ++i){
if (!closure_->count(dotted_ids_.at(i))){
if (dotted_ids_.at(i) == "self"s){
continue;
}
throw std::runtime_error(dotted_ids_.at(i) + ": unknown variable"s);
}
if ((obj = (*closure_)[dotted_ids_.at(i)].TryAs<runtime::ClassInstance>()) == nullptr){
throw std::runtime_error("Undefined class field"s);
}
closure_ = &(obj->Fields());
}
if (!closure_->count(dotted_ids_.back())){
throw std::runtime_error("Unknown variable"s);
}
return (*closure_)[dotted_ids_.back()];
}
unique_ptr<Print> Print::Variable(const std::string& name){
return std::make_unique<Print>(name);
}
Print::Print(std::unique_ptr<Statement> argument)
: argument_(std::move(argument)) {
}
Print::Print(std::vector<std::unique_ptr<Statement>> args){
for (size_t i = 0; i < args.size(); ++i){
args_.push_back(std::move(args.at(i)));
}
}
ObjectHolder Print::Execute(runtime::Closure& closure, runtime::Context& context) {
if (!args_.size()){
if (closure.count(name_)){
closure.at(name_).Get()->Print(context.GetOutputStream(), context);
context.GetOutputStream() << std::endl;
return closure.at(name_);
} else {
context.GetOutputStream() << "\n";
return {};
}
}
for (size_t i=0; i<args_.size(); ++i){
runtime::ObjectHolder obj = args_.at(i).get()->Execute(closure, context);
if (obj.Get()){
obj.Get()->Print(context.GetOutputStream(), context);
} else {
context.GetOutputStream() << "None";
}
if (i != args_.size() - 1){
context.GetOutputStream() << " ";
} else {
context.GetOutputStream() << std::endl;
}
}
return runtime::ObjectHolder::None();
}
MethodCall::MethodCall(std::unique_ptr<Statement> object, std::string method,
std::vector<std::unique_ptr<Statement>> args)
: object_(std::move(object)), method_(method) {
args_.reserve(args.size());
for (size_t i = 0; i < args.size(); ++i){
args_.push_back(std::move(args.at(i)));
}
}
ObjectHolder MethodCall::Execute(runtime::Closure& closure, runtime::Context& context) {
runtime::ObjectHolder obj = object_.get()->Execute(closure, context);
if (runtime::ClassInstance* instance = obj.TryAs<runtime::ClassInstance>();
instance && instance->HasMethod(method_, args_.size())){
std::vector<runtime::ObjectHolder> args_to_pass;
args_to_pass.reserve(args_.size());
for (size_t i = 0; i < args_.size(); ++i){
args_to_pass.push_back(args_.at(i).get()->Execute(closure, context));
}
return instance->Call(method_, args_to_pass, context);
}
return {};
}
ObjectHolder Stringify::Execute(runtime::Closure& closure, runtime::Context& context) {
runtime::ObjectHolder obj = statement_.get()->Execute(closure, context);
if (obj.Get()){
std::ostringstream str;
obj.Get()->Print(str, context);
return runtime::ObjectHolder::Own(runtime::ValueObject<std::string>(str.str()));
}
return runtime::ObjectHolder::Own(runtime::ValueObject<std::string>("None"s));
}
ObjectHolder Add::Execute(runtime::Closure& closure, runtime::Context& context) {
runtime::ObjectHolder lhs = lhs_.get()->Execute(closure, context);
runtime::ObjectHolder rhs = rhs_.get()->Execute(closure, context);
if (runtime::Number *left = lhs.TryAs<runtime::Number>(),
*right = rhs.TryAs<runtime::Number>();
left && right){
return runtime::ObjectHolder::Own<runtime::Number>(left->GetValue() + right->GetValue());
} else if (runtime::String *left = lhs.TryAs<runtime::String>(),
*right = rhs.TryAs<runtime::String>();
left && right){
return runtime::ObjectHolder::Own<runtime::String>(left->GetValue() + right->GetValue());
} else if (runtime::ClassInstance* instance = lhs.TryAs<runtime::ClassInstance>();
instance && instance->HasMethod(ADD_METHOD, 1)){
return instance->Call(ADD_METHOD, {rhs}, context);
}
throw std::runtime_error("Unable to add objects"s);
}
ObjectHolder Sub::Execute(runtime::Closure& closure, runtime::Context& context) {
runtime::ObjectHolder lhs = lhs_.get()->Execute(closure, context);
runtime::ObjectHolder rhs = rhs_.get()->Execute(closure, context);
if (runtime::Number *left = lhs.TryAs<runtime::Number>(),
*right = rhs.TryAs<runtime::Number>();
left && right){
return runtime::ObjectHolder::Own<runtime::Number>(left->GetValue() - right->GetValue());
}
throw std::runtime_error("Unable to subtract numbers"s);
}
ObjectHolder Mult::Execute(runtime::Closure& closure, runtime::Context& context) {
runtime::ObjectHolder lhs = lhs_.get()->Execute(closure, context);
runtime::ObjectHolder rhs = rhs_.get()->Execute(closure, context);
if (runtime::Number *left = lhs.TryAs<runtime::Number>(),
*right = rhs.TryAs<runtime::Number>();
left && right){
return runtime::ObjectHolder::Own<runtime::Number>(left->GetValue() * right->GetValue());
}
throw std::runtime_error("Unable to multiplicate numbers"s);
}
ObjectHolder Div::Execute(runtime::Closure& closure, runtime::Context& context) {
runtime::ObjectHolder lhs = lhs_.get()->Execute(closure, context);
runtime::ObjectHolder rhs = rhs_.get()->Execute(closure, context);
if (runtime::Number *left = lhs.TryAs<runtime::Number>(),
*right = rhs.TryAs<runtime::Number>();
left && right){
return runtime::ObjectHolder::Own<runtime::Number>(left->GetValue() / right->GetValue());
}
throw std::runtime_error("Unable to divide numbers"s);
}
void Compound::AddStatement(std::unique_ptr<Statement> stmt) {
args_.push_back(std::move(stmt));
}
ObjectHolder Compound::Execute(runtime::Closure& closure, runtime::Context& context) {
for (size_t i = 0; i < args_.size(); ++i){
args_.at(i).get()->Execute(closure, context);
}
return {};
}
ObjectHolder Return::Execute(runtime::Closure& closure, runtime::Context& context) {
throw ObjectThrow(statement_.get()->Execute(closure, context));
}
ClassDefinition::ClassDefinition(runtime::ObjectHolder cls)
: cls_(cls) {
}
ObjectHolder ClassDefinition::Execute(runtime::Closure& closure, [[maybe_unused]] runtime::Context& context) {
std::string class_name = cls_.TryAs<runtime::Class>()->GetName();
closure[class_name] = cls_;
return closure[class_name];
}
FieldAssignment::FieldAssignment(VariableValue object, std::string field_name, std::shared_ptr<Statement> rv)
: object_(object), field_name_(field_name), rv_(rv) {
}
ObjectHolder FieldAssignment::Execute(runtime::Closure& closure, runtime::Context& context) {
if (runtime::ClassInstance* instance = object_.Execute(closure, context).TryAs<runtime::ClassInstance>(); instance){
runtime::Closure& fields = instance->Fields();
fields[field_name_] = rv_.get()->Execute(closure, context);
return fields[field_name_];
}
return closure[field_name_];
}
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(runtime::Closure& closure, runtime::Context& context) {
std::ostringstream out;
condition_.get()->Execute(closure, context).Get()->Print(out, context);
if (out.str() == "True"s || out.str() == "1"s){
return if_body_.get()->Execute(closure, context);
} else if (else_body_.get()){
return else_body_.get()->Execute(closure, context);
} else {
return {};
}
}
ObjectHolder Or::Execute(runtime::Closure& closure, runtime::Context& context) {
runtime::ObjectHolder lhs = lhs_.get()->Execute(closure, context);
runtime::ObjectHolder rhs = rhs_.get()->Execute(closure, context);
if (runtime::Bool *left = lhs.TryAs<runtime::Bool>(); left && left->GetValue()){
return runtime::ObjectHolder::Own<runtime::Bool>(true);
} else if (runtime::Bool *right = rhs.TryAs<runtime::Bool>(); right && right->GetValue()){
return runtime::ObjectHolder::Own<runtime::Bool>(true);
}
return runtime::ObjectHolder::Own<runtime::Bool>(false);
}
ObjectHolder And::Execute(runtime::Closure& closure, runtime::Context& context) {
runtime::ObjectHolder lhs = lhs_.get()->Execute(closure, context);
runtime::ObjectHolder rhs = rhs_.get()->Execute(closure, context);
if (runtime::Bool *left = lhs.TryAs<runtime::Bool>();
!left || !left->GetValue()){
return runtime::ObjectHolder::Own<runtime::Bool>(false);
} else if (runtime::Bool *right = rhs.TryAs<runtime::Bool>();
right && right->GetValue()){
return runtime::ObjectHolder::Own<runtime::Bool>(true);
}
return runtime::ObjectHolder::Own<runtime::Bool>(false);
}
ObjectHolder Not::Execute(runtime::Closure& closure, runtime::Context& context) {
return runtime::ObjectHolder::Own<runtime::Bool>(!(statement_.get()->Execute(closure, context).TryAs<runtime::Bool>()->GetValue()));
}
Comparison::Comparison(Comparator cmp, std::unique_ptr<Statement> lhs, std::unique_ptr<Statement> rhs)
: BinaryOperation(std::move(lhs), std::move(rhs)), cmp_(cmp) {
}
ObjectHolder Comparison::Execute(runtime::Closure& closure, runtime::Context& context) {
bool res = cmp_(lhs_.get()->Execute(closure, context),
rhs_.get()->Execute(closure, context),
context);
return runtime::ObjectHolder::Own(runtime::Bool(res));
}
NewInstance::NewInstance(const runtime::Class& class_){
obj_ = runtime::ObjectHolder::Own(runtime::ClassInstance(class_));
}
NewInstance::NewInstance(const runtime::Class& class_, std::vector<std::unique_ptr<Statement>> args){
obj_ = runtime::ObjectHolder::Own(runtime::ClassInstance(class_));
args_.reserve(args.size());
for (size_t i = 0; i < args.size(); ++i){
args_.push_back(std::move(args.at(i)));
}
}
ObjectHolder NewInstance::Execute([[maybe_unused]] runtime::Closure& closure, [[maybe_unused]] runtime::Context& context) {
if (runtime::ClassInstance* instance = obj_.TryAs<runtime::ClassInstance>();
instance && instance->HasMethod(INIT_METHOD, args_.size())){
std::vector<runtime::ObjectHolder> args_to_pass;
args_to_pass.reserve(args_.size());
for (size_t i = 0; i < args_.size(); ++i){
args_to_pass.push_back(args_.at(i).get()->Execute(closure, context));
}
instance->Call(INIT_METHOD, args_to_pass, context);
}
return obj_;
}
MethodBody::MethodBody(std::unique_ptr<Statement>&& body)
: body_(std::move(body)) {
}
ObjectHolder MethodBody::Execute(runtime::Closure& closure, runtime::Context& context) {
try{
body_.get()->Execute(closure, context);
return {};
} catch (ObjectThrow& err) {
return err.Get();
}
}
} // namespace ast