How does the Brainfuck Hello World actually work?

后端 未结 6 1525
长情又很酷
长情又很酷 2021-01-29 17:38

Someone sent this to me and claimed it is a hello world in Brainfuck (and I hope so...)

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


        
6条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-29 18:39

    All the answers are thorough, but they lack one tiny detail: Printing. In building your brainfuck translator, you also consider the character ., this is actually what a printing statement looks like in brainfuck. So what your brainfuck translator should do is, whenever it encounters a . character it prints the currently pointed byte.

    Example:

    suppose you have --> char *ptr = [0] [0] [0] [97] [0]... if this is a brainfuck statement: >>>. your pointer should be moved 3 spaces to right landing at: [97], so now *ptr = 97, after doing that your translator encounters a ., it should then call

    write(1, ptr, 1)
    

    or any equivalent printing statement to print the currently pointed byte, which has the value 97 and the letter a will then be printed on the std_output.

提交回复
热议问题