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/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/action_stack.h"
  15. #include "executable_semantics/interpreter/heap.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. 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. void InitGlobals(llvm::ArrayRef<Nonnull<Declaration*>> fs);
  53. auto CurrentEnv() -> Env;
  54. auto GetFromEnv(SourceLocation source_loc, const std::string& name)
  55. -> Address;
  56. auto CreateStruct(const std::vector<FieldInitializer>& fields,
  57. const std::vector<Nonnull<const Value*>>& values)
  58. -> Nonnull<const Value*>;
  59. auto EvalPrim(Operator op, const std::vector<Nonnull<const Value*>>& args,
  60. SourceLocation source_loc) -> Nonnull<const Value*>;
  61. // Returns the result of converting `value` to type `destination_type`.
  62. auto Convert(Nonnull<const Value*> value,
  63. Nonnull<const Value*> destination_type) const
  64. -> Nonnull<const Value*>;
  65. void PrintState(llvm::raw_ostream& out);
  66. // Runs `action` in a scope consisting of `values`, and returns the result.
  67. // `action` must produce a result. In other words, it must not be a
  68. // StatementAction or ScopeAction.
  69. //
  70. // TODO: consider whether to use this->trace_ rather than a separate
  71. // trace_steps parameter.
  72. auto ExecuteAction(std::unique_ptr<Action> action, Env values,
  73. bool trace_steps) -> Nonnull<const Value*>;
  74. Nonnull<Arena*> arena_;
  75. // Globally-defined entities, such as functions, structs, or choices.
  76. Env globals_;
  77. ActionStack todo_;
  78. Heap heap_;
  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_