-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathObject.cpp
251 lines (238 loc) · 7 KB
/
Object.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
#include "Object.h"
std::string ObjectStringify(Object *object
#ifndef NDEBUG
,
bool printChunkIfIsFunctionObject
#endif
)
{
switch (object->type)
{
case ObjectType::STR:
return TO_STR_OBJ(object)->value;
case ObjectType::ARRAY:
{
auto arrObj = TO_ARRAY_OBJ(object);
std::string result = "[";
if (arrObj->len != 0)
{
for (int32_t i = 0; i < arrObj->len; ++i)
result += arrObj->elements[i].Stringify() + ",";
result = result.substr(0, result.size() - 1);
}
result += "]";
return result;
}
case ObjectType::STRUCT:
{
auto structObj = TO_STRUCT_OBJ(object);
std::string result = "struct instance(0x" + PointerAddressToString(object) + "):\n{\n";
for (size_t i = 0; i < structObj->members->GetCapacity(); ++i)
{
if (structObj->members->IsValid(i))
{
auto key = structObj->members->GetEntries()[i].key;
auto value = structObj->members->GetEntries()[i].value;
result += ObjectStringify(key) + ":" + value.Stringify() + "\n";
}
}
result = result.substr(0, result.size() - 1);
result += "\n}\n";
return result;
}
case ObjectType::REF:
return TO_REF_OBJ(object)->pointer->Stringify();
case ObjectType::FUNCTION:
{
std::string result = "function(0x" + PointerAddressToString(object) + ")";
#ifndef NDEBUG
if (printChunkIfIsFunctionObject)
{
result += ":\n";
result += TO_FUNCTION_OBJ(object)->chunk.Stringify();
}
#endif
return result;
}
case ObjectType::BUILTIN:
{
std::string vStr;
if (TO_BUILTIN_OBJ(object)->Is<BuiltinFn>())
vStr = "(0x" + PointerAddressToString(object) + ")";
else if (TO_BUILTIN_OBJ(object)->Is<NativeData>())
vStr = "(0x" + PointerAddressToString(TO_BUILTIN_OBJ(object)->Get<NativeData>().nativeData) + ")";
else
vStr = TO_BUILTIN_OBJ(object)->Get<Value>().Stringify();
return "Builtin :" + vStr;
}
default:
ASSERT("Unknown object type");
}
}
void ObjectMark(Object *object)
{
object->marked = true;
switch (object->type)
{
case ObjectType::ARRAY:
{
for (int32_t i = 0; i < TO_ARRAY_OBJ(object)->len; ++i)
TO_ARRAY_OBJ(object)->elements[i].Mark();
break;
}
case ObjectType::STRUCT:
{
TO_STRUCT_OBJ(object)->members->Mark();
break;
}
case ObjectType::REF:
{
TO_REF_OBJ(object)->pointer->Mark();
break;
}
case ObjectType::FUNCTION:
{
for (const auto &v : TO_FUNCTION_OBJ(object)->chunk.constants)
v.Mark();
break;
}
case ObjectType::BUILTIN:
{
if (TO_BUILTIN_OBJ(object)->Is<Value>())
TO_BUILTIN_OBJ(object)->Get<Value>().Mark();
break;
}
case ObjectType::STR:
default:
break;
}
}
void ObjectUnMark(Object *object)
{
object->marked = false;
switch (object->type)
{
case ObjectType::ARRAY:
{
for (int32_t i = 0; i < TO_ARRAY_OBJ(object)->len; ++i)
TO_ARRAY_OBJ(object)->elements[i].UnMark();
break;
}
case ObjectType::STRUCT:
{
TO_STRUCT_OBJ(object)->members->UnMark();
break;
}
case ObjectType::REF:
{
TO_REF_OBJ(object)->pointer->UnMark();
break;
}
case ObjectType::FUNCTION:
{
for (const auto &v : TO_FUNCTION_OBJ(object)->chunk.constants)
v.UnMark();
break;
}
case ObjectType::BUILTIN:
{
if (TO_BUILTIN_OBJ(object)->Is<Value>())
TO_BUILTIN_OBJ(object)->Get<Value>().UnMark();
break;
}
case ObjectType::STR:
default:
break;
}
}
bool IsObjectEqual(Object *left, Object *right)
{
if (left->type != right->type)
return false;
switch (left->type)
{
case ObjectType::STR:
return TO_STR_OBJ(left)->hash == TO_STR_OBJ(right)->hash;
case ObjectType::ARRAY:
{
if (TO_ARRAY_OBJ(left)->len != TO_ARRAY_OBJ(right)->len)
return false;
for (size_t i = 0; i < TO_ARRAY_OBJ(left)->len; ++i)
if (TO_ARRAY_OBJ(left)->elements[i] != TO_ARRAY_OBJ(right)->elements[i])
return false;
return true;
}
case ObjectType::STRUCT:
return TO_STRUCT_OBJ(left)->members == TO_STRUCT_OBJ(right)->members;
case ObjectType::REF:
return *TO_REF_OBJ(left)->pointer == *TO_REF_OBJ(right)->pointer;
case ObjectType::FUNCTION:
{
if (TO_FUNCTION_OBJ(left)->chunk.opCodes.size() != TO_FUNCTION_OBJ(right)->chunk.opCodes.size())
return false;
if (TO_FUNCTION_OBJ(left)->parameterCount != TO_FUNCTION_OBJ(right)->parameterCount)
return false;
if (TO_FUNCTION_OBJ(left)->localVarCount != TO_FUNCTION_OBJ(right)->localVarCount)
return false;
for (int32_t i = 0; i < TO_FUNCTION_OBJ(left)->chunk.opCodes.size(); ++i)
if (TO_FUNCTION_OBJ(left)->chunk.opCodes[i] != TO_FUNCTION_OBJ(right)->chunk.opCodes[i])
return false;
return true;
}
case ObjectType::BUILTIN:
{
if (TO_BUILTIN_OBJ(left)->Is<NativeData>())
{
auto thisNd = TO_BUILTIN_OBJ(left)->Get<NativeData>();
auto otherNd = TO_BUILTIN_OBJ(right)->Get<NativeData>();
return PointerAddressToString(thisNd.nativeData) == PointerAddressToString(otherNd.nativeData);
}
else
return TO_BUILTIN_OBJ(left)->data.index() == TO_BUILTIN_OBJ(right)->data.index();
}
default:
ASSERT("Unknown object type");
return false;
}
}
StrObject *StrAdd(StrObject *left, StrObject *right)
{
size_t length = left->len + right->len;
char *newStr = new char[length + 1];
memcpy(newStr, left->value, left->len);
memcpy(newStr + left->len, right->value, right->len);
newStr[length] = '\0';
return new StrObject(newStr);
}
void StrInsert(StrObject *left, uint32_t idx, StrObject *right)
{
size_t length = left->len + right->len;
char *newStr = new char[length + 1];
memset(newStr, '\0', length + 1);
strncpy(newStr, left->value, idx);
newStr = strcat(newStr, right->value);
newStr = strcat(newStr, left->value + idx);
left->value = newStr;
left->len = length;
}
void StrErase(StrObject *left, uint32_t idx)
{
int32_t i = 0, j = 0;
for (; left->value[i] != '\0'; ++i)
if (i != idx)
left->value[j++] = left->value[i];
left->value[j] = '\0';
left->len--;
}
void ArrayInsert(ArrayObject *left, uint32_t idx, const Value &element)
{
//TODO:not implement yet
}
void ArrayErase(ArrayObject *left, uint32_t idx)
{
int32_t i = 0, j = 0;
for (; i < left->len; ++i)
if (i != idx)
left->elements[j++] = left->elements[i];
left->len--;
}