Skip to content

Instantly share code, notes, and snippets.

@jammy-dodgers
Last active November 28, 2023 03:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jammy-dodgers/407a884c72ded25c823a007343ad7ca4 to your computer and use it in GitHub Desktop.
Save jammy-dodgers/407a884c72ded25c823a007343ad7ca4 to your computer and use it in GitHub Desktop.
brainfuck in Javagony ( https://esolangs.org/wiki/Javagony )
//https://esolangs.org/wiki/Javagony
// https://tio.run/##7Vlbc@I2FH7nV2iZ6cQUbEg724eGuE2yZJpOutlh2SegGWMLUGIkjySHpTv89vRINlg2ENiYTF/qYQDrcs53LjoX@8F78uyH4PH5udmcShmJX5tNLFjo0YlwGJ805@SRNP@EVRNGF5Vm8xL7Xiwwmk8ZohgHAnHshchnVHIWonHI5r9VYF1vijkWKOJs5I3CBRJshvVdiGcCzYmcIiJh3SiWyJNITgksZoRKRE4CxD0J@1FAMMx4FNYFDM0YB76MPyJGkw2BN9P/6MSpVMgsYlyiB8DqEOb8eFapRPEoJD7yQ08I9JdHKPpWQXCl40J6En6eGAnQDGatz5IDrf4QeXwiakCZs7lA1yTEH5m8ZjENOl99HEnCVpTUlexCEguJzlHVradX326v/tbdYdtx3bpr9@tDuNOfet/t2@q33U5WtIeurS7H1cOOo75c24F5WApbhi6ssnMXUNXL0rtsuK4@1bM1yst4PAaTBF3sBaDaEQesFM8L45YaSgRKB5RYtdpZUVqfBRgojEE3N3cO@EBwEYY9/FVaI16D@yj0fAxDVrX/92DQHwyG9cHAbjhtd1htoGrVoHh5/RkMgdFohSi5txQLY9nI6cbUSgeWlQp8EsMmGLaaNgVrwtvQQ23TlAKQ3N8XhMqQcCxjTpFwRDwSeofVasBtiOlETq0astHpGqcGxcmTkjCPKs/hIFwZBskXxry6FOgR16a4JRRbBuAl8j3pT5F1c5c5MK4VCKRi0TgMja0vcPTVaXduxpZA7871vq08x1eMBj0eY0TWf3fwFqiOqgNahZ8X9F8gfe2FYkVb/99BvFrdJpY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F4qQzI4G4h2tbGGpdqqc1fKa0vOHaOtNFPRKfSQPa0uJrzwnCYUZ6B7yVE7WNR8WadU3gS7sIRYSTZlyT2h4GaCakX/Aw5WTwkIioZvMPNPQ2ctHABCZOxI/3Lsl3bN1frW5@H7qd/VYlYOkuZcjC@qDuiiIEqCeUpx63K3eo4Q3VD@M7anDlyu9Nl4OLCvL5@d/AQ
//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 Main {
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) { }
catch (StackOverflowError soe) {
Run();
}
}
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