-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVariable.cpp
More file actions
executable file
·94 lines (84 loc) · 2.1 KB
/
Variable.cpp
File metadata and controls
executable file
·94 lines (84 loc) · 2.1 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
#include "Variable.hpp"
#include "Object.hpp"
Variable::Variable(void* v, Types::TypeName t, int s) : value(v), type(t), scope(s) {
}
Variable::Variable(Variable* var) {
type = var->type;
scope = var->scope;
switch(type) {
case Types::INT:
value = (void*)(new int(*(int*)(var->getValue())));
break;
case Types::DOUBLE:
value = (void*)(new double(*(double*)(var->getValue())));
break;
case Types::BOOLEAN:
value = (void*)(new bool(*(bool*)(var->getValue())));
break;
case Types::STRING:
value = (void*)(new std::string(*(std::string*)(var->getValue())));
break;
case Types::OBJECT:
value = var->getValue();
((Object*)value)->createReference();
break;
case Types::LANGUAGEELEMENT:
value = (void*)(new std::string(*(std::string*)(var->getValue())));
break;
}
}
Variable::~Variable() {
deleteValue();
}
void Variable::deleteValue() {
switch(type) {
case Types::INT:
delete ((int*)value);
break;
case Types::DOUBLE:
delete ((double*)value);
break;
case Types::BOOLEAN:
delete ((bool*)value);
break;
case Types::STRING:
delete ((std::string*)value);
break;
case Types::LANGUAGEELEMENT:
delete ((std::string*)value);
break;
case Types::OBJECT:
((Object*)value)->removeReference();
break;
}
}
void Variable::printDetails() {
std::cout << "Type: " << Types::getTypeName(type) << ", Scope: " << scope << ", Value: ";
print();
}
void Variable::print() {
switch(type) {
case Types::INT:
std::cout << *((int*)value);
break;
case Types::DOUBLE:
std::cout << *((double*)value);
break;
case Types::BOOLEAN:
std::cout << (*((bool*)value) == true ? "true" : "false");
break;
case Types::STRING:
std::cout << *((std::string*)value);
break;
case Types::LANGUAGEELEMENT:
std::cout << *((std::string*)value);
break;
}
}
void Variable::setType(Types::TypeName t) {
type = t;
if(type == Types::OBJECT) {
((Object*)(value))->createReference();
}
}
const std::regex Variable::variableRegex = std::regex("^[A-Z]+$");