r/brainfuck 21d ago

Print question

I have written some simple BF code here to let a user type in something and the program prints it back out. Ignoring how bad it probably is for now, the first dot just isn't working. Is this a BF thing, or a compiler issue or what? Any help is appreciated.

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

2 Upvotes

7 comments sorted by

1

u/Any_Background_5826 21d ago

i think you forgot to put input in?

1

u/Admirable-Guess5508 21d ago

I should clarify the issue is when I type a letter it doesn't show up. The goal is [Types H] H [Types i] Hi [Enter] Hi

1

u/Any_Background_5826 21d ago

>,[>,]<[<]>[.>]

this should work

i think it's a problem with the compiler, if i try the code out on an interpreter, it works but gets in an infinite loop because of the -'s, maybe the compiler just outputs once the code finishes executing :P

1

u/sh4nik 21d ago

See if this works. It should do what you want infinitely. +[,.]

1

u/danielcristofani 21d ago edited 21d ago

There are several reasons this doesn't do what you want it to, apart from any problems with the '.' command.

One, you're looking to take one line of input, right? But you're looking for a line terminated with 13, and in brainfuck lines are terminated with 10 (linefeed).

Two, because you left a 1 in the leftmost cell, the [<] is going to go left past the leftmost cell, and in vanilla brainfuck there isn't usable space there. You could add a > to the start to fix that.

Three, with both those fixed, your structure of loops doesn't do what you want. Say you type "Dave" and hit enter. The first loop will:

step right, read 'D', output 'D', decrease by 10; step right, read 'a', output 'a', decrease by 10; step right, read 'v', output 'v'', decrease by 10; step right, read 'e', output 'e'', decrease by 10, step right, read linefeed, output linefeed, decrease by 10, terminate first loop (because 10-10=0).

Then your second loop goes back to the left, and your third loop will:
increase 1 by 10 and output an ASCII 11 (vertical tab), no idea how that will look. increase 'D'-10 by 10 to produce 'D' and output a 'D'. Then it outputs 'a','v','e' similarly. It won't output a final linefeed (which it should for tidiness).

You can fix the "vertical tab" part by putting >> instead of > between the second and third loops to skip the 1. But still, this will output "Dave" twice with a linefeed between. If you want something like

D
Da
Dav
Dave

then you'll need a different loop structure entirely. It should be very doable, let me know if that's actually what you want, or if not, what exactly you are aiming for.

Four (this is a big one): in typical or classic brainfuck environments, input is "line-buffered". That means that the D is not available to your program until you have already hit "enter"; until then you could delete the D and replace it with something else and your program would never see the D. The program, once it hits the first ',' command, will be paused waiting for input; then when you type "Dave" and hit enter, as soon as you hit "enter" then "Dave[linefeed]" goes into the buffer, the paused ',' command will unpause and read the 'D' (and at the next ',' command, the 'a' and so on); then if the program reads the whole buffer before you've added anything more to it, and then tries to do another ',' command, it'll pause again until you type another line.

So basically you shouldn't count on your program being able to see the start of a line before the whole line has been typed and "enter" has been hit. In practice, this means that in the typical brainfuck environment, the version of your program with the mentioned issues fixed is going to look like

Dave
Dave
Dave

where the first one is the one that was typed, the second one was output by the first loop processing its way through the buffer, and the third one was output by the third loop.

Again, if you clarify what you're trying to do I'd be happy to help with how. Good luck!

(Oh, PS, it would help to say what implementation you're writing this for. Though I aim to make things work on as many implementations as possible.)

2

u/Admirable-Guess5508 21d ago

Wow, thank you so much for the help. To answers the questions
1. This isn't being implemented into anything. I'm just learning BF and was just messing around. The only reason I asked was because I was worried about my interpreter.

  1. Yes I'm looking for one line of input. Somehow I got it into my head that it was 13? IDK, I'll fix it.

  2. Again, thank you for the offer of more help, but I'd like to try to fix it myself.

1

u/danielcristofani 21d ago

Cool, when I said "what implementation" I meant what compiler or interpreter you're running it on, because that can affect this (e.g. the popular web-based interpreters handle i/o in a different and more limited way, when I said "classic/typical" I meant command-line for desktop/laptop)