main.cpp 1.4 KB

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