Jump to content

Brainfuck interpreter!

- - - - -

  • Please log in to reply
8 replies to this topic

#1
mebob

mebob

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 490 posts
I made a brainfuck interpreter, and I thought I would share it:

#include <iostream>

using namespace std;


int main()

{

	unsigned int loopindex[1024];

	unsigned int loopdepth=0;

	char code[4096];

	char data[4096];

	unsigned int codep=0;

	unsigned int datap=0;

	cin >> code;

	while(code[codep]!=0)

	{

		if(code[codep]=='+') data[datap]++;

		else if(code[codep]=='-') data[datap]--;

		else if(code[codep]=='>') datap++;

		else if(code[codep]=='<') datap--;

		else if(code[codep]=='.') cout << data[datap];

		else if(code[codep]==',') cin >> data[datap];

		else if(code[codep]=='[')

		{

			loopdepth++;

			loopindex[loopdepth]=codep;

		}

		else if(code[codep]==']')

		{

			if(data[datap]) codep=loopindex[loopdepth];

			else loopdepth--;

		}

		codep++;

	}

	cin.ignore();

	cin.get();

}

I have a question about it, though: How could I cast the input to an int?
Latinamne loqueris?

#2
mnirahd

mnirahd

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 330 posts
Hi

You can have a look how to do castingin C++

Munir

#3
mebob

mebob

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 490 posts
I know, I tried that. I also tried reinterpret_cast<>() but that didn't work either.
Latinamne loqueris?

#4
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
From string to int? Have you tried stringstream?
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#5
Vladimir

Vladimir

    Learning Programmer

  • Members
  • PipPipPip
  • 79 posts
Hi mebob,

You need strtol function. It parses the C string interpreting its content as an integral number and allows you to parse numbers in various formats (i.e. 100, 0xff). Example:


#include <iostream>

#include <stdlib.h>


using namespace std;

int main()

{

	char str[] = "2010 7"; // your string (array of chars)

	char * pEnd; // pointer to the current position in array we are parsing


	long year = strtol(str, &pEnd, 10);

	cout<<"Parsed "<<(pEnd - str)<<" chars, year: "<<year<<endl;


	long month = strtol(pEnd, &pEnd, 10); // 10 is a base of a number

	cout<<"Parsed "<<(pEnd - str)<<" chars, month: "<<month<<endl;

}



#6
mebob

mebob

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 490 posts
It's not getting a string as input, it's only getting a single character.
Latinamne loqueris?

#7
Vladimir

Vladimir

    Learning Programmer

  • Members
  • PipPipPip
  • 79 posts
Then you can try atoi function:

	char your_char = '5';

	int your_int = atoi(&your_char); # your_int = 5

or simply assign char to int:

	char your_char = 'a';

	int your_int = your_char; # your_int = 97, 97 is the ASCII number of char 'a'



#8
mebob

mebob

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 490 posts
Neither of them will work: I know how to use the atoi() function, but it only works with null terminated strings (as far as I know), and I only have a single character, which limits me to the numbers 0-9. The second won't work either, because I want the input to be an int, not a character.
Latinamne loqueris?

#9
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
    char* foo = "43210"; //non-null terminated multicharacter string
    int bar = atoi(foo);
    printf("%i", (int)(bar == 43210));
And if you wanted only 0-9, you can do this:
    char foo = '4';
    int bar = (int)foo - '0';
    printf("%i", (int)(bar == 4));

Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users