main.cpp 1.6 KB

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