Ejemplo n.º 1
0
  /*
    The purpose of this method is to replace the ASTIfElseNode
    given by the var nodeNumber with the new ASTIfNode
    and to add the remianing list of bodies after this ASTIfNode

    The new body is then returned;

  */
  public List<Object> createNewBody(List<Object> oldSubBody, int nodeNumber) {
    if (newIfNode == null) return null;

    List<Object> newSubBody = new ArrayList<Object>();

    if (oldSubBody.size() <= nodeNumber) {
      // something is wrong since the oldSubBody has lesser nodes than nodeNumber
      return null;
    }

    Iterator<Object> oldIt = oldSubBody.iterator();
    int index = 0;
    while (index != nodeNumber) {
      newSubBody.add(oldIt.next());
      index++;
    }

    // check to see that the next is an ASTIfElseNode
    ASTNode temp = (ASTNode) oldIt.next();
    if (!(temp instanceof ASTIfElseNode)) return null;

    newSubBody.add(newIfNode);

    newSubBody.addAll(remainingBody);

    // copy any remaining nodes
    while (oldIt.hasNext()) {
      newSubBody.add(oldIt.next());
    }

    return newSubBody;
  }
Ejemplo n.º 2
0
  /*
    We know this method is called when there is a loop node which has a body
    consisting entirely of one ASTIfElseNode
  */
  public static List getNewNode(ASTNode loopNode, ASTIfElseNode ifElseNode) {
    // make sure that elsebody has only a stmtseq node
    List elseBody = ((ASTIfElseNode) ifElseNode).getElseBody();
    if (elseBody.size() != 1) {
      // this is more than one we need one stmtSeq Node
      return null;
    }
    ASTNode tempNode = (ASTNode) elseBody.get(0);
    if (!(tempNode instanceof ASTStatementSequenceNode)) {
      // not a stmtSeq
      return null;
    }

    List statements = ((ASTStatementSequenceNode) tempNode).getStatements();
    Iterator stmtIt = statements.iterator();
    while (stmtIt.hasNext()) {
      AugmentedStmt as = (AugmentedStmt) stmtIt.next();
      Stmt stmt = as.get_Stmt();
      if (stmt instanceof DAbruptStmt) {
        // this is a abrupt stmt
        DAbruptStmt abStmt = (DAbruptStmt) stmt;
        if (!(abStmt.is_Break())) {
          // we need a break
          return null;
        } else {
          if (stmtIt.hasNext()) {
            // a break should be the last stmt
            return null;
          }
          SETNodeLabel label = abStmt.getLabel();
          String labelBroken = label.toString();
          String loopLabel = ((ASTLabeledNode) loopNode).get_Label().toString();
          if (labelBroken != null && loopLabel != null) { // stmt breaks some label
            if (labelBroken.compareTo(loopLabel) == 0) {
              // we have found a break breaking this label

              // make sure that if the orignal was an ASTWhileNode then there was
              // ONLY a break statement
              if (loopNode instanceof ASTWhileNode) {
                if (statements.size() != 1) {
                  // more than 1 statement
                  return null;
                }
              }

              // pattern matched
              ASTWhileNode newWhileNode = makeWhileNode(ifElseNode, loopNode);
              if (newWhileNode == null) {
                return null;
              }
              List toReturn = new ArrayList();
              toReturn.add(newWhileNode);

              //  Add the statementSequenceNode AFTER the whileNode except for the laststmt
              if (statements.size() != 1) {
                // size 1 means that the only stmt is a break stmt

                Iterator tempIt = statements.iterator();
                List newStmts = new ArrayList();
                while (tempIt.hasNext()) {
                  Object tempStmt = tempIt.next();
                  if (tempIt.hasNext()) {
                    newStmts.add(tempStmt);
                  }
                }
                toReturn.add(new ASTStatementSequenceNode(newStmts));
              }
              return toReturn;
            } // labels matched
          } // non null labels
        } // end of break stmt
      } // stmt is an abrupt stmt
      else if (stmt instanceof ReturnStmt || stmt instanceof ReturnVoidStmt) {
        if (!(loopNode instanceof ASTUnconditionalLoopNode)) {
          // this pattern is only possible for while(true)
          return null;
        }

        if (stmtIt.hasNext()) {
          // returns should come in the end
          return null;
        }

        // pattern matched
        ASTWhileNode newWhileNode = makeWhileNode(ifElseNode, loopNode);
        if (newWhileNode == null) {
          return null;
        }
        List toReturn = new ArrayList();
        toReturn.add(newWhileNode);

        //  Add the statementSequenceNode AFTER the whileNode
        Iterator tempIt = statements.iterator();
        List newStmts = new ArrayList();
        while (tempIt.hasNext()) {
          Object tempStmt = tempIt.next();
          newStmts.add(tempStmt);
        }
        toReturn.add(new ASTStatementSequenceNode(newStmts));
        return toReturn;
      } // if stmt was a return stmt
    } // going through the stmts
    return null;
  } // end of method