BrainFuck part 3 - Write a BF interpreter in BF

DPAmar
17.4K views

Open Source Your Knowledge, Become a Contributor

Technology knowledge has to be shared and made accessible for free. Join the movement.

Create Content

Memory, left and right

Let's design our memory area now, and see how we can implement the 2 first insructions over 8, left and right.

Our new memory map can be : 0, 0, 0, instructions, 0, instruction pointer, 0, 0, inactive flag, direction flag, memory pointer, some free space 0, memory

Note that we need 2 cells after instruction pointer to

  • copy the instruction pointer (2 cells)
  • copy the current instruction code (just one cell, after instruction)
  • switch on instruction (2 cells : one for instruction, one for else flag)

Inactive and direction flags have been quickly introduced at the very first step of this playground, though they have not benn implemented yet.

Memory access will be performed like instructions : with a pointer on an array. This will introduce the same "255 max" limitation, but this will be addressed later.

Now, let's implement < and > instructions

Let's start

  • Memory: 0, 0, 0, instructions, 0, instruction pointer, 0, 0, inactive flag, direction flag, memory pointer, some free space 0, memory
  • Cursor: on current instruction, stored after instruction pointer (note: instruction is either > or <)
  • Input: any

Process

  • Increase (or decrease) memory pointer

Code

>>>>+<<<<

or

>>>>-<<<<

Final state

  • Memory: unchanged but mem pointer value
  • Cursor: unchanged
  • Input: unchanged
  • Output: unchanged
Open Source Your Knowledge: become a Contributor and help others learn. Create New Content