Skip to content

Commit 59421d2

Browse files
committed
fix #1 and #2
1 parent 5e0e4e1 commit 59421d2

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

src/main/java/com/falsepattern/jfunge/Main.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ public static void main(String[] args) throws IOException {
6565
} else {
6666
program = Files.readAllBytes(Paths.get(file));
6767
}
68-
System.exit(Interpreter.executeProgram(trefunge, args, program, maxIter, System.in, System.out, Interpreter.DEFAULT_FILE_IO_SUPPLIER));
68+
int returnCode = Interpreter.executeProgram(trefunge, args, program, maxIter, System.in, System.out, Interpreter.DEFAULT_FILE_IO_SUPPLIER);
69+
System.out.flush();
70+
System.exit(returnCode);
6971
}
7072
}

src/main/java/com/falsepattern/jfunge/interpreter/Interpreter.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,12 @@ public Interpreter(boolean trefunge, String[] args, InputStream input, OutputStr
7878
public static int executeProgram(boolean trefunge, String[] args, byte[] program, long iterLimit, InputStream input, OutputStream output, FileIOSupplier fileIOSupplier) {
7979
val interpreter = new Interpreter(trefunge, args, input, output, fileIOSupplier);
8080
interpreter.fungeSpace().loadFileAt(0, 0, 0, program, trefunge);
81+
//Init step
82+
{
83+
val ip = interpreter.IPs.get(0);
84+
ip.position.sub(ip.delta);
85+
interpreter.step(ip);
86+
}
8187
if (iterLimit > 0) {
8288
long step = 0;
8389
while (!interpreter.stopped() && step < iterLimit) {
@@ -148,7 +154,7 @@ public void interpret(int opcode) {
148154
}
149155

150156

151-
private void wrappingStep(InstructionPointer ip) {
157+
private boolean wrappingStep(InstructionPointer ip) {
152158
ip.position.add(ip.delta);
153159
if (!fungeSpace().bounds().inBounds(ip.position)) {
154160
ip.delta.mul(-1);
@@ -162,7 +168,9 @@ private void wrappingStep(InstructionPointer ip) {
162168
do {
163169
ip.position.add(ip.delta);
164170
} while (!fungeSpace().bounds().inBounds(ip.position));
171+
return true;
165172
}
173+
return false;
166174
}
167175

168176
public void step(InstructionPointer ip) {
@@ -174,19 +182,23 @@ public void step(InstructionPointer ip) {
174182
return;
175183
}
176184
}
185+
int flipCount = 0;
177186
do {
178-
wrappingStep(ip);
187+
flipCount += wrappingStep(ip) ? 1 : 0;
179188
p = fungeSpace.get(ip.position);
180189
if (!ip.stringMode) {
181190
while (p == ';') {
182191
do {
183-
wrappingStep(ip);
192+
flipCount += wrappingStep(ip) ? 1 : 0;
184193
p = fungeSpace.get(ip.position);
185194
} while (p != ';');
186-
wrappingStep(ip);
195+
flipCount += wrappingStep(ip) ? 1 : 0;
187196
p = fungeSpace.get(ip.position);
188197
}
189198
}
199+
if (flipCount == 1000) {
200+
throw new IllegalStateException("IP " + ip.UUID + " is stuck on a blank strip!\nPos: " + ip.position + ", Delta: " + ip.delta + (ip.position.equals(0, 0, 0) && ip.UUID == 0 ? "\nIs the file empty?" : ""));
201+
}
190202
} while (p == ' ');
191203
}
192204

0 commit comments

Comments
 (0)