Add 'internal' types.

This commit is contained in:
Danila Fedorin 2020-09-09 18:08:38 -07:00
parent 92a9ec2021
commit c1a8dc4557
3 changed files with 22 additions and 21 deletions

View File

@ -25,7 +25,7 @@ void yy::parser::error(const yy::location& loc, const std::string& msg) {
void typecheck_program( void typecheck_program(
definition_group& defs, definition_group& defs,
type_mgr& mgr, type_env_ptr& env) { type_mgr& mgr, type_env_ptr& env) {
type_ptr int_type = type_ptr(new type_base("Int")); type_ptr int_type = type_ptr(new type_internal("Int"));
env->bind_type("Int", int_type); env->bind_type("Int", int_type);
type_ptr int_type_app = type_ptr(new type_app(int_type)); type_ptr int_type_app = type_ptr(new type_app(int_type));

View File

@ -5,8 +5,6 @@
#include <vector> #include <vector>
#include "error.hpp" #include "error.hpp"
bool type::is_arrow(const type_mgr& mgr) const { return false; }
void type_scheme::print(const type_mgr& mgr, std::ostream& to) const { void type_scheme::print(const type_mgr& mgr, std::ostream& to) const {
if(forall.size() != 0) { if(forall.size() != 0) {
to << "forall "; to << "forall ";
@ -36,21 +34,17 @@ void type_var::print(const type_mgr& mgr, std::ostream& to) const {
} }
} }
bool type_var::is_arrow(const type_mgr& mgr) const {
auto it = mgr.types.find(name);
if(it != mgr.types.end()) {
return it->second->is_arrow(mgr);
} else {
return false;
}
}
void type_base::print(const type_mgr& mgr, std::ostream& to) const { void type_base::print(const type_mgr& mgr, std::ostream& to) const {
to << name; to << name;
} }
void type_internal::print(const type_mgr& mgr, std::ostream& to) const {
to << "!" << name;
}
void type_arr::print(const type_mgr& mgr, std::ostream& to) const { void type_arr::print(const type_mgr& mgr, std::ostream& to) const {
bool print_parenths = left->is_arrow(mgr); type_var* var;
bool print_parenths = dynamic_cast<type_arr*>(mgr.resolve(left, var).get()) != nullptr;
if(print_parenths) to << "("; if(print_parenths) to << "(";
left->print(mgr, to); left->print(mgr, to);
if(print_parenths) to << ")"; if(print_parenths) to << ")";
@ -58,10 +52,6 @@ void type_arr::print(const type_mgr& mgr, std::ostream& to) const {
right->print(mgr, to); right->print(mgr, to);
} }
bool type_arr::is_arrow(const type_mgr& mgr) const {
return true;
}
void type_app::print(const type_mgr& mgr, std::ostream& to) const { void type_app::print(const type_mgr& mgr, std::ostream& to) const {
constructor->print(mgr, to); constructor->print(mgr, to);
to << "*"; to << "*";
@ -131,7 +121,10 @@ void type_mgr::unify(type_ptr l, type_ptr r, const std::optional<yy::location>&
return; return;
} else if((lid = dynamic_cast<type_base*>(l.get())) && } else if((lid = dynamic_cast<type_base*>(l.get())) &&
(rid = dynamic_cast<type_base*>(r.get()))) { (rid = dynamic_cast<type_base*>(r.get()))) {
if(lid->name == rid->name && lid->arity == rid->arity) return; if(lid->name == rid->name &&
lid->arity == rid->arity &&
lid->is_internal() == rid->is_internal())
return;
} else if((lapp = dynamic_cast<type_app*>(l.get())) && } else if((lapp = dynamic_cast<type_app*>(l.get())) &&
(rapp = dynamic_cast<type_app*>(r.get()))) { (rapp = dynamic_cast<type_app*>(r.get()))) {
unify(lapp->constructor, rapp->constructor, loc); unify(lapp->constructor, rapp->constructor, loc);

View File

@ -13,7 +13,6 @@ struct type {
virtual ~type() = default; virtual ~type() = default;
virtual void print(const type_mgr& mgr, std::ostream& to) const = 0; virtual void print(const type_mgr& mgr, std::ostream& to) const = 0;
virtual bool is_arrow(const type_mgr& mgr) const;
}; };
using type_ptr = std::shared_ptr<type>; using type_ptr = std::shared_ptr<type>;
@ -37,7 +36,6 @@ struct type_var : public type {
: name(std::move(n)) {} : name(std::move(n)) {}
void print(const type_mgr& mgr, std::ostream& to) const; void print(const type_mgr& mgr, std::ostream& to) const;
bool is_arrow(const type_mgr& mgr) const;
}; };
struct type_base : public type { struct type_base : public type {
@ -48,6 +46,17 @@ struct type_base : public type {
: name(std::move(n)), arity(a) {} : name(std::move(n)), arity(a) {}
void print(const type_mgr& mgr, std::ostream& to) const; void print(const type_mgr& mgr, std::ostream& to) const;
virtual bool is_internal() const { return false; }
};
struct type_internal : public type_base {
type_internal(std::string n, int32_t a = 0)
: type_base(std::move(n), a) {}
void print(const type_mgr& mgr, std::ostream& to) const;
bool is_internal() const { return true; }
}; };
struct type_data : public type_base { struct type_data : public type_base {
@ -69,7 +78,6 @@ struct type_arr : public type {
: left(std::move(l)), right(std::move(r)) {} : left(std::move(l)), right(std::move(r)) {}
void print(const type_mgr& mgr, std::ostream& to) const; void print(const type_mgr& mgr, std::ostream& to) const;
bool is_arrow(const type_mgr& mgr) const;
}; };
struct type_app : public type { struct type_app : public type {