|
| 1 | +#ifndef AALTITOAD_HAWK_COMPILER_H |
| 2 | +#define AALTITOAD_HAWK_COMPILER_H |
| 3 | +#include "ntta/tta.h" |
| 4 | +#include "parser/hawk/diagnostics.h" |
| 5 | +#include "symbol_table.h" |
| 6 | +#include <string> |
| 7 | + |
| 8 | +namespace aaltitoad::hawk { |
| 9 | + class compiler; |
| 10 | + |
| 11 | + struct error { |
| 12 | + diagnostic diagnostic; |
| 13 | + }; |
| 14 | + template<typename ok> |
| 15 | + using result = std::variant<error, ok>; // TODO: Consider making this a simple yalibs thing |
| 16 | + |
| 17 | + namespace frontend { |
| 18 | + struct scanner { |
| 19 | + virtual auto scan(compiler& ctx, const std::string& path) const noexcept -> result<std::string> = 0; // TODO: probably shouldnt be a string output... |
| 20 | + }; |
| 21 | + |
| 22 | + struct parser { |
| 23 | + virtual auto parse(compiler& ctx, const std::string& stream) const noexcept -> result<int> = 0; // TODO: Should be an AST output |
| 24 | + }; |
| 25 | + } |
| 26 | + |
| 27 | + namespace middleend { |
| 28 | + struct semantic_analyzer { |
| 29 | + virtual auto analyze(compiler& ctx, const int& ast) const noexcept -> result<int> = 0; // TODO: Should be an ast output |
| 30 | + }; |
| 31 | + |
| 32 | + struct optimizer { |
| 33 | + // Note that optimizers are not allowed to return errors, but can throw exceptions. |
| 34 | + virtual void optimize(compiler& ctx, int& ast) const = 0; |
| 35 | + }; |
| 36 | + } |
| 37 | + |
| 38 | + namespace backend { |
| 39 | + struct generator { |
| 40 | + virtual auto generate(compiler& ctx, const int& ast) const noexcept -> result<ntta_t> = 0; |
| 41 | + }; |
| 42 | + } |
| 43 | + |
| 44 | + class compiler { |
| 45 | + public: |
| 46 | + compiler(const frontend::scanner& scanner, |
| 47 | + const frontend::parser& parser, |
| 48 | + const middleend::semantic_analyzer& analyzer, |
| 49 | + const middleend::optimizer& optimizer, |
| 50 | + const backend::generator& generator); |
| 51 | + void add_symbols(const expr::symbol_table_t& symbols); |
| 52 | + void clear_symbols(); |
| 53 | + auto get_diagnostic_factory() -> diagnostic_factory&; |
| 54 | + auto compile(const std::string& path) -> result<ntta_t>; |
| 55 | + |
| 56 | + private: |
| 57 | + const frontend::scanner& scanner; |
| 58 | + const frontend::parser& parser; |
| 59 | + const middleend::semantic_analyzer& analyzer; |
| 60 | + const middleend::optimizer& optimizer; |
| 61 | + const backend::generator& generator; |
| 62 | + expr::symbol_table_t symbols; |
| 63 | + diagnostic_factory diagnostic_factory; |
| 64 | + }; |
| 65 | +} |
| 66 | + |
| 67 | +#endif // AALTITOAD_HAWK_COMPILER_H |
0 commit comments