Creating an if (x == y) statement in Brainfuck

So, I am working on a program that reads in a file and then prints it back, but I had problems with the fact that the program stopped accepting input at the end of the file. I want him to focus on a specific character, such as "0" or "$", or something really, since a single NULL character cannot be read in my brainf interpreter. Here is the code:

>+[>,][<.] 

The problem starts with [>,] , since the input can never be NULL, this loop never ends.

So, how can I insert an if statement that will end this loop if it ever reaches the given end character?

+6
source share
3 answers

The following code is equivalent to your code, except that it will stop when the input value is 1 (not printable in ASCII). <between cycles because the last value is 0.

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

It decreases the value after input, checks to see if it is 0, and goes back if it is not. If it loops back, it must increase the pointer again to cancel the decrement. An example array can be:

 00 02 H ello _ W orld 00 ^ 

However, [<.] Prints the return line (followed by unprintable 1). The line itself can be printed by moving the pointer to the beginning and moving from there, as shown in this code:

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

In this code, [<] stops when it reaches index 0, >> goes to index 2 (beginning of line), and [.>] Displays characters until it reaches 0 at the end.

If you want to use another ASCII character, such as space (32), repeat the + and - cycle in the first cycle many times. (Warning: this code will result in values โ€‹โ€‹below 0 if your character input is less than 32).

 >+[++++++++++++++++++++++++++++++++>,--------------------------------]<[<]>>[.>] 
+5
source

Start with your significant character - release $ , since its ASCII 36:

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

Read the input by copying both the input and the significant character twice, with the second significant character at the end:

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

for such a structure:

  โ”Œโ”€โ”€โ”€โ”ฒโ”โ”โ”โ”โ”โ”โ”โ”ฑโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”Œโ”€โ”€โ”€โ”
 โ”‚ $ โ”ƒ blank โ”ƒ input โ”‚ input โ”‚ $ โ”‚
 โ””โ”€โ”€โ”€โ”บโ”โ”โ”โ”โ”โ”โ”โ”นโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ””โ”€โ”€โ”€โ”˜ 

Subtract the first $ from the first input :

 <[->>-<<]>> 

If it is not equal to zero, move it three times to an empty cell after copying $ , then step back non-stop, exiting the loop when the input was $ and otherwise leave you at $ , ready to start:

 [>>>]<] 

After the loop, you remain on the blank matching character. Move forward to the corresponding input character and erase it so that it is not reprinted, move back five times to get an intact copy of the second and last input and save a backup from there (this should not rely on transfer interpreters and, for example, if you shift forward a little at the beginning):

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

And then print them out!

 >>>>>[.>>>>>] 

Generally,

 ++++++[->++++++<]> [[->+>>>>+<<<<<]>>,[->+>+<<]<[->>-<<]>>[>>>]<] >>[-]<<<<<[<<<<<]>>>>>[.>>>>>] 
+2
source

This is a useful page full of BF algorithms that I like to use.

Required algorithms: # 19 ( x = x==y ) and either # 28 ( if (x) {code} ) or # 30 ( if (x) {code1} else {code2} ).

Checking for the presence of two cells is quite simple, so I just copy the if (x) {code1} else {code2} algorithm here. temp0 means go to the cell to use as a temporary variable. This particular code requires temp0 and temp1 follow x in memory sequentially.

 temp0[-]+ temp1[-] x[ code1 x>-]> [< code2 x>->]<< 
0
source

Source: https://habr.com/ru/post/972555/


All Articles