-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSymbolTable.cpp
163 lines (147 loc) · 3.88 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
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
#include"SymbolTable.h"
SymbolTable::SymbolTable():globalScope("") {
count = 0;
stringCount = 0;
}
SymbolTable::~SymbolTable() {
map<string, SubSymbolTable*>::iterator itr;
for (itr = scope.begin(); itr != scope.end(); itr++) {
delete itr->second;
}
}
SymbolEntry* SymbolTable::addSymbol(string currentScope, string name,bool isFunction) {
if (debug) {
cout << "debug@SymbolTable:calling addSymbol" << endl;
cout << "currentscope:" + currentScope << ";name:"
<< name << ";isFunction:" << isFunction << endl;
}
SymbolEntry* res;
if (currentScope != "") {
SubSymbolTable* s = scope[currentScope];
if (s == NULL) { return NULL; }
res = s->addSymbol(name, isFunction);
}
else {
res = globalScope.addSymbol(name, isFunction);
if(res!=NULL&&isFunction){
SubSymbolTable* newscope = new SubSymbolTable(name);
scope[name] = newscope;
if (debug) {
cout << "debug@SymbolTable:new scope added" << endl;
}
}
}
if (res != NULL) {
res->id = count++;
symbolId.push_back(res);
}
if (debug&&res==NULL) {
cout << "debug@SymbolTable:duplicate name found" << endl;
}
if (debug) { cout << "==============================" << endl; }
if (name == "main") {
mainSymbolId = res->id;
}
return res;
}
SymbolEntry* SymbolTable::getSymbolByName(string currentScope, string name) {
if (debug) {
cout << "debug@SymbolTable:calling getSymbolByName" << endl;
cout << "currentscope:" + currentScope << ";name:"
<< name<< endl;
}
SymbolEntry* res;
if (currentScope != "") {
SubSymbolTable* s = scope[currentScope];
if (s == NULL) { return NULL; }
res = s->getSymbolByName(name);
if (res != NULL) {
if (debug) { cout << "variable Type:LOCAL" << endl; }
return res;
}
}
res = globalScope.getSymbolByName(name);
if (debug) {
if (res != NULL) {
cout << "variable Type:GLOBAL" << endl;
cout << (*res);
}
else {
cout << "variable doesm't exist" << endl;
}
cout << "=============================" << endl;
}
return res;
}
SymbolEntry* SymbolTable:: getSymbolById(int id) {
if ( id >= (int)(symbolId.size())) {
return NULL;
}
else if (id < 0) {
return tmpSymbolId[id];
}
return symbolId[id];
}
string SymbolTable::getString(int id) {
return unnamedStrings[id];
}
int SymbolTable::addString(string str) {
unnamedStrings.push_back(str);
return stringCount++;
}
void SymbolTable::debugOn() {
debug = true;
}
SubSymbolTable* SymbolTable::getSubSymbolTableByName(string s) {
if (s == "") {
return &globalScope;
}
return scope[s];
}
SymbolEntry* SymbolTable::addTmpSymbol(string function, int id) {
SubSymbolTable* subtable = scope[function];
SymbolEntry*res= subtable->addTmpSymbol(id);
res->scope = function;
tmpSymbolId[id] = res;
return res;
}
ostream& operator<<(ostream& out, SymbolTable& t) {
out << "global:" << endl;
out << t.globalScope;
for (auto& i : t.scope) {
out << i.first << ":" << endl;
out << *(i.second);
}
return out;
}
map<int, vector<int>> SymbolTable::summary() {
map<int, vector<int>>res;
globalScope.summaryAndReport();
for (auto& i : scope) {
int id = globalScope.getSymbolByName(i.first)->id;
vector<int>tmp=i.second->summaryAndReport();
res[id] = tmp;
}
return res;
}
void SymbolTable::dumpMipsCodeHeader(ostream& f) {
for (int i = 0; i < unnamedStrings.size(); i++) {
f << "string$" << i << ": .asciiz \"" << unnamedStrings[i]<<"\""<<endl;
}
f << ".align 2" << endl;
vector<int> sequence = globalScope.sequence;
for (int i : sequence) {
SymbolEntry* entry = getSymbolById(i);
if (entry->type == TYPEINT || entry->type == TYPECHAR) {
f << entry->name << ": .space " << 4<<endl;
}
else if (entry->type == TYPEINTCONST || entry->type == TYPECHARCONST) {
f << entry->name << ": .word " << entry->initValue<<endl;
}
else if (entry->type == TYPEINTARRAY || entry->type == TYPECHARARRAY) {
f<<entry->name << ": .space " << entry->dimension*4 << endl;
}
}
}
void SymbolTable::selfTest() {
}