refract
><> with blocks, a portal and stacks inside stacks.
Phase, 2015 · Turing complete: yes · ●●○ moderate · esolangs.org
Tested with 8 known programs · Test method
Run it
The idea
Phase made refract in June 2015, after ><>, GolfScript and Portal. The program is a grid that wraps at every edge, and the pointer swims over it as the fish does in ><>, turning at arrows and mirrors. On top of that come blocks, which give one character a body of code, a portal the pointer can jump back to, and stacks that open inside other stacks.
Read the story
This implementation leaves out x, y and z. x sends the pointer off in one of eight directions at random, and y and z turn it onto diagonals in ways the specification gives only in part.
The machine
| Command | Does |
|---|---|
> < ^ v | Point the way |
/ \ | _ # | Mirrors, as in ><> |
! ? | Skip the next cell; pop and skip it on 0 |
0 to 9, a to f | Push 0 to 15 |
+ - * , % | Pop x and y and push y op x; , divides with a fraction |
= ) ( | Push 1 when y equals, is greater or is less than x, else 0 |
"..." '...' | Push each character |
: ~ $ @ r | Duplicate, drop, swap, bring the third value up, reverse |
[ ] | Pop n and move the top n values to a new stack; pour it back |
o n i j | Print a character or a number; read a character or a number |
& g p | Move a value to the register and back; read and write the grid |
Ø O | Set the portal; jump to it |
{body}c | Make a block named c |
; | Halt |
Example
The Hello World.
The Hello World. It pushes the text, reverses it so h is on top, and goes down into a loop that prints one character per lap. m pushes 1 once the stack is empty, and only then does ? let the pointer reach ;.
Stacks inside stacks. 3[ moves the top three values to a new stack, r reverses only those, and ] pours them back.
The portal example. The second test is true, so Ø sets the portal there, and O at the bottom sends the pointer back to it, still going down.
Patterns it teaches
- Two-dimensional control flow, with mirrors and a portal.
- Stack machines, with stacks that nest.
- Self-modifying code, as
pwrites into the grid.
Further
See also
Two-dimensional languages
Stack-based
Write your own in Run Convert a program from refract
Implementation
JavaScript interpreter, source and command line
A JavaScript interpreter, used in the browser and on the command line. Id: refract.
Command line
shib run refract -e 'v;<
< ^'shib manual refractSource
- Module
langs/wiki/ports.js- Interpreter
langs/wiki/refract.js
Docs: Kernel instructions · The .shib format · Command line · Adding a language
Source esolangs.org (CC0) · Edit on esolangs.org