contexts merged with type_info_contexts

This commit is contained in:
ProgramSnail 2023-05-09 15:37:30 +03:00
parent e1b9d42da1
commit 6850863f58
7 changed files with 143 additions and 315 deletions

View file

@ -3,14 +3,15 @@
#include <string>
#include <utility>
#include <vector>
#include <optional>
#include <unordered_map>
// for clangd
#include "values.hpp"
#include "utils.hpp"
namespace info {
template<typename Value, typename ValueManager>
class ContextManager {
public:
ContextManager() {
@ -22,29 +23,37 @@ public:
return value_manager_.AddValue(value, value_type);
}
utils::IdType AddAnyValue(value::Value&& value, utils::ValueType value_type) {
utils::IdType AddAnyValue(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);
return value_manager_.template GetValue<T>(value_id);
}
value::Value* GetAnyValue(utils::IdType value_id) {
Value* GetAnyValue(utils::IdType value_id) {
return value_manager_.GetAnyValue(value_id);
}
bool AddValueRequirement(utils::IdType type, utils::IdType requrement) {
return value_manager_.AddTypeRequirement(type, requrement);
}
bool EqualValues(utils::IdType first_type, utils::IdType second_type) {
return value_manager_.EqualTypes(first_type, second_type);
}
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);
Value value = *GetAnyValue(value_id);
return AddAnyValue(std::move(value), new_value_type);
}
value::ValueManager* GetValueManager() {
ValueManager* GetValueManager() {
return &value_manager_;
}
@ -168,7 +177,7 @@ private:
};
std::vector<Context> contexts_;
value::ValueManager value_manager_;
ValueManager value_manager_;
};
} // namespace info

View file

@ -7,6 +7,7 @@
#include "global_info.hpp"
#include "interpreter_tree.hpp"
#include "type_info_contexts.hpp"
#include "types.hpp"
#include "utils.hpp"
#include "values.hpp"
#include "visitor.hpp"
@ -17,11 +18,11 @@ namespace interpreter {
class ExecuteVisitor : public Visitor {
public:
explicit ExecuteVisitor(info::GlobalInfo& global_info,
info::TypeInfoContextManager& type_info_context_manager,
info::ContextManager& context_manager,
info::ContextManager<info::type::Type, info::type::TypeManager>& type_context_manager,
info::ContextManager<info::value::Value, info::value::ValueManager>& context_manager,
interpreter::tokens::PartitionStatement* execution_root)
: namespace_visitor_(global_info.CreateVisitor()),
type_info_context_manager_(type_info_context_manager),
type_context_manager_(type_context_manager),
context_manager_(context_manager) {}
void VisitSourceFile(SourceFile* source_file) override {
@ -161,8 +162,8 @@ private:
}
private:
info::GlobalInfo::NamespaceVisitor namespace_visitor_;
info::TypeInfoContextManager& type_info_context_manager_;
info::ContextManager& context_manager_;
info::ContextManager<info::type::Type, info::type::TypeManager>& type_context_manager_;
info::ContextManager<info::value::Value, info::value::ValueManager>& context_manager_;
utils::IdType current_value_;
std::optional<LoopControlExpression> active_loop_control_expression_;

View file

@ -2,7 +2,8 @@
// for clangd
#include "interpreter_tree.hpp"
#include "type_info_contexts.hpp"
#include "types.hpp"
#include "contexts.hpp"
#include "utils.hpp"
#include "visitor.hpp"
#include "global_info.hpp"
@ -14,7 +15,7 @@ namespace interpreter {
class TypeCheckVisitor : public Visitor {
public:
explicit TypeCheckVisitor(info::GlobalInfo& global_info,
info::TypeInfoContextManager& context_manager)
info::ContextManager<info::type::Type, info::type::TypeManager>& context_manager)
: namespace_visitor_(global_info.CreateVisitor()), context_manager_(context_manager) {}
private:
@ -139,7 +140,7 @@ private:
private:
info::GlobalInfo::NamespaceVisitor namespace_visitor_;
info::TypeInfoContextManager& context_manager_;
info::ContextManager<info::type::Type, info::type::TypeManager>& context_manager_;
utils::IdType current_type_;
std::optional<utils::IdType> returned_type_;

View file

@ -1,183 +0,0 @@
#pragma once
#include <string>
#include <utility>
#include <vector>
#include <unordered_map>
// for clangd
#include "types.hpp"
#include "utils.hpp"
namespace info {
// TODO: remember about type pointers
class TypeInfoContextManager {
public:
TypeInfoContextManager() {
contexts_.emplace_back();
}
template<typename T>
utils::IdType AddType(const T& type, utils::ValueType value_type) {
return type_manager_.AddType(type, value_type);
}
utils::IdType AddAnyType(type::Type&& type, utils::ValueType value_type) {
return type_manager_.AddAnyType(std::move(type), value_type);
}
template<typename T>
std::optional<T*> GetType(utils::IdType type_id) {
return type_manager_.GetType<T>(type_id);
}
type::Type* GetAnyType(utils::IdType type_id) {
return type_manager_.GetAnyType(type_id);
}
utils::ValueType GetValueType(utils::IdType type_id) {
return type_manager_.GetValueType(type_id);
}
bool AddTypeRequirement(utils::IdType type, utils::IdType requrement) {
return type_manager_.AddTypeRequirement(type, requrement);
}
bool EqualTypes(utils::IdType first_type, utils::IdType second_type) {
return type_manager_.EqualTypes(first_type, second_type);
}
utils::IdType ToModifiedType(utils::IdType type_id, utils::ValueType new_value_type) {
type::Type type = *GetAnyType(type_id);
return AddAnyType(std::move(type), new_value_type);
}
type::TypeManager* GetTypeManager() {
return &type_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 type_id) {
// check in previous contexts ??
return contexts_.back().DefineVariable(name, type_id);
}
bool DefineLocalAbstractType(const std::string& name, utils::IdType type_id) {
if (GetLocalAbstractType(name).has_value()) {
return false;
}
return contexts_.back().DefineLocalAbstractType(name, type_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 type_id) {
// for variable namespaces, for loops
contexts_.emplace_back();
DefineVariable(name, type_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;
}
std::optional<utils::IdType> GetLocalAbstractType(const std::string& name) {
for (ssize_t i = (ssize_t)contexts_.size() - 1; i >= 0; --i) {
auto maybe_type = contexts_[i].GetLocalAbstractType(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 type_id) {
if (name == "_") { // placeholder // TODO: ??
return true;
}
if (variables_.count(name) > 0) {
return false;
}
variables_[name] = type_id;
return true;
}
bool DefineLocalAbstractType(const std::string& name, utils::IdType type_id) {
if (local_abstract_types_.count(name) > 0) {
return false;
}
local_abstract_types_[name] = type_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;
}
std::optional<utils::IdType> GetLocalAbstractType(const std::string& name) {
auto local_abstract_type_iter = local_abstract_types_.find(name);
if (local_abstract_type_iter == local_abstract_types_.end()) {
return std::nullopt;
}
return local_abstract_type_iter->second;
}
private:
std::unordered_map<std::string, utils::IdType> variables_;
std::unordered_map<std::string, utils::IdType> local_abstract_types_;
};
std::vector<Context> contexts_;
type::TypeManager type_manager_;
};
} // namespace info

View file

@ -293,25 +293,25 @@ private:
class TypeManager {
public:
template<typename T>
utils::IdType AddType(const T& type, utils::ValueType value_type) {
utils::IdType AddValue(const T& type, utils::ValueType value_type) {
types_.push_back(std::pair<Type, utils::ValueType> {type, value_type});
return types_.size() - 1;
}
utils::IdType AddAnyType(Type&& type, utils::ValueType value_type) {
utils::IdType AddAnyValue(Type&& type, utils::ValueType value_type) {
types_.push_back(std::pair<Type, utils::ValueType> {std::move(type), value_type});
return types_.size() - 1;
}
template<typename T>
std::optional<T*> GetType(utils::IdType type_id) {
std::optional<T*> GetValue(utils::IdType type_id) {
if (!std::holds_alternative<T>(types_.at(type_id).first.GetType())) {
return std::nullopt;
}
return &std::get<T>(types_.at(type_id).first.GetType());
}
Type* GetAnyType(utils::IdType type_id) {
Type* GetAnyValue(utils::IdType type_id) {
return &types_.at(type_id).first;
}
@ -320,11 +320,11 @@ public:
}
bool EqualTypes(utils::IdType first_type, utils::IdType second_type) {
return GetAnyType(first_type)->Same(*GetAnyType(second_type));
return GetAnyValue(first_type)->Same(*GetAnyValue(second_type));
}
bool AddTypeRequirement(utils::IdType type, utils::IdType requrement) {
return *GetAnyType(requrement) < *GetAnyType(type);
return *GetAnyValue(requrement) < *GetAnyValue(type);
}
private: