Get rid of more constructors and make mangled names optional.

This commit is contained in:
Danila Fedorin 2020-09-18 14:09:03 -07:00
parent a336b27b6c
commit d153af5212
3 changed files with 13 additions and 8 deletions

View File

@ -9,10 +9,10 @@ type_ptr parsed_type_app::to_type(
const type_env& e) const { const type_env& e) const {
auto parent_type = e.lookup_type(name); auto parent_type = e.lookup_type(name);
if(parent_type == nullptr) if(parent_type == nullptr)
throw type_error(std::string("no such type or type constructor ") + name); throw type_error("no such type or type constructor " + name);
type_base* base_type; type_base* base_type;
if(!(base_type = dynamic_cast<type_base*>(parent_type.get()))) if(!(base_type = dynamic_cast<type_base*>(parent_type.get())))
throw type_error(std::string("invalid type ") + name); throw type_error("invalid type " + name);
if(base_type->arity != arguments.size()) { if(base_type->arity != arguments.size()) {
std::ostringstream error_stream; std::ostringstream error_stream;
error_stream << "invalid application of type "; error_stream << "invalid application of type ";
@ -34,7 +34,7 @@ type_ptr parsed_type_var::to_type(
const std::set<std::string>& vars, const std::set<std::string>& vars,
const type_env& e) const { const type_env& e) const {
if(vars.find(var) == vars.end()) if(vars.find(var) == vars.end())
throw type_error(std::string("the type variable ") + var + std::string(" was not explicitly declared.")); throw type_error("the type variable " + var + " was not explicitly declared.");
return type_ptr(new type_var(var)); return type_ptr(new type_var(var));
} }

View File

@ -1,6 +1,7 @@
#include "type_env.hpp" #include "type_env.hpp"
#include "type.hpp" #include "type.hpp"
#include "error.hpp" #include "error.hpp"
#include <cassert>
void type_env::find_free(const type_mgr& mgr, std::set<std::string>& into) const { void type_env::find_free(const type_mgr& mgr, std::set<std::string>& into) const {
if(parent != nullptr) parent->find_free(mgr, into); if(parent != nullptr) parent->find_free(mgr, into);
@ -45,7 +46,10 @@ void type_env::set_mangled_name(const std::string& name, const std::string& mang
const std::string& type_env::get_mangled_name(const std::string& name) const { const std::string& type_env::get_mangled_name(const std::string& name) const {
auto it = names.find(name); auto it = names.find(name);
if(it != names.end()) return it->second.mangled_name; if(it != names.end()) {
assert(it->second.mangled_name);
return *it->second.mangled_name;
}
assert(parent != nullptr); assert(parent != nullptr);
return parent->get_mangled_name(name); return parent->get_mangled_name(name);
} }
@ -59,7 +63,7 @@ type_ptr type_env::lookup_type(const std::string& name) const {
void type_env::bind(const std::string& name, type_ptr t, visibility v) { void type_env::bind(const std::string& name, type_ptr t, visibility v) {
type_scheme_ptr new_scheme(new type_scheme(std::move(t))); type_scheme_ptr new_scheme(new type_scheme(std::move(t)));
names[name] = variable_data(std::move(new_scheme), v, ""); names[name] = variable_data(std::move(new_scheme), v, std::nullopt);
} }
void type_env::bind(const std::string& name, type_scheme_ptr t, visibility v) { void type_env::bind(const std::string& name, type_scheme_ptr t, visibility v) {

View File

@ -2,6 +2,7 @@
#include <map> #include <map>
#include <string> #include <string>
#include <set> #include <set>
#include <optional>
#include "graph.hpp" #include "graph.hpp"
#include "type.hpp" #include "type.hpp"
@ -15,11 +16,11 @@ class type_env {
struct variable_data { struct variable_data {
type_scheme_ptr type; type_scheme_ptr type;
visibility vis; visibility vis;
std::string mangled_name; std::optional<std::string> mangled_name;
variable_data() variable_data()
: variable_data(nullptr, visibility::local, "") {} : variable_data(nullptr, visibility::local, std::nullopt) {}
variable_data(type_scheme_ptr t, visibility v, std::string n) variable_data(type_scheme_ptr t, visibility v, std::optional<std::string> n)
: type(std::move(t)), vis(v), mangled_name(std::move(n)) {} : type(std::move(t)), vis(v), mangled_name(std::move(n)) {}
}; };