コード例 #1
0
ファイル: Shell.java プロジェクト: dbremner/fortress
  /**
   * @param out
   * @param path
   * @param file_name
   * @param doLink
   * @return
   * @throws UserError
   * @throws IOException
   */
  private static int compileWithErrorHandling(String s, Option<String> out, boolean doLink)
      throws UserError, IOException {
    int return_code = 0;
    try {
      APIName name = NodeUtil.apiName(s);
      Path path = sourcePath(s, name);
      String file_name = name.toString() + (s.endsWith(".fss") ? ".fss" : ".fsi");

      List<StaticError> errors = new ArrayList<StaticError>();
      for (StaticError error :
          IterUtil.sort(compilerPhases(path, file_name, out, doLink), StaticError.comparator)) {
        if (!error.toString().equals("")) errors.add(error);
      }
      return_code = reportErrors(errors, file_name);
    } catch (StaticError e) {
      return_code = reportErrors(flattenErrors(e), new File(s).getName());
    } catch (ProgramError e) {
      System.err.println(e.getMessage());
      e.printInterpreterStackTrace(System.err);
      if (Debug.stackTraceOn()) {
        e.printStackTrace();
      } else {
        System.err.println(turnOnDebugMessage);
      }
      return_code = 1;
    }
    return return_code;
  }
コード例 #2
0
  private void compileTestProgram(String testFileName) throws UserError {
    Path path = ProjectProperties.SOURCE_PATH;
    String s = ProjectProperties.BASEDIR + "tests" + File.separator + testFileName;

    //        System.err.println("compileTestProgram(" + s + ")");

    File file = new File(s);
    s = file.getPath();

    if (s.contains(File.separator)) {
      String head = s.substring(0, s.lastIndexOf(File.separator));
      s = s.substring(s.lastIndexOf(File.separator) + 1, s.length());
      path = path.prepend(head);
    }

    // HACK: We need to compile these test programs using the old Fortress
    // libraries instead of the new compiler libraries.
    // Shell.useFortressLibraries();

    Iterable<? extends StaticError> errors = Shell.compilerPhases(path, s);

    for (StaticError error : errors) {
      fail(error.toString());
    }
  }
コード例 #3
0
ファイル: Shell.java プロジェクト: dbremner/fortress
  private static void walkTests(List<String> args, boolean verbose)
      throws UserError, IOException, Throwable {
    boolean _verbose = verbose;
    if (args.size() == 0) {
      throw new UserError("Need a file to run through the Fortress interpreter.");
    }
    String s = args.get(0);
    List<String> rest = args.subList(1, args.size());

    if (s.startsWith("-")) {
      if (s.equals("-debug")) {
        rest = Debug.parseOptions(rest);
      } else if (s.equals("-verbose")) {
        _verbose = true;
      } else invalidFlag(s, "test");
      walkTests(rest, _verbose);
    } else {
      for (String file : args) {
        try {
          Iterable<? extends StaticError> errors = IterUtil.empty();
          try {
            if (!isComponent(file)) throw new UserError(file + " is not a component file.");
            APIName name = NodeUtil.apiName(file);
            Path path = sourcePath(file, name);
            GraphRepository bcr = specificInterpreterRepository(path);
            ComponentIndex cu = bcr.getLinkedComponent(name);
            Driver.runTests(bcr, cu, _verbose);
          } catch (Throwable th) {
            // TODO FIXME what is the proper treatment of errors/exceptions etc.?
            if (th instanceof FortressException) {
              FortressException pe = (FortressException) th;
              if (pe.getStaticErrors() != null) errors = pe.getStaticErrors();
            }
            if (th instanceof RuntimeException) throw (RuntimeException) th;
            if (th instanceof Error) throw (Error) th;
            throw new WrappedException(th, Debug.stackTraceOn());
          }

          for (StaticError error : errors) {
            System.err.println(error);
          }
          // If there are no errors,
          // all components will have been written to disk
          // by the CacheBasedRepository.
        } catch (StaticError e) {
          System.err.println(e);
          if (Debug.stackTraceOn()) {
            e.printStackTrace();
          }
        } catch (RepositoryError e) {
          System.err.println(e.getMessage());
        } catch (FortressException e) {
          failureBoilerplate(e);
          System.exit(1);
        }
      }
    }
  }
コード例 #4
0
ファイル: Shell.java プロジェクト: dbremner/fortress
 public static void assertStaticErrors(Iterable<? extends StaticError> errors, String expected)
     throws UserError {
   errors = IterUtil.sort(errors, StaticError.comparator);
   StringBuilder buf = new StringBuilder();
   for (StaticError error : errors) {
     buf.append(error.getMessage() + "\n");
   }
   String message = buf.toString();
   message = message.substring(0, message.length() - 1);
   if (!message.equals(expected))
     throw new UserError("Bad error message: " + message + "\n" + "Should be: " + expected);
 }
コード例 #5
0
ファイル: Shell.java プロジェクト: dbremner/fortress
  private static void walk(String fileName, List<String> args) throws UserError, Throwable {
    try {
      Iterable<? extends StaticError> errors = IterUtil.empty();
      try {
        eval(fileName, args, false);
      } catch (Throwable th) {
        // TODO FIXME what is the proper treatment of errors/exceptions etc.?
        if (th instanceof FortressException) {
          FortressException pe = (FortressException) th;
          if (pe.getStaticErrors() != null) errors = pe.getStaticErrors();
        }
        if (th instanceof RuntimeException) throw (RuntimeException) th;
        if (th instanceof Error) throw (Error) th;
        throw new WrappedException(th, Debug.stackTraceOn());
      }

      if (!IterUtil.isEmpty(errors)) {
        for (StaticError error : errors) {
          System.err.println(error);
        }
        System.exit(-1);
      }
      // If there are no errors,
      // all components will have been written to disk
      // by the CacheBasedRepository.
    } catch (StaticError e) {
      System.err.println(e);
      if (Debug.stackTraceOn()) {
        e.printStackTrace();
      }
      System.exit(-1);
    } catch (RepositoryError e) {
      throw e;
    } catch (LabelException e) {
      System.err.println(e.getMessage());
      if (Debug.stackTraceOn()) {
        e.printStackTrace();
      } else {
        System.err.println(turnOnDebugMessage);
      }
      System.exit(1);
    } catch (FortressException e) {
      failureBoilerplate(e);
      System.exit(1);
    }
  }
コード例 #6
0
ファイル: Shell.java プロジェクト: dbremner/fortress
  /**
   * @param tokens
   * @return
   * @throws InterruptedException
   * @throws Throwable
   */
  public static int subMain(String[] tokens) throws InterruptedException, Throwable {
    int return_code = 0;

    // Now match the assembled string.
    try {
      String what = tokens[0];
      List<String> args = Arrays.asList(tokens).subList(1, tokens.length);
      if (what.equals("compile")) {
        useCompilerLibraries();
        setTypeChecking(true);
        setPhaseOrder(PhaseOrder.compilerPhaseOrder);
        return_code = compilerPhases(args, Option.<String>none(), what);
      } else if (what.equals("junit")) {
        return_code = junit(args);
      } else if (what.equals("link")) {
        useCompilerLibraries();
        setTypeChecking(true);
        setPhaseOrder(PhaseOrder.compilerPhaseOrder);
        return_code = link(args);
      } else if (what.equals("build")) {
        useCompilerLibraries();
        setTypeChecking(true);
        setPhaseOrder(PhaseOrder.compilerPhaseOrder);
        return_code = link(args);
      } else if (what.equals("walk")) {
        useInterpreterLibraries();
        setScala(false);
        setPhaseOrder(PhaseOrder.interpreterPhaseOrder);
        walk(args);
      } else if (what.equals("api")) {
        useCompilerLibraries();
        api(args, Option.<String>none(), Option.<String>none());
      } else if (what.equals("compare")) {
        useCompilerLibraries();
        compare(args);
      } else if (what.equals("parse")) {
        useCompilerLibraries();
        return_code = parse(args, Option.<String>none());
      } else if (what.equals("unparse")) {
        useCompilerLibraries();
        unparse(args, Option.<String>none(), false, false);
      } else if (what.equals("disambiguate")) {
        useCompilerLibraries();
        setPhaseOrder(PhaseOrder.disambiguatePhaseOrder);
        return_code = compilerPhases(args, Option.<String>none(), what);
      } else if (what.equals("desugar")) {
        useCompilerLibraries();
        setTypeChecking(true);
        setObjExprDesugaring(true);
        setPhaseOrder(PhaseOrder.desugarPhaseOrder);

        return_code = compilerPhases(args, Option.<String>none(), what);
      } else if (what.equals("grammar")) {
        useCompilerLibraries();
        setPhaseOrder(PhaseOrder.grammarPhaseOrder);

        return_code = compilerPhases(args, Option.<String>none(), what);
      } else if (what.equals("typecheck")) {
        useCompilerLibraries();
        setTypeChecking(true);
        setPhaseOrder(PhaseOrder.typecheckPhaseOrder);

        return_code = compilerPhases(args, Option.<String>none(), what);
      } else if (what.equals("test-coercion")) {
        useCompilerLibraries();
        setTypeChecking(true);
        setPhaseOrder(PhaseOrder.typecheckPhaseOrder);

        return_code = compilerPhases(args, Option.<String>none(), what);
      } else if (what.equals("typecheck-old")) {
        useInterpreterLibraries();
        setScala(false);
        /* TODO: remove the next line once type checking is permanently turned on */
        setTypeChecking(true);
        setPhaseOrder(PhaseOrder.typecheckPhaseOrder);

        return_code = compilerPhases(args, Option.<String>none(), what);
      } else if (what.equals("test")) {
        useInterpreterLibraries();
        setScala(false);
        setPhaseOrder(PhaseOrder.interpreterPhaseOrder);
        walkTests(args, false);
      } else if (what.contains(ProjectProperties.COMP_SOURCE_SUFFIX)
          || (what.startsWith("-") && tokens.length > 1)) {
        useInterpreterLibraries();
        setScala(false);
        setPhaseOrder(PhaseOrder.interpreterPhaseOrder);
        walk(Arrays.asList(tokens));
      } else if (what.equals("help")) {
        useCompilerLibraries();
        printHelpMessage();
      } else if (what.equals("expand") && tokens.length == 2) {
        System.out.println(ProjectProperties.get(tokens[1], ""));
      } else {
        useCompilerLibraries();
        printUsageMessage();
      }
    } catch (StaticError e) {
      System.err.println(e);
      if (Debug.stackTraceOn()) {
        e.printStackTrace();
      }
      return_code = -1;
    } catch (UserError error) {
      System.err.println(error.getMessage());
      return_code = -1;
    } catch (IOException error) {
      System.err.println(error.getMessage());
      return_code = -2;
    } catch (CompilerBug error) {
      System.err.println(error.getMessage());
      if (Debug.stackTraceOn()) {
        error.printStackTrace();
      }
      return_code = -3;
    }

    Init.allowForLeakChecks();
    return return_code;
  }