exec_program.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 "common/check.h"
  6. #include "common/ostream.h"
  7. #include "executable_semantics/common/arena.h"
  8. #include "executable_semantics/interpreter/interpreter.h"
  9. #include "executable_semantics/interpreter/resolve_control_flow.h"
  10. #include "executable_semantics/interpreter/type_checker.h"
  11. namespace Carbon {
  12. // Adds builtins, currently only Print(). Note Print() is experimental, not
  13. // standardized, but is made available for printing state in tests.
  14. static void AddIntrinsics(Nonnull<Arena*> arena,
  15. std::vector<Nonnull<Declaration*>>* declarations) {
  16. SourceLocation source_loc("<intrinsic>", 0);
  17. std::vector<Nonnull<Pattern*>> print_params = {arena->New<BindingPattern>(
  18. source_loc, "format_str",
  19. arena->New<ExpressionPattern>(
  20. arena->New<StringTypeLiteral>(source_loc)))};
  21. auto print_return = arena->New<Return>(
  22. source_loc,
  23. arena->New<IntrinsicExpression>(IntrinsicExpression::Intrinsic::Print),
  24. false);
  25. auto print = arena->New<FunctionDeclaration>(
  26. source_loc, "Print", std::vector<GenericBinding>(),
  27. arena->New<TuplePattern>(source_loc, print_params),
  28. arena->New<ExpressionPattern>(arena->New<TupleLiteral>(source_loc)),
  29. /*is_omitted_return_type=*/false, print_return);
  30. declarations->insert(declarations->begin(), print);
  31. }
  32. void ExecProgram(Nonnull<Arena*> arena, AST ast, bool trace) {
  33. AddIntrinsics(arena, &ast.declarations);
  34. if (trace) {
  35. llvm::outs() << "********** source program **********\n";
  36. for (const auto decl : ast.declarations) {
  37. llvm::outs() << *decl;
  38. }
  39. llvm::outs() << "********** type checking **********\n";
  40. }
  41. ResolveControlFlow(ast);
  42. TypeChecker type_checker(arena, trace);
  43. TypeChecker::TypeCheckContext p = type_checker.TopLevel(&ast.declarations);
  44. TypeEnv top = p.types;
  45. Env ct_top = p.values;
  46. for (const auto decl : ast.declarations) {
  47. type_checker.TypeCheck(decl, top, ct_top);
  48. }
  49. if (trace) {
  50. llvm::outs() << "\n";
  51. llvm::outs() << "********** type checking complete **********\n";
  52. for (const auto decl : ast.declarations) {
  53. llvm::outs() << *decl;
  54. }
  55. llvm::outs() << "********** starting execution **********\n";
  56. }
  57. SourceLocation source_loc("<main()>", 0);
  58. Nonnull<Expression*> call_main = arena->New<CallExpression>(
  59. source_loc, arena->New<IdentifierExpression>(source_loc, "main"),
  60. arena->New<TupleLiteral>(source_loc));
  61. int result =
  62. Interpreter(arena, trace).InterpProgram(ast.declarations, call_main);
  63. llvm::outs() << "result: " << result << "\n";
  64. }
  65. } // namespace Carbon