Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jul 20th, 2006, 2:22 AM   #41
v0id
Hobbyist Programmer
 
Join Date: Apr 2006
Posts: 155
Rep Power: 3 v0id is on a distinguished road
Arevos did show his version of a Brainf*ck interpreter in Python.
Here's mine in C.
// brainfuck.c
#include <stdio.h>
#define _CELLS 30000 // The original Brainf*ck have 30000 cells

int main(int argc, char **argv) 
{
    int args, length, l;
    int x[_CELLS], pc;
    int p[_CELLS], xc;

    FILE *File;

    for(args = 1; args < argc; args++)
    {
        File   = fopen(argv[args], "r");
        length = l = 0;
        
        for(pc = 0; pc < _CELLS && (p[pc] = getc(File)) != EOF; pc++)
            length++;
        
        fclose(File);    
        pc = 0;
        
        for(xc = 0; xc < _CELLS; xc++)
           x[xc] = 0;

        xc = 0;

        for(pc = 0; pc < length; pc++)
        {
            switch(p[pc])
            {
                case 43:                             // '+'
                    x[xc]++;
                break;
                case 45:                             // '-'
                    x[xc]--;
                break;
                case 62:                             // '>'
                    xc++;
                break;
                case 60:                             // '<'
                    xc--;
                break;
                case 46:                             // '.'
                    putchar(x[xc]);
                break;
                case 44:                             // ','
                    x[xc] = getchar();
                break;
                case 91:                             // '['
                {
                    if(x[xc] == 0) 
                    {
                        pc++;
                        while(l > 0 || p[pc] != 93) 
                        {
                            if(p[pc] == 91) 
                                l++;
                                if(p[pc] == 93) 
                                    l--;
                            pc++;
                        }
                    }
                }
                break;
                case 93:                             // ']'
                {
                    pc--;
                    while(l > 0 || p[pc] != 91) 
                    {
                        if(p[pc] == 93) 
                            l++;
                            if(p[pc] == 91) 
                                l--;
                        pc--;
                    }
                    pc--;
                }
                break; 
            }    

        }  
    }

    putchar(10); // A newline in the end
    return 0;
}
code.txt
++++++++++[>+++++++>+<<-]>++.
>[<+++>>+++<-]<-.<++[>+++>++++<<-]
>+..+++.>>++.[<<->>-]<[<+>-]
<.<++++++[>++++<-]>.+++.<++[>---<-]
>.<++[>----<-]>.
The test
C:\Brainfuck>brainfuck code.txt
Hello World

C:\Brainfuck>
__________________
-- v0id
v0id is offline   Reply With Quote
Old Jul 20th, 2006, 5:23 AM   #42
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
Quote:
Originally Posted by InfoGeek
I think compile humanly here means to understand what a program is doing just by looking at it.
No, I think it's more literal than that. Making a Befunge interpreter is fairly simple; it's relatively easy to see how one would do it. But compiling Befunge into machine code is a different matter entirely.

The main problem is that Befunge is self-modifying. Thus, if you were to translate Befunge into machine code, you'd need an index table to connect each set of machine code instructions with a two dimensional vector that points to the original Befunge cell. The next difficulty is that you'd need to be able to translate Befunge to machine code, and vice versa, in the compiled executable. There's also the problem of turning two dimensional Funge-space into one dimensional assembly - many jumps would be needed.

Indeed, once you're finished packing in all the things you need, you've got a compiler that is barely more than a glorified interpreter. I think this is what the Wikipedia article meant when it said Befunge was difficult to compile.

Quote:
Originally Posted by v0id
Arevos did show his version of a Brainf*ck interpreter in Python.
Here's mine in C.
Nice work!
Arevos is offline   Reply With Quote
Old Jul 20th, 2006, 1:45 PM   #43
mikaoj
Programmer
 
mikaoj's Avatar
 
Join Date: Aug 2005
Location: Norway
Posts: 56
Rep Power: 0 mikaoj is an unknown quantity at this point
Yeah, a interpreter for it would be fairly simple, if you know how Befunge works.
The first phases of interpreter/compiler construction may be easy. Constructing
a scanner can be done easily using scanner generator, or write it yourself, that
not that hard to. And you can write a parser that evaluate the tokens and run
the instruction if it is legal.

btw, v0id: Nice
__________________
Heh.
mikaoj is offline   Reply With Quote
Old Jul 20th, 2006, 2:42 PM   #44
gryfang
Programmer
 
gryfang's Avatar
 
Join Date: May 2006
Location: Ohio
Posts: 36
Rep Power: 0 gryfang is on a distinguished road
Send a message via AIM to gryfang
Quote:
Originally Posted by Arevos
No, I think it's more literal than that. Making a Befunge interpreter is fairly simple...
True, BTW nice job. I am going for a an 2d interpreter probably resembling the javascript one found in the link from wiki. Do you think that would be more "User friendly" or would one you just dump and run be simpler for the user.

Also I'm making Q/A problems for the tutorial and I'm finding it more difficult to show how writing with a very limited set of a toy langauge can be done then when I actually learned the language. ... I think variables will be the FIRST thing I add when I get around to it.
__________________
--------------------
LOAD "*" ,8,1

God bless
- Gryfang
gryfang is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
If you had it to do all over again, which language? peace_of_mind Coder's Corner Lounge 24 Jan 13th, 2008 6:06 PM
The C programming Language (2nd Edition) nnxion Book Reviews 10 Jul 6th, 2007 4:29 PM
Language display in program Prm753 C++ 3 May 30th, 2006 6:45 PM
More languages? UnKnown X Coder's Corner Lounge 27 Dec 18th, 2005 4:06 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 9:16 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC