7

7 is an esoteric programming language created by User:ais523 towards the end of 2016. It's heavily inspired by Underload, but has much more powerful input/output capabilities and a different set of commands. One major difference is that it's based entirely on combinators; unlike Underload, it doesn't have literals (although they can be simulated fairly easily).

Syntax
Although the language 7 has twelve different commands, only eight of them can appear in source files. These have the names  through   inclusive. (Six of the named commands have the purpose of appending other commands to the frame, which is how the four anonymous commands can get involved in a program.)

These commands can either be written with one command per byte (with their numerical names encoded using the ASCII digits), or using octal, with three bits per command. When octal is packed into bytes, it's done in such a way that the view of the input as octal and as octets will look the same in big-endian representation.

is never allowed as the last command of the program (doing so would not be useful anyway); rather, any sequence of consecutive 1 bits at the end of the program is ignored, kind-of like trailing whitespace. If the program appears to end halfway through a command, it'll be padded up to the command boundary with 1 bits. So for example, if a program ends with a  command, that command could be expressed with a single 0 bit (rather than the usual triple of bits 011), because the trailing 1 bits are implied.

Data storage
A 7 program can store data in two places. One is the command list, which initially stores the program; as the name suggests, this is a list of commands. It's used like a stack, with commands running from the start of the list and being removed as they do so, and the command list only ever being refilled via adding on to the front. The other is the frame, which is a construct that has many similarities to a stack. It's a string that can hold commands, but also bars, which are not commands and serve as dividers between sections of commands. Each section is manipulated as a group, and is quite similar to a stack element from a stack-based language (the commands only manipulate sections near the end of the frame). The initial state of the frame is two bars, and nothing else.

Commands
The twelve commands form six pairs, each of which contains a passive and an active command. All the passive commands do much the same thing; they append the corresponding active command to the frame. The active commands are much more varied in their behaviour.

(passive), (active)
The active command pacifies the last section of the frame, then removes the bar to its left. The pacification of a list of commands is defined as follows: identify the passive substrings of the list (favouring one long substring, rather than two shorter adjacent ones), prepend a  and append a   to each such substring, then convert every command outside the passive substrings (all such commands will be active) to its passive equivalent. A passive substring is a substring for which all commands are named, for which  and   commands are correctly matched (as though they were parentheses), and in which the substring  does not appear.

(passive), (active)
The active command appends a bar to the frame.

(passive), plus an anonymous active command
The active command duplicates the last section of the frame (separating the original section from the new section with a bar).

(passive), plus an anonymous active command
The active command outputs the last section of the frame to the user, then discards the last two sections (and the bars to their left). If the section being output contains anonymous commands, it's pacified first (pacification always leads to a section with only named commands), and a  command is prepended. The first command of output via the  command specifies the output format, character encoding, and the like; see the language's documentation for more details.

This command can also be used for input, via "outputting" certain special codes that don't correspond to characters. The input is always converted to a nonnegative integer (either it's one already, or it's a character which is converted to an integer by taking its codepoint and adding 1; EOF + 1 is 0). Then it's used to make that many copies of the contents of the last section of the frame (unlike with the active counterpart of the  command, the copies are all run together, with no bars separating them.

(passive), plus an anonymous active command
The active command here rearranges the frame as follows: the last two sections are swapped; and the bar between them is replaced by two bars.

(passive), plus an anonymous active command
The active command here moves the last section of the frame to the start of the command list, and removes the bar before it.

Execution model
The program repeatedly executes the first command of the command list (removing it from the list in the process) until the command list is empty. At that point, the program cycles; any bars at the very end of the frame are removed, then the last section of the frame is copied to the command list (this differs from the active counterpart of the  command, which would move it). If the command list is ever empty while the frame consists entirely of bars, the program exits; this is a successful exit. (It also exits, with more of a failure-type exit, if the  command runs out of sections to discard. Running out of sections in other commands should produce an error in well-behaved interpreters, although very simple interpreters may prefer to leave this as undefined behaviour.)

Hello, world!
5325101303040432004515131401430134321027403

This makes use of output format 5, which uses the commands 0 through 5 (which are all passive and thus can be handled consistently when pushing them to the frame) to encode a string in Baudot (technically US-TTY, which is a Baudot variant); pairs of base-6 digits give 36 possibilites, compared to the 32 that Baudot has. Most of the program encodes the text "Hello, world!\n"; the 7 separates the data from the code that prints it, which is a simple  (you need to swap the data above the code that was left on the frame, and pacify it to convert the active commands back to passive, before you print it).

Factorial
177172051772664057074056167770236713351357263

The core of this program is. When the last section of the frame is a sequence of passive commands with a  following it, the active counterpart of   will append the active counterparts of those commands to the section before, with all the other changes cancelling each other out. Underload programmers will realise that this is similar to a multiplication. Meanwhile, the active counterpart of  implements an increment instruction;   appends a suitably pacified   section to the frame,   prepends it to the section before, and   appends   to the last section of the frame (which, as we maintain it ending with a 7 and only ever read it via running it, is equivalent to adding a    just before the final 7, because   is a no-op). Prepending  and appending   in 7 is very similar to prepending   and appending   in Underload, which would increment an integer seen.

As such, the program basically forms a loop, repeatedly incrementing the last section of the frame, then multiplying the penultimate section by it. The rest of the program is to deal with input, output, and setting up the initial state of the frame.

Underload translation
The named 7 commands could be approximately translated to Underload along the following lines:


 * becomes
 * becomes
 * becomes
 * becomes  (this is the least accurate translation)
 * becomes
 * becomes
 * becomes
 * becomes

The translation will not work directly, due to things like cycling, but can get a good idea of how 7 works.

Translating the other way:


 * becomes  or   or sometimes even   (the quoting rules act differently between the languages, so it's a bit hard to give an accurate correspondence)
 * becomes
 * becomes
 * becomes
 * becomes
 * becomes
 * becomes
 * becomes

Because 7 programs are initially given in passive form, understanding the quoting rules can be fairly confusing. (Additionally, it tends to be a better idea to use different algorithms from Underload on the small scale; in Underload,  is slightly shorter than , but both are widely useful as appending and prepending text are both common operations; meanwhile, in 7,   to prepend is considerably shorter, with   being longer because it's much more rarely used; to append a constant, you can just use a sequence of passive commands.)

External resources

 * A (rather slow) interpreter written in Perl, which also contains the language specification as documentation, can be found here.