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.cpp
277 lines (212 loc) · 6.45 KB
/
calc_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
#include "calc_value.hpp"
bool CalcValue::operator==(const CalcValue& cv2) {
// same type
if (type == cv2.type) {
// same value
if (type == NUM && number == cv2.number)
return true;
if (type == STR && isEmpty() != !cv2.string)
return false;
if (type == STR && string == cv2.string)
return true;
else if (type == STR || cv2.type == CalcValue::REF)
return strcmp(string, cv2.string) == 0;
// this doesnt work...
else if (type == CalcValue::ARR) {
if (list->size() != cv2.list->size())
return false;
for (size_t i = 0; i < list->size() - 1; i++)
if (!((*list)[i] == (*cv2.list)[i]))
return false;
return true;
} else if (type == INX)
return (index == cv2.index);
else if (type == OBJ) {
return false;
// TODO: find a better solution for REQ == REQ
} else if (type == REQ) {
if (request->size() != cv2.request->size())
return false;
for (size_t i = 0; i < request->size() - 1; i++)
if (!((*request)[i] == (*cv2.request)[i]))
return false;
return true;
}
}
return false;
}
std::string CalcValue::toString(std::vector<UserVar>& var_nodes){
std::string ret;
// null
if (isNull())
return "null";
// macro
else if (type == CalcValue::BLK) {
size_t len = 50;
char* str = (char*) malloc(len); // alloc mem for str
block->toString(&str, &len); // fill str
ret = str; // copy str into ret
free(str); // recycle memory
return ret;
// number
} else if (type == CalcValue::NUM) {
char str[27];
snprintf(str, 26, "%*.*g", 10, 16, getNum());
return strutils::trimStr(str);
// already a string
} else if (type == CalcValue::STR)
return "\"" + std::string(string) + "\"";
// a variable
else if (type == CalcValue::REF) {
CalcValue* val = valAtRef(var_nodes);
while (val && val->type == CalcValue::REF)
val = val->valAtRef(var_nodes);
if (val)
return val->toString(var_nodes);
else
return "reference_error";
// list printing a list
} else if (type == CalcValue::ARR) {
ret += "(";
if (list->size()) {
ret += list->at(0).toString(var_nodes);
for (size_t i = 1; i < list->size(); i++)
ret += ", " + list->at(i).toString(var_nodes);
}
ret+= " )";
// an object
} else if (type == CalcValue::OBJ) {
ret += "{";
for (unsigned i = 0; i < object->members.size(); i++) {
ret += "\n :" + object->members[i] + " ";
ret += object->values[i].toString(var_nodes);
}
ret += "\n} object";
// a member request
} else if (type == CalcValue::REQ) {
if (request->at(0) == " ")
ret += "__";
else
ret += "$" + request->at(0);
for (unsigned i = 1; i < request->size(); i++)
ret += " :" + request->at(i);
}
return ret;
}
// string form of type
const char* CalcValue::typeName() {
switch (type) {
case STR:
if (isNull()) // null ptr
return "null";
else
return "string";
case NUM: return "number/boolean";
case REF: return "reference";
case BLK: return "macro";
case ARR: return "list";
case LAM: return "lambda";
case OBJ: return "object";
case REQ: return "member";
case INX: return "index";
default: return "WTF";
}
}
/* Sub type specific functions which make it easier to work with specific types,
* such as objects/members, lists/indexes, etc.
* ##### # # ### #### ### ### ### ## ### #### ### ## #### # # # # ###
* # # # # # # # # # # # # # # # # # # # # ## # #
* # # ### ### ## ### ## # # ### # # ### # # # # ##
* # # # # # # # # # # # # # # # # # # ## #
* # # # #### ### # ### ## ### # ### ## # # # # # ### #
*/
// list accessor
CalcValue* CalcValue::getListElem(const std::vector<ssize_t>& elem_index) {
if (type != ARR)
return NULL;
CalcValue* ret = this;
for (int16_t i = elem_index.size() - 1; i >= 0; i--) {
if ((ssize_t) ret->list->size() <= elem_index[i] || (ssize_t) ret->list->size() < abs(elem_index[i])) {
return NULL;
}
ret = elem_index[i] < 0 ?
&ret->list->at(ret->list->size() + elem_index[i]) :
&ret->list->at(elem_index[i]);
}
return ret;
}
// list modifier
bool CalcValue::assignElem(const std::vector<ssize_t>& elem_index, const CalcValue& val) {
CalcValue* mod = getListElem(elem_index);
if (!mod)
return false;
mod->setValue(val);
return true;
}
bool CalcValue::deleteListElem(const std::vector<ssize_t>& elem_index) {
if (type != ARR)
return false;
CalcValue* ret = this;
for (int16_t i = elem_index.size() - 1; i > 0; i--) {
if ((ssize_t) ret->list->size() <= elem_index[i] || (ssize_t) ret->list->size() < abs(elem_index[i]))
return false;
ret = elem_index[i] < 0 ?
&ret->list->at(ret->list->size() + elem_index[i]) :
&ret->list->at(elem_index[i]);
}
if (!ret || !ret->list)
return false;
ret->list->erase(ret->list->begin() + elem_index[0]);
return true;
}
CalcValue* CalcValue::requestMember(std::vector<UserVar>& var_nodes) {
if (type != REQ && request->at(0) == " ")
return NULL;
CalcValue* ret = CalcValue()
.setRef(request->at(0).c_str())
.valAtRef(var_nodes);
if (!ret)
return NULL;
for (uint16_t i = 1; i < request->size(); i++) {
if (ret->type != CalcValue::OBJ)
return NULL;
// if the object doesn't have a member with that name
if (! (ret = ret->object->getMember(request->at(i))))
return NULL;
}
return ret;
}
CalcValue* CalcValue::requestMember(UserVar* first_node) {
if (type != REQ || request->at(0) == " ")
return NULL;
CalcValue* ret = CalcValue()
.setRef(request->at(0).c_str())
.valAtRef(first_node);
if (!ret)
return NULL;
for (uint16_t i = 1; i < request->size(); i++) {
if (ret->type != CalcValue::OBJ)
return NULL;
// if the object doesn't have a member with that name
if (! (ret = ret->object->getMember(request->at(i))))
return NULL;
}
return ret;
}
CalcValue* CalcValue::requestMember(std::vector<std::string>& req, std::vector<UserVar>* var_nodes = NULL) {
if (req.at(0) != " ") {
return NULL;
}
CalcValue* ret = this;
//printf("reqMem:%s\n", ret->toString(*var_nodes).c_str());
for (uint16_t i = 1; i < req.size(); i++) {
//printf("reqMem:%s\n", ret->toString(*var_nodes).c_str());
// request expected an object where there is none...
if (ret->type != CalcValue::OBJ)
return NULL;
// if the object doesn't have a member with that name
if (!(ret = ret->object->getMember(req[i])))
return NULL;
}
return ret;
}