XKCD-CS/Languages
Здравствуй, мир!
Contents |
[edit] Brainf***
Features:
- eight instructions
- one-dimensional array of characters, containing the program
- one-dimensional array of integers of implementation-dependent size, used for memory
- an instruction pointer IP, indicating the current location within the program
- a data pointer DP, indicating the currently accessible cell in the memory array
- Turing complete and used to prove Turing completeness of other languages
| Instruction | Behavior |
|---|---|
- |
Decrement value of current memory cell |
+ |
Increment value of current memory cell |
< |
Decrement DP |
> |
Increment DP |
[ |
Jump to matching ] if the current memory cell is zero
|
] |
Jump to matching [ if the current memory cell is nonzero
|
. |
Output value of current memory cell, as a character, to standard output |
,
|
Read a character from standard input and store it in the current memory cell
|
[edit] Bitf***
The Brainf*** instruction set is not minimal. By restricting each memory cell to single bits, the + and - instructions can be combined. + becomes an instruction that changes 0 to 1 or 1 to 0.
[edit] Random access
Traditional Brainf*** accesses memory sequentially. The following additions to the instruction set add random access, which better corresponds to modern computer design. In addition, the memory array must be made two-dimensional, and each additional row of the array requires its own data pointer. A row pointer RP indicates the current row.
| Instruction | Behavior |
|---|---|
( |
Increment RP |
) |
Store value of current memory cell in the data pointer for RP-1 and decrement RP |
[edit] Pseudoliterals
There are no literal values in traditional Brainf***. All numbers must be entered via a complex sequence of increments, decrements, and jumps. This can be simplified while retaining the character of the language. Let a pseudoliteral be an instruction which is a digit in a particular base n. The action of the interpreter upon encountering a pseudoliteral is to multiply the current memory cell by n and then add the value of the digit. Thus, assuming a zero memory cell:
-
10appears to represent and in fact stores the value 10. -
3stores 3. -
0stores 0. -
+0stores 10. -
2+2stores 32. -
1>++[<00>-]<stores 10000.
[edit] e
Pi is an encoding of Brainf*** in π that assigns instructions to incorrect digits. Other constants can be used as well, such as e (2.71828182845904523543). Individual digits of e can be calculated in O(n²) time and O(n) space by a simple algorithm:
buffer[1 ... digits + 8] = {2, 1, 1, 1, ..., 1}
digit = 0
for n in [digits + 8 ... 10]
for x in [n ... 1]
buffer[x] = digit mod x
digit = 10 * buffer[x - 1] + digit / x
end
output digit
end
Arbitrary precision number libraries can produce digits much more quickly, however.
Many different instruction sets are possible with different combinations of correct and incorrect digits.
[edit] e271
Features:
- ten instructions, encoded in e
- one-dimensional array of digits, containing the program
- one-dimensional array of 64-bit signed integers, used for memory
- a data pointer DP, indicating the currently accessible cell in the memory array, which becomes the exit status when the program ends
- unlimited program and memory size
| Instruction | Behavior |
|---|---|
0 |
Negate the current memory cell |
1 |
Decrement value of current memory cell unless it contains the most negative value |
2 |
Increment value of current memory cell unless it contains the most positive value |
3 |
Decrement DP, adding zeroed memory cells as needed |
4 |
Increment DP, adding zeroed memory cells as needed |
5
|
Jump to matching
|
6
|
Jump to matching
|
7
|
Output value of current memory cell, as a character, to standard output
|
8
|
Read a character from standard input and store it in the current memory cell
|
9 |
Set the current memory cell to zero |
The following table gives the instruction to use for an encoded program digit according to the corresponding correct digit in e. When a digit in the program is the correct digit for e at that position, the instruction is 0.
| Correct digit of e | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Digit in program | ||||||||||
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | |
| 0 | 0 |
2 |
7 |
1 |
9 |
8 |
5 |
3 |
6 |
4
|
| 1 | 2 |
0 |
8 |
6 |
7 |
1 |
3 |
4 |
9 |
5
|
| 2 | 7 |
8 |
0 |
3 |
4 |
6 |
9 |
2 |
5 |
1
|
| 3 | 1 |
6 |
3 |
0 |
5 |
9 |
7 |
8 |
4 |
2
|
| 4 | 9 |
7 |
4 |
5 |
0 |
3 |
2 |
6 |
1 |
8
|
| 5 | 8 |
1 |
6 |
9 |
3 |
0 |
4 |
5 |
2 |
7
|
| 6 | 5 |
3 |
9 |
7 |
2 |
4 |
0 |
1 |
8 |
6
|
| 7 | 3 |
4 |
2 |
8 |
6 |
5 |
1 |
0 |
7 |
9
|
| 8 | 6 |
9 |
5 |
4 |
1 |
2 |
8 |
7 |
0 |
3
|
| 9 | 4 |
5 |
1 |
2 |
8 |
7 |
6 |
9 |
3 |
0
|
| Instruction | ||||||||||
[edit] Hello world!
2.7132 8082 5453 0482 3736 7282 4703 5766 7492 7522 4209 9690 9525 7791 6907 6477 7401 6690 3835 2753 4521 3521 0853 5116 4974 5726 6891 3321 0315 9321 5176 1389 6429 2485 7790 1336 2982 6159 9631 7351 3732 5622 9494 9176 6236 8238 8175 9199 2560 6904 1513 3340 8733 0202 0541 8904 9334 5844 6710 9944 6618 6076 8382 9644 0046 8677 6115 5384 2045 8428 3750 7139 3778 4429 2369 1516 0266 1438 7069 6163 1285 5890 0273 2144 3323 0102 1208 9264 0466 6777 4849
[edit] B****zero
Features:
- six instructions
- two one-dimensional arrays of bytes, used for memory, with one active
- a data pointer DP, indicating the currently accessible pair of cells in the memory arrays
| Instruction | Behavior |
|---|---|
% |
Activate the inactive array and inactivate the active array |
+ |
Increment value of active memory cell and decrement value of inactive memory cell |
x |
Increment DP if the first array is active; decrement it otherwise |
[ |
Jump to matching ] if the first array is active and zero or the second array is active and nonzero
|
] |
Jump to matching [ if the first array is active and nonzero or the second array is active and zero
|
.
|
If the first array is active, output value of current memory cell, as a character, to standard output, and remove the pair of cells at DP from memory
|