T-Regs
Regular expressions and one text. Add labels and a goto, and that is the whole language.
2012 · Turing complete: yes · ●●○ moderate · esolangs.org
Tested with 9 known programs · Test method
Run it
The idea
T-Regs was written in August 2012 to find the least syntax that turns regular expressions into a usable language. A program is a list of find and replace lines, all working on one body of text. The text starts as the input and is printed when the last line has run.
Read the story
There are no variables, no conditions and no loops. A line that captures a group named goto jumps to the line with that label, and a line whose pattern does not match changes nothing. That is enough to build functions with return labels, as the subtraction program does.
The machine
Every line is label /find/replace/flags.
| Part | Does |
|---|---|
10 | The label; lines run in the order of their labels, not where they stand |
/find/ | A regular expression; without g only its first match is replaced |
/replace/ | The new text; $0 is the match, $1 a group, ${name} a named group |
g i m s | Every match, ignore case, lines, dot matches a line feed |
n | Only named groups capture |
(?<goto>40) | After the line, go on at label 40 |
# | A comment line |
The patterns are JavaScript's, as the specification's interpreter runs on XRegExp.
Example
Lines run by label, so 30 comes last.
Lines run by label, so 30 comes last.
Line 20 captures 40 as goto, so line 30 is skipped.
With i, the h also matches H, and with n the inner group does not capture.
The subtraction program takes one from each number with a function at label 10 until one of them is 0, so 10 - 3 gives 7 and 3 - 10 gives -7. Its Game of Life computes the next generation of a field 40 cells wide that wraps at the edges, in 13 lines.
Patterns it teaches
- String rewriting: the program is only replacements on one text, with goto for control.
Further
See also
String-rewriting paradigm
Write your own in Run Convert a program from T-Regs
Implementation
JavaScript interpreter, source and command line
A JavaScript interpreter, used in the browser and on the command line. Id: t-regs.
Command line
shib run t-regs -e '10 /.*/Hello world!/'shib manual t-regsSource
- Module
langs/wiki/ports.js- Interpreter
langs/wiki/t-regs.js
Docs: Kernel instructions · The .shib format · Command line · Adding a language
Source esolangs.org (CC0) · Edit on esolangs.org