Пример #1
0
  public Object visit(If ifStatement) {
    Object temp;
    temp = ifStatement.getCondition().accept(this);
    if (temp != null) return temp;
    temp = ifStatement.getOperation().accept(this);
    if (temp != null) return temp;
    if (ifStatement.hasElse()) {
      temp = ifStatement.getElseOperation().accept(this);
      if (temp != null) return temp;
    }

    if (!Type.isBool(ifStatement.getCondition())) {
      return ifStatement;
    }
    return null;
  }
Пример #2
0
  /**
   * check if every control path has a return value
   *
   * @throws SemanticError
   */
  public boolean hasReturnOnControlPaths(List<Statement> statements) throws SemanticError {

    // initialize return value
    boolean found = false;

    for (Statement stmt : statements) {

      if (stmt instanceof Return) {

        // in case we found a return statement, set found to true
        found = true;
      }

      if (stmt instanceof StatementsBlock) {

        // in case we found a statement block, iterate all statements
        found |= hasReturnOnControlPaths(((StatementsBlock) stmt).getStatements());
      }

      if (stmt instanceof If) {

        /*
         * in case we found an if statement, iterate recursively the
         * 'then' statement and the 'else' statement
         */
        If stmtAsIf = (If) stmt;

        if (stmtAsIf.getCondition() instanceof Literal) {
          Literal cond = (Literal) stmtAsIf.getCondition();
          if (cond.getType() == LiteralTypes.TRUE)
            found |= hasReturnOnControlPaths(stmtAsIf.getOperation());
          else if (cond.getType() == LiteralTypes.FALSE) {
            if (stmtAsIf.hasElse())
              found |= hasReturnOnControlPaths(((If) stmt).getElseOperation());
          } else throw new SemanticError("Unexpected literal in if statement", stmt);
        }

        found |=
            (hasReturnOnControlPaths(stmtAsIf.getOperation())
                && (stmtAsIf.hasElse()
                    ? hasReturnOnControlPaths(stmtAsIf.getElseOperation())
                    : false));
      }
    }

    return found;
  }