PDA

View Full Version : So I wrote brain.......... interpreter



cboard_member
04-05-2006, 04:11 AM
Had the morning off so I decided to spend ~1.5 hours of it writing a little brain.......... interpreter. It works so far (the hello world program on the wiki page / website runs, that's the biggest program I've tested it on).

I've added a little feature, where the first line should be a number (signed integer) signifying how much memory should be allocated for you to play with. Also, because I wanted (OK, needed) a corny way to terminate the processing loop, you have to end your program with 'E' :(

So hello world now looks like this:



120
++++++++++
[
>+++++++>++++++++++>+++>+<<<<-
]
>++.
>+.
+++++++.
.
+++.
>++.
<<+++++++++++++++.
>.
+++.
------.
--------.
>+.
>.
E


120 is way too much, I just left it over from a couple of debugging test runs.

Yeah this thread has nothing to do with anything except I've got nothing to do.

Hmm, maybe would could make this a contest? :eek:

EDIT: Oh yeah, the program: http://www.ahluka.co.uk/bf.rar

EDIT2: Oh yeah, usage. Just write you program in a text file and "b.......... <filename>" on the command line. Obviously change the * to a u.

EDIT3: Stoopid dots. It's b-F WORD.

EDIT4: Actually that one is broken. Taking link down to fix :)

EDIT5: Got there eventually.

Rashakil Fol
04-05-2006, 11:59 AM
What, no source code?

Well, have a Bool........ interpreter. http://www.rpi.edu/~hughes/boof/

cboard_member
04-05-2006, 12:38 PM
What, no source code?

Well, have a Bool........ interpreter. http://www.rpi.edu/~hughes/boof/

Don't be silly mine is so poorly written it's embarressing. :(

I found an implementation written in 3 lines of C (well, several statements / expressions on each line). Mine is 112, including header comment I haven't included here.




#include "stdafx.h"

using namespace std;

int main(int argc, char *argv[])
{
FILE *f = 0;
char *programArray = 0;
char *ptr = 0;
char *ch = new char;
char line[60] = {0};
char *lnPtr = line;
char *commandBuf = new char[2048];
int arraySz = 0;
char *lback;
char *lford;


if (argc != 2)
{
cout << "Usage: b........ <filename>" << endl;
return -1;
}

f = fopen(argv[1], "r");
if (! f)
{
cout << "Error opening file " << argv[1] << endl;
return -1;
}

// attempt to get arraySz from first line
fgets(line, 60, f);
if (sscanf(line, "%d", &arraySz) != 1)
arraySz = 100; // default size
cout << "array size: " << arraySz << endl;
memset(line, 0, 60);

try
{
programArray = new char[arraySz];
}
catch (bad_alloc& e)
{
cout << "Error: " << e.what() << endl;
return -1;
}

for (int i = 0; i < arraySz; i++)
*(programArray + i) = 0;

// read whole program into commandBuf
ptr = commandBuf;
while ((*ch = fgetc(f)) != EOF)
*ptr++ = *ch;
ptr = &programArray[0];
ch = &commandBuf[0];

while (*ch != 'E')
{
switch (*ch)
{
case '>':
++ptr;
break;
case '<':
--ptr;
break;
case '+':
++*ptr;
break;
case '-':
--*ptr;
break;
case '.':
cout << *ptr;
break;
case ',':
cin >> *ptr;
break;
case '[':
lback = ch;
if (*ptr == 0)
{
while (*ch != ']')
++ch;
lford = ch;
ch = lback;
}
break;
case ']':
if (*ptr != 0)
{
ch = lback;
}
break;
}

ch++;
}

delete[] programArray;
delete[] commandBuf;
return 0;
}


Yeah it's fugly but it works. I've have no intention of updating it or anything since it was just written to waste an hour or so.

Slag it off if you feel the need, I'll just nod thoughtfully and go "I see". :)

(I probably will edit it, but you don't know that).

viaxd
04-05-2006, 12:51 PM
there is no need to use 'E'



...
while ((*ch = fgetc(f)) != EOF)
*ptr++ = *ch;
while(--ptr - commandBuf >= 0)
{
...
}

i think that should work

cboard_member
04-05-2006, 12:54 PM
Damn you. I'll try it and see.

ElastoManiac
04-07-2006, 09:06 AM
What the hell is this brain.......... interpreter. And what does it do.
It seems to me that it converts source files into some other files with '+' and something.
I'm a little dumb so i don't get it, Can you explain it to me.

laserlight
04-07-2006, 09:24 AM
What the hell is this brain.......... interpreter. And what does it do.
It seems to me that it converts source files into some other files with '+' and something.
For reasons of sanity, I dont program in this language, but it could be an interpreter for this programming language (http://www.muppetlabs.com/~breadbox/bf/).