while-loopbrainfuckesoteric-languages

Efficient implementation of while loop in brainfuck


I am having trouble with implementing a brainfuck assembler for codegolf.se. I managed to load a string in to memory find its length cat it out, print strings n times etc, but I cant seem to load just the non lower case numbers into memory. So lets take the following loop which performs some wizardry. (Hash marks are debugging markers.)

#,#[>#<[<]<<#+#>>>[>]#,#]<[<] 

It starts at pointer 512 and writes the string as ascii values to spots after 512


Now if (for whatever reason) I wish to strip out lowercase characters, it will look like this in psuedo BF.

#,#[>#<[<]<<#+#>>>[>]#do{,(takes input and assigns it)}
while(input>=96/*Go arbitrarily to the right for this implementation but
make sure that the first non-lowercase number is stored at the index*/)#
//Also be sure to zero out any temporary cells used
<[<] 

Now my question is, how do I implement such a while loop while only using spaces to the right of 512 as storage AND clearing them out later.

For those curious, this is the problem I wish to solve in branfuck.


Solution

  • Your code can be simplified to

    ,[[<]<+>>[>],]<[<]
    

    (the <<+>> is probably a result of using online compiler that forgets cell 255)

    and repeated, to produce the outputting operation:

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

    If you want to use only the empty cells in your way, you can do it. But you will need to establish some protocol of your own for defining the next cell, like saving every data cell with the following cell stating the distance to the next one, as:

    [..., 104, 5, x, x, x, x, 108, 3, x, x, 102, 2...] 
    
    [...,  104 ,    5   , x, x, x, x,  108 ,    3    , x, x,  102 ,    2   ...] 
          data   pointer              data   pointer         data   pointer
    

    when x is some arbitrary, none-zero value (otherwise you would use it). This implementation would be kind-of a linked list, but notice it would be space and code expensive.


    Zeroing down cells, or as you call it cleaning them, can be done the same way you did the [<] - by using [-]. this will decrease the cell's value until it reaches 0 - and then will loop-out. You can iterate the string down when you are in its end - and go back while cleaning every cell until you hit the beginning (0 or other reserved number you put there).