This repository has been archived by the owner on Jul 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
brainfuck.html
137 lines (132 loc) · 6.87 KB
/
brainfuck.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<!doctype html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1"><meta http-equiv="X-UA-Compatible" content="ie=edge"><meta name="description" content="Brainfuck is a Turing-complete esolang created in 1993 by Urban Müller."><!-- Bing --><meta name="msvalidate.01" content="45CBBE1BD8265A2217DFDA630EB8F84A" /><title>Tiny Brain Fans - Brainfuck</title><link rel="stylesheet" href="tinystyle.css"></head><body>
<main id="main"><article id="content"><h1 id="title">Brainfuck</h1><p>Brainfuck is an esoteric Turing-complete programming language that is comprised of only 8 instructions.</p>
<h2>Instructions</h2>
<p>Brainfuck is read token by token, left to right. It utilizes an array of data and an input stream.</p>
<p>Data bytes are assumed to store any value between 0 and 255 inclusive. Some implementations use wrapping (255 + 1 = 0, 0 - 1 = 255), while others do not (255 + 1 = 256, 0 - 1 = -1).</p>
<table>
<thead>
<tr>
<th>Token</th>
<th>Effect</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>></code></td>
<td>Increment the data pointer</td>
</tr>
<tr>
<td><code><</code></td>
<td>Decrement the data pointer</td>
</tr>
<tr>
<td><code>+</code></td>
<td>Increment the byte at the data pointer</td>
</tr>
<tr>
<td><code>-</code></td>
<td>Decrement the byte at the data pointer</td>
</tr>
<tr>
<td><code>.</code></td>
<td>Output the byte at the data pointer</td>
</tr>
<tr>
<td><code>,</code></td>
<td>Accept one byte of input and store at data pointer</td>
</tr>
<tr>
<td><code>[</code></td>
<td>If byte at data pointer is zero, jump to command after matching <code>]</code></td>
</tr>
<tr>
<td><code>]</code></td>
<td>If byte at data pointer is not zero, jump to command after matching <code>[</code></td>
</tr>
</tbody></table><h2>Examples</h2>
<h3>Addition</h3>
<p>A simple program that adds 2 + 2 could be written like so:</p>
<pre><code class="language-brainfuck">++>++[<+>-]
</code></pre>
<p>In brainfuck, as the interpreter/compiler will ignore any characters that are not the ones found above, we can add comments, notes, and newlines for clarity. So the example can be rewritten:</p>
<pre><code class="language-brainfuck">Registers:
First addend/Sum
Second addend
++ // Set the value of the first addend to 2
>++ // Set the value of the second addend to 2
[ // While the second addend is not zero
<+ // Increment the sum
>- // Decrement the second addend
] // If the second addend isn't zero; continue
</code></pre>
<p>The process can be visualized like when using <a href="paper-computing.html">paper computers</a>, with multiple registers that are being incremented and decremented.</p>
<h3>Multiplication</h3>
<p>This program will multiply two numbers (between 1 and 3) together and print out the result.</p>
<pre><code>+++>+++[-<[->>+>+<<<]>>>[-<<<+>>>]<<]
>++++++++++++++++++++++++++++++++++++++++++++++++.
</code></pre>
<p>Expanded, it looks like this:</p>
<pre><code class="language-brainfuck">Registers:
Multiplicand // First num
Multiplier // Second num
Product
Temp // Used for refilling multiplier
+++ // set multiplicand
>+++ // set multiplier
[ // while multiplier is not zero
- // decrement multiplier
< // add multiplicand to product
[ // while multiplicand is not zero
- // decrement multiplicand
>>+ // increment product
>+ // increment temp
<<<
] // if multiplicand is not zero; continue
>>> // add temp back to multiplicand
[ // while temp is not zero
- // decrement temp
<<<+ // increment multiplicand
>>>
] // if temp is not zero; continue
<<
]
> // add 0x30 to product for printing
++++++++++++++++
++++++++++++++++
++++++++++++++++
.
</code></pre>
<h3>Fibonacci[10]</h3>
<p>This program will print the ASCII character found at each fibonacci number (<code>0x01</code>, <code>0x01</code>, <code>0x02</code>, <code>0x03</code>, <code>0x05</code>, etc.)</p>
<pre><code class="language-brainfuck">+[.[->+>+<<]>]
</code></pre>
<p>Expanded:</p>
<pre><code>+ // set 0th fib number to 1
[ // while next fib number is not 0
. // print current fib number
[ // while previous fub number is not 0
- // decrement previous fib number
> // increment next fib number
+
> // increment 2nd next fix number
+
<< // go back to previous fib number
] // (if previous fib number is zero,)
> // set current fib number to next fib number
]
</code></pre>
<h2>References</h2>
<ol>
<li><a href="https://en.wikipedia.org/wiki/Brainfuck" target="_blank">https://en.wikipedia.org/wiki/Brainfuck</a></li>
<li><a href="https://www.nayuki.io/page/brainfuck-interpreter-javascript" target="_blank">https://www.nayuki.io/page/brainfuck-interpreter-javascript</a></li>
<li><a href="https://sange.fi/esoteric/brainfuck/" target="_blank">https://sange.fi/esoteric/brainfuck/</a></li>
<li><a href="https://codeberg.org/milofultz/brainfuck" target="_blank">https://codeberg.org/milofultz/brainfuck</a></li>
<li><a href="https://esolangs.org/wiki/Brainfuck" target="_blank">https://esolangs.org/wiki/Brainfuck</a></li>
<li><a href="https://codeberg.org/milofultz/decrypting-the-original-brainfuck-interpreter" target="_blank">https://codeberg.org/milofultz/decrypting-the-original-brainfuck-interpreter</a></li>
<li><a href="https://github.com/stedolan/bf.sed/blob/master/bf.sed" target="_blank">https://github.com/stedolan/bf.sed/blob/master/bf.sed</a></li>
<li><a href="http://calmerthanyouare.org/2015/01/07/optimizing-brainfuck.html" target="_blank">http://calmerthanyouare.org/2015/01/07/optimizing-brainfuck.html</a></li>
<li><a href="https://git.sr.ht/~rabbits/uxn/tree/85a6d348ba93186eaa328f67c625fea1bacae1f4/item/projects/examples/exercises/brainfuck.tal" target="_blank">https://git.sr.ht/~rabbits/uxn/tree/85a6d348ba93186eaa328f67c625fea1bacae1f4/item/projects/examples/exercises/brainfuck.tal</a></li>
<li><a href="https://codegolf.stackexchange.com/a/42443/98237" target="_blank">https://codegolf.stackexchange.com/a/42443/98237</a></li>
<li><a href="http://brainfuck.org/" target="_blank">http://brainfuck.org/</a></li>
</ol>
<p class="last-modified">Last modified: 202206120959</p></article></main><footer><nav><a href="index.html">Sitemap</a></nav><div class="social"><p>Built using <a href="http://codeberg.org/milofultz/swiki" target="_blank" rel="noopener noreferrer">{{SWIKI}}</a></p><p><a href="http://codeberg.org/milofultz/" target="_blank" rel="noopener noreferrer">Codeberg</a></p><p><a href="http://milofultz.com/" target="_blank" rel="noopener noreferrer">milofultz.com</a></p><p><a href="https://merveilles.town/@milofultz" target="_blank" rel="me noopener noreferrer">Mastodon</a></p></div></footer></body></html>