We are currently working on new rules for what content should and shouldn't be allowed on this website, and are looking for feedback! See Esolang:2026 topicality proposal to view and give feedback on the current draft.

Subtext

From Esolang
Jump to navigation Jump to search
Subtext
Paradigm(s) string rewriting, functional
Designed by rlauff
Appeared in 2026
Memory system The program text itself
Computational class Turing complete
Reference implementation subtext (Rust)
File extension(s) .stx

Subtext is an esoteric programming language created by rlauff in 2026. It is a regex-based text rewriting system: a program is a piece of text, and running it means repeatedly replacing parts of that text by their results until nothing is left to replace. All control flow comes from regular expressions matching against text, and all data, including numbers, is plain text.

Computational model

The interpreter holds a single string, the state, which starts out as the program. It scans the state from left to right for the first job: a scope ({ ... }), a function call (name(...)), a register call (#3), or a function definition (def name { ... }). The job is evaluated and replaced by its result, and scanning continues at the beginning of the replacement, so the result is immediately read back in as code. The program ends when the state contains no more jobs.

Because results are read back in, there is no difference between data and code. A function can return text that defines a new function, or a call to itself, which is then run by whoever received it.

Language description

Scopes

The basic building block is the scope:

{ input :: pattern => output }

The input is evaluated until it no longer changes. Then the pattern, a regular expression for the Rust regex crate, is matched against it. If it matches, the output is evaluated, and the whole scope is replaced by it. The pattern itself is never evaluated, so regex syntax never collides with Subtext syntax. Input, pattern and output are trimmed, so whitespace around them is free for formatting.

A scope can have several arms, separated by ||. The first arm whose pattern matches is taken; if none matches, the program stops with an error.

{ foo :: doesnt match => whatever || ... => bar || even more arms => unreachable }

evaluates to bar.

Registers and the caret

Capture groups of the pattern are stored in registers. In the output, #1 is the first capture group, #2 the second, and so on:

{ world, hello :: (.*), (.*) => #2, #1! }

evaluates to hello, world!. Scopes can be nested, and ^ reaches the registers of enclosing scopes: ^#2 is register 2 of the scope one level up, ^^#2 two levels up.

{ world, hello :: (.*), (.*) => { moon, goodbye :: (.*), (.*) => ^#2, ^#1! #2, #1!} }

evaluates to hello, world! goodbye, moon!.

Functions

def name { pattern => output || pattern => output }

defines a function. Its body is a scope without the input part, and the call name(text) behaves like the scope { text :: body }, except that functions can call themselves. Since the argument of a call is its own scope, a register of the surrounding scope needs at least one caret when passed to a function:

def swap { (.*), (.*) => #2, #1 }
{ world, hello :: (.*), (.*) => swap(^#1, ^#2) }

A function always receives one string. By convention, several arguments are separated by a comma and a space.

The ghost character

A call name is everything in front of the ( up to the last whitespace, so 1zeros(4) calls a function named 1zeros. Similarly, #31 is register 31, not register 3 followed by a 1. The ghost character ~ separates the two and disappears afterwards: 1~zeros(4) and #3~1 do what was meant. A register call swallows one ~ after it, so a register directly followed by a call is written #1~~f(x).

Evaluation protection

Anything inside square brackets is skipped when the interpreter looks for jobs. When a scope or function returns, one layer of brackets is stripped from its result before it replaces the scope. Protected text therefore becomes code exactly one level up:

{ define the function f :: => [d]ef f [{ foo => bar }] }

returns def f { foo => bar }, which then defines f in the surrounding code. Without the brackets, the definition would run inside the scope and be gone when it returns. Deeper nesting of brackets delays evaluation by more levels.

Flat loops

The interpreter stops with an error once evaluation is nested 1000 levels deep. A call in the output of a function is evaluated one level deeper than the function itself, so a function that calls itself once per character fails on inputs of about 1000 characters:

def to_zeros {
       1(.*) => 0~to_zeros(^#1)
    ||       =>
}

Evaluation protection gives a way around this. Instead of making the next call itself, the function returns it in brackets:

def to_zeros {
       1(.*) => 0[~to_zeros(]#1[)]
    ||       =>
}

print(to_zeros(111))  // 000

For to_zeros(111), the output evaluates to 0[~to_zeros(]11[)]: the #1 between the brackets is filled in, the call inside them is not run. Returning strips the brackets, so the caller receives 0~to_zeros(11) and runs that call itself, at its own level. Every step hands the next one back to the caller in this way, so the depth never grows, and this version handles inputs of any length. The register is now #1 instead of ^#1, because it is no longer inside the argument of a call, and the ~ keeps the call name from merging with the 0 in front of it.

This works whenever a step does not need the result of the next step: text in front of or behind the returned call is simply kept.

Comments

// starts a comment that runs to the end of the line. Comments are removed when the program is loaded.

Built-in functions

  • print(x) evaluates x, prints it with a newline, and is replaced by nothing.
  • print_raw(x) prints x without evaluating it.
  • get_input(prompt) prints the prompt and is replaced by a line read from standard input.
  • get_file(path) is replaced by the contents of the file, which is how libraries are loaded.
  • debug(x) works like print, but shows every rewrite step on the way.

Examples

Hello, world!

print(Hello, world!)

Binary increment

def to_zeros {
       1(.*) => 0~to_zeros(^#1)
    ||       =>
}

def inc_bin {
       (.*)0(1*) => #1~1~to_zeros(^#2)
    || (1+)      => 1~to_zeros(^#1)
    ||           => 1
}

print(inc_bin(1011))  // 1100

The regex does most of the work: (.*)0(1*) finds the last 0, which becomes a 1, and to_zeros turns the ones behind it into zeros, one character per call.

Compare

Compares two non-negative integers and returns <, = or >. Digits are compared from right to left; the state in the third argument remembers the result so far, and a difference in a more significant digit overrides it.

def compare_digits {
       00     => =
    || 0[1-9] => <
    || 11     => =
    || 1[2-9] => <
    || 22     => =
    || 2[3-9] => <
    || 33     => =
    || 3[4-9] => <
    || 44     => =
    || 4[5-9] => <
    || 55     => =
    || 5[6-9] => <
    || 66     => =
    || 6[7-9] => <
    || 77     => =
    || 7[8-9] => <
    || 88     => =
    || 8[9]   => <
    || 99     => =
    ||        => >  // all cases with left <= right are covered above
}

def compare {
       ^(\d*), (\d*)$                  => compare(^#1, ^#2, =)  // start in state =
    || ^(\d*)(\d), (\d*)(\d), ([<>=])$ => {
              compare_digits(^^#2^^#4)
           :: (.) => compare(^^#1, ^^#3, { ^^#1 :: ([<>]) => #1 || = => ^^^#5 })
       }
    || ^, \d                           => <   // left number is shorter
    || ^\d+, ,                         => >   // right number is shorter
    || ^, , ([<>=])$                   => #1  // same length: return the state
}

print(compare(1234, 1235))  // <
print(compare(999, 1000))   // <

Fibonacci

Numbers are text, so they have no size limit. This program adds them digit by digit, using a lookup table for the sum of two digits and a carry c.

// sums two digits and possibly a carry, written as a c
def sum_two_digits {
       00c                                => 1
    || (?:01|10)c                         => 2
    || (?:02|11|20)c                      => 3
    || (?:03|12|21|30)c                   => 4
    || (?:04|13|22|31|40)c                => 5
    || (?:05|14|23|32|41|50)c             => 6
    || (?:06|15|24|33|42|51|60)c          => 7
    || (?:07|16|25|34|43|52|61|70)c       => 8
    || (?:08|17|26|35|44|53|62|71|80)c    => 9
    || (?:09|18|27|36|45|54|63|72|81|90)c => 0c
    || (?:19|28|37|46|55|64|73|82|91)c    => 1c
    || (?:29|38|47|56|65|74|83|92)c       => 2c
    || (?:39|48|57|66|75|84|93)c          => 3c
    || (?:49|58|67|76|85|94)c             => 4c
    || (?:59|68|77|86|95)c                => 5c
    || (?:69|78|87|96)c                   => 6c
    || (?:79|88|97)c                      => 7c
    || (?:89|98)c                         => 8c
    || 99c                                => 9c
    || 00                                 => 0
    || 01|10                              => 1
    || 02|11|20                           => 2
    || 03|12|21|30                        => 3
    || 04|13|22|31|40                     => 4
    || 05|14|23|32|41|50                  => 5
    || 06|15|24|33|42|51|60               => 6
    || 07|16|25|34|43|52|61|70            => 7
    || 08|17|26|35|44|53|62|71|80         => 8
    || 09|18|27|36|45|54|63|72|81|90      => 9
    || 19|28|37|46|55|64|73|82|91         => 0c
    || 29|38|47|56|65|74|83|92            => 1c
    || 39|48|57|66|75|84|93               => 2c
    || 49|58|67|76|85|94                  => 3c
    || 59|68|77|86|95                     => 4c
    || 69|78|87|96                        => 5c
    || 79|88|97                           => 6c
    || 89|98                              => 7c
    || 99                                 => 8c
    || (\d?)c                             => sum_two_digits(1^#1)
    || (\d?)                              => #1
}

// adds two positive integers; the arguments are A a, B b, c P:
// a and b are the last digits, c is a carry, P is the result so far
def add {
       ^(\d+), (\d+)$                     => add(^#1, ^#2, )
    || ^(\d*)(\d), (\d*)(\d), ?(c?)(\d*)$ => add(^#1, ^#3, {
              sum_two_digits(^^^#2^^^#4^^^#5)
           :: (\d)(c?) => #2#1^^#6
       })
    || ^(\d+), , c(\d*)$                  => add(^#1, 1, ^#2)
    || ^, (\d*), c(\d*)$                  => add(^#1, 1, ^#2)
    || ^(\d+), , ?(\d*)$                  => #1#2
    || ^, (\d*), ?(\d*)$                  => #1#2
    || (\d*)                              => #1
}

// checks if two positive integers are equal, digit by digit from the right
def are_equal {
       (\d*)(\d), (\d*)(\d) => {
              ^#2^#4
           :: 00|11|22|33|44|55|66|77|88|99 => are_equal(^^#1, ^^#3)
           ||                               => n
       }
    || ^\d+,$               => n
    || ^, \d+$              => n
    || ^,$                  => y
}

// fibonacci(A, B, n, N): A and B are Fib(n) and Fib(n-1), N is the target
def fibonacci {
       (\d+), (\d+), (\d+), (\d+) => {
              are_equal(^^#3, ^^#4)
           :: y => ^#1
           || n => fibonacci(add(^^^#1, ^^^#2), ^^#1, add(^^^#3, 1), ^^#4)
       }
    || (\d+)                      => fibonacci(1, 1, 1, ^#1)
}

print(fibonacci(100))  // 573147844013817084101

Turing machine

A Turing machine simulator. The tape is written as state, tape, with > marking the head. The state table below checks whether a binary word is a palindrome: the machine halts with an all-blank tape if it is one.

// the state table maps "state, read symbol" to "new state, write, move"
// HALT stops the machine; the tape is blank (B) wherever it was not written
def state_table {
       START      => q_start
    || q_start, 0 => q_f0 B R
    || q_start, 1 => q_f1 B R
    || q_start, B => HALT
    || q_f0, 0    => q_f0 0 R
    || q_f0, 1    => q_f0 1 R
    || q_f0, B    => q_c0 B L
    || q_f1, 0    => q_f1 0 R
    || q_f1, 1    => q_f1 1 R
    || q_f1, B    => q_c1 B L
    || q_c0, 0    => q_back B L
    || q_c0, 1    => HALT
    || q_c0, B    => HALT
    || q_c1, 1    => q_back B L
    || q_c1, 0    => HALT
    || q_c1, B    => HALT
    || q_back, 0  => q_back 0 L
    || q_back, 1  => q_back 1 L
    || q_back, B  => q_start B R
}

// turing(state, A a>b B): the head is on b, with a and the rest A to its left
def turing {
       (.+), (.*)(.)>(.)(.*) => {
              state_table(^^#1, ^^#4)
           :: HALT         => ^#2^#3^#4^#5
              // new state, symbol to write, direction
           || (.+) (.) (.) => turing({
                     ^^#3, ^^^#2, ^^^#5
                     // move the head; at an end of the tape, add a blank
                  :: ^L, [^,]+,   => ^^#1, ^^^#2>^^^#3^^#2^^^#5
                  || ^L, ,        => ^^#1, B>^^^#2^^^#3^^#2^^^#5
                  || ^R, [^,]*, . => ^^#1, ^^^#2^^^#3^^#2>^^^#5
                  || ^R, [^,]*,$  => ^^#1, ^^^#2^^^#3^^#2>B
              })
       }
    || (.*)                  => turing(state_table(START), B>^#1)
}

print(turing(1011001))  // BBBB10BBB: not a palindrome
print(turing(1011101))  // BBBBBBBBB: a palindrome

Variables and arrays

Evaluation protection lets functions define new functions in the code that called them. set_var(x=1) defines a function get_var_x that returns 1. An array is a function returning its items as |a|b|c; pushing redefines that function with the new item appended.

def set_var {
    (.+)=(.+) => [def get_var_]#1 [{ => ]#2[}]
}

def init_array {
    (.+) =>
        [d]ef #1_values [{ => }]
        [d]ef #1_push [{ (.+) => [d]ef ]#1[_values [{ => ]] #1[_values()][|#1}}]
}

set_var(x=1)
print(x = get_var_x())  // x = 1

init_array(arr)
arr_push(1)
arr_push(2)
arr_push(3)
print(arr_values())     // |1|2|3

In set_var, the parts in brackets are copied as they are and the parts between them are filled in, so for x=1 the function returns def get_var_x { => 1}. init_array uses two levels of brackets: one level is stripped when init_array returns and defines arr_push, the other when arr_push returns and redefines arr_values.

Standard library

The reference implementation comes with a standard library written in Subtext itself, with no help from the interpreter. It is loaded with get_file(std/all.stx) and contains:

File Functions
std/math.stx add, sub, mul, div, mod, inc, dec, cmp, min, max on signed integers of any length
std/string.stx len, rev, upper, lower, repeat, slice, eq, contains, index_of
std/list.stx split, join, count, nth on lists written as |a|b|c
std/error.stx error

With it, a factorial is short:

get_file(std/all.stx)

def factorial {
       ^0$  => 1
    || (.*) => mul(^#1, factorial(dec(^^^#1)))
}

print(factorial(25))  // 15511210043330985984000000

The library uses a few techniques that are typical for Subtext.

Adding digits in unary

Instead of a table of all digit sums, _add_digits converts two digits and a carry to unary (7 becomes IIIIIII), writes them next to each other, and lets a regex detect the carry: ten or more Is means a carry of 1, and the rest is converted back to a digit. _add_digits(981) gives 18.

def _add_digits {
    (\d)(\d)(\d) => {
           _unary(^^#1)~_unary(^^#2)~_unary(^^#3)
        :: ^IIIIIIIIII(I*)$ => 1~_digit(^#1)
        || ^(I*)$           => 0~_digit(^#1)
    }
}

Incrementing in one match

Adding one only changes the last digit that is not a 9, and turns the 9s behind it into 0s. A single regex finds all three parts:

def inc {
       ^(\d*)([0-8])(9*)$ => #1~~_succ(^#2)~_zeros(^#3)  // 129 => 130
    || ^(9+)$             => 1~_zeros(^#1)               // 99  => 100
    || ^-(\d+)$           => _neg(dec(^^#1))
}

_succ maps a digit to the next one, _zeros replaces every digit by 0.

Flat loops in practice

The loops in the string and list functions are all flat loops, so they work on text of any length. _upper, the loop behind upper, skips ahead to the next lowercase letter with a single regex, converts it, and returns the call for the rest:

def _upper {
       (?s)^>([^a-z]*)([a-z])(.*)$ => #1~~_upper_char(^#2)[~_upper(>]#3[)]
    || (?s)^>(.*)$                 => #1
}

_upper(>ab) returns A~_upper(>b), which the caller then runs. The > is a marker that keeps leading whitespace of the rest from being trimmed away.

Comparing text without backreferences

The regex crate has no backreferences, so no pattern can check whether two parts of the input are equal. eq instead builds a new scope whose pattern is the second argument, and returns it to be evaluated:

def eq {
       (?s)^(.*), (.*)$ => [{ ]#1[ :: (?s)^]~_escape(>^#2)[$ => y || => n }]
    || (?s)^(.*),$      => [{ ]#1[ :: ^$ => y || => n }]
}

The output is a scope with holes: the bracketed parts are copied as they are, and the parts between them are filled in. eq(abc, abd) returns { abc :: (?s)^abd$ => y || => n }, which then evaluates to n. _escape turns characters with a meaning in regex into escape sequences first. contains, index_of, slice, nth and split build their scopes the same way.

Raising errors

There is no built-in for errors. error evaluates a scope whose only arm can never match, because $. asks for a character after the end of the input. The interpreter then stops and reports the message as the input that did not match.

def error {
    (?s)^(.*)$ => { ^#1 :: $.^raised by error => }
}

Computational class

Subtext is Turing complete. The Turing machine example above simulates a Turing machine from its state table, and the tape is a string that grows whenever the head moves past its end. The reference interpreter limits nesting to 1000 levels, and the example as written nests one level per step. Written as a flat loop, it can run for any number of steps, so the limit belongs to the implementation and not to the language.

External resources

  • Subtext on GitHub: the reference interpreter in Rust, the standard library, a language server for syntax highlighting, and the examples above.
  • Online interpreter with a tour of the language and programming challenges.