Advertisement
thepowderguy

BrainF interpreter

Jul 19th, 2013
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.34 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4.  
  5. using namespace std;
  6.  
  7. int main ()
  8. {
  9.     string Uin;
  10.     ifstream prefs ("files.dat");
  11.     bool fileStdIn, appendLF, charflush, IOmarkers, EXmarkers, commentCHK, inToOut, memloop;
  12.     while (!prefs.is_open())
  13.     {
  14.         std::cerr << "[ERROR] COULD NOT OPEN SETTINGS FILE, PLEASE SPECIFY LOCATION" << endl;
  15.         std::getline (std::cin, Uin);
  16.         prefs.open (Uin);
  17.     }
  18.     if (prefs.good())
  19.     {
  20.         std::getline (prefs, Uin, '\t');
  21.         std::getline (prefs, Uin);
  22.     }
  23.     else
  24.     {
  25.         cout << "[ERROR] INCOMPLETE SETTINGS FILE" << endl;
  26.         std::cout << "Press [ENTER] to continue" << std::endl;
  27.         std::getline (cin, Uin);
  28.         return 0;
  29.     }
  30.     ifstream srcfile (Uin);
  31.     while (!srcfile.is_open())
  32.     {
  33.         std::cerr << "[ERROR] SOURCE FILE NOT FOUND, PLEASE SPECIFY LOCATION" << std::endl;
  34.         std::getline (std::cin, Uin);
  35.         srcfile.open (Uin);
  36.     }
  37.     cout << "[SRC FILE DETECTED]: " << Uin << std::endl;
  38.     if (prefs.good())
  39.     {
  40.         std::getline (prefs, Uin, '\t');
  41.         std::getline (prefs, Uin);
  42.     }
  43.     else
  44.     {
  45.         cout << "[ERROR] INCOMPLETE SETTINGS FILE" << endl;
  46.         std::cout << "Press [ENTER] to continue" << std::endl;
  47.         std::getline (cin, Uin);
  48.         return 0;
  49.     }
  50.     ofstream outfile (Uin);
  51.     while (!outfile.is_open())
  52.     {
  53.         std::cerr << "[ERROR] STD OUTPUT FILE NOT FOUND, PLEASE SPECIFY LOCATION" << std::endl;
  54.         std::getline (std::cin, Uin);
  55.         outfile.open (Uin);
  56.     }
  57.     cout << "[STDOUT FILE DETECTED]: " << Uin << std::endl;
  58.     if (prefs.good())
  59.     {
  60.         std::getline (prefs, Uin, '\t');
  61.         std::getline (prefs, Uin);
  62.     }
  63.     else
  64.     {
  65.         cout << "[ERROR] INCOMPLETE SETTINGS FILE" << endl;
  66.         std::cout << "Press [ENTER] to continue" << std::endl;
  67.         std::getline (cin, Uin);
  68.         return 0;
  69.     }
  70.     ofstream errfile (Uin);
  71.     while (!errfile.is_open())
  72.     {
  73.         std::cerr << "[ERROR] STD ERROR FILE NOT FOUND, PLEASE SPECIFY LOCATION" << std::endl;
  74.         std::getline (std::cin, Uin);
  75.         errfile.open (Uin);
  76.     }
  77.     cout << "[STDERR FILE DETECTED]: " << Uin << std::endl;
  78.     if (prefs.good())
  79.     {
  80.         std::getline (prefs, Uin, '\t');
  81.         std::getline (prefs, Uin);
  82.     }
  83.     else
  84.     {
  85.         cout << "[ERROR] INCOMPLETE SETTINGS FILE" << endl;
  86.         std::cout << "Press [ENTER] to continue" << std::endl;
  87.         std::getline (cin, Uin);
  88.         return 0;
  89.     }
  90.     ifstream infile (Uin);
  91.     while (!infile.is_open())
  92.     {
  93.         std::cerr << "[ERROR] STD INPUT FILE NOT FOUND, PLEASE SPECIFY LOCATION" << std::endl;
  94.         std::getline (std::cin, Uin);
  95.         infile.open (Uin);
  96.     }
  97.     cout << "[STDIN FILE DETECTED]: " << Uin << std::endl;
  98.  
  99.     if (prefs.good()) { prefs >> Uin; prefs >> Uin; }
  100.     else
  101.     {
  102.         cout << "[ERROR] INCOMPLETE SETTINGS FILE" << endl;
  103.         std::cout << "Press [ENTER] to continue" << std::endl;
  104.         std::getline (cin, Uin);
  105.         return 0;
  106.     }
  107.  
  108.     if (Uin == "true" || Uin == "TRUE" || Uin == "1") fileStdIn = true;
  109.     else fileStdIn = false;
  110.  
  111.     if (prefs.good()) { prefs >> Uin; prefs >> Uin; }
  112.     else
  113.     {
  114.         cout << "[ERROR] INCOMPLETE SETTINGS FILE" << endl;
  115.         std::cout << "Press [ENTER] to continue" << std::endl;
  116.         std::getline (cin, Uin);
  117.         return 0;
  118.     }
  119.  
  120.     if (Uin == "true" || Uin == "TRUE" || Uin == "1") appendLF = true;
  121.     else appendLF = false;
  122.  
  123.     if (prefs.good()) { prefs >> Uin; prefs >> Uin; }
  124.     else
  125.     {
  126.         cout << "[ERROR] INCOMPLETE SETTINGS FILE" << endl;
  127.         std::cout << "Press [ENTER] to continue" << std::endl;
  128.         std::getline (cin, Uin);
  129.         return 0;
  130.     }
  131.     if (Uin == "true" || Uin == "TRUE" || Uin == "1") charflush = true;
  132.     else charflush = false;
  133.  
  134.     if (prefs.good()) { prefs >> Uin; prefs >> Uin; }
  135.     else
  136.     {
  137.         cout << "[ERROR] INCOMPLETE SETTINGS FILE" << endl;
  138.         std::cout << "Press [ENTER] to continue" << std::endl;
  139.         std::getline (cin, Uin);
  140.         return 0;
  141.     }
  142.  
  143.     if (Uin == "true" || Uin == "TRUE" || Uin == "1") IOmarkers = true;
  144.     else IOmarkers = false;
  145.  
  146.     if (prefs.good()) { prefs >> Uin; prefs >> Uin; }
  147.     else
  148.     {
  149.         cout << "[ERROR] INCOMPLETE SETTINGS FILE" << endl;
  150.         std::cout << "Press [ENTER] to continue" << std::endl;
  151.         std::getline (cin, Uin);
  152.         return 0;
  153.     }
  154.  
  155.     if (Uin == "true" || Uin == "TRUE" || Uin == "1") EXmarkers = true;
  156.     else EXmarkers = false;
  157.  
  158.     if (prefs.good()) { prefs >> Uin; prefs >> Uin; }
  159.     else
  160.     {
  161.         cout << "[ERROR] INCOMPLETE SETTINGS FILE" << endl;
  162.         std::cout << "Press [ENTER] to continue" << std::endl;
  163.         std::getline (cin, Uin);
  164.         return 0;
  165.     }
  166.  
  167.     if (Uin == "true" || Uin == "TRUE" || Uin == "1") commentCHK = true;
  168.     else commentCHK = false;
  169.  
  170.     if (prefs.good()) { prefs >> Uin; prefs >> Uin; }
  171.     else
  172.     {
  173.         cout << "[ERROR] INCOMPLETE SETTINGS FILE" << endl;
  174.         std::cout << "Press [ENTER] to continue" << std::endl;
  175.         std::getline (cin, Uin);
  176.         return 0;
  177.     }
  178.  
  179.     if (Uin == "true" || Uin == "TRUE" || Uin == "1") inToOut = true;
  180.     else inToOut = false;
  181.  
  182.     if (prefs.good()) { prefs >> Uin; prefs >> Uin; }
  183.     else
  184.     {
  185.         cout << "[ERROR] INCOMPLETE SETTINGS FILE" << endl;
  186.         std::cout << "Press [ENTER] to continue" << std::endl;
  187.         std::getline (cin, Uin);
  188.         return 0;
  189.     }
  190.  
  191.     if (Uin == "true" || Uin == "TRUE" || Uin == "1") memloop = true;
  192.     else memloop = false;
  193.  
  194.     prefs.close();
  195.     std::cout << std::endl;
  196.  
  197.     if (EXmarkers)
  198.         cout << "USE INPUT FILE FOR STDIN: " << fileStdIn << std::endl
  199.         << "APPEND NEWLINE TO ALL INPUT: " << appendLF << std::endl
  200.         << "FLUSH ALL OUTPUT CHARS: " << charflush << std::endl
  201.         << "OUTPUT IO MARKERS TO CONSOLE: " << IOmarkers << std::endl
  202.         << "OUTPUT INFORMATION AL MARKERS: " << EXmarkers << std::endl
  203.         << "CHECK FOR COMMENT MARKERS: " << commentCHK << std::endl
  204.         << "WRITE INPUT TO OUTPUT FILE: " << inToOut << std::endl
  205.         << "MEMORY POINTER CAN LOOP: " << memloop << std::endl << std::endl;
  206.  
  207.     unsigned int strptr = 0, memptr = 0, Uptr, brPrePtr = 0;
  208.     int br_count = 0; char cur_ins, bfmem[65536] = {0};
  209.     string src = "", input; Uin = "";
  210.     while (srcfile.good())
  211.     {
  212.         std::getline (srcfile, input);
  213.         if (commentCHK)
  214.         {
  215.             if (input.find ("#", 0) != string::npos)
  216.             {
  217.                 input.replace (input.find ("#", 0), input.length() - input.find ("#", 0), "");
  218.             }
  219.         }
  220.         src += input;
  221.     }
  222.     if (EXmarkers) std::cout << "[DONE READING SOURCE]" << std::endl;
  223.     while (strptr < src.length())
  224.     {
  225.         if (src.at(strptr) == '+' || src.at(strptr) == '-' || src.at(strptr) == '>' || src.at(strptr) == '<' ||
  226.             src.at(strptr) == ',' || src.at(strptr) == '.' || src.at(strptr) == '[' || src.at(strptr) == ']')
  227.         strptr++;
  228.        
  229.         else src.erase (strptr, 1);
  230.     }
  231.     strptr = 0;
  232.     while (!(brPrePtr == src.length() || br_count < 0))
  233.     {
  234.         if (src.at (brPrePtr) == '[') br_count++;
  235.         else if (src.at (brPrePtr) == ']') br_count--;
  236.         brPrePtr++;
  237.     }
  238.     if (EXmarkers) std::cout << "[BRACKET CHECK DONE]" << std::endl;
  239.     if (br_count < 0)
  240.     {
  241.         std::cerr << "[ERROR]: NO OPENING BRACKET" << std::endl;
  242.         errfile << "[ERROR]: NO OPENING BRACKET" << std::endl;
  243.     }
  244.     else if (br_count > 0)
  245.     {
  246.         std::cerr << "[ERROR]: NO CLOSING BRACKET" << std::endl;
  247.         errfile << "[ERROR]: NO CLOSING BRACKET" << std::endl;
  248.     }
  249.     else
  250.     {
  251.         input = "";
  252.         br_count = 0;
  253.         std::cout << "[PROGRAM STARTED]" << std::endl;
  254.  
  255.         if (IOmarkers) cout << "[OUT]\t";
  256.  
  257.         while (strptr < src.length())
  258.         {
  259.             cur_ins = src.at (strptr);
  260.             if (cur_ins == '+')
  261.             {
  262.                 if (bfmem[memptr] == 255) bfmem[memptr] = 0;
  263.                 else bfmem[memptr]++;
  264.             }
  265.             else if (cur_ins == '-')
  266.             {
  267.                 if (bfmem[memptr] == 0) bfmem[memptr] = 255;
  268.                 else bfmem[memptr]--;
  269.             }
  270.             else if (cur_ins == '>')
  271.             {
  272.                 if (memptr == 65535)
  273.                 {
  274.                     if (memloop) memptr = 0;
  275.                     else
  276.                     {
  277.                         std::cerr << "[ERROR]: OUT OF BOUNDS MEMORY (0xFFFF)" << std::endl;
  278.                         errfile << "[ERROR]: OUT OF BOUNDS MEMORY (0xFFFF)" << std::endl;
  279.                         std::cout << "Press [ENTER] to continue" << std::endl;
  280.                         std::getline (cin, Uin);
  281.                         return 0;
  282.                     }
  283.                 }
  284.                 else memptr++;
  285.             }
  286.             else if (cur_ins == '<')
  287.             {
  288.                 if (memptr == 0)
  289.                 {
  290.                     if (memloop) memptr = 65535;
  291.                     else
  292.                     {
  293.                         std::cerr << "[ERROR]: OUT OF BOUNDS MEMORY (-0x0001)" << std::endl;
  294.                         errfile << "[ERROR]: OUT OF BOUNDS MEMORY (-0x0001)" << std::endl;
  295.                         std::cout << "Press [ENTER] to continue" << std::endl;
  296.                         std::getline (cin, Uin);
  297.                         return 0;
  298.                     }
  299.                 }
  300.                 else memptr--;
  301.             }
  302.             else if (cur_ins == '.')
  303.             {
  304.                 std::cout << bfmem[memptr];
  305.                 outfile << bfmem[memptr];
  306.  
  307.                 if (bfmem[memptr] == '\n' && IOmarkers) cout << "[OUT]\t";
  308.                 if (charflush)
  309.                 {
  310.                     std::cout << std::flush;
  311.                     outfile << std::flush;
  312.                 }
  313.             }
  314.             else if (cur_ins == ',')
  315.             {
  316.                 if (Uin.length() < 1)
  317.                 {
  318.                     if (IOmarkers) std::cout << "\n[IN]";
  319.  
  320.                     if (fileStdIn)
  321.                     {
  322.                         if (infile.good())
  323.                         {
  324.                             std::getline (infile, Uin);
  325.                             cout << Uin << "\n";
  326.                         }
  327.                         else
  328.                         {
  329.                             Uin = " ";
  330.                             Uin.at(0) = 0;
  331.                         }
  332.                     }
  333.                     else std::getline (std::cin, Uin);
  334.                     if (inToOut) outfile << Uin << std::endl;
  335.                     if (IOmarkers) std::cout << "[OUT]\t";
  336.  
  337.                     Uptr = 0;
  338.                     while (Uptr + 1 < Uin.length() && Uin.length() != 0)
  339.                     {
  340.                         if (Uin.at (Uptr) == '\\')
  341.                         {
  342.                             if (Uptr + 1 == Uin.length()) Uin.replace (Uptr, 1, "-");
  343.  
  344.                             else if (Uin.at (Uptr + 1) == 'n') Uin.replace (Uptr, 2, "\n");
  345.  
  346.                             else if (Uin.at (Uptr + 1) == 'a') Uin.replace (Uptr, 2, "\a");
  347.  
  348.                             else if (Uin.at (Uptr + 1) == 'b') Uin.replace (Uptr, 2, "\b");
  349.  
  350.                             else if (Uin.at (Uptr + 1) == '\\') Uin.replace (Uptr, 2, "\\");
  351.  
  352.                             else Uin.replace (Uptr, 2, "-");
  353.                         }
  354.                         Uptr++;
  355.                     }
  356.                     if (appendLF || Uin.length() == 0) Uin += "\n";
  357.                 }
  358.                 bfmem[memptr] = Uin.at (0);
  359.                 Uin.erase (0, 1);
  360.             }
  361.             else if (cur_ins == '[')
  362.             {
  363.                 if (!bfmem[memptr])
  364.                 {
  365.                     br_count = 1;
  366.                     while (br_count)
  367.                     {
  368.                         strptr++;
  369.                         cur_ins = src.at (strptr);
  370.                         if (cur_ins == '[') br_count++;
  371.                         else if (cur_ins == ']') br_count--;
  372.                     }
  373.                 }
  374.             }
  375.             else if (cur_ins == ']')
  376.             {
  377.                 if (bfmem[memptr])
  378.                 {
  379.                     br_count = 1;
  380.                     while (br_count)
  381.                     {
  382.                         strptr--;
  383.                         cur_ins = src.at (strptr);
  384.                         if (cur_ins == ']') br_count++;
  385.                         else if (cur_ins == '[') br_count--;
  386.                     }
  387.                 }
  388.             }
  389.             if (strptr == src.length() - 1) break;
  390.             strptr++;
  391.         }
  392.         std::cout << "\n[PROGRAM ENDED]" << std::endl;
  393.         outfile.flush();
  394.         outfile.close();
  395.         errfile.flush();
  396.         errfile.close();
  397.         if (EXmarkers) std::cout << "[FILES FLUSHED]" << std::endl;
  398.     }
  399.     std::cout << "Press [ENTER] to continue" << std::flush;
  400.     std::getline (std::cin, Uin);
  401.     return 0;
  402. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement