lily/src/pattern.cpp

33 lines
1.1 KiB
C++

#include "pattern.hpp"
#include "error.hpp"
#include "type_checker.hpp"
#include "type_manager.hpp"
namespace lily {
type* pattern_var::check_modifying(type_manager& mgr, std::shared_ptr<type_env> env) {
type* parameter = mgr.create_type<type_parameter>();
env->set_type(name, parameter);
return parameter;
}
type* pattern_cons::check_modifying(type_manager& mgr, std::shared_ptr<type_env> env) {
type* constructor_type = env->get_identifier_type(name);
int arity = constructor_type->arity();
if(arity != vnames.size())
throw error("invalid number of arguments to constructor");
if(arity == 0) {
return constructor_type;
} else {
type_func* func_type;
type* current = constructor_type;
int index = 0;
while((func_type = dynamic_cast<type_func*>(current))) {
env->set_type(vnames[index++], func_type->left);
current = func_type->right;
}
return current;
}
}
}