mirror of
https://codeberg.org/ProgramSnail/lang_2023.git
synced 2025-12-06 06:58:45 +00:00
likn_symbols _visitor done, but it is not tested yet
This commit is contained in:
parent
3c643d2759
commit
3d74b1383e
10 changed files with 236 additions and 427 deletions
73
include/abstract_types_context.hpp
Normal file
73
include/abstract_types_context.hpp
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <optional>
|
||||
#include <unordered_map>
|
||||
|
||||
// for clangd
|
||||
#include "utils.hpp"
|
||||
|
||||
namespace info {
|
||||
|
||||
class AbstractTypesContextManager {
|
||||
public:
|
||||
void EnterContext() {
|
||||
contexts_.emplace_back();
|
||||
}
|
||||
|
||||
void ExitContext() {
|
||||
if (contexts_.empty()) {
|
||||
// error
|
||||
}
|
||||
|
||||
contexts_.pop_back();
|
||||
}
|
||||
|
||||
void ExitFromAllContexts() {
|
||||
contexts_.clear();
|
||||
}
|
||||
|
||||
bool DefineType(const std::string& type, utils::IdType id) {
|
||||
return contexts_.back().DefineType(type, id);
|
||||
}
|
||||
|
||||
std::optional<utils::IdType> GetTypeId(const std::string& type) {
|
||||
for (ssize_t i = contexts_.size() - 1; i >= 0; --i) {
|
||||
auto maybe_type = contexts_[i].GetTypeId(type);
|
||||
if (maybe_type.has_value()) {
|
||||
return maybe_type.value();
|
||||
}
|
||||
}
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
private:
|
||||
class Context {
|
||||
public:
|
||||
bool DefineType(const std::string& type, utils::IdType id) {
|
||||
if (types_.count(type) > 0) {
|
||||
return false;
|
||||
}
|
||||
types_[type] = id;
|
||||
return true;
|
||||
}
|
||||
|
||||
std::optional<utils::IdType> GetTypeId(const std::string& type) {
|
||||
auto type_iter = types_.find(type);
|
||||
|
||||
if (type_iter == types_.end()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
return type_iter->second;
|
||||
}
|
||||
|
||||
private:
|
||||
std::unordered_map<std::string, utils::IdType> types_;
|
||||
};
|
||||
|
||||
std::vector<Context> contexts_;
|
||||
};
|
||||
|
||||
} // namespace info
|
||||
|
|
@ -1,29 +1,24 @@
|
|||
#pragma once
|
||||
|
||||
#include <iostream>
|
||||
|
||||
enum class ErrorVisitor {
|
||||
BuildVisitor,
|
||||
PrintVisitor,
|
||||
FindSymbolsVisitor,
|
||||
// ...
|
||||
};
|
||||
namespace error_handling {
|
||||
|
||||
// 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;
|
||||
inline void HandleParsingError(const std::string& message, std::pair<size_t, size_t> place) {
|
||||
std::cout << "Parsing Error: " << message << " at (" << place.first << ", " << place.second << ").\n";
|
||||
exit(1);
|
||||
}
|
||||
|
||||
inline void HandleInternalError(const std::string& message, const std::string& place) {
|
||||
std::cout << "Internal Error: " << message << " at " << place << ".\n";
|
||||
exit(1);
|
||||
}
|
||||
|
||||
inline void HandleTypecheckError(const std::string& message) { // TODO: place in code
|
||||
std::cout << "Typecheck Error: " << message << ".\n";
|
||||
exit(1);
|
||||
}
|
||||
|
||||
// ...
|
||||
|
||||
} // namespace error_handling
|
||||
|
|
|
|||
|
|
@ -63,6 +63,10 @@ public:
|
|||
TypeGraph* GetAbstractTypeGraph() {
|
||||
return global_info_.GetAbstractTypeGraph();
|
||||
}
|
||||
|
||||
const std::vector<std::string>& GetCurrentPath() {
|
||||
return current_path_;
|
||||
}
|
||||
private:
|
||||
NamespaceVisitor(GlobalInfo& global_info) : global_info_(global_info),
|
||||
namespace_stack_({&global_info.global_namespace_}) {}
|
||||
|
|
@ -75,6 +79,7 @@ public:
|
|||
const std::vector<std::string>& path);
|
||||
private:
|
||||
std::vector<NamespaceInfo*> namespace_stack_;
|
||||
std::vector<std::string> current_path_;
|
||||
|
||||
GlobalInfo& global_info_;
|
||||
};
|
||||
|
|
|
|||
|
|
@ -297,6 +297,7 @@ struct AliasDefinitionStatement {
|
|||
std::vector<AbstractTypeIdentifier> parameters;
|
||||
std::unique_ptr<ParametrizedType> value;
|
||||
|
||||
std::vector<utils::IdType> parameter_graph_ids_;
|
||||
utils::IdType type_id_;
|
||||
};
|
||||
|
||||
|
|
@ -321,6 +322,7 @@ struct FunctionDefinitionStatement {
|
|||
SuperExpression value;
|
||||
|
||||
utils::IdType function_id_;
|
||||
std::vector<utils::IdType> argument_graph_ids_;
|
||||
};
|
||||
|
||||
struct TypeDefinitionStatement {
|
||||
|
|
@ -335,6 +337,7 @@ struct AbstractTypeDefinitionStatement {
|
|||
enum { Basic, Abstract } modifier;
|
||||
std::unique_ptr<AnnotatedType> type;
|
||||
|
||||
utils::IdType type_graph_id_;
|
||||
utils::IdType type_id_;
|
||||
};
|
||||
|
||||
|
|
@ -362,6 +365,8 @@ struct TypeDefinition {
|
|||
struct AnnotatedAbstractType {
|
||||
AbstractTypeIdentifier type;
|
||||
std::vector<TypeclassUsage> typeclasses;
|
||||
|
||||
utils::IdType type_graph_id_;
|
||||
};
|
||||
|
||||
// ----------------- Flow control -----------------
|
||||
|
|
@ -518,6 +523,8 @@ struct TypeExpression {
|
|||
std::vector<TypeSubExpression> namespaces;
|
||||
AnyTypeIdentifier type;
|
||||
std::optional<size_t> array_size; // if array; 0 - dynamic size
|
||||
|
||||
utils::IdType type_id_;
|
||||
};
|
||||
|
||||
struct ExtendedScopedAnyType {
|
||||
|
|
@ -535,6 +542,8 @@ struct ParametrizedTypeclass {
|
|||
struct TypeclassExpression {
|
||||
std::vector<TypeSubExpression> namespaces;
|
||||
TypeclassIdentifier typeclass;
|
||||
|
||||
utils::IdType type_id_;
|
||||
};
|
||||
|
||||
// ----------------- Comments [IGNORE] -----------------
|
||||
|
|
|
|||
|
|
@ -3,15 +3,17 @@
|
|||
#include <ostream>
|
||||
|
||||
// for clangd
|
||||
#include "abstract_types_context.hpp"
|
||||
#include "visitor.hpp"
|
||||
|
||||
#include "global_info.hpp"
|
||||
|
||||
namespace interpreter {
|
||||
|
||||
// TODO, (maybe add VisitSourceFile?)
|
||||
class LinkSymbolsVisitor : public Visitor {
|
||||
public:
|
||||
explicit LinkSymbolsVisitor() {}
|
||||
explicit LinkSymbolsVisitor(info::GlobalInfo& global_info)
|
||||
: namespace_visitor_(global_info.CreateVisitor()) {}
|
||||
|
||||
private:
|
||||
// Sources -----------------
|
||||
|
|
@ -102,16 +104,18 @@ private:
|
|||
|
||||
// Identifiers, constants, etc. -----------------
|
||||
|
||||
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(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;
|
||||
|
||||
private:
|
||||
info::GlobalInfo::NamespaceVisitor namespace_visitor_;
|
||||
info::AbstractTypesContextManager abstract_types_;
|
||||
};
|
||||
|
||||
} // namespace interpreter
|
||||
|
|
|
|||
|
|
@ -15,13 +15,11 @@ namespace info {
|
|||
|
||||
class TypeGraph {
|
||||
public:
|
||||
size_t AddVertex(const std::vector<std::string>& path,
|
||||
const std::vector<std::string>& methods = {},
|
||||
size_t AddVertex(const std::vector<std::string>& methods = {},
|
||||
const std::vector<std::string>& typeclasses = {}) {
|
||||
is_calculated_ = false;
|
||||
|
||||
Vertex vertex;
|
||||
vertex.path = path;
|
||||
|
||||
for (auto& method : methods) {
|
||||
vertex.new_requirements.methods.insert(storage_.GetId(method));
|
||||
|
|
@ -35,14 +33,9 @@ public:
|
|||
edges_.emplace_back();
|
||||
back_edges_.emplace_back();
|
||||
|
||||
verticle_ids_[vertex.path] = verticles_.size() - 1;
|
||||
return verticles_.size() - 1;
|
||||
}
|
||||
|
||||
size_t FindVertex(const std::vector<std::string>& path) {
|
||||
return verticle_ids_[path];
|
||||
}
|
||||
|
||||
std::vector<std::string> VertexMethods(size_t id) {
|
||||
if (!is_calculated_) {
|
||||
// error
|
||||
|
|
@ -162,7 +155,6 @@ private:
|
|||
};
|
||||
|
||||
struct Vertex {
|
||||
std::vector<std::string> path;
|
||||
RequirementsData new_requirements;
|
||||
std::optional<size_t> cluster;
|
||||
};
|
||||
|
|
@ -212,7 +204,6 @@ private:
|
|||
}
|
||||
|
||||
private:
|
||||
std::unordered_map<std::vector<std::string>, size_t> verticle_ids_;
|
||||
std::vector<std::vector<size_t>> edges_;
|
||||
std::vector<std::vector<size_t>> back_edges_;
|
||||
std::vector<Vertex> verticles_;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue