interpreter.h 5.4 KB

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