exec_program.cpp 1.8 KB

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