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 56bdbdc6ee
commit 137455b0f4
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 {
auto parent_type = e.lookup_type(name);
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;
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()) {
std::ostringstream error_stream;
error_stream << "invalid application of type ";
@ -34,7 +34,7 @@ type_ptr parsed_type_var::to_type(
const std::set<std::string>& vars,
const type_env& e) const {
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));
}

View File

@ -1,6 +1,7 @@
#include "type_env.hpp"
#include "type.hpp"
#include "error.hpp"
#include <cassert>
void type_env::find_free(const type_mgr& mgr, std::set<std::string>& into) const {
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 {
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);
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) {
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) {

View File

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