Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: BrainF*** interpreter

  1. #1
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default BrainF*** interpreter

    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;
    		}
    	}
    }


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Thumbs up Re: BrainF*** interpreter

    Thats brilliant helloworld!! I'm going to give it a go now

    We should start a new software forum where we can offer these types of programs.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.