Skip to content

Commit

Permalink
code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
FalsePattern committed Jun 6, 2022
1 parent f4bb153 commit 0d64993
Show file tree
Hide file tree
Showing 22 changed files with 264 additions and 185 deletions.
16 changes: 1 addition & 15 deletions src/main/java/com/falsepattern/jfunge/Globals.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,5 @@ public class Globals {
public static final String VERSION = "1.0.0";
public static final int FUNGE_VERSION = 1 * 256 * 256 + 0 * 256 + 0;
public static final int HANDPRINT = 0xfa15e9a7; //"falsepat"
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/>.";
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/>.";
}
4 changes: 1 addition & 3 deletions src/main/java/com/falsepattern/jfunge/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@
import lombok.val;
import lombok.var;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
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;
import java.util.stream.Stream;

public class Main {
public static void main(String[] args) throws IOException {
Expand Down Expand Up @@ -61,7 +59,7 @@ public static void main(String[] args) throws IOException {
var read = 0;
val buf = new byte[4096];
while ((read = in.read(buf)) > 0) {
programBytes.write(buf, 0, read);
programBytes.write(buf, 0, read);
}
program = programBytes.toByteArray();
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/falsepattern/jfunge/Releasable.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.falsepattern.jfunge;

public interface Releasable extends AutoCloseable{
public interface Releasable extends AutoCloseable {
/**
* 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.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,37 @@
import java.io.OutputStream;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

public interface ExecutionContext {
InstructionPointer[] allIPs();

InstructionPointer IP();

InstructionPointer cloneIP();

FungeSpace fungeSpace();

int dimensions();

boolean stopped();

void stop(int exitCode);

int exitCode();

void interpret(int code);

void step(InstructionPointer ip);

List<String> args();

Map<String, String> env();

int input(boolean stagger);

OutputStream output();

byte[] readFile(String file);

boolean writeFile(String file, byte[] data);
}
47 changes: 26 additions & 21 deletions src/main/java/com/falsepattern/jfunge/interpreter/Interpreter.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
import lombok.val;
import lombok.var;

import java.io.*;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;
Expand Down Expand Up @@ -61,6 +63,18 @@ public boolean writeFile(String file, byte[] data) throws IOException {
private int inputStagger;


public Interpreter(boolean trefunge, String[] args, InputStream input, OutputStream output, FileIOSupplier fileIOSupplier) {
this.args = Arrays.asList(args);
dimensions = trefunge ? 3 : 2;
baseInstructionManager.loadInstructionSet(Funge98.INSTANCE);
this.input = input;
this.output = output;
this.fileIOSupplier = fileIOSupplier;
val ip = new InstructionPointer();
ip.UUID = nextUUID++;
IPs.add(ip);
}

public static int executeProgram(boolean trefunge, String[] args, byte[] program, long iterLimit, InputStream input, OutputStream output, FileIOSupplier fileIOSupplier) {
val interpreter = new Interpreter(trefunge, args, input, output, fileIOSupplier);
interpreter.fungeSpace().loadFileAt(0, 0, 0, program, trefunge);
Expand All @@ -70,7 +84,8 @@ public static int executeProgram(boolean trefunge, String[] args, byte[] program
interpreter.tick();
step++;
}
if (!interpreter.stopped()) throw new IllegalStateException("Program exceeded max iteration count!");
if (!interpreter.stopped())
throw new IllegalStateException("Program exceeded max iteration count!");
} else {
while (!interpreter.stopped()) {
interpreter.tick();
Expand All @@ -79,18 +94,6 @@ public static int executeProgram(boolean trefunge, String[] args, byte[] program
return interpreter.exitCode();
}

public Interpreter(boolean trefunge, String[] args, InputStream input, OutputStream output, FileIOSupplier fileIOSupplier) {
this.args = Arrays.asList(args);
dimensions = trefunge ? 3 : 2;
baseInstructionManager.loadInstructionSet(Funge98.INSTANCE);
this.input = input;
this.output = output;
this.fileIOSupplier = fileIOSupplier;
val ip = new InstructionPointer();
ip.UUID = nextUUID++;
IPs.add(ip);
}

@Override
public InstructionPointer[] allIPs() {
return IPs.toArray(new InstructionPointer[0]);
Expand Down Expand Up @@ -137,7 +140,8 @@ public void interpret(int opcode) {
if ((instr = IP().instructionManager.fetch(opcode)) != null || (instr = baseInstructionManager.fetch(opcode)) != null) {
instr.process(this);
} else {
if (opcode == 'r') throw new IllegalArgumentException("Language does not implement 'r' reflect instruction.");
if (opcode == 'r')
throw new IllegalArgumentException("Language does not implement 'r' reflect instruction.");
interpret('r');
}
}
Expand Down Expand Up @@ -228,11 +232,6 @@ public boolean writeFile(String file, byte[] data) {
}
}

public interface FileIOSupplier {
byte[] readFile(String file) throws IOException;
boolean writeFile(String file, byte[] data) throws IOException;
}

public void tick() {
currentIP = null;
for (int i = 0; i < IPs.size(); i++) {
Expand All @@ -248,8 +247,14 @@ public void tick() {
clone = null;
}
}
for (val ip: IPs) {
for (val ip : IPs) {
step(ip);
}
}

public interface FileIOSupplier {
byte[] readFile(String file) throws IOException;

boolean writeFile(String file, byte[] data) throws IOException;
}
}
Loading

0 comments on commit 0d64993

Please sign in to comment.