Jump to content




Brainf**k Interpreter

computer lua utility

9 replies to this topic

#1 GravityScore

  • Members
  • 796 posts
  • LocationLand of Meh

Posted 30 April 2013 - 09: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.

#2 Dlcruz129

    What's a Lua?

  • Members
  • 1,423 posts

Posted 30 April 2013 - 09:57 PM

Neat? :P Very nice.

#3 kornichen

  • Members
  • 220 posts
  • LocationGermany

Posted 01 May 2013 - 03:43 AM

Nice. Inspired me to make an own small programing languge :D


BTW: ++++++++++++++[>+++++<-]>.+++++++++++.-------.+++++++++++++++++++. is my Name :D

#4 Mads

  • Members
  • 604 posts
  • LocationCopenhagen, Denmark

Posted 01 May 2013 - 04:11 AM

I made one too :P
https://gist.github....Clausen/5434778

#5 GravityScore

  • Members
  • 796 posts
  • LocationLand of Meh

Posted 01 May 2013 - 04:23 AM

View PostMads, on 01 May 2013 - 04:11 AM, said:


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 :(

#6 Mads

  • Members
  • 604 posts
  • LocationCopenhagen, Denmark

Posted 01 May 2013 - 04:30 AM

By the way, what's the "/" doing in there? 0_o

#7 GravityScore

  • Members
  • 796 posts
  • LocationLand of Meh

Posted 01 May 2013 - 05:11 AM

View PostMads, on 01 May 2013 - 04:30 AM, said:

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.

#8 spyman68

  • Members
  • 155 posts

Posted 01 May 2013 - 05:32 AM

So could you explain how to even use this language?

#9 kornichen

  • Members
  • 220 posts
  • LocationGermany

Posted 01 May 2013 - 05:58 AM

Look at Wikipedia for Brainf**k

http://en.wikipedia.org/wiki/Brainfuck
http://de.wikipedia.org/wiki/Brainfuck

#10 Mads

  • Members
  • 604 posts
  • LocationCopenhagen, Denmark

Posted 01 May 2013 - 09: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.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users