main.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. -> bool {
  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. llvm::errs() << err.message() << "\n";
  56. return false;
  57. }
  58. trace_stream = scoped_trace_stream.get();
  59. }
  60. }
  61. Arena arena;
  62. AST ast;
  63. if (ErrorOr<AST> parse_result = Parse(&arena, input_file_name, parser_debug);
  64. parse_result.ok()) {
  65. ast = *std::move(parse_result);
  66. } else {
  67. llvm::errs() << "SYNTAX ERROR: " << parse_result.error() << "\n";
  68. return false;
  69. }
  70. AddPrelude(prelude_file_name, &arena, &ast.declarations);
  71. // Semantically analyze the parsed program.
  72. if (ErrorOr<AST> analyze_result = AnalyzeProgram(&arena, ast, trace_stream);
  73. analyze_result.ok()) {
  74. ast = *std::move(analyze_result);
  75. } else {
  76. llvm::errs() << "COMPILATION ERROR: " << analyze_result.error() << "\n";
  77. return false;
  78. }
  79. // Run the program.
  80. if (ErrorOr<int> exec_result = ExecProgram(&arena, ast, trace_stream);
  81. exec_result.ok()) {
  82. // Print the return code to stdout.
  83. llvm::outs() << "result: " << *exec_result << "\n";
  84. // When there's a dedicated trace file, print the return code to it too.
  85. if (scoped_trace_stream) {
  86. **trace_stream << "result: " << *exec_result << "\n";
  87. }
  88. } else {
  89. llvm::errs() << "RUNTIME ERROR: " << exec_result.error() << "\n";
  90. return false;
  91. }
  92. return true;
  93. }
  94. auto ExplorerMain(llvm::StringRef default_prelude_file, int argc, char** argv)
  95. -> int {
  96. return Main(default_prelude_file, argc, argv) ? EXIT_SUCCESS : EXIT_FAILURE;
  97. }
  98. } // namespace Carbon