Making Future Posts Runnable Online with Stack Snippets

135

42

Stack Snippets were recently added to PPCG! Reminiscent of JSFiddle, Stack Snippets allow HTML, CSS, and JavaScript to be run directly in posts!

Here is a very simple Stack Snippet:

alert('This is JavaScript')
h3 { color: red } /* This is CSS */
<h3>This is HTML</h3>

This feature of Stack Exchange would be very useful for us if languages besides JavaScript were supported. (The answers to challenges could be tested on the spot, example inputs could be generated dynamically, etc.) This is where you come in.

Challenge

The goal of this challenge is to write an interpreter for some programming language using Stack Snippets and JavaScript. The point is to make something that can be easily copied and used in future PPCG questions and answers.

More or less, you need to create a Stack Snippet that has a "run" button and two textboxes, one for code and one for input. Clicking the run button will execute the code (written in the language you're interpreting) on the input and display the result (probably in another textbox). The snippet should be something akin to cjam.aditsu.net or the sample answer.

For most languages it would make sense for the input and output to represent stdin and sdout respectively, and you might have another input box for the command line. But not all languages have have such traditional I/O mechanisms. HQ9+, for example, doesn't even have input, making a textbox for it pointless. So feel free to take some liberties, design around the language, not this spec. The main requirement is that your language be "runnable" in a Stack Snippet in the accepted sense of the term.

Notes

  • Implementing every single feature of your language, though ideal, is not required. Some things like reading and writing files or importing libraries may be unwieldy or impossible. Focus on making an interpreter that maximizes utility for use on this site.
  • Posting a "language X to JavaScript" interpreter that you didn't write is ok (with attribution).
  • Stack Exchange limits answers to 30,000 characters, so plan accordingly if your interpreter is likely to be long.
  • It is best that you make a version of your interpreter as easy as possible to include in future posts. For example, in the sample answer, the raw Markdown for the entire snippet is provided, with obvious places to put code and input.

Although this question is intended to be more a compendium of interpreters than a proper challenge, it is still a , so the highest voted answer wins.

List of Current Interpreters

(sorted alphabetically by language name)

  1. Beam
  2. Befunge-93
  3. Brainfuck
  4. Brainfuck
  5. CHIQRSX9+
  6. Deadfish
  7. Deadfish (only runs preset code)
  8. Fourier
  9. FRACTRAN
  10. Hello++
  11. HQ9+
  12. Insomnia
  13. Japt
  14. JavaScript (sample answer)
  15. JavaScript ES2015
  16. Marbelous
  17. Neoscript
  18. oOo CODE
  19. Ouroboros
  20. Prelude
  21. Python 2
  22. STATA
  23. TI-Basic
  24. Unary (translates to BF)

(This question might be better suited for Meta but it will be more visible here. People might actually produce very useful interpreters, I think they deserve the rep for it.)

Calvin's Hobbies

Posted 2014-10-21T02:41:05.133

Reputation: 84 000

I wonder if such snippet can send the source to an external server that would transpile it to JS then send it back... Would need to insert back the JS into the answer. Maybe doable... – PhiLho – 2015-02-04T12:35:05.787

Are we allowed to add languages written after the creation of this post? – Beta Decay – 2015-08-29T22:06:54.147

@BetaDecay Sure. Though I don't think I'll be changing the accepted answer. – Calvin's Hobbies – 2015-09-05T22:04:24.250

I'd like to see one for Retina, honestly. An online interpreter would be pretty useful. – mbomb007 – 2015-09-16T21:50:32.453

Also, if anyone wants to make a snippet for Ruby, there's already a client-side JS interpreter available.

– mbomb007 – 2015-09-16T21:59:48.320

1

Slightly off-topic but relevant, https://repl.it runs with their own client-side JavaScript interpreters for [Python3, Ruby, Scheme, Lua, Java, QBasic, Forth, APL, Go, C, C++, Bloop, Emoticon, LOLCODE, BrainFuck, Unlambda, PHP, and various JavaScript derivatives].

– TessellatingHeckler – 2015-10-08T18:24:28.027

2It wouldn't be very fun, but the more practical answer for harder languages like Python would probably be to simply outsource it. – Sp3000 – 2014-10-21T09:37:52.497

7I have no idea how to run these snippets using the stack exchange app. – Jerry Jeremiah – 2014-10-21T10:10:14.127

1

I hope someone tackles C with this challenge. Here's a head start: http://stackoverflow.com/questions/6142193/c-interpreter-written-in-javascript

– Adam Davis – 2014-10-21T15:19:28.547

4Food for thought... – James Williams – 2014-10-21T17:18:31.980

1@Sp3000: Or look for libraries... like below. – ArtOfCode – 2014-10-21T21:37:05.197

I tried doing for CJam, but it gave various CSP insecure errors :( . – Optimizer – 2014-10-21T23:02:15.310

Would be awesome if someone could do OCaml. There's already http://try.ocamlpro.com/ (source at https://github.com/OCamlPro/tryocaml), and js_of_ocaml, an OCaml to javascript compiler.

– ReyCharles – 2014-10-22T11:39:16.373

What is the bounty winning criteria ? "Larger array of languages" - Do you mean that the same person post answers for more languages ? – Optimizer – 2014-10-23T19:55:48.497

@Optimizer It doesn't matter who posts it, I just want to see more languages than the 4 we currently have. The winner will be chosen at my discretion. It is like to be the person who obviously put the most effort in. – Calvin's Hobbies – 2014-10-23T20:02:47.380

I've been working on writing a Befunge-93 interpreter today. It's close to done, so I'm hoping to post it soon. I noticed my old interpreter was flawed, so I decided to start from scratch. – Ingo Bürk – 2014-10-25T15:54:29.983

Coming to think about it: aren't answers relying on external resources sort of defying the purpose here? They are in no way "future-proof". If they're okay I'll find a PHP proxy to fire a POST request to the Golfscript parser and parse it's output. :) – Ingo Bürk – 2014-10-27T18:56:54.537

1I've been wanting to do a self-contained Python 3 builtins-only snippet, but my (broken) parser already takes up 10k, packed... – Sp3000 – 2014-10-30T14:04:09.787

1@IngoBürk The external interpreters are kind of against the point of self-contentedness (and I'm more likely to give my bounty to a local interpreter) but they can still be quite useful. – Calvin's Hobbies – 2014-10-30T21:02:04.230

I am planning on doing a partial C++ to Python 3 after this semester is over, featuring the most common things (input/output, random numbers, loops, if/ternary if, ...). If someone will write a Python 3 interpreter, we can chain them together. – None – 2014-10-31T17:00:12.207

Answers

48

Python 2 (No STDIN)

Thanks to Skulpt, it has become very easy to write a Python interpreter.

function out(text) {
  var output = document.getElementById("output");
  output.innerHTML = output.innerHTML + text;
}

function builtinRead(x) {
  if(Sk.builtinFiles === undefined || Sk.builtinFiles["files"][x] === undefined)
    throw "File not found: '" + x + "'";
  return Sk.builtinFiles["files"][x];
}

function run() {
  var program = document.getElementById("python-in").value;
  var output = document.getElementById("output");
  
  output.innerHTML = "";
  
  Sk.canvas = "canvas";
  Sk.pre = "output";
  
  Sk.configure({
    output: out,
    read: builtinRead
  });
  
  try {
    Sk.importMainWithBody("<stdin>", false, program);
  }
  catch(e) {
    throw new Error(e.toString());
  }
}
#python-in {
  border-radius: 3px;
  background: rgb(250, 250, 250);
  width: 95%;
  height: 200px;
}

#output {
  border-radius: 3px;
  background: lightgray;
  width: 95%;
  height: 200px;
  overflow: auto;
}

#canvas {
  border: 1px solid gray;
  border-radius: 3px;
  height: 400px;
  width: 400px;
}
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="http://www.skulpt.org/static/skulpt.min.js"></script>
<script src="http://www.skulpt.org/static/skulpt-stdlib.js"></script>

Python code:<br/>
<textarea id="python-in" name="python-in" rows="10" cols="80"></textarea><br/>
<button id="run-code" onclick="run()">Run</button><br/>
<br/>

Output:<br/>
<pre id="output" name="output" rows="10" cols="10" disabled></pre><br/>

Canvas for turtles:<br/>
<canvas height="400" width="400" id="canvas">Your browser does not support HTML5 Canvas!</canvas>

No STDIN, but it's a pretty complete implementation of Python in JS. I would load the libraries from somewhere else but I can't find a host of them.

ArtOfCode

Posted 2014-10-21T02:41:05.133

Reputation: 976

Skulpt has since updated and moved a few files, thought you might want to know – Sp3000 – 2015-02-16T12:24:03.230

@Sp3000 It appears they haven't updated the docs on how to use it in a webpage though... I'm going to have to leave this alone until then – ArtOfCode – 2015-02-16T13:54:08.930

2I'm not sure if it's more funny or more cool to think of Marbelous running inside a python interpreter running inside javascript running inside a web browser – vijrox – 2015-06-30T20:19:17.367

1This doesn't work anymore, presumably because the links in the <script> tags are dead :( – oldmud0 – 2015-11-25T03:32:35.973

Can you link the docs? I can't find them for the life of me. >.< – Rɪᴋᴇʀ – 2016-03-17T00:27:46.547

Does this really work for turtle? I tried a few things and the canvas doesn't show any output. – mbomb007 – 2017-01-19T17:04:13.890

1Wow, from random import randrange even works. Nice! – Doorknob – 2014-10-22T00:18:00.717

20Now just load the Marbelous source into this interpreter and you got yourself a Marbelous interpreter! – Ingo Bürk – 2014-10-24T06:30:45.230

3It seems to be python2 since print 2 works. Worth mentioning, I guess. – ReyCharles – 2014-10-26T13:38:14.320

Yeah, skulpt hasn't built a python3 version yet – ArtOfCode – 2014-10-28T15:10:35.170

33

Befunge-93

Edit: I reworked the entire GUI now. I'm much happier with it. Some kind of breakpoint feature would be cool, but it's probably too much for this. Open Points I will probably revisit:

  • Allow an infinite board
  • Displaying the stack when stepping through the program would be neat

Since I just implemented this interpreter for this challenge it's not really extensively tested. However, I tried it with a variety of different programs now and it seems to work fine.

The latest version can be found at the latest revision of this fiddle.

function BefungeBoard(source, constraints) {
    constraints = constraints || {
        width: 80,
        height: 25
    };

    this.constraints = constraints;
    this.grid = source.split(/\r\n|[\n\v\f\r\x85\u2028\u2029]/).map(function (line) {
        return (line + String.repeat(' ', constraints.width - line.length)).split('');
    });
    for (var i = this.grid.length; i < constraints.height; i++) {
        this.grid[i] = String.repeat(' ', constraints.width).split('');
    }

    this.pointer = {
        x: 0,
        y: 0
    };

    this.direction = Direction.RIGHT;
}

BefungeBoard.prototype.nextPosition = function () {
    var vector = this.direction.toVector(),
        nextPosition = {
            x: this.pointer.x + vector[0],
            y: this.pointer.y + vector[1]
        };

    nextPosition.x = nextPosition.x < 0 ? this.constraints.width - 1 : nextPosition.x;
    nextPosition.y = nextPosition.y < 0 ? this.constraints.height - 1 : nextPosition.y;

    nextPosition.x = nextPosition.x >= this.constraints.width ? 0 : nextPosition.x;
    nextPosition.y = nextPosition.y >= this.constraints.height ? 0 : nextPosition.y;

    return nextPosition;
};

BefungeBoard.prototype.advance = function () {
    this.pointer = this.nextPosition();
    if (this.onAdvance) {
        this.onAdvance.call(null, this.pointer);
    }
};

BefungeBoard.prototype.currentToken = function () {
    return this.grid[this.pointer.y][this.pointer.x];
};

BefungeBoard.prototype.nextToken = function () {
    var nextPosition = this.nextPosition();
    return this.grid[nextPosition.y][nextPosition.x];
};

var Direction = (function () {
    var vectors = [
        [1, 0],
        [-1, 0],
        [0, -1],
        [0, 1]
    ];

    function Direction(value) {
        this.value = value;
    }

    Direction.prototype.toVector = function () {
        return vectors[this.value];
    };

    return {
        UP: new Direction(2),
        DOWN: new Direction(3),
        RIGHT: new Direction(0),
        LEFT: new Direction(1)
    };
})();

function BefungeStack() {
    this.stack = [];
}

BefungeStack.prototype.pushAscii = function (item) {
    this.pushNumber(item.charCodeAt());
};

BefungeStack.prototype.pushNumber = function (item) {
    if (isNaN(+item)) {
        throw new Error(typeof item + " | " + item + " is not a number");
    }

    this.stack.push(+item);
};

BefungeStack.prototype.popAscii = function () {
    return String.fromCharCode(this.popNumber());
};

BefungeStack.prototype.popNumber = function () {
    return this.stack.length === 0 ? 0 : this.stack.pop();
};

function Befunge(source, constraints) {
    this.board = new BefungeBoard(source, constraints);
    this.stack = new BefungeStack();
    this.stringMode = false;
    this.terminated = false;

    this.digits = "0123456789".split('');
}

Befunge.prototype.run = function () {
    for (var i = 1; i <= (this.stepsPerTick || 10); i++) {
        this.step();
        if (this.terminated) {
            return;
        }
    }

    requestAnimationFrame(this.run.bind(this));
};

Befunge.prototype.step = function () {
    this.processCurrentToken();
    this.board.advance();
};

Befunge.prototype.processCurrentToken = function () {
    var token = this.board.currentToken();
    if (this.stringMode && token !== '"') {
        return this.stack.pushAscii(token);
    }

    if (this.digits.indexOf(token) !== -1) {
        return this.stack.pushNumber(token);
    }

    switch (token) {
        case ' ':
            while ((token = this.board.nextToken()) == ' ') {
                this.board.advance();
            }
            return;
        case '+':
            return this.stack.pushNumber(this.stack.popNumber() + this.stack.popNumber());
        case '-':
            return this.stack.pushNumber(-this.stack.popNumber() + this.stack.popNumber());
        case '*':
            return this.stack.pushNumber(this.stack.popNumber() * this.stack.popNumber());
        case '/':
            var denominator = this.stack.popNumber(),
                numerator = this.stack.popNumber(),
                result;
            if (denominator === 0) {
                result = +prompt("Illegal division by zero. Please enter the result to use:");
            } else {
                result = Math.floor(numerator / denominator);
            }

            return this.stack.pushNumber(result);
        case '%':
            var modulus = this.stack.popNumber(),
                numerator = this.stack.popNumber(),
                result;
            if (modulus === 0) {
                result = +prompt("Illegal division by zero. Please enter the result to use:");
            } else {
                result = Math.floor(numerator / modulus);
            }

            return this.stack.pushNumber(result);
        case '!':
            return this.stack.pushNumber(this.stack.popNumber() === 0 ? 1 : 0);
        case '`':
            return this.stack.pushNumber(this.stack.popNumber() < this.stack.popNumber() ? 1 : 0);
        case '>':
            this.board.direction = Direction.RIGHT;
            return;
        case '<':
            this.board.direction = Direction.LEFT;
            return;
        case '^':
            this.board.direction = Direction.UP;
            return;
        case 'v':
            this.board.direction = Direction.DOWN;
            return;
        case '?':
            this.board.direction = [Direction.RIGHT, Direction.UP, Direction.LEFT, Direction.DOWN][Math.floor(4 * Math.random())];
            return;
        case '_':
            this.board.direction = this.stack.popNumber() === 0 ? Direction.RIGHT : Direction.LEFT;
            return;
        case '|':
            this.board.direction = this.stack.popNumber() === 0 ? Direction.DOWN : Direction.UP;
            return;
        case '"':
            this.stringMode = !this.stringMode;
            return;
        case ':':
            var top = this.stack.popNumber();
            this.stack.pushNumber(top);
            return this.stack.pushNumber(top);
        case '\\':
            var first = this.stack.popNumber(),
                second = this.stack.popNumber();
            this.stack.pushNumber(first);
            return this.stack.pushNumber(second);
        case '$':
            return this.stack.popNumber();
        case '#':
            return this.board.advance();
        case 'p':
            return this.board.grid[this.stack.popNumber()][this.stack.popNumber()] = this.stack.popAscii();
        case 'g':
            return this.stack.pushAscii(this.board.grid[this.stack.popNumber()][this.stack.popNumber()]);
        case '&':
            return this.stack.pushNumber(+prompt("Please enter a number:"));
        case '~':
            return this.stack.pushAscii(prompt("Please enter a character:")[0]);
        case '.':
            return this.print(this.stack.popNumber());
        case ',':
            return this.print(this.stack.popAscii());
        case '@':
            this.terminated = true;
            return;
    }
};

Befunge.prototype.withStdout = function (printer) {
    this.print = printer;
    return this;
};

Befunge.prototype.withOnAdvance = function (onAdvance) {
    this.board.onAdvance = onAdvance;
    return this;
};

String.repeat = function (str, count) {
    var repeated = "";
    for (var i = 1; i <= count; i++) {
        repeated += str;
    }

    return repeated;
};

window['requestAnimationFrame'] = window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || function (callback) {
    window.setTimeout(callback, 1000 / 60);
};

(function () {
    var currentInstance = null;

    function resetInstance() {
        currentInstance = null;
    }

    function getOrCreateInstance() {
        if (currentInstance !== null && currentInstance.terminated) {
            resetInstance();
        }

        if (currentInstance === null) {
            var boardSize = Editor.getBoardSize();
            currentInstance = new Befunge(Editor.getSource(), {
                width: boardSize.width,
                height: boardSize.height
            });
            currentInstance.stepsPerTick = Editor.getStepsPerTick();

            currentInstance.withStdout(Editor.append);
            currentInstance.withOnAdvance(function (position) {
                Editor.highlight(currentInstance.board.grid, position.x, position.y);
            });
        }

        return currentInstance;
    }

    var Editor = (function (onExecute, onStep, onReset) {
        var source = document.getElementById('source'),
            sourceDisplay = document.getElementById('source-display'),
            sourceDisplayWrapper = document.getElementById('source-display-wrapper'),
            stdout = document.getElementById('stdout');
        var execute = document.getElementById('execute'),
            step = document.getElementById('step'),
            reset = document.getElementById('reset');
        var boardWidth = document.getElementById('board-width'),
            boardHeight = document.getElementById('board-height'),
            stepsPerTick = document.getElementById('steps-per-tick');

        function showEditor() {
            source.style.display = "block";
            sourceDisplayWrapper.style.display = "none";
            source.focus();
        }

        function hideEditor() {
            source.style.display = "none";
            sourceDisplayWrapper.style.display = "block";

            var computedHeight = getComputedStyle(source).height;
            sourceDisplayWrapper.style.minHeight = computedHeight;
            sourceDisplayWrapper.style.maxHeight = computedHeight;

            sourceDisplay.textContent = source.value;
        }

        function resetOutput() {
            stdout.value = null;
        }

        function escapeEntities(input) {
            return input.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
        }

        sourceDisplayWrapper.onclick = function () {
            resetOutput();
            showEditor();
            onReset && onReset.call(null);
        };
        execute.onclick = function () {
            resetOutput();
            hideEditor();
            onExecute && onExecute.call(null);
        };
        step.onclick = function () {
            hideEditor();
            onStep && onStep.call(null);
        };
        reset.onclick = function () {
            resetOutput();
            showEditor();
            onReset && onReset.call(null);
        };

        return {
            getSource: function () {
                return source.value;
            },

            append: function (content) {
                stdout.value = stdout.value + content;
            },

            highlight: function (grid, x, y) {
                var highlighted = [];
                for (var row = 0; row < grid.length; row++) {
                    highlighted[row] = [];
                    for (var column = 0; column < grid[row].length; column++) {
                        highlighted[row][column] = escapeEntities(grid[row][column]);
                    }
                }

                highlighted[y][x] = '<span class="activeToken">' + highlighted[y][x] + '</span>';
                sourceDisplay.innerHTML = highlighted.map(function (lineTokens) {
                    return lineTokens.join('');
                }).join('\n');
            },

            getBoardSize: function () {
                return {
                    width: +boardWidth.innerHTML,
                    height: +boardHeight.innerHTML
                };
            },

            getStepsPerTick: function () {
                return +stepsPerTick.innerHTML;
            }
        };
    })(function () {
        getOrCreateInstance().run();
    }, function () {
        getOrCreateInstance().step();
    }, resetInstance);
})();
.container {
    width: 100%;
}
.so-box {
    font-family:'Helvetica Neue', Arial, sans-serif;
    font-weight: bold;
    color: #fff;
    text-align: center;
    padding: .3em .7em;
    font-size: 1em;
    line-height: 1.1;
    border: 1px solid #c47b07;
    -webkit-box-shadow: 0 2px 2px rgba(0, 0, 0, 0.3), 0 2px 0 rgba(255, 255, 255, 0.15) inset;
    text-shadow: 0 0 2px rgba(0, 0, 0, 0.5);
    background: #f88912;
    box-shadow: 0 2px 2px rgba(0, 0, 0, 0.3), 0 2px 0 rgba(255, 255, 255, 0.15) inset;
}
.control {
    display: inline-block;
    border-radius: 6px;
    float: left;
    margin-right: 25px;
    cursor: pointer;
}
.option {
    padding: 10px 20px;
    margin-right: 25px;
    float: left;
}
input, textarea {
    box-sizing: border-box;
}
textarea {
    display: block;
    white-space: pre;
    overflow: auto;
    height: 75px;
    width: 100%;
    max-width: 100%;
    min-height: 25px;
}
span[contenteditable] {
    padding: 2px 6px;
    background: #cc7801;
    color: #fff;
}
#controls-container, #options-container {
    height: auto;
    padding: 6px 0;
}
#stdout {
    height: 50px;
}
#reset {
    float: right;
}
#source-display-wrapper {
    display: none;
    width: 100%;
    height: 100%;
    overflow: auto;
    border: 1px solid black;
    box-sizing: border-box;
}
#source-display {
    font-family: monospace;
    white-space: pre;
    padding: 2px;
}
.activeToken {
    background: #f88912;
}
.clearfix:after {
    content:".";
    display: block;
    height: 0;
    clear: both;
    visibility: hidden;
}
.clearfix {
    display: inline-block;
}
* html .clearfix {
    height: 1%;
}
.clearfix {
    display: block;
}
<div class="container">
    <textarea id="source" placeholder="Enter your Befunge-93 program here" wrap="off">"39-egnufeB">:#,_@</textarea>
    <div id="source-display-wrapper">
        <div id="source-display"></div>
    </div>
</div>
<div id="controls-container" class="container clearfix">
    <input type="button" id="execute" class="control so-box" value="► Execute" />
    <input type="button" id="step" class="control so-box" value="Step" />
    <input type="button" id="reset" class="control so-box" value="Reset" />
</div>
<div id="stdout-container" class="container">
    <textarea id="stdout" placeholder="Output" wrap="off" readonly></textarea>
</div>
<div id="options-container" class="container">
    <div class="option so-box">Steps per Tick: <span id="steps-per-tick" contenteditable>500</span>

    </div>
    <div class="option so-box">Board Size: <span id="board-width" contenteditable>80</span> x <span id="board-height" contenteditable>25</span>

    </div>
</div>

Ingo Bürk

Posted 2014-10-21T02:41:05.133

Reputation: 2 674

Nice! Do we have to manually input the board size? It seems that if we don't, highlighting tends to go all over the place. (testing with the first program here: https://en.wikipedia.org/wiki/Befunge#Sample_Befunge-93_code)

– Sp3000 – 2014-10-27T06:05:41.420

@Sp3000 You only have to enter it if you want to change it (default is the original 80x25). The program works fine for me. "All over the place" is what Befunge is all about. The only not-so-nice thing is that you have to step through all the whitespace of the board individually, but I will fix this. It's technically correct, though. If you feel that something is weird, maybe you can make a gif/video of how it looks for you? – Ingo Bürk – 2014-10-27T06:30:07.693

@Sp3000 One thing: if you just hit "Execute", the highlighting does seem odd because it is going too fast. If you haven't before, just try "Step" to step through the program command for command. Alternatively, set the steps per tick to 1 and hit Execute for a smoother animation. – Ingo Bürk – 2014-10-27T06:31:25.323

Ahaha yeah by "all over the place" I meant the fact that all the whitespace is stepped through. Are you planning on making the program detect the board size in the future? (If you get time) – Sp3000 – 2014-10-27T06:32:32.837

1@Sp3000 The board size is a property of the interpreter, not of the program. Originally, Befunge boards have a fixed size. But you can allow infinite size which I am planning on doing. I also plan on skipping consecutive whitespace in a single step. – Ingo Bürk – 2014-10-27T06:40:06.747

@Sp3000 It skips whitespaces now. An infinite board is a little bit tricker because I can't display it directly. I'll work on it, though. Just requires some refactoring. – Ingo Bürk – 2014-10-27T18:40:22.583

If I recall correctly, 80x25 is the board size specified by the Befunge-93 specification (source). If you're only claiming to be able to run Befunge-93, then you shouldn't need infinite board sizes.

– Kasran – 2014-10-30T22:37:56.740

@Kasran I know, I just wanted to support it so that the interpreter is Turing-complete. :) – Ingo Bürk – 2014-11-01T11:15:05.340

But Befunge-93 isn't Turing-complete (because of the grid size limit)... Eh. I shouldn't argue. Thanks for even making this thing at all! – Kasran – 2014-11-01T23:53:50.213

@Kasran Yes, it isn't. But you can make it to be Turing-complete. It'd still support traditional Befunge. Just a feature. :) and thank you! – Ingo Bürk – 2014-11-02T07:30:43.397

33

TI-BASIC

'cause who doesn't like TI-Basic?

I wanted to contribute to this, so I picked a language that is (in my humble opinion) slightly more complicated than, say, Deadfish, but within my control. I know a lot about my calculator, so I picked this.

I don't however, have any background experience with JavaScript/CSS/HTML. This was my first program. That means...

Please point out the errors you find in my code!

That being said, this is a limited version of TI-Basic. I will continue to implement things as I get around to them.

Features as of right now

  • common control keywords (if,then,else,end)
  • All available loops (while, repeat, for)
  • disp command
  • JavaScript arithmetic operators, via eval
  • Lists
  • Built in auto colon-ator (see for yourself)
  • Random values of uninitialized variables, just like your calculator
  • limited math - sin,cos,tan,asin,acos,atan,sinh,cosh,tanh,log,ln,int,round,abs.
  • constants pi and e as variables
  • Prompt, input, pause (Yay!)
  • writes to output during execution (thanks to pseudonym117)
  • simple pixel-on/off / clear screen capabilities

Liberties taken

  • supports multi-character variable names. Unfortunately, this means ab is a variable, not the value a*b. This uni-character variable name limit drove me crazy on my calculator.
  • ignoring "goto" and "lbl". They were buggy even on the calculator unless you did it a certain way.
  • Going to ignore prgm, OpenLib and ExecLib because there are no external/other files

What still needs to be done

  • Some sort of canvas for drawing (coming soon!) More drawing stuff
  • Some way for prompt and input commands in the text window (suggestions, anyone?)
  • Support for (the rest of) calculator math operations (there's BUCKETS)
  • Suppot for Ans keyword shenanigans.
  • Support for rest of CTL keywords
  • Some way to support a form of getkey, though most languages nowadays are event driven... Perhaps it could be the last pressed key.
  • I don't know if this is true, but JavaScript seems to think first, then display to a textarea, rather than display as it goes.
  • There's no CSS/HTML prettiness

I still don't know much about JavaScript. If any of the above are possible/impossible, please let me know.

function inputChanged() {
    var codeText = document.getElementById('code-block');
    if (codeText.value.charAt(0) !== ':') {
        codeText.value = ':' + codeText.value;
    }
    var cursor = codeText.selectionEnd;
    if (lastText.length < codeText.value.length) { //adding characters
        if (/(\n)$/.test(codeText.value) === true || /(\n)(\n)/.test(codeText.value) === true) {
            cursor++;
        }
        codeText.value = codeText.value.replace(/(\n)$/, '\n:');
        codeText.value = codeText.value.replace(/(\n)(\n)/, '\n:\n');
    } else { //removing characters
        if (/(\n)$/.test(codeText.value) === true || /(\n)(\n)/.test(codeText.value) === true) {
            cursor--;
        }
        codeText.value = codeText.value.replace(/(\n)$/, '');
        codeText.value = codeText.value.replace(/(\n)(\n)/, '\n');
    }
    codeText.setSelectionRange(cursor, cursor);
    lastText = codeText.value;
}
function onUserInput(e){
    var output = document.getElementById('output');
    //enter has code 13
    if(awaitingInput && e.keyCode===13){
        var lines = output.value.split('\n');
        var input = lines[lines.length-1];
        if(input.indexOf('?')!==-1){
            input = input.substring(input.indexOf('?')+1,input.length);
        }
        if(newInputVarName !== null){
            var iValue = replaceVars(input);
            variables[newInputVarName]= eval(iValue);
        }else{
           output.value = output.value.substring(0,output.value.length-1);  
        }
        awaitingInput = false;
    }
}
var lastText = ':';
var L1 = [],
    L2 = [],
    L3 = [],
    L4 = [],
    L5 = [],
    L6 = []; //note - these are special. They're like variables, and are considered in replaceVars.
var tokens = [':', '-->', 'if', 'then',
    'else', 'for', 'while', ',',
    'repeat', 'end', 'disp','prompt','input',
    'pause','pxl-on','pxl-off','clrdraw'];
var variables = [];
var loopStack = []; //holds line numbers
var ifStack = [true]; //holds true or false
var typeStack = ['if'];
var repeatStack = []; //holds current iteration for repeats. NOTE: starts at 1 to accomidate repeat 0
var awaitingInput = false;
var newInputVarName=null;
var pixels = []; //96*64
var running = false;
var lineProcessing;
function run() {
    if(running===false){
        for(var pixelX=0;pixelX<96;pixelX++){
            for(var pixelY=0;pixelY<64;pixelY++){
                pixels[(pixelX,pixelY)]=0;         //doesn't work
            }
        }
        variables = [];
        loopStack = [];
        ifStack = [true];
        typeStack = ['if'];
        repeatStack = [];
        clear();
        var code = document.getElementById('code-block');
        var lines = code.value.split('\n');
        var currentLine = 0;
        variables['e'] = Math.E;
        variables['pi'] = Math.PI;
        L1.push(3);
        L1.push(4);
        lineProcessing = setInterval(function(){
            if(!awaitingInput){
                running = true;
                if(currentLine < lines.length || loopStack.length !== 0){
                    currentLine = processLine(lines,currentLine);
                    currentLine++;
                }
                else{
                    running = false;
                    lineProcessing.clearInterval();
                }
            }
        },10);
    }
}
function processLine(lines, currentLine) {
    var tokens = tokenize(lines[currentLine]);
    if (ifStack[ifStack.length - 1] === true){
        if (tokens.indexOf('-->') !== -1) {
            var index = tokens.indexOf('-->');
            for (var i = 0; i < index; i++) {
                tokens[i] = replaceVars(tokens[i]);
            }
            var value = eval(tokens[index - 1]);
            var name = tokens[index + 1];
            var listElement = false;
            if (name.length > 4) {
                if (/L[1-6]\(/.test(name) === true) {
                    var nextP = matchParenthese(name, 2);
                    var innerStuff = name.substring(3, nextP);
                    var value2 = eval(replaceVars(innerStuff));
                    var listNum = name.substring(0, 2);
                    if (value2 === Math.round(value2)) {
                        if (value2 > 0) {
                            if (listNum === 'L1') {
                                L1[value2] = value;
                            }
                            if (listNum === 'L2') {
                                L2[value2] = value;
                            }
                            if (listNum === 'L3') {
                                L3[value2] = value;
                            }
                            if (listNum === 'L4') {
                                L4[value2] = value;
                            }
                            if (listNum === 'L5') {
                                L5[value2] = value;
                            }
                            if (listNum === 'L6') {
                                L6[value2] = value;
                            }
                        } else {
                            printE('Arrays can only have positive indexes, and by some weird quirk of Texas Instruments, indexes start at 1, not 0.');
                            throw new Error();
                        }
                    } else {
                        printE('Sorry, this is not Javascript. Array indexes must be integers.');
                        throw new Error();
                    }
                }
            }
            variables[name + ""] = value;
        }
        if (tokens.indexOf('disp') !== -1) {
            var index2 = tokens.indexOf('disp');
            for (var g = index2 + 1; g < tokens.length; g++) {
                tokens[g] = replaceVars(tokens[g]);
                if (tokens[g] === ',') {} else {
                    print(eval(tokens[g]));
                }
            }
        }
        if(tokens.indexOf('prompt')!==-1){
            var pIndex = tokens.indexOf('prompt');
            var newVar = tokens[pIndex+1];
            document.getElementById('output').value += newVar+'=?';
            awaitingInput=true;
            newInputVarName = newVar;
        }
        if(tokens.indexOf('input')!==-1){
            var pIndex = tokens.indexOf('input');
            var newVar = tokens[pIndex+1];
            document.getElementById('output').value += '?';
            awaitingInput=true;
            newInputVarName = newVar;
        }
        if(tokens.indexOf('pause')!==-1){
            awaitingInput=true;
            newInputVarName = null;
        }
        if(tokens.indexOf('pxl-on')!==-1){
            var first = tokens[2].substring(1,tokens[2].length);
            var second = tokens[4].substring(0,tokens[4].length-1);
            first = replaceVars(first);
            second = replaceVars(second);
            var valueF = eval(first);
            var valueS = eval(second);
            drawPixel(valueF,valueS,1);
            pixels[(valueF,valueS)] = 1;
        }
        if(tokens.indexOf('pxl-off')!==-1){
            var first2 = tokens[2].substring(1,tokens[2].length);
            var second2 = tokens[4].substring(0,tokens[4].length-1);
            first2 = replaceVars(first2);
            second2 = replaceVars(second2);
            var valueF2 = eval(first2);
            var valueS2 = eval(second2);
            pixels[(valueF2,valueS2)] = 0;
            drawPixel(valueF2,valueS2,0);
        }
        if(tokens.indexOf('clrdraw')!==-1){
            clearPixels();
        }
        if (tokens.indexOf('if') !== -1) {
            var index3 = tokens.indexOf('if');
            tokens[index3 + 1] = replaceVars(tokens[index3 + 1]);
            var value = eval(tokens[index3 + 1]);
            ifStack.push(value);
            typeStack.push('if');
        }
        if (tokens.indexOf('while') !== -1) {
            var index4 = tokens.indexOf('while');
            var expression = tokens[index4 + 1];
            expression = replaceVars(expression);
            var value2 = eval(expression);
            if (value2 === true) {
                typeStack.push('while');
                loopStack.push(currentLine);
            } else {
                currentLine = nextEnd(currentLine, lines);
                return currentLine;
            }
        }
        if (tokens.indexOf('for') !== -1) {
            var index5 = tokens.indexOf('for');
            var varName = tokens[index5 + 1];
            varName = varName.trim();
            varName = varName.substring(1, varName.length);
            var initValue = eval(replaceVars(tokens[index5 + 3]));
            variables[varName] = initValue;
            var endingValue = eval(replaceVars(tokens[index5 + 5]));
            var increaseVal = replaceVars(tokens[index5 + 7]);
            increaseVal = eval(increaseVal.substring(0, increaseVal.length - 1));
            var diff = endingValue - initValue;
            if (sign(diff) !== sign(increaseVal) && sign(diff) !== 0) {
                currentLine = nextEnd(currentLine, lines);
                return currentLine;
            } else {
                loopStack.push(currentLine);
                typeStack.push('for');
            }
        }
        if (tokens.indexOf('repeat') !== -1) {
            var repeatVal = eval(replaceVars(tokens[1]));
            if (repeatVal === true || repeatVal === false) {
                //basically the same as a while loop...so just replace it!
                var newLine = 'while ' + '!(' + tokens[1] + ')';
                lines[currentLine] = newLine;
                currentLine--;
                return currentLine;
            } else {
                repeatStack.push(0);
                typeStack.push('repeat');
                loopStack.push(currentLine);
            }
        }
    }
    if (tokens.indexOf('else') !== -1) {
       ifStack[ifStack.length - 1] = !ifStack[ifStack.length - 1];
    }
    if (tokens.indexOf('end') !== -1) {
        if (typeStack[typeStack.length - 1] === 'if') {
            ifStack.pop();
            typeStack.pop();
        } else if (typeStack[typeStack.length - 1] === 'while') {
            var prevLineNum = loopStack[loopStack.length - 1];
            var prevLine = lines[prevLineNum];
            var prevTokens = tokenize(prevLine);
            var whileIndex = prevTokens.indexOf('while');
            var expression2 = replaceVars(prevTokens[whileIndex + 1]);
            if (eval(expression2) === true) {
                currentLine = prevLineNum - 1;
                return currentLine;
            } else {
                loopStack.pop();
                typeStack.pop();
            }
        } else if (typeStack[typeStack.length - 1] === 'repeat') {
            var line = lines[loopStack[loopStack.length - 1]];
            var tokenized = tokenize(line);
            var numTimes = eval(replaceVars(tokenized[1]));
            var currentIteration = repeatStack[repeatStack.length - 1];
            currentIteration++;
            if (currentIteration !== numTimes) {
                repeatStack[repeatStack.length - 1] = currentIteration;
                currentLine = loopStack[loopStack.length - 1]; //once currentLine++, will be on next line after repeat;
                return currentLine;
            } else {
                repeatStack.pop();
                typeStack.pop();
                loopStack.pop();
            }
        } else if (typeStack[typeStack.length - 1] === 'for') {
            var prevLineNum2 = loopStack[loopStack.length - 1];
            var prevLine2 = lines[prevLineNum2];
            var prevTokens2 = tokenize(prevLine2);
            var forIndex = prevTokens2.indexOf('for');
            var varName2 = prevTokens2[forIndex + 1];
            varName2 = varName2.substring(1, varName2.length);
            var initVal = eval(replaceVars(prevTokens2[forIndex + 3]));
            var endVal = eval(replaceVars(prevTokens2[forIndex + 5]));
            var stepVal = replaceVars(prevTokens2[forIndex + 7]);
            stepVal = eval(stepVal.substring(0, stepVal.length - 1));
            variables[varName2] = parseInt(stepVal, 10) + parseInt(variables[varName2], 10);
            var signDiff = sign(endVal - initVal);
            var condTrue = true;
            if (signDiff === 0) {
                condTrue = false;
            }
            if (signDiff === 1) {
                if (variables[varName2] > endVal) {
                    condTrue = false;
                }
            } else {
                if (variables[varName2] < endVal) {
                    condTrue = false;
                }
            }
            if (condTrue === true) {
                currentLine = prevLineNum2;
                return currentLine;
            } else {
                typeStack.pop();
                loopStack.pop();
            }
        }
    }
    return currentLine;
}

function sign(num) {
    if (num === 0) {
        return 0;
    }
    if (num > 0) {
        return 1;
    }
    return -1;
}
function drawPixel(x,y,color){//color = 0 if off, 1 if on
    var canvas = document.getElementById('canvas');
    var width = canvas.width;
    var height = canvas.height;
    var pxWidth = width/96;
    var pxHeight = height/64;
    var ctx = canvas.getContext('2d');
    if(color===1){
        ctx.fillStyle = '#000';
    }else{
        ctx.fillStyle = '#fff';
    }
    ctx.fillRect((x/96)*width, (y/64)*height,pxWidth,pxHeight);
}
function nextEnd(line, lines) {
    for (var i = line + 1; i < lines.length; i++) {
        if (lines[i].indexOf('end') !== -1) {
            return i;
        }
    }
    return line;
}

function tokenize(line) {
    line = line.trim();
    var split = [];
    var currentSplit = '';
    var cursor = 0;
    var unknown = '';
    var allTokens = tokens;
    while (cursor < line.length) {
        var lengthOfCursor = line.length - cursor;
        while (lengthOfCursor > 0) {
            var index = allTokens.indexOf(line.substring(cursor, cursor + lengthOfCursor));
            if (index !== -1) {
                if (unknown !== '') {
                    split.push(unknown.trim());
                }
                unknown = '';
                split.push(allTokens[index]);
                cursor += lengthOfCursor - 1; //for cursor++ later
                break;
            }
            lengthOfCursor--;
            if (lengthOfCursor === 0) {
                unknown = unknown.concat(line.charAt(cursor));
            }
        }
        cursor++;
    }
    if (unknown !== '') {
        split.push(unknown.trim());
    }
    return split;
}

function replaceVars(token) {
    for (var i = 0; i < tokens.length; i++) {
        if (token === tokens[i]) {
            return token;
        }
    }
    //deals with lists
    for (var cursor = 0; cursor < token.length - 1; cursor++) {
        if (/[^a-zA-Z_]L[1-6]|^L[1-6]/.test(token.substring(cursor, cursor + 3)) === true) {
            var parenthIndex;
            for (var newCurs = cursor; newCurs < token.length; newCurs++) {
                if (token.charAt(newCurs) === '(') {
                    parenthIndex = newCurs;
                    break;
                }
            }
            var nextPIndex = matchParenthese(token, parenthIndex);
            var inner = token.substring(parenthIndex + 1, nextPIndex);
            //note recursiveness, supports L1(L1(L1(5))), for example
            var innerValue = eval(replaceVars(inner));
            var num = parseInt(token.substring(parenthIndex - 1, parenthIndex));
            var realValue;
            if (num === 1) {
                realValue = L1[innerValue];
            }
            if (num === 2) {
                realValue = L2[innerValue];
            }
            if (num === 3) {
                realValue = L3[innerValue];
            }
            if (num === 4) {
                realValue = L4[innerValue];
            }
            if (num === 5) {
                realValue = L5[innerValue];
            }
            if (num === 6) {
                realValue = L6[innerValue];
            }
            if (innerValue !== Math.round(innerValue) || innerValue < 1) {
                printE('array indexes must be integers greater or equal to 1. Why? ask Texas Instruments.');
                throw new Error();
            }
            token = token.substring(0, parenthIndex - 2) + realValue + token.substring(nextPIndex + 1);
        }
    }
    //Math stuff
    var a;
    var sinFunc = function (value) {
        return Math.sin(value);
    };
    while ((a = returnValue('sin', token, sinFunc)) !== null) {
        token = a;
    }

    var cosFunc = function (value) {
        return Math.cos(value);
    };
    while ((a = returnValue('cos', token, cosFunc)) !== null) {
        token = a;
    }

    var tanFunc = function (value) {
        return Math.tan(value);
    };
    while ((a = returnValue('tan', token, tanFunc)) !== null) {
        token = a;
    }

    var asinFunc = function (value) {
        return Math.asin(value);
    };
    while ((a = returnValue('asin', token, asinFunc)) !== null) {
        token = a;
    }

    var acosFunc = function (value) {
        return Math.acos(value);
    };
    while ((a = returnValue('acos', token, acosFunc)) !== null) {
        token = a;
    }

    var atanFunc = function (value) {
        return Math.atan(value);
    };
    while ((a = returnValue('atan', token, atanFunc)) !== null) {
        token = a;
    }
    //note my calculator doesn't have atan2...

    var absFunc = function (value) {
        return Math.abs(value);
    };
    while ((a = returnValue('abs', token, absFunc)) !== null) {
        token = a;
    }

    var roundFunc = function (value) {
        return Math.round(value);
    };
    while ((a = returnValue('round', token, roundFunc)) !== null) {
        token = a;
    }

    var intFunc = function (value) {
        return Math.floor(value);
    };
    while ((a = returnValue('int', token, intFunc)) !== null) {
        token = a;
    }

    var coshFunc = function (value) {
        return (Math.pow(Math.E, value) + Math.pow(Math.E, -1 * value)) / 2;
    };
    while ((a = returnValue('cosh', token, coshFunc)) !== null) {
        token = a;
    }

    var sinhFunc = function (value) {
        return (Math.pow(Math.E, value) - Math.pow(Math.E, -1 * value)) / 2;
    };
    while ((a = returnValue('sinh', token, sinhFunc)) !== null) {
        token = a;
    }

    var tanhFunc = function (value) {
        return (Math.pow(Math.E, value) - Math.pow(Math.E, -1 * value)) / (Math.pow(Math.E, value) + Math.pow(Math.E, -1 * value));
    };
    while ((a = returnValue('tanh', token, tanhFunc)) !== null) {
        token = a;
    }

    //if token contains new variables, then initialize them with random values
    var newReg = new RegExp('([^a-zA-Z_])([a-zA-Z_]+)([^a-zA-Z_])', 'g');
    var newReg2 = new RegExp('^([a-zA-Z_]+)([^a-zA-Z_])', 'g');
    var newReg3 = new RegExp('([^a-zA-Z_])([a-zA-Z_]+)$', 'g');
    var newReg4 = new RegExp('^([a-zA-Z_]+)$', 'g');
    var match1 = token.match(newReg);
    if (match1 !== null) {
        for (var q = 0; q < match1.length; q++) {
            initializeVar(match1[q].substring(1, match1[q].length - 1));
        }
    }

    var match2 = token.match(newReg2);
    if (match2 !== null) {
        for (var w = 0; w < match2.length; w++) {
            initializeVar(match2[w].substring(0, match2[w].length - 1));
        }
    }

    var match3 = token.match(newReg3);
    if (match3 !== null) {
        for (var e = 0; e < match3.length; e++) {
            initializeVar(match3[e].substring(1, match3[e].length));
        }
    }

    var match4 = token.match(newReg4);
    if (match4 !== null) {
        for (var r = 0; r < match4.length; r++) {
            initializeVar(match4[r].substring(0, match4[r].length));
        }
    }

    var varNames = [];
    for (var key in variables) {
        if (token.indexOf(key) !== -1) {
            var regex1 = '([^a-zA-Z_])(' + key + ')([^a-zA-Z_])';
            var reg = new RegExp(regex1, 'g');
            token = token.replace(reg, '$1' + variables[key] + '$3');
            var regex2 = '(^' + key + ')([^a-zA-Z_])';
            var reg2 = new RegExp(regex2, 'g');
            token = token.replace(reg2, variables[key] + '$2');
            var regex3 = '([^a-zA-Z_])(' + key + ')$';
            var reg3 = new RegExp(regex3, 'g');
            token = token.replace(reg3, '$1' + variables[key]);
            var reg4 = new RegExp('^' + key + '$', 'g');
            token = token.replace(reg4, variables[key]);
        }
    }
    return token;
}

function matchParenthese(token, index) {
    var pStack = [];
    for (var cursor = index + 1; cursor < token.length; cursor++) {
        if (token.charAt(cursor) === '(') {
            pStack.push(1);
        }
        if (token.charAt(cursor) === ')') {
            if (pStack.length > 0) {
                pStack.pop();
            } else {
                return cursor;
            }
        }
    }
    return index;
}

function returnValue(identifyer, token, toDoFunction) { //note: do NOT include parentheses in identifyer
    for (var cursor = 0; cursor < token.length; cursor++) {
        if (token.length > cursor + identifyer.length) {
            if (token.substring(cursor, cursor + identifyer.length) === identifyer) {
                if (cursor === 0 || /^[^a-zA-Z_]/.test(token.charAt(cursor - 1)) === true) {
                    if (nextNonWhiteChar(token, cursor + identifyer.length - 1) === '(') {
                        print(identifyer);
                        var nextParen = matchParenthese(token, cursor + identifyer.length + 1);
                        var inner = token.substring(cursor + identifyer.length + 1, nextParen);
                        inner = replaceVars(inner);
                        var value = eval(inner);
                        value = toDoFunction(value);
                        return token.substring(0, cursor) + value + token.substring(nextParen + 1, token.length);
                    }
                }
            }
        }

    }
    return null;
}

function nextNonWhiteChar(token, index) { //give non-white character
    for (var i = index + 1; i < token.length; i++) {
        if (token.charAt(i) !== ' ') {
            return token.charAt(i);
        }
    }
}
function clearPixels(){
    var canvas = document.getElementById('canvas');
    canvas.getContext('2d').fillStyle = '#fff';
    canvas.getContext('2d').fillRect(0,0,canvas.width,canvas.height);
    for(var x=0;x<96;x++){
        for(var y=0;y<64;y++){
            pixels[(x,y)] = 0;
        }
    }
}
function stop(){
    running = false;
    clearInterval(lineProcessing);
}
function initializeVar(name) {
    if (!(name in variables) && name !== 'L1' && name !== 'L2' && name !== 'L3' && name !== 'L4' && name !== 'L5' && name !== 'L6') {
        variables[name] = Math.random() * 100;
    }
}

function clear() {
    document.getElementById('output').value = '';
    document.getElementById('error').value = '';
}

function printE(str) {
    document.getElementById('error').value += str + '\n'
}

function print(str) {
    document.getElementById('output').value += str + '\n';
}
<h1>Ti-Basic Interpreter</h1>


<h3>Code</h3>

<textarea id='code-block' cols='50' rows='7' oninput='inputChanged()'>:clrdraw
:for(a,2,96,2)
:for(b,(a/2)%2,64,2)
:pxl-on(a,b)
:end
:end</textarea>
<br>
<button width='20' height='10' onclick='run()'>Run</button>
    <button width='20' height='10' onclick='stop()'>Stop</button>

<h3>Output (also input)</h3>

    <textarea id='output' cols='50' rows='7' onkeypress='onUserInput(event)'></textarea>
<br>
    <canvas id='canvas' width='192' height='128'></canvas>
<br>

<textarea id='error' cols='50' rows='7'></textarea>

Stretch Maniac

Posted 2014-10-21T02:41:05.133

Reputation: 3 971

1I am doing things with this code! I found an error: On line ~93, you have lineProcessing.clearInterval();; this instead should be clearInterval(lineProcessing);. – Conor O'Brien – 2015-10-20T00:58:49.163

@CᴏɴᴏʀO'Bʀɪᴇɴ Feel free to edit and change it. It's been awhile since I looked at this code. Also, I'm kinda swamped in school work right now, so it might be awhile till I change anything. – Stretch Maniac – 2015-10-20T01:10:09.017

3@StretchManiac Ah, okay. I understand -- procrastinating ATM ^_^ – Conor O'Brien – 2015-10-20T01:36:50.757

with regards to javascript thinking first then displaying, the browser runs in a single thread. rendering of DOM objects (the actual webpage) happens on this same thread, so until your code yields execution, the page will not update. the way to fix that is by using the setTimeout function (http://www.w3schools.com/jsref/met_win_settimeout.asp) to yield so that the browser can render your update, then continue your processign

– pseudonym117 – 2014-11-01T23:39:16.213

26

Brainfuck

Here's a basic BF interpreter. There's no debugger though, but has a couple of customisation options.

const NUM_CELLS = 30000;
const ITERS_PER_SEC = 100000;
const TIMEOUT_MILLISECS = 5000;
const ERROR_BRACKET = "Mismatched brackets";
const ERROR_TIMEOUT = "Timeout";
const ERROR_INTERRUPT = "Interrupted by user";

var code, input, wrap, timeout, eof, loop_stack, loop_map;
var running, start_time, code_ptr, input_ptr, cell_ptr, cells, iterations;

function clear_output() {
  document.getElementById("output").value = "";
  document.getElementById("stderr").innerHTML = "";
}

function stop() {
  running = false;
  document.getElementById("run").disabled = false;
  document.getElementById("stop").disabled = true;
  document.getElementById("clear").disabled = false;
  document.getElementById("wrap").disabled = false;
  document.getElementById("timeout").disabled = false;
  document.getElementById("eof").disabled = false;
}

function interrupt() {
  error(ERROR_INTERRUPT);
}

function error(msg) {
  document.getElementById("stderr").innerHTML = msg;
  stop();
}

function run() {
  clear_output();
  
  document.getElementById("run").disabled = true;
  document.getElementById("stop").disabled = false;
  document.getElementById("clear").disabled = true;
  document.getElementById("wrap").disabled = true;
  document.getElementById("timeout").disabled = true;
  document.getElementById("eof").disabled = true;

  code = document.getElementById("code").value;
  input = document.getElementById("input").value;
  wrap = document.getElementById("wrap").value;
  timeout = document.getElementById("timeout").checked;
  eof = document.getElementById("eof").value;

  loop_stack = [];
  loop_map = {};

  for (var i = 0; i < code.length; ++i) {
    if (code[i] == "[") {
      loop_stack.push(i);

    } else if (code[i] == "]") {
      if (loop_stack.length == 0) {
        error(ERROR_BRACKET);
        return;

      } else {
        var last_bracket = loop_stack.pop();
        loop_map[last_bracket] = i;
        loop_map[i] = last_bracket;
      }
    }
  }

  if (loop_stack.length > 0) {
    error(ERROR_BRACKET);
    return;
  }

  running = true;
  start_time = Date.now();
  code_ptr = 0;
  input_ptr = 0;
  cell_ptr = Math.floor(NUM_CELLS / 2);
  cells = {};
  iterations = 0;

  bf_iter(1);
}

function bf_iter(niters) {
  if (code_ptr >= code.length || !running) {
    stop();
    return;
  }

  var iter_start_time = Date.now();

  for (var i = 0; i < niters; ++i) {
    if (cells[cell_ptr] == undefined) {
      cells[cell_ptr] = 0;
    }

    switch (code[code_ptr]) {
      case "+":
        if ((wrap == "8" && cells[cell_ptr] == 255) ||
            (wrap == "16" && cells[cell_ptr] == 65535) || 
            (wrap == "32" && cells[cell_ptr] == 2147483647)) {
            cells[cell_ptr] = 0;
        } else {
          cells[cell_ptr]++;
        }
        break;
        
      case "-":
        if (cells[cell_ptr] == 0 && wrap != "-1"){
          if (wrap == "8"){ cells[cell_ptr] = 255 }
          if (wrap == "16"){ cells[cell_ptr] = 65535 }
          if (wrap == "32"){ cells[cell_ptr] = 2147483647 }    
        } else {
          cells[cell_ptr]--;
        }
        break;
      
      case "<": cell_ptr--; break;
      case ">": cell_ptr++; break;

      case ".":
        document.getElementById("output").value += String.fromCharCode(cells[cell_ptr]);
        break;

      case ",":
        if (input_ptr >= input.length) {
          if (eof != "nochange") {
            cells[cell_ptr] = parseInt(eof);
          }
        } else {
          cells[cell_ptr] = input.charCodeAt(input_ptr);
          input_ptr++;
        }
        break;

      case "[":
        if (cells[cell_ptr] == 0) {
          code_ptr = loop_map[code_ptr];
        }
        break;

      case "]":
        if (cells[cell_ptr] != 0) {
          code_ptr = loop_map[code_ptr];
        }
        break;
    }

    code_ptr++;
    iterations++;

    if (timeout && Date.now() - start_time > TIMEOUT_MILLISECS) {
      error(ERROR_TIMEOUT);
      return;
    }
  }

  setTimeout(function() { bf_iter(ITERS_PER_SEC * (Date.now() - iter_start_time)/1000) }, 0);
}
<div style="font-size:12px;font-family:Verdana, Geneva, sans-serif;">
  <div style="float:left; width:50%;">
    Code:
    <br>
    <textarea id="code" rows="4" style="overflow:scroll;overflow-x:hidden;width:90%;">>++++++++[<+++++++++>-]<.>>+>+>++>[-]+<[>[->+<<++++>]<<]>.+++++++..+++.>>+++++++.<<<[[-]<[-]>]<+++++++++++++++.>>.+++.------.--------.>>+.>++++.</textarea>
    <br>Input:
    <br>
    <textarea id="input" rows="2" style="overflow:scroll;overflow-x:hidden;width:90%;"></textarea>
    <p>
      Wrap:
      <select id="wrap">
        <option value="8">8-bit</option>
        <option value="16">16-bit</option>
        <option value="32">32-bit</option>
        <option value="-1">None</option>
      </select>
      &nbsp;
      Timeout:
      <input id="timeout" type="checkbox" checked="true" />&nbsp; EOF:
      <select id="eof">
        <option value="nochange">Same</option>
        <option value="0">0</option>
        <option value="-1">-1</option>
      </select>
    </p>
  </div>
  <div style="float:left; width:50%;">
    Output:
    <br>
    <textarea id="output" rows="6" style="overflow:scroll;width:90%;"></textarea>
    <p>
      <input id="run" type="button" value="Run" onclick="run()" />
      <input id="stop" type="button" value="Stop" onclick="interrupt()" disabled="true" />
      <input id="clear" type="button" value="Clear" onclick="clear_output()" />
      &nbsp;
      <span id="stderr" style="color:red"></span>
    </p>
  </div>
</div>

On my browser everything is contained within the expanded snippet, but I'm no expert on browser compatibility so I can't guarantee that'll be the same for everybody.

Features

  • Cell wrapping and EOF behaviour can be configured
  • Timeout can be bypassed
  • Program can be halted mid-execution
  • A nice little clear button
  • All this within the Stack Snippet box (or at least I tried) and clocking in at around 6k chars ungolfed

Sp3000

Posted 2014-10-21T02:41:05.133

Reputation: 58 729

Needs more semicolons. ASI is the work of devil.

– nyuszika7h – 2014-12-30T09:55:11.207

Perhaps you might like a few const statements in there at the beginning? – Conor O'Brien – 2015-10-20T01:37:26.737

Here is a cat program for you to test: ,.[>,.] (warning: don't try with large texts!). But this code works GREAT!. – Ismael Miguel – 2014-10-22T09:11:14.117

@IsmaelMiguel What about just +[,.] :) – seequ – 2014-10-23T20:01:45.040

@Sieg That one requires that the EOF be set to 0 but it works too! – Ismael Miguel – 2014-10-23T21:38:48.813

@IsmaelMiguel And so does yours. :) – seequ – 2014-10-24T12:59:55.040

@Sieg But mine works with the "default settings". But yours is a great answer and I respect it. – Ismael Miguel – 2014-10-24T13:20:43.483

1@IsmaelMiguel Personally I prefer not to move the pointer inside the loop so that people don't notice that the tape is bounded by Javascript's integer size :P – Sp3000 – 2014-10-24T13:25:55.607

1@Sp3000 Well, If you can read the text length, you can be sure that the code will run fine. If you manage to get a text who's length size is 0xFFFFFFFFFFFFFFFF and my code "dies", then you give a medal to Sieg. – Ismael Miguel – 2014-10-24T14:26:36.187

@IsmaelMiguel Trust me, if there's a difference in how our snippets handle EOF, the implementation is faulty. – seequ – 2014-10-24T20:59:17.840

@Sieg It actually makes a difference with EOF same (i.e. don't change the cell value at EOF), which is what Ismael meant by "default settings". You can configure which one is the default via the snippet generator now though :) – Sp3000 – 2014-10-24T21:36:07.713

@Sp3000 "don't change the cell value at EOF" --> what do you mean with this? This is ambiguous for me :/ – Ismael Miguel – 2014-10-24T21:45:27.620

@IsmaelMiguel Reference: https://en.wikipedia.org/wiki/Brainfuck#End-of-file_behavior

– Sp3000 – 2014-10-24T21:47:39.033

@Sp3000 Oh, now that makes sense! I just don't see why your code doesn't get more upvotes. Maybe it's the length? – Ismael Miguel – 2014-10-24T21:53:51.160

1

I suggest you to use a shortcut for the document.getElementById() function.

– A.L – 2014-10-25T16:15:54.923

@Sp3000 I'm using your interpreter in another codegolf challenge, so you have my gratitude for this wonderful piece of code.

– Cipher – 2014-10-27T09:01:58.050

@Cipher Ahaha no problem. Feel free to tell me if there's any bugs/requests. – Sp3000 – 2014-10-27T09:06:01.320

Cool. I'm testing it with 16 bit and EOF=-1 on Zozotez with input (p 'test). Hopefully I'll see "test" printed out by the end of the day.

– Sylwester – 2014-10-28T13:33:09.993

+1 for your loop maps and features. It's was quite fast to be interpreted. Any reason why it doesn't drain a CPU when running? – Sylwester – 2014-10-28T13:52:43.307

@Sylwester I've set a limit (var ITERS_PER_SEC = 100000) on how many iterations it does per second. You can up it, but doing so might make the page freeze. I tested speed against a few other web interpreters - it's a fair bit slower, but then again I haven't optimised much (e.g. runs of +) – Sp3000 – 2014-10-28T14:13:00.750

21

Marbelous*

This interpreter is probably filled with bugs; if you find any, please let me know.

This supports all features of Marbelous except #include. There is also an option for using a cylindrical board (marbles shoved off the sides of the board reappear on the other side).

The interpreter also comes with many boards present in the examples in the repo for the python interpreter for Marbelous. These boards are listed below:

dec_out.mbl - Dp, Decout
hex_out.mbl - Hp, Hexo
fourwayincrement.mbl - Fwin
threewaysplit.mbl - 3W
bitwise_operations.mbl - Bitx, Bdif, Borr, Band, Bxor, Bnor
logical_operations.mbl - Tf, Nt, Lorr, Land, Lnor, Lxor, Cmpr, Eqal, Gteq, Lteq, Grtr, Less, Sort
replace_input.mbl - Replac
adder.mbl - Plus
arithmetic.mbl - Subt, Subo, Subful, Addo, Addful, Mult
wide_devices.mbl - Wideadditionfunc, Widesubtractfunc, Wbitleft, Wbitrght, Wbitfetchx, Mulx, Doblmult, Widemultiplyfunc

No includes should be used. See the python interpreter's repo for details on what each of these boards do.

*More Information on Marbelous:

Note: Graphical Output (256x256 double buffered with 24-bit RGB color) is available on this interpreter. The following boards may be used for this purpose:

{}{}{}{}{} - Set Pixel for Back Buffer (}0 - x, }1 - y, }2 - red, }3 - green, }4 - blue)
>< - Swap Buffers and Clear Back Buffer (}0 - anything)
@f@f@f - Get Pixel from Front Buffer (}0 - x, }1 - y, {0 - red, {1 - green, {2 - blue)
@b@b@b - Get Pixel from Back Buffer (}0 - x, }1 - y, {0 - red, {1 - green, {2 - blue)

Please note that having Use Draw Buffers checked (required for graphical output) might slow down the interpreter, and that graphical output is slow.

Update: changed buffers to use canvas instead of 1x1 pixel divs; drawing should be much faster now.

var stdin = "";
var boards = {};
var bnames = [];
var max_tick = 1000;
var space_as_blank = false;
var cylindrical_board = false;
var print_numbers = false;
var libraries = true;
var stopped = false;
var gfx = false;
var front_buffer = null;
var back_buffer = null;
function base36(a){ return a.toString(36).toUpperCase();}
function base16(a){ return ("0"+a.toString(16).toUpperCase()).substr(-2);}
function wto16(arr, i){ return arr[i] << 8 | arr[i+1]; }
function wto32(arr, i){ return arr[i] << 24 | arr[i+1] << 16 | arr[i+2] << 8 | arr[i+3]; }
function getch(){
var p = stdin.substr(0, 1);
stdin = stdin.substr(1);
if(p === '') return -1;
else return p.charCodeAt(0) & 255;
}
function putch(obj, ch){
if(print_numbers)
 obj.stdout += ch + ' ';
else
 obj.stdout += String.fromCharCode(ch);
}
function longestMatch(search, list){
var best = null, blen = 0;
for(var i = 0, len = list.length; i < len; ++i)
 if(list[i].length > blen && search.indexOf(list[i]) === 0)
  best = list[i], blen = best.length;
return best;
}
function swapBuffers(){
front_buffer.ctx.drawImage(back_buffer.canvas, 0, 0);
back_buffer.clear();
}

function jsBoard(name, inc, outc, code){
var f = eval('(function(inp, self){return ' + code + ';})');
boards[name] = new Board(name, true, f, inc, outc);
bnames.push(name);
}
function loadDefaultBoards(){
// implement \\ // /\ \/ ++ -- >> << ~~ ]] +n -n ?? ?n ^n =n >n <n as js boards
jsBoard('\\\\', 1, 0, '{37: inp[0]}');
jsBoard('//', 1, 0, '{36: inp[0]}');
jsBoard('/\\', 1, 0, '{36: inp[0], 37: inp[0]}');
jsBoard('\\/', 1, 0, '{}');
jsBoard('++', 1, 1, '{0: (inp[0]+1)&255}');
jsBoard('--', 1, 1, '{0: (inp[0]+255)&255}');
jsBoard('>>', 1, 1, '{0: inp[0]>>1}');
jsBoard('<<', 1, 1, '{0: (inp[0]<<1)&255}');
jsBoard('~~', 1, 1, '{0: (~inp[0])&255}');
jsBoard(']]', 1, 1, '(c=getch())>-1?{0:c}:{37:inp[0]}');
jsBoard('??', 1, 1, '{0: Math.floor(Math.random()*(inp[0]+1))}');
for(var i = 0; i < 36; ++i){
 j = base36(i);
 jsBoard('+'+j, 1, 1, '{0: (inp[0]+'+i+')&255}');
 jsBoard('-'+j, 1, 1, '{0: (inp[0]-'+i+')&255}');
 jsBoard('?'+j, 1, 1, '{0: Math.floor(Math.random()*'+(i+1)+')}');
 jsBoard('='+j, 1, 1, 'inp[0]=='+i+'?{0: inp[0]}:{37: inp[0]}');
 jsBoard('>'+j, 1, 1, 'inp[0]>'+i+'?{0: inp[0]}:{37: inp[0]}');
 jsBoard('<'+j, 1, 1, 'inp[0]<'+i+'?{0: inp[0]}:{37: inp[0]}');
 if(i < 8){
  jsBoard('^'+j, 1, 1, '{0: !!(inp[0]&(1<<'+i+'))}');
 }
}
if(gfx){
 jsBoard('{}{}{}{}{}', 5, 0, 'back_buffer.set(inp[0], inp[1], inp[2], inp[3], inp[4]),{}');
 jsBoard('@f@f@f', 2, 3, 'front_buffer.get(inp[0], inp[1])');
 jsBoard('@b@b@b', 2, 3, 'back_buffer.get(inp[0], inp[1])');
 jsBoard('><', 1, 0, 'swapBuffers(), {}');
}
if(libraries){
 // dec_out.mbl - Dp, Decout
 jsBoard('Dp', 1, 0, 'putch(self,Math.floor(inp[0]/100)+0x30),putch(self,Math.floor(inp[0]/10)%10+0x30),putch(self,inp[0]%10+0x30),{}');
 jsBoard('Decout', 1, 3, '{0: inp[0]/100, 1: inp[0]/10%10, 2: inp[0]%10}');
 // hex_out.mbl - Hp, Hexo
 jsBoard('Hp', 1, 0, 's=base16(inp[0]),putch(self,s.charCodeAt(0)),putch(self,s.charCodeAt(1)),{}');
 jsBoard('Hexo', 1, 2, 's=base16(inp[0]),{0: s.charCodeAt(0), 1: s.charCodeAt(1)}');
 // fourwayincrement.mbl - Fwin
 jsBoard('Fwin', 1, 2, '{36: inp[0], 0: (inp[0]+1)&255, 1: (inp[0]+1)&255, 37: (inp[0]+2)&255}');
 // threewaysplit.mbl - 3W
 jsBoard('3W', 1, 1, '{0: inp[0], 36: inp[0], 37: inp[0]}');
 // bitwise_operations.mbl - Bitx, Bdif, Borr, Band, Bxor, Bnor
 jsBoard('Bitx', 2, 1, 'inp[1]<8?{0: !!(inp[0] & (1 << inp[1]))}:{}');
 jsBoard('Bdif', 2, 1, 'b=inp[0]^inp[1],{0: !!(b&1)+!!(b&2)+!!(b&4)+!!(b&8)+!!(b&16)+!!(b&32)+!!(b&64)+!!(b&128)}');
 jsBoard('Borr', 2, 1, '{0: inp[0]|inp[1]}');
 jsBoard('Band', 2, 1, '{0: inp[0]&inp[1]}');
 jsBoard('Bxor', 2, 1, '{0: inp[0]^inp[1]}');
 jsBoard('Bnor', 2, 1, '{0: ~(inp[0]|inp[1])}');
 // logical_operations.mbl - Tf, Nt, Lorr, Land, Lnor, Lxor, Cmpr, Eqal, Gteq, Lteq, Grtr, Less, Sort
 jsBoard('Tf', 1, 1, '{0: (inp[0]>0)|0}');
 jsBoard('Nt', 1, 1, '{0: (!inp[0])|0}');
 jsBoard('Lorr', 2, 1, '{0: (inp[0]||inp[1])|0}');
 jsBoard('Land', 2, 1, '{0: (inp[0]&&inp[1])|0}');
 jsBoard('Lnor', 2, 1, '{0: !(inp[0]||inp[1])|0}');
 jsBoard('Lxor', 2, 1, '{0: (!inp[0]!=!inp[1])|0}');
 jsBoard('Cmpr', 2, 1, '{0: inp[0]>inp[1]?1:inp[0]<inp[1]?-1:0}');
 jsBoard('Eqal', 2, 1, '{0: (inp[0] == inp[1])|0}');
 jsBoard('Gteq', 2, 1, '{0: (inp[0] >= inp[1])|0}');
 jsBoard('Lteq', 2, 1, '{0: (inp[0] <= inp[1])|0}');
 jsBoard('Grtr', 2, 1, '{0: (inp[0] > inp[1])|0}');
 jsBoard('Less', 2, 1, '{0: (inp[0] < inp[1])|0}');
 jsBoard('Sort', 2, 2, '{0: Math.min(inp[0],inp[1]), 1: Math.max(inp[0],inp[1])}');
 // replace_input.mbl - Replac
 jsBoard('Replac', 3, 1, '{0: inp[0]==inp[1]?inp[2]:inp[0]}');
 // adder.mbl - Plus
 jsBoard('Plus', 2, 1, '{0: (inp[0] + inp[1])&255}');
 // arithmetic.mbl - Subt, Subo, Subful, Addo, Addful, Mult
 jsBoard('Subt', 2, 1, '{0: (inp[0] - inp[1])&255}');
 jsBoard('Subo', 2, 1, '{0: (inp[0] - inp[1])&255, 36: (inp[0] < inp[1])|0}');
 jsBoard('Subful', 3, 1, 'a=(inp[0] - inp[1])&255,{0: (a + 256 - inp[2])&255, 36: (inp[0]<inp[1])+(a<inp[2])}');
 jsBoard('Addo', 2, 1, 'a=inp[0]+inp[1],{0: a&255, 36: (a>255)|0}');
 jsBoard('Addful', 3, 1, 'a=inp[0]+inp[1]+inp[2],{0: a&255, 36: (a>255)|0}');
 jsBoard('Mult', 2, 1, '{0: (inp[0]*inp[1])&255}');
 // wide_devices.mbl - Wideadditionfunc, Widesubtractfunc, Wbitleft, Wbitrght, Wbitfetchx, Mulx, Doblmult, Widemultiplyfunc
 jsBoard('Wideadditionfunc', 8, 4, 'c=(wto32(inp,0)+wto32(inp,4))&0xFFFFFFFF,{0:  (c&0xFF000000)>>>24, 1: (c&0x00FF0000)>>>16, 2: (c&0x0000FF00)>>>8, 3: (c&0x000000FF)}');
 jsBoard('Widesubtractfunc', 8, 4, 'c=(wto32(inp,0)-wto32(inp,4))&0xFFFFFFFF,{0: (c&0xFF000000)>>>24, 1: (c&0x00FF0000)>>>16, 2: (c&0x0000FF00)>>>8, 3: (c&0x000000FF)}');
 jsBoard('Wbitleft', 4, 4, 'c=(wto32(inp,0)<<1)&0xFFFFFFFF,{0: (c&0xFF000000)>>>24, 1: (c&0x00FF0000)>>>16, 2: (c&0x0000FF00)>>>8, 3: (c&0x000000FF)}');
 jsBoard('Wbitrght', 4, 4, 'c=wto32(inp,0)>>>1,{0: (c&0xFF000000)>>>24, 1: (c&0x00FF0000)>>>16, 2: (c&0x0000FF00)>>>8, 3: (c&0x000000FF)}');
 jsBoard('Wbitfetchx', 5, 1, 'inp[4]<32?{0:!!(wto32(inp,0)&(1<<inp[4]))}:{}');
 jsBoard('Mulx', 2, 2, 'c=(inp[0]*inp[1])&0xFFFF,{0: (c&0xFF00)>>>8, 1: (c&0x00FF)}');
 jsBoard('Doblmult', 4, 4, 'c=(wto16(inp,0)*wto16(inp,2))&0xFFFFFFFF,{0: (c&0xFF000000)>>>24, 1: (c&0x00FF0000)>>>16, 2: (c&0x0000FF00)>>>8, 3: (c&0x000000FF)}');
 jsBoard('Widemultiplyfunc', 8, 4, 'c=(wto32(inp,0)*wto32(inp,4))&0xFFFFFFFF,{0: (c&0xFF000000)>>>24, 1: (c&0x00FF0000)>>>16, 2: (c&0x0000FF00)>>>8, 3: (c&0x000000FF)}');
}
}
// most devices are implemented as js subboards
var CTypes = {
PORTAL:   1,
SYNCHRONISER: 2,
INPUT:   3,
OUTPUT:   4,
TERMINATE:  5,
SUBROUTINE:  6,
LITERAL:  7,
};
function Cell(type,value){
this.type = type;
this.value = value;
}
Cell.prototype.copy = function(other){
this.type = other.type;
this.value = other.value;
};
function Board(name, js, jsref, jsinc, jsoutc){
this.name = name;
this.stdout = "";
if(!js){
 this.cells = [];
 this.marbles = [];
 this.cols = 0;
 this.rows = 0;
 this.inputs = [];
 this.outputs = [];
 this.syncs = [];
 this.portals = [];
 this.terminators = [];
 for(var i = 0; i < 36; ++i){
  this.inputs[i] = [];
  this.outputs[i] = [];
  this.syncs[i] = [];
  this.portals[i] = [];
 }
 this.outputs[36] = []; // {<
 this.outputs[37] = []; // {>
 this.subroutines = []; // [r0,c0,board name,size,inputs,outputs]
 this.type = 0;
 this.inc = 0; 
 this.outc = 0;
}else{
 this.func = jsref;
 this.inc = jsinc;
 this.outc = jsoutc;
 this.type = 1;
}
}
Board.prototype.set = function(row,col,cell){
if(row >= this.rows){
 for(var r = this.rows; r <= row; ++r){
  this.cells[r] = [];
  this.marbles[r] = [];
 }
}
this.rows = Math.max(this.rows, row + 1);
this.cols = Math.max(this.cols, col + 1);
if(!cell){
 this.cells[row][col] = null;
 return;
}
if(!this.cells[row][col])
 this.cells[row][col] = new Cell;
this.cells[row][col].copy(cell);
if(cell.type == CTypes.LITERAL){
 this.marbles[row][col] = cell.value;
}else{
 this.marbles[row][col] = null;
}
};
Board.prototype.get = function(r,c){
return this.cells[r][c];
};
Board.prototype.init = function(){
if(this.type == 0){
 var maxin = 0, maxout = 0;
 for(var r = 0; r < this.rows; ++r){
  for(var c = 0; c < this.cols; ++c){
   if(this.cells[r][c] == null) continue;
   switch(this.cells[r][c].type){
    case CTypes.PORTAL:
     this.portals[this.cells[r][c].value].push([r,c]);
    break;
    case CTypes.SYNCHRONISER:
     this.syncs[this.cells[r][c].value].push([r,c]);
    break;
    case CTypes.INPUT:
     this.inputs[this.cells[r][c].value].push([r,c]);
     maxin = Math.max(this.cells[r][c].value + 1, maxin);
    break;
    case CTypes.OUTPUT:
     if(this.cells[r][c].value != '<' && this.cells[r][c].value != '>'){
      this.outputs[this.cells[r][c].value].push([r,c]);
      maxout = Math.max(this.cells[r][c].value + 1, maxout);
     }else{
      this.outputs[this.cells[r][c].value == '<' ? 36 : 37].push([r,c]);
     }
    break;
    case CTypes.TERMINATE:
     this.terminators.push([r,c]);
    break;
   }
  }
 }
 this.inc = maxin;
 this.outc = maxout;
}
var namelen = Math.max(1, this.inc, this.outc) * 2;
this.name = new Array(namelen + 1).join(this.name).substr(0, namelen);
};
Board.prototype.validateSubr = function(names){
if(this.type == 1) return;
for(var r = 0, len = this.cells.length; r < len; ++r){
 var str = "", start = -1;
 for(var c = 0, rlen = this.cells[r].length; c < rlen; ++c){
  if(this.cells[r][c] && this.cells[r][c].type == CTypes.SUBROUTINE){
   if(start == -1) start = c;
   str += this.cells[r][c].value;
  }else if(start != -1){
   var match;
   while(str.length && (match = longestMatch(str, names))){
    var slen = match.length / 2;
    this.subroutines.push([r,start,match,slen,boards[match].inc,boards[match].outc]);
    start += slen;
    str = str.substr(slen * 2); 
   }
   if(str.length){
    throw "No subboard could be found near `" + str + "`";
   }
   start = -1;
   str = "";
  }
 }
 var match;
 while(str.length && (match = longestMatch(str, names))){
  var slen = match.length / 2;
  this.subroutines.push([r,start,match,slen,boards[match].inc,boards[match].outc]);
  start += slen;
  str = str.substr(slen * 2);
 }
 if(str.length){
  throw "No subboard could be found near `" + str + "`";
 }
}
};
Board.prototype.runCopy = function(inp){
var b = new Board('');
b.type = 2;
b.ref = this;
b.tickNum = 0;
if(this.type == 0){
 for(var r = 0, rlen = this.marbles.length; r < rlen; ++r){
  b.marbles[r] = [];
  for(var c = 0, clen = this.marbles[r].length; c < clen; ++c){
   b.marbles[r][c] = this.marbles[r][c]; 
  }
 }
 for(var i = 0; i < this.inc; ++i){
  if(inp[i] != null){
   var k = this.inputs[i];
   for(var j = 0, l = k.length; j < l; ++j){
    b.marbles[k[j][0]][k[j][1]] = ((parseInt(inp[i])&255)+256)&255;
   }
  }
 }
 b.cols = this.cols;
 b.rows = this.rows;
 b.inc = this.inc;
 b.outc = this.outc;
 b.stdout = "";
}else{
 b.inp = inp;
}
return b;
};
Board.prototype.tick = function(){
if(this.type != 2) throw "Calling Board.tick without Board.runCopy";
if(this.tickNum == -1) throw "Copied board has already run to completion";

if(this.ref.type == 1){
 this.tickNum = -1;
 this.outp = this.ref.func(this.inp, this);
 return moved;
}

var moved = false;

var new_marbles = [];
for(var r = 0; r <= this.rows; ++r) 
 new_marbles[r] = [];

// subroutines
for(var i = 0, len = this.ref.subroutines.length; i < len; ++i){
 var r = this.ref.subroutines[i][0], c = this.ref.subroutines[i][1], 
  name = this.ref.subroutines[i][2], sz = this.ref.subroutines[i][3],
  inc = this.ref.subroutines[i][4], outc = this.ref.subroutines[i][5];
 var all = true, inp = [];
 for(var j = 0; j < inc; ++j){
  if(this.marbles[r][c+j] == null && (boards[name].type == 1 || boards[name].inputs[j].length > 0)){
   all = false; break;
  }else{
   inp[j] = this.marbles[r][c+j];
  }
 }
 if(all){
  var cb = boards[name].runCopy(inp);
  while(cb.tickNum != -1 && cb.tickNum < max_tick) cb.tick();
  if(cb.tickNum != -1) throw "Max tick count exceeded running board `" + name + "`";
  var outp = cb.out();
  if(cb.stdout != "") moved = true;
  for(var j = 0; j < outc; ++j){
   if(outp[j] != null){
    new_marbles[r+1][c+j] = ((new_marbles[r+1][c+j]||0)+(outp[j]))&255;
    moved = true;
   }
  }
  if(outp[36] != null){ // left
   var left = c-1;
   if(left < 0 && cylindrical_board){
    left = this.cols - 1;
   }
   if(left >= 0){
    new_marbles[r][left] = ((new_marbles[r][left]||0)+(outp[36]))&255;
    moved = true;
   }
  }
  if(outp[37] != null){ // right
   var right = c+sz;
   if(right >= this.cols && cylindrical_board){
    right = 0;
   }
   if(right < this.cols){
    new_marbles[r][right] = ((new_marbles[r][right]||0)+(outp[37]))&255;
    moved = true;
   }
  }
  this.stdout += cb.stdout;
 }else{
  for(var j = 0; j < inc; ++j){
   if(this.marbles[r][c+j] != null){
    new_marbles[r][c+j] = ((new_marbles[r][c+j]||0)+(this.marbles[r][c+j]))&255;
   }     
  }
 }
}

// synchronisers
for(var i = 0; i < 36; ++i){
 if(this.ref.syncs[i].length){
  var all = true;
  for(var j = 0, len = this.ref.syncs[i].length; j < len; ++j){
   if(this.marbles[this.ref.syncs[i][j][0]][this.ref.syncs[i][j][1]] == null){
    all = false; break;
   }
  }
  if(all){
   for(var j = 0, len = this.ref.syncs[i].length; j < len; ++j){
    var r = this.ref.syncs[i][j][0];
    var c = this.ref.syncs[i][j][1];
    new_marbles[r+1][c] = this.marbles[r][c];
    moved = true;
   }
  }else{
   for(var j = 0, len = this.ref.syncs[i].length; j < len; ++j){
    var r = this.ref.syncs[i][j][0], c = this.ref.syncs[i][j][1];
    if(this.marbles[r][c] != null){
     new_marbles[r][c] = ((new_marbles[r][c]||0)+( this.marbles[r][c]))&255;
    }
   }
  }
 }
}

// input literal null move, output does not
for(var r = 0; r < this.rows; ++r){
 for(var c = 0; c < this.cols; ++c){
  if(this.marbles[r][c] != null){
   var type = this.ref.cells[r][c] && this.ref.cells[r][c].type;
   if(!type || type == CTypes.INPUT || type == CTypes.LITERAL){
    new_marbles[r+1][c] = ((new_marbles[r+1][c]||0)+(this.marbles[r][c]))&255;
    moved = true;
   }else if(type == CTypes.OUTPUT){
    new_marbles[r][c] = ((new_marbles[r][c]||0)+(this.marbles[r][c]))&255;
   }
  }
 }
} 

// shift portal
for(var i = 0; i < 36; ++i){
 if(this.ref.portals[i].length){
  var p = this.ref.portals[i];
  if(p.length == 1){
   var r = p[0][0], c = p[0][1];
   if(this.marbles[r][c] != null){
    new_marbles[r+1][c] = ((new_marbles[r+1][c]||0)+( this.marbles[r][c]))&255;
    moved = true;
   }
  }else{
   var q = [];
   for(var j = 0, l = p.length; j < l; ++j){
    if(this.marbles[p[j][0]][p[j][1]] != null){
     // generate output portal - any other portal except the input
     var toWhere = Math.floor(Math.random() * (l-1));
     if(toWhere >= j) ++toWhere;
     var r = p[j][0], c = p[j][1];
     q[toWhere] = ((q[toWhere]||0)+(this.marbles[r][c]))&255;
     moved = true;
    }
   }
   for(var j = 0, l = p.length; j < l; ++j){
    if(q[j] != null){
     var r = p[j][0] + 1, c = p[j][1];
     new_marbles[r][c] = q[j];
    }
   }
  }
 }
}

// check stdout
if(new_marbles[new_marbles.length-1].length){
 var r = this.rows;
 for(var i = 0, l = new_marbles[r].length; i < l; ++i){
  if(new_marbles[r][i] != null){
   putch(this, new_marbles[r][i]);
   moved = true;
  }
 }
}
new_marbles.splice(this.rows);

if(!moved){
 this.tickNum = -1;
 return moved;
}
this.marbles = new_marbles;
// check terminator
for(var i = 0, len = this.ref.terminators.length; i < len; ++i){
 var r = this.ref.terminators[i][0], c = this.ref.terminators[i][1];
 if(new_marbles[r][c] != null){
  this.tickNum = -1;
  return moved;
 }
}
// check output
if(this.outc){
 var allOuts = true;
 for(var i = 0; i < 38; ++i){
  var o = this.ref.outputs[i];
  if(o.length){
   var occupied = false;
   for(var j = 0, len = o.length; j < len; ++j){
    if(new_marbles[o[j][0]][o[j][1]] != null){
     occupied = true;
     break;
    }
   }
   if(!occupied){
    allOuts = false; break;
   }
  }
 }
 if(allOuts){
  this.tickNum = -1;
  return moved;
 }
}
++this.tickNum;
return moved;
};
Board.prototype.out = function(){
if(this.type != 2) throw "Calling Board.out without Board.runCopy";
if(this.tickNum != -1) throw "Copied board hasn't run to completion yet";
if(this.ref.type == 1) return this.outp;
var outp = {};
for(var i = 0; i < 38; ++i){
 if(this.ref.outputs[i].length){
  outp[i] = 0;
  var o = this.ref.outputs[i], isFilled = false;
  for(var j = 0, len = o.length; j < len; ++j){
   var r = o[j][0], c = o[j][1];
   isFilled = isFilled || this.marbles[r][c] != null;
   outp[i] = ((outp[i])+( this.marbles[r][c] || 0))&255;
  }
  if(!isFilled)
   outp[i] = null;
 }
}
return outp;
};

function DrawBuffer(canvas){
this.canvas = canvas;
this.ctx = this.canvas.getContext('2d');
this.ctx.fillStyle = 'rgb(0,0,0)';
this.ctx.fillRect(0, 0, 256, 256);
}

DrawBuffer.prototype.clear = function(){
this.ctx.fillStyle = 'rgb(0,0,0)';
this.ctx.fillRect(0, 0, 256, 256);
};
DrawBuffer.prototype.set = function(x, y, r, g, b){
this.ctx.fillStyle = 'rgb(' + r + ',' + g + ',' + b + ')';
this.ctx.fillRect(x, y, 1, 1);
};
DrawBuffer.prototype.get = function(x, y){
return this.ctx.getImageData(x, y, 1, 1).data.splice(0, 3);
};
// spaces: is an empty cell denoted with whitespace?
function parseMblSrc(src,spaces){
var lines = (':MB\n'+src).split('\n');
var sb = []; // start lines of new mb subboards
for(var i = lines.length; i-- > 0;){
 // strip mid-line spaces if possible
 if(!spaces){
  lines[i] = lines[i].trim();
  lines[i] = lines[i].replace(/\s/g,'');
 }
 // index of comment
 var o = lines[i].indexOf('#');
 // remove comments
 if(o > 0){
  lines[i] = lines[i].substr(0, o).trim();
 }else if(lines[i].length == 0 || o == 0){
  if(lines[i].indexOf('include') == 1){
   throw "#include not supported";
  }
  lines.splice(i, 1);
 }
}
for(var i = lines.length; i-- > 0;){
 // identify subboards
 if(lines[i].charAt(0) == ':'){
  sb.push(i);
 }
}
sb.sort(function(a,b){return a-b;});
var mbname = '';
for(var i = 0, len = sb.length; i < len; ++i){
 var name = lines[sb[i]].substr(1).trim();
 var board;
 board = new Board(name, false);
 var endl;
 if(i == len - 1) endl = lines.length;
 else endl = sb[i+1];
 for(var j = sb[i]+1; j < endl; ++j){
  var r = j - sb[i] - 1;
  if(lines[j].length % 2 != 0){
   throw "Error near `" + lines[j] + "`";
  }
  var cells = lines[j].match(/../g);
  for(var k = 0, clen = cells.length; k < clen; ++k){
   var val;
   if(val = cells[k].match(/^@([0-9A-Z])$/)){
    board.set(r, k, new Cell(CTypes.PORTAL, parseInt(val[1], 36)));
   }else if(val = cells[k].match(/^&([0-9A-Z])$/)){
    board.set(r, k, new Cell(CTypes.SYNCHRONISER, parseInt(val[1], 36)));
   }else if(val = cells[k].match(/^}([0-9A-Z])$/)){
    board.set(r, k, new Cell(CTypes.INPUT, parseInt(val[1], 36)));
   }else if(val = cells[k].match(/^{([0-9A-Z<>])$/)){
    var value;
    if(val[1] == '<' || val[1] == '>') value = val[1];
    else value = parseInt(val[1], 36);
    board.set(r, k, new Cell(CTypes.OUTPUT, value));
   }else if(cells[k] == '!!'){
    board.set(r, k, new Cell(CTypes.TERMINATE, 0));
   }else if(val = cells[k].match(/^[0-9A-F]{2}$/)){
    board.set(r, k, new Cell(CTypes.LITERAL, parseInt(cells[k], 16)));
   }else if(cells[k].match(/^[ \.]{2}$/)){
    board.set(r, k, null);
   }else{
    board.set(r, k, new Cell(CTypes.SUBROUTINE, cells[k]));
   }
  }
 }
 board.init();
 if(name == 'MB')
  mbname = board.name;
 name = board.name;
 bnames.push(name);
 boards[name] = board;
}
// validate and connect subr
for(var p in boards){
 boards[p].validateSubr(bnames);
}
return mbname;
}
function run(){
// normal init
stopped = false;
max_tick = document.getElementById('mb-ticks').value;
space_as_blank = document.getElementById('mb-spaces').checked;
cylindrical_board = document.getElementById('mb-cylinder').checked;
print_numbers = document.getElementById('mb-numprint').checked;
libraries = document.getElementById('mb-lib').checked;
gfx = document.getElementById('mb-gfx').checked;

src = document.getElementById('mb-src').value;
stdin = document.getElementById('mb-in').value;
boards = {};
bnames = [];
loadDefaultBoards();

if(gfx){
 // prepare draw buffers
 front_buffer = new DrawBuffer(document.getElementById('mb-fb'));
 back_buffer = new DrawBuffer(document.getElementById('mb-bb'));
}else{
 if(front_buffer){
  front_buffer.clear();
  back_buffer.clear();
  front_buffer = null;
  back_buffer = null;
 }
}
try{
 var mb = parseMblSrc(src, space_as_blank);
 var rc = boards[mb].runCopy(document.getElementById('mb-args').value.split(' '));
 var tmp = function(){
  try{
   if(stopped) throw "Board execution stopped.";
   else if(rc.tickNum != -1 && rc.tickNum < max_tick){
    rc.tick();
    document.getElementById('mb-out').value = rc.stdout;
    document.getElementById('mb-out').scrollTop = document.getElementById('mb-out').scrollHeight;
    setTimeout(tmp, 0);
   }else if(rc.tickNum == -1){
    document.getElementById('mb-out').value = rc.stdout;
    document.getElementById('mb-out').scrollTop = document.getElementById('mb-out').scrollHeight;
    document.getElementById('mb-return').value = rc.out()[0] || 0;
   }else throw "Max tick count exceeded running main board.";
  }catch(e){
   document.getElementById('mb-out').value = rc.stdout + '\n' + e;
   document.getElementById('mb-out').scrollTop = document.getElementById('mb-out').scrollHeight;
  }
 };
 setTimeout(tmp, 0);
}catch(e){
 document.getElementById('mb-out').value = (rc ? rc.stdout: '') + '\n' + e;
 document.getElementById('mb-out').scrollTop = document.getElementById('mb-out').scrollHeight;
}
}
function stop(){
stopped = true;
}
<div style="font-size: 12px;font-family: Helvetica, Arial, sans-serif;">
<div style="float: left;">
 Marbelous Source:<br />
 <textarea id="mb-src" rows="4" cols="35">48 65 6C 6C 6F 20 57 6F 72 6C 64 21</textarea><br />
 Arguments: <br />
 <input id="mb-args" size="33" /> <br />
 STDIN: <br />
 <textarea id="mb-in" rows="2" cols="35"></textarea><br />
 <span style="float: left">
  Max Ticks: <input id="mb-ticks" type="number" min="1" style="width: 60px" value="1000" />
 </span>
 <span style="float: right">
  <input type="button" onclick="run()" value="Run Code" style="" />
  <input type="button" onclick="stop()" value="Stop" style="" />
 </span>
</div>
<div style="float: left; margin-left: 15px;">
 <div style="margin-bottom: 10px">
  <input type="checkbox" id="mb-spaces" />Using spaces for blank cells<br />
  <input type="checkbox" id="mb-cylinder" />Cylindrical Board<br />
  <input type="checkbox" id="mb-numprint" />Display output as decimal numbers<br />
  <input type="checkbox" id="mb-lib" checked="true" />Include Libraries<br />
  <input type="checkbox" id="mb-gfx" />Use Draw Buffers<br />
 </div>
 <div>
  STDOUT:<br />
  <textarea id="mb-out" rows="3" cols="35"></textarea><br />
  Return Code: <input id="mb-return" />
 </div>
</div>
<div style="clear: both; text-align: center; width: 550px; padding-top: 10px;" id="mb-draw-buffers">
 <div style="float: left; width: 256px;">
  Front Buffer:
  <canvas style="height: 256px; width: 256px; background: black;" width="256" height="256" id="mb-fb"></canvas>
 </div>
 <div style="float: right; width: 256px;">
  Back Buffer:
  <canvas style="height: 256px; width: 256px; background: black;" width="256" height="256" id="mb-bb"></canvas>
 </div>
</div>
</div>

A few simple programs to try:

cat (prints stdin to stdout)

.. @0 ..
00 ]] \/
@0 /\ ..

rot13 (reads from stdin)

.. @0 ..
00 ]] \/
@0 /\ Rt
:Rt
}0 .. @0 .. }0 }0 }0 }0 }0
-W .. -W .. .. +D -D +D -D
-X .. >C &3 &0 &1 &2 &3 &4
>C &1 >P &4 {0 {0 {0 {0 {0
>P &2 &0 \/ .. .. .. .. ..
@0 \/ \/ .. .. .. .. .. ..

Graphical Output Demo (sets a random pixel to a random color and then flips buffer)

FF FF FF FF FF 00
?? ?? ?? ?? ?? ..
{} {} {} {} {} ><

es1024

Posted 2014-10-21T02:41:05.133

Reputation: 8 953

The last demo gives an error Error near FF FF FF FF FF 00 – Khaled.K – 2017-04-11T10:20:45.917

This is awesome, it seems boards with single letter names don't work though. – overactor – 2014-11-07T16:43:11.137

@overactor that should be fixed now – es1024 – 2014-11-08T04:26:01.373

17

JavaScript (sample answer)

Interpreting JavaScript using JavaScript is somewhat useless, but this shows what your interpreter snippets should roughly behave like.

Here is the bare bones version, intended for quick use in other posts:

BEGIN_CODE
// Put JavaScript code here
// it may be on multiple lines
END_CODE_BEGIN_INPUT
Put input here
it may be on multiple lines
END_INPUT
<!--Interpreter code:--><script>i=document.body.innerHTML;document.body.innerHTML=''</script><script>c=i.match(/BEGIN_CODE\n([\s\S]*)\nEND_CODE_BEGIN_INPUT/);c=c&&c.length>1?c[1]:'// Error reading code. Make sure it is on the lines between BEGIN_CODE and END_CODE_BEGIN_INPUT.';i=i.match(/END_CODE_BEGIN_INPUT\n([\s\S]*)\nEND_INPUT/);i=i&&i.length>1?i[1]:'Error reading input. Make sure it is on the lines between END_CODE_BEGIN_INPUT and END_INPUT.'</script>JavaScript:<br><tt>function(x) {</tt><br><textarea id='c'rows='4'cols='60'></textarea><br><tt>}</tt><br><br>Input string (variable x):<br><textarea id='i'rows='4'cols='60'></textarea><p><input type='button'value='Run'onclick='r()'></p>Output (return value):<br><textarea id='o'rows='4'cols='60'style='background-color:#f0f0f0;'></textarea><script>document.getElementById('c').value=c;document.getElementById('i').value=i;function r(){var r,c,i;r=c=i=null;eval('var f=function(x) {'+document.getElementById('c').value+'}');document.getElementById('o').value=String(f(document.getElementById('i').value))}</script>

The raw Markdown for including this Stack Snippet in another question or answer is:

<!-- begin snippet: js hide: false -->
<!-- language: lang-html -->
BEGIN_CODE
// Put JavaScript code here
// it may be on multiple lines
END_CODE_BEGIN_INPUT
Put input here
it may be on multiple lines
END_INPUT
<!--Interpreter code:--><script>i=document.body.innerHTML;document.body.innerHTML=''</script><script>c=i.match(/BEGIN_CODE\n([\s\S]*)\nEND_CODE_BEGIN_INPUT/);c=c&&c.length>1?c[1]:'// Error reading code. Make sure it is on the lines between BEGIN_CODE and END_CODE_BEGIN_INPUT.';i=i.match(/END_CODE_BEGIN_INPUT\n([\s\S]*)\nEND_INPUT/);i=i&&i.length>1?i[1]:'Error reading input. Make sure it is on the lines between END_CODE_BEGIN_INPUT and END_INPUT.'</script>JavaScript:<br><tt>function(x) {</tt><br><textarea id='c'rows='4'cols='60'></textarea><br><tt>}</tt><br><br>Input string (variable x):<br><textarea id='i'rows='4'cols='60'></textarea><p><input type='button'value='Run'onclick='r()'></p>Output (return value):<br><textarea id='o'rows='4'cols='60'style='background-color:#f0f0f0;'></textarea><script>document.getElementById('c').value=c;document.getElementById('i').value=i;function r(){var r,c,i;r=c=i=null;eval('var f=function(x) {'+document.getElementById('c').value+'}');document.getElementById('o').value=String(f(document.getElementById('i').value))}</script>
<!-- end snippet -->

To use it simply copy it into a new post and put your code on the lines between BEGIN_CODE and END_CODE_BEGIN_INPUT, and your input on the lines between END_CODE_BEGIN_INPUT and END_INPUT. The interpreter will do the rest. Note that nothing needs to be indented.

I would argue that this BEGIN...END technique is presently the best way we have to write these interpreters in terms of ease of future use because...

  • When rendered there is only one code block (the HTML one) and only one line of it is "extra".
  • The user writing the snippet can see exactly where to place their code and input. They don't have to search for the appropriate textarea tag.
  • Users reading the post can easily distinguish the code without even running the snippet. Of course when the snippet is run the code is obvious.
  • It only breaks when the begin/end identifiers appear on their own lines within the code or the input. But this is unlikely, and they can be changed if absolutely necessary.
  • It is self contained. All the code is right there. (I know this might be impossible for a larger interpreter but one of the points of Stack Snippets was to combat dead links to JSFiddle and such, so self-containedness is good.)
  • It could be expanded to suit any number of (text based) inputs.

The snippet below is the non-minimized general version, complete with example and a bit more styling.

function example() {
  document.getElementById('code').value = "var lines = x.split('\\n')\nvar sum = 0\nfor (var i = 0; i < lines.length; i++) {\n  var val = parseInt(lines[i])\n  if (!isNaN(val)) {\n    sum += val\n  }\n}\nreturn sum"
  document.getElementById('input').value = '1\n2\n3'
  run()
}

function run() {
  var run = null, example = null; //shadow other globals
  eval('var f=function(x) {' + document.getElementById('code').value + '}')
  document.getElementById('output').value = String(f(document.getElementById('input').value))
}
#output {
  background-color: #f0f0f0;
}

#run {
  font-size: 120%;
}
JavaScript:<br>
<tt>function(x) {</tt>
<br>
<textarea id='code' rows='12' cols='60'></textarea>
<br><tt>}</tt><br><br>
Input string (variable x):
<br>
<textarea id='input' rows='6' cols='60'></textarea>
<p><input id='run' type='button' value='Run' onclick='run()'></p>
Output (return value):
<br>
<textarea id='output' rows='6' cols='60'></textarea>
<p><a id='example' href='javascript:void(0)' onclick='example()'>Example: Sum Numbers</a></p>

Calvin's Hobbies

Posted 2014-10-21T02:41:05.133

Reputation: 84 000

15

Brainfuck (with basic debugger)

I saw the brainfuck answer and thought: you know what would be awesome? If I could pretend I understand what brainfuck code is doing. And hence I made a basic brainfuck interpreter and debugger. The debugger is SLOW, but it works pretty well, albeit I havent implemented breakpoints as of now (on the todo list).

Note: the Brainfuck "virtual machine" doesnt auto reset after running the code. Press reset manually or the tape will have the same values on it that existed at the end of the last program run.

The debugger works best in full screen.

var mycode = "++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.";

function Brainfuck()    {
    this.cell_max = 255;
    this.read = function()    {return 0;}
    this.reset();
}

Brainfuck.prototype.reset = function()    {
    this.stack = [0];
    this.stackptr = 0;
    
    this.printbuffer = "";
}

Brainfuck.prototype.setCode = function(code)    {
    this.code = code;
    this.codeptr = 0;
    
    this.loop_map = {};
    var loops = [];
    for(var i = 0; i < this.code.length; i++)    {
        if(this.code[i] == '[')    {
            loops.push(i);
        }    else if(this.code[i] == ']')    {
            var last = loops.pop();
            this.loop_map[last] = i;
            this.loop_map[i] = last;
        }
    }
}

Brainfuck.prototype.done = function()    {
    return !(this.codeptr < this.code.length);
}

Brainfuck.prototype.step = function()    {
    switch(this.code.charAt(this.codeptr))    {
        case '>':
            if(++this.stackptr >= this.stack.length)    {
                this.stack.push(0);
            }
            break;
        case '<':
            if(--this.stackptr < 0)    {
                this.stackptr = this.stack.length - 1;
            }
            break;
        case '+':
            if(++this.stack[this.stackptr] > this.cell_max)    {
                this.stack[this.stackptr] = 0;
            }
            break;
        case '-':
            if(--this.stack[this.stackptr] < 0)    {
                this.stack[this.stackptr] = this.cell_max;
            }
            break;
        case '.':
            this.print();
            break;
        case ',':
            this.stack[this.stackptr] = this.read();
            break;
        case '[':
            if(this.stack[this.stackptr] == 0)    {
                this.codeptr = this.loop_map[this.codeptr];
            }
            break;
        case ']':
            if(this.stack[this.stackptr] != 0)    {
                this.codeptr = this.loop_map[this.codeptr];
            }
            break;
        default:
            
    }
    while(!this.done() && '><+-.,[]'.indexOf(this.code[++this.codeptr]) == -1);
}

Brainfuck.prototype.print = function()    {
    this.printbuffer += String.fromCharCode(this.stack[this.stackptr]);
}

// UI File

var $ = document.getElementById.bind(document);
var $_ = document.createElement.bind(document);

String.prototype.decodeHTML = function() {
    var map = {"gt":">", "lt":"<", "nbsp":""};
    return this.replace(/&(#(?:x[0-9a-f]+|\d+)|[a-z]+);?/gi, function($0, $1) {
        if ($1[0] === "#") {
            return String.fromCharCode($1[1].toLowerCase() === "x" ? parseInt($1.substr(2), 16)  : parseInt($1.substr(1), 10));
        } else {
            return map.hasOwnProperty($1) ? map[$1] : $0;
        }
    });
}

var bf = new Brainfuck();

var setup = function()    {
    bf.setCode($('code').value.decodeHTML());
    bf.read = function()    {
        var stdin = $('stdin').value;
        if(bf.read.index < stdin.length)    {
            return stdin[bf.read.index++].charCodeAt(0);
        }
        return 0;
    }
    bf.read.index = 0;
}

var run = function()    {
    setup();
    
    var stdout = $('stdout');
    var func = function()    {
        for(var i = 0; i < 500000 && !bf.done(); i++)    {
            bf.step();
        }
        stdout.innerHTML = bf.printbuffer;
        setTimeout(func, 0);
    };
    func();
}

var debug = function()    {
    $('debug-area').style.display = 'block';

    setup();
    
    var running = $('running-code');
        for(var i = 0; i < bf.code.length; i++)    {
            var s = $_('span');
            s.innerHTML = bf.code[i];
            s.style.fontFamily = "Courier";
            running.appendChild(s);
        }
        running.children[bf.codeptr].setAttribute('class', 'selected');
    
    genStackTable();
}

var genStackTable = function()    {
    var stable = $('stack-table');
    var head = stable.createTHead();
        var row = head.insertRow(0);
            row.insertCell(0).innerHTML = 'ptr';
            row.insertCell(1).innerHTML = 'addr';
            row.insertCell(2).innerHTML = 'value';
            row.insertCell(3).innerHTML = 'ASCII';
    var setStackVal = function(i, num, ascii, changed)    {
        return changed === num ? function() {
            ascii.value = String.fromCharCode(num.value);
            bf.stack[i] = num.value;
        } : function()    {
            num.value = ascii.value.charCodeAt(0);
            bf.stack[i] = num.value;
        }
    }
    for(var i in bf.stack)    {
        row = stable.insertRow();
        row.insertCell(0).innerHTML = i == bf.stackptr ? '>' : '';
        row.insertCell(1).innerHTML = i;
        var cell = row.insertCell(2);
            var numinput = $_('input');
                numinput.type = 'number';
                numinput.value = bf.stack[i];
            cell.appendChild(numinput);
        cell = row.insertCell(3);
            var asciiinput = $_('input');
                asciiinput.type = 'text';
                asciiinput.value = String.fromCharCode(bf.stack[i]);
                asciiinput.setAttribute('maxlength', 1);
            cell.appendChild(asciiinput);
        
        numinput.oninput = setStackVal(i, numinput, asciiinput, numinput);
        asciiinput.oninput = setStackVal(i, numinput, asciiinput, asciiinput);
    }
}

var debugRun = function()    {
    debugRun.pause = false;
    var func = function()    {
        step();
        if(!bf.done() && !debugRun.pause)    {
            setTimeout(func, $('step-duration').value);
        }
    }
    func();
}

var debugPause = function()    {
    debugRun.pause = true;
}

var step = function()    {
    var running = $('running-code');
    running.children[bf.codeptr].setAttribute('class', '');
    bf.step();
    $('stack-table').innerHTML = '';
    genStackTable();
    if(!bf.done())    {
        running.children[bf.codeptr].setAttribute('class', 'selected');
    }
    $('stdout').innerHTML = bf.printbuffer;
}

var reset = function()    {
    bf.reset();
    bf.read.index = 0;
    $('stdout').innerHTML = '';
    $('running-code').innerHTML = '';
    $('stack-table').innerHTML = '';
    $('debug-area').style.display = 'none';
}

var setStackSize = function()    {
    var sel = $('stackSize');
    bf.cell_max = parseInt(sel.options[sel.selectedIndex].value);
}

$('code').value = mycode;
input    {
    box-sizing:border-box;
    -moz-box-sizing:border-box;
}
div.textarea {
    -moz-appearance: textfield-multiline;
    -webkit-appearance: textarea;
    border: 1px solid gray;
    font: -webkit-small-control;
    font-family:Courier;
    height: 28px;
    overflow: auto;
    padding: 2px;
    resize: both;
    width: 200px;
    white-space:pre;
}
#debug-area    {
    display:none;
}
span.selected    {
    background-color:red;
}
<div>
    <h1>Brainfuck</h1>

    <div>
        <div>Code</div>
        <textarea id="code"></textarea>
    </div>
    <div>
        <div>Stdin</div>
        <textarea id="stdin"></textarea>
    </div>
    <div>
        <div>Stdout</div>
        <div id="stdout" class="textarea"></div>
    </div>
    <div>
        <span>Cell Size</span>
        <select id="stackSize" onchange="setStackSize()">
            <option value="255">8-bit</option>
            <option value="65535">16-bit</option>
            <option value="4294967296">32-bit</option>
        </select>
    </div>
    <div>
        <button id="run" onclick="run()">Run</button>
        <button id="debug" onclick="debug()">Debug</button>
        <button id="reset" onclick="reset()">Reset</button>
    </div>
    <div id="debug-area">
        <h2>Debug</h2>
        <div>
            <button onclick="debugRun()">Run</button>
            <button onclick="debugPause()">Pause</button>
            <span>Time between instructions</span>
            <input id="step-duration" type="number" value=5 />
   <br/>
            <button onclick="step()">Step</button>
        </div>
        <div>
            <h3>Executing Code</h3>
            <div id="running-code"></div>
        </div>
        <div id="stack">
            <h3>Stack</h3>
            <table id="stack-table"></table>
        </div>
    </div>
</div>

TODO:

  • add breakpoints
  • add range check to stack changing
  • progress indicator
  • make it look better
  • add rewind for debugging
  • change stdin to some sort of stream?
  • golf code a bit
  • ?

Usage:

To use in a snippet somewhere on the site, change the first line of the snippet to include your code as "mycode".

To run code without debugger, simply press run. It buffers output for half a million instructions before updating stdout.

To debug, start by pressing the debug button. The step button will step to the next instruction. The run button will run the entire program, but one step at a time, updating the stack table every step (which is why this is so slow). The time between instructions is determined by the input box with that name. The pause button pauses execution of the code. It can be resumed by pressing Run again.

All the values on the stack can be changed live to whatever you want. Be careful though, it doesnt range check if you change the integer value in the cell, so if you put in 300 with an 8-bit stack size, it doesnt really care and wont correct it until the next time it increments.

pseudonym117

Posted 2014-10-21T02:41:05.133

Reputation: 1 053

This is pretty neat. I love the debugger! – Ingo Bürk – 2014-10-31T15:58:14.363

14

Japt

Japt (Javascript shortened) is a language I designed in September 2015. Due to being lazy busy, I didn't create an interpreter until early November. You can find the official interpreter and docs here.

Note: If you have any questions, suggestions, or bug reports, please post them in the Japt chatroom.

* { font-size:12px; font-family: Verdana, Geneva, sans-serif; } textarea { overflow: scroll; overflow-x: hidden; width: 90%; } table { float: left; margin-right: 6px; } table, th, td { border: 1px solid lightgrey; border-collapse: collapse; padding: 3px; } .mono { font-family: Fira Mono, Consolas, DejaVu Sans Mono, Inconsolata, Courier New, monospace; font-size: 10p; } #stderr { color: red; }
<script src="https://rawgit.com/ETHproductions/Japt/master/dependencies/shoco.js"></script>
<script src="https://rawgit.com/ETHproductions/Japt/master/src/japt-interpreter.js"></script>
Code:<br>
<textarea id="code" class="mono" rows="6">[U,V*Wg0 ,Wg1]qS</textarea><br>
Input:<br>
<textarea id="input" class="mono" rows="2">"abc" 13 [9 "zyx"]</textarea>
<!-- <p>Timeout: <input id="timeout" type="checkbox" checked="checked"></p> -->
<p>
  <input id="run" type="button" value="Run" onclick="Japt.run(document.getElementById('code').value, document.getElementById('input').value, false, null, function(x){Japt.output(x)}, function(x){Japt.error(x)})">
  <input id="stop" type="button" value="Stop" onclick="Japt.stop()">
  <input id="clear" type="button" value="Clear" onclick="Japt.clear_output()">
  <span id="stderr"></span>
</p>
Output:<br>
<textarea id="output" class="mono" rows="6" readonly></textarea>
<p id="stderr"></p>
<script> Japt.stdout = document.getElementById("output"); Japt.stderr = document.getElementById("stderr"); </script>

Fun fact: this was originally the main interpreter (and in fact the only interpreter). You can see this if you look through the revision history.

ETHproductions

Posted 2014-10-21T02:41:05.133

Reputation: 47 880

Does Japt have a Github? – Downgoat – 2015-11-17T03:59:34.467

@Vɪʜᴀɴ Not yet, but I'll set one up in the next 24 hours. – ETHproductions – 2015-11-18T02:56:19.107

2

@Vɪʜᴀɴ I should've said 24 minutes. ;) Here you go. Readme is on the way.

– ETHproductions – 2015-11-18T03:24:39.253

1You should use rawgit.com instead of raw.githubusercontent.com - Chrome doesn't like the MIME type stuff. – Mama Fun Roll – 2015-11-25T02:08:13.923

@ןnɟuɐɯɹɐןoɯ Thanks, that saved a few bytes! – ETHproductions – 2015-11-25T03:09:20.020

How would I convert input number to string? (i.e. input is 314151 and I want to split the input by 1: Uq1. But this only works if I wrap the input in quotes) – nicael – 2015-11-29T15:50:50.897

1@nicael You can use Us q1, I believe. – ETHproductions – 2015-11-29T16:30:39.337

Ah, thanks. I was looking for it in the wrong table. – nicael – 2015-11-29T16:37:26.853

A bug: Np0 squares N, but it should produce 1. – nicael – 2015-12-30T14:41:06.433

@nicael OK, will fix soon – ETHproductions – 2015-12-30T14:41:48.223

How to replace without g flag? – nicael – 2016-01-01T21:34:56.793

@nicael Try r"regex""replacement"P. The third param is the regex flags, so this basically means no regex flags. Can't test right now because I'm away from computer. – ETHproductions – 2016-01-02T02:50:17.400

No, nothing changes :( – nicael – 2016-01-02T07:29:14.253

Why does "this is a "+T?"test: produce test and strips anything before +? – nicael – 2016-01-02T08:56:03.117

@nicael Because the JS code is basically ("this is a "+T) ? "test" : "". Try "this is a "+(T?"test: I'll take a look into the other problem when I'm back on my computer. – ETHproductions – 2016-01-02T19:07:37.123

Haha, didn't read it properly :D – nicael – 2016-01-02T19:14:55.137

ceil and floor don't work. Throw "can't find variable: x" – nicael – 2016-01-06T20:13:21.913

@nicael Thanks for the bug report. I thought I'd fixed this, but I'll look into it again. – ETHproductions – 2016-01-06T21:47:56.323

Please post any questions, suggestions, or bug reports in the Japt chatroom. – ETHproductions – 2016-01-10T04:08:37.857

13

FRACTRAN

An semi-optimised interpreter for my favourite esoteric language! Unfortunately I don't have much time this week, so additional features will have to come later.

var ITERS_PER_SEC = 100000
var TIMEOUT_MILLISECS = 5000

var ERROR_INPUT = "Invalid input"
var ERROR_PARSE = "Parse error: "
var ERROR_TIMEOUT = "Timeout"
var ERROR_INTERRUPT = "Interrupted by user"

var running, instructions, registers, timeout, start_time, iterations;

function clear_output() {
  document.getElementById("output").value = ""
  document.getElementById("stderr").innerHTML = ""
}

function stop() {
  running = false
  document.getElementById("run").disabled = false
  document.getElementById("stop").disabled = true
  document.getElementById("clear").disabled = false
}

function interrupt() {
  error(ERROR_INTERRUPT)
}

function error(msg) {
  document.getElementById("stderr").innerHTML = msg
  stop()
}

function factorise(n) {
  var factorisation = {}
  var divisor = 2
  
  while (n > 1) {
    if (n % divisor == 0) {
      var power = 0
      
      while (n % divisor == 0) {
        n /= divisor
        power += 1
      }
      
      if (power != 0) {
        factorisation[divisor] = power
      }
    }
    
    divisor += 1
  }
  
  return factorisation
}

function fact_accumulate(fact1, fact2) {
  for (var reg in fact2) {
    if (reg in fact1) {
      fact1[reg] += fact2[reg]
    } else {
      fact1[reg] = fact2[reg]
    }
  } 
  
  return fact1
}

function exp_to_fact(expression) {
  expression = expression.trim().split(/\s*\*\s*/)
  var factorisation = {}
  
  for (var i = 0; i < expression.length; ++i) {
    var term = expression[i].trim().split(/\s*\^\s*/)
    
    if (term.length > 2) {
      throw "error"
    }
    
    term[0] = parseInt(term[0])
    
    if (isNaN(term[0])) {
      throw "error"
    }
    
    if (term.length == 2) {
      term[1] = parseInt(term[1])
      
      if (isNaN(term[1])) {
        throw "error"
      }
    }    
    
    if (term[0] <= 1) {
      continue 
    }
    
    var fact_term = factorise(term[0])
    
    if (term.length == 2) {
      for (var reg in fact_term) {
        fact_term[reg] *= term[1] 
      }
    }
    
    factorisation = fact_accumulate(factorisation, fact_term)
  }
  
  return factorisation
}

function to_instruction(n, d) {
  instruction = []
  divisor = 2
  
  while (n > 1 || d > 1) {
    if (n % divisor == 0 || d % divisor == 0) {
       reg_offset = 0
       
       while (n % divisor == 0) {
         reg_offset += 1
         n /= divisor
       }
      
       while (d % divisor == 0) {
         reg_offset -= 1
         d /= divisor
       }
      
      if (reg_offset != 0) {
        instruction.push(Array(divisor, reg_offset))
      }
    }
    
    divisor += 1
  }
  
  return instruction
}

function run() {
  clear_output()
  
  document.getElementById("run").disabled = true
  document.getElementById("stop").disabled = false
  document.getElementById("clear").disabled = true
  
  timeout = document.getElementById("timeout").checked

  var code = document.getElementById("code").value
  var input = document.getElementById("input").value
  
  instructions = []
  
  code = code.trim().split(/[\s,]+/)
  
  for (i = 0; i < code.length; ++i){
    fraction = code[i]
    
    split_fraction = fraction.split("/")
    
    if (split_fraction.length != 2){
      error(ERROR_PARSE + fraction)
      return
    }
    
    numerator = parseInt(split_fraction[0])
    denominator = parseInt(split_fraction[1])
    
    if (isNaN(numerator) || isNaN(denominator)){
      error(ERROR_PARSE + fraction)
      return
    }
    
    instructions.push(to_instruction(numerator, denominator))
  }
  
  try {
    registers = exp_to_fact(input)
  } catch (err) {
    error(ERROR_INPUT)
    return
  }
  
  running = true
  iterations = 0
  start_time = Date.now()
  
  fractran_iter(1)
}

function regs_to_string(regs) {
  reg_list = Object.keys(regs)
  reg_list.sort(function(a,b){ return a - b })
  
  out_str = []
  
  for (var i = 0; i < reg_list.length; ++i) {
    if (regs[reg_list[i]] != 0) {
      out_str.push(reg_list[i] + "^" + regs[reg_list[i]])
    }
  }
  
  out_str = out_str.join(" * ")  
  
  if (out_str == "") {
    out_str = "1"
  }
  
  return out_str
}

function fractran_iter(niters) {
  if (!running) {
    stop()
    return
  }
  
  var iter_start_time = Date.now()
  
  for (var i = 0; i < niters; ++i)
  {
    program_complete = true
    
    for (var instr_ptr = 0; instr_ptr < instructions.length; ++instr_ptr){
      instruction = instructions[instr_ptr]
      perform_instr = true
    
      for (var j = 0; j < instruction.length; ++j)
      {
        var reg = instruction[j][0]
        var offset = instruction[j][1]
        
        if (registers[reg] == undefined) {
          registers[reg] = 0
        }
        
        if (offset < 0 && registers[reg] < -offset) {
          perform_instr = false
          break
        }         
      }
      
      if (perform_instr) {
        for (var j = 0; j < instruction.length; ++j)
        {
           var reg = instruction[j][0]
           var offset = instruction[j][1]
           
           registers[reg] += offset
        }
        
        program_complete = false
        break
      }
    }
    
    if (program_complete) {
      document.getElementById("output").value += regs_to_string(registers)
      stop()
      return
    }
    
    iterations++
    
    if (timeout && Date.now() - start_time > TIMEOUT_MILLISECS) {
      error(ERROR_TIMEOUT)
      return
    }
  }
  
  setTimeout(function() { fractran_iter(ITERS_PER_SEC * (Date.now() - iter_start_time)/1000) }, 0)
}
<div style="font-size:12px;font-family:Verdana, Geneva, sans-serif;">
  <div style="float:left; width:50%;">
    Code:
    <br>
    <textarea id="code" rows="4" style="overflow:scroll;overflow-x:hidden;width:90%;">37789/221 905293/11063 1961/533 2279/481 57293/16211 2279/611 53/559 1961/403 53/299 13/53 1/13 6557/262727 6059/284321 67/4307 67/4661 6059/3599 59/83 1/59 14279/871933 131/9701 102037079/8633 14017/673819 7729/10057 128886839/8989 13493/757301 7729/11303 89/131 1/89 31133/2603 542249/19043 2483/22879 561731/20413 2483/23701 581213/20687 2483/24523 587707/21509 2483/24797 137/191 1/137 6215941/579 6730777/965 7232447/1351 7947497/2123 193/227 31373/193 23533/37327 5401639/458 229/233 21449/229 55973/24823 55973/25787 6705901/52961 7145447/55973 251/269 24119/251 72217/27913 283/73903 281/283 293/281 293/28997 293/271 9320827/58307 9831643/75301 293/313 28213/293 103459/32651 347/104807 347/88631 337/347 349/337 349/33919 349/317 12566447/68753 13307053/107143 349/367 33197/349 135199/38419 389/137497 389/119113 389/100729 383/389 397/383 397/39911 397/373 1203/140141 2005/142523 2807/123467 4411/104411 802/94883 397/401 193/397 1227/47477 2045/47959 2863/50851 4499/53743 241/409 1/241 1/239
</textarea>
    <br>Input:
    <br>
    <textarea id="input" rows="2" style="overflow:scroll;overflow-x:hidden;width:90%;">2^47 * 193</textarea>
    <p>
      Timeout:
      <input id="timeout" type="checkbox" checked="true"></input>
    </p>
  </div>
  <div style="float:left; width:50%;">
    Output:
    <br>
    <textarea id="output" rows="6" style="overflow:scroll;width:90%;"></textarea>
    <p>
      <input id="run" type="button" value="Run" onclick="run()"></input>
      <input id="stop" type="button" value="Stop" onclick="interrupt()" disabled="true"></input>
      <input id="clear" type="button" value="Clear" onclick="clear_output()"></input>
      &nbsp;
      <span id="stderr" style="color:red"></span>
    </p>
  </div>
</div>

The example program above is my four squares together program - it takes input 2n * 193, and outputs 3a * 5b * 7c * 11d, such that a2 + b2 + c2 + d2 = n.

To-do

  • Snippet generator
  • Allow for factorisation notation for fractions too
  • Less terrible input validation
  • Clean up code to be better
  • Allow for printing of registers under special conditions (e.g. only register 2 is set, in the case of Conway's prime generator). Currently only programs which terminate produce any output.
  • Performance optimisations?

Usage

  • Fractions in the code are split by [\s,]+, allowing you to separate fractions by newlines, spaces or commas (or tabs, or even any combination of the above, if you're insane)
  • Input can be in factorisation notation, e.g. 2^5 * 3^7 * 11
  • Snippet generator coming some time later

Sp3000

Posted 2014-10-21T02:41:05.133

Reputation: 58 729

1

Needs more semicolons. ASI is the work of devil.

– nyuszika7h – 2014-12-30T09:53:37.200

10

Prelude

Maybe I can increase Prelude adoption a bit if people can just play around with it in their browsers, so here is a JavaScript interpreter. It's based on Sp3000's nifty Brainfuck interpreter, whose killer feature is interruptible execution and an optional time out.

var ITERS_PER_SEC = 100000;
var TIMEOUT_MILLISECS = 5000;
var ERROR_LOOP_MISMATCH = "Mismatched parentheses";
var ERROR_LOOP_MULTI = "Multiple parentheses in the same column";
var ERROR_TIMEOUT = "Timeout";
var ERROR_INTERRUPT = "Interrupted by user";

var code, input, timeout, num_input, num_output, loop_stack, loop_map;
var running, start_time, code_ptr, width, input_ptr, voices, iterations;

function clear_output() {
    document.getElementById("output").value = "";
    document.getElementById("stderr").innerHTML = "";
}

function stop() {
    running = false;
    document.getElementById("run").disabled = false;
    document.getElementById("stop").disabled = true;
    document.getElementById("clear").disabled = false;
    document.getElementById("num_input").disabled = false;
    document.getElementById("num_output").disabled = false;
    document.getElementById("timeout").disabled = false;
}

function interrupt() {
    error(ERROR_INTERRUPT);
}

function error(msg) {
    document.getElementById("stderr").innerHTML = msg;
    stop();
}

function run() {
    clear_output();

    document.getElementById("run").disabled = true;
    document.getElementById("stop").disabled = false;
    document.getElementById("clear").disabled = true;
    document.getElementById("num_input").disabled = false;
    document.getElementById("num_output").disabled = false;
    document.getElementById("timeout").disabled = false;

    code = document.getElementById("code").value;
    input = document.getElementById("input").value;
    num_input = document.getElementById("num_input").checked;
    num_output = document.getElementById("num_output").checked;
    timeout = document.getElementById("timeout").checked;

    loop_stack = [];
    loop_map = {};

    if (num_input) {
        input = input.match(/-?\d+/g).map(function (n) {
            return +n;
        });
    } else {
        input = input.split("").map(function (s) {
            return s.charCodeAt(0);
        });
    }

    code = code.split("\n");
    width = 0;
    for (var i = 0; i < code.length; ++i)
        if (code[i].length > width) 
            width = code[i].length;
    console.log(code);
    console.log(width);
    for (var i = 0; i < width; ++i) {
        var hasParens = false;
        for (var j = 0; j < code.length; ++j) {
            var char = code[j][i];
            if (char == "(" || char == ")") {
                if (hasParens) {
                    error(ERROR_LOOP_MULTI);
                    return;
                }

                hasParens = true;
                if (char == "(") loop_stack.push({
                    x: i,
                    y: j
                });
                else {
                    if (loop_stack.length == 0) {
                        error(ERROR_LOOP_MISMATCH);
                        return;
                    } else {
                        var last_parens = loop_stack.pop();
                        loop_map[last_parens.x] = {
                            x: i,
                            y: last_parens.y
                        };
                        loop_map[i] = last_parens;
                    }
                }
            }
        }
        console.log(i);
        console.log(loop_stack);
    }

    if (loop_stack.length > 0) {
        error(ERROR_LOOP_MISMATCH);
        return;
    }

    console.log(loop_map);

    running = true;
    start_time = Date.now();
    code_ptr = 0;
    input_ptr = 0;
    voices = code.map(function () {
        return [];
    });
    iterations = 0;

    prelude_iter(1);
}

function prelude_iter(niters) {
    if (code_ptr >= width || !running) {
        stop();
        return;
    }

    console.log(voices);
    var iter_start_time = Date.now();

    for (var i = 0; i < niters; ++i) {
        var previousTops = voices.map(function(v) { return v[v.length-1] || 0; });
        
        for (var j = 0; j < code.length; ++j) {
            switch (code[j][code_ptr]) {
                case "0":
                case "1":
                case "2":
                case "3":
                case "4":
                case "5":
                case "6":
                case "7":
                case "8":
                case "9":
                    voices[j].push(+code[j][code_ptr]);
                    break;
                    
                case "+":
                    var x = voices[j].pop() || 0;
                    var y = voices[j].pop() || 0;
                    voices[j].push(x + y);
                    break;

                case "-":
                    var x = voices[j].pop() || 0;
                    var y = voices[j].pop() || 0;
                    voices[j].push(y - x);
                    break;

                case "v":
                    voices[j].push(previousTops[j == code.length-1 ? 0 : j + 1]);
                    break;
                    
                case "^":
                    voices[j].push(previousTops[j == 0 ? code.length-1 : j - 1]);
                    break;
                    
                case "#":
                    voices[j].pop();
                    break;

                case "!":
                    var value = voices[j].pop() || 0; 
                    document.getElementById("output").value += num_output ? value+"\n" : String.fromCharCode(value);
                    break;

                case "?":
                    if (input_ptr >= input.length) {
                        voices[j].push(0);
                    } else {
                        voices[j].push(input[input_ptr]);
                        ++input_ptr;
                    }
                    break;
            }
        }

        if (loop_map[code_ptr])
        {
            var other = loop_map[code_ptr];
            code_ptr = previousTops[other.y] ? Math.min(code_ptr, other.x) : Math.max(code_ptr, other.x);
        }

        ++code_ptr;
        ++iterations;

        if (timeout && Date.now() - start_time > TIMEOUT_MILLISECS) {
            error(ERROR_TIMEOUT);
            return;
        }
    }

    setTimeout(function () {
        prelude_iter(ITERS_PER_SEC * (Date.now() - iter_start_time) / 1000)
    }, 0);
}
<div style="font-size:12px;font-family:Verdana, Geneva, sans-serif;">Code:
    <br>
    <textarea id="code" rows="4" style="overflow:scroll;overflow-x:hidden;width:90%;">1(  #^    #^^   (#^+!
6 9+ 05+5+^#^+! #^ ^+!
   (((1- )#^##)^^+   )7-!)1433545514232323344949145353495314235494040404232323013334492349532333344953493435343584233475234501333449530423232349495323232323495323230494)</textarea>
    <br>Input:
    <br>
    <textarea id="input" rows="2" style="overflow:scroll;overflow-x:hidden;width:90%;">5a-13 11 2e3</textarea>
    <p>Numeric Input:
        <input id="num_input" type="checkbox">&nbsp; Numeric Output:
        <input id="num_output" type="checkbox">&nbsp; Timeout:
        <input id="timeout" type="checkbox" checked="checked">&nbsp;
        <br>
        <br>
        <input id="run" type="button" value="Run" onclick="run()">
        <input id="stop" type="button" value="Stop" onclick="interrupt()" disabled="disabled">
        <input id="clear" type="button" value="Clear" onclick="clear_output()">&nbsp; <span id="stderr" style="color:red"></span>

    </p>Output:
    <br>
    <textarea id="output" rows="6" style="overflow:scroll;width:90%;"></textarea>
</div>

Alternatively, here is a JSFiddle.

The interpreter is prepopulated with my quine, but you can easily find more example programs for to play around with by searching for Prelude answers of mine.

All the features of Prelude are implemented. The only shortcoming is the lack of arbitrary-precision arithmetic, due to JavaScript's number type. I might fix that at some point, since I only need to support addition and subtraction for arbitrary integers.

Input and output are usually the byte values of individual characters, but the interpreter comes with two flags to read and/or write (signed) numbers instead.

ToDo

  • Add minified version for copying.
  • Support arbitrary precision arithmetic.
  • Add snippet generator.

Martin Ender

Posted 2014-10-21T02:41:05.133

Reputation: 184 808

9

Deadfish

function run() {
  var code = document.getElementById("code").value
  var unicode = document.getElementById("unicode").checked
  var n = 0
  var output = ""

  for (var i = 0; i < code.length; ++i) {
    switch (code[i]) {
      case "i":
        n++
        break
      case "d":
        n--
        break
      case "s":
        n *= n
        break
      case "o":
        if (unicode) {
          output += String.fromCharCode(n)
        } else {
          output += n
          output += "\n"
        }
        break
      default:
        output += "\n"
    }
    
    if (n == -1 || n == 256) {
      n = 0
    }
  }

  document.getElementById('output').value = output
}
<div style="font-size:12px;font-family:Verdana, Geneva, sans-serif;">
  <div style="float:left; width:50%;">
    Code:<br>
    <textarea id="code" rows="8" style="overflow:scroll;overflow-x:hidden;width:90%;" wrap="hard">iiisdsiiiiiiiioiiiiiiiiiiiiiiiiiiiiiiiiioiiiiiiiiiiiiiiiooiiiiiiiiiodddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddoddddddddddddddddddddddddsiioiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiioiiiiiiiiioiioddddddddddddoddddodddoiiiiiiiiiiiiiiiiiiiiiiiiodddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddodddddddddddddddddddddsdddddodddddodddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddodddddddddddddddddddddddsiiiiiiiioiiiiiiiiiiiiiiiiiiiiiioiiiiiiofdddddddddddddddddddddddddddddddddddddddddddddoiiiiiiiiiiiiiiiiiiiiiiiiioiiiiiiiiiiiiiiiooiiiiiiiiiodddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddoddddddddddddddddddddddddsiioiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiioiiiiiiiiioiioddddddddddddoddddodddoiiiiiiiiiiiiiiiiiiiiiiiiodddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddodddddddddddddddddddddsdddddodddddodddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddodddddddddddddddddddddddsiiiiiiiioiiiiiiiiiiiiiiiiiiiiiioiiiiiiofdddddddddddddddddddddddddddddddddddddddddddddoiiiiiiiiiiiiiiiiiiiiiiiiioiiiiiiiiiiiiiiiooiiiiiiiiiodddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddoddddddddddddddddddddddddsiioiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiioiiiiiiiiioiioddddddddddddoddddodddoiiiiiiiiiiiiiiiiiiiiiiiiodddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddoddddddddddddddddddddddddsiiiioiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiioddddoiiiiiiiiiiiiiiiiioddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddoddddddddddddddddddddddddsiiiioiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiioddddoiiioiioiiioiiiiiiiiiiodddddddddddofddddddddddddddddddddddddddddddddoiiiiiiiiiiiiiiiiiiiiiiiiioiiiiiiiiiiiiiiiooiiiiiiiiiodddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddoddddddddddddddddddddddddsiioiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiioiiiiiiiiioiioddddddddddddoddddodddoiiiiiiiiiiiiiiiiiiiiiiiiodddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddodddddddddddddddddddddsdddddodddddodddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddodddddddddddddddddddddddsiiiiiiiioiiiiiiiiiiiiiiiiiiiiiioiiiiiio</textarea>
    <p>
      <input id="unicode" type="checkbox" checked="true" />Use ASCII/Unicode mode
    </p>
  </div>
  <div style="float:left; width:50%;">
    Output:<br>
    <textarea id="output" rows="6" style="overflow:scroll;width:90%;"></textarea>
  <p>
    <input id='run' type='button' value='Run' onclick='run()' />
  </p>
  </div>
</div>

To pre-initialise the code box, just put something in between the textarea tags. Similarly, by adding checked="true" in the checkbox tag, you can have ASCII/Unicode mode on by default (as seen above).

Note that Deadfish itself doesn't actually have ASCII output in its original spec, I just put it in for questions which may allow "the next closest thing". If you're concerned that a question's rules may not allow Deadfish because of this, you can always look at something like Deadfish~ which does have ASCII output.

The example shown is from the Happy Birthday topic.

Sp3000

Posted 2014-10-21T02:41:05.133

Reputation: 58 729

Needs more semicolons. ASI is the work of devil.

– nyuszika7h – 2014-12-30T09:55:41.917

@nyuszika7h Sorry, I'll update when I have time (Too used to Python and I don't JS much) – Sp3000 – 2014-12-30T10:06:08.430

input elements have no content (nor closing tag). You should use <label><input />Foo</label> instead of <input>Foo</input> – Oriol – 2014-11-01T22:47:54.587

8

Deadfish

This does not conform strictly to the requirement, it is my vision on how we could make use of Stack Snippets.

I don't like the amount of boilerplate, especially the need to include custom HTML for another Run button.

In my vision:

  • place a single <script> tag to include a simple framework and the actual interpreter
  • put the script in the CSS area (syntax highlighting may go horrible)
  • use the existing Run code snippet button to run the code
  • get the output in the snippet frame

For now not decided how would be nicer to specify the input.

iiisdsiiiiiiiioiiiiiiiiiiiiiiiiiiiiiiiiioiiiiiiiiiiiiiiiooiiiiiiiiiodddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddoddddddddddddddddddddddddsiioiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiioiiiiiiiiioiioddddddddddddoddddodddoiiiiiiiiiiiiiiiiiiiiiiiiodddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddodddddddddddddddddddddsdddddodddddodddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddodddddddddddddddddddddddsiiiiiiiioiiiiiiiiiiiiiiiiiiiiiioiiiiiio
dddddddddddddddddddddddddddddddddddddddddddddoiiiiiiiiiiiiiiiiiiiiiiiiioiiiiiiiiiiiiiiiooiiiiiiiiiodddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddoddddddddddddddddddddddddsiioiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiioiiiiiiiiioiioddddddddddddoddddodddoiiiiiiiiiiiiiiiiiiiiiiiiodddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddodddddddddddddddddddddsdddddodddddodddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddodddddddddddddddddddddddsiiiiiiiioiiiiiiiiiiiiiiiiiiiiiioiiiiiio
dddddddddddddddddddddddddddddddddddddddddddddoiiiiiiiiiiiiiiiiiiiiiiiiioiiiiiiiiiiiiiiiooiiiiiiiiiodddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddoddddddddddddddddddddddddsiioiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiioiiiiiiiiioiioddddddddddddoddddodddoiiiiiiiiiiiiiiiiiiiiiiiiodddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddoddddddddddddddddddddddddsiiiioiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiioddddoiiiiiiiiiiiiiiiiioddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddoddddddddddddddddddddddddsiiiioiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiioddddoiiioiioiiioiiiiiiiiiiodddddddddddo
ddddddddddddddddddddddddddddddddoiiiiiiiiiiiiiiiiiiiiiiiiioiiiiiiiiiiiiiiiooiiiiiiiiiodddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddoddddddddddddddddddddddddsiioiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiioiiiiiiiiioiioddddddddddddoddddodddoiiiiiiiiiiiiiiiiiiiiiiiiodddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddodddddddddddddddddddddsdddddodddddodddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddodddddddddddddddddddddddsiiiiiiiioiiiiiiiiiiiiiiiiiiiiiioiiiiiio
<script src="https://bitbucket.org/manatwork/stackcode/raw/681610d0a6face9df62e82c50ec887d66d692e0f/interpret.js#Deadfish"></script>

manatwork

Posted 2014-10-21T02:41:05.133

Reputation: 17 865

Chrome version 57, output is empty – Khaled.K – 2017-04-11T10:32:14.830

there should be a way to easily include the input as well I guess – SztupY – 2014-10-21T13:58:04.210

Certainly, @SztupY. For now I am undecided whether to include the input also in the CSS piece or in the HTML one. Would not use the JavaScript piece for that, as the browsers will execute it before and some code may result endless loop in JavaScript. Sadly, there is no other way as other pieces are not copied over to the stack snippets server. :( – manatwork – 2014-10-21T14:08:54.440

I think the code is fine in the CSS, and the input can go after the <script> tags (maybe in a small container, like <pre>, so the script could get access to it. Hopefully a pre wouldn't distort the input much as well). – SztupY – 2014-10-21T14:29:38.083

A <pre> (or generally any tag) would appear displayed and syntax highlighted in green. As JavaScript is able to extract the input part anyway, I would not include extra markup. The main difference between using the CSS piece vs. the HTML piece is that CSS not appears in the document, while HTML does. Still undecided. – manatwork – 2014-10-21T14:48:50.820

Good idea. Though loading the code from a non-SE site goes against the idea of being self contained, so I'd avoid it if possible. – Calvin's Hobbies – 2014-10-21T17:37:12.110

My answer also may better fit to meta. Is influenced by Chris Jester-Young♦'s comment “I actually want to port the GS interpreter to JS.” My approach was designed having such cases in view, not for my dozen line Deadfish interpreter.

– manatwork – 2014-10-21T17:47:50.710

Gotcha. My only gripe with this method is that there's no way for the viewer to easily be confident that the supposed code is actually being interpreted to yield the output. The js could just be printing the output in an obfuscated way. When it's dynamically runnable the existence of the interpreter is more certain. (As for your input crisis, take a peek at my answer.)

– Calvin's Hobbies – 2014-10-22T01:38:03.323

3Also, am I missing something or is your snippet not working right now? – Calvin's Hobbies – 2014-10-22T01:38:55.850

@Calvin'sHobbies bitbucket must have changed the MIME type to text/plain which is then not executed for safety reasons. It used to work, though. – Ingo Bürk – 2014-10-26T16:13:55.280

It still works for me in Firefox 33. Sometimes there is a longer delay before the output appears, but until now it never failed. Anyway, a Stack CDN would be necessary to start using the Stack Snippets this way. :( – manatwork – 2014-10-26T16:19:04.453

8

JavaScript ES7

This allows you to run ES6/7 code in any browser.

The browsers I am sure this works 100% on are: IE 11, Chrome 46, and Safari 7.

This does support array comprehensions ([for(a in b) c])

function color(n,e){return~[null,void 0].indexOf(n)?'<div class="out '+e+'"><span style="color: #808080">'+String(n)+"</span></div>":"string"==typeof n?'<div class="out '+e+'"><span class="string">"'+n+'"</span></div>':"number"==typeof n?'<div class="out '+e+'"><span class="number">'+n+"</span></div>":"function"==typeof n?'<div class="out '+e+'"><span style="color: #808080">function</span></div>':"undefined"!=typeof n.length?'<div class="out '+e+'">'+JSON.stringify(n).replace(/(["'])((?:(?=\\*)\\.|.)*?)\1/g,'<span class="string">$1$2$1</span>').replace(/(\d+)/g,'<span class="number">$1</span>')+"</div>":'<div class="out '+e+'">'+JSON.stringify(n,null,2).replace(/(["'])((?:(?=\\*)\\.|.)*?)\1/g,'<span class="string">$1$2$1</span>').replace(/(\d+)/g,'<span class="number">$1</span>').replace(/\n/g,"<br>")+"</div>"}document.getElementById("run").onclick=function(){document.getElementById("Console").innerHTML="";var res="",err=!1;try{res=eval(babel.transform(document.getElementById("code").value).code.split("\n").slice(1).join("\n"))}catch(er){res=er.toString(),err=!0}err?document.getElementById("Console").innerHTML+='<div class="out error"><span>'+res+"</span></div>":document.getElementById("Console").innerHTML+=color(res,"implicit")},function(n,e){window.console={log:function(){n.log.apply(n,arguments),document.getElementById("Console").innerHTML+=color(arguments.length-1?Array.prototype.slice.call(arguments):arguments[0],"")},warn:function(){n.warn.apply(n,arguments),document.getElementById("Console").innerHTML+=color(arguments.length-1?Array.prototype.slice.call(arguments):arguments[0],"warn")},error:function(){n.error.apply(n,arguments),document.getElementById("Console").innerHTML+=color(arguments.length-1?Array.prototype.slice.call(arguments):arguments[0],"error")}},window.alert=console.log}(window.console,window.alert);
@import url(http://fonts.googleapis.com/css?family=Open+Sans:400,300,700);@import url(https://fonts.googleapis.com/css?family=Inconsolata);*{font-family:'Open Sans',Arial,sans-serif;font-size:.9rem}.pre,code,pre,pre *{font-family:Inconsolata,monospace}:not(b,bold,strong){font-weight:300}h1,h2,h3,h4,h5,h6{font-weight:600}h1{font-size:1.4rem}h2{font-size:1.38rem}h3{font-size:1.2rem}h4{font-size:1.15rem}h5{font-size:1.11rem}h6{font-size:1.05rem}b,i,p,span{color:#404040}p{line-height:1.3rem}a{color:#605346;text-decoration:none}a:active{color:#523415}a:visited{color:#74513E}hr{border:none;border-top:1px solid #e0d7c1}blockquote{background:#EDE3D3;border-radius:0 .5rem .5rem 0;padding:.5rem .5rem .5rem 4em;margin:.5rem .2rem}code{background:#EBE7DD;border:1px solid #e0d7c1;padding:.3rem .4rem;border-radius:.2rem}.pre,pre{background:rgba(221,221,221,.3);border-radius:.3rem;padding:.5rem;font-size:1.2rem}ol{counter-reset:ol;list-style:none}li{line-height:1.5rem}ol li:before{content:counter(ol) ". ";counter-increment:ol;font-weight:700}table{background:#f2e8d9;border-radius:.75rem;border-collapse:collapse;table-layout:fixed}table.full>tbody{width:100%}thead>tr:first-child>td{border:none;border-bottom:2px solid #e0cbab!important;font-weight:700;z-index:1;text-align:center}td:not(:first-child){border-left:1px solid #eadbc3;z-index:-1}td:only-child{background:#e6dac7;text-align:center;font-weight:600;padding:.25rem;column-span:all}td{padding:.75rem}tr.sub{background:#EDE3D3}input,textarea{border-radius:.5rem;padding:.25rem;border:none;outline:0;transition:box-shadow .3s ease;background-color:#fff!important;margin:.5rem 0}input:focus,textarea:focus{box-shadow:0 0 5px rgba(81,203,238,1)}textarea{resize:vertical}button,input[type=button]{padding:.3rem .5rem;border-radius:.5rem;background-color:#faebd7;cursor:pointer;border:none;border-bottom:1.5px solid #deb887;outline:0}button:active,input[type=button]:active{border:none;border-top:1.5px solid #deb887;color:#000}
#Console{background-color:#fff;border-radius:.5rem;padding:0;height:auto!important;overflow:scroll;-webkit-box-sizing:border-box;-moz-box-sizing:border-box;box-sizing:border-box;min-height:1rem}.out,.out *{font-size:1.1rem;font-family:Inconsoleta,monospace}.out *{padding-left:.3rem}.out{border-top:1px solid #F0F0F0;border-bottom:1px solid #F0F0F0;padding:.7rem 1rem}.string,.string *{color:#C41927!important}.number{color:#1D00CF}.out.implicit:before{content:"<\00B7 ";color:#888;letter-spacing:-2px}.out.error:before,.out.warn:before{display:inline-block;background-size:1.1rem;content:"";width:1.1rem;height:1.1rem}.out.warn:before{background-image:url(http://vihan.org/p/esfiddle/icons/Warn.png)}.out.error:before{background-image:url(http://vihan.org/p/esfiddle/icons/Error.png)}.out.warn,.out.warn *{color:#AA5909;background-color:}.out.implicit,.out.log{border-color:#F0F0F0;background-color:#FFF}.out.error{border-color:#FFD7D8;background-color:#FFEBEB}
body,html{width:100%;height:100%;margin:0}body{background:#F4F3F1;display:-ms-flex;display:-webkit-flex;display:flex;align-items:center;justify-content:center}body>div{background:#EAE8E4;border-radius:.4rem;margin:2rem;padding:1rem;overflow:auto;width:50%;max-height:80%;overflow-x:hidden}label{display:block}.full{width:calc(100% - 1rem);display:inline-block;height:7rem}#input{width:100%}.out.warn  { border-color: #FFEFCC; background-color: #FFFAE1; }
<div id="demo"><h1>Code</h1><textarea id="code" class="full pre" placeholder="Code here">f=n=>n+1;
console.log( f(3) );
console.warn( `Foo` )
console.error( `Bar` );
alert( `Output` );
[ f(1), f(2) ]</textarea><button id="run">Run</button><button onclick="prompt('Link:','http://vihan.org/p/esfiddle?code='+encodeURIComponent(document.getElementById('code').value))">Permalink</button></div><div><h1>Console Output</h1><pre id="Console" class="full"></pre></div><script src="http://babeljs.io/scripts/babel.js"></script>

For permalinks, use ESFiddle

Downgoat

Posted 2014-10-21T02:41:05.133

Reputation: 27 116

I'm Downgoating this answer because when I try to run it, chrome *alerts* ERR_NAME_NOT_RESOLVED and then crashes the tab! – cat – 2015-12-16T03:02:12.007

1Chrome version 57 output ReferenceError: babel is not defined – Khaled.K – 2017-04-11T10:30:27.790

6

Insomnia

The specs is ambiguous about the semantic of instruction 8 and 9, so they are not implemented.

Feel free to improve it to support instruction 0 to 7. There is also some incomplete code to run the program in slow motion, but it was buggy so I remove it from the interface.

function toAsciiValue(t) {
    var i = 0;
    var a = "";
    for (i = 0; i < t.length; i++) {
        a += t.charCodeAt(i);
    }
    return a;
}

// http://stackoverflow.com/questions/1219860/html-encoding-in-javascript-jquery
function escapeHTML(str)
{
    var div = document.createElement('div');
    var text = document.createTextNode(str);
    div.appendChild(text);
    return div.innerHTML;
}

var interpreter = (function () {
    var group;
    var ip;
    var gp;
    var bp;
    var code;
    var halted;
    var tid;
    var output = "";
    
    function flipbit() {
        group[gp] = group[gp] ^ (1 << bp);
    };
    
    var commands = {
        0: function () { 
            flipbit();
            bp = (bp - 1) & 0x7;
        },
        1: function () {
            flipbit();
            bp = (bp + 1) & 0x7;
        },
        2: flipbit,
        3: function () { gp++; bp = 7; },
        4: function () { gp--; bp = 7; },
        5: function () { output += ~~group[gp]; },
        6: function () { output += String.fromCharCode(~~group[gp]); },
        7: function () {}
    };
    
    var nonprint = {
        "\0": "NUL",
        "\x01": "SOH",
        "\x02": "STX",
        "\x03": "ETX",
        "\x04": "EOT",
        "\x05": "ENQ",
        "\x06": "ACK",
        "\x07": "BEL",
        "\x08": "BS",
        "\x09": "TAB",
        "\x0a": "LF",
        "\x0b": "VT",
        "\x0c": "FF",
        "\x0d": "CR",
        "\x0e": "SO",
        "\x0f": "SI",
        "\x10": "DLE",
        "\x11": "DC1",
        "\x12": "DC2",
        "\x13": "DC3",
        "\x14": "DC4",
        "\x15": "NAK",
        "\x16": "SYN",
        "\x17": "ETB",
        "\x18": "CAN",
        "\x19": "EM",
        "\x1a": "SUB",
        "\x1b": "ESC",
        "\x1c": "FS",
        "\x1d": "GS",
        "\x1e": "RS",
        "\x1f": "US",
        "\x7f": "DEL",
        "\x80": "PAD",
        "\x81": "HOP",
        "\x82": "BPH",
        "\x83": "NBH",
        "\x84": "IND",
        "\x85": "NEL",
        "\x86": "SSA",
        "\x87": "ESA",
        "\x88": "HTS",
        "\x89": "HTJ",
        "\x8a": "LTS",
        "\x8b": "PLD",
        "\x8c": "PLU",
        "\x8d": "RI",
        "\x8e": "SS2",
        "\x8f": "SS3",
        "\x90": "DCS",
        "\x91": "PU1",
        "\x92": "PU2",
        "\x93": "STS",
        "\x94": "CCH",
        "\x95": "MW",
        "\x96": "SPA",
        "\x97": "EPA",
        "\x98": "SOS",
        "\x99": "SGCI",
        "\x9a": "SCI",
        "\x9b": "CSI",
        "\x9c": "ST",
        "\x9d": "OSC",
        "\x9e": "PM",
        "\x9f": "APC"
    }
    
    function exec(opcode) {
        var instruction = commands[opcode];
        if (typeof instruction === "function") {
            instruction();
        } else {
            halted = true;
            alert("Unsupported operation: " + opcode + ". Execution halted.");
        }
    }
    
    return {
        init: function (src) {
            group = [];
            ip = 0;
            gp = 0;
            bp = 7;
            halted = false;
            code = toAsciiValue(src);
            output = "";
            tid = void 0;
            
            this.update();
        },
        output: function () {
            return output;
        },
        update: function () {
            var dispCode = code + " ";
            document.getElementById('execcode').innerHTML = 
                dispCode.substring(0, ip) + "<span class='highlight'>" + dispCode[ip] + "</span>" + dispCode.substring(ip + 1);
            
            document.getElementById('output').innerHTML = 
                escapeHTML(output).replace(/[\x00-\x1f\x7f-\x9f]/g, function ($0) { 
                    return "<span class='non-printable'>" + nonprint[$0] + "</span>"; 
                });
                
            document.getElementById('gp').textContent = gp;
            document.getElementById('bp').textContent = bp;
            
            var dispByte = ("0000000" + (~~group[gp]).toString(2)).substr(-8);
            document.getElementById('currgr').innerHTML =
                dispByte.substring(0, 7 - bp) + "<span class='highlight'>" + dispByte[7 - bp] + "</span>" + dispByte.substring(8 - bp);
        },
        step: function () {
            if (ip >= code.length) {
                alert("The program has terminated. Reload the program to run from the beginning.");
                return;
            }
            
            if (halted) {
                alert("Execution has been halted and cannot be recovered");
                return;
            }
            
            exec(code[ip]);
            if (!halted) {
                ip++;
                this.update();
            }
        },
        slow: function () {
            // TODO: Complete this feature. May need to modify run and step.
            if (typeof tid == "number") {
                clearTimeout(tid);
                tid = void 0;
            } else {
                (function auto() {
                    if (!halted && ip < code.length) {
                        interpreter.step();
                        
                        if (!halted) {
                            tid = setTimeout(auto, 200);
                        }
                    }
                })();
            }
        },
        run: function () {
            if (ip >= code.length) {
                alert("The program has terminated. Reload the program to run from the beginning.");
                return;
            }
            
            if (halted) {
                alert("Execution has been halted and cannot be recovered");
                return;
            }
            
            while (ip < code.length) {
                exec(code[ip]);
                
                if (!halted) {
                    ip++;
                } else {
                    // Break out of the loop and update the UI
                    break;
                }
            }
            
            this.update();
        }
    }
})();

document.addEventListener('DOMContentLoaded', function () {

    var $ascii = document.getElementById("ascii");
    var $workspace = document.getElementById("workspace");
    
    $ascii.textContent = toAsciiValue($workspace.value);
    
    $workspace.onkeypress = $workspace.onkeyup = function () {
        $ascii.textContent = toAsciiValue(this.value);
    }
    
    interpreter.init('');
    
    document.getElementById("load").onclick = function () {
        interpreter.init($workspace.value);
    }
    
    document.getElementById("run").onclick = function () {
        interpreter.run();
    }
    
    document.getElementById("step").onclick = function () {
        interpreter.step();
    }
    
});
body {
    font-family: 'Franklin Gothic Book', 'Trebuchet MS', 'Arial', sans-serif;
}

.code {
    font-family: monospace;
    word-wrap: break-word;
    white-space: pre-wrap;
}

pre.code, textarea.code {
    width: 100%;
}

table th {
    padding: 0 10px;
}

table .code {
    width: 100px;
    text-align: center;
}

td {
    border: 1px solid;
}

.highlight {
    background: pink;
}

.non-printable {
    background: black;
    color: white;
    border-radius: 4px;
    margin: 0 1px;
    padding: 0 1px;
}

.box {
    border: 2px groove;
    height: 6em;
}
<h1>Insomnia<sup><a href="http://esolangs.org/wiki/Insomnia">[?]</a></sup></h1>

Currently only supports commands 0-7. Semantics of command 8 and 9 (WHILE loop) is not clearly understood, so it is not implemented.<br>
This interpreter is greatly inspired by <a href="http://www.quirkster.com/iano/js/">Ian's Befunge-93 interpreter</a>.

<h3>Source code</h3>
<textarea cols="80" rows="5" id="workspace" class="code"></textarea>

<h3>ASCII sequence</h3>
<pre id="ascii" class="code"></pre>
<input type="button" id="load" value="Load program" />

<hr>

<h3>Execution</h3>
<pre id="execcode" class="code"></pre>

<input type="button" id="run" value="Run" />
<input type="button" id="step" value="Step" />
<br>

<h3>Debug</h3>
<table>
  <tr>
    <th>Group Pointer</th>
    <td class="code" id="gp"></td>
  </tr>
  <tr>
    <th>Bit Pointer</th>
    <td class="code" id="bp"></td>
  </tr>
  <tr>
    <th>Current group</th>
    <td class="code" id="currgr"></td>
  </tr>
</table>

<h3>Output</h3>
<pre id="output" class="code box"></pre>

n̴̖̋h̷͉̃a̷̭̿h̸̡̅ẗ̵̨́d̷̰̀ĥ̷̳

Posted 2014-10-21T02:41:05.133

Reputation: 5 683

5

HQ9+

Since Calvin's Hobbies even used HQ9+ as an example, I decided to write a quick implementation for it. Well, partial implementation, as I decided to leave out the accumulator (you may hate me for that).

The interpreter is, let's say, partially golfed. I don't know why I sort of golfed it, but not really.

(function () {
    var bottles = (function beerMe(amount) {
        return "012, 01.\n3, 412.5".replace(/\d/g, function (c) {
            return [amount || "No more", " bottles of beer", " on the wall", amount ? "Take one down, pass it around" : "Go to the store and buy some more", ((amount || 100) - 1) || "no more", amount ? "\n\n" : ""][c];
        }) + (amount ? beerMe(amount - 1) : "");
    })(99);

    document.getElementById('run').onclick = function () {
        var source = document.getElementById('source').value,
            output = '';
        source.split('').forEach(function (command) {
            var index = ['H', 'Q', '9'].indexOf(command);
            (index + 1 && (output += ['Hello, world!', source, bottles][index], !0)) || alert('illegal command: ' + command);
        });

        document.getElementById('stdout').value = output;
    };
})();
#source, #stdout {
    display: block;
    width: 100%;
}

#stdout {
    white-space: pre;
    height: 50px;
}

#stdout, #run {
    margin-top: 3px;
}
<div>
    <input type="text" id="source" placeholder="Enter your HQ9+ program here" />
    <textarea id="stdout" wrap="off" readonly></textarea>
    <input type="button" id="run" value="Execute HQ9+" />
</div>

Ingo Bürk

Posted 2014-10-21T02:41:05.133

Reputation: 2 674

No plus! That means it's not HQ9+. – ericw31415 – 2016-04-03T13:09:12.490

1your if on the 9 command is backwards... says go to the store and buy some more every time but the last time – pseudonym117 – 2014-10-30T16:14:57.903

1@pseudonym117 Oops. Fixed. A language with three (useful) commands and I still manage to print the wrong output. :) – Ingo Bürk – 2014-10-30T16:34:56.967

+1 for golfing 9 – Sp3000 – 2014-10-30T20:08:04.150

5Bug: "1 bottles of beer on the wall." – kennytm – 2014-10-31T15:59:19.550

5Your interpreter doesn't support increasing the accumulator! – jimmy23013 – 2014-11-05T11:29:19.350

7I meant you should at least ignore the + command instead of throwing an error. – jimmy23013 – 2014-11-13T15:01:50.223

5

Whitespace

function interpret() {
    function num(sign, bits) { return sign * parseInt(bits.replace("S", "0").replace("T", "1"), 2); }
    var prog = document.getElementById("prog").value, esc = document.getElementById("esc").checked;
    if (!esc)
        prog = prog.replace(" ", "S").replace("\t", "T").replace("\n", "L");
    prog = prog.replace(/[^STL]/g, "");
    var stack = [], labels = {}, heap = [], input = document.getElementById("in").value, output = document.getElementById("out"), inPos = 0, callStack = [];
    for (var i = 0; i < prog.length; i++) {
        switch (prog[i]) {
            case "S":
                switch (prog[++i]) {
                    case "S":
                        stack.push(num(prog[++i] == "S" ? 1 : -1, prog.slice(++i, prog.indexOf("L", i) - 1)));
                        i = prog.indexOf("L", i);
                        break;
                    case "T":
                        switch (prog[++i]) {
                            case "S":
                                stack.push(stack[stack.length - num(prog[++i] == "S" ? 1 : -1, prog.slice(++i, prog.indexOf("L", i) - 1))]);
                                i = prog.indexOf("L", i);
                                break;
                            case "L":
                                var num = num(prog[++i] == "S" ? 1 : -1, prog.slice(++i, prog.indexOf("L", i) - 1));
                                i = prog.indexOf("L", i);
                                stack.splice(stack.length - num - 1, num);
                                break;
                        }
                        break;
                    case "L":
                        switch (prog[++i]) {
                            case "S":
                                stack.push(stack[stack.length - 1]);
                                break;
                            case "T":
                                stack.push.apply(stack, stack.slice(-2).reverse());
                                break;
                            case "L":
                                stack.pop();
                                break;
                        }
                        break;
                }
                break;
            case "T":
                switch (prog[++i]) {
                    case "S":
                        switch (prog[++i]) {
                            case "S":
                                switch (prog[++i]) {
                                    case "S":
                                        stack.push(stack.pop() + stack.pop());
                                        break;
                                    case "T":
                                        stack.push(stack.pop() - stack.pop());
                                        break;
                                    case "L":
                                        stack.push(stack.pop() * stack.pop());
                                        break;
                                }
                                break;
                            case "T":
                                switch (prog[++i]) {
                                    case "S":
                                        stack.push(Math.floor(stack.pop() / stack.pop()));
                                        break;
                                    case "T":
                                        stack.push(stack.pop() % stack.pop());
                                        break;
                                }
                                break;
                        }
                        break;
                }
                break;
            case "T":
                switch (prog[++i]) {
                    case "S":
                        heap[stack[stack.length - 2]] = stack.pop();
                        stack.pop();
                        break;
                    case "T":
                        stack.push(heap[stack.pop()]);
                        break;
                }
                break;
            case "L":
                switch (prog[++i]) {
                    case "S":
                        switch (prog[++i]) {
                            case "S":
                                output.value += String.fromCharCode(stack.pop());
                                break;
                            case "T":
                                output.value += stack.pop();
                                break;
                        }
                        break;
                    case "T":
                        switch (prog[++i]) {
                            case "S":
                                heap[stack.pop()] = input.charCodeAt(inPos++);
                                break;
                            case "T":
                                heap[stack.pop()] = parseInt(input.slice(inPos, input.indexOf("\n", inPos) - 1));
                                break;
                        }
                }
                break;
            case "L":
                switch (prog[++i]) {
                    case "S":
                        switch (prog[++i]) {
                            case "S":
                                labels[num(1, prog.slice(++i, prog.indexOf("L", i) - 1))] = i = prog.indexOf("L", i);
                                break;
                            case "T":
                                callStack.push(prog.indexOf("L", ++i));
                                i = labels[num(1, prog.slice(i, prog.indexOf("L", i) - 1))];
                                break;
                            case "L":
                                i = labels[num(1, prog.slice(++i, prog.indexOf("L", i) - 1))];
                                break;
                        }
                        break;
                    case "T":
                        switch (prog[++i]) {
                            case "S":
                                if (stack.pop() == 0)
                                    i = labels[num(1, prog.slice(++i, prog.indexOf("L", i) - 1))];
                                else
                                    i = prog.indexOf("L", ++i);
                                break;
                            case "T":
                                if (stack.pop() < 0)
                                    i = labels[num(1, prog.slice(++i, prog.indexOf("L", i) - 1))];
                                else
                                    i = prog.indexOf("L", ++i);
                                break;
                        }
                        break;
                    case "L":
                        i = callStack.pop();
                        break;
                }
                break;
            case "L":
                if (prog[++i] == "L")
                    return JSON.stringify(stack);
                break;
        }
    }
    return JSON.stringify(stack);
}
Program:<br/>
<textarea id="prog" rows="4" cols="60"></textarea><br/>
Input:<br/>
<textarea id="in" rows="4" cols="60"></textarea><br/>
<form>
    Is escaped as <code>STL</code>: <input id="esc" type="checkbox"/><br/>
</form>
<button onclick="document.getElementById('stack').innerText = interpret()">Interpret</button><br/>
Output:<br/>
<textarea id="out" rows="4" cols="60"></textarea><br/>
Resulting stack: <span id="stack"/>

LegionMammal978

Posted 2014-10-21T02:41:05.133

Reputation: 15 731

5

Beam

I really think this language deserved to have an interpreter and it appeared simple enough for a non programmer like me to have a go at. I hope that I got it right. It appears to work for the examples I have tried. A lot of the code is plagiarized from other answers in this question. My apologies to the real programmers out there if I have committed any cardinal sins in my code:)

I have put the 'Reverse STDIN' example in from the Esolangs page.

var ITERS_PER_SEC = 100000;
var TIMEOUT_SECS = 50;
var ERROR_INTERRUPT = "Interrupted by user";
var ERROR_TIMEOUT = "Maximum iterations exceeded";
var ERROR_LOSTINSPACE = "Beam is lost in space";

var code, store, beam, ip_x, ip_y, dir, input_ptr, mem;
var input, timeout, width, iterations, running;

function clear_output() {
document.getElementById("output").value = "";
document.getElementById("stderr").innerHTML = "";
}

function stop() {
running = false;
document.getElementById("run").disabled = false;
document.getElementById("stop").disabled = true;
document.getElementById("clear").disabled = false;
document.getElementById("timeout").disabled = false;
}

function interrupt() {
error(ERROR_INTERRUPT);
}

function error(msg) {
document.getElementById("stderr").innerHTML = msg;
stop();
}

function run() {
clear_output();
document.getElementById("run").disabled = true;
document.getElementById("stop").disabled = false;
document.getElementById("clear").disabled = true;
document.getElementById("input").disabled = false;
document.getElementById("timeout").disabled = false;

code = document.getElementById("code").value;
input = document.getElementById("input").value;
timeout = document.getElementById("timeout").checked;
 
code = code.split("\n");
width = 0;
for (var i = 0; i < code.length; ++i){
 if (code[i].length > width){ 
  width = code[i].length;
 }
}
console.log(code);
console.log(width);
 
running = true;
dir = 0;
ip_x = 0;
ip_y = 0;
input_ptr = 0;
beam = 0;
store = 0;
mem = [];
 
input = input.split("").map(function (s) {
  return s.charCodeAt(0);
 });
 
iterations = 0;

beam_iter();
}

function beam_iter() {
while (running) {
 var inst; 
 try {
  inst = code[ip_y][ip_x];
 }
 catch(err) {
  inst = "";
 }
 switch (inst) {
  case ">":
   dir = 0;
   break;
  case "<":
   dir = 1;
   break;
  case "^":
   dir = 2;
   break;
  case "v":
   dir = 3;
   break;
  case "+":
   if(++beam > 255)
    beam = 0;
   break;
  case "-":
   if(--beam < 0)
    beam = 255;
   break;
  case "@":
   document.getElementById("output").value += String.fromCharCode(beam);
   break;
  case ":":
   document.getElementById("output").value += beam;
   break;
  case "/":
   dir ^= 2;
   break;
  case "\\":
   dir ^= 3;
   break;
  case "!":
   if (beam != 0) {
    dir ^= 1;
   }
   break;
  case "?":
   if (beam == 0) {
    dir ^= 1;
   }
   break;
  case "_":
   switch (dir) {
   case 2:
    dir = 3;
    break;
   case 3:
    dir = 2;
    break;
   }
   break;
  case "|":
   switch (dir) {
   case 0:
    dir = 1;
    break;
   case 1:
    dir = 0;
    break;
   }
   break;
  case "H":
   stop();
   break;
  case "S":
   store = beam;
   break;
  case "L":
   beam = store;
   break;
  case "s":
   mem[beam] = store;
   break;
  case "g":
   store = mem[beam];
   break;
  case "P":
   mem[store] = beam;
   break;
  case "p":
   beam = mem[store];
   break;
  case "u":
   if (beam != store) {
    dir = 2;
   }
   break;
  case "n":
   if (beam != store) {
    dir = 3;
   }
   break;
  case "`":
   --store;
   break;
  case "'":
   ++store;
   break;
  case ")":
   if (store != 0) {
    dir = 1;
   }
   break;
  case "(":
   if (store != 0) {
    dir = 0;
   }
   break;
  case "r":
   if (input_ptr >= input.length) {
    beam = 0;
   } else {
    beam = input[input_ptr];
    ++input_ptr;
   }
   break;
  }
 // Move instruction pointer
 switch (dir) {
  case 0:
   ip_x++;
   break;
  case 1:
   ip_x--;
   break;
  case 2:
   ip_y--;
   break;
  case 3:
   ip_y++;
   break;
 }
 if (running && (ip_x < 0 || ip_y < 0 || ip_x >= width || ip_y >= code.length)) {
  error(ERROR_LOSTINSPACE);
 }
 ++iterations;
 if (iterations > ITERS_PER_SEC * TIMEOUT_SECS) {
  error(ERROR_TIMEOUT);
 }
}
}
<div style="font-size:12px;font-family:Verdana, Geneva, sans-serif;">Code:
    <br>
    <textarea id="code" rows="8" style="overflow:scroll;overflow-x:hidden;width:90%;">v
(>``v
!  H(p`@`p)H
P   H
' 
r 
' 
P 
! 
>^
 </textarea>
    <br>Input:
    <br>
    <textarea id="input" rows="2" style="overflow:scroll;overflow-x:hidden;width:90%;">Reverse this string</textarea>
    <p>Timeout:
        <input id="timeout" type="checkbox" checked="checked">&nbsp;
        <br>
        <br>
        <input id="run" type="button" value="Run" onclick="run()">
        <input id="stop" type="button" value="Stop" onclick="interrupt()" disabled="disabled">
        <input id="clear" type="button" value="Clear" onclick="clear_output()">&nbsp; <span id="stderr" style="color:red"></span>
    </p>Output:
    <br>
    <textarea id="output" rows="6" style="overflow:scroll;width:90%;"></textarea>
</div>

MickyT

Posted 2014-10-21T02:41:05.133

Reputation: 11 735

How did I not notice this before? This is great! I love to see languages rescued from the Pit of Unimplementation. ;) – ETHproductions – 2015-09-18T03:03:25.940

1I hope you don't mind, but I cleaned up the JS section somewhat. – ETHproductions – 2015-09-18T03:10:54.747

@ETHproductions I have no problem with that at all. I'm not a real programmer as I mentioned above, but I really wanted something to test out some ideas that you sparked with your answer to the Hello World challenge. – MickyT – 2015-09-18T03:16:10.593

The timeout feature currently doesn't seem to work. If I insert an infinite loop, it crashes my browser, and I remember one time trying to print every ASCII character from ! to ~, and having it always time out after the first three, even with timeout turned off. I'll take a look into this in a little bit. – ETHproductions – 2015-10-08T17:46:53.383

@ETHproductions that came over with the snippet that I largely copied to start this one. Must admit I've had a few issues with the timeouts. – MickyT – 2015-10-08T18:02:39.047

5

Ouroboros

An esoteric language created by me, DLosc, in October 2015. The execution model is based on each line of code being an ouroboros snake. Control proceeds from the head to the tail and loops back to the head; instructions allow the snake to swallow part of its tail or regurgitate what it has swallowed, thus changing the flow of execution. All snakes execute in parallel. Data is stored on a shared stack as well as an individual stack for each snake. For more information, sample programs, and the most up-to-date interpreter, see the Github repository.

// Define Stack class
function Stack() {
  this.stack = [];
  this.length = 0;
}
Stack.prototype.push = function(item) {
  this.stack.push(item);
  this.length++;
}
Stack.prototype.pop = function() {
  var result = 0;
  if (this.length > 0) {
    result = this.stack.pop();
    this.length--;
  }
  return result;
}
Stack.prototype.top = function() {
  var result = 0;
  if (this.length > 0) {
    result = this.stack[this.length - 1];
  }
  return result;
}
Stack.prototype.toString = function() {
  return "" + this.stack;
}

// Define Snake class
function Snake(code) {
  this.code = code;
  this.length = this.code.length;
  this.ip = 0;
  this.ownStack = new Stack();
  this.currStack = this.ownStack;
  this.alive = true;
  this.wait = 0;
  this.partialString = this.partialNumber = null;
}
Snake.prototype.step = function() {
  if (!this.alive) {
    return null;
  }
  if (this.wait > 0) {
    this.wait--;
    return null;
  }
  var instruction = this.code.charAt(this.ip);
  var output = null;
  console.log("Executing instruction " + instruction);
  if (this.partialString !== null) {
    // We're in the middle of a double-quoted string
    if (instruction == '"') {
      // Close the string and push its character codes in reverse order
      for (var i = this.partialString.length - 1; i >= 0; i--) {
        this.currStack.push(this.partialString.charCodeAt(i));
      }
      this.partialString = null;
    } else {
      this.partialString += instruction;
    }
  } else if (instruction == '"') {
    this.partialString = "";
  } else if ("0" <= instruction && instruction <= "9") {
    if (this.partialNumber !== null) {
      this.partialNumber = this.partialNumber + instruction;  // NB: concatenation!
    } else {
      this.partialNumber = instruction;
    }
    next = this.code.charAt((this.ip + 1) % this.length);
    if (next < "0" || "9" < next) {
      // Next instruction is non-numeric, so end number and push it
      this.currStack.push(+this.partialNumber);
      this.partialNumber = null;
    }
  } else if ("a" <= instruction && instruction <= "f") {
    // a-f push numbers 10 through 15
    var value = instruction.charCodeAt(0) - 87;
    this.currStack.push(value);
  } else if (instruction == "$") {
    // Toggle the current stack
    if (this.currStack === this.ownStack) {
      this.currStack = this.program.sharedStack;
    } else {
      this.currStack = this.ownStack;
    }
  } else if (instruction == "s") {
    this.currStack = this.ownStack;
  } else if (instruction == "S") {
    this.currStack = this.program.sharedStack;
  } else if (instruction == "l") {
    this.currStack.push(this.ownStack.length);
  } else if (instruction == "L") {
    this.currStack.push(this.program.sharedStack.length);
  } else if (instruction == ".") {
    var item = this.currStack.pop();
    this.currStack.push(item);
    this.currStack.push(item);
  } else if (instruction == "m") {
    var item = this.ownStack.pop();
    this.program.sharedStack.push(item);
  } else if (instruction == "M") {
    var item = this.program.sharedStack.pop();
    this.ownStack.push(item);
  } else if (instruction == "y") {
    var item = this.ownStack.top();
    this.program.sharedStack.push(item);
  } else if (instruction == "Y") {
    var item = this.program.sharedStack.top();
    this.ownStack.push(item);
  } else if (instruction == "\\") {
    var top = this.currStack.pop();
    var next = this.currStack.pop()
    this.currStack.push(top);
    this.currStack.push(next);
  } else if (instruction == "@") {
    var c = this.currStack.pop();
    var b = this.currStack.pop();
    var a = this.currStack.pop();
    this.currStack.push(b);
    this.currStack.push(c);
    this.currStack.push(a);
  } else if (instruction == ";") {
    this.currStack.pop();
  } else if (instruction == "+") {
    var b = this.currStack.pop();
    var a = this.currStack.pop();
    this.currStack.push(a + b);
  } else if (instruction == "-") {
    var b = this.currStack.pop();
    var a = this.currStack.pop();
    this.currStack.push(a - b);
  } else if (instruction == "*") {
    var b = this.currStack.pop();
    var a = this.currStack.pop();
    this.currStack.push(a * b);
  } else if (instruction == "/") {
    var b = this.currStack.pop();
    var a = this.currStack.pop();
    this.currStack.push(a / b);
  } else if (instruction == "%") {
    var b = this.currStack.pop();
    var a = this.currStack.pop();
    this.currStack.push(a % b);
  } else if (instruction == "_") {
    this.currStack.push(-this.currStack.pop());
  } else if (instruction == "I") {
    var value = this.currStack.pop();
    if (value < 0) {
      this.currStack.push(Math.ceil(value));
    } else {
      this.currStack.push(Math.floor(value));
    }
  } else if (instruction == ">") {
    var b = this.currStack.pop();
    var a = this.currStack.pop();
    this.currStack.push(+(a > b));
  } else if (instruction == "<") {
    var b = this.currStack.pop();
    var a = this.currStack.pop();
    this.currStack.push(+(a < b));
  } else if (instruction == "=") {
    var b = this.currStack.pop();
    var a = this.currStack.pop();
    this.currStack.push(+(a == b));
  } else if (instruction == "!") {
    this.currStack.push(+ !this.currStack.pop());
  } else if (instruction == "?") {
    this.currStack.push(Math.random());
  } else if (instruction == "n") {
    output = "" + this.currStack.pop();
  } else if (instruction == "o") {
    output = String.fromCharCode(this.currStack.pop());
  } else if (instruction == "r") {
    var input = this.program.io.getNumber();
    this.currStack.push(input);
  } else if (instruction == "i") {
    var input = this.program.io.getChar();
    this.currStack.push(input);
  } else if (instruction == "(") {
    this.length -= Math.floor(this.currStack.pop());
    this.length = Math.max(this.length, 0);
  } else if (instruction == ")") {
    this.length += Math.floor(this.currStack.pop());
    this.length = Math.min(this.length, this.code.length);
  } else if (instruction == "w") {
    this.wait = this.currStack.pop();
  }
  // Any unrecognized character is a no-op
  if (this.ip >= this.length) {
    // We've swallowed the IP, so this snake dies
    this.alive = false;
    this.program.snakesLiving--;
  } else {
    // Increment IP and loop if appropriate
    this.ip = (this.ip + 1) % this.length;
  }
  return output;
}
Snake.prototype.getHighlightedCode = function() {
  var result = "";
  for (var i = 0; i < this.code.length; i++) {
    if (i == this.length) {
      result += '<span class="swallowedCode">';
    }
    if (i == this.ip) {
      if (this.wait > 0) {
        result += '<span class="nextActiveToken">';
      } else {
        result += '<span class="activeToken">';
      }
      result += escapeEntities(this.code.charAt(i)) + '</span>';
    } else {
      result += escapeEntities(this.code.charAt(i));
    }
  }
  if (this.length < this.code.length) {
    result += '</span>';
  }
  return result;
}

// Define Program class
function Program(source, speed, io) {
  this.sharedStack = new Stack();
  this.snakes = source.split(/\r?\n/).map(function(snakeCode) {
    var snake = new Snake(snakeCode);
    snake.program = this;
    snake.sharedStack = this.sharedStack;
    return snake;
  }.bind(this));
  this.snakesLiving = this.snakes.length;
  this.io = io;
  this.speed = speed || 10;
  this.halting = false;
}
Program.prototype.run = function() {
  this.step();
  if (this.snakesLiving) {
    this.timeout = window.setTimeout(this.run.bind(this), 1000 / this.speed);
  }
}
Program.prototype.step = function() {
   for (var s = 0; s < this.snakes.length; s++) {
    var output = this.snakes[s].step();
    if (output) {
      this.io.print(output);
    }
  }
  this.io.displaySource(this.snakes.map(function (snake) {
      return snake.getHighlightedCode();
    }).join("<br>"));
 }
Program.prototype.halt = function() {
  window.clearTimeout(this.timeout);
}

var ioFunctions = {
  print: function (item) {
    var stdout = document.getElementById('stdout');
    stdout.value += "" + item;
  },
  getChar: function () {
    if (inputData) {
      var inputChar = inputData[0];
      inputData = inputData.slice(1);
      result = inputChar.charCodeAt(0);
    } else {
      result = -1;
    }
    var stdinDisplay = document.getElementById('stdin-display');
    stdinDisplay.innerHTML = escapeEntities(inputData);
    return result;
  },
  getNumber: function () {
    while (inputData && (inputData[0] < "0" || "9" < inputData[0])) {
      inputData = inputData.slice(1);
    }
    if (inputData) {
      var inputNumber = inputData.match(/\d+/)[0];
      inputData = inputData.slice(inputNumber.length);
      result = +inputNumber;
    } else {
      result = -1;
    }
    var stdinDisplay = document.getElementById('stdin-display');
    stdinDisplay.innerHTML = escapeEntities(inputData);
    return result;
  },
  displaySource: function (formattedCode) {
    var sourceDisplay = document.getElementById('source-display');
    sourceDisplay.innerHTML = formattedCode;
  }
};
var program = null;
var inputData = null;
function showEditor() {
  var source = document.getElementById('source'),
    sourceDisplayWrapper = document.getElementById('source-display-wrapper'),
    stdin = document.getElementById('stdin'),
    stdinDisplayWrapper = document.getElementById('stdin-display-wrapper');
  
  source.style.display = "block";
  stdin.style.display = "block";
  sourceDisplayWrapper.style.display = "none";
  stdinDisplayWrapper.style.display = "none";
  
  source.focus();
}
function hideEditor() {
  var source = document.getElementById('source'),
    sourceDisplay = document.getElementById('source-display'),
    sourceDisplayWrapper = document.getElementById('source-display-wrapper'),
    stdin = document.getElementById('stdin'),
    stdinDisplay = document.getElementById('stdin-display'),
    stdinDisplayWrapper = document.getElementById('stdin-display-wrapper');
  
  source.style.display = "none";
  stdin.style.display = "none";
  sourceDisplayWrapper.style.display = "block";
  stdinDisplayWrapper.style.display = "block";
  
  var sourceHeight = getComputedStyle(source).height,
    stdinHeight = getComputedStyle(stdin).height;
  sourceDisplayWrapper.style.minHeight = sourceHeight;
  sourceDisplayWrapper.style.maxHeight = sourceHeight;
  stdinDisplayWrapper.style.minHeight = stdinHeight;
  stdinDisplayWrapper.style.maxHeight = stdinHeight;
  sourceDisplay.textContent = source.value;
  stdinDisplay.textContent = stdin.value;
}
function escapeEntities(input) {
  return input.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
}
function resetProgram() {
  var stdout = document.getElementById('stdout');
  stdout.value = null;
  if (program !== null) {
    program.halt();
  }
  program = null;
  inputData = null;
  showEditor();
}
function initProgram() {
  var source = document.getElementById('source'),
    stepsPerSecond = document.getElementById('steps-per-second'),
    stdin = document.getElementById('stdin');
  program = new Program(source.value, +stepsPerSecond.innerHTML, ioFunctions);
  hideEditor();
  inputData = stdin.value;
}
function runBtnClick() {
  if (program === null || program.snakesLiving == 0) {
    resetProgram();
    initProgram();
  } else {
    program.halt();
    var stepsPerSecond = document.getElementById('steps-per-second');
    program.speed = +stepsPerSecond.innerHTML;
  }
  program.run();
}
function stepBtnClick() {
  if (program === null) {
    initProgram();
  } else {
    program.halt();
  }
  program.step();
}
function sourceDisplayClick() {
  resetProgram();
}
.container {
    width: 100%;
}
.so-box {
    font-family:'Helvetica Neue', Arial, sans-serif;
    font-weight: bold;
    color: #fff;
    text-align: center;
    padding: .3em .7em;
    font-size: 1em;
    line-height: 1.1;
    border: 1px solid #c47b07;
    -webkit-box-shadow: 0 2px 2px rgba(0, 0, 0, 0.3), 0 2px 0 rgba(255, 255, 255, 0.15) inset;
    text-shadow: 0 0 2px rgba(0, 0, 0, 0.5);
    background: #f88912;
    box-shadow: 0 2px 2px rgba(0, 0, 0, 0.3), 0 2px 0 rgba(255, 255, 255, 0.15) inset;
}
.control {
    display: inline-block;
    border-radius: 6px;
    float: left;
    margin-right: 25px;
    cursor: pointer;
}
.option {
    padding: 10px 20px;
    margin-right: 25px;
    float: left;
}
h1 {
    text-align: center;
    font-family: Georgia, 'Times New Roman', serif;
}
a {
    text-decoration: none;
}
input, textarea {
    box-sizing: border-box;
}
textarea {
    display: block;
    white-space: pre;
    overflow: auto;
    height: 100px;
    width: 100%;
    max-width: 100%;
    min-height: 25px;
}
span[contenteditable] {
    padding: 2px 6px;
    background: #cc7801;
    color: #fff;
}
#stdout-container, #stdin-container {
    height: auto;
    padding: 6px 0;
}
#reset {
    float: right;
}
#source-display-wrapper , #stdin-display-wrapper{
    display: none;
    width: 100%;
    height: 100%;
    overflow: auto;
    border: 1px solid black;
    box-sizing: border-box;
}
#source-display , #stdin-display{
    font-family: monospace;
    white-space: pre;
    padding: 2px;
}
.activeToken {
    background: #f93;
}
.nextActiveToken {
    background: #bbb;
}
.swallowedCode{
    color: #999;
}
.clearfix:after {
    content:".";
    display: block;
    height: 0;
    clear: both;
    visibility: hidden;
}
.clearfix {
    display: inline-block;
}
* html .clearfix {
    height: 1%;
}
.clearfix {
    display: block;
}
<!--
Designed and written 2015 by D. Loscutoff
Much of the HTML and CSS was taken from this Befunge interpreter by Ingo Bürk: http://codegolf.stackexchange.com/a/40331/16766
-->
<h1><a href="https://github.com/dloscutoff/Esolangs/tree/master/Ouroboros">Ouroboros</a></h1>
<div class="container">
    <textarea id="source" placeholder="Enter your program here" wrap="off"></textarea>
    <div id="source-display-wrapper" onclick="sourceDisplayClick()">
        <div id="source-display"></div>
    </div>
</div>
<div id="stdin-container" class="container">
    <textarea id="stdin" placeholder="Input" wrap="off"></textarea>
    <div id="stdin-display-wrapper" onclick="stdinDisplayClick()">
        <div id="stdin-display"></div>
    </div>
</div>
<div id="controls-container" class="container clearfix">
    <input type="button" id="run" class="control so-box" value="Run" onclick="runBtnClick()" />
    <input type="button" id="pause" class="control so-box" value="Pause" onclick="program.halt()" />
    <input type="button" id="step" class="control so-box" value="Step" onclick="stepBtnClick()" />
    <input type="button" id="reset" class="control so-box" value="Reset" onclick="resetProgram()" />
</div>
<div id="stdout-container" class="container">
    <textarea id="stdout" placeholder="Output" wrap="off" readonly></textarea>
</div>
<div id="options-container" class="container">
    <div class="option so-box">Steps per Second: <span id="steps-per-second" contenteditable>10</span>
    </div>
</div>

Todo

  • Debug output: contents of stacks; how much input is left done
  • During execution, display code with current instructions highlighted and swallowed portions of tails grayed-out done

DLosc

Posted 2014-10-21T02:41:05.133

Reputation: 21 213

5

K5

This is a thin, simple wrapper around my JavaScript based K implementation, oK, and references the github repo. I've already built a much nicer browser-based frontend. oK implements a very substantial portion of K5, aside from IO, and has been used in the past to solve a number of problems here on stack overflow. The code isn't golfed, per se, but is well under 1000 lines including comments, the parser, all the primitive operations, the interpreter and the prettyprinter.

* {
  font-size:12px;
  font-family:Verdana, Geneva, sans-serif;
}
textarea {
  overflow:scroll;
  overflow-x:hidden;
  width:90%;
  font-family:Consolas, Courier New, monospace;
  font-size:10p
}
<script src="http://johnearnest.github.io/ok/oK.js"></script>
<script>
    function runk() {
     var code = document.getElementById("code").value;
     var env  = baseEnv();
     var ret  = run(parse(code), baseEnv());
     document.getElementById("output").value = format(ret);
    }
</script>

Code:
<br>
<textarea id="code" rows="8">2*!10</textarea>
<p><input id="run" type="button" value="Run" onclick="runk()"></p>
Output:
<br>
<textarea id="output" rows="6" style="overflow:scroll;width:90%;"></textarea>

JohnE

Posted 2014-10-21T02:41:05.133

Reputation: 4 632

4

oOo CODE

A very simple interpreter for oOo CODE (first time I use javascript). oOo CODE is an esoteric language based on brainfuck that uses uppercase and lowercase to store it's information, the actual text doesn't matter. As oOo CODE is based on brainfuck, I reused one of the brainfuck answers and added a function to translate oOo CODE to brainfuck.

var NUM_CELLS = 30000;
var ITERS_PER_SEC = 100000;
var TIMEOUT_MILLISECS = 5000;
var ERROR_BRACKET = "Mismatched brackets";
var ERROR_TIMEOUT = "Timeout";
var ERROR_INTERRUPT = "Interrupted by user";

var code, input, wrap, timeout, eof, loop_stack, loop_map;
var running, start_time, code_ptr, input_ptr, cell_ptr, cells, iterations;

function clear_output() {
  document.getElementById("output").value = "";
  document.getElementById("stderr").innerHTML = "";
}

function stop() {
  running = false;
  document.getElementById("run").disabled = false;
  document.getElementById("stop").disabled = true;
  document.getElementById("clear").disabled = false;
  document.getElementById("wrap").disabled = false;
  document.getElementById("timeout").disabled = false;
  document.getElementById("eof").disabled = false;
}

function interrupt() {
  error(ERROR_INTERRUPT);
}

function error(msg) {
  document.getElementById("stderr").innerHTML = msg;
  stop();
}

function oOoCODE(code){
    code = code.match(/[a-z]/gi).join('');
    code = code.match(/.../g);
    for (var i = 0; i<code.length; i++) {
        code[i] = [/[A-Z]/.test(code[i][0]),/[A-Z]/.test(code[i][1]),/[A-Z]/.test(code[i][2])];
        if (code[i][0]) {
            if (code[i][1]) {
                if (code[i][2]) {
                    code[i] = ",";
                } else {
                    code[i] = ".";
                }
            } else {
                if (code[i][2]) {
                    code[i] = "+";
                } else {
                    code[i] = "-";
                }
            }
        } else {
            if (code[i][1]) {
                if (code[i][2]) {
                    code[i] = "]";
                } else {
                    code[i] = "[";
                }
            } else {
                if (code[i][2]) {
                    code[i] = ">";
                } else {
                    code[i] = "<";
                }
            }
        }
    }
    return code.join('');
}

function run() {
  clear_output();
  
  document.getElementById("run").disabled = true;
  document.getElementById("stop").disabled = false;
  document.getElementById("clear").disabled = true;
  document.getElementById("wrap").disabled = true;
  document.getElementById("timeout").disabled = true;
  document.getElementById("eof").disabled = true;

  code = document.getElementById("code").value;
  input = document.getElementById("input").value;
  wrap = document.getElementById("wrap").value;
  timeout = document.getElementById("timeout").checked;
  eof = document.getElementById("eof").value;
  
  code = oOoCODE(code);
  
  loop_stack = [];
  loop_map = {};

  for (var i = 0; i < code.length; ++i) {
    if (code[i] == "[") {
      loop_stack.push(i);

    } else if (code[i] == "]") {
      if (loop_stack.length == 0) {
        error(ERROR_BRACKET);
        return;

      } else {
        var last_bracket = loop_stack.pop();
        loop_map[last_bracket] = i;
        loop_map[i] = last_bracket;
      }
    }
  }

  if (loop_stack.length > 0) {
    error(ERROR_BRACKET);
    return;
  }

  running = true;
  start_time = Date.now();
  code_ptr = 0;
  input_ptr = 0;
  cell_ptr = Math.floor(NUM_CELLS / 2);
  cells = {};
  iterations = 0;

  bf_iter(1);
}

function bf_iter(niters) {
  if (code_ptr >= code.length || !running) {
    stop();
    return;
  }

  var iter_start_time = Date.now();

  for (var i = 0; i < niters; ++i) {
    if (cells[cell_ptr] == undefined) {
      cells[cell_ptr] = 0;
    }

    switch (code[code_ptr]) {
      case "+":
        if ((wrap == "8" && cells[cell_ptr] == 255) ||
            (wrap == "16" && cells[cell_ptr] == 65535) || 
            (wrap == "32" && cells[cell_ptr] == 2147483647)) {
            cells[cell_ptr] = 0;
        } else {
          cells[cell_ptr]++;
        }
        break;
        
      case "-":
        if (cells[cell_ptr] == 0){
          if (wrap == "8"){ cells[cell_ptr] = 255; }
          if (wrap == "16"){ cells[cell_ptr] = 65535; }
          if (wrap == "32"){ cells[cell_ptr] = 2147483647; }    
        } else {
          cells[cell_ptr]--;
        }
        break;
      
      case "<": cell_ptr--; break;
      case ">": cell_ptr++; break;

      case ".":
        document.getElementById("output").value += String.fromCharCode(cells[cell_ptr]);
        break;

      case ",":
        if (input_ptr >= input.length) {
          if (eof != "nochange") {
            cells[cell_ptr] = parseInt(eof);
          }
        } else {
          cells[cell_ptr] = input.charCodeAt(input_ptr);
          input_ptr++;
        }
        break;

      case "[":
        if (cells[cell_ptr] == 0) {
          code_ptr = loop_map[code_ptr];
        }
        break;

      case "]":
        if (cells[cell_ptr] != 0) {
          code_ptr = loop_map[code_ptr];
        }
        break;
    }

    code_ptr++;
    iterations++;

    if (timeout && Date.now() - start_time > TIMEOUT_MILLISECS) {
      error(ERROR_TIMEOUT);
      return;
    }
  }

  setTimeout(function() { bf_iter(ITERS_PER_SEC * (Date.now() - iter_start_time)/1000) }, 0);
}
<div style="font-size:12px;font-family:Verdana, Geneva, sans-serif;">
  <div style="float:left; width:50%;">
    Code:
    <br>
    <textarea id="code" rows="4" style="overflow:scroll;overflow-x:hidden;width:90%;">JuLIeT
   O rOMeO, RoMEo! WHeREfOrE art tHoU RoMEo?
   DEnY ThY FaTHeR AnD ReFuse ThY NaME;
   oR, If ThoU wiLT not, BE but SWoRn mY loVe,
   aND i'Ll NO lONgER bE A cAPuLEt.
ROMeO
   [AsIdE] ShALl I HEar moRE, or sHAlL I sPEaK At THiS?
JuLIeT
   'TiS BUt Thy NamE thAt iS my EneMy;
   tHou ARt ThYSeLF, tHOUgH noT a mOntAguE.
   whAt's MOnTagUe? iT is Nor HanD, noR foOt,
   nOR arm (...)  </textarea>
    <br>Input:
    <br>
    <textarea id="input" rows="2" style="overflow:scroll;overflow-x:hidden;width:90%;"></textarea>
    <p>
      Wrap:
      <select id="wrap">
        <option value="8">8-bit</option>
        <option value="16">16-bit</option>
        <option value="32">32-bit</option>
      </select>
      &nbsp;
      Timeout:
      <input id="timeout" type="checkbox" checked="true" />&nbsp; EOF:
      <select id="eof">
        <option value="nochange">Same</option>
        <option value="0">0</option>
        <option value="-1">-1</option>
      </select>
    </p>
  </div>
  <div style="float:left; width:50%;">
    Output:
    <br>
    <textarea id="output" rows="6" style="overflow:scroll;width:90%;"></textarea>
    <p>
      <input id="run" type="button" value="Run" onclick="run()" />
      <input id="stop" type="button" value="Stop" onclick="interrupt()" disabled="true" />
      <input id="clear" type="button" value="Clear" onclick="clear_output()" />
      &nbsp;
      <span id="stderr" style="color:red"></span>
    </p>
  </div>
</div>

Def

Posted 2014-10-21T02:41:05.133

Reputation: 602

1

Needs more semicolons. ASI is the work of devil.

– nyuszika7h – 2014-12-30T09:56:45.777

@nyuszika7h Interesting, I didn't knew it works that way... (python programmer) The code without semi-colons was from the original brainfuck answer from Sp3000 so I didn't notice the lack of semi-colons. – Def – 2014-12-30T13:22:07.757

@Sp3000 No problem, I wrote my answer for fun and to learn a bit of javascript, not for the bounty – Def – 2015-01-01T20:40:17.863

4

STATA

Edit: added replace and checks for generate.

Has support for some parts of display, forvalues, list, generate, and set obs. In addition, it has some supports for macro (STATA's variables) creation and use. It supports enough operations for some of the simpler answers that have been posted on this site, such as Evolution of "Hello World!", Generate the Stöhr sequence, Make me an alphabet tree (replace single quotes with double quotes and make it 3 lines instead of 1). This is a work in progress, and I hope to continue it soon, but I figured I would at least put something here for now.

<html>
<body>
Input
<textarea id="input"></textarea>
<br/>
Code
<textarea id="code"></textarea>
<br/>
<button>Run</button>
<br/>Output:
<textarea id="output"></textarea>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script type="text/javascript">

$(function(){
    _N=0
    dataset=[];
    locals=[];
    globals=[];
    $output=$('#output');
    $input=$('#input');
    inputs=[];
    commands=[];
    comTable = [];
    comTable['di'] = 'display';
    comTable['dis'] = 'display';
    comTable['disp'] = 'display';
    comTable['displ'] = 'display';
    comTable['displa'] = 'display';
    comTable['display'] = 'display';
    comTable['loc'] = 'local';
    comTable['loca'] = 'local';
    comTable['local'] = 'local';
    comTable['gl'] = 'global';
    comTable['glo'] = 'global';
    comTable['glob'] = 'global';
    comTable['globa'] = 'global';
    comTable['global'] = 'global';
    comTable['set']='set';
    comTable['g'] = 'generate';
    comTable['ge'] = 'generate';
    comTable['gen'] = 'generate';
    comTable['gene'] = 'generate';
    comTable['gener'] = 'generate';
    comTable['genera'] = 'generate';
    comTable['generat'] = 'generate';
    comTable['generate'] = 'generate';
    comTable['replace'] = 'replace';
    comTable['l']='list';
    comTable['li']='list';
    comTable['lis']='list';
    comTable['list']='list';
    comTable['forv']='forvalues';
    comTable['forva']='forvalues';
    comTable['forval']='forvalues';
    comTable['forvalu']='forvalues';
    comTable['forvalue']='forvalues';
    comTable['forvalues']='forvalues';
    comTable['}']='No Operation';
    opTable=[];
    opTable['display']=[];
    opTable['display']['_r']='_request';
    opTable['display']['_re']='_request';
    opTable['display']['_req']='_request';
    opTable['display']['_requ']='_request';
    opTable['display']['_reque']='_request';
    opTable['display']['_reques']='_request';
    opTable['display']['_request']='_request';
    opTable['set']=[];
    opTable['set']['ob']='obs';
    opTable['set']['obs']='obs';
});
$('button').click(function () {
    _N=0
    dataset=[];
    inputNum=0;
    inputs=$input.val().split('\n');
    $output.val('');
    locals=[];
    globals=[];
    var code = $('#code').val();
    commands = code.split('\n');
    var tokens = [];
    commands.forEach(function (entry) {
        tokens.push(entry.match(/(?:[^\s"]+|"[^"]*")+/g) );
    });
    var error = '';
    var nextIndex=0;
    $.each(tokens,function (index,entry) {
        if(nextIndex!==index){
            return true;
        }
        var tok = entry[0];
        entry[0] = comTable[tok];
        if (entry[0] === undefined) {
            error = tok + ' is not a recognized command';
            outputError(error);
            return false;
        }
        nextIndex+=execute(entry,index);
        if(!nextIndex){
            return false;
        }
    });
});

function execute(command,ind){
    switch(command[0]){
        case 'display':
            return executeDisplay(command);
        case 'local':
            return executeLocal(command);
        case 'global':
            return executeGlobal(command);
        case 'set':
            return executeSet(command);
        case 'generate':
            return executeGenerate(command);
        case 'replace':
            return executeReplace(command);
        case 'list':
            return executeList(command);
        case 'forvalues':
            return executeForvalues(command,ind);
    }
}

function executeForvalues(command,ind){
    var re=/(\w+)=(.+)\((.+)\)(.+){/;
    var arr=re.exec(command[1]);
    if(arr==null){
        outputError('invalid format to command forvalues: '+command[1]);
        return false;
    }
    var locName=evaluate(arr[1],0);
    var i=Number(evaluate(arr[2],0));
    var increment=Number(evaluate(arr[3],0));
    var maximum=evaluate(arr[4],0);
    console.log(locName+' '+i+' '+increment+' '+maximum);
    maximum=Number(maximum);
    var curInd=ind+1;
    for(;i<=maximum;i+=increment){
        locals[locName]=i;
        globals['_'+locName]=i;
        for(curInd=ind+1;commands[curInd]!=='}';curInd++){
            var entry=commands[curInd].match(/(?:[^\s"]+|"[^"]*")+/g);
            var tok = entry[0];
            entry[0] = comTable[tok];
            if (entry[0] === undefined) {
                error = tok + ' is not a recognized command';
                outputError(error);
                return false;
            }
            console.log(entry);
            if(!execute(entry)){
                return false;
            }
            console.log($output.val());
        }
    }
    return curInd-ind;
}

function executeList(command){
    var out='\t'+Object.keys(dataset).join('\t')+'\n';
    for(var i=0;i<_N;i++){
        out+=(i+1)+'\t';
        Object.keys(dataset).forEach(function(element){
            out+=dataset[element][i]===undefined?'.':dataset[element][i]+'\t';
        })
        out+='\n';
    }
    $output.val(function(i,val){
        return val+out;
    });
    return 1;
}

function executeGenerate(command){
    var parts=command[1].split('=');
    if(dataset[parts[0]]){
        outputError('variable '+parts[0]+' already exists.');
        return false;
    }
    dataset[parts[0]]=[];
    for(var i=0;i<_N;i++){
        dataset[parts[0]][i]=evaluateVariables(parts[1],i);
    }
    return 1;
}

function executeReplace(command){
    var parts=command[1].split('=');
    if(!dataset[parts[0]]){
        outputError('variable '+parts[0]+' does not exist.');
        return false;
    }
    for(var i=0;i<_N;i++){
        dataset[parts[0]][i]=evaluateVariables(parts[1],i);
    }
    return 1;
}

function evaluateVariables(str,i){
    return eval(str.replace('_n',i+1));
}

function executeSet(command){
    if(opTable['set'][command[1]]=='obs'){
        _N=parseInt(command[2],10);
        return 1;
    }
    outputError('invalid option for command set: '+command[1]);
    return false;
}

function executeLocal(command){
    var parts=command[1].split('=');
    var name,value;
    if(parts.length==1){
        var re0=/^\+\+(\w+)$/;
        var re1=/^--(\w+)$/;
        var re2=/^(\w+)\+\+$/;
        var re3=/^(\w+)--$/;
        var arr=re0.exec(parts[0])||re1.exec(parts[0])||re2.exec(parts[0])||re3.exec(parts[0]);
        if(arr!=null){
            name=arr[1];
            if(parts[0].indexOf('-')>-1){
                value=globals[name]-1;
            }
            else{
                value=globals[name]+1;
            }
        }
        else{
            parts[1]=command[2];
        }
    }
    if(!value){
        value=evaluate(parts[1],0);
    }
    if(typeof value==='undefined'){
        return false;
    }
    if(!name){
        name=evaluate(parts[0]);
    }
    locals[name]=value;
    globals['_'+name]=value;
    return 1;
}

function executeGlobal(command){
    var parts=command[1].split('=');
    var name,value;
    if(parts.length==1){
        var re0=/^\+\+(\w+)$/;
        var re1=/^--(\w+)$/;
        var re2=/^(\w+)\+\+$/;
        var re3=/^(\w+)--$/;
        var arr=re0.exec(parts[0])||re1.exec(parts[0])||re2.exec(parts[0])||re3.exec(parts[0]);
        if(arr!=null){
            name=arr[1];
            if(parts[0].indexOf('-')>-1){
                value=globals[name]-1;
            }
            else{
                value=Number(globals[name])+1;
            }
        }
        else{
            parts[1]=command[2];
        }
    }
    if(!value){
        value=evaluate(parts[1],0);
    }
    if(typeof value==='undefined'){
        return false;
    }
    if(!name){
        name=evaluate(parts[0]);
    }
    if(name.charAt(0)=='_'){
        locals[name.substring(1)]=value;
    }
    globals[name]=value;
    return 1;
}

function executeDisplay(command){
    for(var i=1;i<command.length;i++){
        var comVal=command[i];
        $output.val(function(ind,val){
            var re=/^(\w+)/;
            var arr=re.exec(command[i]);
            if(arr!==null&&opTable['display'][arr[0]]=='_request'){
                re=/^\w+\((\w+)\)/;
                arr=re.exec(command[i]);
                var input=getInput();
                globals[arr[1]]=input;
                if(arr[1].charAt(0)=='_'){
                    locals[arr[1].substring(1)]=input;
                }
                return val;
            }
            return val+evaluate(command[i],0)+'\n';
        });
    }
    return 1;
}

function getInput(){
    var x=inputs[inputNum];
    inputNum++;
    return x;
}

function evaluate(str,captured){
    str=String(str);
    var index=str.indexOf('"');
    if(index==-1){
        return evaluate2(str);
    }
    var secondIndex=str.indexOf('"',index+1);
    while(secondIndex>0&&str.charAt(secondIndex-1)=='\\'){
        secondIndex=str.indexOf('"',secondIndex+1);
    }
    if(secondIndex<0){
        if(!captured){
            var error ='unmatched quotes';
            outputError(error);
            return undefined;
        }
        return '';
    }
    return (index>0?evaluate2(str.substring(0,index)):'')+evaluate2(str.substring(index,secondIndex+1))+(secondIndex<str.length-1?evaluate(str.substring(secondIndex)):'');
}

function evaluate2(str){
    var index=str.indexOf('`');
    if(index==-1){
        return evaluate3(str);
    }
    var secondIndex=str.indexOf('\'',index+1);
    while(secondIndex>0&&str.charAt(secondIndex-1)=='\\'){
        secondIndex=str.indexOf('\'',secondIndex+1);
    }
    if(secondIndex<0){
        return evaluate3(str);
    }
    var value=locals[str.substring(index+1,secondIndex)];
    if(typeof value==='undefined'){
        value='';
    }
    return evaluate((index>0?str.substring(0,index):'')+value+(secondIndex<str.length-1?str.substring(secondIndex+1):''));
}

function evaluate3(str){
    var re=/\$(\w+)/;
    var arr=re.exec(str);
    if(arr!==null){
        var index=arr.index;
        var length=arr[0].length;
        var value=globals[arr[1]];
        str=str.substring(0,index)+value+str.substring(index+length);
        return evaluate(str);
    }
    try {
        var value=eval(str.replace('_N',_N));
        return value;
    }catch(err){
        console.log(str);
        return str;
    }
}

function evaluate4(str){
    try {
        var value=eval(str);
        return value;
    }catch(err){
        console.log(str);
        return undefined;
    }
}

function outputError(error) {
    $output.val(function(ind,val){
        return val+error+'\n';
    });
}
</script>
</body>
</html>

bmarks

Posted 2014-10-21T02:41:05.133

Reputation: 2 114

Bug: In a generate expression, only the first _n is replaced. – LegionMammal978 – 2015-10-12T10:11:23.600

I'm guessing JS replace method only replaces the first instance. Is there a way to change that? – bmarks – 2015-10-12T12:46:38.957

str.replace(/_n/g,i+1) – LegionMammal978 – 2015-10-12T21:18:53.873

ಠ_ಠ you used jQuery. – Conor O'Brien – 2015-11-04T03:59:38.590

4

Fourier

This is now a complete version of Fourier, translated directly from the Python. This should stay up to date.

<!DOCTYPE html> <html> <head> <title>FourIDE - Editor</title> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css"> <style> body { padding-left: 2em; background-color: gainsboro } textarea, input { font-family: Courier New; border: none } </style> </head> <body> <h1><a href="https://github.com/beta-decay/Fourier">Fourier</a> Interpreter</h1> <h3>Code:</h3> <textarea id="code" rows="4" oninput="javascript:chars()" cols="40"></textarea> <br/> Function: <select id="funcSelect"> <option value="exec">Execute code</option> <option value="expl">Explode code</option> <option value="debg">Debug code</option> </select> <br/> <h3>Input:</h3> <textarea id="input" rows="4" cols="40"></textarea> <br/><br/> <button id="run" onclick="javascript:main()">Engage</button> <button id="clear" onclick="javascript:clearText()">Clear</button> <br/> Character count: <span id="ccount">0</span> characters <h3>Output:</h3> <textarea id="output" readonly="readonly" rows="4" cols="40" style="cursor: default;"></textarea> <br/> <a href="javascript:getperma()">Get permalink</a> <br/> <h3>Character reference</h3> <h4>ASCII code reference</h4> <input id="charRef" cols="40" oninput="javascript:getCharCode()"> <br/> <input id="asciiRef" readonly="readonly" cols="40" style="background-color: #c2c2c2; cursor: default;"> <script src="https://rawgit.com/beta-decay/Fourier/master/javascript/editor.js"></script> <script src="https://rawgit.com/beta-decay/Fourier/master/javascript/debug.js"></script> <script src="https://rawgit.com/beta-decay/Fourier/master/javascript/interpreter.js"></script> <script> function main() { document.getElementById("output").style.color = "black"; choice = document.getElementById("funcSelect").value; if (choice == "exec") { interpreter(); } else if (choice == "debg") { debug(); } else { document.getElementById("output").value = "Sorry, those features are not ready yet"; } } </script> </body> </html>

See more:

Beta Decay

Posted 2014-10-21T02:41:05.133

Reputation: 21 478

2ᴜɢʜ ɴᴏ ᴊQᴜᴇʀʏʏʏ – Conor O'Brien – 2015-11-02T23:49:10.450

2@CᴏɴᴏʀO'Bʀɪᴇɴ Gah, stop with the small caps :D – Beta Decay – 2015-11-03T06:59:31.553

Pure JS master race! Subvert the dominant jQuery paradigm! – Mama Fun Roll – 2015-11-15T17:47:22.927

4

Labyrinth

An interpreter for Labyrinth, a totally awesome language! (My interpreter doesn't have big integers...)

/*jshint esnext: true */
//Note labyrinth X Y are swapped!
//Syntax highlighting?
//Bigint library?
//Use: https://developers.google.com/web/updates/2014/05/Web-Animations-element.animate-is-now-in-Chrome-36
//Save option 
//a => ASCII code (Explanation of the commands)

var p; //pointer
var labyrinth; //code
var main, auxiliary; //stacks, main.length-1=top
var direction; //N=0,E=1,S=2,W=3;
var inpLoc;
var sucide;
var step;
var debug;

var spiders = false;

var pointerIcons = ["▲", "►", "▼", "◄", "X"];

function mod(x, y) {
  return ((x % y) + y) % y;
}

Array.prototype.top = function() {
  if (this.length === 0) {
    return 0;
  }
  return this[this.length - 1];
};

Array.prototype._pop = Array.prototype.pop;
Array.prototype.pop = function(x) {
  var temp = this._pop();
  return temp !== undefined ? temp : 0;
};

function reset() {
  p = [0, 0];
  labyrinth = lab.innerHTML.replace(/\[/g, " ").replace(/\]/g, " ").replace(/&lt;/g, "<").replace(/&gt;/g, ">").replace(/\n*$|(<br>)*$/, "").split(/<br>|\n/g);
  var maxLength = 0;
  for (var i = 0; i < labyrinth.length; i++) {
    if (maxLength < labyrinth[i].length)
      maxLength = labyrinth[i].length;
  }

  for (i = 0; i < labyrinth.length; i++) {
    labyrinth[i] = (labyrinth[i] + " ".repeat(maxLength - labyrinth[i].length)).split("");
  }

  //Pointer pos
  loop:
    for (i = 0; i < labyrinth.length; i++) {
      for (j = 0; j < labyrinth[i].length; j++) {
        if (labyrinth[i][j] !== " ") {
          p = [j, i];
          break loop;
        }
      }
    }

  main = [];
  auxiliary = [];
  direction = 1;
  sucide = false;
  inpLoc = 0;
  step = 1;
  O.value = "";
}

function run() {
  reset();
  if (debug) {
    spiderContainer.innerHTML = "";
    debugButton.disabled = false;
    lab.contentEditable = false;
    stepButton.disabled = false;
  }
  //Start if
  if (lab.innerHTML.replace(/\[/g, "").replace(/\]/g, "").replace(/<br>|\n/g, "") !== "") {
    if (!debug) {
      while (!sucide) {
        updatePointer();
      }
    } else {
      updatePointer();
    }
  } else {
    stepButton.disabled = "disabled";
    lab.contentEditable = "true";
  }
}

function updatePointer() {
  if (sucide) {
    stepButton.disabled = "disabled";
    lab.contentEditable = "true";
    pointerOverlay.innerHTML = "";
    return -1; //End
  }
  stepDiv.innerHTML = step;
  step++;
  execChar(labyrinth[p[1]][p[0]]);


  var ret = updateDirection();
  //If there are walls
  if (ret != -1) {
    direction = mod(direction, 4);

    if (direction === 0) {
      p[1] --;
    } else if (direction == 1) {
      p[0] ++;
    } else if (direction == 2) {
      p[1] ++;
    } else if (direction == 3) {
      p[0] --;
    }
  }
  deb.value =
    "Pointer: " + p +
    "\nCommand: " + labyrinth[p[1]][p[0]] +
    "\nMain [" + main + " | " + auxiliary.slice().reverse() + "] Auxiliary";
  if (debug) {
    pointerOverlay.innerHTML = "\n".repeat(p[1]) + " ".repeat(p[0]) + (pointerIcons[ret == -1 ? 4 : direction]);
  }

}

function updateGrid() {
  lab.innerHTML = "";
  labyrinth.forEach(s => lab.innerHTML += s.join("") + "\n");
}

function rotVert(rotWhich) {
  var temp;
  for (var i = 0; i < labyrinth.length; i++) {
    if (i === 0) {
      temp = labyrinth[0][rotWhich];
    }
    if (i == labyrinth.length - 1) {
      labyrinth[i][rotWhich] = temp;
    } else {
      labyrinth[i][rotWhich] = labyrinth[i + 1][rotWhich];
    }
    if (labyrinth[i][rotWhich] === undefined) {
      labyrinth[i].splice(rotWhich, 1);
    }
  }
}

function rotVert2(rotWhich) {
  var temp;
  for (var i = labyrinth.length - 1; i >= 0; i--) {
    if (i == labyrinth.length - 1) {
      temp = labyrinth[i][rotWhich];
    }
    if (i === 0) {
      labyrinth[i][rotWhich] = temp;
    } else {
      labyrinth[i][rotWhich] = labyrinth[i - 1][rotWhich];
    }
    if (labyrinth[i][rotWhich] === undefined) {
      labyrinth[i].splice(rotWhich, 1);
    }
  }
}

function lookAt(d) {
  if (mod(d, 4) === 0) {
    try {
      return labyrinth[p[1] - 1][p[0]];
    } catch (e) {
      return " ";
    }
  } else if (mod(d, 4) == 1) {
    try {
      return labyrinth[p[1]][p[0] + 1];
    } catch (e) {
      return " ";
    }
  } else if (mod(d, 4) == 2) {
    try {
      return labyrinth[p[1] + 1][p[0]];
    } catch (e) {
      return " ";
    }
  } else if (mod(d, 4) == 3) {
    try {
      return labyrinth[p[1]][p[0] - 1];
    } catch (e) {
      return " ";
    }
  }
}

function isWall(d) {
  if (lookAt(d) == " " || lookAt(d) === undefined) {
    return true;
  } else {
    return false;
  }
}

function updateDirection() {
  var numOfWalls = 0;
  if (!isWall(0)) {
    numOfWalls++;
  }
  if (!isWall(1)) {
    numOfWalls++;
  }
  if (!isWall(2)) {
    numOfWalls++;
  }
  if (!isWall(3)) {
    numOfWalls++;
  }

  if (numOfWalls == 4) {
    if (main.top() < 0) {
      direction -= 1;
    } else if (main.top() > 0) {
      direction += 1;
    }
  } else if (numOfWalls == 3) {
    if (main.top() < 0) {
      direction -= 1;
    } else if (main.top() > 0) {
      direction += 1;
    }
    if (isWall(direction)) {
      direction += 2;
    }
  } else if (numOfWalls == 2) {
    if (isWall(direction + 2)) {
      if (isWall(direction)) {
        //Special case
        if (main.top() < 0) {
          direction -= 1;
        } else if (main.top() > 0) {
          direction += 1;
        } else {
          direction += Math.random() < 0.5 ? -1 : 1;
        }
      } else {
        //Keep moving..
      }
    } else {
      for (var i = 0; i <= 3; i++) {
        if (!isWall(i) && i != mod((direction + 2), 4)) {
          direction = i;
          break;
        }
      }
    }
  } else if (numOfWalls == 1) {
    if (!isWall(0)) {
      direction = 0;
    } else
    if (!isWall(1)) {
      direction = 1;
    } else
    if (!isWall(2)) {
      direction = 2;
    } else
    if (!isWall(3)) {
      direction = 3;
    }
  } else {
    return -1;
  }
}

function execChar(char) {
  var a;
  var b;
  if (/\d/.exec(char)) {
    main.push(main.pop() * 10 + (+char));
  } else switch (char) {
    case '!':
      O.value += main.pop();
      break;
    case '"': //Nothing...
      break;
    case '#':
      main.push(main.length);
      break;
    case '$':
      main.push(main.pop() ^ main.pop());
      break;
    case '%':
      a = main.pop();
      b = main.pop();
      main.push(mod(b, a));
      break;
    case '&':
      main.push(main.pop() & main.pop());
      break;
    case "'":
      //Debug-Unimplemented
      break;
    case '(':
      main.push(main.pop() - 1);
      break;
    case ')':
      main.push(main.pop() + 1);
      break;
    case '*':
      main.push(main.pop() * main.pop());
      break;
    case '+':
      main.push(main.pop() + main.pop());
      break;
    case ',':
      a = I.value.charCodeAt(inpLoc);
      if (Number.isNaN(a)) {
        main.push(-1);
      } else {
        main.push(a);
        inpLoc++;
      }
      break;
    case '-':
      a = main.pop();
      b = main.pop();
      main.push(b - a);
      break;
    case '.':
      O.value += String.fromCharCode(mod(main.pop(), 256));
      break;
    case '/':
      a = main.pop();
      b = main.pop();
      if (a === 0) {
        sucide = true;
        throw "Stop that";
      }
      main.push(Math.floor(b / a));
      break;
    case ':':
      main.push(main.top());
      break;
    case ';':
      main.pop();
      break;
    case '<':
      //MOVE IP
      b = main.pop();
      a = mod(p[1] + b, labyrinth.length);
      labyrinth[a].push(labyrinth[a].shift());
      updateGrid();
      if (b === 0) {
        p[0] = mod(p[0] - 1, labyrinth[a].length);
      }
      break;
    case '=':
      a = main.pop();
      b = auxiliary.pop();
      main.push(b);
      auxiliary.push(a);
      break;
    case '>':
      //MOVE IP
      b = main.pop();
      a = mod(p[1] + b, labyrinth.length);
      labyrinth[a].unshift(labyrinth[a].pop());
      updateGrid();
      if (b === 0) {
        p[0] = mod(p[0] + 1, labyrinth[a].length);
      }
      break;
    case '?':
      try {
        a = I.value.substr(inpLoc);
        b = (+(/[\+-]?\d+/.exec(a)[0]));
        main.push(b);
        inpLoc += a.search(/[\+-]?\d+/) + (b + "").length;
      } catch (e) {
        main.push(0);
        inpLoc = I.value.length;
      }
      break;
    case '@':
      sucide = true;
      break;
    case '\\':
      O.value += "\n";
      break;
    case '^': //Fix!
      b = main.pop();
      labyrinth = transpose(labyrinth);
      a = mod(p[0] + b, labyrinth[p[1]].length);
      labyrinth[a].unshift(labyrinth[a].pop());
      if (b === 0) {
        p[1] = mod(p[1] - 1, labyrinth.length);
      }
      labyrinth = transpose(labyrinth);
      updateGrid();
      break;
    case '_':
      main.push(0);
      break;
    case '`':
      main.push(-main.pop());
      break;
    case 'v': //Fix!
      b = main.pop();
      a = mod(p[0] + b, labyrinth[p[1]].length);
      rotVert2(a);
      updateGrid();
      if (b === 0) {
        p[1] = mod(p[1] + 1, labyrinth.length);
      }
      break;
    case '{':
      main.push(auxiliary.pop());
      break;
    case '|':
      main.push(main.pop() | main.pop());
      break;
    case '}':
      auxiliary.push(main.pop());
      break;
    case '~':
      main.push(~main.pop());
      break;
  }
}

function transpose(array) {
  var newArray = array[0].map(function(col, i) {
    return array.map(function(row) {
      return row[i];
    });
  });
  return newArray;
}

//Spiders
function startDebug() {
  if (spiders && document.cookie.split("youWereHere=")[1] === undefined) {
    document.cookie = "youWereHere=yep";
    debugButton.disabled = 'disabled';
    var spiderImg = document.createElement('img');
    spiderImg.src = "http://i.imgur.com/fsYfS9H.png"; //http://i.imgur.com/PRPmqMJ.gif
    spiderImg.className = "spider";

    var svg = document.createElementNS('http://www.w3.org/2000/svg', 'line');
    svg.setAttribute("stroke-width", 2);
    var rn = Math.random() * 10 + 5;
    for (var i = 0; i < rn; i++) {
      var tempSpider = spiderImg.cloneNode(true);

      var left = Math.min(Math.random() * innerWidth, innerWidth - 50);
      var top = -Math.random() * 300;
      tempSpider.style.left = left - 26 + "px";
      tempSpider.style.top = top - 5 + "px";

      var tempSvg = svg.cloneNode(true);
      tempSvg.setAttribute("y1", top - innerHeight - 100);
      tempSvg.setAttribute("y2", top);
      tempSvg.setAttribute("x1", left);
      tempSvg.setAttribute("x2", left);

      spiderContainer.appendChild(tempSpider);
      svgId.appendChild(tempSvg);
      // Make sure the initial state is applied.
      window.getComputedStyle(tempSpider).transform; // jshint ignore:line
      window.getComputedStyle(tempSvg).transform; // jshint ignore:line

      tempSpider.style.transform = "translateY(" + (innerHeight + 48 + top) + "px)";
      tempSvg.style.transform = "translateY(" + (innerHeight + 48 + top) + "px)";
    }
    setTimeout(fallDown, 2500);
    debug = true;
    setTimeout(run, 4000);
  } else {
    debug = true;
    run();
  }
}

function fallDown() {
  svgId.innerHTML = "";
  var spiders = document.getElementsByClassName("spider");
  for (var i = 0; i < spiders.length; i++) {
    spiders[i].style.top = innerHeight + "px";
  }
}


//Maze proper copy
lab.addEventListener("paste", function(e) {
  // cancel paste
  e.preventDefault();

  // get text representation of clipboard
  var text = e.clipboardData.getData("text/plain");

  // insert text manually
  //document.execCommand("insertHTML", false, text);
  lab.innerHTML = text;
});
body {
  background: #484848;
}
h3{
  font-family: sans-serif;
  color: white;
  font-weight: bolder;
  margin: 6px;
  margin-bottom: 0px;
}
#inputHeader, #outputHeader, #debugHeader{
  flex: 1;
}
#debugHeader{
  display: none;
}
#deb {
  display: none;
}
.container {
  width: 100%;
  position: relative;
  display: flex;
  flex-flow: row wrap;
}
.container pre,textarea {
  flex: 1;
  min-height: 175px;
  background: #202020;
  color: white;
  border: #202020;
  margin: 6px;
  font: monospace;
  resize: vertical;
}
.container pre[contentEditable=false] {
  cursor: not-allowed;
}

#lab {
  width: 100%;
  min-height: 150px;
}

#pointerOverlay{
  width: 100%;
  position: absolute;
  background: rgba(0,0,0,0);
  /*width: 100%;*/
  resize: vertical;
  height: 200px;
  pointer-events: none;
  color: rgba(255,0,0,0.7);
  left: 0px;
}
button:not(.run) {
  vertical-align: top;
  border: #303030;
  color: white;
  font-weight: bold;
  font-size: 1em;
  letter-spacing: 1px;
  background: #303030;
  cursor: pointer;
  min-width: 100px;
  min-height: 45px;
  margin: 5px;
  line-height: 50px;
}
button:not(.run):hover {
  background: #202020;
}

button:not(.run)[disabled] {
  background: #404040;
  cursor: not-allowed;
}
.buttonImg{
  width: 40px;
  vertical-align: middle;
  transform: translateX(-10px);
}
<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <title>Labyrinth</title>
</head>

<body>
  <button onclick="debug=false; run(); deb.style.display = 'none'; debugHeader.style.display = 'none';"><img src="http://i.imgur.com/XvHQHX9.png" class="buttonImg">Run</button>
  <button id="debugButton" onclick="startDebug(); deb.style.display='flex'; debugHeader.style.display = 'flex';"><img src="http://i.imgur.com/Th4Kyvd.png" class="buttonImg">Debug</button>
  <button id="stepButton" onclick="updatePointer();" disabled>Step: <span id="stepDiv">1</span>
  </button>
  <button onclick="sucide = true; updatePointer();">Stop</button>

  <div class="container">
    <h3>Maze:</h3>
    <div style="width:100%;  margin: -6px;"></div>
    <pre id="pointerOverlay"></pre>
    <pre id="lab" contentEditable="true">,)@
.(</pre>
    <div style="width:100%;"></div>
    
    <h3 id="inputHeader">Input:</h3>
    <h3 id="outputHeader">Output:</h3>
    <h3 id="debugHeader">Debugging:</h3>
    <div style="width:100%"></div>
    <textarea id="I">Labyrinth rocks!</textarea>
    <textarea id="O"></textarea>
    <textarea id="deb"></textarea>
  </div>
  <svg width="100%" id="svgId">
  </svg>
  <div id="spiderContainer">
  </div>
</body>

</html>

Here is the most up-to-date version: http://output.jsbin.com/cuzoxu

Stefnotch

Posted 2014-10-21T02:41:05.133

Reputation: 607

Nice interpreter design! And the debugger. You made me want to learn this language! – RedClover – 2017-09-30T16:11:06.580

@Soaku Thank you! If you have any suggestions/complaints/etc, feel free to tell me. I'm happy to implement pretty much anything! – Stefnotch – 2017-09-30T17:57:35.230

3

Unary (to Brainfuck)

Simply converts Unary to Brainfuck. This could very easily be coupled with one of the Brainfuck interpreters to run Unary code directly - no need to reinvent the wheel!

function interpret() {
  var result = "";
  var table = "><+-.,[]"
  var option = document.getElementById("option").checked
  var code = document.getElementById("code").value
  var number = option ? parseInt(code, 10) : code.length;
  number = number.toString(2)
  if (number.length % 3 == 1) {
    number = number.slice(1)
    for (var i = 0; i < number.length; i += 3) {
      var instruction = number[i] + number[i + 1] + number[i + 2]
      instruction = parseInt(instruction, 2)
      result += table[instruction];
    }
  } else {
    result = "Error: program binary cannot be split into triplets."
  }
  document.getElementById("result").value = result;
}
Input as Number of 0's
<input id="option" type="checkbox" />
<br />
<br />
<input id="code" type="text" style="width:90%;" />
<br />
<br />
<input id="run" type="button" value="Run!" onclick="interpret()" />
<br />
<br />
<input id="result" type="text" style="width:90%;" />

jrich

Posted 2014-10-21T02:41:05.133

Reputation: 3 898

3

Hello++

The easiest language to implement AND learn!

<h1>Hello++ Interpreter</h1>
<hr>
<h2>Code</h2>
<textarea id=c></textarea>
<br>
<button onclick='o.value="",c.value.replace(/[^h]/ig,"").split("").map(function($){$&&(o.value+="Hello World\n")})'>Say hello to the output!</button>
<h2>Output</h2>
<textarea id=o readonly></textarea>
<style>*{font-family:monospace}textarea{width:100%}</style>

Mama Fun Roll

Posted 2014-10-21T02:41:05.133

Reputation: 7 234

3

CHIQRSX9+

HQ9+ with Turing-completeness. I stuck as closely to the specs as possible.

function interpret(c,i){
 c.split('').map(function(v,I){
  if(v=='c'||v=='C')out.push(i);
  else if(v=='h'||v=='H')out.push('Hello, world!\n');
  else if(v=='i'||v=='I')interpret(i,"");
  else if(v=='q'||v=='Q')out.push(c);
  else if(v=='r'||v=='R')out.push(i.replace(/[a-zA-Z]/g,function(C){return String.fromCharCode((C<="Z"?90:122)>=(C=C.charCodeAt(0)+13)?C:C-26);}));
  else if(v=='s'||v=='S')out.push(i.split('\n').sort().join('\n'));
  else if(v=='x'||v=='X')eval(c.split().join(0|Math.random()*256));
  else if(v=='9')for(var o,e,n=100,t=" on the wall";n-->-1;)o=e+t+", "+e+".\n"+(n>-1?"Take one down, pass it around, ":"Go to the store and buy some more, ")+(e=(0>n?99:n||"no more")+" bottle"+(1!=n?"s":"")+" of beer")+t+".\n",99>n&&out.push(o);
  else if(v=='+')acc++;
  else out.push("\n\t!ERROR!\t\n");
 });
  return out.join('').match(/\n\t!ERROR!\t\n/g)?'ERROR: Unknown command':out.join('')
}
<h1>CHIQRSX9+ Interpreter</h1>
<hr>
<h2>Code</h2>
<textarea id=c></textarea>
<br>
<h2>Input</h2>
<textarea id=i></textarea>
<br>
<button onclick='out=[],acc=0;o.value=interpret(c.value,i.value)'>Run!</button>
<h2>Output</h2>
<textarea id=o readonly></textarea>
<style>*{font-family:monospace}textarea{width:100%}</style>

Mama Fun Roll

Posted 2014-10-21T02:41:05.133

Reputation: 7 234

3

Neoscript

Only work on ES6 browsers

"use strict";

function nodejswrapper(url) {
    let data = $.ajax({
        type: "GET",
        url: url,
        async: false
    }).responseText;
    return new Function("let module={};" + data + "return module.exports;")();
}

let tokenize, parse, transpile;

function compile(code) {
    return transpile(parse(tokenize(code)));
}

console.log = function(msg) {
    $("#console").append((""+msg).replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;") + "<br />");
}
console.clear = function(msg) {
    $("#console").html("");
}

$(function() {
    tokenize = nodejswrapper("http://crossorigin.me/https://raw.githubusercontent.com/tuxcrafting/neoscript/master/src/tokenizer.js").tokenize;
    parse = nodejswrapper("http://crossorigin.me/https://raw.githubusercontent.com/tuxcrafting/neoscript/master/src/parser.js").parse;
    transpile = nodejswrapper("http://crossorigin.me/https://raw.githubusercontent.com/tuxcrafting/neoscript/master/src/transpilers/javascript.js").transpile;
    $("#run").on("click", function() {
        let code = $("#code").val();
        let js = compile(code);
        new Function(js)();
    });
});
#console {
    width: 400px;
    height: 400px;
    overflow: auto;
    background: black;
    color: white;
    float: right;
    font-family: monospace;
}
<script type="application/javascript" src="https://raw.githubusercontent.com/tuxcrafting/neoscript/master/src/stdlib/stdlib.js"></script>
<script type="application/javascript" src="https://code.jquery.com/jquery-1.12.3.min.js"></script>
<div id="console"></div>
<textarea id="code" cols="40" rows="15"></textarea><br />
<button id="run">Run</button>

TuxCrafting

Posted 2014-10-21T02:41:05.133

Reputation: 4 547

2

Brainfuck

What, this is still code golf, r-r-right?

On a serious note, I implemented a bunch of esoteric languages in javascript a while ago, feel free to use them, it is pretty much plug and play: https://dl.dropboxusercontent.com/u/69377299/lang%20js/main.html

function run () {
  var code = document.getElementById("code").value;
  var inp  = document.getElementById("in").value;
  var ret  = x(code, inp);
  
  document.getElementById("out").value = ret;
}

function B(s,i,c) {
 i+=1+-c*2
 return s[i]==']['[+c]?i:B(s,s[i]==']['[+!c]?B(s,i,c):i,c)
}

function x(c, a) {
 var y, l=0, p=0, i=0, q=0, r='', t=[]
 for (; q<1000; q++) t[q] = 0

 for (;i<c.length; i++) {
  y = c.charCodeAt(i)
  p -= (y==60) - (y==62)
    t[p] += y==44? (l>a.length? 0: a.charCodeAt(l++))-t[p]: -((y==45) - (y==43))
  if (y==46) r += String.fromCharCode(t[p])
  if (y==91 + (t[p]>0)*2) i = B(c,i,y==93)
 }

 return r;
}
Code: <input id="code"></input>
<button onclick="run()">Run</button>

<br>
Input: <input id="in"></input>

<br><br>
Output: <input id="out" disabled></input>

BlackCap

Posted 2014-10-21T02:41:05.133

Reputation: 3 576