-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmachine.cpp
137 lines (121 loc) · 2.59 KB
/
machine.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include "machine.h"
#include <cstring>
#include <stdexcept>
namespace brainfuck {
void Machine::reset() {
std::memset(data, 0, sizeof(data));
ip = 0;
dp = 0;
run();
}
void Machine::exec(const char* code, size_t size) {
this->code = code;
this->codeSize = size;
reset();
}
void Machine::run() {
while (ip < codeSize) {
char cmd = code[ip++];
Instruction instr = getInstruction(cmd);
switch(instr.opCode) {
case inc:
handleInc(instr.times);
break;
case dec:
handleDec(instr.times);
break;
case next:
handleNext(instr.times);
break;
case prev:
handlePrev(instr.times);
break;
case begin:
handleBegin();
break;
case end:
handleEnd();
break;
case in:
handleIn(instr.times);
break;
case out:
handleOut(instr.times);
break;
case nop:
handleNop();
break;
default:
throw std::domain_error("Unknown instruction");
}
}
}
void Machine::handleInc(int times) {
while(times--) data[dp]++;
}
void Machine::handleDec(int times) {
while(times--) data[dp]--;
}
void Machine::handleNext(int times) {
while(times--) {
dp++;
if (dp >= memorySize) dp = 0;
}
}
void Machine::handlePrev(int times) {
while(times--) {
if (dp == 0) {
dp = memorySize - 1;
} else {
dp--;
}
}
}
void Machine::handleBegin() {
if (data[dp] == 0) {
int depth = 1;
Instruction instr;
do {
if (ip >= codeSize) return;
instr = getInstruction(code[ip]);
if (instr.opCode == OpCode::begin) depth++;
if (instr.opCode == OpCode::end) depth--;
ip++;
} while((instr.opCode != OpCode::end) || (depth > 0));
}
}
void Machine::handleEnd() {
if (data[dp] == 0) return;
ip--;
int depth = 1;
Instruction instr;
do {
if (ip == 0) {
throw std::out_of_range("Instruction pointer < 0");
}
ip--;
instr = getInstruction(code[ip]);
if (instr.opCode == OpCode::begin) depth--;
if (instr.opCode == OpCode::end) depth++;
} while((instr.opCode != OpCode::begin) || (depth > 0));
ip++;
}
void Machine::handleIn(int times) {
while(times--) {
inputStream.read(&data[dp], 1);
}
}
void Machine::handleOut(int times) {
while(times--) {
outputStream.write(&data[dp], 1);
}
}
void Machine::handleNop() {}
Instruction Machine::getInstruction(char cmd) {
return parser.parse(cmd);
}
void Machine::memDump(size_t startOffset, size_t size, std::ostream& output) const {
if (startOffset + size > memorySize) throw std::out_of_range("Out of memory range");
output.write(&data[startOffset], size);
}
}