-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstatement.cpp
More file actions
271 lines (220 loc) · 9.22 KB
/
statement.cpp
File metadata and controls
271 lines (220 loc) · 9.22 KB
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
#include "statement.h"
#include "object.h"
#include <iostream>
#include <sstream>
using namespace std;
namespace Ast {
using Runtime::Closure;
ObjectHolder Assignment::Execute(Closure& closure) {
ObjectHolder statement_result = right_value.get()->Execute(closure);
closure[var_name] = statement_result;
return closure[var_name];
}
Assignment::Assignment(std::string var, std::unique_ptr<Statement> rv) : var_name(var), right_value(std::move(rv)) {
}
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) {
try {
if (dotted_ids.size() == 2) {
return closure.at(dotted_ids[0]).TryAs<Runtime::ClassInstance>()->Fields().at(dotted_ids[1]);
}
return closure.at(dotted_ids[0]);
} catch (std::out_of_range) {
throw std::runtime_error("variable is not defined");
}
}
unique_ptr<Print> Print::Variable(std::string var) {
auto statement = std::make_unique<VariableValue>(var);
return std::make_unique<Print>(std::move(statement));
}
Print::Print(unique_ptr<Statement> argument) { args.push_back(std::move(argument)); }
Print::Print(vector<unique_ptr<Statement>> args) : args(std::move(args)) {}
ObjectHolder Print::Execute(Closure& closure) {
if (args.size() == 1) {
auto object = args[0].get()->Execute(closure);
if (object.Get() == nullptr) { *output << "None";}
else { object.Get()->Print(*output); }
} else {
for (size_t i = 0; i < args.size(); ++i) {
auto object = args[i].get()->Execute(closure);
if (object.Get() == nullptr) { *output << "None"; }
else { object.Get()->Print(*output); }
if (i != args.size()-1) { *output << ' '; }
}
}
*output << '\n';
return ObjectHolder();
}
ostream* Print::output = &cout;
void Print::SetOutputStream(ostream& output_stream) {
output = &output_stream;
}
MethodCall::MethodCall(
std::unique_ptr<Statement> object, std::string method, std::vector<std::unique_ptr<Statement>> args) :
object(std::move(object)), method(std::move(method)), args(std::move(args)) {}
ObjectHolder MethodCall::Execute(Closure& closure) {
auto instance = object.get()->Execute(closure).TryAs<Runtime::ClassInstance>();
std::vector<ObjectHolder> actual_args;
for (const auto& statement : args) {
actual_args.push_back(statement.get()->Execute(closure));
}
return instance->Call(method, actual_args);
}
ObjectHolder Stringify::Execute(Closure& closure) {
auto object = argument.get()->Execute(closure);
std::ostringstream ss;
object.Get()->Print(ss);
return ObjectHolder::Own(Runtime::String{ss.str()});
}
ObjectHolder Add::Execute(Closure& closure) {
ObjectHolder lhs_res = lhs.get()->Execute(closure);
ObjectHolder rhs_res = rhs.get()->Execute(closure);
auto lhs_obj = lhs_res.TryAs<Runtime::ClassInstance>();
if (lhs_obj && lhs_obj->HasMethod("__add__", 1)) {
ObjectHolder var = rhs.get()->Execute(lhs_obj->Fields());
return lhs_obj->Call("__add__", {var});
}
Runtime::Number * lhs_num = lhs_res.TryAs<Runtime::Number>();
Runtime::Number * rhs_num = rhs_res.TryAs<Runtime::Number>();
if (lhs_num && rhs_num) {
auto new_value = lhs_num->GetValue() + rhs_num->GetValue();
return ObjectHolder::Own(Runtime::Number{new_value});
}
Runtime::String * lhs_str = lhs_res.TryAs<Runtime::String>();
Runtime::String * rhs_str = rhs_res.TryAs<Runtime::String>();
if (lhs_str && rhs_str) {
auto new_value = lhs_str->GetValue() + rhs_str->GetValue();
return ObjectHolder::Own(Runtime::String{new_value});
}
throw std::runtime_error("invalid arguments");
}
ObjectHolder Sub::Execute(Closure& closure) {
ObjectHolder lhs_res = lhs.get()->Execute(closure);
ObjectHolder rhs_res = rhs.get()->Execute(closure);
Runtime::Number * lhs_num = lhs_res.TryAs<Runtime::Number>();
Runtime::Number * rhs_num = rhs_res.TryAs<Runtime::Number>();
if (lhs_num && rhs_num) {
auto new_value = lhs_num->GetValue() - rhs_num->GetValue();
return ObjectHolder::Own(Runtime::Number{new_value});
}
throw std::runtime_error("invalid arguments");
}
ObjectHolder Mult::Execute(Runtime::Closure& closure) {
ObjectHolder lhs_res = lhs.get()->Execute(closure);
ObjectHolder rhs_res = rhs.get()->Execute(closure);
Runtime::Number * lhs_num = lhs_res.TryAs<Runtime::Number>();
Runtime::Number * rhs_num = rhs_res.TryAs<Runtime::Number>();
if (lhs_num && rhs_num) {
auto new_value = lhs_num->GetValue() * rhs_num->GetValue();
return ObjectHolder::Own(Runtime::Number{new_value});
}
throw std::runtime_error("invalid arguments");
}
ObjectHolder Div::Execute(Runtime::Closure& closure) {
ObjectHolder lhs_res = lhs.get()->Execute(closure);
ObjectHolder rhs_res = rhs.get()->Execute(closure);
Runtime::Number * lhs_num = lhs_res.TryAs<Runtime::Number>();
Runtime::Number * rhs_num = rhs_res.TryAs<Runtime::Number>();
if (lhs_num && rhs_num) {
auto new_value = lhs_num->GetValue() / rhs_num->GetValue();
return ObjectHolder::Own(Runtime::Number{new_value});
}
throw std::runtime_error("invalid arguments");
}
ObjectHolder Compound::Execute(Closure& closure) {
// ObjectHolder res;
// std::string class_name;
// for (const auto& statement : statements) {
// res = statement.get()->Execute(closure);
// class_name = typeid(*statement.get()).name();
// if (class_name == "class Ast::IfElse" && res.Get() != nullptr) { return res; }
// if (class_name == "class Ast::Return") { return res; }
// if (class_name == "class Ast::Compound") { return res; }
// }
// return ObjectHolder::None();
for (auto& statement : statements) {
auto ret = statement->Execute(closure);
bool check_for_return = false
|| dynamic_cast<Return*>(statement.get())
|| dynamic_cast<IfElse*>(statement.get())
|| dynamic_cast<Compound*>(statement.get());
if (check_for_return && ret.Get()) {
return ret;
}
}
return ObjectHolder::None();
}
ObjectHolder Return::Execute(Closure& closure) {
return statement.get()->Execute(closure);
}
ClassDefinition::ClassDefinition(ObjectHolder class_) : cls(class_), class_name(cls.TryAs<Runtime::Class>()->GetName()) {}
ObjectHolder ClassDefinition::Execute(Runtime::Closure& closure) {
closure[class_name] = cls;
return closure[class_name];
}
FieldAssignment::FieldAssignment(VariableValue object, std::string field_name, std::unique_ptr<Statement> rv)
: object(object), field_name(std::move(field_name)), right_value(std::move(rv))
{
}
ObjectHolder FieldAssignment::Execute(Runtime::Closure& closure) {
Runtime::Closure& object_fields = object.Execute(closure).TryAs<Runtime::ClassInstance>()->Fields();
ObjectHolder right = right_value.get()->Execute(closure);
object_fields[field_name] = right;
return object_fields[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) {
if (Runtime::IsTrue(condition->Execute(closure))) {
return if_body.get()->Execute(closure);
} else if (else_body) {
return else_body->Execute(closure);
}
return ObjectHolder::None();
}
ObjectHolder Or::Execute(Runtime::Closure& closure) {
return ObjectHolder::Own(
Runtime::Bool(Runtime::IsTrue(lhs->Execute(closure)) || Runtime::IsTrue(rhs->Execute(closure)))
);
}
ObjectHolder And::Execute(Runtime::Closure& closure) {
return ObjectHolder::Own(
Runtime::Bool(Runtime::IsTrue(lhs->Execute(closure)) && Runtime::IsTrue(rhs->Execute(closure)))
);
}
ObjectHolder Not::Execute(Runtime::Closure& closure) {
return ObjectHolder::Own(
Runtime::Bool(!Runtime::IsTrue(argument->Execute(closure)))
);
}
Comparison::Comparison(Comparator cmp, unique_ptr<Statement> lhs, unique_ptr<Statement> rhs
) : left(std::move(lhs)), right(std::move(rhs)), comparator(cmp) {}
ObjectHolder Comparison::Execute(Runtime::Closure& closure) {
ObjectHolder left_value = left.get()->Execute(closure);
ObjectHolder right_value = right.get()->Execute(closure);
bool res = comparator(left_value, right_value);
return ObjectHolder::Own(Runtime::Bool(res));
}
NewInstance::NewInstance(const Runtime::Class& class_, std::vector<std::unique_ptr<Statement>> args
) : _class_(class_), args(std::move(args)) {}
NewInstance::NewInstance(const Runtime::Class& class_) : NewInstance(class_, {}) {
}
ObjectHolder NewInstance::Execute(Runtime::Closure& closure) {
Runtime::ClassInstance new_instance(_class_);
if (new_instance.HasMethod("__init__", args.size())) {
std::vector<ObjectHolder> actual_args;
for (const auto& statement : args) {
actual_args.push_back(statement.get()->Execute(closure));
}
new_instance.Call("__init__", std::move(actual_args));
}
ObjectHolder holder = ObjectHolder::Own(std::move(new_instance));
return holder;
}
} /* namespace Ast */