有人给我发了这封信,声称这是Brainfuck的hello world (我希望如此…)
++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.
我知道它的基本原理是通过移动指针、递增和递减……
然而,我仍然想知道,它实际上是如何工作的?首先,它是如何在屏幕上打印任何内容的?它如何对文本进行编码?我一点也不明白...
发布于 2017-03-20 14:34:39
为了回答它如何知道打印内容的问题,我在发生打印的代码右侧添加了ASCII值的计算:
> just means move to the next cell
< just means move to the previous cell
+ and - are used for increment and decrement respectively. The value of the cell is updated when the increment/decrement happens
+++++ +++++ initialize counter (cell #0) to 10
[ use loop to set the next four cells to 70/100/30/10
> +++++ ++ add 7 to cell #1
> +++++ +++++ add 10 to cell #2
> +++ add 3 to cell #3
> + add 1 to cell #4
<<<< - decrement counter (cell #0)
]
> ++ . print 'H' (ascii: 70+2 = 72) //70 is value in current cell. The two +s increment the value of the current cell by 2
> + . print 'e' (ascii: 100+1 = 101)
+++++ ++ . print 'l' (ascii: 101+7 = 108)
. print 'l' dot prints same thing again
+++ . print 'o' (ascii: 108+3 = 111)
> ++ . print ' ' (ascii: 30+2 = 32)
<< +++++ +++++ +++++ . print 'W' (ascii: 72+15 = 87)
> . print 'o' (ascii: 111)
+++ . print 'r' (ascii: 111+3 = 114)
----- - . print 'l' (ascii: 114-6 = 108)
----- --- . print 'd' (ascii: 108-8 = 100)
> + . print '!' (ascii: 32+1 = 33)
> . print '\n'(ascii: 10)
发布于 2018-07-29 18:31:54
我想你在问的是,Brainfuck是如何知道如何处理所有代码的。有一个用Python等高级语言编写的解析器,用于解释点在代码中的含义,或者加号在代码中的含义。
所以解析器将逐行读取你的代码,并说ok有一个>符号,所以我必须前进内存位置,代码很简单,如果(内容在那个内存位置) == >,memlocation =+ memlocation是用更高级的语言编写的,类似的if (内容在内存位置) == ".",然后打印(内存位置的内容)。
希望这能澄清这一点。tc
https://stackoverflow.com/questions/16836860
复制相似问题