Thread: So I wrote brain.......... interpreter

  1. #1
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709

    So I wrote brain.......... interpreter

    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:

    Code:
    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?

    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.
    Last edited by cboard_member; 04-05-2006 at 04:18 AM.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  2. #2
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    What, no source code?

    Well, have a Bool........ interpreter. http://www.rpi.edu/~hughes/boof/
    There are 10 types of people in this world, those who cringed when reading the beginning of this sentence and those who salivated to how superior they are for understanding something as simple as binary.

  3. #3
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Quote Originally Posted by Rashakil Fol
    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.


    Code:
    #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).
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  4. #4
    ~viaxd() viaxd's Avatar
    Join Date
    Aug 2003
    Posts
    246
    there is no need to use 'E'

    Code:
        
    ...
    while ((*ch = fgetc(f)) != EOF)
      *ptr++ = *ch;
    while(--ptr - commandBuf >= 0)
    {
    ...
    }
    i think that should work
    :wq

  5. #5
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Damn you. I'll try it and see.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  6. #6
    the Great ElastoManiac's Avatar
    Join Date
    Nov 2005
    Location
    Republika Srpska - Balkan
    Posts
    377
    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.
    lu lu lu I've got some apples lu lu lu You've got some too lu lu lu Let's make some applesauce Take off our clothes and lu lu lu

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Interpreter Features Question
    By audinue in forum C Programming
    Replies: 0
    Last Post: 10-19-2008, 07:29 AM
  2. Human brain and programing
    By avgprogamerjoe in forum A Brief History of Cprogramming.com
    Replies: 26
    Last Post: 08-27-2007, 04:48 PM
  3. Mouse to have human brain
    By nickname_changed in forum A Brief History of Cprogramming.com
    Replies: 22
    Last Post: 03-10-2005, 05:39 PM
  4. brain vs. computer (brain wins hands down)
    By hk_mp5kpdw in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 09-17-2003, 08:41 PM
  5. Re: Girlfriend Post
    By PsychoBrat in forum A Brief History of Cprogramming.com
    Replies: 57
    Last Post: 05-13-2002, 06:11 AM