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.

type.hpp 1.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #pragma once
  2. #include <memory>
  3. #include <map>
  4. struct type_mgr;
  5. struct type {
  6. virtual ~type() = default;
  7. virtual void print(const type_mgr& mgr, std::ostream& to) const = 0;
  8. };
  9. using type_ptr = std::shared_ptr<type>;
  10. struct type_var : public type {
  11. std::string name;
  12. type_var(std::string n)
  13. : name(std::move(n)) {}
  14. void print(const type_mgr& mgr, std::ostream& to) const;
  15. };
  16. struct type_base : public type {
  17. std::string name;
  18. type_base(std::string n)
  19. : name(std::move(n)) {}
  20. void print(const type_mgr& mgr, std::ostream& to) const;
  21. };
  22. struct type_data : public type_base {
  23. struct constructor {
  24. int tag;
  25. };
  26. std::map<std::string, constructor> constructors;
  27. type_data(std::string n)
  28. : type_base(std::move(n)) {}
  29. };
  30. struct type_arr : public type {
  31. type_ptr left;
  32. type_ptr right;
  33. type_arr(type_ptr l, type_ptr r)
  34. : left(std::move(l)), right(std::move(r)) {}
  35. void print(const type_mgr& mgr, std::ostream& to) const;
  36. };
  37. struct type_mgr {
  38. int last_id = 0;
  39. std::map<std::string, type_ptr> types;
  40. std::string new_type_name();
  41. type_ptr new_type();
  42. type_ptr new_arrow_type();
  43. void unify(type_ptr l, type_ptr r);
  44. type_ptr resolve(type_ptr t, type_var*& var) const;
  45. void bind(const std::string& s, type_ptr t);
  46. };