Last active
October 8, 2016 17:58
-
-
Save tonyonodi/776ee82dac206f981f47cb53c9160796 to your computer and use it in GitHub Desktop.
Brainfuck interpreter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Here's a relatively verbose version that is somewhat easier to read | |
//////////////////////////////////////// | |
let program = '+++>++<[->+<]>.'; | |
let arr = []; | |
let dataPointer = 0; | |
let instructionPointer = 0; | |
let output = ''; | |
while (program[instructionPointer]) { | |
switch (program[instructionPointer]) { | |
// Increment the data pointer (to point to the next cell to the right). | |
case '>': | |
dataPointer++; | |
break; | |
// Decrement the data pointer (to point to the next cell to the left). | |
case '<': | |
dataPointer--; | |
break; | |
// Increment (increase by one) the byte at the data pointer. | |
case '+': | |
arr[dataPointer] = (arr[dataPointer] || 0) + 1; | |
break; | |
// Decrement (decrease by one) the byte at the data pointer. | |
case '-': | |
arr[dataPointer] = (arr[dataPointer] || 0) - 1; | |
break; | |
// Output the byte at the data pointer. | |
case '.': | |
output += arr[dataPointer] | |
break; | |
// Accept one byte of input, storing its value in the byte at the data | |
// pointer. | |
case ',': | |
arr[dataPointer] = parseInt(prompt()); | |
break; | |
// If the byte at the data pointer is zero, then instead of moving the | |
// instruction pointer forward to the next command, jump it forward to | |
// the command after the matching ] command. | |
case '[': | |
if (! arr[dataPointer]) | |
while (program[instructionPointer] != ']') | |
instructionPointer++; | |
break; | |
// If the byte at the data pointer is nonzero, then instead of moving the | |
// instruction pointer forward to the next command, jump it back to the | |
// command after the matching [ command. | |
case ']': | |
if (arr[dataPointer]) | |
while (program[instructionPointer] != '[') | |
instructionPointer--; | |
break; | |
} | |
instructionPointer++; | |
} | |
console.log(output); | |
// Here's a version that starts to bring the character count down | |
//////////////////////////////////////// | |
let program = '[->+<]>.'; | |
let arr = [2,2]; | |
let dataPointer = 0; | |
let instructionPointer = 0; | |
let output = ''; | |
const instructions = '><+-.,[]'; | |
const funcs = [ | |
() => dataPointer++, | |
() => dataPointer--, | |
() => arr[dataPointer] = (arr[dataPointer] || 0) + 1, | |
() => arr[dataPointer] = (arr[dataPointer] || 0) - 1, | |
() => output += arr[dataPointer], | |
() => arr[dataPointer] = parseInt(prompt()), | |
() => {if (! arr[dataPointer]) while (program[instructionPointer] != ']') instructionPointer++}, | |
() => {if (arr[dataPointer]) while (program[instructionPointer] != '[') instructionPointer--}, | |
] | |
while (i = program[instructionPointer]) { | |
funcs[instructions.indexOf(i)](); | |
instructionPointer++; | |
} | |
console.log(output); | |
// This version is just 234 characters. There is still scope to decrease character count. | |
//////////////////////////////////////// | |
B=p=>{for(a=[],o='',d=I=0;i=p[I];I++){[_=>d++,_=>d--,_=>a[d]=(a[d]||0)+1,_=>a[d]=(a[d]||0)-1,_=>o+=(a[d]||0)+'\n',_=>a[d]=+prompt(),_=>{if(!a[d])while(p[I]!=']')I++},_=>{if(a[d])while(p[I]!='[')I--}]['><+-.,[]'.indexOf(i)]()}return o} | |
// You can run a program as follows. | |
// This program takes two numbers as input, adds them together then outputs them. | |
B(',>,<[->+<]>.') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment