Brainfuck
Dari Wikipedia Bahasa Melayu, ensiklopedia bebas.
Bahasa pengaturcaraan brainfuck adalah bahasa pengaturcaraan esotik terkenal kerana ringkasannya (minima). Ia direka sebagai cabaran dan menghibur pengaturcara, dan tidak sesuai bagi kegunaan pratikal. Namanya telah dipelbagai bowdlerized, sebagai brainf*ck atau brainfsck, kerana namanya mengandungi perkataan lucah "fuck". Naba bahasa ini biasanya tidak mengguhakan huruf besar, sungguhpun ia merupakan (proper noun).
Jadual isi kandungan |
[Sunting] Perintah
Lapan perintah bahasa, setiap satu terdiri dari satu huruf, adalah seperti berikut:
Huruf | Erti |
---|---|
> |
tingkat penunjuk (untuk menunjuk kepada sel berikut kekanan). |
< |
mengurangkan penunjuk (untuk menunjuk sel berikut sebelah kiri). |
+ |
tingkat penunjuk (tambah satu) byte pada penunjuk. |
- |
mengurang (kurang satu) byte pada pointer. |
. |
output nilai byte pada penunjuk. |
, |
terima satu byte input, simpan nilainya pada byte di penunjuk. |
[ |
Cabang kehadapan kepada perintah selepas ] yang sama sekiranya byte pada penunjuk adalah sifar. |
] |
Lompat kembali kepada perintah selepas [ yang sama jika byte pada penunjuk adalah bukan sifar. |
(Juga, perintah ]
boleh diterjemah sebagai lompat tanpa syaratkepada perintah [
yang sama, atau sebaliknya; perisian akan bertindak sama tetapi lebih perlahan.)
-],[<+>-]<.</nowiki>
This program adds two single-digit numbers and displays the result correctly if it too has only one digit:
43
7
The first number is input in [0], and 48 is subtracted from it to correct it (the ASCII codes for the digits 0-9 are 48-57). This is done by putting a 6 in [1] and using a loop to subtract 8 from [0] that many times. (This is a common method of adding or subtracting large numbers.) Next, the second number is input in [1].
The next loop [<+>-]
does the real work, moving the second number onto the first, adding them together and zeroing [1]. Each time through, it adds one to [0] and subtracts one from [1]; so by the time [1] is zeroed, as many have been added to [0] as have been removed from [1]. Now a return is input in [1]. (We're not error-checking the input at all.)
Then the pointer is moved back to the [0], which is then output. ([0] is now a + (b + 48), since we didn't correct b; which is identical to (a + b) + 48, which is what we want.) Now the pointer is moved to [1], which holds the return that was input; it is now output, and we're done.
Apparently, some implementation prefers this variant which does not use linefeeds at all:
,>------[<-------->+],[<+>-]<.
[Sunting] Multiplication
,>,>++++++++[<------<------>>-] <<[>[>+>+<<-]>>[<<+>>-]<<<-] >>>++++++[<++++++++>-],<.>.
Like the previous, but does multiplication, not addition.
The first number is input in [0], the second number is input in [1], and both numbers are corrected by having 48 subtracted.
Now we enter the main multiplication loop. The basic idea is that each time through it we subtract one from [0] and add [1] to the running total kept in [2]. In particular: the first inner loop moves [1] onto both [2] and [3], while zeroing [1]. (This is the basic way to duplicate a number.) The next inner loop moves [3] back into [1], zeroing [3]. Then one is subtracted from [0], and the outer loop is ended. On exiting this loop, [0] is zero, [1] still has the second number in it, and [2] has the product of the two numbers. (Had we cared about keeping the first number, we could have added one to [4] each time through the outer loop, then moved the value from [4] back to [0] afterward.)
Now we add 48 to the product, input a return in [3], output the ASCIIfied product, and then output the return we just stored.
[Sunting] Division
This example accepts two numbers from the user, divides them, and displays the truncated quotient.
The following code prompts the user for two numbers; the dividend is stored in [0] and the divisor stored in [1] The code then enters the main loop. With each iteration of the loop, the code subtracts the divisor from the dividend. If the difference is greater than zero, the cell holding the quotient is incremented, and the process repeated until the dividend reaches zero.
,>,>++++++[-<--------<-------->>] Store 2 numbers from kb in (0) and (1); and subtract 48 from each <<[ Loop until the dividend is zero >[->+>+<<] Move the divisor in (1) to (2) and (3); setting (1) to zero >[-<<- Subtract 1 from both the dividend(0) and the divisor(2) until (2) is zero [>]>>>[<[>>>-<<<[-]]>>]<<] If the dividend is zero; exit the loop >>>+ Add one to the quotient in (5) <<[-<<+>>] Move the saved divisor in (3) to (2) <<<] Move ptr to {0} and repeat loop >[-]>>>>[-<<<<<+>>>>>] Move the quotient in (5) to (0) <<<<++++++[-<++++++++>]<. Add 48 and print result
>
[Sunting] Pautan luar
- Brainfuck on the Esolang (Esoteric Languages) wiki
- {{{2}}} di Projek Direktori Terbuka