main.cpp 2.8 KB

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