main.cpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 <unistd.h>
  5. #include <cstdio>
  6. #include <cstring>
  7. #include <iostream>
  8. #include <string>
  9. #include <vector>
  10. #include "executable_semantics/common/arena.h"
  11. #include "executable_semantics/common/nonnull.h"
  12. #include "executable_semantics/interpreter/exec_program.h"
  13. #include "executable_semantics/syntax/parse.h"
  14. #include "llvm/Support/CommandLine.h"
  15. #include "llvm/Support/InitLLVM.h"
  16. // Adds the Carbon prelude to `declarations`.
  17. static void AddPrelude(
  18. std::string_view prelude_file_name, Carbon::Nonnull<Carbon::Arena*> arena,
  19. std::vector<Carbon::Nonnull<Carbon::Declaration*>>* declarations) {
  20. std::variant<Carbon::AST, Carbon::SyntaxErrorCode> parse_result =
  21. Carbon::Parse(arena, prelude_file_name, false);
  22. if (std::holds_alternative<Carbon::SyntaxErrorCode>(parse_result)) {
  23. // Try again with tracing, to help diagnose the problem.
  24. Carbon::Parse(arena, prelude_file_name, true);
  25. FATAL() << "Failed to parse prelude.";
  26. }
  27. const auto& prelude = std::get<Carbon::AST>(parse_result);
  28. declarations->insert(declarations->begin(), prelude.declarations.begin(),
  29. prelude.declarations.end());
  30. }
  31. auto main(int argc, char* argv[]) -> int {
  32. llvm::setBugReportMsg(
  33. "Please report issues to "
  34. "https://github.com/carbon-language/carbon-lang/issues and include the "
  35. "crash backtrace.\n");
  36. llvm::InitLLVM init_llvm(argc, argv);
  37. // Printing to stderr should flush stdout. This is most noticeable when stderr
  38. // is piped to stdout.
  39. llvm::errs().tie(&llvm::outs());
  40. using llvm::cl::desc;
  41. using llvm::cl::opt;
  42. opt<bool> trace_option("trace", desc("Enable tracing"));
  43. opt<std::string> input_file_name(llvm::cl::Positional, desc("<input file>"),
  44. llvm::cl::Required);
  45. opt<std::string> prelude_file_name(
  46. "prelude", desc("<prelude file>"),
  47. llvm::cl::init("executable_semantics/data/prelude.carbon"));
  48. llvm::cl::ParseCommandLineOptions(argc, argv);
  49. Carbon::Arena arena;
  50. std::variant<Carbon::AST, Carbon::SyntaxErrorCode> ast_or_error =
  51. Carbon::Parse(&arena, input_file_name, trace_option);
  52. if (auto* error = std::get_if<Carbon::SyntaxErrorCode>(&ast_or_error)) {
  53. // Diagnostic already reported to std::cerr; this is just a return code.
  54. return *error;
  55. }
  56. auto& ast = std::get<Carbon::AST>(ast_or_error);
  57. AddPrelude(prelude_file_name, &arena, &ast.declarations);
  58. // Typecheck and run the parsed program.
  59. Carbon::ExecProgram(&arena, std::get<Carbon::AST>(ast_or_error),
  60. trace_option);
  61. }