2#include "SymbolTable.h"
10 this->entryId = entryId;
16 this->tableName = tableName;
17 this->entryId = entryId;
32 if(lexemeToPos.find(lexeme) == lexemeToPos.end())
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());
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());
53 if(idToPos.find(entryId) == idToPos.end())
55 tsinfo(
"Entry with id=%d does not exist in table with id=%d and name='%s'",entryId,
id, tableName.c_str());
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());
68 if(lexemeToPos.find(lexeme) == lexemeToPos.end())
70 tsinfo(
"Entry with lexeme='%s' does not exist in table with id=%d and name='%s'",lexeme.c_str(),
id, tableName.c_str());
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());
83 if(lexemeToPos.find(lexeme) == lexemeToPos.end())
85 tserror(
"Entry with lexeme='%s' does not exist", lexeme.c_str());
90 return deleteEntryByPos(lexemeToPos[lexeme]);
96 if(idToPos.find(entryId) == idToPos.end())
98 tserror(
"Entry with id='%d' does not exist", entryId);
103 return deleteEntryByPos(idToPos[entryId]);
107bool SymbolTable::deleteEntryByPos(
int pos)
109 if(pos < 0 || pos >= storage.size())
115 Entry* entry = storage[pos];
117 const std::string lexeme = entry->
getLexeme();
118 int eid = entry->
getId();
119 lexemeToPos.erase(lexeme);
121 tsinfo(
"Entry with id=%d and lexeme=%s successfully deleted",eid,lexeme.c_str());
128 return tableEntriesSize;
133 tableEntriesSize += size;
134 displacement += size;
139 if(attributes.find(attributeName) == attributes.end())
141 tsinfo(
"Attribute of entry %d (%s) with name=%s not found",
id,lexeme.c_str(),attributeName.c_str());
146 tsdebug(
"Attribute of entry %d (%s) with name=%s found",
id,lexeme.c_str(),attributeName.c_str());
147 return attributes[attributeName];
153 if(attributes.find(attributeName) == attributes.end())
155 tsinfo(
"Setting new attribute of entry %d (%s) with name=%s",
id,lexeme.c_str(),attributeName.c_str());
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];
168 if(attributes.find(attributeName) == attributes.end())
170 tsinfo(
"Setting new attribute of entry %d (%s) with name=%s & value=%s",
id,lexeme.c_str(),attributeName.c_str(),value);
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];
184 for(
auto entry : storage)
std::string getLexeme() const
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