-
Notifications
You must be signed in to change notification settings - Fork 0
/
bf.hpp
225 lines (163 loc) · 4.15 KB
/
bf.hpp
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#ifndef __BF_HPP__
#define __BF_HPP__
#include <iostream>
#include <memory>
#include <exception>
#include <vector>
#include <string>
#ifndef CELL_T
#define CELL_T char
#endif
using cell_t = CELL_T;
using inst_t = char;
using ip_t = std::vector<inst_t>;
class Bfexception final: public std::exception
{
const std::string msg;
const unsigned int id;
public:
Bfexception(const std::size_t eid):msg(exc[eid]),id(eid) {}
Bfexception(std::string &&msg):msg(std::move(msg)),id(-1U) {}
~Bfexception() override {}
const char* what() const noexcept override
{
return msg.c_str();
}
static const char *exc[];
enum eid
{
eid_incptr,
eid_decptr,
eid_addptr,
eid_subptr,
eid_while,
eid_endwhile
};
};
const char *Bfexception::exc[]={
"Can not increase PTR Tape. Please increase mem Tape.",
"Can not decrease PTR Tape. Please check your code.",
"Can not Addition PTR Tape. Please increase mem Tape.",
"Can not Subtraction PTR Tape. Please check your code.",
"[ must be end with ]. Please check your code.",
"] must be begin with [. Please check your code.",
nullptr};
class Tape: protected std::unique_ptr<cell_t[]>
{
protected:
cell_t *ptr;
std::size_t length;
public:
Tape(std::size_t s=512)
{
settape(s);
}
void settape(std::size_t s)
{
reset(new cell_t[s]);
ptr=get();
length=s;
for(std::size_t i=0;i<length;++i)
get()[i] = 0;
}
void destroytape(void)
{
release();
ptr=nullptr;
length=0;
}
const cell_t * const getbaseptr() const
{
return get();
}
const cell_t * const getptr() const {return ptr;}
cell_t * const getptr_mutable() const {return ptr;}
std::size_t getlength() const {return length;}
cell_t * const operator ++ ()
{
if(ptr+1>=get()+length) throw Bfexception(Bfexception::eid_incptr);
return ++ptr;
}
cell_t * const operator -- ()
{
if(ptr-1<get()) throw Bfexception(Bfexception::eid_decptr);
return --ptr;
}
cell_t & operator * () const {return *ptr;}
cell_t * const operator + (std::size_t n) const
{
if(ptr+n>=get()+length) throw Bfexception(Bfexception::eid_addptr);
return (ptr+n);
}
cell_t * const operator - (std::size_t n) const
{
if(ptr-n<get()) throw Bfexception(Bfexception::eid_subptr);
return (ptr-n);
}
};
class Brainfuck
{
protected:
std::ostream out;
template <class T>
static std::pair<T,int> openbracket(T i, T j,int n=1)
{
while(n && i!=j)
{
if(*i == '[') ++n;
else if (*i == ']') --n;
if(n) ++i;
}
return std::make_pair(i,n);
}
template <class T>
static std::pair<T,int> closebracket(T i, T j,int n=-1)
{
while(n && i!=j)
{
if(*i == '[') ++n;
else if (*i == ']') --n;
if(n) --i;
}
return std::make_pair(i,n);
}
public:
static const inst_t inst[];
Brainfuck(std::streambuf *rd=std::cout.rdbuf()):out(rd){}
template <class T>
int eval (Tape &tape,T begin, T end,int n=0)
{
auto ip=begin;
while(ip != end)
{
int ch;
switch (*ip)
{
case '>': ++tape; break;
case '<': --tape; break;
case '+': ++(*tape); break;
case '-': --(*tape); break;
case '.': out << static_cast<char>(*tape.getptr()); out.flush(); break;
case ',':
ch = std::cin.get();
#if defined(EOF_UNCHANGED)
if(ch != std::char_traits<char>::eof())
*tape.getptr_mutable() = ch;
#elif defined(EOF_MINUS1)
*tape.getptr_mutable()=(ch == std::char_traits<char>::eof()) ? -1 : ch;
#elif defined(EOF_0)
*tape.getptr_mutable()=(ch == std::char_traits<char>::eof()) ? 0 : ch;
#else
*tape.getptr_mutable() = ch;
#endif
case '[': if (!*tape) std::tie(ip,n) = openbracket(++ip,end); break;
case ']': if (*tape) std::tie(ip,n) = closebracket(--ip,begin); break;
default: break;
}
if(ip!=end) ++ip;
}
return n;
}
};
const inst_t Brainfuck::inst[] = "><+-.,[]";
#endif