esolang

Mutating Stack Machine

The program is the stack. Code is eaten from the bottom while data piles up on top.

Plugnburn, 2013 · Turing complete: unknown · ●●● hard · esolangs.org

Tested with 6 known programs · Test method

Run it

Expected: hello world

The idea

Plugnburn wrote Mutating Stack Machine on August 4, 2013, to make the smallest usable stack machine in JavaScript. The reference is a single function of 274 bytes. Underload was its model.

Read the story

There is one stack and it holds strings. At the start it is the program, one character per item, the first character at the bottom. Each step takes the item at the bottom and runs it. An instruction works on the top of the stack, and anything else is pushed on top. So the program and its data are the same list, read from one end and written at the other, and data that reaches the bottom runs as code.

The run ends when only one item is left. That item is the output.

The machine

ItemDoes
;Duplicate the top
:Pop the top and push its characters one by one
,Drop the top
/Swap the top two
.Pop two and push the top with the second appended
?Skip the next item if the top two are equal
'Push the next item as it is, even an instruction
anything elsePush it on top

Example

The trivial Hello world.

The trivial Hello world. The eleven letters are taken from the bottom and pushed back on top in the same order. Then the ten dots join them, the top first.

This one builds its code at run time. The quoted . and / are joined into /., which is copied and joined until it is ten pairs long. Then : splits that string into twenty items. The stack is now hello world/./././././././././., the second Hello world, and it runs from there.

Expected: hello world

Any program of one character is a quine, as the stack is left with that character. The specification also has a quine of 1,429 characters, which works too.

Expected: Q

Patterns it teaches

Further

Self-modifying

Stack-based

Write your own in Run Convert a program from Mutating Stack Machine

Implementation

JavaScript interpreter, source and command line

A JavaScript interpreter, used in the browser and on the command line. Id: mutating-stack-machine.

Command line

shib run mutating-stack-machine -e 'Q'
shib manual mutating-stack-machine

Source

Module
langs/wiki/ports.js
Interpreter
langs/wiki/mutating-stack-machine.js

Docs: Kernel instructions · The .shib format · Command line · Adding a language

Source esolangs.org (CC0) · Edit on esolangs.org