Because we can't get enough of esoteric language golfs, can we?
///—pronounced slashes—is a fun little language based on the s///
regex-replacement function of Perl fame. It contains only two special characters, slash /
and backslash \
. You can find a full article on it at the esolangs wiki, but I will reproduce a description of the language below, as well as some examples.
In short, it works by identifying /pattern/repl/rest
in the program and making the substitution as many times as possible. No characters are special except /
and \
: /
demarcates patterns and replacements in the program, while \
allows you to insert literal /
or \
characters into your code. Notably, these are not regular expressions, just plain string substitutions.
Your challenge is to produce an interpreter for the /// language, as either a program reading STDIN or a function taking a string argument, in as few characters as possible.
You may use any language except for /// itself. You may not use any libraries that interpret ///; you may, however, use regexes, regex libraries, or string-matching libraries.
Execution
There are four states, print, pattern, replacement, and substitution. In every state except substitution:
- If the program is empty, execution halts.
- Else, if the first character is
\
, do something with the next character (if present) and remove both from the program. - Else, if the first character is
/
, remove it, and change to the next state. - Else, do something with the first character and remove it from the program.
- Repeat.
The states cycle through print, pattern, replacement, and substitution in order.
- In print mode, 'do something' means output the character.
- In pattern mode, 'do something' means add the character to the current Pattern.
- In replacement mode, 'do something' means add the character to the current Replacement.
In substitution mode, you follow a different set of rules. Repeatedly substitute the first occurrence of the current Pattern with the current Replacement in the program, until no more substitutions are possible. At that point, clear the Pattern and Replacement and return to print mode.
In the program /foo/foobar/foo foo foo
, the following happens:
/foo/foobar/foo foo foo
foo foo foo
foobar foo foo
foobarbar foo foo
foobarbarbar foo foo
...
This loops forever and never exits substitution mode. Similarly, if the Pattern is empty, then the first occurrence of the empty string—at the beginning of the program—always matches, so substitution mode loops forever, never halting.
Examples
no
Output: no
.
/ world! world!/Hello,/ world! world! world!
Output: Hello, world!
.
/foo/Hello, world!//B\/\\R/foo/B/\R
Output: Hello, world!
.
a/ab/bbaa/abb
Output: a
. Program does not halt.
//
Output: none.
///
Output: none. Program does not halt.
/\\/good/\/
Output: good
.
There is also a quine on the wiki you can try.
/-/World//--/Hello//--W/--, w/---!
What's not to love? (Try removing dashes from the end) \$\endgroup\$\
character escapes any character that follows it, including/
, which can later be used as normal. While this doesn't look like much, this makes /// Turing-complete. \$\endgroup\$///
IDE that I'm making! \$\endgroup\$