Just for fun, I decided to write a BrainF*** interpreter in Java (sorry for the lack of comments, I was feeling pretty lazy)
By default, it will look for the file "test.b", but you can pass it a command-line argument and it will open that file instead.
For more info about BrainF***: Wikipedia: BrainF***
import java.io.*; import java.util.ArrayList; /** * @author helloworld922 * */ public class BrainF { public static void main(String[] args) { try { BufferedReader reader; if (args.length == 0) { reader = new BufferedReader(new FileReader("test.b")); } else { reader = new BufferedReader(new FileReader(args[0])); } ArrayList<Object> commands = BrainF.parse(reader, false); BrainF.execute(commands, new Node(), false); } catch (IOException e) { e.printStackTrace(); } } public static Node execute(ArrayList<?> commands, Node currentNode, boolean inLoop) throws IOException { if (inLoop) { while (currentNode.val != 0) { currentNode = BrainF.execute(commands, currentNode, false); } } else { for (int i = 0; i < commands.size(); i++) { if (commands.get(i) instanceof ArrayList<?>) { currentNode = BrainF.execute((ArrayList<?>) commands.get(i), currentNode, true); } else if (commands.get(i).equals(">")) { if (currentNode.next == null) { currentNode.next = new Node(); currentNode.next.prev = currentNode; } currentNode = currentNode.next; } else if (commands.get(i).equals("<")) { if (currentNode.prev == null) { currentNode.prev = new Node(); currentNode.prev.next = currentNode; } currentNode = currentNode.prev; } else if (commands.get(i).equals("+")) { currentNode.val++; if (currentNode.val > 0xFF) { currentNode.val = 0; } } else if (commands.get(i).equals("-")) { currentNode.val--; if (currentNode.val < 0 || currentNode.val > 0xFF) { currentNode.val = 0xFF; } } else if (commands.get(i).equals(".")) { System.out.print((char) currentNode.val); } else if (commands.get(i).equals(",")) { currentNode.val = System.in.read(); } else { System.err.print(commands.get(i)); } } } return currentNode; } public static ArrayList<Object> parse(BufferedReader reader, boolean inLoop) throws IOException { ArrayList<Object> commands = new ArrayList<Object>(); int read = 0; while (read != -1) { read = reader.read(); if (read == '>' || read == '<' || read == '+' || read == '-' || read == '.' || read == ',') { commands.add("" + (char) read); } else if (read == '[') { commands.add(BrainF.parse(reader, true)); } else if (read == ']') { if (inLoop) { return commands; } else { System.err.println("Error! Unmatching parenthesis!"); throw new IOException(); } } } if (inLoop) { System.err.println("No closing parenthesis found!"); throw new IOException(); } return commands; } private static class Node { public Node prev, next; public int val; public Node() { this.prev = null; this.next = null; this.val = 0; } @Override public String toString() { Node temp = this; String str = ""; while (temp.prev != null) { temp = temp.prev; } do { str += temp.val + " "; temp = temp.next; } while (temp != null); return str; } } }