-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.d
218 lines (192 loc) · 5.14 KB
/
main.d
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
import std.stdio;
import std.file;
import std.string;
import std.algorithm;
import std.ascii;
import std.array;
import std.exception;
import std.conv;
import std.range;
import std.typecons;
import core.stdc.stdlib;
enum Step
{
L,
R,
}
alias State = string;
alias Symbol = string;
struct Turd
{
State current;
Symbol read;
Symbol write;
Step step;
State next;
}
struct Machine
{
Symbol[] tape;
ulong head;
State state;
bool next(Turd[] program)
{
foreach (turd; program) {
if (turd.current == state && turd.read == tape[head]) {
tape[head] = turd.write;
final switch (turd.step) {
case Step.L:
if (head == 0) {
head = tape.length - 1;
} else {
head -= 1;
}
break;
case Step.R:
head = (head + 1) % tape.length;
break;
}
state = turd.next;
return true;
}
}
return false;
}
void dump_tape()
{
foreach (cell; tape) {
write(cell, ' ');
}
writeln();
}
void debug_dump()
{
writeln("STATE: ", state);
dump_tape();
foreach (i, cell; tape) {
if (i == head) write('^');
for (int j = 0; j < cell.length; ++j) {
write(' ');
}
if (i != head) write(' ');
}
}
}
Turd parseTurd(string filepath,
Tuple!(int, "index", string, "value") s)
{
auto tokens = s.value.split!isWhite.map!(x => x.strip).array;
if (tokens.length != 5) {
writeln(filepath, ":", s.index, ": A single turd is expected to have 5 tokens");
exit(1);
}
immutable int CURRENT = 0;
immutable int READ = 1;
immutable int WRITE = 2;
immutable int STEP = 3;
immutable int NEXT = 4;
Turd turd;
turd.current = tokens[CURRENT];
turd.read = tokens[READ];
turd.write = tokens[WRITE];
switch (tokens[STEP]) {
case "L":
turd.step = Step.L;
break;
case "R":
turd.step = Step.R;
break;
default:
writeln(filepath, ":", s[0], ": `", tokens[STEP], "` is not a correct step. Expected `L` or `R`.");
exit(1);
}
turd.next = tokens[NEXT];
return turd;
}
void usage(File stream)
{
stream.writeln("Usage: turd [OPTIONS] <input.turd> <input.tape>");
stream.writeln("OPTIONS:");
stream.writeln(" --help|-h print this help to stdout and exit with 0 exit code");
stream.writeln(" --state|-s [STATE] start from a specific initial state (default: BEGIN)");
stream.writeln(" --head|-p [POSITION] start from a specific head position (default: 0)");
stream.writeln(" --non-interactively execute the program non-interactively");
}
int main(string[] args)
{
bool non_interactively = false;
Machine machine;
machine.head = 0;
machine.state = "BEGIN";
int args_index = 1;
while (args_index < args.length && !args[args_index].empty && args[args_index][0] == '-') {
auto flag = args[args_index++];
void expect_argument() {
if (args_index >= args.length) {
usage(stderr);
stderr.writeln("ERROR: No argument provided for flag `", flag, "`");
exit(1);
}
}
switch (flag) {
case "--help":
case "-h":
usage(stdout);
exit(0);
break;
case "--state":
case "-s":
expect_argument();
machine.state = args[args_index++];
break;
case "--head":
case "-p":
expect_argument();
machine.head = args[args_index++].to!ulong;
break;
case "--non-interactively":
non_interactively = true;
break;
default:
usage(stderr);
stderr.writeln("ERROR: unknown flag `", flag, "`");
exit(1);
}
}
if (args_index >= args.length) {
usage(stderr);
stderr.writeln("ERROR: not turd input file is provided");
exit(1);
}
auto turd_filepath = args[args_index++];
if (args_index >= args.length) {
usage(stderr);
stderr.writeln("ERROR: not tape input file is provided");
exit(1);
}
auto tape_filepath = args[args_index++];
auto turds = readText(turd_filepath)
.splitLines
.map!(x => x.strip)
.enumerate(1)
// Ignore empty lines
.filter!(x => !x.value.empty)
// Ignore commented out lines
.filter!(x => x.value[0] != '#')
.map!(x => parseTurd(turd_filepath, x))
.array;
machine.tape = readText(tape_filepath)
.split!isWhite
.map!(x => x.strip)
.array;
if (non_interactively) {
while (machine.next(turds)) {}
machine.dump_tape();
} else {
do {
machine.debug_dump();
readln();
} while (machine.next(turds));
}
return 0;
}