mirror of
https://codeberg.org/ProgramSnail/lang_2023.git
synced 2025-12-05 22:48:42 +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
73
archived/abstract_types_contexts.hpp
Normal file
73
archived/abstract_types_contexts.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
|
||||
118
archived/symbols_info.hpp
Normal file
118
archived/symbols_info.hpp
Normal file
|
|
@ -0,0 +1,118 @@
|
|||
#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
|
||||
232
archived/type_graph.hpp
Normal file
232
archived/type_graph.hpp
Normal file
|
|
@ -0,0 +1,232 @@
|
|||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Temporary frozen, TODO
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#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();
|
||||
|
||||
groups_.AddElement();
|
||||
|
||||
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);
|
||||
groups_.Unite(u, v);
|
||||
}
|
||||
|
||||
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_;
|
||||
utils::GroupsManager groups_;
|
||||
|
||||
bool is_calculated_ = false;
|
||||
};
|
||||
|
||||
} // namespace info
|
||||
66
archived/typeclass_graph.hpp
Normal file
66
archived/typeclass_graph.hpp
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
#pragma once
|
||||
|
||||
#include <string>
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Temporary frozen, TODO
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#pragma once
|
||||
|
||||
#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
|
||||
Loading…
Add table
Add a link
Reference in a new issue