mirror of
https://codeberg.org/ProgramSnail/lang_2023.git
synced 2025-12-26 00:38:43 +00:00
find_symbols_visitor, global_info fixed
This commit is contained in:
parent
4d0b527416
commit
a512a92f92
9 changed files with 289 additions and 277 deletions
98
include/definitions.hpp
Normal file
98
include/definitions.hpp
Normal file
|
|
@ -0,0 +1,98 @@
|
|||
#pragma once
|
||||
|
||||
#include "interpreter_tree.hpp"
|
||||
#include <string>
|
||||
#include <variant>
|
||||
#include <vector>
|
||||
#include <optional>
|
||||
#include <unordered_map>
|
||||
#include <memory>
|
||||
|
||||
// for clangd
|
||||
#include "utils.hpp"
|
||||
|
||||
namespace interpreter {
|
||||
class Node;
|
||||
} // namespace interpreter
|
||||
|
||||
namespace info::definition {
|
||||
|
||||
struct TypeUsage {
|
||||
interpreter::tokens::TypeExpression* node;
|
||||
};
|
||||
|
||||
struct Parameter {
|
||||
std::string type;
|
||||
std::vector<interpreter::tokens::TypeclassExpression*> typeclass_nodes;
|
||||
};
|
||||
|
||||
struct AbstractType {
|
||||
enum { Basic, Abstract } modifier;
|
||||
Parameter type;
|
||||
};
|
||||
|
||||
struct AliasType {
|
||||
enum {Alias, Type, Let} modifier;
|
||||
std::vector<std::string> parameters;
|
||||
TypeUsage value;
|
||||
interpreter::tokens::AliasDefinitionStatement* node = nullptr;
|
||||
};
|
||||
|
||||
struct AnyType {
|
||||
Parameter type;
|
||||
std::vector<Parameter> parameters;
|
||||
interpreter::tokens::AnyType* value;
|
||||
};
|
||||
|
||||
struct Type {
|
||||
std::variant<AbstractType, AliasType, AnyType> type;
|
||||
};
|
||||
|
||||
struct Constructor {
|
||||
std::string name;
|
||||
size_t order;
|
||||
utils::IdType type_id;
|
||||
};
|
||||
|
||||
struct FunctionDeclaration {
|
||||
std::vector<Parameter> parameters;
|
||||
std::vector<interpreter::tokens::AnyType*> argument_types;
|
||||
interpreter::tokens::FunctionDeclaration* node = nullptr;
|
||||
};
|
||||
|
||||
struct FunctionDefinition {
|
||||
std::vector<std::string> argument_names;
|
||||
interpreter::tokens::FunctionDefinitionStatement* node = nullptr;
|
||||
};
|
||||
|
||||
struct Function {
|
||||
size_t argument_count = 0;
|
||||
std::optional<FunctionDeclaration> declaration;
|
||||
std::optional<FunctionDefinition> definition;
|
||||
};
|
||||
|
||||
struct Typeclass {
|
||||
std::vector<Parameter> parameters;
|
||||
std::vector<FunctionDeclaration> requirements;
|
||||
};
|
||||
|
||||
struct Import {
|
||||
std::string module_name;
|
||||
std::vector<std::string> symbols; // size = 0 => all symbols imported
|
||||
};
|
||||
|
||||
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;
|
||||
std::unordered_map<std::string, Namespace> variable_namespaces;
|
||||
|
||||
std::optional<Modifier> modifier; // modifier => variable namespace
|
||||
std::string type_name;
|
||||
};
|
||||
|
||||
} // namespace info::definition
|
||||
|
|
@ -17,12 +17,13 @@ public:
|
|||
private:
|
||||
// Sources -----------------
|
||||
|
||||
void Visit(SourceFile* node) override;
|
||||
void Visit(Sources* node) override;
|
||||
// void Visit(SourceFile* node) override; // default
|
||||
|
||||
// Namespaces, partitions -----------------
|
||||
|
||||
// void Visit(PartitionSources* node) override; // default
|
||||
void Visit(Partition* node) override;
|
||||
// void Visit(NamespaceSources* node) override; // default
|
||||
void Visit(Namespace* node) override;
|
||||
|
||||
// Definitions -----------------
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@
|
|||
#include <unordered_map>
|
||||
|
||||
// for clangd
|
||||
#include "symbols_info.hpp"
|
||||
#include "definitions.hpp"
|
||||
#include "type_manager.hpp"
|
||||
#include "utils.hpp"
|
||||
|
||||
|
|
@ -19,11 +19,10 @@ public:
|
|||
friend GlobalInfo;
|
||||
NamespaceVisitor() = delete;
|
||||
public:
|
||||
void AddImport(ImportInfo&& import_info, const std::optional<std::string>& name = std::nullopt);
|
||||
void AddImport(definition::Import&& import_info, const std::optional<std::string>& name = std::nullopt);
|
||||
|
||||
void AddEnterNamespace(const std::string& name,
|
||||
const std::optional<NamespaceInfo::Modifier>& modifier = std::nullopt,
|
||||
const std::optional<std::string>& variable = std::nullopt);
|
||||
const std::optional<definition::Namespace::Modifier>& modifier = std::nullopt);
|
||||
|
||||
void EnterNamespace(const std::string& name);
|
||||
|
||||
|
|
@ -32,29 +31,35 @@ public:
|
|||
void ToGlobalNamespace();
|
||||
|
||||
utils::IdType AddFunctionDeclaration(const std::string& name,
|
||||
FunctionDeclarationInfo&& function_declaration_info);
|
||||
definition::FunctionDeclaration&& function_declaration_info);
|
||||
|
||||
utils::IdType AddFunctionDefinition(const std::string& name,
|
||||
FunctionDefinitionInfo&& function_definition_info);
|
||||
definition::FunctionDefinition&& function_definition_info);
|
||||
|
||||
utils::IdType AddType(const std::string& type, TypeInfo&& type_info);
|
||||
utils::IdType AddType(const std::string& type, definition::Type&& type_info);
|
||||
|
||||
utils::IdType AddTypeclass(const std::string& typeclass, TypeclassInfo&& typeclass_info);
|
||||
utils::IdType AddTypeclass(const std::string& typeclass, definition::Typeclass&& typeclass_info);
|
||||
|
||||
std::optional<NamespaceInfo*> FindNamespace(const std::optional<std::vector<std::string>>& path);
|
||||
utils::IdType AddConstructor(const std::string& constructor,
|
||||
definition::Constructor&& constructor_info);
|
||||
|
||||
std::optional<std::vector<utils::IdType>> FindFunction(const std::optional<std::vector<std::string>>& path,
|
||||
const std::string& name);
|
||||
std::optional<definition::Namespace*> FindNamespace(const std::optional<std::vector<std::string>>& path);
|
||||
|
||||
std::optional<std::vector<utils::IdType>> FindMethod(const std::optional<std::vector<std::string>>& path,
|
||||
std::optional<utils::IdType> FindFunction(const std::optional<std::vector<std::string>>& path,
|
||||
const std::string& name);
|
||||
|
||||
std::optional<utils::IdType> FindMethod(const std::optional<std::vector<std::string>>& path,
|
||||
const std::string& type,
|
||||
const std::string& name);
|
||||
|
||||
std::optional<std::vector<utils::IdType>> FindType(const std::optional<std::vector<std::string>>& path,
|
||||
const std::string type);
|
||||
std::optional<utils::IdType> FindType(const std::optional<std::vector<std::string>>& path,
|
||||
const std::string& type);
|
||||
|
||||
std::optional<std::vector<utils::IdType>> FindTypeclass(const std::optional<std::vector<std::string>>& path,
|
||||
const std::string typeclass);
|
||||
std::optional<utils::IdType> FindTypeclass(const std::optional<std::vector<std::string>>& path,
|
||||
const std::string& typeclass);
|
||||
|
||||
std::optional<utils::IdType> FindConstructor(const std::optional<std::vector<std::string>>& path,
|
||||
const std::string& typeclass);
|
||||
|
||||
NamespaceVisitor CreateVisitor() {
|
||||
return global_info_.CreateVisitor();
|
||||
|
|
@ -77,12 +82,12 @@ public:
|
|||
|
||||
template<typename T>
|
||||
std::optional<T> FindSomething(const std::optional<std::vector<std::string>>& path,
|
||||
std::function<std::optional<T>(NamespaceInfo*)> search_func);
|
||||
std::function<std::optional<T>(definition::Namespace*)> search_func);
|
||||
|
||||
std::optional<NamespaceInfo*> FindNamespaceIn(NamespaceInfo* current_namespace,
|
||||
std::optional<definition::Namespace*> FindNamespaceIn(definition::Namespace* current_namespace,
|
||||
const std::vector<std::string>& path);
|
||||
private:
|
||||
std::vector<NamespaceInfo*> namespace_stack_;
|
||||
std::vector<definition::Namespace*> namespace_stack_;
|
||||
std::vector<std::string> current_path_;
|
||||
|
||||
GlobalInfo& global_info_;
|
||||
|
|
@ -97,30 +102,36 @@ public:
|
|||
}
|
||||
|
||||
// TODO: remember about vector realloc
|
||||
FunctionInfo* GetFunctionInfo(utils::IdType id) {
|
||||
definition::Function* GetFunctionInfo(utils::IdType id) {
|
||||
return &functions_[id];
|
||||
}
|
||||
|
||||
// TODO: remember about vector realloc
|
||||
TypeInfo* GetTypeInfo(utils::IdType id) {
|
||||
definition::Type* GetTypeInfo(utils::IdType id) {
|
||||
return &types_[id];
|
||||
}
|
||||
|
||||
// TODO: remember about vector realloc
|
||||
TypeclassInfo* GetTypeclassInfo(utils::IdType id) {
|
||||
definition::Typeclass* GetTypeclassInfo(utils::IdType id) {
|
||||
return &typeclasses_[id];
|
||||
}
|
||||
|
||||
// TODO: remember about vector realloc
|
||||
definition::Constructor* GetConstructorInfo(utils::IdType id) {
|
||||
return &constructors_[id];
|
||||
}
|
||||
|
||||
private:
|
||||
std::vector<FunctionInfo> functions_;
|
||||
std::vector<TypeInfo> types_;
|
||||
std::vector<TypeclassInfo> typeclasses_;
|
||||
std::vector<definition::Function> functions_;
|
||||
std::vector<definition::Type> types_;
|
||||
std::vector<definition::Typeclass> typeclasses_;
|
||||
std::vector<definition::Constructor> constructors_;
|
||||
|
||||
type::TypeManager type_manager_;
|
||||
|
||||
NamespaceInfo global_namespace_;
|
||||
std::vector<ImportInfo> imports_;
|
||||
std::unordered_map<std::string, ImportInfo> usages_;
|
||||
definition::Namespace global_namespace_;
|
||||
std::vector<definition::Import> imports_;
|
||||
std::unordered_map<std::string, definition::Import> usages_;
|
||||
};
|
||||
|
||||
} // namespace info
|
||||
|
|
|
|||
|
|
@ -314,7 +314,6 @@ struct AliasDefinitionStatement {
|
|||
std::vector<AbstractTypeIdentifier> parameters;
|
||||
std::unique_ptr<TypeExpression> value;
|
||||
|
||||
std::vector<utils::IdType> parameter_graph_ids_;
|
||||
utils::IdType type_id_;
|
||||
};
|
||||
|
||||
|
|
@ -339,8 +338,6 @@ struct FunctionDefinitionStatement {
|
|||
SuperExpression value;
|
||||
|
||||
utils::IdType function_id_;
|
||||
std::vector<utils::IdType> argument_graph_ids_;
|
||||
utils::IdType return_type_graph_id_;
|
||||
};
|
||||
|
||||
struct TypeDefinitionStatement {
|
||||
|
|
@ -382,8 +379,6 @@ struct TypeDefinition {
|
|||
struct AnnotatedAbstractType {
|
||||
AbstractTypeIdentifier type;
|
||||
std::vector<std::unique_ptr<TypeclassExpression>> typeclasses;
|
||||
|
||||
utils::IdType type_graph_id_;
|
||||
};
|
||||
|
||||
// ----------------- Flow control -----------------
|
||||
|
|
|
|||
|
|
@ -3,7 +3,6 @@
|
|||
#include <ostream>
|
||||
|
||||
// for clangd
|
||||
#include "abstract_types_contexts.hpp"
|
||||
#include "visitor.hpp"
|
||||
#include "global_info.hpp"
|
||||
|
||||
|
|
@ -19,11 +18,12 @@ private:
|
|||
// Sources -----------------
|
||||
|
||||
void Visit(SourceFile* node) override;
|
||||
void Visit(Sources* node) override;
|
||||
|
||||
// Namespaces, partitions -----------------
|
||||
|
||||
void Visit(PartitionSources* node) override;
|
||||
void Visit(Partition* node) override;
|
||||
void Visit(NamespaceSources* node) override;
|
||||
void Visit(Namespace* node) override;
|
||||
|
||||
// Definitions -----------------
|
||||
|
|
@ -121,7 +121,6 @@ private:
|
|||
|
||||
private:
|
||||
info::GlobalInfo::NamespaceVisitor namespace_visitor_;
|
||||
info::AbstractTypesContextManager abstract_types_;
|
||||
};
|
||||
|
||||
} // namespace interpreter
|
||||
|
|
|
|||
|
|
@ -1,118 +0,0 @@
|
|||
#pragma once
|
||||
|
||||
#include "interpreter_tree.hpp"
|
||||
#include <string>
|
||||
#include <variant>
|
||||
#include <vector>
|
||||
#include <optional>
|
||||
#include <unordered_map>
|
||||
#include <memory>
|
||||
|
||||
// for clangd
|
||||
#include "utils.hpp"
|
||||
|
||||
namespace interpreter {
|
||||
class Node;
|
||||
} // namespace interpreter
|
||||
|
||||
namespace info {
|
||||
|
||||
struct VariantTypeInfo;
|
||||
struct TupleTypeInfo;
|
||||
struct AliasTypeInfo;
|
||||
|
||||
struct TypeInfo;
|
||||
|
||||
struct TypeclassInfo;
|
||||
|
||||
enum class BuiltInTypeInfo {
|
||||
StringT,
|
||||
IntT,
|
||||
FloatT,
|
||||
UnitT,
|
||||
};
|
||||
|
||||
struct TypeUsageInfo {
|
||||
interpreter::tokens::TypeExpression* node;
|
||||
TypeInfo* info = nullptr;
|
||||
};
|
||||
|
||||
struct ParameterInfo {
|
||||
std::string type;
|
||||
std::vector<interpreter::tokens::TypeclassExpression*> typeclass_nodes;
|
||||
std::vector<TypeclassInfo*> typeclass_types;
|
||||
};
|
||||
|
||||
struct AbstractTypeInfo {
|
||||
enum { Basic, Abstract } modifier;
|
||||
ParameterInfo type;
|
||||
};
|
||||
|
||||
struct AliasTypeInfo {
|
||||
enum {Alias, Type, Let} modifier;
|
||||
std::vector<std::string> parameters;
|
||||
TypeUsageInfo value;
|
||||
};
|
||||
|
||||
struct AnyTypeInfo {
|
||||
ParameterInfo type;
|
||||
std::vector<ParameterInfo> parameters;
|
||||
interpreter::tokens::AnyType* value;
|
||||
};
|
||||
|
||||
struct TypeInfo {
|
||||
std::variant<AbstractTypeInfo, AliasTypeInfo, AnyTypeInfo> type;
|
||||
};
|
||||
|
||||
struct ConstructorInfo {
|
||||
std::string name;
|
||||
size_t order;
|
||||
utils::IdType type_id;
|
||||
};
|
||||
|
||||
struct FunctionDeclarationInfo {
|
||||
std::vector<ParameterInfo> parameters;
|
||||
std::vector<interpreter::tokens::AnyType*> argument_type_nodes;
|
||||
std::vector<AnyTypeInfo*> argument_types;
|
||||
interpreter::tokens::FunctionDeclaration* node = nullptr;
|
||||
};
|
||||
|
||||
struct FunctionDefinitionInfo {
|
||||
std::vector<ParameterInfo> parameters;
|
||||
std::vector<std::string> argument_names;
|
||||
interpreter::tokens::FunctionDefinitionStatement* node = nullptr;
|
||||
};
|
||||
|
||||
struct FunctionInfo {
|
||||
size_t argument_count = 0;
|
||||
std::optional<FunctionDeclarationInfo> declaration;
|
||||
std::optional<FunctionDefinitionInfo> definition;
|
||||
};
|
||||
|
||||
struct TypeclassInfo {
|
||||
std::vector<ParameterInfo> parameters;
|
||||
std::vector<FunctionDeclarationInfo> requirements;
|
||||
};
|
||||
|
||||
struct ImportInfo {
|
||||
std::string module_name;
|
||||
std::vector<std::string> symbols;
|
||||
};
|
||||
|
||||
struct NamespaceInfo {
|
||||
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, NamespaceInfo> namespaces;
|
||||
std::unordered_map<std::string, std::vector<NamespaceInfo>> variable_namespaces;
|
||||
|
||||
std::optional<Modifier> modifier;
|
||||
std::optional<std::string> variable;
|
||||
std::string type_name;
|
||||
TypeInfo* type_info = nullptr;
|
||||
};
|
||||
|
||||
} // namespace info
|
||||
|
|
@ -17,11 +17,12 @@ private:
|
|||
// Sources -----------------
|
||||
|
||||
void Visit(SourceFile* node) override;
|
||||
void Visit(Sources* node) override;
|
||||
|
||||
// Namespaces, partitions -----------------
|
||||
|
||||
void Visit(PartitionSources* node) override;
|
||||
void Visit(Partition* node) override;
|
||||
void Visit(NamespaceSources* node) override;
|
||||
void Visit(Namespace* node) override;
|
||||
|
||||
// Definitions -----------------
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue