Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
brainfuck in Javagony ( https://esolangs.org/wiki/Javagony )
//https://esolangs.org/wiki/Javagony
//Because who needs real control flow?
//Theres probably some problems with it
//but at this point i'd rather die than
//do more work on this damn thing.
import java.io.*;
public class prog {
public static void main(String[] args) throws FileNotFoundException {
String test = ">++++++++[-<+++++++++>]<.>>+>-[+]++>++>+++[>[->+++<<+++>]<<]>-----.>->+++..+++.>-.<<+[>[+>+]>>]<--------------.>>.+++.------.--------.>+.>+.";
BufferedReader br = new BufferedReader(new StringReader(test));
String code = fileIO.readAllText(br).replaceAll("[^\\[\\]+\\-,.<>]", "");
BFState b = new BFState(code);
b.Run();
}
}
class fileIO {
public static String readAllText(BufferedReader br) {
String s = __readAllText(br);
return s.substring(0, s.length() - 1);
}
private static String __readAllText(BufferedReader br) {
String s;
try {
s = br.readLine();
} catch (IOException e) {
return null;
}
try {
cflow.If(s != null);
} catch (IfCondTrue ifCondTrue) {
return s + "\n" + __readAllText(br);
} catch (IfCondFalse ifCondFalse) {
return "";
}
return null; //unreachable
}
}
class BFState {
byte[] mem;
int mempos;
String code;
int codepos;
public BFState(String code) {
mem = new byte[30000];
mempos = 0;
this.code = code;
codepos = 0;
}
public void Run() {
try {
__Run();
} catch (RunEscapeHelper runEscapeHelper) { }
}
private void __Run() throws RunEscapeHelper {
try {
cflow.IfTrue(code.charAt(codepos) == '>');
} catch (IfCondTrue ifCondTrue1) {
this.R();
}
try {
cflow.IfTrue(code.charAt(codepos) == '<');
} catch (IfCondTrue ifCondTrue1) {
this.L();
}
try {
cflow.IfTrue(code.charAt(codepos) == '+');
} catch (IfCondTrue ifCondTrue1) {
this.INC();
}
try {
cflow.IfTrue(code.charAt(codepos) == '-');
} catch (IfCondTrue ifCondTrue1) {
this.DEC();
}
try {
cflow.IfTrue(code.charAt(codepos) == '[');
} catch (IfCondTrue ifCondTrue1) {
this.LBRACE();
}
try {
cflow.IfTrue(code.charAt(codepos) == ']');
} catch (IfCondTrue ifCondTrue1) {
this.RBRACE();
}
try {
cflow.IfTrue(code.charAt(codepos) == '.');
} catch (IfCondTrue ifCondTrue1) {
this.OUTPUT();
}
try {
cflow.IfTrue(code.charAt(codepos) == ',');
} catch (IfCondTrue ifCondTrue1) {
this.INPUT();
}
codepos++;
try {
cflow.IfTrue(codepos < code.length());
} catch (IfCondTrue ifCondTrue) {
__Run(); //as long as we're within the boundaries, keep running
}
throw new RunEscapeHelper();
}
class RunEscapeHelper extends ControlFlowException {}
//exception type to mark end of recursion in __Run
public void L() {
try {
cflow.IfTrue(--mempos < 0);
} catch (IfCondTrue ifCondTrue) {
mempos = mem.length - 1;
}
}
public void R() {
try {
cflow.IfTrue(++mempos >= mem.length);
} catch (IfCondTrue ifCondTrue) {
mempos = 0;
}
}
public void INC() {
try {
cflow.If(mem[mempos] == 255);
} catch (IfCondTrue ifCondTrue) {
mem[mempos] = (byte)0;
} catch (IfCondFalse ifCondFalse) {
mem[mempos]++;
}
}
public void DEC() {
try {
cflow.If(mem[mempos] == 0);
} catch (IfCondTrue ifCondTrue) {
mem[mempos] = (byte)255;
} catch (IfCondFalse ifCondFalse) {
mem[mempos]--;
}
}
public void OUTPUT() {
System.out.print((char)(mem[mempos] % 256));
}
public void INPUT() {
try {
mem[mempos] = (byte)System.in.read();
} catch (IOException e) {
//do nothin lol
System.err.println("COULD NOT READ IN!");
System.exit(1);
}
}
public void LBRACE() {
try {
cflow.IfTrue(mem[mempos] == 0);
} catch (IfCondTrue ifCondTrue) {
try {
SkipPastBraceL(0);
} catch (BraceLeaveHelper braceLeaveHelper) {}
}
}
public void RBRACE() {
try {
cflow.IfTrue(mem[mempos] != 0);
} catch (IfCondTrue ifCondTrue) {
try {
SkipPastBraceR(0);
} catch (BraceLeaveHelper braceLeaveHelper) {}
}
}
private void SkipPastBraceL(int depth) throws BraceLeaveHelper {
codepos++;
try {
cflow.IfTrue(code.charAt(codepos) == '[');
} catch (IfCondTrue ifCondTrue) {
SkipPastBraceL(depth + 1);
}
try {
cflow.IfTrue(code.charAt(codepos) == ']');
} catch (IfCondTrue ifCondTrue) {
depth--;
try {
cflow.IfTrue(depth == -1);
} catch (IfCondTrue ifCondTrue1) {
throw new BraceLeaveHelper();
}
}
SkipPastBraceL(depth);
}
private void SkipPastBraceR(int depth) throws BraceLeaveHelper {
codepos--;
try {
cflow.IfTrue(code.charAt(codepos) == ']');
} catch (IfCondTrue ifCondTrue) {
SkipPastBraceR(depth + 1);
}
try {
cflow.IfTrue(code.charAt(codepos) == '[');
} catch (IfCondTrue ifCondTrue) {
depth--;
try {
cflow.IfTrue(depth == -1);
} catch (IfCondTrue ifCondTrue1) {
throw new BraceLeaveHelper();
}
}
SkipPastBraceR(depth);
}
private class BraceLeaveHelper extends ControlFlowException {}
//exception type to mark leaving brace recursion
}
class cflow<T> {
static void If(boolean condition) throws IfCondTrue, IfCondFalse {
try {
cflow.SideEffect(1 / Boolean.compare(condition, false)); //this is stupid
} catch (ArithmeticException e) {
throw new IfCondFalse();
}
throw new IfCondTrue();
}
static void IfTrue(boolean condition) throws IfCondTrue {
try {
cflow.SideEffect(1 / Boolean.compare(condition, false));
} catch (ArithmeticException e) {
return;
}
throw new IfCondTrue();
}
static <T> void SideEffect(T a) {} //does nothing - just hope the optimizer leaves it in
}
class IfCondTrue extends ControlFlowException { }
class IfCondFalse extends ControlFlowException { }
class ControlFlowException extends Exception {
@Override
public synchronized Throwable fillInStackTrace() {
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment