Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "limitedCompilerO.h"
- ifstream OCompiler::InputFile;
- ofstream OCompiler::OutputFile;
- int OCompiler::l = 0;
- int OCompiler::g_loopID = 0;
- OCompiler::OCompiler()
- {
- }
- OCompiler::OCompiler(int temp_loopID) : loopID(temp_loopID)
- {
- }
- OCompiler::OCompiler(char **strInputFile) : loopID(0)
- {
- InputFile.open(*strInputFile, fstream::in);
- if (InputFile.is_open())
- {
- string strOutputFile("O");
- strOutputFile.append(*strInputFile);
- OutputFile.open(strOutputFile.c_str(), fstream::out | fstream::trunc);
- if (!OutputFile.is_open()) throw 1;
- cout << "New file created:" << strOutputFile << endl;
- }
- else throw 0;
- beginningOfFilePos = InputFile.tellg();
- }
- OCompiler::~OCompiler()
- {
- }
- void OCompiler::RunOCompiler()
- {
- while (FindFunction())
- {
- SyntacticAnalys();
- OutputFile << "\n\tchar o = 0;\n\t\n";
- TranslateCodeBlock();
- }
- cout << "Done! Compile the new file with a C compiler and your code is fully compiled.";
- }
- bool OCompiler::FindFunction()
- {
- while (!InputFile.eof())
- {
- InputFile.get(tempChar);
- OutputFile.put(tempChar);
- //InputFile >> tempChar;
- //OutputFile << tempChar;
- if (tempChar == '{')
- {
- streampos oldpos = InputFile.tellg(); // stores the old position.
- do { // go back all spaces.
- InputFile.unget();
- InputFile.unget();
- tempPos = InputFile.tellg();
- InputFile.get(tempChar);
- } while ((tempChar == ' ' ||
- tempChar == '\r' || tempChar == '\n' || tempChar == '\f' || tempChar == '\t') &&
- tempPos != beginningOfFilePos);
- InputFile.seekg(oldpos); // get back to old position.
- if (tempChar == ')') return true;
- }
- }
- return false;
- }
- void OCompiler::SyntacticAnalys()
- {
- do {
- InputFile.get(tempChar);
- switch (tempChar)
- {
- case ';': // Is it a data type or a "regular" command?
- tempToken.push_back(tempChar);
- case1:
- for (i = 0; tempToken[i] == ' '; ++i); // jump over all the space to the code begins.
- for (; tempToken[i] != ' ' && tempToken[i] != ';' && tempToken[i] != '('; ++i) tempToken_stemComparing.push_back(tempToken[i]);
- if (!tempToken_stemComparing.compare("if"))
- {
- ParseTree.push_back(1);
- tempToken_stemComparing.clear();
- tempToken.erase(0, i); // Erase the "if" part.
- i = 0;
- while (tempToken[i++] == ' ');
- tempToken_stemComparing.push_back('(');
- for (k = 1; k; i++)
- {
- tempToken_stemComparing.push_back(tempToken[i]);
- if (tempToken[i] == '(') ++k;
- if (tempToken[i] == ')' && k) --k;
- }
- ControlStatementDeque.push_back(tempToken_stemComparing);
- tempToken_stemComparing.clear();
- tempToken.erase(0, i);
- goto case1;
- }
- else if (!tempToken_stemComparing.compare("^"))
- {
- for (i = 0; tempToken[i] == ' '; ++i);
- tempToken.erase(0, i);
- ParseTree.push_back(0);
- OperationDeque.push_back("o = 1;");
- }
- else if (!tempToken_stemComparing.compare("v"))
- {
- for (i = 0; tempToken[i] == ' '; ++i);
- tempToken.erase(0, i);
- ParseTree.push_back(0);
- OperationDeque.push_back("o = 0;");
- }
- else
- {
- for (i = 0; i < CDataTypesSize && tempToken_stemComparing.compare(CDataTypes[i]); ++i);
- if (i < CDataTypesSize) // Is it a data type?
- {
- for (i = 0; tempToken[i] == ' '; ++i);
- tempToken.erase(0, i);
- DataDeque.push_back(tempToken);
- }
- else // It is just a regular command.
- {
- for (i = 0; tempToken[i] == ' '; ++i);
- tempToken.erase(0, i);
- ParseTree.push_back(0);
- OperationDeque.push_back(tempToken);
- }
- }
- tempToken_stemComparing.clear();
- tempToken.clear();
- break;
- case '{': // Add new code block.
- tempToken.push_back(tempChar);
- for (i = 0; tempToken[i] == ' '; ++i);
- for (; tempToken[i] != ' ' && tempToken[i] != '{' && tempToken[i] != '('; ++i) tempToken_stemComparing.push_back(tempToken[i]);
- if (!tempToken_stemComparing.compare("if"))
- {
- ParseTree.push_back(1);
- tempToken_stemComparing.clear();
- tempToken.erase(0, i); // Erase the "if" part.
- i = 0;
- while (tempToken[i++] == ' ');
- tempToken_stemComparing.push_back('(');
- for (k = 1; k; i++)
- {
- tempToken_stemComparing.push_back(tempToken[i]);
- if (tempToken[i] == '(') ++k;
- if (tempToken[i] == ')' && k) --k;
- }
- ControlStatementDeque.push_back(tempToken_stemComparing);
- tempToken_stemComparing.clear();
- tempToken.erase(0, i);
- }
- ParseTree.push_back(2);
- ++g_loopID;
- CodeBlockVector.emplace_back(g_loopID); // Creat new code block object in the vector at the end.
- CodeBlockVector.back().SyntacticAnalys(); // Call the procedure in the code block object to handel code block syntactic analys.
- tempToken_stemComparing.clear();
- tempToken.clear();
- break;
- default:
- tempToken.push_back(tempChar);
- case '\r':
- case '\n':
- case '\f':
- case '\t':
- break;
- }
- } while (tempChar != '}');
- tempToken.clear();
- }
- void OCompiler::TranslateCodeBlock()
- { // 0 = command, 1 = if(), 2 = code block
- while (!DataDeque.empty()) // Print all variabals.
- {
- PrintNumberOfTabs();
- OutputFile << "\t" << DataDeque.front() << "\n";
- DataDeque.pop_front();
- }
- //====================================
- PrintNumberOfTabs();
- OutputFile << "\tlong long int c = 0;\n";
- PrintNumberOfTabs();
- OutputFile << "\t\n";
- PrintNumberOfTabs();
- OutputFile << "\tstart" << loopID << ":\n";
- PrintNumberOfTabs();
- OutputFile << "\tswitch(c)\n";
- PrintNumberOfTabs();
- OutputFile << "\t{\n";
- int q = 0; // case number.
- for (deque<char>::iterator it = ParseTree.begin(), end = ParseTree.end(); it != end; ++it, ++q)
- {
- PrintNumberOfTabs();
- OutputFile << "\t\tcase " << q << ":\n";
- PrintNumberOfTabs();
- switch (*it)
- {
- case 0: // commands
- OutputFile << "\t\t\t" << OperationDeque.front() << "\n";
- OperationDeque.pop_front();
- break;
- case 1: // if
- OutputFile << "\t\t\tif (!" << ControlStatementDeque.front() << ")\n";
- PrintNumberOfTabs();
- OutputFile << "\t\t\t{\n";
- PrintNumberOfTabs();
- OutputFile << "\t\t\t\tif (o) c--;\n";
- PrintNumberOfTabs();
- OutputFile << "\t\t\t\telse c++;\n";
- PrintNumberOfTabs();
- OutputFile << "\t\t\t}\n";
- ControlStatementDeque.pop_front();
- break;
- case 2: // code block
- OutputFile << "\t\t\t{\n";
- l+=3;
- CodeBlockVector.front().TranslateCodeBlock();
- CodeBlockVector.erase(CodeBlockVector.begin());
- l-=3;
- OutputFile << "\n";
- default:
- break;
- }
- PrintNumberOfTabs();
- OutputFile << "\t\t\tbreak;\n";
- }
- PrintNumberOfTabs();
- OutputFile << "\t}\n";
- PrintNumberOfTabs();
- OutputFile << "\t\n";
- PrintNumberOfTabs();
- OutputFile << "\tif(o) { --c; if (c < 0) c = (c * -1); }\n";
- PrintNumberOfTabs();
- OutputFile << "\telse { ++c; if (c > " << q << ") c = c - " << q << "; };\n";
- PrintNumberOfTabs();
- OutputFile << "\t";
- if (loopID) OutputFile << "if (c < " << q << " && c > 0) ";
- OutputFile << "goto start" << loopID << ";\n";
- PrintNumberOfTabs();
- OutputFile << "}";
- //====================================
- DataDeque.clear();
- OperationDeque.clear();
- ControlStatementDeque.clear();
- CodeBlockVector.clear();
- ParseTree.clear();
- }
- void OCompiler::PrintNumberOfTabs()
- {
- for (int N = l; N; --N)
- OutputFile << "\t";
- }
Advertisement
Add Comment
Please, Sign In to add comment