lily/src/type_checker.cpp

32 lines
979 B
C++

#include "type_checker.hpp"
#include "error.hpp"
namespace lily {
bool type_env::identifier_exists(const std::string& name) {
return identifier_types.count(name) != 0 || (parent && parent->identifier_exists(name));
}
type* type_env::get_identifier_type(const std::string& name) {
if(!identifier_types.count(name)) {
if(parent) return parent->get_identifier_type(name);
throw error("unknown variable");
}
return identifier_types[name];
}
std::shared_ptr<type_env> type_env::with_type(const std::string& name, type* ntype) {
auto child_env = std::make_shared<type_env>();
child_env->parent = this;
child_env->identifier_types[name] = ntype;
return child_env;
}
void type_env::set_type(const std::string& name, type* ntype) {
identifier_types[name] = ntype;
}
void type_env::set_parent(type_env* parent) {
this->parent = parent;
}
}