lang_2023/include/contexts.hpp

184 lines
4.5 KiB
C++
Raw Normal View History

2023-03-26 15:20:53 +03:00
#pragma once
#include <string>
2023-05-07 20:21:19 +03:00
#include <utility>
2023-03-26 15:20:53 +03:00
#include <vector>
#include <optional>
2023-03-26 15:20:53 +03:00
#include <unordered_map>
// for clangd
2023-05-07 20:21:19 +03:00
#include "utils.hpp"
2023-03-26 15:20:53 +03:00
namespace info {
template<typename Value, typename ValueManager>
2023-03-26 15:20:53 +03:00
class ContextManager {
public:
2023-05-07 20:21:19 +03:00
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, utils::ValueType value_type) {
2023-05-07 20:21:19 +03:00
return value_manager_.AddAnyValue(std::move(value), value_type);
}
template<typename T>
std::optional<T*> GetValue(utils::IdType value_id) {
return value_manager_.template GetValue<T>(value_id);
2023-05-07 20:21:19 +03:00
}
Value* GetAnyValue(utils::IdType value_id) {
2023-05-07 20:21:19 +03:00
return value_manager_.GetAnyValue(value_id);
}
bool AddValueRequirement(utils::IdType type, utils::IdType requrement) {
2023-05-10 23:13:50 +03:00
return value_manager_.AddValueRequirement(type, requrement);
}
bool EqualValues(utils::IdType first_type, utils::IdType second_type) {
2023-05-10 23:13:50 +03:00
return value_manager_.EqualValues(first_type, second_type);
}
2023-05-07 20:21:19 +03:00
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 = *GetAnyValue(value_id);
2023-05-07 20:21:19 +03:00
return AddAnyValue(std::move(value), new_value_type);
}
ValueManager* GetValueManager() {
2023-05-07 20:21:19 +03:00
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);
}
2023-05-09 15:24:19 +03:00
bool DefineLocalType(const std::string& name, utils::IdType type_id) {
if (GetLocalType(name).has_value()) {
return false;
}
return contexts_.back().DefineLocalType(name, type_id);
}
2023-05-07 20:21:19 +03:00
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;
}
2023-03-26 15:20:53 +03:00
2023-05-09 15:24:19 +03:00
std::optional<utils::IdType> GetLocalType(const std::string& name) {
for (ssize_t i = (ssize_t)contexts_.size() - 1; i >= 0; --i) {
auto maybe_type = contexts_[i].GetLocalType(name);
if (maybe_type.has_value()) {
return maybe_type.value();
}
}
return std::nullopt;
}
2023-03-26 15:20:53 +03:00
private:
class Context {
public:
2023-05-07 20:21:19 +03:00
Context() = default;
bool DefineVariable(const std::string& name, utils::IdType value_id) {
if (name == "_") { // placeholder // TODO: ??
return true;
}
if (variables_.count(name) != 0) {
2023-05-07 20:21:19 +03:00
return false;
}
variables_[name] = value_id;
return true;
}
2023-05-09 15:24:19 +03:00
bool DefineLocalType(const std::string& name, utils::IdType type_id) {
if (local_types_.count(name) != 0) {
2023-05-09 15:24:19 +03:00
return false;
}
local_types_[name] = type_id;
return true;
}
2023-05-07 20:21:19 +03:00
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;
}
2023-03-26 15:20:53 +03:00
2023-05-07 20:21:19 +03:00
return variable_iter->second;
}
2023-03-26 15:20:53 +03:00
2023-05-09 15:24:19 +03:00
std::optional<utils::IdType> GetLocalType(const std::string& name) {
auto local_abstract_type_iter = local_types_.find(name);
if (local_abstract_type_iter == local_types_.end()) {
return std::nullopt;
}
return local_abstract_type_iter->second;
}
2023-03-26 15:20:53 +03:00
private:
2023-05-07 20:21:19 +03:00
std::unordered_map<std::string, utils::IdType> variables_;
2023-05-09 15:24:19 +03:00
std::unordered_map<std::string, utils::IdType> local_types_;
2023-03-26 15:20:53 +03:00
};
std::vector<Context> contexts_;
ValueManager value_manager_;
2023-03-26 15:20:53 +03:00
};
} // namespace info