From Esolang
Jump to navigation Jump to search

Incident is a language created by User:ais523 for the CALESYTA 2016 contest. The goals of the language were to make an interesting puzzle for programmers (trying to figure out how to write anything at all in the language is decidedly nontrivial, although once you know the tricks the language is viably possible to write by hand); and to create a language which would allow programs to obey almost arbitrary restrictions on what can appear in the source (e.g. bans on certain characters, polyglotting with some other language, valid as an image / executable / save file for some other program, and the like).


Incident's syntax is fairly unusual, specifically the lexical analyser; what counts as a valid token in the language is determined via analysis of the source code, rather than being predetermined. The basic rule is that the tokens for a particular program are the substrings that appear exactly three times in it. (The input program is interpreted as a string via converting it to a sequence of octets, if it isn't already in that form.) There are some restrictions:

  • A token cannot be a substring of a longer token; in this case, the shorter token is disregarded entirely.
  • Two tokens cannot overlap within the source; if they would, neither is treated as a token.

Anything that isn't part of a token is treated as a comment and discarded.

These syntax rules mean that an Incident interpreter can be fairly hard to write efficiently, given that changing any part of the program can affect how the entire rest of the program is parsed. They also mean that a syntax highlighter is a fairly essential tool when writing programs by hand.


Obviously, Incident can't specify its commands via an explicit list. Instead, each token is treated as a command of its own (which also has an associated stack of bits, initially empty), but the behaviour of a command depends on where it appears in the program:

  • Running the first copy of a command pushes a 0 bit onto the stack associated with that command, then jumps to just after the second copy.
  • Running the third copy of a command pushes a 1 bit onto the stack associated with that command, then jumps to just after the second copy.
  • Running the second copy of a command pops a bit from the stack associated with that command, then jumps to just after the first copy if it was a 0 bit, or just after the third copy if it was a 1 bit.

There are a few exceptions. Two minor ones are to do with I/O, and have no effect on otherwise valid programs (thus, they act like backward-compatible extensions to a hypothetical version of the language with no I/O):

  • When pushing a bit onto the stack associated with the centremost token in the program (breaking ties towards the start of the program), the bit is also output to standard output (in a little-endian way).
  • When attempting to pop an empty stack, the bit is taken from standard input instead (again, in a little-endian way); this applies to all commands, not just the one associated with the centremost token. At EOF, the command simply fails to run (it's just skipped, and doesn't jump anywhere).

There's also an exception that changes the meaning of an otherwise valid program:

  • If a command would trivially cause an infinite loop, it is skipped and has no effect. A command is defined as causing a trivial infinite loop if it pushes a value to a stack, it has pushed that value to that stack before, and no stack has been popped since then.


Cat program

    |\ /|
    (0 0)
      (          _^^,
      (_________) ( )
cat    |||   |||   "
       ||^   ||^
       ^^    ^^

Hello world program


Computational class

Incident's original documentation (in examples/ noted that Minsky machines could be compiled into Incident parses "so long as no counter is the subject of more than one decrement instruction that's capable of failing", explaining how. This restriction turns out not to matter: The Amnesiac From Minsk level 1 is Turing complete, and as it has a more stringent restriction (TAFMl1 requires all decrements of a particular counter to return to the same place, which can be implemented in Incident via jumping all decrements to a common location and then running the decrement from there), there must therefore be an Incident parse corresponding to any Turing machine.

To prove Incident Turing complete, all that remains is to show that for any Incident parse, there's some program that parses to that parse. It turns out that this is indeed true; see the talk page for discussion of this subject.

External resources

  • The CALESYTA submission (including documentation, programming advice / spoilers on how to program in it, the motivation behind the language design, examples, and an implementation) can be obtained via running the command darcs clone
    • Note: the URL in question will not work in a web browser; you will need to use darcs to retrieve the submission. Alternatively, a sporadically updated tarball is available here.

See also

  • Lenguage and A Pear Tree, two other different ways of getting around source format restrictions