-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcodegen.hpp
37 lines (33 loc) · 1.49 KB
/
codegen.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <sstream>
#include <unordered_map>
#include "ast.hpp"
class CodeGen : public Listener {
public:
explicit inline CodeGen() {
builder = {};
indentLevel = 1;
builder << "// Generated by bfc\n#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n\nint main() {\n";
builder << " unsigned char* memory = (unsigned char*) malloc(80000);\n memset(memory, 0, 80000);\n long long current = 40000;\n";
}
inline auto toString() -> std::string {
indentation();
builder << "free(memory);\n}";
return builder.str();
}
protected:
auto visitPrintStatement(const PrintStatement& printStatement) -> void override;
auto visitInputStatement(const InputStatement& inputStatement) -> void override;
auto visitShiftLeftStatement(const ShiftLeftStatement& shiftLeftStatement) -> void override;
auto visitShiftRightStatement(const ShiftRightStatement& shiftLeftStatement) -> void override;
auto visitIncrementStatement(const IncrementStatement& incrementStatement) -> void override;
auto visitDecrementStatement(const DecrementStatement& decrementStatement) -> void override;
auto enterLoopStatement(const LoopStatement& loopStatement) -> void override;
auto exitLoopStatement(const LoopStatement& loopStatement) -> void override;
private:
std::ostringstream builder;
ulong indentLevel;
inline auto indentation() -> void {
for (auto i = 0; i < indentLevel; ++i)
builder << " ";
}
};