main.cpp 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 <string>
  8. #include <vector>
  9. #include "executable_semantics/common/arena.h"
  10. #include "executable_semantics/common/nonnull.h"
  11. #include "executable_semantics/interpreter/exec_program.h"
  12. #include "executable_semantics/syntax/parse.h"
  13. #include "llvm/Support/CommandLine.h"
  14. #include "llvm/Support/InitLLVM.h"
  15. // The Carbon prelude.
  16. //
  17. // TODO: Make this a separate source file that's embedded in the interpreter
  18. // at build time. See https://github.com/bazelbuild/rules_cc/issues/41 for a
  19. // possible mechanism.
  20. static constexpr std::string_view Prelude = R"(
  21. package Carbon api;
  22. // Note that Print is experimental, and not part of an accepted proposal, but
  23. // is included here for printing state in tests.
  24. fn Print(format_str: String) {
  25. __intrinsic_print(format_str);
  26. }
  27. )";
  28. // Adds the Carbon prelude to `declarations`.
  29. static void AddPrelude(
  30. Carbon::Nonnull<Carbon::Arena*> arena,
  31. std::vector<Carbon::Nonnull<Carbon::Declaration*>>* declarations) {
  32. std::variant<Carbon::AST, Carbon::SyntaxErrorCode> parse_result =
  33. ParseFromString(arena, "<prelude>", Prelude, false);
  34. if (std::holds_alternative<Carbon::SyntaxErrorCode>(parse_result)) {
  35. // Try again with tracing, to help diagnose the problem.
  36. ParseFromString(arena, "<prelude>", Prelude, true);
  37. FATAL() << "Failed to parse prelude.";
  38. }
  39. const auto& prelude = std::get<Carbon::AST>(parse_result);
  40. declarations->insert(declarations->begin(), prelude.declarations.begin(),
  41. prelude.declarations.end());
  42. }
  43. auto main(int argc, char* argv[]) -> int {
  44. llvm::setBugReportMsg(
  45. "Please report issues to "
  46. "https://github.com/carbon-language/carbon-lang/issues and include the "
  47. "crash backtrace.\n");
  48. llvm::InitLLVM init_llvm(argc, argv);
  49. // Printing to stderr should flush stdout. This is most noticeable when stderr
  50. // is piped to stdout.
  51. llvm::errs().tie(&llvm::outs());
  52. using llvm::cl::desc;
  53. using llvm::cl::opt;
  54. opt<bool> trace_option("trace", desc("Enable tracing"));
  55. opt<std::string> input_file_name(llvm::cl::Positional, desc("<input file>"),
  56. llvm::cl::Required);
  57. llvm::cl::ParseCommandLineOptions(argc, argv);
  58. Carbon::Arena arena;
  59. std::variant<Carbon::AST, Carbon::SyntaxErrorCode> ast_or_error =
  60. Carbon::Parse(&arena, input_file_name, trace_option);
  61. if (auto* error = std::get_if<Carbon::SyntaxErrorCode>(&ast_or_error)) {
  62. // Diagnostic already reported to std::cerr; this is just a return code.
  63. return *error;
  64. }
  65. auto& ast = std::get<Carbon::AST>(ast_or_error);
  66. AddPrelude(&arena, &ast.declarations);
  67. // Typecheck and run the parsed program.
  68. Carbon::ExecProgram(&arena, std::get<Carbon::AST>(ast_or_error),
  69. trace_option);
  70. }