Emmental

Emmental is an esoteric programming language designed by Chris Pressey in 2007.

Emmental is a self-modifying language. It is defined by a meta-circular interpreter (an Emmental interpreter described in Emmental.) The meta-circular interpreter provides an operation which modifies operations of the meta-circular interpreter.

Emmental is a stack-based language, but it also has a queue.

Instruction set
(Emmental's instruction set can be redefined, so what follows is the initial instruction set.)

Infinite loop
Go into an infinite loop. This is done by redefining  to mean , and then executing. ;#35#52#56#63#48!0

Cat program
Redefines  (ASCII 42) to mean   and executes. This program is an infinite loop that does not observe EOF. ;#44#46#35#52#50#63#42!*

Hello, World!
Output "Hello, world!". This is done by pushing the ASCII representation of every character on to the stack, and then popping them off with the  operator.


 * 1) 0#10#33#100#108#114#111#119#32#44#111#108#108#101#72...............

Alternatively, this code performs a loop over the characters (rather than outputting each character individually.)

;#58#126#63#36!;#46#36#!;#0#1!;#0#2!;#0#3!;#0#4!;#0#5!;#0#6!;#0#7!#0#33#100#108#114#111#119#32#44#111#108#108#101#72$

Conditional branching
Emmental doesn't have explicit conditional statements, but the eval instruction  can be used to "branch" by executing an instruction that depends on the symbol on top of the stack. The discrete base-2 log instruction  can be used in combination with eval: it basically maps the 256 possible symbols into 9 "equivalency classes" 0, 1, 2-3, 4-7, 8-15, 16-31, 32-63, 64-127 and 128-255.

The following programs display different approaches to conditional branching.

Using the discrete log
Input a character and report whether its ASCII value is congruent with 0, 1 or 2 modulo 3. This is done by repeatedly subtracting 3, and branching with the base-2 log. For better readability, the program has been written using the notation  for   (and so forth with every ASCII char). This is not a feature of the Emmental language, it is merely a convention adopted in this article to help understand the examples. With this notation: Note the nested definitions of symbols 2 and 3 inside the definition of symbol 1: those symbols will be defined when symbol 1 is evaluated.
 * means
 * means  which in turn means
 * means  which in turns means

Using implicit modulo 256
Input a character and report whether its ASCII value is odd or even. This is done by multiplying it by 128.

Using massive symbol redefinition
Input a character and report whether its ASCII value is congruent with 0, 1 or 2 modulo 3. This is done by redefining every symbol to "subtract 3 and try again". Note that all symbols are redefined to "subtract 3 and try again" when  is executed; for that reason, that occurrence of   must be the very last character in the program — even whitespace are not allowed.
 * the symbols,   and   are defined to output '0', '1' or '2'.
 * the symbol  is defined to "subtract 3 from top stack element, duplicate it, evaluate the copy", and the symbol   to "pop top stack element and define it to be a synonym of  ".
 * the symbol  is defined to "redefine myself to be a synonym of  ; then take one char from input, duplicate it, evaluate the copy".
 * the symbol  is redefined to "repeat   251 times"
 * the value 4 is pushed on the stack
 * the symbol  is defined to "duplicate top stack element and increment the copy"
 * the symbol  is redefined to "repeat   250 times, then , then  "

Truth-machine
;#58#46#58#63#49! ,:.:?

Computability class
An Underload implementation in Emmental is available, generated by a Haskell program.

It is not quite an ordinary interpreter: It uses Emmental's metacircular evaluation principles to turn the Emmental interpreter into an Underload one. As such, the Underload program you wish to interpret should be appended to the Emmental one.

It supports all Underload commands, but not all Underload characters: By the necessities of this kind of Emmental programming, 10 byte values have been reserved for the interpreter's internal use. These are all non-printable control codes.

In any case I believe this proves Emmental Turing-complete. However it may happen to do so in a way which I have previously disagreed with Chris Pressey on whether it counts or not. :P

I also attempted to minimize the number of initial Emmental instructions used for this, based partially on certain challenges proposed in the Emmental distribution. In particular:


 * It doesn't use the queue.
 * It doesn't use the ~ or : instructions.

In fact, it only uses the initial instructions  (and the special stack-separator function of  ).

Implementations
The Emmental reference interpreter is written in Haskell and can be found in the Emmental distribution on Github. It also contains some example programs as test cases.

There is also an implementation in OCaml by User:Koen, and a Go implementation.

All three implementations are in the public domain.