-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinterpreter.cpp
43 lines (34 loc) · 1.27 KB
/
interpreter.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
#include <iostream>
#include "interpreter.hpp"
auto Interpreter::interpret() -> void {
for (auto& statement : statements)
statement->accept(*this);
}
Interpreter::Interpreter(const std::vector<std::unique_ptr<Statement>>& statements) : statements(statements) {
cellPointer = 0;
cells = std::unordered_map<int64_t, byte>();
}
auto Interpreter::visit(const PrintStatement& printStatement) -> void {
std::cout << cells[cellPointer];
}
auto Interpreter::visit(const InputStatement& inputStatement) -> void {
cells[cellPointer] = getchar();
}
auto Interpreter::visit(const ShiftLeftStatement& shiftLeftStatement) -> void {
cellPointer -= shiftLeftStatement.by;
}
auto Interpreter::visit(const ShiftRightStatement& shiftRightStatement) -> void {
cellPointer += shiftRightStatement.by;
}
auto Interpreter::visit(const LoopStatement& loopStatement) -> void {
while (cells[cellPointer] != 0) {
for (auto& statement : loopStatement.statements)
statement->accept(*this);
}
}
auto Interpreter::visit(const IncrementStatement& incrementStatement) -> void {
cells[cellPointer] += incrementStatement.by;
}
auto Interpreter::visit(const DecrementStatement& decrementStatement) -> void {
cells[cellPointer] -= decrementStatement.by;
}