Brainfuck翻译错误的行为

时间:2017-08-19 18:01:00

标签: javascript interpreter brainfuck

我有以下Brainfuck解释器,它通过了一个最小的测试套件。除了像印刷斐波纳契序列这样的大问题似乎都失败了(套件中的最后一次测试)。我解释无法执行的brainfuck斐波那契代码来自http://esoteric.sange.fi/brainfuck/bf-source/prog/fibonacci.txt我的解释有什么问题? 这是一个小提琴: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>

1 个答案:

答案 0 :(得分:3)

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

&#13;
&#13;
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();
&#13;
<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>
&#13;
&#13;
&#13;