interpreter.h 5.7 KB

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