2019-06-09 16:13:13 -07:00
|
|
|
#include "type_manager.hpp"
|
|
|
|
#include <memory>
|
|
|
|
#include "error.hpp"
|
2019-06-09 20:24:44 -07:00
|
|
|
#include "type.hpp"
|
|
|
|
#include "type_checker.hpp"
|
2019-06-09 16:13:13 -07:00
|
|
|
|
|
|
|
namespace lily {
|
|
|
|
type_manager::type_manager() {
|
|
|
|
create_int_type();
|
|
|
|
create_str_type();
|
|
|
|
}
|
|
|
|
|
|
|
|
type_internal* type_manager::create_int_type() {
|
|
|
|
auto new_type = std::make_unique<type_internal>(next_id++);
|
|
|
|
type_internal* raw_ptr = new_type.get();
|
|
|
|
types.push_back(std::move(new_type));
|
|
|
|
type_names["Int"] = raw_ptr;
|
|
|
|
return raw_ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
type_internal* type_manager::create_str_type() {
|
|
|
|
auto new_type = std::make_unique<type_internal>(next_id++);
|
|
|
|
type_internal* raw_ptr = new_type.get();
|
|
|
|
types.push_back(std::move(new_type));
|
|
|
|
type_names["Str"] = raw_ptr;
|
|
|
|
return raw_ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
type_data* type_manager::create_data_type(const std::string& name) {
|
|
|
|
if(type_names.count(name)) throw error("redefinition of type");
|
|
|
|
|
|
|
|
auto new_type = std::make_unique<type_data>(next_id++);
|
|
|
|
type_data* raw_ptr = new_type.get();
|
|
|
|
types.push_back(std::move(new_type));
|
|
|
|
type_names[name] = raw_ptr;
|
|
|
|
return raw_ptr;
|
|
|
|
}
|
|
|
|
|
2019-06-09 19:51:53 -07:00
|
|
|
type* type_manager::require_type(const std::string& name) const {
|
2019-06-09 16:13:13 -07:00
|
|
|
if(!type_names.count(name)) throw error("invalid type name");
|
2019-06-09 19:51:53 -07:00
|
|
|
return type_names.find(name)->second;
|
2019-06-09 16:13:13 -07:00
|
|
|
}
|
2019-06-09 20:24:44 -07:00
|
|
|
|
|
|
|
void type_manager::register_constructors(std::shared_ptr<type_env> env) {
|
|
|
|
for(auto& type_ref : types) {
|
|
|
|
type_data* data_type = dynamic_cast<type_data*>(type_ref.get());
|
|
|
|
if(!data_type) continue;
|
|
|
|
|
|
|
|
for(auto& pair : data_type->constructors) {
|
|
|
|
size_t param_count = pair.second->params.size();
|
|
|
|
type* current_type = data_type;
|
|
|
|
for(int i = 0; i < param_count; i++) {
|
|
|
|
type* param = pair.second->params[param_count - i - 1];
|
|
|
|
current_type = create_type<type_func>(param, current_type);
|
|
|
|
}
|
|
|
|
env->set_type(pair.first, current_type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-06-09 16:13:13 -07:00
|
|
|
}
|