esolang

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

Expected: Hello, world!

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

CommandDoes
name;Open or close the block of that label
send XPrint each line of X's block
readto XMake a line of input X's block; at the end of input the block is empty
inject X=S/RPut R in place of every match of the regex S in X's block
skipPass over a block that opens on the next line, else go back to the start of the innermost block, else halt
skipif Xskip if X's block has a line
skipq X Yskip 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.

Expected: Hello, Inject!

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.

Expected: Hi there

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.

Expected: *** ** *

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

Further

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 inject

Source

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