main.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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 <unistd.h>
  5. #include <cstdio>
  6. #include <cstring>
  7. #include <iostream>
  8. #include <string>
  9. #include <vector>
  10. #include "common/error.h"
  11. #include "executable_semantics/common/arena.h"
  12. #include "executable_semantics/common/nonnull.h"
  13. #include "executable_semantics/interpreter/exec_program.h"
  14. #include "executable_semantics/syntax/parse.h"
  15. #include "executable_semantics/syntax/prelude.h"
  16. #include "llvm/Support/CommandLine.h"
  17. #include "llvm/Support/InitLLVM.h"
  18. // Prints an error message and returns error code value.
  19. auto PrintError(const Carbon::Error& error) -> int {
  20. llvm::errs() << error.message() << "\n";
  21. return EXIT_FAILURE;
  22. }
  23. auto main(int argc, char* argv[]) -> int {
  24. llvm::setBugReportMsg(
  25. "Please report issues to "
  26. "https://github.com/carbon-language/carbon-lang/issues and include the "
  27. "crash backtrace.\n");
  28. llvm::InitLLVM init_llvm(argc, argv);
  29. // Printing to stderr should flush stdout. This is most noticeable when stderr
  30. // is piped to stdout.
  31. llvm::errs().tie(&llvm::outs());
  32. using llvm::cl::desc;
  33. using llvm::cl::opt;
  34. opt<bool> trace_option("trace", desc("Enable tracing"));
  35. opt<std::string> input_file_name(llvm::cl::Positional, desc("<input file>"),
  36. llvm::cl::Required);
  37. opt<std::string> prelude_file_name(
  38. "prelude", desc("<prelude file>"),
  39. llvm::cl::init("executable_semantics/data/prelude.carbon"));
  40. llvm::cl::ParseCommandLineOptions(argc, argv);
  41. Carbon::Arena arena;
  42. Carbon::ErrorOr<Carbon::AST> ast =
  43. Carbon::Parse(&arena, input_file_name, trace_option);
  44. if (!ast.ok()) {
  45. return PrintError(ast.error());
  46. }
  47. AddPrelude(prelude_file_name, &arena, &ast->declarations);
  48. // Typecheck and run the parsed program.
  49. Carbon::ErrorOr<int> result = Carbon::ExecProgram(&arena, *ast, trace_option);
  50. if (!result.ok()) {
  51. return PrintError(result.error());
  52. }
  53. }