mirror of
https://codeberg.org/ProgramSnail/lang_2023.git
synced 2025-12-06 06:58:45 +00:00
type_check_visitor part
This commit is contained in:
parent
d13faf104d
commit
94ef8702fb
11 changed files with 496 additions and 288 deletions
|
|
@ -23,7 +23,7 @@ struct TypeUsage {
|
|||
|
||||
struct Parameter {
|
||||
std::string type;
|
||||
std::vector<interpreter::tokens::TypeclassExpression*> typeclass_nodes;
|
||||
std::vector<interpreter::tokens::ParametrizedTypeclass*> typeclass_nodes;
|
||||
};
|
||||
|
||||
struct AbstractType {
|
||||
|
|
|
|||
|
|
@ -102,7 +102,6 @@ private:
|
|||
|
||||
// Typeclass
|
||||
|
||||
// // void Visit(TypeclassExpression* node) override;
|
||||
// // void Visit(ParametrizedTypeclass* node) override;
|
||||
|
||||
// Typeclass & Type
|
||||
|
|
@ -124,7 +123,7 @@ private:
|
|||
private:
|
||||
info::GlobalInfo::NamespaceVisitor namespace_visitor_;
|
||||
|
||||
bool is_in_statement = false;
|
||||
bool is_in_statement_ = false;
|
||||
std::any current_info_;
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -101,8 +101,7 @@ private:
|
|||
|
||||
// Typeclass
|
||||
|
||||
void Visit(TypeclassExpression* node) override;
|
||||
// // void Visit(ParametrizedTypeclass* node) override;
|
||||
void Visit(ParametrizedTypeclass* node) override;
|
||||
|
||||
// Typeclass & Type
|
||||
|
||||
|
|
|
|||
|
|
@ -101,7 +101,6 @@ private:
|
|||
|
||||
// Typeclass
|
||||
|
||||
void Visit(TypeclassExpression* node) override;
|
||||
void Visit(ParametrizedTypeclass* node) override;
|
||||
|
||||
// Typeclass & Type
|
||||
|
|
@ -126,6 +125,9 @@ private:
|
|||
|
||||
utils::IdType current_type_;
|
||||
std::optional<utils::IdType> returned_type_;
|
||||
std::optional<bool> is_const_definition_;
|
||||
|
||||
bool is_in_statement_ = false;
|
||||
};
|
||||
|
||||
} // namespace interpreter
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
#pragma once
|
||||
|
||||
#include <cstddef>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
#include <unordered_map>
|
||||
|
||||
|
|
@ -14,6 +14,28 @@ namespace info {
|
|||
// TODO: remember about typespointers
|
||||
class TypeInfoContextManager {
|
||||
public:
|
||||
template<typename T>
|
||||
utils::IdType AddType(const T&& type) {
|
||||
return type_manager_.AddType(std::forward(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);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
type::TypeManager* GetTypeManager() {
|
||||
return &type_manager_;
|
||||
}
|
||||
|
|
@ -35,9 +57,9 @@ public:
|
|||
}
|
||||
|
||||
// TODO: variable modifiers
|
||||
bool DefineVariable(const std::string& name, utils::IdType type_id) {
|
||||
bool DefineVariable(const std::string& name, utils::IdType type_id, bool is_const) {
|
||||
// check in previous contexts ??
|
||||
return contexts_.back().DefineVariable(name, type_id);
|
||||
return contexts_.back().DefineVariable(name, type_id, is_const);
|
||||
}
|
||||
|
||||
bool DefineLocalAbstractType(const std::string& name, utils::IdType type_id) {
|
||||
|
|
@ -59,15 +81,15 @@ public:
|
|||
}
|
||||
|
||||
void EnterVariableContext(const std::string& name,
|
||||
utils::IdType type_id) { // for variable namespaces, for loops
|
||||
utils::IdType type_id, bool is_const) { // for variable namespaces, for loops
|
||||
contexts_.push_back(false);
|
||||
|
||||
DefineVariable(name, type_id);
|
||||
DefineVariable(name, type_id, is_const);
|
||||
}
|
||||
|
||||
std::optional<utils::IdType> GetVariableType(const std::string& name) {
|
||||
std::optional<std::pair<utils::IdType, bool>> GetVariableInfo(const std::string& name) {
|
||||
for (ssize_t i = (ssize_t)contexts_.size() - 1; i >= 0; --i) {
|
||||
auto maybe_type = contexts_[i].GetVariableType(name);
|
||||
auto maybe_type = contexts_[i].GetVariableInfo(name);
|
||||
if (maybe_type.has_value()) {
|
||||
return maybe_type.value();
|
||||
}
|
||||
|
|
@ -90,11 +112,11 @@ private:
|
|||
public:
|
||||
Context(bool hide_previous) : hide_previous_(hide_previous) {}
|
||||
|
||||
bool DefineVariable(const std::string& name, utils::IdType type_id) {
|
||||
if (local_abstract_types_.count(name) > 0) {
|
||||
bool DefineVariable(const std::string& name, utils::IdType type_id, bool is_const) {
|
||||
if (variables_.count(name) > 0) {
|
||||
return false;
|
||||
}
|
||||
local_abstract_types_[name] = type_id;
|
||||
variables_[name] = {type_id, is_const};
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
@ -110,7 +132,7 @@ private:
|
|||
return variables_.erase(name);
|
||||
}
|
||||
|
||||
std::optional<utils::IdType> GetVariableType(const std::string& name) {
|
||||
std::optional<std::pair<utils::IdType, bool>> GetVariableInfo(const std::string& name) {
|
||||
auto variable_iter = variables_.find(name);
|
||||
|
||||
if (variable_iter == variables_.end()) {
|
||||
|
|
@ -133,7 +155,7 @@ private:
|
|||
bool IsFirst() { return hide_previous_; }
|
||||
private:
|
||||
bool hide_previous_;
|
||||
std::unordered_map<std::string, utils::IdType> variables_;
|
||||
std::unordered_map<std::string, std::pair<utils::IdType, bool>> variables_;
|
||||
std::unordered_map<std::string, utils::IdType> local_abstract_types_;
|
||||
};
|
||||
|
||||
|
|
|
|||
66
include/typeclass_graph.hpp
Normal file
66
include/typeclass_graph.hpp
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
#pragma once
|
||||
|
||||
#include <string>
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Temporary frozen, TODO
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <unordered_map>
|
||||
|
||||
// for calngd
|
||||
#include "utils.hpp"
|
||||
#include "interpreter_tree.hpp"
|
||||
#include "error_handling.hpp"
|
||||
|
||||
namespace info {
|
||||
|
||||
class TypeclassGraph {
|
||||
public:
|
||||
struct TypeclassMethod {
|
||||
std::string name;
|
||||
interpreter::tokens::FunctionDeclaration* definition = nullptr;
|
||||
};
|
||||
|
||||
struct ParametrizedTypeclass {
|
||||
utils::IdType typeclass;
|
||||
std::vector<utils::IdType> parameter_ids; // ??
|
||||
};
|
||||
|
||||
struct TypeclassVertex {
|
||||
std::vector<std::pair<std::string, std::vector<ParametrizedTypeclass>>> parameters;
|
||||
std::vector<TypeclassMethod> methods;
|
||||
std::vector<ParametrizedTypeclass> dependencies;
|
||||
interpreter::tokens::TypeclassDefinitionStatement* definition = nullptr;
|
||||
};
|
||||
|
||||
utils::IdType AddTypeclass(const TypeclassVertex& typeclass) { // TODO: universal reference
|
||||
for (auto& method : typeclass.methods) {
|
||||
if (method_to_typeclass_.count(method.name) != 0) {
|
||||
error_handling::HandleTypecheckError("");
|
||||
}
|
||||
}
|
||||
for (auto& method : typeclass.methods) {
|
||||
method_to_typeclass_[method.name] = typeclasses_.size();
|
||||
}
|
||||
typeclasses_.push_back(typeclass);
|
||||
return typeclasses_.size() - 1;
|
||||
}
|
||||
std::optional<utils::IdType> FindMethodTypeclass(const std::string& name) {
|
||||
auto method_iter = method_to_typeclass_.find(name);
|
||||
if (method_iter == method_to_typeclass_.end()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
return method_iter->second;
|
||||
}
|
||||
|
||||
const TypeclassVertex& GetTypeclass(utils::IdType id) {
|
||||
return typeclasses_.at(id);
|
||||
}
|
||||
private:
|
||||
std::unordered_map<std::string, utils::IdType> method_to_typeclass_;
|
||||
std::vector<TypeclassVertex> typeclasses_;
|
||||
};
|
||||
|
||||
} // namespace info
|
||||
|
|
@ -5,6 +5,7 @@
|
|||
#include <variant>
|
||||
#include <memory>
|
||||
#include <unordered_set>
|
||||
#include <unordered_map>
|
||||
|
||||
// for clangd
|
||||
#include "utils.hpp"
|
||||
|
|
@ -25,6 +26,7 @@ public:
|
|||
}
|
||||
}
|
||||
|
||||
std::optional<utils::IdType> InContext(const std::unordered_map<std::string, utils::IdType>& context);
|
||||
bool Same(const AbstractType& type) const;
|
||||
bool operator<(const AbstractType& type) const;
|
||||
bool operator>(const AbstractType& type) const;
|
||||
|
|
@ -41,9 +43,14 @@ public:
|
|||
TypeManager* type_manager)
|
||||
: type_id_(type_id), type_(type), type_manager_(type_manager) {}
|
||||
|
||||
std::optional<utils::IdType> InContext(const std::unordered_map<std::string, utils::IdType>& context);
|
||||
bool Same(const DefinedType& type) const;
|
||||
bool operator<(const DefinedType& type) const;
|
||||
bool operator>(const DefinedType& type) const;
|
||||
|
||||
utils::IdType GetTypeId() {
|
||||
return type_id_;
|
||||
}
|
||||
private:
|
||||
utils::IdType type_id_; // in defined types
|
||||
utils::IdType type_; // in types manager, created using context types (if specific type)
|
||||
|
|
@ -67,9 +74,14 @@ public:
|
|||
TypeManager* type_manager)
|
||||
: name_(name), fields_(fields), type_manager_(type_manager) {}
|
||||
|
||||
std::optional<utils::IdType> InContext(const std::unordered_map<std::string, utils::IdType>& context);
|
||||
bool Same(const TupleType& type) const;
|
||||
bool operator<(const TupleType& type) const;
|
||||
bool operator>(const TupleType& type) const;
|
||||
|
||||
const std::vector<std::pair<std::optional<std::string>, utils::IdType>>& GetFields() const {
|
||||
return fields_;
|
||||
}
|
||||
private:
|
||||
std::optional<std::string> name_;
|
||||
std::vector<std::pair<std::optional<std::string>, utils::IdType>> fields_;
|
||||
|
|
@ -83,9 +95,14 @@ public:
|
|||
const std::vector<TupleType>& constructors)
|
||||
: name_(name), constructors_(constructors){}
|
||||
|
||||
std::optional<utils::IdType> InContext(const std::unordered_map<std::string, utils::IdType>& context);
|
||||
bool Same(const VariantType& type) const;
|
||||
bool operator<(const VariantType& type) const;
|
||||
bool operator>(const VariantType& type) const;
|
||||
|
||||
const std::vector<TupleType>& GetConstructors() const {
|
||||
return constructors_;
|
||||
}
|
||||
private:
|
||||
std::optional<std::string> name_;
|
||||
std::vector<TupleType> constructors_;
|
||||
|
|
@ -98,6 +115,7 @@ public:
|
|||
TypeManager* type_manager)
|
||||
: type_(type), type_manager_(type_manager) {}
|
||||
|
||||
std::optional<utils::IdType> InContext(const std::unordered_map<std::string, utils::IdType>& context);
|
||||
bool Same(const OptionalType& type) const;
|
||||
bool operator<(const OptionalType& type) const;
|
||||
bool operator>(const OptionalType& type) const;
|
||||
|
|
@ -114,6 +132,7 @@ public:
|
|||
TypeManager* type_manager)
|
||||
: references_(references), type_(type), type_manager_(type_manager) {}
|
||||
|
||||
std::optional<utils::IdType> InContext(const std::unordered_map<std::string, utils::IdType>& context);
|
||||
bool Same(const ReferenceToType& type) const;
|
||||
bool operator<(const ReferenceToType& type) const;
|
||||
bool operator>(const ReferenceToType& type) const;
|
||||
|
|
@ -133,6 +152,7 @@ public:
|
|||
TypeManager* type_manager)
|
||||
: argument_types_(argument_types), return_type_(return_type), type_manager_(type_manager) {}
|
||||
|
||||
std::optional<utils::IdType> InContext(const std::unordered_map<std::string, utils::IdType>& context);
|
||||
bool Same(const FunctionType& type) const;
|
||||
bool operator<(const FunctionType& type) const;
|
||||
bool operator>(const FunctionType& type) const;
|
||||
|
|
@ -150,9 +170,14 @@ public:
|
|||
TypeManager* type_manager)
|
||||
: size_(size), elements_type_(elements_type), type_manager_(type_manager) {}
|
||||
|
||||
std::optional<utils::IdType> InContext(const std::unordered_map<std::string, utils::IdType>& context);
|
||||
bool Same(const ArrayType& type) const;
|
||||
bool operator<(const ArrayType& type) const;
|
||||
bool operator>(const ArrayType& type) const;
|
||||
|
||||
utils::IdType GetElementsType() {
|
||||
return elements_type_;
|
||||
}
|
||||
private:
|
||||
size_t size_; // = 0 for dynamic
|
||||
utils::IdType elements_type_;
|
||||
|
|
@ -164,8 +189,9 @@ public:
|
|||
template<typename T>
|
||||
explicit Type(T&& type) : type_(std::forward(type)) {}
|
||||
|
||||
bool Same(const Type& type) const; // some rule exceptions ??
|
||||
bool operator<(const Type& type) const; // TODO: some rule exceptions ??
|
||||
std::optional<utils::IdType> InContext(const std::unordered_map<std::string, utils::IdType>& context);
|
||||
bool Same(const Type& type) const;
|
||||
bool operator<(const Type& type) const; // TODO: rule exceptions
|
||||
bool operator>(const Type& type) const;
|
||||
private:
|
||||
std::variant<AbstractType,
|
||||
|
|
@ -186,10 +212,10 @@ public:
|
|||
template<typename T>
|
||||
std::optional<T*> GetType(utils::IdType type_id);
|
||||
|
||||
std::optional<Type*> GetAnyType(utils::IdType type_id);
|
||||
Type* GetAnyType(utils::IdType type_id);
|
||||
|
||||
void AddTypeRequirement(utils::IdType type, utils::IdType requrement); // TODO
|
||||
void EqualTypes(utils::IdType first_type, utils::IdType second_type); // TODO
|
||||
bool AddTypeRequirement(utils::IdType type, utils::IdType requrement);
|
||||
bool EqualTypes(utils::IdType first_type, utils::IdType second_type);
|
||||
private:
|
||||
std::vector<info::type::Type> types_;
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue