interpreter.h 5.7 KB

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