mirror of
https://codeberg.org/ProgramSnail/lang_2023.git
synced 2025-12-05 22:48:42 +00:00
part of find_symbols_visitor done
This commit is contained in:
parent
f88a23194f
commit
18e85f794f
6 changed files with 827 additions and 57 deletions
29
include/error_handling.hpp
Normal file
29
include/error_handling.hpp
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
#include <iostream>
|
||||
|
||||
enum class ErrorVisitor {
|
||||
BuildVisitor,
|
||||
PrintVisitor,
|
||||
FindSymbolsVisitor,
|
||||
// ...
|
||||
};
|
||||
|
||||
// TODO
|
||||
inline void handle_error(std::string message, ErrorVisitor visitor) { // TODO: add place in code
|
||||
std::string visitor_str;
|
||||
switch (visitor) {
|
||||
case ErrorVisitor::BuildVisitor:
|
||||
visitor_str = "Build Visitor";
|
||||
break;
|
||||
case ErrorVisitor::PrintVisitor:
|
||||
visitor_str = "Print Visitor";
|
||||
break;
|
||||
case ErrorVisitor::FindSymbolsVisitor:
|
||||
visitor_str = "Find Symbols Visitor";
|
||||
break;
|
||||
// ...
|
||||
default:
|
||||
break;
|
||||
}
|
||||
std::cerr << "Error: " << message << " in " << visitor_str;
|
||||
exit(1);
|
||||
}
|
||||
123
include/find_symbols_visitor.hpp
Normal file
123
include/find_symbols_visitor.hpp
Normal file
|
|
@ -0,0 +1,123 @@
|
|||
#pragma once
|
||||
|
||||
#include <ostream>
|
||||
#include <any>
|
||||
|
||||
// for clangd
|
||||
#include "visitor.hpp"
|
||||
#include "global_info.hpp"
|
||||
|
||||
// TODO:
|
||||
// add/remove global variables
|
||||
//
|
||||
|
||||
namespace interpreter {
|
||||
|
||||
class FindSymbolsVisitor : public Visitor {
|
||||
public:
|
||||
explicit FindSymbolsVisitor(info::GlobalInfo& global_info) : global_info_(global_info) {}
|
||||
|
||||
private:
|
||||
//// void Visit(Node* node) override;
|
||||
|
||||
// Sources -----------------
|
||||
|
||||
void Visit(SourceFile* node) override;
|
||||
void Visit(Sources* node) override;
|
||||
|
||||
// Namespaces, partitions -----------------
|
||||
|
||||
void Visit(Partition* node) override;
|
||||
void Visit(Namespace* node) override;
|
||||
|
||||
// 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(FunctionDeclaration* node) override;
|
||||
void Visit(FunctionDefinition* node) override;
|
||||
void Visit(AliasTypeDefinition* node) override;
|
||||
void Visit(TypeDefinition* node) override;
|
||||
void Visit(TypeclassDefinition* 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;
|
||||
|
||||
// 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;
|
||||
|
||||
// Statements, expressions, blocks, etc. -----------------
|
||||
|
||||
//// void Visit(Block* node) override;
|
||||
//// void Visit(ScopedStatement* node) override;
|
||||
|
||||
//// void Visit(LoopControlExpression& node) override; // enum
|
||||
|
||||
// Operators
|
||||
|
||||
void Visit(BinaryOperatorExpression* node) override;
|
||||
void Visit(UnaryOperatorExpression* 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;
|
||||
|
||||
// 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;
|
||||
|
||||
// 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;
|
||||
|
||||
// Typeclass
|
||||
|
||||
void Visit(AnnotatedTypeclass* node) override;
|
||||
void Visit(ParametrizedTypeclass* node) override;
|
||||
void Visit(TypeclassExpression* node) override;
|
||||
|
||||
// Identifiers, constants, etc. -----------------
|
||||
|
||||
//// void Visit(AnyIdentifier* node) override; // std::string
|
||||
|
||||
//// 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_;
|
||||
std::any current_info_;
|
||||
};
|
||||
|
||||
} // namespace interpreter
|
||||
|
|
@ -8,18 +8,95 @@
|
|||
|
||||
namespace info {
|
||||
|
||||
// TODO: partitions
|
||||
class GlobalInfo {
|
||||
public:
|
||||
GlobalInfo();
|
||||
GlobalInfo() {
|
||||
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;
|
||||
} else {
|
||||
imports_.push_back(std::forward(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) {
|
||||
NamespaceInfo* namespace_info = &namespace_stack.back()->namespaces[type].emplace_back();
|
||||
namespace_stack.push_back(namespace_info);
|
||||
|
||||
namespace_info->var = var;
|
||||
namespace_info->type_name = type;
|
||||
}
|
||||
|
||||
void ExitNamespace() {
|
||||
if (namespace_stack.size() <= 1) {
|
||||
// error
|
||||
return;
|
||||
}
|
||||
|
||||
namespace_stack.pop_back();
|
||||
}
|
||||
|
||||
void ToGlobalNamespace() {
|
||||
namespace_stack.clear();
|
||||
namespace_stack.push_back(&global_namespace_);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void AddFunctionDeclaration(const std::string& name,
|
||||
T&& function_declaration_info) {
|
||||
FunctionInfo* function_info = &namespace_stack.back()->functions[name];
|
||||
|
||||
function_info->declaration = std::forward(function_declaration_info);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void AddFunctionDefinition(const std::string& name,
|
||||
T&& function_definition_info) {
|
||||
FunctionInfo* function_info = &namespace_stack.back()->functions[name];
|
||||
|
||||
function_info->definition = std::forward(function_definition_info);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void AddType(const std::string& type,
|
||||
T&& type_info) {
|
||||
&namespace_stack.back()->types[type] = std::forward(type_info);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void AddTypeclass(const std::string& typeclass,
|
||||
T&& typeclass_info) {
|
||||
namespace_stack.back()->typeclasses[typeclass] = std::forward(typeclass_info);
|
||||
}
|
||||
|
||||
// AddVar ?? // TODO: decide
|
||||
|
||||
// FindFunction
|
||||
// FindType
|
||||
|
||||
// FindVar ??
|
||||
|
||||
// ?? EnterNamespace / ExitNamespace ??
|
||||
// concurrent work ??
|
||||
// AddType, AddFunction
|
||||
// TODO
|
||||
private:
|
||||
std::unordered_map<std::string, NamespaceInfo> namespaces_;
|
||||
std::vector<NamespaceInfo*> namespace_stack;
|
||||
std::optional<std::string> waiting_usage;
|
||||
|
||||
NamespaceInfo global_namespace_;
|
||||
// lock for concurrency ??
|
||||
std::vector<ImportInfo> imports_;
|
||||
std::unordered_map<std::string, ImportInfo> usages_;
|
||||
};
|
||||
|
||||
} // namespace info
|
||||
|
|
|
|||
|
|
@ -1,10 +1,12 @@
|
|||
#pragma once
|
||||
|
||||
#include "interpreter_tree.hpp"
|
||||
#include <string>
|
||||
#include <variant>
|
||||
#include <optional>
|
||||
#include <vector>
|
||||
#include <optional>
|
||||
#include <unordered_map>
|
||||
#include <memory>
|
||||
|
||||
// for clangd
|
||||
|
||||
|
|
@ -34,63 +36,110 @@ struct Value {
|
|||
};
|
||||
// better variant value storage (then string) ??
|
||||
|
||||
struct Info {
|
||||
std::string name;
|
||||
};
|
||||
|
||||
struct VariantTypeInfo;
|
||||
struct TupleTypeInfo;
|
||||
struct AliasTypeInfo;
|
||||
|
||||
struct TypeInfo;
|
||||
|
||||
struct TypeclassInfo;
|
||||
|
||||
enum class BuiltInTypeInfo {
|
||||
StringT,
|
||||
IntT,
|
||||
FloatT,
|
||||
};
|
||||
|
||||
using TypeInfo = std::variant<VariantTypeInfo, TupleTypeInfo, AliasTypeInfo, BuiltInTypeInfo>;
|
||||
struct TypeUsageInfo {
|
||||
std::vector<std::string> param_names;
|
||||
std::vector<TypeInfo*> param_types;
|
||||
std::vector<std::string> arg_names; // ??, arg expr ??
|
||||
};
|
||||
|
||||
struct VariableInfo : public Info {
|
||||
Value value;
|
||||
struct ParameterInfo {
|
||||
std::string name;
|
||||
std::vector<std::string> param_names;
|
||||
std::vector<TypeclassInfo*> param_types;
|
||||
std::vector<std::string> arg_names; // ??
|
||||
};
|
||||
|
||||
struct AnyTypeInfo {
|
||||
std::vector<ParameterInfo> params; // ??
|
||||
std::vector<std::string> arg_names;
|
||||
|
||||
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 values
|
||||
|
||||
// TODO aliases ??
|
||||
|
||||
struct TupleTypeInfo : public Info {
|
||||
std::vector<std::pair<std::optional<std::string>, TypeInfo>> fields;
|
||||
struct TupleTypeInfo {
|
||||
std::optional<std::string> name;
|
||||
std::vector<std::pair<std::optional<std::string>, AnyTypeInfo>> fields;
|
||||
};
|
||||
|
||||
struct VariantTypeInfo : public Info {
|
||||
struct VariantTypeInfo {
|
||||
std::optional<std::string> name;
|
||||
std::vector<std::variant<std::string, TupleTypeInfo>> constructors;
|
||||
// ?? any type instead of tuple type ??
|
||||
};
|
||||
|
||||
struct AliasTypeInfo : public Info {
|
||||
bool isAnotherType; // = true by default ??
|
||||
TypeInfo* type = nullptr;
|
||||
struct AliasTypeInfo {
|
||||
std::vector<std::string> params;
|
||||
bool isAnotherType;
|
||||
TypeUsageInfo value;
|
||||
};
|
||||
|
||||
struct TypeclassInfo : public Info {
|
||||
// TODO
|
||||
struct TypeInfo { std::variant<AliasTypeInfo, AnyTypeInfo> type; };
|
||||
|
||||
// struct PointerInfo { // ??
|
||||
// VariableInfo* variable;
|
||||
// };
|
||||
|
||||
struct FunctionDeclarationInfo {
|
||||
std::vector<ParameterInfo> params;
|
||||
std::vector<TypeUsageInfo> arg_types;
|
||||
};
|
||||
|
||||
struct PointerInfo {
|
||||
VariableInfo* variable;
|
||||
struct FunctionDefinitionInfo {
|
||||
std::vector<ParameterInfo> params; // TODO: dublicates ??
|
||||
std::vector<std::string> arg_names;
|
||||
|
||||
interpreter::tokens::SuperExpression* expression;
|
||||
};
|
||||
|
||||
struct FunctionInfo : public Info {
|
||||
interpreter::Node* definition;
|
||||
std::vector<VariableInfo> args;
|
||||
struct FunctionInfo {
|
||||
FunctionDeclarationInfo declaration;
|
||||
FunctionDefinitionInfo definition;
|
||||
// add requirements ??
|
||||
};
|
||||
|
||||
struct NamespaceInfo : public Info {
|
||||
struct TypeclassInfo {
|
||||
std::vector<ParameterInfo> params;
|
||||
std::vector<std::string> arg_names;
|
||||
std::vector<FunctionDeclarationInfo> requirements;
|
||||
};
|
||||
|
||||
struct ImportInfo {
|
||||
std::string module_name;
|
||||
std::vector<std::string> symbols;
|
||||
};
|
||||
|
||||
struct NamespaceInfo {
|
||||
std::unordered_map<std::string, TypeInfo> types;
|
||||
std::unordered_map<std::string, VariableInfo> variables;
|
||||
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, NamespaceInfo> namespaces;
|
||||
std::unordered_map<std::string, std::vector<NamespaceInfo>> namespaces;
|
||||
std::optional<std::string> var;
|
||||
std::string type_name;
|
||||
TypeInfo* type_info = nullptr;
|
||||
};
|
||||
|
||||
} // namespace info
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue