main.cpp 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 "explorer/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 "explorer/common/arena.h"
  14. #include "explorer/common/nonnull.h"
  15. #include "explorer/interpreter/exec_program.h"
  16. #include "explorer/syntax/parse.h"
  17. #include "explorer/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. CARBON_ASSIGN_OR_RETURN(AST ast,
  62. Parse(&arena, input_file_name, parser_debug));
  63. AddPrelude(prelude_file_name, &arena, &ast.declarations);
  64. // Typecheck and run the parsed program.
  65. CARBON_ASSIGN_OR_RETURN(int return_code,
  66. ExecProgram(&arena, ast, trace_stream));
  67. // Always print the return code to stdout.
  68. llvm::outs() << "result: " << return_code << "\n";
  69. // When there's a dedicated trace file, print the return code to it too.
  70. if (scoped_trace_stream) {
  71. **trace_stream << "result: " << return_code << "\n";
  72. }
  73. return Success();
  74. }
  75. auto ExplorerMain(llvm::StringRef default_prelude_file, int argc, char** argv)
  76. -> int {
  77. if (auto result = Main(default_prelude_file, argc, argv); !result.ok()) {
  78. llvm::errs() << result.error() << "\n";
  79. return EXIT_FAILURE;
  80. }
  81. return EXIT_SUCCESS;
  82. }
  83. } // namespace Carbon