exec_program.cpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. }
  21. SourceLocation source_loc("<Main()>", 0);
  22. ast.main_call = arena->New<CallExpression>(
  23. source_loc, arena->New<IdentifierExpression>(source_loc, "Main"),
  24. arena->New<TupleLiteral>(source_loc));
  25. // Although name resolution is currently done once, generic programming
  26. // (particularly templates) may require more passes.
  27. if (trace) {
  28. llvm::outs() << "********** resolving names **********\n";
  29. }
  30. ResolveNames(ast);
  31. if (trace) {
  32. llvm::outs() << "********** resolving control flow **********\n";
  33. }
  34. ResolveControlFlow(ast);
  35. if (trace) {
  36. llvm::outs() << "********** type checking **********\n";
  37. }
  38. TypeChecker(arena, trace).TypeCheck(ast);
  39. if (trace) {
  40. llvm::outs() << "\n";
  41. llvm::outs() << "********** type checking complete **********\n";
  42. for (const auto decl : ast.declarations) {
  43. llvm::outs() << *decl;
  44. }
  45. llvm::outs() << "********** starting execution **********\n";
  46. }
  47. int result = InterpProgram(ast, arena, trace);
  48. llvm::outs() << "result: " << result << "\n";
  49. }
  50. } // namespace Carbon