This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
GravityScore's profile picture

Brainf**k Interpreter

Started by GravityScore, 30 April 2013 - 08:54 PM
GravityScore #1
Posted 30 April 2013 - 10:54 PM
Hey all!

Got bored again this afternoon, so I made a Brainf**k interpreter for you to enjoy!

For those who don't know, Brainf**k is a really simple language with only 8 different symbols, yet can be used to do a whole range of things. The idea of the language is that you have a chunk of memory to yourself, and are standing at a location in that memory. You can increment and decrement the number at that location in memory by 1 using + and -, and also move to the next or previous location in memory using < and >. You can print out the char produced from the number at the spot using . (so basically, string.char the number at that location), and read a single letter input from the using using , which is returned as a number from string.byte. You also have a really simple while loop using [ and ], which will loop over all the code in between it until the current spot in memory at the end of it is 0.

For those of you who know C, C++, or some similar variant, here is a simple table detailing the operators (from Wikipedia):

(Program Start)   char array[30000];
				  char *ptr = array;

>				 ++ptr;
<				 --ptr;
+				 ++*ptr;
-				 --*ptr;
.				 putchar(*ptr);
,				 *ptr = getchar();
				  while (*ptr) {
]				 }
I hope I got most of it right. Please point out any errors you find :)/>

Usage
Just download the interpreter from below, and run it with your Brainf**k program as the first argument, like:

brainf**k test_program

Download
Pastebin: 0AXaibqV
Or type into your computer:

pastebin get 0AXaibqV brainf

Please note: I asterisked out the u and k because this is a family friendly forum, but the actual language name has no asterisks.
Dlcruz129 #2
Posted 30 April 2013 - 10:57 PM
Neat? :P/> Very nice.
kornichen #3
Posted 01 May 2013 - 04:43 AM
Nice. Inspired me to make an own small programing languge :D/>


BTW: ++++++++++++++[>+++++<-]>.+++++++++++.——-.+++++++++++++++++++. is my Name :D/>
Mads #4
Posted 01 May 2013 - 05:11 AM
I made one too :P/>
https://gist.github.com/Mads-Clausen/5434778
GravityScore #5
Posted 01 May 2013 - 05:23 AM

I was just about to make one in C :P/> I would've used a file seek instead of mapping out the loops before the main interpretation, but the file:seek() function in CC is broken :(/>
Mads #6
Posted 01 May 2013 - 05:30 AM
By the way, what's the "/" doing in there? 0_o
GravityScore #7
Posted 01 May 2013 - 06:11 AM
By the way, what's the "/" doing in there? 0_o

I was using it in bug testing, but decided to leave it there. :P/> It prints out the current block of memory as a number, and doesn't apply string.char to the number.
spyman68 #8
Posted 01 May 2013 - 06:32 AM
So could you explain how to even use this language?
kornichen #9
Posted 01 May 2013 - 06:58 AM
Look at Wikipedia for Brainf**k

http://en.wikipedia.org/wiki/Brainfuck
http://de.wikipedia.org/wiki/Brainfuck
Mads #10
Posted 01 May 2013 - 10:29 AM
The table he made explains it very well. It does what it says. E.g., a program which takes input, and adds them together, and displays the output would look like this:


,    ; read one byte
>   ; go to next cell
,    ; read another byte
[    ; start loop
<+ ; go back to cell 0 and add 1
>-  ; go forth to next cell and decrement
]    ; jump to the start of the loop, unless the current cell is 0, which means that we're dona adding.
<.  ; print the contents of cell 0

So if I enter 1 and 2, it should print 'c' IIRC.