Website Summary Files Commits Branches @commit @diff @stats
path: root / spasm / src / asm / symbol.cpp
diff options
context:
space:
mode:
Commitc3252e63c296d1133254f4973fe7e3bf52cc6c1a (patch)
Author
 Dimitar Trendafilov <dimitar@coherent-labs.com>
2018-11-05 23:34:00 +0200
Committer
 Dimitar Trendafilov <dimitar@coherent-labs.com>
2018-11-05 23:34:00 +0200
Files67c6bfc067696562aded70f66d25899f3f70a1c8 /spasm/src/asm/symbol.cpp
IPL-c3252e63c296d1133254f4973fe7e3bf52cc6c1a.tar
IPL-c3252e63c296d1133254f4973fe7e3bf52cc6c1a.tar.gz
IPL-c3252e63c296d1133254f4973fe7e3bf52cc6c1a.zip
Parentc9912fdb174e1580629e00cacab6bc4ed480e643 (diff)
Parent6e19bb560d474736d45f5be011ff0830c7019991 (diff)
Merge commit '6e19bb560d474736d45f5be011ff0830c7019991' as 'spasm'
1 files changed, 120 insertions, 0 deletions
-rw-r--r--spasm/src/asm/symbol.cpp120
diff --git a/spasm/src/asm/symbol.cpp b/spasm/src/asm/symbol.cpp
new file mode 100644
index 0000000..7964142
--- /dev/null
+++ b/spasm/src/asm/symbol.cpp
@@ -0,0 +1,120 @@
+#include <list>
+#include <string>
+
+#include "symbol.hpp"
+
+namespace SpasmImpl
+{
+namespace ASM
+{
+const size_t Symbol::notdefined = ~0ull;
+
+Symbol::Symbol() : _definition(notdefined) {}
+
+Symbol::Symbol(const std::string& identifier, size_t definition)
+ : _identifier(identifier), _definition(definition)
+{
+}
+
+const std::string& Symbol::identifier() const
+{
+ return _identifier;
+}
+
+size_t Symbol::definition() const
+{
+ return _definition;
+}
+
+void Symbol::define(size_t definition)
+{
+ _definition = definition;
+}
+
+size_t Symbol::add_position(size_t position)
+{
+ _positions.push_back(position);
+
+ return _definition;
+}
+
+Symbol::Positions_t::const_iterator Symbol::positions_begin() const
+{
+ return _positions.begin();
+}
+
+Symbol::Positions_t::const_iterator Symbol::positions_end() const
+{
+ return _positions.end();
+}
+
+Symbol_Table::Symbol_Table() {}
+
+/*!
+** Search the table for symbol by identifier
+**
+** \param identifier - identifier of the symbol
+**
+** \return pointer to the symbol if it exists or NULL otherwise.
+*/
+Symbol* Symbol_Table::find(const std::string& identifier) const
+{
+ const_iterator i = _table.find(identifier);
+ return (i != _table.end()) ? i->second : NULL;
+}
+
+/*!
+** Insert symbol with identifier indentifier on position position
+** of just add new position for symbol.
+** \param identifier - identifer of the symbol
+** \param position - position of the symbol
+**
+** \return pointer to the symbol
+*/
+Symbol* Symbol_Table::insert(const std::string& identifier, size_t position)
+{
+ Symbol* psymbol = find(identifier);
+ if (psymbol == NULL)
+ {
+ psymbol = new Symbol(identifier);
+ _table.insert(std::make_pair(identifier, psymbol));
+ }
+ psymbol->add_position(position);
+
+ return psymbol;
+}
+
+/*!
+** define symbol with identifier indentifier
+** If the symbol already exists its definition is changed
+** \param identifier - identifer of the symbol
+** \param definition - definition of the symbol
+**
+** \return pointer to the symbol
+*/
+Symbol* Symbol_Table::define(const std::string& identifier, size_t definition)
+{
+ Symbol* psymbol = find(identifier);
+ if (psymbol == NULL)
+ {
+ psymbol = new Symbol(identifier, definition);
+ _table.insert(std::make_pair(identifier, psymbol));
+ }
+ else
+ psymbol->define(definition);
+
+ return psymbol;
+}
+
+Symbol_Table::const_iterator Symbol_Table::begin() const
+{
+ return _table.begin();
+}
+
+Symbol_Table::const_iterator Symbol_Table::end() const
+{
+ return _table.end();
+}
+
+} // namespace ASM
+} // namespace SpasmImpl