main.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #include "executable_semantics/main.h"
  5. #include <unistd.h>
  6. #include <cstdio>
  7. #include <cstring>
  8. #include <iostream>
  9. #include <string>
  10. #include <vector>
  11. #include "common/error.h"
  12. #include "executable_semantics/common/arena.h"
  13. #include "executable_semantics/common/nonnull.h"
  14. #include "executable_semantics/interpreter/exec_program.h"
  15. #include "executable_semantics/syntax/parse.h"
  16. #include "executable_semantics/syntax/prelude.h"
  17. #include "llvm/Support/CommandLine.h"
  18. #include "llvm/Support/InitLLVM.h"
  19. namespace Carbon {
  20. namespace cl = llvm::cl;
  21. static auto Main(llvm::StringRef default_prelude_file, int argc, char* argv[])
  22. -> ErrorOr<Success> {
  23. llvm::setBugReportMsg(
  24. "Please report issues to "
  25. "https://github.com/carbon-language/carbon-lang/issues and include the "
  26. "crash backtrace.\n");
  27. llvm::InitLLVM init_llvm(argc, argv);
  28. // Printing to stderr should flush stdout. This is most noticeable when stderr
  29. // is piped to stdout.
  30. llvm::errs().tie(&llvm::outs());
  31. cl::opt<bool> trace_option("trace", cl::desc("Enable tracing"));
  32. cl::opt<std::string> input_file_name(cl::Positional, cl::desc("<input file>"),
  33. cl::Required);
  34. // Find the path of the executable if possible and use that as a relative root
  35. cl::opt<std::string> prelude_file_name("prelude", cl::desc("<prelude file>"),
  36. cl::init(default_prelude_file.str()));
  37. cl::ParseCommandLineOptions(argc, argv);
  38. Arena arena;
  39. ASSIGN_OR_RETURN(AST ast, Parse(&arena, input_file_name, trace_option));
  40. AddPrelude(prelude_file_name, &arena, &ast.declarations);
  41. // Typecheck and run the parsed program.
  42. ASSIGN_OR_RETURN(int unused_return_code,
  43. ExecProgram(&arena, ast, trace_option));
  44. (void)unused_return_code;
  45. return Success();
  46. }
  47. auto ExecutableSemanticsMain(llvm::StringRef default_prelude_file, int argc,
  48. char** argv) -> int {
  49. if (auto result = Main(default_prelude_file, argc, argv); !result.ok()) {
  50. llvm::errs() << result.error().message() << "\n";
  51. return EXIT_FAILURE;
  52. }
  53. return EXIT_SUCCESS;
  54. }
  55. } // namespace Carbon