-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVM.h
45 lines (39 loc) · 1.13 KB
/
VM.h
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
#ifndef VIRTUALMACHINE_H
#define VIRTUALMACHINE_H
#include "Instruction.h"
#include <vector>
class VirtualMachine {
public:
VirtualMachine() : m_ip(0), m_acc(0), m_flag(false) {}
void run(const std::vector<Instruction>& program) {
while (m_ip < program.size()) {
const auto& instruction = program[m_ip];
switch (instruction.opcode) {
case OpCode::HALT:
return;
case OpCode::LOAD:
m_acc = instruction.operand;
break;
case OpCode::ADD:
m_acc += instruction.operand;
break;
case OpCode::SUB:
m_acc -= instruction.operand;
break;
case OpCode::MUL:
m_acc *= instruction.operand;
break;
case OpCode::DIV:
m_acc /= instruction.operand;
break;
}
++m_ip;
}
}
int getAccumulator() const { return m_acc; }
private:
int m_ip;
int m_acc;
bool m_flag;
};
#endif