interpreter.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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),
  24. heap_(arena),
  25. todo_(Scope(Env(arena_), &heap_)),
  26. trace_(trace) {}
  27. // Interpret the whole program.
  28. auto InterpProgram(const AST& ast) -> int;
  29. // Interpret an expression at compile-time.
  30. auto InterpExp(Nonnull<const Expression*> e) -> Nonnull<const Value*>;
  31. // Interpret a pattern at compile-time.
  32. auto InterpPattern(Nonnull<const Pattern*> p) -> 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) -> AllocationId {
  39. return heap_.AllocateValue(v);
  40. }
  41. void PrintEnv(Env values, llvm::raw_ostream& out);
  42. private:
  43. void Step();
  44. // State transitions for expressions.
  45. void StepExp();
  46. // State transitions for lvalues.
  47. void StepLvalue();
  48. // State transitions for patterns.
  49. void StepPattern();
  50. // State transition for statements.
  51. void StepStmt();
  52. // State transition for declarations.
  53. void StepDeclaration();
  54. auto CurrentEnv() -> Env;
  55. auto GetFromEnv(SourceLocation source_loc, const std::string& name)
  56. -> Address;
  57. // Calls Step() repeatedly until there are no steps left to execute. Produces
  58. // trace output if trace_steps is true.
  59. void RunAllSteps(bool trace_steps);
  60. auto CreateStruct(const std::vector<FieldInitializer>& fields,
  61. const std::vector<Nonnull<const Value*>>& values)
  62. -> Nonnull<const Value*>;
  63. auto EvalPrim(Operator op, const std::vector<Nonnull<const Value*>>& args,
  64. SourceLocation source_loc) -> Nonnull<const Value*>;
  65. // Returns the result of converting `value` to type `destination_type`.
  66. auto Convert(Nonnull<const Value*> value,
  67. Nonnull<const Value*> destination_type) const
  68. -> Nonnull<const Value*>;
  69. void PrintState(llvm::raw_ostream& out);
  70. // Runs `action` in an environment where the given constants are defined, and
  71. // returns the result. `action` must produce a result. In other words, it must
  72. // not be a StatementAction, ScopeAction, or DeclarationAction. Can only be
  73. // called at compile time (before InterpProgram), and while `todo_` is empty.
  74. auto RunCompileTimeAction(std::unique_ptr<Action> action)
  75. -> Nonnull<const Value*>;
  76. Nonnull<Arena*> arena_;
  77. Heap heap_;
  78. ActionStack todo_;
  79. // The underlying states of continuation values. All StackFragments created
  80. // during execution are tracked here, in order to safely deallocate the
  81. // contents of any non-completed continuations at the end of execution.
  82. std::vector<Nonnull<ContinuationValue::StackFragment*>> stack_fragments_;
  83. bool trace_;
  84. };
  85. } // namespace Carbon
  86. #endif // EXECUTABLE_SEMANTICS_INTERPRETER_INTERPRETER_H_