Have you heard of Brainf***? It's an esoteric programming language, with only 8 commands. It's virtually impossible to build anything useful with this language (hence the name), it's more of thing to amuse software engineers.

It may be a nightmare to write Brainf*** code, but the interpreter is remarkable simple. There are interpreters available for most languages, so I couldn't resist implementing one in Moya Code. It was actually a useful test of a few control structures. Here it is, in all it's dubious glory...

<moya xmlns:let="http://moyaproject.com/let">

    <macro docname="bf">
        <let memory="[0]*30000" cp="0" ip="0" in="0"/>
        <list dst="output"/>

        <repeat>

            <let c="program[ip]"/>
            <break if="missing:c"/>

            <switch on="c">
                <case>&gt;</case>
                <let cp="(cp + 1) % len:memory"/>

                <case>&lt;</case>
                <let cp="(cp - 1) % len:memory"/>

                <case>+</case>
                <set-item src="memory" index="cp" value="memory[cp] + 1" />

                <case>-</case>
                <set-item src="memory" index="cp" value="memory[cp] - 1" />

                <case>.</case>
                <append src="output" value="chr:memory[cp]" />

                <case>,</case>
                <let v="input[in] or 0" in="in+1"/>
                <set-item src="memory" index="cp" value="v"/>

                <case>[</case>
                <if test="memory[cp] == 0">
                    <let braces="1"/>
                    <while test="braces">
                        <let ip="ip + 1" c="program[ip]"/>
                        <switch on="c">
                            <case>[</case><let braces="braces+1"/>
                            <case>]</case><let braces="braces-1"/>
                        </switch>
                    </while>
                </if>

                <case>]</case>
                <let braces="1"/>
                <while test="braces">
                    <let ip="ip - 1" c="program[ip]"/>
                    <switch on="c">
                        <case>[</case><let braces="braces-1"/>
                        <case>]</case><let braces="braces+1"/>
                    </switch>
                </while>
                <dec dst="ip"/>
            </switch>

            <inc dst="ip"/>

        </repeat>
        <return value="join:output"/>
    </macro>

    <macro docname="main">
        <!-- BF programs are tricky to quote in XML due to their use of > and < -->
        <str dst="example"><![CDATA[++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.]]></str>
        <if test="not program">
            <echo>A B*******k interpreter (http://en.wikipedia.org/wiki/Brainfuck)</echo>
            <echo>Run with --let program="BF program"</echo>
            <echo>e.g.</echo>
            <echo>  moya run bf.xml --let program="${example}"</echo>
            <echo>Set input with --let input="my input"</echo>
            <exit/>
        </if>

        <call macro="bf" let:program="program" let:input="input" dst="output"/>
        <echo>${output}</echo>
    </macro>

</moya>

Recent Posts

Moya 0.6.0 Released

I'm happy to announce the release of Moya version 0.6.0. This version contains a number of new […]

Creating a Wiki with Moya - Screencast

I'm starting a series of screencasts on how do build a web application in Moya. In part 1, I show […]

Moya 0.5.14 Released

I'm pleased to announce the release of Moya 0.5.14. Moya is a web application platform written in […]

Moya Context Python Interface

The Context class is an interesting data-structure Moya uses to implement much of the features of […]

Moya 0.5.12 released!

Moya 0.5.12 was recently released. Moya is a language and web application server built in Python. […]

Moya discussion group

I've just create a Google discussion group for Moya. Sign up if you are interested in this project. […]

Moya Package Index

Just live is Moya Package Index, a site where you can find and download packages for Moya. If […]

Encrypted Notes with Moya

Encrypted Notes is an application written in Moya, and only the second Moya website online (the […]

A BrainF*** interpreter written in Moya

Have you heard of Brainf***? It's an esoteric programming language, with only 8 commands. It's […]

Moya is a new web development platform written in Python

In this post I'm going to try and pitch Moya to you, Dragon's Den style. Moya has been my hobby […]

Markdown in Posts

This is actually my second post, I deleted the first post because I changed the default markup from […]