-
Notifications
You must be signed in to change notification settings - Fork 0
/
Agony.java
266 lines (239 loc) · 7.36 KB
/
Agony.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
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
/**
* Created by Roy van Rijn, http://www.royvanrijn.com
* Feel free to edit/copy/distribute, just be nice and give the appropriate credits.
*/
public class Agony {
/**
* See http://esolangs.org/wiki/Agony
*
* Instruction set:
* 0000 $ Halt the program here
* 0001 } Move pointer right
* 0010 { Move pointer left
* 0011 > Move pointer right 2 cells
* 0100 < Move pointer left 2 cells
* 0101 @ Increment cell
* 0110 ~ Decrement cell
* 0111 + Increment character
* 1000 - Decrement character
* 1001 . Output the character (2 cells) at pointer and pointer-0
* 1010 , Input a character (2 cells) to pointer and pointer-0
* 1011 ( Jump past the matching ) if the cell under the pointer is zero
* 1100 ) Jump back to the matching ( if the cell under the pointer is nonzero
* 1101 [ Jump past the matching ] if the character under the pointer is zero
* 1110 ] Jump back to the matching [ if the character under the pointer is nonzero
* 1111 * Swap character (2 cells) at pointer with the buffer (2 cells, buffer has 0000 0000 initially)
*/
private static final char[] instructions = {'$', '}', '{', '>', '<', '@', '~', '+', '-', '.', ',', '(', ')', '[', ']', '*'};
public static void main(String[] args) throws IOException {
if(args.length != 1) {
System.out.println("Arguments expected: filename");
}
//Read file:
BufferedReader reader = new BufferedReader(new FileReader(new File(args[0])));
StringBuilder builder = new StringBuilder();
while(reader.ready()) {
builder.append(reader.readLine());
}
reader.close();
//Run Agony:
Agony agony = new Agony();
agony.interpret(builder.toString());
}
private static final boolean DEBUG = false;
private static final int MAX_CYCLES = 100000000;
private final int SIZE = 1500;
private final byte[] core = new byte[SIZE];
private byte buffer = 0;
private int cellPointer = 0;
private int executionPointer = 0;
private int cycleCount = 0;
private void interpret(String program) throws IOException {
//Parse input
char[] data = program.toCharArray();
for(int i = 0; i< program.length(); i++) {
parseCharacter(data[i]);
}
cellPointer = (cellPointer + 1) % SIZE;
//Execution
while(step() && cycleCount < MAX_CYCLES);
}
/**
* Returns false if instruction was halted
* @return
* @throws IOException
*/
protected boolean step() throws IOException {
cycleCount++;
if(DEBUG) {
printCore();
}
byte instruction = core[executionPointer];
switch(instruction) {
case 0: // 0000 $ Halt the program here
return false;
case 1: // 0001 } Move pointer right
cellPointer = ((cellPointer + 1) % SIZE);
break;
case 2: // 0010 { Move pointer left
cellPointer = ((cellPointer + SIZE - 1) % SIZE);
break;
case 3: // 0011 > Move pointer right 2 cells
cellPointer = ((cellPointer + 2) % SIZE);
break;
case 4: // 0100 < Move pointer left 2 cells
cellPointer = ((cellPointer + SIZE - 2) % SIZE);
break;
case 5: // 0101 @ Increment cell
core[cellPointer] = (byte) ((core[cellPointer] + 1) % 16);
break;
case 6: // 0110 ~ Decrement cell
core[cellPointer] = (byte) ((core[cellPointer] + 16 - 1) % 16);
break;
case 7: // 0111 + Increment character
assignCharacter((byte)(getCharacterData()+1));
break;
case 8: // 1000 - Decrement character
assignCharacter((byte)(getCharacterData()-1));
break;
case 9: // 1001 . Output the character (2 cells) at pointer and pointer-0
char value = (char)getCharacterData();
output(value);
break;
case 10: // 1010 , Input a character (2 cells) to pointer and pointer-0
byte read = input();
if(read == -1) { //Translate EOF into a halt: 0
read = 0;
}
assignCharacter(read);
break;
case 11: // 1011 ( Jump past the matching ) if the cell under the pointer is zero
if(core[cellPointer] == 0) {
if(!jumpForward(11, 12)) {
//No matching bracket found, halt
return false;
}
}
break;
case 12: // 1100 ) Jump back to the matching ( if the cell under the pointer is nonzero
if(core[cellPointer] != 0) {
if(!jumpBackwards(11, 12)) {
//No matching bracket found, halt
return false;
}
}
break;
case 13: // 1101 [ Jump past the matching ] if the character under the pointer is zero
if(getCharacterData() == 0) {
if(!jumpForward(13, 14)) {
//No matching bracket found, halt
return false;
}
}
break;
case 14: // 1110 ] Jump back to the matching [ if the character under the pointer is nonzero
if(getCharacterData() != 0) {
if(!jumpBackwards(13, 14)) {
//No matching bracket found, halt
return false;
}
}
break;
case 15: // 1111 * Swap value in buffer with cell at pointer location (buffer has 0000 initially)
byte temp = getCharacterData();
assignCharacter(buffer);
buffer = temp;
break;
}
//Increase executionPointer:
executionPointer = (executionPointer + 1) % SIZE;
return true;
}
public byte input() throws IOException {
return (byte)System.in.read();
}
public void output(char value) {
System.out.print(value);
}
private void printCore() {
System.out.println("Step: " + cycleCount);
for(int i = 0; i<core.length; i++) {
System.out.print(instructions[core[i]]);
}
System.out.println();
for(int i = 0; i<core.length; i++) {
if(executionPointer == i && cellPointer == i) {
System.out.print("#");
} else if(executionPointer == i) {
System.out.print("^");
} else if(cellPointer == i) {
System.out.print("!");
} else {
System.out.print(" ");
}
}
System.out.println();
}
/**
* Look forward until:
* 1) We've made a complete loop (returns false, halt execution)
* 2) We have a matching closing bracket at same level
*/
private boolean jumpForward(int openBracket, int closeBracket) {
int startAt = executionPointer;
int depth = 1;
do {
executionPointer = (executionPointer+1) % SIZE;
if(core[executionPointer] == openBracket) {
depth++;
} else if(core[executionPointer] == closeBracket) {
depth--;
}
} while(executionPointer != startAt && depth != 0);
if(depth == 0) { //Have we found the matching bracket?
return true;
}
return false;
}
/**
* Look backwards until:
* 1) We've made a complete loop (returns false, halt execution)
* 2) We have a matching opening bracket at same level
*/
private boolean jumpBackwards(int openBracket, int closeBracket) {
int startAt = executionPointer;
int depth = 1;
do {
executionPointer = (executionPointer + SIZE - 1) % SIZE;
if(core[executionPointer] == closeBracket) {
depth++;
} else if(core[executionPointer] == openBracket) {
depth--;
}
} while(executionPointer != startAt && depth != 0);
if(depth == 0) { //Have we found the matching bracket?
return true;
}
return false;
}
public void assignCharacter(byte read) {
core[(cellPointer + SIZE - 1) % SIZE] = (byte) ((read >>> 4) & 15);
core[cellPointer] = (byte) (read & 15);
}
public byte getCharacterData() {
return (byte) ((core[cellPointer]) | (core[(cellPointer + SIZE - 1) % SIZE] << 4));
}
protected void parseCharacter(char instruction) {
for (int i = 0; i < instructions.length; i++) {
if(instructions[i] == instruction) {
core[cellPointer] = (byte) i;
cellPointer = (cellPointer + 1) % SIZE;
return;
}
}
}
}