Advertisement
Guest User

Untitled

a guest
Nov 30th, 2017
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "compiler.h";
  2.  
  3. //////////////////////////////////
  4. //// Constructor & Destructor ////
  5. //////////////////////////////////
  6. source::source(char *sourcename) {
  7.     fin.open(sourcename);
  8.     fillErrorArr();
  9.     if (!fin.is_open()) {
  10.         cerr << "*****   ERROR: Could not open file: "
  11.             << sourcename << endl;
  12.         exit(EXIT_FAILURE);
  13.     }
  14.  
  15.     cout << "LISTING PRODUCED BY MINI-PASCAL COMPILER  MPC-1";
  16.     cout << endl << endl;
  17.  
  18.     readnextline();
  19.  
  20.     positionnow.linenumber = 1;
  21.     positionnow.charnumber = firstinline;
  22.  
  23.     ch = line[positionnow.charnumber];
  24.  
  25.     errorcount = 0;
  26.     errinx = 0;
  27.     erroroverflow = false;
  28. } // source( )
  29.  
  30. source::~source() {
  31.     listthisline();
  32.  
  33.     cout << endl << endl;
  34.     cout << "COMPILATION COMPLETED :";
  35.     errorcount == 0 ? cout << " NO"
  36.         : cout << setw(5) << errorcount;
  37.     cout << " ERROR(S) REPORTED." << endl;
  38.  
  39.     fin.close();
  40. } // ~source( )
  41.  
  42.   //////////////////////////
  43.   //// Public Functions ////
  44.   //////////////////////////
  45. void source::nextch() {
  46.     if (positionnow.charnumber == lastinline) {
  47.         listthisline();
  48.         readnextline();
  49.  
  50.         positionnow.charnumber = firstinline;
  51.         positionnow.linenumber++;
  52.     }
  53.     else {
  54.         positionnow.charnumber++;
  55.     }
  56.  
  57.     ch = line[positionnow.charnumber];
  58. } // nextch( )
  59.  
  60. void source::error(int code, textposition position) {
  61.     if (errinx == errmax) {
  62.         erroroverflow = true;
  63.     }
  64.     else {
  65.         errlist[errinx].errorcode = code;
  66.         errlist[errinx].errorposition = position;
  67.         errinx++;
  68.     }
  69. } // error( )
  70.  
  71. void source::fillErrorArr()
  72. {
  73.     ifstream errFile;
  74.     int nIndex = 0;
  75.     string tmp = "";
  76.     errFile.open("errors.txt", fstream::in);
  77.  
  78.     if (!errFile.is_open()) {
  79.         return;
  80.     }
  81.  
  82.     while (1)
  83.     {
  84.         if ((nIndex + 1) >= 100) {
  85.             break; // FULL
  86.         }
  87.         getline(errFile, tmp);
  88.         errmsg[nIndex].nError = atoi(tmp.substr(0, tmp.find(' ', 1)).c_str());
  89.         errmsg[nIndex].strMsg = tmp.substr(tmp.find(' ', 3) + 1);
  90.         nIndex++;
  91.         if (errFile.eof()) {
  92.             break;
  93.         }
  94.     }
  95.  
  96.     errFile.close();
  97. }
  98.  
  99.  
  100. ///////////////////////////
  101. //// Private Functions ////
  102. ///////////////////////////
  103. void source::readnextline() {
  104.     char    inChar;
  105.     int     i = 0;
  106.  
  107.     firstinline = 0;
  108.  
  109.     while (fin.get(inChar) && (inChar != '\n')) {
  110.         line[i] = inChar;
  111.         i++;
  112.         if (i > maxline) {
  113.             cerr << "*****   ERROR: Line too long! Aborting..." << endl;
  114.             exit(EXIT_FAILURE);
  115.         }
  116.     }
  117.  
  118.     fin.eof() ? line[i] = 0
  119.         : line[i] = ' ';
  120.  
  121.     lastinline = i;
  122. } // readnextline( )
  123.  
  124. void source::listerrors() {
  125.     errorcount += errinx;
  126.     int nIndexOfErrStruct = 0;
  127.  
  128.     for (int k = 0; k<errinx; k++) {
  129.         for (int i = 0; i < 100; i++)
  130.         {
  131.             if (errlist[k].errorcode == errmsg[i].nError)
  132.             {
  133.                 nIndexOfErrStruct = i;
  134.                 break;
  135.             }
  136.         }
  137.         cout << "*****   ";
  138.         if (errlist[k].errorposition.linenumber != positionnow.linenumber) {
  139.             int tempVar = errlist[k].errorposition.charnumber - firstinline + 1;
  140.             cout << "ERROR"
  141.                 << setw(10) << errlist[k].errorcode
  142.                 << " AT CHARACTER"
  143.                 << setw(3) << tempVar
  144.                 << " OF LINE"
  145.                 << setw(6) << errlist[k].errorposition.linenumber;
  146.         }
  147.         else {
  148.             int tempVar = errlist[k].errorposition.charnumber - firstinline + 6;
  149.             cout << setw(tempVar) << "^ERROR"
  150.                 << setw(4) << errlist[k].errorcode << "[" << errmsg[nIndexOfErrStruct].strMsg << "]";
  151.         }
  152.         cout << endl;
  153.     }
  154.  
  155.     if (erroroverflow) {
  156.         cout << "*****   FURTHER ERRORS SUPPRESSED";
  157.         cout << endl;
  158.     }
  159.     cout << endl;
  160.  
  161.     errinx = 0;
  162.     erroroverflow = false;
  163. } // listerrors( )
  164.  
  165. void source::listthisline() {
  166.     cout << setw(5) << positionnow.linenumber << "   ";
  167.     for (int i = firstinline; i <= lastinline; i++) {
  168.         cout << line[i];
  169.     }
  170.     cout << endl;
  171.  
  172.     if (errinx > 0) {
  173.         listerrors();
  174.         cin.get();
  175.     }
  176. } // listthisline( )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement