T-Regs

From Esolang
Jump to navigation Jump to search

T-Regs is a regex-based esoteric programming language written in August 2012 by User:T-Regs, to answer the question:

what is the least amount of syntax we can add to regular expressions to get an arguably usable language.

Overview

The T-Regs homepage describes the language as:

T-Regs (Pronounced "T-Regs") stands for Text and Regular Expressions. We feel strongly that free text is the most natural way of expressing one's thoughts. Therefore T-Regs does away with the restrictions found in traditional programming languages and is entirely based on free text. Free text which is conveniently manipulated by lots and lots of regular expressions.

T-Regs programs consist of lines of find and replace instructions, which are applied to one body of text, which exists as long as the program runs.

This body of text is either input passed to the program on startup or an empty string if no input was defined.

When the program has executed its last line, this text is returned as output.

Syntax

Comments start with a hash.

# this is a comment

Code starts with a unique numeric label like this:

10 /.*/Hello World/

followed by a find regular expression

10 /.*/Hello World/

and a replace string.

10 /.*/Hello World/

These are separated by slashes

10 /.*/Hello World/

and optionally followed by flags

10 /^(?<greetings>h(a?i|ello)).*$/${greetings} world/in

Program flow

The lines in a program are executed in order of their numerical label. The program

10 /.*/aaa/
30 /.*/bbb/
20 /.*/ccc/

will be executed in order 10, 20, 30 and as such result in bbb.

And then there was goto…

All looping and decision making is done using goto.

If a find has a named capture "goto", the program will attempt to jump to that label.

10 /.*/hello 40/
20 /(?<greeting>\w+) (?<goto>\d+)/${greeting}/
30 /.*/$0 cruel/
40 /.*/$0 world/
# output: "hello world"

In this example at label 20 the value "40" is captured with name goto and the program will jump to label 40 as a consequence.

Sample Programs

Hello world

10 /.*/Hello world!/

Conway's Game of Life

This program calculates the next generation of a game of life field. It could easily be made to loop and calculate all the next generations, but that would be visually uninteresting. Given that the T-Regs flow is input -> execute -> output without any possibility to show intermediate output.

1  /^([^\n]+).*\n([^\n]+)$/$2\n$0\n$1/s
2  /^(.).*(.)$/$2$0$1/mg
3  /(#)|-/$0w$1W/g
4  /w(?=\W*W(?:\Ww\W*W\n?){0}#)/w#/gs
5  /w(?=\W*W(?:\Ww\W*W\n?){1}#)/w#/gs
6  /w(?=\W*W(?:\Ww\W*W\n?){41}#)/w#/gs
7  /w(?=\W*W(?:\Ww\W*W\n?){43}#)/w#/gs
8  /w(?=\W*W(?:\Ww\W*W\n?){83}#)/w#/gs
9  /w(?=\W*W(?:\Ww\W*W\n?){84}#)/w#/gs
10 /w(?=\W*W(?:\Ww\W*W\n?){85}#)/w#/gs
11 /\Ww(?:###|#(#))W(?=(?:\Ww\W*W\n?){42}\1)/#/gs
12 /\Ww#*W/-/g
13 /(?:\n[^\n]+){2}$|.{2}(?=\n|$)//gs

Subtracting 2 numbers

This example demonstrates structured programming and function calls.

Functions are not a part of the language, but with goto you can do anything.

# Goto main program (skip functions)
1    /.*/$0;1000/
2    /^(.*?);(?<goto>1000)$/$1/

# Minus 1
#    input:  number;returnlabel;rest
#    output: result;rest
10   /^(\d+)(;.*)$/$1;15$2/
15   /^(?<left>[1-9]\d*?-*)0(?=0*;)(?<right>.*(?<goto>15).*)/${left}-${right}/
20   /(\d*);15(;.*)/$1;0123456789$2/
25   /^(\d*)(?<lastDigit>\d)(?<lookup>-*;\d*(?<newLastDigit>\d)\2.*)(;.*)$/$1${newLastDigit}${lookup}$5/
30   /(\d*);\d+(;.*)/$1;35$2/
35   /-(?=-*;)(?<right>.*(?<goto>35).*)/9${right}/
40   /^0+([^0;])/$1/
45   /^(\d*);[^;]+;(?<goto>\d+)(;.*)$/$1$3/

# Main calculate "a - b"
1000 /^(\d+)\s*-\s*(\d+)$/$1;$2;0;1050/
1003 /^(\d+);([^0]\d*);\1;(?<goto>\d+)$/-$2/
1005 /^(\d+);(\d+);\2;(?<goto>\d+)$/$1/
1010 /^(\d+);(\d+).*$/10;$1;1020;$2/
1015 /^(?<goto>10);(.*)$/$2/
1020 /^(\d+);(\d+)$/10;$2;1040;$1/
1030 /^(?<goto>10);(.*)$/$2/
1040 /^(\d+);(\d+)$/$2-$1;1000/
1045 /^(\d+-\d+);(?<goto>\d+)$/$1/
1050 /^(\d+).*$/$1/

Implementation

The T-Regs website has an online interpreter with syntax highlighting, breakpoints and a step through debugger. The "trace" tab show you all the intermediate steps which have been taken during execution.

You can load example programs into the interpreter from the demo page interpreter by clicking Run next to the example.

Prior art

T-Regs is not the first language to be based on regular expressions. Several different approaches can be found on this wiki like RegexPL and Thutu (which added regular expressions to Thue's mechanism of computation through text replacement).

Compared to these languages T-Regs stays very close to plain regular expressions. T-Regs is not intentionally hard or unreadable, but to the contrary is trying to be as practical as possible. Given of course, that you want to do general purpose programming through text manipulation.

Links