parse_and_execute.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 "explorer/parse_and_execute/parse_and_execute.h"
  5. #include <locale>
  6. #include "common/check.h"
  7. #include "common/error.h"
  8. #include "explorer/interpreter/exec_program.h"
  9. #include "explorer/interpreter/stack_space.h"
  10. #include "explorer/syntax/parse.h"
  11. #include "explorer/syntax/prelude.h"
  12. #include "llvm/ADT/ScopeExit.h"
  13. namespace Carbon {
  14. // Returns a scope exit function for printing the timing of a step on scope
  15. // exit. Note the use prints step timings in reverse order.
  16. static auto PrintTimingOnExit(TraceStream* trace_stream, const char* label,
  17. std::chrono::steady_clock::time_point* cursor) {
  18. auto end = std::chrono::steady_clock::now();
  19. auto duration = end - *cursor;
  20. *cursor = end;
  21. return llvm::make_scope_exit([=]() {
  22. if (trace_stream->is_enabled()) {
  23. *trace_stream << "Time elapsed in " << label << ": "
  24. << std::chrono::duration_cast<std::chrono::milliseconds>(
  25. duration)
  26. .count()
  27. << "ms\n";
  28. }
  29. });
  30. }
  31. static auto ParseAndExecuteHelper(std::function<ErrorOr<AST>(Arena*)> parse,
  32. const std::string& prelude_path,
  33. Nonnull<TraceStream*> trace_stream,
  34. Nonnull<llvm::raw_ostream*> print_stream)
  35. -> ErrorOr<int> {
  36. return RunWithExtraStack([&]() -> ErrorOr<int> {
  37. Arena arena;
  38. auto cursor = std::chrono::steady_clock::now();
  39. ErrorOr<AST> parse_result = parse(&arena);
  40. auto print_parse_time = PrintTimingOnExit(trace_stream, "Parse", &cursor);
  41. if (!parse_result.ok()) {
  42. return ErrorBuilder() << "SYNTAX ERROR: " << parse_result.error();
  43. }
  44. AddPrelude(prelude_path, &arena, &parse_result->declarations,
  45. &parse_result->num_prelude_declarations);
  46. auto print_prelude_time =
  47. PrintTimingOnExit(trace_stream, "AddPrelude", &cursor);
  48. // Semantically analyze the parsed program.
  49. ErrorOr<AST> analyze_result =
  50. AnalyzeProgram(&arena, *parse_result, trace_stream, print_stream);
  51. auto print_analyze_time =
  52. PrintTimingOnExit(trace_stream, "AnalyzeProgram", &cursor);
  53. if (!analyze_result.ok()) {
  54. return ErrorBuilder() << "COMPILATION ERROR: " << analyze_result.error();
  55. }
  56. // Run the program.
  57. ErrorOr<int> exec_result =
  58. ExecProgram(&arena, *analyze_result, trace_stream, print_stream);
  59. auto print_exec_time =
  60. PrintTimingOnExit(trace_stream, "ExecProgram", &cursor);
  61. if (!exec_result.ok()) {
  62. return ErrorBuilder() << "RUNTIME ERROR: " << exec_result.error();
  63. }
  64. return exec_result;
  65. });
  66. }
  67. auto ParseAndExecuteFile(const std::string& prelude_path,
  68. const std::string& input_file_name, bool parser_debug,
  69. Nonnull<TraceStream*> trace_stream,
  70. Nonnull<llvm::raw_ostream*> print_stream)
  71. -> ErrorOr<int> {
  72. auto parse = [&](Arena* arena) {
  73. return Parse(arena, input_file_name, parser_debug);
  74. };
  75. return ParseAndExecuteHelper(parse, prelude_path, trace_stream, print_stream);
  76. }
  77. auto ParseAndExecute(const std::string& prelude_path, const std::string& source)
  78. -> ErrorOr<int> {
  79. auto parse = [&](Arena* arena) {
  80. return ParseFromString(arena, "test.carbon", source,
  81. /*parser_debug=*/false);
  82. };
  83. TraceStream trace_stream;
  84. return ParseAndExecuteHelper(parse, prelude_path, &trace_stream,
  85. &llvm::nulls());
  86. }
  87. } // namespace Carbon