main.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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/ADT/SmallString.h"
  19. #include "llvm/Support/CommandLine.h"
  20. #include "llvm/Support/FileSystem.h"
  21. #include "llvm/Support/InitLLVM.h"
  22. #include "llvm/Support/Path.h"
  23. #include "llvm/Support/raw_ostream.h"
  24. namespace Carbon {
  25. namespace cl = llvm::cl;
  26. namespace path = llvm::sys::path;
  27. auto ExplorerMain(int argc, char** argv, void* static_for_main_addr,
  28. llvm::StringRef relative_prelude_path) -> int {
  29. llvm::setBugReportMsg(
  30. "Please report issues to "
  31. "https://github.com/carbon-language/carbon-lang/issues and include the "
  32. "crash backtrace.\n");
  33. llvm::InitLLVM init_llvm(argc, argv);
  34. // Printing to stderr should flush stdout. This is most noticeable when stderr
  35. // is piped to stdout.
  36. llvm::errs().tie(&llvm::outs());
  37. cl::opt<std::string> input_file_name(cl::Positional, cl::desc("<input file>"),
  38. cl::Required);
  39. cl::opt<bool> parser_debug("parser_debug",
  40. cl::desc("Enable debug output from the parser"));
  41. cl::opt<std::string> trace_file_name(
  42. "trace_file",
  43. cl::desc("Output file for tracing; set to `-` to output to stdout."));
  44. // Use the executable path as a base for the relative prelude path.
  45. std::string exe =
  46. llvm::sys::fs::getMainExecutable(argv[0], static_for_main_addr);
  47. llvm::StringRef install_path = path::parent_path(exe);
  48. llvm::SmallString<256> default_prelude_file(install_path);
  49. path::append(default_prelude_file,
  50. path::begin(relative_prelude_path, path::Style::posix),
  51. path::end(relative_prelude_path));
  52. std::string default_prelude_file_str(default_prelude_file);
  53. cl::opt<std::string> prelude_file_name("prelude", cl::desc("<prelude file>"),
  54. cl::init(default_prelude_file_str));
  55. cl::ParseCommandLineOptions(argc, argv);
  56. // Set up a stream for trace output.
  57. std::unique_ptr<llvm::raw_ostream> scoped_trace_stream;
  58. std::optional<Nonnull<llvm::raw_ostream*>> trace_stream;
  59. if (!trace_file_name.empty()) {
  60. if (trace_file_name == "-") {
  61. trace_stream = &llvm::outs();
  62. } else {
  63. std::error_code err;
  64. scoped_trace_stream =
  65. std::make_unique<llvm::raw_fd_ostream>(trace_file_name, err);
  66. if (err) {
  67. llvm::errs() << err.message() << "\n";
  68. return EXIT_FAILURE;
  69. }
  70. trace_stream = scoped_trace_stream.get();
  71. }
  72. }
  73. Arena arena;
  74. AST ast;
  75. if (ErrorOr<AST> parse_result = Parse(&arena, input_file_name, parser_debug);
  76. parse_result.ok()) {
  77. ast = *std::move(parse_result);
  78. } else {
  79. llvm::errs() << "SYNTAX ERROR: " << parse_result.error() << "\n";
  80. return EXIT_FAILURE;
  81. }
  82. AddPrelude(prelude_file_name, &arena, &ast.declarations);
  83. // Semantically analyze the parsed program.
  84. if (ErrorOr<AST> analyze_result = AnalyzeProgram(&arena, ast, trace_stream);
  85. analyze_result.ok()) {
  86. ast = *std::move(analyze_result);
  87. } else {
  88. llvm::errs() << "COMPILATION ERROR: " << analyze_result.error() << "\n";
  89. return EXIT_FAILURE;
  90. }
  91. // Run the program.
  92. if (ErrorOr<int> exec_result = ExecProgram(&arena, ast, trace_stream);
  93. exec_result.ok()) {
  94. // Print the return code to stdout.
  95. llvm::outs() << "result: " << *exec_result << "\n";
  96. // When there's a dedicated trace file, print the return code to it too.
  97. if (scoped_trace_stream) {
  98. **trace_stream << "result: " << *exec_result << "\n";
  99. }
  100. } else {
  101. llvm::errs() << "RUNTIME ERROR: " << exec_result.error() << "\n";
  102. return EXIT_FAILURE;
  103. }
  104. return EXIT_SUCCESS;
  105. }
  106. } // namespace Carbon