-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbf.cpp
204 lines (193 loc) · 5.44 KB
/
bf.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#include <cstdio>
#include <format>
#include <fstream>
#include <iostream>
#include <istream>
#include <stdexcept>
#include <string>
#include <vector>
const std::string name = "bf";
const std::string read_all(std::istream &in);
const std::string read_file(const char* name);
const std::vector<int> parse(const std::string source);
void add(std::vector<int>& codes, const int code, const int diff);
bool is_mults(const std::vector<int>& codes, const int start);
void execute(const std::vector<int> codes);
int main(const int argc, const char* argv[])
{
try {
execute(parse(argc <= 1 ? read_all(std::cin) : read_file(argv[1])));
} catch (const std::exception& err) {
std::cerr << name << ": " << err.what() << std::endl;
return 1;
}
return 0;
}
const std::string read_all(std::istream &in)
{
std::string str(
(std::istreambuf_iterator<char>(in)),
std::istreambuf_iterator<char>());
return str;
}
const std::string read_file(const char* name)
{
std::ifstream file(name, std::ios::in | std::ios::binary);
if (!file)
throw std::runtime_error("failed to open " +
std::string(name) + ": " +
std::string(std::strerror(errno)));
return read_all(file);
}
const int bf_incr = 0; // + -
const int bf_next = 1; // > <
const int bf_putc = 2; // .
const int bf_getc = 3; // ,
const int bf_jmpz = 4; // [
const int bf_jmpnz = 5; // ]
const int bf_setz = 6; // [-]
const int bf_mult = 7; // [->+>++<<]
const int offset = 3;
const int mask = (1 << offset) - 1;
const int offset_mult = offset + 8;
const std::string format(const char* fmt, const int i) // TODO: use std::format
{
std::string str(fmt);
str.replace(str.find("{}"), 2, std::to_string(i));
return str;
}
const std::vector<int> parse(const std::string source)
{
std::vector<int> codes, jmps;
for (int i = 0; i < source.size(); i++) {
switch (source[i]) {
case '+':
add(codes, bf_incr, 1);
break;
case '-':
add(codes, bf_incr, -1);
break;
case '>':
add(codes, bf_next, 1);
break;
case '<':
add(codes, bf_next, -1);
break;
case '.':
codes.push_back(bf_putc);
break;
case ',':
codes.push_back(bf_getc);
break;
case '[':
jmps.push_back(codes.size());
codes.push_back(bf_jmpz);
break;
case ']':
if (jmps.size() == 0)
throw std::runtime_error(format("unmatched ] at byte {}", i + 1));
int j = jmps.back(), l = codes.size();
if (j+2 == l && codes[j+1] == (bf_incr | -(1 << offset))) {
codes[j] = bf_setz;
codes.resize(j+1);
} else if (j+2 < l && (l-j)%2 == 1 && is_mults(codes, j)) {
for (int k = j+2, p = 0; k < l-1; j += 1, k += 2) {
codes[j] = bf_mult | (p += codes[k] >> offset) << offset_mult
| int(uint8_t(codes[k+1] >> offset)) << offset;
}
codes[j] = bf_setz;
codes.resize(j+1);
} else {
codes[j] |= l << offset;
codes.push_back(bf_jmpnz | j << offset);
}
jmps.pop_back();
break;
}
}
if (!jmps.empty()) {
for (int i = source.size() - 1, depth = 0; i >= 0; i--) {
switch (source[i]) {
case '[':
if (--depth < 0)
throw std::runtime_error(format("unmatched [ at byte {}", i + 1));
break;
case ']':
depth++;
break;
}
}
}
return codes;
}
void add(std::vector<int>& codes, const int code, const int diff)
{
if (codes.empty() || (codes.back() & mask) != code)
codes.push_back(code | diff << offset);
else
codes.back() += diff << offset;
}
bool is_mults(const std::vector<int>& codes, const int start) {
int p = 0;
for (int i = 0, k = start+1; k < codes.size(); i += 1, k += 2) {
if ((codes[k] & mask) == bf_incr &&
(i > 0 || (codes[k] >> offset) == -1) &&
(codes[k+1] & mask) == bf_next) {
p += codes[k+1] >> offset;
} else {
return false;
}
}
return p == 0;
}
void execute(const std::vector<int> codes)
{
std::vector<uint8_t> memory(1, 0);
int pointer = 0;
for (int i = 0; i < codes.size(); i++) {
int c = codes[i];
switch (codes[i] & mask) {
case bf_incr:
memory[pointer] += c >> offset;
break;
case bf_next:
pointer += c >> offset;
if (pointer < 0)
throw std::runtime_error("negative address access");
if (pointer >= memory.size())
memory.resize(pointer + 1);
break;
case bf_putc:
if (std::putchar(memory[pointer]) == EOF)
throw std::runtime_error("putchar failed");
break;
case bf_getc:
int ch;
if ((ch = std::getchar()) == EOF && !feof(stdin))
throw std::runtime_error("getchar failed");
memory[pointer] = ch;
break;
case bf_jmpz:
if (memory[pointer] == 0)
i = c >> offset;
break;
case bf_jmpnz:
if (memory[pointer] != 0)
i = c >> offset;
break;
case bf_setz:
memory[pointer] = 0;
break;
case bf_mult:
if (uint8_t v = memory[pointer]; v != 0) {
int dest = pointer + (c >> offset_mult);
if (dest < 0)
throw std::runtime_error("negative address access");
if (dest >= memory.size())
memory.resize(dest + 1);
memory[dest] += v * uint8_t(c >> offset);
}
break;
}
}
}