コード例 #1
0
  // While loop handles the basic cases like declare while loop for i<10, iterate until i less than
  // ten and j less than 20
  private void while_loop() {
    VarSymbol v = null;
    FuncSymbol f = null;
    int index =
        input.indexOf(
            words.get(wordLoc)); // Index in input string at which the word was encountered
    index += (words.get(wordLoc).length() + 1);
    String str = input.substring(index); // Further processing will be done on this string
    tokens = str.split("[ ]+");
    String expString = "";

    tIndex = 0;
    while (true) {
      if ((v = Controller.varSym.searchSymbol(tokens[tIndex]))
          != null /*TODO Check whether token is a number*/) {
        expString = searchExpression();
        if (words.get(wordLoc).indexOf("until") != -1)
          code.setCodeText("while(!(" + expString + "))");
        else code.setCodeText("while(" + expString + ")");
        code.setErrCode(IErrorCodes.SUCCESS);
        code.setErrParam(null);
        break;
      } else if ((f = Controller.funSym.searchSymbol(tokens[tIndex])) != null) {
        int tempIndex = input.indexOf(tokens[tIndex]);
        expString = exp.generateExpression(input.substring(tempIndex));
        if (words.get(wordLoc).indexOf("until") != -1)
          code.setCodeText("while(!(" + expString + "))");
        else code.setCodeText("while(" + expString + ")");
        code.setErrCode(IErrorCodes.SUCCESS);
        code.setErrParam(null);
        break;
      } else if ((tokens[tIndex].equals("true") || tokens[tIndex].equals("true"))
          && tIndex + 1 == tokens.length) {
        code.setCodeText("while(" + tokens[tIndex] + ")");
      }
    }
  }
コード例 #2
0
  private void for_loop() {
    VarSymbol v = null, tempVar = null;
    FuncSymbol f = null, tempFunc = null;
    int index =
        input.indexOf(
            words.get(wordLoc)); // Index in input string at which the word was encountered
    String expString = "";
    index += (words.get(wordLoc).length() + 1);
    String str = input.substring(index); // Further processing will be done on this string
    tokens = str.split("[ ]+");
    index = 0;
    boolean flag = false;
    String expression = null;
    String tempStr = null;
    String loopCounter = "";

    while (true) {
      if ((v = Controller.varSym.searchSymbol(tokens[tIndex]))
          != null) /*|| (f = Controller.funSym.searchSymbol(tokens[tIndex])) != null*/ {
        loopCounter = v.getVarName(); // Holds the loopCounter variable
        expString = searchExpression();
        expression = exp.generateExpression(expString);
        tempVar = v;
      } else if (tokens[tIndex].equalsIgnoreCase("to")) { // If next token is not an operator
        loopParams[0] = expression;
        tIndex++;
        int tempNum = NumberGenerator.stringToNum(tokens[tIndex]);
        // if((tempStr = String.valueOf(toNumber(tokens[tIndex++])))){			// TODO Method 'toNumber()'
        // to be added later
        if (String.valueOf(tempNum) != null) {
          condition = tempStr;
          loopParams[1] = loopCounter + "<" + condition;
        } else if ((v = Controller.varSym.searchSymbol(tokens[tIndex])) != null
            || (Controller.funSym.searchSymbol(tokens[tIndex]) != null)) {
          tempStr = searchExpression();
          String tempString = exp.generateExpression(tempStr);
          int len = v.getVarName().length();
          if (tempString.startsWith(loopCounter) && tempString.charAt(len) == '=') {
            condition = tempString.substring(len + 1);
            loopParams[1] = loopCounter + "<" + condition;
          } else {
            condition = tempString;
            loopParams[1] = condition;
          }
        }
      } else if (tokens[tIndex].equalsIgnoreCase("increment")
          || (tokens[tIndex] + tokens[tIndex + 1]).equalsIgnoreCase("plusplus")
          || tokens[tIndex].equalsIgnoreCase("postincrement")
          || tokens[tIndex].equalsIgnoreCase("preincrement")) {
        expression = exp.generateExpression((str.substring(str.indexOf(tokens[tIndex]))));
        loopParams[2] = expression;
        break;
      } else if (tokens[tIndex].equalsIgnoreCase("decrement")
          || (tokens[tIndex] + tokens[tIndex + 1]).equalsIgnoreCase("minus minus")
          || tokens[tIndex].equalsIgnoreCase("postdecrement")
          || tokens[tIndex].equalsIgnoreCase("predecrement")) {
        expression = exp.generateExpression((str.substring(str.indexOf(tokens[tIndex]))));
        loopParams[2] = expression;
        break;
      }
      /*if(searchOperator(tIndex) != -1){
      	//TODO Index variable missing error to be handled
      }*/
      /*
      if(token is number){
      	number handling logic.
      	e.g : for loop 0 to 100.
      }
      */
      if ((tIndex) == tokens.length) {
        break;
      }
    }
    if (loopParams[2] == null) {
      loopParams[2] = loopCounter + "++";
    }
    code.setCodeText("for(" + loopParams[0] + ";" + loopParams[1] + ";" + loopParams[2] + ")");
    code.setErrCode(IErrorCodes.SUCCESS);
    code.setErrParam(null);
  }