mirror of
https://codeberg.org/ProgramSnail/lang_2023.git
synced 2025-12-08 16:08:45 +00:00
going to change symbol table structure
This commit is contained in:
parent
9c25cb1c6f
commit
782a48c4ff
11 changed files with 535 additions and 354 deletions
|
|
@ -33,10 +33,7 @@ private:
|
||||||
std::unordered_map<std::string, VariableInfo> variables_;
|
std::unordered_map<std::string, VariableInfo> variables_;
|
||||||
};
|
};
|
||||||
|
|
||||||
// TODO handle current namespace (for class names, function names, etc.)
|
// Context global_context_; // ??
|
||||||
// TODO ?? are global variables forbidden ??
|
|
||||||
|
|
||||||
Context global_context_;
|
|
||||||
std::vector<Context> contexts_;
|
std::vector<Context> contexts_;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,202 +6,90 @@
|
||||||
|
|
||||||
// for clangd
|
// for clangd
|
||||||
#include "symbols_info.hpp"
|
#include "symbols_info.hpp"
|
||||||
|
#include "utils.hpp"
|
||||||
|
|
||||||
namespace info {
|
namespace info {
|
||||||
|
|
||||||
// TODO: partitions
|
// TODO: partitions
|
||||||
class GlobalInfo {
|
class GlobalInfo {
|
||||||
|
friend class NamespaceVisitor;
|
||||||
public:
|
public:
|
||||||
GlobalInfo() {
|
class NamespaceVisitor {
|
||||||
namespace_stack_.push_back(&global_namespace_);
|
friend GlobalInfo;
|
||||||
|
NamespaceVisitor() = delete;
|
||||||
|
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);
|
||||||
|
|
||||||
|
void EnterNamespace(const std::string& name);
|
||||||
|
|
||||||
|
void ExitNamespace();
|
||||||
|
|
||||||
|
void ToGlobalNamespace();
|
||||||
|
|
||||||
|
utils::IdType AddFunctionDeclaration(const std::string& name,
|
||||||
|
FunctionDeclarationInfo&& function_declaration_info);
|
||||||
|
|
||||||
|
utils::IdType AddFunctionDefinition(const std::string& name,
|
||||||
|
FunctionDefinitionInfo&& function_definition_info);
|
||||||
|
|
||||||
|
utils::IdType AddType(const std::string& type, TypeInfo&& type_info);
|
||||||
|
|
||||||
|
utils::IdType AddTypeclass(const std::string& typeclass, TypeclassInfo&& typeclass_info);
|
||||||
|
|
||||||
|
std::optional<NamespaceInfo*> FindNamespace(const std::optional<std::vector<std::string>>& path);
|
||||||
|
|
||||||
|
std::optional<utils::IdType> FindFunction(const std::optional<std::vector<std::string>>& path,
|
||||||
|
const std::string& name);
|
||||||
|
|
||||||
|
std::optional<utils::IdType> FindMethod(const std::optional<std::vector<std::string>>& path,
|
||||||
|
const std::string& type,
|
||||||
|
const std::string& name);
|
||||||
|
|
||||||
|
std::optional<utils::IdType> FindType(const std::optional<std::vector<std::string>>& path,
|
||||||
|
const std::string type);
|
||||||
|
|
||||||
|
std::optional<utils::IdType> FindTypeclass(const std::optional<std::vector<std::string>>& path,
|
||||||
|
const std::string typeclass);
|
||||||
|
private:
|
||||||
|
NamespaceVisitor(GlobalInfo& global_info) : global_info_(global_info),
|
||||||
|
namespace_stack_({&global_info.global_namespace_}) {}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
std::optional<T*> FindSomething(const std::optional<std::vector<std::string>>& path,
|
||||||
|
std::function<std::optional<T*>(NamespaceInfo*)> search_func);
|
||||||
|
|
||||||
|
std::optional<NamespaceInfo*> FindNamespaceIn(NamespaceInfo* current_namespace,
|
||||||
|
const std::vector<std::string>& path);
|
||||||
|
private:
|
||||||
|
std::vector<NamespaceInfo*> namespace_stack_;
|
||||||
|
|
||||||
|
GlobalInfo& global_info_;
|
||||||
|
};
|
||||||
|
|
||||||
|
NamespaceVisitor CreateVisitor() {
|
||||||
|
return NamespaceVisitor(*this);
|
||||||
}
|
}
|
||||||
|
|
||||||
void AddImport(ImportInfo&& import_info, const std::optional<std::string>& name = std::nullopt) {
|
FunctionInfo* GetFunctionInfo(utils::IdType id) {
|
||||||
if (name.has_value()) {
|
return functions_.GetValue(id);
|
||||||
usages_[name.value()] = std::move(import_info);
|
|
||||||
} else {
|
|
||||||
imports_.push_back(std::move(import_info));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void AddEnterNamespace(const std::string& name,
|
TypeInfo* GetTypeInfo(utils::IdType id) {
|
||||||
const std::optional<NamespaceInfo::Modifier>& modifier = std::nullopt,
|
return types_.GetValue(id);
|
||||||
const std::optional<std::string>& variable = std::nullopt) {
|
|
||||||
NamespaceInfo* namespace_info = nullptr;
|
|
||||||
if (variable.has_value()) {
|
|
||||||
namespace_info = &namespace_stack_.back()->variable_namespaces[name].emplace_back();
|
|
||||||
namespace_stack_.push_back(namespace_info);
|
|
||||||
|
|
||||||
namespace_info->modifier = modifier;
|
|
||||||
namespace_info->variable = variable;
|
|
||||||
} else {
|
|
||||||
namespace_info = &namespace_stack_.back()->namespaces[name];
|
|
||||||
namespace_stack_.push_back(namespace_info);
|
|
||||||
}
|
|
||||||
|
|
||||||
namespace_info->type_name = name;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void EnterNamespace(const std::string& name) { // TODO: enter sibling namespace, etc.
|
TypeclassInfo* GetTypeclassInfo(utils::IdType id) {
|
||||||
namespace_stack_.push_back(&namespace_stack_.back()->namespaces[name]);
|
return typeclasses_.GetValue(id);
|
||||||
}
|
|
||||||
|
|
||||||
void ExitAllNameNamespaces(const std::string& name) {
|
|
||||||
while(namespace_stack_.size() > 1 && namespace_stack_.back()->type_name == name) {
|
|
||||||
namespace_stack_.pop_back();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void ExitNamespace() {
|
|
||||||
if (namespace_stack_.size() <= 1) {
|
|
||||||
// error
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
namespace_stack_.pop_back();
|
|
||||||
}
|
|
||||||
|
|
||||||
void ToGlobalNamespace() {
|
|
||||||
namespace_stack_.clear();
|
|
||||||
namespace_stack_.push_back(&global_namespace_);
|
|
||||||
}
|
|
||||||
|
|
||||||
void AddFunctionDeclaration(const std::string& name,
|
|
||||||
FunctionDeclarationInfo&& function_declaration_info) {
|
|
||||||
FunctionInfo* function_info = &namespace_stack_.back()->functions[name];
|
|
||||||
|
|
||||||
function_info->declaration = std::move(function_declaration_info);
|
|
||||||
}
|
|
||||||
|
|
||||||
void AddFunctionDefinition(const std::string& name,
|
|
||||||
FunctionDefinitionInfo&& function_definition_info) {
|
|
||||||
FunctionInfo* function_info = &namespace_stack_.back()->functions[name];
|
|
||||||
|
|
||||||
function_info->definition = std::move(function_definition_info);
|
|
||||||
}
|
|
||||||
|
|
||||||
void AddType(const std::string& type, TypeInfo&& type_info) {
|
|
||||||
namespace_stack_.back()->types[type] = std::move(type_info);
|
|
||||||
}
|
|
||||||
|
|
||||||
void AddTypeclass(const std::string& typeclass,
|
|
||||||
TypeclassInfo&& typeclass_info) {
|
|
||||||
namespace_stack_.back()->typeclasses[typeclass] = std::move(typeclass_info);
|
|
||||||
}
|
|
||||||
|
|
||||||
std::optional<NamespaceInfo*> FindNamespace(const std::vector<std::string>& path) {
|
|
||||||
return FindSomething<NamespaceInfo>(path,
|
|
||||||
[](NamespaceInfo* current_namespace) -> std::optional<NamespaceInfo*> {
|
|
||||||
return current_namespace;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
std::optional<FunctionInfo*> FindFunction(const std::vector<std::string>& path,
|
|
||||||
const std::string& name) {
|
|
||||||
return FindSomething<FunctionInfo>(path,
|
|
||||||
[name](NamespaceInfo* current_namespace) -> std::optional<FunctionInfo*> {
|
|
||||||
|
|
||||||
auto function_info_iter = current_namespace->functions.find(name);
|
|
||||||
if (function_info_iter == current_namespace->functions.end()) {
|
|
||||||
return std::nullopt;
|
|
||||||
}
|
|
||||||
|
|
||||||
return &function_info_iter->second;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
std::optional<FunctionInfo*> FindMethod(const std::vector<std::string>& path,
|
|
||||||
const std::string& type,
|
|
||||||
const std::string& name) {
|
|
||||||
// TODO: remove overhead
|
|
||||||
|
|
||||||
return FindSomething<FunctionInfo>(path,
|
|
||||||
[type, name](NamespaceInfo* current_namespace) -> std::optional<FunctionInfo*> {
|
|
||||||
|
|
||||||
auto variable_namespaces_iter = current_namespace->variable_namespaces.find(type);
|
|
||||||
if (variable_namespaces_iter == current_namespace->variable_namespaces.end()) {
|
|
||||||
return std::nullopt;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (auto& variable_namespace : variable_namespaces_iter->second) {
|
|
||||||
auto method_iter = variable_namespace.functions.find(name);
|
|
||||||
|
|
||||||
if (method_iter == variable_namespace.functions.end()) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
return &method_iter->second;
|
|
||||||
}
|
|
||||||
|
|
||||||
return std::nullopt;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
std::optional<TypeInfo*> FindType(const std::vector<std::string>& path,
|
|
||||||
const std::string type) {
|
|
||||||
return FindSomething<TypeInfo>(path,
|
|
||||||
[type](NamespaceInfo* current_namespace) -> std::optional<TypeInfo*> {
|
|
||||||
|
|
||||||
auto type_info_iter = current_namespace->types.find(type);
|
|
||||||
if (type_info_iter == current_namespace->types.end()) {
|
|
||||||
return std::nullopt;
|
|
||||||
}
|
|
||||||
|
|
||||||
return &type_info_iter->second;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
std::optional<TypeclassInfo*> FindTypeclass(const std::vector<std::string>& path,
|
|
||||||
const std::string typeclass) {
|
|
||||||
return FindSomething<TypeclassInfo>(path,
|
|
||||||
[typeclass](NamespaceInfo* current_namespace) -> std::optional<TypeclassInfo*> {
|
|
||||||
|
|
||||||
auto typeclass_info_iter = current_namespace->typeclasses.find(typeclass);
|
|
||||||
if (typeclass_info_iter == current_namespace->typeclasses.end()) {
|
|
||||||
return std::nullopt;
|
|
||||||
}
|
|
||||||
|
|
||||||
return &typeclass_info_iter->second;
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
template<typename T>
|
utils::Storage<FunctionInfo*> functions_;
|
||||||
std::optional<T*> FindSomething(const std::vector<std::string>& path,
|
utils::Storage<TypeInfo*> types_;
|
||||||
std::function<std::optional<T*>(NamespaceInfo*)> search_func) {
|
utils::Storage<TypeclassInfo*> typeclasses_;
|
||||||
for (ssize_t i = namespace_stack_.size(); i >= 0; --i) {
|
|
||||||
auto maybe_namespace = FindNamespaceIn(namespace_stack_[i], path);
|
|
||||||
|
|
||||||
if (!maybe_namespace.has_value()) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::optional<T*> result = search_func(maybe_namespace.value());
|
|
||||||
|
|
||||||
if (result.has_value()) {
|
|
||||||
return result.value();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return std::nullopt;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::optional<NamespaceInfo*> FindNamespaceIn(NamespaceInfo* current_namespace,
|
|
||||||
const std::vector<std::string>& path) {
|
|
||||||
NamespaceInfo* next_namespace = current_namespace;
|
|
||||||
for (auto& name : path) {
|
|
||||||
auto next_namespace_iter = next_namespace->namespaces.find(name);
|
|
||||||
if (next_namespace_iter == next_namespace->namespaces.end()) {
|
|
||||||
return std::nullopt;
|
|
||||||
}
|
|
||||||
next_namespace = &next_namespace_iter->second;
|
|
||||||
}
|
|
||||||
|
|
||||||
return next_namespace;
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
std::vector<NamespaceInfo*> namespace_stack_;
|
|
||||||
|
|
||||||
NamespaceInfo global_namespace_;
|
NamespaceInfo global_namespace_;
|
||||||
std::vector<ImportInfo> imports_;
|
std::vector<ImportInfo> imports_;
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,7 @@
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
|
||||||
// for clangd
|
// for clangd
|
||||||
|
#include "utils.hpp"
|
||||||
|
|
||||||
namespace interpreter {
|
namespace interpreter {
|
||||||
class Node;
|
class Node;
|
||||||
|
|
@ -110,9 +111,9 @@ struct ImportInfo {
|
||||||
struct NamespaceInfo {
|
struct NamespaceInfo {
|
||||||
enum Modifier { Const, Var };
|
enum Modifier { Const, Var };
|
||||||
|
|
||||||
std::unordered_map<std::string, TypeInfo> types;
|
std::unordered_map<std::string, utils::IdType> types;
|
||||||
std::unordered_map<std::string, TypeclassInfo> typeclasses;
|
std::unordered_map<std::string, utils::IdType> typeclasses;
|
||||||
std::unordered_map<std::string, FunctionInfo> functions;
|
std::unordered_map<std::string, utils::IdType> functions;
|
||||||
std::unordered_map<std::string, NamespaceInfo> namespaces;
|
std::unordered_map<std::string, NamespaceInfo> namespaces;
|
||||||
std::unordered_map<std::string, std::vector<NamespaceInfo>> variable_namespaces;
|
std::unordered_map<std::string, std::vector<NamespaceInfo>> variable_namespaces;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,14 +1,19 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
// for clangd
|
// for clangd
|
||||||
|
#include "type_info_contexts.hpp"
|
||||||
|
#include "symbols_info.hpp"
|
||||||
#include "visitor.hpp"
|
#include "visitor.hpp"
|
||||||
#include "global_info.hpp"
|
#include "global_info.hpp"
|
||||||
|
#include "type_graph.hpp"
|
||||||
|
|
||||||
namespace interpreter {
|
namespace interpreter {
|
||||||
|
|
||||||
class TypeCheckVisitor : public Visitor {
|
class TypeCheckVisitor : public Visitor {
|
||||||
public:
|
public:
|
||||||
explicit TypeCheckVisitor(info::GlobalInfo& global_info) : global_info_(global_info) {}
|
explicit TypeCheckVisitor(info::GlobalInfo& global_info,
|
||||||
|
info::TypeGraph& type_graph)
|
||||||
|
: global_info_(global_info), type_graph_(type_graph) {}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// Sources -----------------
|
// Sources -----------------
|
||||||
|
|
@ -99,9 +104,9 @@ private:
|
||||||
|
|
||||||
// Identifiers, constants, etc. -----------------
|
// Identifiers, constants, etc. -----------------
|
||||||
|
|
||||||
void Visit(ExtendedName* node) override;
|
// // void Visit(ExtendedName* node) override;
|
||||||
|
|
||||||
void Visit(std::string* node) override; // std::string
|
// // void Visit(std::string* node) override; // std::string
|
||||||
|
|
||||||
void Visit(FloatNumberLiteral* node) override;
|
void Visit(FloatNumberLiteral* node) override;
|
||||||
void Visit(NumberLiteral* node) override;
|
void Visit(NumberLiteral* node) override;
|
||||||
|
|
@ -110,6 +115,10 @@ private:
|
||||||
|
|
||||||
private:
|
private:
|
||||||
info::GlobalInfo& global_info_;
|
info::GlobalInfo& global_info_;
|
||||||
|
info::TypeGraph type_graph_;
|
||||||
|
info::TypeInfoContextManager context_manager_;
|
||||||
|
|
||||||
|
info::TypeInfo current_type_;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace interpreter
|
} // namespace interpreter
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,11 @@
|
||||||
#include <optional>
|
#include <optional>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
|
// for clangd
|
||||||
|
#include "utils.hpp"
|
||||||
|
|
||||||
|
namespace info {
|
||||||
|
|
||||||
// TODO: optimize recalc
|
// TODO: optimize recalc
|
||||||
|
|
||||||
class TypeGraph {
|
class TypeGraph {
|
||||||
|
|
@ -17,11 +22,11 @@ public:
|
||||||
vertex.path = path;
|
vertex.path = path;
|
||||||
|
|
||||||
for (auto& method : methods) {
|
for (auto& method : methods) {
|
||||||
vertex.new_requirements.methods.insert(GetHash(method));
|
vertex.new_requirements.methods.insert(storage_.GetId(method));
|
||||||
}
|
}
|
||||||
|
|
||||||
for (auto& typeclass : typeclasses) {
|
for (auto& typeclass : typeclasses) {
|
||||||
vertex.new_requirements.typeclasses.insert(GetHash(typeclass));
|
vertex.new_requirements.typeclasses.insert(storage_.GetId(typeclass));
|
||||||
}
|
}
|
||||||
|
|
||||||
verticles_.push_back(vertex);
|
verticles_.push_back(vertex);
|
||||||
|
|
@ -49,13 +54,13 @@ public:
|
||||||
methods.reserve(cluster_requirements_[cluster_id].methods.size());
|
methods.reserve(cluster_requirements_[cluster_id].methods.size());
|
||||||
|
|
||||||
for (auto& method : cluster_requirements_[cluster_id].methods) {
|
for (auto& method : cluster_requirements_[cluster_id].methods) {
|
||||||
methods.push_back(std::move(GetString(method)));
|
methods.push_back(std::move(storage_.GetValue(method)));
|
||||||
}
|
}
|
||||||
|
|
||||||
return methods;
|
return methods;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<std::string> VertexTypeclasses(size_t id) {
|
std::vector<utils::IdType> VertexTypeclasses(size_t id) {
|
||||||
if (!is_calculated_) {
|
if (!is_calculated_) {
|
||||||
// error
|
// error
|
||||||
return {};
|
return {};
|
||||||
|
|
@ -63,27 +68,27 @@ public:
|
||||||
|
|
||||||
size_t cluster_id = verticles_[id].cluster.value();
|
size_t cluster_id = verticles_[id].cluster.value();
|
||||||
|
|
||||||
std::vector<std::string> typeclasses;
|
std::vector<utils::IdType> typeclasses;
|
||||||
|
|
||||||
typeclasses.reserve(cluster_requirements_[cluster_id].typeclasses.size());
|
typeclasses.reserve(cluster_requirements_[cluster_id].typeclasses.size());
|
||||||
|
|
||||||
for (auto& typeclass : cluster_requirements_[cluster_id].typeclasses) {
|
for (auto& typeclass : cluster_requirements_[cluster_id].typeclasses) {
|
||||||
typeclasses.push_back(std::move(GetString(typeclass)));
|
typeclasses.push_back(typeclass);
|
||||||
}
|
}
|
||||||
|
|
||||||
return typeclasses;
|
return typeclasses;
|
||||||
}
|
}
|
||||||
|
|
||||||
void AddTypeclass(size_t id, const std::string& method) {
|
void AddMethod(size_t id, const std::string& method) {
|
||||||
is_calculated_ = false;
|
is_calculated_ = false;
|
||||||
|
|
||||||
verticles_[id].new_requirements.methods.insert(GetHash(method));
|
verticles_[id].new_requirements.methods.insert(storage_.GetId(method));
|
||||||
}
|
}
|
||||||
|
|
||||||
void AddMethod(size_t id, const std::string& typeclass) {
|
void AddTypeclass(size_t id, utils::IdType typeclass) {
|
||||||
is_calculated_ = false;
|
is_calculated_ = false;
|
||||||
|
|
||||||
verticles_[id].new_requirements.typeclasses.insert(GetHash(typeclass));
|
verticles_[id].new_requirements.typeclasses.insert(typeclass);
|
||||||
}
|
}
|
||||||
|
|
||||||
void AddEdge(size_t from, size_t to) {
|
void AddEdge(size_t from, size_t to) {
|
||||||
|
|
@ -150,8 +155,8 @@ public:
|
||||||
|
|
||||||
private:
|
private:
|
||||||
struct RequirementsData {
|
struct RequirementsData {
|
||||||
std::unordered_set<size_t> methods;
|
std::unordered_set<utils::IdType> methods;
|
||||||
std::unordered_set<size_t> typeclasses;
|
std::unordered_set<utils::IdType> typeclasses;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct Vertex {
|
struct Vertex {
|
||||||
|
|
@ -204,27 +209,8 @@ private:
|
||||||
return sorted_verticles;
|
return sorted_verticles;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t GetHash(const std::string& str) {
|
|
||||||
size_t hash = 0;
|
|
||||||
auto str_position = string_to_hash_.find(str);
|
|
||||||
|
|
||||||
if (str_position == string_to_hash_.end()) {
|
|
||||||
hash = hash_to_string_.size();
|
|
||||||
string_to_hash_[str] = hash;
|
|
||||||
hash_to_string_.push_back(str);
|
|
||||||
} else {
|
|
||||||
hash = str_position->second;
|
|
||||||
}
|
|
||||||
|
|
||||||
return hash;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string GetString(size_t hash) {
|
|
||||||
return hash_to_string_[hash];
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::unordered_map<std::vector<std::string>, size_t> verticle_ids_;
|
std::unordered_map<std::vector<std::string>, size_t> verticle_ids_;
|
||||||
std::vector<std::vector<size_t>> edges_;
|
std::vector<std::vector<size_t>> edges_;
|
||||||
std::vector<std::vector<size_t>> back_edges_;
|
std::vector<std::vector<size_t>> back_edges_;
|
||||||
std::vector<Vertex> verticles_;
|
std::vector<Vertex> verticles_;
|
||||||
|
|
@ -232,8 +218,9 @@ private:
|
||||||
std::vector<std::vector<size_t>> clusters_;
|
std::vector<std::vector<size_t>> clusters_;
|
||||||
std::vector<RequirementsData> cluster_requirements_;
|
std::vector<RequirementsData> cluster_requirements_;
|
||||||
|
|
||||||
std::vector<std::string> hash_to_string_;
|
utils::Storage<std::string> storage_;
|
||||||
std::unordered_map<std::string, size_t> string_to_hash_;
|
|
||||||
|
|
||||||
bool is_calculated_ = false;
|
bool is_calculated_ = false;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
} // namespace info
|
||||||
|
|
|
||||||
96
include/type_info_contexts.hpp
Normal file
96
include/type_info_contexts.hpp
Normal file
|
|
@ -0,0 +1,96 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
#include <unordered_map>
|
||||||
|
|
||||||
|
// for clangd
|
||||||
|
#include "symbols_info.hpp"
|
||||||
|
|
||||||
|
namespace info {
|
||||||
|
|
||||||
|
class TypeInfoContextManager {
|
||||||
|
public:
|
||||||
|
void CallFunction(const std::vector<std::string>& names,
|
||||||
|
const std::vector<TypeInfo*>& types) {
|
||||||
|
if (names.size() != types.size()) {
|
||||||
|
// error
|
||||||
|
}
|
||||||
|
|
||||||
|
contexts_.emplace_back(true);
|
||||||
|
|
||||||
|
for (size_t i = 0; i < names.size(); ++i) {
|
||||||
|
DefineVariable(names[i], types[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void EnterContext() {
|
||||||
|
contexts_.emplace_back(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ExitContext() {
|
||||||
|
if (contexts_.empty()) {
|
||||||
|
// error
|
||||||
|
}
|
||||||
|
|
||||||
|
contexts_.pop_back();
|
||||||
|
}
|
||||||
|
|
||||||
|
void ExitFromAllContexts() {
|
||||||
|
contexts_.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO Variable modifiers
|
||||||
|
bool DefineVariable(const std::string& name, TypeInfo* type) {
|
||||||
|
return contexts_.back().DefineVariable(name, type);
|
||||||
|
}
|
||||||
|
|
||||||
|
void EnterVariableContext(const std::string& name, TypeInfo* type) { // for variable namespaces, for loops
|
||||||
|
contexts_.push_back(false);
|
||||||
|
|
||||||
|
DefineVariable(name, type);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::optional<TypeInfo*> GetVariableType(const std::string& name) {
|
||||||
|
for (ssize_t i = contexts_.size() - 1; i >= 0; --i) {
|
||||||
|
auto maybe_type = contexts_[i].GetVariableType(name);
|
||||||
|
if (maybe_type.has_value()) {
|
||||||
|
return maybe_type.value();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
class Context {
|
||||||
|
public:
|
||||||
|
Context(bool hide_previous) : hide_previous_(hide_previous) {}
|
||||||
|
|
||||||
|
bool DefineVariable(const std::string& name, TypeInfo* type) {
|
||||||
|
if (variables_.count(name) > 0) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
variables_[name] = type;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::optional<TypeInfo*> GetVariableType(const std::string& name) {
|
||||||
|
auto variable_iter = variables_.find(name);
|
||||||
|
|
||||||
|
if (variable_iter == variables_.end()) {
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
|
||||||
|
return variable_iter->second;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool IsFirst() { return hide_previous_; }
|
||||||
|
private:
|
||||||
|
bool hide_previous_;
|
||||||
|
std::unordered_map<std::string, TypeInfo*> variables_;
|
||||||
|
};
|
||||||
|
|
||||||
|
std::vector<Context> contexts_;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace info
|
||||||
48
include/types_info.hpp
Normal file
48
include/types_info.hpp
Normal file
|
|
@ -0,0 +1,48 @@
|
||||||
|
// for clangd
|
||||||
|
#include "symbols_info.hpp"
|
||||||
|
|
||||||
|
namespace info {
|
||||||
|
|
||||||
|
struct BasicType {
|
||||||
|
std::string name;
|
||||||
|
std::vector<size_t> methods; // ??
|
||||||
|
std::vector<size_t> typeclasses;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct AbstractType {
|
||||||
|
size_t graph_id;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct TupleType;
|
||||||
|
struct VariantType;
|
||||||
|
struct ReferenceType;
|
||||||
|
using AnyType = std::variant<std::unique_ptr<BasicType>,
|
||||||
|
std::unique_ptr<AbstractType>,
|
||||||
|
std::unique_ptr<TupleType>,
|
||||||
|
std::unique_ptr<VariantType>,
|
||||||
|
std::unique_ptr<ReferenceType>>;
|
||||||
|
|
||||||
|
struct TupleType {
|
||||||
|
std::vector<std::pair<std::optional<std::string>, AnyType>> fields;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct VariantType {
|
||||||
|
std::vector<std::pair<std::string, TupleType>> constructors;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct ReferenceType {
|
||||||
|
enum { Reference, UniqueReference } reference_type;
|
||||||
|
AnyType type;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct FunctionalType {
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
struct Type {
|
||||||
|
size_t graph_id_;
|
||||||
|
TypeInfo* info = nullptr;
|
||||||
|
std::vector<std::string> requrements_;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace info
|
||||||
36
include/utils.hpp
Normal file
36
include/utils.hpp
Normal file
|
|
@ -0,0 +1,36 @@
|
||||||
|
#include <endian.h>
|
||||||
|
#include <vector>
|
||||||
|
#include <unordered_map>
|
||||||
|
|
||||||
|
namespace utils {
|
||||||
|
|
||||||
|
using IdType = std::size_t;
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
class Storage {
|
||||||
|
public:
|
||||||
|
IdType GetId(const T& value) {
|
||||||
|
IdType id = 0;
|
||||||
|
auto value_position = value_to_id_.find(value);
|
||||||
|
|
||||||
|
if (value_position == value_to_id_.end()) {
|
||||||
|
id = id_to_value_.size();
|
||||||
|
value_to_id_[value] = id;
|
||||||
|
id_to_value_.push_back(value);
|
||||||
|
} else {
|
||||||
|
id = value_position->second;
|
||||||
|
}
|
||||||
|
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
const T& GetValue(IdType id) {
|
||||||
|
return id_to_value_[id];
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::vector<T> id_to_value_;
|
||||||
|
std::unordered_map<T, IdType> value_to_id_;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace utils
|
||||||
BIN
src/.global_info.cpp.kate-swp
Normal file
BIN
src/.global_info.cpp.kate-swp
Normal file
Binary file not shown.
226
src/global_info.cpp
Normal file
226
src/global_info.cpp
Normal file
|
|
@ -0,0 +1,226 @@
|
||||||
|
// for clangd
|
||||||
|
#include "../include/global_info.hpp"
|
||||||
|
|
||||||
|
namespace info {
|
||||||
|
|
||||||
|
void GlobalInfo::NamespaceVisitor::AddImport(ImportInfo&& import_info,
|
||||||
|
const std::optional<std::string>& name) {
|
||||||
|
if (name.has_value()) {
|
||||||
|
global_info_.usages_[name.value()] = std::move(import_info);
|
||||||
|
} else {
|
||||||
|
global_info_.imports_.push_back(std::move(import_info));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void GlobalInfo::NamespaceVisitor::AddEnterNamespace(const std::string& name,
|
||||||
|
const std::optional<NamespaceInfo::Modifier>& modifier,
|
||||||
|
const std::optional<std::string>& variable) {
|
||||||
|
NamespaceInfo* namespace_info = nullptr;
|
||||||
|
if (variable.has_value()) {
|
||||||
|
namespace_info = &namespace_stack_.back()->variable_namespaces[name].emplace_back();
|
||||||
|
namespace_stack_.push_back(namespace_info);
|
||||||
|
|
||||||
|
namespace_info->modifier = modifier;
|
||||||
|
namespace_info->variable = variable;
|
||||||
|
} else {
|
||||||
|
namespace_info = &namespace_stack_.back()->namespaces[name];
|
||||||
|
namespace_stack_.push_back(namespace_info);
|
||||||
|
}
|
||||||
|
|
||||||
|
namespace_info->type_name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
void GlobalInfo::NamespaceVisitor::EnterNamespace(const std::string& name) { // TODO: enter sibling namespace, etc.
|
||||||
|
namespace_stack_.push_back(&namespace_stack_.back()->namespaces[name]);
|
||||||
|
}
|
||||||
|
|
||||||
|
void GlobalInfo::NamespaceVisitor::ExitNamespace() {
|
||||||
|
if (namespace_stack_.size() <= 1) {
|
||||||
|
// error
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
namespace_stack_.pop_back();
|
||||||
|
}
|
||||||
|
|
||||||
|
void GlobalInfo::NamespaceVisitor::ToGlobalNamespace() {
|
||||||
|
namespace_stack_.clear();
|
||||||
|
namespace_stack_.push_back(&global_info_.global_namespace_);
|
||||||
|
}
|
||||||
|
|
||||||
|
utils::IdType GlobalInfo::NamespaceVisitor::AddFunctionDeclaration(const std::string& name,
|
||||||
|
FunctionDeclarationInfo&& function_declaration_info) {
|
||||||
|
FunctionInfo* function_info = &namespace_stack_.back()->functions[name];
|
||||||
|
|
||||||
|
function_info->declaration = std::move(function_declaration_info);
|
||||||
|
|
||||||
|
return global_info_.functions_.GetId(function_info);
|
||||||
|
}
|
||||||
|
|
||||||
|
utils::IdType GlobalInfo::NamespaceVisitor::AddFunctionDefinition(const std::string& name,
|
||||||
|
FunctionDefinitionInfo&& function_definition_info) {
|
||||||
|
FunctionInfo* function_info = &namespace_stack_.back()->functions[name];
|
||||||
|
|
||||||
|
function_info->definition = std::move(function_definition_info);
|
||||||
|
|
||||||
|
return global_info_.functions_.GetId(function_info);
|
||||||
|
}
|
||||||
|
|
||||||
|
utils::IdType GlobalInfo::NamespaceVisitor::AddType(const std::string& type,
|
||||||
|
TypeInfo&& type_info) {
|
||||||
|
namespace_stack_.back()->types[type] = std::move(type_info);
|
||||||
|
}
|
||||||
|
|
||||||
|
utils::IdType GlobalInfo::NamespaceVisitor::AddTypeclass(const std::string& typeclass,
|
||||||
|
TypeclassInfo&& typeclass_info) {
|
||||||
|
namespace_stack_.back()->typeclasses[typeclass] = std::move(typeclass_info);
|
||||||
|
|
||||||
|
return global_info_.functions_.GetId(typeclass_info);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::optional<NamespaceInfo*> GlobalInfo::NamespaceVisitor::FindNamespace(const std::optional<std::vector<std::string>>& path) {
|
||||||
|
return FindSomething<NamespaceInfo>(path,
|
||||||
|
[] (NamespaceInfo* current_namespace) -> std::optional<NamespaceInfo*> {
|
||||||
|
return current_namespace;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
std::optional<utils::IdType> GlobalInfo::NamespaceVisitor::FindFunction(
|
||||||
|
const std::optional<std::vector<std::string>>& path,
|
||||||
|
const std::string& name) {
|
||||||
|
auto result = FindSomething<FunctionInfo>(path,
|
||||||
|
[name] (NamespaceInfo* current_namespace) -> std::optional<FunctionInfo*> {
|
||||||
|
|
||||||
|
auto function_info_iter = current_namespace->functions.find(name);
|
||||||
|
if (function_info_iter == current_namespace->functions.end()) {
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
|
||||||
|
return &function_info_iter->second;
|
||||||
|
});
|
||||||
|
|
||||||
|
if (result.has_value()) {
|
||||||
|
return global_info_.functions_.GetId(result.value());
|
||||||
|
} else {
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
std::optional<utils::IdType> GlobalInfo::NamespaceVisitor::FindMethod(
|
||||||
|
const std::optional<std::vector<std::string>>& path,
|
||||||
|
const std::string& type,
|
||||||
|
const std::string& name) {
|
||||||
|
// TODO: remove overhead
|
||||||
|
|
||||||
|
auto result = GlobalInfo::NamespaceVisitor::FindSomething<FunctionInfo>(path,
|
||||||
|
[type, name] (NamespaceInfo* current_namespace) -> std::optional<FunctionInfo*> {
|
||||||
|
|
||||||
|
auto variable_namespaces_iter = current_namespace->variable_namespaces.find(type);
|
||||||
|
if (variable_namespaces_iter == current_namespace->variable_namespaces.end()) {
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (auto& variable_namespace : variable_namespaces_iter->second) {
|
||||||
|
auto method_iter = variable_namespace.functions.find(name);
|
||||||
|
|
||||||
|
if (method_iter == variable_namespace.functions.end()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
return &method_iter->second;
|
||||||
|
}
|
||||||
|
|
||||||
|
return std::nullopt;
|
||||||
|
});
|
||||||
|
|
||||||
|
if (result.has_value()) {
|
||||||
|
return global_info_.functions_.GetId(result.value());
|
||||||
|
} else {
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
std::optional<utils::IdType> GlobalInfo::NamespaceVisitor::FindType(
|
||||||
|
const std::optional<std::vector<std::string>>& path,
|
||||||
|
const std::string type) {
|
||||||
|
auto result = FindSomething<TypeInfo>(path,
|
||||||
|
[type] (NamespaceInfo* current_namespace) -> std::optional<TypeInfo*> {
|
||||||
|
|
||||||
|
auto type_info_iter = current_namespace->types.find(type);
|
||||||
|
if (type_info_iter == current_namespace->types.end()) {
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
|
||||||
|
return &type_info_iter->second;
|
||||||
|
});
|
||||||
|
|
||||||
|
if (result.has_value()) {
|
||||||
|
return global_info_.types_.GetId(result.value());
|
||||||
|
} else {
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
std::optional<utils::IdType> GlobalInfo::NamespaceVisitor::FindTypeclass(
|
||||||
|
const std::optional<std::vector<std::string>>& path,
|
||||||
|
const std::string typeclass) {
|
||||||
|
auto result = FindSomething<TypeclassInfo>(path,
|
||||||
|
[typeclass] (NamespaceInfo* current_namespace) -> std::optional<TypeclassInfo*> {
|
||||||
|
|
||||||
|
auto typeclass_info_iter = current_namespace->typeclasses.find(typeclass);
|
||||||
|
if (typeclass_info_iter == current_namespace->typeclasses.end()) {
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
|
||||||
|
return &typeclass_info_iter->second;
|
||||||
|
});
|
||||||
|
|
||||||
|
if (result.has_value()) {
|
||||||
|
return global_info_.typeclasses_.GetId(result.value());
|
||||||
|
} else {
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
std::optional<T*> GlobalInfo::NamespaceVisitor::FindSomething(
|
||||||
|
const std::optional<std::vector<std::string>>& path,
|
||||||
|
std::function<std::optional<T*>(NamespaceInfo*)> search_func) {
|
||||||
|
for (ssize_t i = namespace_stack_.size() - 1; i >= 0; --i) {
|
||||||
|
NamespaceInfo* current_namespace = nullptr;
|
||||||
|
if (path.has_value()) {
|
||||||
|
auto maybe_namespace = FindNamespaceIn(namespace_stack_[i], path.value());
|
||||||
|
|
||||||
|
if (!maybe_namespace.has_value()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
current_namespace = namespace_stack_[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
std::optional<T*> result = search_func(current_namespace);
|
||||||
|
|
||||||
|
if (result.has_value()) {
|
||||||
|
return result.value();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::optional<NamespaceInfo*> GlobalInfo::NamespaceVisitor::FindNamespaceIn(
|
||||||
|
NamespaceInfo* current_namespace,
|
||||||
|
const std::vector<std::string>& path) {
|
||||||
|
NamespaceInfo* next_namespace = current_namespace;
|
||||||
|
for (auto& name : path) {
|
||||||
|
auto next_namespace_iter = next_namespace->namespaces.find(name);
|
||||||
|
if (next_namespace_iter == next_namespace->namespaces.end()) {
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
next_namespace = &next_namespace_iter->second;
|
||||||
|
}
|
||||||
|
|
||||||
|
return next_namespace;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace info
|
||||||
|
|
@ -7,7 +7,6 @@ namespace interpreter {
|
||||||
// Sources -----------------
|
// Sources -----------------
|
||||||
|
|
||||||
void TypeCheckVisitor::Visit(SourceFile* node) {
|
void TypeCheckVisitor::Visit(SourceFile* node) {
|
||||||
out_ << "[SourceFile] (\n\n";
|
|
||||||
for (auto& statement : node->statements) {
|
for (auto& statement : node->statements) {
|
||||||
if (std::holds_alternative<Partition>(statement)) {
|
if (std::holds_alternative<Partition>(statement)) {
|
||||||
Visit(&std::get<Partition>(statement));
|
Visit(&std::get<Partition>(statement));
|
||||||
|
|
@ -17,295 +16,197 @@ void TypeCheckVisitor::Visit(SourceFile* node) {
|
||||||
// error
|
// error
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
out_ << "\n)\n";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void TypeCheckVisitor::Visit(Sources* node) {
|
void TypeCheckVisitor::Visit(Sources* node) {
|
||||||
out_ << "[Sources](\n";
|
|
||||||
for (auto& statement : node->statements) {
|
for (auto& statement : node->statements) {
|
||||||
Visitor::Visit(statement);
|
Visitor::Visit(statement);
|
||||||
}
|
}
|
||||||
out_ << ")\n";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Namespaces, partitions -----------------
|
// Namespaces, partitions -----------------
|
||||||
|
|
||||||
void TypeCheckVisitor::Visit(Partition* node) {
|
void TypeCheckVisitor::Visit(Partition* node) {
|
||||||
out_ << "[Partition] ";
|
|
||||||
switch (node->name) {
|
|
||||||
case Partition::Test:
|
|
||||||
out_ << "TEST";
|
|
||||||
break;
|
|
||||||
case Partition::Interface:
|
|
||||||
out_ << "INTERFACE";
|
|
||||||
break;
|
|
||||||
case Partition::Core:
|
|
||||||
out_ << "CORE";
|
|
||||||
break;
|
|
||||||
case Partition::Lib:
|
|
||||||
out_ << "LIB";
|
|
||||||
break;
|
|
||||||
case Partition::Module:
|
|
||||||
out_ << "MODULE";
|
|
||||||
break;
|
|
||||||
case Partition::Exe:
|
|
||||||
out_ << "EXE";
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
out_ << " {\n";
|
|
||||||
Visit(node->scope.get());
|
Visit(node->scope.get());
|
||||||
out_ << "}\n";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void TypeCheckVisitor::Visit(Namespace* node) {
|
void TypeCheckVisitor::Visit(Namespace* node) {
|
||||||
out_ << "[Namespace] ";
|
|
||||||
if (node->name.has_value()) {
|
if (node->name.has_value()) {
|
||||||
if (node->modifier.has_value()) {
|
if (node->modifier.has_value()) {
|
||||||
switch (node->modifier.value()) {
|
switch (node->modifier.value()) { // TODO
|
||||||
case Namespace::Const:
|
case Namespace::Const:
|
||||||
out_ << "const ";
|
// TODO
|
||||||
break;
|
break;
|
||||||
case Namespace::Var:
|
case Namespace::Var:
|
||||||
out_ << "var ";
|
// TODO
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// error
|
// error
|
||||||
}
|
}
|
||||||
Visit(&node->name.value());
|
Visit(&node->name.value());
|
||||||
|
context_manager_.EnterVariableContext(node->name.value().name,
|
||||||
|
global_info_.FindType(std::nullopt, node->type).value()); // TODO ??
|
||||||
}
|
}
|
||||||
Visit(&node->type);
|
// global_info_.FindType(std::nullopt, node->type); // ??
|
||||||
out_ << "{\n";
|
|
||||||
|
global_info_.EnterNamespace(node->type);
|
||||||
|
|
||||||
Visit(node->scope.get());
|
Visit(node->scope.get());
|
||||||
out_ << "}\n";
|
|
||||||
|
global_info_.ExitNamespace();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Definitions -----------------
|
// Definitions -----------------
|
||||||
|
|
||||||
void TypeCheckVisitor::Visit(ImportStatement* node) {
|
void TypeCheckVisitor::Visit(ImportStatement* node) { // TODO
|
||||||
if (node->name.has_value()) {
|
// if (node->name.has_value()) {
|
||||||
out_ << "[Use " << node->name.value() << "] = ";
|
// }
|
||||||
}
|
// if (!node->symbols.empty()) {
|
||||||
out_ << "[Import " << node->module_name << "]";
|
// for (auto& symbol : node->symbols) {
|
||||||
if (!node->symbols.empty()) {
|
// Visit(&symbol);
|
||||||
out_ << " (\n";
|
// }
|
||||||
for (auto& symbol : node->symbols) {
|
// }
|
||||||
Visit(&symbol);
|
|
||||||
out_ << '\n';
|
|
||||||
}
|
|
||||||
out_ << ')';
|
|
||||||
}
|
|
||||||
out_ << '\n';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void TypeCheckVisitor::Visit(AliasDefinitionStatement* node) {
|
void TypeCheckVisitor::Visit(AliasDefinitionStatement* node) {
|
||||||
out_ << "[Alias ";
|
|
||||||
switch (node->modifier) {
|
switch (node->modifier) {
|
||||||
case AliasDefinitionStatement::Alias:
|
case AliasDefinitionStatement::Alias:
|
||||||
out_ << "alias";
|
|
||||||
break;
|
break;
|
||||||
case AliasDefinitionStatement::Type:
|
case AliasDefinitionStatement::Type:
|
||||||
out_ << "type";
|
|
||||||
break;
|
break;
|
||||||
case AliasDefinitionStatement::Let:
|
case AliasDefinitionStatement::Let:
|
||||||
out_ << "let";
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
out_ << ' ';
|
|
||||||
|
|
||||||
Visit(&node->type);
|
Visit(&node->type);
|
||||||
out_ << "] = (";
|
|
||||||
Visit(node->value.get());
|
Visit(node->value.get());
|
||||||
out_ << ")\n";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void TypeCheckVisitor::Visit(VariableDefinitionStatement* node) {
|
void TypeCheckVisitor::Visit(VariableDefinitionStatement* node) {
|
||||||
out_ << "[Variable ";
|
|
||||||
switch (node->modifier) {
|
switch (node->modifier) {
|
||||||
case VariableDefinitionStatement::Const:
|
case VariableDefinitionStatement::Const:
|
||||||
out_ << "const";
|
|
||||||
break;
|
break;
|
||||||
case VariableDefinitionStatement::Var:
|
case VariableDefinitionStatement::Var:
|
||||||
out_ << "var";
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
out_ << ' ';
|
|
||||||
Visitor::Visit(node->name);
|
Visitor::Visit(node->name);
|
||||||
out_ << "] = (";
|
|
||||||
Visitor::Visit(node->value);
|
Visitor::Visit(node->value);
|
||||||
out_ << ")\n";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void TypeCheckVisitor::Visit(FunctionDeclaration* node) {
|
void TypeCheckVisitor::Visit(FunctionDeclaration* node) {
|
||||||
out_ << "[FunctionDeclaration ";
|
|
||||||
Visit(&node->name);
|
Visit(&node->name);
|
||||||
out_ << "] (";
|
|
||||||
for (auto& parameter : node->parameters) {
|
for (auto& parameter : node->parameters) {
|
||||||
Visit(parameter.get());
|
Visit(parameter.get());
|
||||||
}
|
}
|
||||||
out_ << ") : (";
|
|
||||||
Visit(node->type.get());
|
Visit(node->type.get());
|
||||||
out_ << ")\n";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void TypeCheckVisitor::Visit(FunctionDefinitionStatement* node) {
|
void TypeCheckVisitor::Visit(FunctionDefinitionStatement* node) {
|
||||||
out_ << "[Function] (";
|
|
||||||
Visit(node->definition.get());
|
Visit(node->definition.get());
|
||||||
out_ << ") = (";
|
|
||||||
Visitor::Visit(node->value);
|
Visitor::Visit(node->value);
|
||||||
out_ << ")\n";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void TypeCheckVisitor::Visit(TypeDefinitionStatement* node) {
|
void TypeCheckVisitor::Visit(TypeDefinitionStatement* node) {
|
||||||
out_ << "[Type ";
|
|
||||||
switch (node->modifier) {
|
switch (node->modifier) {
|
||||||
case TypeDefinitionStatement::Struct:
|
case TypeDefinitionStatement::Struct:
|
||||||
out_ << "struct";
|
|
||||||
break;
|
break;
|
||||||
case TypeDefinitionStatement::Class:
|
case TypeDefinitionStatement::Class:
|
||||||
out_ << "class";
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
out_ << "] (";
|
|
||||||
Visit(node->definition.get());
|
Visit(node->definition.get());
|
||||||
out_ << ") = (";
|
|
||||||
Visitor::Visit(node->value);
|
Visitor::Visit(node->value);
|
||||||
out_ << ")\n";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void TypeCheckVisitor::Visit(AbstractTypeDefinitionStatement* node) {
|
void TypeCheckVisitor::Visit(AbstractTypeDefinitionStatement* node) {
|
||||||
out_ << "[AbstractType ";
|
|
||||||
switch (node->modifier) {
|
switch (node->modifier) {
|
||||||
case AbstractTypeDefinitionStatement::Basic:
|
case AbstractTypeDefinitionStatement::Basic:
|
||||||
out_ << "basic";
|
|
||||||
break;
|
break;
|
||||||
case AbstractTypeDefinitionStatement::Abstract:
|
case AbstractTypeDefinitionStatement::Abstract:
|
||||||
out_ << "abstract";
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
out_ << "] (";
|
|
||||||
Visit(node->type.get());
|
Visit(node->type.get());
|
||||||
out_ << ")\n";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void TypeCheckVisitor::Visit(TypeclassDefinitionStatement* node) {
|
void TypeCheckVisitor::Visit(TypeclassDefinitionStatement* node) {
|
||||||
out_ << "[Typeclass] (";
|
|
||||||
Visit(node->definition.get());
|
Visit(node->definition.get());
|
||||||
if (!node->requirements.empty()) {
|
if (!node->requirements.empty()) {
|
||||||
out_ << ") : (\n";
|
|
||||||
}
|
}
|
||||||
for (auto& requirement : node->requirements) {
|
for (auto& requirement : node->requirements) {
|
||||||
out_ << "& ";
|
|
||||||
Visit(requirement.get());
|
Visit(requirement.get());
|
||||||
out_ << "\n";
|
|
||||||
}
|
}
|
||||||
out_ << ")\n";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Definition parts
|
// Definition parts
|
||||||
|
|
||||||
void TypeCheckVisitor::Visit(FunctionDefinition* node) {
|
void TypeCheckVisitor::Visit(FunctionDefinition* node) {
|
||||||
out_ << "[FunctionDefinition ";
|
|
||||||
switch (node->modifier) {
|
switch (node->modifier) {
|
||||||
case FunctionDefinition::Operator:
|
case FunctionDefinition::Operator:
|
||||||
out_ << "operator";
|
|
||||||
break;
|
break;
|
||||||
case FunctionDefinition::Function:
|
case FunctionDefinition::Function:
|
||||||
out_ << "function";
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
out_ << ' ';
|
|
||||||
Visit(&node->name);
|
Visit(&node->name);
|
||||||
out_ << "]";
|
|
||||||
if (!node->parameters.empty()) {
|
if (!node->parameters.empty()) {
|
||||||
out_ << " (";
|
|
||||||
for (auto& parameter : node->parameters) {
|
for (auto& parameter : node->parameters) {
|
||||||
Visit(parameter.get());
|
Visit(parameter.get());
|
||||||
}
|
}
|
||||||
out_ << ')';
|
|
||||||
}
|
}
|
||||||
if (!node->arguments.empty()) {
|
if (!node->arguments.empty()) {
|
||||||
out_ << " : (";
|
|
||||||
for (auto& argument : node->arguments) {
|
for (auto& argument : node->arguments) {
|
||||||
Visit(&argument);
|
Visit(&argument);
|
||||||
}
|
}
|
||||||
out_ << ')';
|
|
||||||
}
|
}
|
||||||
out_ << ' ';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void TypeCheckVisitor::Visit(TypeDefinition* node) {
|
void TypeCheckVisitor::Visit(TypeDefinition* node) {
|
||||||
out_ << "[TypeDefinition] (";
|
|
||||||
Visit(node->type.get());
|
Visit(node->type.get());
|
||||||
out_ << ')';
|
|
||||||
if (!node->parameters.empty()) {
|
if (!node->parameters.empty()) {
|
||||||
out_ << '(';
|
|
||||||
for (auto& parameter : node->parameters) {
|
for (auto& parameter : node->parameters) {
|
||||||
Visit(parameter.get());
|
Visit(parameter.get());
|
||||||
}
|
}
|
||||||
out_ << ')';
|
|
||||||
}
|
}
|
||||||
out_ << ' ';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void TypeCheckVisitor::Visit(AnyAnnotatedType* node) {
|
void TypeCheckVisitor::Visit(AnyAnnotatedType* node) {
|
||||||
out_ << "[Annotated (Abstract) Type ";
|
|
||||||
Visit(&node->type);
|
Visit(&node->type);
|
||||||
out_ << ']';
|
|
||||||
if (!node->typeclasses.empty() > 0) {
|
if (!node->typeclasses.empty() > 0) {
|
||||||
out_ << " (";
|
|
||||||
for (auto& typeclass : node->typeclasses) {
|
for (auto& typeclass : node->typeclasses) {
|
||||||
Visitor::Visit(typeclass);
|
Visitor::Visit(typeclass);
|
||||||
}
|
}
|
||||||
out_ << ')';
|
|
||||||
}
|
}
|
||||||
out_ << ' ';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Flow control -----------------
|
// Flow control -----------------
|
||||||
|
|
||||||
void TypeCheckVisitor::Visit(MatchCase* node) {
|
void TypeCheckVisitor::Visit(MatchCase* node) {
|
||||||
out_ << "[MatchCase | ";
|
|
||||||
Visitor::Visit(node->value);
|
Visitor::Visit(node->value);
|
||||||
if (node->condition.has_value()) {
|
if (node->condition.has_value()) {
|
||||||
out_ << " ? ";
|
|
||||||
Visitor::Visit(node->condition.value());
|
Visitor::Visit(node->condition.value());
|
||||||
}
|
}
|
||||||
if (node->statement.has_value()) {
|
if (node->statement.has_value()) {
|
||||||
out_ << " -> ";
|
|
||||||
Visitor::Visit(node->statement.value());
|
Visitor::Visit(node->statement.value());
|
||||||
}
|
}
|
||||||
out_ << "]\n";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void TypeCheckVisitor::Visit(Match* node) {
|
void TypeCheckVisitor::Visit(Match* node) {
|
||||||
out_ << "[Match] (";
|
|
||||||
Visitor::Visit(node->value);
|
Visitor::Visit(node->value);
|
||||||
out_ << ") [with] (\n";
|
|
||||||
for (auto& match_case : node->matches) {
|
for (auto& match_case : node->matches) {
|
||||||
Visit(&match_case);
|
Visit(&match_case);
|
||||||
}
|
}
|
||||||
out_ << ")\n";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void TypeCheckVisitor::Visit(Condition* node) {
|
void TypeCheckVisitor::Visit(Condition* node) {
|
||||||
out_ << "[If] (";
|
|
||||||
Visitor::Visit(node->conditions[0]);
|
Visitor::Visit(node->conditions[0]);
|
||||||
out_ << ") [then] (\n";
|
|
||||||
Visitor::Visit(node->statements[0]);
|
Visitor::Visit(node->statements[0]);
|
||||||
out_ << ')';
|
|
||||||
for (size_t i = 1; i < node->conditions.size(); ++i) {
|
for (size_t i = 1; i < node->conditions.size(); ++i) {
|
||||||
out_ << " [elif] (";
|
|
||||||
Visitor::Visit(node->conditions[i]);
|
Visitor::Visit(node->conditions[i]);
|
||||||
out_ << ") [then] (\n";
|
|
||||||
Visitor::Visit(node->statements[i]);
|
Visitor::Visit(node->statements[i]);
|
||||||
out_ << ')';
|
|
||||||
}
|
}
|
||||||
if (node->statements.size() > node->conditions.size()) {
|
if (node->statements.size() > node->conditions.size()) {
|
||||||
out_ << " [else] (\n";
|
|
||||||
Visitor::Visit(node->statements[node->conditions.size()]);
|
Visitor::Visit(node->statements[node->conditions.size()]);
|
||||||
out_ << ')';
|
|
||||||
}
|
}
|
||||||
out_ << '\n';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void TypeCheckVisitor::Visit(DoWhileLoop* node) {
|
void TypeCheckVisitor::Visit(DoWhileLoop* node) {
|
||||||
|
|
@ -657,16 +558,8 @@ void TypeCheckVisitor::Visit(TypeclassExpression* node) {
|
||||||
|
|
||||||
// Identifiers, constants, etc. -----------------
|
// Identifiers, constants, etc. -----------------
|
||||||
|
|
||||||
void TypeCheckVisitor::Visit(ExtendedName* node) {
|
|
||||||
out_ << "[ExtendedName " << node->name << "] ";
|
|
||||||
}
|
|
||||||
|
|
||||||
void TypeCheckVisitor::Visit(std::string* node) { // std::string
|
|
||||||
out_ << "[Identifier " << *node << "] ";
|
|
||||||
}
|
|
||||||
|
|
||||||
void TypeCheckVisitor::Visit(FloatNumberLiteral* node) {
|
void TypeCheckVisitor::Visit(FloatNumberLiteral* node) {
|
||||||
out_ << "[FloatNumber " << node->value << "] ";
|
current_type_ = info::BuiltInTypeInfo::FloatT;
|
||||||
}
|
}
|
||||||
|
|
||||||
void TypeCheckVisitor::Visit(NumberLiteral* node) {
|
void TypeCheckVisitor::Visit(NumberLiteral* node) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue