interpreter.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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/action.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. class Interpreter {
  20. public:
  21. explicit Interpreter(Nonnull<Arena*> arena, bool trace)
  22. : arena_(arena), globals_(arena), heap_(arena), trace_(trace) {}
  23. // Interpret the whole program.
  24. auto InterpProgram(llvm::ArrayRef<Nonnull<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) -> AllocationId {
  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` stack until it reaches the
  70. // StatementAction associated with `ast_node`. Execution then resumes with
  71. // that StatementAction.
  72. struct UnwindTo {
  73. Nonnull<const Statement*> ast_node;
  74. };
  75. // Transition type which unwinds the `todo` stack down to and including the
  76. // StatementAction associated with `ast_node`. If `result` is set, it will be
  77. // treated as the result of that StatementAction.
  78. struct UnwindPast {
  79. Nonnull<const Statement*> ast_node;
  80. std::optional<Nonnull<const Value*>> result;
  81. };
  82. // Transition type which removes the current action from the top of the todo
  83. // stack, then creates a new stack frame which calls the specified function
  84. // with the specified arguments.
  85. struct CallFunction {
  86. Nonnull<const FunctionDeclaration*> function;
  87. Nonnull<const Value*> args;
  88. SourceLocation source_loc;
  89. };
  90. // Transition type which does nothing.
  91. //
  92. // TODO(geoffromer): This is a temporary placeholder during refactoring. All
  93. // uses of this type should be replaced with meaningful transitions.
  94. struct ManualTransition {};
  95. using Transition = std::variant<Done, Spawn, Delegate, RunAgain, UnwindTo,
  96. UnwindPast, CallFunction, ManualTransition>;
  97. // Visitor which implements the behavior associated with each transition type.
  98. class DoTransition;
  99. friend class DoTransition;
  100. void Step();
  101. // State transitions for expressions.
  102. auto StepExp() -> Transition;
  103. // State transitions for lvalues.
  104. auto StepLvalue() -> Transition;
  105. // State transitions for patterns.
  106. auto StepPattern() -> Transition;
  107. // State transition for statements.
  108. auto StepStmt() -> Transition;
  109. void InitGlobals(llvm::ArrayRef<Nonnull<Declaration*>> fs);
  110. auto CurrentScope() -> Scope&;
  111. auto CurrentEnv() -> Env;
  112. auto GetFromEnv(SourceLocation source_loc, const std::string& name)
  113. -> Address;
  114. auto UnwindTodoTop() -> Nonnull<Action*>;
  115. auto CreateTuple(Nonnull<Action*> act, Nonnull<const Expression*> exp)
  116. -> Nonnull<const Value*>;
  117. auto CreateStruct(const std::vector<FieldInitializer>& fields,
  118. const std::vector<Nonnull<const Value*>>& values)
  119. -> Nonnull<const Value*>;
  120. auto EvalPrim(Operator op, const std::vector<Nonnull<const Value*>>& args,
  121. SourceLocation source_loc) -> Nonnull<const Value*>;
  122. void PatternAssignment(Nonnull<const Value*> pat, Nonnull<const Value*> val,
  123. SourceLocation source_loc);
  124. // Returns the result of converting `value` to type `destination_type`.
  125. auto Convert(Nonnull<const Value*> value,
  126. Nonnull<const Value*> destination_type) const
  127. -> Nonnull<const Value*>;
  128. void PrintState(llvm::raw_ostream& out);
  129. // Runs `action` in a scope consisting of `values`, and returns the result.
  130. // `action` must produce a result. In other words, it must not be a
  131. // StatementAction or ScopeAction.
  132. //
  133. // TODO: consider whether to use this->trace_ rather than a separate
  134. // trace_steps parameter.
  135. auto ExecuteAction(Nonnull<Action*> action, Env values, bool trace_steps)
  136. -> Nonnull<const Value*>;
  137. Nonnull<Arena*> arena_;
  138. // Globally-defined entities, such as functions, structs, or choices.
  139. Env globals_;
  140. Stack<Nonnull<Action*>> todo_;
  141. Heap heap_;
  142. bool trace_;
  143. };
  144. } // namespace Carbon
  145. #endif // EXECUTABLE_SEMANTICS_INTERPRETER_INTERPRETER_H_