parse_and_execute.cpp 3.5 KB

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