This repository has been archived by the owner on Dec 7, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
calc_value.hpp
410 lines (310 loc) · 7.98 KB
/
calc_value.hpp
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
#ifndef CALC_VALUE_H
#define CALC_VALUE_H
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <string>
#include <vector>
#include "string_stack.hpp"
#include "lambda.hpp"
#include "user_types.hpp"
#define USERVAR_NAME_MAXLENGTH 20
// to be defined later
class CalcValue;
class UserType;
class UserVar;
namespace vars {
extern CalcValue* valueAtVar(UserVar* first, const char name[USERVAR_NAME_MAXLENGTH]);
extern CalcValue* valueAtVar(std::vector<UserVar>& vars, const char name[USERVAR_NAME_MAXLENGTH]);
extern UserVar* findVar(std::vector<UserVar>& vars, const char name[USERVAR_NAME_MAXLENGTH]);
extern UserVar* findVar(UserVar* first, const char name[USERVAR_NAME_MAXLENGTH]);
}
// utils.h
namespace strutils {
extern char *trimStr(char *string);
}
// this will be used for list indexes
// I want numbers to by default be converted into CalcValue::NUM and not indexes
// making a separate index class ensures that I won't break anything
class index_t {
public:
ssize_t index;
index_t(const ssize_t& in):
index(in){ }
index_t& operator=(const index_t& in)
{ index = in.index; return *this;}
};
/// a temporary value to hold user data in
class CalcValue {
public:
// which type is represented by the data union
enum base_type {
NUM, // number/boolean
STR, // string
REF, // reference to a variable
BLK, // Block of code (StrStack) (subroutine) (executable array)
ARR, // vector
INX, // index of an array, type made for
LAM, // lambda
REQ, // member request
OBJ // custom type
} type;
// contains the data
union {
double number;
char* string;
StrStack* block;
std::vector<CalcValue>* list;
ssize_t index;
Lambda* lambda;
std::vector<std::string>* request;
UserType* object;
};
// this sometimes causes a core dump (QwQ)
~CalcValue(){
clear();
}
// delete current value
void clear() {
if (type == STR || type == REF)
free(string); // free() accepts NULL pointers
else if (type == BLK)
delete block;
else if (type == ARR)
delete list;
else if (type == LAM)
delete lambda;
else if (type == OBJ)
delete object;
else if (type == REQ)
delete request;
}
// Null object
CalcValue(): type(STR), string(NULL) {}
CalcValue (const double val): type(NUM) {
number = val;
}
CalcValue (const UserType& obj):
type(OBJ)
{
object = new UserType(obj);
}
CalcValue(const char* const str): type(STR) {
if (str) {
// allocate memory for the string
string = (char*) calloc(strlen(str) + 2, sizeof(char));
// write the string to the buffer
strcpy(string, str);
// declaring a NULL_CALCVAL_OBJECT
} else
string = NULL;
}
CalcValue(const StrStack& codeBlock): type(BLK)
{ block = new StrStack(codeBlock); }
CalcValue(const StrStack* codeBlock): type(BLK)
{ block = new StrStack(*codeBlock); }
CalcValue(const std::vector<CalcValue>& in_list):
type(ARR), list(new std::vector<CalcValue>(in_list))
{}
CalcValue(const std::vector<std::string>& in_list):
type(REQ), request(new std::vector<std::string>(in_list))
{}
CalcValue(const index_t in_index):
type(INX), index(in_index.index)
{}
CalcValue(const Lambda& lam) {
type = LAM;
lambda = new Lambda(lam);
}
CalcValue(const CalcValue& in){
// no need to delete anything as nothing is there yet
// they will be the same type of data
type = in.type;
// copy in the value
if (type == NUM)
number = in.number;
else if (type == STR || type == REF) {
if (in.string) {
string = (char*) malloc(strlen(in.string) + 1);
strcpy(string, in.string);
} else
string = NULL;
} else if (type == BLK)
block = new StrStack(*in.block);
else if (type == ARR) {
list = new std::vector<CalcValue>();
for (const CalcValue elem : *in.list)
list->push_back(elem);
} else if (type == INX)
index = in.index;
else if (type == LAM)
lambda = new Lambda(*in.lambda);
else if (type == OBJ)
object = new UserType(*in.object);
else if (type == REQ)
request = new std::vector<std::string>(*in.request);
}
// C++ is awesome
template<class T>
CalcValue& operator=(const T val) {
setValue(val);
return *this;
}
CalcValue& operator=(const CalcValue in) {
setValue(in);
return *this;
}
void setValue(const CalcValue in){
// delete old value
clear();
// they will be the same type of data
type = in.type;
// copy in the value
if (type == NUM)
number = in.number;
else if (type == STR || type == REF) {
if (in.string) {
string = (char*) malloc(strlen(in.string) + 1);
strcpy(string, in.string);
} else
string = NULL;
} else if (type == BLK)
block = new StrStack(*in.block);
else if (type == ARR) {
list = new std::vector<CalcValue>();
for (CalcValue elem : *in.list)
list->push_back(elem);
} else if (type == INX)
index = in.index;
else if (type == LAM)
lambda = new Lambda(*in.lambda);
else if (type == REQ)
request = new std::vector<std::string>(*in.request);
else if (type == OBJ)
object = new UserType(*in.object);
}
void setValue(const UserType in) {
clear();
object = new UserType(in);
type = OBJ;
}
void setValue(const char* const str){
clear();
string = (char*) malloc(strlen(str) + 1);
// write the string to the buffer
strcpy(string, str);
type = STR;
}
void setValue(const char ch){
clear();
type = STR;
string = (char*) malloc(2);
*string = ch;
*(string + 1) = '\0';
}
void setValue(double val){
clear();
type = NUM;
number = val;
}
void setValue(const std::vector<CalcValue>& arr){
// delete old value
clear();
type = ARR;
list = new std::vector<CalcValue>(arr);
}
void setValue(const std::vector<std::string>& req) {
// del old val
clear();
type = REQ;
request = new std::vector<std::string>(req);
}
void setValue(const bool in)
{ setValue(in ? 1.0 : 0.0); }
void setValue(const Lambda& lam) {
// delete old value
clear();
type = LAM;
lambda = new Lambda(lam);
}
CalcValue& setRef(const char* const str){
// memory leaks are pretty bad
clear();
// set type
type = REF;
// create buffer
string = (char*) malloc(strlen(str) + 1);
// write the string to the buffer
strcpy(string, str);
return *this;
}
double getNum(){
if (type == NUM)
return number;
else if (type == STR) // short-circuit logic prevents segfault
return string && strlen(string);
else
return 0;
}
char* getStr(){
if (type == STR)
return string;
else
return NULL;
}
char* getRef(){
if (type == REF)
return string;
else
return NULL;
}
CalcValue* valAtRef(std::vector<UserVar>& vars){
if (type == REF)
return vars::valueAtVar(vars, string);
else
return this;
}
CalcValue* valAtRef(UserVar* first){
if (type == REF)
return vars::valueAtVar(first, string);
else
return this;
}
bool operator==(const CalcValue& cv2);
bool operator!=(const CalcValue& vs) {
return !operator==(vs);
}
// A Null value
bool isEmpty()
{ return (type == STR) && (string == NULL); }
bool isNull()
{ return isEmpty(); }
// type checking (doesn't get used) :/
bool isRef()
{ return type == REF; }
bool isStr()
{ return !isEmpty() && type == STR; }
bool isNum()
{ return type == NUM; }
bool isBlk()
{ return type == BLK; }
bool isArr()
{ return type == ARR; }
bool isObj()
{ return type == OBJ; }
// return a string representation of the variable
std::string toString(std::vector<UserVar>& var_nodes);
// string form of type
const char* typeName();
// list accessor
CalcValue* getListElem(const std::vector<ssize_t>& elem_index);
// list modifiers
bool assignElem(const std::vector<ssize_t>& elem_index, const CalcValue& val);
bool deleteListElem(const std::vector<ssize_t>& elem_index);
// get the member of an object
CalcValue* requestMember(std::vector<UserVar>& var_nodes);
CalcValue* requestMember(UserVar* first_node);
CalcValue* requestMember(std::vector<std::string>& req, std::vector<UserVar>* var_nodes);
};
#define NULL_CALCVAL_OBJECT CalcValue((char*) NULL)
#endif