mirror of
https://codeberg.org/ProgramSnail/lang_2023.git
synced 2025-12-25 16:28:45 +00:00
variable namespace, function declaration fixes
This commit is contained in:
parent
c31b20fa24
commit
f973f65b5b
17 changed files with 511 additions and 98 deletions
|
|
@ -1,73 +0,0 @@
|
|||
#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
|
||||
|
|
@ -6,7 +6,7 @@
|
|||
|
||||
// for clangd
|
||||
#include "symbols_info.hpp"
|
||||
#include "type_graph.hpp"
|
||||
#include "type_manager.hpp"
|
||||
#include "utils.hpp"
|
||||
|
||||
namespace info {
|
||||
|
|
@ -22,8 +22,8 @@ public:
|
|||
void AddImport(ImportInfo&& 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<NamespaceInfo::Modifier>& modifier = std::nullopt,
|
||||
const std::optional<std::string>& variable = std::nullopt);
|
||||
|
||||
void EnterNamespace(const std::string& name);
|
||||
|
||||
|
|
@ -60,8 +60,8 @@ public:
|
|||
return global_info_.CreateVisitor();
|
||||
}
|
||||
|
||||
TypeGraph* GetAbstractTypeGraph() {
|
||||
return global_info_.GetAbstractTypeGraph();
|
||||
type::TypeManager* GetTypeManager() {
|
||||
return global_info_.GetTypeManager();
|
||||
}
|
||||
|
||||
GlobalInfo* GetGlobalInfo() {
|
||||
|
|
@ -92,8 +92,8 @@ public:
|
|||
return NamespaceVisitor(*this);
|
||||
}
|
||||
|
||||
TypeGraph* GetAbstractTypeGraph() {
|
||||
return &abstract_type_graph_;
|
||||
type::TypeManager* GetTypeManager() {
|
||||
return &type_manager_;
|
||||
}
|
||||
|
||||
// TODO: remember about vector realloc
|
||||
|
|
@ -116,7 +116,7 @@ private:
|
|||
std::vector<TypeInfo> types_;
|
||||
std::vector<TypeclassInfo> typeclasses_;
|
||||
|
||||
info::TypeGraph abstract_type_graph_;
|
||||
type::TypeManager type_manager_;
|
||||
|
||||
NamespaceInfo global_namespace_;
|
||||
std::vector<ImportInfo> imports_;
|
||||
|
|
|
|||
|
|
@ -268,8 +268,7 @@ struct Sources {
|
|||
|
||||
struct Namespace {
|
||||
enum Modifier { Const, Var };
|
||||
std::optional<Modifier> modifier;
|
||||
std::optional<ExtendedName> name;
|
||||
std::optional<Modifier> modifier; // modifier => variable namespace
|
||||
TypeIdentifier type;
|
||||
std::unique_ptr<Sources> scope;
|
||||
|
||||
|
|
@ -358,7 +357,6 @@ struct TypeclassDefinitionStatement {
|
|||
struct FunctionDefinition {
|
||||
enum { Operator, Function } modifier;
|
||||
ExtendedName name;
|
||||
std::vector<std::unique_ptr<AnnotatedAbstractType>> parameters;
|
||||
std::vector<ExtendedName> arguments;
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -1,225 +0,0 @@
|
|||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
#include <unordered_set>
|
||||
#include <unordered_map>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
|
||||
// for clangd
|
||||
#include "utils.hpp"
|
||||
|
||||
namespace info {
|
||||
|
||||
// TODO: optimize recalc
|
||||
|
||||
class TypeGraph {
|
||||
public:
|
||||
size_t AddVertex(const std::vector<std::string>& methods = {},
|
||||
const std::vector<std::string>& typeclasses = {}) {
|
||||
is_calculated_ = false;
|
||||
|
||||
Vertex vertex;
|
||||
|
||||
for (auto& method : methods) {
|
||||
vertex.new_requirements.methods.insert(storage_.GetId(method));
|
||||
}
|
||||
|
||||
for (auto& typeclass : typeclasses) {
|
||||
vertex.new_requirements.typeclasses.insert(storage_.GetId(typeclass));
|
||||
}
|
||||
|
||||
verticles_.push_back(vertex);
|
||||
edges_.emplace_back();
|
||||
back_edges_.emplace_back();
|
||||
|
||||
return verticles_.size() - 1;
|
||||
}
|
||||
|
||||
std::vector<std::string> VertexMethods(size_t id) {
|
||||
if (!is_calculated_) {
|
||||
// error
|
||||
return {};
|
||||
}
|
||||
|
||||
size_t cluster_id = verticles_[id].cluster.value();
|
||||
|
||||
std::vector<std::string> methods;
|
||||
|
||||
methods.reserve(cluster_requirements_[cluster_id].methods.size());
|
||||
|
||||
for (auto& method : cluster_requirements_[cluster_id].methods) {
|
||||
methods.push_back(std::move(storage_.GetValue(method)));
|
||||
}
|
||||
|
||||
return methods;
|
||||
}
|
||||
|
||||
std::vector<utils::IdType> VertexTypeclasses(size_t id) {
|
||||
if (!is_calculated_) {
|
||||
// error
|
||||
return {};
|
||||
}
|
||||
|
||||
size_t cluster_id = verticles_[id].cluster.value();
|
||||
|
||||
std::vector<utils::IdType> typeclasses;
|
||||
|
||||
typeclasses.reserve(cluster_requirements_[cluster_id].typeclasses.size());
|
||||
|
||||
for (auto& typeclass : cluster_requirements_[cluster_id].typeclasses) {
|
||||
typeclasses.push_back(typeclass);
|
||||
}
|
||||
|
||||
return typeclasses;
|
||||
}
|
||||
|
||||
void AddMethod(size_t id, const std::string& method) {
|
||||
is_calculated_ = false;
|
||||
|
||||
verticles_[id].new_requirements.methods.insert(storage_.GetId(method));
|
||||
}
|
||||
|
||||
void AddTypeclass(size_t id, utils::IdType typeclass) {
|
||||
is_calculated_ = false;
|
||||
|
||||
verticles_[id].new_requirements.typeclasses.insert(typeclass);
|
||||
}
|
||||
|
||||
void AddEdge(size_t from, size_t to) {
|
||||
is_calculated_ = false;
|
||||
|
||||
edges_[from].push_back(to);
|
||||
back_edges_[to].push_back(from);
|
||||
}
|
||||
|
||||
void SetEqual(size_t u, size_t v) {
|
||||
AddEdge(u, v);
|
||||
AddEdge(v, u);
|
||||
// TODO: set equality
|
||||
}
|
||||
|
||||
void Calculate() {
|
||||
if (is_calculated_) {
|
||||
return;
|
||||
}
|
||||
|
||||
auto clusters = FindClusters();
|
||||
|
||||
std::vector<RequirementsData> cluster_requirements(clusters.size());
|
||||
std::vector<std::unordered_set<size_t>> cluster_edges(clusters.size());
|
||||
|
||||
for (size_t i = 0; i < clusters.size(); ++i) {
|
||||
for (auto& vertex_id : clusters[i]) {
|
||||
for (auto& method : verticles_[vertex_id].new_requirements.methods) {
|
||||
cluster_requirements[i].methods.insert(method);
|
||||
}
|
||||
verticles_[vertex_id].new_requirements.methods.clear();
|
||||
|
||||
for (auto& typeclass : verticles_[vertex_id].new_requirements.typeclasses) {
|
||||
cluster_requirements[i].typeclasses.insert(typeclass);
|
||||
}
|
||||
verticles_[vertex_id].new_requirements.methods.clear();
|
||||
|
||||
for (auto& edge : edges_[vertex_id]) {
|
||||
cluster_edges[i].insert(edge);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: check, that clusters are top sorted
|
||||
for (size_t i = 0; i < clusters.size(); ++i) {
|
||||
for (auto& edge : cluster_edges[i]) {
|
||||
for (auto& method : cluster_requirements[edge].methods) {
|
||||
cluster_requirements[i].methods.insert(method);
|
||||
}
|
||||
|
||||
for (auto& typeclass : cluster_requirements[edge].typeclasses) {
|
||||
cluster_requirements[i].typeclasses.insert(typeclass);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < clusters.size(); ++i) {
|
||||
for (auto& vertex_id : clusters[i]) {
|
||||
verticles_[vertex_id].cluster = i;
|
||||
}
|
||||
}
|
||||
|
||||
clusters_ = std::move(clusters);
|
||||
cluster_requirements_ = std::move(cluster_requirements);
|
||||
|
||||
is_calculated_ = true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
private:
|
||||
struct RequirementsData {
|
||||
std::unordered_set<utils::IdType> methods;
|
||||
std::unordered_set<utils::IdType> typeclasses;
|
||||
};
|
||||
|
||||
struct Vertex {
|
||||
RequirementsData new_requirements;
|
||||
std::optional<size_t> cluster;
|
||||
};
|
||||
|
||||
std::vector<std::vector<size_t>> FindClusters() {
|
||||
std::vector<std::vector<size_t>> clusters;
|
||||
|
||||
auto sorted_verticles = TopSort();
|
||||
|
||||
std::vector<size_t> marks(sorted_verticles.size(), 0);
|
||||
for (size_t i = 0; i < sorted_verticles.size(); ++i) {
|
||||
if (marks[i] == 0) {
|
||||
clusters.emplace_back();
|
||||
VisitDfs(i, clusters[i], marks, back_edges_, clusters.size());
|
||||
}
|
||||
}
|
||||
|
||||
return clusters;
|
||||
}
|
||||
|
||||
void VisitDfs(size_t id,
|
||||
std::vector<size_t>& verticles,
|
||||
std::vector<size_t>& marks,
|
||||
const std::vector<std::vector<size_t>>& edges,
|
||||
size_t mark) {
|
||||
if (marks[id] != 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
marks[id] = mark;
|
||||
verticles.push_back(id);
|
||||
|
||||
for (size_t i = 0; i < edges[id].size(); ++i) {
|
||||
VisitDfs(id, verticles, marks, edges, mark);
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<size_t> TopSort() {
|
||||
std::vector<size_t> sorted_verticles;
|
||||
std::vector<size_t> marks(verticles_.size(), 0);
|
||||
|
||||
for (size_t i = 0; i < marks.size(); ++i) {
|
||||
VisitDfs(i, sorted_verticles, marks, edges_, 1);
|
||||
}
|
||||
|
||||
return sorted_verticles;
|
||||
}
|
||||
|
||||
private:
|
||||
std::vector<std::vector<size_t>> edges_;
|
||||
std::vector<std::vector<size_t>> back_edges_;
|
||||
std::vector<Vertex> verticles_;
|
||||
|
||||
std::vector<std::vector<size_t>> clusters_;
|
||||
std::vector<RequirementsData> cluster_requirements_;
|
||||
|
||||
utils::Storage<std::string> storage_;
|
||||
|
||||
bool is_calculated_ = false;
|
||||
};
|
||||
|
||||
} // namespace info
|
||||
26
include/type_manager.hpp
Normal file
26
include/type_manager.hpp
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
|
||||
// for clangd
|
||||
#include "types.hpp"
|
||||
#include "utils.hpp"
|
||||
|
||||
namespace info::type {
|
||||
|
||||
class TypeManager {
|
||||
public:
|
||||
template<typename T>
|
||||
void AddType(const T& type) {
|
||||
types_.emplace(type);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
T GetType(utils::IdType type_id) {
|
||||
return std::get<T>(types_.at(type_id));
|
||||
}
|
||||
private:
|
||||
std::vector<Type> types_;
|
||||
};
|
||||
|
||||
} // namespace info::type
|
||||
|
|
@ -1,60 +0,0 @@
|
|||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
|
||||
// for calngd
|
||||
#include "utils.hpp"
|
||||
#include "interpreter_tree.hpp"
|
||||
#include "error_handling.hpp"
|
||||
|
||||
namespace info {
|
||||
|
||||
class TypeclassGraph {
|
||||
public:
|
||||
struct TypeclassMethod {
|
||||
std::string name;
|
||||
interpreter::tokens::FunctionDeclaration* definition = nullptr;
|
||||
};
|
||||
|
||||
struct ParametrizedTypeclass {
|
||||
utils::IdType typeclass;
|
||||
std::vector<utils::IdType> parameter_ids; // ??
|
||||
};
|
||||
|
||||
struct TypeclassVertex {
|
||||
std::vector<std::pair<std::string, std::vector<ParametrizedTypeclass>>> parameters;
|
||||
std::vector<TypeclassMethod> methods;
|
||||
std::vector<ParametrizedTypeclass> dependencies;
|
||||
interpreter::tokens::TypeclassDefinitionStatement* definition = nullptr;
|
||||
};
|
||||
|
||||
utils::IdType AddTypeclass(const TypeclassVertex& typeclass) { // TODO: universal reference
|
||||
for (auto& method : typeclass.methods) {
|
||||
if (method_to_typeclass_.count(method.name) != 0) {
|
||||
error_handling::HandleTypecheckError("");
|
||||
}
|
||||
}
|
||||
for (auto& method : typeclass.methods) {
|
||||
method_to_typeclass_[method.name] = typeclasses_.size();
|
||||
}
|
||||
typeclasses_.push_back(typeclass);
|
||||
return typeclasses_.size() - 1;
|
||||
}
|
||||
std::optional<utils::IdType> FindMethodTypeclass(const std::string& name) {
|
||||
auto method_iter = method_to_typeclass_.find(name);
|
||||
if (method_iter == method_to_typeclass_.end()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
return method_iter->second;
|
||||
}
|
||||
|
||||
const TypeclassVertex& GetTypeclass(utils::IdType id) {
|
||||
return typeclasses_.at(id);
|
||||
}
|
||||
private:
|
||||
std::unordered_map<std::string, utils::IdType> method_to_typeclass_;
|
||||
std::vector<TypeclassVertex> typeclasses_;
|
||||
};
|
||||
|
||||
} // namespace info
|
||||
|
|
@ -10,16 +10,16 @@
|
|||
|
||||
namespace info::type {
|
||||
|
||||
// TODO: differentiate abstract and basic types
|
||||
struct AbstractType {
|
||||
utils::IdType graph_id;
|
||||
std::vector<utils::IdType> paramaters;
|
||||
};
|
||||
// // Temporary frozen, TODO
|
||||
// struct AbstractType {
|
||||
// utils::IdType graph_id;
|
||||
// std::vector<utils::IdType> paramaters;
|
||||
// };
|
||||
|
||||
// TODO: check, if defined type is variant, etc.
|
||||
struct DefinedType {
|
||||
utils::IdType type_id;
|
||||
std::vector<utils::IdType> paramaters;
|
||||
utils::IdType type_id; // in defined types
|
||||
utils::IdType type; // in types manager, created using context types
|
||||
};
|
||||
|
||||
enum class InternalType {
|
||||
|
|
@ -31,7 +31,7 @@ enum class InternalType {
|
|||
Unit,
|
||||
};
|
||||
|
||||
struct TupleType { //
|
||||
struct TupleType {
|
||||
std::optional<std::string> type;
|
||||
std::vector<std::pair<std::optional<std::string>, utils::IdType>> fields;
|
||||
};
|
||||
|
|
@ -60,8 +60,7 @@ struct ArrayType {
|
|||
std::optional<utils::IdType> elements_type;
|
||||
};
|
||||
|
||||
using Type = std::variant<AbstractType,
|
||||
DefinedType,
|
||||
using Type = std::variant<DefinedType,
|
||||
InternalType,
|
||||
TupleType,
|
||||
VariantType,
|
||||
|
|
@ -40,9 +40,11 @@ private:
|
|||
std::unordered_map<T, IdType> value_to_id_;
|
||||
};
|
||||
|
||||
class Union { // TODO: recall right algorithm name
|
||||
class GroupsManager { // TODO: recall right algorithm name
|
||||
public:
|
||||
Union(size_t n) {
|
||||
GroupsManager() = default;
|
||||
|
||||
explicit GroupsManager(size_t n) {
|
||||
edges_.resize(n);
|
||||
ranks_.resize(n);
|
||||
|
||||
|
|
@ -52,26 +54,34 @@ public:
|
|||
}
|
||||
}
|
||||
|
||||
void Connect(size_t u, size_t v) { // TODO: recall choice algorithm
|
||||
u = GetRoot(u);
|
||||
v = GetRoot(v);
|
||||
void Unite(size_t u, size_t v) { // TODO: recall choice algorithm
|
||||
u = GetGroupRoot(u);
|
||||
v = GetGroupRoot(v);
|
||||
if (ranks_[v] >= ranks_[u]) {
|
||||
edges_[u] = v;
|
||||
ranks_[v] = std::max(ranks_[u] + 1, ranks_[v]);
|
||||
} else {
|
||||
edges_[v] = u;
|
||||
// always ranks_[u] > ranks_[v]
|
||||
}
|
||||
}
|
||||
|
||||
bool IsConnected(size_t u, size_t v) {
|
||||
return GetRoot(u) == GetRoot(v);
|
||||
bool IsInOneGroup(size_t u, size_t v) {
|
||||
return GetGroupRoot(u) == GetGroupRoot(v);
|
||||
}
|
||||
|
||||
size_t GetRoot(size_t v) {
|
||||
size_t AddElement() {
|
||||
size_t id = edges_.size();
|
||||
edges_.push_back(id);
|
||||
ranks_.push_back(1);
|
||||
return id;
|
||||
}
|
||||
|
||||
size_t GetGroupRoot(size_t v) {
|
||||
if (edges_[v] == v) {
|
||||
return v;
|
||||
}
|
||||
return edges_[v] = GetRoot(edges_[v]);
|
||||
return edges_[v] = GetGroupRoot(edges_[v]);
|
||||
}
|
||||
private:
|
||||
std::vector<size_t> edges_;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue