-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcodegen.cpp
51 lines (43 loc) · 1.66 KB
/
codegen.cpp
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include "codegen.hpp"
auto CodeGen::visitPrintStatement(const PrintStatement& printStatement) -> void {
indentation();
builder << "putchar(memory[current]);\n";
}
auto CodeGen::visitInputStatement(const InputStatement& inputStatement) -> void {
indentation();
builder << "memory[current] = getchar();\n";
}
auto CodeGen::visitShiftLeftStatement(const ShiftLeftStatement& shiftLeftStatement) -> void {
indentation();
if (shiftLeftStatement.by == 1)
builder << "current--;\n";
else builder << "current -= " << shiftLeftStatement.by << ";\n";
}
auto CodeGen::visitShiftRightStatement(const ShiftRightStatement& shiftRightStatement) -> void {
indentation();
if (shiftRightStatement.by == 1)
builder << "current++;\n";
else builder << "current += " << shiftRightStatement.by << ";\n";
}
auto CodeGen::visitIncrementStatement(const IncrementStatement& incrementStatement) -> void {
indentation();
if (incrementStatement.by == 1)
builder << "memory[current]++;\n";
else builder << "memory[current] += " << incrementStatement.by << ";\n";
}
auto CodeGen::visitDecrementStatement(const DecrementStatement& decrementStatement) -> void {
indentation();
if (decrementStatement.by == 1)
builder << "memory[current]--;\n";
else builder << "memory[current] -= " << decrementStatement.by << ";\n";
}
auto CodeGen::enterLoopStatement(const LoopStatement& loopStatement) -> void {
indentation();
builder << "while (memory[current] != 0) {\n";
indentLevel++;
}
auto CodeGen::exitLoopStatement(const LoopStatement& loopStatement) -> void {
indentLevel--;
indentation();
builder << "}\n";
}