Skip to content

Commit 0d64993

Browse files
committed
code cleanup
1 parent f4bb153 commit 0d64993

File tree

22 files changed

+264
-185
lines changed

22 files changed

+264
-185
lines changed

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

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,5 @@ public class Globals {
44
public static final String VERSION = "1.0.0";
55
public static final int FUNGE_VERSION = 1 * 256 * 256 + 0 * 256 + 0;
66
public static final int HANDPRINT = 0xfa15e9a7; //"falsepat"
7-
public static final String LICENSE = "JFunge - A standard-conforming Befunge-98 and Trefunge-98 interpreter\n" +
8-
"Copyright (C) 2022 FalsePattern\n" +
9-
"\n" +
10-
"This program is free software: you can redistribute it and/or modify\n" +
11-
"it under the terms of the GNU General Public License as published by\n" +
12-
"the Free Software Foundation, either version 3 of the License, or\n" +
13-
"(at your option) any later version.\n" +
14-
"\n" +
15-
"This program is distributed in the hope that it will be useful,\n" +
16-
"but WITHOUT ANY WARRANTY; without even the implied warranty of\n" +
17-
"MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n" +
18-
"GNU General Public License for more details.\n" +
19-
"\n" +
20-
"You should have received a copy of the GNU General Public License\n" +
21-
"along with this program. If not, see <https://www.gnu.org/licenses/>.";
7+
public static final String LICENSE = "JFunge - A standard-conforming Befunge-98 and Trefunge-98 interpreter\n" + "Copyright (C) 2022 FalsePattern\n" + "\n" + "This program is free software: you can redistribute it and/or modify\n" + "it under the terms of the GNU General Public License as published by\n" + "the Free Software Foundation, either version 3 of the License, or\n" + "(at your option) any later version.\n" + "\n" + "This program is distributed in the hope that it will be useful,\n" + "but WITHOUT ANY WARRANTY; without even the implied warranty of\n" + "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n" + "GNU General Public License for more details.\n" + "\n" + "You should have received a copy of the GNU General Public License\n" + "along with this program. If not, see <https://www.gnu.org/licenses/>.";
228
}

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,13 @@
44
import lombok.val;
55
import lombok.var;
66

7-
import java.io.ByteArrayInputStream;
87
import java.io.ByteArrayOutputStream;
98
import java.io.IOException;
109
import java.nio.file.Files;
1110
import java.nio.file.Paths;
1211
import java.util.ArrayList;
1312
import java.util.Arrays;
1413
import java.util.List;
15-
import java.util.stream.Stream;
1614

1715
public class Main {
1816
public static void main(String[] args) throws IOException {
@@ -61,7 +59,7 @@ public static void main(String[] args) throws IOException {
6159
var read = 0;
6260
val buf = new byte[4096];
6361
while ((read = in.read(buf)) > 0) {
64-
programBytes.write(buf, 0, read);
62+
programBytes.write(buf, 0, read);
6563
}
6664
program = programBytes.toByteArray();
6765
} else {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.falsepattern.jfunge;
22

3-
public interface Releasable extends AutoCloseable{
3+
public interface Releasable extends AutoCloseable {
44
/**
55
* When called, it signals that this specific object is no longer referenced anywhere in the code, and can be freely deleted or even reused later by the provider.
66
*/

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,37 @@
66
import java.io.OutputStream;
77
import java.util.List;
88
import java.util.Map;
9-
import java.util.Scanner;
109

1110
public interface ExecutionContext {
1211
InstructionPointer[] allIPs();
12+
1313
InstructionPointer IP();
14+
1415
InstructionPointer cloneIP();
16+
1517
FungeSpace fungeSpace();
18+
1619
int dimensions();
20+
1721
boolean stopped();
22+
1823
void stop(int exitCode);
24+
1925
int exitCode();
26+
2027
void interpret(int code);
28+
2129
void step(InstructionPointer ip);
30+
2231
List<String> args();
32+
2333
Map<String, String> env();
34+
2435
int input(boolean stagger);
36+
2537
OutputStream output();
38+
2639
byte[] readFile(String file);
40+
2741
boolean writeFile(String file, byte[] data);
2842
}

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

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
import lombok.val;
1111
import lombok.var;
1212

13-
import java.io.*;
13+
import java.io.IOException;
14+
import java.io.InputStream;
15+
import java.io.OutputStream;
1416
import java.nio.file.Files;
1517
import java.nio.file.Paths;
1618
import java.util.*;
@@ -61,6 +63,18 @@ public boolean writeFile(String file, byte[] data) throws IOException {
6163
private int inputStagger;
6264

6365

66+
public Interpreter(boolean trefunge, String[] args, InputStream input, OutputStream output, FileIOSupplier fileIOSupplier) {
67+
this.args = Arrays.asList(args);
68+
dimensions = trefunge ? 3 : 2;
69+
baseInstructionManager.loadInstructionSet(Funge98.INSTANCE);
70+
this.input = input;
71+
this.output = output;
72+
this.fileIOSupplier = fileIOSupplier;
73+
val ip = new InstructionPointer();
74+
ip.UUID = nextUUID++;
75+
IPs.add(ip);
76+
}
77+
6478
public static int executeProgram(boolean trefunge, String[] args, byte[] program, long iterLimit, InputStream input, OutputStream output, FileIOSupplier fileIOSupplier) {
6579
val interpreter = new Interpreter(trefunge, args, input, output, fileIOSupplier);
6680
interpreter.fungeSpace().loadFileAt(0, 0, 0, program, trefunge);
@@ -70,7 +84,8 @@ public static int executeProgram(boolean trefunge, String[] args, byte[] program
7084
interpreter.tick();
7185
step++;
7286
}
73-
if (!interpreter.stopped()) throw new IllegalStateException("Program exceeded max iteration count!");
87+
if (!interpreter.stopped())
88+
throw new IllegalStateException("Program exceeded max iteration count!");
7489
} else {
7590
while (!interpreter.stopped()) {
7691
interpreter.tick();
@@ -79,18 +94,6 @@ public static int executeProgram(boolean trefunge, String[] args, byte[] program
7994
return interpreter.exitCode();
8095
}
8196

82-
public Interpreter(boolean trefunge, String[] args, InputStream input, OutputStream output, FileIOSupplier fileIOSupplier) {
83-
this.args = Arrays.asList(args);
84-
dimensions = trefunge ? 3 : 2;
85-
baseInstructionManager.loadInstructionSet(Funge98.INSTANCE);
86-
this.input = input;
87-
this.output = output;
88-
this.fileIOSupplier = fileIOSupplier;
89-
val ip = new InstructionPointer();
90-
ip.UUID = nextUUID++;
91-
IPs.add(ip);
92-
}
93-
9497
@Override
9598
public InstructionPointer[] allIPs() {
9699
return IPs.toArray(new InstructionPointer[0]);
@@ -137,7 +140,8 @@ public void interpret(int opcode) {
137140
if ((instr = IP().instructionManager.fetch(opcode)) != null || (instr = baseInstructionManager.fetch(opcode)) != null) {
138141
instr.process(this);
139142
} else {
140-
if (opcode == 'r') throw new IllegalArgumentException("Language does not implement 'r' reflect instruction.");
143+
if (opcode == 'r')
144+
throw new IllegalArgumentException("Language does not implement 'r' reflect instruction.");
141145
interpret('r');
142146
}
143147
}
@@ -228,11 +232,6 @@ public boolean writeFile(String file, byte[] data) {
228232
}
229233
}
230234

231-
public interface FileIOSupplier {
232-
byte[] readFile(String file) throws IOException;
233-
boolean writeFile(String file, byte[] data) throws IOException;
234-
}
235-
236235
public void tick() {
237236
currentIP = null;
238237
for (int i = 0; i < IPs.size(); i++) {
@@ -248,8 +247,14 @@ public void tick() {
248247
clone = null;
249248
}
250249
}
251-
for (val ip: IPs) {
250+
for (val ip : IPs) {
252251
step(ip);
253252
}
254253
}
254+
255+
public interface FileIOSupplier {
256+
byte[] readFile(String file) throws IOException;
257+
258+
boolean writeFile(String file, byte[] data) throws IOException;
259+
}
255260
}

0 commit comments

Comments
 (0)