summaryrefslogtreecommitdiff
path: root/VariableList.cc
blob: 5bf03737517fc92a783e914c1ce6d7424e5b0835 (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
#include "VariableList.hh"
#include "Log.hh"

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

Variable VariableList::Find(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::Add(std::string name, Variable variable) {
    variables_.insert({name, variable});
}
} // namespace pcc