interpreter.h 4.9 KB

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