Hexagonal two-type tiles
[zzandy.git] / brainfuck.html
blob2a565ab8c3fafa93600e430474f4c62608368910
1 <?xml version="1.0" encoding="UTF-8"?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
3 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
4 <head>
5 <meta content="text/html; charset=utf-8" http-equiv="Content-Type"/>
6 <link rel="stylesheet" href="style/pixelless.css" type="text/css"/>
7 <script type="text/javascript" src="script/util.js"></script>
8 <title>Brainfuck</title>
9 </head>
10 <body>
11 <div id="container">
12 <div class="content header">
13 <h1>Brainfuck</h1>
14 </div>
16 <div class="content">
17 <p><a href="http://en.wikipedia.org/wiki/Brainfuck">Brainfuck</a> "IDE"</p>
18 <a href="#" onclick="run(); return false;" id="runit" style="text-decoration: none">&#x25B6; Run</a><br/>
20 <pre>Clear: <a href="javascript:add(this)" onclick="add(this); return false;">[-]</a> Clear prev no zero: <a
21 href="javascript:add(this)" onclick="add(this); return false;">[[-]&lt;]</a>
22 Rewind: <a href="javascript:add(this)" onclick="add(this); return false;">[&lt;]&gt;</a> Fast-forward: <a
23 href="javascript:add(this)" onclick="add(this); return false;">[&gt;]&lt;</a>
24 Cat: <a href="javascript:add(this)" onclick="add(this); return false;">,[.,]</a> Moving the pointer: <a
25 href="javascript:add(this)" onclick="add(this); return false;">&gt;,[.&gt;,]</a>
26 Add: <a href="javascript:add(this)" onclick="add(this); return false;">[-&gt;+&lt;]</a> or <a
27 href="javascript:add(this)" onclick="add(this); return false;">[-&gt;+&gt;+&lt;&lt;]&gt;&gt;[-&lt;&lt;+&gt;&gt;]</a> Copy: <a
28 href="javascript:add(this)" onclick="add(this); return false;">[-]&gt;[-]&lt;&lt;[-&gt;+&gt;+&lt;&lt;]&gt;&gt;[-&lt;&lt;+&gt;&gt;]</a></pre>
31 <div id="memory" style="float:right"></div>
32 <div id="hint" style="clear: right; float:right">
33 &gt; move right<br/>
34 &lt; move left<br/>
35 + increment data<br/>
36 - decrement data<br/>
37 . output<br/>
38 , input<br/>
39 [ if data is zero jump after matching ]<br/>
40 ] id data is not zero goto matching [<br/>
41 </div>
42 <label>Input: <input type="text" id="input" value="" style="margin-bottom: .2em; font-family:monospace; width:80ex"/></label><br/>
43 <textarea id="edit" cols="80" rows="14"></textarea>
44 <textarea id="out" cols="80" rows="6" readonly="readonly"></textarea>
45 </div>
46 </div>
47 <div id="footer">
48 <div class="content">
49 &copy; Copyright
50 </div>
51 </div>
52 <script type="text/javascript" language="JavaScript">
53 var memoryLimit = 30000;
54 var execLimit = 10000;
56 function add(obj)
58 var target = document.getElementById('edit');
59 var x = target.selectionStart;
60 var value = obj.innerHTML.replace(/&lt;/g, "<").replace(/&gt;/g, ">");
61 var before = target.value.substring(0, x);
62 var after = target.value.substr(target.selectionEnd);
63 target.value = before + value + after;
64 target.selectionStart = x + value.length;
65 target.selectionEnd = x + value.length;
66 target.focus();
69 function run()
71 function add(n)
73 var x = (memory[addr] || 0) + n;
74 if (x > 255)x = 0;
75 if (x < 0)x = 255;
76 memory[addr] = x;
77 return x;
80 function get_input()
82 var r = input[inpos++];
83 if (r === undefined) return 0; // EOF
84 return r.charCodeAt(0);
87 var inpos = 0;
88 var input = document.getElementById('input').value;
89 var src = document.getElementById('edit').value;
91 var addr = 0;
92 var memory = [];
93 var out = '';
95 var line = 1;
96 var column = 0;
98 var loops = [];
99 var steps = 0;
100 var loopstack = [];
102 var i = -1, n = src.length;
103 while (++i < n)
105 ++column;
106 var op = src[i];
107 switch (op) {
108 case '[':
109 loops[i] = 0;
110 loopstack.push(i);
112 break;
114 case ']':
115 var loop = loopstack.pop();
117 if (loop === undefined)
118 throw new Error(['Loop brace mismatch at line', line, ', column', column].join(' '));
119 loops[loop] = i;
121 break;
123 case '\n':
124 ++line;
125 column = 1;
127 break;
131 delete loopstack;
133 line = 1;
134 column = 0;
135 i = -1;
136 n = src.length;
137 while (++i < n)
139 if (++steps > execLimit)
140 throw new Error(['Execution limit of ', execLimit, ' cycles exceded at line ', line, ', column', column].join(' '));
142 ++column;
143 var op = src[i];
144 switch (op) {
145 case '\n':
146 ++line;
147 column = 0;
149 break;
151 case '<':
152 if (--addr < 0)
153 throw new Error(['Address underrun at line', line, ', column', column].join(' '));
155 break;
157 case '>':
158 if (++addr > memoryLimit)
159 throw new Error(['Address overrun at line', line, ', column', column].join(' '));
161 break;
163 case '-':
164 add(-1);
166 break;
168 case '+':
169 add(1);
171 break;
173 case '[':
174 if (!memory[addr])
175 i = loops[i];
177 break;
179 case ']':
180 i = loops.indexOf(i);
181 if (i == -1)
182 throw new Error(['Unxepected loop error at line', line, ', column', column].join(' '));
184 --i;
185 break;
187 case '.':
188 if (memory[addr] < 32)
189 out += memory[addr].toString() + ' ';
190 else
191 out += String.fromCharCode(memory[addr]);
193 break;
195 case ',':
196 memory[addr] = get_input();
198 break;
202 document.getElementById('memory').innerHTML = memory.map(function(x, n) {
203 if (n == addr)return '<u>' + x + '</u>'; else return x;
205 document.getElementById('out').value = out;
209 </script>
210 </body>
211 </html>