-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobject.cpp
More file actions
86 lines (67 loc) · 2.52 KB
/
Copy pathobject.cpp
File metadata and controls
86 lines (67 loc) · 2.52 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
#include "object.h"
#include "statement.h"
#include <sstream>
#include <string_view>
using namespace std;
namespace Runtime {
void PrintClosure(const Closure& closure) {
for (const auto& item : closure) {
std::cout << item.first << std::endl;
}
std::cout << "------------------------" << std::endl;
}
void ClassInstance::Print(std::ostream& os) {
if (!HasMethod("__str__", 0)) {
os << this;
}
else {
Call("__str__", {}).Get()->Print(os);
}
}
bool ClassInstance::HasMethod(const std::string& method, size_t argument_count) const {
bool result = false;
for (const auto& class_method : _class_.class_methods) {
if (class_method.name == method && class_method.formal_params.size() == argument_count) {
result = true;
}
}
return result;
}
const Closure& ClassInstance::Fields() const { return fields; }
Closure& ClassInstance::Fields() { return fields; }
ClassInstance::ClassInstance(const Class& cls) : _class_(cls) {
fields["self"] = ObjectHolder::Share(*this);
}
ClassInstance::ClassInstance (ClassInstance&& other) : fields(std::move(other.fields)), _class_(std::move(other._class_)) {
fields["self"] = ObjectHolder::Share(*this);
}
ObjectHolder ClassInstance::Call(const std::string& method, const std::vector<ObjectHolder>& actual_args) {
const Runtime::Method * method_of_class = _class_.GetMethod(method);
if (method_of_class->formal_params.size() != actual_args.size()) {
throw std::runtime_error("not all arguments provided");
}
for (size_t i = 0; i < method_of_class->formal_params.size(); ++i){
fields[method_of_class->formal_params[i]] = actual_args[i];
}
return method_of_class->body.get()->Execute(fields);
}
Class::Class(std::string name, std::vector<Method> methods, const Class* parent) :
class_name(name), class_methods(std::move(methods)), class_parent(parent) {}
const Method* Class::GetMethod(const std::string& name) const {
for (const Method& method : class_methods) {
if (method.name == name) {
return &method;
}
}
if (class_parent != nullptr) {
return class_parent->GetMethod(name);
}
return nullptr;
}
void Class::Print(ostream& os) { os << GetName(); }
const std::string& Class::GetName() const { return class_name; }
void Bool::Print(std::ostream& os) {
if (GetValue()) { os << "True"; }
else { os << "False"; }
}
} /* namespace Runtime */