Milky Way
A stack language where every opcode is one character, from + to the constant Z for 100.
Turing complete: yes · ●○○ easy
Tested with 5 known programs · Hello World on Try It Online: identical except the line end · Test method
Run it
The idea
Milky Way is a small stack language written in Python. A run of digits pushes a number, a quoted string pushes a string, and every other character is an opcode. Capital letters near the end of the alphabet are constants; V is 5, W is 10, Z is 100. Strings and lists are read as Python literals, so "a\nb" holds a newline.
Read the story
Each line of a program runs on its own fresh stack. ! prints the top of the stack and leaves it there. A line that printed nothing writes its bottom value when it ends.
The machine
| Code | Does |
|---|---|
12 "text" [1, 2] | Push a number, a string, a list |
R-Z | Push 1, 2, 3, 4, 5, 10, 20, 50, 100 |
+ - * / | Second from top with the top; / is true division |
; : ^ | Swap, duplicate, drop |
K L G | Range below n, range up to n, sum |
y H Q | Length, reverse, sort |
' ¢ | The whole input, the next input line |
! ¡ @ | Print the top; print and stop; stop |
Example
V3- is 5 minus 3.
V3- is 5 minus 3. WL is the list 0 to 10 and G adds it; nothing was printed, so the line writes the sum. In the last program the string ab is repeated twice and written the same way, which shows it as Python would, in quotes.
This implementation is a subset, after the interpreter in the zachgates/Milky-Way repository, which TIO runs. It has the literals, the stack, arithmetic, list and output opcodes. The control structures ?, %, & and §, tuples and the trigonometric, prime and time opcodes stop the run with the reason.
Patterns it teaches
- Code golf: constants as letters save digits.
- Stack machines: every opcode takes its arguments from the stack.
Try this
- Print 100 times 100.
- Read two lines of input and print their sum.
- Hard: print the sum of the numbers below 50.
Further
- The interpreter, in Python, with the opcode table in its README
See also
Also in Golfing
Write your own in Run Convert a program from Milky Way
Implementation
JavaScript interpreter, source and command line
A JavaScript interpreter, used in the browser and on the command line. Id: milky-way.
Command line
shib run milky-way -e 'WLG'shib manual milky-waySource
- Module
langs/tio/ports.js- Interpreter
langs/tio/milky-way.js
Docs: Kernel instructions · The .shib format · Command line · Adding a language