Making an if(x==y) statement in Brainfuck
Asked Answered
A

3

8

So I'm working on a program that reads in a file and then outputs it back out again but i'm having trouble getting the program to stop taking input at the end of the file. I want it to stop at a specific character like '0' or '$' or anything really since a one character NULL can't be read into my brainf interpreter. Here is the code so far:

>+[>,][<.]

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

So how can I insert an if statement that will terminate this loop if it ever reaches a pre-specified end character?

Abrogate answered 21/7, 2014 at 3:23 Comment(0)
W
7

The following code is equivalent to your code, except it will stop when the value of the input is 1 (non-printable in ASCII). The < between the loops is required because the last value is 0.

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

It decrements the value after input, checks if it is 0, and loops back if it isn't. If it looped back, it must increment the pointer again to undo the decrement. A sample array might be:

00  02  H  e  l  l  o  _  W  o  r  l  d  00
                                          ^

However, the [<.] prints the reverse of the string (followed by a non-printable 1). The string itself can be printed by moving the pointer to the beginning and moving forward from there, as shown in this code:

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

In this code, the [<] stops when it reaches index 0, the >> moves to index 2 (string start), and the [.>] outputs characters until it reaches the 0 at the end.

If you want to use a different ASCII character, such as space (32), repeat the + and - within the first loop that many times. (Warning: this code will result in values below 0 if there are any characters less than 32 in your input).

>+[++++++++++++++++++++++++++++++++>,--------------------------------]<[<]>>[.>]
Wylen answered 21/7, 2014 at 3:53 Comment(0)
H
3

Start with your significant character – let’s go for $, since it’s ASCII 36:

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

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

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

for a structure like this:

┌───┲━━━━━━━┱───────┬───────┬───┐
│ $ ┃ blank ┃ input │ input │ $ │
└───┺━━━━━━━┹───────┴───────┴───┘

Subtract the first $ from the first input:

<[->>-<<]>>

If it’s not zero, move forwards three times to the empty cell after the copy of $, then move backwards unconditionally, exiting the loop when the input was $ and otherwise leaving you at a $, ready to start again:

[>>>]<]

After the loop, you’re left on the blank of the matched character. Move forwards to the matching input character and erase it so it isn’t reprinted, move backwards five times to reach the second-to-last input’s intact copy, and keep backing up from there (this doesn’t have to rely on wrapping interpreters and such if you shift forwards a bit at the start):

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

And then print them out!

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

In all,

++++++[->++++++<]>
[[->+>>>>+<<<<<]>>,[->+>+<<]<[->>-<<]>>[>>>]<]
>>[-]<<<<<[<<<<<]>>>>>[.>>>>>]
Highmuckamuck answered 21/7, 2014 at 4:12 Comment(0)
P
0

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

The algorithms you would need are #19 (x = x==y) and either #28 (if (x) {code}) or #30 (if (x) {code1} else {code2}).

Checking whether two cells are equal is pretty easy, so I'll just copy the if (x) {code1} else {code2} algorithm here. temp0 means move to a cell to use as a temporary variable. This specific code does require that temp0 and temp1 follow x consecutively in memory.

temp0[-]+
temp1[-]
x[
 code1
 x>-]>
[<
 code2
 x>->]<<
Pavier answered 26/2, 2016 at 17:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.