summaryrefslogtreecommitdiff
path: root/VariableList.cc
blob: eef1205d503b433ccaa0f8baf229efe4f5a472c7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include "VariableList.hh"
#include "Log.hh"

namespace pcc {
VariableList::VariableList(VariableList* parent)
    : parent_(parent) {}

Variable VariableList::FindVariable(const std::string& name) {
    bool found = false;
    VariableList* level = this;
    decltype(variables_)::iterator result;
    while (level != nullptr) {
        result = level->variables_.find(name);
        if (result != level->variables_.end()) {
            found = true;
            break;
        } else {
            level = level->parent_;
        }
    }
    if (found == false) {
        Log(LogLevel::PCC_ERROR, "unknown identifier %s", name.c_str());
        return {nullptr, nullptr};
    }
    return std::get<1>(*result);
}

void VariableList::AddVariable(const std::string& name, Variable variable) {
    variables_.insert({name, variable});
}

std::shared_ptr<Type> VariableList::FindTypeAlias(const std::string& name) {
    bool found = false;
    VariableList* level = this;
    decltype(TypeAlias_)::iterator result;
    while (level != nullptr) {
        result = level->TypeAlias_.find(name);
        if (result != level->TypeAlias_.end()) {
            found = true;
            break;
        } else {
            level = level->parent_;
        }
    }
    if (found == false) {
        Log(LogLevel::PCC_ERROR, "unknown type alias %s", name.c_str());
        return nullptr;
    }
    return std::get<1>(*result);
}

void VariableList::AddTypeAlias(const std::string& name,
                                std::shared_ptr<Type> type) {
    TypeAlias_.insert({name, type});
}

} // namespace pcc