interpreter.h 4.9 KB

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