main.cpp 1.3 KB

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