-
Notifications
You must be signed in to change notification settings - Fork 259
Expand file tree
/
Copy pathbf.d
More file actions
128 lines (110 loc) · 2.61 KB
/
bf.d
File metadata and controls
128 lines (110 loc) · 2.61 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
import std.conv : to;
import std.exception : assumeUnique;
import std.file : readText;
import std.stdio : write, stdout;
import std.traits : EnumMembers;
import std.socket;
import std.compiler;
import std.format;
import core.thread;
// 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 main(string[] args)
{
string text = readText(args[1]);
notify("%s\t%d".format(name, getpid()));
Program(text).run();
notify("stop");
}
struct Op
{
OpT op;
Op[] loop;
}
enum OpT : dchar
{
inc = '+',
dec = '-',
movePrev = '<',
moveNext = '>',
print = '.',
loop = '!',
}
struct Tape
{
uint pos;
int[] tape = [0];
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() { write(cast(char)get()); stdout.flush(); }
}
struct Program
{
immutable Op[] ops;
this(string code)
{
this.ops = parse(code).assumeUnique;
}
void run()
{
auto t = Tape();
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;
}
}
}
}