esolang

Microscript

A golfing language built around one register. The stacks are only where values wait.

SuperJedi224, 2015 · Turing complete: yes · ●●○ moderate · esolangs.org

Tested with 4 known programs · Test method

Run it

Expected: 120

The idea

SuperJedi224 designed Microscript in 2015 as an experiment in code golf. Most golfing languages put everything on a stack. Microscript puts the work in a register instead. A number in the program adds to it, * multiplies it by a popped value, p prints it. The two stacks are storage.

Read the story

Loops are short too. c repeats the rest of the program, up to the next ], as many times as the register says. { repeats until the register is 0. At the end the register is printed, unless h halted the program. So is* is a complete "square the input" program: read, push, multiply, done.

The machine

Two 64-bit registers and two stacks, one of them selected.

CharDoes
123 d5Add to the first register, or subtract
"abc"Push the character codes
s o tPush the register, pop into it, copy the top into it
+ - * / %Pop a value and apply it to the register
a qPop the whole stack as text (q adds quotes)
p P nPrint the register as a number, as a character, a newline
i IRead a number into the register, read a line onto the stack
c $Repeat the rest up to ], or the next command, register-many times
{ … } [ … ]Loop while the register is not 0; run only if the stack is empty
e E ! z 'cTwo or ten to the power, not, zero, set to a character code
l v x C f Z #Swap registers, switch stack, copy, reverse, empty, size
r5 hAdd a random number below 5; halt without the final print

Example

i reads 5 and c1s] runs 1s five times: add one, push, so the stack holds 1 to 5.

i reads 5 and c1s] runs 1s five times: add one, push, so the stack holds 1 to 5. z1 sets the register to 1. The loop multiplies by each popped value, and when the stack is empty [ph] prints 120 and halts.

Patterns it teaches

  • Code golf: the implicit final print and "repeat the rest" save bytes everywhere.
  • Stack machines: an accumulator machine with stacks on the side.
  • Quine: "fCqxah"fCqxah copies its own text to the other stack and prints it twice.
  • Turing completeness: each brainfuck command maps to Microscript.

Try this

  1. Read two numbers and print their product.
  2. Print the numbers 1 to 10, one per line.
  3. Hard: read a line and print it backwards.

Further

Golfing languages

Stack-based

Write your own in Run Convert a program from Microscript

Implementation

JavaScript interpreter, source and command line

A JavaScript interpreter, used in the browser and on the command line. Id: microscript.

Command line

shib run microscript -e 'is'\''1Poc'\''0P]h' --input '3'
shib manual microscript

Source

Module
langs/stack/ports.js
Interpreter
langs/stack/microscript.js

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

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