-
Notifications
You must be signed in to change notification settings - Fork 258
/
Copy pathbf.d
230 lines (201 loc) · 3.99 KB
/
bf.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
219
220
221
222
223
224
225
226
227
228
229
230
import std.conv : to;
import std.exception : assumeUnique;
import std.file : readText;
import std.stdio;
import std.traits : EnumMembers;
import std.socket;
import std.compiler;
import std.format;
import core.thread;
import std.process;
import core.stdc.stdlib;
// for compatability with older versions of the standard library
static if (__VERSION__ < 2068)
import std.typetuple : Erase;
else
import std.meta : Erase;
void notify(string msg)
{
try
{
auto socket = new TcpSocket(new InternetAddress("localhost", 9001));
scope (exit)
socket.close();
socket.send(msg);
}
catch (SocketOSException)
{
// standalone usage
}
}
void verify()
{
immutable text = "++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>
---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.";
auto p_left = new Printer(true);
Program(text).run(p_left);
immutable left = p_left.get_checksum();
auto p_right = new Printer(true);
foreach (i, c; "Hello World!\n")
{
p_right.print(c);
}
immutable right = p_right.get_checksum();
if (left != right)
{
stderr.writefln("%s != %s", left, right);
exit(1);
}
}
void main(string[] args)
{
verify();
auto text = readText(args[1]);
auto p = new Printer(environment.get("QUIET") != null);
notify("%s\t%d".format(name, getpid()));
Program(text).run(p);
notify("stop");
if (p.quiet)
{
writeln("Output checksum: %d".format(p.get_checksum()));
}
}
struct Op
{
OpT op;
Op[] loop;
}
enum OpT : dchar
{
inc = '+',
dec = '-',
movePrev = '<',
moveNext = '>',
print = '.',
loop = '!',
}
struct Tape
{
uint pos;
int[] tape;
Printer* p;
this(Printer* p)
{
this.pos = 0;
this.tape = new int[1];
this.p = p;
}
int get()
{
return tape[pos];
}
void inc()
{
tape[pos]++;
}
void dec()
{
tape[pos]--;
}
void movePrev()
{
pos--;
}
void moveNext()
{
pos++;
if (pos == tape.length)
tape ~= 0;
}
void print()
{
p.print(get());
}
}
struct Printer
{
int sum1;
int sum2;
bool quiet;
this(bool quiet)
{
this.sum1 = 0;
this.sum2 = 0;
this.quiet = quiet;
}
void print(int n)
{
if (quiet)
{
sum1 = (sum1 + n) % 255;
sum2 = (sum2 + sum1) % 255;
}
else
{
write(cast(char) n);
stdout.flush();
}
}
int get_checksum() const
{
return (sum2 << 8) | sum1;
}
}
struct Program
{
immutable Op[] ops;
this(string code)
{
this.ops = parse(code).assumeUnique;
}
void run(Printer* p)
{
auto t = Tape(p);
run(ops, t);
}
static Op[] parse(ref string code)
{
Op[] res;
while (code.length)
{
char c = code[0];
code = code[1 .. $];
switch (c)
{
case '+':
case '-':
case '.':
case '>':
case '<':
res ~= Op(cast(OpT) c);
break;
case '[':
res ~= Op(OpT.loop, parse(code));
break;
case ']':
return res;
default:
break;
}
}
return res;
}
static void run(immutable(Op)[] program, ref Tape tape)
{
loop: foreach (op; program)
{
switch (op.op)
{
foreach (type; Erase!(OpT.loop, EnumMembers!OpT))
case type:
mixin("tape." ~ type.to!string ~ "(); continue loop;");
case OpT.loop:
while (tape.get() > 0)
run(op.loop, tape);
break;
default:
break;
}
}
}
}