-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRunTM.cpp
More file actions
165 lines (141 loc) · 5.55 KB
/
Copy pathRunTM.cpp
File metadata and controls
165 lines (141 loc) · 5.55 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
#include "Turing/Turing.h"
#include "Turing/Utilities/Unicode.h"
#include <fstream>
#include <stdexcept>
#include <sstream>
#include <iomanip>
#include <iostream>
namespace {
std::vector<char32_t> toChar32(const std::string& input) {
utf8Reader reader(input);
return std::vector<char32_t>(reader.begin(), reader.end());
}
std::vector<char32_t> toChar32(std::istream& in) {
return toChar32(std::string(std::istreambuf_iterator<char>(in), std::istreambuf_iterator<char>()));
}
std::string displayVersionOf(char32_t ch) {
if (ch == Turing::kBlankSymbol) {
return "";
} else if (ch == '\n') {
return "\\n";
} else if (ch == '\t') {
return "\\t";
} else if (ch == ' ') {
return "' '";
} else {
return toUTF8(ch);
}
}
void printTape(const Turing::Interpreter& interpreter) {
auto [start, end] = interpreter.usedTapeRange();
/* Skip forwards/backwards to ignore unnecessary blanks. */
while (start < end && start < interpreter.tapeHeadPos() && interpreter.tapeAt(start) == Turing::kBlankSymbol) {
start++;
}
while (start < end && end - 1 > interpreter.tapeHeadPos() && interpreter.tapeAt(end - 1) == Turing::kBlankSymbol) {
end--;
}
/* Print header row. */
for (auto i = start; i < end; i++) {
std::cout << "┬───";
}
std::cout << '\n';
/* Print tape contents. */
for (auto i = start; i < end; i++) {
std::cout << "│" << std::setw(3) << displayVersionOf(interpreter.tapeAt(i));
}
std::cout << '\n';
/* Print footer row. */
for (auto i = start; i < end; i++) {
std::cout << "┴───";
}
std::cout << '\n';
/* Print tape head position. */
for (auto i = start; i < end; i++) {
if (i == interpreter.tapeHeadPos()) {
std::cout << " ☝";
break;
} else {
std::cout << " ";
}
}
std::cout << std::endl;
}
/* Lists all errors found in the program. */
void showErrorsIn(const Turing::Program& program) {
std::cout << "Errors found in program:" << std::endl;
for (size_t i = 0; i < program.numLines(); i++) {
auto error = program.errorAtLine(i);
if (error != "") {
std::cout << std::setw(6) << i << " " << program.line(i) << std::endl;
std::cout << " " << error << std::endl;
}
}
}
/* Different modes available for running. */
enum class RunMode {
QUIET, // Don't print anything
FULL, // Print everything
FINAL // Only print tape at the very end
};
/* Command-line argument info */
struct Arguments {
std::string sourceFile;
RunMode mode;
};
Arguments parseArguments(int argc, const char* argv[]) {
if (argc == 2) {
return { argv[1], RunMode::FULL };
}
else if (argc == 3 && std::string(argv[1]) == "--quiet") {
return { argv[2], RunMode::QUIET };
} else if (argc == 3 && std::string(argv[1]) == "--final") {
return { argv[2], RunMode::FINAL };
} else {
std::cerr << "Usage: run-tm [--quiet | --final] tm-program" << std::endl;
std::exit(-1);
}
}
}
int main(int argc, const char* argv[]) try {
auto args = parseArguments(argc, argv);
if (args.mode == RunMode::FULL) std::cout << "Parsing program... " << std::flush;
std::ifstream tmSource(args.sourceFile);
Turing::Program program(tmSource);
if (args.mode == RunMode::FULL) std::cout << "done." << std::endl;
/* Display errors if program can't be run. */
if (!program.isValid()) {
showErrorsIn(program);
return -1;
}
if (args.mode == RunMode::FULL) std::cout << "Initializing interpreter... " << std::flush;
Turing::Interpreter interpreter(program, toChar32(std::cin));
if (args.mode == RunMode::FULL) std::cout << "done" << std::endl;
/* To avoid cluttering the screen, only display the tape at the very beginning
* and when something about the tape changes.
*/
if (args.mode == RunMode::FULL) printTape(interpreter);
char32_t lastChar = interpreter.tapeAt(0);
int64_t lastPos = 0;
while (interpreter.state() == Turing::Result::RUNNING) {
/* Print the current line. */
auto lineNum = interpreter.lineNumber();
if (args.mode == RunMode::FULL) std::cout << std::setw(6) << lineNum << ": " << program.line(lineNum) << std::endl;
/* Advance the simulation. */
interpreter.step();
/* If something changed, update the display. */
if (args.mode == RunMode::FULL && (interpreter.tapeHeadPos() != lastPos || interpreter.tapeAt(lastPos) != lastChar)) {
lastPos = interpreter.tapeHeadPos();
lastChar = interpreter.tapeAt(lastPos);
printTape(interpreter);
}
}
if (args.mode == RunMode::FINAL) {
std::cout << std::setw(6) << interpreter.lineNumber() << ": " << program.line(interpreter.lineNumber()) << std::endl;
printTape(interpreter);
}
return interpreter.state() == Turing::Result::REJECT;
} catch (const std::exception& e) {
std::cerr << "Error: " << e.what() << std::endl;
return -1;
}