main.cpp 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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/ADT/SmallString.h"
  17. #include "llvm/Support/CommandLine.h"
  18. #include "llvm/Support/ErrorOr.h"
  19. #include "llvm/Support/FileSystem.h"
  20. #include "llvm/Support/InitLLVM.h"
  21. #include "llvm/Support/Path.h"
  22. #include "llvm/Support/Program.h"
  23. // Prints an error message and returns error code value.
  24. auto PrintError(const Carbon::Error& error) -> int {
  25. llvm::errs() << error.message() << "\n";
  26. return EXIT_FAILURE;
  27. }
  28. auto main(int argc, char* argv[]) -> int {
  29. llvm::setBugReportMsg(
  30. "Please report issues to "
  31. "https://github.com/carbon-language/carbon-lang/issues and include the "
  32. "crash backtrace.\n");
  33. llvm::InitLLVM init_llvm(argc, argv);
  34. // Printing to stderr should flush stdout. This is most noticeable when stderr
  35. // is piped to stdout.
  36. llvm::errs().tie(&llvm::outs());
  37. using llvm::cl::desc;
  38. using llvm::cl::opt;
  39. opt<bool> trace_option("trace", desc("Enable tracing"));
  40. opt<std::string> input_file_name(llvm::cl::Positional, desc("<input file>"),
  41. llvm::cl::Required);
  42. // Find the path of the executable if possible and use that as a relative root
  43. // for finding the prelude. FIXME: Currently, this assumes a Bazel-like
  44. // runfiles tree rather than any kind of installation tree.
  45. llvm::SmallString<256> exe_path(argv[0]);
  46. if (!llvm::sys::fs::exists(exe_path)) {
  47. // Try to lookup the program name in the `PATH`.
  48. if (llvm::ErrorOr<std::string> path =
  49. llvm::sys::findProgramByName(exe_path)) {
  50. exe_path = *path;
  51. }
  52. }
  53. // If we have a valid path, find the parent directory. Otherwise, just use an
  54. // empty string to get the current working directory.
  55. if (llvm::sys::fs::exists(exe_path)) {
  56. llvm::sys::path::remove_filename(exe_path);
  57. } else {
  58. exe_path = "";
  59. }
  60. llvm::SmallString<256> prelude_path = exe_path;
  61. llvm::sys::path::append(prelude_path, "data/prelude.carbon");
  62. opt<std::string> prelude_file_name("prelude", desc("<prelude file>"),
  63. llvm::cl::init(prelude_path.str().str()));
  64. llvm::cl::ParseCommandLineOptions(argc, argv);
  65. Carbon::Arena arena;
  66. Carbon::ErrorOr<Carbon::AST> ast =
  67. Carbon::Parse(&arena, input_file_name, trace_option);
  68. if (!ast.ok()) {
  69. return PrintError(ast.error());
  70. }
  71. AddPrelude(prelude_file_name, &arena, &ast->declarations);
  72. // Typecheck and run the parsed program.
  73. Carbon::ErrorOr<int> result = Carbon::ExecProgram(&arena, *ast, trace_option);
  74. if (!result.ok()) {
  75. return PrintError(result.error());
  76. }
  77. }