
-----------------------------------
Insectoid
Sat Jul 09, 2011 4:16 am

Brainfuck Interpreter
-----------------------------------
Version 0.1 of my brainfuck interpreter, written in C. It is partially functional at the moment, but it will run the Hello World, addition, and multiplication programs as shown on the brainfuck wiki. 

Bugs:
I/O: Currently reads from stdin. Need to rewrite it to read individual keystrokes. I'll probably end up using SDL, because I know how to use it, even though it's serious overkill (hunting rabbits with a rocket launcher-scale overkill). If someone has a better multi-platform solution, lemme know.

Loops: If the value at the data pointer is zero when entering a loop (for example, if your program is nothing more than /*
* C Brainfuck interpreter
*Author: Jordan Dykstra
*      ~FroopWare~
*/

//#define VERBOSE //if VERBOSE is defined, output will describe every command in the brainfuck source while executing.


#include 

int processChar(char);
int loop();

char data

-----------------------------------
Insectoid
Sat Jul 09, 2011 7:24 am

RE:Brainfuck Interpreter
-----------------------------------
So, instead of going to bed and sleeping 'till 2, I kept slogging away at this. I managed to kinda fix the loop issue (it still won't complete the division program- gets about 75% of the way there). Now, it seems to 'forget' that it's in a loop. It gets 3 loops deep into the division program (I think), exits 2 of them properly, and then at the bottom of the 3rd it thinks that it isn't in a loop (same error as before). 

I'm trying to fix it, but I keep distracting myself with throwing in more #ifdefs (verbose mode is so much fun!). However, being so tired (from coding all night), it's probably doing more harm than good. Anyway, if anyone thinks they can help out, that would be awesome.

Code for V0.2
/*
* C Brainfuck interpreter
*Author: Jordan Dykstra
*      ~FroopWare~
*/

#define VERBOSE //if VERBOSE is defined, output will describe every command in the brainfuck source while executing.


#include 

int processChar(char);
int loop();
int skipLoop();


char data

If you try to interpret the division program (I'll paste a copy), you'll see that toward the end of the program, it bypasses a loop of depth 3 (depth being the number of 'nestings'). It then automatically bypasses loops 4 and 5 (because they are nested inside loop 3), exits the loop 5 bypass correctly, and then...loops 3 and 4 'complete' inside the loop function (which is odd, since it returns as soon as skipLoop() returns) and then I get that damn 'expected [ before ]' error, which I assume happens because the stream pointer isn't bumped forward enough because it's not running the rest of the skipLoop() function?

I dunno. It's completely baffling me. Anyway, here's the division program.
[code],>,>++++++[-] Store 2 numbers from keyboard in (0) and (1);
                                  and subtract 48 from each
+>+>[>>-+                              Add one to the quotient in (5)
[-]            Destructively copy the quotient in (5) to (0)
                                  (not necessary; but cleaner)
>>+

Save that as a file and pass it as an arg to my interpreter (henceforth "FroopWare Brainfuck Interpreter" or FWBI), then paste in any brainfuck program, followed by an exclamation point, and the input to that program (lol, I know). I warn you; nested interpreters are slow!

And a final word for this post; if anyone knows of a free, cross-platform console I/O library easier to use than ncurses (I really just need a getch() function) lemme know, 'cause it's gonna be a fair bit of work to make this pretty.

-----------------------------------
mirhagk
Tue Jul 12, 2011 6:51 pm

RE:Brainfuck Interpreter
-----------------------------------
Developing cross-platform in C seems to be insanely difficult lol, might have been easier to use Brainfuck.

Brainfuck is a very interesting language, and attempting to code in it is very fun. Brainfuck was probably the first interpreter I ever wrote, as it is pretty easy to do.

I wanna do a project where I build a brainfuck machine out of logic gates, or perhaps even cooler, a brainfuck interpreter in minecraft lol, would you be interested in this?

-----------------------------------
DemonWasp
Wed Jul 13, 2011 10:38 am

RE:Brainfuck Interpreter
-----------------------------------
Out of curiosity, why did you use #ifdefs for verbosity? Isn't that something most people would want to specify at run-time, not compile-time? Did you maybe mean "debug"?

Also, yes, cross-platform C is a royal pain in the ass.

-----------------------------------
Insectoid
Wed Jul 13, 2011 12:43 pm

RE:Brainfuck Interpreter
-----------------------------------
yeah, I meant debug. Much of this was written early in the morning following a sleepless night, so mistakes like this (and worse) are expected.

-----------------------------------
DemonWasp
Wed Jul 13, 2011 1:43 pm

RE:Brainfuck Interpreter
-----------------------------------
Ah well, quickly solved via :%s/VERBOSE/DEBUG/g

-----------------------------------
btiffin
Sun Jul 17, 2011 12:12 am

Re: Brainfuck Interpreter
-----------------------------------
Insectoid: "And a final word for this post; if anyone knows of a free, cross-platform console I/O library easier to use than ncurses (I really just need a getch() function) lemme know, 'cause it's gonna be a fair bit of work to make this pretty."

Check out S-Lang http://en.wikipedia.org/wiki/S-Lang_(programming_library)
I tried making a sample for OpenCOBOL and posted the experiments to http://www.opencobol.org/modules/newbb/viewtopic.php?topic_id=589&forum=1#forumpost2983
Works well, and won't virtually erase the screen as ncurses is fond off when you just need a keyboard keycode.

And if you'd like to see OpenCOBOL to Guile to Scheme to bf in action, see http://www.opencobol.org/modules/newbb/viewtopic.php?topic_id=1167&forum=1

Well done Insectoid.  The CompFu is strong in this one.   ;)

Insectoid; Try this one. +++++ +++++ [ > +++++ ++ > +++++ +++ > +  ++ .  -- .  > .  > , [  .  > - ]

Cheers
