-
Notifications
You must be signed in to change notification settings - Fork 2
/
ScopeTable.cpp
192 lines (149 loc) · 3.45 KB
/
ScopeTable.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
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
/*
*
* Hash Table : Separate Chaining Method
*
* - Md. Zarif Ul Alam
* Staring at : 16 November 2020 6:10pm
*
* */
/* Which of the favors of your Lord will you deny ? */
#include<bits/stdc++.h>
#include "SymbolInfo.h"
#include "ScopeTable.h"
using namespace std;
ScopeTable::~ScopeTable() /// destructor
{
// cout<<"ScopeTable memory deallocated"<<endl;
for(int i=0; i<ht.size(); i++)
{
SymbolInfo* now = ht[i];
while(now != NULL)
{
SymbolInfo* tmp = now->nxt;
delete now;
now = tmp;
}
}
}
string ScopeTable::get_id()
{
return id;
}
void ScopeTable::set_id(string id)
{
this->id = id;
}
int ScopeTable::get_counter()
{
return counter;
}
void ScopeTable::set_counter(int counter)
{
this->counter = counter;
}
void ScopeTable::increase_counter()
{
counter++;
}
int ScopeTable::hash(string key)
{
int ret = (hashValue(key) % M);
return ret;
}
SymbolInfo* ScopeTable::search(string key)
{
int bucket = hash(key);
int bucket_pos = 0;
SymbolInfo* now = ht[bucket];
while(now != NULL)
{
if(now->key == key)
{
now->bucket_pos = bucket_pos;
return now;
}
now = now->nxt;
bucket_pos++;
}
return NULL; /*not found*/
}
SymbolInfo* ScopeTable::insert(SymbolInfo si)
{
string key = si.key;
string val = si.val;
string var_type = si.var_type;
string stk_offset = si.stk_offset;
int ara_size = si.ara_size;
vector<string>param_v = si.param_v;
bool isFunctionDeclaration = si.isFunctionDeclaration;
bool isFunction = si.isFunction;
int bucket = hash(key);
SymbolInfo* to_insert = new SymbolInfo(key,val,var_type,stk_offset,ara_size,param_v,isFunctionDeclaration,isFunction,NULL);
to_insert->bucket = bucket;
assert(bucket<M);
if(ht[bucket] == NULL) ht[bucket] = to_insert , to_insert->bucket_pos = 0;
else
{
SymbolInfo* now = ht[bucket];
if(now->key == key) return NULL; /*already exists*/
int bucket_pos = 1;
while(now->nxt != NULL)
{
if(now->key == key)
return NULL; /*already exists*/
now = now->nxt;
bucket_pos++;
}
to_insert->bucket_pos = bucket_pos;
now->nxt = to_insert;
}
return to_insert;
}
bool ScopeTable::erase(string key)
{
int idx = hash(key);
SymbolInfo* prv = NULL;
SymbolInfo* now = ht[idx];
while(now != NULL)
{
if(now->key == key)
{
if(prv) prv->nxt = now->nxt;
else ht[idx] = now->nxt;
delete (now);
return true;
}
prv = now;
now = now->nxt;
}
return false;
}
void ScopeTable::print(ofstream &out)
{
out<<"ScopeTable # "<<id<<endl;
for(int i=0; i<M; i++)
{
SymbolInfo* now = ht[i];
if(now == NULL) continue;
out<<i<<" --> ";
while(now != NULL)
{
out<<"< "<<now->key <<" , "<<now->val<<" > ";
// cout<<"< "<<now->key <<" , "<<now->val<<" > ("<<now->var_type<<")";
now = now->nxt;
}
out<<endl;
}
out<<endl;
}
void ScopeTable::printChainLengths()
{
for(int i=0; i<M; i++)
{
int cnt = 0;
SymbolInfo* now = ht[i];
while(now != NULL) now = now->nxt, cnt++;
cout<<endl;
cout<<"Chain "<<i<<" : "<<cnt<<endl;
}
}