33#include " ntta/tta.h"
44#include " parser/hawk/diagnostics.h"
55#include " symbol_table.h"
6+ #include < expected>
67#include < string>
78
89namespace aaltitoad ::hawk {
@@ -11,54 +12,57 @@ namespace aaltitoad::hawk {
1112 struct error {
1213 diagnostic diagnostic;
1314 };
14- template <typename ok>
15- using result = std::variant<error, ok>; // TODO: Consider making this a simple yalibs thing
1615
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- };
16+ struct scanner_t {
17+ scanner_t () = default ;
18+ virtual ~scanner_t () = default ;
19+ virtual auto scan (compiler& ctx, const std::string& path) const noexcept -> std::expected<std::string, error> = 0; // TODO: probably shouldnt be a string output...
20+ };
2121
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- }
22+ struct parser_t {
23+ parser_t () = default ;
24+ virtual ~parser_t () = default ;
25+ virtual auto parse (compiler& ctx, const std::string& stream) const noexcept -> std::expected<int, error> = 0; // TODO: Should be an AST output
26+ };
2627
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- };
28+ struct semantic_analyzer_t {
29+ semantic_analyzer_t () = default ;
30+ virtual ~semantic_analyzer_t () = default ;
31+ virtual auto analyze (compiler& ctx, const int & ast) const noexcept -> std::expected<int, error> = 0; // TODO: Should be an ast output
32+ };
3133
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- }
34+ struct optimizer_t {
35+ optimizer_t () = default ;
36+ virtual ~optimizer_t () = default ;
37+ // Note that optimizers are not allowed to return errors, but can throw exceptions.
38+ virtual void optimize (compiler& ctx, int & ast) const = 0;
39+ };
3740
38- namespace backend {
39- struct generator {
40- virtual auto generate (compiler& ctx, const int & ast) const noexcept -> result<ntta_t> = 0 ;
41- } ;
42- }
41+ struct generator_t {
42+ generator_t () = default ;
43+ virtual ~generator_t () = default ;
44+ virtual auto generate (compiler& ctx, const int & ast) const noexcept -> std::expected<ntta_t, error> = 0 ;
45+ };
4346
4447 class compiler {
4548 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);
49+ compiler (
50+ std::unique_ptr<scanner_t >&& scanner,
51+ std::unique_ptr<parser_t >&& parser,
52+ std::unique_ptr<semantic_analyzer_t >&& analyzer,
53+ std::unique_ptr<optimizer_t >&& optimizer,
54+ std::unique_ptr<generator_t >&& generator);
5155 void add_symbols (const expr::symbol_table_t & symbols);
5256 void clear_symbols ();
5357 auto get_diagnostic_factory () -> diagnostic_factory&;
54- auto compile (const std::string& path) -> result <ntta_t>;
58+ auto compile (const std::string& path) -> std::expected <ntta_t, error >;
5559
5660 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;
61+ std::unique_ptr< scanner_t > scanner;
62+ std::unique_ptr< parser_t > parser;
63+ std::unique_ptr< semantic_analyzer_t > analyzer;
64+ std::unique_ptr< optimizer_t > optimizer;
65+ std::unique_ptr< generator_t > generator;
6266 expr::symbol_table_t symbols;
6367 diagnostic_factory diagnostic_factory;
6468 };
0 commit comments