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.

definition.hpp 2.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #pragma once
  2. #include <memory>
  3. #include <vector>
  4. #include <map>
  5. #include <set>
  6. #include "instruction.hpp"
  7. #include "llvm_context.hpp"
  8. #include "parsed_type.hpp"
  9. #include "type_env.hpp"
  10. #include "global_scope.hpp"
  11. struct ast;
  12. using ast_ptr = std::unique_ptr<ast>;
  13. struct constructor {
  14. std::string name;
  15. std::vector<parsed_type_ptr> types;
  16. int8_t tag;
  17. constructor(std::string n, std::vector<parsed_type_ptr> ts)
  18. : name(std::move(n)), types(std::move(ts)) {}
  19. };
  20. using constructor_ptr = std::unique_ptr<constructor>;
  21. struct definition_defn {
  22. std::string name;
  23. std::vector<std::string> params;
  24. ast_ptr body;
  25. type_env_ptr env;
  26. type_env_ptr var_env;
  27. std::set<std::string> free_variables;
  28. std::set<std::string> nearby_variables;
  29. type_ptr full_type;
  30. type_ptr return_type;
  31. definition_defn(std::string n, std::vector<std::string> p, ast_ptr b)
  32. : name(std::move(n)), params(std::move(p)), body(std::move(b)) {
  33. }
  34. void find_free();
  35. void insert_types(type_mgr& mgr, type_env_ptr& env, visibility v);
  36. void typecheck(type_mgr& mgr);
  37. global_function& into_global(global_scope& scope);
  38. };
  39. using definition_defn_ptr = std::unique_ptr<definition_defn>;
  40. struct definition_data {
  41. std::string name;
  42. std::vector<std::string> vars;
  43. std::vector<constructor_ptr> constructors;
  44. type_env_ptr env;
  45. definition_data(
  46. std::string n,
  47. std::vector<std::string> vs,
  48. std::vector<constructor_ptr> cs)
  49. : name(std::move(n)), vars(std::move(vs)), constructors(std::move(cs)) {}
  50. void insert_types(type_env_ptr& env);
  51. void insert_constructors() const;
  52. void into_globals(global_scope& scope);
  53. };
  54. using definition_data_ptr = std::unique_ptr<definition_data>;
  55. struct definition_group {
  56. std::map<std::string, definition_data_ptr> defs_data;
  57. std::map<std::string, definition_defn_ptr> defs_defn;
  58. visibility vis;
  59. type_env_ptr env;
  60. definition_group(visibility v = visibility::local) : vis(v) {}
  61. void find_free(std::set<std::string>& into);
  62. void typecheck(type_mgr& mgr, type_env_ptr& env);
  63. };