-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypes.cpp
More file actions
261 lines (250 loc) · 7.05 KB
/
types.cpp
File metadata and controls
261 lines (250 loc) · 7.05 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
/*
* types.cpp
*
* Implements the Value, Constant, and Function types used in the interpreter
*/
#include "types.h"
#include "frame.h"
#include "gc/gc.h"
/* Constant */
const string Constant::typeS = "Constant";
/* ValWrapper */
const string ValWrapper::typeS = "ValWrapper";
string ValWrapper::toString() {
throw RuntimeException("can't cast ValWrapper to String");
}
size_t ValWrapper::getSize() {
return sizeof(ValWrapper);
}
bool ValWrapper::equals(Value* other) {
throw RuntimeException("can't call equals on ValWrapper");
}
void ValWrapper::follow(CollectedHeap& heap){
// mark the value this points to
if (ptr && !is_tagged(ptr)) {
heap.markSuccessors(get_collectable(ptr));
}
}
/* Function */
const string Function::typeS = "Function";
string Function::toString() {
throw RuntimeException("can't cast Function to a String (try Closure instead)");
}
bool Function::equals(Value* other) {
throw RuntimeException("can't call equals on a Function (try Closure instead)");
}
void Function::follow(CollectedHeap& heap) {
// follow functions_ and constants_,
for (Function* f : functions_) {
heap.markSuccessors(f);
}
for (tagptr_t c : constants_) {
if (!is_tagged(c)) {
heap.markSuccessors(get_collectable(c));
}
}
}
size_t Function::getSize() {
size_t overhead = sizeof(Function);
size_t funcsSize = getVecSize(functions_);
size_t consSize = getVecSize(constants_);
size_t localsSize = getVecSize(local_vars_);
size_t refsSize = getVecSize(local_reference_vars_);
size_t freeSize = getVecSize(free_vars_);
size_t namesSize = getVecSize(names_);
size_t instrSize = sizeof(instructions) + instructions.capacity()*sizeof(BcInstruction);
return overhead + funcsSize + consSize + localsSize + refsSize + freeSize + namesSize + instrSize;
}
/* None */
const string None::typeS = "None";
string None::toString() {
return "None";
}
bool None::equals(Value* other) {
None* otherV = dynamic_cast<None*>(other);
if (otherV == NULL) {
return false;
}
return true;
}
void None::follow(CollectedHeap& heap) {
// no-op; no pointers
}
size_t None::getSize() {
return sizeof(None);
}
/* Integer */
const string Integer::typeS = "Integer";
string Integer::toString() {
return to_string(value);
}
bool Integer::equals(Value* other) {
auto otherV = dynamic_cast<Integer*>(other);
if (otherV == NULL) {
return false;
}
return this->value == otherV->value;
}
void Integer::follow(CollectedHeap& heap) {
// no-op: no pointers
}
size_t Integer::getSize() {
return sizeof(Integer);
}
/* String */
const string String::typeS = "String";
string String::toString() {
string replaced(value);
auto pos = replaced.find("\\");
while (pos != string::npos) {
if (replaced.at(pos + 1) == 'n') {
replaced.replace(pos, 2, "\n");
} else if (replaced.at(pos + 1) == 't') {
replaced.replace(pos, 2, "\t");
} else if (replaced.at(pos + 1) == '\\') {
replaced.replace(pos, 2, "\\");
} else if (replaced.at(pos + 1) == '"') {
replaced.replace(pos, 2, "\"");
}
pos = replaced.find("\\", pos + 1);
}
return replaced;
}
bool String::equals(Value* other) {
auto otherV = dynamic_cast<String*>(other);
if (otherV == NULL) {
return false;
}
return this->value.compare(otherV->value) == 0;
}
void String::follow(CollectedHeap& heap) {
// no-op: no pointers
}
size_t String::getSize() {
size_t overhead = sizeof(String);
size_t stringSize = getStringSize(value);
return overhead + stringSize;
}
/* Boolean */
const string Boolean::typeS = "Boolean";
string Boolean::toString() {
return value? "true" : "false";
};
bool Boolean::equals(Value* other) {
auto otherV = dynamic_cast<Boolean*>(other);
if (otherV == NULL) {
return false;
}
return this->value == otherV->value;
}
void Boolean::follow(CollectedHeap& heap) {
// no-op; no pointers
}
size_t Boolean::getSize() {
return sizeof(Boolean);
}
/* Record */
const string Record::typeS = "Record";
string Record::toString() {
string res = "{";
for (auto x: value) {
res += x.first + ":" + ptr_to_str(x.second) + " ";
}
res += "}";
return res;
}
tagptr_t Record::get(string key) {
return value[key];
}
void Record::set(string key, tagptr_t val, CollectedHeap& collector) {
if (value.count(key) == 0) {
collector.increment(sizeof(key) + key.size() + sizeof(val));
}
value[key] = val;
}
bool Record::equals(Value* other) {
auto otherV = dynamic_cast<Record*>(other);
if (otherV == NULL) {
return false;
}
return &value == &otherV->value;
}
void Record::follow(CollectedHeap& heap) {
// point to all the values contained in the record
for (auto it = value.begin(); it != value.end(); it++) {
if (!is_tagged(it->second)) {
heap.markSuccessors(get_collectable(it->second));
}
}
}
size_t Record::getSize() {
size_t overhead = sizeof(Record);
size_t mapSize = getMapSize(value);
return overhead + mapSize;
}
/* Closure */
const string Closure::typeS = "Closure";
string Closure::toString() {
return "FUNCTION";
}
bool Closure::equals(Value* other) {
auto otherV = dynamic_cast<Closure*>(other);
if (otherV == NULL) {
return false;
}
if (func != otherV->func) {
return false;
}
if (refs.size() != otherV->refs.size()) {
return false;
}
for (int i = 0; i < refs.size(); i++) {
if (refs[i] != otherV->refs[i]) {
return false;
}
}
return true;
}
size_t Closure::getSize() {
size_t overhead = sizeof(Closure);
size_t refsSize = getVecSize(refs);
return overhead + refsSize;
}
void Closure::follow(CollectedHeap& heap) {
// follow the refs and the function
for (ValWrapper* v : refs) {
heap.markSuccessors(v);
}
heap.markSuccessors(func);
}
/* Native functions */
tagptr_t PrintNativeFunction::evalNativeFunction(Frame& currentFrame, CollectedHeap& ch) {
string name = currentFrame.getLocalByIndex(0);
auto val = currentFrame.getLocalVar(name);
cout << ptr_to_str(val) << endl;
return ch.allocate<None>();
};
tagptr_t InputNativeFunction::evalNativeFunction(Frame& currentFrame, CollectedHeap& ch) {
string* input = new string();
getline(cin, *input);
return make_ptr(input);
};
tagptr_t IntcastNativeFunction::evalNativeFunction(Frame& currentFrame, CollectedHeap& ch) {
string name = currentFrame.getLocalByIndex(0);
auto val = currentFrame.getLocalVar(name);
if (check_tag(val, INT_TAG)) {
return val;
}
if (check_tag(val, STR_TAG)) {
string* s = get_str(val);
if (*s == "0") {
return make_ptr(0);
}
int result = atoi(s->c_str());
if (result == 0) {
throw IllegalCastException("cannot convert value " + *s + " to IntValue");
}
return make_ptr(result);
}
throw IllegalCastException("expected String for intcast");
};