exec_program.cpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. namespace Carbon {
  14. void ExecProgram(Nonnull<Arena*> arena, AST ast, bool trace) {
  15. if (trace) {
  16. llvm::outs() << "********** source program **********\n";
  17. for (const auto decl : ast.declarations) {
  18. llvm::outs() << *decl;
  19. }
  20. llvm::outs() << "********** type checking **********\n";
  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. ResolveNames(ast);
  29. ResolveControlFlow(ast);
  30. TypeChecker(arena, trace).TypeCheck(ast);
  31. if (trace) {
  32. llvm::outs() << "\n";
  33. llvm::outs() << "********** type checking complete **********\n";
  34. for (const auto decl : ast.declarations) {
  35. llvm::outs() << *decl;
  36. }
  37. llvm::outs() << "********** starting execution **********\n";
  38. }
  39. int result = InterpProgram(ast, arena, trace);
  40. llvm::outs() << "result: " << result << "\n";
  41. }
  42. } // namespace Carbon