42 lines
1.3 KiB
C++
42 lines
1.3 KiB
C++
|
#include "type_manager.hpp"
|
||
|
#include <memory>
|
||
|
#include "error.hpp"
|
||
|
|
||
|
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;
|
||
|
}
|
||
|
|
||
|
type* type_manager::require_type(const std::string& name) {
|
||
|
if(!type_names.count(name)) throw error("invalid type name");
|
||
|
return type_names[name];
|
||
|
}
|
||
|
}
|