-
Notifications
You must be signed in to change notification settings - Fork 0
/
brainfuck.cc
144 lines (120 loc) · 2.97 KB
/
brainfuck.cc
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
#include <fstream>
#include <iostream>
#include <limits>
#include <memory>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
#include <cerrno>
#include <cstring>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
using std::cerr;
using std::cin;
using std::endl;
using std::vector;
using std::ifstream;
using std::numeric_limits;
using pos_t = long long;
using cell_t = unsigned char;
class Code {
public:
using value_type = cell_t;
using isiterator = std::istreambuf_iterator<char>;
Code(ifstream &input) : code_(isiterator(input), isiterator()), pos_{0} {}
bool done() { return pos_ >= static_cast<pos_t>(code_.size()); }
cell_t &curr() { return code_.at(pos_); }
const cell_t &curr() const { return code_.at(pos_); }
pos_t &pos() { return pos_; }
const pos_t &pos() const { return pos_; }
private:
vector<cell_t> code_{};
pos_t pos_{};
};
class Tape {
public:
using value_type = cell_t;
pos_t &pos() { return pos_; }
const pos_t &pos() const { return pos_; }
cell_t &curr() { return tape_.at(pos_); }
const cell_t &curr() const { return tape_.at(pos_); }
size_t size() const { return tape_.size(); }
void push(cell_t item){ tape_.push_back(item); }
private:
vector<cell_t> tape_{};
pos_t pos_{};
};
class Interpreter {
public:
Interpreter(ifstream &input) : code{input}, tape{} {}
bool run(bool skip = false) {
while (tape.pos() >= 0 && !code.done()) {
if (tape.pos() >= static_cast<pos_t>(tape.size())) {
tape.push(0);
}
if (code.curr() == '[') {
code.pos()++;
const pos_t oldPos = code.pos();
while (run(tape.curr() == 0)) {
code.pos() = oldPos;
}
} else if (code.curr() == ']') {
return tape.curr() != 0;
} else if (!skip) {
switch (code.curr()) {
case '+': {
if (tape.curr() < numeric_limits<decltype(tape)::value_type>::max())
tape.curr()++;
} break;
case '-': {
if (tape.curr() > 0)
tape.curr()--;
} break;
case '>': {
tape.pos()++;
} break;
case '<': {
tape.pos()--;
} break;
case '.': {
cerr << tape.curr();
} break;
case ',': {
cin >> tape.curr();
} break;
}
}
code.pos()++;
}
return true;
}
private:
Code code;
Tape tape;
};
int main(int c, char **v) {
if (c < 2) {
cerr << "usage: brainfuck filename.b" << endl;
return 127;
}
const std::string filename{v[1]};
struct stat st {};
if (stat(filename.c_str(), &st) == -1) {
cerr << "brainfuck: " << strerror(errno) << endl;
return 127;
}
if (!S_ISREG(st.st_mode)) {
cerr << "brainfuck: " << filename << " not a regular file" << endl;
return 127;
}
ifstream ff(filename);
if (ff) {
Interpreter interpreter{ff};
interpreter.run();
} else {
cerr << "brainfuck: " << strerror(errno) << endl;
return 127;
}
}