goobers
Member
Hi,
Out of boredom, I decided to write a brainfuck interpreter today. It runs some programs, such as this one:
CODE
++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.
(prints "hello world")
but not others... I was wondering if I might have made an error in the interpreter, or perhaps have missed something.
I know probably a lot of people won't be knowledgeable on such things, but I thought I'd post it for the heck of it...
Wikipedia page on brainfuck
here is the code:
CODE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
unsigned char* p;
unsigned char PC;
char* bfProg;
char bfCommand;
int loadFile(char * pFileName){
FILE * pFile;
long fSize;
size_t result;
pFileName = "test.b";
char * buffer;
pFile = fopen ( pFileName, "rb");
if (pFile==NULL) {fprintf(stderr, "Error opening file"); exit(1);}
else
{
fseek (pFile, 0, SEEK_END);
fSize = ftell (pFile);
rewind (pFile);
// Size the memory buffer to the size of the input file
buffer = (char*) malloc ((sizeof(char)*fSize));
if (buffer == NULL) {fprintf(stderr, "Error loading file into memory buffer"); exit(1);}
// Copy the file into the memory buffer
result = fread (buffer,1, fSize,pFile);
if (result != fSize) {fprintf(stderr, "Error reading file"); exit(1);}
bfProg = (char*) malloc ((sizeof(char)*fSize));
int j;
j=0;
for (int i=0; i < fSize; i++){
if (buffer == 0x3E || buffer == 0x3C || buffer == 0x2B || buffer == 0x2D || buffer == 0x2E || buffer == 0x2C || buffer == 0x5B || buffer == 0x5D){
bfProg[j] = buffer;
j +=1;
}
}
return 0;
}
}
int interpret(char bfCommand){
switch (bfCommand) {
case 0x3E: // >
++p;
PC +=1;
break;
case 0x3C: // <
--p;
PC +=1;
break;
case 0x2B: // +
++*p;
PC +=1;
break;
case 0x2D: // -
--*p;
PC +=1;
break;
case 0x2E: // .
putchar(*p);
PC +=1;
break;
case 0x2C: // ,
*p = getchar ();
PC +=1;
break;
case 0x5B: // [
if(*p){
PC +=1;
}
else{
while (bfProg[PC] != 0x5D){
PC +=1;
}
PC +=1;
}
break;
case 0x5D: // ]
if(*p){
while (bfProg[PC] != 0x5B){
PC -=1;
}
}
else{
PC +=1;
}
break;
}
}
int main(int argc, char *argv[])
{
p = (unsigned char*) malloc ((sizeof(char)*30000));
if (p == NULL){ exit (1); fprintf(stderr, "Memory Error");}
memset (p,0x00,30000);
PC = 0;
loadFile("test.b");
while (bfProg[PC] == 0x3E || bfProg[PC] == 0x3C || bfProg[PC] == 0x2B || bfProg[PC] == 0x2D || bfProg[PC] == 0x2E || bfProg[PC] == 0x2C || bfProg[PC] == 0x5B || bfProg[PC] == 0x5D){
interpret(bfProg[PC]);
//putchar(bfProg[PC]);
}
//bfProg
return 0;
}
Cheers
Out of boredom, I decided to write a brainfuck interpreter today. It runs some programs, such as this one:
CODE
++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.
(prints "hello world")
but not others... I was wondering if I might have made an error in the interpreter, or perhaps have missed something.
I know probably a lot of people won't be knowledgeable on such things, but I thought I'd post it for the heck of it...
Wikipedia page on brainfuck
here is the code:
CODE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
unsigned char* p;
unsigned char PC;
char* bfProg;
char bfCommand;
int loadFile(char * pFileName){
FILE * pFile;
long fSize;
size_t result;
pFileName = "test.b";
char * buffer;
pFile = fopen ( pFileName, "rb");
if (pFile==NULL) {fprintf(stderr, "Error opening file"); exit(1);}
else
{
fseek (pFile, 0, SEEK_END);
fSize = ftell (pFile);
rewind (pFile);
// Size the memory buffer to the size of the input file
buffer = (char*) malloc ((sizeof(char)*fSize));
if (buffer == NULL) {fprintf(stderr, "Error loading file into memory buffer"); exit(1);}
// Copy the file into the memory buffer
result = fread (buffer,1, fSize,pFile);
if (result != fSize) {fprintf(stderr, "Error reading file"); exit(1);}
bfProg = (char*) malloc ((sizeof(char)*fSize));
int j;
j=0;
for (int i=0; i < fSize; i++){
if (buffer == 0x3E || buffer == 0x3C || buffer == 0x2B || buffer == 0x2D || buffer == 0x2E || buffer == 0x2C || buffer == 0x5B || buffer == 0x5D){
bfProg[j] = buffer;
j +=1;
}
}
return 0;
}
}
int interpret(char bfCommand){
switch (bfCommand) {
case 0x3E: // >
++p;
PC +=1;
break;
case 0x3C: // <
--p;
PC +=1;
break;
case 0x2B: // +
++*p;
PC +=1;
break;
case 0x2D: // -
--*p;
PC +=1;
break;
case 0x2E: // .
putchar(*p);
PC +=1;
break;
case 0x2C: // ,
*p = getchar ();
PC +=1;
break;
case 0x5B: // [
if(*p){
PC +=1;
}
else{
while (bfProg[PC] != 0x5D){
PC +=1;
}
PC +=1;
}
break;
case 0x5D: // ]
if(*p){
while (bfProg[PC] != 0x5B){
PC -=1;
}
}
else{
PC +=1;
}
break;
}
}
int main(int argc, char *argv[])
{
p = (unsigned char*) malloc ((sizeof(char)*30000));
if (p == NULL){ exit (1); fprintf(stderr, "Memory Error");}
memset (p,0x00,30000);
PC = 0;
loadFile("test.b");
while (bfProg[PC] == 0x3E || bfProg[PC] == 0x3C || bfProg[PC] == 0x2B || bfProg[PC] == 0x2D || bfProg[PC] == 0x2E || bfProg[PC] == 0x2C || bfProg[PC] == 0x5B || bfProg[PC] == 0x5D){
interpret(bfProg[PC]);
//putchar(bfProg[PC]);
}
//bfProg
return 0;
}
Cheers