Make type_mgr a class.

This commit is contained in:
Danila Fedorin 2020-09-15 19:19:58 -07:00
parent f4b5069125
commit 127f51041b
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 { void type_var::print(const type_mgr& mgr, std::ostream& to) const {
auto it = mgr.types.find(name); auto type = mgr.lookup(name);
if(it != mgr.types.end()) { if(type) {
it->second->print(mgr, to); type->print(mgr, to);
} else { } else {
to << name; to << name;
} }
@ -82,6 +82,12 @@ type_ptr type_mgr::new_arrow_type() {
return type_ptr(new type_arr(new_type(), new_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_ptr type_mgr::resolve(type_ptr t, type_var*& var) const {
type_var* cast; type_var* cast;

View File

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