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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #pragma once
  2. #include <memory>
  3. #include <vector>
  4. #include "instruction.hpp"
  5. #include "llvm_context.hpp"
  6. #include "type_env.hpp"
  7. struct ast;
  8. using ast_ptr = std::unique_ptr<ast>;
  9. struct definition {
  10. virtual ~definition() = default;
  11. virtual void typecheck_first(type_mgr& mgr, type_env& env) = 0;
  12. virtual void typecheck_second(type_mgr& mgr, const type_env& env) const = 0;
  13. virtual void resolve(const type_mgr& mgr) = 0;
  14. virtual void compile() = 0;
  15. virtual void gen_llvm_first(llvm_context& ctx) = 0;
  16. virtual void gen_llvm_second(llvm_context& ctx) = 0;
  17. };
  18. using definition_ptr = std::unique_ptr<definition>;
  19. struct constructor {
  20. std::string name;
  21. std::vector<std::string> types;
  22. int8_t tag;
  23. constructor(std::string n, std::vector<std::string> ts)
  24. : name(std::move(n)), types(std::move(ts)) {}
  25. };
  26. using constructor_ptr = std::unique_ptr<constructor>;
  27. struct definition_defn : public definition {
  28. std::string name;
  29. std::vector<std::string> params;
  30. ast_ptr body;
  31. type_ptr return_type;
  32. std::vector<type_ptr> param_types;
  33. std::vector<instruction_ptr> instructions;
  34. llvm::Function* generated_function;
  35. definition_defn(std::string n, std::vector<std::string> p, ast_ptr b)
  36. : name(std::move(n)), params(std::move(p)), body(std::move(b)) {
  37. }
  38. void typecheck_first(type_mgr& mgr, type_env& env);
  39. void typecheck_second(type_mgr& mgr, const type_env& env) const;
  40. void resolve(const type_mgr& mgr);
  41. void compile();
  42. void gen_llvm_first(llvm_context& ctx);
  43. void gen_llvm_second(llvm_context& ctx);
  44. };
  45. struct definition_data : public definition {
  46. std::string name;
  47. std::vector<constructor_ptr> constructors;
  48. definition_data(std::string n, std::vector<constructor_ptr> cs)
  49. : name(std::move(n)), constructors(std::move(cs)) {}
  50. void typecheck_first(type_mgr& mgr, type_env& env);
  51. void typecheck_second(type_mgr& mgr, const type_env& env) const;
  52. void resolve(const type_mgr& mgr);
  53. void compile();
  54. void gen_llvm_first(llvm_context& ctx);
  55. void gen_llvm_second(llvm_context& ctx);
  56. };