-
Notifications
You must be signed in to change notification settings - Fork 2
/
SymbolTable.cpp
133 lines (105 loc) · 2.85 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
/* Which of the favors of your Lord will you deny ? */
#include<bits/stdc++.h>
#include "ScopeTable.h"
#include "SymbolTable.h"
using namespace std;
SymbolTable::~SymbolTable()
{
// cout<<"SymbolTable memory deallocated"<<endl;
// cout<<endl;
while(cur != NULL)
{
ScopeTable* tmp = cur->parentScope;
delete cur;
cur = tmp;
}
}
void SymbolTable::enter_scope() /// enter scope = push : create and push a new ScopeTable
{
ScopeTable* st = new ScopeTable(bucket_size,func);
st->parentScope = cur;
cur = st;
if(st->parentScope != NULL)
{
st->set_id(st->parentScope->get_id()+"."+to_str(st->parentScope->get_counter()));
}
cout<<"New ScopeTable with id "<<st->get_id()<<" created"<<endl;
cout<<endl;
}
void SymbolTable::exit_scope() /// exit scope = pop : remove the current ScopeTable
{
if(cur != NULL)
{
cout<<"ScopeTable with id "<<getCurScopeTableId()<<" removed"<<endl;
cout<<endl;
ScopeTable* temp = cur;
cur = cur->parentScope;
cur->increase_counter();
delete temp;
}
}
bool SymbolTable::insert_symbol(SymbolInfo si,ofstream &out)
{
SymbolInfo* temp = cur->search(si.key);
if(temp != NULL) /// cant insert
{
out<<temp->key<<" already exists in current ScopeTable\n"<<endl;
return false;
}
SymbolInfo* ret = cur->insert(si);
cout<<"Inserted in ScopeTable# "<<getCurScopeTableId()<<" at position "<<ret->bucket<<","<<ret->bucket_pos<<endl;
cout<<endl;
return ret!=NULL ;
}
bool SymbolTable::remove_symbol(string key)
{
SymbolInfo* temp = cur->search(key);
if(temp != NULL) /// present , so can delete
{
cout<<"Found in ScopeTable# "<<getCurScopeTableId()<<" at position "<<temp->bucket<<","<<temp->bucket_pos<<endl;
cout<<"Deleted Entry "<<temp->bucket<<","<<temp->bucket_pos<<" from current ScopeTable"<<endl;
cout<<endl;
return cur->erase(key);
}
else
{
cout<<"Not found"<<endl;
cout<<key<<" not found"<<endl;
cout<<endl;
return false;
}
}
SymbolInfo* SymbolTable::lookup(string key)
{
ScopeTable* now = cur;
while(now != NULL)
{
SymbolInfo* ret = now->search(key);
if(ret != NULL)
{
cout<<"Found in ScopeTable# "<<now->get_id()<<" at position "<<ret->bucket<<","<<ret->bucket_pos<<endl;
cout<<endl;
return ret;
}
now = now->parentScope;
}
return NULL; /// not found
}
void SymbolTable::print_current_scope(ofstream &out)
{
cur->print(out);
}
void SymbolTable::print_all_scope(ofstream &out)
{
ScopeTable* now = cur;
while(now != NULL)
{
now->print(out);
now = now->parentScope;
}
// out<<endl;
}
string SymbolTable::getCurScopeTableId()
{
return cur->get_id();
}