main.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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 <cstdio>
  5. #include <cstring>
  6. #include <iostream>
  7. #include "executable_semantics/interpreter/exec_program.h"
  8. #include "executable_semantics/syntax/parse.h"
  9. #include "llvm/Support/CommandLine.h"
  10. #include "llvm/Support/InitLLVM.h"
  11. auto main(int argc, char* argv[]) -> int {
  12. llvm::setBugReportMsg(
  13. "Please report issues to "
  14. "https://github.com/carbon-language/carbon-lang/issues and include the "
  15. "crash backtrace.\n");
  16. llvm::InitLLVM init_llvm(argc, argv);
  17. // Printing to stderr should flush stdout. This is most noticeable when stderr
  18. // is piped to stdout.
  19. llvm::errs().tie(&llvm::outs());
  20. using llvm::cl::desc;
  21. using llvm::cl::opt;
  22. opt<bool> trace_option("trace", desc("Enable tracing"));
  23. opt<std::string> input_file_name(llvm::cl::Positional, desc("<input file>"),
  24. llvm::cl::Required);
  25. llvm::cl::ParseCommandLineOptions(argc, argv);
  26. Carbon::Arena arena;
  27. std::variant<Carbon::AST, Carbon::SyntaxErrorCode> ast_or_error =
  28. Carbon::Parse(&arena, input_file_name, trace_option);
  29. if (auto* error = std::get_if<Carbon::SyntaxErrorCode>(&ast_or_error)) {
  30. // Diagnostic already reported to std::cerr; this is just a return code.
  31. return *error;
  32. }
  33. // Typecheck and run the parsed program.
  34. Carbon::ExecProgram(&arena, std::get<Carbon::AST>(ast_or_error),
  35. trace_option);
  36. }