-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsymboltable.cpp
112 lines (94 loc) · 2.86 KB
/
symboltable.cpp
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
#include <iostream>
using namespace std;
#include "Register.hh"
#include "symboltable.hh"
SymbolTableEntry::SymbolTableEntry(){
variablename="";
variabledatatype=INT;
scope=LOCAL;
lineNumber=0;
}
//SymbolTableEntry::SymbolTableEntry(){};
SymbolTableEntry::~SymbolTableEntry(){};
SymbolTableEntry::SymbolTableEntry(string &s, DataType d, int a){
variablename=s;
variabledatatype=d;
lineNumber=a;
}
int SymbolTableEntry::getLineNumber(){
return lineNumber;
}
void SymbolTableEntry::setSymbolScope(TableScope sp){
scope=sp;
}
TableScope SymbolTableEntry :: getSymbolScope(){
return scope;
}
DataType SymbolTableEntry :: getDataType(){
return variabledatatype;
}
void SymbolTableEntry :: setDataType(DataType d){
variabledatatype=d;
}
string SymbolTableEntry :: getVariableName(){
return variablename;
}
void SymbolTableEntry ::setStartOffset(int num){startOffset=num;}
int SymbolTableEntry ::getStartOffset(){return startOffset;}
void SymbolTableEntry ::setEndOffset(int num){endOffset=num;}
int SymbolTableEntry ::getEndOffset(){return endOffset;}
RegisterDescriptor * SymbolTableEntry ::getRegister(){return registerDescription;}
void SymbolTableEntry ::setRegister(RegisterDescriptor * reg){registerDescription=reg;}
SymbolTable:: SymbolTable(){
scope=LOCAL;
}
SymbolTable:: ~SymbolTable(){
varTable.clear();
}
TableScope SymbolTable:: getTableScope(){
return scope;
}
void SymbolTable :: setTableScope(TableScope s){
scope=s;
}
void SymbolTable :: print(ostream &f){
f<<"SymbolTableEntry - ";
f<<"TableScope : "<<((scope)==LOCAL?"LOCAL":"")<<endl;
list<SymbolTableEntry*>::iterator it;
for(it=varTable.begin();it!=varTable.end();it++){
f<<(*it)->getVariableName()<<" "<<(((*it)->getDataType())==INT?"INT":"")<<" "<<(((*it)->getSymbolScope())==LOCAL?"LOCAL":"")<<" "<<(*it)->getLineNumber()<<endl;
}
f<<endl;
}
void SymbolTable :: pushSymbol(SymbolTableEntry * se){
varTable.push_back(se);
}
bool SymbolTable :: variableInSymbolListCheck(string s){
list<SymbolTableEntry*>::iterator it;
for(it=varTable.begin();it!=varTable.end();it++){
if(!(s.compare((*it)->getVariableName())))
return true;
}
return false;
}
SymbolTableEntry & SymbolTable :: getSymbolTableEntry(string variablename){
list<SymbolTableEntry*>::iterator it;
SymbolTableEntry *ste=NULL;
for(it=varTable.begin();it!=varTable.end();it++){
if((*it)->getVariableName()==variablename)
return **it;
}
return *ste;
}
int SymbolTable :: getSizeOfType(DataType d){return sizeof(int);}
void SymbolTable :: assignOffsetsToSymbols(){
list<SymbolTableEntry*>::reverse_iterator it;
int size=0;
for(it=varTable.rbegin();it!=varTable.rend();it++){
(*it)->setStartOffset(size);
size-=4;
(*it)->setEndOffset(size);
//cout<<size<<endl;
}
}
int SymbolTable :: size(){return totalSize;}