-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterpreter.cpp
65 lines (56 loc) · 1.67 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include "interpreter.h"
#include <cstdio>
#include "opcodes.h"
namespace bf {
Interpreter::VM::VM(size_t _ram) {
pointer = 0;
memory = new char[_ram];
std::fill(memory, memory + _ram, 0);
}
Interpreter::VM::~VM() {
delete[] memory;
}
Interpreter::Interpreter(size_t _ram) : m_vm(_ram) {
}
void Interpreter::run(std::vector<Statement>& _program) {
size_t brc;
for (size_t it = 0; it < _program.size(); ++it)
switch (_program[it].opCode) {
case OPCODE::SET_ZERO:
m_vm.memory[m_vm.pointer] = 0;
break;
case OPCODE::INC:
m_vm.memory[m_vm.pointer] += _program[it].arg;
break;
case OPCODE::DEC:
m_vm.memory[m_vm.pointer] -= _program[it].arg;
break;
case OPCODE::NEXT:
m_vm.pointer += _program[it].arg;
break;
case OPCODE::PREV:
m_vm.pointer -= _program[it].arg;
break;
case OPCODE::JMP_FW:
if (m_vm.memory[m_vm.pointer] != 0) {
continue;
}
it = _program[it].arg;
break;
case OPCODE::JMP_BK:
if (m_vm.memory[m_vm.pointer] == 0) {
continue;
}
it = _program[it].arg;
break;
case OPCODE::WRITE:
std::cout << m_vm.memory[m_vm.pointer];
break;
case OPCODE::READ:
m_vm.memory[m_vm.pointer] = getchar();
break;
default:
break;
}
}
} // namespace bf