interpreter.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. #ifndef EXECUTABLE_SEMANTICS_INTERPRETER_INTERPRETER_H_
  5. #define EXECUTABLE_SEMANTICS_INTERPRETER_INTERPRETER_H_
  6. #include <optional>
  7. #include <utility>
  8. #include <vector>
  9. #include "common/ostream.h"
  10. #include "executable_semantics/ast/declaration.h"
  11. #include "executable_semantics/ast/expression.h"
  12. #include "executable_semantics/ast/pattern.h"
  13. #include "executable_semantics/interpreter/frame.h"
  14. #include "executable_semantics/interpreter/heap.h"
  15. #include "executable_semantics/interpreter/stack.h"
  16. #include "executable_semantics/interpreter/value.h"
  17. namespace Carbon {
  18. using Env = Dictionary<std::string, Address>;
  19. class Interpreter {
  20. public:
  21. explicit Interpreter(Ptr<Arena> arena)
  22. : arena(arena), globals(arena), heap(arena) {}
  23. // Interpret the whole program.
  24. auto InterpProgram(const std::vector<Ptr<const Declaration>>& fs) -> int;
  25. // Interpret an expression at compile-time.
  26. auto InterpExp(Env values, Ptr<const Expression> e) -> Ptr<const Value>;
  27. // Interpret a pattern at compile-time.
  28. auto InterpPattern(Env values, Ptr<const Pattern> p) -> Ptr<const Value>;
  29. // Attempts to match `v` against the pattern `p`. If matching succeeds,
  30. // returns the bindings of pattern variables to their matched values.
  31. auto PatternMatch(Ptr<const Value> p, Ptr<const Value> v, SourceLocation loc)
  32. -> std::optional<Env>;
  33. // Support TypeChecker allocating values on the heap.
  34. auto AllocateValue(Ptr<const Value> v) -> Address {
  35. return heap.AllocateValue(v);
  36. }
  37. void InitEnv(const Declaration& d, Env* env);
  38. void PrintEnv(Env values, llvm::raw_ostream& out);
  39. private:
  40. // State transition functions
  41. //
  42. // The `Step*` family of functions implement state transitions in the
  43. // interpreter by executing a step of the Action at the top of the todo stack,
  44. // and then returning a Transition that specifies how `state.stack` should be
  45. // updated. `Transition` is a variant of several "transition types"
  46. // representing the different kinds of state transition.
  47. // Transition type which indicates that the current Action is now done.
  48. struct Done {
  49. // The value computed by the Action. Should always be nullopt for Statement
  50. // Actions, and never null for any other kind of Action.
  51. std::optional<Ptr<const Value>> result;
  52. };
  53. // Transition type which spawns a new Action on the todo stack above the
  54. // current Action, and increments the current Action's position counter.
  55. struct Spawn {
  56. Ptr<Action> child;
  57. };
  58. // Transition type which spawns a new Action that replaces the current action
  59. // on the todo stack.
  60. struct Delegate {
  61. Ptr<Action> delegate;
  62. };
  63. // Transition type which keeps the current Action at the top of the stack,
  64. // and increments its position counter.
  65. struct RunAgain {};
  66. // Transition type which unwinds the `todo` and `scopes` stacks until it
  67. // reaches a specified Action lower in the stack.
  68. struct UnwindTo {
  69. const Ptr<Action> new_top;
  70. };
  71. // Transition type which unwinds the entire current stack frame, and returns
  72. // a specified value to the caller.
  73. struct UnwindFunctionCall {
  74. Ptr<const Value> return_val;
  75. };
  76. // Transition type which removes the current action from the top of the todo
  77. // stack, then creates a new stack frame which calls the specified function
  78. // with the specified arguments.
  79. struct CallFunction {
  80. Ptr<const FunctionValue> function;
  81. Ptr<const Value> args;
  82. SourceLocation loc;
  83. };
  84. // Transition type which does nothing.
  85. //
  86. // TODO(geoffromer): This is a temporary placeholder during refactoring. All
  87. // uses of this type should be replaced with meaningful transitions.
  88. struct ManualTransition {};
  89. using Transition =
  90. std::variant<Done, Spawn, Delegate, RunAgain, UnwindTo,
  91. UnwindFunctionCall, CallFunction, ManualTransition>;
  92. // Visitor which implements the behavior associated with each transition type.
  93. class DoTransition;
  94. void Step();
  95. // State transitions for expressions.
  96. auto StepExp() -> Transition;
  97. // State transitions for lvalues.
  98. auto StepLvalue() -> Transition;
  99. // State transitions for patterns.
  100. auto StepPattern() -> Transition;
  101. // State transition for statements.
  102. auto StepStmt() -> Transition;
  103. void InitGlobals(const std::vector<Ptr<const Declaration>>& fs);
  104. auto CurrentEnv() -> Env;
  105. auto GetFromEnv(SourceLocation loc, const std::string& name) -> Address;
  106. void DeallocateScope(Ptr<Scope> scope);
  107. void DeallocateLocals(Ptr<Frame> frame);
  108. auto CreateTuple(Ptr<Action> act, Ptr<const Expression> exp)
  109. -> Ptr<const Value>;
  110. auto EvalPrim(Operator op, const std::vector<Ptr<const Value>>& args,
  111. SourceLocation loc) -> Ptr<const Value>;
  112. void PatternAssignment(Ptr<const Value> pat, Ptr<const Value> val,
  113. SourceLocation loc);
  114. void PrintState(llvm::raw_ostream& out);
  115. Ptr<Arena> arena;
  116. // Globally-defined entities, such as functions, structs, or choices.
  117. Env globals;
  118. Stack<Ptr<Frame>> stack;
  119. Heap heap;
  120. std::optional<Ptr<const Value>> program_value;
  121. };
  122. } // namespace Carbon
  123. #endif // EXECUTABLE_SEMANTICS_INTERPRETER_INTERPRETER_H_