Project

General

Profile

root / trunk / compiler / cppAst / ast / SymbolTable.cpp @ 3

1
/**
2
  *
3
  *                  OOAS Compiler - C++ AST
4
  *
5
  * Copyright 2015, AIT Austrian Institute of Technology.
6
  * All rights reserved.
7
  *
8
  * SEE THE "LICENSE" FILE FOR THE TERMS UNDER WHICH THIS FILE IS PROVIDED.
9
  *
10
  * If you modify the file please update the list of contributors below to in-
11
  * clude your name. Please also stick to the coding convention of using TABs
12
  * to do the basic (block-level) indentation and spaces for anything after
13
  * that. (Enable the display of special chars and it should be pretty obvious
14
  * what this means.) Also, remove all trailing whitespace.
15
  *
16
  * Contributors:
17
  *               Willibald Krenn (AIT)
18
  *               Stephan Zimmerer (AIT)
19
  *               Christoph Czurda (AIT)
20
  *
21
  */
22

    
23

    
24

    
25
/*
26
 * SymbolTable.cpp
27
 *
28
 *  Created on: Dec 23, 2013
29
 *      Author: willibald
30
 */
31

    
32
#include "SymbolTable.hpp"
33

    
34
namespace Ast {
35

    
36
bool SymbolTable::addIdentifier(Identifier* anIdent) {
37
        if (anIdent != nullptr) {
38
                std::pair<std::string,Identifier*> toInsert(anIdent->tokenText(),anIdent);
39
                auto pair = m_symbols.insert(toInsert);
40
                if (!pair.second) {
41
                        throw new Base::UnsupportedOperationException(
42
                                boost::format("Identifier '%s' already exists") % anIdent->tokenText());
43
                }
44
                m_symbols[anIdent->tokenText()] = anIdent;
45
                m_identifiers.insert(anIdent);
46
                return true;
47
        }
48
        return false;
49
}
50

    
51
//Binary predicate that, taking two values of the same type of those contained in the list,
52
//returns true if the first argument goes before the second argument in the strict weak ordering it defines, and false otherwise.
53
bool symbolSort(Identifier*& a, Identifier*& b) {
54
        return a->tokenText() < b->tokenText();
55
}
56

    
57
void SymbolTable::setSymbolList(std::list<Identifier*>& newList) {
58
        newList.sort(&symbolSort);
59
        m_symbols.clear();
60
        m_identifiers.clear();
61
        for (auto& s: newList)
62
                addIdentifier(s);
63
}
64

    
65
Identifier* SymbolTable::get(const char* aKey) {
66
        auto iter = m_symbols.find(aKey);
67
        return iter == m_symbols.end() ? nullptr : iter->second;
68
}
69

    
70

    
71
}