-
Notifications
You must be signed in to change notification settings - Fork 2
/
bytecc_value.cc
406 lines (336 loc) · 10.4 KB
/
bytecc_value.cc
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
// Copyright (c) 2019 ASMlover. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list ofconditions and the following disclaimer.
//
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in
// the documentation and/or other materialsprovided with the
// distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
// COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
#include <cstring>
#include "bytecc_chunk.hh"
#include "bytecc_vm.hh"
#include "bytecc_value.hh"
namespace loxcc::bytecc {
template <typename T, typename... Args>
inline T* make_object(VM& vm, Args&&... args) {
auto* o = new T(std::forward<Args>(args)...);
vm.append_object(o);
return o;
}
StringObject* TagValue::as_string(void) const {
return Xt::down<StringObject>(as_object());
}
const char* TagValue::as_cstring(void) const {
return Xt::down<StringObject>(as_object())->cstr();
}
NativeObject* TagValue::as_native(void) const {
return Xt::down<NativeObject>(as_object());
}
FunctionObject* TagValue::as_function(void) const {
return Xt::down<FunctionObject>(as_object());
}
UpvalueObject* TagValue::as_upvalue(void) const {
return Xt::down<UpvalueObject>(as_object());
}
ClosureObject* TagValue::as_closure(void) const {
return Xt::down<ClosureObject>(as_object());
}
ClassObject* TagValue::as_class(void) const {
return Xt::down<ClassObject>(as_object());
}
InstanceObject* TagValue::as_instance(void) const {
return Xt::down<InstanceObject>(as_object());
}
BoundMehtodObject* TagValue::as_bound_method(void) const {
return Xt::down<BoundMehtodObject>(as_object());
}
bool TagValue::is_truthy(void) const {
if (is_nil())
return false;
else if (is_boolean())
return as_boolean();
else if (is_numeric())
return as_numeric() != 0.f;
else if (is_object())
return as_object()->is_truthy();
return false;
}
str_t TagValue::stringify(void) const {
if (is_nil())
return "nil";
else if (is_boolean())
return as_boolean() ? "true" : "false";
else if (is_numeric())
return Xt::to_string(as_numeric());
else if (is_object())
return as_object()->stringify();
return "";
}
StringObject* ObjValue::as_string(void) const {
return Xt::down<StringObject>(as_.object);
}
const char* ObjValue::as_cstring(void) const {
return Xt::down<StringObject>(as_.object)->cstr();
}
NativeObject* ObjValue::as_native(void) const {
return Xt::down<NativeObject>(as_.object);
}
FunctionObject* ObjValue::as_function(void) const {
return Xt::down<FunctionObject>(as_.object);
}
UpvalueObject* ObjValue::as_upvalue(void) const {
return Xt::down<UpvalueObject>(as_.object);
}
ClosureObject* ObjValue::as_closure(void) const {
return Xt::down<ClosureObject>(as_.object);
}
ClassObject* ObjValue::as_class(void) const {
return Xt::down<ClassObject>(as_.object);
}
InstanceObject* ObjValue::as_instance(void) const {
return Xt::down<InstanceObject>(as_.object);
}
BoundMehtodObject* ObjValue::as_bound_method(void) const {
return Xt::down<BoundMehtodObject>(as_.object);
}
bool ObjValue::operator==(const ObjValue& r) const {
if (this == &r)
return true;
if (type_ != r.type_)
return false;
switch (type_) {
case ValueType::NIL: return true;
case ValueType::BOOLEAN: return as_.boolean == r.as_.boolean;
case ValueType::NUMERIC: return as_.numeric == r.as_.numeric;
case ValueType::OBJECT: return as_.object == r.as_.object; // TODO: FIXME:
}
return false;
}
bool ObjValue::operator!=(const ObjValue& r) const {
return !(*this == r);
}
bool ObjValue::is_truthy(void) const {
switch (type_) {
case ValueType::NIL: return false;
case ValueType::BOOLEAN: return as_.boolean;
case ValueType::NUMERIC: return as_.numeric == 0.f;
case ValueType::OBJECT: return as_.object->is_truthy();
}
return false;
}
str_t ObjValue::stringify(void) const {
switch (type_) {
case ValueType::NIL: return "nil";
case ValueType::BOOLEAN: return as_.boolean ? "true" : "false";
case ValueType::NUMERIC: return Xt::to_string(as_.numeric);
case ValueType::OBJECT: return as_.object->stringify();
}
return "";
}
StringObject::StringObject(
const char* s, int n, u32_t h, bool replace_owner) noexcept
: BaseObject(ObjType::STRING)
, size_(n)
, hash_(h) {
if (replace_owner) {
data_ = Xt::as_ptr<char>(s);
}
else {
data_ = new char[Xt::as_type<sz_t>(size_) + 1];
memcpy(data_, s, size_);
data_[size_] = 0;
}
}
StringObject::~StringObject(void) {
delete [] data_;
}
str_t StringObject::stringify(void) const {
return data_;
}
StringObject* StringObject::create(VM& vm, const str_t& s) {
return create(vm, s.c_str(), Xt::as_type<int>(s.size()));
}
StringObject* StringObject::create(VM& vm, const char* s, int n) {
u32_t h = Xt::hasher(s, n);
if (auto* o = vm.get_interned(h); o != nullptr)
return o;
auto* o = make_object<StringObject>(vm, s, n, h);
vm.set_interned(h, o);
return o;
}
StringObject* StringObject::concat(VM& vm, StringObject* a, StringObject* b) {
int n = a->size() + b->size();
char* s = new char[Xt::as_type<sz_t>(n) + 1];
memcpy(s, a->data(), a->size());
memcpy(s + a->size(), b->data(), b->size());
s[n] = 0;
u32_t h = Xt::hasher(s, n);
if (auto* o = vm.get_interned(h); o != nullptr) {
delete [] s;
return o;
}
auto* o = make_object<StringObject>(vm, s, n, h, true);
vm.set_interned(h, o);
return o;
}
NativeObject::NativeObject(const NativeFn& fn) noexcept
: BaseObject(ObjType::NATIVE)
, fn_(fn) {
}
NativeObject::NativeObject(NativeFn&& fn) noexcept
: BaseObject(ObjType::NATIVE)
, fn_(std::move(fn)) {
}
str_t NativeObject::stringify(void) const {
std::stringstream ss;
ss << "<native function at `" << this << "`>";
return ss.str();
}
NativeObject* NativeObject::create(VM& vm, const NativeFn& fn) {
return make_object<NativeObject>(vm, fn);
}
NativeObject* NativeObject::create(VM& vm, NativeFn&& fn) {
return make_object<NativeObject>(vm, std::move(fn));
}
FunctionObject::FunctionObject(StringObject* name) noexcept
: BaseObject(ObjType::FUNCTION)
, name_(name)
, chunk_(new Chunk()) {
}
FunctionObject::~FunctionObject(void) {
delete chunk_;
}
str_t FunctionObject::stringify(void) const {
std::stringstream ss;
ss << "<function `" << name_astr() << "` at `" << this << "`>";
return ss.str();
}
void FunctionObject::blacken(VM& vm) {
vm.mark_object(name_);
chunk_->iter_constants([&vm](const Value& v) { vm.mark_value(v); });
}
FunctionObject* FunctionObject::create(VM& vm, StringObject* name) {
return make_object<FunctionObject>(vm, name);
}
UpvalueObject::UpvalueObject(Value* value, UpvalueObject* next) noexcept
: BaseObject(ObjType::UPVALUE)
, value_(value)
, next_(next) {
}
str_t UpvalueObject::stringify(void) const {
return "<upvalue>";
}
void UpvalueObject::blacken(VM& vm) {
vm.mark_value(closed_);
}
UpvalueObject* UpvalueObject::create(
VM& vm, Value* value, UpvalueObject* next) {
return make_object<UpvalueObject>(vm, value, next);
}
ClosureObject::ClosureObject(FunctionObject* fn) noexcept
: BaseObject(ObjType::CLOSURE)
, fn_(fn)
, upvalues_count_(fn->upvalues_count()) {
if (upvalues_count_ > 0) {
upvalues_ = new UpvalueObject*[upvalues_count_];
for (int i = 0; i < upvalues_count_; ++i)
upvalues_[i] = nullptr;
}
}
ClosureObject::~ClosureObject(void) {
if (upvalues_ != nullptr)
delete [] upvalues_;
}
str_t ClosureObject::stringify(void) const {
std::stringstream ss;
ss << "<closure function `" << fn_->name_astr() << "` at `" << this << "`>";
return ss.str();
}
void ClosureObject::blacken(VM& vm) {
vm.mark_object(fn_);
for (int i = 0; i < upvalues_count_; ++i)
vm.mark_object(upvalues_[i]);
}
ClosureObject* ClosureObject::create(VM& vm, FunctionObject* fn) {
return make_object<ClosureObject>(vm, fn);
}
ClassObject::ClassObject(StringObject* name) noexcept
: BaseObject(ObjType::CLASS)
, name_(name) {
}
void ClassObject::inherit_from(ClassObject* superclass) {
for (auto& method : superclass->methods_)
methods_[method.first] = method.second;
}
str_t ClassObject::stringify(void) const {
std::stringstream ss;
ss << "<class `" << name_->cstr() << "`>";
return ss.str();
}
void ClassObject::blacken(VM& vm) {
vm.mark_value(name_);
for (auto& m : methods_) {
vm.mark_value(m.second);
}
}
ClassObject* ClassObject::create(VM& vm, StringObject* name) {
return make_object<ClassObject>(vm, name);
}
InstanceObject::InstanceObject(ClassObject* cls) noexcept
: BaseObject(ObjType::INSTANCE)
, cls_(cls) {
}
str_t InstanceObject::stringify(void) const {
std::stringstream ss;
ss << "<`" << cls_->name_astr() << "` object at `" << this << "`>";
return ss.str();
}
void InstanceObject::blacken(VM& vm) {
vm.mark_object(cls_);
for (auto& attr : attrs_)
vm.mark_value(attr.second);
}
InstanceObject* InstanceObject::create(VM& vm, ClassObject* cls) {
return make_object<InstanceObject>(vm, cls);
}
BoundMehtodObject::BoundMehtodObject(
const Value& owner, ClosureObject* method) noexcept
: BaseObject(ObjType::BOUND_METHOD)
, owner_(owner)
, method_(method) {
}
str_t BoundMehtodObject::stringify(void) const {
std::stringstream ss;
InstanceObject* inst = owner_.as_instance();
ss << "<bound method "
<< "`" << method_->fn()->name_astr() << "` of "
<< inst->stringify() << ">";
return ss.str();
}
void BoundMehtodObject::blacken(VM& vm) {
vm.mark_value(owner_);
vm.mark_object(method_);
}
BoundMehtodObject* BoundMehtodObject::create(
VM& vm, const Value& owner, ClosureObject* method) {
return make_object<BoundMehtodObject>(vm, owner, method);
}
}