lang_2023/include/contexts.hpp
ProgramSnail 3fca384446 fixes
2023-05-07 20:21:19 +03:00

139 lines
3.2 KiB
C++

#pragma once
#include <string>
#include <utility>
#include <vector>
#include <unordered_map>
// for clangd
#include "values.hpp"
#include "utils.hpp"
namespace info {
class ContextManager {
public:
ContextManager() {
contexts_.emplace_back();
}
template<typename T>
utils::IdType AddValue(const T& value, utils::ValueType value_type) {
return value_manager_.AddValue(value, value_type);
}
utils::IdType AddAnyValue(value::Value&& value, utils::ValueType value_type) {
return value_manager_.AddAnyValue(std::move(value), value_type);
}
template<typename T>
std::optional<T*> GetValue(utils::IdType value_id) {
return value_manager_.GetValue<T>(value_id);
}
value::Value* GetAnyValue(utils::IdType value_id) {
return value_manager_.GetAnyValue(value_id);
}
utils::ValueType GetValueType(utils::IdType value_id) {
return value_manager_.GetValueType(value_id);
}
utils::IdType ToModifiedValue(utils::IdType value_id, utils::ValueType new_value_type) {
value::Value value = *GetAnyValue(value_id);
return AddAnyValue(std::move(value), new_value_type);
}
value::ValueManager* GetValueManager() {
return &value_manager_;
}
void EnterContext() {
contexts_.emplace_back();
}
void ExitContext() {
if (contexts_.empty()) {
// error
}
contexts_.pop_back();
}
void ExitFromAllContexts() {
contexts_.clear();
contexts_.emplace_back();
}
bool DefineVariable(const std::string& name, utils::IdType value_id) {
// check in previous contexts ??
return contexts_.back().DefineVariable(name, value_id);
}
bool RemoveVariable(const std::string& name) {
for (ssize_t i = (ssize_t)contexts_.size() - 1; i >= 0; --i) {
if (contexts_[i].RemoveVariable(name)) {
return true;
}
}
return false;
}
void EnterVariableContext(const std::string& name, utils::IdType value_id) {
// for variable namespaces, for loops
contexts_.emplace_back();
DefineVariable(name, value_id);
}
std::optional<utils::IdType> GetVariableInfo(const std::string& name) {
for (ssize_t i = (ssize_t)contexts_.size() - 1; i >= 0; --i) {
auto maybe_type = contexts_[i].GetVariableInfo(name);
if (maybe_type.has_value()) {
return maybe_type.value();
}
}
return std::nullopt;
}
private:
class Context {
public:
Context() = default;
bool DefineVariable(const std::string& name, utils::IdType value_id) {
if (name == "_") { // placeholder // TODO: ??
return true;
}
if (variables_.count(name) > 0) {
return false;
}
variables_[name] = value_id;
return true;
}
bool RemoveVariable(const std::string& name) {
return variables_.erase(name);
}
std::optional<utils::IdType> GetVariableInfo(const std::string& name) {
auto variable_iter = variables_.find(name);
if (variable_iter == variables_.end()) {
return std::nullopt;
}
return variable_iter->second;
}
private:
std::unordered_map<std::string, utils::IdType> variables_;
};
std::vector<Context> contexts_;
value::ValueManager value_manager_;
};
} // namespace info