I love a bit of brainfuck. I've been working on this interpreter for about a total of an hour, and it's already finished. It's fully functional, and interprets any brainfuck application.
The best part is, it's only 61 lines long! Interpreting the brainfuck code itself takes only 12 lines~ But I've added some more functionality to my application so that it can be called from the command line, and interpret at runtime. No translated code, no output files~ It simply executes the BF code.
I've added two commands to it:
BF-i -? will display a little bit about the program~ Who it was written by, which language it was written in, etc etc~
BF-i -i C:\file.bf will interpret the file located at C:\file.bf
Simple to use!
It's available to download here, including the MS executable, C++ source code, and a small about file.
I'll write up a tutorial on creating an interpreter soon.
Lastly, here is the code:
The best part is, it's only 61 lines long! Interpreting the brainfuck code itself takes only 12 lines~ But I've added some more functionality to my application so that it can be called from the command line, and interpret at runtime. No translated code, no output files~ It simply executes the BF code.
I've added two commands to it:
BF-i -? will display a little bit about the program~ Who it was written by, which language it was written in, etc etc~
BF-i -i C:\file.bf will interpret the file located at C:\file.bf
Simple to use!
It's available to download here, including the MS executable, C++ source code, and a small about file.
I'll write up a tutorial on creating an interpreter soon.
Lastly, here is the code:
/*
* A brainfuck interpreter
* Author: Danny Battison
* Contact: gabehabe@gmail.com
*/
#include <iostream>
#include <fstream>
using namespace std;
void DisplayHelp();
int main(int argc, char *argv[]) {
if (argc < 2 || argc > 3) {
DisplayHelp();
return EXIT_FAILURE;
}
if (!strcmp(argv[1], "-?")) {
cout << "BRAINFUCK INTERPRETER: WRITTEN BY DANNY BATTISON" << endl
<< "This program is designed to interpret a brainfuck application." << endl
<< "Written in C++" << endl
<< "Contact the author: gabehabe (at) gmail (dot) com";
}
else if (!strcmp(argv[1],"-i")) {
if (argc < 2 || argc > 3) {
DisplayHelp();
return EXIT_FAILURE;
} else {
ifstream file;
file.open(argv[2]);
if (!file.is_open()) {
cout << "ERROR: File could not be found.";
return EXIT_FAILURE;
}
string bf = "";
char buffer;
file.get(buffer);
bf += buffer;
while (!file.eof()) {file.get(buffer); bf += buffer;}
file.close();
char* ptr = new char[5000];
int sub;
for (sub = 0; sub < 5000; sub++) ptr[sub] = 0;
sub = 0;
unsigned int i;
int loopPosition = -1;
for (i = 0; i < bf.length(); i++) {
switch (bf[i]) {
case '+': ptr[sub]++; break;
case '-': ptr[sub]--; break;
case '>': sub++; break;
case '<': sub--; break;
case '.': cout << ptr[sub]; break;
case ',': ptr[sub] = getchar(); if (ptr[sub] == 13) {ptr[sub] = '\0'; loopPosition = -1;} break;
case '[': loopPosition = i; break;
case ']': if (loopPosition != -1) {if (ptr[sub] != '\0') i = loopPosition-1; else loopPosition = -1;} break;
}
}
}
return EXIT_SUCCESS;
}
}
void DisplayHelp() {
cout << "Invalid arguments. Example Usages:" << endl
<< "\tInterpret:\tbf.exe -i C:\\file.bf" << endl
<< "\tabout:\t\tbf.exe -?";
}
2 Comments On This Entry
Page 1 of 1
WolfCoder
03 November 2008 - 08:03 PM
Great! You've done an interpreter, now do a compiler that makes it an .exe on it's own!
Page 1 of 1
Request A Topic!
Want me to blog about something? Perhaps a language? A piece of software? A specific topic? Let me know! Even guests can post here on my blog!
If you would like to request a topic, please post a comment here and I'll get on it right away!
If you would like to request a topic, please post a comment here and I'll get on it right away!
Search My Blog
0 user(s) viewing
0 Guests
0 member(s)
0 anonymous member(s)
0 member(s)
0 anonymous member(s)
gabehabe's off-topic ramblings
Follow me on Twitter!
lol, my other blog died a horrible lonely death. Ah well.
lol, my other blog died a horrible lonely death. Ah well.
Smiley of the [however often I change it]
Contact Me
e-mail: gabehabe@gmail.com
Google Talk: gabehabe@gmail.com
MSN: gabehabe@hotmail.com
Yahoo: gabehabe (rarely used)
AIM: gabehabe (rarely used)
Skype: gabehabe
Want me to work for you? [click]
Google Talk: gabehabe@gmail.com
MSN: gabehabe@hotmail.com
Yahoo: gabehabe (rarely used)
AIM: gabehabe (rarely used)
Skype: gabehabe
Want me to work for you? [click]
My Blog Links
|
|



2 Comments








|