mirror of
https://codeberg.org/ProgramSnail/lang_2023.git
synced 2025-12-06 06:58:45 +00:00
first iteration of find_symbols_visitor
This commit is contained in:
parent
87bd815bbb
commit
399631b9ca
13 changed files with 92065 additions and 92399 deletions
|
|
@ -43,7 +43,7 @@ private:
|
|||
|
||||
void Visit(FunctionDefinition* node) override;
|
||||
void Visit(TypeDefinition* node) override;
|
||||
void Visit(DefinitionParameter* node) override;
|
||||
void Visit(AnyAnnotatedType* node) override;
|
||||
|
||||
// Flow control -----------------
|
||||
|
||||
|
|
@ -108,7 +108,6 @@ private:
|
|||
void Visit(FunctionType* node) override;
|
||||
void Visit(TupleType* node) override;
|
||||
void Visit(VariantType* node) override;
|
||||
void Visit(AnnotatedType* node) override;
|
||||
void Visit(ParametrizedType* node) override;
|
||||
void Visit(TypeExpression* node) override;
|
||||
|
||||
|
|
|
|||
|
|
@ -18,9 +18,7 @@ public:
|
|||
explicit FindSymbolsVisitor(info::GlobalInfo& global_info) : global_info_(global_info) {}
|
||||
|
||||
private:
|
||||
//// void Visit(Node* node) override;
|
||||
|
||||
// Sources -----------------
|
||||
// Sources -----------------
|
||||
|
||||
void Visit(SourceFile* node) override;
|
||||
void Visit(Sources* node) override;
|
||||
|
|
@ -33,90 +31,93 @@ private:
|
|||
// Definitions -----------------
|
||||
|
||||
void Visit(ImportStatement* node) override;
|
||||
void Visit(UsageDefinition* node) override;
|
||||
void Visit(AliasDefinition* node) override;
|
||||
//// void Visit(VariableDefinition* node) override; // TODO: decide
|
||||
void Visit(AliasDefinitionStatement* node) override;
|
||||
// // void Visit(VariableDefinitionStatement* node) override;
|
||||
void Visit(FunctionDeclaration* node) override;
|
||||
void Visit(FunctionDefinition* node) override;
|
||||
void Visit(AliasTypeDefinition* node) override;
|
||||
void Visit(TypeDefinition* node) override;
|
||||
void Visit(TypeclassDefinition* 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(DefinedName* node) override;
|
||||
void Visit(DefinedAnnotatedName* node) override;
|
||||
void Visit(DefinedType* node) override;
|
||||
void Visit(DefinedTypeclass* node) override;
|
||||
void Visit(DefinitionParameter* node) override;
|
||||
void Visit(DefinitionArgument* node) override;
|
||||
// // void Visit(FunctionDefinition* node) override;
|
||||
// // void Visit(TypeDefinition* node) override;
|
||||
void Visit(AnyAnnotatedType* node) override;
|
||||
|
||||
// Flow control -----------------
|
||||
|
||||
//// void Visit(MatchCase* node) override;
|
||||
//// void Visit(Match* node) override;
|
||||
//// void Visit(Condition* node) override;
|
||||
//// void Visit(DoWhileLoop* node) override;
|
||||
//// void Visit(WhileLoop* node) override;
|
||||
//// void Visit(ForLoop* node) override;
|
||||
//// void Visit(LoopLoop* node) override;
|
||||
// // void Visit(MatchCase* node) override;
|
||||
// // void Visit(Match* node) override;
|
||||
// // void Visit(Condition* node) override;
|
||||
// // void Visit(DoWhileLoop* node) override;
|
||||
// // void Visit(WhileLoop* node) override;
|
||||
// // void Visit(ForLoop* node) override;
|
||||
// // void Visit(LoopLoop* node) override;
|
||||
|
||||
// Statements, expressions, blocks, etc. -----------------
|
||||
|
||||
//// void Visit(Block* node) override;
|
||||
//// void Visit(ScopedStatement* node) override;
|
||||
|
||||
//// void Visit(LoopControlExpression& node) override; // enum
|
||||
// // void Visit(Block* node) override;
|
||||
// //
|
||||
// // void Visit(ScopedStatement* node) override;
|
||||
|
||||
// Operators
|
||||
|
||||
//// void Visit(BinaryOperatorExpression* node) override;
|
||||
//// void Visit(UnaryOperatorExpression* node) override;
|
||||
// // void Visit(BinaryOperatorExpression* node) override;
|
||||
// // void Visit(UnaryOperatorExpression* node) override;
|
||||
// // void Visit(ReferenceExpression* node) override;
|
||||
|
||||
// Simple Expressions
|
||||
|
||||
//// void Visit(FunctionCallExpression* node) override;
|
||||
//// void Visit(TupleExpression* node) override;
|
||||
//// void Visit(VariantExpression* node) override;
|
||||
//// void Visit(ReturnExpression* node) override;
|
||||
|
||||
// Lambda
|
||||
|
||||
//// void Visit(LambdaFunction* node) override;
|
||||
// // void Visit(FunctionCallExpression* node) override;
|
||||
// //
|
||||
// // void Visit(TupleExpression* node) override;
|
||||
// // void Visit(VariantExpression* node) override;
|
||||
// // void Visit(ReturnExpression* node) override;
|
||||
// // void Visit(TypeConstructor* node) override;
|
||||
// // void Visit(LambdaFunction* node) override;
|
||||
// // void Visit(ArrayExpression* node) override;
|
||||
// //
|
||||
// // void Visit(LoopControlExpression& node) override; // enum
|
||||
|
||||
// Name
|
||||
|
||||
void Visit(NameSuperExpression* node) override;
|
||||
void Visit(NameExpression* node) override;
|
||||
void Visit(TupleName* node) override;
|
||||
void Visit(VariantName* node) override;
|
||||
void Visit(AnnotatedName* node) override;
|
||||
// // void Visit(NameExpression* node) override;
|
||||
// // void Visit(TupleName* node) override;
|
||||
// // void Visit(VariantName* node) override;
|
||||
// // void Visit(AnnotatedName* node) override;
|
||||
|
||||
// Type, typeclass, etc. -----------------
|
||||
|
||||
// Type
|
||||
|
||||
//// void Visit(TypeConstructor* node) override;
|
||||
void Visit(TupleType* node) override;
|
||||
void Visit(VariantType* node) override;
|
||||
void Visit(AnnotatedType* node) override;
|
||||
void Visit(ParametrizedType* node) override;
|
||||
void Visit(TypeExpression* node) override;
|
||||
// // void Visit(FunctionType* node) override;
|
||||
// // void Visit(TupleType* node) override;
|
||||
// // void Visit(VariantType* node) override;
|
||||
// // void Visit(ParametrizedType* node) override;
|
||||
// // void Visit(TypeExpression* node) override;
|
||||
// //
|
||||
// // void Visit(ExtendedScopedAnyType* node) override;
|
||||
|
||||
// Typeclass
|
||||
|
||||
void Visit(AnnotatedTypeclass* node) override;
|
||||
void Visit(ParametrizedTypeclass* node) override;
|
||||
void Visit(TypeclassExpression* node) override;
|
||||
// // void Visit(ParametrizedTypeclass* node) override;
|
||||
// // void Visit(TypeclassExpression* node) override;
|
||||
|
||||
// Identifiers, constants, etc. -----------------
|
||||
|
||||
//// void Visit(AnyIdentifier* node) override; // std::string
|
||||
// // void Visit(ExtendedName* node) override;
|
||||
// //
|
||||
// // void Visit(std::string* node) override; // std::string
|
||||
// //
|
||||
// // void Visit(FloatNumberLiteral* node) override;
|
||||
// // void Visit(NumberLiteral* node) override;
|
||||
// // void Visit(StringLiteral* node) override;
|
||||
// // void Visit(CharLiteral* node) override;
|
||||
|
||||
//// void Visit(FloatNumberLiteral* node) override;
|
||||
//// void Visit(NumberLiteral* node) override;
|
||||
//// void Visit(StringLiteral* node) override;
|
||||
//// void Visit(CharLiteral* node) override;
|
||||
private:
|
||||
info::GlobalInfo& global_info_;
|
||||
bool is_in_statement = false;
|
||||
std::any current_info_;
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -15,29 +15,22 @@ public:
|
|||
namespace_stack.push_back(&global_namespace_);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void AddImport(T&& import_info) {
|
||||
if (waiting_usage.has_value()) {
|
||||
usages_[waiting_usage.value()] = std::forward(import_info);
|
||||
waiting_usage = std::nullopt;
|
||||
void AddImport(ImportInfo&& import_info, const std::optional<std::string>& name = std::nullopt) {
|
||||
if (name.has_value()) {
|
||||
usages_[name.value()] = std::move(import_info);
|
||||
} else {
|
||||
imports_.push_back(std::forward(import_info));
|
||||
imports_.push_back(std::move(import_info));
|
||||
}
|
||||
}
|
||||
|
||||
void AddUsageForNextImport(const std::string& name) {
|
||||
if (waiting_usage.has_value()) {
|
||||
// error
|
||||
}
|
||||
waiting_usage = name;
|
||||
}
|
||||
|
||||
void AddEnterNamespace(const std::optional<std::string>& var,
|
||||
const std::string& type) {
|
||||
void AddEnterNamespace(const std::string& type,
|
||||
const std::optional<NamespaceInfo::Modifier>& modifier = std::nullopt,
|
||||
const std::optional<std::string>& variable = std::nullopt) {
|
||||
NamespaceInfo* namespace_info = &namespace_stack.back()->namespaces[type].emplace_back();
|
||||
namespace_stack.push_back(namespace_info);
|
||||
|
||||
namespace_info->var = var;
|
||||
namespace_info->modifier = modifier;
|
||||
namespace_info->variable = variable;
|
||||
namespace_info->type_name = type;
|
||||
}
|
||||
|
||||
|
|
@ -55,36 +48,30 @@ public:
|
|||
namespace_stack.push_back(&global_namespace_);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void AddFunctionDeclaration(const std::string& name,
|
||||
T&& function_declaration_info) {
|
||||
FunctionDeclarationInfo&& function_declaration_info) {
|
||||
FunctionInfo* function_info = &namespace_stack.back()->functions[name];
|
||||
|
||||
function_info->declaration = std::forward(function_declaration_info);
|
||||
function_info->declaration = std::move(function_declaration_info);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void AddFunctionDefinition(const std::string& name,
|
||||
T&& function_definition_info) {
|
||||
FunctionDefinitionInfo&& function_definition_info) {
|
||||
FunctionInfo* function_info = &namespace_stack.back()->functions[name];
|
||||
|
||||
function_info->definition = std::forward(function_definition_info);
|
||||
function_info->definition = std::move(function_definition_info);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void AddType(const std::string& type,
|
||||
T&& type_info) {
|
||||
&namespace_stack.back()->types[type] = std::forward(type_info);
|
||||
TypeInfo&& type_info) {
|
||||
namespace_stack.back()->types[type] = std::move(type_info);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void AddTypeclass(const std::string& typeclass,
|
||||
T&& typeclass_info) {
|
||||
namespace_stack.back()->typeclasses[typeclass] = std::forward(typeclass_info);
|
||||
TypeclassInfo&& typeclass_info) {
|
||||
namespace_stack.back()->typeclasses[typeclass] = std::move(typeclass_info);
|
||||
}
|
||||
|
||||
// AddVar ?? // TODO: decide
|
||||
|
||||
// FindFunction
|
||||
// FindType
|
||||
|
||||
|
|
@ -92,7 +79,6 @@ public:
|
|||
|
||||
private:
|
||||
std::vector<NamespaceInfo*> namespace_stack;
|
||||
std::optional<std::string> waiting_usage;
|
||||
|
||||
NamespaceInfo global_namespace_;
|
||||
std::vector<ImportInfo> imports_;
|
||||
|
|
|
|||
|
|
@ -56,7 +56,12 @@ using ImportSymbol = AnyIdentifier;
|
|||
|
||||
struct FunctionDefinition;
|
||||
struct TypeDefinition;
|
||||
struct DefinitionParameter;
|
||||
struct AnnotatedAbstractType;
|
||||
|
||||
// TypeIdentifier <-> AbstractTypeIdentifier <-> std::string
|
||||
using AnnotatedType = AnnotatedAbstractType;
|
||||
|
||||
using AnyAnnotatedType = AnnotatedType;
|
||||
|
||||
// Flow control -----------------
|
||||
|
||||
|
|
@ -168,8 +173,6 @@ using ScopedAnyName = AnyName;
|
|||
struct FunctionType;
|
||||
struct TupleType;
|
||||
struct VariantType;
|
||||
|
||||
struct AnnotatedType;
|
||||
struct ParametrizedType;
|
||||
struct TypeExpression;
|
||||
|
||||
|
|
@ -301,7 +304,7 @@ struct VariableDefinitionStatement {
|
|||
|
||||
struct FunctionDeclaration {
|
||||
NameOrOperatorIdentifier name;
|
||||
std::vector<std::unique_ptr<DefinitionParameter>> parameters;
|
||||
std::vector<std::unique_ptr<AnnotatedAbstractType>> parameters;
|
||||
std::unique_ptr<FunctionType> type;
|
||||
};
|
||||
|
||||
|
|
@ -332,16 +335,16 @@ struct TypeclassDefinitionStatement {
|
|||
struct FunctionDefinition {
|
||||
enum { Operator, Function } modifier;
|
||||
NameOrOperatorIdentifier name;
|
||||
std::vector<std::unique_ptr<DefinitionParameter>> parameters;
|
||||
std::vector<std::unique_ptr<AnnotatedAbstractType>> parameters;
|
||||
std::vector<ExtendedName> arguments;
|
||||
};
|
||||
|
||||
struct TypeDefinition {
|
||||
std::unique_ptr<AnnotatedType> type;
|
||||
std::vector<std::unique_ptr<DefinitionParameter>> parameters;
|
||||
std::vector<std::unique_ptr<AnnotatedAbstractType>> parameters;
|
||||
};
|
||||
|
||||
struct DefinitionParameter {
|
||||
struct AnnotatedAbstractType {
|
||||
AbstractTypeIdentifier type;
|
||||
std::vector<TypeclassUsage> typeclasses;
|
||||
};
|
||||
|
|
@ -444,7 +447,7 @@ struct TypeConstructor {
|
|||
};
|
||||
|
||||
struct LambdaFunction {
|
||||
std::vector<std::unique_ptr<DefinitionParameter>> parameters;
|
||||
std::vector<std::unique_ptr<AnnotatedAbstractType>> parameters;
|
||||
std::vector<ExtendedName> arguments;
|
||||
Expression expression;
|
||||
};
|
||||
|
|
@ -491,11 +494,6 @@ struct VariantType {
|
|||
std::vector<std::variant<Constructor, std::unique_ptr<TupleType>>> constructors;
|
||||
};
|
||||
|
||||
struct AnnotatedType {
|
||||
std::unique_ptr<TypeExpression> type_expression;
|
||||
std::vector<TypeclassUsage> annotations;
|
||||
};
|
||||
|
||||
struct ParametrizedType {
|
||||
std::unique_ptr<TypeExpression> type_expression;
|
||||
std::vector<TypeParameter> parameters;
|
||||
|
|
|
|||
|
|
@ -32,7 +32,8 @@ const std::string TypeclassDefinitionStatement = "typeclass_definition_statement
|
|||
const std::string ImportSymbol = "import_symbol";
|
||||
const std::string FunctionDefinition = "function_definition";
|
||||
const std::string TypeDefinition = "type_definition";
|
||||
const std::string DefinitionParameter = "definition_parameter";
|
||||
const std::string AnnotatedAbstractType = "annotated_abstract_type";
|
||||
const std::string AnnotatedType = "annotated_type";
|
||||
|
||||
// Flow control -----------------
|
||||
|
||||
|
|
@ -91,7 +92,6 @@ const std::string ScopedAnyName = "scoped_any_name";
|
|||
const std::string FunctionType = "function_type";
|
||||
const std::string TupleType = "tuple_type";
|
||||
const std::string VariantType = "variant_type";
|
||||
const std::string AnnotatedType = "annotated_type";
|
||||
const std::string ParametrizedType = "parametrized_type";
|
||||
const std::string TypeExpression = "type_expression";
|
||||
const std::string Constructor = "constructor";
|
||||
|
|
|
|||
|
|
@ -37,7 +37,7 @@ private:
|
|||
|
||||
void Visit(FunctionDefinition* node) override;
|
||||
void Visit(TypeDefinition* node) override;
|
||||
void Visit(DefinitionParameter* node) override;
|
||||
void Visit(AnyAnnotatedType* node) override;
|
||||
|
||||
// Flow control -----------------
|
||||
|
||||
|
|
@ -88,7 +88,6 @@ private:
|
|||
void Visit(FunctionType* node) override;
|
||||
void Visit(TupleType* node) override;
|
||||
void Visit(VariantType* node) override;
|
||||
void Visit(AnnotatedType* node) override;
|
||||
void Visit(ParametrizedType* node) override;
|
||||
void Visit(TypeExpression* node) override;
|
||||
|
||||
|
|
|
|||
|
|
@ -50,68 +50,46 @@ enum class BuiltInTypeInfo {
|
|||
FloatT,
|
||||
};
|
||||
|
||||
struct TypeUsageInfo { // TODO: typeclass_expression
|
||||
std::vector<std::string> param_names;
|
||||
std::vector<TypeInfo*> params;
|
||||
std::vector<std::string> arg_names; // ??, arg expr ??
|
||||
struct TypeUsageInfo {
|
||||
interpreter::tokens::ParametrizedType* node;
|
||||
TypeInfo* info = nullptr;
|
||||
};
|
||||
|
||||
struct ParameterInfo {
|
||||
std::string name;
|
||||
std::vector<std::string> param_names; // TODO: paramaters
|
||||
std::vector<TypeclassInfo*> param_types;
|
||||
std::string type;
|
||||
std::vector<interpreter::tokens::TypeclassUsage*> typeclass_nodes;
|
||||
std::vector<TypeclassInfo*> typeclass_types;
|
||||
};
|
||||
|
||||
struct SymbolDefinitionInfo {
|
||||
std::string name;
|
||||
std::vector<ParameterInfo> params;
|
||||
std::vector<std::string> arg_names;
|
||||
};
|
||||
|
||||
struct AnyTypeInfo : SymbolDefinitionInfo {
|
||||
std::variant<std::unique_ptr<VariantTypeInfo>,
|
||||
std::unique_ptr<TupleTypeInfo>,
|
||||
std::unique_ptr<BuiltInTypeInfo>> info;
|
||||
};
|
||||
|
||||
// struct VariableInfo {
|
||||
// std::string name;
|
||||
// std::optional<Value> value;
|
||||
// TypeInfo* type = nullptr;
|
||||
// };
|
||||
// TODO lambda functions as value
|
||||
// TODO: decide=
|
||||
|
||||
struct TupleTypeInfo {
|
||||
std::optional<std::string> name;
|
||||
std::vector<std::pair<std::optional<std::string>, AnyTypeInfo>> fields;
|
||||
};
|
||||
|
||||
struct VariantTypeInfo {
|
||||
std::optional<std::string> name;
|
||||
std::vector<std::variant<std::string, TupleTypeInfo>> constructors;
|
||||
// ?? any type instead of tuple type ??
|
||||
struct AbstractTypeInfo {
|
||||
enum { Basic, Abstract } modifier;
|
||||
ParameterInfo type;
|
||||
};
|
||||
|
||||
struct AliasTypeInfo {
|
||||
std::vector<std::string> params;
|
||||
bool isAnotherType;
|
||||
enum {Alias, Type, Let} modifier;
|
||||
std::vector<std::string> parameters;
|
||||
TypeUsageInfo value;
|
||||
};
|
||||
|
||||
struct TypeInfo { std::variant<AliasTypeInfo, AnyTypeInfo> type; };
|
||||
|
||||
// struct PointerInfo { // ??
|
||||
// VariableInfo* variable;
|
||||
// };
|
||||
|
||||
struct FunctionDeclarationInfo {
|
||||
std::vector<ParameterInfo> params;
|
||||
std::vector<TypeUsageInfo> arg_types;
|
||||
struct AnyTypeInfo {
|
||||
ParameterInfo type;
|
||||
std::vector<ParameterInfo> parameters;
|
||||
interpreter::tokens::AnyType* value;
|
||||
};
|
||||
|
||||
struct FunctionDefinitionInfo : SymbolDefinitionInfo {
|
||||
interpreter::tokens::SuperExpression* expression;
|
||||
struct TypeInfo { std::variant<AbstractTypeInfo, AliasTypeInfo, AnyTypeInfo> type; };
|
||||
|
||||
struct FunctionDeclarationInfo {
|
||||
std::vector<ParameterInfo> parameters;
|
||||
std::vector<interpreter::tokens::AnyType*> argument_type_nodes;
|
||||
std::vector<AnyTypeInfo*> argument_types;
|
||||
};
|
||||
|
||||
struct FunctionDefinitionInfo {
|
||||
std::vector<ParameterInfo> parameters;
|
||||
std::vector<std::string> argument_names;
|
||||
interpreter::tokens::SuperExpression* expression = nullptr;
|
||||
};
|
||||
|
||||
struct FunctionInfo {
|
||||
|
|
@ -119,7 +97,8 @@ struct FunctionInfo {
|
|||
FunctionDefinitionInfo definition;
|
||||
};
|
||||
|
||||
struct TypeclassInfo : SymbolDefinitionInfo {
|
||||
struct TypeclassInfo {
|
||||
std::vector<ParameterInfo> parameters;
|
||||
std::vector<FunctionDeclarationInfo> requirements;
|
||||
};
|
||||
|
||||
|
|
@ -129,18 +108,17 @@ struct ImportInfo {
|
|||
};
|
||||
|
||||
struct NamespaceInfo {
|
||||
enum Modifier { Const, Var };
|
||||
|
||||
std::unordered_map<std::string, TypeInfo> types;
|
||||
std::unordered_map<std::string, TypeclassInfo> typeclasses;
|
||||
// std::unordered_map<std::string, VariableInfo> variables; // TODO
|
||||
std::unordered_map<std::string, FunctionInfo> functions;
|
||||
std::unordered_map<std::string, std::vector<NamespaceInfo>> namespaces;
|
||||
std::optional<std::string> var;
|
||||
|
||||
std::optional<Modifier> modifier;
|
||||
std::optional<std::string> variable;
|
||||
std::string type_name;
|
||||
TypeInfo* type_info = nullptr;
|
||||
};
|
||||
|
||||
using AnyTypeVariant = std::variant<VariantTypeInfo,
|
||||
TupleTypeInfo,
|
||||
BuiltInTypeInfo>;
|
||||
|
||||
} // namespace info
|
||||
|
|
|
|||
|
|
@ -41,7 +41,7 @@ protected:
|
|||
|
||||
virtual void Visit(FunctionDefinition* node) {}
|
||||
virtual void Visit(TypeDefinition* node) {}
|
||||
virtual void Visit(DefinitionParameter* node) {}
|
||||
virtual void Visit(AnyAnnotatedType* node) {}
|
||||
|
||||
// Flow control -----------------
|
||||
|
||||
|
|
@ -106,7 +106,6 @@ protected:
|
|||
virtual void Visit(FunctionType* node) {}
|
||||
virtual void Visit(TupleType* node) {}
|
||||
virtual void Visit(VariantType* node) {}
|
||||
virtual void Visit(AnnotatedType* node) {}
|
||||
virtual void Visit(ParametrizedType* node) {}
|
||||
virtual void Visit(TypeExpression* node) {}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue