Skip to content

Instantly share code, notes, and snippets.

@ptrelford
Created December 17, 2012 06:13
Show Gist options
  • Save ptrelford/4316147 to your computer and use it in GitHub Desktop.
Save ptrelford/4316147 to your computer and use it in GitHub Desktop.
Try Brainfuck web page
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Try Brainfuck</title>
<meta name="expires" content="0" />
<meta name="keywords" content="Brainfuck" />
</head>
<body>
<h1>Try Brainfuck</h1>
<textarea id="editor" cols="40" rows="4">
</textarea>
<table>
<tr>
<td><button id="startButton" onclick="onstart();">Start</button></td>
<td><button id="cancelButton" onclick="oncancel();" disabled="disabled">Cancel</button></td>
</tr>
</table>
<textarea id="console" cols="40" rows="4" wrap="hard"></textarea>
<script type="text/javascript">
var program, pc;
var data = Array(30000), p;
function execute() {
var i = program[pc];
switch (i)
{
case '>': ++p; ++pc; break;
case '<': --p; ++pc; break;
case '+': data[p] = data[p] + 1; ++pc; break;
case '-': data[p] = data[p] - 1; ++pc; break;
case '.': write(String.fromCharCode(data[p])); ++pc; break;
case ',': read(function(key){data[p]=key.charCodeAt(0); pc++;}); return false;
case '[':
if (data[p] == 0) {
var open = 0;
while (pc < program.length) {
if (program[pc] == ']') {
--open;
if (open == 0) break;
}
else if (program[pc] == '[')++open;
++pc;
}
}
else ++pc;
break;
case ']':
if (data[p] != 0) {
var open = 0;
while (pc < program.length)
{
if (program[pc] == '[') {
--open;
if (open == 0) break;
}
else if (program[pc] == ']') ++open;
--pc;
}
}
else ++pc;
break;
default: ++pc; break;
}
return true;
}
var editor, console;
var startButton, cancelButton;
function read(callback) {
console.readOnly = false;
console.onkeypress = function (event) {
callback(getChar(event));
console.onkeypress = function(event) {return true;}
setTimeout(run);
}
}
function write(x) {
console.value += x;
console.focus();
}
var cancelled;
function run() {
console.readOnly = true;
var counter = 0;
while (pc < program.length) {
if (cancelled) break;
if (++counter == 1000)
{
setTimeout(run);
return;
}
if (!execute()) break;
}
startButton.disabled = false;
cancelButton.disabled = true;
}
function $(id) { return document.getElementById(id); }
// event.type must be keypress
function getChar(event) {
if (event.which == null) {
return String.fromCharCode(event.keyCode) // IE
} else if (event.which != 0 && event.charCode != 0) {
return String.fromCharCode(event.which) // the rest
} else {
return null // special key
}
}
function init() {
editor = $("editor");
console = $("console");
startButton = $("startButton");
cancelButton = $("cancelButton");
}
function onstart() {
init();
cancelled = false;
startButton.disabled = true;
cancelButton.disabled = false;
pc = 0;
for (var i = 0; i < 30000; i++) data[i] = 0;
p = 0;
program = editor.value;
console.value = "";
console.focus();
run();
}
function oncancel() {
init();
cancelled = true;
cancelButton.disabled = true;
startButton.disabled = false;
}
function edit(code) {
oncancel();
editor.value = code;
editor.focus();
}
function echo() {
edit("+[,.]");
}
function fib() {
edit(">++++++++++>+>+[" +
"[+++++[>++++++++<-]>.<++++++[>--------<-]+<<<]>.>>[" +
"[-]<[>+<-]>>[<<+>+>-]<[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-" +
"[>+<-[>+<-[>+<-[>[-]>+>+<<<-[>+<-]]]]]]]]]]]+>>>" +
"]<<<" +
"]");
}
function helloWorld() {
edit("++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.");
}
</script>
<ul>
<li><a href="JavaScript:helloWorld();">Hello World!</a></li>
<li><a href="JavaScript:fib();">fib</a></li>
<li><a href="JavaScript:echo();">echo</a></li>
</ul>
<p><a href="http://brainfuck.org">more Brainfuck snippets</a></p>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment