Brainf* script parser C->C++, wrong output

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
MadPumpkin
Chaos Rift Maniac
Chaos Rift Maniac
Posts: 484
Joined: Fri Feb 13, 2009 4:48 pm
Current Project: Octopia
Favorite Gaming Platforms: PS1-3, Genesis, Dreamcast, SNES, PC
Programming Language of Choice: C/++,Java,Py,LUA,XML
Location: C:\\United States of America\Utah\West Valley City\Neighborhood\House\Computer Desk

Brainf* script parser C->C++, wrong output

Post by MadPumpkin »

I previously wrote a brainfuck script parser in C with some help, and I have been converting it into more preferable C++ with less emphasis on files, and more on strings. Though I've been changing my methods for hours, trying to get the correct output, I can't seem to get it to do what it's told!

The original written in C

Code: Select all

#include <stdio.h>
#include <stdlib.h>


#define MAX_FILENAME 300

static char *ptr;

int do_command(const char command, FILE *pFile)
{
	char c;
	int pos;

	switch (command)
	{
		case '>':
			++ptr;
			break;
		case '<':
			--ptr;
			break;
		case '+':
			++*ptr;
			break;
		case '-':
			--*ptr;
			break;
		case '.':
			putchar(*ptr);
			break;
		case ',':
			*ptr=getchar();
			break;
		case '[':
			pos = ftell(pFile);
			while (*ptr)
			{
				fseek(pFile, pos, SEEK_SET);
				c = getc(pFile);
				while (c != ']' && c != EOF)
				{
					do_command(c, pFile);
					c = getc(pFile);
				}
			}
			break;
	}
}

int parse_source(const char *source)
{
	char command; // Stores each command
	FILE *pFile = fopen(source, "r");

	if (pFile) // If we were able to open the file
	{
		while ((command = getc(pFile)) != EOF) // For each character in file
		{
			do_command(command, pFile); // Execute command
		}
	}
	else
	{
		printf("File could not be read.");
	}

	fclose(pFile);
}

int main(int argc, const char *argv[])
{
	int memory[9001]; // Make array to store values in
	ptr = &memory; // Put it into the global pointer

	parse_source(argv[1]); // Parse the source file defined in argument
	return 0;
}


The C++ conversion

Code: Select all

#define BRAINF_ARRAY 100

class Brainf
{
public:
	Brainf(){  bf = (char*)mem;  }

	~Brainf(){  bf = NULL;
		delete bf;  }

	void Parse(std::string line)
	{
		int position=0;
		const char *str = line.c_str();
		char cmd;

		while (position != line.length())
		{
			cmd=str[position];

			switch (cmd)
			{

				// THESE CASES ARE ALL THE SAME EXCEPT THE ONE BELOW
				// SO I HAVE OMITTED THEM FROM THE CODE FOR LENGTH REASONS
				case '[':
					{
						std::string tmp;
						position++;          // If you find '[' add one to the position, so that I don't create an infinite loop of  [
						while (*bf)
						{
							while (cmd != ']')
							{
								cmd=str[position];	// AS FAR AS I KNOW: This code should append each command after '[' and before ']'
								tmp += cmd;		//		to the string 'tmp' and add one to the position afterward, before going back and doing it again
								position++;		// 	However, the output of the string I input should be "Hello world!" just for testing, 
							}					//	and it works properly with the C code. But instead outputs characters it isn't supposed to.
							Parse(tmp);			//	I know it's because of this loop segment, or perhaps with the position it's looping through.
						}
					}
					break;
			}
			position++; // After each character of the array is parsed, the position is increased
		}
	}


private:
	// std::fstream file;
	int mem[BRAINF_ARRAY];
	// bf = &memory;
	char *bf;
};

I realize this is a lot of code to post, but most of it is near the same, so I've omitted it from the C++ conversion. I know this is probably some stupid mistake on my part. I also realize supplying this much code is a major stretch on my part, but commented it thoroughly and have tried to fix it for a long time now. Here is the brainfuck code that should output "Hello world!"...

Code: Select all

++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.
Last edited by MadPumpkin on Thu Dec 22, 2011 8:52 pm, edited 2 times in total.
While Jesus equipped with angels, the Devil's equipped with cops
For God so loved the world that he blessed the thugs with rock
Image
Image
Image
User avatar
short
ES Beta Backer
ES Beta Backer
Posts: 548
Joined: Thu Apr 30, 2009 2:22 am
Current Project: c++, c
Favorite Gaming Platforms: SNES, PS2, SNES, SNES, PC NES
Programming Language of Choice: c, c++
Location: Oregon, US

Re: Brainf* script parser C->C++, wrong output

Post by short »

1> main.cpp
1>c:\users\benjamin\desktop\brainfuck\brainfuck\brainf_c89.h(9): error C2065: 'ptr' : undeclared identifier
1>c:\users\benjamin\desktop\brainfuck\brainfuck\brainf_c89.h(12): error C2065: 'ptr' : undeclared identifier
1>c:\users\benjamin\desktop\brainfuck\brainfuck\brainf_c89.h(15): error C2065: 'ptr' : undeclared identifier
1>c:\users\benjamin\desktop\brainfuck\brainfuck\brainf_c89.h(18): error C2065: 'ptr' : undeclared identifier
1>c:\users\benjamin\desktop\brainfuck\brainfuck\brainf_c89.h(21): error C2065: 'ptr' : undeclared identifier
1>c:\users\benjamin\desktop\brainfuck\brainfuck\brainf_c89.h(24): error C2065: 'ptr' : undeclared identifier
1>c:\users\benjamin\desktop\brainfuck\brainfuck\brainf_c89.h(31): error C2065: 'ptr' : undeclared identifier
trying to compile the c version, something is left something out

edit: upload your code to github and it would be much easier to look through the entire thing :)
My github repository contains the project I am currently working on,
link: https://github.com/bjadamson
User avatar
MadPumpkin
Chaos Rift Maniac
Chaos Rift Maniac
Posts: 484
Joined: Fri Feb 13, 2009 4:48 pm
Current Project: Octopia
Favorite Gaming Platforms: PS1-3, Genesis, Dreamcast, SNES, PC
Programming Language of Choice: C/++,Java,Py,LUA,XML
Location: C:\\United States of America\Utah\West Valley City\Neighborhood\House\Computer Desk

Re: Brainf* script parser C->C++, wrong output

Post by MadPumpkin »

There the cut down C code has been replaced with the full one. It compiles under gcc without flaw and produces exactly the program I want it to.
While Jesus equipped with angels, the Devil's equipped with cops
For God so loved the world that he blessed the thugs with rock
Image
Image
Image
User avatar
short
ES Beta Backer
ES Beta Backer
Posts: 548
Joined: Thu Apr 30, 2009 2:22 am
Current Project: c++, c
Favorite Gaming Platforms: SNES, PS2, SNES, SNES, PC NES
Programming Language of Choice: c, c++
Location: Oregon, US

Re: Brainf* script parser C->C++, wrong output

Post by short »

MadPumpkin wrote:There the cut down C code has been replaced with the full one. It compiles under gcc without flaw and produces exactly the program I want it to.
I am glad they can both compile with parse_source(...) and do_command(...) not actually returning any values despite their signatures... GG gcc :roll:

edit: also it would help to have the file your loading in :)
My github repository contains the project I am currently working on,
link: https://github.com/bjadamson
User avatar
MadPumpkin
Chaos Rift Maniac
Chaos Rift Maniac
Posts: 484
Joined: Fri Feb 13, 2009 4:48 pm
Current Project: Octopia
Favorite Gaming Platforms: PS1-3, Genesis, Dreamcast, SNES, PC
Programming Language of Choice: C/++,Java,Py,LUA,XML
Location: C:\\United States of America\Utah\West Valley City\Neighborhood\House\Computer Desk

Re: Brainf* script parser C->C++, wrong output

Post by MadPumpkin »

short wrote:
MadPumpkin wrote:There the cut down C code has been replaced with the full one. It compiles under gcc without flaw and produces exactly the program I want it to.
I am glad they can both compile with parse_source(...) and do_command(...) not actually returning any values despite their signatures... GG gcc :roll:

edit: also it would help to have the file your loading in :)
Lol the one I replaced it with isn't my code don't worry, I would never forget something like a return value. The C I wrote, is just based on ths "full version" that is now there.

The files complete contents are the symbols in the last code tag. (generally brainfuck scripts are saved in a text file with .b, though this program doesn't require it obviously)
While Jesus equipped with angels, the Devil's equipped with cops
For God so loved the world that he blessed the thugs with rock
Image
Image
Image
Post Reply