mirror of
https://codeberg.org/ProgramSnail/lang_2023.git
synced 2025-12-06 06:58:45 +00:00
type structs -> type classes
This commit is contained in:
parent
a512a92f92
commit
648f78afa3
14 changed files with 638 additions and 383 deletions
|
|
@ -45,7 +45,7 @@ struct AnyType {
|
|||
};
|
||||
|
||||
struct Type {
|
||||
std::variant<AbstractType, AliasType, AnyType> type;
|
||||
std::variant<AliasType, AnyType> type;
|
||||
};
|
||||
|
||||
struct Constructor {
|
||||
|
|
@ -85,7 +85,6 @@ struct Namespace {
|
|||
enum Modifier { Const, Var };
|
||||
|
||||
std::unordered_map<std::string, utils::IdType> types;
|
||||
std::unordered_map<std::string, utils::IdType> typeclasses;
|
||||
std::unordered_map<std::string, utils::IdType> functions;
|
||||
std::unordered_map<std::string, utils::IdType> constructors;
|
||||
std::unordered_map<std::string, Namespace> namespaces;
|
||||
|
|
|
|||
|
|
@ -6,7 +6,6 @@
|
|||
|
||||
// for clangd
|
||||
#include "definitions.hpp"
|
||||
#include "type_manager.hpp"
|
||||
#include "utils.hpp"
|
||||
|
||||
namespace info {
|
||||
|
|
@ -38,6 +37,9 @@ public:
|
|||
|
||||
utils::IdType AddType(const std::string& type, definition::Type&& type_info);
|
||||
|
||||
utils::IdType AddAbstractType(const std::string& abstract_type,
|
||||
definition::AbstractType&& abstract_type_info);
|
||||
|
||||
utils::IdType AddTypeclass(const std::string& typeclass, definition::Typeclass&& typeclass_info);
|
||||
|
||||
utils::IdType AddConstructor(const std::string& constructor,
|
||||
|
|
@ -55,20 +57,17 @@ public:
|
|||
std::optional<utils::IdType> FindType(const std::optional<std::vector<std::string>>& path,
|
||||
const std::string& type);
|
||||
|
||||
std::optional<utils::IdType> FindTypeclass(const std::optional<std::vector<std::string>>& path,
|
||||
const std::string& typeclass);
|
||||
std::optional<utils::IdType> FindAbstractType(const std::string& abstract_type);
|
||||
|
||||
std::optional<utils::IdType> FindTypeclass(const std::string& typeclass);
|
||||
|
||||
std::optional<utils::IdType> FindConstructor(const std::optional<std::vector<std::string>>& path,
|
||||
const std::string& typeclass);
|
||||
const std::string& constructor);
|
||||
|
||||
NamespaceVisitor CreateVisitor() {
|
||||
return global_info_.CreateVisitor();
|
||||
}
|
||||
|
||||
type::TypeManager* GetTypeManager() {
|
||||
return global_info_.GetTypeManager();
|
||||
}
|
||||
|
||||
GlobalInfo* GetGlobalInfo() {
|
||||
return &global_info_;
|
||||
}
|
||||
|
|
@ -97,10 +96,6 @@ public:
|
|||
return NamespaceVisitor(*this);
|
||||
}
|
||||
|
||||
type::TypeManager* GetTypeManager() {
|
||||
return &type_manager_;
|
||||
}
|
||||
|
||||
// TODO: remember about vector realloc
|
||||
definition::Function* GetFunctionInfo(utils::IdType id) {
|
||||
return &functions_[id];
|
||||
|
|
@ -124,10 +119,12 @@ public:
|
|||
private:
|
||||
std::vector<definition::Function> functions_;
|
||||
std::vector<definition::Type> types_;
|
||||
std::vector<definition::AbstractType> abstract_types_;
|
||||
std::vector<definition::Typeclass> typeclasses_;
|
||||
std::vector<definition::Constructor> constructors_;
|
||||
|
||||
type::TypeManager type_manager_;
|
||||
std::unordered_map<std::string, utils::IdType> name_to_typeclass_;
|
||||
std::unordered_map<std::string, utils::IdType> name_to_abstract_type_;
|
||||
|
||||
definition::Namespace global_namespace_;
|
||||
std::vector<definition::Import> imports_;
|
||||
|
|
|
|||
|
|
@ -297,7 +297,8 @@ struct Namespace {
|
|||
TypeIdentifier type;
|
||||
NamespaceSources scope;
|
||||
|
||||
std::optional<utils::IdType> type_id_;
|
||||
std::optional<utils::IdType> link_type_id_;
|
||||
std::optional<utils::IdType> link_typeclass_id_;
|
||||
};
|
||||
|
||||
// ----------------- Definitions -----------------
|
||||
|
|
@ -570,7 +571,7 @@ struct ExtendedScopedAnyType {
|
|||
struct TypeclassExpression {
|
||||
TypeclassSubExpression typeclass;
|
||||
|
||||
utils::IdType type_id_;
|
||||
utils::IdType typeclass_id_;
|
||||
};
|
||||
|
||||
struct ParametrizedTypeclass {
|
||||
|
|
|
|||
|
|
@ -17,30 +17,30 @@ public:
|
|||
private:
|
||||
// Sources -----------------
|
||||
|
||||
void Visit(SourceFile* node) override;
|
||||
// // void Visit(SourceFile* node) override;
|
||||
|
||||
// Namespaces, partitions -----------------
|
||||
|
||||
void Visit(PartitionSources* node) override;
|
||||
void Visit(Partition* node) override;
|
||||
void Visit(NamespaceSources* node) override;
|
||||
// // void Visit(PartitionSources* node) override;
|
||||
// // void Visit(Partition* node) override;
|
||||
// // void Visit(NamespaceSources* node) override;
|
||||
void Visit(Namespace* node) override;
|
||||
|
||||
// Definitions -----------------
|
||||
|
||||
void Visit(ImportStatement* node) override;
|
||||
void Visit(AliasDefinitionStatement* node) override;
|
||||
void Visit(VariableDefinitionStatement* node) override;
|
||||
void Visit(FunctionDeclaration* node) override;
|
||||
void Visit(FunctionDefinitionStatement* node) override;
|
||||
void Visit(TypeDefinitionStatement* node) override;
|
||||
void Visit(AbstractTypeDefinitionStatement* node) override;
|
||||
void Visit(TypeclassDefinitionStatement* node) override;
|
||||
// // void Visit(ImportStatement* node) override;
|
||||
// // void Visit(AliasDefinitionStatement* node) override;
|
||||
// // void Visit(VariableDefinitionStatement* node) override;
|
||||
// // void Visit(FunctionDeclaration* node) override;
|
||||
// // void Visit(FunctionDefinitionStatement* node) override;
|
||||
// // void Visit(TypeDefinitionStatement* node) override;
|
||||
// // void Visit(AbstractTypeDefinitionStatement* node) override;
|
||||
// // void Visit(TypeclassDefinitionStatement* node) override;
|
||||
|
||||
// Definition parts
|
||||
|
||||
void Visit(FunctionDefinition* node) override;
|
||||
void Visit(TypeDefinition* node) override;
|
||||
// // void Visit(FunctionDefinition* node) override;
|
||||
// // void Visit(TypeDefinition* node) override;
|
||||
// // void Visit(AnyAnnotatedType* node) override;
|
||||
|
||||
// Flow control -----------------
|
||||
|
|
@ -76,7 +76,7 @@ private:
|
|||
// // void Visit(ReturnExpression* node) override;
|
||||
// // void Visit(TypeConstructorParameter* node) override;
|
||||
// // void Visit(TypeConstructor* node) override;
|
||||
void Visit(LambdaFunction* node) override;
|
||||
// // void Visit(LambdaFunction* node) override;
|
||||
// // void Visit(ArrayExpression* node) override;
|
||||
|
||||
// // void Visit(LoopControlExpression& node) override; // enum
|
||||
|
|
@ -95,13 +95,13 @@ private:
|
|||
// // void Visit(FunctionType* node) override;
|
||||
// // void Visit(TupleType* node) override;
|
||||
// // void Visit(VariantType* node) override;
|
||||
void Visit(TypeExpression* node) override; // TODO
|
||||
void Visit(TypeExpression* node) override;
|
||||
|
||||
// // void Visit(ExtendedScopedAnyType* node) override;
|
||||
|
||||
// Typeclass
|
||||
|
||||
void Visit(TypeclassExpression* node) override; // TODO
|
||||
void Visit(TypeclassExpression* node) override;
|
||||
// // void Visit(ParametrizedTypeclass* node) override;
|
||||
|
||||
// Typeclass & Type
|
||||
|
|
|
|||
|
|
@ -10,8 +10,8 @@ namespace interpreter {
|
|||
|
||||
class TypeCheckVisitor : public Visitor {
|
||||
public:
|
||||
explicit TypeCheckVisitor(info::GlobalInfo& global_info)
|
||||
: namespace_visitor_(global_info.CreateVisitor()) {}
|
||||
explicit TypeCheckVisitor(info::GlobalInfo& global_info, info::TypeInfoContextManager& context_manager)
|
||||
: namespace_visitor_(global_info.CreateVisitor()), context_manager_(context_manager) {}
|
||||
|
||||
private:
|
||||
// Sources -----------------
|
||||
|
|
@ -122,7 +122,7 @@ private:
|
|||
|
||||
private:
|
||||
info::GlobalInfo::NamespaceVisitor namespace_visitor_;
|
||||
info::TypeInfoContextManager context_manager_;
|
||||
info::TypeInfoContextManager& context_manager_;
|
||||
|
||||
utils::IdType current_type_;
|
||||
std::optional<utils::IdType> returned_type_;
|
||||
|
|
|
|||
|
|
@ -1,11 +1,13 @@
|
|||
#pragma once
|
||||
|
||||
#include <cstddef>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <unordered_map>
|
||||
|
||||
// for clangd
|
||||
#include "types_info.hpp"
|
||||
#include "error_handling.hpp"
|
||||
#include "types.hpp"
|
||||
#include "utils.hpp"
|
||||
|
||||
namespace info {
|
||||
|
|
@ -13,32 +15,10 @@ namespace info {
|
|||
// TODO: remember about typespointers
|
||||
class TypeInfoContextManager {
|
||||
public:
|
||||
template<typename T>
|
||||
utils::IdType AddType(T&& type) {
|
||||
types_.push_back(std::forward(type));
|
||||
return types_.size() - 1;
|
||||
type::TypeManager* GetTypeManager() {
|
||||
return &type_manager_;
|
||||
}
|
||||
|
||||
const info::type::Type& GetType(utils::IdType type_id) {
|
||||
return types_[type_id];
|
||||
}
|
||||
|
||||
void AddTypeRequirement(utils::IdType type, utils::IdType requrement) {} // TODO
|
||||
void EqualTypes(utils::IdType first_type, utils::IdType second_type) {} // TODO
|
||||
|
||||
// void CallFunction(const std::vector<std::string>& names,
|
||||
// const std::vector<utils::IdType>& argument_types) {
|
||||
// if (names.size() != argument_types.size()) {
|
||||
// // error
|
||||
// }
|
||||
//
|
||||
// contexts_.emplace_back(true);
|
||||
//
|
||||
// for (size_t i = 0; i < names.size(); ++i) {
|
||||
// DefineVariable(names[i], argument_types[i]);
|
||||
// }
|
||||
// }
|
||||
|
||||
void EnterContext() {
|
||||
contexts_.emplace_back(false);
|
||||
}
|
||||
|
|
@ -60,6 +40,10 @@ public:
|
|||
return contexts_.back().DefineVariable(name, type_id);
|
||||
}
|
||||
|
||||
bool DefineLocalAbstractType() {
|
||||
// TODO
|
||||
}
|
||||
|
||||
bool RemoveVariable(const std::string& name) {
|
||||
for (ssize_t i = (ssize_t)contexts_.size() - 1; i >= 0; --i) {
|
||||
if (contexts_[i].RemoveVariable(name)) {
|
||||
|
|
@ -120,7 +104,7 @@ private:
|
|||
};
|
||||
|
||||
std::vector<Context> contexts_;
|
||||
std::vector<info::type::Type> types_;
|
||||
type::TypeManager type_manager_;
|
||||
};
|
||||
|
||||
} // namespace info
|
||||
|
|
|
|||
|
|
@ -1,26 +0,0 @@
|
|||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
|
||||
// for clangd
|
||||
#include "types.hpp"
|
||||
#include "utils.hpp"
|
||||
|
||||
namespace info::type {
|
||||
|
||||
class TypeManager {
|
||||
public:
|
||||
template<typename T>
|
||||
void AddType(const T& type) {
|
||||
types_.emplace(type);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
T GetType(utils::IdType type_id) {
|
||||
return std::get<T>(types_.at(type_id));
|
||||
}
|
||||
private:
|
||||
std::vector<Type> types_;
|
||||
};
|
||||
|
||||
} // namespace info::type
|
||||
|
|
@ -4,68 +4,195 @@
|
|||
#include <string>
|
||||
#include <variant>
|
||||
#include <memory>
|
||||
#include <unordered_set>
|
||||
|
||||
// for clangd
|
||||
#include "utils.hpp"
|
||||
|
||||
namespace info::type {
|
||||
|
||||
// // Temporary frozen, TODO
|
||||
// struct AbstractType {
|
||||
// utils::IdType graph_id;
|
||||
// std::vector<utils::IdType> paramaters;
|
||||
// };
|
||||
// TODO: move in constructors
|
||||
|
||||
// TODO: check, if defined type is variant, etc.
|
||||
struct DefinedType {
|
||||
utils::IdType type_id; // in defined types
|
||||
utils::IdType type; // in types manager, created using context types
|
||||
class TypeManager;
|
||||
|
||||
class AbstractType { // later will be found in context
|
||||
public:
|
||||
AbstractType() = default;
|
||||
AbstractType(const std::string& name,
|
||||
const std::vector<utils::IdType>& requirements) : name_(name) {
|
||||
for (auto& typeclass : requirements) {
|
||||
requirements_.insert(typeclass);
|
||||
}
|
||||
}
|
||||
|
||||
bool Same(const AbstractType& type) const;
|
||||
bool operator<(const AbstractType& type) const;
|
||||
bool operator>(const AbstractType& type) const;
|
||||
private:
|
||||
std::string name_;
|
||||
std::unordered_set<utils::IdType> requirements_; // TODO: all typeclasses from tree
|
||||
};
|
||||
|
||||
class DefinedType {
|
||||
public:
|
||||
DefinedType() = default;
|
||||
DefinedType(utils::IdType type_id,
|
||||
utils::IdType type,
|
||||
TypeManager* type_manager)
|
||||
: type_id_(type_id), type_(type), type_manager_(type_manager) {}
|
||||
|
||||
bool Same(const DefinedType& type) const;
|
||||
bool operator<(const DefinedType& type) const;
|
||||
bool operator>(const DefinedType& type) const;
|
||||
private:
|
||||
utils::IdType type_id_; // in defined types
|
||||
utils::IdType type_; // in types manager, created using context types (if specific type)
|
||||
TypeManager* type_manager_ = nullptr;
|
||||
};
|
||||
|
||||
enum class InternalType {
|
||||
Float,
|
||||
Int,
|
||||
String,
|
||||
Char,
|
||||
Bool,
|
||||
Unit,
|
||||
Float = 0,
|
||||
Int = 1,
|
||||
String = 2,
|
||||
Char = 3,
|
||||
Bool = 4,
|
||||
Unit = 5,
|
||||
};
|
||||
|
||||
struct TupleType {
|
||||
std::optional<std::string> type;
|
||||
std::vector<std::pair<std::optional<std::string>, utils::IdType>> fields;
|
||||
class TupleType {
|
||||
public:
|
||||
TupleType() = default;
|
||||
TupleType(const std::optional<std::string>& name,
|
||||
const std::vector<std::pair<std::optional<std::string>, utils::IdType>>& fields,
|
||||
TypeManager* type_manager)
|
||||
: name_(name), fields_(fields), type_manager_(type_manager) {}
|
||||
|
||||
bool Same(const TupleType& type) const;
|
||||
bool operator<(const TupleType& type) const;
|
||||
bool operator>(const TupleType& type) const;
|
||||
private:
|
||||
std::optional<std::string> name_;
|
||||
std::vector<std::pair<std::optional<std::string>, utils::IdType>> fields_;
|
||||
TypeManager* type_manager_ = nullptr;
|
||||
};
|
||||
|
||||
struct VariantType {
|
||||
std::optional<std::string> type;
|
||||
std::vector<TupleType> constructors;
|
||||
class VariantType {
|
||||
public:
|
||||
VariantType() = default;
|
||||
VariantType(const std::optional<std::string>& name,
|
||||
const std::vector<TupleType>& constructors)
|
||||
: name_(name), constructors_(constructors){}
|
||||
|
||||
bool Same(const VariantType& type) const;
|
||||
bool operator<(const VariantType& type) const;
|
||||
bool operator>(const VariantType& type) const;
|
||||
private:
|
||||
std::optional<std::string> name_;
|
||||
std::vector<TupleType> constructors_;
|
||||
};
|
||||
|
||||
struct OptionalType {
|
||||
std::optional<utils::IdType> type; // Can be empty (Some or None)
|
||||
class OptionalType {
|
||||
public:
|
||||
OptionalType() = default;
|
||||
OptionalType(utils::IdType type,
|
||||
TypeManager* type_manager)
|
||||
: type_(type), type_manager_(type_manager) {}
|
||||
|
||||
bool Same(const OptionalType& type) const;
|
||||
bool operator<(const OptionalType& type) const;
|
||||
bool operator>(const OptionalType& type) const;
|
||||
private:
|
||||
utils::IdType type_;
|
||||
TypeManager* type_manager_ = nullptr;
|
||||
};
|
||||
|
||||
struct ReferenceToType {
|
||||
std::vector<utils::ReferenceType> references;
|
||||
utils::IdType type;
|
||||
class ReferenceToType {
|
||||
public:
|
||||
ReferenceToType() = default;
|
||||
ReferenceToType(const std::vector<utils::ReferenceType>& references,
|
||||
utils::IdType type,
|
||||
TypeManager* type_manager)
|
||||
: references_(references), type_(type), type_manager_(type_manager) {}
|
||||
|
||||
bool Same(const ReferenceToType& type) const;
|
||||
bool operator<(const ReferenceToType& type) const;
|
||||
bool operator>(const ReferenceToType& type) const;
|
||||
private:
|
||||
std::vector<utils::ReferenceType> references_;
|
||||
utils::IdType type_;
|
||||
TypeManager* type_manager_ = nullptr;
|
||||
};
|
||||
|
||||
struct FunctionType {
|
||||
std::vector<utils::IdType> argument_types;
|
||||
utils::IdType return_type;
|
||||
/////////////////////////////
|
||||
|
||||
class FunctionType {
|
||||
public:
|
||||
FunctionType() = default;
|
||||
FunctionType(const std::vector<utils::IdType>& argument_types,
|
||||
utils::IdType return_type,
|
||||
TypeManager* type_manager)
|
||||
: argument_types_(argument_types), return_type_(return_type), type_manager_(type_manager) {}
|
||||
|
||||
bool Same(const FunctionType& type) const;
|
||||
bool operator<(const FunctionType& type) const;
|
||||
bool operator>(const FunctionType& type) const;
|
||||
private:
|
||||
std::vector<utils::IdType> argument_types_;
|
||||
utils::IdType return_type_;
|
||||
TypeManager* type_manager_ = nullptr;
|
||||
};
|
||||
|
||||
struct ArrayType {
|
||||
size_t size; // ?? = 0 for dynamic ??
|
||||
std::optional<utils::IdType> elements_type;
|
||||
class ArrayType {
|
||||
public:
|
||||
ArrayType() = default;
|
||||
ArrayType(size_t size,
|
||||
utils::IdType elements_type,
|
||||
TypeManager* type_manager)
|
||||
: size_(size), elements_type_(elements_type), type_manager_(type_manager) {}
|
||||
|
||||
bool Same(const ArrayType& type) const;
|
||||
bool operator<(const ArrayType& type) const;
|
||||
bool operator>(const ArrayType& type) const;
|
||||
private:
|
||||
size_t size_; // = 0 for dynamic
|
||||
utils::IdType elements_type_;
|
||||
TypeManager* type_manager_ = nullptr;
|
||||
};
|
||||
|
||||
using Type = std::variant<DefinedType,
|
||||
InternalType,
|
||||
TupleType,
|
||||
VariantType,
|
||||
ReferenceToType,
|
||||
FunctionType,
|
||||
ArrayType>;
|
||||
class Type {
|
||||
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 ??
|
||||
bool operator>(const Type& type) const;
|
||||
private:
|
||||
std::variant<AbstractType,
|
||||
DefinedType,
|
||||
InternalType,
|
||||
TupleType,
|
||||
VariantType,
|
||||
ReferenceToType,
|
||||
FunctionType,
|
||||
ArrayType> type_;
|
||||
};
|
||||
|
||||
class TypeManager {
|
||||
public:
|
||||
template<typename T>
|
||||
utils::IdType AddType(const T&& type);
|
||||
|
||||
template<typename T>
|
||||
std::optional<T*> GetType(utils::IdType type_id);
|
||||
|
||||
std::optional<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
|
||||
private:
|
||||
std::vector<info::type::Type> types_;
|
||||
};
|
||||
|
||||
} // namespace info::type
|
||||
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@ using std::size_t;
|
|||
|
||||
using IdType = size_t;
|
||||
|
||||
enum class ReferenceType { Reference, UniqueReference };
|
||||
enum class ReferenceType { Reference = 0, UniqueReference = 1 };
|
||||
|
||||
template<typename T>
|
||||
class Storage {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue