main.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 <optional>
  10. #include <string>
  11. #include <vector>
  12. #include "common/error.h"
  13. #include "executable_semantics/common/arena.h"
  14. #include "executable_semantics/common/nonnull.h"
  15. #include "executable_semantics/interpreter/exec_program.h"
  16. #include "executable_semantics/syntax/parse.h"
  17. #include "executable_semantics/syntax/prelude.h"
  18. #include "llvm/Support/CommandLine.h"
  19. #include "llvm/Support/InitLLVM.h"
  20. #include "llvm/Support/raw_ostream.h"
  21. namespace Carbon {
  22. namespace cl = llvm::cl;
  23. static auto Main(llvm::StringRef default_prelude_file, int argc, char* argv[])
  24. -> ErrorOr<Success> {
  25. llvm::setBugReportMsg(
  26. "Please report issues to "
  27. "https://github.com/carbon-language/carbon-lang/issues and include the "
  28. "crash backtrace.\n");
  29. llvm::InitLLVM init_llvm(argc, argv);
  30. // Printing to stderr should flush stdout. This is most noticeable when stderr
  31. // is piped to stdout.
  32. llvm::errs().tie(&llvm::outs());
  33. cl::opt<std::string> input_file_name(cl::Positional, cl::desc("<input file>"),
  34. cl::Required);
  35. cl::opt<bool> parser_debug("parser_debug",
  36. cl::desc("Enable debug output from the parser"));
  37. cl::opt<std::string> trace_file_name(
  38. "trace_file",
  39. cl::desc("Output file for tracing; set to `-` to output to stdout."));
  40. // Find the path of the executable if possible and use that as a relative root
  41. cl::opt<std::string> prelude_file_name("prelude", cl::desc("<prelude file>"),
  42. cl::init(default_prelude_file.str()));
  43. cl::ParseCommandLineOptions(argc, argv);
  44. // Set up a stream for trace output.
  45. std::unique_ptr<llvm::raw_ostream> scoped_trace_stream;
  46. std::optional<Nonnull<llvm::raw_ostream*>> trace_stream;
  47. if (!trace_file_name.empty()) {
  48. if (trace_file_name == "-") {
  49. trace_stream = &llvm::outs();
  50. } else {
  51. std::error_code err;
  52. scoped_trace_stream =
  53. std::make_unique<llvm::raw_fd_ostream>(trace_file_name, err);
  54. if (err) {
  55. return Error(err.message());
  56. }
  57. trace_stream = scoped_trace_stream.get();
  58. }
  59. }
  60. Arena arena;
  61. ASSIGN_OR_RETURN(AST ast, Parse(&arena, input_file_name, parser_debug));
  62. AddPrelude(prelude_file_name, &arena, &ast.declarations);
  63. // Typecheck and run the parsed program.
  64. ASSIGN_OR_RETURN(int return_code, ExecProgram(&arena, ast, trace_stream));
  65. // Print the return code to stdout even when we aren't tracing.
  66. (trace_stream ? **trace_stream : llvm::outs())
  67. << "result: " << return_code << "\n";
  68. return Success();
  69. }
  70. auto ExecutableSemanticsMain(llvm::StringRef default_prelude_file, int argc,
  71. char** argv) -> int {
  72. if (auto result = Main(default_prelude_file, argc, argv); !result.ok()) {
  73. llvm::errs() << result.error().message() << "\n";
  74. return EXIT_FAILURE;
  75. }
  76. return EXIT_SUCCESS;
  77. }
  78. } // namespace Carbon