diff --git a/13/type.cpp b/13/type.cpp index 4dd18e1..fb3ab68 100644 --- a/13/type.cpp +++ b/13/type.cpp @@ -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; diff --git a/13/type.hpp b/13/type.hpp index af417b6..cb1d77e 100644 --- a/13/type.hpp +++ b/13/type.hpp @@ -7,7 +7,7 @@ #include #include "location.hh" -struct type_mgr; +class type_mgr; struct type { virtual ~type() = default; @@ -90,20 +90,23 @@ struct type_app : public type { void print(const type_mgr& mgr, std::ostream& to) const; }; -struct type_mgr { - int last_id = 0; - std::map types; +class type_mgr { + private: + int last_id = 0; + std::map types; - std::string new_type_name(); - type_ptr new_type(); - type_ptr new_arrow_type(); + public: + std::string new_type_name(); + type_ptr new_type(); + type_ptr new_arrow_type(); - void unify(type_ptr l, type_ptr r, const std::optional& loc = std::nullopt); - type_ptr substitute( - const std::map& subst, - const type_ptr& t) 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& into) const; - void find_free(const type_scheme_ptr& t, std::set& into) const; + void unify(type_ptr l, type_ptr r, const std::optional& loc = std::nullopt); + type_ptr substitute( + const std::map& 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& into) const; + void find_free(const type_scheme_ptr& t, std::set& into) const; };