exec_program.cpp 2.2 KB

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