-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVariables.cpp
More file actions
201 lines (170 loc) · 5.21 KB
/
Variables.cpp
File metadata and controls
201 lines (170 loc) · 5.21 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
#include "Variables.h"
#include <QDebug>
// manage storage of variables
// variables are limited to types (defined in Types.h):
int Variables::e = ERROR_NONE;
// T_REF is a variable number in the specified recursion level
// ************************************************************
// * the Variable object - defines a single value or an array *
// ************************************************************
Variable::Variable() {
data = new DataElement();
}
Variable::~Variable() {
delete(data);
}
// ************************************************************************
// * Variables contain the individual variable(s) for the recursion level *
// ************************************************************************
Variables::Variables(int n) : numsyms(n){
// n is the size of the symbol table
recurselevel = -1;
increaserecurse(); // go to recurse level 0 (main level)
isglobal = new bool[numsyms];
int i=numsyms;
while(i-- > 0) {
isglobal[i]=false;
}
}
Variables::~Variables() {
// free variables and all storage
// delete a recurse level being sure to delete
// the variables in the runlevel before removing itmake
while(recurselevel >= 0) {
decreaserecurse();
}
varmap.resize(0);
delete[] isglobal;
}
QString Variables::debug() {
// return a string representing the variables
QString s("");
if(!varmap.empty()) {
for(int i=0;i<=recurselevel;i++) {
s += QStringLiteral("recurse Level ") + QString::number(i) + "\n";
for(int j=0; j < numsyms; j++) {
s += QStringLiteral(" varnum ") + QString::number(j) + QStringLiteral(" ") ;
s += varmap[i][j]->data->debug();
s += "\n";
}
s += "\n";
}
}
return s;
}
void Variables::increaserecurse() {
// initialize a new recurse level
// create a new Variable for each symbol at this recurse level
// wasteful for the labels and jump points but vector is so fast
// to access an element it does not matter recurselevel++;
if (recurselevel>=MAX_RECURSE_LEVELS) {
recurselevel--;
e = ERROR_MAXRECURSE;
} else {
recurselevel++;
varmap.resize(recurselevel+1);
varmap[recurselevel] = new Variable*[numsyms];
int i=numsyms;
while(i-- > 0) {
varmap[recurselevel][i] = NULL;
}
}
}
int Variables::getrecurse() {
return recurselevel;
}
void Variables::decreaserecurse() {
if (recurselevel>=0) {
int i=numsyms;
while(i-- > 0) {
if (varmap[recurselevel][i]) delete varmap[recurselevel][i];
}
delete varmap[recurselevel];
// return back to prev variable context
recurselevel--;
varmap.resize(recurselevel+1);
}
}
Variable* Variables::get(int varnum, int level) {
// get v from map or follow REF to recurse level if needed
Variable *v;
//printf("variables::get - varnum %d level %d\n", varnum, level);
v = getAt(varnum, level);
if(DataElement::getType(v->data)==T_REF) {
// reference to other level - recurse down the rabitty hole
return get(v->data->intval, v->data->level);
} else {
return(v);
}
}
Variable* Variables::get(int varnum) {
return get(varnum, recurselevel);
}
Variable* Variables::getAt(int varnum, int level) {
// get variable from map without follow REF
// need for variable window too
Variable *v;
if(isglobal[varnum]) level = 0;
v = varmap[level][varnum];
if (not v) {
v = new Variable();
varmap[level][varnum] = v;
}
return v;
}
Variable* Variables::getAt(int varnum) {
// get variable without recurse from this level
return getAt(varnum, recurselevel);
}
DataElement* Variables::getData(int varnum) {
// get data from variable - return varnum's data
// DO NOT DELETE ****** COPY OF THE VARIABLE's INTERNAL STORAGE
Variable *v = get(varnum);
return v->data;
}
void Variables::setData(int varnum, DataElement* d) {
// recieves a DataElement pointed pulled from the stack
// e is a pointer in the stack vector AND MUST NOT BE DELETED
Variable *v;
if (DataElement::getType(d)==T_REF) {
// if we are assigning a "REF" dont recurse down the previous ref is there is one
// just assign it at the current run level.
v = getAt(varnum);
} else {
v = get(varnum, recurselevel);
}
v->data->copy(d);
}
void Variables::setData(int varnum, long l) {
Variable *v = get(varnum);
v->data->clear();
v->data->type = T_INT;
v->data->intval = l;
}
void Variables::setData(int varnum, double f) {
Variable *v = get(varnum);
v->data->clear();
v->data->type = T_FLOAT;
v->data->floatval = f;
}
void Variables::setData(int varnum, QString s) {
//fprintf(stderr,"s");
Variable *v = get(varnum);
v->data->clear();
v->data->type = T_STRING;
v->data->stringval = s;
}
void Variables::setData(int varnum, std::string s) {
// used in foreach for maps
setData(varnum, QString::fromStdString(s));
}
void Variables::unassign(int varnum) {
//unassign true variable, not the reference (unlink)
Variable *v = varmap[(recurselevel==0||isglobal[varnum])?0:recurselevel][varnum];
v->data->clear();
}
void Variables::makeglobal(int varnum) {
// make a variable global - if there is a value in a run level greater than 0 then
// that value will be lost to the program
isglobal[varnum] = true;
}