And
No if and no while. One chain of && and || runs again and again, and laziness does the rest.
Rdococ, 2015 · Turing complete: yes · ●●○ moderate · esolangs.org
Tested with 3 known programs · Test method
Run it
The idea
Rdococ made And in 2015. A program is a chain of && and || that is evaluated over and over, and every pass reads one character of input first. && stops at the first false part and || at the first true one, so a && b is an if and a long || is a list of cases. Assignment and print always count as true, so they can sit anywhere in a chain. The run ends only at exit.
The machine
| Part | Does |
|---|---|
a && b a || b | Lazy and, lazy or |
x := e a[i] := e | Assign, and count as true |
print e | Print, and count as true |
exit | End the program |
input | The character read for this pass |
= != < > | Compare, giving 1 or 0 |
+ - % | Add or join text, subtract, remainder |
5 'a' "text" true | A number, a character's number, text, and 1 |
Variables and array entries need no declaring and start at 0. A quoted character is its number, as the brainfuck interpreter compares input with '\n' and also takes it % 256.
The specification leaves the output open. In this implementation print writes text as it is and a number as the character with that code, and adds no line feed, so the cat echoes and the brainfuck interpreter prints bytes. Text joined with + writes a number in decimal, as the 99 bottles needs. At the end of the input input is -1, which has no character, so the cat print input stops there with an error.
Example
The second program is not from the specification.
The second program is not from the specification. Each pass adds 1 to n and prints it, and only when n is 5 does the chain reach exit.
The specification proves And Turing complete with a brainfuck interpreter. The variable state picks the case. In state 0 each pass stores one character of the program, up to the line feed. State 1 runs one command a pass, and states 2 and 3 walk to the matching bracket one cell a pass. Running the classic brainfuck Hello World takes 113,225 steps.
The 99 bottles of beer program sets n to 99 on its first pass and then prints one line of the song a pass, with no line feed between them, as print adds none. After the last line it starts again at 99, so it never ends.
Patterns it teaches
- Turing completeness: a brainfuck interpreter built from nothing but lazy
&&and||.
Further
See also
Also in Strange time
Write your own in Run Convert a program from And
Implementation
JavaScript interpreter, source and command line
A JavaScript interpreter, used in the browser and on the command line. Id: and.
Command line
shib run and -e 'print "Hello World!" && exit'shib manual andSource
- Module
langs/wiki/ports.js- Interpreter
langs/wiki/and.js
Docs: Kernel instructions · The .shib format · Command line · Adding a language
Source esolangs.org (CC0) · Edit on esolangs.org