Make type_mgr a class.

This commit is contained in:
Danila Fedorin 2020-09-15 19:19:58 -07:00
parent 55e4e61906
commit c17d532802
2 changed files with 27 additions and 18 deletions

View File

@ -26,9 +26,9 @@ type_ptr type_scheme::instantiate(type_mgr& mgr) const {
}
void type_var::print(const type_mgr& mgr, std::ostream& to) const {
auto it = mgr.types.find(name);
if(it != mgr.types.end()) {
it->second->print(mgr, to);
auto type = mgr.lookup(name);
if(type) {
type->print(mgr, to);
} else {
to << name;
}
@ -82,6 +82,12 @@ type_ptr type_mgr::new_arrow_type() {
return type_ptr(new type_arr(new_type(), new_type()));
}
type_ptr type_mgr::lookup(const std::string& var) const {
auto types_it = types.find(var);
if(types_it != types.end()) return types_it->second;
return nullptr;
}
type_ptr type_mgr::resolve(type_ptr t, type_var*& var) const {
type_var* cast;

View File

@ -7,7 +7,7 @@
#include <optional>
#include "location.hh"
struct type_mgr;
class type_mgr;
struct type {
virtual ~type() = default;
@ -90,10 +90,12 @@ struct type_app : public type {
void print(const type_mgr& mgr, std::ostream& to) const;
};
struct type_mgr {
class type_mgr {
private:
int last_id = 0;
std::map<std::string, type_ptr> types;
public:
std::string new_type_name();
type_ptr new_type();
type_ptr new_arrow_type();
@ -102,6 +104,7 @@ struct type_mgr {
type_ptr substitute(
const std::map<std::string, type_ptr>& subst,
const type_ptr& t) const;
type_ptr lookup(const std::string& var) const;
type_ptr resolve(type_ptr t, type_var*& var) const;
void bind(const std::string& s, type_ptr t);
void find_free(const type_ptr& t, std::set<std::string>& into) const;