exec_program.cpp 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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<TuplePattern::Field> print_fields = {TuplePattern::Field(
  18. "0", arena->New<BindingPattern>(
  19. source_loc, "format_str",
  20. arena->New<ExpressionPattern>(
  21. arena->New<StringTypeLiteral>(source_loc))))};
  22. auto print_return = arena->New<Return>(
  23. source_loc,
  24. arena->New<IntrinsicExpression>(IntrinsicExpression::Intrinsic::Print),
  25. false);
  26. auto print = arena->New<FunctionDeclaration>(arena->New<FunctionDefinition>(
  27. source_loc, "Print", std::vector<GenericBinding>(),
  28. arena->New<TuplePattern>(source_loc, print_fields),
  29. arena->New<ExpressionPattern>(arena->New<TupleLiteral>(source_loc)),
  30. /*is_omitted_return_type=*/false, print_return));
  31. declarations->insert(declarations->begin(), print);
  32. }
  33. void ExecProgram(Nonnull<Arena*> arena, AST ast) {
  34. AddIntrinsics(arena, &ast.declarations);
  35. if (tracing_output) {
  36. llvm::outs() << "********** source program **********\n";
  37. for (const auto decl : ast.declarations) {
  38. llvm::outs() << *decl;
  39. }
  40. llvm::outs() << "********** type checking **********\n";
  41. }
  42. TypeChecker type_checker(arena);
  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 (tracing_output) {
  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 = Interpreter(arena).InterpProgram(ast.declarations, call_main);
  62. llvm::outs() << "result: " << result << "\n";
  63. }
  64. } // namespace Carbon