-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTuringMachine.h
More file actions
360 lines (323 loc) · 7.27 KB
/
Copy pathTuringMachine.h
File metadata and controls
360 lines (323 loc) · 7.27 KB
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
// TuringMachine.h
// Reed Axewielder
// 2025 Copyright AXEWIELDER GOODS
// GMDA
#pragma once
#include <vector>
#include <cmath>
#include <iostream>
#include <valarray>
#include <string>
enum TMInst: char
{
FE, TE, ST, LT, RT, WE, RD, RN, LD, CL, ED, NG = -1
};
template<typename T>
using TMTape = std::valarray<T>;
template <typename T>
TMTape<T> MakeTape(const unsigned long long& k) {
return std::valarray<T>(T(false), std::pow(2, double(k)));
}
class TuringMachine {
public:
TuringMachine() {
NewTape(16);
}
~TuringMachine() {}
TuringMachine(TMTape<bool>& tape) {
Tape = tape;
zero = Tape.size() / 2;
order = std::log2(zero) + 1;
}
// turing machine with tape of 2^n squares
TuringMachine(const unsigned long long& n) {
NewTape(n);
}
template <typename T>
TuringMachine(T program) {
LoadAndRun(program);
}
template <typename T>
TuringMachine(TMTape<bool>& tape, T program) {
TuringMachine(tape);
LoadAndRun(program);
}
template <typename T>
bool LoadAndRun(T program) {
unsigned ld = Load(program);
if (ld > 0 && states.size() > 0 && ld <= states.size()) {
unsigned se = unsigned(states.size()) - ld;
return Run(states[se]);
}
else return false;
}
void Reset() {
Start();
End();
}
protected:
std::vector<std::string> states{};
long long head = 0;
const unsigned long long infimum = 0;
unsigned long long order = 16;
unsigned long long zero = 1024*64 / 2;
TMTape<bool> Tape = MakeTape<bool>(order);
unsigned state = 0;
unsigned count = 0;
std::vector<unsigned> instnum{};
std::vector<unsigned> previous{};
void NewTape() {
Tape = MakeTape<bool>(order);
}
//new tape with 2^n squares
void NewTape(unsigned long long n) {
Tape = MakeTape<bool>(n);
zero = Tape.size() / 2;
order = n;
}
void MoreTape() {
TMTape<bool> VTape = MakeTape<bool>(order+1);
if (VTape.size() == 2*Tape.size()) {
for (unsigned i = 0; i < Tape.size(); i++) {
VTape[i + zero] = Tape[i];
}
Tape = VTape;
zero = Tape.size() / 2;
++order;
}
else {
std::cerr << "No more tape available. Sorry.\n"
<< "Womp, womp...\n";
}
}
unsigned Load(std::string program) {
unsigned count = 0;
std::string prog = "";
for (auto it = program.begin(); it != program.end(); ++it) {
if (*it != LD) {
prog += (*it);
}
else {
if (!prog.empty() && prog != "") {
states.push_back(prog);
count++;
}
prog = "";
}
}
if (!prog.empty() && prog != "") {
states.push_back(prog);
count++;
}
return count;
}
unsigned Load(std::vector<std::string> program) {
unsigned count = 0;
for (std::string& s : program) {
count += Load(s);
}
return count;
}
bool Call(unsigned state) {
bool retval = false;
if (state < states.size()) {
previous.push_back(this->state);
instnum.push_back(count);
this->state = state;
retval = Run(states[state]);
this->state = previous.back();
previous.pop_back();
count = instnum.back();
instnum.pop_back();
}
else std::cerr << "State not in memory";
return retval;
}
void Left() {
if (--head < -(long long(zero))) {
MoreTape();
}
}
void Right() {
if (++head >= long long(zero)) {
MoreTape();
}
}
void Write(bool a) {
Tape[head + (zero)] = a;
}
bool Read() {
return Tape[head + (zero)] ;
}
void Start(unsigned long long n) {
head = state = count = 0;
NewTape(n);
states.clear();
instnum.clear();
previous.clear();
}
void Start() {
head = state = count = 0;
Tape = false;
states.clear();
instnum.clear();
previous.clear();
}
void End() {
std::string mess = "";
std::string lt = "", rt = "";
long long block = head / 64;
for (unsigned i = 0; i < 64; i++) {
if (Tape[(zero/64 + block - 1) * 64 + i] == false) lt += "0";
else lt += "1";
if (Tape[(zero/64 + block + 1) * 64 - i - 1] == false) rt += "0";
else rt += "1";
}
if (head >= 0) {
for (int i = 0; i < 63 - (head % 64); i++) {
mess += " ";
}
mess += "_";
}
else if (head < 0) {
for (int i = -1; i > -64 - ((head + 1) % 64); i--) {
mess += " ";
}
mess += "^";
}
std::cout << "State of the machine: \n"
<< lt << std::endl
<< mess << std::endl
<< rt << std::endl;
std::cout << "Head: " << head << std::endl;
std::cout << "State: " << state << std::endl;
std::cout << "Count: " << count << std::endl;
std::cout << "Trail: ";
for (auto a : previous) {
std::cout << a << " ";
}
std::cout << "\n"
<< "Route: ";
for (auto a : instnum) {
std::cout << a << " ";
}
std::cout << "\n"
<< "States: " << states.size() << std::endl;
}
bool Run(std::string Program) {
bool retval = true;
count = 0;
std::string prgrm = "";
for (std::string::iterator it = (Program.begin() + count); it < Program.end(); ++it) {
char c = *it;
count++;
switch (c)
{
case ST: // Start. Initialize to zero.
{
std::string number = "";
while (it + 1 != Program.end() && *(it+1) != NG) {
++it;
++count;
number += *it;
}
if (number == "" && std::stoi(number)>=0) {
Start(std::stoi(number));
}
else {
Start();
}
break;
}
case LT: // Move tape head to the left.
Left();
break;
case RT: // Move tape head to the right.
Right();
break;
case CL: // Conditional Call
{
++it;
count++;
if (it != Program.end() && *it == TE) {
++it;
count++;
if (Read() == true)
retval = Call(int(*it));
else {
++it;
count++;
if (it != Program.end() && *it != NG)
retval = Call(int(*it));
}
}
else if (it != Program.end() && *it == FE) {
++it;
count++;
if (Read() == false)
retval = Call(int(*it));
else {
++it;
count++;
if (it != Program.end() && *it != NG)
retval = Call(int(*it));
}
}
if (it == Program.end()) {
break; break;
}
break;
}
case WE: // Write TE or FE at tape head.
if ((it + 1) == Program.end()) { break; break; }
if (*(it + 1) == TE) Write(true);
if (*(it + 1) == FE) Write(false);
++it;
count++;
break;
case NG: // Do nothing.
break;
case RD: // Read the value at the tape head.
retval = Read();
break;
case LD: // Load a machine state program.
{
std::string prog(it + 1, Program.end());
if (!prog.empty() && prog != "") {
Load(prog);
}
break;
}
case RN: // Run the following program until the end.
{
prgrm.clear();
prgrm = "";
++it;
count++;
while (it != Program.end() && (*it) != ED) {
prgrm += (*it);
++it;
count++;
}
if ((*it) == ED) {
prgrm += (*it);
++it;
count++;
}
TuringMachine* TM = new TuringMachine();
retval = TM->LoadAndRun(prgrm);
delete(TM);
if (it == Program.end()) { break; break; }
break;
}
case ED: // End the program and print the machine state.
End();
break;
default:
retval = false;
break;
}
}
return retval;
}
};