mirror of
https://codeberg.org/ProgramSnail/lang_2023.git
synced 2025-12-05 22:48:42 +00:00
type_check_visitor part
This commit is contained in:
parent
d13faf104d
commit
94ef8702fb
11 changed files with 496 additions and 288 deletions
|
|
@ -1,66 +0,0 @@
|
|||
#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