TS_CPP
An efficient & easy to use implementation of a Symbol table used in most compilers
Loading...
Searching...
No Matches
Entry.cpp
1#include "TSConfig.h"
2#include "Entry.h"
3#include "SymbolTable.h"
4
5using namespace TS_CPP;
6
7Entry::Entry(SymbolTable* parentTable, const std::string& lexeme, int id)
8{
9 this->parentTable = parentTable;
10 this->lexeme = lexeme;
11 this->id = id;
12 this->haveSize = false;
13 this->isDeleted = false;
14}
15
16std::string Entry::getLexeme() const
17{
18 return lexeme;
19}
20
21int Entry::getId() const
22{
23 return id;
24}
25
26AttributeValue* Entry::getAttribute(const std::string& attributeName)
27{
28 if(attributes.find(attributeName) == attributes.end())
29 {
30 tsinfo("Attribute of entry %d (%s) with name=%s not found",id,lexeme.c_str(),attributeName.c_str());
31 return nullptr;
32 }
33 else
34 {
35 tsdebug("Attribute of entry %d (%s) with name=%s found",id,lexeme.c_str(),attributeName.c_str());
36 return attributes[attributeName];
37 }
38}
39
40void Entry::setNumericAttribute(const std::string &attributeName, long long value)
41{
42 if(attributes.find(attributeName) == attributes.end())
43 {
44 tsinfo("Setting new attribute of entry %d (%s) with name=%s",id,lexeme.c_str(),attributeName.c_str());
45 }
46 else
47 {
48 tsinfo("Attribute of entry %d (%s) with name=%s already exist. Overwriting previous value...",id,lexeme.c_str(),attributeName.c_str());
49 delete attributes[attributeName];
50 }
51
52 attributes[attributeName] = new AttributeValue(value);
53}
54
55void Entry::setStringAttribute(const std::string &attributeName, const char *value)
56{
57 if(attributes.find(attributeName) == attributes.end())
58 {
59 tsinfo("Setting new attribute of entry %d (%s) with name=%s & value=%s",id,lexeme.c_str(),attributeName.c_str(),value);
60 }
61 else
62 {
63 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);
64 delete attributes[attributeName];
65 }
66
67 attributes[attributeName] = new AttributeValue(value);
68}
69
70bool Entry::deleteAttribute(const std::string &attributeName)
71{
72 if(attributes.find(attributeName) == attributes.end()) // if entry does not exist
73 {
74 tserror("Attribute of entry %d (%s) with name=%s does not exist",id,lexeme.c_str(), attributeName.c_str());
75 return false;
76 }
77 else
78 {
79 attributes.erase(attributeName);
80 tsinfo("Attribute of entry %d (%s) with name=%s successfully deleted",id,lexeme.c_str(), attributeName.c_str());
81 return true;
82 }
83}
84
85std::unordered_map<std::string, AttributeValue*> &Entry::_getAttributesStorage()
86{
87 return attributes;
88}
89
90void Entry::_setDisplacement(int displacement)
91{
92 setNumericAttribute(TS_DISPLACEMENT_ATTR_NAME,displacement);
93}
94
95void Entry::setSize(int size)
96{
97 setNumericAttribute(TS_SIZE_ATTR_NAME,size);
98 parentTable->_addTableEntriesSize(size);
99 this->haveSize = true;
100}
101
103{
104 return getAttribute(TS_DISPLACEMENT_ATTR_NAME)->getValue().second.integer;
105}
106
108{
109 return this->haveSize;
110}
111
113{
114 this->isDeleted = true;
115}
116
118{
119 return this->isDeleted;
120}
std::pair< AttributeType, DataUnion > getValue()
void _setDisplacement(int displacement)
Definition Entry.cpp:90
bool getHaveSize() const
Definition Entry.cpp:107
AttributeValue * getAttribute(const std::string &attributeName)
Definition Entry.cpp:26
void setStringAttribute(const std::string &attributeName, const char *value)
Definition Entry.cpp:55
int getId() const
Definition Entry.cpp:21
void setNumericAttribute(const std::string &attributeName, long long value)
Definition Entry.cpp:40
bool deleteAttribute(const std::string &attributeName)
Definition Entry.cpp:70
Entry(SymbolTable *parentTable, const std::string &lexeme, int id)
Definition Entry.cpp:7
void _markAsDeleted()
Definition Entry.cpp:112
bool _isDeleted() const
Definition Entry.cpp:117
long long getDisplacement()
Definition Entry.cpp:102
std::string getLexeme() const
Definition Entry.cpp:16
void setSize(int size)
Definition Entry.cpp:95
std::unordered_map< std::string, AttributeValue * > & _getAttributesStorage()
Definition Entry.cpp:85
void _addTableEntriesSize(int size)