![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#41 |
|
Hobbyist Programmer
Join Date: Apr 2006
Posts: 155
Rep Power: 3
![]() |
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
|
|
|
|
|
|
#42 | ||
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
Quote:
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:
|
||
|
|
|
|
|
#43 |
|
Programmer
Join Date: Aug 2005
Location: Norway
Posts: 56
Rep Power: 0
![]() |
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. |
|
|
|
|
|
#44 | |
|
Programmer
|
Quote:
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 |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
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 |