TS_CPP
An efficient & easy to use implementation of a Symbol table used in most compilers
Loading...
Searching...
No Matches
SymbolTable.cpp
1#include "TSConfig.h"
2#include "SymbolTable.h"
3
4using namespace TS_CPP;
5
6SymbolTable::SymbolTable(int id, int* entryId)
7{
8 this->id = id;
9 this->tableName = "";
10 this->entryId = entryId;
11}
12
13SymbolTable::SymbolTable(const int id, int* entryId, const std::string& tableName)
14{
15 this->id = id;
16 this->tableName = tableName;
17 this->entryId = entryId;
18}
19
21{
22 return id;
23}
24
25std::string SymbolTable::getTableName() const
26{
27 return tableName;
28}
29
30Entry* SymbolTable::createNewEntry(const std::string& lexeme)
31{
32 if(lexemeToPos.find(lexeme) == lexemeToPos.end()) // if entry does not exist
33 {
34 int newEntryID = ++(*entryId);
35 lexemeToPos[lexeme] = (int)storage.size();
36 idToPos[newEntryID] = (int)storage.size();
37 auto* entry = new Entry(this, lexeme, newEntryID);
38 entry->_setDisplacement(displacement);
39 storage.push_back(entry);
40 tsinfo("Created new Entry for table with id=%d (%s) with entryId=%d, lexeme=%s",id,tableName.c_str(),entry->getId(),entry->getLexeme().c_str());
41 return entry;
42 }
43 else // if entry exists
44 {
45 Entry* entry = storage[lexemeToPos[lexeme]];
46 tserror("Cannot create Entry; it already exist for table with id=%d (%s) with entryId=%d, lexeme=%s",id,tableName.c_str(),entry->getId(),entry->getLexeme().c_str());
47 return entry;
48 }
49}
50
51Entry *SymbolTable::getEntry(const int entryId)
52{
53 if(idToPos.find(entryId) == idToPos.end()) // if entry does not exist
54 {
55 tsinfo("Entry with id=%d does not exist in table with id=%d and name='%s'",entryId, id, tableName.c_str());
56 return nullptr;
57 }
58 else
59 {
60 Entry* entry = storage[idToPos[entryId]];
61 tsinfo("Entry with id=%d, lexeme='%s' found in table with id=%d and name='%s'",entry->getId(), entry->getLexeme().c_str(), id, tableName.c_str());
62 return entry;
63 }
64}
65
66Entry *SymbolTable::searchEntry(const std::string& lexeme)
67{
68 if(lexemeToPos.find(lexeme) == lexemeToPos.end()) // if entry does not exist
69 {
70 tsinfo("Entry with lexeme='%s' does not exist in table with id=%d and name='%s'",lexeme.c_str(), id, tableName.c_str());
71 return nullptr;
72 }
73 else
74 {
75 Entry* entry = storage[lexemeToPos[lexeme]];
76 tsinfo("Entry with id=%d, lexeme='%s' found in table with id=%d and name='%s'",entry->getId(), entry->getLexeme().c_str(), id, tableName.c_str());
77 return entry;
78 }
79}
80
81bool SymbolTable::deleteEntry(const std::string& lexeme)
82{
83 if(lexemeToPos.find(lexeme) == lexemeToPos.end()) // if entry does not exist
84 {
85 tserror("Entry with lexeme='%s' does not exist", lexeme.c_str());
86 return false;
87 }
88 else
89 {
90 return deleteEntryByPos(lexemeToPos[lexeme]);
91 }
92}
93
94bool SymbolTable::deleteEntry(int entryId)
95{
96 if(idToPos.find(entryId) == idToPos.end()) // if entry does not exist
97 {
98 tserror("Entry with id='%d' does not exist", entryId);
99 return false;
100 }
101 else
102 {
103 return deleteEntryByPos(idToPos[entryId]);
104 }
105}
106
107bool SymbolTable::deleteEntryByPos(int pos)
108{
109 if(pos < 0 || pos >= storage.size())
110 {
111 return false;
112 }
113 else
114 {
115 Entry* entry = storage[pos];
116 entry->_markAsDeleted();
117 const std::string lexeme = entry->getLexeme();
118 int eid = entry->getId();
119 lexemeToPos.erase(lexeme);
120 idToPos.erase(eid);
121 tsinfo("Entry with id=%d and lexeme=%s successfully deleted",eid,lexeme.c_str());
122 return true;
123 }
124}
125
127{
128 return tableEntriesSize;
129}
130
132{
133 tableEntriesSize += size;
134 displacement += size;
135}
136
137AttributeValue* SymbolTable::getAttribute(const std::string& attributeName)
138{
139 if(attributes.find(attributeName) == attributes.end())
140 {
141 tsinfo("Attribute of entry %d (%s) with name=%s not found",id,lexeme.c_str(),attributeName.c_str());
142 return nullptr;
143 }
144 else
145 {
146 tsdebug("Attribute of entry %d (%s) with name=%s found",id,lexeme.c_str(),attributeName.c_str());
147 return attributes[attributeName];
148 }
149}
150
151void SymbolTable::setNumericAttribute(const std::string &attributeName, long long value)
152{
153 if(attributes.find(attributeName) == attributes.end())
154 {
155 tsinfo("Setting new attribute of entry %d (%s) with name=%s",id,lexeme.c_str(),attributeName.c_str());
156 }
157 else
158 {
159 tsinfo("Attribute of entry %d (%s) with name=%s already exist. Overwriting previous value...",id,lexeme.c_str(),attributeName.c_str());
160 delete attributes[attributeName];
161 }
162
163 attributes[attributeName] = new AttributeValue(value);
164}
165
166void SymbolTable::setStringAttribute(const std::string &attributeName, const char *value)
167{
168 if(attributes.find(attributeName) == attributes.end())
169 {
170 tsinfo("Setting new attribute of entry %d (%s) with name=%s & value=%s",id,lexeme.c_str(),attributeName.c_str(),value);
171 }
172 else
173 {
174 tsinfo("Attribute of entry %d (%s) with name=%s already exist. Overwriting previous value with the new one (%s)",id,lexeme.c_str(),attributeName.c_str(),value);
175 delete attributes[attributeName];
176 }
177
178 attributes[attributeName] = new AttributeValue(value);
179}
180
181
183{
184 for(auto entry : storage)
185 {
186 delete entry;
187 }
188}
189
190std::vector<Entry *> &SymbolTable::_getEntriesStorage()
191{
192 return storage;
193}
194
196{
197 return displacement;
198}
Definition Entry.h:19
int getId() const
Definition Entry.cpp:21
void _markAsDeleted()
Definition Entry.cpp:112
std::string getLexeme() const
Definition Entry.cpp:16
std::vector< Entry * > & _getEntriesStorage()
int _getDisplacement() const
AttributeValue * getAttribute(const std::string &attributeName)
int getTableEntriesSize() const
Entry * createNewEntry(const std::string &lexeme)
void _addTableEntriesSize(int size)
void setNumericAttribute(const std::string &attributeName, long long value)
SymbolTable(int id, int *entryId, const std::string &tableName)
bool deleteEntry(const std::string &lexeme)
Entry * searchEntry(const std::string &lexeme)
void setStringAttribute(const std::string &attributeName, const char *value)
Entry * getEntry(int entryId)
std::string getTableName() const