-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvariableList.hpp
More file actions
197 lines (181 loc) · 6.55 KB
/
variableList.hpp
File metadata and controls
197 lines (181 loc) · 6.55 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
//
// This class is used for storing variables that come in.
// Pui-Hin Vincent Lee: 180006178
// Alina Littek: 180016182
// Bjarne Kopplin: 180016866
// Jacque Nalwanga: 180004882
// Andrew Spence: 170018476
//
#ifndef MBASSEMBLER_VARIABLELIST_HPP
#define MBASSEMBLER_VARIABLELIST_HPP
#include <vector>
#include "variable.hpp"
#include "error.hpp"
#include "config.hpp"
using namespace std;
class variableList{
vector<variable> variableContainer; // Contains the variables
public:
// Look below the class declaration for the use of each function
bool doesContainName(const string& variableName);
void addVariable(string variableName, long variableLong);
void updateVariable(string variableName, long variableLong);
void setMemoryLocation(string variableName, int memoryLocation);
void bulkSetMemoryLocation(int fromMemoryLocation);
long getVariableData(string variableName);
int getMemoryLocation(string variableName);
int sizeOfVariableList();
void printVariableList();
variable getVariable(int vectorLocation);
};
/**
* Checks if a variable with that name already exists
* @param variableName the name you want to see is in the list or not
* @return whether the item exists or not
*/
bool variableList::doesContainName(const string& variableName) {
// We look through each item in the vector
for(auto & i : variableContainer){
if(i.getVariableName() == variableName){ //if the object we have a name matching
return true;// we return true as the object exists
}
}
return false; // if we go through the whole vector with no valid responses we return false for no doesn't exist
}
/**
* Adds a variable to the list
* @param variableName name of the variable to be added
* @param variableLong the size of the variable.
*/
void variableList::addVariable(string variableName, long variableLong) {
if(!doesContainName(variableName)){
variable temp; //create object
temp.assign(variableName, variableLong); //set values
variableContainer.push_back(temp); // place object into vector
}else{
throw VARIABLE_NAME_PRESENT;
}
}
/**
* Updates the value of a variable
* @param variableName name of variable to update
* @param variableLong the size of the variable.
*/
void variableList::updateVariable(string variableName, long variableLong) {
if(doesContainName(variableName)){
//for each variable object in the vector
for(auto & i : variableContainer){
if(i.getVariableName() == variableName){// if the variables name match
i.setVariableValue(variableLong);//set the variable value of object
return;
}
}
}else{
throw VARIABLE_DOES_NOT_EXIST;
}
}
/**
* Sets the memory location of a variable
* @param variableName name of variable to update
* @param memoryLocation memory location 0-31 to set
*/
void variableList::setMemoryLocation(string variableName, int memoryLocation) {
if(doesContainName(variableName)){
//for each variable object in the vector
for(auto & i : variableContainer){
if(i.getVariableName() == variableName){// if the variables name match
i.setMemoryLocation(memoryLocation);// set the memory location of the object
return;
}
}
}else{
throw VARIABLE_DOES_NOT_EXIST;
}
}
/**
* Allows for bulk setting of memory addresses of the whole list
* @param fromMemoryLocation memory address to set from
*/
void variableList::bulkSetMemoryLocation(int fromMemoryLocation) {
// Checks if we have enough memory locations to allocate to all variables
if((numberOfMemoryLocationsConfig-fromMemoryLocation) < (int)variableContainer.size()){
throw NOT_ENOUGH_MEMORY;
}else{
int memoryLocationBeingSet = fromMemoryLocation;
for(auto & i : variableContainer){
i.setMemoryLocation(memoryLocationBeingSet); // set memory location
memoryLocationBeingSet++; //increment the memory location being set
// if we have too many variables for after the instructions we use location 0 to store a variable
if(memoryLocationBeingSet == numberOfMemoryLocationsConfig){
memoryLocationBeingSet = 0;
}
}
return;
}
}
/**
* Returns the variable data
* @param variableName name of variable to check from
* @return the variable data
*/
long variableList::getVariableData(string variableName) {
if(!doesContainName(variableName)){
throw VARIABLE_DOES_NOT_EXIST;
}else{
//for each variable object in the vector
for(auto & i : variableContainer){
if(i.getVariableName() == variableName){ // if the variables name match
return i.getVariableValue(); //return the variable value of object
}
}
}
throw VARIABLE_DOES_NOT_EXIST;
}
/**
* Returns the memory location of an item
* @param variableName name of variable to check from
* @return the memory location of the variable
*/
int variableList::getMemoryLocation(string variableName) {
if(!doesContainName(variableName)){
throw VARIABLE_DOES_NOT_EXIST;
}else{
//for each variable object in the vector
for(auto & i : variableContainer){
if(i.getVariableName() == variableName){ // if the variables name match
return i.getMemoryLocation(); // return the memory location of the object
}
}
}
throw VARIABLE_DOES_NOT_EXIST;
}
/**
* Returns the size of the list of variables
* @return size of the list of variables
*/
int variableList::sizeOfVariableList() {
return (int)variableContainer.size();
}
/**
* Prints out the list of variables to the console
*/
void variableList::printVariableList() {
cout << "==========================" << endl;
cout << "Variable Vector List" << endl;
for(auto & i : variableContainer){ // for each item in the vector
cout << "Variable Name: " << i.getVariableName() << " | Variable Value: " << i.getVariableValue() << " | Memory Address: " << i.getMemoryLocation() << endl;
}
cout << "==========================" << endl;
}
/**
* returns the variable object
* @param vectorLocation the vector locations
* @return the variable object from the requested object location.
*/
variable variableList::getVariable(int vectorLocation) {
if(vectorLocation > (int)variableContainer.size() || vectorLocation < 0){
throw OUTSIDE_OF_VECTOR_RANGE;
}
return variableContainer.at(vectorLocation);
}
#endif //MBASSEMBLER_VARIABLELIST_HPP