exec_program.cpp 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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/common/tracing_flag.h"
  9. #include "executable_semantics/interpreter/interpreter.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>(arena->New<FunctionDefinition>(
  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) {
  33. AddIntrinsics(arena, &ast.declarations);
  34. if (tracing_output) {
  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. TypeChecker type_checker(arena);
  42. TypeChecker::TypeCheckContext p = type_checker.TopLevel(&ast.declarations);
  43. TypeEnv top = p.types;
  44. Env ct_top = p.values;
  45. for (const auto decl : ast.declarations) {
  46. type_checker.TypeCheck(decl, top, ct_top);
  47. }
  48. if (tracing_output) {
  49. llvm::outs() << "\n";
  50. llvm::outs() << "********** type checking complete **********\n";
  51. for (const auto decl : ast.declarations) {
  52. llvm::outs() << *decl;
  53. }
  54. llvm::outs() << "********** starting execution **********\n";
  55. }
  56. SourceLocation source_loc("<main()>", 0);
  57. Nonnull<Expression*> call_main = arena->New<CallExpression>(
  58. source_loc, arena->New<IdentifierExpression>(source_loc, "main"),
  59. arena->New<TupleLiteral>(source_loc));
  60. int result = Interpreter(arena).InterpProgram(ast.declarations, call_main);
  61. llvm::outs() << "result: " << result << "\n";
  62. }
  63. } // namespace Carbon