Motorway
A program is a drive across Britain. Twelve of the motorways do something to a stack on the way.
Ninesquared81, 2022 · Turing complete: unknown · ●●○ moderate · esolangs.org
Tested with 3 known programs · Test method
Run it
The idea
Ninesquared81 made Motorway in 2022. A program is a route along the British motorway network, written as a list of motorways such as M1 or A1M. Twelve motorways are commands. The others only connect them, because each motorway on the route has to meet the next one at a junction. A command motorway in brackets, such as (M25), is driven along without doing its command.
Read the story
So the M25 is a loop and the M4 prints, and getting from one command to the next can take a detour. Everything that is not a motorway is a comment.
The machine
The stack holds bytes that wrap around. Popping an empty stack is an error.
| Motorway | Does |
|---|---|
M6 | Push 0 |
M1 | Add 1 to the top |
M48 M49 | Pop and add to, or subtract from, the next |
M40 M42 M5 | Duplicate, swap, pop |
M60 | Turn c b a into b a c, with a on top |
M4 | Pop and print as a character |
M20 | Read a character |
M25 | Pop, and jump past the matching M26 if it was 0 |
M26 | Jump back to the matching M25 |
The specification lists the junctions of each command motorway. The M1 meets the M6, M18, M25, M45, M62, M69, M621 and A1(M), and not itself. This implementation checks every step that has a command motorway on one side. It does not have the whole network, so a step between two other motorways is taken on trust, and that makes it a subset.
Example
The truth machine reads a digit, takes 48 off it and loops forever if what is left is not 0.
The truth machine reads a digit, takes 48 off it and loops forever if what is left is not 0. On 0 it prints once.
It builds 48 as 4, doubles that twice into 16 and adds three 16s. M1 M45 M1 adds 2. The M1 cannot meet itself, so the route turns round on the M45.
The cat loops until it reads a 0, which this implementation gives at the end of the input.
The Hello world first fills the stack with 2, 4, 8, 16, 32 and 64, then mixes them into each letter. Its route has 276 motorways, 110 of them commands that run, and it prints Hello, World! with a line feed.
Patterns it teaches
- Camouflage: the program reads as directions for a long drive.
- Stack machines: a stack of bytes with a rotate of three.
Further
See also
Stack-based
Write your own in Run Convert a program from Motorway
Implementation
JavaScript interpreter, source and command line
A JavaScript interpreter, used in the browser and on the command line. Id: motorway.
Command line
shib run motorway -e 'read first character: M20
duplicate it: (M25) M40
if non-zero, loop: M25
in the loop, print: M4 (M25)
read next character: M20 (M25)
duplicate: M40 (M25)
end loop: M26
clear up stack: (M25) (M4) M5' --input 'cat '