-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDtype.cpp
More file actions
353 lines (244 loc) · 6.82 KB
/
Dtype.cpp
File metadata and controls
353 lines (244 loc) · 6.82 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
#include <string>
#include <algorithm>
#include "Dtype.h"
Dtype::Dtype() {
did = MATH_CPP_DTYPE_INVALID;
is_resolved = false;
}
Dtype::~Dtype() {
}
/* Concrete class now*/
Dtype_INT::Dtype_INT() {
this->did = MATH_CPP_INT;
this->dtype.int_val = 0;
}
Dtype_INT::Dtype_INT(int val) {
this->did = MATH_CPP_INT;
this->dtype.int_val = val;
this->is_resolved = true;
}
Dtype_INT::~Dtype_INT() {
}
MexprNode *
Dtype_INT::clone() {
Dtype_INT *obj = new Dtype_INT();
*obj = *this;
obj->parent = NULL;
obj->left = NULL;
obj->right = NULL;
obj->lst_left = NULL;
obj->lst_right = NULL;
return obj;
}
void
Dtype_INT::SetValue(void *value) {
int val = atoi ((const char *)value);
this->dtype.int_val = val;
this->is_resolved = true;
}
void
Dtype_INT::SetValue(Dtype *value) {
this->dtype.int_val = dynamic_cast <Dtype_INT *> (value)->dtype.int_val;
this->is_resolved = true;
}
mexprcpp_dtypes_t
Dtype_INT::ResultStorageType(mexprcpp_dtypes_t did1, mexprcpp_dtypes_t did2) {
return MATH_CPP_INT;
}
Dtype*
Dtype_INT::compute(Dtype *dtype1, Dtype *dtype2) {
return dynamic_cast <Dtype *>(clone() );
}
Dtype_DOUBLE::Dtype_DOUBLE() {
this->did = MATH_CPP_DOUBLE;
this->dtype.d_val = 0;
}
Dtype_DOUBLE::Dtype_DOUBLE(double val) {
this->did = MATH_CPP_DOUBLE;
this->dtype.d_val = val;
this->is_resolved = true;
}
Dtype_DOUBLE::~Dtype_DOUBLE() {
}
void
Dtype_DOUBLE::SetValue(void *value) {
double val = (double)atof ((const char *)value);
this->dtype.d_val = val;
this->is_resolved = true;
}
void
Dtype_DOUBLE::SetValue(Dtype *value) {
this->dtype.d_val = dynamic_cast <Dtype_DOUBLE *> (value)->dtype.d_val;
this->is_resolved = true;
}
MexprNode *
Dtype_DOUBLE::clone() {
Dtype_DOUBLE *obj = new Dtype_DOUBLE();
*obj = *this;
obj->parent = NULL;
obj->left = NULL;
obj->right = NULL;
obj->lst_left = NULL;
obj->lst_right = NULL;
return obj;
}
mexprcpp_dtypes_t
Dtype_DOUBLE::ResultStorageType(mexprcpp_dtypes_t did1, mexprcpp_dtypes_t did2) {
return MATH_CPP_DOUBLE;
}
Dtype*
Dtype_DOUBLE::compute(Dtype *dtype1, Dtype *dtype2) {
return dynamic_cast <Dtype *>(clone() );
}
Dtype_STRING::Dtype_STRING() {
this->did = MATH_CPP_STRING;
}
Dtype_STRING::~Dtype_STRING() {
}
void
Dtype_STRING::SetValue(void *value) {
this->dtype.str_val.assign(std::string ((char *)value));
// remove the " " on both the sides of the strings if present.
// remove the ' ' on both the sides of the string if present.
// because user enters string in single or double quotes. While stroing the strings, we dont store the doubl/single quotes
// Chat GPT gave me this to remove " (double quote) or ' (single quote)
// #include <algorithm> to use below functions
this->dtype.str_val.erase(
std::remove(this->dtype.str_val.begin(), this->dtype.str_val.end(), '\"'),
this->dtype.str_val.end());
this->dtype.str_val.erase(
std::remove(this->dtype.str_val.begin(), this->dtype.str_val.end(), '\''),
this->dtype.str_val.end());
this->is_resolved = true;
}
void
Dtype_STRING::SetValue(Dtype *value) {
Dtype_STRING *value_str = dynamic_cast<Dtype_STRING *> (value);
this->SetValue ((void *)value_str->dtype.str_val.c_str()); // leverage the Ist SetValue fn
this->is_resolved = true;
}
MexprNode *
Dtype_STRING::clone() {
Dtype_STRING *obj = new Dtype_STRING();
obj->dtype.str_val = this->dtype.str_val;
obj->parent = NULL;
obj->left = NULL;
obj->right = NULL;
obj->lst_left = NULL;
obj->lst_right = NULL;
return obj;
}
mexprcpp_dtypes_t
Dtype_STRING::ResultStorageType(mexprcpp_dtypes_t did1, mexprcpp_dtypes_t did2) {
return MATH_CPP_STRING;
}
Dtype*
Dtype_STRING::compute(Dtype *dtype1, Dtype *dtype2) {
return dynamic_cast <Dtype *>(clone() );
}
Dtype_BOOL::Dtype_BOOL() {
this->did = MATH_CPP_BOOL;
this->dtype.b_val = false;
this->is_resolved = true;
}
Dtype_BOOL::~Dtype_BOOL() {
}
void
Dtype_BOOL::SetValue(void *value) {
// empty
}
void
Dtype_BOOL::SetValue(Dtype *dtype) {
Dtype_BOOL *dtype_bool = dynamic_cast <Dtype_BOOL *> (dtype);
this->dtype.b_val = dtype_bool->dtype.b_val;
this->is_resolved = true;
}
MexprNode *
Dtype_BOOL::clone() {
Dtype_BOOL *obj = new Dtype_BOOL();
*obj = *this;
obj->parent = NULL;
obj->left = NULL;
obj->right = NULL;
obj->lst_left = NULL;
obj->lst_right = NULL;
return obj;
}
mexprcpp_dtypes_t
Dtype_BOOL::ResultStorageType(mexprcpp_dtypes_t did1, mexprcpp_dtypes_t did2) {
return MATH_CPP_BOOL;
}
Dtype*
Dtype_BOOL::compute(Dtype *dtype1, Dtype *dtype2) {
return dynamic_cast <Dtype *>(clone() );
}
Dtype_VARIABLE::Dtype_VARIABLE() {
this->did = MATH_CPP_VARIABLE;
this->dtype.variable_name.assign("");
this->is_resolved = false;
this->resolved_did = MATH_CPP_DTYPE_WILDCRAD;
}
Dtype_VARIABLE::Dtype_VARIABLE(std::string var_name) {
did = MATH_CPP_VARIABLE;
this->dtype.variable_name.assign(var_name);
this->is_resolved = false;
this->resolved_did = MATH_CPP_DTYPE_WILDCRAD;
}
Dtype_VARIABLE::~Dtype_VARIABLE() {
}
void
Dtype_VARIABLE::SetValue(void *value) {
}
void
Dtype_VARIABLE::SetValue(Dtype *dtype) {
}
MexprNode *
Dtype_VARIABLE::clone() {
Dtype_VARIABLE *obj = new Dtype_VARIABLE(this->dtype.variable_name);
*obj = *this;
obj->parent = NULL;
obj->left = NULL;
obj->right = NULL;
obj->lst_left = NULL;
obj->lst_right = NULL;
return obj;
}
mexprcpp_dtypes_t
Dtype_VARIABLE::ResultStorageType(mexprcpp_dtypes_t did1, mexprcpp_dtypes_t did2) {
if (this->is_resolved) return this->resolved_did;
return MATH_CPP_DTYPE_WILDCRAD;
}
Dtype *
Dtype_VARIABLE::compute(Dtype *dtype1, Dtype *dtype2) {
if (!this->is_resolved) return NULL;
Dtype *res = this->compute_fn_ptr ((char *)this->dtype.variable_name.c_str(), this->data_src);
return res;
}
void
Dtype_VARIABLE::ResolveOperand (
mexprcpp_dtypes_t resolved_did,
void *data_src,
Dtype *(*compute_fn_ptr)(char *, void *)) {
assert (!this->is_resolved);
this->data_src = data_src;
this->compute_fn_ptr = compute_fn_ptr;
this->is_resolved = true;
this->resolved_did = resolved_did;
}
Dtype *
Dtype::factory(mexprcpp_dtypes_t did) {
switch (did) {
case MATH_CPP_INT:
return new Dtype_INT();
case MATH_CPP_DOUBLE:
return new Dtype_DOUBLE();
case MATH_CPP_STRING:
return new Dtype_STRING();
case MATH_CPP_BOOL:
return new Dtype_BOOL();
case MATH_CPP_VARIABLE:
return new Dtype_VARIABLE();
default:
return NULL;
}
}