Inject
Labels cut the program into blocks of lines. The blocks are all the memory there is, and a regex can rewrite them while they run.
PythonshellDebugwindow, 2021 · Turing complete: yes · ●●○ moderate · esolangs.org
Tested with 4 known programs · Test method
Run it
The idea
PythonshellDebugwindow made Inject in 2021 and named it after its first command. A label is a name with a semicolon after it, on a line of its own. The first time a label is written it opens a block, and the second time it closes it. The lines in between are the block, and blocks may overlap.
Read the story
There are no numbers and no variables. Data lives in blocks, and so does code. inject rewrites a block with a regular expression, so a program can change the lines it will run next. The only jump is skip, which either passes over a block or goes back to the start of the block it stands in.
The machine
| Command | Does |
|---|---|
name; | Open or close the block of that label |
send X | Print each line of X's block |
readto X | Make a line of input X's block; at the end of input the block is empty |
inject X=S/R | Put R in place of every match of the regex S in X's block |
skip | Pass over a block that opens on the next line, else go back to the start of the innermost block, else halt |
skipif X | skip if X's block has a line |
skipq X Y | skip if the blocks of X and Y are equal |
A line that is no command and no label is data, and it does nothing when it runs. The regex knows |, ., ( ), *, + and ? as the specification asks, and also ^ and $. In R, \1 gives a group.
Example
send prints the block's one line.
send prints the block's one line. skip then jumps over the block, so the data never runs.
The same with inject first, which rewrites the line before it is printed.
The cat. skipif stands just before the label that closes loop, so it goes back to the top for as long as readto finds a line.
A loop that rewrites its own data. Each pass prints the bar and takes its last character off with the regex .$, until the block is empty and skipif lets the run fall through. It prints three lines.
The truth machine runs the wrong way round. Its skipq data 0 goes back to the start of the loop when the input is 0, so on 0 it prints 0 for ever, and on 1 it prints 1 once and stops.
Patterns it teaches
- Self-modifying code: code and data are both lines in blocks.
- String rewriting: regex replacement is the one way to compute.
Further
See also
Also in Rewriting
Write your own in Run Convert a program from Inject
Implementation
JavaScript interpreter, source and command line
A JavaScript interpreter, used in the browser and on the command line. Id: inject.
Command line
shib run inject -e 'send data
skip
data;
Hello, world!
data;'shib manual injectSource
- Module
langs/wiki/ports.js- Interpreter
langs/wiki/inject.js
Docs: Kernel instructions · The .shib format · Command line · Adding a language
Source esolangs.org (CC0) · Edit on esolangs.org