-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArray.cpp
More file actions
executable file
·77 lines (62 loc) · 1.75 KB
/
Array.cpp
File metadata and controls
executable file
·77 lines (62 loc) · 1.75 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
#include "Array.hpp"
Array::Array(int sz) : Object(ObjectType::ARRAY), size(sz), arrayType(Types::OTHER) {
internalArray = new Variable*[size];
for(int i = 0; i < size; i++) {
internalArray[i] = (Variable*)(new ArrayVariable(nullptr, arrayType, -2, *this));
}
}
Array::~Array() {
if(internalArray != nullptr)
deleteObject();
}
void Array::deleteObject() {
for(int i = 0; i < size; i++) {
delete internalArray[i];
}
delete[] internalArray;
internalArray = nullptr;
}
Variable* Array::get(int idx) {
if(idx >= 0 && idx < size) {
return internalArray[idx];
} else {
throw RuntimeException("Attempting to access array out of bounds");
}
}
//Technically useless
/*void Array::set(int idx, Variable* var) {
std::cout << "At least set got called" << std::endl;
if(idx >= 0 && idx < size) {
if(!initialized) {
arrayType = var->getType();
initializeInternalArray();
}
if(var->getType() == arrayType) {
internalArray[idx]->setValue(var);
} else {
throw RuntimeException("Array-variable type mismatch");
}
} else {
throw RuntimeException("Attempting to access array out of bounds");
}
}*/
void Array::initializeInternalArray(Types::TypeName t) {
arrayType = t;
for(int i = 0; i < size; i++) {
if(internalArray[i]->getType() != Types::OTHER) continue;
switch(arrayType) {
case Types::INT:
internalArray[i]->setValue((void*)(new int(0)));
break;
case Types::DOUBLE:
internalArray[i]->setValue((void*)(new double(0.0d)));
break;
case Types::BOOLEAN:
internalArray[i]->setValue((void*)(new bool(false)));
break;
case Types::STRING:
internalArray[i]->setValue((void*)(new std::string("")));
break;
}
}
}