-
Notifications
You must be signed in to change notification settings - Fork 0
/
brain_fuck.cpp
139 lines (131 loc) · 3.41 KB
/
brain_fuck.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
138
139
#include <array>
#include <iostream>
#include <string>
#include <unordered_map>
const int MAX_NUM = 30000;
const int INVALID_INDEX = -2;
std::unordered_map<int, int> brack_map;
std::array<char, MAX_NUM> init() { return std::array<char, MAX_NUM>{{0}}; }
int find_match(const std::string &inputs, int index, char c) {
auto search = brack_map.find(index);
if (search != brack_map.end())
return search->second;
if (c == ']') {
std::cerr << "Input Error, there is no match with char \'" << c
<< "\' in position:" << index + 1 << std::endl;
return INVALID_INDEX;
}
int begin = index, num = 1;
while (++index < inputs.length()) {
if (inputs[index] == '[') {
num++;
} else if (inputs[index] == ']') {
num--;
}
if (num == 0) {
brack_map.insert({begin, index - 1});
brack_map.insert({index, begin - 1});
return index - 1;
} else if (num < 0) {
break;
}
}
std::cerr << "Input Error, there is no match with char \'" << c
<< "\' in position:" << index + 1 << std::endl;
return INVALID_INDEX;
}
int processBf(const std::string &inputs) {
std::array<char, MAX_NUM> nums = init();
int index = 0;
for (int i = 0; i < inputs.length(); i++) {
char c = inputs[i];
switch (c) {
case '+':
nums[index]++;
break;
case '-':
nums[index]--;
break;
case '>':
if (index < MAX_NUM - 1) {
index++;
} else {
std::cerr
<< "Compute error, beyond the upper bound of array in position:"
<< i + 1 << std::endl;
return -2;
}
break;
case '<':
if (index > 0) {
index--;
} else {
std::cerr
<< "Compute error, below the lower bound of array in position:"
<< i + 1 << std::endl;
return -2;
}
break;
case '.':
std::cout << nums[index];
break;
case '[':
find_match(inputs, i, c);
if (nums[index] == 0) {
i = find_match(inputs, i, c);
if (i == INVALID_INDEX) {
return -1;
}
}
break;
case ']':
if (nums[index] != 0) {
i = find_match(inputs, i, c);
if (i == INVALID_INDEX) {
return -1;
}
}
break;
case ',':
std::cin >> nums[index];
break;
default:
break;
}
}
return 0;
}
std::string charProcess(const char c) {
std::string rest;
int quotient = c / 10;
int remainder = c % 10;
while (quotient) {
rest += "+";
quotient--;
}
rest += "[>++++++++++<-]>";
while (remainder) {
rest += "+";
remainder--;
}
rest += ".>";
return rest;
}
std::string processStr(std::string inputs) {
std::string rest;
for (const auto &c : inputs) {
rest += (charProcess(c) + "\n");
}
return rest;
}
int main(int argc, char *argv[]) {
std::string inputs =
">++++++++\
[<+++++++++> - ]<.> ++++[<+++++++> - ]<+.+++++++..+++.> >\
++++++[<+++++++> - ]<++.------------.> ++++++[<+++++++++> -\
]<+.<.+++.------.--------.>> > ++++[<++++++++> - ] < +.";
processBf(inputs);
inputs = "Test encode with bf\n";
int rest = processBf(processStr(inputs));
return rest;
}