exec_program.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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/interpreter/exec_program.h"
  5. #include <variant>
  6. #include "common/check.h"
  7. #include "common/ostream.h"
  8. #include "explorer/common/arena.h"
  9. #include "explorer/interpreter/interpreter.h"
  10. #include "explorer/interpreter/resolve_control_flow.h"
  11. #include "explorer/interpreter/resolve_names.h"
  12. #include "explorer/interpreter/type_checker.h"
  13. #include "llvm/Support/Error.h"
  14. namespace Carbon {
  15. auto ExecProgram(Nonnull<Arena*> arena, AST ast,
  16. std::optional<Nonnull<llvm::raw_ostream*>> trace_stream)
  17. -> ErrorOr<int> {
  18. if (trace_stream) {
  19. **trace_stream << "********** source program **********\n";
  20. for (const auto decl : ast.declarations) {
  21. **trace_stream << *decl;
  22. }
  23. }
  24. SourceLocation source_loc("<Main()>", 0);
  25. ast.main_call = arena->New<CallExpression>(
  26. source_loc, arena->New<IdentifierExpression>(source_loc, "Main"),
  27. arena->New<TupleLiteral>(source_loc));
  28. // Although name resolution is currently done once, generic programming
  29. // (particularly templates) may require more passes.
  30. if (trace_stream) {
  31. **trace_stream << "********** resolving names **********\n";
  32. }
  33. RETURN_IF_ERROR(ResolveNames(ast));
  34. if (trace_stream) {
  35. **trace_stream << "********** resolving control flow **********\n";
  36. }
  37. RETURN_IF_ERROR(ResolveControlFlow(ast));
  38. if (trace_stream) {
  39. **trace_stream << "********** type checking **********\n";
  40. }
  41. RETURN_IF_ERROR(TypeChecker(arena, trace_stream).TypeCheck(ast));
  42. if (trace_stream) {
  43. **trace_stream << "\n";
  44. **trace_stream << "********** type checking complete **********\n";
  45. for (const auto decl : ast.declarations) {
  46. **trace_stream << *decl;
  47. }
  48. **trace_stream << "********** starting execution **********\n";
  49. }
  50. return InterpProgram(ast, arena, trace_stream);
  51. }
  52. } // namespace Carbon