mirror of
https://codeberg.org/ProgramSnail/lang_2023.git
synced 2025-12-05 22:48:42 +00:00
adding types to typeclass tree, part 1
This commit is contained in:
parent
17328b842c
commit
d841e2754b
7 changed files with 260 additions and 167 deletions
|
|
@ -350,19 +350,19 @@ public:
|
|||
return &typeclass_graph_;
|
||||
}
|
||||
|
||||
std::unordered_set<utils::IdType>
|
||||
GetAnnotatedTypeTypeclassesSet(interpreter::tokens::AnnotatedType* node);
|
||||
|
||||
std::vector<utils::IdType>
|
||||
GetAnnotatedTypeTypeclassesVector(interpreter::tokens::AnnotatedType* node);
|
||||
|
||||
std::unordered_map<std::string, TypeclassGraph::FunctionInfo>
|
||||
GetAnnotatedTypeFunctionsMap(interpreter::tokens::AnnotatedType* node,
|
||||
const interpreter::tokens::BaseNode& base_node);
|
||||
|
||||
std::vector<std::pair<std::string, TypeclassGraph::FunctionInfo>>
|
||||
GetAnnotatedTypeFunctionsVector(interpreter::tokens::AnnotatedType* node,
|
||||
const interpreter::tokens::BaseNode& base_node);
|
||||
// std::unordered_set<utils::IdType>
|
||||
// GetAnnotatedTypeTypeclassesSet(interpreter::tokens::AnnotatedType* node);
|
||||
//
|
||||
// std::vector<utils::IdType>
|
||||
// GetAnnotatedTypeTypeclassesVector(interpreter::tokens::AnnotatedType* node);
|
||||
//
|
||||
// std::unordered_map<std::string, TypeclassGraph::FunctionInfo>
|
||||
// GetAnnotatedTypeFunctionsMap(interpreter::tokens::AnnotatedType* node,
|
||||
// const interpreter::tokens::BaseNode& base_node);
|
||||
//
|
||||
// std::vector<std::pair<std::string, TypeclassGraph::FunctionInfo>>
|
||||
// GetAnnotatedTypeFunctionsVector(interpreter::tokens::AnnotatedType* node,
|
||||
// const interpreter::tokens::BaseNode& base_node);
|
||||
|
||||
std::unordered_map<std::string, utils::IdType>* ChooseNamespaces(
|
||||
utils::ClassInternalsModifier modifier,
|
||||
|
|
@ -370,6 +370,9 @@ public:
|
|||
|
||||
std::optional<utils::IdType> AddTypeclassToGraph(utils::IdType typeclass);
|
||||
|
||||
// function declarations & definitions should be added latter
|
||||
std::optional<utils::IdType> AddAnnotatedTypeToGraph(interpreter::tokens::AnnotatedType* node);
|
||||
|
||||
private:
|
||||
void CollectFunctionInfo(
|
||||
utils::IdType current_namespace,
|
||||
|
|
|
|||
|
|
@ -17,9 +17,7 @@ public:
|
|||
|
||||
void VisitSourceFile(SourceFile* source_file) override {
|
||||
Visitor::Visit(source_file);
|
||||
if (!namespace_visitor_.GetTypeclassGraph()->CalculateGraph()) {
|
||||
error_handling::HandleInternalError("Can't calculate typeclass graph", "LinkSymbolsVisitor.VisitSourceFile");
|
||||
}
|
||||
namespace_visitor_.GetTypeclassGraph()->CalculateGraph();
|
||||
}
|
||||
|
||||
private:
|
||||
|
|
@ -39,7 +37,7 @@ private:
|
|||
// // void Visit(VariableDefinitionStatement* node) override;
|
||||
// // void Visit(FunctionDeclaration* node) override;
|
||||
// // void Visit(FunctionDefinitionStatement* node) override;
|
||||
// // void Visit(TypeDefinitionStatement* node) override;
|
||||
void Visit(TypeDefinitionStatement* node) override;
|
||||
// // void Visit(AbstractTypeDefinitionStatement* node) override;
|
||||
void Visit(TypeclassDefinitionStatement* node) override;
|
||||
// // void Visit(PartitionStatement* node) override;
|
||||
|
|
|
|||
|
|
@ -12,57 +12,77 @@
|
|||
|
||||
namespace info {
|
||||
|
||||
// move constructor parameters ??
|
||||
class TypeclassGraph {
|
||||
public:
|
||||
enum class Modifier {
|
||||
Typeclass = 0,
|
||||
Type = 1,
|
||||
};
|
||||
|
||||
struct FunctionInfo {
|
||||
utils::ClassInternalsModifier modifier = utils::ClassInternalsModifier::Static;
|
||||
interpreter::tokens::FunctionDeclaration* declaration = nullptr;
|
||||
std::optional<interpreter::tokens::FunctionDefinitionStatement*> definition;
|
||||
bool is_defined_in_owner = false;
|
||||
};
|
||||
|
||||
struct TypeclassVertex {
|
||||
struct Vertex { // make constructor ??
|
||||
std::string name;
|
||||
interpreter::tokens::TypeclassDefinitionStatement* definition = nullptr;
|
||||
std::unordered_map<std::string, FunctionInfo> functions;
|
||||
std::unordered_set<std::string> dependencies; // TODO: parameters
|
||||
interpreter::tokens::BaseNode* base_node; // for error handling
|
||||
Modifier modifier;
|
||||
};
|
||||
|
||||
std::optional<utils::IdType> AddTypeclass( // move parameters ??
|
||||
utils::IdType AddVertex(
|
||||
const std::string& name,
|
||||
interpreter::tokens::TypeclassDefinitionStatement* definition,
|
||||
const std::vector<std::string>& dependencies, // TODO: parameters
|
||||
const std::vector<std::string>& dependencies,
|
||||
const std::vector<std::pair<std::string, std::pair<utils::ClassInternalsModifier, interpreter::tokens::FunctionDeclaration*>>>& function_declarations,
|
||||
const std::vector<std::pair<std::string, interpreter::tokens::FunctionDefinitionStatement*>>& function_definitions);
|
||||
const std::vector<std::pair<std::string, interpreter::tokens::FunctionDefinitionStatement*>>& function_definitions,
|
||||
interpreter::tokens::BaseNode* base_node,
|
||||
Modifier modifier);
|
||||
|
||||
std::optional<utils::IdType> FindFunctionTypeclass(const std::string& name);
|
||||
|
||||
bool IsFunctionInTypeclass(const std::string& name, utils::IdType typeclass_id) {
|
||||
return typeclasses_[typeclass_id].functions.count(name) != 0;
|
||||
bool IsFunctionInVertex(const std::string& name, utils::IdType vertex_id) {
|
||||
return verticles_[vertex_id].functions.count(name) != 0;
|
||||
}
|
||||
|
||||
// if vertex_id == std::nullopt, info can be not complete
|
||||
std::optional<FunctionInfo*> GetFunctionInfo(const std::string& name,
|
||||
std::optional<utils::IdType> typeclass_id);
|
||||
std::optional<utils::IdType> vertex_id);
|
||||
|
||||
const TypeclassVertex& GetTypeclass(utils::IdType typeclass_id) { // check, if calculated ??
|
||||
return typeclasses_.at(typeclass_id);
|
||||
const Vertex& GetVertex(utils::IdType id) {
|
||||
return verticles_.at(id);
|
||||
}
|
||||
|
||||
std::optional<Vertex*> GetTypeVertex(utils::IdType id) {
|
||||
if (verticles_.at(id).modifier == Modifier::Type) {
|
||||
return &verticles_[id];
|
||||
}
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
// cache ??
|
||||
std::vector<utils::IdType> GetTypeclassDependencies(utils::IdType id);
|
||||
std::unordered_set<utils::IdType> GetDependenciesSet(utils::IdType id);
|
||||
std::vector<utils::IdType> GetDependenciesVector(utils::IdType id);
|
||||
|
||||
// cache ??
|
||||
std::vector<std::pair<std::string, TypeclassGraph::FunctionInfo*>> GetTypeclassFunctions(utils::IdType id);
|
||||
const std::unordered_map<std::string, FunctionInfo>& GetVertexFunctions(utils::IdType id) {
|
||||
return verticles_.at(id).functions;
|
||||
}
|
||||
|
||||
bool IsCalculated() {
|
||||
return is_calculated_;
|
||||
}
|
||||
|
||||
bool CalculateGraph();
|
||||
// can exit by typecheck error
|
||||
void CalculateGraph();
|
||||
|
||||
private:
|
||||
std::unordered_map<std::string, utils::IdType> method_to_typeclass_;
|
||||
std::unordered_map<std::string, utils::IdType> method_to_vertex_;
|
||||
std::unordered_map<std::string, utils::IdType> name_to_typeclass_;
|
||||
std::vector<TypeclassVertex> typeclasses_;
|
||||
std::vector<Vertex> verticles_;
|
||||
|
||||
bool is_calculated_ = true;
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue