## Simple brainfuck interpreter

Show off your games, demos and other (playable) creations.
Ducktor Cid
Prole
Posts: 18
Joined: Wed Oct 05, 2016 8:08 pm

### Simple brainfuck interpreter

This started out as a small project in ROBLOX Studio, but since it was just core lua, I could easily port the code over and add stuff like console text input.

It's not perfect by a long shot. It seems that using a translator doesn't work, but this will (wasn't done by a translator):
+++++ +++++ [ > +++++ ++ > +++++ +++++ <<- ] >++. >+++++.

Line breaks also have to be removed since the command line seems to see them as a new input and runs each line as it's own input.

(also if you have no idea what brainfuck is, it's an esoteric language made for the purpose of having the smallest possible compiler. More info here: https://en.wikipedia.org/wiki/Brainfuck)
Attachments
Brainfuck.love
Last edited by Ducktor Cid on Sat Oct 08, 2016 5:36 pm, edited 2 times in total.

pgimeno
Party member
Posts: 1342
Joined: Sun Oct 18, 2015 2:58 pm

### Re: Simple brainfuck interpreter

Hi, welcome to the forum. I don't quite get how it works. I don't see any print command (dot) in the program, yet the output is:

Code: Select all

\$ love10 Brainfuck.love
+++++ +++++ [ > +++++ ++ > +++++ +++++ <<- ] >++ >+++++
55
0
2
Hi

The program doesn't generate 55 nor 2, so I wonder why does it print that.
Thrust II Reloaded - GifLoad for Löve - GSpöt GUI - My NotABug.org repositories - portland (mobile orientation)
The MS-Github repositories I had have been closed after the acquisition announcement and will be removed in the near future.

Sulunia
Party member
Posts: 198
Joined: Tue Mar 22, 2016 1:10 pm
Location: SRS, Brazil

### Re: Simple brainfuck interpreter

Would be useful if you posted this along:
https://en.wikipedia.org/wiki/Brainfuck

Eh, 'tis simple but does the job. Congratulations!
Don't check my github! It contains thousands of lines of spaghetti code in many different languages cool software!
https://github.com/Sulunia

Ducktor Cid
Prole
Posts: 18
Joined: Wed Oct 05, 2016 8:08 pm

### Re: Simple brainfuck interpreter

I didn't include the . command since I just wanted to translate brainfuck code. I can easily add it if needed

Positive07
Party member
Posts: 999
Joined: Sun Aug 12, 2012 4:34 pm
Location: Argentina

### Re: Simple brainfuck interpreter

What it basically does is print the complete memory stack, by checking that they are "valid" printable characters. Though that may be wrong because it just checks that the number is above 32 and not if the number is under a value, so if you try to print numbers above 255 you'll probably have errors.

One thing I consider you do wrong is filling the table at startup, that is not something you need, a 0 even though is 0 uses 64bits of memory so loading thousands of those inside a table for nothing is really a waste. As an example you could do:

Code: Select all

cells[currentCell] = (cells[currentCell] or 0) + 1

To icrement a cell, this allows the cell to be nil which doesn't consume memory like a 0 does.

Anyway, the program looks really nice, and can be easily extended which is cool, I would implement the dot though since that is what you expect brainfuck to do, I wrote programs to use two or three cells to write whole sentences which I can display with this interpreter.

Also you should delete all print statements that are simply for debugging, since those are confusing to the user running your interpreter
for i, person in ipairs(everybody) do
[tab]if not person.obey then person:setObey(true) end
end
love.system.openURL(Github.com/Positive07)

Ducktor Cid
Prole
Posts: 18
Joined: Wed Oct 05, 2016 8:08 pm

### Re: Simple brainfuck interpreter

Added all Positive's suggestions and updated the OP.

Positive07
Party member
Posts: 999
Joined: Sun Aug 12, 2012 4:34 pm
Location: Argentina

### Re: Simple brainfuck interpreter

That is great! I'm glad I could help, I downloaded the new file and had a look again and it was much better, so I decided to have some fun with it, first I must note that all the changes I made are TOTALLY UNNECESSARY! but if you find something you deem useful you can feel free to use it in your program.

So what did I do:
• Made the output a string instead of directly printing to the console
• I sandboxed the program, now it environment only contains string.char that is the only lua function the program needs.
• Made the program return the output string at the end so that you can print it or something (you could access it from the environment too but it is easier this way)
• Made a run function that runs the program with a hook that errors when the program is in an infinite loop, this is pretty useful for Brainfuck programs that have many loops and some of them may never return. The quota (number of instructions) can be passed as an argument to run if 10000 is too little.
• The returned function pcalls the run function so that the error doesn't propagate to the main program
• When a char is not recognized in the compilation step, instead of ignoring it I add a new line, this way errors, which commonly point to the line where the Lua error is, can be used to know what character in the Brainfuck program has the error, since Lua line == Brainfuck character
• In your program if you try to print a cell with a value of 1000 it completely crashes I fixed this so cells can only contain numbers up to 255 and if that value is exceeded they wrap to 0, this could be changed with some more work in the dot (making sure that the cell is a printable character or something like that).
• I removed some unused variables like BfString and utf8, and took the compile function outside of interpret so that it isn't generated every time interpret is called
The next step would be to implement input command (,) but that may be harder, anyway have fun!
Attachments
Brainfuck.love
for i, person in ipairs(everybody) do
[tab]if not person.obey then person:setObey(true) end
end
love.system.openURL(Github.com/Positive07)

drunken_munki
Party member
Posts: 134
Joined: Tue Mar 29, 2011 11:05 pm

### Re: Simple brainfuck interpreter

You are all crazy.

pgimeno
Party member
Posts: 1342
Joined: Sun Oct 18, 2015 2:58 pm

### Re: Simple brainfuck interpreter

Looks like it no longer violates the ENSI Brainfuck standard. Congratulations!
Thrust II Reloaded - GifLoad for Löve - GSpöt GUI - My NotABug.org repositories - portland (mobile orientation)
The MS-Github repositories I had have been closed after the acquisition announcement and will be removed in the near future.

### Who is online

Users browsing this forum: No registered users and 4 guests