Guest User

Untitled

a guest
Jan 30th, 2015
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.43 KB | None | 0 0
  1. #include "limitedCompilerO.h"
  2.  
  3.  
  4. ifstream OCompiler::InputFile;
  5. ofstream OCompiler::OutputFile;
  6. int OCompiler::l = 0;
  7. int OCompiler::g_loopID = 0;
  8.  
  9.  
  10. OCompiler::OCompiler()
  11. {
  12. }
  13. OCompiler::OCompiler(int temp_loopID) : loopID(temp_loopID)
  14. {
  15. }
  16. OCompiler::OCompiler(char **strInputFile) : loopID(0)
  17. {
  18.     InputFile.open(*strInputFile, fstream::in);
  19.  
  20.     if (InputFile.is_open())
  21.     {
  22.         string strOutputFile("O");
  23.         strOutputFile.append(*strInputFile);
  24.         OutputFile.open(strOutputFile.c_str(), fstream::out | fstream::trunc);
  25.         if (!OutputFile.is_open()) throw 1;
  26.         cout << "New file created:" << strOutputFile << endl;
  27.     }
  28.     else throw 0;
  29.    
  30.     beginningOfFilePos = InputFile.tellg();
  31. }
  32. OCompiler::~OCompiler()
  33. {
  34. }
  35.  
  36. void OCompiler::RunOCompiler()
  37. {
  38.     while (FindFunction())
  39.     {
  40.         SyntacticAnalys();
  41.         OutputFile << "\n\tchar o = 0;\n\t\n";
  42.         TranslateCodeBlock();
  43.     }
  44.    
  45.     cout << "Done! Compile the new file with a C compiler and your code is fully compiled.";
  46. }
  47.  
  48. bool OCompiler::FindFunction()
  49. {
  50.     while (!InputFile.eof())
  51.     {
  52.         InputFile.get(tempChar);
  53.         OutputFile.put(tempChar);
  54.         //InputFile >> tempChar;
  55.         //OutputFile << tempChar;
  56.        
  57.         if (tempChar == '{')
  58.         {
  59.             streampos oldpos = InputFile.tellg();  // stores the old position.
  60.            
  61.             do { // go back all spaces.
  62.                 InputFile.unget();
  63.                 InputFile.unget();
  64.                 tempPos = InputFile.tellg();
  65.                 InputFile.get(tempChar);
  66.             } while ((tempChar == ' ' ||
  67.                 tempChar == '\r' || tempChar == '\n' || tempChar == '\f' || tempChar == '\t') &&
  68.                 tempPos != beginningOfFilePos);
  69.            
  70.             InputFile.seekg(oldpos);   // get back to old position.
  71.             if (tempChar == ')') return true;
  72.         }
  73.     }
  74.  
  75.     return false;
  76. }
  77.  
  78. void OCompiler::SyntacticAnalys()
  79. {
  80.     do {
  81.         InputFile.get(tempChar);
  82.        
  83.         switch (tempChar)
  84.         {
  85.             case ';': // Is it a data type or a "regular" command?
  86.                 tempToken.push_back(tempChar);
  87.             case1:
  88.                 for (i = 0; tempToken[i] == ' '; ++i); // jump over all the space to the code begins.
  89.                 for (; tempToken[i] != ' ' && tempToken[i] != ';' && tempToken[i] != '('; ++i) tempToken_stemComparing.push_back(tempToken[i]);
  90.                
  91.                 if (!tempToken_stemComparing.compare("if"))
  92.                 {
  93.                     ParseTree.push_back(1);
  94.                     tempToken_stemComparing.clear();
  95.                     tempToken.erase(0, i); // Erase the "if" part.
  96.                    
  97.                     i = 0;
  98.                     while (tempToken[i++] == ' ');
  99.                     tempToken_stemComparing.push_back('(');
  100.                     for (k = 1; k; i++)
  101.                     {
  102.                         tempToken_stemComparing.push_back(tempToken[i]);
  103.                         if (tempToken[i] == '(') ++k;
  104.                         if (tempToken[i] == ')' && k) --k;
  105.                     }
  106.                     ControlStatementDeque.push_back(tempToken_stemComparing);
  107.                     tempToken_stemComparing.clear();
  108.                     tempToken.erase(0, i);
  109.                     goto case1;
  110.                 }
  111.                 else if (!tempToken_stemComparing.compare("^"))
  112.                 {
  113.                     for (i = 0; tempToken[i] == ' '; ++i);
  114.                     tempToken.erase(0, i);
  115.                     ParseTree.push_back(0);
  116.                     OperationDeque.push_back("o = 1;");
  117.                 }
  118.                 else if (!tempToken_stemComparing.compare("v"))
  119.                 {
  120.                     for (i = 0; tempToken[i] == ' '; ++i);
  121.                     tempToken.erase(0, i);
  122.                     ParseTree.push_back(0);
  123.                     OperationDeque.push_back("o = 0;");
  124.                 }
  125.                 else
  126.                 {
  127.                     for (i = 0; i < CDataTypesSize && tempToken_stemComparing.compare(CDataTypes[i]); ++i);
  128.                     if (i < CDataTypesSize) // Is it a data type?
  129.                     {
  130.                         for (i = 0; tempToken[i] == ' '; ++i);
  131.                         tempToken.erase(0, i);
  132.                         DataDeque.push_back(tempToken);
  133.                     }
  134.                     else // It is just a regular command.
  135.                     {
  136.                         for (i = 0; tempToken[i] == ' '; ++i);
  137.                         tempToken.erase(0, i);
  138.                         ParseTree.push_back(0);
  139.                         OperationDeque.push_back(tempToken);
  140.                     }
  141.                 }
  142.                
  143.                 tempToken_stemComparing.clear();
  144.                 tempToken.clear();
  145.                 break;
  146.             case '{': // Add new code block.
  147.                 tempToken.push_back(tempChar);
  148.                 for (i = 0; tempToken[i] == ' '; ++i);
  149.                 for (; tempToken[i] != ' ' && tempToken[i] != '{' && tempToken[i] != '('; ++i) tempToken_stemComparing.push_back(tempToken[i]);
  150.                 if (!tempToken_stemComparing.compare("if"))
  151.                 {
  152.                     ParseTree.push_back(1);
  153.                     tempToken_stemComparing.clear();
  154.                     tempToken.erase(0, i); // Erase the "if" part.
  155.                    
  156.                     i = 0;
  157.                     while (tempToken[i++] == ' ');
  158.                     tempToken_stemComparing.push_back('(');
  159.                     for (k = 1; k; i++)
  160.                     {
  161.                         tempToken_stemComparing.push_back(tempToken[i]);
  162.                         if (tempToken[i] == '(') ++k;
  163.                         if (tempToken[i] == ')' && k) --k;
  164.                     }
  165.                     ControlStatementDeque.push_back(tempToken_stemComparing);
  166.                     tempToken_stemComparing.clear();
  167.                     tempToken.erase(0, i);
  168.                 }
  169.                
  170.                 ParseTree.push_back(2);
  171.                 ++g_loopID;
  172.                 CodeBlockVector.emplace_back(g_loopID); // Creat new code block object in the vector at the end.
  173.                 CodeBlockVector.back().SyntacticAnalys();   // Call the procedure in the code block object to handel code block syntactic analys.
  174.                
  175.                 tempToken_stemComparing.clear();
  176.                 tempToken.clear();
  177.                 break;
  178.             default:
  179.                 tempToken.push_back(tempChar);
  180.             case '\r':
  181.             case '\n':
  182.             case '\f':
  183.             case '\t':
  184.                 break;
  185.         }
  186.     } while (tempChar != '}');
  187.    
  188.     tempToken.clear();
  189. }
  190.  
  191. void OCompiler::TranslateCodeBlock()
  192. { // 0 = command, 1 = if(), 2 = code block
  193.     while (!DataDeque.empty()) // Print all variabals.
  194.     {
  195.         PrintNumberOfTabs();
  196.         OutputFile << "\t" << DataDeque.front() << "\n";
  197.         DataDeque.pop_front();
  198.     }
  199.    
  200.     //====================================
  201.     PrintNumberOfTabs();
  202.     OutputFile << "\tlong long int c = 0;\n";
  203.     PrintNumberOfTabs();
  204.     OutputFile << "\t\n";
  205.     PrintNumberOfTabs();
  206.     OutputFile << "\tstart" << loopID << ":\n";
  207.     PrintNumberOfTabs();
  208.     OutputFile << "\tswitch(c)\n";
  209.     PrintNumberOfTabs();
  210.     OutputFile << "\t{\n";
  211.    
  212.     int q = 0; // case number.
  213.    
  214.     for (deque<char>::iterator it = ParseTree.begin(), end = ParseTree.end(); it != end; ++it, ++q)
  215.     {
  216.         PrintNumberOfTabs();
  217.         OutputFile << "\t\tcase " << q << ":\n";
  218.         PrintNumberOfTabs();
  219.        
  220.         switch (*it)
  221.         {
  222.             case 0: // commands
  223.                 OutputFile << "\t\t\t" << OperationDeque.front() << "\n";
  224.                 OperationDeque.pop_front();
  225.                 break;
  226.             case 1: // if
  227.                 OutputFile << "\t\t\tif (!" << ControlStatementDeque.front() << ")\n";
  228.                 PrintNumberOfTabs();
  229.                 OutputFile << "\t\t\t{\n";
  230.                 PrintNumberOfTabs();
  231.                 OutputFile << "\t\t\t\tif (o) c--;\n";
  232.                 PrintNumberOfTabs();
  233.                 OutputFile << "\t\t\t\telse c++;\n";
  234.                 PrintNumberOfTabs();
  235.                 OutputFile << "\t\t\t}\n";
  236.                 ControlStatementDeque.pop_front();
  237.                 break;
  238.             case 2: // code block
  239.                 OutputFile << "\t\t\t{\n";
  240.                 l+=3;
  241.                 CodeBlockVector.front().TranslateCodeBlock();
  242.                 CodeBlockVector.erase(CodeBlockVector.begin());
  243.                 l-=3;
  244.                 OutputFile << "\n";
  245.             default:
  246.                 break;
  247.         }
  248.        
  249.         PrintNumberOfTabs();
  250.         OutputFile << "\t\t\tbreak;\n";
  251.     }
  252.    
  253.     PrintNumberOfTabs();
  254.     OutputFile << "\t}\n";
  255.     PrintNumberOfTabs();
  256.     OutputFile << "\t\n";
  257.     PrintNumberOfTabs();
  258.     OutputFile << "\tif(o) { --c; if (c < 0) c = (c * -1); }\n";
  259.     PrintNumberOfTabs();
  260.     OutputFile << "\telse { ++c; if (c > " << q << ") c = c - " << q << "; };\n";
  261.     PrintNumberOfTabs();
  262.     OutputFile << "\t";
  263.     if (loopID) OutputFile << "if (c < " << q << " && c > 0) ";
  264.     OutputFile << "goto start" << loopID << ";\n";
  265.     PrintNumberOfTabs();
  266.     OutputFile << "}";
  267.     //====================================
  268.    
  269.     DataDeque.clear();
  270.     OperationDeque.clear();
  271.     ControlStatementDeque.clear();
  272.     CodeBlockVector.clear();
  273.     ParseTree.clear();
  274. }
  275. void OCompiler::PrintNumberOfTabs()
  276. {
  277.     for (int N = l; N; --N)
  278.         OutputFile << "\t";
  279. }
Advertisement
Add Comment
Please, Sign In to add comment