-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterpreter.hpp
194 lines (153 loc) · 5.11 KB
/
interpreter.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
#pragma once
#include <algorithm>
#include <functional>
#include <map>
#include <memory>
#include <string>
#include <vector>
#include <iterator>
namespace BF {
/* global constexprs */
constexpr size_t DEFAULT_MEM_SIZE = static_cast<size_t>(30000);
struct OpInfo;
/* typedefs */
typedef unsigned char mem_t;
typedef mem_t* Ptr;
typedef std::function<bool(OpInfo&)> Operation;
/* ------------------------------ */
/* DEFINITION */
/* ------------------------------ */
struct OpInfo {
Ptr& ptr;
const std::string& input;
std::string::const_iterator& it;
const std::vector<std::string::const_iterator>& jump_table;
friend class Interpreter;
OpInfo(Ptr& ptr, const std::string& input, std::string::const_iterator& it, const std::vector<std::string::const_iterator>& jump_table) :
ptr(ptr), input(input), it(it), jump_table(jump_table) {}
};
class Interpreter {
public:
Interpreter();
Interpreter(size_t mem_size);
void reset();
bool interpret(const std::string& input);
private:
size_t mem_size_;
std::unique_ptr<mem_t[]> mem_;
Ptr ptr_;
std::string::const_iterator init_jump_table(const std::string& input, std::string::const_iterator);
std::map<char, Operation> ops_;
std::vector<std::string::const_iterator> jump_table_;
};
//static void add_operation(std::map<char, Operation>& ops, char op_name, Operation& op);
static void init_BF_operation_map(std::map<char, Operation>& ops);
/* ------------------------------ */
/* IMPLEMENTATION */
/* ------------------------------ */
Interpreter::Interpreter() : Interpreter(DEFAULT_MEM_SIZE) {}
Interpreter::Interpreter(size_t mem_size) : mem_size_(mem_size), mem_(new mem_t[mem_size_]), ptr_(mem_.get()) {
/* init vals to 0 */
reset();
init_BF_operation_map(ops_);
}
void Interpreter::reset() {
/* set all values of mem_ to 0 */
std::fill(mem_.get(), mem_.get() + mem_size_, static_cast<mem_t>(0));
/* set ptr_ to beginning of mem_ */
ptr_ = mem_.get();
}
bool Interpreter::interpret(const std::string& input) {
jump_table_.clear();
jump_table_.resize(input.size(), input.end());
init_jump_table(input, input.begin());
/* interpret string */
for (auto it = input.begin(); it != input.end(); it++) {
auto& op_sym = *it;
/* handle EOF and \n */
if ((*it) == EOF || (*it) == '\n' || (*it) == '\r' || (*it) == '\t') {
continue;
}
auto search = ops_.find(op_sym);
if (search == ops_.end()) {
/* bad input */
std::cerr << "Invalid symbol: " << op_sym << "\n";
continue;
}
Operation& op = search->second;
OpInfo info (ptr_, input, it, jump_table_);
if (!op(info)) {
std::cerr << "Operation failed: " << op_sym << "\n";
}
}
return true;
}
std::string::const_iterator Interpreter::init_jump_table(const std::string& input, std::string::const_iterator it) {
for (; it != input.end(); it++) {
std::string::const_iterator ending;
std::string::const_iterator tmp;
switch (*it) {
case ']':
return it;
case '[':
tmp = it;
tmp++;
ending = init_jump_table(input, tmp);
/* bad match */
if (ending == input.end()) return ending;
jump_table_[std::distance(input.begin(), it)] = ending;
jump_table_[std::distance(input.begin(), ending)] = it;
it = ending;
}
}
return input.end();
}
static void add_operation(std::map<char, Operation>& ops, char op_name, Operation op) {
ops.insert(std::make_pair(op_name, op));
}
static void init_BF_operation_map(std::map<char, Operation>& ops) {
/* clear to begin with */
ops.clear();
/* fill the ops map here */
add_operation(ops, '>', [](OpInfo& info) -> bool {
++(info.ptr);
return true;
});
add_operation(ops, '<', [](OpInfo& info) -> bool {
--(info.ptr);
return true;
});
add_operation(ops, '+', [](OpInfo& info) -> bool {
++(*(info.ptr));
return true;
});
add_operation(ops, '-', [](OpInfo& info) -> bool {
--(*(info.ptr));
return true;
});
add_operation(ops, '.', [](OpInfo& info) -> bool {
std::cout << (*(info.ptr));
return true;
});
add_operation(ops, ',', [](OpInfo& info) -> bool {
std::cin >> (*(info.ptr));
return true;
});
add_operation(ops, '[', [](OpInfo& info) -> bool {
/* move iterator to ending ] */
/* only move to end if value at ptr is 0 */
if ((*info.ptr) != static_cast<mem_t>(0)) {
return true;
}
info.it = info.jump_table[std::distance(info.input.begin(), info.it)];
return true;
});
add_operation(ops, ']', [](OpInfo& info) -> bool {
if ((*info.ptr) == 0) {
return true;
}
info.it = info.jump_table[std::distance(info.input.begin(), info.it)];
return true;
});
}
} // namespace BF