-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsymbtable.cc
142 lines (113 loc) · 2.23 KB
/
symbtable.cc
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
#include <string>
#include <list>
#include "symbtable.h"
using namespace std;
Symbol::Symbol()
: nam(), typ(TY_BAD)
{}
Symbol::Symbol(const string &name, type_t type, int address)
: nam(name), typ(type), addr(address)
{}
const string &Symbol::name() const
{
return nam;
}
type_t &Symbol::type()
{
return typ;
}
type_t Symbol::type() const
{
return typ;
}
int &Symbol::address()
{
return addr;
}
int Symbol::address() const
{
return addr;
}
bool Symbol::operator<(const Symbol &s) const
{
return nam < s.nam;
}
bool Symbol::operator==(const Symbol &s) const
{
return nam == s.nam;
}
bool Symbol::operator!=(const Symbol &s) const
{
return nam != s.nam;
}
SymbolTable::SymbolTable()
: hashes(1) // one (empty) hash table
{}
SymbolTable::SymbolTable(const SymbolTable &st)
: hashes(st.hashes)
{}
SymbolTable &SymbolTable::operator=(const SymbolTable &st)
{
if(this != &st)
hashes = st.hashes;
return *this;
}
Symbol *SymbolTable::operator[](const string &s)
{
for(symtabint::iterator i=hashes.begin(); i != hashes.end(); ++i) {
symtabsingle::iterator pos = i->find(s);
if (pos != i->end())
return &(pos->second);
}
return 0;
}
const Symbol *SymbolTable::operator[](const string &s) const
{
for(symtabint::const_iterator i=hashes.begin(); i != hashes.end(); ++i) {
symtabsingle::const_iterator pos = i->find(s);
if (pos != i->end())
return &(pos->second);
}
return 0;
}
bool SymbolTable::insert(Symbol s)
{
hashes.front().insert(make_pair(s.name(), s));
return true;
}
void SymbolTable::enter()
{
hashes.push_front(symtabsingle());
}
void SymbolTable::leave()
{
// We should not pop off the first element
if(hashes.size() > 1)
hashes.pop_front();
}
bool SymbolTable::exists(const string &str) const
{
for(symtabint::const_iterator i=hashes.begin(); i!=hashes.end(); ++i)
if(i->count(str))
return true;
return false;
}
int SymbolTable::numvars() const
{
return hashes.front().size();
}
int SymbolTable::levelof(const string &str) const
{
symtabint::const_iterator i;
int j = 0;
for(i=hashes.begin(); i != hashes.end(); ++i, ++j)
if(i->count(str))
return j;
return -1;
}
//void SymbolTable::setNumArgs( int num ) {
// numarg = num;
//}
//int SymbolTable::numargs() {
// return numarg;
//}