interpreter.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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/ast.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/action.h"
  15. #include "executable_semantics/interpreter/action_stack.h"
  16. #include "executable_semantics/interpreter/heap.h"
  17. #include "executable_semantics/interpreter/value.h"
  18. #include "llvm/ADT/ArrayRef.h"
  19. namespace Carbon {
  20. class Interpreter {
  21. public:
  22. explicit Interpreter(Nonnull<Arena*> arena, bool trace)
  23. : arena_(arena), heap_(arena), trace_(trace) {}
  24. // Interpret the whole program.
  25. auto InterpProgram(const AST& ast) -> int;
  26. // Interpret an expression at compile-time.
  27. auto InterpExp(Nonnull<const Expression*> e) -> Nonnull<const Value*>;
  28. // Interpret a pattern at compile-time.
  29. auto InterpPattern(Nonnull<const Pattern*> p) -> Nonnull<const Value*>;
  30. // Attempts to match `v` against the pattern `p`, returning whether matching
  31. // is successful. If it is, populates **bindings with the variables bound by
  32. // the match; `bindings` should only be nullopt in contexts where `p`
  33. // is not permitted to bind variables. **bindings may be modified even if the
  34. // match is unsuccessful, so it should typically be created for the
  35. // PatternMatch call and then merged into an existing scope on success.
  36. [[nodiscard]] auto PatternMatch(
  37. Nonnull<const Value*> p, Nonnull<const Value*> v,
  38. SourceLocation source_loc, std::optional<Nonnull<RuntimeScope*>> bindings)
  39. -> bool;
  40. // Support TypeChecker allocating values on the heap.
  41. auto AllocateValue(Nonnull<const Value*> v) -> AllocationId {
  42. return heap_.AllocateValue(v);
  43. }
  44. private:
  45. void Step();
  46. // State transitions for expressions.
  47. void StepExp();
  48. // State transitions for lvalues.
  49. void StepLvalue();
  50. // State transitions for patterns.
  51. void StepPattern();
  52. // State transition for statements.
  53. void StepStmt();
  54. // State transition for declarations.
  55. void StepDeclaration();
  56. // Calls Step() repeatedly until there are no steps left to execute. Produces
  57. // trace output if trace_steps is true.
  58. void RunAllSteps(bool trace_steps);
  59. auto CreateStruct(const std::vector<FieldInitializer>& fields,
  60. const std::vector<Nonnull<const Value*>>& values)
  61. -> Nonnull<const Value*>;
  62. auto EvalPrim(Operator op, const std::vector<Nonnull<const Value*>>& args,
  63. SourceLocation source_loc) -> Nonnull<const Value*>;
  64. // Returns the result of converting `value` to type `destination_type`.
  65. auto Convert(Nonnull<const Value*> value,
  66. Nonnull<const Value*> destination_type) const
  67. -> Nonnull<const Value*>;
  68. void PrintState(llvm::raw_ostream& out);
  69. // Runs `action` in an environment where the given constants are defined, and
  70. // returns the result. `action` must produce a result. In other words, it must
  71. // not be a StatementAction, ScopeAction, or DeclarationAction. Can only be
  72. // called at compile time (before InterpProgram), and while `todo_` is empty.
  73. auto RunCompileTimeAction(std::unique_ptr<Action> action)
  74. -> Nonnull<const Value*>;
  75. Nonnull<Arena*> arena_;
  76. Heap heap_;
  77. ActionStack todo_;
  78. // The underlying states of continuation values. All StackFragments created
  79. // during execution are tracked here, in order to safely deallocate the
  80. // contents of any non-completed continuations at the end of execution.
  81. std::vector<Nonnull<ContinuationValue::StackFragment*>> stack_fragments_;
  82. bool trace_;
  83. };
  84. } // namespace Carbon
  85. #endif // EXECUTABLE_SEMANTICS_INTERPRETER_INTERPRETER_H_