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