-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsymboltable.cpp
executable file
·211 lines (179 loc) · 4.93 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
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#define _CRT_SECURE_NO_WARNINGS
#include <assert.h>
#include <list>
#include <map>
#include <string>
#include "symboltable.h"
//
//
//
static const char *_typeStrings[] =
{
"undef",
"enum",
"string literal",
"float",
"int",
"char",
"define",
"user-defined"
};
// ensure that the array matches the enumeration
static_assert(ARRAY_SIZE(_typeStrings) == stNumSymbolTypes, "array size mismatch");
//
//
//
SymbolTable::SymbolTable()
{
// add the first level to the table
m_symbolTable.push_back(SymbolMap());
}
//======================================================================
// Add another depth level to the symbol table
//======================================================================
void SymbolTable::push()
{
m_symbolTable.push_back(SymbolMap());
}
//======================================================================
// Pop a level off the symbol table stack
//======================================================================
void SymbolTable::pop()
{
m_symbolTable.pop_back();
// ensure that we don't underflow the stack!
assert(m_symbolTable.size() >= 0);
}
//
//
//
SymbolEntry *SymbolTable::getFirstGlobal()
{
stack_iterator iter = m_symbolTable.begin();
m_globalIter = (*iter).begin();
return &m_globalIter->second;
}
//
//
//
SymbolEntry *SymbolTable::getNextGlobal()
{
m_globalIter++;
stack_iterator iter = begin_stack();
if (m_globalIter == (*iter).end())
return nullptr;
return &m_globalIter->second;
}
//
//
//
const char *SymbolTable::getTypeName(SymbolType st)
{
assert(st < stNumSymbolTypes);
return _typeStrings[st];
}
//
void SymbolTable::dumpContents()
{
char szText[256];
SymbolMap::iterator iter;
SymbolStack::reverse_iterator riter = m_symbolTable.rbegin();
// for each level of table
for (; riter != m_symbolTable.rend(); riter++)
{
iter = (*riter).begin();
for (; iter != (*riter).end(); iter++)
{
if (iter->second.type == stInteger)
sprintf(szText, "%s\t(%u, 0x%08X)\n", iter->first.c_str(), iter->second.ival, iter->second.ival);
else
sprintf(szText, "%s\t%f\n", iter->first.c_str(), iter->second.fval);
puts(szText);
}
}
}
//===============================================================
// Look for a symbol in the nested symbol stack
//===============================================================
SymbolEntry *SymbolTable::lookup(const char *lexeme)
{
SymbolMap::iterator iter;
SymbolStack::reverse_iterator riter = m_symbolTable.rbegin();
for (; riter != m_symbolTable.rend(); riter++)
{
// if we are done with this level, search next highest level
iter = (*riter).find(lexeme);
if (iter == (*riter).end())
continue;
return &(iter->second);
}
// symbol was not found anywhere in the table
return nullptr;
}
//===============================================================
// Look for a symbol in the nested symbol stack
//===============================================================
SymbolEntry *SymbolTable::reverse_lookup(int ival)
{
SymbolMap::iterator iter;
SymbolStack::reverse_iterator riter = m_symbolTable.rbegin();
for (; riter != m_symbolTable.rend(); riter++)
{
iter = (*riter).begin();
for (; iter != (*riter).end(); iter++)
{
if (iter->second.ival == ival)
return &(iter->second);
}
}
// symbol was not found anywhere in the table
return nullptr;
}
//======================================================================
// Install given lexeme in the symbol table at the current level.
// Duplicates are not allowed.
//======================================================================
SymbolEntry *SymbolTable::install(const char *lexeme, SymbolType type)
{
SymbolMap ¤tMap = m_symbolTable.back();
// see if already in table
SymbolEntry se;
se.type = type;
se.lexeme = lexeme;
std::pair<SymbolMap::iterator, bool> result = currentMap.insert(SymbolMap::value_type(lexeme, se));
// if symbol already exist in the table at this level, validate it
if (!result.second)
assert(result.first->second.type == type);
return &(result.first->second);
}
const char *SymbolTable::getTypeString(int type)
{
if (type <= stUser)
return _typeStrings[type];
return _typeStrings[stUser];
}
//
int SymbolTable::dumpUnreferencedSymbolsAtCurrentLevel()
{
SymbolMap::iterator iter;
SymbolStack::reverse_iterator current_level_riter = m_symbolTable.rbegin();
SymbolEntry *pSymbol;
int count = 0;
// foreach symbol at the current top of stack
iter = (*current_level_riter).begin();
for (; iter != (*current_level_riter).end(); iter++)
{
pSymbol = &(iter->second);
if (!pSymbol->isReferenced)
{
count++;
printf("%s(%d) : warning: %s '%s' not referenced.\n",
pSymbol->srcFile.c_str(),
pSymbol->srcLine,
getTypeString(pSymbol->type),
pSymbol->lexeme.c_str()
);
}
}
return count;
}