-
Notifications
You must be signed in to change notification settings - Fork 0
/
value.cpp
486 lines (419 loc) · 11.7 KB
/
value.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
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
#include <cstdio>
#include <sstream>
#include <string>
#include "value.hpp"
#include "extend.hpp"
#include "frame.hpp"
// constructors
Value::Value():
type(Value::EMT) {}
Value::Value(const vtype t):
type(t) {}
Value::Value(const vtype t, const std::string v):
type(t), str(new std::string(v)) {}
Value::Value(const char* v):
type(STR), str(new std::string(v)) {}
Value::Value(const std::string& v):
type(STR), str(new std::string(v)) {}
Value::Value(const double v):
type(DEC), dec(v) {}
Value::Value(std::shared_ptr<Value> ref):
type(REF),ref(new std::shared_ptr<Value>(ref)), related(nullptr) {}
Value::Value(const vtype t, const std::shared_ptr<Value>& ref):
type(t), ref(new std::shared_ptr<Value>(ref)), related(nullptr) {}
Value::Value(mpz_class mp_integer):
type(INT), mp_int(new mpz_class(mp_integer)) {}
Value::Value(const std::vector<std::shared_ptr<Value>>& v):
type(ARR), arr(new std::vector<std::shared_ptr<Value>>(v)) {}
Value::Value(const nullptr_t& null):
type(REF), ref(new std::shared_ptr<Value>(nullptr)) {}
Value::Value(const Def& def):
type(DEF), def(new Def(def)) {}
Value::Value(const Namespace& ns):
type(NSP), ns(new Namespace(ns)) {}
Value::Value(const Lambda& lam):
type(LAM), lam(new Lambda(lam)) {}
Value::Value(const Object& obj):
type(OBJ), obj(new Object(obj)) {}
Value::Value(const std::shared_ptr<Value>& ref, const std::shared_ptr<Value>& related):
type(REF), ref(new std::shared_ptr<Value>(ref)), related(new std::shared_ptr<Value>(related)) {}
void Value::erase() {
// dealloc relevant data
switch (type) {
case DEC: case EMT: return;
case STR: case MAC: delete str; return;
case INT: delete mp_int; return;
case REF: delete related;
case IMR: delete ref; return;
case ARR: delete arr; return;
case NSP: ns->clear(); delete ns; return;
case DEF: delete def; return;
case OBJ: delete obj; return;
case LAM: delete lam; return;
default: return;
}
}
Value& Value::set_noerase(const Value& v) {
type = v.type;
switch (type) {
case DEC:
dec = v.dec;
break;
case REF:
related = v.related ? new std::shared_ptr<Value>(*v.related) : nullptr;
case IMR:
ref = new std::shared_ptr<Value>(*v.ref);
break;
case STR: case MAC:
str = new std::string(*v.str);
break;
case INT:
mp_int = new mpz_class(*v.mp_int);
break;
case ARR:
arr = new std::vector<std::shared_ptr<Value>>(*v.arr);
break;
case NSP:
ns = new Namespace(*v.ns);
break;
case DEF:
def = new Def(*v.def);
break;
case OBJ:
obj = new Object(*v.obj);
break;
case LAM:
lam = new Lambda(*v.lam);
break;
case EMT:
break;
}
return *this;
}
inline static size_t countLines(const std::string& s)
{
size_t lc = 0;
for (char c : s)
if (c == '\n')
lc++;
return lc;
}
// equivalent to python repr()
std::string Value::depict() {
if (type == DEC) {
// add .0 for whole numbers
std::ostringstream ss;
ss << dec;
return ss.str();
} else if (type == INT) {
std::ostringstream ss;
ss << *mp_int;
return ss.str();
} else if (type == STR) {
// escape escape sequences
std::string ret;
for (const char c : *str)
if (c == '\\') ret += "\\\\";
else if (c == '\n') ret += "\\n";
else if (c == '\r') ret += "\\r";
else if (c == '\b') ret += "\\b";
else if (c == '\t') ret += "\\t";
else if (c == '\f') ret += "\\f";
else if (c == '\v') ret += "\\v";
else ret += c;
return "\"" + ret + "\"";
} else if (type == EMT) {
return "empty";
} else if (type == MAC) {
const size_t lines = countLines(*str);
return "{" + (lines > 1 ?
std::to_string(lines) + std::string(" lines")
: *str) + " }";
} else if (type == REF || type == IMR) {
Value* v = (Value*) defer();
if (v)
return v->depict();
return "cyclic/null reference";
} else if (type == ARR) {
std::string ret = "(";
for (auto &v : *arr)
ret += v->depict() + ", ";
if (arr->empty()) {
return ret + ')';
} else {
ret[ret.length() - 2] = ')';
return ret.substr(0, ret.length() - 1); // trim trailing space
}
} else if (type == NSP) {
std::string ret = "{\n";
for (const auto& e : *ns)
ret += "\t\"" + e.first + "\"\t" + Value(e.second).depict() + "\n";
ret += "} namespace";
return ret;
} else if (type == DEF) {
if (def->native) {
return "<native> @define";
} else {
std::string ret;
ret += def->_val->depict();
if (def->run)
ret += '@';
ret += "define";
return ret;
}
} else if (type == OBJ) {
std::string ret = "{";
for (const auto& v : obj->members) {
const Value* mem = v.second->defer();
if (mem->type != Value::OBJ)
ret += "\n\tself." + v.first + "\t" + v.second->depict();
else
ret += "\n\tself." + v.first + "\t { ... " + std::to_string(mem->obj->members.size()) + " members } object";
}
if (!obj->members.empty())
ret += '\n';
return ret + "} object";
} else if (type == LAM) {
std::string ret = "{...} (";
if (lam->params.empty())
ret += ' ';
for (const std::string& param : lam->params)
ret += "\"" + param + "\",";
ret.at(ret.length() - 1) = ')';
return ret + " lambda";
}
return "idk";
}
// equivalent to python `str()`
std::string Value::toString() {
if (type == DEC) {
std::ostringstream ss;
ss << dec;
return ss.str();
} else if (type == INT) {
std::ostringstream ss;
ss << *mp_int;
return ss.str();
} else if (type == STR || type == MAC) {
return *str;
} else if (type == EMT) {
return "empty";
} else if (type == REF || type == IMR) {
Value* v = (Value*) defer();
if (v)
return v->toString();
return "cyclic/null reference";
} else if (type == ARR) {
std::string ret = "(";
for (auto &v : *arr)
ret += v->depict() + ", ";
if (arr->empty()) {
return ret + ')';
} else {
ret[ret.length() - 2] = ')';
return ret.substr(0, ret.length() - 1); // trim trailing space
}
} else if (type == NSP) {
std::string ret = "{\n";
for (const auto& e : *ns)
ret += "\t\"" + e.first + "\"\t" + Value(e.second).depict() + "\n";
ret += "} namespace";
return ret;
} else if (type == DEF) {
if (def->native) {
return "<native> @define";
} else {
std::string ret;
ret += def->_val->depict();
if (def->run)
ret += '@';
ret += "define";
return ret;
}
} else if (type == OBJ) {
std::string ret = "{";
for (const auto& v : obj->members) {
const Value* mem = v.second->defer();
if (mem->type != Value::OBJ)
ret += "\n\tself." + v.first + "\t" + v.second->depict();
else
ret += "\n\tself." + v.first + "\t { ... " + std::to_string(mem->obj->members.size()) + " members } object";
}
if (!obj->members.empty())
ret += '\n';
return ret + "} object";
} else if (type == LAM) {
std::string ret = "{...} (";
if (lam->params.empty())
ret += ' ';
for (const std::string& param : lam->params)
ret += "\"" + param + "\",";
ret.at(ret.length() - 1) = ')';
return ret + " lambda";
}
return "idk";
}
// get ptr to value that a reference points to
const Value* Value::defer(std::vector<std::shared_ptr<Value>*>& pastPtrs) {
// end of ref recursion
if (type != REF && type != IMR)
return this;
if (!ref)
return nullptr;
// if it's been seen before it should be cyclic reference
if (std::find(pastPtrs.begin(), pastPtrs.end(), ref) != pastPtrs.end())
return nullptr;
// follow reference tree
pastPtrs.emplace_back(ref);
if (!ref || !*ref)
return nullptr;
return (*ref)->defer(pastPtrs);
}
const Value* Value::defer(bool& imr, std::vector<std::shared_ptr<Value>*>& pastPtrs) {
// end of ref recursion
if (type != REF && type != IMR)
return this;
if (!ref)
return nullptr;
if (type == IMR)
imr = true;
// if it's been seen before it should be cyclic reference
if (std::find(pastPtrs.begin(), pastPtrs.end(), ref) != pastPtrs.end())
return nullptr;
// follow reference tree
pastPtrs.emplace_back(ref);
if (!ref || !*ref)
return nullptr;
return (*ref)->defer(pastPtrs);
}
// get (copy) the value that a reference points to
bool Value::deferValue(Value& ret, std::vector<std::shared_ptr<Value>*>& pastPtrs) {
// end of ref recursion
if (type != REF && type != IMR) {
ret = *this;
return true;
}
if (!ref || !*ref)
return false;
// if it's been seen before it should be cyclic reference
if (std::find(pastPtrs.begin(), pastPtrs.end(), ref) != pastPtrs.end())
return false;
// follow reference tree
pastPtrs.emplace_back(ref);
if (!ref || !*ref)
return false;
// trace down refs
const bool&& found = (*ref)->deferValue(ret, pastPtrs);
if (found && ret.type == LAM && related)
ret.lam->self = *related;
return found;
}
// get muteable value
// stops at immuteable references
// use this instead of defer if we want to change the value
Value* Value::deferMuteable(std::vector<std::shared_ptr<Value>*>& pastPtrs) {
// end of ref recursion
if (type != REF) // will return on IMR
return this;
if (!ref || !*ref)
return nullptr;
// if it's been seen before it should be a cyclic reference
if (std::find(pastPtrs.begin(), pastPtrs.end(), ref) != pastPtrs.end())
return nullptr;
// follow reference tree
pastPtrs.emplace_back(ref);
if (!ref || !*ref)
return nullptr;
return (*ref)->deferMuteable(pastPtrs);
}
Value* Value::deferChange() {
const Value* v = defer();
Value* mut = deferMuteable();
if (v != mut)
mut->set(*v);
return mut;
}
// last reference before non-reference type
std::shared_ptr<Value> Value::lastRef(std::vector<std::shared_ptr<Value>*>& pastPtrs) {
// end of ref recursion
if (type != REF && type != IMR)
return pastPtrs.empty() ? std::make_shared<Value>(*this) : *pastPtrs.back();
if (!ref || !*ref)
return nullptr;
// if it's been seen before it should be cyclic reference
if (std::find(pastPtrs.begin(), pastPtrs.end(), ref) != pastPtrs.end())
return nullptr;
// follow reference tree
pastPtrs.emplace_back(ref);
if (!ref || !*ref)
return nullptr;
return (*ref)->lastRef(pastPtrs);
}
// name for type
const char* Value::typeName() {
return typeName(type);
}
const char* Value::typeName(const Value::vtype value_type) {
// could be optimized to an array of strings
// and using type as index, but im not certain if i can keep the values in correct order
switch (value_type) {
case EMT: return "empty";
case INT: return "int";
case DEC: return "float";
case REF: return "reference";
case IMR: return "weak_ref";
case STR: return "string";
case MAC: return "macro";
case ARR: return "list";
case OBJ: return "object";
case LAM: return "lambda";
case NSP: return "namespace";
case DEF: return "def"; // how did they do this?
default:
std::cout <<"unknown type#" <<(const long)value_type <<std::endl;
return "unknown";
}
}
bool Value::truthy() {
if (type == REF) {
Value* v = (Value*) defer();
return v && v->truthy();
}
if (type == INT) return *mp_int != 0;
if (type == DEC) return dec != 0;
if (type == EMT) return false;
if (type == STR) return (bool) str->length();
if (type == ARR) return !arr->empty();
if (type == OBJ || type == MAC || type == LAM)
return true;
std::cout <<"invalid type in Value.truthy()\n";
// glitch
return false;
}
// should i?
bool Value::operator==(Value& v) {
if (v.type != type)
return false;
if (type == EMT)
return true;
if (type == INT)
return *v.mp_int == *mp_int;
if (type == DEC)
return v.dec == dec;
if (type == STR || type == MAC)
return *v.str == *str;
// returns if they reference same data,
// use copy operator if u wanna compare by value
// move this to an `is` operator ? not inclined
if (type == REF)
return v.defer() == defer();
if (type == OBJ)
return obj->members == v.obj->members;
if (type == LAM)
return lam->params == v.lam->params && lam->body == v.lam->body;
// obj.__operator== not called here
if (type == OBJ)
return obj->members == v.obj->members;
// for now at least
return false;
}