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.

153 lines
5.3 KiB

1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
  1. #pragma once
  2. #include <string>
  3. #include <set>
  4. #include <vector>
  5. #include <memory>
  6. #include <optional>
  7. #include <charconv>
  8. #include <concepts>
  9. #include "molasses/lexer.h"
  10. namespace molasses {
  11. struct type {
  12. [[nodiscard]] virtual std::string name() const = 0;
  13. [[nodiscard]] virtual size_t byte_size() const = 0;
  14. };
  15. inline auto operator<=>(const type& lhs, const type& rhs) {
  16. return lhs.name() <=> rhs.name();
  17. }
  18. struct primitive_type : public type {
  19. std::string _name;
  20. size_t _byte_size;
  21. primitive_type(std::string name, size_t byte_size)
  22. : _name(std::forward<std::string>(name))
  23. , _byte_size(byte_size)
  24. {}
  25. [[nodiscard]] std::string name() const final {
  26. return _name;
  27. }
  28. [[nodiscard]] size_t byte_size() const final {
  29. return _byte_size;
  30. };
  31. };
  32. struct parser_context;
  33. struct operation {
  34. [[nodiscard]] virtual std::string name() const = 0;
  35. [[nodiscard]] virtual std::vector<std::string> argument_types() const = 0;
  36. [[nodiscard]] virtual std::vector<std::string> return_types() const = 0;
  37. [[nodiscard]] virtual std::vector<std::string> generate(const parser_context&, const lexed_output& lexer_data) const = 0;
  38. [[nodiscard]] virtual std::vector<std::string> emit(const parser_context&) const = 0;
  39. };
  40. struct primitive_operation : public operation {
  41. std::string _name;
  42. std::vector<std::string> _args;
  43. std::vector<std::string> _rets;
  44. std::vector<std::string> _instructions;
  45. primitive_operation(std::string name, std::vector<std::string> args, std::vector<std::string> rets, std::vector<std::string> body)
  46. : _name(std::forward<std::string>(name))
  47. , _args(std::forward<std::vector<std::string>>(args))
  48. , _rets(std::forward<std::vector<std::string>>(rets))
  49. , _instructions(std::forward<std::vector<std::string>>(body))
  50. {}
  51. [[nodiscard]] std::string name() const final {
  52. return _name;
  53. }
  54. [[nodiscard]] std::vector<std::string> argument_types() const final {
  55. return _args;
  56. }
  57. [[nodiscard]] std::vector<std::string> return_types() const final {
  58. return _rets;
  59. }
  60. [[nodiscard]] std::vector<std::string> generate(const parser_context&, const lexed_output& lexer_data) const final {
  61. return {};
  62. }
  63. [[nodiscard]] std::vector<std::string> emit(const parser_context&) const final {
  64. return _instructions;
  65. }
  66. };
  67. struct procedure_operation : public operation {
  68. std::string _name;
  69. std::vector<std::string> _args;
  70. std::vector<std::string> _rets;
  71. std::vector<symbol> _body;
  72. procedure_operation(std::string name, std::vector<std::string> args, std::vector<std::string> rets, std::vector<symbol> body)
  73. : _name(std::forward<std::string>(name))
  74. , _args(std::forward<std::vector<std::string>>(args))
  75. , _rets(std::forward<std::vector<std::string>>(rets))
  76. , _body(std::forward<std::vector<symbol>>(body))
  77. {}
  78. [[nodiscard]] std::string name() const final {
  79. return _name;
  80. }
  81. [[nodiscard]] std::vector<std::string> argument_types() const final {
  82. return _args;
  83. }
  84. [[nodiscard]] std::vector<std::string> return_types() const final {
  85. return _rets;
  86. }
  87. [[nodiscard]] std::vector<std::string> generate(const parser_context&, const lexed_output& lexer_data) const final;
  88. [[nodiscard]] std::vector<std::string> emit(const parser_context&) const final;
  89. };
  90. inline auto operator<=>(const operation& lhs, const operation& rhs) {
  91. return lhs.name() <=> rhs.name();
  92. }
  93. struct TypeInputError : std::runtime_error {
  94. TypeInputError() : std::runtime_error("Bad type provided") {}
  95. // TODO: Better error message
  96. };
  97. struct ValueMissingError : std::runtime_error {
  98. ValueMissingError() : std::runtime_error("Expected value, none provided") {}
  99. // TODO: Better error message
  100. };
  101. struct ProcedureStackError : std::runtime_error {
  102. ProcedureStackError() : std::runtime_error("Expected the stack to look like the return stack upon completion") {}
  103. // TODO: Better error message
  104. };
  105. struct UnexpectedTokenError : std::runtime_error {
  106. UnexpectedTokenError() : std::runtime_error("An unexpected token has been encountered") {}
  107. // TODO: Better error message
  108. };
  109. struct ExpectingTokenError : std::runtime_error {
  110. ExpectingTokenError() : std::runtime_error("An expected token has not been encountered before the end of the input") {}
  111. // TODO: Better error message
  112. };
  113. struct UnknownTokenError : std::runtime_error {
  114. UnknownTokenError() : std::runtime_error("An unknown token has not been encountered") {}
  115. // TODO: Better error message
  116. };
  117. std::vector<std::string> operator>>(std::vector<std::string> current_stack, const operation& next_op);
  118. std::optional<int32_t> try_parse_int32(const std::string& str);
  119. struct parser_context {
  120. std::vector<std::shared_ptr<type>> types;
  121. std::vector<std::shared_ptr<operation>> operations;
  122. [[nodiscard]] std::shared_ptr<type> lookup_type(const std::string&) const;
  123. [[nodiscard]] std::shared_ptr<operation> lookup_operation(const std::string&) const;
  124. };
  125. parser_context parse(parser_context, const lexed_output&);
  126. parser_context register_integers(parser_context);
  127. parser_context register_i32_operations(parser_context);
  128. bool type_check(const parser_context&, const lexed_output&, const std::vector<symbol>&, std::vector<std::string> execution_input, const std::vector<std::string>& execution_output);
  129. }