GOTO Instruction in Brainf ***

There is an enhanced version of bf , which has instructions goto

, ?

.

I know that in theory it should be possible to simulate goto in the classic version 8 of bf. How can I do this in practice? Is there an existing bf goto pattern or algorithm ? Is there a way to convert goto statement ?

to bf version without goto statement ?

?

+3


source to share


3 answers


There are no easy and trivial ways to do this that I know of. This is one of the main problems when compiling a language in Brainfuck.

This is of course possible in theory, as you said, but it requires you to structure your code in a very disciplined manner, by implementing a stack or heap or some similar data structure.

The C2BF project is a partial C to Brainfuck compiler that does just that. It emulates a stack by interpreting Brainfuck cells in a repeating pattern of five, namely



1) Stack
2) Heap
3) Stack location marker / top marker
4) Walk
5) Carry

If you're interested in more specific implementation details for this sort of thing, you might be interested in looking at this description for BrainFix , which goes into more detail on how to do simple flow and memory control.

+1


source


It's actually not that hard. The pseudocode you want to do is the following:

goflg := 1
while goflg do begin

  // repeat this for each section
  if goflg <> 0 then begin
    goflg := goflg - 1
  end else begin
    // run your code in here and at the end
    // set goflg to get to the section you want next.
  end
  // to here

  // repeat this for each section
  if goflg <> 0 then begin
    goflg := goflg - 1
  end else begin
    // run your code in here and at the end
    // set goflg to get to the section you want next.
  end
  // to here


end

      



You will need to add a flag for the ELSE part and another if you want to make the IF part without a copy , but this is perfectly doable.

0


source


The only way I can think of is to use the brainfuck interpreter written in brainfuck to run the program (like dbfi). With some modifications, you can add new instructions like LBL and GOTO.

The only problem is that it will be very slow. Another problem is that you will need to save the actual brainfuck program to memory tape (the easiest way to do this is to type the program like dbfi does). For a cleaner way to do this, you have to make a brainfuck program that puts the actual program on tape so that the interpreter can read and run it.

This is certainly not a very elegant method, but I think it might actually work very well, although it will of course be very slow.

0


source







All Articles