123

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <div class="container">
    <div class="row">
        <header>
            <h1>Interactive Brainfuck Interpreter</h1>
        </header>
    </div>

    <div class="row">
        <div class="one-half column">
            <textarea id="input" placeholder="Enter you code here..." autofocus></textarea>
        </div>
        <div class="one-half column">
            <textarea id="output" placeholder="Output of your script and error messages will be there..." readonly></textarea>
        </div>
    </div>
    <div class="row">
        <div class="spoiler u-full-width">
            <input type="checkbox" id="spoiler-commands">
            <div class="row">
                <label for="spoiler-commands" class="button button-primary four columns offset-by-four">View help</label>
            </div>
            <div class="content">
                <div class="row">
                    <p class="u-full-width">Brainfuck is an esoteric programming language. It consists of only eight simple commands and an instruction pointer. It was created in 1993 by Urban Müller. This interpreter allows You to write scripts in Brainfuck and run this scripts in any modern browser (Google Chrome recommended)</p>
                </div>
                <div class="row">
                    <h5>Language commands</h5>
                </div>
                <div class="row">
                    <ol class="u-full-width">
                        <li><code>&gt;</code> increment the data pointer</li>
                        <li><code>&lt;</code> decrement the data pointer</li>
                        <li><code>+</code> increment the byte at the data pointer</li>
                        <li><code>-</code> decrement the byte at the data pointer</li>
                        <li><code>.</code> output the byte at the data pointer</li>
                        <li><code>,</code> storing one byte of input at the data pointer (not supported in this interpreter)</li>
                        <li><code>[</code> if the byte at the data pointer is zero, then jump it forward to the command after the matching ] command</li>
                        <li><code>]</code> if the byte at the data pointer is nonzero, then jump it back to the command after the matching [ command</li>
                    </ol>
                </div>
                <div class="row">
                    <h5>For example</h5>
                </div>
                <div class="row">
                    <ul class="u-full-width">
                        <li><code>+++++++</code> sets value of #0 to 7 (on start all bytes setted to zeros)</li>
                        <li><code>&gt;&gt;&gt;++</code> moves right to 3 bytes and then increase #3 byte to 2</li>
                        <li><code>&lt;&lt;&lt;.</code> moves back to #0 byte and output it"s value (ascii)</li>
                    </ul>
                </div>
                <h5>"Hello, World!" program</h5>
                <div class="row">
                    <p class="u-full-width">(source from http://en.wikipedia.org/wiki/Brainfuck):</p>
                    <pre class="u-full-width">
                                <code>
++++++++
[
&gt;++++
[
    &gt;++
    &gt;+++
    &gt;+++
    &gt;+
    &lt;&lt;&lt;&lt;-
]
&gt;+
&gt;+
&gt;-
&gt;&gt;+
[&lt;]
&lt;-
]
&gt;&gt;.
&gt;---.+++++++..+++.
&gt;&gt;.
&lt;-.
&lt;.+++.------.--------.
&gt;&gt;+.
&gt;++.
                                </code>
                            </pre>
                </div>
                <div class="row">
                    <h5>Have fun!</h5>
                </div>
            </div>
        </div>
    </div>
    <hr>
    <footer class="row">
        <div class="u-full-width">Return to <a href="https://sfi0zy.github.io/">https://sfi0zy.github.io/</a></div>
    </footer>
</div>
            
          
!
            
              header {
    text-align: center;
    padding: 50px 0;
}

.spoiler .content {
    padding-top: 50px;
}

.pre {
    padding-top: 0;
}

textarea {
    display: inline-block;
    height: 400px;
    outline: none;
    width: 100%;
}

.spoiler label {
    margin-bottom: -15px;
}

.spoiler pre {
    margin-top: 20px;
}

.spoiler .content {
    margin-top: 0;
    max-height: 0;
    overflow: hidden;
    transition: max-height 0.8s ease;
    will-change: max-height;
}

.spoiler input {
    display: none;
}

.spoiler input[type="checkbox"]:checked ~ .content {
    display: block;
    max-height: 2000px;
}

footer {
    text-align: center;
    padding: 20px 0;
}
            
          
!
            
              var BrainfuckInterpreter = (function() {
    'use strict';

    function BrainfuckInterpreter() {
        this.source = '';
        this.output = '';

        this.errorOutput = [];
    }


    BrainfuckInterpreter.MAX_OPERATIONS = 10000000;


    BrainfuckInterpreter.errors = {
        INVAL_ARG:  'Invalid argument',
        INVAL_CHAR: 'Syntax Error: Invalid char',
        UNEXP_CHAR: 'Syntax Error: Unexpected char',
        EMPTY_CODE: 'Code is empty'
    };

    
    BrainfuckInterpreter.prototype.setCode = function(code) {
        if (typeof code === 'string') {
            this.source = code;
        } else {
            throw new Error(BrainfuckInterpreter.errors.INVAL_ARG);
        }
    };

    
    BrainfuckInterpreter.prototype.getOutput = function() {
        return this.output;
    };

    
    BrainfuckInterpreter.prototype.getErrorOutput = function() {
        return this.errorOutput;
    };

    
    BrainfuckInterpreter.prototype.run = function() {
        var source, sourceLength, buffer, ptr, output, loopDepth, operations,
            maxOperations, i;

        source = this.source;
        sourceLength = source.length;
        maxOperations = BrainfuckInterpreter.MAX_OPERATIONS;        

        this.output = '';
        this.errorOutput = [];

        if (source === '') {
            return;
            //throw new Error(BrainfuckInterpreter.errors.EMPTY_CODE);
        }

        this.checkSource();

        if (this.errorOutput.length === 0) {
            output = this.output;
            buffer = new Array(30000);

            for (i = 0; i < 30000; ++i) {
                buffer[i] = 0;
            }

            ptr = 0;
            operations = 0;

            for (i = 0; i < sourceLength; ++i) {
                operations++;

                if (operations > maxOperations) {
                    this.errorOutput.push('It seems like your script not responding');
                    return;
                }

                switch (source[i]) {
                    case '>': {
                        if (++ptr === 30000) {
                            ptr = 0;
                        }

                        break;
                    }

                    case '<': {
                        if (--ptr === -1) {
                            ptr = 29999;
                        }

                        break;
                    }

                    case '.': {
                        this.output += String.fromCharCode(buffer[ptr]);
                        break;
                    }

                    case '+': {
                        buffer[ptr]++;
                        break;
                    }

                    case '-': {
                        buffer[ptr]--;
                        break;
                    }

                    case '[': {
                        if (buffer[ptr] === 0) {
                            loopDepth = 1;

                            while (loopDepth > 0) {
                                i++;
                                operations++;

                                if (operations > maxOperations) {
                                    this.errorOutput.push('It seems like your script has infinite loop...');
                                    return;
                                }

                                switch (source[i]) {
                                    case '[':
                                        loopDepth++;
                                        break;
                                    case ']':
                                        loopDepth--;
                                        break;
                                    default:
                                        break;
                                }
                            }
                        }

                        break;
                    }

                    case ']': {
                        loopDepth = 1;

                        while (loopDepth > 0) {
                            i--;
                            operations++;

                            if (operations > maxOperations) {
                                this.errorOutput.push('It seems like your script has infinite loop...');
                                return;
                            }                                                                                                                                                                               

                            switch (source[i]) {
                                case '[':
                                    loopDepth--;
                                    break;
                                case ']':
                                    loopDepth++;
                                    break;
                                default:
                                    break;
                            }
                        }

                        i--;

                        break;
                    }

                    case ',': {
                        break;
                    }

                    default:
                        break;
                }
            };
        }
    };


    BrainfuckInterpreter.prototype.checkSource = function() {
        var source, sourceLength, validLetters, loopDepthCnt, i, currentChar;

        source = this.source;

        if (typeof source !== 'string') {
            throw new Error(BrainfuckInterpreter.errors.INVAL_ARG);
        }

        if (source === '') {
            throw new Error(BrainfuckInterpreter.errors.EMPTY_CODE);
        }

        sourceLength = source.length;
        validLetters = '<>+-[]., \t\r\b\n';
        loopDepthCnt = 0;        

        for (i = 0; i < sourceLength; ++i) {
            currentChar = source[i];

            if (validLetters.indexOf(currentChar) === -1) {
                this.errorOutput.push(i + ': ' + BrainfuckInterpreter.errors.INVAL_CHAR + ' ' + currentChar);
            }

            switch (currentChar) {
                case '[':
                    loopDepthCnt++;
                    break;
                case ']':
                    loopDepthCnt--;
                default:
                    break;
            }

            if (loopDepthCnt < 0) {
                this.errorOutput.push(i + ': ' + BrainfuckInterpreter.errors.UNEXP_CHAR +
                    ' before ' + currentChar);
            }
        }

        if (loopDepthCnt > 0) {
            this.errorOutput.push(source.length + ': ' + BrainfuckInterpreter.errors.UNEXP_CHAR + ' \']\'');
        } else if (loopDepthCnt < 0) {
            this.errorOutput.push(source.length + ': ' + BrainfuckInterpreter.errors.UNEXP_CHAR + ' \'[\'');
        }

    };

    return BrainfuckInterpreter;
})();


window.onload = function() {
    try {
        var interpreter, inputArea, outputArea,
            input, output, outputErr, i;

        interpreter = new BrainfuckInterpreter();

        inputArea = document.getElementById('input');
        outputArea = document.getElementById('output');

        setInterval(function() {
            if (input === inputArea.value) {
                return;
            }

            input = inputArea.value;
            outputArea.value = '';
            output = '';

            interpreter.setCode(input);
            interpreter.run();

            output = interpreter.getOutput();
            outputErr = interpreter.getErrorOutput();

            for (i = 0; i < outputErr.length; ++i) {
                outputArea.value += (outputErr[i] + '\n');
            }

            outputArea.value += output;

        }, 1);
    } catch (e) {
        //console.log(e);
    }
};
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console