<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Codng &#187; Compilers</title>
	<atom:link href="http://codng.com/category/compilers/feed" rel="self" type="application/rss+xml" />
	<link>http://codng.com</link>
	<description>Nerd is the new cool.</description>
	<lastBuildDate>Wed, 02 Dec 2009 21:39:46 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Naive Brainf**k to Java compiler</title>
		<link>http://codng.com/naive-brainfk-to-java-compiler</link>
		<comments>http://codng.com/naive-brainfk-to-java-compiler#comments</comments>
		<pubDate>Wed, 02 Dec 2009 20:02:03 +0000</pubDate>
		<dc:creator>juancn</dc:creator>
				<category><![CDATA[Compilers]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Miscellaneous]]></category>

		<guid isPermaLink="false">http://codng.com/?p=19</guid>
		<description><![CDATA[Some time ago the brainf**k language caught my eye (I still don&#8217;t feel comfortable spelling it right).
It&#8217;s a turing tarpit, but one not very complicated at that (for something more esoteric see Malbolge or Whitespace).
The language is very simple, it has eight instructions. So I had to write a compiler for it. It turns out [...]]]></description>
			<content:encoded><![CDATA[<p>Some time ago the <a href="http://en.wikipedia.org/wiki/Brainfuck">brainf**k</a> language caught my eye (I still don&#8217;t feel comfortable spelling it right).</p>
<p>It&#8217;s a <a href="http://en.wikipedia.org/wiki/Turing_tarpit">turing tarpit</a>, but one not very complicated at that (for something more esoteric see <a href="http://en.wikipedia.org/wiki/Malbolge">Malbolge</a> or <a href="http://en.wikipedia.org/wiki/Whitespace_(programming_language)">Whitespace</a>).</p>
<p>The language is very simple, it has eight instructions. So I had to write a compiler for it. It turns out to be quite easy to do:</p>
<pre class="brush: java;">
import static java.lang.System.out;

public class BrainFk
{
    public static void main(String[] args) {
        out.println(&quot;public class &quot; + args[0] + &quot;{&quot;);
        out.println(&quot;public static void main(String args[]) throws Throwable {&quot;);
        out.println(&quot;int[] memory = new int[30000];&quot;);
        out.println(&quot;int data = 0;&quot;);
        final String code = args[1];
        for(int i = 0; i &lt; code.length(); i++) {
            char c = code.charAt(i);
            switch(c) {
                case '&gt;':
                    out.println(&quot;++data;&quot;);
                    break;
                case '&lt;':
                    out.println(&quot;--data;&quot;);
                    break;
                case '+':
                    out.println(&quot;++memory[data];&quot;);
                    break;
                case '-':
                    out.println(&quot;--memory[data];&quot;);
                    break;
                case '.':
                    out.println(&quot;System.out.print((char)(0xFF &amp; memory[data]));&quot;);
                    break;
                case ',':
                    out.println(&quot;memory[data] = System.in.read();&quot;);
                    break;
                case '[':
                    out.println(&quot;while(memory[data] != 0) {&quot;);
                    break;
                case ']':
                    out.println(&quot;}&quot;);
                    break;
            }
        }
        out.println(&quot;}}&quot;);
    }
}
</pre>
<p>You can use it to compile the hello world sample from the Wikipedia page:</p>
<pre class="brush: plain; light: true;">
~&gt;java BrainFk Hello &quot;++++++++++[&gt;+++++++&gt;++++++++++&gt;+++&gt;+&lt;&lt;&lt;&lt;-]&gt;++.&gt;+.+++++++..+++.&gt;++.&lt;&lt;+++++++++++++++.&gt;.+++.------.--------.&gt;+.&gt;.&quot; &gt; Hello.java
~&gt;javac Hello.java
~&gt;java Hello
Hello World!
</pre>
<p>Sometime I&#8217;ll have to post the Forth interpreter in Java I wrote (I know some might consider sacrilege to use both languages in the same sentence, but you can&#8217;t please everyone!).</p>
]]></content:encoded>
			<wfw:commentRss>http://codng.com/naive-brainfk-to-java-compiler/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
