-
Notifications
You must be signed in to change notification settings - Fork 0
/
Value.hpp
456 lines (361 loc) · 9.14 KB
/
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
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
#pragma once
#include <string>
#include <vector>
#include <unordered_map>
#include <istream>
#include <ostream>
#include "Identifier.hpp"
#include "Exceptions.hpp"
class Value
{
public:
Value(bool isConst = false): isConst(isConst) {}
virtual ~Value() {}
virtual void setValue(Value* value) = 0;
virtual Value* copy() = 0;
virtual void print(std::ostream& os) = 0;
virtual void read(std::istream& is) = 0;
friend std::ostream& operator<< (std::ostream& os, Value* v)
{
v->print(os);
return os;
}
friend std::istream& operator>> (std::istream& is, Value* v)
{
v->read(is);
return is;
}
virtual Identifier getType() = 0;
virtual Value* negation()
{
throw wrong_type("Operand of negation operator has incorrect type " + getType());
}
virtual Value* complement()
{
throw wrong_type("Operand of complement operator has incorrect type " + getType());
}
virtual Value* plus(Value* operand)
{
throw wrong_type("Left operand of plus operator has incorrect type " + getType());
}
virtual Value* minus(Value* operand)
{
throw wrong_type("Left operand of minus operator has incorrect type " + getType());
}
virtual Value* div(Value* operand)
{
throw wrong_type("Left operand of div operator has incorrect type " + getType());
}
virtual Value* divide(Value* operand)
{
throw wrong_type("Left operand of divide operator has incorrect type " + getType());
}
virtual Value* mod(Value* operand)
{
throw wrong_type("Left operand of mod operator has incorrect type " + getType());
}
virtual Value* multiply(Value* operand)
{
throw wrong_type("Left operand of multiply operator has incorrect type " + getType());
}
virtual Value* orOperator(Value* operand)
{
throw wrong_type("Left operand of or operator has incorrect type " + getType());
}
virtual Value* andOperator(Value* operand)
{
throw wrong_type("Left operand of and operator has incorrect type " + getType());
}
virtual Value* equal(Value* operand)
{
throw wrong_type("Left operand of equal operator has incorrect type " + getType());
}
virtual Value* notEqual(Value* operand)
{
throw wrong_type("Left operand of not equal operator has incorrect type " + getType());
}
virtual Value* less(Value* operand)
{
throw wrong_type("Left operand of less operator has incorrect type " + getType());
}
virtual Value* greater(Value* operand)
{
throw wrong_type("Left operand of greater operator has incorrect type " + getType());
}
virtual Value* lessEqual(Value* operand)
{
throw wrong_type("Left operand of less equal operator has incorrect type " + getType());
}
virtual Value* greaterEqual(Value* operand)
{
throw wrong_type("Left operand of greater equal operator has incorrect type " + getType());
}
bool isConst;
};
class ArrayValue : public Value
{
public:
ArrayValue(int arraySize)
{
array.resize(arraySize);
}
virtual ~ArrayValue() {}
virtual void setValue(Value* value)
{
ArrayValue* arrayValue = dynamic_cast<ArrayValue*> (value);
if (!arrayValue)
{
throw wrong_type("Wrong assignment type");
}
for (int i = 0; i < array.size(); ++i)
{
array[i]->setValue(arrayValue->array[i]);
}
}
virtual Value* copy()
{
ArrayValue* newValue = new ArrayValue(array.size());
for (int i = 0; i < array.size(); ++i)
{
newValue->array[i] = array[i]->copy();
}
return newValue;
}
virtual void print(std::ostream& os)
{
os << "[";
for (int i = 0; i < array.size(); ++i)
{
if (i != 0)
os << ", ";
os << array[i];
}
os << "]";
}
virtual void read(std::istream& is)
{
for (int i = 0; i < array.size(); ++i)
{
is >> array[i];
}
}
virtual Identifier getType() { return Identifier("ARRAY"); }
std::vector<Value*> array;
};
class StructValue : public Value
{
public:
StructValue() {}
virtual ~StructValue() {}
virtual void setValue(Value* value)
{
StructValue* structValue = dynamic_cast<StructValue*> (value);
if (!structValue)
{
throw wrong_type("Wrong assignment type");
}
for (auto& field : fields)
{
if (!structValue->hasField(field.first))
{
throw wrong_type("Wrong assignment type, field " + field.first + " is missing");
}
field.second->setValue(structValue->fields[field.first]);
}
}
virtual Value* copy()
{
StructValue* newValue = new StructValue();
for (auto& field : fields)
{
newValue->fields[field.first] = field.second->copy();
}
return newValue;
}
virtual void print(std::ostream& os)
{
os << '{';
bool isFirst = true;
for (auto& field : fields)
{
if (!isFirst)
os << ", ";
isFirst = false;
os << field.first << " : " << field.second;
}
os << '}';
}
virtual void read(std::istream& is)
{
throw wrong_type("Read operation is unsupported");
}
bool hasField(Identifier identifier)
{
return (fields.find(identifier) != fields.end());
}
virtual Identifier getType() { return Identifier("RECORD"); }
std::unordered_map<Identifier, Value*> fields;
};
class IntegerValue : public Value
{
public:
explicit IntegerValue(int value): value(value) {}
virtual ~IntegerValue() {}
virtual void setValue(Value* value)
{
IntegerValue* integerValue = dynamic_cast<IntegerValue*> (value);
if (!integerValue)
{
throw wrong_type("Wrong assignment type");
}
this->value = integerValue->value;
}
virtual Value* copy()
{
return new IntegerValue(value);
}
virtual void print(std::ostream& os)
{
os << value;
}
virtual void read(std::istream& is)
{
is >> value;
}
virtual Identifier getType() { return Identifier("INTEGER"); }
virtual Value* negation();
virtual Value* complement();
virtual Value* plus(Value* operand);
virtual Value* minus(Value* operand);
virtual Value* div(Value* operand);
virtual Value* mod(Value* operand);
virtual Value* divide(Value* operand);
virtual Value* multiply(Value* operand);
virtual Value* orOperator(Value* operand);
virtual Value* andOperator(Value* operand);
virtual Value* equal(Value* operand);
virtual Value* notEqual(Value* operand);
virtual Value* less(Value* operand);
virtual Value* greater(Value* operand);
virtual Value* lessEqual(Value* operand);
virtual Value* greaterEqual(Value* operand);
int value;
};
class DoubleValue : public Value
{
public:
explicit DoubleValue(double value): value(value) {}
virtual ~DoubleValue() {}
virtual void setValue(Value* value)
{
DoubleValue* doubleValue = dynamic_cast<DoubleValue*> (value);
if (!doubleValue)
{
throw wrong_type("Wrong assignment type");
}
this->value = doubleValue->value;
}
virtual Value* copy()
{
return new DoubleValue(value);
}
virtual void print(std::ostream& os)
{
os << value;
}
virtual void read(std::istream& is)
{
is >> value;
}
virtual Identifier getType() { return Identifier("DOUBLE"); }
virtual Value* negation();
virtual Value* plus(Value* operand);
virtual Value* minus(Value* operand);
virtual Value* divide(Value* operand);
virtual Value* multiply(Value* operand);
virtual Value* equal(Value* operand);
virtual Value* notEqual(Value* operand);
virtual Value* less(Value* operand);
virtual Value* greater(Value* operand);
virtual Value* lessEqual(Value* operand);
virtual Value* greaterEqual(Value* operand);
double value;
};
class BooleanValue : public Value
{
public:
explicit BooleanValue(bool value): value(value) {}
virtual ~BooleanValue() {}
virtual void setValue(Value* value)
{
BooleanValue* booleanValue = dynamic_cast<BooleanValue*> (value);
if (!booleanValue)
{
throw wrong_type("Wrong assignment type");
}
this->value = booleanValue->value;
}
virtual Value* copy()
{
return new BooleanValue(value);
}
virtual void print(std::ostream& os)
{
os << value;
}
virtual void read(std::istream& is)
{
is >> value;
}
virtual Identifier getType() { return Identifier("BOOLEAN"); }
virtual Value* negation();
virtual Value* complement();
virtual Value* plus(Value* operand);
virtual Value* multiply(Value* operand);
virtual Value* orOperator(Value* operand);
virtual Value* andOperator(Value* operand);
virtual Value* equal(Value* operand);
virtual Value* notEqual(Value* operand);
virtual Value* less(Value* operand);
virtual Value* greater(Value* operand);
virtual Value* lessEqual(Value* operand);
virtual Value* greaterEqual(Value* operand);
bool value;
};
class StringValue : public Value
{
public:
explicit StringValue(std::string value): value(value) {}
virtual ~StringValue() {}
virtual void setValue(Value* value)
{
StringValue* stringValue = dynamic_cast<StringValue*> (value);
if (!stringValue)
{
throw wrong_type("Wrong assignment type");
}
this->value = stringValue->value;
}
virtual Value* copy()
{
return new StringValue(value);
}
virtual void print(std::ostream& os)
{
os << value;
}
virtual void read(std::istream& is)
{
is >> value;
}
virtual Identifier getType() { return Identifier("STRING"); }
virtual Value* plus(Value* operand);
virtual Value* multiply(Value* operand);
virtual Value* equal(Value* operand);
virtual Value* notEqual(Value* operand);
virtual Value* less(Value* operand);
virtual Value* greater(Value* operand);
virtual Value* lessEqual(Value* operand);
virtual Value* greaterEqual(Value* operand);
std::string value;
};