Advertisement
Guest User

Untitled

a guest
Nov 11th, 2019
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. if (lexemsTable[index] === "T_FOR") {
  2.     if (lexemsTable[index + 1] === "T_VARIABLE") {
  3.       if (lexemsTable[index + 2] === "T_ASSIGNMENT") {
  4.         if (
  5.           lexemsTable[index + 3] !== "T_VARIABLE" &&
  6.           lexemsTable[index + 3] !== "T_INTEGER" &&
  7.           lexemsTable[index + 3] !== "T_LEFT_PARENTHESIS"
  8.         ) {
  9.           errorOccured = true;
  10.           console.log(
  11.             `ERROR at ${
  12.               lexemsIndexes[index + 3]
  13.             } index : You can't use ${lexemsTable[index + 3].substring(2,30)} after ASSIGNMENT OPERATOR`
  14.          );
  15.        }
  16.        
  17.        if (lexemsTable[index + 3] === "T_LEFT_PARENTHESIS") {
  18.          index = findParenthesisRightPair(index + 3);
  19.        }else if (
  20.          lexemsTable[index + 3] === "T_VARIABLE" &&
  21.          lexemsTable[index + 4] === "T_LEFT_PARENTHESIS"
  22.        ) {
  23.          index = findParenthesisRightPair(index + 4);
  24.        } else if (
  25.          lexemsTable[index + 3] === "T_VARIABLE" &&
  26.          lexemsTable[index + 4] === "T_LEFT_BRACKET"
  27.        ) {
  28.          index = findBracketRightPair(index + 4);
  29.        } else {
  30.          const toIndex = lexemsTable.indexOf("T_TO", index);
  31.          const contentBetweenForAndTo = checkContentBetweenKeywords(index+3, toIndex, 0);
  32.          if(contentBetweenForAndTo){
  33.            errorOccured = true;
  34.            console.log(contentBetweenForAndTo)
  35.          }
  36.          index = toIndex - 1;
  37.        }
  38.  
  39.  
  40.        if (lexemsTable[index + 1] !== "T_TO") {
  41.          console.log(index);
  42.          errorOccured = true;
  43.          console.log(
  44.            `ERROR at ${
  45.              lexemsIndexes[index + 1]
  46.            } index : You have to place 'TO' here`
  47.          );
  48.        } else if (lexemsTable[index + 2] === "T_DO") {
  49.          errorOccured = true;
  50.          console.log(
  51.            `ERROR at ${
  52.              lexemsIndexes[index + 2]
  53.            } index : You have to define the end of the range after 'TO' here`
  54.          );
  55.        } else if (lexemsTable[index + 2] === "T_TO") {
  56.          errorOccured = true;
  57.          console.log(
  58.            `ERROR at ${
  59.              lexemsIndexes[index + 2]
  60.            } index : You can't place another 'TO' after 'TO' here`
  61.           );
  62.         }else{
  63.           const doIndex = lexemsTable.indexOf("T_DO", index + 1);
  64.           const contentBetweenToAndDo = checkContentBetweenKeywords(index+2, doIndex, 1);
  65.           if(contentBetweenToAndDo){
  66.             errorOccured = true;
  67.             console.log(contentBetweenToAndDo)
  68.           }
  69.           if(doIndex !== -1){
  70.           } else{
  71.             errorOccured = true;
  72.               console.log(
  73.                 `ERROR at ${
  74.                   lexemsIndexes[index + 1]
  75.                 } index : You have to place "DO" after "TO"`
  76.               );
  77.           }
  78.          
  79.           index = doIndex+2
  80.         }
  81.       }else{
  82.         errorOccured = true;
  83.         console.log(
  84.           `ERROR at ${lexemsIndexes[index]} index : You have to initialize iterator's value here`
  85.        );
  86.      }
  87.    }else{
  88.      errorOccured = true;
  89.      console.log(
  90.        `ERROR at ${lexemsIndexes[index]} index : You have to place iterator here`
  91.      );
  92.    }
  93.  }
  94.  
  95.  if(lexemsTable[index] === "T_IF"){
  96.    ifAmount++;
  97.    if (lexemsTable[index + 1] === "T_IF"){
  98.      errorOccured = true;
  99.      console.log(
  100.        `ERROR at ${lexemsIndexes[index+1]} index : You can't place another 'IF' here`
  101.       );
  102.     }
  103.     const thenIndex = lexemsTable.indexOf("T_THEN", index);
  104.     if(thenIndex === -1 ){
  105.       errorOccured = true;
  106.       console.log(
  107.         `ERROR at ${lexemsIndexes[index+1]} index : Expected 'THEN'`
  108.       );
  109.     }
  110.     console.log(thenIndex)
  111.     console.log(index)
  112.     if(thenIndex - index === 1 ){
  113.       errorOccured = true;
  114.       console.log(
  115.         `ERROR at ${lexemsIndexes[index+1]} index : You have to write your condition`
  116.       );
  117.     }
  118.     if(lexemsTable[thenIndex - 1] === "T_SEMICOLUMN"){
  119.       errorOccured = true;
  120.       console.log(
  121.         `ERROR at ${lexemsIndexes[index+1]} index : Expected 'THEN' got ';'`
  122.       );
  123.     }
  124.     const contentBetweenIfAndThen = checkContentBetweenKeywords(index+1, thenIndex, 2);
  125.       if(contentBetweenIfAndThen){
  126.         errorOccured = true;
  127.         console.log(contentBetweenIfAndThen)
  128.       }
  129.     index = thenIndex
  130.   }
  131.  
  132.   if (lexemsTable[index] === "T_ELSE") {
  133.     if (ifAmount > 0) {
  134.      
  135.     }else{
  136.       errorOccured = true;
  137.       console.log(
  138.         `ERROR at ${lexemsIndexes[index]} index : You have to use 'IF' before 'ELSE'`
  139.       );
  140.     }
  141.     if (lexemsTable[index - 1] !== "T_END") {
  142.       errorOccured = true;
  143.       console.log(
  144.         `ERROR at ${lexemsIndexes[index]} index : You have to use 'ELSE' after 'END'`
  145.       );
  146.     }
  147.  
  148.     const closestIf = findClosestLeftIf(index);
  149.     const closestThen = findClosestThen(closestIf);
  150.     const closestBegin = closestThen + 1;
  151.     const correspondingEnd = findEnd(closestBegin);
  152.     if (index - 1 !== correspondingEnd){
  153.       errorOccured = true;
  154.       console.log(
  155.         `ERROR at ${lexemsIndexes[index]} index : You have to use 'ELSE' after 'IF' construction`
  156.       );
  157.     }
  158.  
  159.    
  160.     if (lexemsTable[index + 1] !== "T_BEGIN") {
  161.       errorOccured = true;
  162.       console.log(
  163.         `ERROR at ${lexemsIndexes[index]} index : You have to open new logic statement after 'ELSE'`
  164.       );
  165.     }
  166.   }
  167.  
  168.   if (lexemsTable[index] === "T_THEN") {
  169.     errorOccured = true;
  170.     console.log(
  171.       `ERROR at ${lexemsIndexes[index]} index : You can't place 'THEN' here`
  172.    );
  173.  }
  174.  if (lexemsTable[index] === "T_TO") {
  175.    errorOccured = true;
  176.    console.log(
  177.      `ERROR at ${lexemsIndexes[index]} index : You can't place 'TO' here`
  178.     );
  179.   }
  180.   if (lexemsTable[index] === "T_DO") {
  181.     errorOccured = true;
  182.     console.log(
  183.       `ERROR at ${lexemsIndexes[index]} index : You can't place 'DO' here`
  184.    );
  185.  }
  186.  if (!errorOccured && index <= lexemsTable.length) {
  187.    checkSyntax(lexemsTable, index + 1);
  188.  }
  189.  if (!errorOccured && index > lexemsTable.length) {
  190.    console.log(`\n~~~~~~~~~~Anylize completed successfully~~~~~~~~~~`);
  191.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement