4

i have written an alpha version of an brainfuck ide. i wrote my own interpreter although i had massive help from my teacher regarding loops in the code because i had a really hard time understanding it in the "IT way". now for my report i need a state chart of the algorithm of the interpreter, how he handles each char.

i have come up with the following diagram, only thing missing is how the interpreter handles loops. i looked at the code my teacher wrote almost by himself but i dont understand it. i hope you can point me in the right direction here, i dont want a finished answer just a few sidenotes what is being done when an [ or ] is encountered in the code.

enter image description here

codeZeiger = codePointer (the pointer which moves through the code)
memoryZeiger = memoryPointer (the pointer wich moves through the memory stack)
memory = the memory stack
code = the code as a string oject
i = counter of the interpre() method (single chars are read from the string and then parsed through a switch statement whose statechart you see below)

enter image description here

LeonidasFett
  • 2,574
  • 4
  • 32
  • 67

2 Answers2

4

You should really try to understand the looping mechanism. In brainfuck, loops are enclosed with [ and ]. That means the code inside the brackets will execute and start all over again if a certain condition is met. For example:

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

Line 1 checks whether memory[memoryZeiger] is equal to 0. If it is, it jumps to line 5. If not, it executes cmd1, cmd2, and so on up to line 4. If your interpreter is on line 4, it automatically jumps to line1 (or it could check the condition and move one step further - but let's keep it simple and assume it jumps 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++

The other case for ] should be equivalent.

Btw, "matching ]" is important. Those brackets can be nested!

duedl0r
  • 8,785
  • 3
  • 27
  • 44
  • thanks i will read into this a little more, for now i have done a structure diagram. it's a little wide, sorry about that. i think i understand what you are trying to say, but i am pretty new to uml (thats why we are having this practice project) but with a few tries i think i can get the diagram to be clear and expressive. – LeonidasFett Dec 18 '12 at 11:14
3

1) you don't need a statechart, as your compiler does not have states (only memory, memory pointer and code pointer and possibly two for finding the matching bracket) - a simple table like on wikipedia (german like your variable names) would be enough

2) if you stick to a statechart don't put conditions (like code[codeZeiger]=='+') into states but on the transitions

3) i must be changed to codeZeiger instead

4) The code to interpret brainfuck should be very simple. If you don't understand it read e.g. the wikipedia page and try to interpret the program given there without a software. Let it run on paper :)

Christian
  • 9,499
  • 2
  • 27
  • 47