-
Notifications
You must be signed in to change notification settings - Fork 0
/
class.cpp
140 lines (104 loc) · 3.91 KB
/
class.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
#include "class.h"
#include "object.h"
#include "context.h"
#include "instruction_parser.h"
Class::Class(std::string n) {
name = n;
std::vector<std::string> args;
args.push_back("name");
this->addMethod(new Method("new", "", std::vector<std::string>(), Method::V_PUBLIC, true));
this->addMethod(new Method("sayHello", "return 'Hello ' + name", args, Method::V_PUBLIC, true));
this->addMethod(new Method("hello", "return @foo + @bar", std::vector<std::string>(), Method::V_PUBLIC, false));
this->addMethod(new Method("hi", "return @foo - @bar", std::vector<std::string>(), Method::V_PUBLIC, false));
this->addMethod(new Method("class", "", std::vector<std::string>(), Method::V_PUBLIC, false));
}
std::string Class::getName() {
return name;
}
void Class::addMethod(Method* method) {
methods.push_back(method);
}
bool Class::hasMethod(std::string name) {
for (std::vector<Method*>::iterator it = methods.begin() ; it != methods.end() ; ++it) {
Method* m = *it;
if (m->name == name && m->is_static == false) {
return true;
}
}
return false;
}
bool Class::hasStaticMethod(std::string name) {
for (std::vector<Method*>::iterator it = methods.begin() ; it != methods.end() ; ++it) {
Method* m = *it;
if (m->name == name && m->is_static == true) {
return true;
}
}
return false;
}
Method* Class::getMethod(std::string name) {
for (std::vector<Method*>::iterator it = methods.begin() ; it != methods.end() ; ++it) {
Method* m = *it;
if (m->name == name) {
return m;
}
}
return NULL;
}
std::vector<Method*> Class::getMethods() {
return methods;
}
Value Class::callStatic(std::string method_name, Context* context, std::vector<Value> arguments) {
if (method_name == "new") {
return callNew(context);
}
Method* m = getMethod(method_name);
if (m == NULL) {
throw std::string("Undefined static method "+name+"::"+method_name);
}
InstructionParser* p = new InstructionParser(context);
return p->parse(m->body);
}
Value Class::call(std::string method_name, Object* instance, Context* context, std::vector<Value> arguments) {
Method* m = getMethod(method_name);
if (method_name == "class") {
return Value(this);
}
if (m != NULL) {
InstructionParser* p = new InstructionParser(context);
context->is_object_context = true;
context->current_instance = instance;
// Create narrower scope for method
Scope* childScope = context->scope->child();
context->scope = childScope;
// Inject arguments into child scope
std::vector<Value>::iterator vit = arguments.begin();
for (std::vector<std::string>::iterator nit = m->arguments.begin() ; nit != m->arguments.end() ; ++nit) {
std::string name = *nit;
Value v = *vit;
context->scope->set(name, v);
++vit;
}
// Eval method
Value retValue = p->parse(m->body);
// Return to parent scope
context->scope = context->scope->parent;
context->is_object_context = false;
context->current_instance = NULL;
return retValue;
}
throw std::string("Undefined method "+instance->to_string()+"::"+method_name);
}
Value Class::callNew(Context* context) {
Object* obj = Object::instantiate(this);
obj->addAttribute(new Attribute("foo", new Value(42), Attribute::V_PUBLIC, false));
obj->addAttribute(new Attribute("bar", new Value(6), Attribute::V_PUBLIC, false));
return Value(obj);
}
std::string Class::to_string() const {
return "<Class:" + name + ">";
}
std::ostream& operator<<(std::ostream& s, const Class& c) {
s << c.to_string();
return s;
}