ForWhile
One loop that counts down, so every program stops, unless it writes more of itself.
Bsoelch, 2023 · Turing complete: unknown · ●●● hard · esolangs.org
Tested with 4 known programs · Test method
Run it
The idea
Bsoelch made ForWhile in 2023 and named it after its one loop. ( takes a count from the stack, and ) goes round again while the value it pops is not 0, but never more often than the count. Calls stop at a depth of 3. So a program that keeps to positive addresses always ends.
Read the story
The program is stored in memory from address -1 downwards and runs from there. It can read its own source with @, which makes a quine easy, and write new code past its own end with $. Copying itself forward like that is the only way to run forever.
The machine
| Command | Does |
|---|---|
12 "text" | Push a number; push the characters and then the length |
( ... ) | Pop a count, push it on every pass, repeat while the popped value is not 0 |
[ ... ] | Skip the block if the popped value is 0 |
{ ... } | Push the address inside and skip the procedure |
? | Call the address on top; } returns |
. : ; ' | Pop, duplicate, copy the second up, swap |
, | Rotate the top n items |
@ $ | Read memory; store the second item at the address on top |
# _ | Print the low byte of the top and keep it; read a byte |
+ - * / % ` | Arithmetic and power | |
& \| ^ ~ ~~ '< '> | Bits, negation and shifts |
< = > ! | Compare; not |
Values are 64-bit integers, and \ comments out the rest of a line.
Example
The string pushes 12 characters and then 12.
The string pushes 12 characters and then 12. Each pass of the loop pushes the count, and , rotates the oldest character left to the top for #. The quine walks down from address -1 and prints each cell until it reads the 0 past its end.
The Fibonacci keeps its print procedure at address 0 and calls it with 0@?.
The truth machine. On 0 its [ skips to the last ] and # prints the 0 it read. On 1 it copies its 34 character body past its own end on every pass, so it prints 1 forever.
Patterns it teaches
- Self-modifying code: the only unbounded loop is code that copies itself forward.
- Quine: the source is in memory, so a quine reads it.
Further
See also
Self-modifying
Stack-based
Write your own in Run Convert a program from ForWhile
Implementation
JavaScript interpreter, source and command line
A JavaScript interpreter, used in the browser and on the command line. Id: forwhile.
Command line
shib run forwhile -e '0~100(.:@#.1-:@).'shib manual forwhileSource
- Module
langs/wiki/ports.js- Interpreter
langs/wiki/forwhile.js
Docs: Kernel instructions · The .shib format · Command line · Adding a language
Source esolangs.org (CC0) · Edit on esolangs.org