Cerebral ear

I wrote an alpha version of the brain mind. I wrote my own interpreter, although I had a huge help from my teacher regarding loops in the code, because it was very difficult for me to understand it in the โ€œIT wayโ€. Now for my report I need a state diagram of the interpreter algorithm, how it processes each char.

I came up with the following diagram: only the way the interpreter handles loops is missing. I looked at the code that my teacher wrote almost myself, but I do not understand it. I hope you can point me in the right direction here, I donโ€™t want the finished answer to be just a few side effects, which is done when [or] occurs in the code.

enter image description here

codeZeiger = codePointer (pointer that moves through the code)
memoryZeiger = memoryPointer (pointer that moves through the memory stack)
memory = memory stack
code = code in the form of a string i = counter of the interpre () method (individual characters are read from the string and then analyzed using the switch statement, whose status bar is displayed below)

enter image description here

+4
source share
2 answers

You really should try to understand the mechanism of the loop. In brainfuck, loops are enclosed in [ and ] . This means that the code inside the brackets will be executed and run again if a certain condition is met. For instance:

 1: [ 2: cmd1 3: cmd2 4: ] 5: cmd3 

Line 1 checks to see if memory[memoryZeiger] 0. If there is one, it goes to line 5. If not, it executes cmd1, cmd2, etc. to line 4. If your interpreter is on line 4, it automatically jumps to line 1 (or it can check the status and move one step further, but let it be simple and suppose that it goes to line1). Then the whole process starts again.

So, to answer your question about the state diagram. You need something like this:

  _____________________________ | code[codeZeiger] == '[' | ----------------------------- / \ / \ memory[memoryZeiger] == 0 memory[memoryZeiger] != 0 | | "go to matching ']'" codeZeiger++ 

Another case for ] should be equivalent.

Btw, matching is important. These brackets can be nested!

+4
source

1) you do not need a status file, since your compiler does not have states (only memory, a memory pointer and a code pointer, and possibly two for finding a suitable bracket) - a simple table, for example, on wikipedia (German as your variable names) will be enough

2) if you adhere to the state template, do not put conditions (for example, code[codeZeiger]=='+' ) in states, but on transitions

3) i should be replaced with codeZeiger instead

4) The code for interpreting brainfuck should be very simple. If you do not understand this, read for example. wikipedia page and try to interpret this program without software. Let it work on paper :)

+3
source

All Articles