Пример #1
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;
  }
Пример #2
0
 public Object visit(Literal literal) {
   String bah = literal.getType().getDescription();
   if (bah.compareTo("Literal") == 0) {
     if (!Type.isNull(literal)) {
       return literal; // problem, types dont match
     }
   } else if (bah.compareTo("Boolean literal") == 0) {
     if (!Type.isBool(literal)) {
       return literal;
     }
   } else if (bah.compareTo("String literal") == 0) {
     if (!Type.isString(literal)) {
       return literal;
     }
   } else if (bah.compareTo("Integer literal") == 0) {
     if (!Type.isInt(literal)) {
       return literal;
     }
   }
   // Literal is correctly typed
   return null;
 }