-
Notifications
You must be signed in to change notification settings - Fork 256
/
bf.java
188 lines (159 loc) · 5.42 KB
/
bf.java
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
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public final class bf {
public enum OpT {INC, MOVE, PRINT, LOOP}
public static final class Op {
private static final Op[] NO_LOOP_OPS = new Op[0];
public final OpT op;
public final int v;
public final Op[] loop;
public Op(final OpT _op, final int _v) { op = _op; v = _v; loop = NO_LOOP_OPS; }
public Op(final OpT _op, final Op[] _l) { op = _op; loop = _l; v = 0; }
}
public static final class CharacterIterator {
private final String str;
private final int strLen;
private int pos = 0;
public CharacterIterator(final String str) {
this.str = str;
strLen = str.length();
}
public boolean hasNext() {
return pos < strLen;
}
public char next() {
return str.charAt(pos++);
}
}
public static final class Tape {
private int[] tape;
private int pos;
public Tape() {
tape = new int[1];
}
public int get() {
return tape[pos];
}
public void inc(final int x) {
tape[pos] += x;
}
public void move(final int x) {
pos += x;
while ( pos >= tape.length ) {
this.tape = Arrays.copyOf(this.tape, this.tape.length * 2);
}
}
}
static final class Printer {
private int sum1 = 0;
private int sum2 = 0;
final boolean quiet;
Printer(boolean quiet) {
this.quiet = quiet;
}
void print(int n) {
if (quiet) {
sum1 = (sum1 + n) % 255;
sum2 = (sum2 + sum1) % 255;
} else {
System.out.print((char)n);
}
}
int getChecksum() {
return (sum2 << 8) | sum1;
}
}
public static final class Program {
private final Op[] ops;
private final Printer p;
public Program(final String code, final Printer p) {
final var it = new CharacterIterator(code);
ops = parse(it);
this.p = p;
}
private Op[] parse(final CharacterIterator it) {
final var res = new ArrayList<Op>();
while (it.hasNext()) {
switch(it.next()) {
case '+':
res.add(new Op(OpT.INC, 1));
break;
case '-':
res.add(new Op(OpT.INC, -1));
break;
case '>':
res.add(new Op(OpT.MOVE, 1));
break;
case '<':
res.add(new Op(OpT.MOVE, -1));
break;
case '.':
res.add(new Op(OpT.PRINT, 0));
break;
case '[':
res.add(new Op(OpT.LOOP, parse(it)));
break;
case ']':
return res.toArray(new Op[res.size()]);
}
}
return res.toArray(new Op[res.size()]);
}
public void run() {
_run(ops, new Tape());
}
private void _run(final Op[] program, final Tape tape) {
for (final var op : program)
switch (op.op) {
case INC: tape.inc(op.v); break;
case MOVE: tape.move(op.v); break;
case LOOP: while (tape.get() > 0) _run(op.loop, tape); break;
case PRINT: p.print(tape.get()); break;
}
}
}
private static void notify(final String msg) {
try (final var socket = new java.net.Socket("localhost", 9001);
final var out = socket.getOutputStream()) {
out.write(msg.getBytes("UTF-8"));
} catch (java.io.IOException e) {
// standalone usage
}
}
private static void verify() {
final var text = """
++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>
---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.
""";
final var pLeft = new Printer(true);
new Program(text, pLeft).run();
final var left = pLeft.getChecksum();
final var pRight = new Printer(true);
for (final var c : "Hello World!\n".toCharArray()) {
pRight.print(c);
}
final var right = pRight.getChecksum();
if (left != right) {
System.err.printf("%d != %d\n", left, right);
System.exit(1);
}
}
public static void main( final String[] args ) throws IOException {
verify();
final var code = new String(Files.readAllBytes( Paths.get( args[0] ) ));
final var p = new Printer(System.getenv("QUIET") != null);
notify("Java\t" + ProcessHandle.current().pid());
final var startTime = System.currentTimeMillis();
new Program(code, p).run();
final var elapsed = (System.currentTimeMillis()-startTime) / 1e3;
notify("stop");
System.err.println("time: " + elapsed +"s");
if (p.quiet) {
System.out.println("Output checksum: " + p.getChecksum());
}
}
}