Red Mercury Red Mercury - 2 months ago 10
Brainfuck Question

Brainfuck interpreter wrong behaviour

I have the following Brainfuck interpreter that is passing a minimal test suite. Except a sizeable problem like printing fibonacci sequence seems to fail (the last test in the suite). The brainfuck fibonacci code that my interpreted fails to execute is from http://esoteric.sange.fi/brainfuck/bf-source/prog/fibonacci.txt. What is wrong with my interpreted?
Here is a fiddle: https://jsfiddle.net/rt017kpz/



function brainLuck(code, input) {
// "Infinite" heap of memory initialized as 0 for bf to store program values
let data = new Proxy([], {get: (arr, i) => arr[i] ? arr[i] : 0 })
let ptr = 0;
let inputPtr = 0;
let codePtr = 0;
let output = '';
let loopStack = []

const op = {
'+' : () => {
if (data[ptr] === 255) { data[ptr] = 0; }
else { data[ptr]++; }
codePtr++;
},
'-' : () => {
if (data[ptr] === 0) { data[ptr] = 255; }
else { data[ptr]--; }
codePtr++;
},
'.' : () => {
output += String.fromCharCode(data[ptr]);
codePtr++;
},
',' : () => {
data[ptr] = input.charCodeAt(inputPtr++);
codePtr++;
},
'>' : () => {
ptr++; codePtr++;
},
'<' : () => {
if (ptr > 0) { ptr--; }
codePtr++;
},
'[' : () => {
if (data[ptr] === 0) {
while(code[codePtr] !== ']') codePtr++;
} else {
loopStack.unshift(codePtr);
}
codePtr++
},
']' : () => {
if (data[ptr] === 0) { loopStack.shift(); }
else { codePtr = loopStack[0] }
codePtr++
}
}

while(codePtr < code.length) {
if(op[code[codePtr]]) op[code[codePtr]]()
else codePtr++
}
return output;
}



////////// TESTS //////////////

it('handles `+` and `.` operants', () => {
expect(
brainLuck('+++.')
).to.equal(
String.fromCharCode(3)
)
})

it('handles `-` operant and underflows from 0 to 255', () => {
expect(
brainLuck('+++----.')
).to.equal(
String.fromCharCode(255)
)
})

it('handles `,` the input operand', () => {
expect(
brainLuck(',.', 'A')
).to.equal(
'A'
)
})

it('handles input in conjuction with arithmetic', () => {
expect(
brainLuck(',+.', 'A')
).to.equal(
'B'
)
})

it('handles looping (`[`, `]`) and shift (`<`, `>`) operants', () => {
expect(
brainLuck(',>+++[<.>-]', 'A')
).to.equal(
'AAA'
)
})

it('only parses known symbols', () => {
expect(
brainLuck(',nothing>++else+[<.>matters!-]', 'A')
).to.equal(
'AAA'
)
})

it('handles nested loops', () => {
expect(
brainLuck(',>+++[->+++[<<.>>-]<]', 'A')
).to.equal(
'AAAAAAAAA'
)
})

it('can multiply two numbers', () => {
expect(
brainLuck(',>,<[>[->+>+<<]>>[-<<+>>]<<<-]>>.', String.fromCharCode(8,9))
).to.equal(
String.fromCharCode(72)
)
})

it('can print the fibonacci sequence', () => {
expect(
brainLuck('++++>+>>>>++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++<<<<<<[>[>>>>>>+>+<<<<<<<-]>>>>>>>[<<<<<<<+>>>>>>>-]<[>++++++++++[-<-[>>+>+<<<-]>>>[<<<+>>>-]+<[>[-]<[-]]>[<<[>>>+<<<-]>>[-]]<<]>>>[>>+>+<<<-]>>>[<<<+>>>-]+<[>[-]<[-]]>[<<+>>[-]]<<<<<<<]>>>>>[++++++++++++++++++++++++++++++++++++++++++++++++.[-]]++++++++++<[->-<]>++++++++++++++++++++++++++++++++++++++++++++++++.[-]<<<<<<<<<<<<[>>>+>+<<<<-]>>>>[<<<<+>>>>-]<-[>>.>.<<<[-]]<<[>>+>+<<<-]>>>[<<<+>>>-]<<[<+>-]>[<+>-]<<<-]')
).to.equal(
'1, 1, 2, 3'
)
})


mocha.run();

<html>
<head>
<meta charset="utf-8">
<title>Mocha Tests</title>
<link href="https://cdn.rawgit.com/mochajs/mocha/2.2.5/mocha.css" rel="stylesheet" />
</head>
<body>
<div id="mocha"></div>
<script src="https://cdn.rawgit.com/Automattic/expect.js/0.3.1/index.js"></script>
<script src="https://cdn.rawgit.com/mochajs/mocha/2.2.5/mocha.js"></script>
<script>mocha.setup('bdd')</script>
</body>
</html>




Answer Source

Your implementation of [ searches for the first ] rather than the matching ], which might be the second, third, fourth, etc. ] symbol. You need to count them as you scan. The following snippet has an implementation of [ that does this, and the fib test now works:

function brainLuck(code, input) {
  // "Infinite" heap of memory initialized as 0 for bf to store program values
  let data = new Proxy([], {get: (arr, i) => arr[i] ? arr[i] : 0 })
  let ptr = 0;
  let inputPtr = 0;
  let codePtr = 0;
  let output = '';
  let loopStack = []

  const op = {
    '+' : () => {
      if (data[ptr] === 255) { data[ptr] = 0; }
      else { data[ptr]++; }
      codePtr++;
    },
    '-' : () => {
      if (data[ptr] === 0) { data[ptr] = 255; }
      else { data[ptr]--; }
      codePtr++;
    },
    '.' : () => { 
      output += String.fromCharCode(data[ptr]); 
      codePtr++;
    },
    ',' : () => {
      data[ptr] = input.charCodeAt(inputPtr++); 
      codePtr++;
    },
    '>' : () => { 
      ptr++; codePtr++; 
    },
    '<' : () => {
      if (ptr > 0) { ptr--; }
      codePtr++; 
    },
    '[' : () => {
      if (data[ptr] === 0) {
        let level = 0;
        while(code[codePtr] !== ']' || level > 1) {
           if (code[codePtr] === '[')
              level += 1;
           if (code[codePtr] === ']')
              level -= 1;
           codePtr++;
        }
      } else { 
        loopStack.unshift(codePtr);
      }
      codePtr++ 
    },
    ']' : () => {
      if (data[ptr] === 0) { loopStack.shift(); }
      else { codePtr = loopStack[0] }
      codePtr++
    }
  }

  while(codePtr < code.length) {
    if(op[code[codePtr]]) op[code[codePtr]]()
    else codePtr++
  }
  return output;
}



////////// TESTS //////////////

it('handles `+` and `.` operants', () => {
  expect(
    brainLuck('+++.')
  ).to.equal(
    String.fromCharCode(3)
  )
})

it('handles `-` operant and underflows from 0 to 255', () => {
  expect(
    brainLuck('+++----.')
  ).to.equal(
    String.fromCharCode(255)
  )
})

it('handles `,` the input operand', () => {
  expect(
    brainLuck(',.', 'A')
  ).to.equal(
    'A'
  )
})

it('handles input in conjuction with arithmetic', () => {
  expect(
    brainLuck(',+.', 'A')
  ).to.equal(
    'B'
  )
})

it('handles looping (`[`, `]`) and shift (`<`, `>`) operants', () => {
  expect(
    brainLuck(',>+++[<.>-]', 'A')
  ).to.equal(
    'AAA'
  )
})

it('only parses known symbols', () => {
  expect(
    brainLuck(',nothing>++else+[<.>matters!-]', 'A')
  ).to.equal(
    'AAA'
  )
})

it('handles nested loops', () => {
  expect(
    brainLuck(',>+++[->+++[<<.>>-]<]', 'A')
  ).to.equal(
    'AAAAAAAAA'
  )
})

it('can multiply two numbers', () => {
  expect(
    brainLuck(',>,<[>[->+>+<<]>>[-<<+>>]<<<-]>>.', String.fromCharCode(8,9))
  ).to.equal(
    String.fromCharCode(72)
  )
})

it('can print the fibonacci sequence', () => {
  expect(
    brainLuck('++++>+>>>>++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++<<<<<<[>[>>>>>>+>+<<<<<<<-]>>>>>>>[<<<<<<<+>>>>>>>-]<[>++++++++++[-<-[>>+>+<<<-]>>>[<<<+>>>-]+<[>[-]<[-]]>[<<[>>>+<<<-]>>[-]]<<]>>>[>>+>+<<<-]>>>[<<<+>>>-]+<[>[-]<[-]]>[<<+>>[-]]<<<<<<<]>>>>>[++++++++++++++++++++++++++++++++++++++++++++++++.[-]]++++++++++<[->-<]>++++++++++++++++++++++++++++++++++++++++++++++++.[-]<<<<<<<<<<<<[>>>+>+<<<<-]>>>>[<<<<+>>>>-]<-[>>.>.<<<[-]]<<[>>+>+<<<-]>>>[<<<+>>>-]<<[<+>-]>[<+>-]<<<-]')
  ).to.equal(
    '1, 1, 2, 3'
  )
})


mocha.run();
<html>
<head>
  <meta charset="utf-8">
  <title>Mocha Tests</title>
  <link href="https://cdn.rawgit.com/mochajs/mocha/2.2.5/mocha.css" rel="stylesheet" />
</head>
<body>
  <div id="mocha"></div>
  <script src="https://cdn.rawgit.com/Automattic/expect.js/0.3.1/index.js"></script>
  <script src="https://cdn.rawgit.com/mochajs/mocha/2.2.5/mocha.js"></script>
  <script>mocha.setup('bdd')</script>
</body>
</html>