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.

ast.hpp 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #pragma once
  2. #include <memory>
  3. #include <vector>
  4. #include "type.hpp"
  5. #include "type_env.hpp"
  6. #include "binop.hpp"
  7. #include "instruction.hpp"
  8. #include "env.hpp"
  9. struct ast {
  10. type_ptr node_type;
  11. virtual ~ast() = default;
  12. virtual void print(int indent, std::ostream& to) const = 0;
  13. virtual type_ptr typecheck(type_mgr& mgr, const type_env& env) const = 0;
  14. virtual void resolve(const type_mgr& mgr) const = 0;
  15. virtual void compile(const env_ptr& env,
  16. std::vector<instruction_ptr>& into) const = 0;
  17. type_ptr typecheck_common(type_mgr& mgr, const type_env& env);
  18. void resolve_common(const type_mgr& mgr);
  19. };
  20. using ast_ptr = std::unique_ptr<ast>;
  21. struct pattern {
  22. virtual ~pattern() = default;
  23. virtual void print(std::ostream& to) const = 0;
  24. virtual void match(type_ptr t, type_mgr& mgr, type_env& env) const = 0;
  25. };
  26. using pattern_ptr = std::unique_ptr<pattern>;
  27. struct branch {
  28. pattern_ptr pat;
  29. ast_ptr expr;
  30. branch(pattern_ptr p, ast_ptr a)
  31. : pat(std::move(p)), expr(std::move(a)) {}
  32. };
  33. using branch_ptr = std::unique_ptr<branch>;
  34. struct ast_int : public ast {
  35. int value;
  36. explicit ast_int(int v)
  37. : value(v) {}
  38. void print(int indent, std::ostream& to) const;
  39. type_ptr typecheck(type_mgr& mgr, const type_env& env) const;
  40. void resolve(const type_mgr& mgr) const;
  41. void compile(const env_ptr& env, std::vector<instruction_ptr>& into) const;
  42. };
  43. struct ast_lid : public ast {
  44. std::string id;
  45. explicit ast_lid(std::string i)
  46. : id(std::move(i)) {}
  47. void print(int indent, std::ostream& to) const;
  48. type_ptr typecheck(type_mgr& mgr, const type_env& env) const;
  49. void resolve(const type_mgr& mgr) const;
  50. void compile(const env_ptr& env, std::vector<instruction_ptr>& into) const;
  51. };
  52. struct ast_uid : public ast {
  53. std::string id;
  54. explicit ast_uid(std::string i)
  55. : id(std::move(i)) {}
  56. void print(int indent, std::ostream& to) const;
  57. type_ptr typecheck(type_mgr& mgr, const type_env& env) const;
  58. void resolve(const type_mgr& mgr) const;
  59. void compile(const env_ptr& env, std::vector<instruction_ptr>& into) const;
  60. };
  61. struct ast_binop : public ast {
  62. binop op;
  63. ast_ptr left;
  64. ast_ptr right;
  65. ast_binop(binop o, ast_ptr l, ast_ptr r)
  66. : op(o), left(std::move(l)), right(std::move(r)) {}
  67. void print(int indent, std::ostream& to) const;
  68. type_ptr typecheck(type_mgr& mgr, const type_env& env) const;
  69. void resolve(const type_mgr& mgr) const;
  70. void compile(const env_ptr& env, std::vector<instruction_ptr>& into) const;
  71. };
  72. struct ast_app : public ast {
  73. ast_ptr left;
  74. ast_ptr right;
  75. ast_app(ast_ptr l, ast_ptr r)
  76. : left(std::move(l)), right(std::move(r)) {}
  77. void print(int indent, std::ostream& to) const;
  78. type_ptr typecheck(type_mgr& mgr, const type_env& env) const;
  79. void resolve(const type_mgr& mgr) const;
  80. void compile(const env_ptr& env, std::vector<instruction_ptr>& into) const;
  81. };
  82. struct ast_case : public ast {
  83. ast_ptr of;
  84. std::vector<branch_ptr> branches;
  85. ast_case(ast_ptr o, std::vector<branch_ptr> b)
  86. : of(std::move(o)), branches(std::move(b)) {}
  87. void print(int indent, std::ostream& to) const;
  88. type_ptr typecheck(type_mgr& mgr, const type_env& env) const;
  89. void resolve(const type_mgr& mgr) const;
  90. void compile(const env_ptr& env, std::vector<instruction_ptr>& into) const;
  91. };
  92. struct pattern_var : public pattern {
  93. std::string var;
  94. pattern_var(std::string v)
  95. : var(std::move(v)) {}
  96. void print(std::ostream &to) const;
  97. void match(type_ptr t, type_mgr& mgr, type_env& env) const;
  98. };
  99. struct pattern_constr : public pattern {
  100. std::string constr;
  101. std::vector<std::string> params;
  102. pattern_constr(std::string c, std::vector<std::string> p)
  103. : constr(std::move(c)), params(std::move(p)) {}
  104. void print(std::ostream &to) const;
  105. void match(type_ptr t, type_mgr&, type_env& env) const;
  106. };