-
Notifications
You must be signed in to change notification settings - Fork 0
/
bfm_memory.cpp
84 lines (72 loc) · 1.86 KB
/
bfm_memory.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
#include "bfm_memory.hpp"
namespace bfm {
int64_t memory::index_to_trueindex(int64_t index) const {
return index + offset;
}
int64_t memory::trueindex_to_index(int64_t true_index) const {
return true_index - offset;
}
unsigned char &memory::operator[](int64_t index) {
int64_t true_index = index_to_trueindex(index);
while (true_index < 0) {
data.push_front(0);
++offset;
++true_index;
}
while (true_index >= data.size()) {
data.push_back(0);
}
return data[true_index];
}
unsigned char memory::get(int64_t index) {
return (*this)[index];
}
unsigned char memory::set(int64_t index, unsigned char val) {
return (*this)[index] = val;
}
unsigned char memory::inc(int64_t index) {
return ++(*this)[index];
}
unsigned char memory::dec(int64_t index) {
return --(*this)[index];
}
int64_t memory::find_match_forward(int64_t index) const {
int64_t true_index = index_to_trueindex(index);
int64_t count = 1;
while (count > 0 && ++true_index < data.size()) {
switch (data[true_index]) {
case '[':
++count; break;
case ']':
--count; break;
}
}
if (true_index == data.size()) throw "unmatched bracket";
return trueindex_to_index(true_index);
}
int64_t memory::find_match_backward(int64_t index) const {
int64_t true_index = index_to_trueindex(index);
int64_t count = 1;
while (count > 0 && --true_index >= 0) {
switch (data[true_index]) {
case '[':
--count; break;
case ']':
++count; break;
}
}
if (true_index == -1) throw "unmatched bracket";
return trueindex_to_index(true_index);
}
int64_t memory::find_match(int64_t index) const {
int64_t true_index = index_to_trueindex(index);
switch (data[true_index]) {
case '[':
return find_match_forward(index);
case ']':
return find_match_backward(index);
default:
throw "it is not bracket";
}
}
} // namespace bfm