|
/**
|
|
*
|
|
* OOAS Compiler - C++ AST
|
|
*
|
|
* Copyright 2015, AIT Austrian Institute of Technology.
|
|
* All rights reserved.
|
|
*
|
|
* SEE THE "LICENSE" FILE FOR THE TERMS UNDER WHICH THIS FILE IS PROVIDED.
|
|
*
|
|
* If you modify the file please update the list of contributors below to in-
|
|
* clude your name. Please also stick to the coding convention of using TABs
|
|
* to do the basic (block-level) indentation and spaces for anything after
|
|
* that. (Enable the display of special chars and it should be pretty obvious
|
|
* what this means.) Also, remove all trailing whitespace.
|
|
*
|
|
* Contributors:
|
|
* Willibald Krenn (AIT)
|
|
* Stephan Zimmerer (AIT)
|
|
* Christoph Czurda (AIT)
|
|
*
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
* SymbolTable.cpp
|
|
*
|
|
* Created on: Dec 23, 2013
|
|
* Author: willibald
|
|
*/
|
|
|
|
#include "SymbolTable.hpp"
|
|
|
|
namespace Ast {
|
|
|
|
bool SymbolTable::addIdentifier(Identifier* anIdent) {
|
|
if (anIdent != nullptr) {
|
|
std::pair<std::string,Identifier*> toInsert(anIdent->tokenText(),anIdent);
|
|
auto pair = m_symbols.insert(toInsert);
|
|
if (!pair.second) {
|
|
throw new Base::UnsupportedOperationException(
|
|
boost::format("Identifier '%s' already exists") % anIdent->tokenText());
|
|
}
|
|
m_symbols[anIdent->tokenText()] = anIdent;
|
|
m_identifiers.insert(anIdent);
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
//Binary predicate that, taking two values of the same type of those contained in the list,
|
|
//returns true if the first argument goes before the second argument in the strict weak ordering it defines, and false otherwise.
|
|
bool symbolSort(Identifier*& a, Identifier*& b) {
|
|
return a->tokenText() < b->tokenText();
|
|
}
|
|
|
|
void SymbolTable::setSymbolList(std::list<Identifier*>& newList) {
|
|
newList.sort(&symbolSort);
|
|
m_symbols.clear();
|
|
m_identifiers.clear();
|
|
for (auto& s: newList)
|
|
addIdentifier(s);
|
|
}
|
|
|
|
Identifier* SymbolTable::get(const char* aKey) {
|
|
auto iter = m_symbols.find(aKey);
|
|
return iter == m_symbols.end() ? nullptr : iter->second;
|
|
}
|
|
|
|
|
|
}
|