exec_program.cpp 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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<Block>(
  22. source_loc,
  23. arena->New<Sequence>(
  24. source_loc,
  25. arena->New<Return>(source_loc,
  26. arena->New<IntrinsicExpression>(
  27. IntrinsicExpression::Intrinsic::Print),
  28. false),
  29. std::nullopt));
  30. auto print = arena->New<FunctionDeclaration>(
  31. source_loc, "Print", std::vector<GenericBinding>(),
  32. arena->New<TuplePattern>(source_loc, print_params),
  33. arena->New<ExpressionPattern>(arena->New<TupleLiteral>(source_loc)),
  34. /*is_omitted_return_type=*/false, print_return);
  35. declarations->insert(declarations->begin(), print);
  36. }
  37. void ExecProgram(Nonnull<Arena*> arena, AST ast, bool trace) {
  38. AddIntrinsics(arena, &ast.declarations);
  39. if (trace) {
  40. llvm::outs() << "********** source program **********\n";
  41. for (const auto decl : ast.declarations) {
  42. llvm::outs() << *decl;
  43. }
  44. llvm::outs() << "********** type checking **********\n";
  45. }
  46. ResolveControlFlow(ast);
  47. TypeChecker type_checker(arena, trace);
  48. TypeChecker::TypeCheckContext p = type_checker.TopLevel(&ast.declarations);
  49. TypeEnv top = p.types;
  50. Env ct_top = p.values;
  51. for (const auto decl : ast.declarations) {
  52. type_checker.TypeCheck(decl, top, ct_top);
  53. }
  54. if (trace) {
  55. llvm::outs() << "\n";
  56. llvm::outs() << "********** type checking complete **********\n";
  57. for (const auto decl : ast.declarations) {
  58. llvm::outs() << *decl;
  59. }
  60. llvm::outs() << "********** starting execution **********\n";
  61. }
  62. SourceLocation source_loc("<main()>", 0);
  63. Nonnull<Expression*> call_main = arena->New<CallExpression>(
  64. source_loc, arena->New<IdentifierExpression>(source_loc, "main"),
  65. arena->New<TupleLiteral>(source_loc));
  66. int result =
  67. Interpreter(arena, trace).InterpProgram(ast.declarations, call_main);
  68. llvm::outs() << "result: " << result << "\n";
  69. }
  70. } // namespace Carbon