Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string>
- using namespace std;
- void execCode(string progCode, unsigned char *p);
- int main(int argc, char **argv)
- {
- fstream Code;
- string progCode = "", loadBuff;
- unsigned char progMem[30000];
- unsigned char *p;
- int i;
- if (argc==1)
- {
- cout << "usage: " << argv[0] << " filename.bf" << endl;
- return 1;
- }
- else
- {
- Code.open(argv[1], ios::in);
- if (Code.good() == true)
- {
- while (!Code.eof())
- {
- getline(Code, loadBuff);
- progCode = progCode + loadBuff;
- loadBuff = "";
- }
- Code.close();
- }
- else
- {
- cout << "cannot open " << argv[1] << endl;
- return 2;
- }
- for (i=0; i<30000; i++)
- {
- progMem[i]=0;
- }
- p = &progMem[0];
- execCode(progCode, p);
- }
- }
- void execCode(string progCode, unsigned char *p)
- {
- unsigned int i = 0;
- unsigned int loopCount = 0;
- char currentInstruction;
- for (i=0; progCode[i]!=0; i++)
- {
- currentInstruction=progCode[i];
- switch(currentInstruction)
- {
- case '>':
- ++p;
- break;
- case '<':
- --p;
- break;
- case '+':
- ++(*p);
- break;
- case '-':
- --(*p);
- break;
- case '.':
- cout << *p;
- break;
- case ',':
- cin >> *p;
- break;
- case '[':
- continue;
- break;
- }
- if (currentInstruction == ']' && *p)
- {
- loopCount = 1;
- while (loopCount > 0)
- {
- currentInstruction = progCode[--i];
- if (currentInstruction == '[')
- {
- loopCount--;
- }
- else if (currentInstruction == ']')
- {
- loopCount++;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement