Skip to content

Commit 7af185c

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 7af185c

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

src/parser/hawk/compiler.cpp

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

src/parser/hawk/compiler.h

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#ifndef AALTITOAD_HAWK_COMPILER_H
2+
#define AALTITOAD_HAWK_COMPILER_H
3+
#include "ntta/tta.h"
4+
#include "symbol_table.h"
5+
#include <string>
6+
7+
namespace aaltitoad::hawk {
8+
class compiler;
9+
10+
namespace frontend {
11+
struct scanner {
12+
virtual auto scan(compiler& ctx, const std::string& path) const -> std::string = 0; // TODO: probably shouldnt be a string output...
13+
};
14+
15+
struct parser {
16+
virtual auto parse(compiler& ctx, const std::string& stream) const -> int = 0; // TODO: Should be an AST output
17+
};
18+
}
19+
20+
namespace middleend {
21+
struct semantic_analyzer {
22+
virtual auto analyze(compiler& ctx, const int& ast) const -> int = 0; // TODO: Should be an ast output
23+
};
24+
25+
struct optimizer {
26+
virtual void optimize(compiler& ctx, int& ast) const = 0;
27+
};
28+
}
29+
30+
namespace backend {
31+
struct generator {
32+
virtual auto generate(compiler& ctx, const int& ast) const -> ntta_t = 0;
33+
};
34+
}
35+
36+
class compiler {
37+
public:
38+
compiler(const frontend::scanner& scanner,
39+
const frontend::parser& parser,
40+
const middleend::semantic_analyzer& analyzer,
41+
const middleend::optimizer& optimizer,
42+
const backend::generator& generator);
43+
void add_symbols(const expr::symbol_table_t& symbols);
44+
void clear_symbols();
45+
auto compile(const std::string& path) -> ntta_t;
46+
47+
private:
48+
const frontend::scanner& scanner;
49+
const frontend::parser& parser;
50+
const middleend::semantic_analyzer& analyzer;
51+
const middleend::optimizer& optimizer;
52+
const backend::generator& generator;
53+
expr::symbol_table_t symbols;
54+
};
55+
}
56+
57+
#endif // AALTITOAD_HAWK_COMPILER_H

0 commit comments

Comments
 (0)