Brainfuck 解释器的错误行为

Brainfuck interpreter wrong behaviour

我有以下正在通过最小测试套件的 Brainfuck 解释器。除了一个相当大的问题,比如打印斐波那契数列似乎失败了(套件中的最后一个测试)。我解释执行失败的 brainfuck fibonacci 代码来自 http://esoteric.sange.fi/brainfuck/bf-source/prog/fibonacci.txt我的解释有什么问题? 这是一个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>

您的 [ 实现搜索第一个 ] 而不是匹配的 ],后者可能是第二个、第三个、第四个等 ] 符号.您需要在扫描时对它们进行计数。以下代码段具有执行此操作的 [ 的实现,并且 fib 测试现在有效:

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>