-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathValue.cpp
More file actions
550 lines (482 loc) · 15.3 KB
/
Copy pathValue.cpp
File metadata and controls
550 lines (482 loc) · 15.3 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
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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "Value.h"
#include "Object.h"
using Type = Value::Type;
bool Value::isUndef() const {
return Value::type == Type::UndefType;
}
bool Value::isNumber() const {
return Value::type == Type::NumberType;
}
bool Value::isBoolean() const {
return Value::type == Type::BooleanType;
}
bool Value::isString() const {
return Value::type == Type::StringType;
}
bool Value::isObject() const {
return Value::type == Type::ObjectType;
}
bool Value::isProgramFunction() const {
return Value::type == Type::ProgramFunctionType;
}
bool Value::isLibraryFunction() const {
return Value::type == Type::LibraryFunctionType;
}
bool Value::isNativePtr() const {
return Value::type == Type::NativePtrType;
}
bool Value::isNil() const {
return Value::type == Type::NilType;
}
void Value::fromNil () {
clear();
type = Type::NilType;
}
void Value::fromDouble (double _numVal) {
clear();
type = Type::NumberType;
data.numVal = _numVal;
}
void Value::fromBool (bool _boolVal) {
clear();
type = Type::BooleanType;
data.boolVal = _boolVal;
}
void Value::fromString (const char *_strVal) {
clear();
type = Type::StringType;
data.strVal = strdup(_strVal);
}
void Value::fromLibraryFunction (LibraryFunction ast, char *name) {
clear();
type = Type::LibraryFunctionType;
data.libFuncVal.ast = ast;
data.libFuncVal.libFuncName = strdup(name);
}
void Value::fromObject (Object* _objectVal) {
clear();
data.objectVal = _objectVal;
_objectVal->incRefCounter();
}
void Value::fromProgramFunction (Object* _funcAst, Object* _funcClosure, const char *name) {
clear();
type = Type::ProgramFunctionType;
data.progFuncVal.ast = _funcAst;
_funcAst->incRefCounter();
data.progFuncVal.closure = _funcClosure;
_funcClosure->incRefCounter();
data.progFuncVal.funcName = strdup(name);
}
void Value::fromNativePtr (void* _ptr, char* _typeId) {
clear();
type = Type::NativePtrType;
data.nativePtrVal.ptr = _ptr;
data.nativePtrVal.typeId = _typeId;
}
Value::Value(Type _type) {
type = _type;
switch(_type){
case(Type::BooleanType):
data.boolVal = false;
break;
case(Type::LibraryFunctionType):
data.libFuncVal.ast = nullptr;
data.libFuncVal.libFuncName = nullptr;
data.libFuncVal.libFuncName = nullptr;
break;
case(Type::NativePtrType):
data.nativePtrVal.ptr = nullptr;
data.nativePtrVal.typeId = nullptr;
break;
case(Type::NumberType):
data.numVal = 0;
break;
case(Type::ObjectType):
data.objectVal = nullptr;
break;
case(Type::ProgramFunctionType):
data.progFuncVal.ast = nullptr;
data.progFuncVal.closure = nullptr;
data.progFuncVal.funcName = nullptr;
break;
case(Type::StringType):
data.strVal = nullptr;
default:
; //ena semicolon kati
}
}
Value::Value(double _numVal) {
type = Type::NumberType;
data.numVal = _numVal;
}
Value::Value(bool _boolVal) {
type = Type::BooleanType;
data.boolVal = _boolVal;
}
Value::Value(const char* _strVal) {
type = Type::StringType;
data.strVal = strdup(_strVal);
}
Value::Value(LibraryFunction libFuncAst, const char *name) {
type = Type::LibraryFunctionType;
data.libFuncVal.ast = libFuncAst;
data.libFuncVal.libFuncName = strdup(name);
}
Value::Value(Object* _objectVal) {
type = Type::ObjectType;
data.objectVal = _objectVal;
_objectVal->incRefCounter();
}
Value::Value(Object* _funcAst, Object* _funcClosure, const char *name) {
type = Type::ProgramFunctionType;
data.progFuncVal.ast = _funcAst;
data.progFuncVal.closure = _funcClosure;
data.progFuncVal.funcName = strdup(name);
_funcAst->incRefCounter();
_funcClosure->incRefCounter();
}
Value::Value(void* _ptr, const char* _typeId) {
type = Type::NativePtrType;
data.nativePtrVal.ptr = _ptr;
data.nativePtrVal.typeId = _typeId;
}
Value::Value(const Value& _v) {
type = _v.getType();
switch(_v.getType()) {
case(Type::StringType):
data.strVal = strdup(_v.data.strVal);
break;
case(Type::LibraryFunctionType):
data.libFuncVal.ast = _v.data.libFuncVal.ast;
data.libFuncVal.libFuncName = strdup(_v.data.libFuncVal.libFuncName);
break;
case(Type::ObjectType):
data.objectVal = _v.data.objectVal;
_v.data.objectVal->incRefCounter();
break;
case(Type::ProgramFunctionType):
data.progFuncVal.ast = _v.data.progFuncVal.ast;
_v.data.progFuncVal.ast->incRefCounter();
data.progFuncVal.closure = _v.data.progFuncVal.closure;
_v.data.progFuncVal.closure->incRefCounter();
data.progFuncVal.funcName = strdup(_v.data.progFuncVal.funcName);
break;
case(Type::NativePtrType):
data.nativePtrVal.ptr = _v.data.nativePtrVal.ptr;
data.nativePtrVal.typeId = _v.data.nativePtrVal.typeId;
break;
case(Type::NumberType):
data.numVal = _v.data.numVal;
break;
case(Type::BooleanType):
data.boolVal = _v.data.boolVal;
default:
;
}
}
Value::Value(Value&& _v){
this->data = _v.data;
this->type = _v.type;
_v.type = UndefType; //cancel the temp value
}
void Value::clear() {
if (isObject()) {
(data.objectVal)->decRefCounter();
if (!(data.objectVal)->getRefCounter())
delete data.objectVal;
}
else if (isProgramFunction()) {
(data.progFuncVal.ast)->decRefCounter();
if (!(data.progFuncVal.ast)->getRefCounter())
delete data.progFuncVal.ast;
(data.progFuncVal.closure)->decRefCounter();
if (!(data.progFuncVal.closure)->getRefCounter())
delete data.progFuncVal.closure;
free(data.progFuncVal.funcName);
}
else if (isLibraryFunction()) {
free(data.libFuncVal.libFuncName);
}
else if (isString()) {
free(data.strVal);
}
type = Type::UndefType;
}
Value::~Value() {
clear();
}
Type Value::getType() const {
return type;
}
double Value::toNumber() const {
assert(type == Type::NumberType);
return data.numVal;
}
bool Value::toBoolean() const {
assert(type == Type::BooleanType);
return data.boolVal;
}
char* Value::toString() const {
assert(type == Type::StringType);
return data.strVal;
}
const Object* Value::toObject() const {
assert(type == ObjectType);
return toObjectNoConst();
}
const Object* Value::toProgramFunctionAST() const {
assert(type == ProgramFunctionType);
return toProgramFunctionASTNoConst();
}
const Object* Value::toProgramFunctionClosure() const {
assert(type == ProgramFunctionType);
return toProgramFunctionClosureNoConst();
}
const char* Value::toProgramFunctionName() const {
assert(type == ProgramFunctionType);
return data.progFuncVal.funcName;
}
Object* Value::toObjectNoConst() const {
assert(type == Type::ObjectType);
return data.objectVal;
}
Object* Value::toProgramFunctionASTNoConst() const {
assert(type == Type::ProgramFunctionType);
return data.progFuncVal.ast;
}
Object* Value::toProgramFunctionClosureNoConst() const {
assert(type == Type::ProgramFunctionType);
return data.progFuncVal.closure;
}
const LibraryFunction Value::toLibraryFunction() const {
assert(type == Type::LibraryFunctionType);
return data.libFuncVal.ast;
}
void* Value::toNativePtr() const {
assert(type == Type::NativePtrType);
return data.nativePtrVal.ptr;
}
const char* Value::toNativeTypeId() const {
assert(type == Type::NativePtrType);
return data.nativePtrVal.typeId;
}
Value* Value::clone() const {
auto tmp = new Value(*this);
return tmp;
}
void Value::swap(Value& _v) {
Type tmp = type;
type = _v.type;
_v.type = tmp;
auto tmp2 = data;
data = _v.data;
_v.data = tmp2;
}
const Value Value::operator=(const Value& _v) {
Value tmp(_v);
swap(tmp);
return *this;
}
Value& Value::operator=(Value&& _v){
if(this != &_v){
clear();
this->data = _v.data;
this->type = _v.type;
_v.type = UndefType;
}
return *this;
}
//NUMERIC OPERATORS
Value Value::operator+(const Value&other) const {
if(isNumber() && other.isNumber())
return Value(this->data.numVal + other.data.numVal);
else if(isNumber() && other.isString()) {
std::string tmp = std::to_string(this->data.numVal);
tmp = tmp + std::string(other.data.strVal);
return Value(tmp.c_str());
}
else if(isString() && other.isNumber()) {
std::string tmp = std::to_string(other.data.numVal);
tmp = std::string(this->data.strVal) + tmp;
return Value(tmp.c_str());
}
else if(isString() && other.isString())
return Value((std::string(data.strVal) + std::string(other.data.strVal)).c_str());
throw std::runtime_error("Invalid operands to \"+\"");
}
Value Value::operator-(const Value&other) const {
if(isNumber() && other.isNumber())
return Value(this->data.numVal - other.data.numVal);
throw std::runtime_error("Invalid operands to \"-\"");
}
Value Value::operator*(const Value&other) const {
if(isNumber() && other.isNumber())
return Value(this->data.numVal * other.data.numVal);
throw std::runtime_error("Invalid operands to \"*\"");
}
Value Value::operator/(const Value&other) const {
if(isNumber() && other.isNumber()){
if(other.data.numVal == 0)
throw std::runtime_error("Division by zero");
return Value(this->data.numVal / other.data.numVal);
}
throw std::runtime_error("Invalid operands to \"/\"");
}
Value Value::operator%(const Value&other) const {
if(isNumber() && other.isNumber()){
if(this->data.numVal != (int)this->data.numVal && other.data.numVal != (int)other.data.numVal)
throw std::runtime_error("Non-integer modulus operands");
if(other.data.numVal == 0)
throw std::runtime_error("Modulus with 0");
return Value( (double)((int)this->data.numVal % (int)other.data.numVal));
}
throw std::runtime_error("Invalid operands to %");
}
//RELATIONAL OPERATORS
Value Value::operator>(const Value& other) const{
if(isNumber() && other.isNumber())
return this->data.numVal > other.data.numVal;
if(isString() && other.isString())
return strcmp(this->data.strVal, other.data.strVal) > 0;
if(isUndef() || other.isUndef())
throw std::runtime_error("Operand of \">\" is undefined");
throw std::runtime_error("Invalid operands to \">\"");
}
Value Value::operator>=(const Value& other) const{
try{
return this->operator==(other) || this->operator>(other);
}
catch(std::runtime_error &e){
if(isUndef() || other.isUndef())
throw e;
throw std::runtime_error("Invalid operands to \">=\"");
}
}
Value Value::operator<(const Value& other) const{
if(isNumber() && other.isNumber())
return this->data.numVal < other.data.numVal;
if(isString() && other.isString())
return strcmp(this->data.strVal, other.data.strVal) < 0;
if(isUndef() || other.isUndef())
throw std::runtime_error("Operand of \"<\" is undefined");
throw std::runtime_error("Invalid operands to \"<\"");
}
Value Value::operator<=(const Value& other) const{
try{
return this->operator==(other) || this->operator<(other);
}
catch(std::runtime_error &e){
if(isUndef() || other.isUndef())
throw e;
throw std::runtime_error("Invalid operands to \"<=\"");
}
}
Value Value::operator==(const Value& other) const{
if(this->type != other.type){
return false;
}
switch(this->type){
case(NumberType):
return this->data.numVal == other.data.numVal;
case(StringType):
return strcmp(this->data.strVal, other.data.strVal) == 0;
case(BooleanType):
return this->data.boolVal == other.data.boolVal;
case(ObjectType):
return this->data.objectVal == other.data.objectVal;
case(LibraryFunctionType):
return this->data.libFuncVal.ast == other.data.libFuncVal.ast && !strcmp(this->data.libFuncVal.libFuncName, other.data.libFuncVal.libFuncName);
case(ProgramFunctionType):
return this->data.progFuncVal.ast == other.data.progFuncVal.ast && this->data.progFuncVal.closure == other.data.progFuncVal.closure;
case(NativePtrType):
return this->data.nativePtrVal.ptr == other.data.nativePtrVal.ptr;
case(NilType):
return true;
case(UndefType):
throw std::runtime_error("Operand of \"==\" is undefined");
}
return false;
}
Value Value::operator!=(const Value& other) const{
try{
return !(this->operator==(other));
}
catch(std::runtime_error &e){
if(isUndef())
throw e;
throw std::runtime_error("Invalid operands to \"!=\"");
}
}
Value::operator bool() const {
switch(type) {
case UndefType:
throw std::runtime_error("Operand to logical operator is undefined\n");
case NumberType:
return (data.numVal != 0);
case BooleanType:
return data.boolVal;
case StringType:
return strlen(data.strVal) != 0;
case ObjectType:
case ProgramFunctionType:
case LibraryFunctionType:
return true;
case NativePtrType:
return data.nativePtrVal.ptr != nullptr;
case NilType:
return false;
default:
assert(false); //bad area
}
}
std::string Value::makeString() const{
switch(type) {
case(Type::StringType):
return std::string(data.strVal);
case(Type::LibraryFunctionType):
return "library function " + std::string(data.libFuncVal.libFuncName);
case(Type::ObjectType):
return data.objectVal->toString();
case(Type::ProgramFunctionType):
return "user function " + std::string(data.progFuncVal.funcName);
case(Type::NativePtrType):
return data.nativePtrVal.typeId;
case(Type::NumberType):
return std::to_string(data.numVal);
case(Type::BooleanType):
return data.boolVal ? "true" : "false";
case(Type::UndefType):
return "undefined";
case(Type::NilType):
return "nil";
default:
;
}
return std::string();
}
std::string Value::typeOfToString() {
switch (type) {
case(Type::StringType):
return "string";
case(Type::LibraryFunctionType):
return "libraryfunction";
case(Type::ObjectType):
return "object";
case(Type::ProgramFunctionType):
return "userfunction";
case(Type::NativePtrType):
return "nativepointer";
case(Type::NumberType):
return "number";
case(Type::BooleanType):
return "boolean";
case(Type::NilType):
return "nil";
default:
;
}
return std::string();
}