Skip to content

Commit b55c276

Browse files
committed
wip: refactor the hawk compilation process
It is a bit of a mess right now. I'm trying to get it to smell a bit more like a traditional compiler architecture.
1 parent b7cac14 commit b55c276

File tree

3 files changed

+99
-1
lines changed

3 files changed

+99
-1
lines changed

src/parser/hawk/compiler.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include "compiler.h"
2+
#include <overload>
3+
#include <variant>
4+
5+
namespace aaltitoad::hawk {
6+
compiler::compiler(const frontend::scanner& scanner, const frontend::parser& parser, const middleend::semantic_analyzer& analyzer, const middleend::optimizer& optimizer, const backend::generator& generator)
7+
: scanner{scanner}, parser{parser}, analyzer{analyzer}, optimizer{optimizer}, generator{generator}, symbols{}, diagnostic_factory{}
8+
{
9+
10+
}
11+
12+
void compiler::add_symbols(const expr::symbol_table_t& symbols) {
13+
this->symbols.put(symbols);
14+
}
15+
16+
void compiler::clear_symbols() {
17+
symbols.clear();
18+
}
19+
20+
auto compiler::compile(const std::string& path) -> result<ntta_t> {
21+
auto stream = scanner.scan(*this, path);
22+
std::visit(ya::overload(
23+
[](const std::string& stream){},
24+
[](const error& err){}
25+
), stream);
26+
auto ast = parser.parse(*this, stream);
27+
auto dast = analyzer.analyze(*this, ast);
28+
optimizer.optimize(*this, dast);
29+
return generator.generate(*this, dast);
30+
}
31+
}

src/parser/hawk/compiler.h

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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

src/parser/hawk/diagnostics.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ namespace aaltitoad::hawk {
1717
// TODO: suggestion generator
1818
public:
1919
diagnostic_factory() = default;
20-
auto without_context() -> diagnostic_factory&;
20+
auto without_context() const -> diagnostic_factory&;
2121
auto with_model_key(const std::string& key) -> diagnostic_factory&;
2222
auto with_context(const std::string& element) -> diagnostic_factory&;
2323
auto with_context(const std::initializer_list<std::string>& elements) -> diagnostic_factory&;

0 commit comments

Comments
 (0)