cboin / brainfuck-interpreter

A simple brainfuck interpreter
0 stars 1 forks source link

You are not a fascist aren’t you? #3

Open irevoire opened 4 years ago

irevoire commented 4 years ago

If you want to be able to run program like that:

>++++++++[-<+++++++++>]<.>>+>-[+]++>++>+++[>[->+++<<+++>]<<]>-----.>->
+++..+++.>-.<<+[>[+>+]>>]<--------------.>>.+++.------.--------.>+.>+.

You need to handle negative cells which you don’t currently: https://github.com/cboin/brainfuck-interpreter/blob/dab492f526c5d72173f020e32aacc5af16a6e384/brainfuck-interpreter.c#L76-L82 I guess an easy fix would be to move the starting pointer to the middle of the allocated memory so the brainfuck program would be able to go either to the left or to the right:

// ptr = t;
ptr = t + (buflen / 2);

Also the fascist appellation for this kind of interpreter come from here: http://www.hevanet.com/cristofd/brainfuck/

cboin commented 4 years ago

If I move the stating point to the middle of the allocated memory, I think I should also double the size of the allocated memory. If the loaded program wants use all the right or all left memory space, no?

rdebath commented 4 years ago

I didn't design that hello world to use negative cell positions. If your interpreter is able to handle both running and skipping over nested loops that hello world will work. It does use a cell value of -1, but that's already okay because your unsigned char cells will wrap nicely.

>++++++++[-<+++++++++>]<.>>+>-[+]++>++>+++[>[->+++<<+++>]<<]>-----.>->
+++..+++.>-.<<+[>[+>+]>>]<--------------.>>.+++.------.--------.>+.>+.
Hello World!
Program size 140
Final tape contents:
 :  72   0  87   0 100  33  10
                             ^
Tape pointer maximum 6, cell value range -1..114
Skipped loops (zero on '['): 2
Counts:     +: 395          -: 93           >: 124          <: 118
Counts:     [: 8            ]: 62           .: 13           ,: 0
Total:         813          run: 0.000s

PS: bcci errors if you do a - command on a cell with value 0 as well as a < on position 0.

PPS: Another one ...

+[>[<-[]>+[>+++>[+++++++++++>][>]-[<]>-]]++++++++++<]>
>>>>>----.<<+++.<-..+++.<-.>>>.<<.+++.------.>-.<<+.<.
Hello World!
Program size 108
Final tape contents:
Pointer at: 1
000000:  00(0a)21 6c 64 57 48 41  36 2b 20 15 0a -1        ..!ldWHA6+....
Tape pointer maximum 13, cell value range -1..114
Skipped loops (zero on '['): 14
Counts:     +: 670          -: 36           >: 101          <: 100
Counts:     [: 38           ]: 157          .: 13           ,: 0
Total:         1115         run: 0.000s