main.cpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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::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. Carbon::Arena arena;
  27. std::variant<Carbon::AST, Carbon::SyntaxErrorCode> ast_or_error =
  28. Carbon::Parse(&arena, input_file_name);
  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. }