Skip to content

Instantly share code, notes, and snippets.

@unnikked
Last active September 7, 2022 00:20
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save unnikked/cfad836abd9e4619a1b1 to your computer and use it in GitHub Desktop.
Save unnikked/cfad836abd9e4619a1b1 to your computer and use it in GitHub Desktop.
Tiny Brainfuck Interpreter Written in Java
import java.util.*;
public class Brainfuck {
private Scanner sc = new Scanner(System.in);
private final int LENGTH = 65535;
private byte[] mem = new byte[LENGTH];
private int dataPointer;
public void interpret(String code) {
int l = 0;
for(int i = 0; i < code.length(); i++) {
if(code.charAt(i) == '>') {
dataPointer = (dataPointer == LENGTH-1) ? 0 : dataPointer + 1;
} else if(code.charAt(i) == '<') {
dataPointer = (dataPointer == 0) ? LENGTH-1 : dataPointer - 1;
} else if(code.charAt(i) == '+') {
mem[dataPointer]++;
} else if(code.charAt(i) == '-') {
mem[dataPointer]--;
} else if(code.charAt(i) == '.') {
System.out.print((char) mem[dataPointer]);
} else if(code.charAt(i) == ',') {
mem[dataPointer] = (byte) sc.next().charAt(0);
} else if(code.charAt(i) == '[') {
if(mem[dataPointer] == 0) {
i++;
while(l > 0 || code.charAt(i) != ']') {
if(code.charAt(i) == '[') l++;
if(code.charAt(i) == ']') l--;
i++;
}
}
} else if(code.charAt(i) == ']') {
if(mem[dataPointer] != 0) {
i--;
while(l > 0 || code.charAt(i) != '[') {
if(code.charAt(i) == ']') l++;
if(code.charAt(i) == '[') l--;
i--;
}
i--;
}
}
}
}
public static void main(String[] args) {
new Brainfuck().interpret(args[0]);
}
}
public class BrainfuckToJava {
private StringBuilder source;
private int ident;
public BrainfuckToJava(String code) {
source = new StringBuilder();
source.append("import java.util.Scanner;\n");
source.append("public class BFConverted {\n");
source.append("\tprivate static int ptr;\n");
source.append("\tprivate static byte[] mem = new byte[65535];\n");
source.append("\tprivate static Scanner in = new Scanner(System.in);\n");
source.append("\tpublic static void main(String[] args) {\n");
convert(code, source);
source.append("\t}\n");
source.append("}\n");
System.out.println(source.toString());
}
private void convert(String code, StringBuilder source) {
for(int i = 0; i < code.length(); i++) {
for(int t = 0; t < ident; t++) source.append('\t');
switch(code.charAt(i)) {
case '>': source.append("\t\tptr++;\n"); break;
case '<': source.append("\t\tptr--;\n"); break;
case '+': source.append("\t\tmem[ptr]++;\n"); break;
case '-': source.append("\t\tmem[ptr]--;\n"); break;
case '.': source.append("\t\tSystem.out.print((char) mem[ptr]);\n"); break;
case ',': source.append("\t\tmem[ptr] = (byte) in.next().charAt(0);\n"); break;
case '[': source.append("\t\twhile(mem[ptr] != 0) {\n"); ident++; break;
case ']': source.append("\t\t}\n"); break;
default: continue;
}
if(i+1 < code.length() && code.charAt(i+1) == ']') ident--;
}
}
public static void main(String[] args) {
new BrainfuckToJava(args[0]);
}
}
@evjeny
Copy link

evjeny commented Nov 18, 2016

Helpful code 👍

@Calcaware
Copy link

Love it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment