-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory.cpp
More file actions
98 lines (84 loc) · 3.04 KB
/
memory.cpp
File metadata and controls
98 lines (84 loc) · 3.04 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
//
// Created by erthax on 14.01.2021.
//
#include "memory.h"
Memory::Memory() {
this->createRegisters();
}
void Memory::createRegisters() {
registers["a"] = new Register("a");
registers["b"] = new Register("b");
registers["c"] = new Register("c");
registers["d"] = new Register("d");
registers["e"] = new Register("e");
registers["f"] = new Register("f");
}
Variable *Memory::getVariable(std::string tVarName) {
checkIfDeclared(tVarName);
if(this->symbolTable[tVarName]->variableType == eVariableType::ITERATOR && !(this->symbolTable[tVarName]->isInsideLoop)){
throw tVarName + " was not declared";
}
return symbolTable[tVarName];
}
void Memory::checkIfDeclared(std::string tVarName) {
if (symbolTable.find(tVarName) == symbolTable.end()) {
throw tVarName + " was not declared";
}
}
void Memory::declareVariable(std::string pid) {
if (symbolTable.find(pid) != symbolTable.end()) {
throw pid + " is already declared";
}
this->symbolTable[pid] = new Variable(this->memoryAddresses, pid);
this->memoryAddresses++;
}
void Memory::declareArray(std::string tPid, uint tStart, uint tEnd) {
if (symbolTable.find(tPid) != symbolTable.end()) {
throw tPid + " is already declared";
}
this->symbolTable[tPid] = new Variable(this->memoryAddresses, tPid, tStart, tEnd);
this->memoryAddresses += (tEnd - tStart) + 1;
}
void Memory::declareConstant(std::string tPid, uint value) {
if (symbolTable.find(tPid) == symbolTable.end()) {
this->symbolTable[tPid] = new Variable(this->memoryAddresses, tPid, eVariableType::CONSTANT);
this->memoryAddresses++;
}
}
void Memory::declareIterator(std::string tPid) {
if (symbolTable.find(tPid) == symbolTable.end()) { // if we cant find it, create it
this->symbolTable[tPid] = new Variable(this->memoryAddresses, tPid, eVariableType::ITERATOR);
this->symbolTable[tPid]->bInitialized = true;
this->symbolTable[tPid]->isInsideLoop = true;
this->memoryAddresses++;
this->iteratorCount++;
} else if(symbolTable[tPid]->isInsideLoop){ // if we can find it and it inside of a loop
throw tPid + " is already declared";
} else { //we can find it, but outside of a loop
symbolTable[tPid]->isInsideLoop = true;
}
}
void Memory::eraseIterator(std::string tPid) {
this->symbolTable.erase(tPid);
this->iteratorCount--;
}
void Memory::freeRegisters() { // TODO i think i should do freeing only specified registers
for (auto &it : registers) {
if (it.second->isFull()) {
it.second->freeRegister();
//reset(it.second);
}
}
}
Register *Memory::getFreeRegister() {
for (auto &it : registers) {
if (!(it.second->isFull())) {
it.second->setFull(true);
return it.second;
}
}
throw "no more registers rry"; //TODO jezeli wszystkie pełne wyrzucać cos do zmiennej w pamięci? ale nie powinno tak być
}
Variable* Memory::getIterator(std::string tPid) {
return symbolTable[tPid];
}