A Hugo incarnation of the blog.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

parsed_type.cpp 1.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #include "parsed_type.hpp"
  2. #include <sstream>
  3. #include "type.hpp"
  4. #include "type_env.hpp"
  5. #include "error.hpp"
  6. type_ptr parsed_type_app::to_type(
  7. const std::set<std::string>& vars,
  8. const type_env& e) const {
  9. auto parent_type = e.lookup_type(name);
  10. if(parent_type == nullptr)
  11. throw type_error("no such type or type constructor " + name);
  12. type_base* base_type;
  13. if(!(base_type = dynamic_cast<type_base*>(parent_type.get())))
  14. throw type_error("invalid type " + name);
  15. if(base_type->arity != arguments.size()) {
  16. std::ostringstream error_stream;
  17. error_stream << "invalid application of type ";
  18. error_stream << name;
  19. error_stream << " (" << base_type->arity << " argument(s) expected, ";
  20. error_stream << "but " << arguments.size() << " provided)";
  21. throw type_error(error_stream.str());
  22. }
  23. type_app* new_app = new type_app(std::move(parent_type));
  24. type_ptr to_return(new_app);
  25. for(auto& arg : arguments) {
  26. new_app->arguments.push_back(arg->to_type(vars, e));
  27. }
  28. return to_return;
  29. }
  30. type_ptr parsed_type_var::to_type(
  31. const std::set<std::string>& vars,
  32. const type_env& e) const {
  33. if(vars.find(var) == vars.end())
  34. throw type_error("the type variable " + var + " was not explicitly declared.");
  35. return type_ptr(new type_var(var));
  36. }
  37. type_ptr parsed_type_arr::to_type(
  38. const std::set<std::string>& vars,
  39. const type_env& env) const {
  40. auto new_left = left->to_type(vars, env);
  41. auto new_right = right->to_type(vars, env);
  42. return type_ptr(new type_arr(std::move(new_left), std::move(new_right)));
  43. }